summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/Kconfig10
-rw-r--r--test/Makefile24
-rw-r--r--test/dm/Makefile13
-rw-r--r--test/dm/of_platdata.c222
-rw-r--r--test/dm/ofnode.c22
-rw-r--r--test/dm/test-main.c69
-rw-r--r--test/lib/Makefile1
-rw-r--r--test/lib/getopt.c123
-rw-r--r--test/log/Makefile1
-rw-r--r--test/log/log_filter.c108
-rw-r--r--test/log/log_test.c530
-rw-r--r--test/log/syslog_test.h2
-rw-r--r--test/py/conftest.py13
-rw-r--r--test/py/tests/test_log.py104
-rw-r--r--test/py/tests/test_ofplatdata.py47
-rw-r--r--test/py/tests/test_spl.py34
-rwxr-xr-xtest/run2
17 files changed, 940 insertions, 385 deletions
diff --git a/test/Kconfig b/test/Kconfig
index 28704a25b6..2646e7d825 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -6,6 +6,16 @@ menuconfig UNIT_TEST
This does not require sandbox to be included, but it is most
often used there.
+config SPL_UNIT_TEST
+ bool "Unit tests in SPL"
+ # We need to be able to unbind devices for tests to work
+ select SPL_DM_DEVICE_REMOVE
+ help
+ Select this to enable unit tests in SPL. Most test are designed for
+ running in U-Boot proper, but some are intended for SPL, such as
+ of-platdata and SPL handover. To run these tests with the sandbox_spl
+ board, use the -u (unit test) option.
+
config UT_LIB
bool "Unit tests for library functions"
depends on UNIT_TEST
diff --git a/test/Makefile b/test/Makefile
index 7c4039964e..1c930b3148 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -2,15 +2,19 @@
#
# (C) Copyright 2012 The Chromium Authors
-obj-$(CONFIG_SANDBOX) += bloblist.o
-obj-$(CONFIG_CMDLINE) += cmd/
-obj-$(CONFIG_UNIT_TEST) += cmd_ut.o
-obj-$(CONFIG_UNIT_TEST) += ut.o
-obj-$(CONFIG_SANDBOX) += command_ut.o
-obj-$(CONFIG_SANDBOX) += compression.o
-obj-$(CONFIG_SANDBOX) += print_ut.o
-obj-$(CONFIG_SANDBOX) += str_ut.o
+obj-$(CONFIG_$(SPL_)CMDLINE) += bloblist.o
+obj-$(CONFIG_$(SPL_)CMDLINE) += cmd/
+obj-$(CONFIG_$(SPL_)CMDLINE) += cmd_ut.o
+obj-$(CONFIG_$(SPL_)CMDLINE) += command_ut.o
+obj-$(CONFIG_$(SPL_)CMDLINE) += compression.o
+obj-y += dm/
+obj-$(CONFIG_$(SPL_)CMDLINE) += print_ut.o
+obj-$(CONFIG_$(SPL_)CMDLINE) += str_ut.o
obj-$(CONFIG_UT_TIME) += time_ut.o
-obj-$(CONFIG_UT_UNICODE) += unicode_ut.o
-obj-y += log/
+obj-y += ut.o
+
+ifeq ($(CONFIG_SPL_BUILD),)
obj-$(CONFIG_UNIT_TEST) += lib/
+obj-y += log/
+obj-$(CONFIG_$(SPL_)UT_UNICODE) += unicode_ut.o
+endif
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 8b3d77e34e..57e13ad533 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -2,15 +2,18 @@
#
# Copyright (c) 2013 Google, Inc
+obj-$(CONFIG_UT_DM) += test-main.o
+
+# Tests for particular subsystems - when enabling driver model for a new
+# subsystem you must add sandbox tests here.
+ifeq ($(CONFIG_SPL_BUILD),y)
+obj-$(CONFIG_SPL_OF_PLATDATA) += of_platdata.o
+else
obj-$(CONFIG_UT_DM) += bus.o
-obj-$(CONFIG_UT_DM) += nop.o
obj-$(CONFIG_UT_DM) += test-driver.o
obj-$(CONFIG_UT_DM) += test-fdt.o
-obj-$(CONFIG_UT_DM) += test-main.o
obj-$(CONFIG_UT_DM) += test-uclass.o
-# Tests for particular subsystems - when enabling driver model for a new
-# subsystem you must add sandbox tests here.
obj-$(CONFIG_UT_DM) += core.o
ifneq ($(CONFIG_SANDBOX),)
obj-$(CONFIG_ACPIGEN) += acpi.o
@@ -36,6 +39,7 @@ obj-$(CONFIG_DM_MAILBOX) += mailbox.o
obj-$(CONFIG_DM_MMC) += mmc.o
obj-$(CONFIG_CMD_MUX) += mux-cmd.o
obj-y += fdtdec.o
+obj-$(CONFIG_UT_DM) += nop.o
obj-y += ofnode.o
obj-y += ofread.o
obj-$(CONFIG_OSD) += osd.o
@@ -88,3 +92,4 @@ ifneq ($(CONFIG_PINMUX),)
obj-$(CONFIG_PINCONF) += pinmux.o
endif
endif
+endif # !SPL
diff --git a/test/dm/of_platdata.c b/test/dm/of_platdata.c
new file mode 100644
index 0000000000..4f3cc159d0
--- /dev/null
+++ b/test/dm/of_platdata.c
@@ -0,0 +1,222 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <common.h>
+#include <dm.h>
+#include <dt-structs.h>
+#include <dm/test.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+/* Test that we can find a device using of-platdata */
+static int dm_test_of_platdata_base(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+
+ ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
+ ut_asserteq_str("sandbox_serial", dev->name);
+
+ return 0;
+}
+DM_TEST(dm_test_of_platdata_base, UT_TESTF_SCAN_PDATA);
+
+/* Test that we can read properties from a device */
+static int dm_test_of_platdata_props(struct unit_test_state *uts)
+{
+ struct dtd_sandbox_spl_test *plat;
+ struct udevice *dev;
+ int i;
+
+ /* Skip the clock */
+ ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
+ ut_asserteq_str("sandbox_clk_test", dev->name);
+
+ ut_assertok(uclass_next_device_err(&dev));
+ plat = dev_get_platdata(dev);
+ ut_assert(plat->boolval);
+ ut_asserteq(1, plat->intval);
+ ut_asserteq(4, ARRAY_SIZE(plat->intarray));
+ ut_asserteq(2, plat->intarray[0]);
+ ut_asserteq(3, plat->intarray[1]);
+ ut_asserteq(4, plat->intarray[2]);
+ ut_asserteq(0, plat->intarray[3]);
+ ut_asserteq(5, plat->byteval);
+ ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
+ ut_asserteq(6, plat->bytearray[0]);
+ ut_asserteq(0, plat->bytearray[1]);
+ ut_asserteq(0, plat->bytearray[2]);
+ ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
+ for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
+ ut_asserteq(9 + i, plat->longbytearray[i]);
+ ut_asserteq_str("message", plat->stringval);
+ ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
+ ut_asserteq_str("multi-word", plat->stringarray[0]);
+ ut_asserteq_str("message", plat->stringarray[1]);
+ ut_asserteq_str("", plat->stringarray[2]);
+
+ ut_assertok(uclass_next_device_err(&dev));
+ plat = dev_get_platdata(dev);
+ ut_assert(!plat->boolval);
+ ut_asserteq(3, plat->intval);
+ ut_asserteq(5, plat->intarray[0]);
+ ut_asserteq(0, plat->intarray[1]);
+ ut_asserteq(0, plat->intarray[2]);
+ ut_asserteq(0, plat->intarray[3]);
+ ut_asserteq(8, plat->byteval);
+ ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
+ ut_asserteq(1, plat->bytearray[0]);
+ ut_asserteq(0x23, plat->bytearray[1]);
+ ut_asserteq(0x34, plat->bytearray[2]);
+ for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
+ ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
+ ut_asserteq_str("message2", plat->stringval);
+ ut_asserteq_str("another", plat->stringarray[0]);
+ ut_asserteq_str("multi-word", plat->stringarray[1]);
+ ut_asserteq_str("message", plat->stringarray[2]);
+
+ ut_assertok(uclass_next_device_err(&dev));
+ plat = dev_get_platdata(dev);
+ ut_assert(!plat->boolval);
+ ut_asserteq_str("one", plat->stringarray[0]);
+ ut_asserteq_str("", plat->stringarray[1]);
+ ut_asserteq_str("", plat->stringarray[2]);
+
+ ut_assertok(uclass_next_device_err(&dev));
+ plat = dev_get_platdata(dev);
+ ut_assert(!plat->boolval);
+ ut_asserteq_str("spl", plat->stringarray[0]);
+
+ ut_asserteq(-ENODEV, uclass_next_device_err(&dev));
+
+ return 0;
+}
+DM_TEST(dm_test_of_platdata_props, UT_TESTF_SCAN_PDATA);
+
+/*
+ * find_driver_info - recursively find the driver_info for a device
+ *
+ * This sets found[idx] to true when it finds the driver_info record for a
+ * device, where idx is the index in the driver_info linker list.
+ *
+ * @uts: Test state
+ * @parent: Parent to search
+ * @found: bool array to update
+ * @return 0 if OK, non-zero on error
+ */
+static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
+ bool found[])
+{
+ struct udevice *dev;
+
+ /* If not the root device, find the entry that caused it to be bound */
+ if (parent->parent) {
+ const int n_ents =
+ ll_entry_count(struct driver_info, driver_info);
+ int idx = -1;
+ int i;
+
+ for (i = 0; i < n_ents; i++) {
+ const struct driver_rt *drt = gd_dm_driver_rt() + i;
+
+ if (drt->dev == parent) {
+ idx = i;
+ found[idx] = true;
+ break;
+ }
+ }
+
+ ut_assert(idx != -1);
+ }
+
+ device_foreach_child(dev, parent) {
+ int ret;
+
+ ret = find_driver_info(uts, dev, found);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+/* Check that every device is recorded in its driver_info struct */
+static int dm_test_of_platdata_dev(struct unit_test_state *uts)
+{
+ const struct driver_info *info =
+ ll_entry_start(struct driver_info, driver_info);
+ const int n_ents = ll_entry_count(struct driver_info, driver_info);
+ bool found[n_ents];
+ uint i;
+
+ /* Record the indexes that are found */
+ memset(found, '\0', sizeof(found));
+ ut_assertok(find_driver_info(uts, gd->dm_root, found));
+
+ /* Make sure that the driver entries without devices have no ->dev */
+ for (i = 0; i < n_ents; i++) {
+ const struct driver_rt *drt = gd_dm_driver_rt() + i;
+ const struct driver_info *entry = info + i;
+ struct udevice *dev;
+
+ if (found[i]) {
+ /* Make sure we can find it */
+ ut_assertnonnull(drt->dev);
+ ut_assertok(device_get_by_driver_info(entry, &dev));
+ ut_asserteq_ptr(dev, drt->dev);
+ } else {
+ ut_assertnull(drt->dev);
+ ut_asserteq(-ENOENT,
+ device_get_by_driver_info(entry, &dev));
+ }
+ }
+
+ return 0;
+}
+DM_TEST(dm_test_of_platdata_dev, UT_TESTF_SCAN_PDATA);
+
+/* Test handling of phandles that point to other devices */
+static int dm_test_of_platdata_phandle(struct unit_test_state *uts)
+{
+ struct dtd_sandbox_clk_test *plat;
+ struct udevice *dev, *clk;
+
+ ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
+ ut_asserteq_str("sandbox_clk_test", dev->name);
+ plat = dev_get_platdata(dev);
+
+ ut_assertok(device_get_by_driver_info_idx(plat->clocks[0].idx, &clk));
+ ut_asserteq_str("fixed_clock", clk->name);
+
+ ut_assertok(device_get_by_driver_info_idx(plat->clocks[1].idx, &clk));
+ ut_asserteq_str("sandbox_clk", clk->name);
+ ut_asserteq(1, plat->clocks[1].arg[0]);
+
+ ut_assertok(device_get_by_driver_info_idx(plat->clocks[2].idx, &clk));
+ ut_asserteq_str("sandbox_clk", clk->name);
+ ut_asserteq(0, plat->clocks[2].arg[0]);
+
+ ut_assertok(device_get_by_driver_info_idx(plat->clocks[3].idx, &clk));
+ ut_asserteq_str("sandbox_clk", clk->name);
+ ut_asserteq(3, plat->clocks[3].arg[0]);
+
+ ut_assertok(device_get_by_driver_info_idx(plat->clocks[4].idx, &clk));
+ ut_asserteq_str("sandbox_clk", clk->name);
+ ut_asserteq(2, plat->clocks[4].arg[0]);
+
+ return 0;
+}
+DM_TEST(dm_test_of_platdata_phandle, UT_TESTF_SCAN_PDATA);
+
+#if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
+/* Test that device parents are correctly set up */
+static int dm_test_of_platdata_parent(struct unit_test_state *uts)
+{
+ struct udevice *rtc, *i2c;
+
+ ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
+ ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
+ ut_asserteq_ptr(i2c, dev_get_parent(rtc));
+
+ return 0;
+}
+DM_TEST(dm_test_of_platdata_parent, UT_TESTF_SCAN_PDATA);
+#endif
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index 01ac3c2094..fb1ceb1318 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -207,6 +207,28 @@ static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
}
DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
+{
+ const void *val;
+ ofnode node;
+ int size;
+
+ node = ofnode_get_aliases_node("eth3");
+ ut_assert(ofnode_valid(node));
+ ut_asserteq_str("sbe5", ofnode_get_name(node));
+
+ node = ofnode_get_aliases_node("unknown");
+ ut_assert(!ofnode_valid(node));
+
+ val = ofnode_read_aliases_prop("spi0", &size);
+ ut_assertnonnull(val);
+ ut_asserteq(7, size);
+ ut_asserteq_str("/spi@0", (const char *)val);
+
+ return 0;
+}
+DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
{
ofnode node, child_node;
diff --git a/test/dm/test-main.c b/test/dm/test-main.c
index 38b7b1481a..fd24635006 100644
--- a/test/dm/test-main.c
+++ b/test/dm/test-main.c
@@ -30,13 +30,12 @@ static int dm_test_init(struct unit_test_state *uts, bool of_live)
memset(dms, '\0', sizeof(*dms));
gd->dm_root = NULL;
- memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
+ if (!CONFIG_IS_ENABLED(OF_PLATDATA))
+ memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
state_reset_for_test(state_get_current());
-#ifdef CONFIG_OF_LIVE
/* Determine whether to make the live tree available */
- gd->of_root = of_live ? uts->of_root : NULL;
-#endif
+ gd_set_of_root(of_live ? uts->of_root : NULL);
ut_assertok(dm_init(of_live));
dms->root = dm_root();
@@ -93,7 +92,8 @@ static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
ut_assertok(dm_scan_platdata(false));
if (test->flags & UT_TESTF_PROBE_TEST)
ut_assertok(do_autoprobe(uts));
- if (test->flags & UT_TESTF_SCAN_FDT)
+ if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
+ (test->flags & UT_TESTF_SCAN_FDT))
ut_assertok(dm_extended_scan_fdt(gd->fdt_blob, false));
/*
@@ -127,7 +127,25 @@ static bool dm_test_run_on_flattree(struct unit_test *test)
return !strstr(fname, "video") || strstr(test->name, "video_base");
}
-static int dm_test_main(const char *test_name)
+static bool test_matches(const char *test_name, const char *find_name)
+{
+ if (!find_name)
+ return true;
+
+ if (!strcmp(test_name, find_name))
+ return true;
+
+ /* All tests have this prefix */
+ if (!strncmp(test_name, "dm_test_", 8))
+ test_name += 8;
+
+ if (!strcmp(test_name, find_name))
+ return true;
+
+ return false;
+}
+
+int dm_test_main(const char *test_name)
{
struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
const int n_ents = ll_entry_count(struct unit_test, dm_test);
@@ -138,36 +156,34 @@ static int dm_test_main(const char *test_name)
uts->priv = &_global_priv_dm_test_state;
uts->fail_count = 0;
- /*
- * If we have no device tree, or it only has a root node, then these
- * tests clearly aren't going to work...
- */
- if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
- puts("Please run with test device tree:\n"
- " ./u-boot -d arch/sandbox/dts/test.dtb\n");
- ut_assert(gd->fdt_blob);
+ if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
+ /*
+ * If we have no device tree, or it only has a root node, then
+ * these * tests clearly aren't going to work...
+ */
+ if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
+ puts("Please run with test device tree:\n"
+ " ./u-boot -d arch/sandbox/dts/test.dtb\n");
+ ut_assert(gd->fdt_blob);
+ }
}
if (!test_name)
printf("Running %d driver model tests\n", n_ents);
+ else
found = 0;
-#ifdef CONFIG_OF_LIVE
- uts->of_root = gd->of_root;
-#endif
+ uts->of_root = gd_of_root();
for (test = tests; test < tests + n_ents; test++) {
const char *name = test->name;
int runs;
- /* All tests have this prefix */
- if (!strncmp(name, "dm_test_", 8))
- name += 8;
- if (test_name && strcmp(test_name, name))
+ if (!test_matches(name, test_name))
continue;
/* Run with the live tree if possible */
runs = 0;
- if (IS_ENABLED(CONFIG_OF_LIVE)) {
+ if (CONFIG_IS_ENABLED(OF_LIVE)) {
if (!(test->flags & UT_TESTF_FLAT_TREE)) {
ut_assertok(dm_do_test(uts, test, true));
runs++;
@@ -192,13 +208,12 @@ static int dm_test_main(const char *test_name)
printf("Failures: %d\n", uts->fail_count);
/* Put everything back to normal so that sandbox works as expected */
-#ifdef CONFIG_OF_LIVE
- gd->of_root = uts->of_root;
-#endif
+ gd_set_of_root(uts->of_root);
gd->dm_root = NULL;
- ut_assertok(dm_init(IS_ENABLED(CONFIG_OF_LIVE)));
+ ut_assertok(dm_init(CONFIG_IS_ENABLED(OF_LIVE)));
dm_scan_platdata(false);
- dm_scan_fdt(gd->fdt_blob, false);
+ if (!CONFIG_IS_ENABLED(OF_PLATDATA))
+ dm_scan_fdt(gd->fdt_blob, false);
return uts->fail_count ? CMD_RET_FAILURE : 0;
}
diff --git a/test/lib/Makefile b/test/lib/Makefile
index 15cd512506..98a9abf40e 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -14,3 +14,4 @@ obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
obj-$(CONFIG_UT_LIB_RSA) += rsa.o
obj-$(CONFIG_AES) += test_aes.o
+obj-$(CONFIG_GETOPT) += getopt.o
diff --git a/test/lib/getopt.c b/test/lib/getopt.c
new file mode 100644
index 0000000000..3c68b93c8a
--- /dev/null
+++ b/test/lib/getopt.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
+ *
+ * Portions of these tests were inspired by glibc's posix/bug-getopt1.c and
+ * posix/tst-getopt-cancel.c
+ */
+
+#include <common.h>
+#include <getopt.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int do_test_getopt(struct unit_test_state *uts, int line,
+ struct getopt_state *gs, const char *optstring,
+ int args, char *argv[], int expected_count,
+ int expected[])
+{
+ int opt;
+
+ getopt_init_state(gs);
+ for (int i = 0; i < expected_count; i++) {
+ opt = getopt_silent(gs, args, argv, optstring);
+ if (expected[i] != opt) {
+ /*
+ * Fudge the line number so we can tell which test
+ * failed
+ */
+ ut_failf(uts, __FILE__, line, __func__,
+ "expected[i] == getopt()",
+ "Expected '%c' (%d) with i=%d, got '%c' (%d)",
+ expected[i], expected[i], i, opt, opt);
+ return CMD_RET_FAILURE;
+ }
+ }
+
+ opt = getopt_silent(gs, args, argv, optstring);
+ if (opt != -1) {
+ ut_failf(uts, __FILE__, line, __func__,
+ "getopt() != -1",
+ "Expected -1, got '%c' (%d)", opt, opt);
+ return CMD_RET_FAILURE;
+ }
+
+ return 0;
+}
+
+#define test_getopt(optstring, argv, expected) do { \
+ int ret = do_test_getopt(uts, __LINE__, &gs, optstring, \
+ ARRAY_SIZE(argv) - 1, argv, \
+ ARRAY_SIZE(expected), expected); \
+ if (ret) \
+ return ret; \
+} while (0)
+
+static int lib_test_getopt(struct unit_test_state *uts)
+{
+ struct getopt_state gs;
+
+ /* Happy path */
+ test_getopt("ab:c",
+ ((char *[]){ "program", "-cb", "x", "-a", "foo", 0 }),
+ ((int []){ 'c', 'b', 'a' }));
+ ut_asserteq(4, gs.index);
+
+ /* Make sure we pick up the optional argument */
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-cbx", "-a", "foo", 0 }),
+ ((int []){ 'c', 'b', 'a' }));
+ ut_asserteq(4, gs.index);
+
+ /* Test required arguments */
+ test_getopt("a:b", ((char *[]){ "program", "-a", 0 }),
+ ((int []){ ':' }));
+ ut_asserteq('a', gs.opt);
+ test_getopt("a:b", ((char *[]){ "program", "-b", "-a", 0 }),
+ ((int []){ 'b', ':' }));
+ ut_asserteq('a', gs.opt);
+
+ /* Test invalid arguments */
+ test_getopt("ab:c", ((char *[]){ "program", "-d", 0 }),
+ ((int []){ '?' }));
+ ut_asserteq('d', gs.opt);
+
+ /* Test arg */
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-a", 0 }),
+ ((int []){ 'a' }));
+ ut_asserteq(2, gs.index);
+ ut_assertnull(gs.arg);
+
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-afoo", 0 }),
+ ((int []){ 'a' }));
+ ut_asserteq(2, gs.index);
+ ut_assertnonnull(gs.arg);
+ ut_asserteq_str("foo", gs.arg);
+
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-a", "foo", 0 }),
+ ((int []){ 'a' }));
+ ut_asserteq(3, gs.index);
+ ut_assertnonnull(gs.arg);
+ ut_asserteq_str("foo", gs.arg);
+
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-bfoo", 0 }),
+ ((int []){ 'b' }));
+ ut_asserteq(2, gs.index);
+ ut_assertnonnull(gs.arg);
+ ut_asserteq_str("foo", gs.arg);
+
+ test_getopt("a::b:c",
+ ((char *[]){ "program", "-b", "foo", 0 }),
+ ((int []){ 'b' }));
+ ut_asserteq(3, gs.index);
+ ut_assertnonnull(gs.arg);
+ ut_asserteq_str("foo", gs.arg);
+
+ return 0;
+}
+LIB_TEST(lib_test_getopt, 0);
diff --git a/test/log/Makefile b/test/log/Makefile
index fdf529582d..88bc573e9f 100644
--- a/test/log/Makefile
+++ b/test/log/Makefile
@@ -3,6 +3,7 @@
# Copyright (c) 2017 Google, Inc
obj-$(CONFIG_LOG_TEST) += log_test.o
+obj-$(CONFIG_CMD_LOG) += log_filter.o
ifdef CONFIG_UT_LOG
diff --git a/test/log/log_filter.c b/test/log/log_filter.c
new file mode 100644
index 0000000000..e8a6e01a5c
--- /dev/null
+++ b/test/log/log_filter.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <log.h>
+#include <test/log.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Test invalid options */
+static int log_test_filter_invalid(struct unit_test_state *uts)
+{
+ ut_asserteq(1, run_command("log filter-add -AD", 0));
+ ut_asserteq(1, run_command("log filter-add -l1 -L1", 0));
+ ut_asserteq(1, run_command("log filter-add -l1 -L1", 0));
+ ut_asserteq(1, run_command("log filter-add -lfoo", 0));
+ ut_asserteq(1, run_command("log filter-add -cfoo", 0));
+ ut_asserteq(1, run_command("log filter-add -ccore -ccore -ccore -ccore "
+ "-ccore -ccore", 0));
+
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_filter_invalid, UT_TESTF_CONSOLE_REC);
+
+/* Test adding and removing filters */
+static int log_test_filter(struct unit_test_state *uts)
+{
+ bool any_found = false;
+ bool filt1_found = false;
+ bool filt2_found = false;
+ char cmd[32];
+ struct log_filter *filt;
+ struct log_device *ldev;
+ ulong filt1, filt2;
+
+#define create_filter(args, filter_num) do {\
+ ut_assertok(console_record_reset_enable()); \
+ ut_assertok(run_command("log filter-add -p " args, 0)); \
+ ut_assert_skipline(); \
+ ut_assertok(strict_strtoul(uts->actual_str, 10, &(filter_num))); \
+ ut_assert_console_end(); \
+} while (0)
+
+ create_filter("", filt1);
+ create_filter("-DL warning -cmmc -cspi -ffile", filt2);
+
+ ldev = log_device_find_by_name("console");
+ ut_assertnonnull(ldev);
+ list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
+ if (filt->filter_num == filt1) {
+ filt1_found = true;
+ ut_asserteq(0, filt->flags);
+ ut_asserteq(LOGL_MAX, filt->level);
+ ut_assertnull(filt->file_list);
+ } else if (filt->filter_num == filt2) {
+ filt2_found = true;
+ ut_asserteq(LOGFF_HAS_CAT | LOGFF_DENY |
+ LOGFF_LEVEL_MIN, filt->flags);
+ ut_asserteq(true, log_has_cat(filt->cat_list,
+ log_uc_cat(UCLASS_MMC)));
+ ut_asserteq(true, log_has_cat(filt->cat_list,
+ log_uc_cat(UCLASS_SPI)));
+ ut_asserteq(LOGL_WARNING, filt->level);
+ ut_asserteq_str("file", filt->file_list);
+ }
+ }
+ ut_asserteq(true, filt1_found);
+ ut_asserteq(true, filt2_found);
+
+#define remove_filter(filter_num) do { \
+ ut_assertok(console_record_reset_enable()); \
+ snprintf(cmd, sizeof(cmd), "log filter-remove %lu", filter_num); \
+ ut_assertok(run_command(cmd, 0)); \
+ ut_assert_console_end(); \
+} while (0)
+
+ remove_filter(filt1);
+ remove_filter(filt2);
+
+ filt1_found = false;
+ filt2_found = false;
+ list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
+ if (filt->filter_num == filt1)
+ filt1_found = true;
+ else if (filt->filter_num == filt2)
+ filt2_found = true;
+ }
+ ut_asserteq(false, filt1_found);
+ ut_asserteq(false, filt2_found);
+
+ create_filter("", filt1);
+ create_filter("", filt2);
+
+ ut_assertok(console_record_reset_enable());
+ ut_assertok(run_command("log filter-remove -a", 0));
+ ut_assert_console_end();
+
+ list_for_each_entry(filt, &ldev->filter_head, sibling_node)
+ any_found = true;
+ ut_asserteq(false, any_found);
+
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_filter, UT_TESTF_CONSOLE_REC);
diff --git a/test/log/log_test.c b/test/log/log_test.c
index 6a60ff6be3..ea4fc6bc30 100644
--- a/test/log/log_test.c
+++ b/test/log/log_test.c
@@ -9,12 +9,17 @@
#include <common.h>
#include <command.h>
#include <log.h>
+#include <test/log.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
/* emit some sample log records in different ways, for testing */
-static int log_run(enum uclass_id cat, const char *file)
+static int do_log_run(int cat, const char *file)
{
int i;
+ gd->log_fmt = LOGF_TEST;
debug("debug\n");
for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
log(cat, i, "log %d\n", i);
@@ -22,203 +27,358 @@ static int log_run(enum uclass_id cat, const char *file)
i);
}
+ gd->log_fmt = log_get_default_format();
return 0;
}
-static int log_test(int testnum)
+#define log_run_cat(cat) do_log_run(cat, "file")
+#define log_run_file(file) do_log_run(UCLASS_SPI, file)
+#define log_run() do_log_run(UCLASS_SPI, "file")
+
+#define EXPECT_LOG BIT(0)
+#define EXPECT_DIRECT BIT(1)
+#define EXPECT_EXTRA BIT(2)
+
+static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
+ int max)
{
- int ret;
+ int i;
- printf("test %d\n", testnum);
- switch (testnum) {
- case 0: {
- /* Check a category filter using the first category */
- enum log_category_t cat_list[] = {
- log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
- LOGC_NONE, LOGC_END
- };
-
- ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
- if (ret < 0)
- return ret;
- log_run(UCLASS_MMC, "file");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 1: {
- /* Check a category filter using the second category */
- enum log_category_t cat_list[] = {
- log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI), LOGC_END
- };
-
- ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 2: {
- /* Check a category filter that should block log entries */
- enum log_category_t cat_list[] = {
- log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
- };
-
- ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 3: {
- /* Check a passing file filter */
- ret = log_add_filter("console", NULL, LOGL_MAX, "file");
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 4: {
- /* Check a failing file filter */
- ret = log_add_filter("console", NULL, LOGL_MAX, "file");
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file2");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 5: {
- /* Check a passing file filter (second in list) */
- ret = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file2");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 6: {
- /* Check a passing file filter */
- ret = log_add_filter("console", NULL, LOGL_MAX,
- "file,file2,log/log_test.c");
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file2");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 7: {
- /* Check a log level filter */
- ret = log_add_filter("console", NULL, LOGL_WARNING, NULL);
- if (ret < 0)
- return ret;
- log_run(UCLASS_SPI, "file");
- ret = log_remove_filter("console", ret);
- if (ret < 0)
- return ret;
- break;
- }
- case 8: {
- /* Check two filters, one of which passes everything */
- int filt1, filt2;
-
- ret = log_add_filter("console", NULL, LOGL_WARNING, NULL);
- if (ret < 0)
- return ret;
- filt1 = ret;
- ret = log_add_filter("console", NULL, LOGL_MAX, NULL);
- if (ret < 0)
- return ret;
- filt2 = ret;
- log_run(UCLASS_SPI, "file");
- ret = log_remove_filter("console", filt1);
- if (ret < 0)
- return ret;
- ret = log_remove_filter("console", filt2);
- if (ret < 0)
- return ret;
- break;
- }
- case 9: {
- /* Check three filters, which together pass everything */
- int filt1, filt2, filt3;
-
- ret = log_add_filter("console", NULL, LOGL_MAX, "file)");
- if (ret < 0)
- return ret;
- filt1 = ret;
- ret = log_add_filter("console", NULL, LOGL_MAX, "file2");
- if (ret < 0)
- return ret;
- filt2 = ret;
- ret = log_add_filter("console", NULL, LOGL_MAX,
- "log/log_test.c");
- if (ret < 0)
- return ret;
- filt3 = ret;
- log_run(UCLASS_SPI, "file2");
- ret = log_remove_filter("console", filt1);
- if (ret < 0)
- return ret;
- ret = log_remove_filter("console", filt2);
- if (ret < 0)
- return ret;
- ret = log_remove_filter("console", filt3);
- if (ret < 0)
- return ret;
- break;
- }
- case 10: {
- log_err("level %d\n", LOGL_EMERG);
- log_err("level %d\n", LOGL_ALERT);
- log_err("level %d\n", LOGL_CRIT);
- log_err("level %d\n", LOGL_ERR);
- log_warning("level %d\n", LOGL_WARNING);
- log_notice("level %d\n", LOGL_NOTICE);
- log_info("level %d\n", LOGL_INFO);
- log_debug("level %d\n", LOGL_DEBUG);
- log_content("level %d\n", LOGL_DEBUG_CONTENT);
- log_io("level %d\n", LOGL_DEBUG_IO);
- break;
- }
- case 11:
- log_err("default\n");
- ret = log_device_set_enable(LOG_GET_DRIVER(console), false);
- log_err("disabled\n");
- ret = log_device_set_enable(LOG_GET_DRIVER(console), true);
- log_err("enabled\n");
- break;
+ for (i = min; i <= max; i++) {
+ if (flags & EXPECT_LOG)
+ ut_assert_nextline("do_log_run() log %d", i);
+ if (flags & EXPECT_DIRECT)
+ ut_assert_nextline("func() _log %d", i);
}
+ if (flags & EXPECT_EXTRA)
+ for (; i <= LOGL_MAX ; i++)
+ ut_assert_nextline("func() _log %d", i);
+
+ ut_assert_console_end();
+ return 0;
+}
+
+#define check_log_entries_flags_levels(flags, min, max) do {\
+ int ret = do_check_log_entries(uts, flags, min, max); \
+ if (ret) \
+ return ret; \
+} while (0)
+
+#define check_log_entries_flags(flags) \
+ check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL)
+#define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT)
+#define check_log_entries_extra() \
+ check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA)
+#define check_log_entries_none() check_log_entries_flags(0)
+
+/* Check a category filter using the first category */
+int log_test_cat_allow(struct unit_test_state *uts)
+{
+ enum log_category_t cat_list[] = {
+ log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
+ LOGC_NONE, LOGC_END
+ };
+ int filt;
+
+ filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_cat(UCLASS_MMC);
+ check_log_entries_extra();
+
+ ut_assertok(console_record_reset_enable());
+ log_run_cat(UCLASS_SPI);
+ check_log_entries_extra();
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
+
+/* Check a category filter that should block log entries */
+int log_test_cat_deny_implicit(struct unit_test_state *uts)
+{
+ enum log_category_t cat_list[] = {
+ log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
+ };
+ int filt;
+
+ filt = log_add_filter("console", cat_list, LOGL_MAX, NULL);
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_cat(UCLASS_SPI);
+ check_log_entries_none();
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
+
+/* Check passing and failing file filters */
+int log_test_file(struct unit_test_state *uts)
+{
+ int filt;
+
+ filt = log_add_filter("console", NULL, LOGL_MAX, "file");
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file");
+ check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file2");
+ check_log_entries_none();
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
+
+/* Check a passing file filter (second in list) */
+int log_test_file_second(struct unit_test_state *uts)
+{
+ int filt;
+
+ filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file2");
+ check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA);
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
+
+/* Check a passing file filter (middle of list) */
+int log_test_file_mid(struct unit_test_state *uts)
+{
+ int filt;
+
+ filt = log_add_filter("console", NULL, LOGL_MAX,
+ "file,file2,log/log_test.c");
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file2");
+ check_log_entries_extra();
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
+
+/* Check a log level filter */
+int log_test_level(struct unit_test_state *uts)
+{
+ int filt;
+ filt = log_add_filter("console", NULL, LOGL_WARNING, NULL);
+ ut_assert(filt >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run();
+ check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT, LOGL_FIRST,
+ LOGL_WARNING);
+
+ ut_assertok(log_remove_filter("console", filt));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
+
+/* Check two filters, one of which passes everything */
+int log_test_double(struct unit_test_state *uts)
+{
+ int filt1, filt2;
+
+ filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL);
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL);
+ ut_assert(filt2 >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run();
+ check_log_entries_extra();
+
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
+
+/* Check three filters, which together pass everything */
+int log_test_triple(struct unit_test_state *uts)
+{
+ int filt1, filt2, filt3;
+
+ filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)");
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2");
+ ut_assert(filt2 >= 0);
+ filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c");
+ ut_assert(filt3 >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file2");
+ check_log_entries_extra();
+
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ ut_assertok(log_remove_filter("console", filt3));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
+
+int do_log_test_helpers(struct unit_test_state *uts)
+{
+ int i;
+
+ ut_assertok(console_record_reset_enable());
+ log_err("level %d\n", LOGL_EMERG);
+ log_err("level %d\n", LOGL_ALERT);
+ log_err("level %d\n", LOGL_CRIT);
+ log_err("level %d\n", LOGL_ERR);
+ log_warning("level %d\n", LOGL_WARNING);
+ log_notice("level %d\n", LOGL_NOTICE);
+ log_info("level %d\n", LOGL_INFO);
+ log_debug("level %d\n", LOGL_DEBUG);
+ log_content("level %d\n", LOGL_DEBUG_CONTENT);
+ log_io("level %d\n", LOGL_DEBUG_IO);
+
+ for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
+ ut_assert_nextline("%s() level %d", __func__, i);
+ ut_assert_console_end();
+ return 0;
+}
+
+int log_test_helpers(struct unit_test_state *uts)
+{
+ int ret;
+
+ gd->log_fmt = LOGF_TEST;
+ ret = do_log_test_helpers(uts);
+ gd->log_fmt = log_get_default_format();
+ return ret;
+}
+LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
+
+int do_log_test_disable(struct unit_test_state *uts)
+{
+ ut_assertok(console_record_reset_enable());
+ log_err("default\n");
+ ut_assert_nextline("%s() default", __func__);
+
+ ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
+ log_err("disabled\n");
+
+ ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
+ log_err("enabled\n");
+ ut_assert_nextline("%s() enabled", __func__);
+ ut_assert_console_end();
return 0;
}
-int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+int log_test_disable(struct unit_test_state *uts)
{
- int testnum = 0;
int ret;
- if (argc > 1)
- testnum = simple_strtoul(argv[1], NULL, 10);
+ gd->log_fmt = LOGF_TEST;
+ ret = do_log_test_disable(uts);
+ gd->log_fmt = log_get_default_format();
+ return ret;
+}
+LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on category */
+int log_test_cat_deny(struct unit_test_state *uts)
+{
+ int filt1, filt2;
+ enum log_category_t cat_list[] = {
+ log_uc_cat(UCLASS_SPI), LOGC_END
+ };
+
+ filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
+ LOGFF_DENY);
+ ut_assert(filt2 >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run_cat(UCLASS_SPI);
+ check_log_entries_none();
+
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on file */
+int log_test_file_deny(struct unit_test_state *uts)
+{
+ int filt1, filt2;
+
+ filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
+ LOGFF_DENY);
+ ut_assert(filt2 >= 0);
- ret = log_test(testnum);
- if (ret)
- printf("Test failure (err=%d)\n", ret);
+ ut_assertok(console_record_reset_enable());
+ log_run_file("file");
+ check_log_entries_none();
- return ret ? CMD_RET_FAILURE : 0;
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on level */
+int log_test_level_deny(struct unit_test_state *uts)
+{
+ int filt1, filt2;
+
+ filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+ LOGFF_DENY);
+ ut_assert(filt2 >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run();
+ check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
+ LOGL_WARNING + 1, _LOG_MAX_LEVEL);
+
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ return 0;
+}
+LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check matching based on minimum level */
+int log_test_min(struct unit_test_state *uts)
+{
+ int filt1, filt2;
+
+ filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+ LOGFF_LEVEL_MIN);
+ ut_assert(filt1 >= 0);
+ filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
+ LOGFF_DENY | LOGFF_LEVEL_MIN);
+ ut_assert(filt2 >= 0);
+
+ ut_assertok(console_record_reset_enable());
+ log_run();
+ check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
+ LOGL_WARNING, LOGL_INFO - 1);
+
+ ut_assertok(log_remove_filter("console", filt1));
+ ut_assertok(log_remove_filter("console", filt2));
+ return 0;
}
+LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);
diff --git a/test/log/syslog_test.h b/test/log/syslog_test.h
index 1310257bfe..bfaa6daef8 100644
--- a/test/log/syslog_test.h
+++ b/test/log/syslog_test.h
@@ -8,8 +8,6 @@
#ifndef __SYSLOG_TEST_H
#define __SYSLOG_TEST_H
-#define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
-
/**
* struct sb_log_env - private data for sandbox ethernet driver
*
diff --git a/test/py/conftest.py b/test/py/conftest.py
index 30920474b3..dc92c0be32 100644
--- a/test/py/conftest.py
+++ b/test/py/conftest.py
@@ -227,7 +227,7 @@ def pytest_configure(config):
console = u_boot_console_exec_attach.ConsoleExecAttach(log, ubconfig)
re_ut_test_list = re.compile(r'_u_boot_list_2_(.*)_test_2_\1_test_(.*)\s*$')
-def generate_ut_subtest(metafunc, fixture_name):
+def generate_ut_subtest(metafunc, fixture_name, sym_path):
"""Provide parametrization for a ut_subtest fixture.
Determines the set of unit tests built into a U-Boot binary by parsing the
@@ -237,12 +237,13 @@ def generate_ut_subtest(metafunc, fixture_name):
Args:
metafunc: The pytest test function.
fixture_name: The fixture name to test.
+ sym_path: Relative path to the symbol file with preceding '/'
+ (e.g. '/u-boot.sym')
Returns:
Nothing.
"""
-
- fn = console.config.build_dir + '/u-boot.sym'
+ fn = console.config.build_dir + sym_path
try:
with open(fn, 'rt') as f:
lines = f.readlines()
@@ -317,10 +318,12 @@ def pytest_generate_tests(metafunc):
Returns:
Nothing.
"""
-
for fn in metafunc.fixturenames:
if fn == 'ut_subtest':
- generate_ut_subtest(metafunc, fn)
+ generate_ut_subtest(metafunc, fn, '/u-boot.sym')
+ continue
+ if fn == 'ut_spl_subtest':
+ generate_ut_subtest(metafunc, fn, '/spl/u-boot-spl.sym')
continue
generate_config(metafunc, fn)
diff --git a/test/py/tests/test_log.py b/test/py/tests/test_log.py
index 275f9382d2..387b392ce9 100644
--- a/test/py/tests/test_log.py
+++ b/test/py/tests/test_log.py
@@ -10,110 +10,6 @@ and checks that the output is correct.
import pytest
-LOGL_FIRST, LOGL_WARNING, LOGL_INFO = (0, 4, 6)
-
-@pytest.mark.buildconfigspec('cmd_log')
-def test_log(u_boot_console):
- """Test that U-Boot logging works correctly."""
- def check_log_entries(lines, mask, max_level=LOGL_INFO):
- """Check that the expected log records appear in the output
-
- Args:
- lines: iterator containing lines to check
- mask: bit mask to select which lines to check for:
- bit 0: standard log line
- bit 1: _log line
- max_level: maximum log level to expect in the output
- """
- for i in range(max_level):
- if mask & 1:
- assert 'log_run() log %d' % i == next(lines)
- if mask & 3:
- assert 'func() _log %d' % i == next(lines)
-
- def run_test(testnum):
- """Run a particular test number (the 'log test' command)
-
- Args:
- testnum: Test number to run
- Returns:
- iterator containing the lines output from the command
- """
- output = u_boot_console.run_command('log format fm')
- assert output == ''
- with cons.log.section('basic'):
- output = u_boot_console.run_command('log test %d' % testnum)
- split = output.replace('\r', '').splitlines()
- lines = iter(split)
- assert 'test %d' % testnum == next(lines)
- return lines
-
- def test0():
- lines = run_test(0)
- check_log_entries(lines, 3)
-
- def test1():
- lines = run_test(1)
- check_log_entries(lines, 3)
-
- def test2():
- lines = run_test(2)
-
- def test3():
- lines = run_test(3)
- check_log_entries(lines, 2)
-
- def test4():
- lines = run_test(4)
- assert next(lines, None) == None
-
- def test5():
- lines = run_test(5)
- check_log_entries(lines, 2)
-
- def test6():
- lines = run_test(6)
- check_log_entries(lines, 3)
-
- def test7():
- lines = run_test(7)
- check_log_entries(lines, 3, LOGL_WARNING)
-
- def test8():
- lines = run_test(8)
- check_log_entries(lines, 3)
-
- def test9():
- lines = run_test(9)
- check_log_entries(lines, 3)
-
- def test10():
- lines = run_test(10)
- for i in range(7):
- assert 'log_test() level %d' % i == next(lines)
-
- def test11():
- """Test use of log_device_set_enable()"""
- lines = run_test(11)
- assert 'log_test() default'
- # disabled should not be displayed
- assert 'log_test() enabled'
-
- # TODO(sjg@chromium.org): Consider structuring this as separate tests
- cons = u_boot_console
- test0()
- test1()
- test2()
- test3()
- test4()
- test5()
- test6()
- test7()
- test8()
- test9()
- test10()
- test11()
-
@pytest.mark.buildconfigspec('cmd_log')
def test_log_format(u_boot_console):
"""Test the 'log format' and 'log rec' commands"""
diff --git a/test/py/tests/test_ofplatdata.py b/test/py/tests/test_ofplatdata.py
index 263334b074..78837a3c00 100644
--- a/test/py/tests/test_ofplatdata.py
+++ b/test/py/tests/test_ofplatdata.py
@@ -4,53 +4,6 @@
import pytest
import u_boot_utils as util
-OF_PLATDATA_OUTPUT = '''
-of-platdata probe:
-bool 1
-byte 05
-bytearray 06 00 00
-int 1
-intarray 2 3 4 0
-longbytearray 09 0a 0b 0c 0d 0e 0f 10 11
-string message
-stringarray "multi-word" "message" ""
-of-platdata probe:
-bool 0
-byte 08
-bytearray 01 23 34
-int 3
-intarray 5 0 0 0
-longbytearray 09 00 00 00 00 00 00 00 00
-string message2
-stringarray "another" "multi-word" "message"
-of-platdata probe:
-bool 0
-byte 00
-bytearray 00 00 00
-int 0
-intarray 0 0 0 0
-longbytearray 00 00 00 00 00 00 00 00 00
-string <NULL>
-stringarray "one" "" ""
-of-platdata probe:
-bool 0
-byte 00
-bytearray 00 00 00
-int 0
-intarray 0 0 0 0
-longbytearray 00 00 00 00 00 00 00 00 00
-string <NULL>
-stringarray "spl" "" ""
-'''
-
-@pytest.mark.buildconfigspec('spl_of_platdata')
-def test_ofplatdata(u_boot_console):
- """Test that of-platdata can be generated and used in sandbox"""
- cons = u_boot_console
- cons.restart_uboot_with_flags(['--show_of_platdata'])
- output = cons.get_spawn_output().replace('\r', '')
- assert OF_PLATDATA_OUTPUT in output
-
@pytest.mark.buildconfigspec('spl_of_platdata')
def test_spl_devicetree(u_boot_console):
"""Test content of spl device-tree"""
diff --git a/test/py/tests/test_spl.py b/test/py/tests/test_spl.py
new file mode 100644
index 0000000000..bd273dad89
--- /dev/null
+++ b/test/py/tests/test_spl.py
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2020 Google LLC
+# Written by Simon Glass <sjg@chromium.org>
+
+import os.path
+import pytest
+
+def test_spl(u_boot_console, ut_spl_subtest):
+ """Execute a "ut" subtest.
+
+ The subtests are collected in function generate_ut_subtest() from linker
+ generated lists by applying a regular expression to the lines of file
+ spl/u-boot-spl.sym. The list entries are created using the C macro
+ UNIT_TEST().
+
+ Strict naming conventions have to be followed to match the regular
+ expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
+ test suite foo that can be executed via command 'ut foo bar' and is
+ implemented in C function foo_test_bar().
+
+ Args:
+ u_boot_console (ConsoleBase): U-Boot console
+ ut_subtest (str): SPL test to be executed (e.g. 'dm platdata_phandle')
+ """
+ try:
+ cons = u_boot_console
+ cons.restart_uboot_with_flags(['-u', '-k', ut_spl_subtest.split()[1]])
+ output = cons.get_spawn_output().replace('\r', '')
+ assert 'Failures: 0' in output
+ finally:
+ # Restart afterward in case a non-SPL test is run next. This should not
+ # happen since SPL tests are run in their own invocation of test.py, but
+ # the cost of doing this is not too great at present.
+ u_boot_console.restart_uboot()
diff --git a/test/run b/test/run
index de87e7530b..735628e7e3 100755
--- a/test/run
+++ b/test/run
@@ -28,7 +28,7 @@ fi
# Run tests which require sandbox_spl
run_test "sandbox_spl" ./test/py/test.py --bd sandbox_spl --build \
- -k 'test_ofplatdata or test_handoff'
+ -k 'test_ofplatdata or test_handoff or test_spl'
if [ -z "$tools_only" ]; then
# Run tests for the flat-device-tree version of sandbox. This is a special