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;
46 fd = mkostemp(t, O_CLOEXEC);
58 "invalid line #comment\n"
61 "four = \'44\\\"44\'\n"
62 "five = \'55\\\'55\' \"FIVE\" cinco \n"
63 "six = seis sechs\\\n"
65 "seven=\"sevenval\" #nocomment\n"
66 "eight=eightval #nocomment\n"
67 "export nine=nineval\n"
73 r = load_env_file(t, NULL, &a);
77 log_info("Got: <%s>", *i);
79 assert_se(streq(a[0], "one=BAR"));
80 assert_se(streq(a[1], "two=bar"));
81 assert_se(streq(a[2], "three=333\nxxxx"));
82 assert_se(streq(a[3], "four=44\"44"));
83 assert_se(streq(a[4], "five=55\'55FIVEcinco"));
84 assert_se(streq(a[5], "six=seis sechs sis"));
85 assert_se(streq(a[6], "seven=sevenval#nocomment"));
86 assert_se(streq(a[7], "eight=eightval #nocomment"));
87 assert_se(streq(a[8], "export nine=nineval"));
88 assert_se(streq(a[9], "ten="));
89 assert_se(a[10] == NULL);
91 strv_env_clean_log(a, "test");
95 log_info("Got2: <%s>", *i);
96 assert_se(streq(*i, a[k++]));
109 "export nine", &nine,
115 log_info("one=[%s]", strna(one));
116 log_info("two=[%s]", strna(two));
117 log_info("three=[%s]", strna(three));
118 log_info("four=[%s]", strna(four));
119 log_info("five=[%s]", strna(five));
120 log_info("six=[%s]", strna(six));
121 log_info("seven=[%s]", strna(seven));
122 log_info("eight=[%s]", strna(eight));
123 log_info("export nine=[%s]", strna(nine));
124 log_info("ten=[%s]", strna(nine));
126 assert_se(streq(one, "BAR"));
127 assert_se(streq(two, "bar"));
128 assert_se(streq(three, "333\nxxxx"));
129 assert_se(streq(four, "44\"44"));
130 assert_se(streq(five, "55\'55FIVEcinco"));
131 assert_se(streq(six, "seis sechs sis"));
132 assert_se(streq(seven, "sevenval#nocomment"));
133 assert_se(streq(eight, "eightval #nocomment"));
134 assert_se(streq(nine, "nineval"));
135 assert_se(ten == NULL);
137 r = write_env_file(p, a);
140 r = load_env_file(p, NULL, &b);
147 static void test_parse_multiline_env_file(void) {
148 char t[] = "/tmp/test-fileio-in-XXXXXX",
149 p[] = "/tmp/test-fileio-out-XXXXXX";
152 _cleanup_strv_free_ char **a = NULL, **b = NULL;
155 assert_se(mktemp(p));
157 fd = mkostemp(t, O_CLOEXEC);
178 r = load_env_file(t, NULL, &a);
182 log_info("Got: <%s>", *i);
184 assert_se(streq(a[0], "one=BAR VAR\tGAR"));
185 assert_se(streq(a[1], "two=bar var\tgar"));
186 assert_se(streq(a[2], "tri=bar var \tgar "));
187 assert_se(a[3] == NULL);
189 r = write_env_file(p, a);
192 r = load_env_file(p, NULL, &b);
200 static void test_executable_is_script(void) {
201 char t[] = "/tmp/test-executable-XXXXXX";
206 fd = mkostemp(t, O_CLOEXEC);
212 fputs("#! /bin/script -a -b \ngoo goo", f);
215 r = executable_is_script(t, &command);
217 assert_se(streq(command, "/bin/script"));
220 r = executable_is_script("/bin/sh", &command);
223 r = executable_is_script("/usr/bin/yum", &command);
224 assert_se(r > 0 || r == -ENOENT);
226 assert_se(startswith(command, "/"));
234 static void test_status_field(void) {
235 _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL;
236 unsigned long long total = 0, buffers = 0;
239 assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
241 assert_se(streq(t, "1"));
243 r = get_status_field("/proc/meminfo", "MemTotal:", &p);
247 assert_se(safe_atollu(p, &total) == 0);
250 r = get_status_field("/proc/meminfo", "\nBuffers:", &s);
254 assert_se(safe_atollu(s, &buffers) == 0);
258 assert(buffers < total);
260 /* Seccomp should be a good test for field full of zeros. */
261 r = get_status_field("/proc/meminfo", "\nSeccomp:", &z);
265 assert_se(safe_atollu(z, &buffers) == 0);
269 static void test_capeff(void) {
272 for (pid = 0; pid < 2; pid++) {
273 _cleanup_free_ char *capeff = NULL;
276 r = get_process_capeff(0, &capeff);
277 log_info("capeff: '%s' (r=%d)", capeff, r);
279 if (r == -ENOENT || r == -EPERM)
284 p = capeff[strspn(capeff, DIGITS "abcdefABCDEF")];
285 assert(!p || isspace(p));
289 int main(int argc, char *argv[]) {
290 log_parse_environment();
293 test_parse_env_file();
294 test_parse_multiline_env_file();
295 test_executable_is_script();