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