Skip to contentSkip to conversation

Input: tests: add test to cover all input_grab_device() function

PATCH Superseded Dana Elfassy 2 participants 2 messages May 16, 2023 – May 17, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: tests: add test to cover all input_grab_device() function

Currently input_grab_device() isn't covered by any tests Thus, adding a test to cover the cases:
1. The device is grabbed successfully
2. Trying to grab a device that is already grabbed by another input handle

---
 drivers/input/tests/input_test.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/input/tests/input_test.c +28 -0
index 25bbf51b5c87..cd4db365e9fa 100644
@@ -124,10 +124,38 @@ static void input_test_match_device_id(struct kunit *test)
 	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
 }
 
+
+static void input_test_grab(struct kunit *test)
+{
+	struct input_dev *input_dev = test->priv;
+	struct input_handle test_handle;
+	struct input_handler handler;
+	struct input_handle handle;
+	struct input_device_id id;
+	int res;
+
+	handler.name = "handler";
+	handler.id_table = &id;
+
+	handle.dev = input_get_device(input_dev);
+	handle.name = dev_name(&input_dev->dev);
+	handle.handler = &handler;
+	res = input_grab_device(&handle);
+	KUNIT_ASSERT_TRUE(test, input_grab_device(&handle));
+
+	test_handle.dev = input_get_device(input_dev);
+	test_handle.name = dev_name(&input_dev->dev);
+	test_handle.handler = &handler;
+
+	res = input_grab_device(&test_handle);
+	KUNIT_ASSERT_EQ(test, res, -EBUSY);
+}
+
 static struct kunit_case input_tests[] = {
 	KUNIT_CASE(input_test_polling),
 	KUNIT_CASE(input_test_timestamp),
 	KUNIT_CASE(input_test_match_device_id),
+	KUNIT_CASE(input_test_grab),
 	{ /* sentinel */ }
 };
 
Signature · 1 line
2.40.1
Tested-by Reviewed-by on v1 1/1
#1 lore raw

Dana Elfassy <[email protected]> writes:

Hello Dana,

Is great to see more input Kunit tests being added, thanks!

Quoted 8 lines

Currently input_grab_device() isn't covered by any tests Thus, adding a test to cover the cases:
1. The device is grabbed successfully
2. Trying to grab a device that is already grabbed by another input handle

---

I tested this and it worked for me:

$ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/input/tests/.kunitconfig
...
[09:36:42] Starting KUnit Kernel (1/1)...
[09:36:42] ============================================================
[09:36:43] ================= input_core (4 subtests) ==================
[09:36:43] [PASSED] input_test_polling
[09:36:43] [PASSED] input_test_timestamp
[09:36:43] [PASSED] input_test_match_device_id
[09:36:43] [PASSED] input_test_grab
[09:36:43] =================== [PASSED] input_core ====================
[09:36:43] ============================================================
[09:36:43] Testing complete. Ran 4 tests: passed: 4
[09:36:43] Elapsed time: 129.985s total, 5.005s configuring, 124.864s building, 0.062s running
Quoted 20 lines
 drivers/input/tests/input_test.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/input/tests/input_test.c +9 -0
index 25bbf51b5c87..cd4db365e9fa 100644
@@ -124,10 +124,38 @@ static void input_test_match_device_id(struct kunit *test)
 	KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
 }
 
+
+static void input_test_grab(struct kunit *test)
+{
+	struct input_dev *input_dev = test->priv;
+	struct input_handle test_handle;
+	struct input_handler handler;
+	struct input_handle handle;
+	struct input_device_id id;
+	int res;

drivers/input/tests/input_test.c static void input_test_match_device_id(struct kunit *test) · line 145

Quoted 10 lines
drivers/input/tests/input_test.c +10 -0
+
+	handler.name = "handler";
+	handler.id_table = &id;
+
+	handle.dev = input_get_device(input_dev);
+	handle.name = dev_name(&input_dev->dev);
+	handle.handler = &handler;
+	res = input_grab_device(&handle);
+	KUNIT_ASSERT_TRUE(test, input_grab_device(&handle));
+

I think you need to add a input_put_device(input_dev) here ?

Otherwise the reference counter won't be decremented.

drivers/input/tests/input_test.c static void input_test_match_device_id(struct kunit *test) · line 149

Quoted 4 lines
drivers/input/tests/input_test.c +4 -0
+	test_handle.dev = input_get_device(input_dev);
+	test_handle.name = dev_name(&input_dev->dev);
+	test_handle.handler = &handler;
+

I think you can just reuse the handle variable that was set-up before ?

There's no need to another test_handle variable as far as I can tell.

drivers/input/tests/input_test.c static void input_test_match_device_id(struct kunit *test) · line 151

Quoted 2 lines
drivers/input/tests/input_test.c +2 -0
+	res = input_grab_device(&test_handle);
+	KUNIT_ASSERT_EQ(test, res, -EBUSY);

And here add an input_put_device(input_dev) call too.

Other than that the patch looks good to me.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat