diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-10-26 12:30:04 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-10-26 13:17:49 +0200 |
commit | 1c1d719aecdb67d2d8098befa884d45fa76cb84e (patch) | |
tree | d5440bca097668368198c505d3cbd989b1699f04 /src/test/test-serialize.c | |
parent | fa2951def756e396c92cf18f5aea70cdad311da5 (diff) | |
download | systemd-1c1d719aecdb67d2d8098befa884d45fa76cb84e.tar.gz systemd-1c1d719aecdb67d2d8098befa884d45fa76cb84e.tar.bz2 systemd-1c1d719aecdb67d2d8098befa884d45fa76cb84e.zip |
Move tests for *serialize_environment into test-serialize.c
The implementation is in serialize.c.
Diffstat (limited to 'src/test/test-serialize.c')
-rw-r--r-- | src/test/test-serialize.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/test/test-serialize.c b/src/test/test-serialize.c index e38b48fdbc..b70eb3daa7 100644 --- a/src/test/test-serialize.c +++ b/src/test/test-serialize.c @@ -133,6 +133,65 @@ static void test_serialize_strv(void) { assert_se(strv_equal(strv, strv2)); } +static void test_deserialize_environment(void) { + _cleanup_strv_free_ char **env; + + log_info("/* %s */", __func__); + + assert_se(env = strv_new("A=1", NULL)); + + assert_se(deserialize_environment("B=2", &env) >= 0); + assert_se(deserialize_environment("FOO%%=a\\177b\\nc\\td e", &env) >= 0); + + assert_se(strv_equal(env, STRV_MAKE("A=1", "B=2", "FOO%%=a\177b\nc\td e"))); + + assert_se(deserialize_environment("foo\\", &env) < 0); + assert_se(deserialize_environment("bar\\_baz", &env) < 0); +} + +static void test_serialize_environment(void) { + _cleanup_strv_free_ char **env = NULL, **env2 = NULL; + _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-env-util.XXXXXXX"; + _cleanup_fclose_ FILE *f = NULL; + int r; + + assert_se(fmkostemp_safe(fn, "r+", &f) == 0); + log_info("/* %s (%s) */", __func__, fn); + + assert_se(env = strv_new("A=1", + "B=2", + "C=ąęółń", + "D=D=a\\x0Ab", + "FOO%%=a\177b\nc\td e", + NULL)); + + assert_se(serialize_strv(f, "env", env) == 1); + assert_se(fflush_and_check(f) == 0); + + rewind(f); + + for (;;) { + _cleanup_free_ char *line = NULL; + const char *l; + + r = read_line(f, LONG_LINE_MAX, &line); + assert_se(r >= 0); + + if (r == 0) + break; + + l = strstrip(line); + + assert_se(startswith(l, "env=")); + + r = deserialize_environment(l+4, &env2); + assert_se(r >= 0); + } + assert_se(feof(f)); + + assert_se(strv_equal(env, env2)); +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_INFO); @@ -143,6 +202,8 @@ int main(int argc, char *argv[]) { test_serialize_item_escaped(); test_serialize_usec(); test_serialize_strv(); + test_deserialize_environment(); + test_serialize_environment(); return EXIT_SUCCESS; } |