confirmationui_rate_limiting_test.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include "../confirmationui_rate_limiting.h"
  18. #include <keymaster/logger.h>
  19. using std::vector;
  20. namespace keystore {
  21. namespace test {
  22. namespace {
  23. class StdoutLogger : public ::keymaster::Logger {
  24. public:
  25. StdoutLogger() { set_instance(this); }
  26. int log_msg(LogLevel level, const char* fmt, va_list args) const {
  27. int output_len = 0;
  28. switch (level) {
  29. case DEBUG_LVL:
  30. output_len = printf("DEBUG: ");
  31. break;
  32. case INFO_LVL:
  33. output_len = printf("INFO: ");
  34. break;
  35. case WARNING_LVL:
  36. output_len = printf("WARNING: ");
  37. break;
  38. case ERROR_LVL:
  39. output_len = printf("ERROR: ");
  40. break;
  41. case SEVERE_LVL:
  42. output_len = printf("SEVERE: ");
  43. break;
  44. }
  45. output_len += vprintf(fmt, args);
  46. output_len += printf("\n");
  47. return output_len;
  48. }
  49. };
  50. StdoutLogger logger;
  51. class FakeClock : public std::chrono::steady_clock {
  52. private:
  53. static time_point sNow;
  54. public:
  55. static void setNow(time_point newNow) { sNow = newNow; }
  56. static time_point now() noexcept { return sNow; }
  57. };
  58. FakeClock::time_point FakeClock::sNow;
  59. } // namespace
  60. /*
  61. * Test that there are no residual slots when various apps receive successful confirmations.
  62. */
  63. TEST(ConfirmationUIRateLimitingTest, noPenaltyTest) {
  64. auto now = std::chrono::steady_clock::now();
  65. RateLimiting<FakeClock> rateLimiting;
  66. FakeClock::setNow(now);
  67. for (int i = 0; i < 10000; ++i) {
  68. ASSERT_TRUE(rateLimiting.tryPrompt(rand()));
  69. rateLimiting.processResult(ConfirmationResponseCode::OK);
  70. }
  71. ASSERT_EQ(0U, rateLimiting.usedSlots());
  72. }
  73. TEST(ConfirmationUIRateLimitingTest, policyTest) {
  74. using namespace std::chrono_literals;
  75. auto now = std::chrono::steady_clock::now();
  76. RateLimiting<FakeClock> rateLimiting;
  77. FakeClock::setNow(now);
  78. // first three tries are free
  79. for (int i = 0; i < 3; ++i) {
  80. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  81. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  82. }
  83. // throw in a couple of successful confirmations by other apps to make sure there
  84. // is not cross talk
  85. for (int i = 0; i < 10000; ++i) {
  86. uid_t id = rand();
  87. if (id == 20) continue;
  88. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  89. rateLimiting.processResult(ConfirmationResponseCode::OK);
  90. }
  91. // the next three tries get a 30s penalty
  92. for (int i = 3; i < 6; ++i) {
  93. FakeClock::setNow(FakeClock::now() + 29s);
  94. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  95. FakeClock::setNow(FakeClock::now() + 1s);
  96. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  97. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  98. }
  99. // throw in a couple of successful confirmations by other apps to make sure there
  100. // is not cross talk
  101. for (int i = 0; i < 10000; ++i) {
  102. uid_t id = rand();
  103. if (id == 20) continue;
  104. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  105. rateLimiting.processResult(ConfirmationResponseCode::OK);
  106. }
  107. // there after the penalty doubles with each cancellation
  108. for (int i = 6; i < 17; ++i) {
  109. FakeClock::setNow((FakeClock::now() + 60s * (1ULL << (i - 6))) - 1s);
  110. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  111. FakeClock::setNow(FakeClock::now() + 1s);
  112. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  113. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  114. }
  115. // throw in a couple of successful confirmations by other apps to make sure there
  116. // is not cross talk
  117. for (int i = 0; i < 10000; ++i) {
  118. uid_t id = rand();
  119. if (id == 20) continue;
  120. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  121. rateLimiting.processResult(ConfirmationResponseCode::OK);
  122. }
  123. ASSERT_EQ(1U, rateLimiting.usedSlots());
  124. FakeClock::setNow(FakeClock::now() + 24h - 1s);
  125. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  126. // after 24h the counter is forgotten
  127. FakeClock::setNow(FakeClock::now() + 1s);
  128. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  129. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  130. // throw in a couple of successful confirmations by other apps to make sure there
  131. // is not cross talk
  132. for (int i = 0; i < 10000; ++i) {
  133. uid_t id = rand();
  134. if (id == 20) continue;
  135. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  136. rateLimiting.processResult(ConfirmationResponseCode::OK);
  137. }
  138. for (int i = 1; i < 3; ++i) {
  139. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  140. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  141. }
  142. // throw in a couple of successful confirmations by other apps to make sure there
  143. // is not cross talk
  144. for (int i = 0; i < 10000; ++i) {
  145. uid_t id = rand();
  146. if (id == 20) continue;
  147. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  148. rateLimiting.processResult(ConfirmationResponseCode::OK);
  149. }
  150. for (int i = 3; i < 6; ++i) {
  151. FakeClock::setNow(FakeClock::now() + 29s);
  152. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  153. FakeClock::setNow(FakeClock::now() + 1s);
  154. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  155. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  156. }
  157. // throw in a couple of successful confirmations by other apps to make sure there
  158. // is not cross talk
  159. for (int i = 0; i < 10000; ++i) {
  160. uid_t id = rand();
  161. if (id == 20) continue;
  162. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  163. rateLimiting.processResult(ConfirmationResponseCode::OK);
  164. }
  165. for (int i = 6; i < 17; ++i) {
  166. FakeClock::setNow((FakeClock::now() + 60s * (1ULL << (i - 6))) - 1s);
  167. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  168. FakeClock::setNow(FakeClock::now() + 1s);
  169. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  170. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  171. }
  172. // throw in a couple of successful confirmations by other apps to make sure there
  173. // is not cross talk
  174. for (int i = 0; i < 10000; ++i) {
  175. uid_t id = rand();
  176. if (id == 20) continue;
  177. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  178. rateLimiting.processResult(ConfirmationResponseCode::OK);
  179. }
  180. ASSERT_EQ(1U, rateLimiting.usedSlots());
  181. }
  182. TEST(ConfirmationUIRateLimitingTest, rewindTest) {
  183. using namespace std::chrono_literals;
  184. auto now = std::chrono::steady_clock::now();
  185. RateLimiting<FakeClock> rateLimiting;
  186. // first three tries are free
  187. for (int i = 0; i < 3; ++i) {
  188. FakeClock::setNow(now);
  189. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  190. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  191. }
  192. for (int i = 3; i < 6; ++i) {
  193. FakeClock::setNow(FakeClock::now() + 29s);
  194. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  195. FakeClock::setNow(FakeClock::now() + 1s);
  196. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  197. rateLimiting.processResult(ConfirmationResponseCode::Canceled);
  198. }
  199. FakeClock::setNow(FakeClock::now() + 59s);
  200. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  201. FakeClock::setNow(FakeClock::now() + 1s);
  202. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  203. rateLimiting.processResult(ConfirmationResponseCode::Aborted);
  204. FakeClock::setNow(FakeClock::now() - 1s);
  205. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  206. FakeClock::setNow(FakeClock::now() + 1s);
  207. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  208. rateLimiting.processResult(ConfirmationResponseCode::SystemError);
  209. // throw in a couple of successful confirmations by other apps to make sure there
  210. // is not cross talk
  211. for (int i = 0; i < 10000; ++i) {
  212. uid_t id = rand();
  213. if (id == 20) continue;
  214. ASSERT_TRUE(rateLimiting.tryPrompt(id));
  215. rateLimiting.processResult(ConfirmationResponseCode::OK);
  216. }
  217. FakeClock::setNow(FakeClock::now() - 1s);
  218. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  219. FakeClock::setNow(FakeClock::now() + 1s);
  220. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  221. rateLimiting.processResult(ConfirmationResponseCode::UIError);
  222. FakeClock::setNow(FakeClock::now() - 1s);
  223. ASSERT_FALSE(rateLimiting.tryPrompt(20));
  224. FakeClock::setNow(FakeClock::now() + 1s);
  225. ASSERT_TRUE(rateLimiting.tryPrompt(20));
  226. ASSERT_EQ(1U, rateLimiting.usedSlots());
  227. }
  228. } // namespace test
  229. } // namespace keystore