|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * KUnit test for core test infrastructure. |
| 4 | + * |
| 5 | + * Copyright (C) 2019, Google LLC. |
| 6 | + * Author: Brendan Higgins <brendanhiggins@google.com> |
| 7 | + */ |
| 8 | +#include <kunit/test.h> |
| 9 | + |
| 10 | +struct kunit_try_catch_test_context { |
| 11 | + struct kunit_try_catch *try_catch; |
| 12 | + bool function_called; |
| 13 | +}; |
| 14 | + |
| 15 | +static void kunit_test_successful_try(void *data) |
| 16 | +{ |
| 17 | + struct kunit *test = data; |
| 18 | + struct kunit_try_catch_test_context *ctx = test->priv; |
| 19 | + |
| 20 | + ctx->function_called = true; |
| 21 | +} |
| 22 | + |
| 23 | +static void kunit_test_no_catch(void *data) |
| 24 | +{ |
| 25 | + struct kunit *test = data; |
| 26 | + |
| 27 | + KUNIT_FAIL(test, "Catch should not be called\n"); |
| 28 | +} |
| 29 | + |
| 30 | +static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) |
| 31 | +{ |
| 32 | + struct kunit_try_catch_test_context *ctx = test->priv; |
| 33 | + struct kunit_try_catch *try_catch = ctx->try_catch; |
| 34 | + |
| 35 | + kunit_try_catch_init(try_catch, |
| 36 | + test, |
| 37 | + kunit_test_successful_try, |
| 38 | + kunit_test_no_catch); |
| 39 | + kunit_try_catch_run(try_catch, test); |
| 40 | + |
| 41 | + KUNIT_EXPECT_TRUE(test, ctx->function_called); |
| 42 | +} |
| 43 | + |
| 44 | +static void kunit_test_unsuccessful_try(void *data) |
| 45 | +{ |
| 46 | + struct kunit *test = data; |
| 47 | + struct kunit_try_catch_test_context *ctx = test->priv; |
| 48 | + struct kunit_try_catch *try_catch = ctx->try_catch; |
| 49 | + |
| 50 | + kunit_try_catch_throw(try_catch); |
| 51 | + KUNIT_FAIL(test, "This line should never be reached\n"); |
| 52 | +} |
| 53 | + |
| 54 | +static void kunit_test_catch(void *data) |
| 55 | +{ |
| 56 | + struct kunit *test = data; |
| 57 | + struct kunit_try_catch_test_context *ctx = test->priv; |
| 58 | + |
| 59 | + ctx->function_called = true; |
| 60 | +} |
| 61 | + |
| 62 | +static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) |
| 63 | +{ |
| 64 | + struct kunit_try_catch_test_context *ctx = test->priv; |
| 65 | + struct kunit_try_catch *try_catch = ctx->try_catch; |
| 66 | + |
| 67 | + kunit_try_catch_init(try_catch, |
| 68 | + test, |
| 69 | + kunit_test_unsuccessful_try, |
| 70 | + kunit_test_catch); |
| 71 | + kunit_try_catch_run(try_catch, test); |
| 72 | + |
| 73 | + KUNIT_EXPECT_TRUE(test, ctx->function_called); |
| 74 | +} |
| 75 | + |
| 76 | +static int kunit_try_catch_test_init(struct kunit *test) |
| 77 | +{ |
| 78 | + struct kunit_try_catch_test_context *ctx; |
| 79 | + |
| 80 | + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); |
| 81 | + if (!ctx) |
| 82 | + return -ENOMEM; |
| 83 | + |
| 84 | + test->priv = ctx; |
| 85 | + |
| 86 | + ctx->try_catch = kunit_kmalloc(test, |
| 87 | + sizeof(*ctx->try_catch), |
| 88 | + GFP_KERNEL); |
| 89 | + if (!ctx->try_catch) |
| 90 | + return -ENOMEM; |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +static struct kunit_case kunit_try_catch_test_cases[] = { |
| 96 | + KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch), |
| 97 | + KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch), |
| 98 | + {} |
| 99 | +}; |
| 100 | + |
| 101 | +static struct kunit_suite kunit_try_catch_test_suite = { |
| 102 | + .name = "kunit-try-catch-test", |
| 103 | + .init = kunit_try_catch_test_init, |
| 104 | + .test_cases = kunit_try_catch_test_cases, |
| 105 | +}; |
| 106 | +kunit_test_suite(kunit_try_catch_test_suite); |
0 commit comments