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/>.
33 static void test_parse_env_file(void) {
34 char t[] = "/tmp/test-fileio-in-XXXXXX",
35 p[] = "/tmp/test-fileio-out-XXXXXX";
38 _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
39 *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
40 _cleanup_strv_free_ char **a = NULL, **b = NULL;
44 fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
48 fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
60 "invalid line #comment\n"
63 "four = \'44\\\"44\'\n"
64 "five = \'55\\\'55\' \"FIVE\" cinco \n"
65 "six = seis sechs\\\n"
67 "seven=\"sevenval\" #nocomment\n"
68 "eight=eightval #nocomment\n"
69 "export nine=nineval\n"
75 r = load_env_file(NULL, t, NULL, &a);
79 log_info("Got: <%s>", *i);
81 assert_se(streq_ptr(a[0], "one=BAR"));
82 assert_se(streq_ptr(a[1], "two=bar"));
83 assert_se(streq_ptr(a[2], "three=333\nxxxx"));
84 assert_se(streq_ptr(a[3], "four=44\"44"));
85 assert_se(streq_ptr(a[4], "five=55\'55FIVEcinco"));
86 assert_se(streq_ptr(a[5], "six=seis sechs sis"));
87 assert_se(streq_ptr(a[6], "seven=sevenval#nocomment"));
88 assert_se(streq_ptr(a[7], "eight=eightval #nocomment"));
89 assert_se(streq_ptr(a[8], "export nine=nineval"));
90 assert_se(streq_ptr(a[9], "ten="));
91 assert_se(a[10] == NULL);
93 strv_env_clean_log(a, "test");
97 log_info("Got2: <%s>", *i);
98 assert_se(streq(*i, a[k++]));
111 "export nine", &nine,
117 log_info("one=[%s]", strna(one));
118 log_info("two=[%s]", strna(two));
119 log_info("three=[%s]", strna(three));
120 log_info("four=[%s]", strna(four));
121 log_info("five=[%s]", strna(five));
122 log_info("six=[%s]", strna(six));
123 log_info("seven=[%s]", strna(seven));
124 log_info("eight=[%s]", strna(eight));
125 log_info("export nine=[%s]", strna(nine));
126 log_info("ten=[%s]", strna(nine));
128 assert_se(streq(one, "BAR"));
129 assert_se(streq(two, "bar"));
130 assert_se(streq(three, "333\nxxxx"));
131 assert_se(streq(four, "44\"44"));
132 assert_se(streq(five, "55\'55FIVEcinco"));
133 assert_se(streq(six, "seis sechs sis"));
134 assert_se(streq(seven, "sevenval#nocomment"));
135 assert_se(streq(eight, "eightval #nocomment"));
136 assert_se(streq(nine, "nineval"));
137 assert_se(ten == NULL);
139 r = write_env_file(p, a);
142 r = load_env_file(NULL, p, NULL, &b);
149 static void test_parse_multiline_env_file(void) {
150 char t[] = "/tmp/test-fileio-in-XXXXXX",
151 p[] = "/tmp/test-fileio-out-XXXXXX";
154 _cleanup_strv_free_ char **a = NULL, **b = NULL;
157 fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
161 fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
182 r = load_env_file(NULL, t, NULL, &a);
186 log_info("Got: <%s>", *i);
188 assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR"));
189 assert_se(streq_ptr(a[1], "two=bar var\tgar"));
190 assert_se(streq_ptr(a[2], "tri=bar var \tgar "));
191 assert_se(a[3] == NULL);
193 r = write_env_file(p, a);
196 r = load_env_file(NULL, p, NULL, &b);
204 static void test_executable_is_script(void) {
205 char t[] = "/tmp/test-executable-XXXXXX";
210 fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
216 fputs("#! /bin/script -a -b \ngoo goo", f);
219 r = executable_is_script(t, &command);
221 assert_se(streq(command, "/bin/script"));
224 r = executable_is_script("/bin/sh", &command);
227 r = executable_is_script("/usr/bin/yum", &command);
228 assert_se(r > 0 || r == -ENOENT);
230 assert_se(startswith(command, "/"));
238 static void test_status_field(void) {
239 _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL;
240 unsigned long long total = 0, buffers = 0;
243 assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
245 assert_se(streq(t, "1"));
247 r = get_status_field("/proc/meminfo", "MemTotal:", &p);
251 assert_se(safe_atollu(p, &total) == 0);
254 r = get_status_field("/proc/meminfo", "\nBuffers:", &s);
258 assert_se(safe_atollu(s, &buffers) == 0);
262 assert(buffers < total);
264 /* Seccomp should be a good test for field full of zeros. */
265 r = get_status_field("/proc/meminfo", "\nSeccomp:", &z);
269 assert_se(safe_atollu(z, &buffers) == 0);
273 static void test_capeff(void) {
276 for (pid = 0; pid < 2; pid++) {
277 _cleanup_free_ char *capeff = NULL;
280 r = get_process_capeff(0, &capeff);
281 log_info("capeff: '%s' (r=%d)", capeff, r);
283 if (r == -ENOENT || r == -EPERM)
288 p = capeff[strspn(capeff, DIGITS "abcdefABCDEF")];
289 assert(!p || isspace(p));
293 static void test_write_string_stream(void) {
294 char fn[] = "/tmp/test-write_string_stream-XXXXXX";
295 _cleanup_fclose_ FILE *f = NULL;
299 fd = mkostemp_safe(fn, O_RDWR);
304 assert_se(write_string_stream(f, "boohoo") < 0);
306 f = fdopen(fd, "r+");
309 assert_se(write_string_stream(f, "boohoo") == 0);
312 assert_se(fgets(buf, sizeof(buf), f));
313 assert_se(streq(buf, "boohoo\n"));
318 static void test_write_string_file(void) {
319 char fn[] = "/tmp/test-write_string_file-XXXXXX";
323 fd = mkostemp_safe(fn, O_RDWR);
326 assert_se(write_string_file(fn, "boohoo") == 0);
328 assert_se(read(fd, buf, sizeof(buf)));
329 assert_se(streq(buf, "boohoo\n"));
334 static void test_sendfile_full(void) {
335 char in_fn[] = "/tmp/test-sendfile_full-XXXXXX";
336 char out_fn[] = "/tmp/test-sendfile_full-XXXXXX";
337 _cleanup_close_ int in_fd = -1;
339 char text[] = "boohoo\nfoo\n\tbar\n";
342 in_fd = mkostemp_safe(in_fn, O_RDWR);
343 assert_se(in_fd >= 0);
344 out_fd = mkostemp_safe(out_fn, O_RDWR);
345 assert_se(out_fd >= 0);
347 assert_se(write_string_file(in_fn, text) == 0);
348 assert_se(sendfile_full(out_fd, "/a/file/which/does/not/exist/i/guess") < 0);
349 assert_se(sendfile_full(out_fd, in_fn) == sizeof(text) - 1);
350 assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
352 assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
353 assert_se(streq(buf, text));
359 int main(int argc, char *argv[]) {
360 log_parse_environment();
363 test_parse_env_file();
364 test_parse_multiline_env_file();
365 test_executable_is_script();
368 test_write_string_stream();
369 test_write_string_file();
370 test_sendfile_full();