blob: ea4fc6bc30b11fdf932353089361022043f0e7d6 [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 */
Sean Andersonc1821822020-10-27 19:55:27 -040018static int do_log_run(int cat, const char *file)
Simon Glass14c8a062017-12-04 13:48:27 -070019{
20 int i;
21
Sean Andersonc1821822020-10-27 19:55:27 -040022 gd->log_fmt = LOGF_TEST;
Simon Glass14c8a062017-12-04 13:48:27 -070023 debug("debug\n");
24 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
25 log(cat, i, "log %d\n", i);
26 _log(log_uc_cat(cat), i, file, 100 + i, "func", "_log %d\n",
27 i);
28 }
29
Sean Andersonc1821822020-10-27 19:55:27 -040030 gd->log_fmt = log_get_default_format();
Simon Glass14c8a062017-12-04 13:48:27 -070031 return 0;
32}
33
Sean Andersonc1821822020-10-27 19:55:27 -040034#define log_run_cat(cat) do_log_run(cat, "file")
35#define log_run_file(file) do_log_run(UCLASS_SPI, file)
36#define log_run() do_log_run(UCLASS_SPI, "file")
Simon Glass14c8a062017-12-04 13:48:27 -070037
Sean Andersonc1821822020-10-27 19:55:27 -040038#define EXPECT_LOG BIT(0)
39#define EXPECT_DIRECT BIT(1)
40#define EXPECT_EXTRA BIT(2)
Simon Glass14c8a062017-12-04 13:48:27 -070041
Sean Andersonc1821822020-10-27 19:55:27 -040042static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
43 int max)
44{
45 int i;
Simon Glass14c8a062017-12-04 13:48:27 -070046
Sean Andersonc1821822020-10-27 19:55:27 -040047 for (i = min; i <= max; i++) {
48 if (flags & EXPECT_LOG)
49 ut_assert_nextline("do_log_run() log %d", i);
50 if (flags & EXPECT_DIRECT)
51 ut_assert_nextline("func() _log %d", i);
Simon Glass14c8a062017-12-04 13:48:27 -070052 }
Sean Andersonc1821822020-10-27 19:55:27 -040053 if (flags & EXPECT_EXTRA)
54 for (; i <= LOGL_MAX ; i++)
55 ut_assert_nextline("func() _log %d", i);
Simon Glass14c8a062017-12-04 13:48:27 -070056
Sean Andersonc1821822020-10-27 19:55:27 -040057 ut_assert_console_end();
58 return 0;
59}
Simon Glass14c8a062017-12-04 13:48:27 -070060
Sean Andersonc1821822020-10-27 19:55:27 -040061#define check_log_entries_flags_levels(flags, min, max) do {\
62 int ret = do_check_log_entries(uts, flags, min, max); \
63 if (ret) \
64 return ret; \
65} while (0)
Simon Glass14c8a062017-12-04 13:48:27 -070066
Sean Andersonc1821822020-10-27 19:55:27 -040067#define check_log_entries_flags(flags) \
68 check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL)
69#define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT)
70#define check_log_entries_extra() \
71 check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA)
72#define check_log_entries_none() check_log_entries_flags(0)
73
74/* Check a category filter using the first category */
Sean Andersonba956672020-10-27 19:55:28 -040075int log_test_cat_allow(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -040076{
77 enum log_category_t cat_list[] = {
78 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
79 LOGC_NONE, LOGC_END
80 };
81 int filt;
82
83 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
84 ut_assert(filt >= 0);
85
86 ut_assertok(console_record_reset_enable());
87 log_run_cat(UCLASS_MMC);
88 check_log_entries_extra();
89
90 ut_assertok(console_record_reset_enable());
91 log_run_cat(UCLASS_SPI);
92 check_log_entries_extra();
Simon Glass14c8a062017-12-04 13:48:27 -070093
Sean Andersonc1821822020-10-27 19:55:27 -040094 ut_assertok(log_remove_filter("console", filt));
Simon Glass14c8a062017-12-04 13:48:27 -070095 return 0;
96}
Sean Andersonba956672020-10-27 19:55:28 -040097LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
Simon Glass14c8a062017-12-04 13:48:27 -070098
Sean Andersonc1821822020-10-27 19:55:27 -040099/* Check a category filter that should block log entries */
Sean Andersonba956672020-10-27 19:55:28 -0400100int log_test_cat_deny_implicit(struct unit_test_state *uts)
Simon Glass14c8a062017-12-04 13:48:27 -0700101{
Sean Andersonc1821822020-10-27 19:55:27 -0400102 enum log_category_t cat_list[] = {
103 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
104 };
105 int filt;
106
107 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
108 ut_assert(filt >= 0);
109
110 ut_assertok(console_record_reset_enable());
111 log_run_cat(UCLASS_SPI);
112 check_log_entries_none();
113
114 ut_assertok(log_remove_filter("console", filt));
115 return 0;
116}
Sean Andersonba956672020-10-27 19:55:28 -0400117LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400118
119/* Check passing and failing file filters */
Sean Andersonba956672020-10-27 19:55:28 -0400120int log_test_file(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400121{
122 int filt;
123
124 filt = log_add_filter("console", NULL, LOGL_MAX, "file");
125 ut_assert(filt >= 0);
126
127 ut_assertok(console_record_reset_enable());
128 log_run_file("file");
129 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
130
131 ut_assertok(console_record_reset_enable());
132 log_run_file("file2");
133 check_log_entries_none();
134
135 ut_assertok(log_remove_filter("console", filt));
136 return 0;
137}
Sean Andersonba956672020-10-27 19:55:28 -0400138LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400139
140/* Check a passing file filter (second in list) */
Sean Andersonba956672020-10-27 19:55:28 -0400141int log_test_file_second(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400142{
143 int filt;
144
145 filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
146 ut_assert(filt >= 0);
147
148 ut_assertok(console_record_reset_enable());
149 log_run_file("file2");
150 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
151
152 ut_assertok(log_remove_filter("console", filt));
153 return 0;
154}
Sean Andersonba956672020-10-27 19:55:28 -0400155LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400156
157/* Check a passing file filter (middle of list) */
Sean Andersonba956672020-10-27 19:55:28 -0400158int log_test_file_mid(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400159{
160 int filt;
161
162 filt = log_add_filter("console", NULL, LOGL_MAX,
163 "file,file2,log/log_test.c");
164 ut_assert(filt >= 0);
165
166 ut_assertok(console_record_reset_enable());
167 log_run_file("file2");
168 check_log_entries_extra();
169
170 ut_assertok(log_remove_filter("console", filt));
171 return 0;
172}
Sean Andersonba956672020-10-27 19:55:28 -0400173LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400174
175/* Check a log level filter */
Sean Andersonba956672020-10-27 19:55:28 -0400176int log_test_level(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400177{
178 int filt;
179
180 filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
181 ut_assert(filt >= 0);
182
183 ut_assertok(console_record_reset_enable());
184 log_run();
185 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT, LOGL_FIRST,
186 LOGL_WARNING);
187
188 ut_assertok(log_remove_filter("console", filt));
189 return 0;
190}
Sean Andersonba956672020-10-27 19:55:28 -0400191LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400192
193/* Check two filters, one of which passes everything */
Sean Andersonba956672020-10-27 19:55:28 -0400194int log_test_double(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400195{
196 int filt1, filt2;
197
198 filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL);
199 ut_assert(filt1 >= 0);
200 filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL);
201 ut_assert(filt2 >= 0);
202
203 ut_assertok(console_record_reset_enable());
204 log_run();
205 check_log_entries_extra();
206
207 ut_assertok(log_remove_filter("console", filt1));
208 ut_assertok(log_remove_filter("console", filt2));
209 return 0;
210}
Sean Andersonba956672020-10-27 19:55:28 -0400211LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400212
213/* Check three filters, which together pass everything */
Sean Andersonba956672020-10-27 19:55:28 -0400214int log_test_triple(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400215{
216 int filt1, filt2, filt3;
217
218 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)");
219 ut_assert(filt1 >= 0);
220 filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2");
221 ut_assert(filt2 >= 0);
222 filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c");
223 ut_assert(filt3 >= 0);
224
225 ut_assertok(console_record_reset_enable());
226 log_run_file("file2");
227 check_log_entries_extra();
228
229 ut_assertok(log_remove_filter("console", filt1));
230 ut_assertok(log_remove_filter("console", filt2));
231 ut_assertok(log_remove_filter("console", filt3));
232 return 0;
233}
Sean Andersonba956672020-10-27 19:55:28 -0400234LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400235
Sean Andersonba956672020-10-27 19:55:28 -0400236int do_log_test_helpers(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400237{
238 int i;
239
240 ut_assertok(console_record_reset_enable());
241 log_err("level %d\n", LOGL_EMERG);
242 log_err("level %d\n", LOGL_ALERT);
243 log_err("level %d\n", LOGL_CRIT);
244 log_err("level %d\n", LOGL_ERR);
245 log_warning("level %d\n", LOGL_WARNING);
246 log_notice("level %d\n", LOGL_NOTICE);
247 log_info("level %d\n", LOGL_INFO);
248 log_debug("level %d\n", LOGL_DEBUG);
249 log_content("level %d\n", LOGL_DEBUG_CONTENT);
250 log_io("level %d\n", LOGL_DEBUG_IO);
251
252 for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
253 ut_assert_nextline("%s() level %d", __func__, i);
254 ut_assert_console_end();
255 return 0;
256}
257
Sean Andersonba956672020-10-27 19:55:28 -0400258int log_test_helpers(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400259{
Simon Glass14c8a062017-12-04 13:48:27 -0700260 int ret;
261
Sean Andersonc1821822020-10-27 19:55:27 -0400262 gd->log_fmt = LOGF_TEST;
Sean Andersonba956672020-10-27 19:55:28 -0400263 ret = do_log_test_helpers(uts);
Sean Andersonc1821822020-10-27 19:55:27 -0400264 gd->log_fmt = log_get_default_format();
265 return ret;
266}
Sean Andersonba956672020-10-27 19:55:28 -0400267LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
Sean Andersonc1821822020-10-27 19:55:27 -0400268
Sean Andersonba956672020-10-27 19:55:28 -0400269int do_log_test_disable(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400270{
271 ut_assertok(console_record_reset_enable());
272 log_err("default\n");
273 ut_assert_nextline("%s() default", __func__);
274
275 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
276 log_err("disabled\n");
Simon Glass14c8a062017-12-04 13:48:27 -0700277
Sean Andersonc1821822020-10-27 19:55:27 -0400278 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
279 log_err("enabled\n");
280 ut_assert_nextline("%s() enabled", __func__);
281 ut_assert_console_end();
282 return 0;
283}
284
Sean Andersonba956672020-10-27 19:55:28 -0400285int log_test_disable(struct unit_test_state *uts)
Sean Andersonc1821822020-10-27 19:55:27 -0400286{
287 int ret;
Simon Glass14c8a062017-12-04 13:48:27 -0700288
Sean Andersonc1821822020-10-27 19:55:27 -0400289 gd->log_fmt = LOGF_TEST;
Sean Andersonba956672020-10-27 19:55:28 -0400290 ret = do_log_test_disable(uts);
Sean Andersonc1821822020-10-27 19:55:27 -0400291 gd->log_fmt = log_get_default_format();
292 return ret;
Simon Glass14c8a062017-12-04 13:48:27 -0700293}
Sean Andersonba956672020-10-27 19:55:28 -0400294LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
Sean Anderson14e99862020-10-27 19:55:29 -0400295
296/* Check denying based on category */
297int log_test_cat_deny(struct unit_test_state *uts)
298{
299 int filt1, filt2;
300 enum log_category_t cat_list[] = {
301 log_uc_cat(UCLASS_SPI), LOGC_END
302 };
303
304 filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
305 ut_assert(filt1 >= 0);
306 filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
307 LOGFF_DENY);
308 ut_assert(filt2 >= 0);
309
310 ut_assertok(console_record_reset_enable());
311 log_run_cat(UCLASS_SPI);
312 check_log_entries_none();
313
314 ut_assertok(log_remove_filter("console", filt1));
315 ut_assertok(log_remove_filter("console", filt2));
316 return 0;
317}
318LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
319
320/* Check denying based on file */
321int log_test_file_deny(struct unit_test_state *uts)
322{
323 int filt1, filt2;
324
325 filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
326 ut_assert(filt1 >= 0);
327 filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
328 LOGFF_DENY);
329 ut_assert(filt2 >= 0);
330
331 ut_assertok(console_record_reset_enable());
332 log_run_file("file");
333 check_log_entries_none();
334
335 ut_assertok(log_remove_filter("console", filt1));
336 ut_assertok(log_remove_filter("console", filt2));
337 return 0;
338}
339LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
340
341/* Check denying based on level */
342int log_test_level_deny(struct unit_test_state *uts)
343{
344 int filt1, filt2;
345
346 filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
347 ut_assert(filt1 >= 0);
348 filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
349 LOGFF_DENY);
350 ut_assert(filt2 >= 0);
351
352 ut_assertok(console_record_reset_enable());
353 log_run();
354 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
355 LOGL_WARNING + 1, _LOG_MAX_LEVEL);
356
357 ut_assertok(log_remove_filter("console", filt1));
358 ut_assertok(log_remove_filter("console", filt2));
359 return 0;
360}
361LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
Sean Anderson41473172020-10-27 19:55:31 -0400362
363/* Check matching based on minimum level */
364int log_test_min(struct unit_test_state *uts)
365{
366 int filt1, filt2;
367
368 filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
369 LOGFF_LEVEL_MIN);
370 ut_assert(filt1 >= 0);
371 filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
372 LOGFF_DENY | LOGFF_LEVEL_MIN);
373 ut_assert(filt2 >= 0);
374
375 ut_assertok(console_record_reset_enable());
376 log_run();
377 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
378 LOGL_WARNING, LOGL_INFO - 1);
379
380 ut_assertok(log_remove_filter("console", filt1));
381 ut_assertok(log_remove_filter("console", filt2));
382 return 0;
383}
384LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);