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.

Dependency Surface

Detected Declarations

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

Implementation Notes