Skip to contentSkip to conversation

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

PATCH Superseded Dana Elfassy 3 participants 4 messages May 17, 2023 – May 18, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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

---

Changes in v2:
- Use input_put_device() to decrement the refcount increased by get().
- Remove unnecessary struct input_handle test_handle variable.

 drivers/input/tests/input_test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/input/tests/input_test.c +23 -0
index 25bbf51b5c87..cea0167a74d2 100644
@@ -124,10 +124,33 @@ 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_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));
+
+	res = input_grab_device(&handle);
+	KUNIT_ASSERT_EQ(test, res, -EBUSY);
+	input_put_device(input_dev);
+}
+
 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
maintainer
#1 lore raw

Hi Dana,

Quoted 6 lines · Dana Elfassy

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

Quoted 8 lines

---

Changes in v2:
- Use input_put_device() to decrement the refcount increased by get().
- Remove unnecessary struct input_handle test_handle variable.

So this tests something different than what patch description states. You are testing that there is no "recursive" grabbing happening (an API could be designed to allow the same handle grab device several times). This is a good and useful test, but you do want to also use 2nd separate handle to see that it gets -EBUSY as well. And ideally we should have another test verifying that the 2nd handle can successfully grab the device once the first handle releases it.

Thanks.

Signature · 1 line
Dmitry
#2 lore raw

Dmitry Torokhov <[email protected]> writes:

Hello Dmitry,

Quoted 18 lines

Hi Dana,

Quoted 6 lines · Dana Elfassy

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

Quoted 8 lines

---

Changes in v2:
- Use input_put_device() to decrement the refcount increased by get().
- Remove unnecessary struct input_handle test_handle variable.

Quoted 5 lines

So this tests something different than what patch description states. You are testing that there is no "recursive" grabbing happening (an API could be designed to allow the same handle grab device several times). This is a good and useful test, but you do want to also use 2nd separate handle to see that it gets -EBUSY as well. And ideally we should have

That was my fault since v1 had two different handles but since it wasn't releasing it, didn't add any value really so I asked Dana to just drop it.

Quoted 3 lines

another test verifying that the 2nd handle can successfully grab the device once the first handle releases it.

That's the correct approach indeed and would make the test more useful.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#3 lore raw
In reply to Dana Elfassy

Dana Elfassy <[email protected]> writes:

Hello Dana,

Quoted 10 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

---

[...]

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

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

Another thing I noticed is that your test will try to grab the same input_handle twice. So you need to remove the line above I believe.

Quoted 2 lines
(quoted diff) +2 -0
+	KUNIT_ASSERT_TRUE(test, input_grab_device(&handle));
+
Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat