Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

revocable: Add Kunit test cases

Add Kunit test cases for the revocable API.

The test cases cover the following scenarios:
- Basic: Verifies that a consumer can successfully access the resource
provided via the provider.
- Revocation: Verifies that after the provider revokes the resource,
the consumer correctly receives a NULL pointer on a subsequent access.
- Try Access Macro: Same as "Revocation" but uses the
REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED().

A way to run the test:
$ ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=y \
revocable_test

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
Link: https://patch.msgid.link/20260116080235.350305-3-tzungbi@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Tzung-Bi Shih and committed by
Greg Kroah-Hartman
cd769341 62eb5575

+151
+1
MAINTAINERS
··· 22373 22373 L: linux-kernel@vger.kernel.org 22374 22374 S: Maintained 22375 22375 F: drivers/base/revocable.c 22376 + F: drivers/base/revocable_test.c 22376 22377 F: include/linux/revocable.h 22377 22378 22378 22379 RFKILL
+8
drivers/base/Kconfig
··· 250 250 work on. 251 251 252 252 endmenu 253 + 254 + # Kunit test cases 255 + config REVOCABLE_KUNIT_TEST 256 + tristate "Kunit tests for revocable" if !KUNIT_ALL_TESTS 257 + depends on KUNIT 258 + default KUNIT_ALL_TESTS 259 + help 260 + Kunit tests for the revocable API.
+3
drivers/base/Makefile
··· 35 35 # define_trace.h needs to know how to find our header 36 36 CFLAGS_trace.o := -I$(src) 37 37 obj-$(CONFIG_TRACING) += trace.o 38 + 39 + # Kunit test cases 40 + obj-$(CONFIG_REVOCABLE_KUNIT_TEST) += revocable_test.o
+139
drivers/base/revocable_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright 2026 Google LLC 4 + * 5 + * Kunit tests for the revocable API. 6 + * 7 + * The test cases cover the following scenarios: 8 + * 9 + * - Basic: Verifies that a consumer can successfully access the resource 10 + * provided via the provider. 11 + * 12 + * - Revocation: Verifies that after the provider revokes the resource, 13 + * the consumer correctly receives a NULL pointer on a subsequent access. 14 + * 15 + * - Try Access Macro: Same as "Revocation" but uses the 16 + * REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED(). 17 + */ 18 + 19 + #include <kunit/test.h> 20 + #include <linux/revocable.h> 21 + 22 + static void revocable_test_basic(struct kunit *test) 23 + { 24 + struct revocable_provider *rp; 25 + struct revocable *rev; 26 + void *real_res = (void *)0x12345678, *res; 27 + 28 + rp = revocable_provider_alloc(real_res); 29 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp); 30 + 31 + rev = revocable_alloc(rp); 32 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rev); 33 + 34 + res = revocable_try_access(rev); 35 + KUNIT_EXPECT_PTR_EQ(test, res, real_res); 36 + revocable_withdraw_access(rev); 37 + 38 + revocable_free(rev); 39 + revocable_provider_revoke(rp); 40 + } 41 + 42 + static void revocable_test_revocation(struct kunit *test) 43 + { 44 + struct revocable_provider *rp; 45 + struct revocable *rev; 46 + void *real_res = (void *)0x12345678, *res; 47 + 48 + rp = revocable_provider_alloc(real_res); 49 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp); 50 + 51 + rev = revocable_alloc(rp); 52 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rev); 53 + 54 + res = revocable_try_access(rev); 55 + KUNIT_EXPECT_PTR_EQ(test, res, real_res); 56 + revocable_withdraw_access(rev); 57 + 58 + revocable_provider_revoke(rp); 59 + 60 + res = revocable_try_access(rev); 61 + KUNIT_EXPECT_PTR_EQ(test, res, NULL); 62 + revocable_withdraw_access(rev); 63 + 64 + revocable_free(rev); 65 + } 66 + 67 + static void revocable_test_try_access_macro(struct kunit *test) 68 + { 69 + struct revocable_provider *rp; 70 + struct revocable *rev; 71 + void *real_res = (void *)0x12345678, *res; 72 + 73 + rp = revocable_provider_alloc(real_res); 74 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp); 75 + 76 + rev = revocable_alloc(rp); 77 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rev); 78 + 79 + { 80 + REVOCABLE_TRY_ACCESS_WITH(rev, res); 81 + KUNIT_EXPECT_PTR_EQ(test, res, real_res); 82 + } 83 + 84 + revocable_provider_revoke(rp); 85 + 86 + { 87 + REVOCABLE_TRY_ACCESS_WITH(rev, res); 88 + KUNIT_EXPECT_PTR_EQ(test, res, NULL); 89 + } 90 + 91 + revocable_free(rev); 92 + } 93 + 94 + static void revocable_test_try_access_macro2(struct kunit *test) 95 + { 96 + struct revocable_provider *rp; 97 + struct revocable *rev; 98 + void *real_res = (void *)0x12345678, *res; 99 + bool accessed; 100 + 101 + rp = revocable_provider_alloc(real_res); 102 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp); 103 + 104 + rev = revocable_alloc(rp); 105 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rev); 106 + 107 + accessed = false; 108 + REVOCABLE_TRY_ACCESS_SCOPED(rev, res) { 109 + KUNIT_EXPECT_PTR_EQ(test, res, real_res); 110 + accessed = true; 111 + } 112 + KUNIT_EXPECT_TRUE(test, accessed); 113 + 114 + revocable_provider_revoke(rp); 115 + 116 + accessed = false; 117 + REVOCABLE_TRY_ACCESS_SCOPED(rev, res) { 118 + KUNIT_EXPECT_PTR_EQ(test, res, NULL); 119 + accessed = true; 120 + } 121 + KUNIT_EXPECT_TRUE(test, accessed); 122 + 123 + revocable_free(rev); 124 + } 125 + 126 + static struct kunit_case revocable_test_cases[] = { 127 + KUNIT_CASE(revocable_test_basic), 128 + KUNIT_CASE(revocable_test_revocation), 129 + KUNIT_CASE(revocable_test_try_access_macro), 130 + KUNIT_CASE(revocable_test_try_access_macro2), 131 + {} 132 + }; 133 + 134 + static struct kunit_suite revocable_test_suite = { 135 + .name = "revocable_test", 136 + .test_cases = revocable_test_cases, 137 + }; 138 + 139 + kunit_test_suite(revocable_test_suite);