Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Logging support test program |
| 4 | * |
| 5 | * Copyright (c) 2017 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 13 | #include <test/log.h> |
| 14 | #include <test/ut.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 17 | |
| 18 | /* emit some sample log records in different ways, for testing */ |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 19 | static int do_log_run(struct unit_test_state *uts, int cat, const char *file) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 20 | { |
| 21 | int i; |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 22 | int ret, expected_ret; |
| 23 | |
| 24 | if (gd->flags & GD_FLG_LOG_READY) |
| 25 | expected_ret = 0; |
| 26 | else |
| 27 | expected_ret = -ENOSYS; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 28 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 29 | gd->log_fmt = LOGF_TEST; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 30 | debug("debug\n"); |
| 31 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
| 32 | log(cat, i, "log %d\n", i); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 33 | ret = _log(log_uc_cat(cat), i, file, 100 + i, |
| 34 | "func", "_log %d\n", i); |
| 35 | ut_asserteq(ret, expected_ret); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 36 | } |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 37 | /* test with LOGL_COUNT flag */ |
| 38 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 39 | 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 42 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 43 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 44 | gd->log_fmt = log_get_default_format(); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 48 | #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") |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 51 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 52 | #define EXPECT_LOG BIT(0) |
| 53 | #define EXPECT_DIRECT BIT(1) |
| 54 | #define EXPECT_EXTRA BIT(2) |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 55 | #define EXPECT_FORCE BIT(3) |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 56 | #define EXPECT_DEBUG BIT(4) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 57 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 58 | static int do_check_log_entries(struct unit_test_state *uts, int flags, int min, |
| 59 | int max) |
| 60 | { |
| 61 | int i; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 62 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 63 | 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 Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 68 | if (flags & EXPECT_DEBUG) { |
| 69 | ut_assert_nextline("log %d", i); |
| 70 | ut_assert_nextline("_log %d", i); |
| 71 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 72 | } |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 73 | if (flags & EXPECT_EXTRA) |
| 74 | for (; i <= LOGL_MAX ; i++) |
| 75 | ut_assert_nextline("func() _log %d", i); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 76 | |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 77 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
| 78 | if (flags & EXPECT_FORCE) |
| 79 | ut_assert_nextline("func() _log force %d", i); |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 80 | if (flags & EXPECT_DEBUG) |
| 81 | ut_assert_nextline("_log force %d", i); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 84 | ut_assert_console_end(); |
| 85 | return 0; |
| 86 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 87 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 88 | #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) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 93 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 94 | #define check_log_entries_flags(flags) \ |
| 95 | check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL) |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 96 | #define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 97 | #define check_log_entries_extra() \ |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 98 | 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 Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 100 | |
| 101 | /* Check a category filter using the first category */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 102 | int log_test_cat_allow(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 103 | { |
| 104 | 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(); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 120 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 121 | ut_assertok(log_remove_filter("console", filt)); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 124 | LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 125 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 126 | /* Check a category filter that should block log entries */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 127 | int log_test_cat_deny_implicit(struct unit_test_state *uts) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 128 | { |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 144 | LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 145 | |
| 146 | /* Check passing and failing file filters */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 147 | int log_test_file(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 148 | { |
| 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 156 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 157 | |
| 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 165 | LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 166 | |
| 167 | /* Check a passing file filter (second in list) */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 168 | int log_test_file_second(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 169 | { |
| 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 177 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 178 | |
| 179 | ut_assertok(log_remove_filter("console", filt)); |
| 180 | return 0; |
| 181 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 182 | LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 183 | |
| 184 | /* Check a passing file filter (middle of list) */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 185 | int log_test_file_mid(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 186 | { |
| 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 200 | LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 201 | |
| 202 | /* Check a log level filter */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 203 | int log_test_level(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 204 | { |
| 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 212 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
| 213 | LOGL_FIRST, LOGL_WARNING); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 214 | |
| 215 | ut_assertok(log_remove_filter("console", filt)); |
| 216 | return 0; |
| 217 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 218 | LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 219 | |
| 220 | /* Check two filters, one of which passes everything */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 221 | int log_test_double(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 222 | { |
| 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 238 | LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 239 | |
| 240 | /* Check three filters, which together pass everything */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 241 | int log_test_triple(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 242 | { |
| 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 261 | LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 262 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 263 | int do_log_test_helpers(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 264 | { |
| 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 285 | int log_test_helpers(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 286 | { |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 287 | int ret; |
| 288 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 289 | gd->log_fmt = LOGF_TEST; |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 290 | ret = do_log_test_helpers(uts); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 291 | gd->log_fmt = log_get_default_format(); |
| 292 | return ret; |
| 293 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 294 | LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 295 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 296 | int do_log_test_disable(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 297 | { |
| 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"); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 304 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 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 Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 312 | int log_test_disable(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 313 | { |
| 314 | int ret; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 315 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 316 | gd->log_fmt = LOGF_TEST; |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 317 | ret = do_log_test_disable(uts); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 318 | gd->log_fmt = log_get_default_format(); |
| 319 | return ret; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 320 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 321 | LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC); |
Sean Anderson | 14e9986 | 2020-10-27 19:55:29 -0400 | [diff] [blame] | 322 | |
| 323 | /* Check denying based on category */ |
| 324 | int 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 | } |
| 345 | LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC); |
| 346 | |
| 347 | /* Check denying based on file */ |
| 348 | int 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 | } |
| 366 | LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC); |
| 367 | |
| 368 | /* Check denying based on level */ |
| 369 | int 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 381 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
Sean Anderson | 14e9986 | 2020-10-27 19:55:29 -0400 | [diff] [blame] | 382 | 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 | } |
| 388 | LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC); |
Sean Anderson | 4147317 | 2020-10-27 19:55:31 -0400 | [diff] [blame] | 389 | |
| 390 | /* Check matching based on minimum level */ |
| 391 | int 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 Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 404 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
Sean Anderson | 4147317 | 2020-10-27 19:55:31 -0400 | [diff] [blame] | 405 | 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 | } |
| 411 | LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 412 | |
| 413 | /* Check dropped traces */ |
| 414 | int 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 Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 424 | check_log_entries_flags_levels(EXPECT_DEBUG, LOGL_FIRST, CONFIG_LOG_DEFAULT_LEVEL); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 425 | |
| 426 | gd->flags |= GD_FLG_LOG_READY; |
| 427 | gd->log_drop_count = 0; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | LOG_TEST_FLAGS(log_test_dropped, UT_TESTF_CONSOLE_REC); |
Simon Glass | 7fb9759 | 2021-05-08 07:00:06 -0600 | [diff] [blame] | 432 | |
| 433 | /* Check log_buffer() */ |
| 434 | int log_test_buffer(struct unit_test_state *uts) |
| 435 | { |
| 436 | u8 *buf; |
| 437 | int i; |
| 438 | |
| 439 | buf = malloc(0x20); |
| 440 | ut_assertnonnull(buf); |
| 441 | memset(buf, '\0', 0x20); |
| 442 | for (i = 0; i < 0x11; i++) |
| 443 | buf[i] = i * 0x11; |
| 444 | |
| 445 | ut_assertok(console_record_reset_enable()); |
| 446 | log_buffer(LOGC_BOOT, LOGL_INFO, 0, buf, 1, 0x12, 0); |
| 447 | |
| 448 | /* This one should product no output due to the debug level */ |
| 449 | log_buffer(LOGC_BOOT, LOGL_DEBUG, 0, buf, 1, 0x12, 0); |
| 450 | |
| 451 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 452 | ut_assert_nextline("00000010: 10 00 .."); |
| 453 | ut_assert_console_end(); |
| 454 | free(buf); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | LOG_TEST_FLAGS(log_test_buffer, UT_TESTF_CONSOLE_REC); |