lib/kunit/platform-test.c
Source file repositories/reference/linux-study-clean/lib/kunit/platform-test.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kunit/platform-test.c- Extension
.c- Size
- 7102 bytes
- Lines
- 225
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source 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.
- 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/platform_device.hkunit/platform_device.hkunit/test.h
Detected Declarations
struct kunit_platform_driver_test_contextfunction kunit_platform_device_allocfunction kunit_platform_device_addfunction kunit_platform_device_addfunction kunit_platform_device_find_by_namefunction kunit_platform_device_addfunction to_test_contextfunction kunit_platform_driver_probefunction kunit_platform_driver_register_testfunction kunit_platform_device_prepare_wait_for_probe
Annotated Snippet
struct kunit_platform_driver_test_context {
struct platform_driver pdrv;
const char *data;
};
static const char * const test_data = "test data";
static inline struct kunit_platform_driver_test_context *
to_test_context(struct platform_device *pdev)
{
return container_of(to_platform_driver(pdev->dev.driver),
struct kunit_platform_driver_test_context,
pdrv);
}
static int kunit_platform_driver_probe(struct platform_device *pdev)
{
struct kunit_platform_driver_test_context *ctx;
ctx = to_test_context(pdev);
ctx->data = test_data;
return 0;
}
/* Test that kunit_platform_driver_register() registers a driver that probes. */
static void kunit_platform_driver_register_test(struct kunit *test)
{
struct platform_device *pdev;
struct kunit_platform_driver_test_context *ctx;
DECLARE_COMPLETION_ONSTACK(comp);
const char *name = "kunit-platform-register";
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
pdev = kunit_platform_device_alloc(test, name, -1);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
ctx->pdrv.probe = kunit_platform_driver_probe;
ctx->pdrv.driver.name = name;
ctx->pdrv.driver.owner = THIS_MODULE;
KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
KUNIT_EXPECT_STREQ(test, ctx->data, test_data);
}
/*
* Test that kunit_platform_device_prepare_wait_for_probe() completes the completion
* when the device is already probed.
*/
static void kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed(struct kunit *test)
{
struct platform_device *pdev;
struct kunit_platform_driver_test_context *ctx;
DECLARE_COMPLETION_ONSTACK(comp);
const char *name = "kunit-platform-wait";
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
pdev = kunit_platform_device_alloc(test, name, -1);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
ctx->pdrv.probe = kunit_platform_driver_probe;
ctx->pdrv.driver.name = name;
ctx->pdrv.driver.owner = THIS_MODULE;
/* Make sure driver has actually probed */
KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
reinit_completion(&comp);
KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, HZ));
}
static struct kunit_case kunit_platform_driver_test_cases[] = {
KUNIT_CASE(kunit_platform_driver_register_test),
KUNIT_CASE(kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed),
{}
};
Annotation
- Immediate include surface: `linux/platform_device.h`, `kunit/platform_device.h`, `kunit/test.h`.
- Detected declarations: `struct kunit_platform_driver_test_context`, `function kunit_platform_device_alloc`, `function kunit_platform_device_add`, `function kunit_platform_device_add`, `function kunit_platform_device_find_by_name`, `function kunit_platform_device_add`, `function to_test_context`, `function kunit_platform_driver_probe`, `function kunit_platform_driver_register_test`, `function kunit_platform_device_prepare_wait_for_probe`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source 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.