18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * KUnit test for core test infrastructure. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC. 68c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <kunit/test.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include "try-catch-impl.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistruct kunit_try_catch_test_context { 138c2ecf20Sopenharmony_ci struct kunit_try_catch *try_catch; 148c2ecf20Sopenharmony_ci bool function_called; 158c2ecf20Sopenharmony_ci}; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic void kunit_test_successful_try(void *data) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci struct kunit *test = data; 208c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx = test->priv; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci ctx->function_called = true; 238c2ecf20Sopenharmony_ci} 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic void kunit_test_no_catch(void *data) 268c2ecf20Sopenharmony_ci{ 278c2ecf20Sopenharmony_ci struct kunit *test = data; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci KUNIT_FAIL(test, "Catch should not be called\n"); 308c2ecf20Sopenharmony_ci} 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx = test->priv; 358c2ecf20Sopenharmony_ci struct kunit_try_catch *try_catch = ctx->try_catch; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci kunit_try_catch_init(try_catch, 388c2ecf20Sopenharmony_ci test, 398c2ecf20Sopenharmony_ci kunit_test_successful_try, 408c2ecf20Sopenharmony_ci kunit_test_no_catch); 418c2ecf20Sopenharmony_ci kunit_try_catch_run(try_catch, test); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, ctx->function_called); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic void kunit_test_unsuccessful_try(void *data) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci struct kunit *test = data; 498c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx = test->priv; 508c2ecf20Sopenharmony_ci struct kunit_try_catch *try_catch = ctx->try_catch; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci kunit_try_catch_throw(try_catch); 538c2ecf20Sopenharmony_ci KUNIT_FAIL(test, "This line should never be reached\n"); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic void kunit_test_catch(void *data) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct kunit *test = data; 598c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx = test->priv; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci ctx->function_called = true; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx = test->priv; 678c2ecf20Sopenharmony_ci struct kunit_try_catch *try_catch = ctx->try_catch; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci kunit_try_catch_init(try_catch, 708c2ecf20Sopenharmony_ci test, 718c2ecf20Sopenharmony_ci kunit_test_unsuccessful_try, 728c2ecf20Sopenharmony_ci kunit_test_catch); 738c2ecf20Sopenharmony_ci kunit_try_catch_run(try_catch, test); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, ctx->function_called); 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic int kunit_try_catch_test_init(struct kunit *test) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci struct kunit_try_catch_test_context *ctx; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 838c2ecf20Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 848c2ecf20Sopenharmony_ci test->priv = ctx; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci ctx->try_catch = kunit_kmalloc(test, 878c2ecf20Sopenharmony_ci sizeof(*ctx->try_catch), 888c2ecf20Sopenharmony_ci GFP_KERNEL); 898c2ecf20Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci return 0; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic struct kunit_case kunit_try_catch_test_cases[] = { 958c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch), 968c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch), 978c2ecf20Sopenharmony_ci {} 988c2ecf20Sopenharmony_ci}; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic struct kunit_suite kunit_try_catch_test_suite = { 1018c2ecf20Sopenharmony_ci .name = "kunit-try-catch-test", 1028c2ecf20Sopenharmony_ci .init = kunit_try_catch_test_init, 1038c2ecf20Sopenharmony_ci .test_cases = kunit_try_catch_test_cases, 1048c2ecf20Sopenharmony_ci}; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* 1078c2ecf20Sopenharmony_ci * Context for testing test managed resources 1088c2ecf20Sopenharmony_ci * is_resource_initialized is used to test arbitrary resources 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_cistruct kunit_test_resource_context { 1118c2ecf20Sopenharmony_ci struct kunit test; 1128c2ecf20Sopenharmony_ci bool is_resource_initialized; 1138c2ecf20Sopenharmony_ci int allocate_order[2]; 1148c2ecf20Sopenharmony_ci int free_order[2]; 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int fake_resource_init(struct kunit_resource *res, void *context) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = context; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci res->data = &ctx->is_resource_initialized; 1228c2ecf20Sopenharmony_ci ctx->is_resource_initialized = true; 1238c2ecf20Sopenharmony_ci return 0; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic void fake_resource_free(struct kunit_resource *res) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci bool *is_resource_initialized = res->data; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci *is_resource_initialized = false; 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic void kunit_resource_test_init_resources(struct kunit *test) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci kunit_init_test(&ctx->test, "testing_test_init_test", NULL); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic void kunit_resource_test_alloc_resource(struct kunit *test) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 1458c2ecf20Sopenharmony_ci struct kunit_resource *res; 1468c2ecf20Sopenharmony_ci kunit_resource_free_t free = fake_resource_free; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci res = kunit_alloc_and_get_resource(&ctx->test, 1498c2ecf20Sopenharmony_ci fake_resource_init, 1508c2ecf20Sopenharmony_ci fake_resource_free, 1518c2ecf20Sopenharmony_ci GFP_KERNEL, 1528c2ecf20Sopenharmony_ci ctx); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); 1558c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, 1568c2ecf20Sopenharmony_ci &ctx->is_resource_initialized, 1578c2ecf20Sopenharmony_ci (bool *)res->data); 1588c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources)); 1598c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, free, res->free); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci kunit_put_resource(res); 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/* 1658c2ecf20Sopenharmony_ci * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence 1668c2ecf20Sopenharmony_ci * they have a reference to the associated resource that they must release 1678c2ecf20Sopenharmony_ci * via kunit_put_resource(). In normal operation, users will only 1688c2ecf20Sopenharmony_ci * have to do this for cases where they use kunit_find_resource(), and the 1698c2ecf20Sopenharmony_ci * kunit_alloc_resource() function will be used (which does not take a 1708c2ecf20Sopenharmony_ci * resource reference). 1718c2ecf20Sopenharmony_ci */ 1728c2ecf20Sopenharmony_cistatic void kunit_resource_test_destroy_resource(struct kunit *test) 1738c2ecf20Sopenharmony_ci{ 1748c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 1758c2ecf20Sopenharmony_ci struct kunit_resource *res = kunit_alloc_and_get_resource( 1768c2ecf20Sopenharmony_ci &ctx->test, 1778c2ecf20Sopenharmony_ci fake_resource_init, 1788c2ecf20Sopenharmony_ci fake_resource_free, 1798c2ecf20Sopenharmony_ci GFP_KERNEL, 1808c2ecf20Sopenharmony_ci ctx); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci kunit_put_resource(res); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci KUNIT_ASSERT_FALSE(test, 1858c2ecf20Sopenharmony_ci kunit_destroy_resource(&ctx->test, 1868c2ecf20Sopenharmony_ci kunit_resource_instance_match, 1878c2ecf20Sopenharmony_ci res->data)); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized); 1908c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic void kunit_resource_test_cleanup_resources(struct kunit *test) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci int i; 1968c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 1978c2ecf20Sopenharmony_ci struct kunit_resource *resources[5]; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(resources); i++) { 2008c2ecf20Sopenharmony_ci resources[i] = kunit_alloc_and_get_resource(&ctx->test, 2018c2ecf20Sopenharmony_ci fake_resource_init, 2028c2ecf20Sopenharmony_ci fake_resource_free, 2038c2ecf20Sopenharmony_ci GFP_KERNEL, 2048c2ecf20Sopenharmony_ci ctx); 2058c2ecf20Sopenharmony_ci kunit_put_resource(resources[i]); 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci kunit_cleanup(&ctx->test); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic void kunit_resource_test_mark_order(int order_array[], 2148c2ecf20Sopenharmony_ci size_t order_size, 2158c2ecf20Sopenharmony_ci int key) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci int i; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci for (i = 0; i < order_size && order_array[i]; i++) 2208c2ecf20Sopenharmony_ci ; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci order_array[i] = key; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \ 2268c2ecf20Sopenharmony_ci kunit_resource_test_mark_order(ctx->order_field, \ 2278c2ecf20Sopenharmony_ci ARRAY_SIZE(ctx->order_field), \ 2288c2ecf20Sopenharmony_ci key) 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic int fake_resource_2_init(struct kunit_resource *res, void *context) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = context; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci res->data = ctx; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci return 0; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic void fake_resource_2_free(struct kunit_resource *res) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = res->data; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2); 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic int fake_resource_1_init(struct kunit_resource *res, void *context) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = context; 2518c2ecf20Sopenharmony_ci struct kunit_resource *res2; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci res2 = kunit_alloc_and_get_resource(&ctx->test, 2548c2ecf20Sopenharmony_ci fake_resource_2_init, 2558c2ecf20Sopenharmony_ci fake_resource_2_free, 2568c2ecf20Sopenharmony_ci GFP_KERNEL, 2578c2ecf20Sopenharmony_ci ctx); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci res->data = ctx; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci kunit_put_resource(res2); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci return 0; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic void fake_resource_1_free(struct kunit_resource *res) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = res->data; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci/* 2768c2ecf20Sopenharmony_ci * TODO(brendanhiggins@google.com): replace the arrays that keep track of the 2778c2ecf20Sopenharmony_ci * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro 2788c2ecf20Sopenharmony_ci * to assert allocation and freeing order when the feature becomes available. 2798c2ecf20Sopenharmony_ci */ 2808c2ecf20Sopenharmony_cistatic void kunit_resource_test_proper_free_ordering(struct kunit *test) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 2838c2ecf20Sopenharmony_ci struct kunit_resource *res; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci /* fake_resource_1 allocates a fake_resource_2 in its init. */ 2868c2ecf20Sopenharmony_ci res = kunit_alloc_and_get_resource(&ctx->test, 2878c2ecf20Sopenharmony_ci fake_resource_1_init, 2888c2ecf20Sopenharmony_ci fake_resource_1_free, 2898c2ecf20Sopenharmony_ci GFP_KERNEL, 2908c2ecf20Sopenharmony_ci ctx); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* 2938c2ecf20Sopenharmony_ci * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER 2948c2ecf20Sopenharmony_ci * before returning to fake_resource_1_init, it should be the first to 2958c2ecf20Sopenharmony_ci * put its key in the allocate_order array. 2968c2ecf20Sopenharmony_ci */ 2978c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2); 2988c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci kunit_put_resource(res); 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci kunit_cleanup(&ctx->test); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci /* 3058c2ecf20Sopenharmony_ci * Because fake_resource_2 finishes allocation before fake_resource_1, 3068c2ecf20Sopenharmony_ci * fake_resource_1 should be freed first since it could depend on 3078c2ecf20Sopenharmony_ci * fake_resource_2. 3088c2ecf20Sopenharmony_ci */ 3098c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1); 3108c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2); 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cistatic void kunit_resource_test_static(struct kunit *test) 3148c2ecf20Sopenharmony_ci{ 3158c2ecf20Sopenharmony_ci struct kunit_test_resource_context ctx; 3168c2ecf20Sopenharmony_ci struct kunit_resource res; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx), 3198c2ecf20Sopenharmony_ci 0); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci kunit_cleanup(test); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); 3268c2ecf20Sopenharmony_ci} 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_cistatic void kunit_resource_test_named(struct kunit *test) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci struct kunit_resource res1, res2, *found = NULL; 3318c2ecf20Sopenharmony_ci struct kunit_test_resource_context ctx; 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, 3348c2ecf20Sopenharmony_ci kunit_add_named_resource(test, NULL, NULL, &res1, 3358c2ecf20Sopenharmony_ci "resource_1", &ctx), 3368c2ecf20Sopenharmony_ci 0); 3378c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, 3408c2ecf20Sopenharmony_ci kunit_add_named_resource(test, NULL, NULL, &res1, 3418c2ecf20Sopenharmony_ci "resource_1", &ctx), 3428c2ecf20Sopenharmony_ci -EEXIST); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, 3458c2ecf20Sopenharmony_ci kunit_add_named_resource(test, NULL, NULL, &res2, 3468c2ecf20Sopenharmony_ci "resource_2", &ctx), 3478c2ecf20Sopenharmony_ci 0); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci found = kunit_find_named_resource(test, "resource_1"); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, found, &res1); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci if (found) 3548c2ecf20Sopenharmony_ci kunit_put_resource(&res1); 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"), 3578c2ecf20Sopenharmony_ci 0); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci kunit_cleanup(test); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); 3628c2ecf20Sopenharmony_ci} 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_cistatic int kunit_resource_test_init(struct kunit *test) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = 3678c2ecf20Sopenharmony_ci kzalloc(sizeof(*ctx), GFP_KERNEL); 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci test->priv = ctx; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci kunit_init_test(&ctx->test, "test_test_context", NULL); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci return 0; 3768c2ecf20Sopenharmony_ci} 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_cistatic void kunit_resource_test_exit(struct kunit *test) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci struct kunit_test_resource_context *ctx = test->priv; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci kunit_cleanup(&ctx->test); 3838c2ecf20Sopenharmony_ci kfree(ctx); 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic struct kunit_case kunit_resource_test_cases[] = { 3878c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_init_resources), 3888c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_alloc_resource), 3898c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_destroy_resource), 3908c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_cleanup_resources), 3918c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_proper_free_ordering), 3928c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_static), 3938c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_resource_test_named), 3948c2ecf20Sopenharmony_ci {} 3958c2ecf20Sopenharmony_ci}; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_cistatic struct kunit_suite kunit_resource_test_suite = { 3988c2ecf20Sopenharmony_ci .name = "kunit-resource-test", 3998c2ecf20Sopenharmony_ci .init = kunit_resource_test_init, 4008c2ecf20Sopenharmony_ci .exit = kunit_resource_test_exit, 4018c2ecf20Sopenharmony_ci .test_cases = kunit_resource_test_cases, 4028c2ecf20Sopenharmony_ci}; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_cistatic void kunit_log_test(struct kunit *test); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_cistatic struct kunit_case kunit_log_test_cases[] = { 4078c2ecf20Sopenharmony_ci KUNIT_CASE(kunit_log_test), 4088c2ecf20Sopenharmony_ci {} 4098c2ecf20Sopenharmony_ci}; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_cistatic struct kunit_suite kunit_log_test_suite = { 4128c2ecf20Sopenharmony_ci .name = "kunit-log-test", 4138c2ecf20Sopenharmony_ci .test_cases = kunit_log_test_cases, 4148c2ecf20Sopenharmony_ci}; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_cistatic void kunit_log_test(struct kunit *test) 4178c2ecf20Sopenharmony_ci{ 4188c2ecf20Sopenharmony_ci struct kunit_suite *suite = &kunit_log_test_suite; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci kunit_log(KERN_INFO, test, "put this in log."); 4218c2ecf20Sopenharmony_ci kunit_log(KERN_INFO, test, "this too."); 4228c2ecf20Sopenharmony_ci kunit_log(KERN_INFO, suite, "add to suite log."); 4238c2ecf20Sopenharmony_ci kunit_log(KERN_INFO, suite, "along with this."); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci#ifdef CONFIG_KUNIT_DEBUGFS 4268c2ecf20Sopenharmony_ci KUNIT_EXPECT_NOT_ERR_OR_NULL(test, 4278c2ecf20Sopenharmony_ci strstr(test->log, "put this in log.")); 4288c2ecf20Sopenharmony_ci KUNIT_EXPECT_NOT_ERR_OR_NULL(test, 4298c2ecf20Sopenharmony_ci strstr(test->log, "this too.")); 4308c2ecf20Sopenharmony_ci KUNIT_EXPECT_NOT_ERR_OR_NULL(test, 4318c2ecf20Sopenharmony_ci strstr(suite->log, "add to suite log.")); 4328c2ecf20Sopenharmony_ci KUNIT_EXPECT_NOT_ERR_OR_NULL(test, 4338c2ecf20Sopenharmony_ci strstr(suite->log, "along with this.")); 4348c2ecf20Sopenharmony_ci#else 4358c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, test->log, (char *)NULL); 4368c2ecf20Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, suite->log, (char *)NULL); 4378c2ecf20Sopenharmony_ci#endif 4388c2ecf20Sopenharmony_ci} 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_cikunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, 4418c2ecf20Sopenharmony_ci &kunit_log_test_suite); 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 444