summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-04-24 22:03:06 +0200
committerGitHub <noreply@github.com>2023-04-24 22:03:06 +0200
commit208a59c15fd41f87248a1a981e558acd3de5e47b (patch)
tree45f73492212a2c220305ab41bb3c74cd6265efeb /src/test
parentparse-util: allow parse_pid() to work with NULL return parameter (diff)
parentrun: expand variables also with --scope (diff)
downloadsystemd-208a59c15fd41f87248a1a981e558acd3de5e47b.tar.xz
systemd-208a59c15fd41f87248a1a981e558acd3de5e47b.zip
Merge pull request #27113 from keszybz/variable-expansion-rework
Rework serialization of command lines in pid1 and make run not expand variables
Diffstat (limited to 'src/test')
-rw-r--r--src/test/meson.build247
-rw-r--r--src/test/test-core-unit.c120
-rw-r--r--src/test/test-logarithm.c21
3 files changed, 266 insertions, 122 deletions
diff --git a/src/test/meson.build b/src/test/meson.build
index da843a7e47..289967760e 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -219,25 +219,6 @@ tests += [
'condition' : 'ENABLE_EFI',
},
{
- 'sources' : files('test-bpf-devices.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-bpf-firewall.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-bpf-foreign-programs.c'),
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-bpf-lsm.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
'sources' : files('test-btrfs.c'),
'type' : 'manual',
},
@@ -253,27 +234,10 @@ tests += [
'dependencies' : libcap,
},
{
- 'sources' : files('test-cgroup-cpu.c'),
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-cgroup-mask.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-cgroup-unit-default.c'),
- 'base' : test_core_base,
- },
- {
'sources' : files('test-chase-manual.c'),
'type' : 'manual',
},
{
- 'sources' : files('test-chown-rec.c'),
- 'base' : test_core_base,
- },
- {
'sources' : files('test-compress-benchmark.c'),
'link_with' : [
libbasic_compress,
@@ -299,27 +263,12 @@ tests += [
'dependencies' : libp11kit_cflags
},
{
- 'sources' : files('test-emergency-action.c'),
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-engine.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
'sources' : [
files('test-errno-list.c'),
generated_gperf_headers,
],
},
{
- 'sources' : files('test-execute.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- 'timeout' : 360,
- },
- {
'sources' : files('test-fd-util.c'),
'dependencies' : libseccomp,
},
@@ -332,11 +281,6 @@ tests += [
'timeout' : 180,
},
{
- 'sources' : files('test-install.c'),
- 'base' : test_core_base,
- 'type' : 'manual',
- },
- {
'sources' : [
files('test-ip-protocol-list.c'),
shared_generated_gperf_headers,
@@ -347,11 +291,6 @@ tests += [
'type' : 'unsafe',
},
{
- 'sources' : files('test-job-type.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
'sources' : files('test-json.c'),
'dependencies' : libm,
},
@@ -368,25 +307,10 @@ tests += [
],
},
{
- 'sources' : files('test-load-fragment.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
- 'sources' : files('test-loop-block.c'),
- 'dependencies' : [threads, libblkid],
- 'base' : test_core_base,
- 'parallel' : false,
- },
- {
'sources' : files('test-loopback.c'),
'dependencies' : common_test_dependencies,
},
{
- 'sources' : files('test-manager.c'),
- 'base' : test_core_base,
- },
- {
'sources' : files('test-math-util.c'),
'dependencies' : libm,
},
@@ -395,26 +319,12 @@ tests += [
'dependencies' : threads,
},
{
- 'sources' : files('test-namespace.c'),
- 'dependencies' : [
- libblkid,
- threads,
- ],
- 'base' : test_core_base,
- },
- {
'sources' : files('test-netlink-manual.c'),
'dependencies' : libkmod,
'condition' : 'HAVE_KMOD',
'type' : 'manual',
},
{
- 'sources' : files('test-ns.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- 'type' : 'manual',
- },
- {
'sources' : files('test-nscd-flush.c'),
'condition' : 'ENABLE_NSCD',
'type' : 'manual',
@@ -441,12 +351,6 @@ tests += [
'dependencies' : libm,
},
{
- 'sources' : files('test-path.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- 'timeout' : 120,
- },
- {
'sources' : files('test-process-util.c'),
'dependencies' : threads,
},
@@ -465,11 +369,6 @@ tests += [
'c_args' : '-I@0@'.format(efi_config_h_dir),
},
{
- 'sources' : files('test-sched-prio.c'),
- 'dependencies' : common_test_dependencies,
- 'base' : test_core_base,
- },
- {
'sources' : files('test-seccomp.c'),
'dependencies' : libseccomp,
'condition' : 'HAVE_SECCOMP',
@@ -527,39 +426,149 @@ tests += [
'type' : 'manual',
},
{
- 'sources' : files('test-unit-name.c'),
+ 'sources' : files('test-utmp.c'),
+ 'condition' : 'ENABLE_UTMP',
+ },
+ {
+ 'sources' : files('test-varlink.c'),
+ 'dependencies' : threads,
+ },
+ {
+ 'sources' : files('test-watchdog.c'),
+ 'type' : 'unsafe',
+ },
+
+
+ # Tests that link to libcore, i.e. tests for pid1 code.
+ {
+ 'sources' : files('test-bpf-devices.c'),
'dependencies' : common_test_dependencies,
'base' : test_core_base,
},
{
- 'sources' : files('test-unit-serialize.c'),
+ 'sources' : files('test-bpf-firewall.c'),
'dependencies' : common_test_dependencies,
'base' : test_core_base,
},
{
- 'sources' : files('test-utmp.c'),
- 'condition' : 'ENABLE_UTMP',
+ 'sources' : files('test-bpf-foreign-programs.c'),
+ 'base' : test_core_base,
},
{
- 'sources' : files('test-varlink.c'),
- 'dependencies' : threads,
+ 'sources' : files('test-bpf-lsm.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
},
{
- 'sources' : files('test-watch-pid.c'),
+ 'sources' : files('test-cgroup-cpu.c'),
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-cgroup-mask.c'),
'dependencies' : common_test_dependencies,
'base' : test_core_base,
},
{
- 'sources' : files('test-watchdog.c'),
- 'type' : 'unsafe',
+ 'sources' : files('test-cgroup-unit-default.c'),
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-chown-rec.c'),
+ '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,
+ },
+ {
+ 'sources' : files('test-engine.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-execute.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ 'timeout' : 360,
+ },
+ {
+ 'sources' : files('test-install.c'),
+ 'base' : test_core_base,
+ 'type' : 'manual',
+ },
+ {
+ 'sources' : files('test-job-type.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-load-fragment.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-loop-block.c'),
+ 'dependencies' : [threads, libblkid],
+ 'base' : test_core_base,
+ 'parallel' : false,
+ },
+ {
+ 'sources' : files('test-manager.c'),
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-namespace.c'),
+ 'dependencies' : [
+ libblkid,
+ threads,
+ ],
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-ns.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ 'type' : 'manual',
+ },
+ {
+ 'sources' : files('test-path.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ 'timeout' : 120,
+ },
+ {
+ 'sources' : files('test-sched-prio.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-socket-bind.c'),
+ 'dependencies' : libdl,
+ 'condition' : 'BPF_FRAMEWORK',
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-unit-name.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-unit-serialize.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
+ },
+ {
+ 'sources' : files('test-watch-pid.c'),
+ 'dependencies' : common_test_dependencies,
+ 'base' : test_core_base,
},
-]
-
-############################################################
-
-# define some tests here, because the link_with deps were not defined earlier
-tests += [
+ # Tests from other directories that have link_with deps that were not defined earlier
{
'sources' : files('../libsystemd/sd-bus/test-bus-error.c'),
'link_with' : [
@@ -577,10 +586,4 @@ tests += [
'link_with' : libudev,
'dependencies' : threads,
},
- {
- 'sources' : files('test-socket-bind.c'),
- 'dependencies' : libdl,
- 'condition' : 'BPF_FRAMEWORK',
- '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..1a08507d1d
--- /dev/null
+++ b/src/test/test-core-unit.c
@@ -0,0 +1,120 @@
+/* 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_env,
+ const char *expected_exec,
+ const char *expected_c) {
+
+ _cleanup_free_ char *a = NULL, *b, *c, *d,
+ *s_esc, *a_esc, *b_esc, *c_esc, *d_esc;
+ const char *t;
+
+ if (!expected_exec_env)
+ expected_exec_env = s;
+ if (!expected_exec)
+ expected_exec = expected_exec_env;
+ 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_ENV, &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_env));
+
+ assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_EXEC_SYNTAX, &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_exec));
+
+ assert_se(t = unit_escape_setting(s, UNIT_ESCAPE_C, &d));
+ assert_se(d_esc = cescape(t));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, d_esc);
+ assert_se(d == NULL || streq(d, t));
+ assert_se(streq(t, expected_c));
+}
+
+TEST(unit_escape_setting) {
+ test_unit_escape_setting_one("/sbin/sbash", NULL, NULL, NULL);
+ test_unit_escape_setting_one("$", "$$", "$", "$");
+ test_unit_escape_setting_one("$$", "$$$$", "$$", "$$");
+ test_unit_escape_setting_one("'", "'", NULL, "\\'");
+ test_unit_escape_setting_one("\"", "\\\"", NULL, NULL);
+ test_unit_escape_setting_one("\t", "\\t", NULL, NULL);
+ test_unit_escape_setting_one(" ", NULL, NULL, NULL);
+ test_unit_escape_setting_one("$;'\"\t\n", "$$;'\\\"\\t\\n", "$;'\\\"\\t\\n", "$;\\'\\\"\\t\\n");
+}
+
+static void test_unit_concat_strv_one(
+ char **s,
+ const char *expected_none,
+ const char *expected_exec_env,
+ const char *expected_exec,
+ const char *expected_c) {
+
+ _cleanup_free_ char *a, *b, *c, *d,
+ *s_ser, *s_esc, *a_esc, *b_esc, *c_esc, *d_esc;
+
+ assert_se(s_ser = strv_join(s, "_"));
+ assert_se(s_esc = cescape(s_ser));
+ if (!expected_exec_env)
+ expected_exec_env = expected_none;
+ 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_ENV));
+ assert_se(b_esc = cescape(b));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, b_esc);
+ assert_se(streq(b, expected_exec_env));
+
+ assert_se(c = unit_concat_strv(s, UNIT_ESCAPE_EXEC_SYNTAX));
+ assert_se(c_esc = cescape(c));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, c_esc);
+ assert_se(streq(c, expected_exec));
+
+ assert_se(d = unit_concat_strv(s, UNIT_ESCAPE_C));
+ assert_se(d_esc = cescape(d));
+ log_debug("%s: [%s] → [%s]", __func__, s_esc, d_esc);
+ assert_se(streq(d, expected_c));
+}
+
+TEST(unit_concat_strv) {
+ test_unit_concat_strv_one(STRV_MAKE("a", "b", "c"),
+ "\"a\" \"b\" \"c\"",
+ NULL,
+ NULL,
+ NULL);
+ test_unit_concat_strv_one(STRV_MAKE("a", " ", "$", "$$", ""),
+ "\"a\" \" \" \"$\" \"$$\" \"\"",
+ "\"a\" \" \" \"$$\" \"$$$$\" \"\"",
+ NULL,
+ NULL);
+ test_unit_concat_strv_one(STRV_MAKE("\n", " ", "\t"),
+ "\"\n\" \" \" \"\t\"",
+ "\"\\n\" \" \" \"\\t\"",
+ "\"\\n\" \" \" \"\\t\"",
+ "\"\\n\" \" \" \"\\t\"");
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);
diff --git a/src/test/test-logarithm.c b/src/test/test-logarithm.c
index b6818b422c..b35fea9c27 100644
--- a/src/test/test-logarithm.c
+++ b/src/test/test-logarithm.c
@@ -71,4 +71,25 @@ TEST(log2i) {
assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
}
+TEST(popcount) {
+ uint16_t u16a = 0x0000;
+ uint16_t u16b = 0xFFFF;
+ uint32_t u32a = 0x00000010;
+ uint32_t u32b = 0xFFFFFFFF;
+ uint64_t u64a = 0x0000000000000010;
+ uint64_t u64b = 0x0100000000100010;
+
+ assert_se(popcount(u16a) == 0);
+ assert_se(popcount(u16b) == 16);
+ assert_se(popcount(u32a) == 1);
+ assert_se(popcount(u32b) == 32);
+ assert_se(popcount(u64a) == 1);
+ assert_se(popcount(u64b) == 3);
+
+ /* This would fail:
+ * error: ‘_Generic’ selector of type ‘int’ is not compatible with any association
+ * assert_se(popcount(0x10) == 1);
+ */
+}
+
DEFINE_TEST_MAIN(LOG_INFO);