gatekeeper_messages_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2015 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 <string.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <gatekeeper/gatekeeper_messages.h>
  21. using ::gatekeeper::SizedBuffer;
  22. using ::testing::Test;
  23. using ::gatekeeper::EnrollRequest;
  24. using ::gatekeeper::EnrollResponse;
  25. using ::gatekeeper::VerifyRequest;
  26. using ::gatekeeper::VerifyResponse;
  27. using std::cout;
  28. using std::endl;
  29. static const uint32_t USER_ID = 3857;
  30. static SizedBuffer *make_buffer(uint32_t size) {
  31. SizedBuffer *result = new SizedBuffer;
  32. result->length = size;
  33. uint8_t *buffer = new uint8_t[size];
  34. srand(size);
  35. for (uint32_t i = 0; i < size; i++) {
  36. buffer[i] = rand();
  37. }
  38. result->buffer.reset(buffer);
  39. return result;
  40. }
  41. TEST(RoundTripTest, EnrollRequestNullEnrolledNullHandle) {
  42. const uint32_t password_size = 512;
  43. SizedBuffer *provided_password = make_buffer(password_size);
  44. const SizedBuffer *deserialized_password;
  45. // create request, serialize, deserialize, and validate
  46. EnrollRequest msg(USER_ID, NULL, provided_password, NULL);
  47. SizedBuffer serialized_msg(msg.GetSerializedSize());
  48. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  49. EnrollRequest deserialized_msg;
  50. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  51. + serialized_msg.length);
  52. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  53. deserialized_msg.error);
  54. deserialized_password = &deserialized_msg.provided_password;
  55. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  56. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  57. ASSERT_EQ(0, memcmp(msg.provided_password.buffer.get(), deserialized_password->buffer.get(), password_size));
  58. ASSERT_EQ((uint32_t) 0, deserialized_msg.enrolled_password.length);
  59. ASSERT_EQ(NULL, deserialized_msg.enrolled_password.buffer.get());
  60. ASSERT_EQ((uint32_t) 0, deserialized_msg.password_handle.length);
  61. ASSERT_EQ(NULL, deserialized_msg.password_handle.buffer.get());
  62. delete provided_password;
  63. }
  64. TEST(RoundTripTest, EnrollRequestEmptyEnrolledEmptyHandle) {
  65. const uint32_t password_size = 512;
  66. SizedBuffer *provided_password = make_buffer(password_size);
  67. SizedBuffer enrolled;
  68. SizedBuffer handle;
  69. const SizedBuffer *deserialized_password;
  70. // create request, serialize, deserialize, and validate
  71. EnrollRequest msg(USER_ID, &handle, provided_password, &enrolled);
  72. SizedBuffer serialized_msg(msg.GetSerializedSize());
  73. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  74. EnrollRequest deserialized_msg;
  75. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  76. + serialized_msg.length);
  77. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  78. deserialized_msg.error);
  79. deserialized_password = &deserialized_msg.provided_password;
  80. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  81. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  82. ASSERT_EQ(0, memcmp(msg.provided_password.buffer.get(), deserialized_password->buffer.get(), password_size));
  83. ASSERT_EQ((uint32_t) 0, deserialized_msg.enrolled_password.length);
  84. ASSERT_EQ(NULL, deserialized_msg.enrolled_password.buffer.get());
  85. ASSERT_EQ((uint32_t) 0, deserialized_msg.password_handle.length);
  86. ASSERT_EQ(NULL, deserialized_msg.password_handle.buffer.get());
  87. delete provided_password;
  88. }
  89. TEST(RoundTripTest, EnrollRequestNonNullEnrolledOrHandle) {
  90. const uint32_t password_size = 512;
  91. SizedBuffer *provided_password = make_buffer(password_size);
  92. SizedBuffer *enrolled_password = make_buffer(password_size);
  93. SizedBuffer *password_handle = make_buffer(password_size);
  94. const SizedBuffer *deserialized_password;
  95. const SizedBuffer *deserialized_enrolled;
  96. const SizedBuffer *deserialized_handle;
  97. // create request, serialize, deserialize, and validate
  98. EnrollRequest msg(USER_ID, password_handle, provided_password, enrolled_password);
  99. SizedBuffer serialized_msg(msg.GetSerializedSize());
  100. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  101. EnrollRequest deserialized_msg;
  102. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  103. + serialized_msg.length);
  104. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  105. deserialized_msg.error);
  106. deserialized_password = &deserialized_msg.provided_password;
  107. deserialized_enrolled = &deserialized_msg.enrolled_password;
  108. deserialized_handle = &deserialized_msg.password_handle;
  109. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  110. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  111. ASSERT_EQ(0, memcmp(msg.provided_password.buffer.get(), deserialized_password->buffer.get(), password_size));
  112. ASSERT_EQ((uint32_t) password_size, deserialized_enrolled->length);
  113. ASSERT_EQ(0, memcmp(msg.enrolled_password.buffer.get(), deserialized_enrolled->buffer.get(), password_size));
  114. ASSERT_EQ((uint32_t) password_size, deserialized_handle->length);
  115. ASSERT_EQ(0, memcmp(msg.password_handle.buffer.get(), deserialized_handle->buffer.get(), password_size));
  116. delete provided_password;
  117. delete enrolled_password;
  118. delete password_handle;
  119. }
  120. TEST(RoundTripTest, EnrollResponse) {
  121. const uint32_t password_size = 512;
  122. SizedBuffer *enrolled_password = make_buffer(password_size);
  123. const SizedBuffer *deserialized_password;
  124. // create request, serialize, deserialize, and validate
  125. EnrollResponse msg(USER_ID, enrolled_password);
  126. SizedBuffer serialized_msg(msg.GetSerializedSize());
  127. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  128. EnrollResponse deserialized_msg;
  129. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  130. + serialized_msg.length);
  131. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  132. deserialized_msg.error);
  133. deserialized_password = &deserialized_msg.enrolled_password_handle;
  134. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  135. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  136. ASSERT_EQ(0, memcmp(msg.enrolled_password_handle.buffer.get(),
  137. deserialized_password->buffer.get(), password_size));
  138. }
  139. TEST(RoundTripTest, VerifyRequest) {
  140. const uint32_t password_size = 512;
  141. SizedBuffer *provided_password = make_buffer(password_size),
  142. *password_handle = make_buffer(password_size);
  143. const SizedBuffer *deserialized_password;
  144. // create request, serialize, deserialize, and validate
  145. VerifyRequest msg(USER_ID, 1, password_handle, provided_password);
  146. SizedBuffer serialized_msg(msg.GetSerializedSize());
  147. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  148. VerifyRequest deserialized_msg;
  149. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  150. + serialized_msg.length);
  151. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  152. deserialized_msg.error);
  153. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  154. ASSERT_EQ((uint64_t) 1, deserialized_msg.challenge);
  155. deserialized_password = &deserialized_msg.password_handle;
  156. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  157. ASSERT_EQ(0, memcmp(msg.provided_password.buffer.get(), deserialized_password->buffer.get(),
  158. password_size));
  159. deserialized_password = &deserialized_msg.password_handle;
  160. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  161. ASSERT_EQ(0, memcmp(msg.password_handle.buffer.get(), deserialized_password->buffer.get(),
  162. password_size));
  163. }
  164. TEST(RoundTripTest, VerifyResponse) {
  165. const uint32_t password_size = 512;
  166. SizedBuffer *auth_token = make_buffer(password_size);
  167. const SizedBuffer *deserialized_password;
  168. // create request, serialize, deserialize, and validate
  169. VerifyResponse msg(USER_ID, auth_token);
  170. SizedBuffer serialized_msg(msg.GetSerializedSize());
  171. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  172. VerifyResponse deserialized_msg;
  173. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get()
  174. + serialized_msg.length);
  175. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_NONE,
  176. deserialized_msg.error);
  177. ASSERT_EQ(USER_ID, deserialized_msg.user_id);
  178. deserialized_password = &deserialized_msg.auth_token;
  179. ASSERT_EQ((uint32_t) password_size, deserialized_password->length);
  180. ASSERT_EQ(0, memcmp(msg.auth_token.buffer.get(), deserialized_password->buffer.get(),
  181. password_size));
  182. }
  183. TEST(RoundTripTest, VerifyResponseError) {
  184. VerifyResponse msg;
  185. msg.error = gatekeeper::gatekeeper_error_t::ERROR_INVALID;
  186. SizedBuffer serialized_msg(msg.GetSerializedSize());
  187. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  188. VerifyResponse deserialized_msg;
  189. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  190. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_INVALID,
  191. deserialized_msg.error);
  192. }
  193. TEST(RoundTripTest, VerifyRequestError) {
  194. VerifyRequest msg;
  195. msg.error = gatekeeper::gatekeeper_error_t::ERROR_INVALID;
  196. SizedBuffer serialized_msg(msg.GetSerializedSize());
  197. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  198. VerifyRequest deserialized_msg;
  199. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  200. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_INVALID,
  201. deserialized_msg.error);
  202. }
  203. TEST(RoundTripTest, EnrollResponseError) {
  204. EnrollResponse msg;
  205. msg.error = gatekeeper::gatekeeper_error_t::ERROR_INVALID;
  206. SizedBuffer serialized_msg(msg.GetSerializedSize());
  207. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  208. EnrollResponse deserialized_msg;
  209. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  210. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_INVALID,
  211. deserialized_msg.error);
  212. }
  213. TEST(RoundTripTest, EnrollRequestError) {
  214. EnrollRequest msg;
  215. msg.error = gatekeeper::gatekeeper_error_t::ERROR_INVALID;
  216. SizedBuffer serialized_msg(msg.GetSerializedSize());
  217. msg.Serialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  218. EnrollRequest deserialized_msg;
  219. deserialized_msg.Deserialize(serialized_msg.buffer.get(), serialized_msg.buffer.get() + serialized_msg.length);
  220. ASSERT_EQ(gatekeeper::gatekeeper_error_t::ERROR_INVALID,
  221. deserialized_msg.error);
  222. }
  223. uint8_t msgbuf[] = {
  224. 220, 88, 183, 255, 71, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 173, 0, 0, 0, 228, 174, 98, 187, 191, 135, 253, 200, 51, 230, 114, 247, 151, 109,
  226. 237, 79, 87, 32, 94, 5, 204, 46, 154, 30, 91, 6, 103, 148, 254, 129, 65, 171, 228,
  227. 167, 224, 163, 9, 15, 206, 90, 58, 11, 205, 55, 211, 33, 87, 178, 149, 91, 28, 236,
  228. 218, 112, 231, 34, 82, 82, 134, 103, 137, 115, 27, 156, 102, 159, 220, 226, 89, 42, 25,
  229. 37, 9, 84, 239, 76, 161, 198, 72, 167, 163, 39, 91, 148, 191, 17, 191, 87, 169, 179,
  230. 136, 10, 194, 154, 4, 40, 107, 109, 61, 161, 20, 176, 247, 13, 214, 106, 229, 45, 17,
  231. 5, 60, 189, 64, 39, 166, 208, 14, 57, 25, 140, 148, 25, 177, 246, 189, 43, 181, 88,
  232. 204, 29, 126, 224, 100, 143, 93, 60, 57, 249, 55, 0, 87, 83, 227, 224, 166, 59, 214,
  233. 81, 144, 129, 58, 6, 57, 46, 254, 232, 41, 220, 209, 230, 167, 138, 158, 94, 180, 125,
  234. 247, 26, 162, 116, 238, 202, 187, 100, 65, 13, 180, 44, 245, 159, 83, 161, 176, 58, 72,
  235. 236, 109, 105, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  236. 0, 11, 0, 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0,
  237. 0, 32, 3, 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0,
  238. 1, 0, 0, 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112,
  239. 1, 246, 1, 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145,
  240. 1, 0, 96, 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0,
  241. 0, 0, 0, 0, 190, 2, 0, 16, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0,
  242. 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 0, 0, 0, 0, 0, 11, 0,
  243. 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0, 0, 32, 3,
  244. 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0, 1, 0, 0,
  245. 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112, 1, 246, 1,
  246. 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145, 1, 0, 96,
  247. 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0, 0, 0, 0,
  248. 0, 190, 2, 0, 16, 1, 0, 0, 0,
  249. };
  250. /*
  251. * These tests don't have any assertions or expectations. They just try to parse garbage, to see if
  252. * the result will be a crash. This is especially informative when run under Valgrind memcheck.
  253. */
  254. template <typename Message> void parse_garbage() {
  255. Message msg;
  256. uint32_t array_length = sizeof(msgbuf) / sizeof(msgbuf[0]);
  257. const uint8_t* end = msgbuf + array_length;
  258. for (uint32_t i = 0; i < array_length; ++i) {
  259. const uint8_t* begin = msgbuf + i;
  260. const uint8_t* p = begin;
  261. msg.Deserialize(p, end);
  262. }
  263. }
  264. #define GARBAGE_TEST(Message) \
  265. TEST(GarbageTest, Message) { parse_garbage<Message>(); }
  266. GARBAGE_TEST(VerifyRequest);
  267. GARBAGE_TEST(VerifyResponse);
  268. GARBAGE_TEST(EnrollRequest);
  269. GARBAGE_TEST(EnrollResponse);