summaryrefslogtreecommitdiff
path: root/src/test/test-serialize.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/test-serialize.c')
-rw-r--r--src/test/test-serialize.c61
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;
}