]>
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/>.
31 #include "parse-util.h"
32 #include "process-util.h"
33 #include "string-util.h"
37 static void test_parse_env_file(void) {
38 char t
[] = "/tmp/test-fileio-in-XXXXXX",
39 p
[] = "/tmp/test-fileio-out-XXXXXX";
42 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
43 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
44 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
48 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
52 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
64 "invalid line #comment\n"
67 "four = \'44\\\"44\'\n"
68 "five = \'55\\\'55\' \"FIVE\" cinco \n"
69 "six = seis sechs\\\n"
71 "seven=\"sevenval\" #nocomment\n"
72 "eight=eightval #nocomment\n"
73 "export nine=nineval\n"
79 r
= load_env_file(NULL
, t
, NULL
, &a
);
83 log_info("Got: <%s>", *i
);
85 assert_se(streq_ptr(a
[0], "one=BAR"));
86 assert_se(streq_ptr(a
[1], "two=bar"));
87 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
88 assert_se(streq_ptr(a
[3], "four=44\"44"));
89 assert_se(streq_ptr(a
[4], "five=55\'55FIVEcinco"));
90 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
91 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
92 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
93 assert_se(streq_ptr(a
[8], "export nine=nineval"));
94 assert_se(streq_ptr(a
[9], "ten="));
95 assert_se(a
[10] == NULL
);
101 log_info("Got2: <%s>", *i
);
102 assert_se(streq(*i
, a
[k
++]));
115 "export nine", &nine
,
121 log_info("one=[%s]", strna(one
));
122 log_info("two=[%s]", strna(two
));
123 log_info("three=[%s]", strna(three
));
124 log_info("four=[%s]", strna(four
));
125 log_info("five=[%s]", strna(five
));
126 log_info("six=[%s]", strna(six
));
127 log_info("seven=[%s]", strna(seven
));
128 log_info("eight=[%s]", strna(eight
));
129 log_info("export nine=[%s]", strna(nine
));
130 log_info("ten=[%s]", strna(nine
));
132 assert_se(streq(one
, "BAR"));
133 assert_se(streq(two
, "bar"));
134 assert_se(streq(three
, "333\nxxxx"));
135 assert_se(streq(four
, "44\"44"));
136 assert_se(streq(five
, "55\'55FIVEcinco"));
137 assert_se(streq(six
, "seis sechs sis"));
138 assert_se(streq(seven
, "sevenval#nocomment"));
139 assert_se(streq(eight
, "eightval #nocomment"));
140 assert_se(streq(nine
, "nineval"));
141 assert_se(ten
== NULL
);
143 r
= write_env_file(p
, a
);
146 r
= load_env_file(NULL
, p
, NULL
, &b
);
153 static void test_parse_multiline_env_file(void) {
154 char t
[] = "/tmp/test-fileio-in-XXXXXX",
155 p
[] = "/tmp/test-fileio-out-XXXXXX";
158 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
161 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
165 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
186 r
= load_env_file(NULL
, t
, NULL
, &a
);
190 log_info("Got: <%s>", *i
);
192 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
193 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
194 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
195 assert_se(a
[3] == NULL
);
197 r
= write_env_file(p
, a
);
200 r
= load_env_file(NULL
, p
, NULL
, &b
);
208 static void test_executable_is_script(void) {
209 char t
[] = "/tmp/test-executable-XXXXXX";
214 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
220 fputs("#! /bin/script -a -b \ngoo goo", f
);
223 r
= executable_is_script(t
, &command
);
225 assert_se(streq(command
, "/bin/script"));
228 r
= executable_is_script("/bin/sh", &command
);
231 r
= executable_is_script("/usr/bin/yum", &command
);
232 assert_se(r
> 0 || r
== -ENOENT
);
234 assert_se(startswith(command
, "/"));
242 static void test_status_field(void) {
243 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
244 unsigned long long total
= 0, buffers
= 0;
247 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
249 assert_se(streq(t
, "1"));
251 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
255 assert_se(safe_atollu(p
, &total
) == 0);
258 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
262 assert_se(safe_atollu(s
, &buffers
) == 0);
266 assert_se(buffers
< total
);
268 /* Seccomp should be a good test for field full of zeros. */
269 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
273 assert_se(safe_atollu(z
, &buffers
) == 0);
277 static void test_capeff(void) {
280 for (pid
= 0; pid
< 2; pid
++) {
281 _cleanup_free_
char *capeff
= NULL
;
284 r
= get_process_capeff(0, &capeff
);
285 log_info("capeff: '%s' (r=%d)", capeff
, r
);
287 if (r
== -ENOENT
|| r
== -EPERM
)
292 p
= capeff
[strspn(capeff
, DIGITS
"abcdefABCDEF")];
293 assert_se(!p
|| isspace(p
));
297 static void test_write_string_stream(void) {
298 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
299 _cleanup_fclose_
FILE *f
= NULL
;
303 fd
= mkostemp_safe(fn
, O_RDWR
);
308 assert_se(write_string_stream(f
, "boohoo", true) < 0);
310 f
= freopen(fn
, "r+", f
);
313 assert_se(write_string_stream(f
, "boohoo", true) == 0);
316 assert_se(fgets(buf
, sizeof(buf
), f
));
317 assert_se(streq(buf
, "boohoo\n"));
319 f
= freopen(fn
, "w+", f
);
322 assert_se(write_string_stream(f
, "boohoo", false) == 0);
325 assert_se(fgets(buf
, sizeof(buf
), f
));
327 assert_se(streq(buf
, "boohoo"));
332 static void test_write_string_file(void) {
333 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
335 _cleanup_close_
int fd
;
337 fd
= mkostemp_safe(fn
, O_RDWR
);
340 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
342 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
343 assert_se(streq(buf
, "boohoo\n"));
348 static void test_write_string_file_no_create(void) {
349 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
350 _cleanup_close_
int fd
;
353 fd
= mkostemp_safe(fn
, O_RDWR
);
356 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
357 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
359 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
360 assert_se(streq(buf
, "boohoo\n"));
365 static void test_load_env_file_pairs(void) {
366 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
369 _cleanup_fclose_
FILE *f
= NULL
;
370 _cleanup_strv_free_
char **l
= NULL
;
373 fd
= mkostemp_safe(fn
, O_RDWR
);
376 r
= write_string_file(fn
,
377 "NAME=\"Arch Linux\"\n"
379 "PRETTY_NAME=\"Arch Linux\"\n"
380 "ANSI_COLOR=\"0;36\"\n"
381 "HOME_URL=\"https://www.archlinux.org/\"\n"
382 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
383 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
384 WRITE_STRING_FILE_CREATE
);
390 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
393 assert_se(strv_length(l
) == 14);
394 STRV_FOREACH_PAIR(k
, v
, l
) {
395 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
396 printf("%s=%s\n", *k
, *v
);
397 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
398 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
399 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
400 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
401 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
402 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
403 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
409 int main(int argc
, char *argv
[]) {
410 log_parse_environment();
413 test_parse_env_file();
414 test_parse_multiline_env_file();
415 test_executable_is_script();
418 test_write_string_stream();
419 test_write_string_file();
420 test_write_string_file_no_create();
421 test_load_env_file_pairs();