Currently, we can run string-stream and assertion tests only when they are built into the kernel (with config options = y), since some of the symbols (string-stream functions and functions from assert.c) are not exported into any of the namespaces, therefore they are not accessible for the modules.
This patch series exports the required symbols into the KUnit namespace. Also, it makes the string-stream test a separate module and removes the log test stub from kunit-test since now we can access the string-stream symbols even if the test which uses it is built as a module.
Additionally, this patch series merges the assertion test suite into the kunit-test, since assert.c (and all of the assertion formatting functions in it) is a part of the KUnit core.
V1 -> V2: - Patch which exports the non-static assert.c functions is replaced with the patch which prepares assert_test.c to be merged into kunit-test.c - Also, David Gow davidgow@google.com suggested merging 4th and 5th patches together, but since now the 4th patch does more than it used to do, I send it separately
Ivan Orlov (5): kunit: string-stream: export non-static functions kunit: kunit-test: Remove stub for log tests kunit: string-stream-test: Make it a separate module kunit: assert_test: Prepare to be merged into kunit-test.c kunit: Merge assertion test into kunit-test.c
include/kunit/assert.h | 4 +- lib/kunit/Kconfig | 8 + lib/kunit/Makefile | 7 +- lib/kunit/assert.c | 19 +- lib/kunit/assert_test.c | 388 -------------------------------- lib/kunit/kunit-test.c | 397 +++++++++++++++++++++++++++++++-- lib/kunit/string-stream-test.c | 2 + lib/kunit/string-stream.c | 12 +- 8 files changed, 416 insertions(+), 421 deletions(-) delete mode 100644 lib/kunit/assert_test.c
Export non-static functions from the string-stream.c file into the KUnit namespace in order to be able to access them from the KUnit core tests (when they are loaded as modules).
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com --- V1 -> V2: - No changes
lib/kunit/string-stream.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 54f4fdcbfac8..a5e3339854da 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -10,7 +10,7 @@ #include <kunit/test.h> #include <linux/list.h> #include <linux/slab.h> - +#include <kunit/visibility.h> #include "string-stream.h"
@@ -86,6 +86,7 @@ int string_stream_vadd(struct string_stream *stream,
return 0; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_vadd);
int string_stream_add(struct string_stream *stream, const char *fmt, ...) { @@ -98,6 +99,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...)
return result; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_add);
void string_stream_clear(struct string_stream *stream) { @@ -113,6 +115,7 @@ void string_stream_clear(struct string_stream *stream) stream->length = 0; spin_unlock(&stream->lock); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_clear);
char *string_stream_get_string(struct string_stream *stream) { @@ -131,6 +134,7 @@ char *string_stream_get_string(struct string_stream *stream)
return buf; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_get_string);
int string_stream_append(struct string_stream *stream, struct string_stream *other) @@ -148,11 +152,13 @@ int string_stream_append(struct string_stream *stream,
return ret; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_append);
bool string_stream_is_empty(struct string_stream *stream) { return list_empty(&stream->fragments); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_is_empty);
struct string_stream *alloc_string_stream(gfp_t gfp) { @@ -168,6 +174,7 @@ struct string_stream *alloc_string_stream(gfp_t gfp)
return stream; } +EXPORT_SYMBOL_IF_KUNIT(alloc_string_stream);
void string_stream_destroy(struct string_stream *stream) { @@ -179,6 +186,7 @@ void string_stream_destroy(struct string_stream *stream) string_stream_clear(stream); kfree(stream); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_destroy);
static void resource_free_string_stream(void *p) { @@ -200,8 +208,10 @@ struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
return stream; } +EXPORT_SYMBOL_IF_KUNIT(kunit_alloc_string_stream);
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream) { kunit_release_action(test, resource_free_string_stream, (void *)stream); } +EXPORT_SYMBOL_IF_KUNIT(kunit_free_string_stream);
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Export non-static functions from the string-stream.c file into the KUnit namespace in order to be able to access them from the KUnit core tests (when they are loaded as modules).
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This looks good to me.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
V1 -> V2:
- No changes
lib/kunit/string-stream.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 54f4fdcbfac8..a5e3339854da 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -10,7 +10,7 @@ #include <kunit/test.h> #include <linux/list.h> #include <linux/slab.h>
+#include <kunit/visibility.h> #include "string-stream.h"
@@ -86,6 +86,7 @@ int string_stream_vadd(struct string_stream *stream,
return 0;
} +EXPORT_SYMBOL_IF_KUNIT(string_stream_vadd);
int string_stream_add(struct string_stream *stream, const char *fmt, ...) { @@ -98,6 +99,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...)
return result;
} +EXPORT_SYMBOL_IF_KUNIT(string_stream_add);
void string_stream_clear(struct string_stream *stream) { @@ -113,6 +115,7 @@ void string_stream_clear(struct string_stream *stream) stream->length = 0; spin_unlock(&stream->lock); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_clear);
char *string_stream_get_string(struct string_stream *stream) { @@ -131,6 +134,7 @@ char *string_stream_get_string(struct string_stream *stream)
return buf;
} +EXPORT_SYMBOL_IF_KUNIT(string_stream_get_string);
int string_stream_append(struct string_stream *stream, struct string_stream *other) @@ -148,11 +152,13 @@ int string_stream_append(struct string_stream *stream,
return ret;
} +EXPORT_SYMBOL_IF_KUNIT(string_stream_append);
bool string_stream_is_empty(struct string_stream *stream) { return list_empty(&stream->fragments); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_is_empty);
struct string_stream *alloc_string_stream(gfp_t gfp) { @@ -168,6 +174,7 @@ struct string_stream *alloc_string_stream(gfp_t gfp)
return stream;
} +EXPORT_SYMBOL_IF_KUNIT(alloc_string_stream);
void string_stream_destroy(struct string_stream *stream) { @@ -179,6 +186,7 @@ void string_stream_destroy(struct string_stream *stream) string_stream_clear(stream); kfree(stream); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_destroy);
static void resource_free_string_stream(void *p) { @@ -200,8 +208,10 @@ struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp)
return stream;
} +EXPORT_SYMBOL_IF_KUNIT(kunit_alloc_string_stream);
void kunit_free_string_stream(struct kunit *test, struct string_stream *stream) { kunit_release_action(test, resource_free_string_stream, (void *)stream); }
+EXPORT_SYMBOL_IF_KUNIT(kunit_free_string_stream);
2.34.1
Since now we are exporting string-stream functions into the KUnit namespace, we can safely use them in kunit-test when it is compiled as a module as well. So, remove the stubs used when kunit-test is compiled as a module. Import the KUnit namespace in the test.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com --- V1 -> V2: - No changes
lib/kunit/kunit-test.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-)
diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 37e02be1e710..d86f7cb3b3e4 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -577,12 +577,6 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, };
-/* - * Log tests call string_stream functions, which aren't exported. So only - * build this code if this test is built-in. - */ -#if IS_BUILTIN(CONFIG_KUNIT_TEST) - /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
@@ -637,17 +631,6 @@ static void kunit_log_newline_test(struct kunit *test) kunit_skip(test, "only useful when debugfs is enabled"); } } -#else -static void kunit_log_test(struct kunit *test) -{ - kunit_skip(test, "Log tests only run when built-in"); -} - -static void kunit_log_newline_test(struct kunit *test) -{ - kunit_skip(test, "Log tests only run when built-in"); -} -#endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_test), @@ -872,4 +855,5 @@ kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_fault_test_suite);
MODULE_DESCRIPTION("KUnit test for core test infrastructure"); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); MODULE_LICENSE("GPL v2");
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Since now we are exporting string-stream functions into the KUnit namespace, we can safely use them in kunit-test when it is compiled as a module as well. So, remove the stubs used when kunit-test is compiled as a module. Import the KUnit namespace in the test.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This seems good to me.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
V1 -> V2:
- No changes
lib/kunit/kunit-test.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-)
diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 37e02be1e710..d86f7cb3b3e4 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -577,12 +577,6 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, };
-/*
- Log tests call string_stream functions, which aren't exported. So only
- build this code if this test is built-in.
- */
-#if IS_BUILTIN(CONFIG_KUNIT_TEST)
/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
@@ -637,17 +631,6 @@ static void kunit_log_newline_test(struct kunit *test) kunit_skip(test, "only useful when debugfs is enabled"); } } -#else -static void kunit_log_test(struct kunit *test) -{
kunit_skip(test, "Log tests only run when built-in");
-}
-static void kunit_log_newline_test(struct kunit *test) -{
kunit_skip(test, "Log tests only run when built-in");
-} -#endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_test), @@ -872,4 +855,5 @@ kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_fault_test_suite);
MODULE_DESCRIPTION("KUnit test for core test infrastructure"); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); MODULE_LICENSE("GPL v2"); -- 2.34.1
Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`).
Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com --- V1 -> V2: - No changes
lib/kunit/Kconfig | 8 ++++++++ lib/kunit/Makefile | 2 +- lib/kunit/string-stream-test.c | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index 34d7242d526d..b0713c0f9265 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -45,6 +45,14 @@ config KUNIT_TEST purposes by developers interested in testing that KUnit works as expected.
+config KUNIT_STRING_STREAM_TEST + tristate "KUnit test for string-stream" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + Enables the KUnit test for the string-stream (C++ stream style string + builder used in KUnit for building messages). For the string-stream + implementation, see lib/kunit/string-stream.c. + config KUNIT_EXAMPLE_TEST tristate "Example test for KUnit" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTS diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 30f6bbf04a4a..478beb536dc9 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -19,10 +19,10 @@ endif obj-y += hooks.o
obj-$(CONFIG_KUNIT_TEST) += kunit-test.o +obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o
# string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o obj-$(CONFIG_KUNIT_TEST) += assert_test.o endif
diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 7511442ea98f..d03cac934e04 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -534,3 +534,5 @@ static struct kunit_suite string_stream_test_suite = { .init = string_stream_test_init, }; kunit_test_suites(&string_stream_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); +MODULE_LICENSE("GPL");
On 6/18/24 10:03, Ivan Orlov wrote:
Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`).
Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
V1 -> V2:
No changes
lib/kunit/Kconfig | 8 ++++++++ lib/kunit/Makefile | 2 +- lib/kunit/string-stream-test.c | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-)
...
diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 7511442ea98f..d03cac934e04 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -534,3 +534,5 @@ static struct kunit_suite string_stream_test_suite = { .init = string_stream_test_init, }; kunit_test_suites(&string_stream_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); +MODULE_LICENSE("GPL");
missing MODULE_DESCRIPTION() this will cause a warning with make W=1
On 6/19/24 19:09, Jeff Johnson wrote:
On 6/18/24 10:03, Ivan Orlov wrote:
Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`).
Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
V1 -> V2:
- No changes
lib/kunit/Kconfig | 8 ++++++++ lib/kunit/Makefile | 2 +- lib/kunit/string-stream-test.c | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-)
...
diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 7511442ea98f..d03cac934e04 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -534,3 +534,5 @@ static struct kunit_suite string_stream_test_suite = { .init = string_stream_test_init, }; kunit_test_suites(&string_stream_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); +MODULE_LICENSE("GPL");
missing MODULE_DESCRIPTION() this will cause a warning with make W=1
Hi Jeff,
Thank you for noticing this, I'm going to add the MODULE_DESCRIPTION in the V3 of this patch series.
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`).
Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This is looking good to me other than the module description as noted by Jeff. That could be a separate patch since the rest of the series is looking good.
There is the checkpatch warning on the module description. But as David mentioned, the description looks ok to me. If there is a new version of this patch, it may be worth trying to get rid of the warning by lengthening the description.
But I am happy with this patch as is.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
V1 -> V2:
- No changes
lib/kunit/Kconfig | 8 ++++++++ lib/kunit/Makefile | 2 +- lib/kunit/string-stream-test.c | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index 34d7242d526d..b0713c0f9265 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -45,6 +45,14 @@ config KUNIT_TEST purposes by developers interested in testing that KUnit works as expected.
+config KUNIT_STRING_STREAM_TEST
tristate "KUnit test for string-stream" if !KUNIT_ALL_TESTS
default KUNIT_ALL_TESTS
help
Enables the KUnit test for the string-stream (C++ stream style string
builder used in KUnit for building messages). For the string-stream
implementation, see lib/kunit/string-stream.c.
config KUNIT_EXAMPLE_TEST tristate "Example test for KUnit" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTS diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 30f6bbf04a4a..478beb536dc9 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -19,10 +19,10 @@ endif obj-y += hooks.o
obj-$(CONFIG_KUNIT_TEST) += kunit-test.o +obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o
# string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o obj-$(CONFIG_KUNIT_TEST) += assert_test.o endif
diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 7511442ea98f..d03cac934e04 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -534,3 +534,5 @@ static struct kunit_suite string_stream_test_suite = { .init = string_stream_test_init, }; kunit_test_suites(&string_stream_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
+MODULE_LICENSE("GPL");
2.34.1
On 6/21/24 22:07, 'Rae Moar' via KUnit Development wrote:
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`).
Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible.
Reviewed-by: David Gow davidgow@google.com Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This is looking good to me other than the module description as noted by Jeff. That could be a separate patch since the rest of the series is looking good.
There is the checkpatch warning on the module description. But as David mentioned, the description looks ok to me. If there is a new version of this patch, it may be worth trying to get rid of the warning by lengthening the description.
But I am happy with this patch as is.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
Hi Rae,
Thank you for the review. I believe I'm going to send the V3 and add the module description there, to make the whole series as good as possible before it gets merged :)
Thank you so much for reviewing the series once again.
Add 'kunit_assert_' prefix for 'is_literal' and 'is_str_literal' functions. This way we will be sure that we are not exporting ambiguous symbols into the KUnit namespace.
Export these (and other) functions from assert into the KUnit namespace, so we could use them in the tests (and cover them as well).
Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com --- V1 -> V2: - Besides exporting the non-static functions from assert.c into the KUnit namespace, rename some of them as well (add kunit_assert_ prefix to make their names less ambiguous).
include/kunit/assert.h | 4 ++-- lib/kunit/assert.c | 19 +++++++++++++------ lib/kunit/assert_test.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 7e7490a74b13..3994acc520ae 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -221,8 +221,8 @@ void kunit_mem_assert_format(const struct kunit_assert *assert, #if IS_ENABLED(CONFIG_KUNIT) void kunit_assert_print_msg(const struct va_format *message, struct string_stream *stream); -bool is_literal(const char *text, long long value); -bool is_str_literal(const char *text, const char *value); +bool kunit_assert_is_literal(const char *text, long long value); +bool kunit_assert_is_str_literal(const char *text, const char *value); void kunit_assert_hexdump(struct string_stream *stream, const void *buf, const void *compared_buf, diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 867aa5c4bccf..62b86bf5603e 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -38,6 +38,7 @@ void kunit_assert_print_msg(const struct va_format *message, if (message->fmt) string_stream_add(stream, "\n%pV", message); } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_print_msg);
void kunit_fail_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -91,7 +92,8 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */ -VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value) +VISIBLE_IF_KUNIT +bool kunit_assert_is_literal(const char *text, long long value) { char *buffer; int len; @@ -112,6 +114,7 @@ VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
return ret; } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_is_literal);
void kunit_binary_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -127,12 +130,12 @@ void kunit_binary_assert_format(const struct kunit_assert *assert, binary_assert->text->left_text, binary_assert->text->operation, binary_assert->text->right_text); - if (!is_literal(binary_assert->text->left_text, binary_assert->left_value)) + if (!kunit_assert_is_literal(binary_assert->text->left_text, binary_assert->left_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n", binary_assert->text->left_text, binary_assert->left_value, binary_assert->left_value); - if (!is_literal(binary_assert->text->right_text, binary_assert->right_value)) + if (!kunit_assert_is_literal(binary_assert->text->right_text, binary_assert->right_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)", binary_assert->text->right_text, binary_assert->right_value, @@ -168,7 +171,8 @@ EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format); /* Checks if KUNIT_EXPECT_STREQ() args were string literals. * Note: `text` will have ""s where as `value` will not. */ -VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value) +VISIBLE_IF_KUNIT +bool kunit_assert_is_str_literal(const char *text, const char *value) { int len;
@@ -180,6 +184,7 @@ VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
return strncmp(text + 1, value, len - 2) == 0; } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_is_str_literal);
void kunit_binary_str_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -195,11 +200,12 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert, binary_assert->text->left_text, binary_assert->text->operation, binary_assert->text->right_text); - if (!is_str_literal(binary_assert->text->left_text, binary_assert->left_value)) + if (!kunit_assert_is_str_literal(binary_assert->text->left_text, binary_assert->left_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == "%s"\n", binary_assert->text->left_text, binary_assert->left_value); - if (!is_str_literal(binary_assert->text->right_text, binary_assert->right_value)) + if (!kunit_assert_is_str_literal(binary_assert->text->right_text, + binary_assert->right_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == "%s"", binary_assert->text->right_text, binary_assert->right_value); @@ -232,6 +238,7 @@ void kunit_assert_hexdump(struct string_stream *stream, string_stream_add(stream, " %02x ", buf1[i]); } } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_hexdump);
void kunit_mem_assert_format(const struct kunit_assert *assert, const struct va_format *message, diff --git a/lib/kunit/assert_test.c b/lib/kunit/assert_test.c index 4a5967712186..4999233180d6 100644 --- a/lib/kunit/assert_test.c +++ b/lib/kunit/assert_test.c @@ -11,28 +11,28 @@ #define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) #define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr))
-static void kunit_test_is_literal(struct kunit *test) +static void kunit_test_assert_is_literal(struct kunit *test) { - KUNIT_EXPECT_TRUE(test, is_literal("5", 5)); - KUNIT_EXPECT_TRUE(test, is_literal("0", 0)); - KUNIT_EXPECT_TRUE(test, is_literal("1234567890", 1234567890)); - KUNIT_EXPECT_TRUE(test, is_literal("-1234567890", -1234567890)); - KUNIT_EXPECT_FALSE(test, is_literal("05", 5)); - KUNIT_EXPECT_FALSE(test, is_literal("", 0)); - KUNIT_EXPECT_FALSE(test, is_literal("-0", 0)); - KUNIT_EXPECT_FALSE(test, is_literal("12#45", 1245)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245)); }
-static void kunit_test_is_str_literal(struct kunit *test) +static void kunit_test_assert_is_str_literal(struct kunit *test) { - KUNIT_EXPECT_TRUE(test, is_str_literal(""Hello, World!"", "Hello, World!")); - KUNIT_EXPECT_TRUE(test, is_str_literal("""", "")); - KUNIT_EXPECT_TRUE(test, is_str_literal(""""", """)); - KUNIT_EXPECT_FALSE(test, is_str_literal("", "")); - KUNIT_EXPECT_FALSE(test, is_str_literal(""", """)); - KUNIT_EXPECT_FALSE(test, is_str_literal(""Abacaba", "Abacaba")); - KUNIT_EXPECT_FALSE(test, is_str_literal("Abacaba"", "Abacaba")); - KUNIT_EXPECT_FALSE(test, is_str_literal(""Abacaba"", ""Abacaba"")); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""Hello, World!"", "Hello, World!")); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("""", "")); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""""", """)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", "")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""", """)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba", "Abacaba")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba"", "Abacaba")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba"", ""Abacaba"")); }
KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); @@ -366,8 +366,8 @@ static void kunit_test_mem_assert_format(struct kunit *test) }
static struct kunit_case assert_test_cases[] = { - KUNIT_CASE(kunit_test_is_literal), - KUNIT_CASE(kunit_test_is_str_literal), + KUNIT_CASE(kunit_test_assert_is_literal), + KUNIT_CASE(kunit_test_assert_is_str_literal), KUNIT_CASE(kunit_test_assert_prologue), KUNIT_CASE(kunit_test_assert_print_msg), KUNIT_CASE(kunit_test_unary_assert_format),
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Add 'kunit_assert_' prefix for 'is_literal' and 'is_str_literal' functions. This way we will be sure that we are not exporting ambiguous symbols into the KUnit namespace.
Export these (and other) functions from assert into the KUnit namespace, so we could use them in the tests (and cover them as well).
Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hi!
This looks good to me. I am happy with the changes since v1.
Reviewed-by: Rae Moar rmoar@google.com
Thanks for your work on this! -Rae
V1 -> V2:
- Besides exporting the non-static functions from assert.c into the
KUnit namespace, rename some of them as well (add kunit_assert_ prefix to make their names less ambiguous).
include/kunit/assert.h | 4 ++-- lib/kunit/assert.c | 19 +++++++++++++------ lib/kunit/assert_test.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 7e7490a74b13..3994acc520ae 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -221,8 +221,8 @@ void kunit_mem_assert_format(const struct kunit_assert *assert, #if IS_ENABLED(CONFIG_KUNIT) void kunit_assert_print_msg(const struct va_format *message, struct string_stream *stream); -bool is_literal(const char *text, long long value); -bool is_str_literal(const char *text, const char *value); +bool kunit_assert_is_literal(const char *text, long long value); +bool kunit_assert_is_str_literal(const char *text, const char *value); void kunit_assert_hexdump(struct string_stream *stream, const void *buf, const void *compared_buf, diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 867aa5c4bccf..62b86bf5603e 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -38,6 +38,7 @@ void kunit_assert_print_msg(const struct va_format *message, if (message->fmt) string_stream_add(stream, "\n%pV", message); } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_print_msg);
void kunit_fail_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -91,7 +92,8 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */ -VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value) +VISIBLE_IF_KUNIT +bool kunit_assert_is_literal(const char *text, long long value) { char *buffer; int len; @@ -112,6 +114,7 @@ VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
return ret;
} +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_is_literal);
void kunit_binary_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -127,12 +130,12 @@ void kunit_binary_assert_format(const struct kunit_assert *assert, binary_assert->text->left_text, binary_assert->text->operation, binary_assert->text->right_text);
if (!is_literal(binary_assert->text->left_text, binary_assert->left_value))
if (!kunit_assert_is_literal(binary_assert->text->left_text, binary_assert->left_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n", binary_assert->text->left_text, binary_assert->left_value, binary_assert->left_value);
if (!is_literal(binary_assert->text->right_text, binary_assert->right_value))
if (!kunit_assert_is_literal(binary_assert->text->right_text, binary_assert->right_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)", binary_assert->text->right_text, binary_assert->right_value,
@@ -168,7 +171,8 @@ EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format); /* Checks if KUNIT_EXPECT_STREQ() args were string literals.
- Note: `text` will have ""s where as `value` will not.
*/ -VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value) +VISIBLE_IF_KUNIT +bool kunit_assert_is_str_literal(const char *text, const char *value) { int len;
@@ -180,6 +184,7 @@ VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
return strncmp(text + 1, value, len - 2) == 0;
} +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_is_str_literal);
void kunit_binary_str_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -195,11 +200,12 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert, binary_assert->text->left_text, binary_assert->text->operation, binary_assert->text->right_text);
if (!is_str_literal(binary_assert->text->left_text, binary_assert->left_value))
if (!kunit_assert_is_str_literal(binary_assert->text->left_text, binary_assert->left_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"\n", binary_assert->text->left_text, binary_assert->left_value);
if (!is_str_literal(binary_assert->text->right_text, binary_assert->right_value))
if (!kunit_assert_is_str_literal(binary_assert->text->right_text,
binary_assert->right_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"", binary_assert->text->right_text, binary_assert->right_value);
@@ -232,6 +238,7 @@ void kunit_assert_hexdump(struct string_stream *stream, string_stream_add(stream, " %02x ", buf1[i]); } } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_hexdump);
void kunit_mem_assert_format(const struct kunit_assert *assert, const struct va_format *message, diff --git a/lib/kunit/assert_test.c b/lib/kunit/assert_test.c index 4a5967712186..4999233180d6 100644 --- a/lib/kunit/assert_test.c +++ b/lib/kunit/assert_test.c @@ -11,28 +11,28 @@ #define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) #define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr))
-static void kunit_test_is_literal(struct kunit *test) +static void kunit_test_assert_is_literal(struct kunit *test) {
KUNIT_EXPECT_TRUE(test, is_literal("5", 5));
KUNIT_EXPECT_TRUE(test, is_literal("0", 0));
KUNIT_EXPECT_TRUE(test, is_literal("1234567890", 1234567890));
KUNIT_EXPECT_TRUE(test, is_literal("-1234567890", -1234567890));
KUNIT_EXPECT_FALSE(test, is_literal("05", 5));
KUNIT_EXPECT_FALSE(test, is_literal("", 0));
KUNIT_EXPECT_FALSE(test, is_literal("-0", 0));
KUNIT_EXPECT_FALSE(test, is_literal("12#45", 1245));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245));
}
-static void kunit_test_is_str_literal(struct kunit *test) +static void kunit_test_assert_is_str_literal(struct kunit *test) {
KUNIT_EXPECT_TRUE(test, is_str_literal("\"Hello, World!\"", "Hello, World!"));
KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"", ""));
KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"\"", "\""));
KUNIT_EXPECT_FALSE(test, is_str_literal("", ""));
KUNIT_EXPECT_FALSE(test, is_str_literal("\"", "\""));
KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba", "Abacaba"));
KUNIT_EXPECT_FALSE(test, is_str_literal("Abacaba\"", "Abacaba"));
KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba\"", "\"Abacaba\""));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"Hello, World!\"", "Hello, World!"));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"", ""));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", ""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba\"", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba\"", "\"Abacaba\""));
}
KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); @@ -366,8 +366,8 @@ static void kunit_test_mem_assert_format(struct kunit *test) }
static struct kunit_case assert_test_cases[] = {
KUNIT_CASE(kunit_test_is_literal),
KUNIT_CASE(kunit_test_is_str_literal),
KUNIT_CASE(kunit_test_assert_is_literal),
KUNIT_CASE(kunit_test_assert_is_str_literal), KUNIT_CASE(kunit_test_assert_prologue), KUNIT_CASE(kunit_test_assert_print_msg), KUNIT_CASE(kunit_test_unary_assert_format),
-- 2.34.1
Since assert_test covers the part of the KUnit core (the assertion formatting functions), I believe it would be better to have it merged into kunit-test (as it is done for other tests for the KUnit core).
Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com --- V1 -> V2: - Update considering the changes in the previous patch (use kunit_assert_ prefixed functions)
lib/kunit/Makefile | 5 - lib/kunit/assert_test.c | 388 ---------------------------------------- lib/kunit/kunit-test.c | 379 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 378 insertions(+), 394 deletions(-) delete mode 100644 lib/kunit/assert_test.c
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 478beb536dc9..18e506b897a6 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -21,9 +21,4 @@ obj-y += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o
-# string-stream-test compiles built-in only. -ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += assert_test.o -endif - obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/assert_test.c b/lib/kunit/assert_test.c deleted file mode 100644 index 4999233180d6..000000000000 --- a/lib/kunit/assert_test.c +++ /dev/null @@ -1,388 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * KUnit test for the assertion formatting functions. - * Author: Ivan Orlov ivan.orlov0322@gmail.com - */ -#include <kunit/test.h> -#include "string-stream.h" - -#define TEST_PTR_EXPECTED_BUF_SIZE 32 -#define HEXDUMP_TEST_BUF_LEN 5 -#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) -#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr)) - -static void kunit_test_assert_is_literal(struct kunit *test) -{ - KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5)); - KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0)); - KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890)); - KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245)); -} - -static void kunit_test_assert_is_str_literal(struct kunit *test) -{ - KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""Hello, World!"", "Hello, World!")); - KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("""", "")); - KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""""", """)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", "")); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""", """)); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba", "Abacaba")); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba"", "Abacaba")); - KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba"", ""Abacaba"")); -} - -KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); - -/* this function is used to get a "char *" string from the string stream and defer its cleanup */ -static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) -{ - char *str = string_stream_get_string(stream); - - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); - kunit_add_action(test, kfree_wrapper, (void *)str); - - return str; -} - -static void kunit_test_assert_prologue(struct kunit *test) -{ - struct string_stream *stream; - char *str; - const struct kunit_loc location = { - .file = "testfile.c", - .line = 1337, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* Test an expectation fail prologue */ - kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); - - /* Test an assertion fail prologue */ - string_stream_clear(stream); - kunit_assert_prologue(&location, KUNIT_ASSERTION, stream); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); -} - -/* - * This function accepts an arbitrary count of parameters and generates a va_format struct, - * which can be used to validate kunit_assert_print_msg function - */ -static void verify_assert_print_msg(struct kunit *test, - struct string_stream *stream, - char *expected, const char *format, ...) -{ - va_list list; - const struct va_format vformat = { - .fmt = format, - .va = &list, - }; - - va_start(list, format); - string_stream_clear(stream); - kunit_assert_print_msg(&vformat, stream); - KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected); -} - -static void kunit_test_assert_print_msg(struct kunit *test) -{ - struct string_stream *stream; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - verify_assert_print_msg(test, stream, "\nTest", "Test"); - verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u", - "Abacaba", -123, 234U); - verify_assert_print_msg(test, stream, "", NULL); -} - -/* - * Further code contains the tests for different assert format functions. - * This helper function accepts the assert format function, executes it and - * validates the result string from the stream by checking that all of the - * substrings exist in the output. - */ -static void validate_assert(assert_format_t format_func, struct kunit *test, - const struct kunit_assert *assert, - struct string_stream *stream, int num_checks, ...) -{ - size_t i; - va_list checks; - char *cur_substr_exp; - struct va_format message = { NULL, NULL }; - - va_start(checks, num_checks); - string_stream_clear(stream); - format_func(assert, &message, stream); - - for (i = 0; i < num_checks; i++) { - cur_substr_exp = va_arg(checks, char *); - ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp); - } -} - -static void kunit_test_unary_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_unary_assert un_assert = { - .assert = assert, - .condition = "expr", - .expected_true = true, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - validate_assert(kunit_unary_assert_format, test, &un_assert.assert, - stream, 2, "true", "is false"); - - un_assert.expected_true = false; - validate_assert(kunit_unary_assert_format, test, &un_assert.assert, - stream, 2, "false", "is true"); -} - -static void kunit_test_ptr_not_err_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_ptr_not_err_assert not_err_assert = { - .assert = assert, - .text = "expr", - .value = NULL, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* Value is NULL. The corresponding message should be printed out */ - validate_assert(kunit_ptr_not_err_assert_format, test, - ¬_err_assert.assert, - stream, 1, "null"); - - /* Value is not NULL, but looks like an error pointer. Error should be printed out */ - not_err_assert.value = (void *)-12; - validate_assert(kunit_ptr_not_err_assert_format, test, - ¬_err_assert.assert, stream, 2, - "error", "-12"); -} - -static void kunit_test_binary_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_binary_assert_text text = { - .left_text = "1 + 2", - .operation = "==", - .right_text = "2", - }; - const struct kunit_binary_assert binary_assert = { - .assert = assert, - .text = &text, - .left_value = 3, - .right_value = 2, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* - * Printed values should depend on the input we provide: the left text, right text, left - * value and the right value. - */ - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 4, "1 + 2", "2", "3", "=="); - - text.right_text = "4 - 2"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 3, "==", "1 + 2", "4 - 2"); - - text.left_text = "3"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 4, "3", "4 - 2", "2", "=="); - - text.right_text = "2"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 3, "3", "2", "=="); -} - -static void kunit_test_binary_ptr_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - char *addr_var_a, *addr_var_b; - static const void *var_a = (void *)0xDEADBEEF; - static const void *var_b = (void *)0xBADDCAFE; - struct kunit_binary_assert_text text = { - .left_text = "var_a", - .operation = "==", - .right_text = "var_b", - }; - struct kunit_binary_ptr_assert binary_ptr_assert = { - .assert = assert, - .text = &text, - .left_value = var_a, - .right_value = var_b, - }; - - addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a); - addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b); - /* - * Print the addresses to the buffers first. - * This is necessary as we may have different count of leading zeros in the pointer - * on different architectures. - */ - snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a); - snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b); - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert, - stream, 3, addr_var_a, addr_var_b, "=="); -} - -static void kunit_test_binary_str_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - static const char *var_a = "abacaba"; - static const char *var_b = "kernel"; - struct kunit_binary_assert_text text = { - .left_text = "var_a", - .operation = "==", - .right_text = "var_b", - }; - struct kunit_binary_str_assert binary_str_assert = { - .assert = assert, - .text = &text, - .left_value = var_a, - .right_value = var_b, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - validate_assert(kunit_binary_str_assert_format, test, - &binary_str_assert.assert, - stream, 5, "var_a", "var_b", ""abacaba"", - ""kernel"", "=="); - - text.left_text = ""abacaba""; - validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, - stream, 4, ""abacaba"", "var_b", ""kernel"", "=="); - - text.right_text = ""kernel""; - validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, - stream, 3, ""abacaba"", ""kernel"", "=="); -} - -static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, - 0x45, 0x9d, 0x47, 0xd6, 0x47, - 0x2, 0x89, 0x8c, 0x81, 0x94, - 0x12, 0xfe, 0x01 }; -static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, - 0x45, 0x9d, 0x47, 0x21, 0x47, - 0xcd, 0x89, 0x24, 0x50, 0x94, - 0x12, 0xba, 0x01 }; -static void kunit_test_assert_hexdump(struct kunit *test) -{ - struct string_stream *stream; - char *str; - size_t i; - char buf[HEXDUMP_TEST_BUF_LEN]; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - /* Check that we are getting output like <xx> for non-matching numbers. */ - kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1)); - str = get_str_from_stream(test, stream); - for (i = 0; i < sizeof(hex_testbuf1); i++) { - snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]); - if (hex_testbuf1[i] != hex_testbuf2[i]) - ASSERT_TEST_EXPECT_CONTAIN(test, str, buf); - } - /* We shouldn't get any <xx> numbers when comparing the buffer with itself. */ - string_stream_clear(stream); - kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1)); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<"); - ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">"); -} - -static void kunit_test_mem_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct string_stream *expected_stream; - struct kunit_assert assert = {}; - static const struct kunit_binary_assert_text text = { - .left_text = "hex_testbuf1", - .operation = "==", - .right_text = "hex_testbuf2", - }; - struct kunit_mem_assert mem_assert = { - .assert = assert, - .text = &text, - .left_value = NULL, - .right_value = hex_testbuf2, - .size = sizeof(hex_testbuf1), - }; - - expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream); - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* The left value is NULL */ - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 2, "hex_testbuf1", "is not null"); - - /* The right value is NULL, the left value is not NULL */ - mem_assert.left_value = hex_testbuf1; - mem_assert.right_value = NULL; - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 2, "hex_testbuf2", "is not null"); - - /* Both arguments are not null */ - mem_assert.left_value = hex_testbuf1; - mem_assert.right_value = hex_testbuf2; - - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 3, "hex_testbuf1", "hex_testbuf2", "=="); -} - -static struct kunit_case assert_test_cases[] = { - KUNIT_CASE(kunit_test_assert_is_literal), - KUNIT_CASE(kunit_test_assert_is_str_literal), - KUNIT_CASE(kunit_test_assert_prologue), - KUNIT_CASE(kunit_test_assert_print_msg), - KUNIT_CASE(kunit_test_unary_assert_format), - KUNIT_CASE(kunit_test_ptr_not_err_assert_format), - KUNIT_CASE(kunit_test_binary_assert_format), - KUNIT_CASE(kunit_test_binary_ptr_assert_format), - KUNIT_CASE(kunit_test_binary_str_assert_format), - KUNIT_CASE(kunit_test_assert_hexdump), - KUNIT_CASE(kunit_test_mem_assert_format), - {} -}; - -static struct kunit_suite assert_test_suite = { - .name = "kunit-assert", - .test_cases = assert_test_cases, -}; - -kunit_test_suites(&assert_test_suite); diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index d86f7cb3b3e4..71a3edde2ff4 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -849,10 +849,387 @@ static struct kunit_suite kunit_current_test_suite = { .test_cases = kunit_current_test_cases, };
+#define TEST_PTR_EXPECTED_BUF_SIZE 32 +#define HEXDUMP_TEST_BUF_LEN 5 +#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) +#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr)) + +static void kunit_test_assert_is_literal(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890)); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245)); +} + +static void kunit_test_assert_is_str_literal(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""Hello, World!"", "Hello, World!")); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("""", "")); + KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal(""""", """)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", "")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""", """)); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba", "Abacaba")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba"", "Abacaba")); + KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal(""Abacaba"", ""Abacaba"")); +} + +/* this function is used to get a "char *" string from the string stream and defer its cleanup */ +static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) +{ + char *str = string_stream_get_string(stream); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); + kunit_add_action(test, kfree_wrapper, (void *)str); + + return str; +} + +static void kunit_test_assert_prologue(struct kunit *test) +{ + struct string_stream *stream; + char *str; + const struct kunit_loc location = { + .file = "testfile.c", + .line = 1337, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* Test an expectation fail prologue */ + kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); + + /* Test an assertion fail prologue */ + string_stream_clear(stream); + kunit_assert_prologue(&location, KUNIT_ASSERTION, stream); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); +} + +/* + * This function accepts an arbitrary count of parameters and generates a va_format struct, + * which can be used to validate kunit_assert_print_msg function + */ +static void verify_assert_print_msg(struct kunit *test, + struct string_stream *stream, + char *expected, const char *format, ...) +{ + va_list list; + const struct va_format vformat = { + .fmt = format, + .va = &list, + }; + + va_start(list, format); + string_stream_clear(stream); + kunit_assert_print_msg(&vformat, stream); + KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected); +} + +static void kunit_test_assert_print_msg(struct kunit *test) +{ + struct string_stream *stream; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + verify_assert_print_msg(test, stream, "\nTest", "Test"); + verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u", + "Abacaba", -123, 234U); + verify_assert_print_msg(test, stream, "", NULL); +} + +/* + * Further code contains the tests for different assert format functions. + * This helper function accepts the assert format function, executes it and + * validates the result string from the stream by checking that all of the + * substrings exist in the output. + */ +static void validate_assert(assert_format_t format_func, struct kunit *test, + const struct kunit_assert *assert, + struct string_stream *stream, int num_checks, ...) +{ + size_t i; + va_list checks; + char *cur_substr_exp; + struct va_format message = { NULL, NULL }; + + va_start(checks, num_checks); + string_stream_clear(stream); + format_func(assert, &message, stream); + + for (i = 0; i < num_checks; i++) { + cur_substr_exp = va_arg(checks, char *); + ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp); + } +} + +static void kunit_test_unary_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_unary_assert un_assert = { + .assert = assert, + .condition = "expr", + .expected_true = true, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + validate_assert(kunit_unary_assert_format, test, &un_assert.assert, + stream, 2, "true", "is false"); + + un_assert.expected_true = false; + validate_assert(kunit_unary_assert_format, test, &un_assert.assert, + stream, 2, "false", "is true"); +} + +static void kunit_test_ptr_not_err_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_ptr_not_err_assert not_err_assert = { + .assert = assert, + .text = "expr", + .value = NULL, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* Value is NULL. The corresponding message should be printed out */ + validate_assert(kunit_ptr_not_err_assert_format, test, + ¬_err_assert.assert, + stream, 1, "null"); + + /* Value is not NULL, but looks like an error pointer. Error should be printed out */ + not_err_assert.value = (void *)-12; + validate_assert(kunit_ptr_not_err_assert_format, test, + ¬_err_assert.assert, stream, 2, + "error", "-12"); +} + +static void kunit_test_binary_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_binary_assert_text text = { + .left_text = "1 + 2", + .operation = "==", + .right_text = "2", + }; + const struct kunit_binary_assert binary_assert = { + .assert = assert, + .text = &text, + .left_value = 3, + .right_value = 2, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* + * Printed values should depend on the input we provide: the left text, right text, left + * value and the right value. + */ + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 4, "1 + 2", "2", "3", "=="); + + text.right_text = "4 - 2"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 3, "==", "1 + 2", "4 - 2"); + + text.left_text = "3"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 4, "3", "4 - 2", "2", "=="); + + text.right_text = "2"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 3, "3", "2", "=="); +} + +static void kunit_test_binary_ptr_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + char *addr_var_a, *addr_var_b; + static const void *var_a = (void *)0xDEADBEEF; + static const void *var_b = (void *)0xBADDCAFE; + struct kunit_binary_assert_text text = { + .left_text = "var_a", + .operation = "==", + .right_text = "var_b", + }; + struct kunit_binary_ptr_assert binary_ptr_assert = { + .assert = assert, + .text = &text, + .left_value = var_a, + .right_value = var_b, + }; + + addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a); + addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b); + /* + * Print the addresses to the buffers first. + * This is necessary as we may have different count of leading zeros in the pointer + * on different architectures. + */ + snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a); + snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b); + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert, + stream, 3, addr_var_a, addr_var_b, "=="); +} + +static void kunit_test_binary_str_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + static const char *var_a = "abacaba"; + static const char *var_b = "kernel"; + struct kunit_binary_assert_text text = { + .left_text = "var_a", + .operation = "==", + .right_text = "var_b", + }; + struct kunit_binary_str_assert binary_str_assert = { + .assert = assert, + .text = &text, + .left_value = var_a, + .right_value = var_b, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + validate_assert(kunit_binary_str_assert_format, test, + &binary_str_assert.assert, + stream, 5, "var_a", "var_b", ""abacaba"", + ""kernel"", "=="); + + text.left_text = ""abacaba""; + validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, + stream, 4, ""abacaba"", "var_b", ""kernel"", "=="); + + text.right_text = ""kernel""; + validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, + stream, 3, ""abacaba"", ""kernel"", "=="); +} + +static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, + 0x45, 0x9d, 0x47, 0xd6, 0x47, + 0x2, 0x89, 0x8c, 0x81, 0x94, + 0x12, 0xfe, 0x01 }; +static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, + 0x45, 0x9d, 0x47, 0x21, 0x47, + 0xcd, 0x89, 0x24, 0x50, 0x94, + 0x12, 0xba, 0x01 }; +static void kunit_test_assert_hexdump(struct kunit *test) +{ + struct string_stream *stream; + char *str; + size_t i; + char buf[HEXDUMP_TEST_BUF_LEN]; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + /* Check that we are getting output like <xx> for non-matching numbers. */ + kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1)); + str = get_str_from_stream(test, stream); + for (i = 0; i < sizeof(hex_testbuf1); i++) { + snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]); + if (hex_testbuf1[i] != hex_testbuf2[i]) + ASSERT_TEST_EXPECT_CONTAIN(test, str, buf); + } + /* We shouldn't get any <xx> numbers when comparing the buffer with itself. */ + string_stream_clear(stream); + kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1)); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<"); + ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">"); +} + +static void kunit_test_mem_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct string_stream *expected_stream; + struct kunit_assert assert = {}; + static const struct kunit_binary_assert_text text = { + .left_text = "hex_testbuf1", + .operation = "==", + .right_text = "hex_testbuf2", + }; + struct kunit_mem_assert mem_assert = { + .assert = assert, + .text = &text, + .left_value = NULL, + .right_value = hex_testbuf2, + .size = sizeof(hex_testbuf1), + }; + + expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream); + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* The left value is NULL */ + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 2, "hex_testbuf1", "is not null"); + + /* The right value is NULL, the left value is not NULL */ + mem_assert.left_value = hex_testbuf1; + mem_assert.right_value = NULL; + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 2, "hex_testbuf2", "is not null"); + + /* Both arguments are not null */ + mem_assert.left_value = hex_testbuf1; + mem_assert.right_value = hex_testbuf2; + + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 3, "hex_testbuf1", "hex_testbuf2", "=="); +} + +static struct kunit_case kunit_assert_test_cases[] = { + KUNIT_CASE(kunit_test_assert_is_literal), + KUNIT_CASE(kunit_test_assert_is_str_literal), + KUNIT_CASE(kunit_test_assert_prologue), + KUNIT_CASE(kunit_test_assert_print_msg), + KUNIT_CASE(kunit_test_unary_assert_format), + KUNIT_CASE(kunit_test_ptr_not_err_assert_format), + KUNIT_CASE(kunit_test_binary_assert_format), + KUNIT_CASE(kunit_test_binary_ptr_assert_format), + KUNIT_CASE(kunit_test_binary_str_assert_format), + KUNIT_CASE(kunit_test_assert_hexdump), + KUNIT_CASE(kunit_test_mem_assert_format), + {} +}; + +static struct kunit_suite kunit_assert_test_suite = { + .name = "kunit-assert", + .test_cases = kunit_assert_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, &kunit_current_test_suite, &kunit_device_test_suite, - &kunit_fault_test_suite); + &kunit_fault_test_suite, &kunit_assert_test_suite);
MODULE_DESCRIPTION("KUnit test for core test infrastructure"); MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Since assert_test covers the part of the KUnit core (the assertion formatting functions), I believe it would be better to have it merged into kunit-test (as it is done for other tests for the KUnit core).
Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This looks good to me. I don't know if it was necessary to move the assert tests but I definitely see the reasoning. Happy with this as it is. There are a few checkpatch warnings I have mentioned below but I think the use case makes it necessary.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
V1 -> V2:
- Update considering the changes in the previous patch (use
kunit_assert_ prefixed functions)
lib/kunit/Makefile | 5 - lib/kunit/assert_test.c | 388 ---------------------------------------- lib/kunit/kunit-test.c | 379 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 378 insertions(+), 394 deletions(-) delete mode 100644 lib/kunit/assert_test.c
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 478beb536dc9..18e506b897a6 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -21,9 +21,4 @@ obj-y += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o
-# string-stream-test compiles built-in only. -ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += assert_test.o -endif
obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/assert_test.c b/lib/kunit/assert_test.c deleted file mode 100644 index 4999233180d6..000000000000 --- a/lib/kunit/assert_test.c +++ /dev/null @@ -1,388 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/*
- KUnit test for the assertion formatting functions.
- Author: Ivan Orlov ivan.orlov0322@gmail.com
- */
-#include <kunit/test.h> -#include "string-stream.h"
-#define TEST_PTR_EXPECTED_BUF_SIZE 32 -#define HEXDUMP_TEST_BUF_LEN 5 -#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) -#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr))
-static void kunit_test_assert_is_literal(struct kunit *test) -{
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245));
-}
-static void kunit_test_assert_is_str_literal(struct kunit *test) -{
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"Hello, World!\"", "Hello, World!"));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"", ""));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", ""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba\"", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba\"", "\"Abacaba\""));
-}
-KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
-/* this function is used to get a "char *" string from the string stream and defer its cleanup */ -static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) -{
char *str = string_stream_get_string(stream);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str);
kunit_add_action(test, kfree_wrapper, (void *)str);
return str;
-}
-static void kunit_test_assert_prologue(struct kunit *test) -{
struct string_stream *stream;
char *str;
const struct kunit_loc location = {
.file = "testfile.c",
.line = 1337,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Test an expectation fail prologue */
kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream);
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337");
/* Test an assertion fail prologue */
string_stream_clear(stream);
kunit_assert_prologue(&location, KUNIT_ASSERTION, stream);
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337");
-}
-/*
- This function accepts an arbitrary count of parameters and generates a va_format struct,
- which can be used to validate kunit_assert_print_msg function
- */
-static void verify_assert_print_msg(struct kunit *test,
struct string_stream *stream,
char *expected, const char *format, ...)
-{
va_list list;
const struct va_format vformat = {
.fmt = format,
.va = &list,
};
va_start(list, format);
string_stream_clear(stream);
kunit_assert_print_msg(&vformat, stream);
KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected);
-}
-static void kunit_test_assert_print_msg(struct kunit *test) -{
struct string_stream *stream;
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
verify_assert_print_msg(test, stream, "\nTest", "Test");
verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u",
"Abacaba", -123, 234U);
verify_assert_print_msg(test, stream, "", NULL);
-}
-/*
- Further code contains the tests for different assert format functions.
- This helper function accepts the assert format function, executes it and
- validates the result string from the stream by checking that all of the
- substrings exist in the output.
- */
-static void validate_assert(assert_format_t format_func, struct kunit *test,
const struct kunit_assert *assert,
struct string_stream *stream, int num_checks, ...)
-{
size_t i;
va_list checks;
char *cur_substr_exp;
struct va_format message = { NULL, NULL };
va_start(checks, num_checks);
string_stream_clear(stream);
format_func(assert, &message, stream);
for (i = 0; i < num_checks; i++) {
cur_substr_exp = va_arg(checks, char *);
ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp);
}
-}
-static void kunit_test_unary_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_unary_assert un_assert = {
.assert = assert,
.condition = "expr",
.expected_true = true,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_unary_assert_format, test, &un_assert.assert,
stream, 2, "true", "is false");
un_assert.expected_true = false;
validate_assert(kunit_unary_assert_format, test, &un_assert.assert,
stream, 2, "false", "is true");
-}
-static void kunit_test_ptr_not_err_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_ptr_not_err_assert not_err_assert = {
.assert = assert,
.text = "expr",
.value = NULL,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Value is NULL. The corresponding message should be printed out */
validate_assert(kunit_ptr_not_err_assert_format, test,
¬_err_assert.assert,
stream, 1, "null");
/* Value is not NULL, but looks like an error pointer. Error should be printed out */
not_err_assert.value = (void *)-12;
validate_assert(kunit_ptr_not_err_assert_format, test,
¬_err_assert.assert, stream, 2,
"error", "-12");
-}
-static void kunit_test_binary_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_binary_assert_text text = {
.left_text = "1 + 2",
.operation = "==",
.right_text = "2",
};
const struct kunit_binary_assert binary_assert = {
.assert = assert,
.text = &text,
.left_value = 3,
.right_value = 2,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/*
* Printed values should depend on the input we provide: the left text, right text, left
* value and the right value.
*/
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 4, "1 + 2", "2", "3", "==");
text.right_text = "4 - 2";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 3, "==", "1 + 2", "4 - 2");
text.left_text = "3";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 4, "3", "4 - 2", "2", "==");
text.right_text = "2";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 3, "3", "2", "==");
-}
-static void kunit_test_binary_ptr_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct kunit_assert assert = {};
char *addr_var_a, *addr_var_b;
static const void *var_a = (void *)0xDEADBEEF;
static const void *var_b = (void *)0xBADDCAFE;
struct kunit_binary_assert_text text = {
.left_text = "var_a",
.operation = "==",
.right_text = "var_b",
};
struct kunit_binary_ptr_assert binary_ptr_assert = {
.assert = assert,
.text = &text,
.left_value = var_a,
.right_value = var_b,
};
addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a);
addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b);
/*
* Print the addresses to the buffers first.
* This is necessary as we may have different count of leading zeros in the pointer
* on different architectures.
*/
snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a);
snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b);
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert,
stream, 3, addr_var_a, addr_var_b, "==");
-}
-static void kunit_test_binary_str_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct kunit_assert assert = {};
static const char *var_a = "abacaba";
static const char *var_b = "kernel";
struct kunit_binary_assert_text text = {
.left_text = "var_a",
.operation = "==",
.right_text = "var_b",
};
struct kunit_binary_str_assert binary_str_assert = {
.assert = assert,
.text = &text,
.left_value = var_a,
.right_value = var_b,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_binary_str_assert_format, test,
&binary_str_assert.assert,
stream, 5, "var_a", "var_b", "\"abacaba\"",
"\"kernel\"", "==");
text.left_text = "\"abacaba\"";
validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert,
stream, 4, "\"abacaba\"", "var_b", "\"kernel\"", "==");
text.right_text = "\"kernel\"";
validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert,
stream, 3, "\"abacaba\"", "\"kernel\"", "==");
-}
-static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55,
0x45, 0x9d, 0x47, 0xd6, 0x47,
0x2, 0x89, 0x8c, 0x81, 0x94,
0x12, 0xfe, 0x01 };
-static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55,
0x45, 0x9d, 0x47, 0x21, 0x47,
0xcd, 0x89, 0x24, 0x50, 0x94,
0x12, 0xba, 0x01 };
-static void kunit_test_assert_hexdump(struct kunit *test) -{
struct string_stream *stream;
char *str;
size_t i;
char buf[HEXDUMP_TEST_BUF_LEN];
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Check that we are getting output like <xx> for non-matching numbers. */
kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1));
str = get_str_from_stream(test, stream);
for (i = 0; i < sizeof(hex_testbuf1); i++) {
snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]);
if (hex_testbuf1[i] != hex_testbuf2[i])
ASSERT_TEST_EXPECT_CONTAIN(test, str, buf);
}
/* We shouldn't get any <xx> numbers when comparing the buffer with itself. */
string_stream_clear(stream);
kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1));
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<");
ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">");
-}
-static void kunit_test_mem_assert_format(struct kunit *test) -{
struct string_stream *stream;
struct string_stream *expected_stream;
struct kunit_assert assert = {};
static const struct kunit_binary_assert_text text = {
.left_text = "hex_testbuf1",
.operation = "==",
.right_text = "hex_testbuf2",
};
struct kunit_mem_assert mem_assert = {
.assert = assert,
.text = &text,
.left_value = NULL,
.right_value = hex_testbuf2,
.size = sizeof(hex_testbuf1),
};
expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream);
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* The left value is NULL */
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 2, "hex_testbuf1", "is not null");
/* The right value is NULL, the left value is not NULL */
mem_assert.left_value = hex_testbuf1;
mem_assert.right_value = NULL;
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 2, "hex_testbuf2", "is not null");
/* Both arguments are not null */
mem_assert.left_value = hex_testbuf1;
mem_assert.right_value = hex_testbuf2;
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 3, "hex_testbuf1", "hex_testbuf2", "==");
-}
-static struct kunit_case assert_test_cases[] = {
KUNIT_CASE(kunit_test_assert_is_literal),
KUNIT_CASE(kunit_test_assert_is_str_literal),
KUNIT_CASE(kunit_test_assert_prologue),
KUNIT_CASE(kunit_test_assert_print_msg),
KUNIT_CASE(kunit_test_unary_assert_format),
KUNIT_CASE(kunit_test_ptr_not_err_assert_format),
KUNIT_CASE(kunit_test_binary_assert_format),
KUNIT_CASE(kunit_test_binary_ptr_assert_format),
KUNIT_CASE(kunit_test_binary_str_assert_format),
KUNIT_CASE(kunit_test_assert_hexdump),
KUNIT_CASE(kunit_test_mem_assert_format),
{}
-};
-static struct kunit_suite assert_test_suite = {
.name = "kunit-assert",
.test_cases = assert_test_cases,
-};
-kunit_test_suites(&assert_test_suite); diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index d86f7cb3b3e4..71a3edde2ff4 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -849,10 +849,387 @@ static struct kunit_suite kunit_current_test_suite = { .test_cases = kunit_current_test_cases, };
+#define TEST_PTR_EXPECTED_BUF_SIZE 32 +#define HEXDUMP_TEST_BUF_LEN 5 +#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) +#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr))
+static void kunit_test_assert_is_literal(struct kunit *test) +{
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("5", 5));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("0", 0));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("1234567890", 1234567890));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_literal("-1234567890", -1234567890));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("05", 5));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("-0", 0));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_literal("12#45", 1245));
+}
+static void kunit_test_assert_is_str_literal(struct kunit *test) +{
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"Hello, World!\"", "Hello, World!"));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"", ""));
KUNIT_EXPECT_TRUE(test, kunit_assert_is_str_literal("\"\"\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("", ""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"", "\""));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("Abacaba\"", "Abacaba"));
KUNIT_EXPECT_FALSE(test, kunit_assert_is_str_literal("\"Abacaba\"", "\"Abacaba\""));
+}
+/* this function is used to get a "char *" string from the string stream and defer its cleanup */ +static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) +{
char *str = string_stream_get_string(stream);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str);
kunit_add_action(test, kfree_wrapper, (void *)str);
return str;
+}
+static void kunit_test_assert_prologue(struct kunit *test) +{
struct string_stream *stream;
char *str;
const struct kunit_loc location = {
.file = "testfile.c",
.line = 1337,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Test an expectation fail prologue */
kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream);
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337");
/* Test an assertion fail prologue */
string_stream_clear(stream);
kunit_assert_prologue(&location, KUNIT_ASSERTION, stream);
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c");
ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337");
+}
+/*
- This function accepts an arbitrary count of parameters and generates a va_format struct,
- which can be used to validate kunit_assert_print_msg function
- */
+static void verify_assert_print_msg(struct kunit *test,
struct string_stream *stream,
char *expected, const char *format, ...)
+{
va_list list;
const struct va_format vformat = {
.fmt = format,
.va = &list,
};
va_start(list, format);
string_stream_clear(stream);
kunit_assert_print_msg(&vformat, stream);
KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected);
+}
+static void kunit_test_assert_print_msg(struct kunit *test) +{
struct string_stream *stream;
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
verify_assert_print_msg(test, stream, "\nTest", "Test");
verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u",
"Abacaba", -123, 234U);
verify_assert_print_msg(test, stream, "", NULL);
+}
+/*
- Further code contains the tests for different assert format functions.
- This helper function accepts the assert format function, executes it and
- validates the result string from the stream by checking that all of the
- substrings exist in the output.
- */
+static void validate_assert(assert_format_t format_func, struct kunit *test,
const struct kunit_assert *assert,
struct string_stream *stream, int num_checks, ...)
+{
size_t i;
va_list checks;
char *cur_substr_exp;
struct va_format message = { NULL, NULL };
va_start(checks, num_checks);
string_stream_clear(stream);
format_func(assert, &message, stream);
for (i = 0; i < num_checks; i++) {
cur_substr_exp = va_arg(checks, char *);
ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp);
}
+}
+static void kunit_test_unary_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_unary_assert un_assert = {
.assert = assert,
.condition = "expr",
.expected_true = true,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_unary_assert_format, test, &un_assert.assert,
stream, 2, "true", "is false");
un_assert.expected_true = false;
validate_assert(kunit_unary_assert_format, test, &un_assert.assert,
stream, 2, "false", "is true");
+}
+static void kunit_test_ptr_not_err_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_ptr_not_err_assert not_err_assert = {
.assert = assert,
.text = "expr",
.value = NULL,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Value is NULL. The corresponding message should be printed out */
validate_assert(kunit_ptr_not_err_assert_format, test,
¬_err_assert.assert,
stream, 1, "null");
/* Value is not NULL, but looks like an error pointer. Error should be printed out */
not_err_assert.value = (void *)-12;
validate_assert(kunit_ptr_not_err_assert_format, test,
¬_err_assert.assert, stream, 2,
"error", "-12");
+}
+static void kunit_test_binary_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct kunit_assert assert = {};
struct kunit_binary_assert_text text = {
.left_text = "1 + 2",
.operation = "==",
.right_text = "2",
};
const struct kunit_binary_assert binary_assert = {
.assert = assert,
.text = &text,
.left_value = 3,
.right_value = 2,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/*
* Printed values should depend on the input we provide: the left text, right text, left
* value and the right value.
*/
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 4, "1 + 2", "2", "3", "==");
text.right_text = "4 - 2";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 3, "==", "1 + 2", "4 - 2");
text.left_text = "3";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 4, "3", "4 - 2", "2", "==");
text.right_text = "2";
validate_assert(kunit_binary_assert_format, test, &binary_assert.assert,
stream, 3, "3", "2", "==");
+}
+static void kunit_test_binary_ptr_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct kunit_assert assert = {};
char *addr_var_a, *addr_var_b;
static const void *var_a = (void *)0xDEADBEEF;
static const void *var_b = (void *)0xBADDCAFE;
struct kunit_binary_assert_text text = {
.left_text = "var_a",
.operation = "==",
.right_text = "var_b",
};
struct kunit_binary_ptr_assert binary_ptr_assert = {
.assert = assert,
.text = &text,
.left_value = var_a,
.right_value = var_b,
};
addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a);
addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b);
/*
* Print the addresses to the buffers first.
* This is necessary as we may have different count of leading zeros in the pointer
* on different architectures.
*/
snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a);
snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b);
This use of %px is flagged here as a checkpatch warning. However, since this functionality would not work if we just used %p, I think we can make the case that the use of %px is necessary here. Shuah, let me know what you think?
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert,
stream, 3, addr_var_a, addr_var_b, "==");
+}
+static void kunit_test_binary_str_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct kunit_assert assert = {};
static const char *var_a = "abacaba";
static const char *var_b = "kernel";
struct kunit_binary_assert_text text = {
.left_text = "var_a",
.operation = "==",
.right_text = "var_b",
};
struct kunit_binary_str_assert binary_str_assert = {
.assert = assert,
.text = &text,
.left_value = var_a,
.right_value = var_b,
};
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
validate_assert(kunit_binary_str_assert_format, test,
&binary_str_assert.assert,
stream, 5, "var_a", "var_b", "\"abacaba\"",
"\"kernel\"", "==");
text.left_text = "\"abacaba\"";
validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert,
stream, 4, "\"abacaba\"", "var_b", "\"kernel\"", "==");
text.right_text = "\"kernel\"";
validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert,
stream, 3, "\"abacaba\"", "\"kernel\"", "==");
+}
+static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55,
0x45, 0x9d, 0x47, 0xd6, 0x47,
0x2, 0x89, 0x8c, 0x81, 0x94,
0x12, 0xfe, 0x01 };
+static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55,
0x45, 0x9d, 0x47, 0x21, 0x47,
0xcd, 0x89, 0x24, 0x50, 0x94,
0x12, 0xba, 0x01 };
+static void kunit_test_assert_hexdump(struct kunit *test) +{
struct string_stream *stream;
char *str;
size_t i;
char buf[HEXDUMP_TEST_BUF_LEN];
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* Check that we are getting output like <xx> for non-matching numbers. */
kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1));
str = get_str_from_stream(test, stream);
for (i = 0; i < sizeof(hex_testbuf1); i++) {
snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]);
if (hex_testbuf1[i] != hex_testbuf2[i])
ASSERT_TEST_EXPECT_CONTAIN(test, str, buf);
}
/* We shouldn't get any <xx> numbers when comparing the buffer with itself. */
string_stream_clear(stream);
kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1));
str = get_str_from_stream(test, stream);
ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<");
ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">");
+}
+static void kunit_test_mem_assert_format(struct kunit *test) +{
struct string_stream *stream;
struct string_stream *expected_stream;
struct kunit_assert assert = {};
static const struct kunit_binary_assert_text text = {
.left_text = "hex_testbuf1",
.operation = "==",
.right_text = "hex_testbuf2",
};
struct kunit_mem_assert mem_assert = {
.assert = assert,
.text = &text,
.left_value = NULL,
.right_value = hex_testbuf2,
.size = sizeof(hex_testbuf1),
};
expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream);
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
/* The left value is NULL */
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 2, "hex_testbuf1", "is not null");
/* The right value is NULL, the left value is not NULL */
mem_assert.left_value = hex_testbuf1;
mem_assert.right_value = NULL;
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 2, "hex_testbuf2", "is not null");
/* Both arguments are not null */
mem_assert.left_value = hex_testbuf1;
mem_assert.right_value = hex_testbuf2;
validate_assert(kunit_mem_assert_format, test, &mem_assert.assert,
stream, 3, "hex_testbuf1", "hex_testbuf2", "==");
+}
+static struct kunit_case kunit_assert_test_cases[] = {
KUNIT_CASE(kunit_test_assert_is_literal),
KUNIT_CASE(kunit_test_assert_is_str_literal),
KUNIT_CASE(kunit_test_assert_prologue),
KUNIT_CASE(kunit_test_assert_print_msg),
KUNIT_CASE(kunit_test_unary_assert_format),
KUNIT_CASE(kunit_test_ptr_not_err_assert_format),
KUNIT_CASE(kunit_test_binary_assert_format),
KUNIT_CASE(kunit_test_binary_ptr_assert_format),
KUNIT_CASE(kunit_test_binary_str_assert_format),
KUNIT_CASE(kunit_test_assert_hexdump),
KUNIT_CASE(kunit_test_mem_assert_format),
{}
+};
+static struct kunit_suite kunit_assert_test_suite = {
.name = "kunit-assert",
.test_cases = kunit_assert_test_cases,
+};
kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, &kunit_current_test_suite, &kunit_device_test_suite,
&kunit_fault_test_suite);
&kunit_fault_test_suite, &kunit_assert_test_suite);
MODULE_DESCRIPTION("KUnit test for core test infrastructure"); MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); -- 2.34.1
On 6/21/24 22:38, Rae Moar wrote:
On Tue, Jun 18, 2024 at 1:03 PM Ivan Orlov ivan.orlov0322@gmail.com wrote:
Since assert_test covers the part of the KUnit core (the assertion formatting functions), I believe it would be better to have it merged into kunit-test (as it is done for other tests for the KUnit core).
Signed-off-by: Ivan Orlov ivan.orlov0322@gmail.com
Hello!
This looks good to me. I don't know if it was necessary to move the assert tests but I definitely see the reasoning. Happy with this as it is. There are a few checkpatch warnings I have mentioned below but I think the use case makes it necessary.
Reviewed-by: Rae Moar rmoar@google.com
Thanks! -Rae
Hi Rae,
Thank you so much for the review, and sorry for the late reply ( I've been on vacation this week, and didn't have access to my inbox :( ).
And yes, since we want to get the actual pointer address "%px" is our only option :(
linux-kselftest-mirror@lists.linaro.org