tests: add tests for environment serialization
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 24 Jun 2017 00:43:48 +0000 (20:43 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 24 Jun 2017 00:46:33 +0000 (20:46 -0400)
src/test/test-env-util.c
src/test/test-escape.c

index 904c50f..3a2492d 100644 (file)
@@ -21,6 +21,8 @@
 #include <string.h>
 
 #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;
 }
index 181d7db..d060afa 100644 (file)
@@ -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) {