]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-proc-cmdline.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.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 static void test_runlevel_to_target(void) {
43 in_initrd_force(false);
44 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
45 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
46 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
47 assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET));
48 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL));
49
50 in_initrd_force(true);
51 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
52 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
53 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
54 assert_se(streq_ptr(runlevel_to_target("3"), NULL));
55 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET));
56 }
57
58 static void test_proc_cmdline_get_key(void) {
59 _cleanup_free_ char *value = NULL;
60
61 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm");
62
63 assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL);
64 assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0);
65 assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL);
66 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL);
67
68 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
69 value = mfree(value);
70 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
71 value = mfree(value);
72 assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux"));
73 value = mfree(value);
74 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
75 value = mfree(value);
76 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0);
77 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
78
79 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
80 value = mfree(value);
81 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
82 value = mfree(value);
83 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
84 value = mfree(value);
85 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
86 value = mfree(value);
87 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0);
88 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
89
90 assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL);
91 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
92 assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0);
93 }
94
95 static void test_proc_cmdline_get_bool(void) {
96 bool value = false;
97
98 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep");
99
100 assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
101 assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
102 assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
103 assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
104 assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
105 assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
106 assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
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("quux", &value) == -EINVAL && value == false);
111 }
112
113 static void test_proc_cmdline_key_streq(void) {
114
115 assert_se(proc_cmdline_key_streq("", ""));
116 assert_se(proc_cmdline_key_streq("a", "a"));
117 assert_se(!proc_cmdline_key_streq("", "a"));
118 assert_se(!proc_cmdline_key_streq("a", ""));
119 assert_se(proc_cmdline_key_streq("a", "a"));
120 assert_se(!proc_cmdline_key_streq("a", "b"));
121 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
122 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
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-z_z"));
127 }
128
129 static void test_proc_cmdline_key_startswith(void) {
130
131 assert_se(proc_cmdline_key_startswith("", ""));
132 assert_se(proc_cmdline_key_startswith("x", ""));
133 assert_se(!proc_cmdline_key_startswith("", "x"));
134 assert_se(proc_cmdline_key_startswith("x", "x"));
135 assert_se(!proc_cmdline_key_startswith("x", "y"));
136 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
137 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
138 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
139 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
140 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
141 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
142 }
143
144 int main(void) {
145 log_parse_environment();
146 log_open();
147
148 test_proc_cmdline_parse();
149 test_proc_cmdline_key_streq();
150 test_proc_cmdline_key_startswith();
151 test_proc_cmdline_get_key();
152 test_proc_cmdline_get_bool();
153 test_runlevel_to_target();
154
155 return 0;
156 }