blob: 6bafc1e315089225d7f935c5f7d433a2fe45bed9 [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>
Sean Anderson62ef8182020-10-27 19:55:27 -040012#include <test/log.h>
13#include <test/ut.h>
14
15DECLARE_GLOBAL_DATA_PTR;
Simon Glassef11ed82017-12-04 13:48:27 -070016
17/* emit some sample log records in different ways, for testing */
Patrick Delaunayce9af2a2020-11-27 11:20:53 +010018static int do_log_run(struct unit_test_state *uts, int cat, const char *file)
Simon Glassef11ed82017-12-04 13:48:27 -070019{
20 int i;
Patrick Delaunayce9af2a2020-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 Glassef11ed82017-12-04 13:48:27 -070027
Sean Anderson62ef8182020-10-27 19:55:27 -040028 gd->log_fmt = LOGF_TEST;
Simon Glassef11ed82017-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 Delaunayce9af2a2020-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 Glassef11ed82017-12-04 13:48:27 -070035 }
Patrick Delaunay6278ec12020-11-27 11:20:51 +010036 /* test with LOGL_COUNT flag */
37 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
Patrick Delaunayce9af2a2020-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 Delaunay6278ec12020-11-27 11:20:51 +010041 }
Simon Glassef11ed82017-12-04 13:48:27 -070042
Sean Anderson62ef8182020-10-27 19:55:27 -040043 gd->log_fmt = log_get_default_format();
Simon Glassef11ed82017-12-04 13:48:27 -070044 return 0;
45}
46
Patrick Delaunayce9af2a2020-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")
Sean Anderson62ef8182020-10-27 19:55:27 -040050
51#define EXPECT_LOG BIT(0)
52#define EXPECT_DIRECT BIT(1)
53#define EXPECT_EXTRA BIT(2)
Patrick Delaunay6278ec12020-11-27 11:20:51 +010054#define EXPECT_FORCE BIT(3)
Sean Anderson62ef8182020-10-27 19:55:27 -040055
56static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
57 int max)
Simon Glassef11ed82017-12-04 13:48:27 -070058{
Sean Anderson62ef8182020-10-27 19:55:27 -040059 int i;
Simon Glassef11ed82017-12-04 13:48:27 -070060
Sean Anderson62ef8182020-10-27 19:55:27 -040061 for (i = min; i <= max; i++) {
62 if (flags & EXPECT_LOG)
63 ut_assert_nextline("do_log_run() log %d", i);
64 if (flags & EXPECT_DIRECT)
65 ut_assert_nextline("func() _log %d", i);
66 }
67 if (flags & EXPECT_EXTRA)
68 for (; i <= LOGL_MAX ; i++)
69 ut_assert_nextline("func() _log %d", i);
Simon Glassef11ed82017-12-04 13:48:27 -070070
Patrick Delaunay6278ec12020-11-27 11:20:51 +010071 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
72 if (flags & EXPECT_FORCE)
73 ut_assert_nextline("func() _log force %d", i);
74 }
75
Sean Anderson62ef8182020-10-27 19:55:27 -040076 ut_assert_console_end();
Simon Glassef11ed82017-12-04 13:48:27 -070077 return 0;
78}
79
Sean Anderson62ef8182020-10-27 19:55:27 -040080#define check_log_entries_flags_levels(flags, min, max) do {\
81 int ret = do_check_log_entries(uts, flags, min, max); \
82 if (ret) \
83 return ret; \
84} while (0)
85
86#define check_log_entries_flags(flags) \
87 check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL)
Patrick Delaunay6278ec12020-11-27 11:20:51 +010088#define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE)
Sean Anderson62ef8182020-10-27 19:55:27 -040089#define check_log_entries_extra() \
Patrick Delaunay6278ec12020-11-27 11:20:51 +010090 check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE)
91#define check_log_entries_none() check_log_entries_flags(EXPECT_FORCE)
Sean Anderson62ef8182020-10-27 19:55:27 -040092
93/* Check a category filter using the first category */
Sean Anderson046b8c02020-10-27 19:55:28 -040094int log_test_cat_allow(struct unit_test_state *uts)
Simon Glassef11ed82017-12-04 13:48:27 -070095{
Sean Anderson62ef8182020-10-27 19:55:27 -040096 enum log_category_t cat_list[] = {
97 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
98 LOGC_NONE, LOGC_END
99 };
100 int filt;
101
102 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
103 ut_assert(filt >= 0);
104
105 ut_assertok(console_record_reset_enable());
106 log_run_cat(UCLASS_MMC);
107 check_log_entries_extra();
108
109 ut_assertok(console_record_reset_enable());
110 log_run_cat(UCLASS_SPI);
111 check_log_entries_extra();
112
113 ut_assertok(log_remove_filter("console", filt));
114 return 0;
115}
Sean Anderson046b8c02020-10-27 19:55:28 -0400116LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400117
118/* Check a category filter that should block log entries */
Sean Anderson046b8c02020-10-27 19:55:28 -0400119int log_test_cat_deny_implicit(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400120{
121 enum log_category_t cat_list[] = {
122 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
123 };
124 int filt;
125
126 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
127 ut_assert(filt >= 0);
128
129 ut_assertok(console_record_reset_enable());
130 log_run_cat(UCLASS_SPI);
131 check_log_entries_none();
132
133 ut_assertok(log_remove_filter("console", filt));
134 return 0;
135}
Sean Anderson046b8c02020-10-27 19:55:28 -0400136LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400137
138/* Check passing and failing file filters */
Sean Anderson046b8c02020-10-27 19:55:28 -0400139int log_test_file(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400140{
141 int filt;
142
143 filt = log_add_filter("console", NULL, LOGL_MAX, "file");
144 ut_assert(filt >= 0);
145
146 ut_assertok(console_record_reset_enable());
147 log_run_file("file");
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100148 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Anderson62ef8182020-10-27 19:55:27 -0400149
150 ut_assertok(console_record_reset_enable());
151 log_run_file("file2");
152 check_log_entries_none();
153
154 ut_assertok(log_remove_filter("console", filt));
155 return 0;
156}
Sean Anderson046b8c02020-10-27 19:55:28 -0400157LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400158
159/* Check a passing file filter (second in list) */
Sean Anderson046b8c02020-10-27 19:55:28 -0400160int log_test_file_second(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400161{
162 int filt;
163
164 filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
165 ut_assert(filt >= 0);
166
167 ut_assertok(console_record_reset_enable());
168 log_run_file("file2");
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100169 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE);
Sean Anderson62ef8182020-10-27 19:55:27 -0400170
171 ut_assertok(log_remove_filter("console", filt));
172 return 0;
173}
Sean Anderson046b8c02020-10-27 19:55:28 -0400174LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400175
176/* Check a passing file filter (middle of list) */
Sean Anderson046b8c02020-10-27 19:55:28 -0400177int log_test_file_mid(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400178{
179 int filt;
180
181 filt = log_add_filter("console", NULL, LOGL_MAX,
182 "file,file2,log/log_test.c");
183 ut_assert(filt >= 0);
184
185 ut_assertok(console_record_reset_enable());
186 log_run_file("file2");
187 check_log_entries_extra();
188
189 ut_assertok(log_remove_filter("console", filt));
190 return 0;
191}
Sean Anderson046b8c02020-10-27 19:55:28 -0400192LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400193
194/* Check a log level filter */
Sean Anderson046b8c02020-10-27 19:55:28 -0400195int log_test_level(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400196{
197 int filt;
198
199 filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
200 ut_assert(filt >= 0);
201
202 ut_assertok(console_record_reset_enable());
203 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100204 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
205 LOGL_FIRST, LOGL_WARNING);
Sean Anderson62ef8182020-10-27 19:55:27 -0400206
207 ut_assertok(log_remove_filter("console", filt));
208 return 0;
209}
Sean Anderson046b8c02020-10-27 19:55:28 -0400210LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400211
212/* Check two filters, one of which passes everything */
Sean Anderson046b8c02020-10-27 19:55:28 -0400213int log_test_double(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400214{
215 int filt1, filt2;
216
217 filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL);
218 ut_assert(filt1 >= 0);
219 filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL);
220 ut_assert(filt2 >= 0);
221
222 ut_assertok(console_record_reset_enable());
223 log_run();
224 check_log_entries_extra();
225
226 ut_assertok(log_remove_filter("console", filt1));
227 ut_assertok(log_remove_filter("console", filt2));
228 return 0;
229}
Sean Anderson046b8c02020-10-27 19:55:28 -0400230LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400231
232/* Check three filters, which together pass everything */
Sean Anderson046b8c02020-10-27 19:55:28 -0400233int log_test_triple(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400234{
235 int filt1, filt2, filt3;
236
237 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)");
238 ut_assert(filt1 >= 0);
239 filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2");
240 ut_assert(filt2 >= 0);
241 filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c");
242 ut_assert(filt3 >= 0);
243
244 ut_assertok(console_record_reset_enable());
245 log_run_file("file2");
246 check_log_entries_extra();
247
248 ut_assertok(log_remove_filter("console", filt1));
249 ut_assertok(log_remove_filter("console", filt2));
250 ut_assertok(log_remove_filter("console", filt3));
251 return 0;
252}
Sean Anderson046b8c02020-10-27 19:55:28 -0400253LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400254
Sean Anderson046b8c02020-10-27 19:55:28 -0400255int do_log_test_helpers(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400256{
257 int i;
258
259 ut_assertok(console_record_reset_enable());
260 log_err("level %d\n", LOGL_EMERG);
261 log_err("level %d\n", LOGL_ALERT);
262 log_err("level %d\n", LOGL_CRIT);
263 log_err("level %d\n", LOGL_ERR);
264 log_warning("level %d\n", LOGL_WARNING);
265 log_notice("level %d\n", LOGL_NOTICE);
266 log_info("level %d\n", LOGL_INFO);
267 log_debug("level %d\n", LOGL_DEBUG);
268 log_content("level %d\n", LOGL_DEBUG_CONTENT);
269 log_io("level %d\n", LOGL_DEBUG_IO);
270
271 for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
272 ut_assert_nextline("%s() level %d", __func__, i);
273 ut_assert_console_end();
274 return 0;
275}
276
Sean Anderson046b8c02020-10-27 19:55:28 -0400277int log_test_helpers(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400278{
Simon Glassef11ed82017-12-04 13:48:27 -0700279 int ret;
280
Sean Anderson62ef8182020-10-27 19:55:27 -0400281 gd->log_fmt = LOGF_TEST;
Sean Anderson046b8c02020-10-27 19:55:28 -0400282 ret = do_log_test_helpers(uts);
Sean Anderson62ef8182020-10-27 19:55:27 -0400283 gd->log_fmt = log_get_default_format();
284 return ret;
Simon Glassef11ed82017-12-04 13:48:27 -0700285}
Sean Anderson046b8c02020-10-27 19:55:28 -0400286LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
Sean Anderson62ef8182020-10-27 19:55:27 -0400287
Sean Anderson046b8c02020-10-27 19:55:28 -0400288int do_log_test_disable(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400289{
290 ut_assertok(console_record_reset_enable());
291 log_err("default\n");
292 ut_assert_nextline("%s() default", __func__);
293
294 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
295 log_err("disabled\n");
296
297 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
298 log_err("enabled\n");
299 ut_assert_nextline("%s() enabled", __func__);
300 ut_assert_console_end();
301 return 0;
302}
303
Sean Anderson046b8c02020-10-27 19:55:28 -0400304int log_test_disable(struct unit_test_state *uts)
Sean Anderson62ef8182020-10-27 19:55:27 -0400305{
306 int ret;
307
308 gd->log_fmt = LOGF_TEST;
Sean Anderson046b8c02020-10-27 19:55:28 -0400309 ret = do_log_test_disable(uts);
Sean Anderson62ef8182020-10-27 19:55:27 -0400310 gd->log_fmt = log_get_default_format();
311 return ret;
312}
Sean Anderson046b8c02020-10-27 19:55:28 -0400313LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
Sean Andersonf51e5ec2020-10-27 19:55:29 -0400314
315/* Check denying based on category */
316int log_test_cat_deny(struct unit_test_state *uts)
317{
318 int filt1, filt2;
319 enum log_category_t cat_list[] = {
320 log_uc_cat(UCLASS_SPI), LOGC_END
321 };
322
323 filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
324 ut_assert(filt1 >= 0);
325 filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
326 LOGFF_DENY);
327 ut_assert(filt2 >= 0);
328
329 ut_assertok(console_record_reset_enable());
330 log_run_cat(UCLASS_SPI);
331 check_log_entries_none();
332
333 ut_assertok(log_remove_filter("console", filt1));
334 ut_assertok(log_remove_filter("console", filt2));
335 return 0;
336}
337LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
338
339/* Check denying based on file */
340int log_test_file_deny(struct unit_test_state *uts)
341{
342 int filt1, filt2;
343
344 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
345 ut_assert(filt1 >= 0);
346 filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
347 LOGFF_DENY);
348 ut_assert(filt2 >= 0);
349
350 ut_assertok(console_record_reset_enable());
351 log_run_file("file");
352 check_log_entries_none();
353
354 ut_assertok(log_remove_filter("console", filt1));
355 ut_assertok(log_remove_filter("console", filt2));
356 return 0;
357}
358LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
359
360/* Check denying based on level */
361int log_test_level_deny(struct unit_test_state *uts)
362{
363 int filt1, filt2;
364
365 filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
366 ut_assert(filt1 >= 0);
367 filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
368 LOGFF_DENY);
369 ut_assert(filt2 >= 0);
370
371 ut_assertok(console_record_reset_enable());
372 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100373 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Andersonf51e5ec2020-10-27 19:55:29 -0400374 LOGL_WARNING + 1, _LOG_MAX_LEVEL);
375
376 ut_assertok(log_remove_filter("console", filt1));
377 ut_assertok(log_remove_filter("console", filt2));
378 return 0;
379}
380LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
Sean Anderson12273042020-10-27 19:55:31 -0400381
382/* Check matching based on minimum level */
383int log_test_min(struct unit_test_state *uts)
384{
385 int filt1, filt2;
386
387 filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
388 LOGFF_LEVEL_MIN);
389 ut_assert(filt1 >= 0);
390 filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
391 LOGFF_DENY | LOGFF_LEVEL_MIN);
392 ut_assert(filt2 >= 0);
393
394 ut_assertok(console_record_reset_enable());
395 log_run();
Patrick Delaunay6278ec12020-11-27 11:20:51 +0100396 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE,
Sean Anderson12273042020-10-27 19:55:31 -0400397 LOGL_WARNING, LOGL_INFO - 1);
398
399 ut_assertok(log_remove_filter("console", filt1));
400 ut_assertok(log_remove_filter("console", filt2));
401 return 0;
402}
403LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);
Patrick Delaunayce9af2a2020-11-27 11:20:53 +0100404
405/* Check dropped traces */
406int log_test_dropped(struct unit_test_state *uts)
407{
408 /* force LOG not ready */
409 gd->flags &= ~(GD_FLG_LOG_READY);
410 gd->log_drop_count = 0;
411
412 ut_assertok(console_record_reset_enable());
413 log_run();
414
415 ut_asserteq(gd->log_drop_count, 3 * (LOGL_COUNT - LOGL_FIRST - 1));
416
417 gd->flags |= GD_FLG_LOG_READY;
418 gd->log_drop_count = 0;
419
420 return 0;
421}
422LOG_TEST_FLAGS(log_test_dropped, UT_TESTF_CONSOLE_REC);