summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-04-03 11:42:41 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-04-04 21:28:36 +0200
commit6eccc3cfa9dcfea3c8b508a66d2d592e6b9fcb93 (patch)
treece27b8622a46099d99bed5eff6cb0514de2a6348 /src/test
parentmeson: redo grouping of tests under src/test/ (diff)
downloadsystemd-6eccc3cfa9dcfea3c8b508a66d2d592e6b9fcb93.tar.xz
systemd-6eccc3cfa9dcfea3c8b508a66d2d592e6b9fcb93.zip
test-core-unit: add new test file for unit_escape_setting() and friends
None of the existing test files fit very well. test-unit-serialize is pretty close, but it does special cgroup setup, which we don't need in this case. I hope we can add more tests in the future for this basic functionality, so I'm adding a brand new file names after the source file it's testing.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/meson.build5
-rw-r--r--src/test/test-core-unit.c100
2 files changed, 105 insertions, 0 deletions
diff --git a/src/test/meson.build b/src/test/meson.build
index 0310212700..640f49596f 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -475,6 +475,11 @@ tests += [
'base' : test_core_base,
},
{
+ 'sources' : files('test-core-unit.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
'sources' : files('test-emergency-action.c'),
'base' : test_core_base,
},
diff --git a/src/test/test-core-unit.c b/src/test/test-core-unit.c
new file mode 100644
index 0000000000..6593f2fb4b
--- /dev/null
+++ b/src/test/test-core-unit.c
@@ -0,0 +1,100 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "alloc-util.h"
+#include "escape.h"
+#include "tests.h"
+#include "unit.h"
+
+static void test_unit_escape_setting_one(
+ const char *s,
+ const char *expected_exec,
+ const char *expected_c) {
+
+ _cleanup_free_ char *a = NULL, *b, *c,
+ *s_esc, *a_esc, *b_esc, *c_esc;
+ const char *t;
+
+ if (!expected_exec)
+ expected_exec = s;
+ if (!expected_c)
+ expected_c = expected_exec;
+ assert_se(s_esc = cescape(s));
+
+ assert_se(t = unit_escape_setting(s, 0, &a));
+ assert_se(a_esc = cescape(t));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, a_esc);
+ assert_se(a == NULL);
+ assert_se(t == s);
+
+ assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_EXEC_SYNTAX, &b));
+ assert_se(b_esc = cescape(t));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, b_esc);
+ assert_se(b == NULL || streq(b, t));
+ assert_se(streq(t, expected_exec));
+
+ assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_C, &c));
+ assert_se(c_esc = cescape(t));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, c_esc);
+ assert_se(c == NULL || streq(c, t));
+ assert_se(streq(t, expected_c));
+}
+
+TEST(unit_escape_setting) {
+ test_unit_escape_setting_one("/sbin/sbash", NULL, NULL);
+ test_unit_escape_setting_one("$", "\\$", "$");
+ test_unit_escape_setting_one("$$", "\\$\\$", "$$");
+ test_unit_escape_setting_one("'", "\\'", NULL);
+ test_unit_escape_setting_one("\"", "\\\"", NULL);
+ test_unit_escape_setting_one("\t", "\\t", NULL);
+ test_unit_escape_setting_one(" ", NULL, NULL);
+ test_unit_escape_setting_one("$;'\"\t\n", "\\$\\;\\'\\\"\\t\\n", "$;\\'\\\"\\t\\n");
+}
+
+static void test_unit_concat_strv_one(
+ char **s,
+ const char *expected_none,
+ const char *expected_exec,
+ const char *expected_c) {
+
+ _cleanup_free_ char *a, *b, *c,
+ *s_ser, *s_esc, *a_esc, *b_esc, *c_esc;
+
+ assert_se(s_ser = strv_join(s, "_"));
+ assert_se(s_esc = cescape(s_ser));
+ if (!expected_exec)
+ expected_exec = expected_none;
+ if (!expected_c)
+ expected_c = expected_none;
+
+ assert_se(a = unit_concat_strv(s, 0));
+ assert_se(a_esc = cescape(a));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, a_esc);
+ assert_se(streq(a, expected_none));
+
+ assert_se(b = unit_concat_strv(s, UNIT_ESCAPE_EXEC_SYNTAX));
+ assert_se(b_esc = cescape(b));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, b_esc);
+ assert_se(streq(b, expected_exec));
+
+ assert_se(c = unit_concat_strv(s, UNIT_ESCAPE_C));
+ assert_se(c_esc = cescape(c));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, c_esc);
+ assert_se(streq(c, expected_c));
+}
+
+TEST(unit_concat_strv) {
+ test_unit_concat_strv_one(STRV_MAKE("a", "b", "c"),
+ "\"a\" \"b\" \"c\"",
+ NULL,
+ NULL);
+ test_unit_concat_strv_one(STRV_MAKE("a", " ", "$", "$$", ""),
+ "\"a\" \" \" \"$\" \"$$\" \"\"",
+ "\"a\" \" \" \"\\$\" \"\\$\\$\" \"\"",
+ NULL);
+ test_unit_concat_strv_one(STRV_MAKE("\n", " ", "\t"),
+ "\"\n\" \" \" \"\t\"",
+ "\"\\n\" \" \" \"\\t\"",
+ "\"\\n\" \" \" \"\\t\"");
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);