]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d376cbb7 RC |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include "alloc-util.h" | |
22 | #include "log.h" | |
23 | #include "macro.h" | |
24 | #include "proc-cmdline.h" | |
25 | #include "special.h" | |
26 | #include "string-util.h" | |
dcd61450 | 27 | #include "util.h" |
d376cbb7 | 28 | |
96287a49 ZJS |
29 | static int obj; |
30 | ||
31 | static int parse_item(const char *key, const char *value, void *data) { | |
d376cbb7 | 32 | assert_se(key); |
96287a49 | 33 | assert_se(data == &obj); |
d376cbb7 RC |
34 | |
35 | log_info("kernel cmdline option <%s> = <%s>", key, strna(value)); | |
36 | return 0; | |
37 | } | |
38 | ||
1d84ad94 LP |
39 | static void test_proc_cmdline_parse(void) { |
40 | assert_se(proc_cmdline_parse(parse_item, &obj, true) >= 0); | |
d376cbb7 RC |
41 | } |
42 | ||
43 | static void test_runlevel_to_target(void) { | |
dcd61450 | 44 | in_initrd_force(false); |
d376cbb7 RC |
45 | assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); |
46 | assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); | |
dcd61450 | 47 | assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); |
d376cbb7 | 48 | assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET)); |
dcd61450 IS |
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)); | |
d376cbb7 RC |
57 | } |
58 | ||
1d84ad94 LP |
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 | ||
114 | static void test_proc_cmdline_key_streq(void) { | |
115 | ||
116 | assert_se(proc_cmdline_key_streq("", "")); | |
117 | assert_se(proc_cmdline_key_streq("a", "a")); | |
118 | assert_se(!proc_cmdline_key_streq("", "a")); | |
119 | assert_se(!proc_cmdline_key_streq("a", "")); | |
120 | assert_se(proc_cmdline_key_streq("a", "a")); | |
121 | assert_se(!proc_cmdline_key_streq("a", "b")); | |
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-y_z")); | |
127 | assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z")); | |
128 | } | |
129 | ||
130 | static void test_proc_cmdline_key_startswith(void) { | |
131 | ||
132 | assert_se(proc_cmdline_key_startswith("", "")); | |
133 | assert_se(proc_cmdline_key_startswith("x", "")); | |
134 | assert_se(!proc_cmdline_key_startswith("", "x")); | |
135 | assert_se(proc_cmdline_key_startswith("x", "x")); | |
136 | assert_se(!proc_cmdline_key_startswith("x", "y")); | |
137 | assert_se(!proc_cmdline_key_startswith("foo-bar", "quux")); | |
138 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo")); | |
139 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar")); | |
140 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar")); | |
141 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo_")); | |
142 | assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx")); | |
143 | } | |
144 | ||
d376cbb7 RC |
145 | int main(void) { |
146 | log_parse_environment(); | |
147 | log_open(); | |
148 | ||
1d84ad94 LP |
149 | test_proc_cmdline_parse(); |
150 | test_proc_cmdline_key_streq(); | |
151 | test_proc_cmdline_key_startswith(); | |
152 | test_proc_cmdline_get_key(); | |
153 | test_proc_cmdline_get_bool(); | |
d376cbb7 RC |
154 | test_runlevel_to_target(); |
155 | ||
156 | return 0; | |
157 | } |