input_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for the input core.
  4. *
  5. * Copyright (c) 2023 Red Hat Inc
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/input.h>
  9. #include <kunit/test.h>
  10. #define POLL_INTERVAL 100
  11. static int input_test_init(struct kunit *test)
  12. {
  13. struct input_dev *input_dev;
  14. int ret;
  15. input_dev = input_allocate_device();
  16. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, input_dev);
  17. input_dev->name = "Test input device";
  18. input_dev->id.bustype = BUS_VIRTUAL;
  19. input_dev->id.vendor = 1;
  20. input_dev->id.product = 1;
  21. input_dev->id.version = 1;
  22. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  23. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  24. ret = input_register_device(input_dev);
  25. if (ret) {
  26. input_free_device(input_dev);
  27. KUNIT_FAIL_AND_ABORT(test, "Register device failed: %d", ret);
  28. }
  29. test->priv = input_dev;
  30. return 0;
  31. }
  32. static void input_test_exit(struct kunit *test)
  33. {
  34. struct input_dev *input_dev = test->priv;
  35. if (input_dev)
  36. input_unregister_device(input_dev);
  37. }
  38. static void input_test_poll(struct input_dev *input) { }
  39. static void input_test_polling(struct kunit *test)
  40. {
  41. struct input_dev *input_dev = test->priv;
  42. /* Must fail because a poll handler has not been set-up yet */
  43. KUNIT_ASSERT_EQ(test, input_get_poll_interval(input_dev), -EINVAL);
  44. KUNIT_ASSERT_EQ(test, input_setup_polling(input_dev, input_test_poll), 0);
  45. input_set_poll_interval(input_dev, POLL_INTERVAL);
  46. /* Must succeed because poll handler was set-up and poll interval set */
  47. KUNIT_ASSERT_EQ(test, input_get_poll_interval(input_dev), POLL_INTERVAL);
  48. }
  49. static void input_test_timestamp(struct kunit *test)
  50. {
  51. const ktime_t invalid_timestamp = ktime_set(0, 0);
  52. struct input_dev *input_dev = test->priv;
  53. ktime_t *timestamp, time;
  54. timestamp = input_get_timestamp(input_dev);
  55. time = timestamp[INPUT_CLK_MONO];
  56. /* The returned timestamp must always be valid */
  57. KUNIT_ASSERT_EQ(test, ktime_compare(time, invalid_timestamp), 1);
  58. time = ktime_get();
  59. input_set_timestamp(input_dev, time);
  60. timestamp = input_get_timestamp(input_dev);
  61. /* The timestamp must be the same than set before */
  62. KUNIT_ASSERT_EQ(test, ktime_compare(timestamp[INPUT_CLK_MONO], time), 0);
  63. }
  64. static void input_test_match_device_id(struct kunit *test)
  65. {
  66. struct input_dev *input_dev = test->priv;
  67. struct input_device_id id = { 0 };
  68. /*
  69. * Must match when the input device bus, vendor, product, version
  70. * and events capable of handling are the same and fail to match
  71. * otherwise.
  72. */
  73. id.flags = INPUT_DEVICE_ID_MATCH_BUS;
  74. id.bustype = BUS_VIRTUAL;
  75. KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));
  76. id.bustype = BUS_I2C;
  77. KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
  78. id.flags = INPUT_DEVICE_ID_MATCH_VENDOR;
  79. id.vendor = 1;
  80. KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));
  81. id.vendor = 2;
  82. KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
  83. id.flags = INPUT_DEVICE_ID_MATCH_PRODUCT;
  84. id.product = 1;
  85. KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));
  86. id.product = 2;
  87. KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
  88. id.flags = INPUT_DEVICE_ID_MATCH_VERSION;
  89. id.version = 1;
  90. KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));
  91. id.version = 2;
  92. KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
  93. id.flags = INPUT_DEVICE_ID_MATCH_EVBIT;
  94. __set_bit(EV_KEY, id.evbit);
  95. KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id));
  96. __set_bit(EV_ABS, id.evbit);
  97. KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id));
  98. }
  99. static void input_test_grab(struct kunit *test)
  100. {
  101. struct input_dev *input_dev = test->priv;
  102. struct input_handle test_handle;
  103. struct input_handler handler;
  104. struct input_handle handle;
  105. struct input_device_id id;
  106. int res;
  107. handler.name = "handler";
  108. handler.id_table = &id;
  109. handle.dev = input_get_device(input_dev);
  110. handle.name = dev_name(&input_dev->dev);
  111. handle.handler = &handler;
  112. res = input_grab_device(&handle);
  113. KUNIT_ASSERT_TRUE(test, res == 0);
  114. test_handle.dev = input_get_device(input_dev);
  115. test_handle.name = dev_name(&input_dev->dev);
  116. test_handle.handler = &handler;
  117. res = input_grab_device(&test_handle);
  118. KUNIT_ASSERT_EQ(test, res, -EBUSY);
  119. input_release_device(&handle);
  120. input_put_device(input_dev);
  121. res = input_grab_device(&test_handle);
  122. KUNIT_ASSERT_TRUE(test, res == 0);
  123. input_put_device(input_dev);
  124. }
  125. static struct kunit_case input_tests[] = {
  126. KUNIT_CASE(input_test_polling),
  127. KUNIT_CASE(input_test_timestamp),
  128. KUNIT_CASE(input_test_match_device_id),
  129. KUNIT_CASE(input_test_grab),
  130. { /* sentinel */ }
  131. };
  132. static struct kunit_suite input_test_suite = {
  133. .name = "input_core",
  134. .init = input_test_init,
  135. .exit = input_test_exit,
  136. .test_cases = input_tests,
  137. };
  138. kunit_test_suite(input_test_suite);
  139. MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>");
  140. MODULE_DESCRIPTION("KUnit test for the input core");
  141. MODULE_LICENSE("GPL");