]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fileio.c
test-path-util,test-sched-prio: uninitialize manager to appease valgrind
[thirdparty/systemd.git] / src / test / test-fileio.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include "util.h"
27 #include "fileio.h"
28 #include "strv.h"
29 #include "env-util.h"
30
31 static void test_parse_env_file(void) {
32 char t[] = "/tmp/test-parse-env-file-XXXXXX";
33 int fd, r;
34 FILE *f;
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;
38 char **i;
39 unsigned k;
40
41 fd = mkostemp(t, O_CLOEXEC);
42 assert_se(fd >= 0);
43
44 f = fdopen(fd, "w");
45 assert_se(f);
46
47 fputs("one=BAR \n"
48 "# comment\n"
49 " # comment \n"
50 " ; comment \n"
51 " two = bar \n"
52 "invalid line\n"
53 "invalid line #comment\n"
54 "three = \"333\n"
55 "xxxx\"\n"
56 "four = \'44\\\"44\'\n"
57 "five = \'55\\\'55\' \"FIVE\" cinco \n"
58 "six = seis sechs\\\n"
59 " sis\n"
60 "seven=\"sevenval\" #nocomment\n"
61 "eight=eightval #nocomment\n"
62 "export nine=nineval\n"
63 "ten=", f);
64
65 fflush(f);
66 fclose(f);
67
68 r = load_env_file(t, NULL, &a);
69 assert_se(r >= 0);
70
71 STRV_FOREACH(i, a)
72 log_info("Got: <%s>", *i);
73
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);
85
86 strv_env_clean_log(a, "/tmp/test-fileio");
87
88 k = 0;
89 STRV_FOREACH(i, b) {
90 log_info("Got2: <%s>", *i);
91 assert_se(streq(*i, a[k++]));
92 }
93
94 r = parse_env_file(
95 t, NULL,
96 "one", &one,
97 "two", &two,
98 "three", &three,
99 "four", &four,
100 "five", &five,
101 "six", &six,
102 "seven", &seven,
103 "eight", &eight,
104 "export nine", &nine,
105 "ten", &ten,
106 NULL);
107
108 assert_se(r >= 0);
109
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));
120
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);
131
132 r = write_env_file("/tmp/test-fileio", a);
133 assert_se(r >= 0);
134
135 r = load_env_file("/tmp/test-fileio", NULL, &b);
136 assert_se(r >= 0);
137
138 unlink(t);
139 unlink("/tmp/test-fileio");
140 }
141
142 int main(int argc, char *argv[]) {
143 test_parse_env_file();
144 return 0;
145 }