Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * KUnit test for core test infrastructure.
0004  *
0005  * Copyright (C) 2019, Google LLC.
0006  * Author: Brendan Higgins <brendanhiggins@google.com>
0007  */
0008 #include <kunit/test.h>
0009 
0010 #include "try-catch-impl.h"
0011 
0012 struct kunit_try_catch_test_context {
0013     struct kunit_try_catch *try_catch;
0014     bool function_called;
0015 };
0016 
0017 static void kunit_test_successful_try(void *data)
0018 {
0019     struct kunit *test = data;
0020     struct kunit_try_catch_test_context *ctx = test->priv;
0021 
0022     ctx->function_called = true;
0023 }
0024 
0025 static void kunit_test_no_catch(void *data)
0026 {
0027     struct kunit *test = data;
0028 
0029     KUNIT_FAIL(test, "Catch should not be called\n");
0030 }
0031 
0032 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
0033 {
0034     struct kunit_try_catch_test_context *ctx = test->priv;
0035     struct kunit_try_catch *try_catch = ctx->try_catch;
0036 
0037     kunit_try_catch_init(try_catch,
0038                  test,
0039                  kunit_test_successful_try,
0040                  kunit_test_no_catch);
0041     kunit_try_catch_run(try_catch, test);
0042 
0043     KUNIT_EXPECT_TRUE(test, ctx->function_called);
0044 }
0045 
0046 static void kunit_test_unsuccessful_try(void *data)
0047 {
0048     struct kunit *test = data;
0049     struct kunit_try_catch_test_context *ctx = test->priv;
0050     struct kunit_try_catch *try_catch = ctx->try_catch;
0051 
0052     kunit_try_catch_throw(try_catch);
0053     KUNIT_FAIL(test, "This line should never be reached\n");
0054 }
0055 
0056 static void kunit_test_catch(void *data)
0057 {
0058     struct kunit *test = data;
0059     struct kunit_try_catch_test_context *ctx = test->priv;
0060 
0061     ctx->function_called = true;
0062 }
0063 
0064 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
0065 {
0066     struct kunit_try_catch_test_context *ctx = test->priv;
0067     struct kunit_try_catch *try_catch = ctx->try_catch;
0068 
0069     kunit_try_catch_init(try_catch,
0070                  test,
0071                  kunit_test_unsuccessful_try,
0072                  kunit_test_catch);
0073     kunit_try_catch_run(try_catch, test);
0074 
0075     KUNIT_EXPECT_TRUE(test, ctx->function_called);
0076 }
0077 
0078 static int kunit_try_catch_test_init(struct kunit *test)
0079 {
0080     struct kunit_try_catch_test_context *ctx;
0081 
0082     ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
0083     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
0084     test->priv = ctx;
0085 
0086     ctx->try_catch = kunit_kmalloc(test,
0087                        sizeof(*ctx->try_catch),
0088                        GFP_KERNEL);
0089     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
0090 
0091     return 0;
0092 }
0093 
0094 static struct kunit_case kunit_try_catch_test_cases[] = {
0095     KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
0096     KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
0097     {}
0098 };
0099 
0100 static struct kunit_suite kunit_try_catch_test_suite = {
0101     .name = "kunit-try-catch-test",
0102     .init = kunit_try_catch_test_init,
0103     .test_cases = kunit_try_catch_test_cases,
0104 };
0105 
0106 /*
0107  * Context for testing test managed resources
0108  * is_resource_initialized is used to test arbitrary resources
0109  */
0110 struct kunit_test_resource_context {
0111     struct kunit test;
0112     bool is_resource_initialized;
0113     int allocate_order[2];
0114     int free_order[2];
0115 };
0116 
0117 static int fake_resource_init(struct kunit_resource *res, void *context)
0118 {
0119     struct kunit_test_resource_context *ctx = context;
0120 
0121     res->data = &ctx->is_resource_initialized;
0122     ctx->is_resource_initialized = true;
0123     return 0;
0124 }
0125 
0126 static void fake_resource_free(struct kunit_resource *res)
0127 {
0128     bool *is_resource_initialized = res->data;
0129 
0130     *is_resource_initialized = false;
0131 }
0132 
0133 static void kunit_resource_test_init_resources(struct kunit *test)
0134 {
0135     struct kunit_test_resource_context *ctx = test->priv;
0136 
0137     kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
0138 
0139     KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
0140 }
0141 
0142 static void kunit_resource_test_alloc_resource(struct kunit *test)
0143 {
0144     struct kunit_test_resource_context *ctx = test->priv;
0145     struct kunit_resource *res;
0146     kunit_resource_free_t free = fake_resource_free;
0147 
0148     res = kunit_alloc_and_get_resource(&ctx->test,
0149                        fake_resource_init,
0150                        fake_resource_free,
0151                        GFP_KERNEL,
0152                        ctx);
0153 
0154     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
0155     KUNIT_EXPECT_PTR_EQ(test,
0156                 &ctx->is_resource_initialized,
0157                 (bool *)res->data);
0158     KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
0159     KUNIT_EXPECT_PTR_EQ(test, free, res->free);
0160 
0161     kunit_put_resource(res);
0162 }
0163 
0164 /*
0165  * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
0166  * they have a reference to the associated resource that they must release
0167  * via kunit_put_resource().  In normal operation, users will only
0168  * have to do this for cases where they use kunit_find_resource(), and the
0169  * kunit_alloc_resource() function will be used (which does not take a
0170  * resource reference).
0171  */
0172 static void kunit_resource_test_destroy_resource(struct kunit *test)
0173 {
0174     struct kunit_test_resource_context *ctx = test->priv;
0175     struct kunit_resource *res = kunit_alloc_and_get_resource(
0176             &ctx->test,
0177             fake_resource_init,
0178             fake_resource_free,
0179             GFP_KERNEL,
0180             ctx);
0181 
0182     kunit_put_resource(res);
0183 
0184     KUNIT_ASSERT_FALSE(test,
0185                kunit_destroy_resource(&ctx->test,
0186                           kunit_resource_instance_match,
0187                           res->data));
0188 
0189     KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
0190     KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
0191 }
0192 
0193 static void kunit_resource_test_remove_resource(struct kunit *test)
0194 {
0195     struct kunit_test_resource_context *ctx = test->priv;
0196     struct kunit_resource *res = kunit_alloc_and_get_resource(
0197             &ctx->test,
0198             fake_resource_init,
0199             fake_resource_free,
0200             GFP_KERNEL,
0201             ctx);
0202 
0203     /* The resource is in the list */
0204     KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
0205 
0206     /* Remove the resource. The pointer is still valid, but it can't be
0207      * found.
0208      */
0209     kunit_remove_resource(test, res);
0210     KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
0211     /* We haven't been freed yet. */
0212     KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
0213 
0214     /* Removing the resource multiple times is valid. */
0215     kunit_remove_resource(test, res);
0216     KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
0217     /* Despite having been removed twice (from only one reference), the
0218      * resource still has not been freed.
0219      */
0220     KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
0221 
0222     /* Free the resource. */
0223     kunit_put_resource(res);
0224     KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
0225 }
0226 
0227 static void kunit_resource_test_cleanup_resources(struct kunit *test)
0228 {
0229     int i;
0230     struct kunit_test_resource_context *ctx = test->priv;
0231     struct kunit_resource *resources[5];
0232 
0233     for (i = 0; i < ARRAY_SIZE(resources); i++) {
0234         resources[i] = kunit_alloc_and_get_resource(&ctx->test,
0235                                 fake_resource_init,
0236                                 fake_resource_free,
0237                                 GFP_KERNEL,
0238                                 ctx);
0239         kunit_put_resource(resources[i]);
0240     }
0241 
0242     kunit_cleanup(&ctx->test);
0243 
0244     KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
0245 }
0246 
0247 static void kunit_resource_test_mark_order(int order_array[],
0248                        size_t order_size,
0249                        int key)
0250 {
0251     int i;
0252 
0253     for (i = 0; i < order_size && order_array[i]; i++)
0254         ;
0255 
0256     order_array[i] = key;
0257 }
0258 
0259 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key)              \
0260         kunit_resource_test_mark_order(ctx->order_field,           \
0261                            ARRAY_SIZE(ctx->order_field),   \
0262                            key)
0263 
0264 static int fake_resource_2_init(struct kunit_resource *res, void *context)
0265 {
0266     struct kunit_test_resource_context *ctx = context;
0267 
0268     KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
0269 
0270     res->data = ctx;
0271 
0272     return 0;
0273 }
0274 
0275 static void fake_resource_2_free(struct kunit_resource *res)
0276 {
0277     struct kunit_test_resource_context *ctx = res->data;
0278 
0279     KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
0280 }
0281 
0282 static int fake_resource_1_init(struct kunit_resource *res, void *context)
0283 {
0284     struct kunit_test_resource_context *ctx = context;
0285     struct kunit_resource *res2;
0286 
0287     res2 = kunit_alloc_and_get_resource(&ctx->test,
0288                         fake_resource_2_init,
0289                         fake_resource_2_free,
0290                         GFP_KERNEL,
0291                         ctx);
0292 
0293     KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
0294 
0295     res->data = ctx;
0296 
0297     kunit_put_resource(res2);
0298 
0299     return 0;
0300 }
0301 
0302 static void fake_resource_1_free(struct kunit_resource *res)
0303 {
0304     struct kunit_test_resource_context *ctx = res->data;
0305 
0306     KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
0307 }
0308 
0309 /*
0310  * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
0311  * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
0312  * to assert allocation and freeing order when the feature becomes available.
0313  */
0314 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
0315 {
0316     struct kunit_test_resource_context *ctx = test->priv;
0317     struct kunit_resource *res;
0318 
0319     /* fake_resource_1 allocates a fake_resource_2 in its init. */
0320     res = kunit_alloc_and_get_resource(&ctx->test,
0321                        fake_resource_1_init,
0322                        fake_resource_1_free,
0323                        GFP_KERNEL,
0324                        ctx);
0325 
0326     /*
0327      * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
0328      * before returning to fake_resource_1_init, it should be the first to
0329      * put its key in the allocate_order array.
0330      */
0331     KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
0332     KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
0333 
0334     kunit_put_resource(res);
0335 
0336     kunit_cleanup(&ctx->test);
0337 
0338     /*
0339      * Because fake_resource_2 finishes allocation before fake_resource_1,
0340      * fake_resource_1 should be freed first since it could depend on
0341      * fake_resource_2.
0342      */
0343     KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
0344     KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
0345 }
0346 
0347 static void kunit_resource_test_static(struct kunit *test)
0348 {
0349     struct kunit_test_resource_context ctx;
0350     struct kunit_resource res;
0351 
0352     KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
0353             0);
0354 
0355     KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
0356 
0357     kunit_cleanup(test);
0358 
0359     KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
0360 }
0361 
0362 static void kunit_resource_test_named(struct kunit *test)
0363 {
0364     struct kunit_resource res1, res2, *found = NULL;
0365     struct kunit_test_resource_context ctx;
0366 
0367     KUNIT_EXPECT_EQ(test,
0368             kunit_add_named_resource(test, NULL, NULL, &res1,
0369                          "resource_1", &ctx),
0370             0);
0371     KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
0372 
0373     KUNIT_EXPECT_EQ(test,
0374             kunit_add_named_resource(test, NULL, NULL, &res1,
0375                          "resource_1", &ctx),
0376             -EEXIST);
0377 
0378     KUNIT_EXPECT_EQ(test,
0379             kunit_add_named_resource(test, NULL, NULL, &res2,
0380                          "resource_2", &ctx),
0381             0);
0382 
0383     found = kunit_find_named_resource(test, "resource_1");
0384 
0385     KUNIT_EXPECT_PTR_EQ(test, found, &res1);
0386 
0387     if (found)
0388         kunit_put_resource(&res1);
0389 
0390     KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
0391             0);
0392 
0393     kunit_cleanup(test);
0394 
0395     KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
0396 }
0397 
0398 static int kunit_resource_test_init(struct kunit *test)
0399 {
0400     struct kunit_test_resource_context *ctx =
0401             kzalloc(sizeof(*ctx), GFP_KERNEL);
0402 
0403     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
0404 
0405     test->priv = ctx;
0406 
0407     kunit_init_test(&ctx->test, "test_test_context", NULL);
0408 
0409     return 0;
0410 }
0411 
0412 static void kunit_resource_test_exit(struct kunit *test)
0413 {
0414     struct kunit_test_resource_context *ctx = test->priv;
0415 
0416     kunit_cleanup(&ctx->test);
0417     kfree(ctx);
0418 }
0419 
0420 static struct kunit_case kunit_resource_test_cases[] = {
0421     KUNIT_CASE(kunit_resource_test_init_resources),
0422     KUNIT_CASE(kunit_resource_test_alloc_resource),
0423     KUNIT_CASE(kunit_resource_test_destroy_resource),
0424     KUNIT_CASE(kunit_resource_test_remove_resource),
0425     KUNIT_CASE(kunit_resource_test_cleanup_resources),
0426     KUNIT_CASE(kunit_resource_test_proper_free_ordering),
0427     KUNIT_CASE(kunit_resource_test_static),
0428     KUNIT_CASE(kunit_resource_test_named),
0429     {}
0430 };
0431 
0432 static struct kunit_suite kunit_resource_test_suite = {
0433     .name = "kunit-resource-test",
0434     .init = kunit_resource_test_init,
0435     .exit = kunit_resource_test_exit,
0436     .test_cases = kunit_resource_test_cases,
0437 };
0438 
0439 static void kunit_log_test(struct kunit *test);
0440 
0441 static struct kunit_case kunit_log_test_cases[] = {
0442     KUNIT_CASE(kunit_log_test),
0443     {}
0444 };
0445 
0446 static struct kunit_suite kunit_log_test_suite = {
0447     .name = "kunit-log-test",
0448     .test_cases = kunit_log_test_cases,
0449 };
0450 
0451 static void kunit_log_test(struct kunit *test)
0452 {
0453     struct kunit_suite suite;
0454 
0455     suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
0456     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
0457 
0458     kunit_log(KERN_INFO, test, "put this in log.");
0459     kunit_log(KERN_INFO, test, "this too.");
0460     kunit_log(KERN_INFO, &suite, "add to suite log.");
0461     kunit_log(KERN_INFO, &suite, "along with this.");
0462 
0463 #ifdef CONFIG_KUNIT_DEBUGFS
0464     KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
0465                      strstr(test->log, "put this in log."));
0466     KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
0467                      strstr(test->log, "this too."));
0468     KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
0469                      strstr(suite.log, "add to suite log."));
0470     KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
0471                      strstr(suite.log, "along with this."));
0472 #else
0473     KUNIT_EXPECT_NULL(test, test->log);
0474 #endif
0475 }
0476 
0477 static void kunit_status_set_failure_test(struct kunit *test)
0478 {
0479     struct kunit fake;
0480 
0481     kunit_init_test(&fake, "fake test", NULL);
0482 
0483     KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
0484     kunit_set_failure(&fake);
0485     KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
0486 }
0487 
0488 static void kunit_status_mark_skipped_test(struct kunit *test)
0489 {
0490     struct kunit fake;
0491 
0492     kunit_init_test(&fake, "fake test", NULL);
0493 
0494     /* Before: Should be SUCCESS with no comment. */
0495     KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
0496     KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
0497 
0498     /* Mark the test as skipped. */
0499     kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
0500 
0501     /* After: Should be SKIPPED with our comment. */
0502     KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
0503     KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
0504 }
0505 
0506 static struct kunit_case kunit_status_test_cases[] = {
0507     KUNIT_CASE(kunit_status_set_failure_test),
0508     KUNIT_CASE(kunit_status_mark_skipped_test),
0509     {}
0510 };
0511 
0512 static struct kunit_suite kunit_status_test_suite = {
0513     .name = "kunit_status",
0514     .test_cases = kunit_status_test_cases,
0515 };
0516 
0517 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
0518           &kunit_log_test_suite, &kunit_status_test_suite);
0519 
0520 MODULE_LICENSE("GPL v2");