blob: 4a814ff4132061ce75031ae413d22069bfb57052 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassef11ed82017-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 Glassef11ed82017-12-04 13:48:27 -07007 */
8
9#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Sean Anderson62ef8182020-10-27 19:55:27 -040013#include <test/log.h>
14#include <test/ut.h>
15
16DECLARE_GLOBAL_DATA_PTR;
Simon Glassef11ed82017-12-04 13:48:27 -070017
18/* emit some sample log records in different ways, for testing */
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010019static int do_log_run(struct unit_test_state *uts, int cat, const char *file)
Simon Glassef11ed82017-12-04 13:48:27 -070020{
21 int i;
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010022 int ret, expected_ret;
23
24 if (gd->flags & GD_FLG_LOG_READY)
25 expected_ret = 0;
26 else
27 expected_ret = -ENOSYS;
Simon Glassef11ed82017-12-04 13:48:27 -070028
Sean Anderson62ef8182020-10-27 19:55:27 -040029 gd->log_fmt = LOGF_TEST;
Simon Glassef11ed82017-12-04 13:48:27 -070030 debug("debug\n");
31 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
32 log(cat, i, "log %d\n", i);
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010033 ret = _log(log_uc_cat(cat), i, file, 100 + i,
34 "func", "_log %d\n", i);
35 ut_asserteq(ret, expected_ret);
Simon Glassef11ed82017-12-04 13:48:27 -070036 }
Patrick Delaunay6278ec12020-11-27 11:20:51 +010037 /* test with LOGL_COUNT flag */
38 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010039 ret = _log(log_uc_cat(cat), i | LOGL_FORCE_DEBUG, file, 100 + i,
40 "func", "_log force %d\n", i);
41 ut_asserteq(ret, expected_ret);
Patrick Delaunay6278ec12020-11-27 11:20:51 +010042 }
Simon Glassef11ed82017-12-04 13:48:27 -070043
Sean Anderson62ef8182020-10-27 19:55:27 -040044 gd->log_fmt = log_get_default_format();
Simon Glassef11ed82017-12-04 13:48:27 -070045 return 0;
46}
47
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010048#define log_run_cat(cat) do_log_run(uts, cat, "file")
49#define log_run_file(file) do_log_run(uts, UCLASS_SPI, file)
50#define log_run() do_log_run(uts, UCLASS_SPI, "file")
Sean Anderson62ef8182020-10-27 19:55:27 -040051
52#define EXPECT_LOG BIT(0)
53#define EXPECT_DIRECT BIT(1)
54#define EXPECT_EXTRA BIT(2)
Patrick Delaunay6278ec12020-11-27 11:20:51 +010055#define EXPECT_FORCE BIT(3)
Patrick Delaunay742346f2020-11-27 11:20:58 +010056#define EXPECT_DEBUG BIT(4)
Sean Anderson62ef8182020-10-27 19:55:27 -040057
58static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
59 int max)
Simon Glassef11ed82017-12-04 13:48:27 -070060{
Sean Anderson62ef8182020-10-27 19:55:27 -040061 int i;
Simon Glassef11ed82017-12-04 13:48:27 -070062
Sean Anderson62ef8182020-10-27 19:55:27 -040063 for (i = min; i <= max; i++) {
64 if (flags & EXPECT_LOG)
65 ut_assert_nextline("do_log_run() log %d", i);
66 if (flags & EXPECT_DIRECT)
67 ut_assert_nextline("func() _log %d", i);
Patrick Delaunay742346f2020-11-27 11:20:58 +010068 if (flags & EXPECT_DEBUG) {
69 ut_assert_nextline("log %d", i);
70 ut_assert_nextline("_log %d", i);
71 }
Sean Anderson62ef8182020-10-27 19:55:27 -040072 }
73 if (flags & EXPECT_EXTRA)
74 for (; i <= LOGL_MAX ; i++)
75 ut_assert_nextline("func() _log %d", i);
Simon Glassef11ed82017-12-04 13:48:27 -070076
Patrick Delaunay6278ec12020-11-27 11:20:51 +010077 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
78 if (flags & EXPECT_FORCE)
79 ut_assert_nextline("func() _log force %d", i);
Patrick Delaunay742346f2020-11-27 11:20:58 +010080 if (flags & EXPECT_DEBUG)
81 ut_assert_nextline("_log force %d", i);
Patrick Delaunay6278ec12020-11-27 11:20:51 +010082 }
83
Sean Anderson62ef8182020-10-27 19:55:27 -040084 ut_assert_console_end();
Simon Glassef11ed82017-12-04 13:48:27 -070085 return 0;
86}
87
Sean Anderson62ef8182020-10-27 19:55:27 -040088#define check_log_entries_flags_levels(flags, min, max) do {\
89 int ret = do_check_log_entries(uts, flags, min, max); \
90 if (ret) \
91 return ret; \
92} while (0)
93
94#define check_log_entries_flags(flags) \
95 check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL)
Patrick Delaunay6278ec12020-11-27 11:20:51 +010096#define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE)
Sean Anderson62ef8182020-10-27 19:55:27 -040097#define check_log_entries_extra() \
Patrick Delaunay6278ec12020-11-27 11:20:51 +010098 check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE)
99#define check_log_entries_none() check_log_entries_flags(EXPECT_FORCE)
Sean Anderson62ef8182020-10-27 19:55:27 -0400100
101/* Check a category filter using the first category */
Sean Anderson046b8c02020-10-27 19:55:28 -0400102int log_test_cat_allow(struct unit_test_state *uts)
Simon Glassef11ed82017-12-04 13:48:27 -0700103{
Sean Anderson62ef8182020-10-27 19:55:27 -0400104 enum log_category_t cat_list[] = {
105 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
106 LOGC_NONE, LOGC_END
107 };
108 int filt;
109
110 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
111 ut_assert(filt >= 0);
112
113 ut_assertok(console_record_reset_enable());
114 log_run_cat(UCLASS_MMC);
115 check_log_entries_extra();
116
117 ut_assertok(console_record_reset_enable());
118 log_run_cat(UCLASS_SPI);
119 check_log_entries_extra();
120
121 ut_assertok(log_remove_filter("console", filt));
122 return 0;
123}
Sean Anderson046b8c02020-10-27 19:55:28 -0400124LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400125
126/* Check a category filter that should block log entries */
Sean Anderson046b8c02020-10-27 19:55:28 -0400127int log_test_cat_deny_implicit(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400128{
129 enum log_category_t cat_list[] = {
130 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
131 };
132 int filt;
133
134 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
135 ut_assert(filt >= 0);
136
137 ut_assertok(console_record_reset_enable());
138 log_run_cat(UCLASS_SPI);
139 check_log_entries_none();
140
141 ut_assertok(log_remove_filter("console", filt));
142 return 0;
143}
Sean Anderson046b8c02020-10-27 19:55:28 -0400144LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400145
146/* Check passing and failing file filters */
Sean Anderson046b8c02020-10-27 19:55:28 -0400147int log_test_file(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400148{
149 int filt;
150
151 filt = log_add_filter("console", NULL, LOGL_MAX, "file");
152 ut_assert(filt >= 0);
153
154 ut_assertok(console_record_reset_enable());
155 log_run_file("file");
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100156 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Anderson62ef8182020-10-27 19:55:27 -0400157
158 ut_assertok(console_record_reset_enable());
159 log_run_file("file2");
160 check_log_entries_none();
161
162 ut_assertok(log_remove_filter("console", filt));
163 return 0;
164}
Sean Anderson046b8c02020-10-27 19:55:28 -0400165LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400166
167/* Check a passing file filter (second in list) */
Sean Anderson046b8c02020-10-27 19:55:28 -0400168int log_test_file_second(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400169{
170 int filt;
171
172 filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
173 ut_assert(filt >= 0);
174
175 ut_assertok(console_record_reset_enable());
176 log_run_file("file2");
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100177 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Anderson62ef8182020-10-27 19:55:27 -0400178
179 ut_assertok(log_remove_filter("console", filt));
180 return 0;
181}
Sean Anderson046b8c02020-10-27 19:55:28 -0400182LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400183
184/* Check a passing file filter (middle of list) */
Sean Anderson046b8c02020-10-27 19:55:28 -0400185int log_test_file_mid(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400186{
187 int filt;
188
189 filt = log_add_filter("console", NULL, LOGL_MAX,
190 "file,file2,log/log_test.c");
191 ut_assert(filt >= 0);
192
193 ut_assertok(console_record_reset_enable());
194 log_run_file("file2");
195 check_log_entries_extra();
196
197 ut_assertok(log_remove_filter("console", filt));
198 return 0;
199}
Sean Anderson046b8c02020-10-27 19:55:28 -0400200LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400201
202/* Check a log level filter */
Sean Anderson046b8c02020-10-27 19:55:28 -0400203int log_test_level(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400204{
205 int filt;
206
207 filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
208 ut_assert(filt >= 0);
209
210 ut_assertok(console_record_reset_enable());
211 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100212 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
213 LOGL_FIRST, LOGL_WARNING);
Sean Anderson62ef8182020-10-27 19:55:27 -0400214
215 ut_assertok(log_remove_filter("console", filt));
216 return 0;
217}
Sean Anderson046b8c02020-10-27 19:55:28 -0400218LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400219
220/* Check two filters, one of which passes everything */
Sean Anderson046b8c02020-10-27 19:55:28 -0400221int log_test_double(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400222{
223 int filt1, filt2;
224
225 filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL);
226 ut_assert(filt1 >= 0);
227 filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL);
228 ut_assert(filt2 >= 0);
229
230 ut_assertok(console_record_reset_enable());
231 log_run();
232 check_log_entries_extra();
233
234 ut_assertok(log_remove_filter("console", filt1));
235 ut_assertok(log_remove_filter("console", filt2));
236 return 0;
237}
Sean Anderson046b8c02020-10-27 19:55:28 -0400238LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400239
240/* Check three filters, which together pass everything */
Sean Anderson046b8c02020-10-27 19:55:28 -0400241int log_test_triple(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400242{
243 int filt1, filt2, filt3;
244
245 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)");
246 ut_assert(filt1 >= 0);
247 filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2");
248 ut_assert(filt2 >= 0);
249 filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c");
250 ut_assert(filt3 >= 0);
251
252 ut_assertok(console_record_reset_enable());
253 log_run_file("file2");
254 check_log_entries_extra();
255
256 ut_assertok(log_remove_filter("console", filt1));
257 ut_assertok(log_remove_filter("console", filt2));
258 ut_assertok(log_remove_filter("console", filt3));
259 return 0;
260}
Sean Anderson046b8c02020-10-27 19:55:28 -0400261LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400262
Sean Anderson046b8c02020-10-27 19:55:28 -0400263int do_log_test_helpers(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400264{
265 int i;
266
267 ut_assertok(console_record_reset_enable());
268 log_err("level %d\n", LOGL_EMERG);
269 log_err("level %d\n", LOGL_ALERT);
270 log_err("level %d\n", LOGL_CRIT);
271 log_err("level %d\n", LOGL_ERR);
272 log_warning("level %d\n", LOGL_WARNING);
273 log_notice("level %d\n", LOGL_NOTICE);
274 log_info("level %d\n", LOGL_INFO);
275 log_debug("level %d\n", LOGL_DEBUG);
276 log_content("level %d\n", LOGL_DEBUG_CONTENT);
277 log_io("level %d\n", LOGL_DEBUG_IO);
278
279 for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
280 ut_assert_nextline("%s() level %d", __func__, i);
281 ut_assert_console_end();
282 return 0;
283}
284
Sean Anderson046b8c02020-10-27 19:55:28 -0400285int log_test_helpers(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400286{
Simon Glassef11ed82017-12-04 13:48:27 -0700287 int ret;
288
Sean Anderson62ef8182020-10-27 19:55:27 -0400289 gd->log_fmt = LOGF_TEST;
Sean Anderson046b8c02020-10-27 19:55:28 -0400290 ret = do_log_test_helpers(uts);
Sean Anderson62ef8182020-10-27 19:55:27 -0400291 gd->log_fmt = log_get_default_format();
292 return ret;
Simon Glassef11ed82017-12-04 13:48:27 -0700293}
Sean Anderson046b8c02020-10-27 19:55:28 -0400294LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400295
Sean Anderson046b8c02020-10-27 19:55:28 -0400296int do_log_test_disable(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400297{
298 ut_assertok(console_record_reset_enable());
299 log_err("default\n");
300 ut_assert_nextline("%s() default", __func__);
301
302 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
303 log_err("disabled\n");
304
305 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
306 log_err("enabled\n");
307 ut_assert_nextline("%s() enabled", __func__);
308 ut_assert_console_end();
309 return 0;
310}
311
Sean Anderson046b8c02020-10-27 19:55:28 -0400312int log_test_disable(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400313{
314 int ret;
315
316 gd->log_fmt = LOGF_TEST;
Sean Anderson046b8c02020-10-27 19:55:28 -0400317 ret = do_log_test_disable(uts);
Sean Anderson62ef8182020-10-27 19:55:27 -0400318 gd->log_fmt = log_get_default_format();
319 return ret;
320}
Sean Anderson046b8c02020-10-27 19:55:28 -0400321LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
Sean Andersonf51e5ec2020-10-27 19:55:29 -0400322
323/* Check denying based on category */
324int log_test_cat_deny(struct unit_test_state *uts)
325{
326 int filt1, filt2;
327 enum log_category_t cat_list[] = {
328 log_uc_cat(UCLASS_SPI), LOGC_END
329 };
330
331 filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
332 ut_assert(filt1 >= 0);
333 filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
334 LOGFF_DENY);
335 ut_assert(filt2 >= 0);
336
337 ut_assertok(console_record_reset_enable());
338 log_run_cat(UCLASS_SPI);
339 check_log_entries_none();
340
341 ut_assertok(log_remove_filter("console", filt1));
342 ut_assertok(log_remove_filter("console", filt2));
343 return 0;
344}
345LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
346
347/* Check denying based on file */
348int log_test_file_deny(struct unit_test_state *uts)
349{
350 int filt1, filt2;
351
352 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
353 ut_assert(filt1 >= 0);
354 filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
355 LOGFF_DENY);
356 ut_assert(filt2 >= 0);
357
358 ut_assertok(console_record_reset_enable());
359 log_run_file("file");
360 check_log_entries_none();
361
362 ut_assertok(log_remove_filter("console", filt1));
363 ut_assertok(log_remove_filter("console", filt2));
364 return 0;
365}
366LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
367
368/* Check denying based on level */
369int log_test_level_deny(struct unit_test_state *uts)
370{
371 int filt1, filt2;
372
373 filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
374 ut_assert(filt1 >= 0);
375 filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
376 LOGFF_DENY);
377 ut_assert(filt2 >= 0);
378
379 ut_assertok(console_record_reset_enable());
380 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100381 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Andersonf51e5ec2020-10-27 19:55:29 -0400382 LOGL_WARNING + 1, _LOG_MAX_LEVEL);
383
384 ut_assertok(log_remove_filter("console", filt1));
385 ut_assertok(log_remove_filter("console", filt2));
386 return 0;
387}
388LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
Sean Anderson12273042020-10-27 19:55:31 -0400389
390/* Check matching based on minimum level */
391int log_test_min(struct unit_test_state *uts)
392{
393 int filt1, filt2;
394
395 filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
396 LOGFF_LEVEL_MIN);
397 ut_assert(filt1 >= 0);
398 filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
399 LOGFF_DENY | LOGFF_LEVEL_MIN);
400 ut_assert(filt2 >= 0);
401
402 ut_assertok(console_record_reset_enable());
403 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100404 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Anderson12273042020-10-27 19:55:31 -0400405 LOGL_WARNING, LOGL_INFO - 1);
406
407 ut_assertok(log_remove_filter("console", filt1));
408 ut_assertok(log_remove_filter("console", filt2));
409 return 0;
410}
411LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);
Patrick Delaunayce9af2a2020-11-27 11:20:53 +0100412
413/* Check dropped traces */
414int log_test_dropped(struct unit_test_state *uts)
415{
416 /* force LOG not ready */
417 gd->flags &= ~(GD_FLG_LOG_READY);
418 gd->log_drop_count = 0;
419
420 ut_assertok(console_record_reset_enable());
421 log_run();
422
423 ut_asserteq(gd->log_drop_count, 3 * (LOGL_COUNT - LOGL_FIRST - 1));
Patrick Delaunay742346f2020-11-27 11:20:58 +0100424 check_log_entries_flags_levels(EXPECT_DEBUG, LOGL_FIRST, CONFIG_LOG_DEFAULT_LEVEL);
Patrick Delaunayce9af2a2020-11-27 11:20:53 +0100425
426 gd->flags |= GD_FLG_LOG_READY;
427 gd->log_drop_count = 0;
428
429 return 0;
430}
431LOG_TEST_FLAGS(log_test_dropped, UT_TESTF_CONSOLE_REC);