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.

Dependency Surface

Detected Declarations

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

Implementation Notes