lib/kunit/kunit-test.c
Source file repositories/reference/linux-study-clean/lib/kunit/kunit-test.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kunit/kunit-test.c- Extension
.c- Size
- 25722 bytes
- Lines
- 924
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/gfp_types.hkunit/test.hkunit/test-bug.hkunit/static_stub.hlinux/device.hkunit/device.hstring-stream.htry-catch-impl.h
Detected Declarations
struct kunit_try_catch_test_contextstruct kunit_test_resource_contextstruct driver_test_statefunction kunit_test_successful_tryfunction kunit_test_no_catchfunction kunit_test_try_catch_successful_try_no_catchfunction kunit_test_unsuccessful_tryfunction kunit_test_catchfunction kunit_test_try_catch_unsuccessful_try_does_catchfunction kunit_try_catch_test_initfunction kunit_test_null_dereferencefunction kunit_test_fault_null_dereferencefunction fake_resource_initfunction fake_resource_freefunction kunit_resource_test_init_resourcesfunction kunit_resource_test_alloc_resourcefunction kunit_resource_instance_matchfunction kunit_put_resourcefunction kunit_resource_test_remove_resourcefunction kunit_resource_test_cleanup_resourcesfunction kunit_resource_test_mark_orderfunction kunit_resource_test_mark_orderfunction fake_resource_2_freefunction fake_resource_1_initfunction fake_resource_1_freefunction TODOfunction kunit_resource_test_staticfunction kunit_resource_test_namedfunction increment_intfunction kunit_resource_test_actionfunction kunit_resource_test_remove_actionfunction kunit_resource_test_release_actionfunction action_order_1function action_order_2function kunit_resource_test_action_orderingfunction kunit_resource_test_initfunction kunit_resource_test_exitfunction kunit_log_testfunction kunit_log_newline_testfunction kunit_log_testfunction kunit_log_newline_testfunction kunit_status_set_failure_testfunction kunit_status_mark_skipped_testfunction kunit_current_testfunction kunit_current_fail_testfunction test_dev_actionfunction kunit_device_testfunction kunit_device_cleanup_test
Annotated Snippet
struct device_driver *test_driver;
struct device *test_device;
struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
test->priv = test_state;
test_driver = kunit_driver_create(test, "my_driver");
// This can fail with an error pointer.
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
test_driver->probe = driver_probe_hook;
test_driver->remove = driver_remove_hook;
test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
// This can fail with an error pointer.
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
// Make sure the probe function was called.
KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
// Add an action to verify cleanup.
devm_add_action(test_device, test_dev_action, &test_state->action_was_run);
KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
kunit_device_unregister(test, test_device);
test_device = NULL;
// Make sure the remove hook was called.
KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
// We're going to test this again.
test_state->driver_device_probed = false;
// The driver should not automatically be destroyed by
// kunit_device_unregister, so we can re-use it.
test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
// This can fail with an error pointer.
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
// Probe was called again.
KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
// Everything is automatically freed here.
}
static struct kunit_case kunit_device_test_cases[] = {
KUNIT_CASE(kunit_device_test),
KUNIT_CASE(kunit_device_cleanup_test),
KUNIT_CASE(kunit_device_driver_test),
{}
};
static struct kunit_suite kunit_device_test_suite = {
.name = "kunit_device",
.test_cases = kunit_device_test_cases,
};
static struct kunit_suite kunit_current_test_suite = {
.name = "kunit_current",
.test_cases = kunit_current_test_cases,
};
static void kunit_stub_test(struct kunit *test)
{
struct kunit fake_test;
const unsigned long fake_real_fn_addr = 0x1234;
const unsigned long fake_replacement_addr = 0x5678;
struct kunit_resource *res;
struct {
void *real_fn_addr;
void *replacement_addr;
} *stub_ctx;
kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL);
KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
__kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr,
(void *)fake_replacement_addr);
KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1);
res = list_first_entry(&fake_test.resources, struct kunit_resource, node);
KUNIT_EXPECT_NOT_NULL(test, res);
Annotation
- Immediate include surface: `linux/gfp_types.h`, `kunit/test.h`, `kunit/test-bug.h`, `kunit/static_stub.h`, `linux/device.h`, `kunit/device.h`, `string-stream.h`, `try-catch-impl.h`.
- Detected declarations: `struct kunit_try_catch_test_context`, `struct kunit_test_resource_context`, `struct driver_test_state`, `function kunit_test_successful_try`, `function kunit_test_no_catch`, `function kunit_test_try_catch_successful_try_no_catch`, `function kunit_test_unsuccessful_try`, `function kunit_test_catch`, `function kunit_test_try_catch_unsuccessful_try_does_catch`, `function kunit_try_catch_test_init`.
- Atlas domain: Kernel Services / lib.
- Implementation status: pattern implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.