]>
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 static void test_parse_env_file(void) {
32 char t
[] = "/tmp/test-fileio-in-XXXXXX",
33 p
[] = "/tmp/test-fileio-out-XXXXXX";
36 _cleanup_free_
char *one
= NULL
, *two
= NULL
, *three
= NULL
, *four
= NULL
, *five
= NULL
,
37 *six
= NULL
, *seven
= NULL
, *eight
= NULL
, *nine
= NULL
, *ten
= NULL
;
38 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
44 fd
= mkostemp(t
, O_CLOEXEC
);
56 "invalid line #comment\n"
59 "four = \'44\\\"44\'\n"
60 "five = \'55\\\'55\' \"FIVE\" cinco \n"
61 "six = seis sechs\\\n"
63 "seven=\"sevenval\" #nocomment\n"
64 "eight=eightval #nocomment\n"
65 "export nine=nineval\n"
71 r
= load_env_file(t
, NULL
, &a
);
75 log_info("Got: <%s>", *i
);
77 assert_se(streq(a
[0], "one=BAR"));
78 assert_se(streq(a
[1], "two=bar"));
79 assert_se(streq(a
[2], "three=333\nxxxx"));
80 assert_se(streq(a
[3], "four=44\"44"));
81 assert_se(streq(a
[4], "five=55\'55FIVEcinco"));
82 assert_se(streq(a
[5], "six=seis sechs sis"));
83 assert_se(streq(a
[6], "seven=sevenval#nocomment"));
84 assert_se(streq(a
[7], "eight=eightval #nocomment"));
85 assert_se(streq(a
[8], "export nine=nineval"));
86 assert_se(streq(a
[9], "ten="));
87 assert_se(a
[10] == NULL
);
89 strv_env_clean_log(a
, "test");
93 log_info("Got2: <%s>", *i
);
94 assert_se(streq(*i
, a
[k
++]));
107 "export nine", &nine
,
113 log_info("one=[%s]", strna(one
));
114 log_info("two=[%s]", strna(two
));
115 log_info("three=[%s]", strna(three
));
116 log_info("four=[%s]", strna(four
));
117 log_info("five=[%s]", strna(five
));
118 log_info("six=[%s]", strna(six
));
119 log_info("seven=[%s]", strna(seven
));
120 log_info("eight=[%s]", strna(eight
));
121 log_info("export nine=[%s]", strna(nine
));
122 log_info("ten=[%s]", strna(nine
));
124 assert_se(streq(one
, "BAR"));
125 assert_se(streq(two
, "bar"));
126 assert_se(streq(three
, "333\nxxxx"));
127 assert_se(streq(four
, "44\"44"));
128 assert_se(streq(five
, "55\'55FIVEcinco"));
129 assert_se(streq(six
, "seis sechs sis"));
130 assert_se(streq(seven
, "sevenval#nocomment"));
131 assert_se(streq(eight
, "eightval #nocomment"));
132 assert_se(streq(nine
, "nineval"));
133 assert_se(ten
== NULL
);
135 r
= write_env_file(p
, a
);
138 r
= load_env_file(p
, NULL
, &b
);
145 static void test_parse_multiline_env_file(void) {
146 char t
[] = "/tmp/test-fileio-in-XXXXXX",
147 p
[] = "/tmp/test-fileio-out-XXXXXX";
150 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
153 assert_se(mktemp(p
));
155 fd
= mkostemp(t
, O_CLOEXEC
);
176 r
= load_env_file(t
, NULL
, &a
);
180 log_info("Got: <%s>", *i
);
182 assert_se(streq(a
[0], "one=BAR VAR\tGAR"));
183 assert_se(streq(a
[1], "two=bar var\tgar"));
184 assert_se(streq(a
[2], "tri=bar var \tgar "));
185 assert_se(a
[3] == NULL
);
187 r
= write_env_file(p
, a
);
190 r
= load_env_file(p
, NULL
, &b
);
198 static void test_executable_is_script(void) {
199 char t
[] = "/tmp/test-executable-XXXXXX";
204 fd
= mkostemp(t
, O_CLOEXEC
);
210 fputs("#! /bin/script -a -b \ngoo goo", f
);
213 r
= executable_is_script(t
, &command
);
215 assert_se(streq(command
, "/bin/script"));
218 r
= executable_is_script("/bin/sh", &command
);
221 r
= executable_is_script("/usr/bin/yum", &command
);
222 assert_se(r
> 0 || r
== -ENOENT
);
224 assert_se(startswith(command
, "/"));
232 static void test_status_field(void) {
233 _cleanup_free_
char *t
= NULL
, *p
= NULL
, *s
= NULL
;
234 unsigned long long total
, buffers
;
236 assert_se(get_status_field("/proc/self/status", "\nThreads:", &t
) == 0);
238 assert_se(streq(t
, "1"));
240 assert_se(get_status_field("/proc/meminfo", "MemTotal:", &p
) == 0);
242 assert_se(safe_atollu(p
, &total
) == 0);
244 assert_se(get_status_field("/proc/meminfo", "\nBuffers:", &s
) == 0);
246 assert_se(safe_atollu(s
, &buffers
) == 0);
248 assert(buffers
< total
);
251 int main(int argc
, char *argv
[]) {
252 test_parse_env_file();
253 test_parse_multiline_env_file();
254 test_executable_is_script();