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