]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fileio.c
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/>.
27 #include "process-util.h"
34 static void test_parse_env_file(void) {
35 char t
[] = "/tmp/test-fileio-in-XXXXXX",
36 p
[] = "/tmp/test-fileio-out-XXXXXX";
39 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
40 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
41 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
45 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
49 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
61 "invalid line #comment\n"
64 "four = \'44\\\"44\'\n"
65 "five = \'55\\\'55\' \"FIVE\" cinco \n"
66 "six = seis sechs\\\n"
68 "seven=\"sevenval\" #nocomment\n"
69 "eight=eightval #nocomment\n"
70 "export nine=nineval\n"
76 r
= load_env_file(NULL
, t
, NULL
, &a
);
80 log_info("Got: <%s>", *i
);
82 assert_se(streq_ptr(a
[0], "one=BAR"));
83 assert_se(streq_ptr(a
[1], "two=bar"));
84 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
85 assert_se(streq_ptr(a
[3], "four=44\"44"));
86 assert_se(streq_ptr(a
[4], "five=55\'55FIVEcinco"));
87 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
88 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
89 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
90 assert_se(streq_ptr(a
[8], "export nine=nineval"));
91 assert_se(streq_ptr(a
[9], "ten="));
92 assert_se(a
[10] == NULL
);
98 log_info("Got2: <%s>", *i
);
99 assert_se(streq(*i
, a
[k
++]));
112 "export nine", &nine
,
118 log_info("one=[%s]", strna(one
));
119 log_info("two=[%s]", strna(two
));
120 log_info("three=[%s]", strna(three
));
121 log_info("four=[%s]", strna(four
));
122 log_info("five=[%s]", strna(five
));
123 log_info("six=[%s]", strna(six
));
124 log_info("seven=[%s]", strna(seven
));
125 log_info("eight=[%s]", strna(eight
));
126 log_info("export nine=[%s]", strna(nine
));
127 log_info("ten=[%s]", strna(nine
));
129 assert_se(streq(one
, "BAR"));
130 assert_se(streq(two
, "bar"));
131 assert_se(streq(three
, "333\nxxxx"));
132 assert_se(streq(four
, "44\"44"));
133 assert_se(streq(five
, "55\'55FIVEcinco"));
134 assert_se(streq(six
, "seis sechs sis"));
135 assert_se(streq(seven
, "sevenval#nocomment"));
136 assert_se(streq(eight
, "eightval #nocomment"));
137 assert_se(streq(nine
, "nineval"));
138 assert_se(ten
== NULL
);
140 r
= write_env_file(p
, a
);
143 r
= load_env_file(NULL
, p
, NULL
, &b
);
150 static void test_parse_multiline_env_file(void) {
151 char t
[] = "/tmp/test-fileio-in-XXXXXX",
152 p
[] = "/tmp/test-fileio-out-XXXXXX";
155 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
158 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
162 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
183 r
= load_env_file(NULL
, t
, NULL
, &a
);
187 log_info("Got: <%s>", *i
);
189 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
190 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
191 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
192 assert_se(a
[3] == NULL
);
194 r
= write_env_file(p
, a
);
197 r
= load_env_file(NULL
, p
, NULL
, &b
);
205 static void test_executable_is_script(void) {
206 char t
[] = "/tmp/test-executable-XXXXXX";
211 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
217 fputs("#! /bin/script -a -b \ngoo goo", f
);
220 r
= executable_is_script(t
, &command
);
222 assert_se(streq(command
, "/bin/script"));
225 r
= executable_is_script("/bin/sh", &command
);
228 r
= executable_is_script("/usr/bin/yum", &command
);
229 assert_se(r
> 0 || r
== -ENOENT
);
231 assert_se(startswith(command
, "/"));
239 static void test_status_field(void) {
240 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
241 unsigned long long total
= 0, buffers
= 0;
244 assert_se(get_status_field("/proc/self/status", "\nThreads:", &t
) == 0);
246 assert_se(streq(t
, "1"));
248 r
= get_status_field("/proc/meminfo", "MemTotal:", &p
);
252 assert_se(safe_atollu(p
, &total
) == 0);
255 r
= get_status_field("/proc/meminfo", "\nBuffers:", &s
);
259 assert_se(safe_atollu(s
, &buffers
) == 0);
263 assert_se(buffers
< total
);
265 /* Seccomp should be a good test for field full of zeros. */
266 r
= get_status_field("/proc/meminfo", "\nSeccomp:", &z
);
270 assert_se(safe_atollu(z
, &buffers
) == 0);
274 static void test_capeff(void) {
277 for (pid
= 0; pid
< 2; pid
++) {
278 _cleanup_free_
char *capeff
= NULL
;
281 r
= get_process_capeff(0, &capeff
);
282 log_info("capeff: '%s' (r=%d)", capeff
, r
);
284 if (r
== -ENOENT
|| r
== -EPERM
)
289 p
= capeff
[strspn(capeff
, DIGITS
"abcdefABCDEF")];
290 assert_se(!p
|| isspace(p
));
294 static void test_write_string_stream(void) {
295 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
296 _cleanup_fclose_
FILE *f
= NULL
;
300 fd
= mkostemp_safe(fn
, O_RDWR
);
305 assert_se(write_string_stream(f
, "boohoo") < 0);
307 f
= freopen(fn
, "r+", f
);
310 assert_se(write_string_stream(f
, "boohoo") == 0);
313 assert_se(fgets(buf
, sizeof(buf
), f
));
314 assert_se(streq(buf
, "boohoo\n"));
319 static void test_write_string_file(void) {
320 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
322 _cleanup_close_
int fd
;
324 fd
= mkostemp_safe(fn
, O_RDWR
);
327 assert_se(write_string_file(fn
, "boohoo") == 0);
329 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
330 assert_se(streq(buf
, "boohoo\n"));
335 static void test_write_string_file_no_create(void) {
336 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
337 _cleanup_close_
int fd
;
340 fd
= mkostemp_safe(fn
, O_RDWR
);
343 assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0);
344 assert_se(write_string_file_no_create(fn
, "boohoo") == 0);
346 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
347 assert_se(streq(buf
, "boohoo\n"));
352 static void test_load_env_file_pairs(void) {
353 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
356 _cleanup_fclose_
FILE *f
= NULL
;
357 _cleanup_strv_free_
char **l
= NULL
;
360 fd
= mkostemp_safe(fn
, O_RDWR
);
363 r
= write_string_file(fn
,
364 "NAME=\"Arch Linux\"\n"
366 "PRETTY_NAME=\"Arch Linux\"\n"
367 "ANSI_COLOR=\"0;36\"\n"
368 "HOME_URL=\"https://www.archlinux.org/\"\n"
369 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
370 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"
377 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
380 assert_se(strv_length(l
) == 14);
381 STRV_FOREACH_PAIR(k
, v
, l
) {
382 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
383 printf("%s=%s\n", *k
, *v
);
384 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
385 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
386 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
387 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
388 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
389 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
390 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
396 int main(int argc
, char *argv
[]) {
397 log_parse_environment();
400 test_parse_env_file();
401 test_parse_multiline_env_file();
402 test_executable_is_script();
405 test_write_string_stream();
406 test_write_string_file();
407 test_write_string_file_no_create();
408 test_load_env_file_pairs();