kunit: test: adds KUnit, a basic unit testing library for Linux
Adds the core of KUnit along with a minimal set of features. In
particular, this adds:
- the base reporting infrastructure for reporting test results.
- the concept of test modules and test cases, which is how tests are
specified and organized.
- infrastructure for running test modules and cases on UML.
- expectations, which is how properties to be verified by a test are
asserted.
Change-Id: I61ea0abe30fd0f1679e5cd16d0c101b956380d52
Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
diff --git a/include/test/string-stream.h b/include/test/string-stream.h
new file mode 100644
index 0000000..8e8b531
--- /dev/null
+++ b/include/test/string-stream.h
@@ -0,0 +1,27 @@
+#ifndef _TEST_STRING_STREAM_H
+#define _TEST_STRING_STREAM_H
+
+#include <linux/types.h>
+#include <stdarg.h>
+
+struct string_stream_fragment {
+ struct list_head node;
+ char *fragment;
+};
+
+struct string_stream {
+ size_t length;
+ struct list_head fragments;
+
+ int (*add)(struct string_stream *this, const char *fmt, ...);
+ int (*vadd)(struct string_stream *this, const char *fmt, va_list args);
+ char *(*get_string)(struct string_stream *this);
+ void (*clear)(struct string_stream *this);
+ bool (*is_empty)(struct string_stream *this);
+};
+
+struct string_stream *new_string_stream(void);
+
+void destroy_string_stream(struct string_stream *stream);
+
+#endif /* _TEST_STRING_STREAM_H */
diff --git a/include/test/test-stream.h b/include/test/test-stream.h
new file mode 100644
index 0000000..540d341
--- /dev/null
+++ b/include/test/test-stream.h
@@ -0,0 +1,39 @@
+#ifndef _TEST_TEST_STREAM_H
+#define _TEST_TEST_STREAM_H
+
+#include <linux/types.h>
+#include <test/string-stream.h>
+
+struct test;
+
+/**
+ * struct test_stream - a std::stream style string builder.
+ * @set_level: sets the level that this string should be printed at.
+ * @add: adds the formatted input to the internal buffer.
+ * @commit: prints out the internal buffer to the user.
+ * @clear: clears the internal buffer.
+ *
+ * A std::stream style string builder. Allows messages to be built up and
+ * printed all at once.
+ */
+struct test_stream {
+ void (*set_level)(struct test_stream *this, const char *level);
+ void (*add)(struct test_stream *this, const char *fmt, ...);
+ void (*append)(struct test_stream *this, struct test_stream *other);
+ void (*commit)(struct test_stream *this);
+ void (*clear)(struct test_stream *this);
+ /* private: internal use only. */
+ struct test *test;
+ const char *level;
+ struct string_stream *internal_stream;
+};
+
+/**
+ * test_new_stream() - constructs a new &struct test_stream.
+ * @test: The test context object.
+ *
+ * Constructs a new test managed &struct test_stream.
+ */
+struct test_stream *test_new_stream(struct test *test);
+
+#endif /* _TEST_TEST_STREAM_H */
diff --git a/include/test/test.h b/include/test/test.h
new file mode 100644
index 0000000..a2419ec
--- /dev/null
+++ b/include/test/test.h
@@ -0,0 +1,512 @@
+#ifndef _TEST_TEST_H
+#define _TEST_TEST_H
+
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <test/test-stream.h>
+
+/**
+ * struct test_resource - represents a *test managed resource*
+ * @allocation: for the user to store arbitrary data.
+ * @free: a user supplied function to free the resource. Populated by
+ * test_alloc_resource().
+ *
+ * Represents a *test managed resource*, a resource which will automatically be
+ * cleaned up at the end of a test case.
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ * struct test_kmalloc_params {
+ * size_t size;
+ * gfp_t gfp;
+ * };
+ *
+ * static int test_kmalloc_init(struct test_resource *res, void *context)
+ * {
+ * struct test_kmalloc_params *params = context;
+ * res->allocation = kmalloc(params->size, params->gfp);
+ *
+ * if (!res->allocation)
+ * return -ENOMEM;
+ *
+ * return 0;
+ * }
+ *
+ * static void test_kmalloc_free(struct test_resource *res)
+ * {
+ * kfree(res->allocation);
+ * }
+ *
+ * void *test_kmalloc(struct test *test, size_t size, gfp_t gfp)
+ * {
+ * struct test_kmalloc_params params;
+ * struct test_resource *res;
+ *
+ * params.size = size;
+ * params.gfp = gfp;
+ *
+ * // TODO(felixguo@google.com): The & gets interpreted via
+ * // Kerneldoc but we don't want that.
+ * res = test_alloc_resource(test, test_kmalloc_init,
+ * test_kmalloc_free, & params);
+ * if (res)
+ * return res->allocation;
+ * else
+ * return NULL;
+ * }
+ */
+struct test_resource {
+ void *allocation;
+ void (*free)(struct test_resource *res);
+ /* private: internal use only. */
+ struct list_head node;
+};
+
+struct test;
+
+/**
+ * struct test_case - represents an individual test case.
+ * @run_case: the function representing the actual test case.
+ * @name: the name of the test case.
+ *
+ * A test case is a function with the signature, ``void (*)(struct test *)``
+ * that makes expectations (see EXPECT_TRUE()) about code under test. Each test
+ * case is associated with a &struct test_module and will be run after the
+ * module's init function and followed by the module's exit function.
+ *
+ * A test case should be static and should only be created with the TEST_CASE()
+ * macro; additionally, every array of test cases should be terminated with an
+ * empty test case.
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ * void add_test_basic(struct test *test)
+ * {
+ * EXPECT_EQ(test, 1, add(1, 0));
+ * EXPECT_EQ(test, 2, add(1, 1));
+ * EXPECT_EQ(test, 0, add(-1, 1));
+ * EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
+ * EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
+ * }
+ *
+ * static struct test_case example_test_cases[] = {
+ * TEST_CASE(add_test_basic),
+ * {},
+ * };
+ *
+ */
+struct test_case {
+ void (*run_case)(struct test *test);
+ const char name[256];
+ /* private: internal use only. */
+ bool success;
+};
+
+/**
+ * TEST_CASE - A helper for creating a &struct test_case
+ * @test_name: a reference to a test case function.
+ *
+ * Takes a symbol for a function representing a test case and creates a &struct
+ * test_case object from it. See the documentation for &struct test_case for an
+ * example on how to use it.
+ */
+#define TEST_CASE(test_name) { .run_case = test_name, .name = #test_name }
+
+/**
+ * struct test_module - describes a related collection of &struct test_case s.
+ * @name: the name of the test. Purely informational.
+ * @init: called before every test case.
+ * @exit: called after every test case.
+ * @test_cases: a null terminated array of test cases.
+ *
+ * A test_module is a collection of related &struct test_case s, such that
+ * @init is called before every test case and @exit is called after every test
+ * case, similar to the notion of a *test fixture* or a *test class* in other
+ * unit testing frameworks like JUnit or Googletest.
+ *
+ * Every &struct test_case must be associated with a test_module for KUnit to
+ * run it.
+ */
+struct test_module {
+ const char name[256];
+ int (*init)(struct test *test);
+ void (*exit)(struct test *test);
+ struct test_case *test_cases;
+};
+
+/**
+ * struct test - represents a running instance of a test.
+ * @priv: for user to store arbitrary data. Commonly used to pass data created
+ * in the init function (see &struct test_module).
+ *
+ * Used to store information about the current context under which the test is
+ * running. Most of this data is private and should only be accessed indirectly
+ * via public functions; the one exception is @priv which can be used by the
+ * test writer to store arbitrary data.
+ */
+struct test {
+ void *priv;
+ /* private: internal use only. */
+ struct list_head resources;
+ const char *name;
+ bool success;
+ void (*vprintk)(const struct test *test,
+ const char *level,
+ struct va_format *vaf);
+ void (*fail)(struct test *test, struct test_stream *stream);
+};
+
+int test_init_test(struct test *test, const char *name);
+
+int test_run_tests(struct test_module *module);
+
+/**
+ * module_test() - used to register a &struct test_module with KUnit.
+ * @module: a statically allocated &struct test_module.
+ *
+ * Registers @module with the test framework. See &struct test_module for more
+ * information.
+ */
+#define module_test(module) \
+ static int module_test_init##module(void) \
+ { \
+ return test_run_tests(&module); \
+ } \
+ late_initcall(module_test_init##module)
+
+/**
+ * test_alloc_resource() - Allocates a *test managed resource*.
+ * @test: The test context object.
+ * @init: a user supplied function to initialize the resource.
+ * @free: a user supplied function to free the resource.
+ * @context: for the user to pass in arbitrary data.
+ *
+ * Allocates a *test managed resource*, a resource which will automatically be
+ * cleaned up at the end of a test case. See &struct test_resource for an
+ * example.
+ */
+struct test_resource *test_alloc_resource(struct test *test,
+ int (*init)(struct test_resource *,
+ void *),
+ void (*free)(struct test_resource *),
+ void *context);
+
+void test_free_resource(struct test *test, struct test_resource *res);
+
+/**
+ * test_kmalloc() - Just like kmalloc() except the allocation is *test managed*.
+ * @test: The test context object.
+ * @size: The size in bytes of the desired memory.
+ * @gfp: flags passed to underlying kmalloc().
+ *
+ * Just like `kmalloc(...)`, except the allocation is managed by the test case
+ * and is automatically cleaned up after the test case concludes. See &struct
+ * test_resource for more information.
+ */
+void *test_kmalloc(struct test *test, size_t size, gfp_t gfp);
+
+/**
+ * test_kzalloc() - Just like test_kmalloc(), but zeroes the allocation.
+ * @test: The test context object.
+ * @size: The size in bytes of the desired memory.
+ * @gfp: flags passed to underlying kmalloc().
+ *
+ * See kzalloc() and test_kmalloc() for more information.
+ */
+static inline void *test_kzalloc(struct test *test, size_t size, gfp_t gfp)
+{
+ return test_kmalloc(test, size, gfp | __GFP_ZERO);
+}
+
+void test_cleanup(struct test *test);
+
+void test_printk(const char *level,
+ const struct test *test,
+ const char *fmt, ...);
+
+/**
+ * test_info() - Prints an INFO level message associated with the current test.
+ * @test: The test context object.
+ * @fmt: A printk() style format string.
+ *
+ * Prints an info level message associated with the test module being run. Takes
+ * a variable number of format parameters just like printk().
+ */
+#define test_info(test, fmt, ...) \
+ test_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
+
+/**
+ * test_warn() - Prints a WARN level message associated with the current test.
+ * @test: The test context object.
+ * @fmt: A printk() style format string.
+ *
+ * See test_info().
+ */
+#define test_warn(test, fmt, ...) \
+ test_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
+
+/**
+ * test_err() - Prints an ERROR level message associated with the current test.
+ * @test: The test context object.
+ * @fmt: A printk() style format string.
+ *
+ * See test_info().
+ */
+#define test_err(test, fmt, ...) \
+ test_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
+
+static inline struct test_stream *test_expect_start(struct test *test,
+ const char *file,
+ const char *line)
+{
+ struct test_stream *stream = test_new_stream(test);
+
+ stream->add(stream, "EXPECTATION FAILED at %s:%s\n\t", file, line);
+
+ return stream;
+}
+
+static inline void test_expect_end(struct test *test,
+ bool success,
+ struct test_stream *stream)
+{
+ if (!success)
+ test->fail(test, stream);
+ else
+ stream->clear(stream);
+}
+
+#define EXPECT_START(test) \
+ test_expect_start(test, __FILE__, __stringify(__LINE__))
+
+#define EXPECT_END(test, success, stream) test_expect_end(test, success, stream)
+
+#define EXPECT(test, success, message) do {\
+ struct test_stream *__stream = EXPECT_START(test); \
+ \
+ __stream->add(__stream, message); \
+ EXPECT_END(test, success, __stream); \
+} while (0)
+
+/**
+ * SUCCEED() - A no-op expectation. Only exists for code clarity.
+ * @test: The test context object.
+ *
+ * The opposite of FAIL(), it is an expectation that cannot fail. In other
+ * words, it does nothing and only exists for code clarity. See EXPECT_TRUE()
+ * for more information.
+ */
+#define SUCCEED(test) do {} while (0)
+
+/**
+ * FAIL() - Always causes a test to fail when evaluated.
+ * @test: The test context object.
+ * @message: an informational message to be printed when the assertion is made.
+ *
+ * The opposite of SUCCEED(), it is an expectation that always fails. In other
+ * words, it always results in a failed expectation, and consequently always
+ * causes the test case to fail when evaluated. See EXPECT_TRUE() for more
+ * information.
+ */
+#define FAIL(test, message) EXPECT(test, false, message)
+
+/**
+ * EXPECT_TRUE() - Causes a test failure when the given expression is not true.
+ * @test: The test context object.
+ * @condition: an arbitrary boolean expression. The test fails when this does
+ * not evaluate to true.
+ *
+ * This and expectations of the form `EXPECT_*` will cause the test case to fail
+ * when the specified condition is not met; however, it will not prevent the
+ * test case from continuing to run; this is otherwise known as an *expectation
+ * failure*.
+ */
+#define EXPECT_TRUE(test, condition) \
+ EXPECT(test, (condition), \
+ "Expected " #condition " is true, but is false.")
+
+/**
+ * EXPECT_FALSE() - Causes a test failure when the expression is not false.
+ * @test: The test context object.
+ * @condition: an arbitrary boolean expression. The test fails when this does
+ * not evaluate to false.
+ *
+ * Sets an expectation that @condition evaluates to false. See EXPECT_TRUE() for
+ * more information.
+ */
+#define EXPECT_FALSE(test, condition) \
+ EXPECT(test, !(condition), \
+ "Expected " #condition " is false, but is true.")
+
+static inline void test_expect_binary(struct test *test,
+ long long left, const char *left_name,
+ long long right, const char *right_name,
+ bool compare_result,
+ const char *compare_name,
+ const char *file,
+ const char *line)
+{
+ struct test_stream *stream = test_expect_start(test, file, line);
+
+ stream->add(stream,
+ "Expected %s %s %s, but\n",
+ left_name, compare_name, right_name);
+ stream->add(stream, "\t\t%s == %lld\n", left_name, left);
+ stream->add(stream, "\t\t%s == %lld", right_name, right);
+
+ test_expect_end(test, compare_result, stream);
+}
+
+/*
+ * A factory macro for defining the expectations for the basic comparisons
+ * defined for the built in types.
+ *
+ * Unfortunately, there is no common type that all types can be promoted to for
+ * which all the binary operators behave the same way as for the actual types
+ * (for example, there is no type that long long and unsigned long long can
+ * both be cast to where the comparison result is preserved for all values). So
+ * the best we can do is do the comparison in the original types and then coerce
+ * everything to long long for printing; this way, the comparison behaves
+ * correctly and the printed out value usually makes sense without
+ * interpretation, but can always be interpretted to figure out the actual
+ * value.
+ */
+#define EXPECT_BINARY(test, left, condition, right) do { \
+ typeof(left) __left = (left); \
+ typeof(right) __right = (right); \
+ test_expect_binary(test, \
+ (long long) __left, #left, \
+ (long long) __right, #right, \
+ __left condition __right, #condition, \
+ __FILE__, __stringify(__LINE__)); \
+} while (0)
+
+/**
+ * EXPECT_EQ() - Sets an expectation that @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to EXPECT_TRUE(@test, (@left) ==
+ * (@right)). See EXPECT_TRUE() for more information.
+ */
+#define EXPECT_EQ(test, left, right) EXPECT_BINARY(test, left, ==, right)
+
+/**
+ * EXPECT_NE() - An expectation that @left and @right are not equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are not
+ * equal. This is semantically equivalent to EXPECT_TRUE(@test, (@left) !=
+ * (@right)). See EXPECT_TRUE() for more information.
+ */
+#define EXPECT_NE(test, left, right) EXPECT_BINARY(test, left, !=, right)
+
+/**
+ * EXPECT_LT() - An expectation that @left is less than @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is less than the
+ * value that @right evaluates to. This is semantically equivalent to
+ * EXPECT_TRUE(@test, (@left) < (@right)). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_LT(test, left, right) EXPECT_BINARY(test, left, <, right)
+
+/**
+ * EXPECT_LE() - An expectation that @left is less than or equal to @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is less than or
+ * equal to the value that @right evaluates to. Semantically this is equivalent
+ * to EXPECT_TRUE(@test, (@left) <= (@right)). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_LE(test, left, right) EXPECT_BINARY(test, left, <=, right)
+
+/**
+ * EXPECT_GT() - An expectation that @left is greater than @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is greater than
+ * the value that @right evaluates to. This is semantically equivalent to
+ * EXPECT_TRUE(@test, (@left) > (@right)). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_GT(test, left, right) EXPECT_BINARY(test, left, >, right)
+
+/**
+ * EXPECT_GE() - An expectation that @left is greater than or equal to @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is greater than
+ * the value that @right evaluates to. This is semantically equivalent to
+ * EXPECT_TRUE(@test, (@left) >= (@right)). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_GE(test, left, right) EXPECT_BINARY(test, left, >=, right)
+
+/**
+ * EXPECT_STREQ() - An expectation that strings @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a null terminated string.
+ * @right: an arbitrary expression that evaluates to a null terminated string.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * EXPECT_TRUE(@test, !strcmp((@left), (@right))). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_STREQ(test, left, right) do { \
+ struct test_stream *__stream = EXPECT_START(test); \
+ typeof(left) __left = (left); \
+ typeof(right) __right = (right); \
+ \
+ __stream->add(__stream, "Expected " #left " == " #right ", but\n"); \
+ __stream->add(__stream, "\t\t%s == %s\n", #left, __left); \
+ __stream->add(__stream, "\t\t%s == %s\n", #right, __right); \
+ \
+ EXPECT_END(test, !strcmp(left, right), __stream); \
+} while (0)
+
+/**
+ * EXPECT_NOT_ERR_OR_NULL() - An expectation that @ptr is not null and not err.
+ * @test: The test context object.
+ * @ptr: an arbitrary pointer.
+ *
+ * Sets an expectation that the value that @ptr evaluates to is not null and not
+ * an errno stored in a pointer. This is semantically equivalent to
+ * EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See EXPECT_TRUE() for more
+ * information.
+ */
+#define EXPECT_NOT_ERR_OR_NULL(test, ptr) do { \
+ struct test_stream *__stream = EXPECT_START(test); \
+ typeof(ptr) __ptr = (ptr); \
+ \
+ if (!__ptr) \
+ __stream->add(__stream, \
+ "Expected " #ptr " is not null, but is."); \
+ if (IS_ERR(__ptr)) \
+ __stream->add(__stream, \
+ "Expected " #ptr " is not error, but is: %ld", \
+ PTR_ERR(__ptr)); \
+ \
+ EXPECT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \
+} while (0)
+
+#endif /* _TEST_TEST_H */
diff --git a/test/Kconfig b/test/Kconfig
new file mode 100644
index 0000000..2b6f57c
--- /dev/null
+++ b/test/Kconfig
@@ -0,0 +1,14 @@
+config TEST
+ bool
+ default y
+
+config TEST_TEST
+ bool
+ depends on TEST
+ default y
+
+config EXAMPLE_TEST
+ bool
+ depends on TEST
+ default y
+
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..911c9ff
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,3 @@
+obj-$(CONFIG_TEST) += test.o string-stream.o test-stream.o
+obj-$(CONFIG_TEST_TEST) += string-stream-test.o
+obj-$(CONFIG_EXAMPLE_TEST) += example-test.o
diff --git a/test/example-test.c b/test/example-test.c
new file mode 100644
index 0000000..53ec488
--- /dev/null
+++ b/test/example-test.c
@@ -0,0 +1,25 @@
+#include <test/test.h>
+
+static void example_simple_test(struct test *test)
+{
+ EXPECT_EQ(test, 1, 1);
+}
+
+static int example_test_init(struct test *test)
+{
+ test_info(test, "initializing");
+
+ return 0;
+}
+
+static struct test_case example_test_cases[] = {
+ TEST_CASE(example_simple_test),
+ {},
+};
+
+static struct test_module example_test_module = {
+ .name = "example",
+ .init = example_test_init,
+ .test_cases = example_test_cases,
+};
+module_test(example_test_module);
diff --git a/test/string-stream-test.c b/test/string-stream-test.c
new file mode 100644
index 0000000..442e748
--- /dev/null
+++ b/test/string-stream-test.c
@@ -0,0 +1,53 @@
+#include <linux/slab.h>
+#include <test/test.h>
+#include <test/string-stream.h>
+
+static void string_stream_test_get_string(struct test *test)
+{
+ struct string_stream *stream = new_string_stream();
+ char *output;
+
+ stream->add(stream, "Foo");
+ stream->add(stream, " %s", "bar");
+
+ output = stream->get_string(stream);
+ EXPECT_STREQ(test, output, "Foo bar");
+ kfree(output);
+ destroy_string_stream(stream);
+}
+
+static void string_stream_test_add_and_clear(struct test *test)
+{
+ struct string_stream *stream = new_string_stream();
+ char *output;
+ int i;
+
+ for (i = 0; i < 10; i++)
+ stream->add(stream, "A");
+
+ output = stream->get_string(stream);
+ EXPECT_STREQ(test, output, "AAAAAAAAAA");
+ EXPECT_EQ(test, stream->length, 10);
+ EXPECT_FALSE(test, stream->is_empty(stream));
+ kfree(output);
+
+ stream->clear(stream);
+
+ output = stream->get_string(stream);
+ EXPECT_STREQ(test, output, "");
+ EXPECT_TRUE(test, stream->is_empty(stream));
+ destroy_string_stream(stream);
+}
+
+static struct test_case string_stream_test_cases[] = {
+ TEST_CASE(string_stream_test_get_string),
+ TEST_CASE(string_stream_test_add_and_clear),
+ {}
+};
+
+static struct test_module string_stream_test_module = {
+ .name = "string-stream-test",
+ .test_cases = string_stream_test_cases
+};
+module_test(string_stream_test_module);
+
diff --git a/test/string-stream.c b/test/string-stream.c
new file mode 100644
index 0000000..f6049d6
--- /dev/null
+++ b/test/string-stream.c
@@ -0,0 +1,105 @@
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <test/string-stream.h>
+
+static int string_stream_vadd(struct string_stream *this,
+ const char *fmt,
+ va_list args)
+{
+ struct string_stream_fragment *fragment;
+ int len;
+ va_list args_for_counting;
+
+ /* Make a copy because `vsnprintf` could change it */
+ va_copy(args_for_counting, args);
+
+ /* Need space for null byte. */
+ len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1;
+
+ va_end(args_for_counting);
+
+ fragment = kmalloc(sizeof(*fragment), GFP_KERNEL);
+ if (!fragment)
+ return -ENOMEM;
+
+ fragment->fragment = kmalloc(len + 1, GFP_KERNEL);
+ if (!fragment->fragment) {
+ kfree(fragment);
+ return -ENOMEM;
+ }
+
+ len = vsnprintf(fragment->fragment, len, fmt, args);
+ this->length += len;
+ list_add_tail(&fragment->node, &this->fragments);
+ return 0;
+}
+
+static int string_stream_add(struct string_stream *this, const char *fmt, ...)
+{
+ va_list args;
+ int result;
+
+ va_start(args, fmt);
+ result = string_stream_vadd(this, fmt, args);
+ va_end(args);
+ return result;
+}
+
+static void string_stream_clear(struct string_stream *this)
+{
+ struct string_stream_fragment *fragment, *fragment_safe;
+
+ list_for_each_entry_safe(fragment,
+ fragment_safe,
+ &this->fragments,
+ node) {
+ list_del(&fragment->node);
+ kfree(fragment->fragment);
+ kfree(fragment);
+ }
+
+ this->length = 0;
+}
+
+static char *string_stream_get_string(struct string_stream *this)
+{
+ struct string_stream_fragment *fragment;
+ char *buf;
+
+ buf = kzalloc(this->length + 1, GFP_KERNEL);
+ if (!buf)
+ return NULL;
+
+ list_for_each_entry(fragment, &this->fragments, node) {
+ strcat(buf, fragment->fragment);
+ }
+ return buf;
+}
+
+static bool string_stream_is_empty(struct string_stream *this)
+{
+ return list_empty(&this->fragments);
+}
+
+void destroy_string_stream(struct string_stream *stream)
+{
+ stream->clear(stream);
+ kfree(stream);
+}
+
+struct string_stream *new_string_stream(void)
+{
+ struct string_stream *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+
+ if (!stream)
+ return NULL;
+
+ INIT_LIST_HEAD(&stream->fragments);
+ stream->length = 0;
+ stream->add = string_stream_add;
+ stream->vadd = string_stream_vadd;
+ stream->get_string = string_stream_get_string;
+ stream->clear = string_stream_clear;
+ stream->is_empty = string_stream_is_empty;
+ return stream;
+}
diff --git a/test/test-stream.c b/test/test-stream.c
new file mode 100644
index 0000000..89f0a08
--- /dev/null
+++ b/test/test-stream.c
@@ -0,0 +1,125 @@
+#include <test/test.h>
+#include <test/test-stream.h>
+#include <test/string-stream.h>
+
+static void test_stream_set_level(struct test_stream *this,
+ const char *level)
+{
+ this->level = level;
+}
+
+static void test_stream_add(struct test_stream *this, const char *fmt, ...)
+{
+ va_list args;
+ struct string_stream *stream = this->internal_stream;
+
+ va_start(args, fmt);
+ if (stream->vadd(stream, fmt, args) < 0)
+ test_err(this->test, "Failed to allocate fragment: %s", fmt);
+
+ va_end(args);
+}
+
+static void test_stream_append(struct test_stream *this,
+ struct test_stream *other)
+{
+ struct string_stream *other_stream = other->internal_stream;
+ const char *other_content;
+
+ other_content = other_stream->get_string(other_stream);
+
+ if (!other_content) {
+ test_err(this->test,
+ "Failed to get string from second argument for appending.");
+ return;
+ }
+
+ this->add(this, other_content);
+}
+
+static void test_stream_clear(struct test_stream *this)
+{
+ this->internal_stream->clear(this->internal_stream);
+}
+
+static void test_stream_commit(struct test_stream *this)
+{
+ char *buf;
+ struct string_stream_fragment *fragment;
+ struct string_stream *stream = this->internal_stream;
+
+ if (!this->level) {
+ test_err(this->test,
+ "Stream was committed without a specified log level.");
+ this->set_level(this, KERN_ERR);
+ }
+
+ buf = stream->get_string(stream);
+ if (!buf) {
+ test_err(this->test,
+ "Could not allocate buffer, dumping stream:");
+ list_for_each_entry(fragment, &stream->fragments, node) {
+ test_err(this->test, fragment->fragment);
+ }
+ goto cleanup;
+ }
+
+ test_printk(this->level, this->test, buf);
+ kfree(buf);
+
+cleanup:
+ this->clear(this);
+}
+
+static int test_stream_init(struct test_resource *res, void *context)
+{
+ struct test_stream *stream;
+ struct test *test = context;
+
+ stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+ if (!stream)
+ return -ENOMEM;
+ res->allocation = stream;
+ stream->test = test;
+ stream->level = NULL;
+ stream->internal_stream = new_string_stream();
+
+ if (!stream->internal_stream)
+ return -ENOMEM;
+
+ stream->set_level = test_stream_set_level;
+ stream->add = test_stream_add;
+ stream->append = test_stream_append;
+ stream->commit = test_stream_commit;
+ stream->clear = test_stream_clear;
+ return 0;
+}
+
+static void test_stream_free(struct test_resource *res)
+{
+ struct test_stream *stream = res->allocation;
+
+ if (!stream->internal_stream->is_empty(stream->internal_stream)) {
+ test_err(stream->test,
+ "End of test case reached with uncommitted stream entries.");
+ stream->commit(stream);
+ }
+
+ destroy_string_stream(stream->internal_stream);
+ kfree(stream);
+}
+
+struct test_stream *test_new_stream(struct test *test)
+{
+ struct test_resource *res;
+
+ res = test_alloc_resource(test,
+ test_stream_init,
+ test_stream_free,
+ test);
+
+ if (res)
+ return res->allocation;
+ else
+ return NULL;
+}
diff --git a/test/test.c b/test/test.c
new file mode 100644
index 0000000..ba8deef
--- /dev/null
+++ b/test/test.c
@@ -0,0 +1,236 @@
+#include <linux/sched.h>
+#include <linux/sched/debug.h>
+#include <os.h>
+#include <test/test.h>
+
+static int test_vprintk_emit(const struct test *test,
+ int level,
+ const char *fmt,
+ va_list args)
+{
+ return vprintk_emit(0, level, NULL, 0, fmt, args);
+}
+
+static int test_printk_emit(const struct test *test,
+ int level,
+ const char *fmt, ...)
+{
+ va_list args;
+ int ret;
+
+ va_start(args, fmt);
+ ret = test_vprintk_emit(test, level, fmt, args);
+ va_end(args);
+
+ return ret;
+}
+
+static void test_vprintk(const struct test *test,
+ const char *level,
+ struct va_format *vaf)
+{
+ test_printk_emit(test,
+ level[1] - '0',
+ "kunit %s: %pV", test->name, vaf);
+}
+
+static void test_fail(struct test *test, struct test_stream *stream)
+{
+ test->success = false;
+ stream->set_level(stream, KERN_ERR);
+ stream->commit(stream);
+}
+
+int test_init_test(struct test *test, const char *name)
+{
+ INIT_LIST_HEAD(&test->resources);
+ test->name = name;
+ test->vprintk = test_vprintk;
+ test->fail = test_fail;
+
+ return 0;
+}
+
+/*
+ * Initializes and runs test case. Does not clean up or do post validations.
+ */
+static void test_run_case_internal(struct test *test,
+ struct test_module *module,
+ struct test_case *test_case)
+{
+ int ret;
+
+ if (module->init) {
+ ret = module->init(test);
+ if (ret) {
+ test_err(test, "failed to initialize: %d", ret);
+ test->success = false;
+ return;
+ }
+ }
+
+ test_case->run_case(test);
+}
+
+static void test_case_internal_cleanup(struct test *test)
+{
+ test_cleanup(test);
+}
+
+/*
+ * Performs post validations and cleanup after a test case was run.
+ * XXX: Should ONLY BE CALLED AFTER test_run_case_internal!
+ */
+static void test_run_case_cleanup(struct test *test,
+ struct test_module *module,
+ struct test_case *test_case)
+{
+ if (module->exit)
+ module->exit(test);
+
+ test_case_internal_cleanup(test);
+}
+
+/*
+ * Performs all logic to run a test case.
+ */
+static bool test_run_case(struct test *test,
+ struct test_module *module,
+ struct test_case *test_case)
+{
+ test->success = true;
+
+ test_run_case_internal(test, module, test_case);
+ test_run_case_cleanup(test, module, test_case);
+
+ return test->success;
+}
+
+int test_run_tests(struct test_module *module)
+{
+ bool all_passed = true, success;
+ struct test_case *test_case;
+ struct test test;
+ int ret;
+
+ ret = test_init_test(&test, module->name);
+ if (ret)
+ return ret;
+
+ for (test_case = module->test_cases; test_case->run_case; test_case++) {
+ success = test_run_case(&test, module, test_case);
+ if (!success)
+ all_passed = false;
+
+ test_info(&test,
+ "%s %s",
+ test_case->name,
+ success ? "passed" : "failed");
+ }
+
+ if (all_passed)
+ test_info(&test, "all tests passed");
+ else
+ test_info(&test, "one or more tests failed");
+
+ return 0;
+}
+
+struct test_resource *test_alloc_resource(struct test *test,
+ int (*init)(struct test_resource *,
+ void *),
+ void (*free)(struct test_resource *),
+ void *context)
+{
+ struct test_resource *res;
+ int ret;
+
+ res = kzalloc(sizeof(*res), GFP_KERNEL);
+ if (!res)
+ return NULL;
+
+ ret = init(res, context);
+ if (ret)
+ return NULL;
+
+ res->free = free;
+ list_add_tail(&res->node, &test->resources);
+
+ return res;
+}
+
+void test_free_resource(struct test *test, struct test_resource *res)
+{
+ res->free(res);
+ list_del(&res->node);
+ kfree(res);
+}
+
+struct test_kmalloc_params {
+ size_t size;
+ gfp_t gfp;
+};
+
+static int test_kmalloc_init(struct test_resource *res, void *context)
+{
+ struct test_kmalloc_params *params = context;
+
+ res->allocation = kmalloc(params->size, params->gfp);
+ if (!res->allocation)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static void test_kmalloc_free(struct test_resource *res)
+{
+ kfree(res->allocation);
+}
+
+void *test_kmalloc(struct test *test, size_t size, gfp_t gfp)
+{
+ struct test_kmalloc_params params;
+ struct test_resource *res;
+
+ params.size = size;
+ params.gfp = gfp;
+
+ res = test_alloc_resource(test,
+ test_kmalloc_init,
+ test_kmalloc_free,
+ ¶ms);
+
+ if (res)
+ return res->allocation;
+ else
+ return NULL;
+}
+
+void test_cleanup(struct test *test)
+{
+ struct test_resource *resource, *resource_safe;
+
+ list_for_each_entry_safe(resource,
+ resource_safe,
+ &test->resources,
+ node) {
+ test_free_resource(test, resource);
+ }
+}
+
+void test_printk(const char *level,
+ const struct test *test,
+ const char *fmt, ...)
+{
+ struct va_format vaf;
+ va_list args;
+
+ va_start(args, fmt);
+
+ vaf.fmt = fmt;
+ vaf.va = &args;
+
+ test->vprintk(test, level, &vaf);
+
+ va_end(args);
+}