blob: 82234a69942dcd95abda102326c83cca25eaf6d1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass14c8a062017-12-04 13:48:27 -07002/*
3 * Logging support test program
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glass14c8a062017-12-04 13:48:27 -07007 */
8
9#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Sean Andersonc1821822020-10-27 19:55:27 -040012#include <test/log.h>
13#include <test/ut.h>
14
15DECLARE_GLOBAL_DATA_PTR;
Simon Glass14c8a062017-12-04 13:48:27 -070016
17/* emit some sample log records in different ways, for testing */
Patrick Delaunaycc034972020-11-27 11:20:53 +010018static int do_log_run(struct unit_test_state *uts, int cat, const char *file)
Simon Glass14c8a062017-12-04 13:48:27 -070019{
20 int i;
Patrick Delaunaycc034972020-11-27 11:20:53 +010021 int ret, expected_ret;
22
23 if (gd->flags & GD_FLG_LOG_READY)
24 expected_ret = 0;
25 else
26 expected_ret = -ENOSYS;
Simon Glass14c8a062017-12-04 13:48:27 -070027
Sean Andersonc1821822020-10-27 19:55:27 -040028 gd->log_fmt = LOGF_TEST;
Simon Glass14c8a062017-12-04 13:48:27 -070029 debug("debug\n");
30 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
31 log(cat, i, "log %d\n", i);
Patrick Delaunaycc034972020-11-27 11:20:53 +010032 ret = _log(log_uc_cat(cat), i, file, 100 + i,
33 "func", "_log %d\n", i);
34 ut_asserteq(ret, expected_ret);
Simon Glass14c8a062017-12-04 13:48:27 -070035 }
Patrick Delaunay529e46f2020-11-27 11:20:51 +010036 /* test with LOGL_COUNT flag */
37 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
Patrick Delaunaycc034972020-11-27 11:20:53 +010038 ret = _log(log_uc_cat(cat), i | LOGL_FORCE_DEBUG, file, 100 + i,
39 "func", "_log force %d\n", i);
40 ut_asserteq(ret, expected_ret);
Patrick Delaunay529e46f2020-11-27 11:20:51 +010041 }
Simon Glass14c8a062017-12-04 13:48:27 -070042
Sean Andersonc1821822020-10-27 19:55:27 -040043 gd->log_fmt = log_get_default_format();
Simon Glass14c8a062017-12-04 13:48:27 -070044 return 0;
45}
46
Patrick Delaunaycc034972020-11-27 11:20:53 +010047#define log_run_cat(cat) do_log_run(uts, cat, "file")
48#define log_run_file(file) do_log_run(uts, UCLASS_SPI, file)
49#define log_run() do_log_run(uts, UCLASS_SPI, "file")
Simon Glass14c8a062017-12-04 13:48:27 -070050
Sean Andersonc1821822020-10-27 19:55:27 -040051#define EXPECT_LOG BIT(0)
52#define EXPECT_DIRECT BIT(1)
53#define EXPECT_EXTRA BIT(2)
Patrick Delaunay529e46f2020-11-27 11:20:51 +010054#define EXPECT_FORCE BIT(3)
Patrick Delaunay293798c2020-11-27 11:20:58 +010055#define EXPECT_DEBUG BIT(4)
Simon Glass14c8a062017-12-04 13:48:27 -070056
Sean Andersonc1821822020-10-27 19:55:27 -040057static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
58 int max)
59{
60 int i;
Simon Glass14c8a062017-12-04 13:48:27 -070061
Sean Andersonc1821822020-10-27 19:55:27 -040062 for (i = min; i <= max; i++) {
63 if (flags & EXPECT_LOG)
64 ut_assert_nextline("do_log_run() log %d", i);
65 if (flags & EXPECT_DIRECT)
66 ut_assert_nextline("func() _log %d", i);
Patrick Delaunay293798c2020-11-27 11:20:58 +010067 if (flags & EXPECT_DEBUG) {
68 ut_assert_nextline("log %d", i);
69 ut_assert_nextline("_log %d", i);
70 }
Simon Glass14c8a062017-12-04 13:48:27 -070071 }
Sean Andersonc1821822020-10-27 19:55:27 -040072 if (flags & EXPECT_EXTRA)
73 for (; i <= LOGL_MAX ; i++)
74 ut_assert_nextline("func() _log %d", i);
Simon Glass14c8a062017-12-04 13:48:27 -070075
Patrick Delaunay529e46f2020-11-27 11:20:51 +010076 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
77 if (flags & EXPECT_FORCE)
78 ut_assert_nextline("func() _log force %d", i);
Patrick Delaunay293798c2020-11-27 11:20:58 +010079 if (flags & EXPECT_DEBUG)
80 ut_assert_nextline("_log force %d", i);
Patrick Delaunay529e46f2020-11-27 11:20:51 +010081 }
82
Sean Andersonc1821822020-10-27 19:55:27 -040083 ut_assert_console_end();
84 return 0;
85}
Simon Glass14c8a062017-12-04 13:48:27 -070086
Sean Andersonc1821822020-10-27 19:55:27 -040087#define check_log_entries_flags_levels(flags, min, max) do {\
88 int ret = do_check_log_entries(uts, flags, min, max); \
89 if (ret) \
90 return ret; \
91} while (0)
Simon Glass14c8a062017-12-04 13:48:27 -070092
Sean Andersonc1821822020-10-27 19:55:27 -040093#define check_log_entries_flags(flags) \
94 check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL)
Patrick Delaunay529e46f2020-11-27 11:20:51 +010095#define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE)
Sean Andersonc1821822020-10-27 19:55:27 -040096#define check_log_entries_extra() \
Patrick Delaunay529e46f2020-11-27 11:20:51 +010097 check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE)
98#define check_log_entries_none() check_log_entries_flags(EXPECT_FORCE)
Sean Andersonc1821822020-10-27 19:55:27 -040099
100/* Check a category filter using the first category */
Sean Andersonba956672020-10-27 19:55:28 -0400101int log_test_cat_allow(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400102{
103 enum log_category_t cat_list[] = {
104 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
105 LOGC_NONE, LOGC_END
106 };
107 int filt;
108
109 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
110 ut_assert(filt >= 0);
111
112 ut_assertok(console_record_reset_enable());
113 log_run_cat(UCLASS_MMC);
114 check_log_entries_extra();
115
116 ut_assertok(console_record_reset_enable());
117 log_run_cat(UCLASS_SPI);
118 check_log_entries_extra();
Simon Glass14c8a062017-12-04 13:48:27 -0700119
Sean Andersonc1821822020-10-27 19:55:27 -0400120 ut_assertok(log_remove_filter("console", filt));
Simon Glass14c8a062017-12-04 13:48:27 -0700121 return 0;
122}
Sean Andersonba956672020-10-27 19:55:28 -0400123LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
Simon Glass14c8a062017-12-04 13:48:27 -0700124
Sean Andersonc1821822020-10-27 19:55:27 -0400125/* Check a category filter that should block log entries */
Sean Andersonba956672020-10-27 19:55:28 -0400126int log_test_cat_deny_implicit(struct unit_test_state *uts)
Simon Glass14c8a062017-12-04 13:48:27 -0700127{
Sean Andersonc1821822020-10-27 19:55:27 -0400128 enum log_category_t cat_list[] = {
129 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
130 };
131 int filt;
132
133 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
134 ut_assert(filt >= 0);
135
136 ut_assertok(console_record_reset_enable());
137 log_run_cat(UCLASS_SPI);
138 check_log_entries_none();
139
140 ut_assertok(log_remove_filter("console", filt));
141 return 0;
142}
Sean Andersonba956672020-10-27 19:55:28 -0400143LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400144
145/* Check passing and failing file filters */
Sean Andersonba956672020-10-27 19:55:28 -0400146int log_test_file(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400147{
148 int filt;
149
150 filt = log_add_filter("console", NULL, LOGL_MAX, "file");
151 ut_assert(filt >= 0);
152
153 ut_assertok(console_record_reset_enable());
154 log_run_file("file");
Patrick Delaunay529e46f2020-11-27 11:20:51 +0100155 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Andersonc1821822020-10-27 19:55:27 -0400156
157 ut_assertok(console_record_reset_enable());
158 log_run_file("file2");
159 check_log_entries_none();
160
161 ut_assertok(log_remove_filter("console", filt));
162 return 0;
163}
Sean Andersonba956672020-10-27 19:55:28 -0400164LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400165
166/* Check a passing file filter (second in list) */
Sean Andersonba956672020-10-27 19:55:28 -0400167int log_test_file_second(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400168{
169 int filt;
170
171 filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
172 ut_assert(filt >= 0);
173
174 ut_assertok(console_record_reset_enable());
175 log_run_file("file2");
Patrick Delaunay529e46f2020-11-27 11:20:51 +0100176 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Andersonc1821822020-10-27 19:55:27 -0400177
178 ut_assertok(log_remove_filter("console", filt));
179 return 0;
180}
Sean Andersonba956672020-10-27 19:55:28 -0400181LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400182
183/* Check a passing file filter (middle of list) */
Sean Andersonba956672020-10-27 19:55:28 -0400184int log_test_file_mid(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400185{
186 int filt;
187
188 filt = log_add_filter("console", NULL, LOGL_MAX,
189 "file,file2,log/log_test.c");
190 ut_assert(filt >= 0);
191
192 ut_assertok(console_record_reset_enable());
193 log_run_file("file2");
194 check_log_entries_extra();
195
196 ut_assertok(log_remove_filter("console", filt));
197 return 0;
198}
Sean Andersonba956672020-10-27 19:55:28 -0400199LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400200
201/* Check a log level filter */
Sean Andersonba956672020-10-27 19:55:28 -0400202int log_test_level(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400203{
204 int filt;
205
206 filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
207 ut_assert(filt >= 0);
208
209 ut_assertok(console_record_reset_enable());
210 log_run();
Patrick Delaunay529e46f2020-11-27 11:20:51 +0100211 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
212 LOGL_FIRST, LOGL_WARNING);
Sean Andersonc1821822020-10-27 19:55:27 -0400213
214 ut_assertok(log_remove_filter("console", filt));
215 return 0;
216}
Sean Andersonba956672020-10-27 19:55:28 -0400217LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400218
219/* Check two filters, one of which passes everything */
Sean Andersonba956672020-10-27 19:55:28 -0400220int log_test_double(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400221{
222 int filt1, filt2;
223
224 filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL);
225 ut_assert(filt1 >= 0);
226 filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL);
227 ut_assert(filt2 >= 0);
228
229 ut_assertok(console_record_reset_enable());
230 log_run();
231 check_log_entries_extra();
232
233 ut_assertok(log_remove_filter("console", filt1));
234 ut_assertok(log_remove_filter("console", filt2));
235 return 0;
236}
Sean Andersonba956672020-10-27 19:55:28 -0400237LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400238
239/* Check three filters, which together pass everything */
Sean Andersonba956672020-10-27 19:55:28 -0400240int log_test_triple(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400241{
242 int filt1, filt2, filt3;
243
244 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)");
245 ut_assert(filt1 >= 0);
246 filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2");
247 ut_assert(filt2 >= 0);
248 filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c");
249 ut_assert(filt3 >= 0);
250
251 ut_assertok(console_record_reset_enable());
252 log_run_file("file2");
253 check_log_entries_extra();
254
255 ut_assertok(log_remove_filter("console", filt1));
256 ut_assertok(log_remove_filter("console", filt2));
257 ut_assertok(log_remove_filter("console", filt3));
258 return 0;
259}
Sean Andersonba956672020-10-27 19:55:28 -0400260LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400261
Sean Andersonba956672020-10-27 19:55:28 -0400262int do_log_test_helpers(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400263{
264 int i;
265
266 ut_assertok(console_record_reset_enable());
267 log_err("level %d\n", LOGL_EMERG);
268 log_err("level %d\n", LOGL_ALERT);
269 log_err("level %d\n", LOGL_CRIT);
270 log_err("level %d\n", LOGL_ERR);
271 log_warning("level %d\n", LOGL_WARNING);
272 log_notice("level %d\n", LOGL_NOTICE);
273 log_info("level %d\n", LOGL_INFO);
274 log_debug("level %d\n", LOGL_DEBUG);
275 log_content("level %d\n", LOGL_DEBUG_CONTENT);
276 log_io("level %d\n", LOGL_DEBUG_IO);
277
278 for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
279 ut_assert_nextline("%s() level %d", __func__, i);
280 ut_assert_console_end();
281 return 0;
282}
283
Sean Andersonba956672020-10-27 19:55:28 -0400284int log_test_helpers(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400285{
Simon Glass14c8a062017-12-04 13:48:27 -0700286 int ret;
287
Sean Andersonc1821822020-10-27 19:55:27 -0400288 gd->log_fmt = LOGF_TEST;
Sean Andersonba956672020-10-27 19:55:28 -0400289 ret = do_log_test_helpers(uts);
Sean Andersonc1821822020-10-27 19:55:27 -0400290 gd->log_fmt = log_get_default_format();
291 return ret;
292}
Sean Andersonba956672020-10-27 19:55:28 -0400293LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400294
Sean Andersonba956672020-10-27 19:55:28 -0400295int do_log_test_disable(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400296{
297 ut_assertok(console_record_reset_enable());
298 log_err("default\n");
299 ut_assert_nextline("%s() default", __func__);
300
301 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
302 log_err("disabled\n");
Simon Glass14c8a062017-12-04 13:48:27 -0700303
Sean Andersonc1821822020-10-27 19:55:27 -0400304 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
305 log_err("enabled\n");
306 ut_assert_nextline("%s() enabled", __func__);
307 ut_assert_console_end();
308 return 0;
309}
310
Sean Andersonba956672020-10-27 19:55:28 -0400311int log_test_disable(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400312{
313 int ret;
Simon Glass14c8a062017-12-04 13:48:27 -0700314
Sean Andersonc1821822020-10-27 19:55:27 -0400315 gd->log_fmt = LOGF_TEST;
Sean Andersonba956672020-10-27 19:55:28 -0400316 ret = do_log_test_disable(uts);
Sean Andersonc1821822020-10-27 19:55:27 -0400317 gd->log_fmt = log_get_default_format();
318 return ret;
Simon Glass14c8a062017-12-04 13:48:27 -0700319}
Sean Andersonba956672020-10-27 19:55:28 -0400320LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
Sean Anderson14e99862020-10-27 19:55:29 -0400321
322/* Check denying based on category */
323int log_test_cat_deny(struct unit_test_state *uts)
324{
325 int filt1, filt2;
326 enum log_category_t cat_list[] = {
327 log_uc_cat(UCLASS_SPI), LOGC_END
328 };
329
330 filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
331 ut_assert(filt1 >= 0);
332 filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
333 LOGFF_DENY);
334 ut_assert(filt2 >= 0);
335
336 ut_assertok(console_record_reset_enable());
337 log_run_cat(UCLASS_SPI);
338 check_log_entries_none();
339
340 ut_assertok(log_remove_filter("console", filt1));
341 ut_assertok(log_remove_filter("console", filt2));
342 return 0;
343}
344LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
345
346/* Check denying based on file */
347int log_test_file_deny(struct unit_test_state *uts)
348{
349 int filt1, filt2;
350
351 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
352 ut_assert(filt1 >= 0);
353 filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
354 LOGFF_DENY);
355 ut_assert(filt2 >= 0);
356
357 ut_assertok(console_record_reset_enable());
358 log_run_file("file");
359 check_log_entries_none();
360
361 ut_assertok(log_remove_filter("console", filt1));
362 ut_assertok(log_remove_filter("console", filt2));
363 return 0;
364}
365LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
366
367/* Check denying based on level */
368int log_test_level_deny(struct unit_test_state *uts)
369{
370 int filt1, filt2;
371
372 filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
373 ut_assert(filt1 >= 0);
374 filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
375 LOGFF_DENY);
376 ut_assert(filt2 >= 0);
377
378 ut_assertok(console_record_reset_enable());
379 log_run();
Patrick Delaunay529e46f2020-11-27 11:20:51 +0100380 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Anderson14e99862020-10-27 19:55:29 -0400381 LOGL_WARNING + 1, _LOG_MAX_LEVEL);
382
383 ut_assertok(log_remove_filter("console", filt1));
384 ut_assertok(log_remove_filter("console", filt2));
385 return 0;
386}
387LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
Sean Anderson41473172020-10-27 19:55:31 -0400388
389/* Check matching based on minimum level */
390int log_test_min(struct unit_test_state *uts)
391{
392 int filt1, filt2;
393
394 filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
395 LOGFF_LEVEL_MIN);
396 ut_assert(filt1 >= 0);
397 filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
398 LOGFF_DENY | LOGFF_LEVEL_MIN);
399 ut_assert(filt2 >= 0);
400
401 ut_assertok(console_record_reset_enable());
402 log_run();
Patrick Delaunay529e46f2020-11-27 11:20:51 +0100403 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Anderson41473172020-10-27 19:55:31 -0400404 LOGL_WARNING, LOGL_INFO - 1);
405
406 ut_assertok(log_remove_filter("console", filt1));
407 ut_assertok(log_remove_filter("console", filt2));
408 return 0;
409}
410LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);
Patrick Delaunaycc034972020-11-27 11:20:53 +0100411
412/* Check dropped traces */
413int log_test_dropped(struct unit_test_state *uts)
414{
415 /* force LOG not ready */
416 gd->flags &= ~(GD_FLG_LOG_READY);
417 gd->log_drop_count = 0;
418
419 ut_assertok(console_record_reset_enable());
420 log_run();
421
422 ut_asserteq(gd->log_drop_count, 3 * (LOGL_COUNT - LOGL_FIRST - 1));
Patrick Delaunay293798c2020-11-27 11:20:58 +0100423 check_log_entries_flags_levels(EXPECT_DEBUG, LOGL_FIRST, CONFIG_LOG_DEFAULT_LEVEL);
Patrick Delaunaycc034972020-11-27 11:20:53 +0100424
425 gd->flags |= GD_FLG_LOG_READY;
426 gd->log_drop_count = 0;
427
428 return 0;
429}
430LOG_TEST_FLAGS(log_test_dropped, UT_TESTF_CONSOLE_REC);