12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <kunit/test.h>
- #include <linux/string.h>
- static const char array_of_10[] = "this is 10";
- static const char *ptr_of_11 = "this is 11!";
- static char array_unknown[] = "compiler thinks I might change";
- static void known_sizes_test(struct kunit *test)
- {
- KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
- KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10);
- KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11);
- KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX);
-
- KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX);
- }
- static volatile int pick;
- static noinline size_t want_minus_one(int pick)
- {
- const char *str;
- switch (pick) {
- case 1:
- str = "4444";
- break;
- case 2:
- str = "333";
- break;
- default:
- str = "1";
- break;
- }
- return __compiletime_strlen(str);
- }
- static void control_flow_split_test(struct kunit *test)
- {
- KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX);
- }
- static struct kunit_case fortify_test_cases[] = {
- KUNIT_CASE(known_sizes_test),
- KUNIT_CASE(control_flow_split_test),
- {}
- };
- static struct kunit_suite fortify_test_suite = {
- .name = "fortify",
- .test_cases = fortify_test_cases,
- };
- kunit_test_suite(fortify_test_suite);
- MODULE_LICENSE("GPL");
|