]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fileio.c
Make test-login and test-sleep output debugging
[thirdparty/systemd.git] / src / test / test-fileio.c
CommitLineData
f73141d7
LP
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"
ebc05a09 29#include "env-util.h"
f73141d7
LP
30
31static void test_parse_env_file(void) {
095b30cb
ZJS
32 char t[] = "/tmp/test-fileio-in-XXXXXX",
33 p[] = "/tmp/test-fileio-out-XXXXXX";
f73141d7
LP
34 int fd, r;
35 FILE *f;
ebc05a09
HH
36 _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
37 *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
768100ef 38 _cleanup_strv_free_ char **a = NULL, **b = NULL;
f73141d7 39 char **i;
768100ef 40 unsigned k;
f73141d7 41
095b30cb
ZJS
42 assert_se(mktemp(p));
43
f73141d7
LP
44 fd = mkostemp(t, O_CLOEXEC);
45 assert_se(fd >= 0);
46
47 f = fdopen(fd, "w");
48 assert_se(f);
49
50 fputs("one=BAR \n"
51 "# comment\n"
52 " # comment \n"
98f59e59 53 " ; comment \n"
f73141d7
LP
54 " two = bar \n"
55 "invalid line\n"
98f59e59 56 "invalid line #comment\n"
f73141d7
LP
57 "three = \"333\n"
58 "xxxx\"\n"
59 "four = \'44\\\"44\'\n"
60 "five = \'55\\\'55\' \"FIVE\" cinco \n"
61 "six = seis sechs\\\n"
62 " sis\n"
98f59e59
HH
63 "seven=\"sevenval\" #nocomment\n"
64 "eight=eightval #nocomment\n"
ebc05a09
HH
65 "export nine=nineval\n"
66 "ten=", f);
f73141d7
LP
67
68 fflush(f);
69 fclose(f);
70
ebc05a09
HH
71 r = load_env_file(t, NULL, &a);
72 assert_se(r >= 0);
73
74 STRV_FOREACH(i, a)
75 log_info("Got: <%s>", *i);
76
77 assert_se(streq(a[0], "one=BAR"));
78 assert_se(streq(a[1], "two=bar"));
79 assert_se(streq(a[2], "three=333\nxxxx"));
80 assert_se(streq(a[3], "four=44\"44"));
81 assert_se(streq(a[4], "five=55\'55FIVEcinco"));
82 assert_se(streq(a[5], "six=seis sechs sis"));
98f59e59
HH
83 assert_se(streq(a[6], "seven=sevenval#nocomment"));
84 assert_se(streq(a[7], "eight=eightval #nocomment"));
ebc05a09
HH
85 assert_se(streq(a[8], "export nine=nineval"));
86 assert_se(streq(a[9], "ten="));
87 assert_se(a[10] == NULL);
88
095b30cb 89 strv_env_clean_log(a, "test");
ebc05a09 90
ebc05a09
HH
91 k = 0;
92 STRV_FOREACH(i, b) {
93 log_info("Got2: <%s>", *i);
94 assert_se(streq(*i, a[k++]));
95 }
96
f73141d7
LP
97 r = parse_env_file(
98 t, NULL,
99 "one", &one,
100 "two", &two,
101 "three", &three,
102 "four", &four,
103 "five", &five,
104 "six", &six,
105 "seven", &seven,
db537209 106 "eight", &eight,
ebc05a09
HH
107 "export nine", &nine,
108 "ten", &ten,
f73141d7
LP
109 NULL);
110
111 assert_se(r >= 0);
112
113 log_info("one=[%s]", strna(one));
114 log_info("two=[%s]", strna(two));
115 log_info("three=[%s]", strna(three));
116 log_info("four=[%s]", strna(four));
117 log_info("five=[%s]", strna(five));
118 log_info("six=[%s]", strna(six));
119 log_info("seven=[%s]", strna(seven));
db537209 120 log_info("eight=[%s]", strna(eight));
ebc05a09
HH
121 log_info("export nine=[%s]", strna(nine));
122 log_info("ten=[%s]", strna(nine));
f73141d7
LP
123
124 assert_se(streq(one, "BAR"));
125 assert_se(streq(two, "bar"));
126 assert_se(streq(three, "333\nxxxx"));
127 assert_se(streq(four, "44\"44"));
128 assert_se(streq(five, "55\'55FIVEcinco"));
129 assert_se(streq(six, "seis sechs sis"));
98f59e59
HH
130 assert_se(streq(seven, "sevenval#nocomment"));
131 assert_se(streq(eight, "eightval #nocomment"));
ebc05a09
HH
132 assert_se(streq(nine, "nineval"));
133 assert_se(ten == NULL);
f73141d7 134
095b30cb 135 r = write_env_file(p, a);
98f59e59
HH
136 assert_se(r >= 0);
137
095b30cb 138 r = load_env_file(p, NULL, &b);
98f59e59
HH
139 assert_se(r >= 0);
140
f73141d7 141 unlink(t);
095b30cb 142 unlink(p);
f73141d7
LP
143}
144
ac4c8d6d
ZJS
145static void test_parse_multiline_env_file(void) {
146 char t[] = "/tmp/test-fileio-in-XXXXXX",
147 p[] = "/tmp/test-fileio-out-XXXXXX";
148 int fd, r;
149 FILE *f;
150 _cleanup_strv_free_ char **a = NULL, **b = NULL;
151 char **i;
152
153 assert_se(mktemp(p));
154
155 fd = mkostemp(t, O_CLOEXEC);
156 assert_se(fd >= 0);
157
158 f = fdopen(fd, "w");
159 assert_se(f);
160
161 fputs("one=BAR\\\n"
162 " VAR\\\n"
163 "\tGAR\n"
164 "#comment\n"
165 "two=\"bar\\\n"
166 " var\\\n"
167 "\tgar\"\n"
168 "#comment\n"
169 "tri=\"bar \\\n"
170 " var \\\n"
171 "\tgar \"\n", f);
172
173 fflush(f);
174 fclose(f);
175
176 r = load_env_file(t, NULL, &a);
177 assert_se(r >= 0);
178
179 STRV_FOREACH(i, a)
180 log_info("Got: <%s>", *i);
181
182 assert_se(streq(a[0], "one=BAR VAR\tGAR"));
183 assert_se(streq(a[1], "two=bar var\tgar"));
184 assert_se(streq(a[2], "tri=bar var \tgar "));
185 assert_se(a[3] == NULL);
186
187 r = write_env_file(p, a);
188 assert_se(r >= 0);
189
190 r = load_env_file(p, NULL, &b);
191 assert_se(r >= 0);
192
193 unlink(t);
194 unlink(p);
195}
196
197
68fee104
ZJS
198static void test_executable_is_script(void) {
199 char t[] = "/tmp/test-executable-XXXXXX";
200 int fd, r;
201 FILE *f;
202 char *command;
203
204 fd = mkostemp(t, O_CLOEXEC);
205 assert_se(fd >= 0);
206
207 f = fdopen(fd, "w");
208 assert_se(f);
209
210 fputs("#! /bin/script -a -b \ngoo goo", f);
211 fflush(f);
212
213 r = executable_is_script(t, &command);
214 assert_se(r > 0);
215 assert_se(streq(command, "/bin/script"));
216 free(command);
217
218 r = executable_is_script("/bin/sh", &command);
219 assert_se(r == 0);
220
221 r = executable_is_script("/usr/bin/yum", &command);
222 assert_se(r > 0 || r == -ENOENT);
223 if (r > 0) {
224 assert_se(startswith(command, "/"));
225 free(command);
226 }
227
228 fclose(f);
229 unlink(t);
230}
231
69ab8088
ZJS
232static void test_status_field(void) {
233 _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL;
234 unsigned long long total, buffers;
442e0083 235 int r;
69ab8088
ZJS
236
237 assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
238 puts(t);
239 assert_se(streq(t, "1"));
240
442e0083
ZJS
241 r = get_status_field("/proc/meminfo", "MemTotal:", &p);
242 if (r == -ENOENT)
243 return;
244 assert(r == 0);
69ab8088
ZJS
245 puts(p);
246 assert_se(safe_atollu(p, &total) == 0);
247
248 assert_se(get_status_field("/proc/meminfo", "\nBuffers:", &s) == 0);
249 puts(s);
250 assert_se(safe_atollu(s, &buffers) == 0);
251
252 assert(buffers < total);
253}
254
f73141d7
LP
255int main(int argc, char *argv[]) {
256 test_parse_env_file();
ac4c8d6d 257 test_parse_multiline_env_file();
68fee104 258 test_executable_is_script();
69ab8088 259 test_status_field();
f73141d7
LP
260 return 0;
261}