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