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/>.
31 static void test_parse_env_file(void) {
32 char t[] = "/tmp/test-parse-env-file-XXXXXX";
35 _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
36 *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
37 _cleanup_strv_free_ char **a = NULL, **b = NULL;
41 fd = mkostemp(t, O_CLOEXEC);
54 "four = \'44\\\"44\'\n"
55 "five = \'55\\\'55\' \"FIVE\" cinco \n"
56 "six = seis sechs\\\n"
58 "seven=\"sevenval\"#comment\n"
60 "export nine=nineval\n"
66 r = load_env_file(t, NULL, &a);
70 log_info("Got: <%s>", *i);
72 assert_se(streq(a[0], "one=BAR"));
73 assert_se(streq(a[1], "two=bar"));
74 assert_se(streq(a[2], "three=333\nxxxx"));
75 assert_se(streq(a[3], "four=44\"44"));
76 assert_se(streq(a[4], "five=55\'55FIVEcinco"));
77 assert_se(streq(a[5], "six=seis sechs sis"));
78 assert_se(streq(a[6], "seven=sevenval"));
79 assert_se(streq(a[7], "eight="));
80 assert_se(streq(a[8], "export nine=nineval"));
81 assert_se(streq(a[9], "ten="));
82 assert_se(a[10] == NULL);
84 strv_env_clean_log(a, "/tmp/test-fileio");
86 r = write_env_file("/tmp/test-fileio", a);
89 r = load_env_file("/tmp/test-fileio", NULL, &b);
94 log_info("Got2: <%s>", *i);
95 assert_se(streq(*i, a[k++]));
108 "export nine", &nine,
114 log_info("one=[%s]", strna(one));
115 log_info("two=[%s]", strna(two));
116 log_info("three=[%s]", strna(three));
117 log_info("four=[%s]", strna(four));
118 log_info("five=[%s]", strna(five));
119 log_info("six=[%s]", strna(six));
120 log_info("seven=[%s]", strna(seven));
121 log_info("eight=[%s]", strna(eight));
122 log_info("export nine=[%s]", strna(nine));
123 log_info("ten=[%s]", strna(nine));
125 assert_se(streq(one, "BAR"));
126 assert_se(streq(two, "bar"));
127 assert_se(streq(three, "333\nxxxx"));
128 assert_se(streq(four, "44\"44"));
129 assert_se(streq(five, "55\'55FIVEcinco"));
130 assert_se(streq(six, "seis sechs sis"));
131 assert_se(streq(seven, "sevenval"));
132 assert_se(eight == NULL);
133 assert_se(streq(nine, "nineval"));
134 assert_se(ten == NULL);
137 unlink("/tmp/test-fileio");
140 int main(int argc, char *argv[]) {
141 test_parse_env_file();