~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/kunit/kunit-test.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/kunit/kunit-test.c (Architecture sparc) and /lib/kunit/kunit-test.c (Architecture m68k)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * KUnit test for core test infrastructure.         3  * KUnit test for core test infrastructure.
  4  *                                                  4  *
  5  * Copyright (C) 2019, Google LLC.                  5  * Copyright (C) 2019, Google LLC.
  6  * Author: Brendan Higgins <brendanhiggins@goo      6  * Author: Brendan Higgins <brendanhiggins@google.com>
  7  */                                                 7  */
  8 #include "linux/gfp_types.h"                        8 #include "linux/gfp_types.h"
  9 #include <kunit/test.h>                             9 #include <kunit/test.h>
 10 #include <kunit/test-bug.h>                        10 #include <kunit/test-bug.h>
 11                                                    11 
 12 #include <linux/device.h>                          12 #include <linux/device.h>
 13 #include <kunit/device.h>                          13 #include <kunit/device.h>
 14                                                    14 
 15 #include "string-stream.h"                         15 #include "string-stream.h"
 16 #include "try-catch-impl.h"                        16 #include "try-catch-impl.h"
 17                                                    17 
 18 struct kunit_try_catch_test_context {              18 struct kunit_try_catch_test_context {
 19         struct kunit_try_catch *try_catch;         19         struct kunit_try_catch *try_catch;
 20         bool function_called;                      20         bool function_called;
 21 };                                                 21 };
 22                                                    22 
 23 static void kunit_test_successful_try(void *da     23 static void kunit_test_successful_try(void *data)
 24 {                                                  24 {
 25         struct kunit *test = data;                 25         struct kunit *test = data;
 26         struct kunit_try_catch_test_context *c     26         struct kunit_try_catch_test_context *ctx = test->priv;
 27                                                    27 
 28         ctx->function_called = true;               28         ctx->function_called = true;
 29 }                                                  29 }
 30                                                    30 
 31 static void kunit_test_no_catch(void *data)        31 static void kunit_test_no_catch(void *data)
 32 {                                                  32 {
 33         struct kunit *test = data;                 33         struct kunit *test = data;
 34                                                    34 
 35         KUNIT_FAIL(test, "Catch should not be      35         KUNIT_FAIL(test, "Catch should not be called\n");
 36 }                                                  36 }
 37                                                    37 
 38 static void kunit_test_try_catch_successful_tr     38 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
 39 {                                                  39 {
 40         struct kunit_try_catch_test_context *c     40         struct kunit_try_catch_test_context *ctx = test->priv;
 41         struct kunit_try_catch *try_catch = ct     41         struct kunit_try_catch *try_catch = ctx->try_catch;
 42                                                    42 
 43         kunit_try_catch_init(try_catch,            43         kunit_try_catch_init(try_catch,
 44                              test,                 44                              test,
 45                              kunit_test_succes     45                              kunit_test_successful_try,
 46                              kunit_test_no_cat     46                              kunit_test_no_catch);
 47         kunit_try_catch_run(try_catch, test);      47         kunit_try_catch_run(try_catch, test);
 48                                                    48 
 49         KUNIT_EXPECT_TRUE(test, ctx->function_     49         KUNIT_EXPECT_TRUE(test, ctx->function_called);
 50 }                                                  50 }
 51                                                    51 
 52 static void kunit_test_unsuccessful_try(void *     52 static void kunit_test_unsuccessful_try(void *data)
 53 {                                                  53 {
 54         struct kunit *test = data;                 54         struct kunit *test = data;
 55         struct kunit_try_catch_test_context *c     55         struct kunit_try_catch_test_context *ctx = test->priv;
 56         struct kunit_try_catch *try_catch = ct     56         struct kunit_try_catch *try_catch = ctx->try_catch;
 57                                                    57 
 58         kunit_try_catch_throw(try_catch);          58         kunit_try_catch_throw(try_catch);
 59         KUNIT_FAIL(test, "This line should nev     59         KUNIT_FAIL(test, "This line should never be reached\n");
 60 }                                                  60 }
 61                                                    61 
 62 static void kunit_test_catch(void *data)           62 static void kunit_test_catch(void *data)
 63 {                                                  63 {
 64         struct kunit *test = data;                 64         struct kunit *test = data;
 65         struct kunit_try_catch_test_context *c     65         struct kunit_try_catch_test_context *ctx = test->priv;
 66                                                    66 
 67         ctx->function_called = true;               67         ctx->function_called = true;
 68 }                                                  68 }
 69                                                    69 
 70 static void kunit_test_try_catch_unsuccessful_     70 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
 71 {                                                  71 {
 72         struct kunit_try_catch_test_context *c     72         struct kunit_try_catch_test_context *ctx = test->priv;
 73         struct kunit_try_catch *try_catch = ct     73         struct kunit_try_catch *try_catch = ctx->try_catch;
 74                                                    74 
 75         kunit_try_catch_init(try_catch,            75         kunit_try_catch_init(try_catch,
 76                              test,                 76                              test,
 77                              kunit_test_unsucc     77                              kunit_test_unsuccessful_try,
 78                              kunit_test_catch)     78                              kunit_test_catch);
 79         kunit_try_catch_run(try_catch, test);      79         kunit_try_catch_run(try_catch, test);
 80                                                    80 
 81         KUNIT_EXPECT_TRUE(test, ctx->function_     81         KUNIT_EXPECT_TRUE(test, ctx->function_called);
 82 }                                                  82 }
 83                                                    83 
 84 static int kunit_try_catch_test_init(struct ku     84 static int kunit_try_catch_test_init(struct kunit *test)
 85 {                                                  85 {
 86         struct kunit_try_catch_test_context *c     86         struct kunit_try_catch_test_context *ctx;
 87                                                    87 
 88         ctx = kunit_kzalloc(test, sizeof(*ctx)     88         ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
 89         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx     89         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
 90         test->priv = ctx;                          90         test->priv = ctx;
 91                                                    91 
 92         ctx->try_catch = kunit_kmalloc(test,       92         ctx->try_catch = kunit_kmalloc(test,
 93                                        sizeof(     93                                        sizeof(*ctx->try_catch),
 94                                        GFP_KER     94                                        GFP_KERNEL);
 95         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx     95         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
 96                                                    96 
 97         return 0;                                  97         return 0;
 98 }                                                  98 }
 99                                                    99 
100 static struct kunit_case kunit_try_catch_test_    100 static struct kunit_case kunit_try_catch_test_cases[] = {
101         KUNIT_CASE(kunit_test_try_catch_succes    101         KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
102         KUNIT_CASE(kunit_test_try_catch_unsucc    102         KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
103         {}                                        103         {}
104 };                                                104 };
105                                                   105 
106 static struct kunit_suite kunit_try_catch_test    106 static struct kunit_suite kunit_try_catch_test_suite = {
107         .name = "kunit-try-catch-test",           107         .name = "kunit-try-catch-test",
108         .init = kunit_try_catch_test_init,        108         .init = kunit_try_catch_test_init,
109         .test_cases = kunit_try_catch_test_cas    109         .test_cases = kunit_try_catch_test_cases,
110 };                                                110 };
111                                                   111 
112 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)           112 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
113                                                   113 
114 static void kunit_test_null_dereference(void *    114 static void kunit_test_null_dereference(void *data)
115 {                                                 115 {
116         struct kunit *test = data;                116         struct kunit *test = data;
117         int *null = NULL;                         117         int *null = NULL;
118                                                   118 
119         *null = 0;                                119         *null = 0;
120                                                   120 
121         KUNIT_FAIL(test, "This line should nev    121         KUNIT_FAIL(test, "This line should never be reached\n");
122 }                                                 122 }
123                                                   123 
124 static void kunit_test_fault_null_dereference(    124 static void kunit_test_fault_null_dereference(struct kunit *test)
125 {                                                 125 {
126         struct kunit_try_catch_test_context *c    126         struct kunit_try_catch_test_context *ctx = test->priv;
127         struct kunit_try_catch *try_catch = ct    127         struct kunit_try_catch *try_catch = ctx->try_catch;
128                                                   128 
129         kunit_try_catch_init(try_catch,           129         kunit_try_catch_init(try_catch,
130                              test,                130                              test,
131                              kunit_test_null_d    131                              kunit_test_null_dereference,
132                              kunit_test_catch)    132                              kunit_test_catch);
133         kunit_try_catch_run(try_catch, test);     133         kunit_try_catch_run(try_catch, test);
134                                                   134 
135         KUNIT_EXPECT_EQ(test, try_catch->try_r    135         KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
136         KUNIT_EXPECT_TRUE(test, ctx->function_    136         KUNIT_EXPECT_TRUE(test, ctx->function_called);
137 }                                                 137 }
138                                                   138 
139 #endif /* CONFIG_KUNIT_FAULT_TEST */              139 #endif /* CONFIG_KUNIT_FAULT_TEST */
140                                                   140 
141 static struct kunit_case kunit_fault_test_case    141 static struct kunit_case kunit_fault_test_cases[] = {
142 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)           142 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
143         KUNIT_CASE(kunit_test_fault_null_deref    143         KUNIT_CASE(kunit_test_fault_null_dereference),
144 #endif /* CONFIG_KUNIT_FAULT_TEST */              144 #endif /* CONFIG_KUNIT_FAULT_TEST */
145         {}                                        145         {}
146 };                                                146 };
147                                                   147 
148 static struct kunit_suite kunit_fault_test_sui    148 static struct kunit_suite kunit_fault_test_suite = {
149         .name = "kunit_fault",                    149         .name = "kunit_fault",
150         .init = kunit_try_catch_test_init,        150         .init = kunit_try_catch_test_init,
151         .test_cases = kunit_fault_test_cases,     151         .test_cases = kunit_fault_test_cases,
152 };                                                152 };
153                                                   153 
154 /*                                                154 /*
155  * Context for testing test managed resources     155  * Context for testing test managed resources
156  * is_resource_initialized is used to test arb    156  * is_resource_initialized is used to test arbitrary resources
157  */                                               157  */
158 struct kunit_test_resource_context {              158 struct kunit_test_resource_context {
159         struct kunit test;                        159         struct kunit test;
160         bool is_resource_initialized;             160         bool is_resource_initialized;
161         int allocate_order[2];                    161         int allocate_order[2];
162         int free_order[4];                        162         int free_order[4];
163 };                                                163 };
164                                                   164 
165 static int fake_resource_init(struct kunit_res    165 static int fake_resource_init(struct kunit_resource *res, void *context)
166 {                                                 166 {
167         struct kunit_test_resource_context *ct    167         struct kunit_test_resource_context *ctx = context;
168                                                   168 
169         res->data = &ctx->is_resource_initiali    169         res->data = &ctx->is_resource_initialized;
170         ctx->is_resource_initialized = true;      170         ctx->is_resource_initialized = true;
171         return 0;                                 171         return 0;
172 }                                                 172 }
173                                                   173 
174 static void fake_resource_free(struct kunit_re    174 static void fake_resource_free(struct kunit_resource *res)
175 {                                                 175 {
176         bool *is_resource_initialized = res->d    176         bool *is_resource_initialized = res->data;
177                                                   177 
178         *is_resource_initialized = false;         178         *is_resource_initialized = false;
179 }                                                 179 }
180                                                   180 
181 static void kunit_resource_test_init_resources    181 static void kunit_resource_test_init_resources(struct kunit *test)
182 {                                                 182 {
183         struct kunit_test_resource_context *ct    183         struct kunit_test_resource_context *ctx = test->priv;
184                                                   184 
185         kunit_init_test(&ctx->test, "testing_t    185         kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
186                                                   186 
187         KUNIT_EXPECT_TRUE(test, list_empty(&ct    187         KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
188 }                                                 188 }
189                                                   189 
190 static void kunit_resource_test_alloc_resource    190 static void kunit_resource_test_alloc_resource(struct kunit *test)
191 {                                                 191 {
192         struct kunit_test_resource_context *ct    192         struct kunit_test_resource_context *ctx = test->priv;
193         struct kunit_resource *res;               193         struct kunit_resource *res;
194         kunit_resource_free_t free = fake_reso    194         kunit_resource_free_t free = fake_resource_free;
195                                                   195 
196         res = kunit_alloc_and_get_resource(&ct    196         res = kunit_alloc_and_get_resource(&ctx->test,
197                                            fak    197                                            fake_resource_init,
198                                            fak    198                                            fake_resource_free,
199                                            GFP    199                                            GFP_KERNEL,
200                                            ctx    200                                            ctx);
201                                                   201 
202         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res    202         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
203         KUNIT_EXPECT_PTR_EQ(test,                 203         KUNIT_EXPECT_PTR_EQ(test,
204                             &ctx->is_resource_    204                             &ctx->is_resource_initialized,
205                             (bool *)res->data)    205                             (bool *)res->data);
206         KUNIT_EXPECT_TRUE(test, list_is_last(&    206         KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
207         KUNIT_EXPECT_PTR_EQ(test, free, res->f    207         KUNIT_EXPECT_PTR_EQ(test, free, res->free);
208                                                   208 
209         kunit_put_resource(res);                  209         kunit_put_resource(res);
210 }                                                 210 }
211                                                   211 
212 static inline bool kunit_resource_instance_mat    212 static inline bool kunit_resource_instance_match(struct kunit *test,
213                                                   213                                                  struct kunit_resource *res,
214                                                   214                                                  void *match_data)
215 {                                                 215 {
216         return res->data == match_data;           216         return res->data == match_data;
217 }                                                 217 }
218                                                   218 
219 /*                                                219 /*
220  * Note: tests below use kunit_alloc_and_get_r    220  * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
221  * they have a reference to the associated res    221  * they have a reference to the associated resource that they must release
222  * via kunit_put_resource().  In normal operat    222  * via kunit_put_resource().  In normal operation, users will only
223  * have to do this for cases where they use ku    223  * have to do this for cases where they use kunit_find_resource(), and the
224  * kunit_alloc_resource() function will be use    224  * kunit_alloc_resource() function will be used (which does not take a
225  * resource reference).                           225  * resource reference).
226  */                                               226  */
227 static void kunit_resource_test_destroy_resour    227 static void kunit_resource_test_destroy_resource(struct kunit *test)
228 {                                                 228 {
229         struct kunit_test_resource_context *ct    229         struct kunit_test_resource_context *ctx = test->priv;
230         struct kunit_resource *res = kunit_all    230         struct kunit_resource *res = kunit_alloc_and_get_resource(
231                         &ctx->test,               231                         &ctx->test,
232                         fake_resource_init,       232                         fake_resource_init,
233                         fake_resource_free,       233                         fake_resource_free,
234                         GFP_KERNEL,               234                         GFP_KERNEL,
235                         ctx);                     235                         ctx);
236                                                   236 
237         kunit_put_resource(res);                  237         kunit_put_resource(res);
238                                                   238 
239         KUNIT_ASSERT_FALSE(test,                  239         KUNIT_ASSERT_FALSE(test,
240                            kunit_destroy_resou    240                            kunit_destroy_resource(&ctx->test,
241                                                   241                                                   kunit_resource_instance_match,
242                                                   242                                                   res->data));
243                                                   243 
244         KUNIT_EXPECT_FALSE(test, ctx->is_resou    244         KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
245         KUNIT_EXPECT_TRUE(test, list_empty(&ct    245         KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
246 }                                                 246 }
247                                                   247 
248 static void kunit_resource_test_remove_resourc    248 static void kunit_resource_test_remove_resource(struct kunit *test)
249 {                                                 249 {
250         struct kunit_test_resource_context *ct    250         struct kunit_test_resource_context *ctx = test->priv;
251         struct kunit_resource *res = kunit_all    251         struct kunit_resource *res = kunit_alloc_and_get_resource(
252                         &ctx->test,               252                         &ctx->test,
253                         fake_resource_init,       253                         fake_resource_init,
254                         fake_resource_free,       254                         fake_resource_free,
255                         GFP_KERNEL,               255                         GFP_KERNEL,
256                         ctx);                     256                         ctx);
257                                                   257 
258         /* The resource is in the list */         258         /* The resource is in the list */
259         KUNIT_EXPECT_FALSE(test, list_empty(&c    259         KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
260                                                   260 
261         /* Remove the resource. The pointer is    261         /* Remove the resource. The pointer is still valid, but it can't be
262          * found.                                 262          * found.
263          */                                       263          */
264         kunit_remove_resource(test, res);         264         kunit_remove_resource(test, res);
265         KUNIT_EXPECT_TRUE(test, list_empty(&ct    265         KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
266         /* We haven't been freed yet. */          266         /* We haven't been freed yet. */
267         KUNIT_EXPECT_TRUE(test, ctx->is_resour    267         KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
268                                                   268 
269         /* Removing the resource multiple time    269         /* Removing the resource multiple times is valid. */
270         kunit_remove_resource(test, res);         270         kunit_remove_resource(test, res);
271         KUNIT_EXPECT_TRUE(test, list_empty(&ct    271         KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
272         /* Despite having been removed twice (    272         /* Despite having been removed twice (from only one reference), the
273          * resource still has not been freed.     273          * resource still has not been freed.
274          */                                       274          */
275         KUNIT_EXPECT_TRUE(test, ctx->is_resour    275         KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
276                                                   276 
277         /* Free the resource. */                  277         /* Free the resource. */
278         kunit_put_resource(res);                  278         kunit_put_resource(res);
279         KUNIT_EXPECT_FALSE(test, ctx->is_resou    279         KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
280 }                                                 280 }
281                                                   281 
282 static void kunit_resource_test_cleanup_resour    282 static void kunit_resource_test_cleanup_resources(struct kunit *test)
283 {                                                 283 {
284         int i;                                    284         int i;
285         struct kunit_test_resource_context *ct    285         struct kunit_test_resource_context *ctx = test->priv;
286         struct kunit_resource *resources[5];      286         struct kunit_resource *resources[5];
287                                                   287 
288         for (i = 0; i < ARRAY_SIZE(resources);    288         for (i = 0; i < ARRAY_SIZE(resources); i++) {
289                 resources[i] = kunit_alloc_and    289                 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
290                                                   290                                                             fake_resource_init,
291                                                   291                                                             fake_resource_free,
292                                                   292                                                             GFP_KERNEL,
293                                                   293                                                             ctx);
294                 kunit_put_resource(resources[i    294                 kunit_put_resource(resources[i]);
295         }                                         295         }
296                                                   296 
297         kunit_cleanup(&ctx->test);                297         kunit_cleanup(&ctx->test);
298                                                   298 
299         KUNIT_EXPECT_TRUE(test, list_empty(&ct    299         KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
300 }                                                 300 }
301                                                   301 
302 static void kunit_resource_test_mark_order(int    302 static void kunit_resource_test_mark_order(int order_array[],
303                                            siz    303                                            size_t order_size,
304                                            int    304                                            int key)
305 {                                                 305 {
306         int i;                                    306         int i;
307                                                   307 
308         for (i = 0; i < order_size && order_ar    308         for (i = 0; i < order_size && order_array[i]; i++)
309                 ;                                 309                 ;
310                                                   310 
311         order_array[i] = key;                     311         order_array[i] = key;
312 }                                                 312 }
313                                                   313 
314 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, or    314 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key)                  \
315                 kunit_resource_test_mark_order    315                 kunit_resource_test_mark_order(ctx->order_field,               \
316                                                   316                                                ARRAY_SIZE(ctx->order_field),   \
317                                                   317                                                key)
318                                                   318 
319 static int fake_resource_2_init(struct kunit_r    319 static int fake_resource_2_init(struct kunit_resource *res, void *context)
320 {                                                 320 {
321         struct kunit_test_resource_context *ct    321         struct kunit_test_resource_context *ctx = context;
322                                                   322 
323         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, al    323         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
324                                                   324 
325         res->data = ctx;                          325         res->data = ctx;
326                                                   326 
327         return 0;                                 327         return 0;
328 }                                                 328 }
329                                                   329 
330 static void fake_resource_2_free(struct kunit_    330 static void fake_resource_2_free(struct kunit_resource *res)
331 {                                                 331 {
332         struct kunit_test_resource_context *ct    332         struct kunit_test_resource_context *ctx = res->data;
333                                                   333 
334         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, fr    334         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
335 }                                                 335 }
336                                                   336 
337 static int fake_resource_1_init(struct kunit_r    337 static int fake_resource_1_init(struct kunit_resource *res, void *context)
338 {                                                 338 {
339         struct kunit_test_resource_context *ct    339         struct kunit_test_resource_context *ctx = context;
340         struct kunit_resource *res2;              340         struct kunit_resource *res2;
341                                                   341 
342         res2 = kunit_alloc_and_get_resource(&c    342         res2 = kunit_alloc_and_get_resource(&ctx->test,
343                                             fa    343                                             fake_resource_2_init,
344                                             fa    344                                             fake_resource_2_free,
345                                             GF    345                                             GFP_KERNEL,
346                                             ct    346                                             ctx);
347                                                   347 
348         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, al    348         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
349                                                   349 
350         res->data = ctx;                          350         res->data = ctx;
351                                                   351 
352         kunit_put_resource(res2);                 352         kunit_put_resource(res2);
353                                                   353 
354         return 0;                                 354         return 0;
355 }                                                 355 }
356                                                   356 
357 static void fake_resource_1_free(struct kunit_    357 static void fake_resource_1_free(struct kunit_resource *res)
358 {                                                 358 {
359         struct kunit_test_resource_context *ct    359         struct kunit_test_resource_context *ctx = res->data;
360                                                   360 
361         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, fr    361         KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
362 }                                                 362 }
363                                                   363 
364 /*                                                364 /*
365  * TODO(brendanhiggins@google.com): replace th    365  * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
366  * order of allocation and freeing with strict    366  * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
367  * to assert allocation and freeing order when    367  * to assert allocation and freeing order when the feature becomes available.
368  */                                               368  */
369 static void kunit_resource_test_proper_free_or    369 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
370 {                                                 370 {
371         struct kunit_test_resource_context *ct    371         struct kunit_test_resource_context *ctx = test->priv;
372         struct kunit_resource *res;               372         struct kunit_resource *res;
373                                                   373 
374         /* fake_resource_1 allocates a fake_re    374         /* fake_resource_1 allocates a fake_resource_2 in its init. */
375         res = kunit_alloc_and_get_resource(&ct    375         res = kunit_alloc_and_get_resource(&ctx->test,
376                                            fak    376                                            fake_resource_1_init,
377                                            fak    377                                            fake_resource_1_free,
378                                            GFP    378                                            GFP_KERNEL,
379                                            ctx    379                                            ctx);
380                                                   380 
381         /*                                        381         /*
382          * Since fake_resource_2_init calls KU    382          * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
383          * before returning to fake_resource_1    383          * before returning to fake_resource_1_init, it should be the first to
384          * put its key in the allocate_order a    384          * put its key in the allocate_order array.
385          */                                       385          */
386         KUNIT_EXPECT_EQ(test, ctx->allocate_or    386         KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
387         KUNIT_EXPECT_EQ(test, ctx->allocate_or    387         KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
388                                                   388 
389         kunit_put_resource(res);                  389         kunit_put_resource(res);
390                                                   390 
391         kunit_cleanup(&ctx->test);                391         kunit_cleanup(&ctx->test);
392                                                   392 
393         /*                                        393         /*
394          * Because fake_resource_2 finishes al    394          * Because fake_resource_2 finishes allocation before fake_resource_1,
395          * fake_resource_1 should be freed fir    395          * fake_resource_1 should be freed first since it could depend on
396          * fake_resource_2.                       396          * fake_resource_2.
397          */                                       397          */
398         KUNIT_EXPECT_EQ(test, ctx->free_order[    398         KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
399         KUNIT_EXPECT_EQ(test, ctx->free_order[    399         KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
400 }                                                 400 }
401                                                   401 
402 static void kunit_resource_test_static(struct     402 static void kunit_resource_test_static(struct kunit *test)
403 {                                                 403 {
404         struct kunit_test_resource_context ctx    404         struct kunit_test_resource_context ctx;
405         struct kunit_resource res;                405         struct kunit_resource res;
406                                                   406 
407         KUNIT_EXPECT_EQ(test, kunit_add_resour    407         KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
408                         0);                       408                         0);
409                                                   409 
410         KUNIT_EXPECT_PTR_EQ(test, res.data, (v    410         KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
411                                                   411 
412         kunit_cleanup(test);                      412         kunit_cleanup(test);
413                                                   413 
414         KUNIT_EXPECT_TRUE(test, list_empty(&te    414         KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
415 }                                                 415 }
416                                                   416 
417 static void kunit_resource_test_named(struct k    417 static void kunit_resource_test_named(struct kunit *test)
418 {                                                 418 {
419         struct kunit_resource res1, res2, *fou    419         struct kunit_resource res1, res2, *found = NULL;
420         struct kunit_test_resource_context ctx    420         struct kunit_test_resource_context ctx;
421                                                   421 
422         KUNIT_EXPECT_EQ(test,                     422         KUNIT_EXPECT_EQ(test,
423                         kunit_add_named_resour    423                         kunit_add_named_resource(test, NULL, NULL, &res1,
424                                                   424                                                  "resource_1", &ctx),
425                         0);                       425                         0);
426         KUNIT_EXPECT_PTR_EQ(test, res1.data, (    426         KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
427                                                   427 
428         KUNIT_EXPECT_EQ(test,                     428         KUNIT_EXPECT_EQ(test,
429                         kunit_add_named_resour    429                         kunit_add_named_resource(test, NULL, NULL, &res1,
430                                                   430                                                  "resource_1", &ctx),
431                         -EEXIST);                 431                         -EEXIST);
432                                                   432 
433         KUNIT_EXPECT_EQ(test,                     433         KUNIT_EXPECT_EQ(test,
434                         kunit_add_named_resour    434                         kunit_add_named_resource(test, NULL, NULL, &res2,
435                                                   435                                                  "resource_2", &ctx),
436                         0);                       436                         0);
437                                                   437 
438         found = kunit_find_named_resource(test    438         found = kunit_find_named_resource(test, "resource_1");
439                                                   439 
440         KUNIT_EXPECT_PTR_EQ(test, found, &res1    440         KUNIT_EXPECT_PTR_EQ(test, found, &res1);
441                                                   441 
442         if (found)                                442         if (found)
443                 kunit_put_resource(&res1);        443                 kunit_put_resource(&res1);
444                                                   444 
445         KUNIT_EXPECT_EQ(test, kunit_destroy_na    445         KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
446                         0);                       446                         0);
447                                                   447 
448         kunit_cleanup(test);                      448         kunit_cleanup(test);
449                                                   449 
450         KUNIT_EXPECT_TRUE(test, list_empty(&te    450         KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
451 }                                                 451 }
452                                                   452 
453 static void increment_int(void *ctx)              453 static void increment_int(void *ctx)
454 {                                                 454 {
455         int *i = (int *)ctx;                      455         int *i = (int *)ctx;
456         (*i)++;                                   456         (*i)++;
457 }                                                 457 }
458                                                   458 
459 static void kunit_resource_test_action(struct     459 static void kunit_resource_test_action(struct kunit *test)
460 {                                                 460 {
461         int num_actions = 0;                      461         int num_actions = 0;
462                                                   462 
463         kunit_add_action(test, increment_int,     463         kunit_add_action(test, increment_int, &num_actions);
464         KUNIT_EXPECT_EQ(test, num_actions, 0);    464         KUNIT_EXPECT_EQ(test, num_actions, 0);
465         kunit_cleanup(test);                      465         kunit_cleanup(test);
466         KUNIT_EXPECT_EQ(test, num_actions, 1);    466         KUNIT_EXPECT_EQ(test, num_actions, 1);
467                                                   467 
468         /* Once we've cleaned up, the action q    468         /* Once we've cleaned up, the action queue is empty. */
469         kunit_cleanup(test);                      469         kunit_cleanup(test);
470         KUNIT_EXPECT_EQ(test, num_actions, 1);    470         KUNIT_EXPECT_EQ(test, num_actions, 1);
471                                                   471 
472         /* Check the same function can be defe    472         /* Check the same function can be deferred multiple times. */
473         kunit_add_action(test, increment_int,     473         kunit_add_action(test, increment_int, &num_actions);
474         kunit_add_action(test, increment_int,     474         kunit_add_action(test, increment_int, &num_actions);
475         kunit_cleanup(test);                      475         kunit_cleanup(test);
476         KUNIT_EXPECT_EQ(test, num_actions, 3);    476         KUNIT_EXPECT_EQ(test, num_actions, 3);
477 }                                                 477 }
478 static void kunit_resource_test_remove_action(    478 static void kunit_resource_test_remove_action(struct kunit *test)
479 {                                                 479 {
480         int num_actions = 0;                      480         int num_actions = 0;
481                                                   481 
482         kunit_add_action(test, increment_int,     482         kunit_add_action(test, increment_int, &num_actions);
483         KUNIT_EXPECT_EQ(test, num_actions, 0);    483         KUNIT_EXPECT_EQ(test, num_actions, 0);
484                                                   484 
485         kunit_remove_action(test, increment_in    485         kunit_remove_action(test, increment_int, &num_actions);
486         kunit_cleanup(test);                      486         kunit_cleanup(test);
487         KUNIT_EXPECT_EQ(test, num_actions, 0);    487         KUNIT_EXPECT_EQ(test, num_actions, 0);
488 }                                                 488 }
489 static void kunit_resource_test_release_action    489 static void kunit_resource_test_release_action(struct kunit *test)
490 {                                                 490 {
491         int num_actions = 0;                      491         int num_actions = 0;
492                                                   492 
493         kunit_add_action(test, increment_int,     493         kunit_add_action(test, increment_int, &num_actions);
494         KUNIT_EXPECT_EQ(test, num_actions, 0);    494         KUNIT_EXPECT_EQ(test, num_actions, 0);
495         /* Runs immediately on trigger. */        495         /* Runs immediately on trigger. */
496         kunit_release_action(test, increment_i    496         kunit_release_action(test, increment_int, &num_actions);
497         KUNIT_EXPECT_EQ(test, num_actions, 1);    497         KUNIT_EXPECT_EQ(test, num_actions, 1);
498                                                   498 
499         /* Doesn't run again on test exit. */     499         /* Doesn't run again on test exit. */
500         kunit_cleanup(test);                      500         kunit_cleanup(test);
501         KUNIT_EXPECT_EQ(test, num_actions, 1);    501         KUNIT_EXPECT_EQ(test, num_actions, 1);
502 }                                                 502 }
503 static void action_order_1(void *ctx)             503 static void action_order_1(void *ctx)
504 {                                                 504 {
505         struct kunit_test_resource_context *re    505         struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
506                                                   506 
507         KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx    507         KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 1);
508         kunit_log(KERN_INFO, current->kunit_te    508         kunit_log(KERN_INFO, current->kunit_test, "action_order_1");
509 }                                                 509 }
510 static void action_order_2(void *ctx)             510 static void action_order_2(void *ctx)
511 {                                                 511 {
512         struct kunit_test_resource_context *re    512         struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
513                                                   513 
514         KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx    514         KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 2);
515         kunit_log(KERN_INFO, current->kunit_te    515         kunit_log(KERN_INFO, current->kunit_test, "action_order_2");
516 }                                                 516 }
517 static void kunit_resource_test_action_orderin    517 static void kunit_resource_test_action_ordering(struct kunit *test)
518 {                                                 518 {
519         struct kunit_test_resource_context *ct    519         struct kunit_test_resource_context *ctx = test->priv;
520                                                   520 
521         kunit_add_action(test, action_order_1,    521         kunit_add_action(test, action_order_1, ctx);
522         kunit_add_action(test, action_order_2,    522         kunit_add_action(test, action_order_2, ctx);
523         kunit_add_action(test, action_order_1,    523         kunit_add_action(test, action_order_1, ctx);
524         kunit_add_action(test, action_order_2,    524         kunit_add_action(test, action_order_2, ctx);
525         kunit_remove_action(test, action_order    525         kunit_remove_action(test, action_order_1, ctx);
526         kunit_release_action(test, action_orde    526         kunit_release_action(test, action_order_2, ctx);
527         kunit_cleanup(test);                      527         kunit_cleanup(test);
528                                                   528 
529         /* [2 is triggered] [2], [(1 is cancel    529         /* [2 is triggered] [2], [(1 is cancelled)] [1] */
530         KUNIT_EXPECT_EQ(test, ctx->free_order[    530         KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
531         KUNIT_EXPECT_EQ(test, ctx->free_order[    531         KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
532         KUNIT_EXPECT_EQ(test, ctx->free_order[    532         KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
533 }                                                 533 }
534                                                   534 
535 static int kunit_resource_test_init(struct kun    535 static int kunit_resource_test_init(struct kunit *test)
536 {                                                 536 {
537         struct kunit_test_resource_context *ct    537         struct kunit_test_resource_context *ctx =
538                         kzalloc(sizeof(*ctx),     538                         kzalloc(sizeof(*ctx), GFP_KERNEL);
539                                                   539 
540         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx    540         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
541                                                   541 
542         test->priv = ctx;                         542         test->priv = ctx;
543                                                   543 
544         kunit_init_test(&ctx->test, "test_test    544         kunit_init_test(&ctx->test, "test_test_context", NULL);
545                                                   545 
546         return 0;                                 546         return 0;
547 }                                                 547 }
548                                                   548 
549 static void kunit_resource_test_exit(struct ku    549 static void kunit_resource_test_exit(struct kunit *test)
550 {                                                 550 {
551         struct kunit_test_resource_context *ct    551         struct kunit_test_resource_context *ctx = test->priv;
552                                                   552 
553         kunit_cleanup(&ctx->test);                553         kunit_cleanup(&ctx->test);
554         kfree(ctx);                               554         kfree(ctx);
555 }                                                 555 }
556                                                   556 
557 static struct kunit_case kunit_resource_test_c    557 static struct kunit_case kunit_resource_test_cases[] = {
558         KUNIT_CASE(kunit_resource_test_init_re    558         KUNIT_CASE(kunit_resource_test_init_resources),
559         KUNIT_CASE(kunit_resource_test_alloc_r    559         KUNIT_CASE(kunit_resource_test_alloc_resource),
560         KUNIT_CASE(kunit_resource_test_destroy    560         KUNIT_CASE(kunit_resource_test_destroy_resource),
561         KUNIT_CASE(kunit_resource_test_remove_    561         KUNIT_CASE(kunit_resource_test_remove_resource),
562         KUNIT_CASE(kunit_resource_test_cleanup    562         KUNIT_CASE(kunit_resource_test_cleanup_resources),
563         KUNIT_CASE(kunit_resource_test_proper_    563         KUNIT_CASE(kunit_resource_test_proper_free_ordering),
564         KUNIT_CASE(kunit_resource_test_static)    564         KUNIT_CASE(kunit_resource_test_static),
565         KUNIT_CASE(kunit_resource_test_named),    565         KUNIT_CASE(kunit_resource_test_named),
566         KUNIT_CASE(kunit_resource_test_action)    566         KUNIT_CASE(kunit_resource_test_action),
567         KUNIT_CASE(kunit_resource_test_remove_    567         KUNIT_CASE(kunit_resource_test_remove_action),
568         KUNIT_CASE(kunit_resource_test_release    568         KUNIT_CASE(kunit_resource_test_release_action),
569         KUNIT_CASE(kunit_resource_test_action_    569         KUNIT_CASE(kunit_resource_test_action_ordering),
570         {}                                        570         {}
571 };                                                571 };
572                                                   572 
573 static struct kunit_suite kunit_resource_test_    573 static struct kunit_suite kunit_resource_test_suite = {
574         .name = "kunit-resource-test",            574         .name = "kunit-resource-test",
575         .init = kunit_resource_test_init,         575         .init = kunit_resource_test_init,
576         .exit = kunit_resource_test_exit,         576         .exit = kunit_resource_test_exit,
577         .test_cases = kunit_resource_test_case    577         .test_cases = kunit_resource_test_cases,
578 };                                                578 };
579                                                   579 
580 /*                                                580 /*
581  * Log tests call string_stream functions, whi    581  * Log tests call string_stream functions, which aren't exported. So only
582  * build this code if this test is built-in.      582  * build this code if this test is built-in.
583  */                                               583  */
584 #if IS_BUILTIN(CONFIG_KUNIT_TEST)                 584 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
585                                                   585 
586 /* This avoids a cast warning if kfree() is pa    586 /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
587 KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfr    587 KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
588                                                   588 
589 static void kunit_log_test(struct kunit *test)    589 static void kunit_log_test(struct kunit *test)
590 {                                                 590 {
591         struct kunit_suite suite;                 591         struct kunit_suite suite;
592 #ifdef CONFIG_KUNIT_DEBUGFS                       592 #ifdef CONFIG_KUNIT_DEBUGFS
593         char *full_log;                           593         char *full_log;
594 #endif                                            594 #endif
595         suite.log = kunit_alloc_string_stream(    595         suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
596         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, sui    596         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
597         string_stream_set_append_newlines(suit    597         string_stream_set_append_newlines(suite.log, true);
598                                                   598 
599         kunit_log(KERN_INFO, test, "put this i    599         kunit_log(KERN_INFO, test, "put this in log.");
600         kunit_log(KERN_INFO, test, "this too."    600         kunit_log(KERN_INFO, test, "this too.");
601         kunit_log(KERN_INFO, &suite, "add to s    601         kunit_log(KERN_INFO, &suite, "add to suite log.");
602         kunit_log(KERN_INFO, &suite, "along wi    602         kunit_log(KERN_INFO, &suite, "along with this.");
603                                                   603 
604 #ifdef CONFIG_KUNIT_DEBUGFS                       604 #ifdef CONFIG_KUNIT_DEBUGFS
605         KUNIT_EXPECT_TRUE(test, test->log->app    605         KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
606                                                   606 
607         full_log = string_stream_get_string(te    607         full_log = string_stream_get_string(test->log);
608         kunit_add_action(test, kfree_wrapper,     608         kunit_add_action(test, kfree_wrapper, full_log);
609         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,        609         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
610                                      strstr(fu    610                                      strstr(full_log, "put this in log."));
611         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,        611         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
612                                      strstr(fu    612                                      strstr(full_log, "this too."));
613                                                   613 
614         full_log = string_stream_get_string(su    614         full_log = string_stream_get_string(suite.log);
615         kunit_add_action(test, kfree_wrapper,     615         kunit_add_action(test, kfree_wrapper, full_log);
616         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,        616         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
617                                      strstr(fu    617                                      strstr(full_log, "add to suite log."));
618         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,        618         KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
619                                      strstr(fu    619                                      strstr(full_log, "along with this."));
620 #else                                             620 #else
621         KUNIT_EXPECT_NULL(test, test->log);       621         KUNIT_EXPECT_NULL(test, test->log);
622 #endif                                            622 #endif
623 }                                                 623 }
624                                                   624 
625 static void kunit_log_newline_test(struct kuni    625 static void kunit_log_newline_test(struct kunit *test)
626 {                                                 626 {
627         char *full_log;                           627         char *full_log;
628                                                   628 
629         kunit_info(test, "Add newline\n");        629         kunit_info(test, "Add newline\n");
630         if (test->log) {                          630         if (test->log) {
631                 full_log = string_stream_get_s    631                 full_log = string_stream_get_string(test->log);
632                 kunit_add_action(test, kfree_w    632                 kunit_add_action(test, kfree_wrapper, full_log);
633                 KUNIT_ASSERT_NOT_NULL_MSG(test    633                 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
634                         "Missing log line, ful    634                         "Missing log line, full log:\n%s", full_log);
635                 KUNIT_EXPECT_NULL(test, strstr    635                 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
636         } else {                                  636         } else {
637                 kunit_skip(test, "only useful     637                 kunit_skip(test, "only useful when debugfs is enabled");
638         }                                         638         }
639 }                                                 639 }
640 #else                                             640 #else
641 static void kunit_log_test(struct kunit *test)    641 static void kunit_log_test(struct kunit *test)
642 {                                                 642 {
643         kunit_skip(test, "Log tests only run w    643         kunit_skip(test, "Log tests only run when built-in");
644 }                                                 644 }
645                                                   645 
646 static void kunit_log_newline_test(struct kuni    646 static void kunit_log_newline_test(struct kunit *test)
647 {                                                 647 {
648         kunit_skip(test, "Log tests only run w    648         kunit_skip(test, "Log tests only run when built-in");
649 }                                                 649 }
650 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */        650 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
651                                                   651 
652 static struct kunit_case kunit_log_test_cases[    652 static struct kunit_case kunit_log_test_cases[] = {
653         KUNIT_CASE(kunit_log_test),               653         KUNIT_CASE(kunit_log_test),
654         KUNIT_CASE(kunit_log_newline_test),       654         KUNIT_CASE(kunit_log_newline_test),
655         {}                                        655         {}
656 };                                                656 };
657                                                   657 
658 static struct kunit_suite kunit_log_test_suite    658 static struct kunit_suite kunit_log_test_suite = {
659         .name = "kunit-log-test",                 659         .name = "kunit-log-test",
660         .test_cases = kunit_log_test_cases,       660         .test_cases = kunit_log_test_cases,
661 };                                                661 };
662                                                   662 
663 static void kunit_status_set_failure_test(stru    663 static void kunit_status_set_failure_test(struct kunit *test)
664 {                                                 664 {
665         struct kunit fake;                        665         struct kunit fake;
666                                                   666 
667         kunit_init_test(&fake, "fake test", NU    667         kunit_init_test(&fake, "fake test", NULL);
668                                                   668 
669         KUNIT_EXPECT_EQ(test, fake.status, (en    669         KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
670         kunit_set_failure(&fake);                 670         kunit_set_failure(&fake);
671         KUNIT_EXPECT_EQ(test, fake.status, (en    671         KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
672 }                                                 672 }
673                                                   673 
674 static void kunit_status_mark_skipped_test(str    674 static void kunit_status_mark_skipped_test(struct kunit *test)
675 {                                                 675 {
676         struct kunit fake;                        676         struct kunit fake;
677                                                   677 
678         kunit_init_test(&fake, "fake test", NU    678         kunit_init_test(&fake, "fake test", NULL);
679                                                   679 
680         /* Before: Should be SUCCESS with no c    680         /* Before: Should be SUCCESS with no comment. */
681         KUNIT_EXPECT_EQ(test, fake.status, KUN    681         KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
682         KUNIT_EXPECT_STREQ(test, fake.status_c    682         KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
683                                                   683 
684         /* Mark the test as skipped. */           684         /* Mark the test as skipped. */
685         kunit_mark_skipped(&fake, "Accepts for    685         kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
686                                                   686 
687         /* After: Should be SKIPPED with our c    687         /* After: Should be SKIPPED with our comment. */
688         KUNIT_EXPECT_EQ(test, fake.status, (en    688         KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
689         KUNIT_EXPECT_STREQ(test, fake.status_c    689         KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
690 }                                                 690 }
691                                                   691 
692 static struct kunit_case kunit_status_test_cas    692 static struct kunit_case kunit_status_test_cases[] = {
693         KUNIT_CASE(kunit_status_set_failure_te    693         KUNIT_CASE(kunit_status_set_failure_test),
694         KUNIT_CASE(kunit_status_mark_skipped_t    694         KUNIT_CASE(kunit_status_mark_skipped_test),
695         {}                                        695         {}
696 };                                                696 };
697                                                   697 
698 static struct kunit_suite kunit_status_test_su    698 static struct kunit_suite kunit_status_test_suite = {
699         .name = "kunit_status",                   699         .name = "kunit_status",
700         .test_cases = kunit_status_test_cases,    700         .test_cases = kunit_status_test_cases,
701 };                                                701 };
702                                                   702 
703 static void kunit_current_test(struct kunit *t    703 static void kunit_current_test(struct kunit *test)
704 {                                                 704 {
705         /* Check results of both current->kuni    705         /* Check results of both current->kunit_test and
706          * kunit_get_current_test() are equiva    706          * kunit_get_current_test() are equivalent to current test.
707          */                                       707          */
708         KUNIT_EXPECT_PTR_EQ(test, test, curren    708         KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
709         KUNIT_EXPECT_PTR_EQ(test, test, kunit_    709         KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
710 }                                                 710 }
711                                                   711 
712 static void kunit_current_fail_test(struct kun    712 static void kunit_current_fail_test(struct kunit *test)
713 {                                                 713 {
714         struct kunit fake;                        714         struct kunit fake;
715                                                   715 
716         kunit_init_test(&fake, "fake test", NU    716         kunit_init_test(&fake, "fake test", NULL);
717         KUNIT_EXPECT_EQ(test, fake.status, KUN    717         KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
718                                                   718 
719         /* Set current->kunit_test to fake tes    719         /* Set current->kunit_test to fake test. */
720         current->kunit_test = &fake;              720         current->kunit_test = &fake;
721                                                   721 
722         kunit_fail_current_test("This should m    722         kunit_fail_current_test("This should make `fake` test fail.");
723         KUNIT_EXPECT_EQ(test, fake.status, (en    723         KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
724         kunit_cleanup(&fake);                     724         kunit_cleanup(&fake);
725                                                   725 
726         /* Reset current->kunit_test to curren    726         /* Reset current->kunit_test to current test. */
727         current->kunit_test = test;               727         current->kunit_test = test;
728 }                                                 728 }
729                                                   729 
730 static struct kunit_case kunit_current_test_ca    730 static struct kunit_case kunit_current_test_cases[] = {
731         KUNIT_CASE(kunit_current_test),           731         KUNIT_CASE(kunit_current_test),
732         KUNIT_CASE(kunit_current_fail_test),      732         KUNIT_CASE(kunit_current_fail_test),
733         {}                                        733         {}
734 };                                                734 };
735                                                   735 
736 static void test_dev_action(void *priv)           736 static void test_dev_action(void *priv)
737 {                                                 737 {
738         *(void **)priv = (void *)1;               738         *(void **)priv = (void *)1;
739 }                                                 739 }
740                                                   740 
741 static void kunit_device_test(struct kunit *te    741 static void kunit_device_test(struct kunit *test)
742 {                                                 742 {
743         struct device *test_device;               743         struct device *test_device;
744         long action_was_run = 0;                  744         long action_was_run = 0;
745                                                   745 
746         test_device = kunit_device_register(te    746         test_device = kunit_device_register(test, "my_device");
747         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tes    747         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
748                                                   748 
749         // Add an action to verify cleanup.       749         // Add an action to verify cleanup.
750         devm_add_action(test_device, test_dev_    750         devm_add_action(test_device, test_dev_action, &action_was_run);
751                                                   751 
752         KUNIT_EXPECT_EQ(test, action_was_run,     752         KUNIT_EXPECT_EQ(test, action_was_run, 0);
753                                                   753 
754         kunit_device_unregister(test, test_dev    754         kunit_device_unregister(test, test_device);
755                                                   755 
756         KUNIT_EXPECT_EQ(test, action_was_run,     756         KUNIT_EXPECT_EQ(test, action_was_run, 1);
757 }                                                 757 }
758                                                   758 
759 static void kunit_device_cleanup_test(struct k    759 static void kunit_device_cleanup_test(struct kunit *test)
760 {                                                 760 {
761         struct device *test_device;               761         struct device *test_device;
762         long action_was_run = 0;                  762         long action_was_run = 0;
763                                                   763 
764         test_device = kunit_device_register(te    764         test_device = kunit_device_register(test, "my_device");
765         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tes    765         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
766                                                   766 
767         /* Add an action to verify cleanup. */    767         /* Add an action to verify cleanup. */
768         devm_add_action(test_device, test_dev_    768         devm_add_action(test_device, test_dev_action, &action_was_run);
769                                                   769 
770         KUNIT_EXPECT_EQ(test, action_was_run,     770         KUNIT_EXPECT_EQ(test, action_was_run, 0);
771                                                   771 
772         /* Force KUnit to run cleanup early. *    772         /* Force KUnit to run cleanup early. */
773         kunit_cleanup(test);                      773         kunit_cleanup(test);
774                                                   774 
775         KUNIT_EXPECT_EQ(test, action_was_run,     775         KUNIT_EXPECT_EQ(test, action_was_run, 1);
776 }                                                 776 }
777                                                   777 
778 struct driver_test_state {                        778 struct driver_test_state {
779         bool driver_device_probed;                779         bool driver_device_probed;
780         bool driver_device_removed;               780         bool driver_device_removed;
781         long action_was_run;                      781         long action_was_run;
782 };                                                782 };
783                                                   783 
784 static int driver_probe_hook(struct device *de    784 static int driver_probe_hook(struct device *dev)
785 {                                                 785 {
786         struct kunit *test = kunit_get_current    786         struct kunit *test = kunit_get_current_test();
787         struct driver_test_state *state = (str    787         struct driver_test_state *state = (struct driver_test_state *)test->priv;
788                                                   788 
789         state->driver_device_probed = true;       789         state->driver_device_probed = true;
790         return 0;                                 790         return 0;
791 }                                                 791 }
792                                                   792 
793 static int driver_remove_hook(struct device *d    793 static int driver_remove_hook(struct device *dev)
794 {                                                 794 {
795         struct kunit *test = kunit_get_current    795         struct kunit *test = kunit_get_current_test();
796         struct driver_test_state *state = (str    796         struct driver_test_state *state = (struct driver_test_state *)test->priv;
797                                                   797 
798         state->driver_device_removed = true;      798         state->driver_device_removed = true;
799         return 0;                                 799         return 0;
800 }                                                 800 }
801                                                   801 
802 static void kunit_device_driver_test(struct ku    802 static void kunit_device_driver_test(struct kunit *test)
803 {                                                 803 {
804         struct device_driver *test_driver;        804         struct device_driver *test_driver;
805         struct device *test_device;               805         struct device *test_device;
806         struct driver_test_state *test_state =    806         struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
807                                                   807 
808         test->priv = test_state;                  808         test->priv = test_state;
809         test_driver = kunit_driver_create(test    809         test_driver = kunit_driver_create(test, "my_driver");
810                                                   810 
811         // This can fail with an error pointer    811         // This can fail with an error pointer.
812         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tes    812         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
813                                                   813 
814         test_driver->probe = driver_probe_hook    814         test_driver->probe = driver_probe_hook;
815         test_driver->remove = driver_remove_ho    815         test_driver->remove = driver_remove_hook;
816                                                   816 
817         test_device = kunit_device_register_wi    817         test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
818                                                   818 
819         // This can fail with an error pointer    819         // This can fail with an error pointer.
820         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tes    820         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
821                                                   821 
822         // Make sure the probe function was ca    822         // Make sure the probe function was called.
823         KUNIT_ASSERT_TRUE(test, test_state->dr    823         KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
824                                                   824 
825         // Add an action to verify cleanup.       825         // Add an action to verify cleanup.
826         devm_add_action(test_device, test_dev_    826         devm_add_action(test_device, test_dev_action, &test_state->action_was_run);
827                                                   827 
828         KUNIT_EXPECT_EQ(test, test_state->acti    828         KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
829                                                   829 
830         kunit_device_unregister(test, test_dev    830         kunit_device_unregister(test, test_device);
831         test_device = NULL;                       831         test_device = NULL;
832                                                   832 
833         // Make sure the remove hook was calle    833         // Make sure the remove hook was called.
834         KUNIT_ASSERT_TRUE(test, test_state->dr    834         KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
835                                                   835 
836         // We're going to test this again.        836         // We're going to test this again.
837         test_state->driver_device_probed = fal    837         test_state->driver_device_probed = false;
838                                                   838 
839         // The driver should not automatically    839         // The driver should not automatically be destroyed by
840         // kunit_device_unregister, so we can     840         // kunit_device_unregister, so we can re-use it.
841         test_device = kunit_device_register_wi    841         test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
842                                                   842 
843         // This can fail with an error pointer    843         // This can fail with an error pointer.
844         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tes    844         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
845                                                   845 
846         // Probe was called again.                846         // Probe was called again.
847         KUNIT_ASSERT_TRUE(test, test_state->dr    847         KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
848                                                   848 
849         // Everything is automatically freed h    849         // Everything is automatically freed here.
850 }                                                 850 }
851                                                   851 
852 static struct kunit_case kunit_device_test_cas    852 static struct kunit_case kunit_device_test_cases[] = {
853         KUNIT_CASE(kunit_device_test),            853         KUNIT_CASE(kunit_device_test),
854         KUNIT_CASE(kunit_device_cleanup_test),    854         KUNIT_CASE(kunit_device_cleanup_test),
855         KUNIT_CASE(kunit_device_driver_test),     855         KUNIT_CASE(kunit_device_driver_test),
856         {}                                        856         {}
857 };                                                857 };
858                                                   858 
859 static struct kunit_suite kunit_device_test_su    859 static struct kunit_suite kunit_device_test_suite = {
860         .name = "kunit_device",                   860         .name = "kunit_device",
861         .test_cases = kunit_device_test_cases,    861         .test_cases = kunit_device_test_cases,
862 };                                                862 };
863                                                   863 
864 static struct kunit_suite kunit_current_test_s    864 static struct kunit_suite kunit_current_test_suite = {
865         .name = "kunit_current",                  865         .name = "kunit_current",
866         .test_cases = kunit_current_test_cases    866         .test_cases = kunit_current_test_cases,
867 };                                                867 };
868                                                   868 
869 kunit_test_suites(&kunit_try_catch_test_suite,    869 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
870                   &kunit_log_test_suite, &kuni    870                   &kunit_log_test_suite, &kunit_status_test_suite,
871                   &kunit_current_test_suite, &    871                   &kunit_current_test_suite, &kunit_device_test_suite,
872                   &kunit_fault_test_suite);       872                   &kunit_fault_test_suite);
873                                                   873 
874 MODULE_DESCRIPTION("KUnit test for core test i    874 MODULE_DESCRIPTION("KUnit test for core test infrastructure");
875 MODULE_LICENSE("GPL v2");                         875 MODULE_LICENSE("GPL v2");
876                                                   876 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php