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);
53 "invalid line #comment\n"
56 "four = \'44\\\"44\'\n"
57 "five = \'55\\\'55\' \"FIVE\" cinco \n"
58 "six = seis sechs\\\n"
60 "seven=\"sevenval\" #nocomment\n"
61 "eight=eightval #nocomment\n"
62 "export nine=nineval\n"
68 r = load_env_file(t, NULL, &a);
72 log_info("Got: <%s>", *i);
74 assert_se(streq(a[0], "one=BAR"));
75 assert_se(streq(a[1], "two=bar"));
76 assert_se(streq(a[2], "three=333\nxxxx"));
77 assert_se(streq(a[3], "four=44\"44"));
78 assert_se(streq(a[4], "five=55\'55FIVEcinco"));
79 assert_se(streq(a[5], "six=seis sechs sis"));
80 assert_se(streq(a[6], "seven=sevenval#nocomment"));
81 assert_se(streq(a[7], "eight=eightval #nocomment"));
82 assert_se(streq(a[8], "export nine=nineval"));
83 assert_se(streq(a[9], "ten="));
84 assert_se(a[10] == NULL);
86 strv_env_clean_log(a, "/tmp/test-fileio");
90 log_info("Got2: <%s>", *i);
91 assert_se(streq(*i, a[k++]));
104 "export nine", &nine,
110 log_info("one=[%s]", strna(one));
111 log_info("two=[%s]", strna(two));
112 log_info("three=[%s]", strna(three));
113 log_info("four=[%s]", strna(four));
114 log_info("five=[%s]", strna(five));
115 log_info("six=[%s]", strna(six));
116 log_info("seven=[%s]", strna(seven));
117 log_info("eight=[%s]", strna(eight));
118 log_info("export nine=[%s]", strna(nine));
119 log_info("ten=[%s]", strna(nine));
121 assert_se(streq(one, "BAR"));
122 assert_se(streq(two, "bar"));
123 assert_se(streq(three, "333\nxxxx"));
124 assert_se(streq(four, "44\"44"));
125 assert_se(streq(five, "55\'55FIVEcinco"));
126 assert_se(streq(six, "seis sechs sis"));
127 assert_se(streq(seven, "sevenval#nocomment"));
128 assert_se(streq(eight, "eightval #nocomment"));
129 assert_se(streq(nine, "nineval"));
130 assert_se(ten == NULL);
132 r = write_env_file("/tmp/test-fileio", a);
135 r = load_env_file("/tmp/test-fileio", NULL, &b);
139 unlink("/tmp/test-fileio");
142 int main(int argc, char *argv[]) {
143 test_parse_env_file();