blob: c5ca6dcb7a482238c39d5eff563fc795c1360b99 [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 */
Sean Anderson62ef8182020-10-27 19:55:27 -040018static int do_log_run(int cat, const char *file)
Simon Glassef11ed82017-12-04 13:48:27 -070019{
20 int i;
21
Sean Anderson62ef8182020-10-27 19:55:27 -040022 gd->log_fmt = LOGF_TEST;
Simon Glassef11ed82017-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 Anderson62ef8182020-10-27 19:55:27 -040030 gd->log_fmt = log_get_default_format();
Simon Glassef11ed82017-12-04 13:48:27 -070031 return 0;
32}
33
Sean Anderson62ef8182020-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")
37
38#define EXPECT_LOG BIT(0)
39#define EXPECT_DIRECT BIT(1)
40#define EXPECT_EXTRA BIT(2)
41
42static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
43 int max)
Simon Glassef11ed82017-12-04 13:48:27 -070044{
Sean Anderson62ef8182020-10-27 19:55:27 -040045 int i;
Simon Glassef11ed82017-12-04 13:48:27 -070046
Sean Anderson62ef8182020-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);
52 }
53 if (flags & EXPECT_EXTRA)
54 for (; i <= LOGL_MAX ; i++)
55 ut_assert_nextline("func() _log %d", i);
Simon Glassef11ed82017-12-04 13:48:27 -070056
Sean Anderson62ef8182020-10-27 19:55:27 -040057 ut_assert_console_end();
Simon Glassef11ed82017-12-04 13:48:27 -070058 return 0;
59}
60
Sean Anderson62ef8182020-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)
66
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)
73
74/* Check a category filter using the first category */
75int log_test_00(struct unit_test_state *uts)
Simon Glassef11ed82017-12-04 13:48:27 -070076{
Sean Anderson62ef8182020-10-27 19:55:27 -040077 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();
93
94 ut_assertok(log_remove_filter("console", filt));
95 return 0;
96}
97LOG_TEST_FLAGS(log_test_00, UT_TESTF_CONSOLE_REC);
98
99/* Check a category filter that should block log entries */
100int log_test_02(struct unit_test_state *uts)
101{
102 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}
117LOG_TEST_FLAGS(log_test_02, UT_TESTF_CONSOLE_REC);
118
119/* Check passing and failing file filters */
120int log_test_03(struct unit_test_state *uts)
121{
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}
138LOG_TEST_FLAGS(log_test_03, UT_TESTF_CONSOLE_REC);
139
140/* Check a passing file filter (second in list) */
141int log_test_05(struct unit_test_state *uts)
142{
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}
155LOG_TEST_FLAGS(log_test_05, UT_TESTF_CONSOLE_REC);
156
157/* Check a passing file filter (middle of list) */
158int log_test_06(struct unit_test_state *uts)
159{
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}
173LOG_TEST_FLAGS(log_test_06, UT_TESTF_CONSOLE_REC);
174
175/* Check a log level filter */
176int log_test_07(struct unit_test_state *uts)
177{
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}
191LOG_TEST_FLAGS(log_test_07, UT_TESTF_CONSOLE_REC);
192
193/* Check two filters, one of which passes everything */
194int log_test_08(struct unit_test_state *uts)
195{
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}
211LOG_TEST_FLAGS(log_test_08, UT_TESTF_CONSOLE_REC);
212
213/* Check three filters, which together pass everything */
214int log_test_09(struct unit_test_state *uts)
215{
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}
234LOG_TEST_FLAGS(log_test_09, UT_TESTF_CONSOLE_REC);
235
236int do_log_test_10(struct unit_test_state *uts)
237{
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
258int log_test_10(struct unit_test_state *uts)
259{
Simon Glassef11ed82017-12-04 13:48:27 -0700260 int ret;
261
Sean Anderson62ef8182020-10-27 19:55:27 -0400262 gd->log_fmt = LOGF_TEST;
263 ret = do_log_test_10(uts);
264 gd->log_fmt = log_get_default_format();
265 return ret;
Simon Glassef11ed82017-12-04 13:48:27 -0700266}
Sean Anderson62ef8182020-10-27 19:55:27 -0400267LOG_TEST_FLAGS(log_test_10, UT_TESTF_CONSOLE_REC);
268
269int do_log_test_11(struct unit_test_state *uts)
270{
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");
277
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();
282 return 0;
283}
284
285int log_test_11(struct unit_test_state *uts)
286{
287 int ret;
288
289 gd->log_fmt = LOGF_TEST;
290 ret = do_log_test_10(uts);
291 gd->log_fmt = log_get_default_format();
292 return ret;
293}
294LOG_TEST_FLAGS(log_test_11, UT_TESTF_CONSOLE_REC);