summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-09-11 15:18:07 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-09-20 12:11:06 +0200
commitc9d593803402ac0aac1250e97c9aed94d78d32a5 (patch)
tree1bd7b0d08f248e96cedd2331690b52cd97d6d5c9
parent08ecffc983a61100457097a642e69c9b456e1067 (diff)
downloadsystemd-c9d593803402ac0aac1250e97c9aed94d78d32a5.tar.gz
test-env-file: add test that tests our env file read + writer + shell against each other
Should hopefully make regressions on this unlikely. (cherry picked from commit 55f99c26c678b4bf4fb697a57d540b0266aa3f49) (cherry picked from commit 251b96c9b5c8424c1a3bc2a91e3aaff03cdb14d8)
-rw-r--r--src/test/test-env-file.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/test/test-env-file.c b/src/test/test-env-file.c
index 47f86a5676..861238169b 100644
--- a/src/test/test-env-file.c
+++ b/src/test/test-env-file.c
@@ -2,6 +2,7 @@
#include "env-file.h"
#include "fd-util.h"
+#include "fileio.h"
#include "fs-util.h"
#include "macro.h"
#include "strv.h"
@@ -132,6 +133,46 @@ static void test_load_env_file_5(void) {
assert_se(data[2] == NULL);
}
+static void test_write_and_load_env_file(void) {
+ const char *v;
+
+ /* Make sure that our writer, parser and the shell agree on what our env var files mean */
+
+ FOREACH_STRING(v,
+ "obbardc-laptop",
+ "obbardc\\-laptop",
+ "obbardc-lap\\top",
+ "obbardc-lap\\top",
+ "obbardc-lap\\\\top",
+ "double\"quote",
+ "single\'quote",
+ "dollar$dollar",
+ "newline\nnewline") {
+ _cleanup_(unlink_and_freep) char *p = NULL;
+ _cleanup_strv_free_ char **l = NULL;
+ _cleanup_free_ char *j = NULL, *w = NULL, *cmd = NULL, *from_shell = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ size_t sz;
+
+ assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
+
+ assert_se(j = strjoin("TEST=", v));
+ assert_se(write_env_file(p, STRV_MAKE(j)) >= 0);
+
+ assert_se(cmd = strjoin(". ", p, " && /bin/echo -n \"$TEST\""));
+ assert_se(f = popen(cmd, "re"));
+ assert_se(read_full_stream(f, &from_shell, &sz) >= 0);
+ assert_se(sz == strlen(v));
+ assert_se(streq(from_shell, v));
+
+ assert_se(load_env_file(NULL, p, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE(j)));
+
+ assert_se(parse_env_file(NULL, p, "TEST", &w) >= 0);
+ assert_se(streq_ptr(w, v));
+ }
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
@@ -140,4 +181,8 @@ int main(int argc, char *argv[]) {
test_load_env_file_3();
test_load_env_file_4();
test_load_env_file_5();
+
+ test_write_and_load_env_file();
+
+ return 0;
}