]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-log.c
udev: gracefully handle ENODEV or friends in opening device node
[thirdparty/systemd.git] / src / test / test-log.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "format-util.h"
4 #include "iovec-util.h"
5 #include "iovec-wrapper.h"
6 #include "log.h"
7 #include "log-context.h"
8 #include "process-util.h"
9 #include "string-util.h"
10 #include "strv.h"
11 #include "tests.h"
12
13 #define X10(x) x x x x x x x x x x
14 #define X100(x) X10(X10(x))
15 #define X1000(x) X100(X10(x))
16
17 static int fail_with_EINVAL(void) {
18 assert_return(false, -EINVAL);
19 return 0;
20 }
21
22 static void test_assert_return_is_critical(void) {
23 SAVE_ASSERT_RETURN_IS_CRITICAL;
24
25 log_set_assert_return_is_critical(false);
26 assert_se(fail_with_EINVAL() == -EINVAL);
27
28 log_set_assert_return_is_critical(true);
29 ASSERT_RETURN_IS_CRITICAL(false, assert_se(fail_with_EINVAL() == -EINVAL));
30 assert_se(log_get_assert_return_is_critical() == true);
31 ASSERT_RETURN_EXPECTED(assert_se(fail_with_EINVAL() == -EINVAL));
32 assert_se(log_get_assert_return_is_critical() == true);
33 ASSERT_RETURN_EXPECTED_SE(fail_with_EINVAL() == -EINVAL);
34 assert_se(log_get_assert_return_is_critical() == true);
35 }
36
37 static void test_file(void) {
38 log_info("__FILE__: %s", __FILE__);
39 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH);
40 log_info("PROJECT_FILE: %s", PROJECT_FILE);
41
42 assert_se(startswith(__FILE__, RELATIVE_SOURCE_PATH "/"));
43 }
44
45 static void test_log_struct(void) {
46 log_struct(LOG_INFO,
47 "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(),
48 "SERVICE=piepapo");
49
50 /* The same as above, just using LOG_MESSAGE() and LOG_ITEM(), which is generally recommended */
51 log_struct(LOG_INFO,
52 LOG_MESSAGE("Waldo PID="PID_FMT" (no errno)", getpid_cached()),
53 LOG_ITEM("SERVICE=piepapo"));
54
55 log_struct_errno(LOG_INFO, EILSEQ,
56 LOG_MESSAGE("Waldo PID="PID_FMT": %m (normal)", getpid_cached()),
57 LOG_ITEM("SERVICE=piepapo"));
58
59 log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ),
60 LOG_MESSAGE("Waldo PID="PID_FMT": %m (synthetic)", getpid_cached()),
61 LOG_ITEM("SERVICE=piepapo"));
62
63 log_struct(LOG_INFO,
64 LOG_MESSAGE("Foobar PID="PID_FMT, getpid_cached()),
65 LOG_ITEM("FORMAT_STR_TEST=1=%i A=%c 2=%hi 3=%li 4=%lli 1=%p foo=%s 2.5=%g 3.5=%g 4.5=%Lg",
66 (int) 1, 'A', (short) 2, (long) 3, (long long) 4, (void*) 1, "foo", (float) 2.5f, (double) 3.5, (long double) 4.5),
67 LOG_ITEM("SUFFIX=GOT IT"));
68 }
69
70 static void test_long_lines(void) {
71 log_object_internal(LOG_NOTICE,
72 EUCLEAN,
73 X1000("abcd_") ".txt",
74 1000000,
75 X1000("fff") "unc",
76 "OBJECT=",
77 X1000("obj_") "ect",
78 "EXTRA=",
79 X1000("ext_") "tra",
80 "asdfasdf %s asdfasdfa", "foobar");
81 }
82
83 static void test_log_syntax(void) {
84 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, EINVAL, "EINVAL: %s: %m", "hogehoge") == -EINVAL);
85 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, -ENOENT, "ENOENT: %s: %m", "hogehoge") == -ENOENT);
86 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY);
87 }
88
89 static void test_log_context(void) {
90 {
91 char **strv = STRV_MAKE("FIRST=abc", "SECOND=qrs");
92
93 LOG_CONTEXT_PUSH("THIRD=pfs");
94 LOG_CONTEXT_PUSH("FOURTH=def");
95 LOG_CONTEXT_PUSH_STRV(strv);
96 LOG_CONTEXT_PUSH_STRV(strv);
97
98 /* Test that the log context was set up correctly. The strv we pushed twice should only
99 * result in one log context which is reused. */
100 assert_se(log_context_num_contexts() == 3);
101 assert_se(log_context_num_fields() == 4);
102
103 /* Test that everything still works with modifications to the log context. */
104 test_log_struct();
105 test_long_lines();
106 test_log_syntax();
107
108 {
109 LOG_CONTEXT_PUSH("FIFTH=123");
110 LOG_CONTEXT_PUSH_STRV(strv);
111
112 /* Check that our nested fields got added correctly. */
113 assert_se(log_context_num_contexts() == 4);
114 assert_se(log_context_num_fields() == 5);
115
116 /* Test that everything still works in a nested block. */
117 test_log_struct();
118 test_long_lines();
119 test_log_syntax();
120 }
121
122 /* Check that only the fields from the nested block got removed. */
123 assert_se(log_context_num_contexts() == 3);
124 assert_se(log_context_num_fields() == 4);
125 }
126
127 assert_se(log_context_num_contexts() == 0);
128 assert_se(log_context_num_fields() == 0);
129
130 {
131 _cleanup_(log_context_unrefp) LogContext *ctx = NULL;
132
133 char **strv = STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
134 assert_se(ctx = log_context_new_strv(strv, /*owned=*/ false));
135
136 assert_se(log_context_num_contexts() == 1);
137 assert_se(log_context_num_fields() == 2);
138
139 /* Test that everything still works with a manually configured log context. */
140 test_log_struct();
141 test_long_lines();
142 test_log_syntax();
143 }
144
145 {
146 char **strv = NULL;
147
148 assert_se(strv = strv_new("ABC", "DEF"));
149 LOG_CONTEXT_CONSUME_STRV(strv);
150
151 assert_se(log_context_num_contexts() == 1);
152 assert_se(log_context_num_fields() == 2);
153 }
154
155 {
156 /* Test that everything still works with a mixed strv and iov. */
157 struct iovec iov[] = {
158 IOVEC_MAKE_STRING("ABC=def"),
159 IOVEC_MAKE_STRING("GHI=jkl"),
160 };
161 _cleanup_free_ struct iovec_wrapper *iovw = iovw_new();
162 assert_se(iovw);
163 assert_se(iovw_consume(iovw, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
164
165 LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov));
166 LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov));
167 LOG_CONTEXT_CONSUME_IOV(iovw->iovec, iovw->count);
168 LOG_CONTEXT_PUSH("STU=vwx");
169
170 assert_se(log_context_num_contexts() == 3);
171 assert_se(log_context_num_fields() == 4);
172
173 test_log_struct();
174 test_long_lines();
175 test_log_syntax();
176 }
177
178 {
179 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
180 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
181 assert_se(log_context_num_contexts() == 1);
182 assert_se(log_context_num_fields() == 1);
183
184 test_log_struct();
185 test_long_lines();
186 test_log_syntax();
187 }
188
189 assert_se(log_context_num_contexts() == 0);
190 assert_se(log_context_num_fields() == 0);
191 }
192
193 static void test_log_prefix(void) {
194 {
195 LOG_SET_PREFIX("ABC");
196
197 test_log_struct();
198 test_long_lines();
199 test_log_syntax();
200
201 {
202 LOG_SET_PREFIX("QED");
203
204 test_log_struct();
205 test_long_lines();
206 test_log_syntax();
207 }
208
209 test_log_struct();
210 test_long_lines();
211 test_log_syntax();
212 }
213
214 test_log_struct();
215 test_long_lines();
216 test_log_syntax();
217 }
218
219 int main(int argc, char* argv[]) {
220 test_setup_logging(LOG_DEBUG);
221
222 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
223 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(-EINVAL)));
224 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
225 assert_cc(!IS_SYNTHETIC_ERRNO(-EINVAL));
226 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
227 assert_cc(!IS_SYNTHETIC_ERRNO(0));
228 ASSERT_EQ(ERRNO_VALUE(EINVAL), EINVAL);
229 ASSERT_EQ(ERRNO_VALUE(SYNTHETIC_ERRNO(-EINVAL)), EINVAL);
230
231 test_assert_return_is_critical();
232 test_file();
233
234 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN);
235
236 for (int target = 0; target < _LOG_TARGET_MAX; target++) {
237 log_set_target(target);
238 log_open();
239
240 test_log_struct();
241 test_long_lines();
242 test_log_syntax();
243 test_log_context();
244 test_log_prefix();
245 }
246
247 return 0;
248 }