1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4  */
5 
6 #include <common.h>
7 #include <console.h>
8 #include <log.h>
9 #include <asm/global_data.h>
10 #include <test/log.h>
11 #include <test/ut.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
14 
15 /* Test invalid options */
log_test_filter_invalid(struct unit_test_state * uts)16 static int log_test_filter_invalid(struct unit_test_state *uts)
17 {
18 	ut_asserteq(1, run_command("log filter-add -AD", 0));
19 	ut_asserteq(1, run_command("log filter-add -l1 -L1", 0));
20 	ut_asserteq(1, run_command("log filter-add -l1 -L1", 0));
21 	ut_asserteq(1, run_command("log filter-add -lfoo", 0));
22 	ut_asserteq(1, run_command("log filter-add -cfoo", 0));
23 	ut_asserteq(1, run_command("log filter-add -ccore -ccore -ccore -ccore "
24 				   "-ccore -ccore", 0));
25 
26 	return 0;
27 }
28 LOG_TEST_FLAGS(log_test_filter_invalid, UT_TESTF_CONSOLE_REC);
29 
30 /* Test adding and removing filters */
log_test_filter(struct unit_test_state * uts)31 static int log_test_filter(struct unit_test_state *uts)
32 {
33 	bool any_found = false;
34 	bool filt1_found = false;
35 	bool filt2_found = false;
36 	char cmd[32];
37 	struct log_filter *filt;
38 	struct log_device *ldev;
39 	ulong filt1, filt2;
40 
41 #define create_filter(args, filter_num) do {\
42 	ut_assertok(console_record_reset_enable()); \
43 	ut_assertok(run_command("log filter-add -p " args, 0)); \
44 	ut_assert_skipline(); \
45 	ut_assertok(strict_strtoul(uts->actual_str, 10, &(filter_num))); \
46 	ut_assert_console_end(); \
47 } while (0)
48 
49 	create_filter("", filt1);
50 	create_filter("-DL warning -cmmc -cspi -ffile", filt2);
51 
52 	ldev = log_device_find_by_name("console");
53 	ut_assertnonnull(ldev);
54 	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
55 		if (filt->filter_num == filt1) {
56 			filt1_found = true;
57 			ut_asserteq(0, filt->flags);
58 			ut_asserteq(LOGL_MAX, filt->level);
59 			ut_assertnull(filt->file_list);
60 		} else if (filt->filter_num == filt2) {
61 			filt2_found = true;
62 			ut_asserteq(LOGFF_HAS_CAT | LOGFF_DENY |
63 				    LOGFF_LEVEL_MIN, filt->flags);
64 			ut_asserteq(true, log_has_cat(filt->cat_list,
65 						      log_uc_cat(UCLASS_MMC)));
66 			ut_asserteq(true, log_has_cat(filt->cat_list,
67 						      log_uc_cat(UCLASS_SPI)));
68 			ut_asserteq(LOGL_WARNING, filt->level);
69 			ut_asserteq_str("file", filt->file_list);
70 		}
71 	}
72 	ut_asserteq(true, filt1_found);
73 	ut_asserteq(true, filt2_found);
74 
75 #define remove_filter(filter_num) do { \
76 	ut_assertok(console_record_reset_enable()); \
77 	snprintf(cmd, sizeof(cmd), "log filter-remove %lu", filter_num); \
78 	ut_assertok(run_command(cmd, 0)); \
79 	ut_assert_console_end(); \
80 } while (0)
81 
82 	remove_filter(filt1);
83 	remove_filter(filt2);
84 
85 	filt1_found = false;
86 	filt2_found = false;
87 	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
88 		if (filt->filter_num == filt1)
89 			filt1_found = true;
90 		else if (filt->filter_num == filt2)
91 			filt2_found = true;
92 	}
93 	ut_asserteq(false, filt1_found);
94 	ut_asserteq(false, filt2_found);
95 
96 	create_filter("", filt1);
97 	create_filter("", filt2);
98 
99 	ut_assertok(console_record_reset_enable());
100 	ut_assertok(run_command("log filter-remove -a", 0));
101 	ut_assert_console_end();
102 
103 	list_for_each_entry(filt, &ldev->filter_head, sibling_node)
104 		any_found = true;
105 	ut_asserteq(false, any_found);
106 
107 	return 0;
108 }
109 LOG_TEST_FLAGS(log_test_filter, UT_TESTF_CONSOLE_REC);
110