chiark / gitweb /
Prep v233.3: Add all possible coverage tests for elogind
[elogind.git] / src / test / test-proc-cmdline.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "proc-cmdline.h"
24 //#include "special.h"
25 #include "string-util.h"
26 #include "util.h"
27
28 static int obj;
29
30 static int parse_item(const char *key, const char *value, void *data) {
31         assert_se(key);
32         assert_se(data == &obj);
33
34         log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
35         return 0;
36 }
37
38 static void test_proc_cmdline_parse(void) {
39         assert_se(proc_cmdline_parse(parse_item, &obj, true) >= 0);
40 }
41
42 #if 0 /// UNNEEDED by elogind
43 static void test_runlevel_to_target(void) {
44         in_initrd_force(false);
45         assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
46         assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
47         assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
48         assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET));
49         assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL));
50
51         in_initrd_force(true);
52         assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
53         assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
54         assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
55         assert_se(streq_ptr(runlevel_to_target("3"), NULL));
56         assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET));
57 }
58
59 static void test_proc_cmdline_get_key(void) {
60         _cleanup_free_ char *value = NULL;
61
62         putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm");
63
64         assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL);
65         assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0);
66         assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL);
67         assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL);
68
69         assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
70         value = mfree(value);
71         assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
72         value = mfree(value);
73         assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux"));
74         value = mfree(value);
75         assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
76         value = mfree(value);
77         assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0);
78         assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
79
80         assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
81         value = mfree(value);
82         assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
83         value = mfree(value);
84         assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
85         value = mfree(value);
86         assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
87         value = mfree(value);
88         assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0);
89         assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
90
91         assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL);
92         assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
93         assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0);
94 }
95
96 static void test_proc_cmdline_get_bool(void) {
97         bool value = false;
98
99         putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep");
100
101         assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
102         assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
103         assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
104         assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
105         assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
106         assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
107         assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
108         assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false);
109         assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false);
110         assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false);
111         assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false);
112 }
113 #endif // 0
114
115 static void test_proc_cmdline_key_streq(void) {
116
117         assert_se(proc_cmdline_key_streq("", ""));
118         assert_se(proc_cmdline_key_streq("a", "a"));
119         assert_se(!proc_cmdline_key_streq("", "a"));
120         assert_se(!proc_cmdline_key_streq("a", ""));
121         assert_se(proc_cmdline_key_streq("a", "a"));
122         assert_se(!proc_cmdline_key_streq("a", "b"));
123         assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
124         assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
125         assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
126         assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
127         assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
128         assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
129 }
130
131 #if 0 /// UNNEEDED by elogind
132 static void test_proc_cmdline_key_startswith(void) {
133
134         assert_se(proc_cmdline_key_startswith("", ""));
135         assert_se(proc_cmdline_key_startswith("x", ""));
136         assert_se(!proc_cmdline_key_startswith("", "x"));
137         assert_se(proc_cmdline_key_startswith("x", "x"));
138         assert_se(!proc_cmdline_key_startswith("x", "y"));
139         assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
140         assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
141         assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
142         assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
143         assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
144         assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
145 }
146 #endif // 0
147
148 int main(void) {
149         log_parse_environment();
150         log_open();
151
152         test_proc_cmdline_parse();
153         test_proc_cmdline_key_streq();
154 #if 0 /// UNNEEDED by elogind
155         test_proc_cmdline_key_startswith();
156         test_proc_cmdline_get_key();
157         test_proc_cmdline_get_bool();
158         test_runlevel_to_target();
159 #endif // 0
160
161         return 0;
162 }