1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 static void test_parse_env_file(void) {
31 char t[] = "/tmp/test-parse-env-file-XXXXXX";
34 _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL;
35 _cleanup_strv_free_ char **a = NULL, **b = NULL;
39 fd = mkostemp(t, O_CLOEXEC);
52 "four = \'44\\\"44\'\n"
53 "five = \'55\\\'55\' \"FIVE\" cinco \n"
54 "six = seis sechs\\\n"
56 "export seven=\"sevenval\"#comment\n"
78 log_info("one=[%s]", strna(one));
79 log_info("two=[%s]", strna(two));
80 log_info("three=[%s]", strna(three));
81 log_info("four=[%s]", strna(four));
82 log_info("five=[%s]", strna(five));
83 log_info("six=[%s]", strna(six));
84 log_info("seven=[%s]", strna(seven));
85 log_info("eight=[%s]", strna(eight));
86 log_info("nine=[%s]", strna(nine));
88 assert_se(streq(one, "BAR"));
89 assert_se(streq(two, "bar"));
90 assert_se(streq(three, "333\nxxxx"));
91 assert_se(streq(four, "44\"44"));
92 assert_se(streq(five, "55\'55FIVEcinco"));
93 assert_se(streq(six, "seis sechs sis"));
94 assert_se(streq(seven, "sevenval"));
95 assert_se(eight == NULL);
96 assert_se(nine == NULL);
98 r = load_env_file(t, NULL, &a);
102 log_info("Got: <%s>", *i);
104 assert_se(streq(a[0], "one=BAR"));
105 assert_se(streq(a[1], "two=bar"));
106 assert_se(streq(a[2], "three=333\nxxxx"));
107 assert_se(streq(a[3], "four=44\"44"));
108 assert_se(streq(a[4], "five=55\'55FIVEcinco"));
109 assert_se(streq(a[5], "six=seis sechs sis"));
110 assert_se(streq(a[6], "seven=sevenval"));
111 assert_se(streq(a[7], "eight="));
112 assert_se(streq(a[8], "nine="));
113 assert_se(a[9] == NULL);
115 r = write_env_file("/tmp/test-fileio", a);
118 r = load_env_file("/tmp/test-fileio", NULL, &b);
123 log_info("Got2: <%s>", *i);
124 assert_se(streq(*i, a[k++]));
128 unlink("/tmp/test-fileio");
131 int main(int argc, char *argv[]) {
132 test_parse_env_file();