]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
877d54e9 LP |
2 | |
3 | #include <stddef.h> | |
4 | #include <unistd.h> | |
5 | ||
f97b34a6 | 6 | #include "format-util.h" |
2461943b | 7 | #include "io-util.h" |
bd1ae178 | 8 | #include "iovec-util.h" |
ea1f3814 | 9 | #include "iovec-wrapper.h" |
877d54e9 | 10 | #include "log.h" |
dccca82b | 11 | #include "process-util.h" |
a0b15b41 | 12 | #include "string-util.h" |
7c7a9138 | 13 | #include "strv.h" |
965040d8 | 14 | #include "tests.h" |
877d54e9 | 15 | |
52d86690 ZJS |
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 | ||
f8e6f4aa ZJS |
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)) | |
877d54e9 | 24 | |
8161f608 YW |
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 | ||
a0b15b41 ZJS |
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 | ||
f21b863e | 50 | assert_se(startswith(__FILE__, RELATIVE_SOURCE_PATH "/")); |
a0b15b41 ZJS |
51 | } |
52 | ||
52d86690 | 53 | static void test_log_struct(void) { |
877d54e9 | 54 | log_struct(LOG_INFO, |
52d86690 | 55 | "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(), |
a1230ff9 | 56 | "SERVICE=piepapo"); |
877d54e9 | 57 | |
92663a5e ZJS |
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()), | |
52d86690 ZJS |
61 | "SERVICE=piepapo"); |
62 | ||
92663a5e ZJS |
63 | log_struct_errno(LOG_INFO, EILSEQ, |
64 | LOG_MESSAGE("Waldo PID="PID_FMT": %m (normal)", getpid_cached()), | |
65 | "SERVICE=piepapo"); | |
66 | ||
52d86690 | 67 | log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ), |
92663a5e ZJS |
68 | LOG_MESSAGE("Waldo PID="PID_FMT": %m (synthetic)", getpid_cached()), |
69 | "SERVICE=piepapo"); | |
877d54e9 | 70 | |
963ddb91 | 71 | log_struct(LOG_INFO, |
92663a5e | 72 | LOG_MESSAGE("Foobar PID="PID_FMT, getpid_cached()), |
963ddb91 LP |
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, | |
a1230ff9 | 75 | "SUFFIX=GOT IT"); |
f8e6f4aa ZJS |
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 | ||
59c1546b YW |
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 | ||
7c7a9138 DDM |
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 | ||
88335453 DDM |
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); | |
7c7a9138 DDM |
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. */ | |
88335453 DDM |
121 | assert_se(log_context_num_contexts() == 4); |
122 | assert_se(log_context_num_fields() == 5); | |
7c7a9138 DDM |
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. */ | |
88335453 DDM |
131 | assert_se(log_context_num_contexts() == 3); |
132 | assert_se(log_context_num_fields() == 4); | |
7c7a9138 DDM |
133 | } |
134 | ||
135 | assert_se(log_context_num_contexts() == 0); | |
136 | assert_se(log_context_num_fields() == 0); | |
137 | ||
138 | { | |
88335453 | 139 | _cleanup_(log_context_unrefp) LogContext *ctx = NULL; |
7c7a9138 DDM |
140 | |
141 | char **strv = STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP"); | |
81b3565e | 142 | assert_se(ctx = log_context_new_strv(strv, /*owned=*/ false)); |
7c7a9138 DDM |
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 | ||
2461943b LB |
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 | ||
88335453 | 173 | LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov)); |
2461943b LB |
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 | ||
81b3565e DDM |
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 | ||
7c7a9138 DDM |
197 | assert_se(log_context_num_contexts() == 0); |
198 | assert_se(log_context_num_fields() == 0); | |
199 | } | |
200 | ||
ee2975a9 DDM |
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 | ||
f8e6f4aa | 227 | int main(int argc, char* argv[]) { |
965040d8 YW |
228 | test_setup_logging(LOG_DEBUG); |
229 | ||
8161f608 | 230 | test_assert_return_is_critical(); |
a0b15b41 ZJS |
231 | test_file(); |
232 | ||
169d980b ZJS |
233 | assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN); |
234 | ||
235 | for (int target = 0; target < _LOG_TARGET_MAX; target++) { | |
f8e6f4aa ZJS |
236 | log_set_target(target); |
237 | log_open(); | |
238 | ||
52d86690 | 239 | test_log_struct(); |
f8e6f4aa | 240 | test_long_lines(); |
59c1546b | 241 | test_log_syntax(); |
7c7a9138 | 242 | test_log_context(); |
ee2975a9 | 243 | test_log_prefix(); |
f8e6f4aa | 244 | } |
963ddb91 | 245 | |
877d54e9 LP |
246 | return 0; |
247 | } |