summaryrefslogtreecommitdiff
path: root/include/test
diff options
context:
space:
mode:
Diffstat (limited to 'include/test')
-rw-r--r--include/test/cedit-test.h32
-rw-r--r--include/test/cmd.h15
-rw-r--r--include/test/common.h15
-rw-r--r--include/test/env.h15
-rw-r--r--include/test/export.h16
-rw-r--r--include/test/fdt_overlay.h19
-rw-r--r--include/test/fuzz.h51
-rw-r--r--include/test/hush.h15
-rw-r--r--include/test/lib.h14
-rw-r--r--include/test/log.h20
-rw-r--r--include/test/optee.h16
-rw-r--r--include/test/spl.h159
-rw-r--r--include/test/test.h291
-rw-r--r--include/test/ut.h541
-rw-r--r--include/test/video.h45
15 files changed, 1264 insertions, 0 deletions
diff --git a/include/test/cedit-test.h b/include/test/cedit-test.h
new file mode 100644
index 00000000000..0d38a953415
--- /dev/null
+++ b/include/test/cedit-test.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Binding shared between cedit.dtsi and test/boot/expo.c
+ *
+ * Copyright 2023 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#ifndef __cedit_test_h
+#define __cedit_test_h
+
+#define ID_PROMPT 5
+#define ID_SCENE1 6
+#define ID_SCENE1_TITLE 7
+
+#define ID_CPU_SPEED 8
+#define ID_CPU_SPEED_TITLE 9
+#define ID_CPU_SPEED_1 10
+#define ID_CPU_SPEED_2 11
+#define ID_CPU_SPEED_3 12
+
+#define ID_POWER_LOSS 13
+#define ID_AC_OFF 14
+#define ID_AC_ON 15
+#define ID_AC_MEMORY 16
+
+#define ID_MACHINE_NAME 17
+#define ID_MACHINE_NAME_EDIT 18
+
+#define ID_DYNAMIC_START 19
+
+#endif
diff --git a/include/test/cmd.h b/include/test/cmd.h
new file mode 100644
index 00000000000..3d1e3e3bddb
--- /dev/null
+++ b/include/test/cmd.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2023 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#ifndef __TEST_CMD_H__
+#define __TEST_CMD_H__
+
+#include <test/test.h>
+
+/* Declare a new command test */
+#define CMD_TEST(_name, _flags) UNIT_TEST(_name, _flags, cmd)
+
+#endif /* __TEST_CMD_H__ */
diff --git a/include/test/common.h b/include/test/common.h
new file mode 100644
index 00000000000..d5a65d5b50b
--- /dev/null
+++ b/include/test/common.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ * Copyright (c) 2021 Steffen Jaeckel <jaeckel-floss@eyet-services.de>
+ */
+
+#ifndef __TEST_COMMON_H__
+#define __TEST_COMMON_H__
+
+#include <test/test.h>
+
+/* Declare a new common function test */
+#define COMMON_TEST(_name, _flags) UNIT_TEST(_name, _flags, common)
+
+#endif /* __TEST_COMMON_H__ */
diff --git a/include/test/env.h b/include/test/env.h
new file mode 100644
index 00000000000..6a63cc972e9
--- /dev/null
+++ b/include/test/env.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * (C) Copyright 2015
+ * Joe Hershberger, National Instruments, joe.hershberger@ni.com
+ */
+
+#ifndef __TEST_ENV_H__
+#define __TEST_ENV_H__
+
+#include <test/test.h>
+
+/* Declare a new environment test */
+#define ENV_TEST(_name, _flags) UNIT_TEST(_name, _flags, env)
+
+#endif /* __TEST_ENV_H__ */
diff --git a/include/test/export.h b/include/test/export.h
new file mode 100644
index 00000000000..afc755a8ffd
--- /dev/null
+++ b/include/test/export.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
+ */
+
+#ifndef TEST_EXPORT_H
+#define TEST_EXPORT_H
+
+/* Declare something static, unless we are doing unit tests */
+#ifdef CONFIG_UNIT_TEST
+#define TEST_STATIC
+#else
+#define TEST_STATIC static
+#endif
+
+#endif /* TEST_EXPORT_H */
diff --git a/include/test/fdt_overlay.h b/include/test/fdt_overlay.h
new file mode 100644
index 00000000000..251ad0ec97a
--- /dev/null
+++ b/include/test/fdt_overlay.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2016 NextThing Co
+ * Copyright (c) 2016 Free Electrons
+ */
+
+#ifndef __TEST_OVERLAY_H__
+#define __TEST_OVERLAY_H__
+
+#include <test/test.h>
+
+/* Declare a new FDT-overlay test */
+#define FDT_OVERLAY_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_overlay)
+
+/* Declare init for FDT-overlay test */
+#define FDT_OVERLAY_TEST_INIT(_name, _flags) \
+ UNIT_TEST_INIT(_name, _flags, fdt_overlay)
+
+#endif /* __TEST_OVERLAY_H__ */
diff --git a/include/test/fuzz.h b/include/test/fuzz.h
new file mode 100644
index 00000000000..d4c57540eb3
--- /dev/null
+++ b/include/test/fuzz.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022 Google, Inc.
+ * Written by Andrew Scull <ascull@google.com>
+ */
+
+#ifndef __TEST_FUZZ_H
+#define __TEST_FUZZ_H
+
+#include <linker_lists.h>
+#include <linux/types.h>
+
+/**
+ * struct fuzz_test - Information about a fuzz test
+ *
+ * @name: Name of fuzz test
+ * @func: Function to call to perform fuzz test on an input
+ * @flags: Flags indicate pre-conditions for fuzz test
+ */
+struct fuzz_test {
+ const char *name;
+ int (*func)(const uint8_t * data, size_t size);
+ int flags;
+};
+
+/**
+ * FUZZ_TEST() - register a fuzz test
+ *
+ * The fuzz test function must return 0 as other values are reserved for future
+ * use.
+ *
+ * @_name: the name of the fuzz test function
+ * @_flags: an integer field that can be evaluated by the fuzzer
+ * implementation
+ */
+#define FUZZ_TEST(_name, _flags) \
+ ll_entry_declare(struct fuzz_test, _name, fuzz_tests) = { \
+ .name = #_name, \
+ .func = _name, \
+ .flags = _flags, \
+ }
+
+/** Get the start of the list of fuzz tests */
+#define FUZZ_TEST_START() \
+ ll_entry_start(struct fuzz_test, fuzz_tests)
+
+/** Get the number of elements in the list of fuzz tests */
+#define FUZZ_TEST_COUNT() \
+ ll_entry_count(struct fuzz_test, fuzz_tests)
+
+#endif /* __TEST_FUZZ_H */
diff --git a/include/test/hush.h b/include/test/hush.h
new file mode 100644
index 00000000000..e57bf13ea61
--- /dev/null
+++ b/include/test/hush.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * (C) Copyright 2021
+ * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
+ */
+
+#ifndef __TEST_HUSH_H__
+#define __TEST_HUSH_H__
+
+#include <test/test.h>
+
+/* Declare a new environment test */
+#define HUSH_TEST(_name, _flags) UNIT_TEST(_name, _flags, hush)
+
+#endif /* __TEST_HUSH_H__ */
diff --git a/include/test/lib.h b/include/test/lib.h
new file mode 100644
index 00000000000..b19eb863a33
--- /dev/null
+++ b/include/test/lib.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ */
+
+#ifndef __TEST_LIB_H__
+#define __TEST_LIB_H__
+
+#include <test/test.h>
+
+/* Declare a new library function test */
+#define LIB_TEST(_name, _flags) UNIT_TEST(_name, _flags, lib)
+
+#endif /* __TEST_LIB_H__ */
diff --git a/include/test/log.h b/include/test/log.h
new file mode 100644
index 00000000000..0921c0c1cbc
--- /dev/null
+++ b/include/test/log.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
+ *
+ * Tests for logging functions
+ */
+
+#ifndef __TEST_LOG_H__
+#define __TEST_LOG_H__
+
+#include <test/test.h>
+
+#define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
+
+/* Declare a new logging test */
+#define LOG_TEST(_name) UNIT_TEST(_name, UTF_CONSOLE, log)
+#define LOG_TEST_FLAGS(_name, _flags) \
+ UNIT_TEST(_name, _flags | UTF_CONSOLE, log)
+
+#endif /* __TEST_LOG_H__ */
diff --git a/include/test/optee.h b/include/test/optee.h
new file mode 100644
index 00000000000..0a548a59e83
--- /dev/null
+++ b/include/test/optee.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+ */
+
+#ifndef __TEST_OPTEE_H__
+#define __TEST_OPTEE_H__
+
+#include <test/test.h>
+
+/* Declare a new optee test */
+#define OPTEE_TEST(_name, _flags) UNIT_TEST(_name, _flags, optee)
+#define OPTEE_TEST_INIT(_name, _flags) UNIT_TEST_INIT(_name, _flags, optee)
+#define OPTEE_TEST_UNINIT(_name, _flags) UNIT_TEST_UNINIT(_name, _flags, optee)
+
+#endif /* __TEST_OPTEE_H__ */
diff --git a/include/test/spl.h b/include/test/spl.h
new file mode 100644
index 00000000000..5fd28d92706
--- /dev/null
+++ b/include/test/spl.h
@@ -0,0 +1,159 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
+ */
+
+#ifndef TEST_SPL_H
+#define TEST_SPL_H
+
+struct unit_test_state;
+struct spl_image_info;
+
+/* Declare a new SPL test */
+#define SPL_TEST(_name, _flags) UNIT_TEST(_name, _flags, spl_test)
+
+/**
+ * generate_data() - Generate some test payload data
+ * @data: The location to fill
+ * @size: The size of @data
+ * @test_name: The seed for the data
+ *
+ * Fill @data with data. The upper nibbles will be an incrementing counter
+ * (0x00, 0x10, 0x20...) to make the data identifiable in a hex dump. The lower
+ * nibbles are random bits seeded with @test_name.
+ */
+void generate_data(char *data, size_t size, const char *test_name);
+
+/**
+ * enum spl_test_image - Image types for testing
+ * @LEGACY: "Legacy" uImages
+ * @LEGACY_LZMA: "Legacy" uImages, LZMA compressed
+ * @IMX8: i.MX8 Container images
+ * @FIT_INTERNAL: FITs with internal data
+ * @FIT_EXTERNAL: FITs with external data
+ */
+enum spl_test_image {
+ LEGACY,
+ LEGACY_LZMA,
+ IMX8,
+ FIT_INTERNAL,
+ FIT_EXTERNAL,
+};
+
+/**
+ * create_image() - Create an image for testing
+ * @dst: The location to create the image at
+ * @type: The type of image to create
+ * @info: Image parameters
+ * @data_offset: Offset of payload data within the image
+ *
+ * Create a new image at @dst. @dst must be initialized to all zeros. @info
+ * should already have name and size filled in. All other parameters will be
+ * filled in by this function. @info can later be passed to check_image_info().
+ *
+ * If @dst is %NULL, then no data is written. Otherwise, @dst must be
+ * initialized to zeros, except payload data which must already be present at
+ * @data_offset. @data_offset may be %NULL if unnecessary.
+ *
+ * Typically, this function will be called as follows:
+ *
+ * size = create_image(NULL, type, &info, &off);
+ * img = calloc(size, 1);
+ * generate_data(img + off, ...);
+ * create_image(img, type, &info, NULL);
+ *
+ * Return: The size of the image, or 0 on error
+ */
+size_t create_image(void *dst, enum spl_test_image type,
+ struct spl_image_info *info, size_t *data_offset);
+
+/**
+ * check_image_info() - Check image info after loading
+ * @uts: Current unit test state
+ * @info1: The base, known good info
+ * @info2: The info to check
+ *
+ * Check @info2 against @info1. This function is typically called after calling
+ * a function to load/parse an image. Image data is not checked.
+ *
+ * Return: 0 on success, or 1 on failure
+ */
+int check_image_info(struct unit_test_state *uts, struct spl_image_info *info1,
+ struct spl_image_info *info2);
+
+/* Some compressed data and it size */
+extern const char lzma_compressed[];
+extern const size_t lzma_compressed_size;
+
+/**
+ * typedef write_image_t - Callback for writing an image
+ * @uts: Current unit test state
+ * @img: Image to write
+ * @size: Size of @img
+ *
+ * Write @img to a location which will be read by a &struct spl_image_loader.
+ *
+ * Return: 0 on success or 1 on failure
+ */
+typedef int write_image_t(struct unit_test_state *its, void *img, size_t size);
+
+/**
+ * do_spl_test_load() - Test loading with an SPL image loader
+ * @uts: Current unit test state
+ * @test_name: Name of the current test
+ * @type: Type of image to try loading
+ * @loader: The loader to test
+ * @write_image: Callback to write the image to the backing storage
+ *
+ * Test @loader, performing the common tasks of setting up the image and
+ * checking it was loaded correctly. The caller must supply a @write_image
+ * callback to write the image to a location which will be read by @loader.
+ *
+ * Return: 0 on success or 1 on failure
+ */
+int do_spl_test_load(struct unit_test_state *uts, const char *test_name,
+ enum spl_test_image type, struct spl_image_loader *loader,
+ write_image_t write_image);
+
+/**
+ * image_supported() - Determine whether an image type is supported
+ * @type: The image type to check
+ *
+ * Return: %true if supported and %false otherwise
+ */
+static inline bool image_supported(enum spl_test_image type)
+{
+ switch (type) {
+ case LEGACY_LZMA:
+ if (!IS_ENABLED(CONFIG_SPL_LZMA))
+ return false;
+ case LEGACY:
+ return IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT);
+ case IMX8:
+ return IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER);
+ case FIT_INTERNAL:
+ case FIT_EXTERNAL:
+ return IS_ENABLED(CONFIG_SPL_LOAD_FIT) ||
+ IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL);
+ }
+
+ return false;
+}
+
+/* Declare an image test (skipped if the image type is unsupported) */
+#define SPL_IMG_TEST(func, type, flags) \
+static int func##_##type(struct unit_test_state *uts) \
+{ \
+ if (!image_supported(type)) \
+ return -EAGAIN; \
+ return func(uts, __func__, type); \
+} \
+SPL_TEST(func##_##type, flags)
+
+/* More than a couple blocks, and will not be aligned to anything */
+#define SPL_TEST_DATA_SIZE 4099
+
+/* Flags necessary for accessing DM devices */
+#define DM_FLAGS (UTF_DM | UTF_SCAN_FDT)
+
+#endif /* TEST_SPL_H */
diff --git a/include/test/test.h b/include/test/test.h
new file mode 100644
index 00000000000..0f2b68a5dee
--- /dev/null
+++ b/include/test/test.h
@@ -0,0 +1,291 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2013 Google, Inc.
+ */
+
+#ifndef __TEST_TEST_H
+#define __TEST_TEST_H
+
+#include <malloc.h>
+#include <linux/bitops.h>
+
+/**
+ * struct ut_stats - Statistics about tests run
+ *
+ * @fail_count: Number of tests that failed
+ * @skip_count: Number of tests that were skipped
+ * @test_count: Number of tests run. If a test is run muiltiple times, only one
+ * is counted
+ * @start: Timer value when test started
+ * @duration_ms: Suite duration in milliseconds
+ */
+struct ut_stats {
+ int fail_count;
+ int skip_count;
+ int test_count;
+ ulong start;
+ ulong duration_ms;
+};
+
+/*
+ * struct unit_test_state - Entire state of test system
+ *
+ * @cur: Statistics for the current run
+ * @total: Statistics for all test runs
+ * @run_count: Number of times ut_run_list() has been called
+ * @worst: Sute which had the first per-text run time
+ * @worst_ms: Time taken by that test
+ * @start: Store the starting mallinfo when doing leak test
+ * @of_live: true to use livetree if available, false to use flattree
+ * @of_root: Record of the livetree root node (used for setting up tests)
+ * @root: Root device
+ * @testdev: Test device
+ * @force_fail_alloc: Force all memory allocs to fail
+ * @skip_post_probe: Skip uclass post-probe processing
+ * @fdt_chksum: crc8 of the device tree contents
+ * @fdt_copy: Copy of the device tree
+ * @fdt_size: Size of the device-tree copy
+ * @other_fdt: Buffer for the other FDT (UTF_OTHER_FDT)
+ * @other_fdt_size: Size of the other FDT (UTF_OTHER_FDT)
+ * @of_other: Live tree for the other FDT
+ * @runs_per_test: Number of times to run each test (typically 1)
+ * @force_run: true to run tests marked with the UTF_MANUAL flag
+ * @old_bloblist: stores the old gd->bloblist pointer
+ * @expect_str: Temporary string used to hold expected string value
+ * @actual_str: Temporary string used to hold actual string value
+ */
+struct unit_test_state {
+ struct ut_stats cur;
+ struct ut_stats total;
+ int run_count;
+ const struct suite *worst;
+ int worst_ms;
+ struct mallinfo start;
+ struct device_node *of_root;
+ bool of_live;
+ struct udevice *root;
+ struct udevice *testdev;
+ int force_fail_alloc;
+ int skip_post_probe;
+ uint fdt_chksum;
+ void *fdt_copy;
+ uint fdt_size;
+ void *other_fdt;
+ int other_fdt_size;
+ struct device_node *of_other;
+ int runs_per_test;
+ bool force_run;
+ void *old_bloblist;
+ char expect_str[512];
+ char actual_str[512];
+};
+
+/* Test flags for each test */
+enum ut_flags {
+ UTF_SCAN_PDATA = BIT(0), /* test needs platform data */
+ UTF_PROBE_TEST = BIT(1), /* probe test uclass */
+ UTF_SCAN_FDT = BIT(2), /* scan device tree */
+ UTF_FLAT_TREE = BIT(3), /* test needs flat DT */
+ UTF_LIVE_TREE = BIT(4), /* needs live device tree */
+ UTF_CONSOLE = BIT(5), /* needs console recording */
+ /* do extra driver model init and uninit */
+ UTF_DM = BIT(6),
+ UTF_OTHER_FDT = BIT(7), /* read in other device tree */
+ /*
+ * Only run if explicitly requested with 'ut -f <suite> <test>'. The
+ * test name must end in "_norun" so that pytest detects this also,
+ * since it cannot access the flags.
+ */
+ UTF_MANUAL = BIT(8),
+ UTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */
+ UTF_SF_BOOTDEV = BIT(10), /* enable SPI flash bootdevs */
+ UFT_BLOBLIST = BIT(11), /* test changes gd->bloblist */
+ UTF_INIT = BIT(12), /* test inits a suite */
+ UTF_UNINIT = BIT(13), /* test uninits a suite */
+};
+
+/**
+ * struct unit_test - Information about a unit test
+ *
+ * @name: Name of test
+ * @func: Function to call to perform test
+ * @flags: Flags indicated pre-conditions for test
+ */
+struct unit_test {
+ const char *file;
+ const char *name;
+ int (*func)(struct unit_test_state *state);
+ int flags;
+};
+
+/**
+ * UNIT_TEST() - create linker generated list entry for unit a unit test
+ *
+ * The macro UNIT_TEST() is used to create a linker generated list entry. These
+ * list entries are enumerate tests that can be execute using the ut command.
+ * The list entries are used both by the implementation of the ut command as
+ * well as in a related Python test.
+ *
+ * For Python testing the subtests are collected in Python function
+ * generate_ut_subtest() by applying a regular expression to the lines of file
+ * u-boot.sym. The list entries have to follow strict naming conventions to be
+ * matched by the 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
+ * function foo_test_bar().
+ *
+ * @_name: concatenation of name of the test suite, "_test_", and the name
+ * of the test
+ * @_flags: an integer field that can be evaluated by the test suite
+ * implementation (see enum ut_flags)
+ * @_suite: name of the test suite concatenated with "_test"
+ */
+#define UNIT_TEST(_name, _flags, _suite) \
+ ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
+ .file = __FILE__, \
+ .name = #_name, \
+ .flags = _flags, \
+ .func = _name, \
+ }
+
+/* init function for unit-test suite (the 'A' makes it first) */
+#define UNIT_TEST_INIT(_name, _flags, _suite) \
+ ll_entry_declare(struct unit_test, A ## _name, ut_ ## _suite) = { \
+ .file = __FILE__, \
+ .name = #_name, \
+ .flags = (_flags) | UTF_INIT, \
+ .func = _name, \
+ }
+
+/* uninit function for unit-test suite (the 'aaa' makes it last) */
+#define UNIT_TEST_UNINIT(_name, _flags, _suite) \
+ ll_entry_declare(struct unit_test, zzz ## _name, ut_ ## _suite) = { \
+ .file = __FILE__, \
+ .name = #_name, \
+ .flags = (_flags) | UTF_UNINIT, \
+ .func = _name, \
+ }
+
+/* Get the start of a list of unit tests for a particular suite */
+#define UNIT_TEST_SUITE_START(_suite) \
+ ll_entry_start(struct unit_test, ut_ ## _suite)
+#define UNIT_TEST_SUITE_COUNT(_suite) \
+ ll_entry_count(struct unit_test, ut_ ## _suite)
+
+/* Use ! and ~ so that all tests will be sorted between these two values */
+#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
+#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
+#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
+
+/* Sizes for devres tests */
+enum {
+ TEST_DEVRES_SIZE = 100,
+ TEST_DEVRES_COUNT = 10,
+ TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
+
+ /* A few different sizes */
+ TEST_DEVRES_SIZE2 = 15,
+ TEST_DEVRES_SIZE3 = 37,
+};
+
+/**
+ * testbus_get_clear_removed() - Test function to obtain removed device
+ *
+ * This is used in testbus to find out which device was removed. Calling this
+ * function returns a pointer to the device and then clears it back to NULL, so
+ * that a future test can check it.
+ */
+struct udevice *testbus_get_clear_removed(void);
+
+#ifdef CONFIG_SANDBOX
+#include <asm/state.h>
+#include <asm/test.h>
+#endif
+
+static inline void arch_reset_for_test(void)
+{
+#ifdef CONFIG_SANDBOX
+ state_reset_for_test(state_get_current());
+#endif
+}
+static inline int test_load_other_fdt(struct unit_test_state *uts)
+{
+ int ret = 0;
+#ifdef CONFIG_SANDBOX
+ ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
+#endif
+ return ret;
+}
+
+/**
+ * Control skipping of time delays
+ *
+ * Some tests have unnecessay time delays (e.g. USB). Allow these to be
+ * skipped to speed up testing
+ *
+ * @param skip_delays true to skip delays from now on, false to honour delay
+ * requests
+ */
+static inline void test_set_skip_delays(bool skip_delays)
+{
+#ifdef CONFIG_SANDBOX
+ state_set_skip_delays(skip_delays);
+#endif
+}
+
+/**
+ * test_set_eth_enable() - Enable / disable Ethernet
+ *
+ * Allows control of whether Ethernet packets are actually send/received
+ *
+ * @enable: true to enable Ethernet, false to disable
+ */
+static inline void test_set_eth_enable(bool enable)
+{
+#ifdef CONFIG_SANDBOX
+ sandbox_set_eth_enable(enable);
+#endif
+}
+
+/* Allow ethernet to be disabled for testing purposes */
+static inline bool test_eth_enabled(void)
+{
+ bool enabled = true;
+
+#ifdef CONFIG_SANDBOX
+ enabled = sandbox_eth_enabled();
+#endif
+ return enabled;
+}
+
+/* Allow ethernet bootdev to be ignored for testing purposes */
+static inline bool test_eth_bootdev_enabled(void)
+{
+ bool enabled = true;
+
+#ifdef CONFIG_SANDBOX
+ enabled = sandbox_eth_enabled();
+#endif
+ return enabled;
+}
+
+/* Allow SPI flash bootdev to be ignored for testing purposes */
+static inline bool test_sf_bootdev_enabled(void)
+{
+ bool enabled = true;
+
+#ifdef CONFIG_SANDBOX
+ enabled = sandbox_sf_bootdev_enabled();
+#endif
+ return enabled;
+}
+
+static inline void test_sf_set_enable_bootdevs(bool enable)
+{
+#ifdef CONFIG_SANDBOX
+ sandbox_sf_set_enable_bootdevs(enable);
+#endif
+}
+
+#endif /* __TEST_TEST_H */
diff --git a/include/test/ut.h b/include/test/ut.h
new file mode 100644
index 00000000000..be5502e03a1
--- /dev/null
+++ b/include/test/ut.h
@@ -0,0 +1,541 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Simple unit test library
+ *
+ * Copyright (c) 2013 Google, Inc
+ */
+
+#ifndef __TEST_UT_H
+#define __TEST_UT_H
+
+#include <command.h>
+#include <hexdump.h>
+#include <linux/err.h>
+#include <test/test.h>
+
+struct unit_test_state;
+
+/**
+ * ut_fail() - Record failure of a unit test
+ *
+ * @uts: Test state
+ * @fname: Filename where the error occurred
+ * @line: Line number where the error occurred
+ * @func: Function name where the error occurred
+ * @cond: The condition that failed
+ */
+void ut_fail(struct unit_test_state *uts, const char *fname, int line,
+ const char *func, const char *cond);
+
+/**
+ * ut_failf() - Record failure of a unit test
+ *
+ * @uts: Test state
+ * @fname: Filename where the error occurred
+ * @line: Line number where the error occurred
+ * @func: Function name where the error occurred
+ * @cond: The condition that failed
+ * @fmt: printf() format string for the error, followed by args
+ */
+void ut_failf(struct unit_test_state *uts, const char *fname, int line,
+ const char *func, const char *cond, const char *fmt, ...)
+ __attribute__ ((format (__printf__, 6, 7)));
+
+/**
+ * ut_check_console_line() - Check the next console line against expectations
+ *
+ * This creates a string and then checks it against the next line of console
+ * output obtained with console_record_readline().
+ *
+ * After the function returns, uts->expect_str holds the expected string and
+ * uts->actual_str holds the actual string read from the console.
+ *
+ * @uts: Test state
+ * @fmt: printf() format string for the error, followed by args
+ * Return: 0 if OK, other value on error
+ */
+int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
+ __attribute__ ((format (__printf__, 2, 3)));
+
+/**
+ * ut_check_console_linen() - Check part of the next console line
+ *
+ * This creates a string and then checks it against the next line of console
+ * output obtained with console_record_readline(). Only the length of the
+ * string is checked
+ *
+ * After the function returns, uts->expect_str holds the expected string and
+ * uts->actual_str holds the actual string read from the console.
+ *
+ * @uts: Test state
+ * @fmt: printf() format string for the error, followed by args
+ * Return: 0 if OK, other value on error
+ */
+int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
+ __attribute__ ((format (__printf__, 2, 3)));
+
+/**
+ * ut_check_skipline() - Check that the next console line exists and skip it
+ *
+ * @uts: Test state
+ * Return: 0 if OK, other value on error
+ */
+int ut_check_skipline(struct unit_test_state *uts);
+
+/**
+ * ut_check_skip_to_line() - skip output until a line is found
+ *
+ * This creates a string and then checks it against the following lines of
+ * console output obtained with console_record_readline() until it is found.
+ *
+ * After the function returns, uts->expect_str holds the expected string and
+ * uts->actual_str holds the actual string read from the console.
+ *
+ * @uts: Test state
+ * @fmt: printf() format string to look for, followed by args
+ * Return: 0 if OK, -ENOENT if not found, other value on error
+ */
+int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...);
+
+/**
+ * ut_check_skip_to_linen() - skip output until a partial line is found
+ *
+ * This creates a string and then checks it against the following lines of
+ * console output obtained with console_record_readline() until it is found.
+ * Only the characters up to the length of the string are checked, so the line
+ * may extend further
+ *
+ * After the function returns, uts->expect_str holds the expected string and
+ * uts->actual_str holds the actual string read from the console.
+ *
+ * @uts: Test state
+ * @fmt: printf() format string to look for, followed by args
+ * Return: 0 if OK, -ENOENT if not found, other value on error
+ */
+int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...);
+
+/**
+ * ut_check_console_end() - Check there is no more console output
+ *
+ * After the function returns, uts->actual_str holds the actual string read
+ * from the console
+ *
+ * @uts: Test state
+ * Return: 0 if OK (console has no output), other value on error
+ */
+int ut_check_console_end(struct unit_test_state *uts);
+
+/**
+ * ut_check_console_dump() - Check that next lines have a print_buffer() dump
+ *
+ * This only supports a byte dump.
+ *
+ * @total_bytes: Size of the expected dump in bytes`
+ * Return: 0 if OK (looks like a dump and the length matches), other value on
+ * error
+ */
+int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
+
+/* Report a failure, with printf() string */
+#define ut_reportf(fmt, args...) \
+ ut_failf(uts, __FILE__, __LINE__, __func__, "report", \
+ fmt, ##args)
+
+/* Assert that a condition is non-zero */
+#define ut_assert(cond) ({ \
+ int __ret = 0; \
+ \
+ if (!(cond)) { \
+ ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a condition is non-zero, with printf() string */
+#define ut_assertf(cond, fmt, args...) ({ \
+ int __ret = 0; \
+ \
+ if (!(cond)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
+ fmt, ##args); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two int expressions are equal */
+#define ut_asserteq(expr1, expr2) ({ \
+ unsigned int _val1 = (expr1), _val2 = (expr2); \
+ int __ret = 0; \
+ \
+ if (_val1 != _val2) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " == " #expr2, \
+ "Expected %#x (%d), got %#x (%d)", \
+ _val1, _val1, _val2, _val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two 64 int expressions are equal */
+#define ut_asserteq_64(expr1, expr2) ({ \
+ u64 _val1 = (expr1), _val2 = (expr2); \
+ int __ret = 0; \
+ \
+ if (_val1 != _val2) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " == " #expr2, \
+ "Expected %#llx (%lld), got %#llx (%lld)", \
+ (unsigned long long)_val1, \
+ (unsigned long long)_val1, \
+ (unsigned long long)_val2, \
+ (unsigned long long)_val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two string expressions are equal */
+#define ut_asserteq_str(expr1, expr2) ({ \
+ const char *_val1 = (expr1), *_val2 = (expr2); \
+ int __ret = 0; \
+ \
+ if (strcmp(_val1, _val2)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " = " #expr2, \
+ "Expected \"%s\", got \"%s\"", _val1, _val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/*
+ * Assert that two string expressions are equal, up to length of the
+ * first
+ */
+#define ut_asserteq_strn(expr1, expr2) ({ \
+ const char *_val1 = (expr1), *_val2 = (expr2); \
+ int _len = strlen(_val1); \
+ int __ret = 0; \
+ \
+ if (memcmp(_val1, _val2, _len)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " = " #expr2, \
+ "Expected \"%.*s\", got \"%.*s\"", \
+ _len, _val1, _len, _val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two memory areas are equal */
+#define ut_asserteq_mem(expr1, expr2, len) ({ \
+ const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
+ const uint __len = len; \
+ int __ret = 0; \
+ \
+ if (memcmp(_val1, _val2, __len)) { \
+ char __buf1[64 + 1] = "\0"; \
+ char __buf2[64 + 1] = "\0"; \
+ bin2hex(__buf1, _val1, min(__len, (uint)32)); \
+ bin2hex(__buf2, _val2, min(__len, (uint)32)); \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " = " #expr2, \
+ "Expected \"%s\", got \"%s\"", \
+ __buf1, __buf2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two pointers are equal */
+#define ut_asserteq_ptr(expr1, expr2) ({ \
+ const void *_val1 = (expr1), *_val2 = (expr2); \
+ int __ret = 0; \
+ \
+ if (_val1 != _val2) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " = " #expr2, \
+ "Expected %p, got %p", _val1, _val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that two addresses (converted from pointers) are equal */
+#define ut_asserteq_addr(expr1, expr2) ({ \
+ ulong _val1 = map_to_sysmem(expr1); \
+ ulong _val2 = map_to_sysmem(expr2); \
+ int __ret = 0; \
+ \
+ if (_val1 != _val2) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr1 " = " #expr2, \
+ "Expected %lx, got %lx", _val1, _val2); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a pointer is NULL */
+#define ut_assertnull(expr) ({ \
+ const void *_val = (expr); \
+ int __ret = 0; \
+ \
+ if (_val) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr " != NULL", \
+ "Expected NULL, got %p", _val); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a pointer is not NULL */
+#define ut_assertnonnull(expr) ({ \
+ const void *_val = (expr); \
+ int __ret = 0; \
+ \
+ if (!_val) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr " = NULL", \
+ "Expected non-null, got NULL"); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a pointer is not an error pointer */
+#define ut_assertok_ptr(expr) ({ \
+ const void *_val = (expr); \
+ int __ret = 0; \
+ \
+ if (IS_ERR(_val)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ #expr " = NULL", \
+ "Expected pointer, got error %ld", \
+ PTR_ERR(_val)); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that an operation succeeds (returns 0) */
+#define ut_assertok(cond) ut_asserteq(0, cond)
+
+/* Assert that the next console output line matches */
+#define ut_assert_nextline(fmt, args...) ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_console_line(uts, fmt, ##args)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "\nExpected '%s',\n got '%s'", \
+ uts->expect_str, uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that the next console output line matches up to the length */
+#define ut_assert_nextlinen(fmt, args...) ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_console_linen(uts, fmt, ##args)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "\nExpected '%s',\n got '%s'", \
+ uts->expect_str, uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that there is a 'next' console output line, and skip it */
+#define ut_assert_skipline() ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_skipline(uts)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "\nExpected a line, got end"); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a following console output line matches */
+#define ut_assert_skip_to_line(fmt, args...) ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_skip_to_line(uts, fmt, ##args)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "\nExpected '%s',\n got to '%s'", \
+ uts->expect_str, uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that a following console output line matches */
+#define ut_assert_skip_to_linen(fmt, args...) ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_skip_to_linen(uts, fmt, ##args)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "\nExpected '%s',\n got to '%s'", \
+ uts->expect_str, uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that there is no more console output */
+#define ut_assert_console_end() ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_console_end(uts)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", "Expected no more output, got '%s'",\
+ uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that the next lines are print_buffer() dump at an address */
+#define ut_assert_nextlines_are_dump(total_bytes) ({ \
+ int __ret = 0; \
+ \
+ if (ut_check_console_dump(uts, total_bytes)) { \
+ ut_failf(uts, __FILE__, __LINE__, __func__, \
+ "console", \
+ "Expected dump of length %x bytes, got '%s'", \
+ total_bytes, uts->actual_str); \
+ return CMD_RET_FAILURE; \
+ } \
+ __ret; \
+})
+
+/* Assert that the next console output line is empty */
+#define ut_assert_nextline_empty() \
+ ut_assert_nextline("%s", "")
+
+/**
+ * ut_check_free() - Return the number of bytes free in the malloc() pool
+ *
+ * Return: bytes free
+ */
+ulong ut_check_free(void);
+
+/**
+ * ut_check_delta() - Return the number of bytes allocated/freed
+ *
+ * @last: Last value from ut_check_free
+ * Return: free memory delta from @last; positive means more memory has been
+ * allocated, negative means less has been allocated (i.e. some is freed)
+ */
+long ut_check_delta(ulong last);
+
+/**
+ * ut_silence_console() - Silence the console if requested by the user
+ *
+ * This stops test output from appear on the console. It is the default on
+ * sandbox, unless the -v flag is given. For other boards, this does nothing.
+ *
+ * @uts: Test state (in case in future we want to keep state here)
+ */
+void ut_silence_console(struct unit_test_state *uts);
+
+/**
+ * ut_unsilence_console() - Unsilence the console after a test
+ *
+ * This restarts console output again and turns off console recording. This
+ * happens on all boards, including sandbox.
+ */
+void ut_unsilence_console(struct unit_test_state *uts);
+
+/**
+ * ut_set_skip_delays() - Sets whether delays should be skipped
+ *
+ * Normally functions like mdelay() cause U-Boot to wait for a while. This
+ * allows all such delays to be skipped on sandbox, to speed up tests
+ *
+ * @uts: Test state (in case in future we want to keep state here)
+ * @skip_delays: true to skip delays, false to process them normally
+ */
+void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
+
+/**
+ * ut_state_get() - Get the active test state
+ *
+ * Return: the currently active test state, or NULL if none
+ */
+struct unit_test_state *ut_get_state(void);
+
+/**
+ * ut_set_state() - Set the active test state
+ *
+ * @uts: Test state to use as currently active test state, or NULL if none
+ */
+void ut_set_state(struct unit_test_state *uts);
+
+/**
+ * ut_init_state() - Set up a new test state
+ *
+ * This must be called before using the test state with ut_run_tests()
+ *
+ * @uts: Test state to init
+ */
+void ut_init_state(struct unit_test_state *uts);
+
+/**
+ * ut_uninit_state() - Free memory used by test state
+ *
+ * This must be called before after the test state with ut_run_tests(). To later
+ * reuse the test state to run more tests, call test_state_init() first
+ *
+ * @uts: Test state to uninit
+ */
+void ut_uninit_state(struct unit_test_state *uts);
+
+/**
+ * ut_run_tests() - Run a set of tests
+ *
+ * This runs the test, handling any preparation and clean-up needed. It prints
+ * the name of each test before running it.
+ *
+ * @uts: Unit-test state, which must be ready for use, i.e. ut_init_state()
+ * has been called. The caller is responsible for calling
+ * ut_uninit_state() after this function returns
+ * @category: Category of these tests. This is a string printed at the start to
+ * announce the the number of tests
+ * @prefix: String prefix for the tests. Any tests that have this prefix will be
+ * printed without the prefix, so that it is easier to see the unique part
+ * of the test name. If NULL, no prefix processing is done
+ * @tests: List of tests to run
+ * @count: Number of tests to run
+ * @select_name: Name of a single test to run (from the list provided). If NULL
+ * then all tests are run
+ * @runs_per_test: Number of times to run each test (typically 1)
+ * @force_run: Run tests that are marked as manual-only (UTF_MANUAL)
+ * @test_insert: String describing a test to run after n other tests run, in the
+ * format n:name where n is the number of tests to run before this one and
+ * name is the name of the test to run. This is used to find which test causes
+ * another test to fail. If the one test fails, testing stops immediately.
+ * Pass NULL to disable this
+ * Return: 0 if all tests passed, -1 if any failed
+ */
+int ut_run_list(struct unit_test_state *uts, const char *category,
+ const char *prefix, struct unit_test *tests, int count,
+ const char *select_name, int runs_per_test, bool force_run,
+ const char *test_insert);
+
+/**
+ * ut_report() - Report stats on a test run
+ *
+ * @stats: Stats to show
+ * @run_count: Number of suites that were run
+ */
+void ut_report(struct ut_stats *stats, int run_count);
+
+#endif
diff --git a/include/test/video.h b/include/test/video.h
new file mode 100644
index 00000000000..000fd708c86
--- /dev/null
+++ b/include/test/video.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2013 Google, Inc.
+ */
+
+#ifndef __TEST_VIDEO_H
+#define __TEST_VIDEO_H
+
+#include <stdbool.h>
+
+struct udevice;
+struct unit_test_state;
+
+/**
+ * video_compress_fb() - Compress the frame buffer and return its size
+ *
+ * We want to write tests which perform operations on the video console and
+ * check that the frame buffer ends up with the correct contents. But it is
+ * painful to store 'known good' images for comparison with the frame
+ * buffer. As an alternative, we can compress the frame buffer and check the
+ * size of the compressed data. This provides a pretty good level of
+ * certainty and the resulting tests need only check a single value.
+ *
+ * @uts: Test state
+ * @dev: Video device
+ * @use_copy: Use copy frame buffer if available
+ * Return: compressed size of the frame buffer, or -ve on error
+ */
+int video_compress_fb(struct unit_test_state *uts, struct udevice *dev,
+ bool use_copy);
+
+/**
+ * check_copy_frame_buffer() - Compare main frame buffer to copy
+ *
+ * If the copy frame buffer is enabled, this compares it to the main
+ * frame buffer. Normally they should have the same contents after a
+ * sync.
+ *
+ * @uts: Test state
+ * @dev: Video device
+ * Return: 0, or -ve on error
+ */
+int video_check_copy_fb(struct unit_test_state *uts, struct udevice *dev);
+
+#endif