From: Zbigniew Jędrzejewski-Szmek Date: Sat, 24 Jun 2017 00:43:48 +0000 (-0400) Subject: tests: add tests for environment serialization X-Git-Tag: v234~103^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a096d8c88f65dcba78bcd34048f463dfd7783046;p=thirdparty%2Fsystemd.git tests: add tests for environment serialization --- diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index 904c50f0edc..3a2492dc6fd 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -21,6 +21,8 @@ #include #include "env-util.h" +#include "fd-util.h" +#include "fileio.h" #include "string-util.h" #include "strv.h" #include "util.h" @@ -323,6 +325,48 @@ static void test_deserialize_environment(void) { assert_se(strv_equal(env, STRV_MAKE("A=1", "B=2"))); } +static void test_serialize_environment(void) { + char fn[] = "/tmp/test-env-util.XXXXXXX"; + int fd, r; + _cleanup_fclose_ FILE *f = NULL; + + _cleanup_strv_free_ char **env = strv_new("A=1", + "B=2", + "C=ąęółń", + "D=D=a\\x0Ab", + NULL); + _cleanup_strv_free_ char **env2 = NULL; + + fd = mkostemp_safe(fn); + assert_se(fd >= 0); + + assert_se(f = fdopen(fd, "r+")); + + assert_se(serialize_environment(f, env) == 0); + assert_se(fflush_and_check(f) == 0); + + rewind(f); + + for (;;) { + char line[LINE_MAX]; + const char *l; + + if (!fgets(line, sizeof line, f)) + break; + + char_array_0(line); + l = strstrip(line); + + r = deserialize_environment(&env2, l); + assert_se(r == 1); + } + assert_se(feof(f)); + + assert_se(strv_equal(env, env2)); + + unlink(fn); +} + int main(int argc, char *argv[]) { test_strv_env_delete(); test_strv_env_get(); @@ -340,6 +384,7 @@ int main(int argc, char *argv[]) { test_env_value_is_valid(); test_env_assignment_is_valid(); test_deserialize_environment(); + test_serialize_environment(); return 0; } diff --git a/src/test/test-escape.c b/src/test/test-escape.c index 181d7db5b4c..d060afaffa7 100644 --- a/src/test/test-escape.c +++ b/src/test/test-escape.c @@ -69,6 +69,14 @@ static void test_cunescape(void) { assert_se(cunescape("\\073", 0, &unescaped) >= 0); assert_se(streq_ptr(unescaped, ";")); + unescaped = mfree(unescaped); + + assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0); + assert_se(streq_ptr(unescaped, "A=A\\x0aB")); + unescaped = mfree(unescaped); + + assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0); + assert_se(streq_ptr(unescaped, "A=A\\x0aB")); } static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {