]>
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 "process-util.h"
32 #include "string-util.h"
36 static void test_parse_env_file(void) {
37 char t
[] = "/tmp/test-fileio-in-XXXXXX",
38 p
[] = "/tmp/test-fileio-out-XXXXXX";
41 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
42 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
43 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
47 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
51 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
63 "invalid line #comment\n"
66 "four = \'44\\\"44\'\n"
67 "five = \'55\\\'55\' \"FIVE\" cinco \n"
68 "six = seis sechs\\\n"
70 "seven=\"sevenval\" #nocomment\n"
71 "eight=eightval #nocomment\n"
72 "export nine=nineval\n"
78 r
= load_env_file(NULL
, t
, NULL
, &a
);
82 log_info("Got: <%s>", *i
);
84 assert_se(streq_ptr(a
[0], "one=BAR"));
85 assert_se(streq_ptr(a
[1], "two=bar"));
86 assert_se(streq_ptr(a
[2], "three=333\nxxxx"));
87 assert_se(streq_ptr(a
[3], "four=44\"44"));
88 assert_se(streq_ptr(a
[4], "five=55\'55FIVEcinco"));
89 assert_se(streq_ptr(a
[5], "six=seis sechs sis"));
90 assert_se(streq_ptr(a
[6], "seven=sevenval#nocomment"));
91 assert_se(streq_ptr(a
[7], "eight=eightval #nocomment"));
92 assert_se(streq_ptr(a
[8], "export nine=nineval"));
93 assert_se(streq_ptr(a
[9], "ten="));
94 assert_se(a
[10] == NULL
);
100 log_info("Got2: <%s>", *i
);
101 assert_se(streq(*i
, a
[k
++]));
114 "export nine", &nine
,
120 log_info("one=[%s]", strna(one
));
121 log_info("two=[%s]", strna(two
));
122 log_info("three=[%s]", strna(three
));
123 log_info("four=[%s]", strna(four
));
124 log_info("five=[%s]", strna(five
));
125 log_info("six=[%s]", strna(six
));
126 log_info("seven=[%s]", strna(seven
));
127 log_info("eight=[%s]", strna(eight
));
128 log_info("export nine=[%s]", strna(nine
));
129 log_info("ten=[%s]", strna(nine
));
131 assert_se(streq(one
, "BAR"));
132 assert_se(streq(two
, "bar"));
133 assert_se(streq(three
, "333\nxxxx"));
134 assert_se(streq(four
, "44\"44"));
135 assert_se(streq(five
, "55\'55FIVEcinco"));
136 assert_se(streq(six
, "seis sechs sis"));
137 assert_se(streq(seven
, "sevenval#nocomment"));
138 assert_se(streq(eight
, "eightval #nocomment"));
139 assert_se(streq(nine
, "nineval"));
140 assert_se(ten
== NULL
);
142 r
= write_env_file(p
, a
);
145 r
= load_env_file(NULL
, p
, NULL
, &b
);
152 static void test_parse_multiline_env_file(void) {
153 char t
[] = "/tmp/test-fileio-in-XXXXXX",
154 p
[] = "/tmp/test-fileio-out-XXXXXX";
157 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
160 fd
= mkostemp_safe(p
, O_RDWR
|O_CLOEXEC
);
164 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
185 r
= load_env_file(NULL
, t
, NULL
, &a
);
189 log_info("Got: <%s>", *i
);
191 assert_se(streq_ptr(a
[0], "one=BAR VAR\tGAR"));
192 assert_se(streq_ptr(a
[1], "two=bar var\tgar"));
193 assert_se(streq_ptr(a
[2], "tri=bar var \tgar "));
194 assert_se(a
[3] == NULL
);
196 r
= write_env_file(p
, a
);
199 r
= load_env_file(NULL
, p
, NULL
, &b
);
207 static void test_executable_is_script(void) {
208 char t
[] = "/tmp/test-executable-XXXXXX";
213 fd
= mkostemp_safe(t
, O_RDWR
|O_CLOEXEC
);
219 fputs("#! /bin/script -a -b \ngoo goo", f
);
222 r
= executable_is_script(t
, &command
);
224 assert_se(streq(command
, "/bin/script"));
227 r
= executable_is_script("/bin/sh", &command
);
230 r
= executable_is_script("/usr/bin/yum", &command
);
231 assert_se(r
> 0 || r
== -ENOENT
);
233 assert_se(startswith(command
, "/"));
241 static void test_status_field(void) {
242 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
, *z
= NULL
;
243 unsigned long long total
= 0, buffers
= 0;
246 assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE
, &t
) == 0);
248 assert_se(streq(t
, "1"));
250 r
= get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE
, &p
);
254 assert_se(safe_atollu(p
, &total
) == 0);
257 r
= get_proc_field("/proc/meminfo", "Buffers", WHITESPACE
, &s
);
261 assert_se(safe_atollu(s
, &buffers
) == 0);
265 assert_se(buffers
< total
);
267 /* Seccomp should be a good test for field full of zeros. */
268 r
= get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE
, &z
);
272 assert_se(safe_atollu(z
, &buffers
) == 0);
276 static void test_capeff(void) {
279 for (pid
= 0; pid
< 2; pid
++) {
280 _cleanup_free_
char *capeff
= NULL
;
283 r
= get_process_capeff(0, &capeff
);
284 log_info("capeff: '%s' (r=%d)", capeff
, r
);
286 if (r
== -ENOENT
|| r
== -EPERM
)
291 p
= capeff
[strspn(capeff
, DIGITS
"abcdefABCDEF")];
292 assert_se(!p
|| isspace(p
));
296 static void test_write_string_stream(void) {
297 char fn
[] = "/tmp/test-write_string_stream-XXXXXX";
298 _cleanup_fclose_
FILE *f
= NULL
;
302 fd
= mkostemp_safe(fn
, O_RDWR
);
307 assert_se(write_string_stream(f
, "boohoo", true) < 0);
309 f
= freopen(fn
, "r+", f
);
312 assert_se(write_string_stream(f
, "boohoo", true) == 0);
315 assert_se(fgets(buf
, sizeof(buf
), f
));
316 assert_se(streq(buf
, "boohoo\n"));
318 f
= freopen(fn
, "w+", f
);
321 assert_se(write_string_stream(f
, "boohoo", false) == 0);
324 assert_se(fgets(buf
, sizeof(buf
), f
));
326 assert_se(streq(buf
, "boohoo"));
331 static void test_write_string_file(void) {
332 char fn
[] = "/tmp/test-write_string_file-XXXXXX";
334 _cleanup_close_
int fd
;
336 fd
= mkostemp_safe(fn
, O_RDWR
);
339 assert_se(write_string_file(fn
, "boohoo", WRITE_STRING_FILE_CREATE
) == 0);
341 assert_se(read(fd
, buf
, sizeof(buf
)) == 7);
342 assert_se(streq(buf
, "boohoo\n"));
347 static void test_write_string_file_no_create(void) {
348 char fn
[] = "/tmp/test-write_string_file_no_create-XXXXXX";
349 _cleanup_close_
int fd
;
352 fd
= mkostemp_safe(fn
, O_RDWR
);
355 assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
356 assert_se(write_string_file(fn
, "boohoo", 0) == 0);
358 assert_se(read(fd
, buf
, sizeof(buf
)) == strlen("boohoo\n"));
359 assert_se(streq(buf
, "boohoo\n"));
364 static void test_load_env_file_pairs(void) {
365 char fn
[] = "/tmp/test-load_env_file_pairs-XXXXXX";
368 _cleanup_fclose_
FILE *f
= NULL
;
369 _cleanup_strv_free_
char **l
= NULL
;
372 fd
= mkostemp_safe(fn
, O_RDWR
);
375 r
= write_string_file(fn
,
376 "NAME=\"Arch Linux\"\n"
378 "PRETTY_NAME=\"Arch Linux\"\n"
379 "ANSI_COLOR=\"0;36\"\n"
380 "HOME_URL=\"https://www.archlinux.org/\"\n"
381 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
382 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
383 WRITE_STRING_FILE_CREATE
);
389 r
= load_env_file_pairs(f
, fn
, NULL
, &l
);
392 assert_se(strv_length(l
) == 14);
393 STRV_FOREACH_PAIR(k
, v
, l
) {
394 assert_se(STR_IN_SET(*k
, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
395 printf("%s=%s\n", *k
, *v
);
396 if (streq(*k
, "NAME")) assert_se(streq(*v
, "Arch Linux"));
397 if (streq(*k
, "ID")) assert_se(streq(*v
, "arch"));
398 if (streq(*k
, "PRETTY_NAME")) assert_se(streq(*v
, "Arch Linux"));
399 if (streq(*k
, "ANSI_COLOR")) assert_se(streq(*v
, "0;36"));
400 if (streq(*k
, "HOME_URL")) assert_se(streq(*v
, "https://www.archlinux.org/"));
401 if (streq(*k
, "SUPPORT_URL")) assert_se(streq(*v
, "https://bbs.archlinux.org/"));
402 if (streq(*k
, "BUG_REPORT_URL")) assert_se(streq(*v
, "https://bugs.archlinux.org/"));
408 int main(int argc
, char *argv
[]) {
409 log_parse_environment();
412 test_parse_env_file();
413 test_parse_multiline_env_file();
414 test_executable_is_script();
417 test_write_string_stream();
418 test_write_string_file();
419 test_write_string_file_no_create();
420 test_load_env_file_pairs();