tests: add tests for environment serialization

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-06-23 20:46:33 -04:00
parent 417eefb170
commit a096d8c88f
2 changed files with 53 additions and 0 deletions
+45
View 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;
}
+8
View 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) {