kunit: introduce KUnit, the Linux kernel unit testing framework
Subsystems (11)
v4 cover kunit: introduce KUnit, the Linux kernel unit testing framework
## TLDR
A quick follow up to yesterday's revision. I got some feedback that I wanted to incorporate before anyone else read the update. For this reason, I will leave a TLDR of the biggest changes since v2.
Biggest things to look out for (since v2):
- KUnit core now outputs results in TAP14.
- Heavily reworked tools/testing/kunit/kunit.py
- Changed how parsing works.
- Added testing.
- Greg, Logan, you might want to re-review this.
- Added documentation on how to use KUnit on non-UML kernels. You can see the docs rendered here[1].
There is still some discussion going on on the [PATCH v2 00/17] thread, but I wanted to get some of these updates out before they got too stale (and too difficult for me to keep track of). I hope no one minds.
## Background
This patch set proposes KUnit, a lightweight unit testing and mocking framework for the Linux kernel.
Unlike Autotest and kselftest, KUnit is a true unit testing framework; it does not require installing the kernel on a test machine or in a VM (however, KUnit still allows you to run tests on test machines or in VMs if you want) and does not require tests to be written in userspace running on a host kernel. Additionally, KUnit is fast: From invocation to completion KUnit can run several dozen tests in under a second. Currently, the entire KUnit test suite for KUnit runs in under a second from the initial invocation (build time excluded).
KUnit is heavily inspired by JUnit, Python's unittest.mock, and Googletest/Googlemock for C++. KUnit provides facilities for defining unit test cases, grouping related test cases into test suites, providing common infrastructure for running tests, mocking, spying, and much more.
## What's so special about unit testing?
A unit test is supposed to test a single unit of code in isolation, hence the name. There should be no dependencies outside the control of the test; this means no external dependencies, which makes tests orders of magnitudes faster. Likewise, since there are no external dependencies, there are no hoops to jump through to run the tests. Additionally, this makes unit tests deterministic: a failing unit test always indicates a problem. Finally, because unit tests necessarily have finer granularity, they are able to test all code paths easily solving the classic problem of difficulty in exercising error handling code.
## Is KUnit trying to replace other testing frameworks for the kernel?
No. Most existing tests for the Linux kernel are end-to-end tests, which have their place. A well tested system has lots of unit tests, a reasonable number of integration tests, and some end-to-end tests. KUnit is just trying to address the unit test space which is currently not being addressed.
## More information on KUnit
There is a bunch of documentation near the end of this patch set that describes how to use KUnit and best practices for writing unit tests. For convenience I am hosting the compiled docs here[2].
Additionally for convenience, I have applied these patches to a branch[3].
The repo may be cloned with:
git clone https://kunit.googlesource.com/linux
This patchset is on the kunit/rfc/v5.1/v4 branch.
## Changes Since Last Version
As I mentioned above, there are a significant number of updates since v2:
- Converted KUnit core to print test results in TAP14 format as suggested by Greg and Frank.
- Heavily reworked tools/testing/kunit/kunit.py
- Changed how parsing works.
- Added testing.
- Added documentation on how to use KUnit on non-UML kernels. You can see the docs rendered here[1].
- Added a new set of EXPECTs and ASSERTs for pointer comparison.
- Removed more function indirection as suggested by Logan.
- Added a new patch that adds kunit_try_catch_throw to objtool's noreturn list.
- Fixed a number of minorish issues pointed out by Shuah, Masahiro, and kbuild bot.
Nevertheless, there are only a couple of minor updates since v3:
- Added more context to the changelog on the objtool patch, as per Peter's request.
- Moved all KUnit documentation under the Documentation/dev-tools/ directory as per Jonathan's suggestion.
[1] https://google.github.io/kunit-docs/third_party/kernel/docs/usage.html#kunit-on-non-uml-architectures
[2] https://google.github.io/kunit-docs/third_party/kernel/docs/
[3] https://kunit.googlesource.com/linux/+/kunit/rfc/v5.1/v4
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 1/18 kunit: test: add KUnit test runner core
Add core facilities for defining unit tests; this provides a common way to define test cases, functions that execute code which is under test and determine whether the code under test behaves as expected; this also provides a way to group together related test cases in test suites (here we call them test_modules).
Just define test cases and how to execute them for now; setting expectations on code will be defined later.
--- include/kunit/test.h | 162 ++++++++++++++++++++++++++++++ kunit/Kconfig | 16 +++ kunit/Makefile | 1 + kunit/test.c | 229 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 408 insertions(+) create mode 100644 include/kunit/test.h create mode 100644 kunit/Kconfig create mode 100644 kunit/Makefile create mode 100644 kunit/test.c
include/kunit/test.h new file +162 -0
@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Base unit test (KUnit) API. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins <[email protected]> + */ + +#ifndef _KUNIT_TEST_H +#define _KUNIT_TEST_H + +#include <linux/types.h> +#include <linux/slab.h> + +struct kunit; + +/** + * struct kunit_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 kunit *)`` + * that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each + * test case is associated with a &struct kunit_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 KUNIT_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 kunit *test) + * { + * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); + * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); + * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); + * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); + * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); + * } + * + * static struct kunit_case example_test_cases[] = { + * KUNIT_CASE(add_test_basic), + * {}, + * }; + * + */ +struct kunit_case { + void (*run_case)(struct kunit *test); + const char name[256]; + + /* private: internal use only. */ + bool success; +}; + +/** + * KUNIT_CASE - A helper for creating a &struct kunit_case + * @test_name: a reference to a test case function. + * + * Takes a symbol for a function representing a test case and creates a + * &struct kunit_case object from it. See the documentation for + * &struct kunit_case for an example on how to use it. + */ +#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } + +/** + * struct kunit_module - describes a related collection of &struct kunit_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 kunit_module is a collection of related &struct kunit_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 kunit_case must be associated with a kunit_module for KUnit to + * run it. + */ +struct kunit_module { + const char name[256]; + int (*init)(struct kunit *test); + void (*exit)(struct kunit *test); + struct kunit_case *test_cases; +}; + +/** + * struct kunit - 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 kunit_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 kunit { + void *priv; + + /* private: internal use only. */ + const char *name; /* Read only after initialization! */ + spinlock_t lock; /* Gaurds all mutable test state. */ + bool success; /* Protected by lock. */ +}; + +void kunit_init_test(struct kunit *test, const char *name); + +int kunit_run_tests(struct kunit_module *module); + +/** + * module_test() - used to register a &struct kunit_module with KUnit. + * @module: a statically allocated &struct kunit_module. + * + * Registers @module with the test framework. See &struct kunit_module for more + * information. + */ +#define module_test(module) \ + static int module_kunit_init##module(void) \ + { \ + return kunit_run_tests(&module); \ + } \ + late_initcall(module_kunit_init##module) + +void __printf(3, 4) kunit_printk(const char *level, + const struct kunit *test, + const char *fmt, ...); + +/** + * kunit_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 kunit_info(test, fmt, ...) \ + kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) + +/** + * kunit_warn() - Prints a WARN level message associated with the current test. + * @test: The test context object. + * @fmt: A printk() style format string. + * + * See kunit_info(). + */ +#define kunit_warn(test, fmt, ...) \ + kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) + +/** + * kunit_err() - Prints an ERROR level message associated with the current test. + * @test: The test context object. + * @fmt: A printk() style format string. + * + * See kunit_info(). + */ +#define kunit_err(test, fmt, ...) \ + kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) + +#endif /* _KUNIT_TEST_H */
kunit/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# +# KUnit base configuration +# + +menu "KUnit support" + +config KUNIT + bool "Enable support for unit tests (KUnit)" + help + Enables support for kernel unit tests (KUnit), a lightweight unit + testing and mocking framework for the Linux kernel. These tests are + able to be run locally on a developer's workstation without a VM or + special hardware. For more information, please see + Documentation/kunit/ + +endmenu
kunit/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_KUNIT) += test.o
kunit/test.c new file +229 -0
229 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH v4 01/18] kunit: test: add KUnit test runner core
Quoting Brendan Higgins (2019-05-14 15:16:54)
Quoted 6 lines
include/kunit/test.h new file +0
-0@@ -0,0 +1,162 @@
[..]
Quoted 7 lines
include/kunit/test.h new file +7
-0+/** +* struct kunit - 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 kunit_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
include/kunit/test.h line 106
Quoted 10 lines
include/kunit/test.h new file +10
-0+* via public functions; the one exception is @priv which can be used by the +* test writer to store arbitrary data. +*/ +struct kunit { +void *priv; + +/* private: internal use only. */ +const char *name;/* Read only after initialization! */ +spinlock_t lock;/* Gaurds all mutable test state. */ +bool success;/* Protected by lock. */
Is this all the spinlock protects? Doesn't seem useful if it's just protecting access to the variable being set or not because code that reads it will have a stale view of the value.
Quoted 18 lines
kunit/test.c new file +12
-0@@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Base unit test (KUnit) API. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#include <linux/sched.h> +#include <linux/sched/debug.h> +#include <kunit/test.h> +
[...]
kunit/test.c line 122
Quoted 2 lines
kunit/test.c new file +2
-0+ +size_t kunit_module_counter =1 ;
static?
Re: [PATCH v4 01/18] kunit: test: add KUnit test runner core
In reply to Brendan HigginsQuoting Brendan Higgins (2019-05-14 15:16:54)
Quoted 9 lines
include/kunit/test.h new file +3
-0@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Base unit test (KUnit) API.
include/kunit/test.h line 13
Quoted 10 lines
include/kunit/test.h new file +10
-0+* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#ifndef _KUNIT_TEST_H +#define _KUNIT_TEST_H + +#include <linux/types.h> +#include <linux/slab.h>
Is this include used here?
Quoted 23 lines
include/kunit/test.h new file +23
-0+ +struct kunit ; + +/** +* struct kunit_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 kunit *)`` +* that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each +* test case is associated with a &struct kunit_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 KUNIT_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 kunit *test) +* {
include/kunit/test.h line 46
Quoted 10 lines
include/kunit/test.h new file +10
-0+* KUNIT_EXPECT_EQ(test, 1, add(1, 0)); +* KUNIT_EXPECT_EQ(test, 2, add(1, 1)); +* KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); +* KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); +* KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); +* } +* +* static struct kunit_case example_test_cases[] = { +* KUNIT_CASE(add_test_basic), +* {},
Nitpick: Please drop the comma on the sentinel so nobody gets ideas to add another entry after it.
include/kunit/test.h line 52
Quoted 6 lines
include/kunit/test.h new file +6
-0+* }; +* +*/ +struct kunit_case { +void (*run_case)(struct kunit *test); +const char name[256 ];
Maybe 256 can be a #define KUNIT_NAME_MAX_LEN? Or it could just be a const char pointer to a literal pool? Are unit tests making up names at runtime?
Quoted 25 lines
include/kunit/test.h new file +25
-0+ +/* private: internal use only. */ +bool success; +}; + +/** +* KUNIT_CASE - A helper for creating a &struct kunit_case +* @test_name: a reference to a test case function. +* +* Takes a symbol for a function representing a test case and creates a +* &struct kunit_case object from it. See the documentation for +* &struct kunit_case for an example on how to use it. +*/ +#define KUNIT_CASE (test_name) { .run_case = test_name, .name = #test_name } + +/** +* struct kunit_module - describes a related collection of &struct kunit_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 kunit_module is a collection of related &struct kunit_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
include/kunit/test.h line 87
Quoted 10 lines
include/kunit/test.h new file +10
-0+* unit testing frameworks like JUnit or Googletest. +* +* Every &struct kunit_case must be associated with a kunit_module for KUnit to +* run it. +*/ +struct kunit_module { +const char name[256 ]; +int (*init)(struct kunit *test); +void (*exit)(struct kunit *test); +struct kunit_case *test_cases;
Can this variable be const? Or we expect test modules to adjust test_cases after the fact?
Quoted 28 lines
include/kunit/test.h new file +28
-0+}; + +/** +* struct kunit - 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 kunit_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 kunit { +void *priv; + +/* private: internal use only. */ +const char *name;/* Read only after initialization! */ +spinlock_t lock;/* Gaurds all mutable test state. */ +bool success;/* Protected by lock. */ +}; + +void kunit_init_test (struct kunit *test,const char *name); + +int kunit_run_tests (struct kunit_module *module); + +/** +* module_test() - used to register a &struct kunit_module with KUnit. +* @module: a statically allocated &struct kunit_module.
include/kunit/test.h line 125
Quoted 10 lines
include/kunit/test.h new file +10
-0+* +* Registers @module with the test framework. See &struct kunit_module for more +* information. +*/ +#define module_test (module) \ + static int module_kunit_init##module(void) \ + { \ + return kunit_run_tests(&module); \ + } \ + late_initcall(module_kunit_init##module)
Maybe we need to introduce another initcall level after late_initcall_sync() for tests? I wonder if there will be tests that need to run after all other initcalls have run, including late sync initcalls.
Quoted 12 lines
include/kunit/test.h new file +12
-0+ +void __printf(3, 4) kunit_printk(const char *level, + const struct kunit *test, + const char *fmt, ...); + +/** + * kunit_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().
include/kunit/test.h line 147
Quoted 10 lines
include/kunit/test.h new file +10
-0+*/ +#define kunit_info (test, fmt, ...) \ + kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) + +/** +* kunit_warn() - Prints a WARN level message associated with the current test. +* @test: The test context object. +* @fmt: A printk() style format string. +* +* See kunit_info().
Why? Just write out that it "Prints a warning level message".
include/kunit/test.h line 157
Quoted 10 lines
include/kunit/test.h new file +10
-0+*/ +#define kunit_warn (test, fmt, ...) \ + kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) + +/** +* kunit_err() - Prints an ERROR level message associated with the current test. +* @test: The test context object. +* @fmt: A printk() style format string. +* +* See kunit_info().
Same comment.
Quoted 15 lines
include/kunit/test.h new file +5
-0+*/ +#define kunit_err (test, fmt, ...) \ + kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) + +#endif /* _KUNIT_TEST_H */ kunit/Kconfig new file +4
-0@@ -0,0 +1,16 @@ +# +# KUnit base configuration +# +
kunit/Kconfig line 14
Quoted 10 lines
kunit/Kconfig new file +10
-0+menu "KUnit support" + +config KUNIT + bool "Enable support for unit tests (KUnit)" + help + Enables support for kernel unit tests (KUnit), a lightweight unit + testing and mocking framework for the Linux kernel. These tests are + able to be run locally on a developer's workstation without a VM or + special hardware. For more information, please see + Documentation/kunit/
This moved and needs an update.
Quoted 15 lines
kunit/Kconfig new file +2
-0+ +endmenukunit/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_KUNIT) += test.okunit/test.c new file +0
-0
kunit/test.c line 9
Quoted 9 lines
kunit/test.c new file +9
-0@@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Base unit test (KUnit) API. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#include <linux/sched.h>
This include gets removed later in the series, was it ever needed?
Quoted 93 lines
kunit/test.c new file +93
-0+#include <linux/sched/debug.h> +#include <kunit/test.h> + +static bool kunit_get_success (struct kunit *test) +{ +unsigned long flags; +bool success; + +spin_lock_irqsave (&test->lock, flags); + success = test->success; +spin_unlock_irqrestore (&test->lock, flags); + +return success; +} + +static void kunit_set_success (struct kunit *test,bool success) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->success = success; +spin_unlock_irqrestore (&test->lock, flags); +} + +static int kunit_vprintk_emit (int level,const char *fmt,va_list args) +{ +return vprintk_emit (0 , level,NULL ,0 , fmt, args); +} + +static int kunit_printk_emit (int level,const char *fmt, ...) +{ +va_list args; +int ret; + +va_start (args, fmt); + ret =kunit_vprintk_emit (level, fmt, args); +va_end (args); + +return ret; +} + +static void kunit_vprintk (const struct kunit *test, +const char *level, +struct va_format *vaf) +{ +kunit_printk_emit (level[1 ] -'0' ,"\t# %s: %pV" , test->name, vaf); +} + +static bool kunit_has_printed_tap_version; + +static void kunit_print_tap_version (void ) +{ +if (!kunit_has_printed_tap_version) { +kunit_printk_emit (LOGLEVEL_INFO ,"TAP version 14\n" ); + kunit_has_printed_tap_version = true; + } +} + +static size_t kunit_test_cases_len (struct kunit_case *test_cases) +{ +struct kunit_case *test_case; +size_t len =0 ; + +for (test_case = test_cases; test_case->run_case; test_case++) + len++; + +return len; +} + +static void kunit_print_subtest_start (struct kunit_module *module) +{ +kunit_print_tap_version (); +kunit_printk_emit (LOGLEVEL_INFO ,"\t# Subtest: %s\n" , module->name); +kunit_printk_emit (LOGLEVEL_INFO , +"\t1..%zd\n" , +kunit_test_cases_len (module->test_cases)); +} + +static void kunit_print_ok_not_ok (bool should_indent, +bool is_ok, +size_t test_number, +const char *description) +{ +const char *indent, *ok_not_ok; + +if (should_indent) + indent ="\t" ; +else + indent ="" ; + +if (is_ok) + ok_not_ok ="ok" ; +else
kunit/test.c line 112
Quoted 10 lines
kunit/test.c new file +10
-0+ ok_not_ok ="not ok" ; + +kunit_printk_emit (LOGLEVEL_INFO , +"%s%s %zd - %s\n" , + indent, ok_not_ok, test_number, description); +} + +static bool kunit_module_has_succeeded (struct kunit_module *module) +{ +struct kunit_case *test_case;
This can be const?
kunit/test.c line 117
Quoted 5 lines
kunit/test.c new file +5
-0+bool success = true; + +for (test_case = module->test_cases; test_case->run_case; test_case++) +if (!test_case->success) + success = false;
Bail out early here on first "fail" with return false?
Quoted 47 lines
kunit/test.c new file +47
-0+ +return success; +} + +size_t kunit_module_counter =1 ; + +static void kunit_print_subtest_end (struct kunit_module *module) +{ +kunit_print_ok_not_ok (false, +kunit_module_has_succeeded (module), + kunit_module_counter++, + module->name); +} + +static void kunit_print_test_case_ok_not_ok (struct kunit_case *test_case, +size_t test_number) +{ +kunit_print_ok_not_ok (true, + test_case->success, + test_number, + test_case->name); +} + +void kunit_init_test (struct kunit *test,const char *name) +{ +spin_lock_init (&test->lock); + test->name = name; +} + +/* +* Initializes and runs test case. Does not clean up or do post validations. +*/ +static void kunit_run_case_internal (struct kunit *test, +struct kunit_module *module, +struct kunit_case *test_case) +{ +int ret; + +if (module->init) { + ret = module->init (test); +if (ret) { +kunit_err (test,"failed to initialize: %d\n" , ret); +kunit_set_success (test, false); +return ; + } + } +
kunit/test.c line 174
Quoted 10 lines
kunit/test.c new file +10
-0+ test_case->run_case (test); +} + +/* +* Performs post validations and cleanup after a test case was run. +* XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal! +*/ +static void kunit_run_case_cleanup (struct kunit *test, +struct kunit_module *module, +struct kunit_case *test_case)
But test_case isn't used?
kunit/test.c line 176
Quoted 2 lines
kunit/test.c new file +2
-0+{ +if (module->exit)
Aha, so we don't need empty functions in the sysctl test.
kunit/test.c line 189
Quoted 13 lines
kunit/test.c new file +13
-0+ module->exit (test); +} + +/* +* Performs all logic to run a test case. +*/ +static void kunit_run_case (struct kunit_module *module, +struct kunit_case *test_case) +{ +struct kunit test; + +kunit_init_test (&test, test_case->name); +kunit_set_success (&test, true);
Can kunit_init_test() also kunit_set_success() to true or false, depending on what is desired as the initial state?
kunit/test.c line 192
Quoted 3 lines
kunit/test.c new file +3
-0+ +kunit_run_case_internal (&test, module, test_case); +kunit_run_case_cleanup (&test, module, test_case);
I find this odd, we have run_case_internal() that does two things, init and run_case, while case_cleanup() does one thing, call module->exit().
Can we just inline all those functions in here so that it looks like this:
int ret = 0;
if (module->init) {
ret = module->init(test);
if (ret) {
kunit_err(test, "failed to initialize: %d\n", ret);
kunit_set_success(&test, false);
}
}
if (!ret)
test_case->run_case(&test);
if (module->exit)
module->exit(&test);
return kunit_get_success(&test);Then I don't have to read two more functions to figure out the flow of running a test case.
kunit/test.c line 200
Quoted 8 lines
kunit/test.c new file +8
-0+ + test_case->success =kunit_get_success (&test); +} + +int kunit_run_tests (struct kunit_module *module) +{ +struct kunit_case *test_case; +size_t test_case_count =1 ;
Might make sense to assign this to 0 first and then pre-increment so that test_case_count can't be 1 when there aren't any tests?
kunit/test.c line 206
Quoted 6 lines
kunit/test.c new file +6
-0+ +kunit_print_subtest_start (module); + +for (test_case = module->test_cases; test_case->run_case; test_case++) { +kunit_run_case (module, test_case); +kunit_print_test_case_ok_not_ok (test_case, test_case_count++);
Can this be pushed into kunit_run_case() and have that function take a test_case_count number? Maybe that would allow us to avoid storing test_case->success entirely? Assuming that kunit_run_case() returned a value like success or failure, then yes it would work.
unsigned int failed = 0;
for (test_case = module->test_cases; test_case->run_case; test_case++) {
failed |= kunit_run_case(module, test_case, ++test_case_count);
kunit_print_ok_not_ok(false,
!failed,
kunit_module_counter++,
module->name);Quoted 4 lines
kunit/test.c new file +4
-0+kunit_print_subtest_end (module); + +return 0 ; +}
Re: [PATCH v4 01/18] kunit: test: add KUnit test runner core
Quoted 15 lines · Stephen Boyd
Quoting Brendan Higgins (2019-05-14 15:16:54)
Quoted 13 lines
include/kunit/test.h new file +7
-0@@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Base unit test (KUnit) API. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/
Quoted 8 lines
include/kunit/test.h line 13
Quoted 6 lines
include/kunit/test.h new file +6
-0+ +#ifndef _KUNIT_TEST_H +#define _KUNIT_TEST_H + +#include <linux/types.h> +#include <linux/slab.h> Is this include used here?
Err, it is used in the very next commit in the sequence. Sorry, I will add it in the commit that actually uses it in the next revision.
Quoted 28 lines
Quoted 28 lines
include/kunit/test.h new file +28
-0+ +struct kunit ; + +/** +* struct kunit_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 kunit *)`` +* that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each +* test case is associated with a &struct kunit_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 KUNIT_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 kunit *test) +* { +* KUNIT_EXPECT_EQ(test, 1, add(1, 0)); +* KUNIT_EXPECT_EQ(test, 2, add(1, 1)); +* KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); +* KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); +* KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
Quoted 8 lines
include/kunit/test.h line 46
Quoted 5 lines
include/kunit/test.h new file +5
-0+* } +* +* static struct kunit_case example_test_cases[] = { +* KUNIT_CASE(add_test_basic), +* {}, Nitpick: Please drop the comma on the sentinel so nobody gets ideas to add another entry after it.
Good idea. Will fix here and elsewhere.
Quoted 10 lines
include/kunit/test.h line 52
Quoted 6 lines
include/kunit/test.h new file +6
-0+* }; +* +*/ +struct kunit_case { +void (*run_case)(struct kunit *test); +const char name[256 ];Maybe 256 can be a #define
KUNIT_NAME_MAX_LEN? Or it could just be a const char pointer to a literal pool? Are unit tests making up names at runtime?
Yeah, sorry, I forgot why I did it this way in the first place. Will fix in next revision.
Quoted 30 lines
Quoted 30 lines
include/kunit/test.h new file +30
-0+ +/* private: internal use only. */ +bool success; +}; + +/** +* KUNIT_CASE - A helper for creating a &struct kunit_case +* @test_name: a reference to a test case function. +* +* Takes a symbol for a function representing a test case and creates a +* &struct kunit_case object from it. See the documentation for +* &struct kunit_case for an example on how to use it. +*/ +#define KUNIT_CASE (test_name) { .run_case = test_name, .name = #test_name } + +/** +* struct kunit_module - describes a related collection of &struct kunit_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 kunit_module is a collection of related &struct kunit_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 kunit_case must be associated with a kunit_module for KUnit to +* run it. +*/
Quoted 8 lines
include/kunit/test.h line 87
Quoted 5 lines
include/kunit/test.h new file +5
-0+struct kunit_module { +const char name[256 ]; +int (*init)(struct kunit *test); +void (*exit)(struct kunit *test); +struct kunit_case *test_cases;Can this variable be const? Or we expect test modules to adjust
test_casesafter the fact?
I understand why it would be nice to do it that way, but we store the failed result on test cases; I don't think it really makes sense to have another parallel data structure just for the results on each test case.
Quoted 35 lines
Quoted 35 lines
include/kunit/test.h new file +35
-0+}; + +/** +* struct kunit - 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 kunit_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 kunit { +void *priv; + +/* private: internal use only. */ +const char *name;/* Read only after initialization! */ +spinlock_t lock;/* Gaurds all mutable test state. */ +bool success;/* Protected by lock. */ +}; + +void kunit_init_test (struct kunit *test,const char *name); + +int kunit_run_tests (struct kunit_module *module); + +/** +* module_test() - used to register a &struct kunit_module with KUnit. +* @module: a statically allocated &struct kunit_module. +* +* Registers @module with the test framework. See &struct kunit_module for more +* information. +*/ +#define module_test (module) \ + static int module_kunit_init##module(void) \ + { \
Quoted 8 lines
include/kunit/test.h line 125
Quoted 3 lines
include/kunit/test.h new file +3
-0+return kunit_run_tests (&module); \ + } \ + late_initcall(module_kunit_init##module)Maybe we need to introduce another initcall level after
late_initcall_sync()for tests? I wonder if there will be tests that need to run after all other initcalls have run, including late sync initcalls.
Yeah, I have another patch ready to go to do that. I guess I just figured that was something that could lead to a lot of bikeshedding that I wanted to avoid in the initial patchset.
Nevertheless, I can add it in if you feel it is better to discuss now.
Quoted 84 lines
Quoted 12 lines
include/kunit/test.h new file +12
-0+ +void __printf(3, 4) kunit_printk(const char *level, + const struct kunit *test, + const char *fmt, ...); + +/** + * kunit_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().include/kunit/test.h line 147
Quoted 10 lines
include/kunit/test.h new file +10
-0+*/ +#define kunit_info (test, fmt, ...) \ + kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) + +/** +* kunit_warn() - Prints a WARN level message associated with the current test. +* @test: The test context object. +* @fmt: A printk() style format string. +* +* See kunit_info(). Why? Just write out that it "Prints a warning level message".
include/kunit/test.h line 157
Quoted 10 lines
include/kunit/test.h new file +10
-0+*/ +#define kunit_warn (test, fmt, ...) \ + kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) + +/** +* kunit_err() - Prints an ERROR level message associated with the current test. +* @test: The test context object. +* @fmt: A printk() style format string. +* +* See kunit_info(). Same comment.
Quoted 15 lines
include/kunit/test.h new file +5
-0+*/ +#define kunit_err (test, fmt, ...) \ + kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) + +#endif /* _KUNIT_TEST_H */ kunit/Kconfig new file +4
-0@@ -0,0 +1,16 @@ +# +# KUnit base configuration +# +kunit/Kconfig line 14
Quoted 10 lines
kunit/Kconfig new file +10
-0+menu "KUnit support" + +config KUNIT + bool "Enable support for unit tests (KUnit)" + help + Enables support for kernel unit tests (KUnit), a lightweight unit + testing and mocking framework for the Linux kernel. These tests are + able to be run locally on a developer's workstation without a VM or + special hardware. For more information, please see + Documentation/kunit/This moved and needs an update.
Quoted 18 lines
kunit/Kconfig new file +2
-0+ +endmenukunit/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_KUNIT) += test.okunit/test.c new file +3
-0@@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Base unit test (KUnit) API.
Quoted 8 lines
kunit/test.c line 9
Quoted 6 lines
kunit/test.c new file +6
-0+* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#include <linux/sched.h> This include gets removed later in the series, was it ever needed?
Nope, that was a mistake. Sorry.
Quoted 97 lines
Quoted 97 lines
kunit/test.c new file +97
-0+#include <linux/sched/debug.h> +#include <kunit/test.h> + +static bool kunit_get_success (struct kunit *test) +{ +unsigned long flags; +bool success; + +spin_lock_irqsave (&test->lock, flags); + success = test->success; +spin_unlock_irqrestore (&test->lock, flags); + +return success; +} + +static void kunit_set_success (struct kunit *test,bool success) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->success = success; +spin_unlock_irqrestore (&test->lock, flags); +} + +static int kunit_vprintk_emit (int level,const char *fmt,va_list args) +{ +return vprintk_emit (0 , level,NULL ,0 , fmt, args); +} + +static int kunit_printk_emit (int level,const char *fmt, ...) +{ +va_list args; +int ret; + +va_start (args, fmt); + ret =kunit_vprintk_emit (level, fmt, args); +va_end (args); + +return ret; +} + +static void kunit_vprintk (const struct kunit *test, +const char *level, +struct va_format *vaf) +{ +kunit_printk_emit (level[1 ] -'0' ,"\t# %s: %pV" , test->name, vaf); +} + +static bool kunit_has_printed_tap_version; + +static void kunit_print_tap_version (void ) +{ +if (!kunit_has_printed_tap_version) { +kunit_printk_emit (LOGLEVEL_INFO ,"TAP version 14\n" ); + kunit_has_printed_tap_version = true; + } +} + +static size_t kunit_test_cases_len (struct kunit_case *test_cases) +{ +struct kunit_case *test_case; +size_t len =0 ; + +for (test_case = test_cases; test_case->run_case; test_case++) + len++; + +return len; +} + +static void kunit_print_subtest_start (struct kunit_module *module) +{ +kunit_print_tap_version (); +kunit_printk_emit (LOGLEVEL_INFO ,"\t# Subtest: %s\n" , module->name); +kunit_printk_emit (LOGLEVEL_INFO , +"\t1..%zd\n" , +kunit_test_cases_len (module->test_cases)); +} + +static void kunit_print_ok_not_ok (bool should_indent, +bool is_ok, +size_t test_number, +const char *description) +{ +const char *indent, *ok_not_ok; + +if (should_indent) + indent ="\t" ; +else + indent ="" ; + +if (is_ok) + ok_not_ok ="ok" ; +else + ok_not_ok ="not ok" ; + + kunit_printk_emit(LOGLEVEL_INFO, + "%s%s %zd - %s\n",
Quoted 8 lines
kunit/test.c line 112
Quoted 6 lines
kunit/test.c new file +6
-0+ indent, ok_not_ok, test_number, description); +} + +static bool kunit_module_has_succeeded (struct kunit_module *module) +{ +struct kunit_case *test_case;This can be const?
Yep, nice catch.
Quoted 7 lines
kunit/test.c line 117
Quoted 5 lines
kunit/test.c new file +5
-0+bool success = true; + +for (test_case = module->test_cases; test_case->run_case; test_case++) +if (!test_case->success) + success = false;Bail out early here on first "fail" with return false?
Will fix on next revision.
Quoted 51 lines
Quoted 51 lines
kunit/test.c new file +51
-0+ +return success; +} + +size_t kunit_module_counter =1 ; + +static void kunit_print_subtest_end (struct kunit_module *module) +{ +kunit_print_ok_not_ok (false, +kunit_module_has_succeeded (module), + kunit_module_counter++, + module->name); +} + +static void kunit_print_test_case_ok_not_ok (struct kunit_case *test_case, +size_t test_number) +{ +kunit_print_ok_not_ok (true, + test_case->success, + test_number, + test_case->name); +} + +void kunit_init_test (struct kunit *test,const char *name) +{ +spin_lock_init (&test->lock); + test->name = name; +} + +/* +* Initializes and runs test case. Does not clean up or do post validations. +*/ +static void kunit_run_case_internal (struct kunit *test, +struct kunit_module *module, +struct kunit_case *test_case) +{ +int ret; + +if (module->init) { + ret = module->init (test); +if (ret) { +kunit_err (test,"failed to initialize: %d\n" , ret); +kunit_set_success (test, false); +return ; + } + } + + test_case->run_case (test); +} + +/*
Quoted 8 lines
kunit/test.c line 174
Quoted 6 lines
kunit/test.c new file +6
-0+* Performs post validations and cleanup after a test case was run. +* XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal! +*/ +static void kunit_run_case_cleanup (struct kunit *test, +struct kunit_module *module, +struct kunit_case *test_case)But
test_caseisn't used?
Whoops, will fix.
Quoted 4 lines
kunit/test.c line 176
Quoted 2 lines
kunit/test.c new file +2
-0+{ +if (module->exit)Aha, so we don't need empty functions in the sysctl test.
Nope, sorry about that. That will be fixed in the next revision.
Quoted 45 lines
kunit/test.c line 189
Quoted 13 lines
kunit/test.c new file +13
-0+ module->exit (test); +} + +/* +* Performs all logic to run a test case. +*/ +static void kunit_run_case (struct kunit_module *module, +struct kunit_case *test_case) +{ +struct kunit test; + +kunit_init_test (&test, test_case->name); +kunit_set_success (&test, true);Can
kunit_init_test()alsokunit_set_success()to true or false, depending on what is desired as the initial state?kunit/test.c line 192
Quoted 3 lines
kunit/test.c new file +3
-0+ +kunit_run_case_internal (&test, module, test_case); +kunit_run_case_cleanup (&test, module, test_case);I find this odd, we have
run_case_internal()that does two things, init andrun_case, whilecase_cleanup()does one thing, callmodule->exit().Can we just inline all those functions in here so that it looks like this:
int ret = 0; if (module->init) { ret = module->init(test); if (ret) { kunit_err(test, "failed to initialize: %d\n", ret); kunit_set_success(&test, false); } } if (!ret) test_case->run_case(&test); if (module->exit) module->exit(&test); return kunit_get_success(&test);
Quoted 2 lines
Then I don't have to read two more functions to figure out the flow of running a test case.
Sorry about that. A lot more logic gets added to running test cases later on in this patchset. This logic added "later" is actually older than what's here, so I basically "unrefactored" something I had already written to get this earlier patch.
In anycase, you are right; these little tiny functions don't make any sense (yet); I will remove them in the next revision (and let the later patch make these changes in a more organic looking way).
Quoted 12 lines
kunit/test.c line 200
Quoted 9 lines
kunit/test.c new file +8
-0+ + test_case->success =kunit_get_success (&test); +} + +int kunit_run_tests (struct kunit_module *module) +{ +struct kunit_case *test_case; +size_t test_case_count =1 ;Might make sense to assign this to 0 first and then pre-increment so that
test_case_countcan't be 1 when there aren't any tests?
No, sorry, this is actually specified by the Test Anything Protocol (TAP); it indexes it's tests starting at 1; not what I would have done, but we (Greg, Frank, myself, and others) previously agreed that KUnit should follow TAP[1].
Maybe the name of this variable is a bit misleading since it is the "test number" or test index. Would test_case_number be better?
Quoted 11 lines
kunit/test.c line 206
Quoted 6 lines
kunit/test.c new file +6
-0+ +kunit_print_subtest_start (module); + +for (test_case = module->test_cases; test_case->run_case; test_case++) { +kunit_run_case (module, test_case); +kunit_print_test_case_ok_not_ok (test_case, test_case_count++);Can this be pushed into
kunit_run_case()and have that function take atest_case_countnumber? Maybe that would allow us to avoid storingtest_case->successentirely? Assuming thatkunit_run_case()returned a value like success or failure, then yes it would work.
Eh, I would prefer not to do that. I like keeping the printing/reporting functions as orthogonal as possible; it kind of mirrors the parsing logic on the test harness script side, and will also be easier to pull out when I go back to improve the expectation failure reporting when I get around to that later. Even if I never get around to that, I think this will be much easier for me to maintain.
Quoted 10 lines
unsigned int failed = 0;
for (test_case = module->test_cases; test_case->run_case; test_case++) {
failed |= kunit_run_case(module, test_case, ++test_case_count);
kunit_print_ok_not_ok(false,
!failed,
kunit_module_counter++,
module->name);Quoted 4 lines
kunit/test.c line 212
Quoted 4 lines
kunit/test.c new file +4
-0+kunit_print_subtest_end (module); + +return 0 ; +}
PATCH v4 2/18 kunit: test: add test resource management API
In reply to Brendan HigginsCreate a common API for test managed resources like memory and test objects. A lot of times a test will want to set up infrastructure to be used in test cases; this could be anything from just wanting to allocate some memory to setting up a driver stack; this defines facilities for creating "test resources" which are managed by the test infrastructure and are automatically cleaned up at the conclusion of the test.
--- include/kunit/test.h | 109 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 95 +++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+)
include/kunit/test.h +109 -0
@@ -12,6 +12,69 @@#include <linux/types.h> #include <linux/slab.h> +struct kunit_resource ; + +typedef int (*kunit_resource_init_t )(struct kunit_resource *,void *); +typedef void (*kunit_resource_free_t )(struct kunit_resource *); + +/** +* struct kunit_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 +* kunit_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 kunit_kmalloc_params { +* size_t size; +* gfp_t gfp; +* }; +* +* static int kunit_kmalloc_init(struct kunit_resource *res, void *context) +* { +* struct kunit_kmalloc_params *params = context; +* res->allocation = kmalloc(params->size, params->gfp); +* +* if (!res->allocation) +* return -ENOMEM; +* +* return 0; +* } +* +* static void kunit_kmalloc_free(struct kunit_resource *res) +* { +* kfree(res->allocation); +* } +* +* void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) +* { +* struct kunit_kmalloc_params params; +* struct kunit_resource *res; +* +* params.size = size; +* params.gfp = gfp; +* +* res = kunit_alloc_resource(test, kunit_kmalloc_init, +* kunit_kmalloc_free, ¶ms); +* if (res) +* return res->allocation; +* else +* return NULL; +* } +*/ +struct kunit_resource { +void *allocation; +kunit_resource_free_t free; + +/* private: internal use only. */ +struct list_head node; +}; +struct kunit ;/**
@@ -104,6 +167,7 @@ struct kunit {const char *name;/* Read only after initialization! */ spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +struct list_head resources;/* Protected by lock. */ };void kunit_init_test (struct kunit *test,const char *name);
@@ -124,6 +188,51 @@ int kunit_run_tests(struct kunit_module *module); } \ late_initcall(module_kunit_init##module) +/** +* kunit_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 to the init function. +* +* Allocates a *test managed resource*, a resource which will automatically be +* cleaned up at the end of a test case. See &struct kunit_resource for an +* example. +*/ +struct kunit_resource *kunit_alloc_resource (struct kunit *test, +kunit_resource_init_t init, +kunit_resource_free_t free, +void *context); + +void kunit_free_resource (struct kunit *test,struct kunit_resource *res); + +/** +* kunit_kmalloc() - 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 +* kunit_resource for more information. +*/ +void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp); + +/** +* kunit_kzalloc() - Just like kunit_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 kunit_kmalloc() for more information. +*/ +static inline void *kunit_kzalloc (struct kunit *test,size_t size,gfp_t gfp) +{ +return kunit_kmalloc (test, size, gfp | __GFP_ZERO); +} + +void kunit_cleanup (struct kunit *test); + void __printf(3, 4) kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...);
kunit/test.c +95 -0
119 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH v4 02/18] kunit: test: add test resource management API
Quoting Brendan Higgins (2019-05-14 15:16:55)
Quoted 4 lines
kunit/test.c +0
-0
[..]
Quoted 8 lines
kunit/test.c +8
-0+ +void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp) +{ +struct kunit_kmalloc_params params; +struct kunit_resource *res; + + params.size = size; + params.gfp = gfp;
kunit/test.c int kunit_run_tests(struct kunit_module *module) · line 291
Quoted 10 lines
kunit/test.c +10
-0+ + res =kunit_alloc_resource (test, + kunit_kmalloc_init, + kunit_kmalloc_free, + ¶ms); + +if (res) +return res->allocation; +else +return NULL ;
Can be written as
if (res) return .... return
and some static analysis tools prefer this.
kunit/test.c int kunit_run_tests(struct kunit_module *module) · line 299
Quoted 8 lines
kunit/test.c +8
-0+} + +void kunit_cleanup (struct kunit *test) +{ +struct kunit_resource *resource, *resource_safe; +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags);
Ah ok, test->lock is protecting everything now? Does it need to be a spinlock, or can it be a mutex?
Quoted 9 lines
kunit/test.c +9
-0+list_for_each_entry_safe (resource, + resource_safe, + &test->resources, + node) { +kunit_free_resource (test, resource); + } +spin_unlock_irqrestore (&test->lock, flags); +} +
Re: [PATCH v4 02/18] kunit: test: add test resource management API
Quoted 28 lines · Stephen Boyd
Quoting Brendan Higgins (2019-05-14 15:16:55)
Quoted 4 lines
kunit/test.c +0
-0[..]
Quoted 8 lines
kunit/test.c +8
-0+ +void *kunit_kmalloc (struct kunit *test,size_t size,gfp_t gfp) +{ +struct kunit_kmalloc_params params; +struct kunit_resource *res; + + params.size = size; + params.gfp = gfp;kunit/test.c int kunit_run_tests(struct kunit_module *module) · line 291
Quoted 10 lines
kunit/test.c +10
-0+ + res =kunit_alloc_resource (test, + kunit_kmalloc_init, + kunit_kmalloc_free, + ¶ms); + +if (res) +return res->allocation; +else +return NULL ;Can be written as
Quoted 5 lines
if (res) return .... returnand some static analysis tools prefer this.
Sounds reasonable, will fix in next revision.
Quoted 11 lines
kunit/test.c int kunit_run_tests(struct kunit_module *module) · line 299
Quoted 8 lines
kunit/test.c +8
-0+} + +void kunit_cleanup (struct kunit *test) +{ +struct kunit_resource *resource, *resource_safe; +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags);Ah ok,
test->lockis protecting everything now? Does it need to be a spinlock, or can it be a mutex?
No it needs to be a spin lock. There are some conceivable circumstances where the test object can be accessed by code in which it isn't safe to sleep.
Quoted 9 lines
kunit/test.c int kunit_run_tests(struct kunit_module *module) · line 308
Quoted 9 lines
kunit/test.c +9
-0+list_for_each_entry_safe (resource, + resource_safe, + &test->resources, + node) { +kunit_free_resource (test, resource); + } +spin_unlock_irqrestore (&test->lock, flags); +} +
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v4 3/18 kunit: test: add string_stream a std::stream like string builder
In reply to Brendan HigginsA number of test features need to do pretty complicated string printing where it may not be possible to rely on a single preallocated string with parameters.
So provide a library for constructing the string as you go similar to C++'s std::string.
--- include/kunit/string-stream.h | 51 ++++++++++++ kunit/Makefile | 3 +- kunit/string-stream.c | 143 ++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 include/kunit/string-stream.h create mode 100644 kunit/string-stream.c
include/kunit/string-stream.h new file +51 -0
@@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* C++ stream style string builder used in KUnit for building messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#ifndef _KUNIT_STRING_STREAM_H +#define _KUNIT_STRING_STREAM_H + +#include <linux/types.h> +#include <linux/spinlock.h> +#include <linux/kref.h> +#include <stdarg.h> + +struct string_stream_fragment { +struct list_head node; +char *fragment; +}; + +struct string_stream { +size_t length; +struct list_head fragments; + +/* length and fragments are protected by this lock */ +spinlock_t lock; +struct kref refcount; +}; + +struct string_stream *new_string_stream (void ); + +void destroy_string_stream (struct string_stream *stream); + +void string_stream_get (struct string_stream *stream); + +int string_stream_put (struct string_stream *stream); + +int string_stream_add (struct string_stream *this,const char *fmt, ...); + +int string_stream_vadd (struct string_stream *this, +const char *fmt, +va_list args); + +char *string_stream_get_string (struct string_stream *this); + +void string_stream_clear (struct string_stream *this); + +bool string_stream_is_empty (struct string_stream *this); + +#endif /* _KUNIT_STRING_STREAM_H */
kunit/Makefile +2 -1
@@ -1 +1,2 @@ -obj-$(CONFIG_KUNIT) += test.o +obj-$(CONFIG_KUNIT) += test.o \ + string-stream.o
kunit/string-stream.c new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH v4 03/18] kunit: test: add string_stream a std::stream like string builder
Quoting Brendan Higgins (2019-05-14 15:16:56)
Quoted 10 lines
A number of test features need to do pretty complicated string printing where it may not be possible to rely on a single preallocated string with parameters.
So provide a library for constructing the string as you go similar to C++'s
std::string.Signed-off-by Brendan Higgins <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Logan Gunthorpe <[email protected]>
Is there any reason why we can't use the seqfile API for this? These both share a similar goal, formatting strings into a buffer to be read later. Maybe some new APIs would be needed to extract the buffer differently, but I hope we could share the code.
If it can't be used, can you please add the reasoning to the commit text here?
Re: [PATCH v4 03/18] kunit: test: add string_stream a std::stream like string builder
Quoted 9 lines · Stephen Boyd
Quoting Brendan Higgins (2019-05-14 15:16:56)
Quoted 7 lines
A number of test features need to do pretty complicated string printing where it may not be possible to rely on a single preallocated string with parameters.
So provide a library for constructing the string as you go similar to C++'s
std::string.
Quoted 8 lines
Quoted 3 lines
Signed-off-by Brendan Higgins <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Logan Gunthorpe <[email protected]>Is there any reason why we can't use the seqfile API for this? These both share a similar goal, formatting strings into a buffer to be read later. Maybe some new APIs would be needed to extract the buffer differently, but I hope we could share the code.
I can see why you are asking. It seems as though they are trying to do *similar* things, and it seems possible that we might be able to extract some common functionality out of seq_file that could replace this; however, it looks like it would be require a significant refactoring of seq_file to separate out the file system specific bits from the more general stringbuilder functionality.
In my opinion, a refactoring like this makes no sense in this patchset; it probably belongs in its own patchset (preferably as a follow on). I also am not sure if the FS people would appreciate indirection that serves them no benefit, but I can ask if you like.
Quoted 2 lines
If it can't be used, can you please add the reasoning to the commit text here?
Will do.
Thanks!
PATCH v4 4/18 kunit: test: add kunit_stream a std::stream like logger
In reply to Brendan HigginsA lot of the expectation and assertion infrastructure prints out fairly complicated test failure messages, so add a C++ style log library for for logging test results.
--- include/kunit/kunit-stream.h | 85 ++++++++++++++++++++ include/kunit/test.h | 3 + kunit/Makefile | 3 +- kunit/kunit-stream.c | 152 +++++++++++++++++++++++++++++++++++ kunit/test.c | 7 ++ 5 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 include/kunit/kunit-stream.h create mode 100644 kunit/kunit-stream.c
include/kunit/kunit-stream.h new file +85 -0
@@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* C++ stream style string formatter and printer used in KUnit for outputting +* KUnit messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#ifndef _KUNIT_KUNIT_STREAM_H +#define _KUNIT_KUNIT_STREAM_H + +#include <linux/types.h> +#include <kunit/string-stream.h> + +struct kunit ; + +/** +* struct kunit_stream - a std::stream style string builder. +* +* A std::stream style string builder. Allows messages to be built up and +* printed all at once. +*/ +struct kunit_stream { +/* private: internal use only. */ +struct kunit *test; +spinlock_t lock;/* Guards level. */ +const char *level; +struct string_stream *internal_stream; +}; + +/** +* kunit_new_stream() - constructs a new &struct kunit_stream. +* @test: The test context object. +* +* Constructs a new test managed &struct kunit_stream. +*/ +struct kunit_stream *kunit_new_stream (struct kunit *test); + +/** +* kunit_stream_set_level(): sets the level that string should be printed at. +* @this: the stream being operated on. +* @level: the print level the stream is set to output to. +* +* Sets the print level at which the stream outputs. +*/ +void kunit_stream_set_level (struct kunit_stream *this,const char *level); + +/** +* kunit_stream_add(): adds the formatted input to the internal buffer. +* @this: the stream being operated on. +* @fmt: printf style format string to append to stream. +* +* Appends the formatted string, @fmt, to the internal buffer. +*/ +void __printf(2, 3) kunit_stream_add(struct kunit_stream *this, + const char *fmt, ...); + +/** + * kunit_stream_append(): appends the contents of @other to @this. + * @this: the stream to which @other is appended. + * @other: the stream whose contents are appended to @this. + * + * Appends the contents of @other to @this. + */ +void kunit_stream_append(struct kunit_stream *this, struct kunit_stream *other); + +/** + * kunit_stream_commit(): prints out the internal buffer to the user. + * @this: the stream being operated on. + * + * Outputs the contents of the internal buffer as a kunit_printk formatted + * output. + */ +void kunit_stream_commit(struct kunit_stream *this); + +/** + * kunit_stream_clear(): clears the internal buffer. + * @this: the stream being operated on. + * + * Clears the contents of the internal buffer. + */ +void kunit_stream_clear(struct kunit_stream *this); + +#endif /* _KUNIT_KUNIT_STREAM_H */
include/kunit/test.h +3 -0
@@ -11,6 +11,7 @@#include <linux/types.h> #include <linux/slab.h> +#include <kunit/kunit-stream.h> struct kunit_resource ;
@@ -172,6 +173,8 @@ struct kunit {void kunit_init_test (struct kunit *test,const char *name); +void kunit_fail (struct kunit *test,struct kunit_stream *stream); +int kunit_run_tests (struct kunit_module *module);/**
kunit/Makefile +2 -1
@@ -1,2 +1,3 @@ obj-$(CONFIG_KUNIT) += test.o \ - string-stream.o + string-stream.o \ + kunit-stream.o
kunit/kunit-stream.c new file +152 -0
152 lines not on this page · raw message
kunit/test.c +7 -0
@@ -138,6 +138,13 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case, test_case->name); } +void kunit_fail (struct kunit *test,struct kunit_stream *stream) +{ +kunit_set_success (test, false); +kunit_stream_set_level (stream,KERN_ERR ); +kunit_stream_commit (stream); +} +void kunit_init_test (struct kunit *test,const char *name) {spin_lock_init (&test->lock);
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v4 04/18] kunit: test: add kunit_stream a std::stream like logger
Quoting Brendan Higgins (2019-05-14 15:16:57)
Quoted 19 lines
kunit/kunit-stream.c new file +13
-0@@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* C++ stream style string formatter and printer used in KUnit for outputting +* KUnit messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#include <kunit/test.h> +#include <kunit/kunit-stream.h> +#include <kunit/string-stream.h> +
kunit/kunit-stream.c line 23
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+static const char *kunit_stream_get_level (struct kunit_stream *this) +{ +unsigned long flags; +const char *level; + +spin_lock_irqsave (&this->lock, flags); + level = this->level; +spin_unlock_irqrestore (&this->lock, flags); + +return level;
Please remove this whole function and inline it to the one call-site.
kunit/kunit-stream.c line 32
Quoted 9 lines
kunit/kunit-stream.c new file +9
-0+} + +void kunit_stream_set_level (struct kunit_stream *this,const char *level) +{ +unsigned long flags; + +spin_lock_irqsave (&this->lock, flags); + this->level = level; +spin_unlock_irqrestore (&this->lock, flags);
I don't get the locking here. What are we protecting against? Are tests running in parallel using the same kunit_stream? If so, why is the level changeable in one call and then adding strings is done in a different function call? It would make sense to combine the level setting and string adding so that it's one atomic operation if it's truly a parallel operation, or remove the locking entirely.
Quoted 28 lines
kunit/kunit-stream.c new file +28
-0+} + +void kunit_stream_add (struct kunit_stream *this,const char *fmt, ...) +{ +va_list args; +struct string_stream *stream = this->internal_stream; + +va_start (args, fmt); + +if (string_stream_vadd (stream, fmt, args) <0 ) +kunit_err (this->test,"Failed to allocate fragment: %s\n" , fmt); + +va_end (args); +} + +void kunit_stream_append (struct kunit_stream *this, +struct kunit_stream *other) +{ +struct string_stream *other_stream = other->internal_stream; +const char *other_content; + + other_content =string_stream_get_string (other_stream); + +if (!other_content) { +kunit_err (this->test, +"Failed to get string from second argument for appending.\n" ); +return ; + }
kunit/kunit-stream.c line 70
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+ +kunit_stream_add (this, other_content); +} + +void kunit_stream_clear (struct kunit_stream *this) +{ +string_stream_clear (this->internal_stream); +} + +void kunit_stream_commit (struct kunit_stream *this)
Should this be rather called kunit_stream_flush()?
kunit/kunit-stream.c line 80
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+{ +struct string_stream *stream = this->internal_stream; +struct string_stream_fragment *fragment; +const char *level; +char *buf; + + level =kunit_stream_get_level (this); +if (!level) { +kunit_err (this->test, +"Stream was committed without a specified log level.\n" );
Drop the full-stop?
kunit/kunit-stream.c line 87
Quoted 7 lines
kunit/kunit-stream.c new file +7
-0+ level =KERN_ERR ; +kunit_stream_set_level (this, level); + } + + buf =string_stream_get_string (stream); +if (!buf) { + kunit_err(this->test,
Can you grow a local variable for 'this->test'? It's used many times.
Also, 'this' is not very kernel idiomatic. We usually name variables by their type instead of 'this' which is a keyword in other languages. Perhaps it could be named 'kstream'?
Quoted 11 lines
kunit/kunit-stream.c new file +11
-0+ "Could not allocate buffer, dumping stream:\n"); +list_for_each_entry (fragment, &stream->fragments, node) { +kunit_err (this->test, fragment->fragment); + } +kunit_err (this->test,"\n" ); + goto cleanup; + } + +kunit_printk (level, this->test, buf); +kfree (buf); +
kunit/kunit-stream.c line 108
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+cleanup: +kunit_stream_clear (this); +} + +static int kunit_stream_init (struct kunit_resource *res,void *context) +{ +struct kunit *test = context; +struct kunit_stream *stream; + + stream =kzalloc (sizeof (*stream),GFP_KERNEL );
Of course, here it's called 'stream', so maybe it should be 'kstream' here too.
kunit/kunit-stream.c line 115
Quoted 7 lines
kunit/kunit-stream.c new file +7
-0+if (!stream) +return -ENOMEM ; + + res->allocation = stream; + stream->test = test; +spin_lock_init (&stream->lock); + stream->internal_stream =new_string_stream ();
Can new_string_stream() be renamed to alloc_string_stream()? Sorry, I just see so much C++ isms in here it's hard to read from the kernel developer perspective.
kunit/kunit-stream.c line 117
Quoted 2 lines
kunit/kunit-stream.c new file +2
-0+ +if (!stream->internal_stream) {
Nitpick: Please join this to the "allocation" event above instead of keeping it separated.
Quoted 24 lines
kunit/kunit-stream.c new file +24
-0+kfree (stream); +return -ENOMEM ; + } + +return 0 ; +} + +static void kunit_stream_free (struct kunit_resource *res) +{ +struct kunit_stream *stream = res->allocation; + +if (!string_stream_is_empty (stream->internal_stream)) { +kunit_err (stream->test, +"End of test case reached with uncommitted stream entries.\n" ); +kunit_stream_commit (stream); + } + +destroy_string_stream (stream->internal_stream); +kfree (stream); +} + +struct kunit_stream *kunit_new_stream (struct kunit *test) +{ +struct kunit_resource *res;
kunit/kunit-stream.c line 151
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+ + res =kunit_alloc_resource (test, + kunit_stream_init, + kunit_stream_free, + test); + +if (res) +return res->allocation; +else +return NULL ;
Don't have if (...) return ...; else return ..., just return instead of else.
Re: [PATCH v4 04/18] kunit: test: add kunit_stream a std::stream like logger
Quoted 34 lines · Stephen Boyd
Quoting Brendan Higgins (2019-05-14 15:16:57)
Quoted 19 lines
kunit/kunit-stream.c new file +13
-0@@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* C++ stream style string formatter and printer used in KUnit for outputting +* KUnit messages. +* +* Copyright (C) 2019, Google LLC. +* Author: Brendan Higgins <[email protected]> +*/ + +#include <kunit/test.h> +#include <kunit/kunit-stream.h> +#include <kunit/string-stream.h> +kunit/kunit-stream.c line 23
Quoted 10 lines
kunit/kunit-stream.c new file +10
-0+static const char *kunit_stream_get_level (struct kunit_stream *this) +{ +unsigned long flags; +const char *level; + +spin_lock_irqsave (&this->lock, flags); + level = this->level; +spin_unlock_irqrestore (&this->lock, flags); + +return level;Please remove this whole function and inline it to the one call-site.
Quoted 16 lines
kunit/kunit-stream.c line 32
Quoted 9 lines
kunit/kunit-stream.c new file +9
-0+} + +void kunit_stream_set_level (struct kunit_stream *this,const char *level) +{ +unsigned long flags; + +spin_lock_irqsave (&this->lock, flags); + this->level = level; +spin_unlock_irqrestore (&this->lock, flags);I don't get the locking here. What are we protecting against? Are tests running in parallel using the same
kunit_stream? If so, why is the level changeable in one call and then adding strings is done in a different function call? It would make sense to combine the level setting and string adding so that it's one atomic operation if it's truly a parallel operation, or remove the locking entirely.
I think you are right. I am not sure it makes sense for two separate threads to share a kunit_stream; even if locked properly, it would end up printing out corrupted text.
In anycase, I think it makes sense to decide the level when the stream is allocated which would sidestep this issue entirely.
Quoted 32 lines
Quoted 32 lines
kunit/kunit-stream.c new file +32
-0+} + +void kunit_stream_add (struct kunit_stream *this,const char *fmt, ...) +{ +va_list args; +struct string_stream *stream = this->internal_stream; + +va_start (args, fmt); + +if (string_stream_vadd (stream, fmt, args) <0 ) +kunit_err (this->test,"Failed to allocate fragment: %s\n" , fmt); + +va_end (args); +} + +void kunit_stream_append (struct kunit_stream *this, +struct kunit_stream *other) +{ +struct string_stream *other_stream = other->internal_stream; +const char *other_content; + + other_content =string_stream_get_string (other_stream); + +if (!other_content) { +kunit_err (this->test, +"Failed to get string from second argument for appending.\n" ); +return ; + } + +kunit_stream_add (this, other_content); +} +
Quoted 8 lines
kunit/kunit-stream.c line 70
Quoted 6 lines
kunit/kunit-stream.c new file +6
-0+void kunit_stream_clear (struct kunit_stream *this) +{ +string_stream_clear (this->internal_stream); +} + +void kunit_stream_commit (struct kunit_stream *this)Should this be rather called
kunit_stream_flush()?
So the intention is that the string in the buffer will not get printed out until commit is called. In this way, you can build up a message and then decide not to print it. This is useful when you are parsing through a lot of data that would be useful in debugging a failing or broken test, but are not yet sure if it is going to pass or not.
I think flush has the connotation, that you are just forcing the buffer to get written out now, but that it will happen regardless eventually, where commit has the correct connotation that you *must* call it in order to write out the data stored in the buffer.
Seems as though I should probably add this distinction to the kernel-doc comment.
Quoted 4 lines
Quoted 4 lines
kunit/kunit-stream.c new file +4
-0+{ +struct string_stream *stream = this->internal_stream; +struct string_stream_fragment *fragment; +const char *level;
Quoted 8 lines
kunit/kunit-stream.c line 80
Quoted 6 lines
kunit/kunit-stream.c new file +6
-0+char *buf; + + level =kunit_stream_get_level (this); +if (!level) { +kunit_err (this->test, +"Stream was committed without a specified log level.\n" );Drop the full-stop?
Whoops, nice catch. Will fix in next revision.
Quoted 9 lines
kunit/kunit-stream.c line 87
Quoted 7 lines
kunit/kunit-stream.c new file +7
-0+ level =KERN_ERR ; +kunit_stream_set_level (this, level); + } + + buf =string_stream_get_string (stream); +if (!buf) { + kunit_err(this->test,Can you grow a local variable for '
this->test'? It's used many times.
Sure, will fix in next revision.
Quoted 3 lines
Also, 'this' is not very kernel idiomatic. We usually name variables by their type instead of 'this' which is a keyword in other languages. Perhaps it could be named 'kstream'?
Seems reasonable. Will fix in next revision.
Quoted 16 lines
Quoted 16 lines
kunit/kunit-stream.c new file +16
-0+ "Could not allocate buffer, dumping stream:\n"); +list_for_each_entry (fragment, &stream->fragments, node) { +kunit_err (this->test, fragment->fragment); + } +kunit_err (this->test,"\n" ); + goto cleanup; + } + +kunit_printk (level, this->test, buf); +kfree (buf); + +cleanup: +kunit_stream_clear (this); +} + +static int kunit_stream_init (struct kunit_resource *res,void *context)
Quoted 8 lines
kunit/kunit-stream.c line 108
Quoted 5 lines
kunit/kunit-stream.c new file +5
-0+{ +struct kunit *test = context; +struct kunit_stream *stream; + + stream =kzalloc (sizeof (*stream),GFP_KERNEL );Of course, here it's called 'stream', so maybe it should be 'kstream' here too.
Will do.
Quoted 9 lines
kunit/kunit-stream.c line 115
Quoted 8 lines
kunit/kunit-stream.c new file +7
-0+if (!stream) +return -ENOMEM ; + + res->allocation = stream; + stream->test = test; +spin_lock_init (&stream->lock); + stream->internal_stream =new_string_stream ();
Quoted 3 lines
Can
new_string_stream()be renamed toalloc_string_stream()? Sorry, I just see so much C++ isms in here it's hard to read from the kernel developer perspective.
No problem. WIll fix in next revision.
Quoted 5 lines
kunit/kunit-stream.c line 117
Quoted 2 lines
kunit/kunit-stream.c new file +2
-0+ +if (!stream->internal_stream) {Nitpick: Please join this to the "allocation" event above instead of keeping it separated.
Yeah, that's a lot cleaner. Will do.
Quoted 29 lines
Quoted 29 lines
kunit/kunit-stream.c new file +29
-0+kfree (stream); +return -ENOMEM ; + } + +return 0 ; +} + +static void kunit_stream_free (struct kunit_resource *res) +{ +struct kunit_stream *stream = res->allocation; + +if (!string_stream_is_empty (stream->internal_stream)) { +kunit_err (stream->test, +"End of test case reached with uncommitted stream entries.\n" ); +kunit_stream_commit (stream); + } + +destroy_string_stream (stream->internal_stream); +kfree (stream); +} + +struct kunit_stream *kunit_new_stream (struct kunit *test) +{ +struct kunit_resource *res; + + res =kunit_alloc_resource (test, + kunit_stream_init, + kunit_stream_free, + test);
Quoted 8 lines
kunit/kunit-stream.c line 151
Quoted 5 lines
kunit/kunit-stream.c new file +5
-0+ +if (res) +return res->allocation; +else +return NULL ;Don't have if (...) return ...; else return ..., just return instead of else.
Sorry. Will fix.
Thanks!
PATCH v4 5/18 kunit: test: add the concept of expectations
In reply to Brendan HigginsAdd support for expectations, which allow properties to be specified and then verified in tests.
--- include/kunit/test.h | 518 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 66 ++++++ 2 files changed, 584 insertions(+)
include/kunit/test.h +518 -0
@@ -271,4 +271,522 @@ void __printf(3, 4) kunit_printk(const char *level,#define kunit_err (test, fmt, ...) \ kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) +static inline struct kunit_stream *kunit_expect_start (struct kunit *test, +const char *file, +const char *line) +{ +struct kunit_stream *stream =kunit_new_stream (test); + +kunit_stream_add (stream,"EXPECTATION FAILED at %s:%s\n\t" , file, line); + +return stream; +} + +static inline void kunit_expect_end (struct kunit *test, +bool success, +struct kunit_stream *stream) +{ +if (!success) +kunit_fail (test, stream); +else +kunit_stream_clear (stream); +} + +#define KUNIT_EXPECT_START (test) \ + kunit_expect_start(test, __FILE__, __stringify(__LINE__)) + +#define KUNIT_EXPECT_END (test, success, stream) \ + kunit_expect_end(test, success, stream) + +#define KUNIT_EXPECT_MSG (test, success, message, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + \ + kunit_stream_add(__stream, message); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_EXPECT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_EXPECT (test, success, message) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + \ + kunit_stream_add(__stream, message); \ + KUNIT_EXPECT_END(test, success, __stream); \ +} while (0) + +/** +* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. +* @test: The test context object. +* +* The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other +* words, it does nothing and only exists for code clarity. See +* KUNIT_EXPECT_TRUE() for more information. +*/ +#define KUNIT_SUCCEED (test) do {} while (0) + +/** +* KUNIT_FAIL() - Always causes a test to fail when evaluated. +* @test: The test context object. +* @fmt: an informational message to be printed when the assertion is made. +* @...: string format arguments. +* +* The opposite of KUNIT_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 KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_FAIL (test, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_EXPECT_END(test, false, __stream); \ +} while (0) + +/** +* KUNIT_EXPECT_TRUE() - Causes a test failure when the 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 `KUNIT_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 KUNIT_EXPECT_TRUE (test, condition) \ + KUNIT_EXPECT(test, (condition), \ + "Expected " #condition " is true, but is false.\n") + +#define KUNIT_EXPECT_TRUE_MSG (test, condition, fmt, ...) \ + KUNIT_EXPECT_MSG(test, (condition), \ + "Expected " #condition " is true, but is false.\n",\ + fmt, ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_FALSE() - Makes 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 +* KUNIT_EXPECT_TRUE() for more information. +*/ +#define KUNIT_EXPECT_FALSE (test, condition) \ + KUNIT_EXPECT(test, !(condition), \ + "Expected " #condition " is false, but is true.") + +#define KUNIT_EXPECT_FALSE_MSG (test, condition, fmt, ...) \ + KUNIT_EXPECT_MSG(test, !(condition), \ + "Expected " #condition " is false, but is true.\n",\ + fmt, ##__VA_ARGS__) + +void kunit_expect_binary_msg (struct kunit *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, +const char *fmt, ...); + +static inline void kunit_expect_binary (struct kunit *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) +{ +kunit_expect_binary_msg (test, + left, left_name, + right, right_name, + compare_result, + compare_name, + file, + line, +NULL ); +} + +void kunit_expect_ptr_binary_msg (struct kunit *test, +void *left,const char *left_name, +void *right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...); + +static inline void kunit_expect_ptr_binary (struct kunit *test, +void *left,const char *left_name, +void *right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line) +{ +kunit_expect_ptr_binary_msg (test, + left, left_name, + right, right_name, + compare_result, + compare_name, + file, + line, +NULL ); +} + +/* +* 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 KUNIT_EXPECT_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_binary(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_EXPECT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_binary_msg(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/* +* Just like KUNIT_EXPECT_BINARY, but for comparing pointer types. +*/ +#define KUNIT_EXPECT_PTR_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_ptr_binary(test, \ + (void *) __left, #left, \ + (void *) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_EXPECT_PTR_BINARY_MSG (test, \ + left, \ + condition, \ + right, \ + fmt, \ + ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_expect_ptr_binary_msg(test, \ + (void *) __left, #left, \ + (void *) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/** +* KUNIT_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 +* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_EQ (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, ==, right) + +#define KUNIT_EXPECT_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a pointer. +* @right: an arbitrary expression that evaluates to a pointer. +* +* Sets an expectation that the values that @left and @right evaluate to are +* equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_PTR_EQ (test, left, right) \ + KUNIT_EXPECT_PTR_BINARY(test, left, ==, right) + +#define KUNIT_EXPECT_PTR_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_PTR_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_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 +* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_NE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, !=, right) + +#define KUNIT_EXPECT_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a pointer. +* @right: an arbitrary expression that evaluates to a pointer. +* +* Sets an expectation that the values that @left and @right evaluate to are not +* equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_PTR_NE (test, left, right) \ + KUNIT_EXPECT_PTR_BINARY(test, left, !=, right) + +#define KUNIT_EXPECT_PTR_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_PTR_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_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 +* KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_LT (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, <, right) + +#define KUNIT_EXPECT_LT_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + <, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_LE() - Expects 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 KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_LE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, <=, right) + +#define KUNIT_EXPECT_LE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + <=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_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 +* KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_GT (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, >, right) + +#define KUNIT_EXPECT_GT_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + >, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_GE() - Expects 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 +* KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_GE (test, left, right) \ + KUNIT_EXPECT_BINARY(test, left, >=, right) + +#define KUNIT_EXPECT_GE_MSG (test, left, right, fmt, ...) \ + KUNIT_EXPECT_BINARY_MSG(test, \ + left, \ + >=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_EXPECT_STREQ() - Expects 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 +* KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_EXPECT_STREQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Expected " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_EXPECT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_EXPECT_STREQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Expected " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_EXPECT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not 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 +* not equal. This is semantically equivalent to +* KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() +* for more information. +*/ +#define KUNIT_EXPECT_STRNEQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Expected " #left " != " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_EXPECT_END(test, strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_EXPECT_STRNEQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Expected " #left " != " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_EXPECT_END(test, strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects 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 +* KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for +* more information. +*/ +#define KUNIT_EXPECT_NOT_ERR_OR_NULL (test, ptr) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) \ + kunit_stream_add(__stream, \ + "Expected " #ptr " is not null, but is."); \ + if (IS_ERR(__ptr)) \ + kunit_stream_add(__stream, \ + "Expected " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + KUNIT_EXPECT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) + +#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG (test, ptr, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_EXPECT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) { \ + kunit_stream_add(__stream, \ + "Expected " #ptr " is not null, but is."); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + } \ + if (IS_ERR(__ptr)) { \ + kunit_stream_add(__stream, \ + "Expected " #ptr " is not error, but is: %ld", \ + PTR_ERR(__ptr)); \ + \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + } \ + KUNIT_EXPECT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) +#endif /* _KUNIT_TEST_H */
kunit/test.c +66 -0
69 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 6/18 kbuild: enable building KUnit
In reply to Brendan HigginsKUnit is a new unit testing framework for the kernel and when used is built into the kernel as a part of it. Add KUnit to the root Kconfig and Makefile to allow it to be actually built.
--- Kconfig | 2 ++ Makefile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
Kconfig +2 -0
@@ -30,3 +30,5 @@ source "crypto/Kconfig" source "lib/Kconfig" source "lib/Kconfig.debug" + +source "kunit/Kconfig"
Makefile +1 -1
@@ -969,7 +969,7 @@ endif PHONY += prepare0 ifeq ($(KBUILD_EXTMOD),) -core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ +core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ kunit/ vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \ $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 7/18 kunit: test: add initial tests
In reply to Brendan HigginsAdd a test for string stream along with a simpler example.
--- kunit/Kconfig | 25 ++++++++++- kunit/Makefile | 4 ++ kunit/example-test.c | 88 ++++++++++++++++++++++++++++++++++++++ kunit/string-stream-test.c | 60 ++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 kunit/example-test.c create mode 100644 kunit/string-stream-test.c
kunit/Kconfig +23 -2
@@ -10,7 +10,28 @@ config KUNIT Enables support for kernel unit tests (KUnit), a lightweight unit testing and mocking framework for the Linux kernel. These tests are able to be run locally on a developer's workstation without a VM or - special hardware. For more information, please see - Documentation/kunit/ + special hardware when using UML. Can also be used on most other + architectures. For more information, please see Documentation/kunit/. + +config KUNIT_TEST + bool "KUnit test for KUnit" + depends on KUNIT + help + Enables the unit tests for the KUnit test framework. These tests test + the KUnit test framework itself; the tests are both written using + KUnit and test KUnit. This option should only be enabled for testing + purposes by developers interested in testing that KUnit works as + expected. + +config KUNIT_EXAMPLE_TEST + bool "Example test for KUnit" + depends on KUNIT + help + Enables an example unit test that illustrates some of the basic + features of KUnit. This test only exists to help new users understand + what KUnit is and how it is used. Please refer to the example test + itself, kunit/example-test.c, for more information. This option is + intended for curious hackers who would like to understand how to use + KUnit for kernel development. endmenu
kunit/Makefile +4 -0
@@ -1,3 +1,7 @@ obj-$(CONFIG_KUNIT) += test.o \ string-stream.o \ kunit-stream.o + +obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o + +obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o
kunit/example-test.c new file +88 -0
88 lines not on this page · raw message
kunit/string-stream-test.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 8/18 objtool: add kunit_try_catch_throw to the noreturn list
In reply to Brendan HigginsFix the following warning seen on GCC 7.3:
kunit/test-test.o: warning: objtool: kunit_test_unsuccessful_try() falls through to next function kunit_test_catch()
kunit_try_catch_throw is a function added in the following patch in this series; it allows KUnit, a unit testing framework for the kernel, to bail out of a broken test. As a consequence, it is a new __noreturn function that objtool thinks is broken (as seen above). So fix this warning by adding kunit_try_catch_throw to objtool's noreturn list.
--- tools/objtool/check.c | 1 + 1 file changed, 1 insertion(+)
tools/objtool/check.c +1 -0
@@ -166,6 +166,7 @@ static int __dead_end_function(struct objtool_file *file, struct symbol *func,"usercopy_abort" ,"machine_real_restart" ,"rewind_stack_do_exit" , + "kunit_try_catch_throw", };if (func->bind ==STB_WEAK )
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 9/18 kunit: test: add support for test abort
In reply to Brendan HigginsAdd support for aborting/bailing out of test cases, which is needed for implementing assertions.
An assertion is like an expectation, but bails out of the test case early if the assertion is not met. The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove.
--- include/kunit/test.h | 14 ++++ include/kunit/try-catch.h | 69 +++++++++++++++++++ kunit/Makefile | 3 +- kunit/test.c | 136 ++++++++++++++++++++++++++++++++++++-- kunit/try-catch.c | 95 ++++++++++++++++++++++++++ 5 files changed, 309 insertions(+), 8 deletions(-) create mode 100644 include/kunit/try-catch.h create mode 100644 kunit/try-catch.c
include/kunit/test.h +14 -0
@@ -12,6 +12,7 @@#include <linux/types.h> #include <linux/slab.h> #include <kunit/kunit-stream.h> +#include <kunit/try-catch.h> struct kunit_resource ;
@@ -166,15 +167,28 @@ struct kunit {/* private: internal use only. */ const char *name;/* Read only after initialization! */ +struct kunit_try_catch try_catch;spinlock_t lock;/* Gaurds all mutable test state. */ bool success;/* Protected by lock. */ +bool death_test;/* Protected by lock. */ struct list_head resources;/* Protected by lock. */ }; +static inline void kunit_set_death_test (struct kunit *test,bool death_test) +{ +unsigned long flags; + +spin_lock_irqsave (&test->lock, flags); + test->death_test = death_test; +spin_unlock_irqrestore (&test->lock, flags); +} +void kunit_init_test (struct kunit *test,const char *name);void kunit_fail (struct kunit *test,struct kunit_stream *stream); +void kunit_abort (struct kunit *test); +int kunit_run_tests (struct kunit_module *module);/**
include/kunit/try-catch.h new file +69 -0
69 lines not on this page · raw message
kunit/Makefile +2 -1
@@ -1,6 +1,7 @@ obj-$(CONFIG_KUNIT) += test.o \ string-stream.o \ - kunit-stream.o + kunit-stream.o \ + try-catch.o obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o
kunit/test.c +129 -7
175 lines not on this page · raw message
kunit/try-catch.c new file +95 -0
95 lines not on this page · raw message
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v4 10/18 kunit: test: add tests for kunit test abort
In reply to Brendan HigginsAdd KUnit tests for the KUnit test abort mechanism (see preceding commit). Add tests both for general try catch mechanism as well as non-architecture specific mechanism.
--- kunit/Makefile | 3 +- kunit/test-test.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 kunit/test-test.c
kunit/Makefile +2 -1
@@ -3,6 +3,7 @@ obj-$(CONFIG_KUNIT) += test.o \ kunit-stream.o \ try-catch.o -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o +obj-$(CONFIG_KUNIT_TEST) += test-test.o \ + string-stream-test.o obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o
kunit/test-test.c new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 11/18 kunit: test: add the concept of assertions
In reply to Brendan HigginsAdd support for assertions which are like expectations except the test terminates if the assertion is not satisfied.
The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove. It is not used universally in x-unit style test frameworks, but I really like it as a convention. You could still express the idea of a premise using the above idiom, but I think KUNIT_ASSERT_* states the intended idea perfectly.
--- include/kunit/test.h | 498 ++++++++++++++++++++++++++++++++++++- kunit/string-stream-test.c | 12 +- kunit/test-test.c | 2 + kunit/test.c | 66 +++++ 4 files changed, 569 insertions(+), 9 deletions(-)
include/kunit/test.h +495 -3
@@ -85,9 +85,10 @@ struct kunit;* @name: the name of the test case. * * A test case is a function with the signature, ``void (*)(struct kunit *)`` -* that makes expectations (see KUNIT_EXPECT_TRUE()) about code under test. Each -* test case is associated with a &struct kunit_module and will be run after the -* module's init function and followed by the module's exit function. +* that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and +* KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated with +* a &struct kunit_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 KUNIT_CASE() * macro; additionally, every array of test cases should be terminated with an
@@ -803,4 +804,495 @@ static inline void kunit_expect_ptr_binary(struct kunit *test,KUNIT_EXPECT_END (test, !IS_ERR_OR_NULL (__ptr), __stream); \ } while (0) +static inline struct kunit_stream *kunit_assert_start(struct kunit *test, +const char *file, +const char *line) +{ +struct kunit_stream *stream =kunit_new_stream (test); + +kunit_stream_add (stream,"ASSERTION FAILED at %s:%s\n\t" , file, line); + +return stream; +} + +static inline void kunit_assert_end (struct kunit *test, +bool success, +struct kunit_stream *stream) +{ +if (!success) { +kunit_fail (test, stream); +kunit_abort (test); + }else { +kunit_stream_clear (stream); + } +} + +#define KUNIT_ASSERT_START (test) \ + kunit_assert_start(test, __FILE__, __stringify(__LINE__)) + +#define KUNIT_ASSERT_END (test, success, stream) \ + kunit_assert_end(test, success, stream) + +#define KUNIT_ASSERT (test, success, message) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + \ + kunit_stream_add(__stream, message); \ + KUNIT_ASSERT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_ASSERT_MSG (test, success, message, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + \ + kunit_stream_add(__stream, message); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_ASSERT_END(test, success, __stream); \ +} while (0) + +#define KUNIT_ASSERT_FAILURE (test, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + KUNIT_ASSERT_END(test, false, __stream); \ +} while (0) + +/** +* KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. +* @test: The test context object. +* @condition: an arbitrary boolean expression. The test fails and aborts when +* this does not evaluate to true. +* +* This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to +* fail *and immediately abort* when the specified condition is not met. Unlike +* an expectation failure, it will prevent the test case from continuing to run; +* this is otherwise known as an *assertion failure*. +*/ +#define KUNIT_ASSERT_TRUE (test, condition) \ + KUNIT_ASSERT(test, (condition), \ + "Asserted " #condition " is true, but is false.\n") + +#define KUNIT_ASSERT_TRUE_MSG (test, condition, fmt, ...) \ + KUNIT_ASSERT_MSG(test, (condition), \ + "Asserted " #condition " is true, but is false.\n",\ + fmt, ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. +* @test: The test context object. +* @condition: an arbitrary boolean expression. +* +* Sets an assertion that the value that @condition evaluates to is false. This +* is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure +* (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_FALSE (test, condition) \ + KUNIT_ASSERT(test, !(condition), \ + "Asserted " #condition " is false, but is true.\n") + +#define KUNIT_ASSERT_FALSE_MSG (test, condition, fmt, ...) \ + KUNIT_ASSERT_MSG(test, !(condition), \ + "Asserted " #condition " is false, but is true.\n",\ + fmt, ##__VA_ARGS__) + +void kunit_assert_binary_msg (struct kunit *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, +const char *fmt, ...); + +static inline void kunit_assert_binary (struct kunit *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) +{ +kunit_assert_binary_msg (test, + left, left_name, + right, right_name, + compare_result, + compare_name, + file, + line, +NULL ); +} + +void kunit_assert_ptr_binary_msg (struct kunit *test, +void *left,const char *left_name, +void *right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line, +const char *fmt, ...); + +static inline void kunit_assert_ptr_binary (struct kunit *test, +void *left,const char *left_name, +void *right,const char *right_name, +bool compare_result, +const char *compare_name, +const char *file, +const char *line) +{ +kunit_assert_ptr_binary_msg (test, + left, left_name, + right, right_name, + compare_result, + compare_name, + file, + line, +NULL ); +} + +/* +* 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 KUNIT_ASSERT_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_binary(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_ASSERT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_binary_msg(test, \ + (long long) __left, #left, \ + (long long) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/* +* Just like KUNIT_EXPECT_BINARY, but for comparing pointer types. +*/ +#define KUNIT_ASSERT_PTR_BINARY (test, left, condition, right) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_ptr_binary(test, \ + (void *) __left, #left, \ + (void *) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__)); \ +} while (0) + +#define KUNIT_ASSERT_PTR_BINARY_MSG (test, \ + left, \ + condition, \ + right, \ + fmt, ...) do { \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + kunit_assert_ptr_binary_msg(test, \ + (void *) __left, #left, \ + (void *) __right, #right, \ + __left condition __right, #condition, \ + __FILE__, __stringify(__LINE__), \ + fmt, ##__VA_ARGS__); \ +} while (0) + +/** +* KUNIT_ASSERT_EQ() - Sets an assertion 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 assertion that the values that @left and @right evaluate to are +* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_EQ (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, ==, right) + +#define KUNIT_ASSERT_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a pointer. +* @right: an arbitrary expression that evaluates to a pointer. +* +* Sets an assertion that the values that @left and @right evaluate to are +* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_PTR_EQ (test, left, right) \ + KUNIT_ASSERT_PTR_BINARY(test, left, ==, right) + +#define KUNIT_ASSERT_PTR_EQ_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_PTR_BINARY_MSG(test, \ + left, \ + ==, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_NE() - An assertion 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 assertion that the values that @left and @right evaluate to are not +* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_NE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, !=, right) + +#define KUNIT_ASSERT_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. +* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. +* @test: The test context object. +* @left: an arbitrary expression that evaluates to a pointer. +* @right: an arbitrary expression that evaluates to a pointer. +* +* Sets an assertion that the values that @left and @right evaluate to are not +* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion +* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_PTR_NE (test, left, right) \ + KUNIT_ASSERT_PTR_BINARY(test, left, !=, right) + +#define KUNIT_ASSERT_PTR_NE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_PTR_BINARY_MSG(test, \ + left, \ + !=, \ + right, \ + fmt, \ + ##__VA_ARGS__) +/** +* KUNIT_ASSERT_LT() - An assertion 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 assertion that the value that @left evaluates to is less than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_LT (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, <, right) + +#define KUNIT_ASSERT_LT_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + <, \ + right, \ + fmt, \ + ##__VA_ARGS__) +/** +* KUNIT_ASSERT_LE() - An assertion 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 assertion that the value that @left evaluates to is less than or +* equal to the value that @right evaluates to. This is the same as +* KUNIT_EXPECT_LE(), except it causes an assertion failure (see +* KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_LE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, <=, right) + +#define KUNIT_ASSERT_LE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + <=, \ + right, \ + fmt, \ + ##__VA_ARGS__) +/** +* KUNIT_ASSERT_GT() - An assertion 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 assertion that the value that @left evaluates to is greater than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_GT (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, >, right) + +#define KUNIT_ASSERT_GT_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + >, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_GE() - Assertion 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 assertion that the value that @left evaluates to is greater than the +* value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except +* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion +* is not met. +*/ +#define KUNIT_ASSERT_GE (test, left, right) \ + KUNIT_ASSERT_BINARY(test, left, >=, right) + +#define KUNIT_ASSERT_GE_MSG (test, left, right, fmt, ...) \ + KUNIT_ASSERT_BINARY_MSG(test, \ + left, \ + >=, \ + right, \ + fmt, \ + ##__VA_ARGS__) + +/** +* KUNIT_ASSERT_STREQ() - An assertion 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 assertion that the values that @left and @right evaluate to are +* equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an +* assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_STREQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Asserted " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_ASSERT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_ASSERT_STREQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Asserted " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_ASSERT_END(test, !strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not 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 +* not equal. This is semantically equivalent to +* KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() +* for more information. +*/ +#define KUNIT_ASSERT_STRNEQ (test, left, right) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Asserted " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + \ + KUNIT_ASSERT_END(test, strcmp(left, right), __stream); \ +} while (0) + +#define KUNIT_ASSERT_STRNEQ_MSG (test, left, right, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(left) __left = (left); \ + typeof(right) __right = (right); \ + \ + kunit_stream_add(__stream, "Asserted " #left " == " #right ", but\n"); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #left, __left); \ + kunit_stream_add(__stream, "\t\t%s == %s\n", #right, __right); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + \ + KUNIT_ASSERT_END(test, strcmp(left, right), __stream); \ +} while (0) + +/** +* KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. +* @test: The test context object. +* @ptr: an arbitrary pointer. +* +* Sets an assertion that the value that @ptr evaluates to is not null and not +* an errno stored in a pointer. This is the same as +* KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see +* KUNIT_ASSERT_TRUE()) when the assertion is not met. +*/ +#define KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) \ + kunit_stream_add(__stream, \ + "Asserted " #ptr " is not null, but is.\n"); \ + if (IS_ERR(__ptr)) \ + kunit_stream_add(__stream, \ + "Asserted " #ptr " is not error, but is: %ld\n",\ + PTR_ERR(__ptr)); \ + \ + KUNIT_ASSERT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) + +#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG (test, ptr, fmt, ...) do { \ + struct kunit_stream *__stream = KUNIT_ASSERT_START(test); \ + typeof(ptr) __ptr = (ptr); \ + \ + if (!__ptr) { \ + kunit_stream_add(__stream, \ + "Asserted " #ptr " is not null, but is.\n"); \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + } \ + if (IS_ERR(__ptr)) { \ + kunit_stream_add(__stream, \ + "Asserted " #ptr " is not error, but is: %ld\n",\ + PTR_ERR(__ptr)); \ + \ + kunit_stream_add(__stream, fmt, ##__VA_ARGS__); \ + } \ + KUNIT_ASSERT_END(test, !IS_ERR_OR_NULL(__ptr), __stream); \ +} while (0) +#endif /* _KUNIT_TEST_H */
kunit/string-stream-test.c +6 -6
@@ -19,7 +19,7 @@ static void string_stream_test_get_string(struct kunit *test)string_stream_add (stream," %s" ,"bar" ); output =string_stream_get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"Foo bar" ); +KUNIT_ASSERT_STREQ (test, output,"Foo bar" );kfree (output);destroy_string_stream (stream); }
@@ -34,16 +34,16 @@ static void string_stream_test_add_and_clear(struct kunit *test)string_stream_add (stream,"A" ); output =string_stream_get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"AAAAAAAAAA" ); -KUNIT_EXPECT_EQ (test, stream->length,10 ); -KUNIT_EXPECT_FALSE (test,string_stream_is_empty (stream)); +KUNIT_ASSERT_STREQ (test, output,"AAAAAAAAAA" ); +KUNIT_ASSERT_EQ (test, stream->length,10 ); +KUNIT_ASSERT_FALSE (test,string_stream_is_empty (stream));kfree (output);string_stream_clear (stream); output =string_stream_get_string (stream); -KUNIT_EXPECT_STREQ (test, output,"" ); -KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); +KUNIT_ASSERT_STREQ (test, output,"" ); +KUNIT_ASSERT_TRUE (test,string_stream_is_empty (stream));destroy_string_stream (stream); }
kunit/test-test.c +2 -0
@@ -78,11 +78,13 @@ static int kunit_try_catch_test_init(struct kunit *test)struct kunit_try_catch_test_context *ctx; ctx =kunit_kzalloc (test,sizeof (*ctx),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx); test->priv = ctx; ctx->try_catch =kunit_kmalloc (test,sizeof (*ctx->try_catch),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ctx->try_catch);return 0 ; }
kunit/test.c +66 -0
69 lines not on this page · raw message
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v4 12/18 kunit: test: add tests for KUnit managed resources
In reply to Brendan HigginsFrom: Avinash Kondareddy <[email protected]>
Add unit tests for KUnit managed resources. KUnit managed resources (struct kunit_resource) are resources that are automatically cleaned up at the end of a KUnit test, similar to the concept of devm_* managed resources.
--- kunit/test-test.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+)
kunit/test-test.c +123 -0
@@ -101,3 +101,126 @@ static struct kunit_module kunit_try_catch_test_module = { .test_cases = kunit_try_catch_test_cases, };module_test (kunit_try_catch_test_module); + +/* +* Context for testing test managed resources +* is_resource_initialized is used to test arbitrary resources +*/ +struct kunit_test_resource_context { +struct kunit test; +bool is_resource_initialized; +}; + +static int fake_resource_init (struct kunit_resource *res,void *context) +{ +struct kunit_test_resource_context *ctx = context; + + res->allocation = &ctx->is_resource_initialized; + ctx->is_resource_initialized = true; +return 0 ; +} + +static void fake_resource_free (struct kunit_resource *res) +{ +bool *is_resource_initialized = res->allocation; + + *is_resource_initialized = false; +} + +static void kunit_resource_test_init_resources (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; + +kunit_init_test (&ctx->test,"testing_test_init_test" ); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +static void kunit_resource_test_alloc_resource (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *res; +kunit_resource_free_t free = fake_resource_free; + + res =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, res); +KUNIT_EXPECT_PTR_EQ (test, + &ctx->is_resource_initialized, res->allocation); +KUNIT_EXPECT_TRUE (test,list_is_last (&res->node, &ctx->test.resources)); +KUNIT_EXPECT_PTR_EQ (test, free, res->free); +} + +static void kunit_resource_test_free_resource (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *res =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + +kunit_free_resource (&ctx->test, res); + +KUNIT_EXPECT_EQ (test, false, ctx->is_resource_initialized); +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +static void kunit_resource_test_cleanup_resources (struct kunit *test) +{ +int i; +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *resources[5 ]; + +for (i =0 ; i <ARRAY_SIZE (resources); i++) { + resources[i] =kunit_alloc_resource (&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + +kunit_cleanup (&ctx->test); + +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +} + +static int kunit_resource_test_init (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = +kzalloc (sizeof (*ctx),GFP_KERNEL ); + +if (!ctx) +return -ENOMEM ; + + test->priv = ctx; + +kunit_init_test (&ctx->test,"test_test_context" ); + +return 0 ; +} + +static void kunit_resource_test_exit (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; + +kunit_cleanup (&ctx->test); +kfree (ctx); +} + +static struct kunit_case kunit_resource_test_cases[] = { +KUNIT_CASE (kunit_resource_test_init_resources), +KUNIT_CASE (kunit_resource_test_alloc_resource), +KUNIT_CASE (kunit_resource_test_free_resource), +KUNIT_CASE (kunit_resource_test_cleanup_resources), + {}, +}; + +static struct kunit_module kunit_resource_test_module = { + .name ="kunit-resource-test" , + .init = kunit_resource_test_init, + .exit = kunit_resource_test_exit, + .test_cases = kunit_resource_test_cases, +}; +module_test (kunit_resource_test_module);
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v4 13/18 kunit: tool: add Python wrappers for running KUnit tests
In reply to Brendan HigginsFrom: Felix Guo <[email protected]>
The ultimate goal is to create minimal isolated test binaries; in the meantime we are using UML to provide the infrastructure to run tests, so define an abstract way to configure and run tests that allow us to change the context in which tests are built without affecting the user. This also makes pretty and dynamic error reporting, and a lot of other nice features easier.
kunit_config.py:
- parse .config and Kconfig files.
kunit_kernel.py: provides helper functions to:
- configure the kernel using kunitconfig.
- build the kernel with the appropriate configuration.
- provide function to invoke the kernel and stream the output back.
--- tools/testing/kunit/.gitignore | 3 + tools/testing/kunit/kunit.py | 116 +++++++ tools/testing/kunit/kunit_config.py | 66 ++++ tools/testing/kunit/kunit_kernel.py | 148 +++++++++ tools/testing/kunit/kunit_parser.py | 290 ++++++++++++++++++ tools/testing/kunit/kunit_tool_test.py | 206 +++++++++++++ .../test_is_test_passed-all_passed.log | 32 ++ .../test_data/test_is_test_passed-crash.log | 69 +++++ .../test_data/test_is_test_passed-failure.log | 36 +++ .../test_is_test_passed-no_tests_run.log | 75 +++++ .../test_output_isolated_correctly.log | 106 +++++++ .../test_data/test_read_from_file.kconfig | 17 + 12 files changed, 1164 insertions(+) create mode 100644 tools/testing/kunit/.gitignore create mode 100755 tools/testing/kunit/kunit.py create mode 100644 tools/testing/kunit/kunit_config.py create mode 100644 tools/testing/kunit/kunit_kernel.py create mode 100644 tools/testing/kunit/kunit_parser.py create mode 100755 tools/testing/kunit/kunit_tool_test.py create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-all_passed.log create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-crash.log create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-failure.log create mode 100644 tools/testing/kunit/test_data/test_is_test_passed-no_tests_run.log create mode 100644 tools/testing/kunit/test_data/test_output_isolated_correctly.log create mode 100644 tools/testing/kunit/test_data/test_read_from_file.kconfig
tools/testing/kunit/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod]
tools/testing/kunit/kunit.py new file +116 -0
116 lines not on this page · raw message
tools/testing/kunit/kunit_config.py new file +66 -0
66 lines not on this page · raw message
tools/testing/kunit/kunit_kernel.py new file +148 -0
148 lines not on this page · raw message
tools/testing/kunit/kunit_parser.py new file +290 -0
290 lines not on this page · raw message
tools/testing/kunit/kunit_tool_test.py new file +206 -0
206 lines not on this page · raw message
tools/testing/kunit/test_data/test_is_test_passed-all_passed.log new file +32 -0
@@ -0,0 +1,32 @@ +TAP version 14 + # Subtest: sysctl_test + 1..8 + # sysctl_test_dointvec_null_tbl_data: sysctl_test_dointvec_null_tbl_data passed + ok 1 - sysctl_test_dointvec_null_tbl_data + # sysctl_test_dointvec_table_maxlen_unset: sysctl_test_dointvec_table_maxlen_unset passed + ok 2 - sysctl_test_dointvec_table_maxlen_unset + # sysctl_test_dointvec_table_len_is_zero: sysctl_test_dointvec_table_len_is_zero passed + ok 3 - sysctl_test_dointvec_table_len_is_zero + # sysctl_test_dointvec_table_read_but_position_set: sysctl_test_dointvec_table_read_but_position_set passed + ok 4 - sysctl_test_dointvec_table_read_but_position_set + # sysctl_test_dointvec_happy_single_positive: sysctl_test_dointvec_happy_single_positive passed + ok 5 - sysctl_test_dointvec_happy_single_positive + # sysctl_test_dointvec_happy_single_negative: sysctl_test_dointvec_happy_single_negative passed + ok 6 - sysctl_test_dointvec_happy_single_negative + # sysctl_test_dointvec_single_less_int_min: sysctl_test_dointvec_single_less_int_min passed + ok 7 - sysctl_test_dointvec_single_less_int_min + # sysctl_test_dointvec_single_greater_int_max: sysctl_test_dointvec_single_greater_int_max passed + ok 8 - sysctl_test_dointvec_single_greater_int_max +kunit sysctl_test: all tests passed +ok 1 - sysctl_test + # Subtest: example + 1..2 +init_module + # example_simple_test: initializing + # example_simple_test: example_simple_test passed + ok 1 - example_simple_test + # example_mock_test: initializing + # example_mock_test: example_mock_test passed + ok 2 - example_mock_test +kunit example: all tests passed +ok 2 - example
tools/testing/kunit/test_data/test_is_test_passed-crash.log new file +69 -0
69 lines not on this page · raw message
tools/testing/kunit/test_data/test_is_test_passed-failure.log new file +36 -0
@@ -0,0 +1,36 @@ +TAP version 14 + # Subtest: sysctl_test + 1..8 + # sysctl_test_dointvec_null_tbl_data: sysctl_test_dointvec_null_tbl_data passed + ok 1 - sysctl_test_dointvec_null_tbl_data + # sysctl_test_dointvec_table_maxlen_unset: sysctl_test_dointvec_table_maxlen_unset passed + ok 2 - sysctl_test_dointvec_table_maxlen_unset + # sysctl_test_dointvec_table_len_is_zero: sysctl_test_dointvec_table_len_is_zero passed + ok 3 - sysctl_test_dointvec_table_len_is_zero + # sysctl_test_dointvec_table_read_but_position_set: sysctl_test_dointvec_table_read_but_position_set passed + ok 4 - sysctl_test_dointvec_table_read_but_position_set + # sysctl_test_dointvec_happy_single_positive: sysctl_test_dointvec_happy_single_positive passed + ok 5 - sysctl_test_dointvec_happy_single_positive + # sysctl_test_dointvec_happy_single_negative: sysctl_test_dointvec_happy_single_negative passed + ok 6 - sysctl_test_dointvec_happy_single_negative + # sysctl_test_dointvec_single_less_int_min: sysctl_test_dointvec_single_less_int_min passed + ok 7 - sysctl_test_dointvec_single_less_int_min + # sysctl_test_dointvec_single_greater_int_max: sysctl_test_dointvec_single_greater_int_max passed + ok 8 - sysctl_test_dointvec_single_greater_int_max +kunit sysctl_test: all tests passed +ok 1 - sysctl_test + # Subtest: example + 1..2 +init_module + # example_simple_test: initializing + # example_simple_test: EXPECTATION FAILED at kunit/example-test.c:30 + Expected 1 + 1 == 3, but + 1 + 1 == 2 + 3 == 3 + # example_simple_test: example_simple_test failed + not ok 1 - example_simple_test + # example_mock_test: initializing + # example_mock_test: example_mock_test passed + ok 2 - example_mock_test +kunit example: one or more tests failed +not ok 2 - example
tools/testing/kunit/test_data/test_is_test_passed-no_tests_run.log new file +75 -0
75 lines not on this page · raw message
tools/testing/kunit/test_data/test_output_isolated_correctly.log new file +106 -0
106 lines not on this page · raw message
tools/testing/kunit/test_data/test_read_from_file.kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# +# Automatically generated file; DO NOT EDIT. +# User Mode Linux/x86 4.12.0-rc3 Kernel Configuration +# +CONFIG_UML=y +CONFIG_MMU=y + +# +# UML-specific options +# + +# +# Host processor type and features +# +# CONFIG_MK8 is not set +CONFIG_TEST=y +CONFIG_EXAMPLE_TEST=y
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v4 14/18 kunit: defconfig: add defconfigs for building KUnit tests
In reply to Brendan HigginsAdd defconfig for UML and a fragment that can be used to configure other architectures for building KUnit tests. Add option to kunit_tool to use a defconfig to create the kunitconfig.
--- arch/um/configs/kunit_defconfig | 8 ++++++++ tools/testing/kunit/configs/all_tests.config | 8 ++++++++ tools/testing/kunit/kunit.py | 18 ++++++++++++++++-- tools/testing/kunit/kunit_kernel.py | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 arch/um/configs/kunit_defconfig create mode 100644 tools/testing/kunit/configs/all_tests.config
arch/um/configs/kunit_defconfig new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_OF=y +CONFIG_OF_UNITTEST=y +CONFIG_OF_OVERLAY=y +CONFIG_I2C=y +CONFIG_I2C_MUX=y +CONFIG_KUNIT=y +CONFIG_KUNIT_TEST=y +CONFIG_KUNIT_EXAMPLE_TEST=y
tools/testing/kunit/configs/all_tests.config new file +8 -0
@@ -0,0 +1,8 @@ +CONFIG_OF=y +CONFIG_OF_UNITTEST=y +CONFIG_OF_OVERLAY=y +CONFIG_I2C=y +CONFIG_I2C_MUX=y +CONFIG_KUNIT=y +CONFIG_KUNIT_TEST=y +CONFIG_KUNIT_EXAMPLE_TEST=y
tools/testing/kunit/kunit.py +16 -2
@@ -11,6 +11,7 @@ import argparse import sys import os import time +import shutil from collections import namedtuple from enum import Enum, auto
@@ -21,7 +22,7 @@ import kunit_parser KunitResult = namedtuple('KunitResult', ['status','result']) -KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir']) +KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir', 'defconfig']) class KunitStatus(Enum): SUCCESS = auto()
@@ -29,8 +30,16 @@ class KunitStatus(Enum): BUILD_FAILURE = auto() TEST_FAILURE = auto() +def create_default_kunitconfig(): + if not os.path.exists(kunit_kernel.KUNITCONFIG_PATH): + shutil.copyfile('arch/um/configs/kunit_defconfig', + kunit_kernel.KUNITCONFIG_PATH) + def run_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitRequest) -> KunitResult: + if request.defconfig: + create_default_kunitconfig() + config_start = time.time() success = linux.build_reconfig(request.build_dir) config_end = time.time()
@@ -99,13 +108,18 @@ def main(argv, linux): 'directory.', type=str, default=None, metavar='build_dir') + run_parser.add_argument('--defconfig', + help='Uses a default kunitconfig.', + action='store_true') + cli_args = parser.parse_args(argv) if cli_args.subcommand == 'run': request = KunitRequest(cli_args.raw_output, cli_args.timeout, cli_args.jobs, - cli_args.build_dir) + cli_args.build_dir, + cli_args.defconfig) result = run_tests(linux, request) if result.status != KunitStatus.SUCCESS: sys.exit(1)
tools/testing/kunit/kunit_kernel.py +2 -1
@@ -14,6 +14,7 @@ import os import kunit_config KCONFIG_PATH = '.config' +KUNITCONFIG_PATH = 'kunitconfig' class ConfigError(Exception): """Represents an error trying to configure the Linux kernel."""
@@ -81,7 +82,7 @@ class LinuxSourceTree(object): def __init__(self): self._kconfig = kunit_config.Kconfig() - self._kconfig.read_from_file('kunitconfig') + self._kconfig.read_from_file(KUNITCONFIG_PATH) self._ops = LinuxSourceTreeOperations() def clean(self):
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 15/18 Documentation: kunit: add documentation for KUnit
In reply to Brendan HigginsAdd documentation for KUnit, the Linux kernel unit testing framework.
- Add intro and usage guide for KUnit
- Add API reference
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kunit/api/index.rst | 16 + Documentation/dev-tools/kunit/api/test.rst | 14 + Documentation/dev-tools/kunit/faq.rst | 62 +++ Documentation/dev-tools/kunit/index.rst | 79 +++ Documentation/dev-tools/kunit/start.rst | 180 ++++++ Documentation/dev-tools/kunit/usage.rst | 575 ++++++++++++++++++++ 7 files changed, 927 insertions(+) create mode 100644 Documentation/dev-tools/kunit/api/index.rst create mode 100644 Documentation/dev-tools/kunit/api/test.rst create mode 100644 Documentation/dev-tools/kunit/faq.rst create mode 100644 Documentation/dev-tools/kunit/index.rst create mode 100644 Documentation/dev-tools/kunit/start.rst create mode 100644 Documentation/dev-tools/kunit/usage.rst
Documentation/dev-tools/index.rst +1 -0
@@ -24,6 +24,7 @@ whole; patches welcome! gdb-kernel-debugging kgdb kselftest + kunit/index .. only:: subproject and html
Documentation/dev-tools/kunit/api/index.rst new file +16 -0
@@ -0,0 +1,16 @@ +.. SPDX-License-Identifier: GPL-2.0 + +============= +API Reference +============= +.. toctree:: + + test + +This section documents the KUnit kernel testing API. It is divided into the +following sections: + +================================= ============================================== +:doc:`test` documents all of the standard testing API + excluding mocking or mocking related features. +================================= ==============================================
Documentation/dev-tools/kunit/api/test.rst new file +14 -0
@@ -0,0 +1,14 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======== +Test API +======== + +This file documents all of the standard testing API excluding mocking or mocking +related features. + +.. kernel-doc:: include/kunit/test.h + :internal: + +.. kernel-doc:: include/kunit/kunit-stream.h + :internal:
Documentation/dev-tools/kunit/faq.rst new file +62 -0
62 lines not on this page · raw message
Documentation/dev-tools/kunit/index.rst new file +79 -0
79 lines not on this page · raw message
Documentation/dev-tools/kunit/start.rst new file +180 -0
180 lines not on this page · raw message
Documentation/dev-tools/kunit/usage.rst new file +575 -0
575 lines not on this page · raw message
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 16/18 MAINTAINERS: add entry for KUnit the unit testing framework
In reply to Brendan HigginsAdd myself as maintainer of KUnit, the Linux kernel's unit testing framework.
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -8448,6 +8448,17 @@ S: Maintained F: tools/testing/selftests/ F: Documentation/dev-tools/kselftest* +KERNEL UNIT TESTING FRAMEWORK (KUnit) +M: Brendan Higgins <[email protected]> +L: [email protected] +L: [email protected] +W: https://google.github.io/kunit-docs/third_party/kernel/docs/ +S: Maintained +F: Documentation/kunit/ +F: include/kunit/ +F: kunit/ +F: tools/testing/kunit/ + KERNEL USERMODE HELPER M: Luis Chamberlain <[email protected]> L: [email protected]
Signature · 6 lines
2.21.0.1020.gf2820cf01a-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v4 16/18] MAINTAINERS: add entry for KUnit the unit testing framework
Quoted 15 lines · Brendan Higgins
Add myself as maintainer of KUnit, the Linux kernel's unit testing framework.
Signed-off-by Brendan Higgins <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Logan Gunthorpe <[email protected]>--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)MAINTAINERS +0
-0
MAINTAINERS S: Maintained · line 8457
Quoted 10 lines
MAINTAINERS +7
-0@@ -8448,6 +8448,17 @@ S: Maintained F: tools/testing/selftests/ F: Documentation/dev-tools/kselftest* +KERNEL UNIT TESTING FRAMEWORK (KUnit) +M: Brendan Higgins <[email protected]> +L: [email protected] +L: [email protected] +W: https://google.github.io/kunit-docs/third_party/kernel/docs/ +S: Maintained +F: Documentation/kunit/
Dang it! I forgot to update the documentation path...
Will fix in next revision.
Quoted 10 lines
MAINTAINERS +4
-0+F: include/kunit/ +F: kunit/ +F: tools/testing/kunit/ + KERNEL USERMODE HELPER M: Luis Chamberlain <[email protected]> L: [email protected]Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH v4 17/18 kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
In reply to Brendan HigginsFrom: Iurii Zaikin <[email protected]>
KUnit tests for initialized data behavior of proc_dointvec that is explicitly checked in the code. Includes basic parsing tests including int min/max overflow.
--- kernel/Makefile | 2 + kernel/sysctl-test.c | 293 +++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 6 + 3 files changed, 301 insertions(+) create mode 100644 kernel/sysctl-test.c
kernel/Makefile +2 -0
@@ -112,6 +112,8 @@ obj-$(CONFIG_HAS_IOMEM) += iomem.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_RSEQ) += rseq.o +obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o + obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o KASAN_SANITIZE_stackleak.o := n KCOV_INSTRUMENT_stackleak.o := n
kernel/sysctl-test.c new file +293 -0
293 lines not on this page · raw message
lib/Kconfig.debug +6 -0
@@ -1908,6 +1908,12 @@ config TEST_SYSCTL If unsure, say N. +config SYSCTL_KUNIT_TEST + bool "KUnit test for sysctl" + depends on KUNIT + help + Enables KUnit sysctl test. + config TEST_UDELAY tristate "udelay test driver" help
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoting Brendan Higgins (2019-05-14 15:17:10)
kernel/sysctl-test.c line 7
Quoted 13 lines
kernel/sysctl-test.c new file +7
-0@@ -0,0 +1,293 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test of proc sysctl. +*/ + +#include <kunit/test.h> +#include <linux/printk.h>
Is this include used?
kernel/sysctl-test.c line 9
Quoted 2 lines
kernel/sysctl-test.c new file +2
-0+#include <linux/sysctl.h> +#include <linux/uaccess.h>
Is this include used?
Quoted 12 lines
kernel/sysctl-test.c new file +12
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred,
kernel/sysctl-test.c line 168
Quoted 10 lines
kernel/sysctl-test.c new file +10
-0+ }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; + + table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);
Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 60 lines
kernel/sysctl-test.c new file +60
-0+} + +static void sysctl_test_dointvec_single_less_int_min (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[32 ]; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +unsigned long abs_of_less_than_min = (unsigned long )INT_MAX + - (INT_MAX +INT_MIN ) +1 ; + +KUNIT_EXPECT_LT (test, +snprintf (input,sizeof (input),"-%lu" , + abs_of_less_than_min), +sizeof (input)); + + table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test, -EINVAL , +proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,0 , *(int *)table.data); +} + +static void sysctl_test_dointvec_single_greater_int_max (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[32 ]; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +unsigned long greater_than_max = (unsigned long )INT_MAX +1 ; + +KUNIT_EXPECT_GT (test, greater_than_max,INT_MAX ); +KUNIT_EXPECT_LT (test,snprintf (input,sizeof (input),"%lu" , + greater_than_max), +sizeof (input)); + table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test, -EINVAL , +proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,0 , *(int *)table.data); +} + +static int sysctl_test_init (struct kunit *test) +{
kernel/sysctl-test.c line 238
Quoted 10 lines
kernel/sysctl-test.c new file +10
-0+return 0 ; +} + +/* +* This is run once after each test case, see the comment on example_test_module +* for more information. +*/ +static void sysctl_test_exit (struct kunit *test) +{ +}
Can the above two be omitted? If they can be empty sometimes it would be nice to avoid the extra symbols and code by letting them be assigned to NULL in the kunit_module.
Quoted 32 lines
kernel/sysctl-test.c new file +32
-0+ +/* +* Here we make a list of all the test cases we want to add to the test module +* below. +*/ +static struct kunit_case sysctl_test_cases[] = { +/* +* This is a helper to create a test case object from a test case +* function; its exact function is not important to understand how to +* use KUnit, just know that this is how you associate test cases with a +* test module. +*/ +KUNIT_CASE (sysctl_test_dointvec_null_tbl_data), +KUNIT_CASE (sysctl_test_dointvec_table_maxlen_unset), +KUNIT_CASE (sysctl_test_dointvec_table_len_is_zero), +KUNIT_CASE (sysctl_test_dointvec_table_read_but_position_set), +KUNIT_CASE (sysctl_test_dointvec_happy_single_positive), +KUNIT_CASE (sysctl_test_dointvec_happy_single_negative), +KUNIT_CASE (sysctl_test_dointvec_single_less_int_min), +KUNIT_CASE (sysctl_test_dointvec_single_greater_int_max), + {}, +}; + +/* +* This defines a suite or grouping of tests. +* +* Test cases are defined as belonging to the suite by adding them to +* `test_cases`. +* +* Often it is desirable to run some function which will set up things which +* will be used by every test; this is accomplished with an `init` function +* which runs before each test case is invoked. Similarly, an `exit` function
kernel/sysctl-test.c line 280
Quoted 10 lines
kernel/sysctl-test.c new file +10
-0+* may be specified which runs after every test case and can be used to for +* cleanup. For clarity, running tests in a test module would behave as follows: +* +* module.init(test); +* module.test_case[0](test); +* module.exit(test); +* module.init(test); +* module.test_case[1](test); +* module.exit(test); +* ...;
This comment (and the one above for "this is a helper") looks generic and should probably only be in some documentation somewhere and not for a sysctl test?
kernel/sysctl-test.c line 292
Quoted 12 lines
kernel/sysctl-test.c new file +12
-0+*/ +static struct kunit_module sysctl_test_module = { + .name ="sysctl_test" , + .init = sysctl_test_init, + .exit = sysctl_test_exit, + .test_cases = sysctl_test_cases, +}; + +/* +* This registers the above test module telling KUnit that this is a suite of +* tests that need to be run. +*/
Same comment about generic comment.
lib/Kconfig.debug config TEST_SYSCTL · line 1912
Quoted 11 lines
kernel/sysctl-test.c new file +1
-0+module_test (sysctl_test_module);lib/Kconfig.debug +2
-0@@ -1908,6 +1908,12 @@ config TEST_SYSCTL If unsure, say N. +config SYSCTL_KUNIT_TEST + bool "KUnit test for sysctl"
Why not tristate?
Quoted 4 lines
lib/Kconfig.debug +4
-0+ depends on KUNIT + help + Enables KUnit sysctl test. +
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoted 17 lines · Stephen Boyd
Quoting Brendan Higgins (2019-05-14 15:17:10)
kernel/sysctl-test.c line 7
Quoted 13 lines
kernel/sysctl-test.c new file +7
-0@@ -0,0 +1,293 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test of proc sysctl. +*/ + +#include <kunit/test.h> +#include <linux/printk.h> Is this include used?
Deleted.
Quoted 5 lines
kernel/sysctl-test.c line 9
Quoted 3 lines
kernel/sysctl-test.c new file +2
-0+#include <linux/sysctl.h> +#include <linux/uaccess.h> Is this include used?
Deleted.
Quoted 18 lines
Quoted 18 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +
Quoted 8 lines
kernel/sysctl-test.c line 168
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.
Quoted 66 lines
Quoted 66 lines
kernel/sysctl-test.c new file +65
-0+} + +static void sysctl_test_dointvec_single_less_int_min (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[32 ]; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +unsigned long abs_of_less_than_min = (unsigned long )INT_MAX + - (INT_MAX +INT_MIN ) +1 ; + +KUNIT_EXPECT_LT (test, +snprintf (input,sizeof (input),"-%lu" , + abs_of_less_than_min), +sizeof (input)); + + table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test, -EINVAL , +proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,0 , *(int *)table.data); +} + +static void sysctl_test_dointvec_single_greater_int_max (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[32 ]; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +unsigned long greater_than_max = (unsigned long )INT_MAX +1 ; + +KUNIT_EXPECT_GT (test, greater_than_max,INT_MAX ); +KUNIT_EXPECT_LT (test,snprintf (input,sizeof (input),"%lu" , + greater_than_max), +sizeof (input)); + table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test, -EINVAL , +proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,0 , *(int *)table.data); +} + +static int sysctl_test_init (struct kunit *test) +{ +return 0 ; +} + +/* +* This is run once after each test case, see the comment on example_test_module
Quoted 8 lines
kernel/sysctl-test.c line 238
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+* for more information. +*/ +static void sysctl_test_exit (struct kunit *test) +{ +}Can the above two be omitted? If they can be empty sometimes it would be nice to avoid the extra symbols and code by letting them be assigned to NULL in the
kunit_module.
Deleted.
Quoted 39 lines
Quoted 39 lines
kernel/sysctl-test.c new file +38
-0+ +/* +* Here we make a list of all the test cases we want to add to the test module +* below. +*/ +static struct kunit_case sysctl_test_cases[] = { +/* +* This is a helper to create a test case object from a test case +* function; its exact function is not important to understand how to +* use KUnit, just know that this is how you associate test cases with a +* test module. +*/ +KUNIT_CASE (sysctl_test_dointvec_null_tbl_data), +KUNIT_CASE (sysctl_test_dointvec_table_maxlen_unset), +KUNIT_CASE (sysctl_test_dointvec_table_len_is_zero), +KUNIT_CASE (sysctl_test_dointvec_table_read_but_position_set), +KUNIT_CASE (sysctl_test_dointvec_happy_single_positive), +KUNIT_CASE (sysctl_test_dointvec_happy_single_negative), +KUNIT_CASE (sysctl_test_dointvec_single_less_int_min), +KUNIT_CASE (sysctl_test_dointvec_single_greater_int_max), + {}, +}; + +/* +* This defines a suite or grouping of tests. +* +* Test cases are defined as belonging to the suite by adding them to +* `test_cases`. +* +* Often it is desirable to run some function which will set up things which +* will be used by every test; this is accomplished with an `init` function +* which runs before each test case is invoked. Similarly, an `exit` function +* may be specified which runs after every test case and can be used to for +* cleanup. For clarity, running tests in a test module would behave as follows: +* +* module.init(test); +* module.test_case[0](test); +* module.exit(test);
Quoted 9 lines
kernel/sysctl-test.c line 280
Quoted 4 lines
kernel/sysctl-test.c new file +4
-0+* module.init(test); +* module.test_case[1](test); +* module.exit(test); +* ...; This comment (and the one above for "this is a helper") looks generic and should probably only be in some documentation somewhere and not for a sysctl test?
Deleted.
Quoted 6 lines
Quoted 6 lines
kernel/sysctl-test.c new file +6
-0+*/ +static struct kunit_module sysctl_test_module = { + .name ="sysctl_test" , + .init = sysctl_test_init, + .exit = sysctl_test_exit, + .test_cases = sysctl_test_cases,
Quoted 9 lines
kernel/sysctl-test.c line 292
Quoted 6 lines
kernel/sysctl-test.c new file +6
-0+}; + +/* +* This registers the above test module telling KUnit that this is a suite of +* tests that need to be run. +*/ Same comment about generic comment.
Deleted.
Quoted 7 lines
Quoted 7 lines
kernel/sysctl-test.c new file +1
-0+module_test (sysctl_test_module);lib/Kconfig.debug +0
-0@@ -1908,6 +1908,12 @@ config TEST_SYSCTL
Quoted 7 lines
lib/Kconfig.debug config TEST_SYSCTL · line 1912
Quoted 4 lines
lib/Kconfig.debug +2
-0If unsure, say N. +config SYSCTL_KUNIT_TEST + bool "KUnit test for sysctl"Why not tristate?
I don't believe KUnit as a module is currently supported.
Quoted 4 lines
Quoted 4 lines
lib/Kconfig.debug +4
-0+ depends on KUNIT + help + Enables KUnit sysctl test. +
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoting Iurii Zaikin (2019-06-05 18:29:42)
Quoted 33 lines · Stephen Boyd
Quoted 24 lines
Quoting Brendan Higgins (2019-05-14 15:17:10)
Quoted 22 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +Quoted 8 lines
kernel/sysctl-test.c line 168
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 5 lines
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.
Ok, I still wonder if we should make KUNIT_EXPECT_EQ check the types on both sides and cause a build warning/error if the types aren't the same. This would be similar to our min/max macros that complain about mismatched types in the comparisons. Then if a test developer needs to convert one type or the other they could do so with a KUNIT_EXPECT_EQ_T() macro that lists the types to coerce both sides to explicitly.
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoted 41 lines · Stephen Boyd
Quoting Iurii Zaikin (2019-06-05 18:29:42)
Quoted 33 lines · Stephen Boyd
Quoted 24 lines
Quoting Brendan Higgins (2019-05-14 15:17:10)
Quoted 22 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +Quoted 8 lines
kernel/sysctl-test.c line 168
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 5 lines
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.
Quoted 7 lines
Ok, I still wonder if we should make
KUNIT_EXPECT_EQcheck the types on both sides and cause a build warning/error if the types aren't the same. This would be similar to our min/max macros that complain about mismatched types in the comparisons. Then if a test developer needs to convert one type or the other they could do so with aKUNIT_EXPECT_EQ_T()macro that lists the types to coerce both sides to explicitly.
Good point. I would definitely like to do this, for me it is only a question of how difficult it would be to make all that happen.
We will investigate and report back on it.
Thanks for the suggestion! It's a really good idea!
Cheers
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
In reply to Stephen BoydQuoted 40 lines · Stephen Boyd
Quoting Iurii Zaikin (2019-06-05 18:29:42)
Quoted 33 lines · Stephen Boyd
Quoted 24 lines
Quoting Brendan Higgins (2019-05-14 15:17:10)
Quoted 22 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +Quoted 8 lines
kernel/sysctl-test.c
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 5 lines
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.
Quoted 7 lines
Ok, I still wonder if we should make
KUNIT_EXPECT_EQcheck the types on both sides and cause a build warning/error if the types aren't the same. This would be similar to our min/max macros that complain about mismatched types in the comparisons. Then if a test developer needs to convert one type or the other they could do so with aKUNIT_EXPECT_EQ_T()macro that lists the types to coerce both sides to explicitly.
Do you think it would be better to do a phony compare similar to how min/max used to work prior to 4.17, or to use the new __typecheck(...) macro? This might seem like a dumb question (and maybe it is), but Iurii and I thought the former created an error message that was a bit easier to understand, whereas __typecheck is obviously superior in terms of code reuse.
This is what we are thinking right now; if you don't have any complaints I will squash it into the relevant commits on the next revision:
---
From: Iurii Zaikin <[email protected]>
Adds a warning message when comparing values of different types similar to what min() / max() macros do.
--- include/kunit/test.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
include/kunit/test.h +15 -0
@@ -335,6 +335,13 @@ void __printf(3, 4) kunit_printk(const char *level,#define kunit_err (test, fmt, ...) \ kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) +/* +* 'Unnecessary' cast serves to generate a compile-time warning in case +* of comparing incompatible types. Inspired by include/linux/kernel.h +*/ +#define __kunit_typecheck (lhs, rhs) \ + ((void) (&(lhs) == &(rhs))) +static inline struct kunit_stream *kunit_expect_start (struct kunit *test,const char *file,const char *line)
@@ -514,6 +521,7 @@ static inline void kunit_expect_ptr_binary(struct kunit *test,#define KUNIT_EXPECT_BINARY (test, left, condition, right) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_expect_binary(test, \ (long long) __left, #left, \ (long long) __right, #right, \
@@ -524,6 +532,7 @@ static inline void kunit_expect_ptr_binary(struct kunit *test,#define KUNIT_EXPECT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_expect_binary_msg(test, \ (long long) __left, #left, \ (long long) __right, #right, \
@@ -538,6 +547,7 @@ static inline void kunit_expect_ptr_binary(struct kunit *test,#define KUNIT_EXPECT_PTR_BINARY (test, left, condition, right) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_expect_ptr_binary(test, \ (void *) __left, #left, \ (void *) __right, #right, \
@@ -553,6 +563,7 @@ static inline void kunit_expect_ptr_binary(struct kunit *test, ...) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_expect_ptr_binary_msg(test, \ (void *) __left, #left, \ (void *) __right, #right, \
@@ -1013,6 +1024,7 @@ static inline void kunit_assert_ptr_binary(struct kunit *test,#define KUNIT_ASSERT_BINARY (test, left, condition, right) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_assert_binary(test, \ (long long) __left, #left, \ (long long) __right, #right, \
@@ -1023,6 +1035,7 @@ static inline void kunit_assert_ptr_binary(struct kunit *test,#define KUNIT_ASSERT_BINARY_MSG (test, left, condition, right, fmt, ...) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_assert_binary_msg(test, \ (long long) __left, #left, \ (long long) __right, #right, \
@@ -1037,6 +1050,7 @@ static inline void kunit_assert_ptr_binary(struct kunit *test,#define KUNIT_ASSERT_PTR_BINARY (test, left, condition, right) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_assert_ptr_binary(test, \ (void *) __left, #left, \ (void *) __right, #right, \
@@ -1051,6 +1065,7 @@ static inline void kunit_assert_ptr_binary(struct kunit *test, fmt, ...) do { \ typeof(left) __left = (left); \ typeof(right) __right = (right); \ + __kunit_typecheck(__left, __right); \ kunit_assert_ptr_binary_msg(test, \ (void *) __left, #left, \ (void *) __right, #right, \
Signature · 6 lines
2.22.0.rc2.383.gf4fbbf30c2-goog _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoting Brendan Higgins (2019-06-11 10:58:30)
Quoted 56 lines · Stephen Boyd
Quoted 40 lines
Quoting Iurii Zaikin (2019-06-05 18:29:42)
Quoted 33 lines · Stephen Boyd
Quoted 24 lines
Quoting Brendan Higgins (2019-05-14 15:17:10)
Quoted 22 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +Quoted 8 lines
kernel/sysctl-test.c
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 5 lines
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.Quoted 7 lines
Ok, I still wonder if we should make
KUNIT_EXPECT_EQcheck the types on both sides and cause a build warning/error if the types aren't the same. This would be similar to our min/max macros that complain about mismatched types in the comparisons. Then if a test developer needs to convert one type or the other they could do so with aKUNIT_EXPECT_EQ_T()macro that lists the types to coerce both sides to explicitly.Do you think it would be better to do a phony compare similar to how min/max used to work prior to 4.17, or to use the new
__typecheck(...)macro? This might seem like a dumb question (and maybe it is), but Iurii and I thought the former created an error message that was a bit easier to understand, whereas__typecheckis obviously superior in terms of code reuse.
Quoted 2 lines
This is what we are thinking right now; if you don't have any complaints I will squash it into the relevant commits on the next revision:
Can you provide the difference in error messages and describe that in the commit text? The commit message is where you "sell" the patch, so being able to compare the tradeoff of having another macro to do type comparisons vs. reusing the one that's there in kernel.h would be useful to allay concerns that we're duplicating logic for better error messages.
Honestly, I'd prefer we just use the macros that we've developed in kernel.h to do comparisons here so that we can get code reuse, but more importantly so that we don't trip over problems that caused those macros to be created in the first place. If the error message is bad, perhaps that can be fixed with some sort of compiler directive to make the error message a little more useful, i.e. compiletime_warning() thrown into __typecheck() or something.
Quoted 7 lines
---
From: Iurii Zaikin <[email protected]>Adds a warning message when comparing values of different types similar to what
min()/max()macros do.Signed-off-by Iurii Zaikin <[email protected]>
Re: [PATCH v4 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec()
Quoted 61 lines · Stephen Boyd
Quoting Brendan Higgins (2019-06-11 10:58:30)
Quoted 56 lines · Stephen Boyd
Quoted 40 lines
Quoting Iurii Zaikin (2019-06-05 18:29:42)
Quoted 33 lines · Stephen Boyd
Quoted 24 lines
Quoting Brendan Higgins (2019-05-14 15:17:10)
Quoted 22 lines
kernel/sysctl-test.c new file +17
-0+ + +static void sysctl_test_dointvec_happy_single_negative (struct kunit *test) +{ +struct ctl_table table = { + .procname ="foo" , + .data = &test_data.int_0001, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + .extra1 = &i_zero, + .extra2 = &i_one_hundred, + }; +char input[] ="-9" ; +size_t len =sizeof (input) -1 ; +loff_t pos =0 ; +Quoted 8 lines
kernel/sysctl-test.c
Quoted 5 lines
kernel/sysctl-test.c new file +5
-0+ table.data =kunit_kzalloc (test,sizeof (int ),GFP_USER ); +KUNIT_EXPECT_EQ (test,0 ,proc_dointvec (&table,1 , input, &len, &pos)); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , len); +KUNIT_EXPECT_EQ (test,sizeof (input) -1 , pos); +KUNIT_EXPECT_EQ (test,-9 , *(int *)table.data);Is the casting necessary? Or can the macro do a type coercion of the second parameter based on the first type?
Quoted 5 lines
Data field is defined as void* so I believe casting is necessary to
dereference it as a pointer to an array of ints. I don't think the macro should do any type coercion that == operator wouldn't do.
I did change the cast to make it more clear that it's a pointer to an
array of ints being dereferenced.Quoted 7 lines
Ok, I still wonder if we should make
KUNIT_EXPECT_EQcheck the types on both sides and cause a build warning/error if the types aren't the same. This would be similar to our min/max macros that complain about mismatched types in the comparisons. Then if a test developer needs to convert one type or the other they could do so with aKUNIT_EXPECT_EQ_T()macro that lists the types to coerce both sides to explicitly.Do you think it would be better to do a phony compare similar to how min/max used to work prior to 4.17, or to use the new
__typecheck(...)macro? This might seem like a dumb question (and maybe it is), but Iurii and I thought the former created an error message that was a bit easier to understand, whereas__typecheckis obviously superior in terms of code reuse.Quoted 2 lines
This is what we are thinking right now; if you don't have any complaints I will squash it into the relevant commits on the next revision:
Quoted 6 lines
Can you provide the difference in error messages and describe that in the commit text? The commit message is where you "sell" the patch, so being able to compare the tradeoff of having another macro to do type comparisons vs. reusing the one that's there in
kernel.hwould be useful to allay concerns that we're duplicating logic for better error messages.
Oh sorry, I didn't think too hard about the commit message since I figured it would get split up and squashed into the existing commits. I just wanted to get it out sooner to discuss this before I post the next revision (probably later this week).
Quoted 7 lines
Honestly, I'd prefer we just use the macros that we've developed in
kernel.hto do comparisons here so that we can get code reuse, but more importantly so that we don't trip over problems that caused those macros to be created in the first place. If the error message is bad, perhaps that can be fixed with some sort of compiler directive to make the error message a little more useful, i.e.compiletime_warning()thrown into__typecheck()or something.
That's a good point. I have no qualms sticking with __typecheck(...) for now; if we later feel that it is causing problems, we can always fix it later by supplying our own warning in the manner you suggest.
Iurii, do you have any additional thoughts on this?
Quoted 8 lines
Quoted 8 lines
---
From: Iurii Zaikin <[email protected]>Adds a warning message when comparing values of different types similar to what
min()/max()macros do.Signed-off-by Iurii Zaikin <[email protected]>
PATCH v4 18/18 MAINTAINERS: add proc sysctl KUnit test to PROC SYSCTL section
In reply to Brendan HigginsAdd entry for the new proc sysctl KUnit test to the PROC SYSCTL section.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -12526,6 +12526,7 @@ S: Maintained F: fs/proc/proc_sysctl.c F: include/linux/sysctl.h F: kernel/sysctl.c +F: kernel/sysctl-test.c F: tools/testing/selftests/sysctl/ PS3 NETWORK SUPPORT
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog