1 // SPDX-License-Identifier: GPL-2.0+
3 * Logging support test program
5 * Copyright (c) 2017 Google, Inc
15 DECLARE_GLOBAL_DATA_PTR;
17 /* emit some sample log records in different ways, for testing */
18 static int do_log_run(int cat, const char *file)
22 gd->log_fmt = LOGF_TEST;
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",
30 gd->log_fmt = log_get_default_format();
34 #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")
38 #define EXPECT_LOG BIT(0)
39 #define EXPECT_DIRECT BIT(1)
40 #define EXPECT_EXTRA BIT(2)
42 static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
47 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);
53 if (flags & EXPECT_EXTRA)
54 for (; i <= LOGL_MAX ; i++)
55 ut_assert_nextline("func() _log %d", i);
57 ut_assert_console_end();
61 #define check_log_entries_flags_levels(flags, min, max) do {\
62 int ret = do_check_log_entries(uts, flags, min, max); \
67 #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)
74 /* Check a category filter using the first category */
75 int log_test_cat_allow(struct unit_test_state *uts)
77 enum log_category_t cat_list[] = {
78 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
83 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
86 ut_assertok(console_record_reset_enable());
87 log_run_cat(UCLASS_MMC);
88 check_log_entries_extra();
90 ut_assertok(console_record_reset_enable());
91 log_run_cat(UCLASS_SPI);
92 check_log_entries_extra();
94 ut_assertok(log_remove_filter("console", filt));
97 LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
99 /* Check a category filter that should block log entries */
100 int log_test_cat_deny_implicit(struct unit_test_state *uts)
102 enum log_category_t cat_list[] = {
103 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
107 filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
108 ut_assert(filt >= 0);
110 ut_assertok(console_record_reset_enable());
111 log_run_cat(UCLASS_SPI);
112 check_log_entries_none();
114 ut_assertok(log_remove_filter("console", filt));
117 LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
119 /* Check passing and failing file filters */
120 int log_test_file(struct unit_test_state *uts)
124 filt = log_add_filter("console", NULL, LOGL_MAX, "file");
125 ut_assert(filt >= 0);
127 ut_assertok(console_record_reset_enable());
128 log_run_file("file");
129 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
131 ut_assertok(console_record_reset_enable());
132 log_run_file("file2");
133 check_log_entries_none();
135 ut_assertok(log_remove_filter("console", filt));
138 LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
140 /* Check a passing file filter (second in list) */
141 int log_test_file_second(struct unit_test_state *uts)
145 filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
146 ut_assert(filt >= 0);
148 ut_assertok(console_record_reset_enable());
149 log_run_file("file2");
150 check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
152 ut_assertok(log_remove_filter("console", filt));
155 LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
157 /* Check a passing file filter (middle of list) */
158 int log_test_file_mid(struct unit_test_state *uts)
162 filt = log_add_filter("console", NULL, LOGL_MAX,
163 "file,file2,log/log_test.c");
164 ut_assert(filt >= 0);
166 ut_assertok(console_record_reset_enable());
167 log_run_file("file2");
168 check_log_entries_extra();
170 ut_assertok(log_remove_filter("console", filt));
173 LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
175 /* Check a log level filter */
176 int log_test_level(struct unit_test_state *uts)
180 filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
181 ut_assert(filt >= 0);
183 ut_assertok(console_record_reset_enable());
185 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT, LOGL_FIRST,
188 ut_assertok(log_remove_filter("console", filt));
191 LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
193 /* Check two filters, one of which passes everything */
194 int log_test_double(struct unit_test_state *uts)
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);
203 ut_assertok(console_record_reset_enable());
205 check_log_entries_extra();
207 ut_assertok(log_remove_filter("console", filt1));
208 ut_assertok(log_remove_filter("console", filt2));
211 LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
213 /* Check three filters, which together pass everything */
214 int log_test_triple(struct unit_test_state *uts)
216 int filt1, filt2, filt3;
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);
225 ut_assertok(console_record_reset_enable());
226 log_run_file("file2");
227 check_log_entries_extra();
229 ut_assertok(log_remove_filter("console", filt1));
230 ut_assertok(log_remove_filter("console", filt2));
231 ut_assertok(log_remove_filter("console", filt3));
234 LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
236 int do_log_test_helpers(struct unit_test_state *uts)
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);
252 for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
253 ut_assert_nextline("%s() level %d", __func__, i);
254 ut_assert_console_end();
258 int log_test_helpers(struct unit_test_state *uts)
262 gd->log_fmt = LOGF_TEST;
263 ret = do_log_test_helpers(uts);
264 gd->log_fmt = log_get_default_format();
267 LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
269 int do_log_test_disable(struct unit_test_state *uts)
271 ut_assertok(console_record_reset_enable());
272 log_err("default\n");
273 ut_assert_nextline("%s() default", __func__);
275 ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
276 log_err("disabled\n");
278 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();
285 int log_test_disable(struct unit_test_state *uts)
289 gd->log_fmt = LOGF_TEST;
290 ret = do_log_test_disable(uts);
291 gd->log_fmt = log_get_default_format();
294 LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
296 /* Check denying based on category */
297 int log_test_cat_deny(struct unit_test_state *uts)
300 enum log_category_t cat_list[] = {
301 log_uc_cat(UCLASS_SPI), LOGC_END
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,
308 ut_assert(filt2 >= 0);
310 ut_assertok(console_record_reset_enable());
311 log_run_cat(UCLASS_SPI);
312 check_log_entries_none();
314 ut_assertok(log_remove_filter("console", filt1));
315 ut_assertok(log_remove_filter("console", filt2));
318 LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
320 /* Check denying based on file */
321 int log_test_file_deny(struct unit_test_state *uts)
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",
329 ut_assert(filt2 >= 0);
331 ut_assertok(console_record_reset_enable());
332 log_run_file("file");
333 check_log_entries_none();
335 ut_assertok(log_remove_filter("console", filt1));
336 ut_assertok(log_remove_filter("console", filt2));
339 LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
341 /* Check denying based on level */
342 int log_test_level_deny(struct unit_test_state *uts)
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,
350 ut_assert(filt2 >= 0);
352 ut_assertok(console_record_reset_enable());
354 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
355 LOGL_WARNING + 1, _LOG_MAX_LEVEL);
357 ut_assertok(log_remove_filter("console", filt1));
358 ut_assertok(log_remove_filter("console", filt2));
361 LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
363 /* Check matching based on minimum level */
364 int log_test_min(struct unit_test_state *uts)
368 filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
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);
375 ut_assertok(console_record_reset_enable());
377 check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
378 LOGL_WARNING, LOGL_INFO - 1);
380 ut_assertok(log_remove_filter("console", filt1));
381 ut_assertok(log_remove_filter("console", filt2));
384 LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);