test_secure.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright 2021 Stefan Kerkmann
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "gtest/gtest.h"
  17. #include "keyboard_report_util.hpp"
  18. #include "test_common.hpp"
  19. using testing::_;
  20. using testing::AnyNumber;
  21. using testing::InSequence;
  22. class Secure : public TestFixture {
  23. public:
  24. void SetUp() override {
  25. secure_lock();
  26. }
  27. };
  28. TEST_F(Secure, test_lock) {
  29. TestDriver driver;
  30. // Don't allow empty reports.
  31. EXPECT_NO_REPORT(driver);
  32. EXPECT_FALSE(secure_is_unlocked());
  33. secure_unlock();
  34. EXPECT_TRUE(secure_is_unlocked());
  35. run_one_scan_loop();
  36. EXPECT_TRUE(secure_is_unlocked());
  37. secure_lock();
  38. EXPECT_FALSE(secure_is_unlocked());
  39. testing::Mock::VerifyAndClearExpectations(&driver);
  40. }
  41. TEST_F(Secure, test_unlock_timeout) {
  42. TestDriver driver;
  43. // Don't allow empty reports.
  44. EXPECT_NO_REPORT(driver);
  45. EXPECT_FALSE(secure_is_unlocked());
  46. secure_unlock();
  47. EXPECT_TRUE(secure_is_unlocked());
  48. idle_for(SECURE_IDLE_TIMEOUT + 1);
  49. EXPECT_FALSE(secure_is_unlocked());
  50. testing::Mock::VerifyAndClearExpectations(&driver);
  51. }
  52. TEST_F(Secure, test_unlock_request) {
  53. TestDriver driver;
  54. auto key_mo = KeymapKey(0, 0, 0, MO(1));
  55. auto key_a = KeymapKey(0, 1, 0, KC_A);
  56. auto key_b = KeymapKey(0, 2, 0, KC_B);
  57. auto key_c = KeymapKey(0, 3, 0, KC_C);
  58. auto key_d = KeymapKey(0, 4, 0, KC_D);
  59. set_keymap({key_mo, key_a, key_b, key_c, key_d});
  60. // Don't allow empty reports.
  61. EXPECT_NO_REPORT(driver);
  62. EXPECT_TRUE(secure_is_locked());
  63. secure_request_unlock();
  64. EXPECT_TRUE(secure_is_unlocking());
  65. tap_keys(key_a, key_b, key_c, key_d);
  66. EXPECT_TRUE(secure_is_unlocked());
  67. testing::Mock::VerifyAndClearExpectations(&driver);
  68. }
  69. TEST_F(Secure, test_unlock_request_fail) {
  70. TestDriver driver;
  71. auto key_e = KeymapKey(0, 0, 0, KC_E);
  72. auto key_a = KeymapKey(0, 1, 0, KC_A);
  73. auto key_b = KeymapKey(0, 2, 0, KC_B);
  74. auto key_c = KeymapKey(0, 3, 0, KC_C);
  75. auto key_d = KeymapKey(0, 4, 0, KC_D);
  76. set_keymap({key_e, key_a, key_b, key_c, key_d});
  77. // Allow any number of empty reports.
  78. EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
  79. { // Expect the following reports in this order.
  80. InSequence s;
  81. EXPECT_REPORT(driver, (KC_A));
  82. EXPECT_REPORT(driver, (KC_B));
  83. EXPECT_REPORT(driver, (KC_C));
  84. EXPECT_REPORT(driver, (KC_D));
  85. }
  86. EXPECT_TRUE(secure_is_locked());
  87. secure_request_unlock();
  88. EXPECT_TRUE(secure_is_unlocking());
  89. tap_keys(key_e, key_a, key_b, key_c, key_d);
  90. EXPECT_FALSE(secure_is_unlocked());
  91. testing::Mock::VerifyAndClearExpectations(&driver);
  92. }
  93. TEST_F(Secure, test_unlock_request_timeout) {
  94. TestDriver driver;
  95. // Don't allow empty reports.
  96. EXPECT_NO_REPORT(driver);
  97. EXPECT_FALSE(secure_is_unlocked());
  98. secure_request_unlock();
  99. EXPECT_TRUE(secure_is_unlocking());
  100. idle_for(SECURE_UNLOCK_TIMEOUT + 1);
  101. EXPECT_FALSE(secure_is_unlocking());
  102. EXPECT_FALSE(secure_is_unlocked());
  103. testing::Mock::VerifyAndClearExpectations(&driver);
  104. }
  105. TEST_F(Secure, test_unlock_request_fail_mid) {
  106. TestDriver driver;
  107. auto key_e = KeymapKey(0, 0, 0, KC_E);
  108. auto key_a = KeymapKey(0, 1, 0, KC_A);
  109. auto key_b = KeymapKey(0, 2, 0, KC_B);
  110. auto key_c = KeymapKey(0, 3, 0, KC_C);
  111. auto key_d = KeymapKey(0, 4, 0, KC_D);
  112. set_keymap({key_e, key_a, key_b, key_c, key_d});
  113. // Allow any number of empty reports.
  114. EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
  115. { // Expect the following reports in this order.
  116. InSequence s;
  117. EXPECT_REPORT(driver, (KC_C));
  118. EXPECT_REPORT(driver, (KC_D));
  119. }
  120. EXPECT_FALSE(secure_is_unlocked());
  121. secure_request_unlock();
  122. EXPECT_TRUE(secure_is_unlocking());
  123. tap_keys(key_a, key_b, key_e, key_c, key_d);
  124. EXPECT_FALSE(secure_is_unlocking());
  125. EXPECT_FALSE(secure_is_unlocked());
  126. testing::Mock::VerifyAndClearExpectations(&driver);
  127. }
  128. TEST_F(Secure, test_unlock_request_fail_out_of_order) {
  129. TestDriver driver;
  130. auto key_e = KeymapKey(0, 0, 0, KC_E);
  131. auto key_a = KeymapKey(0, 1, 0, KC_A);
  132. auto key_b = KeymapKey(0, 2, 0, KC_B);
  133. auto key_c = KeymapKey(0, 3, 0, KC_C);
  134. auto key_d = KeymapKey(0, 4, 0, KC_D);
  135. set_keymap({key_e, key_a, key_b, key_c, key_d});
  136. // Allow any number of empty reports.
  137. EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
  138. { // Expect the following reports in this order.
  139. InSequence s;
  140. EXPECT_REPORT(driver, (KC_B));
  141. EXPECT_REPORT(driver, (KC_C));
  142. }
  143. EXPECT_FALSE(secure_is_unlocked());
  144. secure_request_unlock();
  145. EXPECT_TRUE(secure_is_unlocking());
  146. tap_keys(key_a, key_d, key_b, key_c);
  147. EXPECT_TRUE(secure_is_locked());
  148. EXPECT_FALSE(secure_is_unlocking());
  149. EXPECT_FALSE(secure_is_unlocked());
  150. testing::Mock::VerifyAndClearExpectations(&driver);
  151. }
  152. TEST_F(Secure, test_unlock_request_on_layer) {
  153. TestDriver driver;
  154. auto key_mo = KeymapKey(0, 0, 0, MO(1));
  155. auto key_a = KeymapKey(0, 1, 0, KC_A);
  156. auto key_b = KeymapKey(0, 2, 0, KC_B);
  157. auto key_c = KeymapKey(0, 3, 0, KC_C);
  158. auto key_d = KeymapKey(0, 4, 0, KC_D);
  159. set_keymap({key_mo, key_a, key_b, key_c, key_d});
  160. // Don't allow empty reports.
  161. EXPECT_NO_REPORT(driver);
  162. EXPECT_TRUE(secure_is_locked());
  163. key_mo.press();
  164. run_one_scan_loop();
  165. secure_request_unlock();
  166. key_mo.release();
  167. run_one_scan_loop();
  168. EXPECT_TRUE(secure_is_unlocking());
  169. tap_keys(key_a, key_b, key_c, key_d);
  170. EXPECT_TRUE(secure_is_unlocked());
  171. EXPECT_FALSE(layer_state_is(1));
  172. testing::Mock::VerifyAndClearExpectations(&driver);
  173. }
  174. TEST_F(Secure, test_unlock_request_mid_stroke) {
  175. TestDriver driver;
  176. auto key_e = KeymapKey(0, 0, 0, KC_E);
  177. auto key_a = KeymapKey(0, 1, 0, KC_A);
  178. auto key_b = KeymapKey(0, 2, 0, KC_B);
  179. auto key_c = KeymapKey(0, 3, 0, KC_C);
  180. auto key_d = KeymapKey(0, 4, 0, KC_D);
  181. set_keymap({key_e, key_a, key_b, key_c, key_d});
  182. EXPECT_REPORT(driver, (KC_E));
  183. EXPECT_EMPTY_REPORT(driver);
  184. EXPECT_TRUE(secure_is_locked());
  185. key_e.press();
  186. run_one_scan_loop();
  187. secure_request_unlock();
  188. key_e.release();
  189. run_one_scan_loop();
  190. EXPECT_TRUE(secure_is_unlocking());
  191. tap_keys(key_a, key_b, key_c, key_d);
  192. EXPECT_TRUE(secure_is_unlocked());
  193. testing::Mock::VerifyAndClearExpectations(&driver);
  194. }
  195. TEST_F(Secure, test_unlock_request_mods) {
  196. TestDriver driver;
  197. auto key_lsft = KeymapKey(0, 0, 0, KC_LSFT);
  198. auto key_a = KeymapKey(0, 1, 0, KC_A);
  199. auto key_b = KeymapKey(0, 2, 0, KC_B);
  200. auto key_c = KeymapKey(0, 3, 0, KC_C);
  201. auto key_d = KeymapKey(0, 4, 0, KC_D);
  202. set_keymap({key_lsft, key_a, key_b, key_c, key_d});
  203. EXPECT_REPORT(driver, (key_lsft.report_code));
  204. EXPECT_EMPTY_REPORT(driver);
  205. EXPECT_TRUE(secure_is_locked());
  206. key_lsft.press();
  207. run_one_scan_loop();
  208. secure_request_unlock();
  209. key_lsft.release();
  210. run_one_scan_loop();
  211. EXPECT_TRUE(secure_is_unlocking());
  212. tap_keys(key_a, key_b, key_c, key_d);
  213. EXPECT_TRUE(secure_is_unlocked());
  214. testing::Mock::VerifyAndClearExpectations(&driver);
  215. }