1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * KUnit test for the KUnit executor. 4 * 5 * Copyright (C) 2021, Google LLC. 6 * Author: Daniel Latypov <dlatypov@google.com> 7 */ 8 9 #include <kunit/test.h> 10 11 static void kfree_at_end(struct kunit *test, const void *to_free); 12 static struct kunit_suite *alloc_fake_suite(struct kunit *test, 13 const char *suite_name); 14 15 static void filter_subsuite_test(struct kunit *test) 16 { 17 struct kunit_suite *subsuite[3] = {NULL, NULL, NULL}; 18 struct kunit_suite * const *filtered; 19 20 subsuite[0] = alloc_fake_suite(test, "suite1"); 21 subsuite[1] = alloc_fake_suite(test, "suite2"); 22 23 /* Want: suite1, suite2, NULL -> suite2, NULL */ 24 filtered = kunit_filter_subsuite(subsuite, "suite2*"); 25 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered); 26 kfree_at_end(test, filtered); 27 28 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered[0]); 29 KUNIT_EXPECT_STREQ(test, (const char *)filtered[0]->name, "suite2"); 30 31 KUNIT_EXPECT_FALSE(test, filtered[1]); 32 } 33 34 static void filter_subsuite_to_empty_test(struct kunit *test) 35 { 36 struct kunit_suite *subsuite[3] = {NULL, NULL, NULL}; 37 struct kunit_suite * const *filtered; 38 39 subsuite[0] = alloc_fake_suite(test, "suite1"); 40 subsuite[1] = alloc_fake_suite(test, "suite2"); 41 42 filtered = kunit_filter_subsuite(subsuite, "not_found"); 43 kfree_at_end(test, filtered); /* just in case */ 44 45 KUNIT_EXPECT_FALSE_MSG(test, filtered, 46 "should be NULL to indicate no match"); 47 } 48 49 static void kfree_subsuites_at_end(struct kunit *test, struct suite_set *suite_set) 50 { 51 struct kunit_suite * const * const *suites; 52 53 kfree_at_end(test, suite_set->start); 54 for (suites = suite_set->start; suites < suite_set->end; suites++) 55 kfree_at_end(test, *suites); 56 } 57 58 static void filter_suites_test(struct kunit *test) 59 { 60 /* Suites per-file are stored as a NULL terminated array */ 61 struct kunit_suite *subsuites[2][2] = { 62 {NULL, NULL}, 63 {NULL, NULL}, 64 }; 65 /* Match the memory layout of suite_set */ 66 struct kunit_suite * const * const suites[2] = { 67 subsuites[0], subsuites[1], 68 }; 69 70 const struct suite_set suite_set = { 71 .start = suites, 72 .end = suites + 2, 73 }; 74 struct suite_set filtered = {.start = NULL, .end = NULL}; 75 76 /* Emulate two files, each having one suite */ 77 subsuites[0][0] = alloc_fake_suite(test, "suite0"); 78 subsuites[1][0] = alloc_fake_suite(test, "suite1"); 79 80 /* Filter out suite1 */ 81 filtered = kunit_filter_suites(&suite_set, "suite0"); 82 kfree_subsuites_at_end(test, &filtered); /* let us use ASSERTs without leaking */ 83 KUNIT_ASSERT_EQ(test, filtered.end - filtered.start, (ptrdiff_t)1); 84 85 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start); 86 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start[0]); 87 KUNIT_EXPECT_STREQ(test, (const char *)filtered.start[0][0]->name, "suite0"); 88 } 89 90 static struct kunit_case executor_test_cases[] = { 91 KUNIT_CASE(filter_subsuite_test), 92 KUNIT_CASE(filter_subsuite_to_empty_test), 93 KUNIT_CASE(filter_suites_test), 94 {} 95 }; 96 97 static struct kunit_suite executor_test_suite = { 98 .name = "kunit_executor_test", 99 .test_cases = executor_test_cases, 100 }; 101 102 kunit_test_suites(&executor_test_suite); 103 104 /* Test helpers */ 105 106 static void kfree_res_free(struct kunit_resource *res) 107 { 108 kfree(res->data); 109 } 110 111 /* Use the resource API to register a call to kfree(to_free). 112 * Since we never actually use the resource, it's safe to use on const data. 113 */ 114 static void kfree_at_end(struct kunit *test, const void *to_free) 115 { 116 /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */ 117 if (IS_ERR_OR_NULL(to_free)) 118 return; 119 kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL, 120 (void *)to_free); 121 } 122 123 static struct kunit_suite *alloc_fake_suite(struct kunit *test, 124 const char *suite_name) 125 { 126 struct kunit_suite *suite; 127 128 /* We normally never expect to allocate suites, hence the non-const cast. */ 129 suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL); 130 strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1); 131 132 return suite; 133 } 134