test_keypin.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (c) 2014-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define KEYPIN_PRIVATE
  5. #include "core/or/or.h"
  6. #include "feature/dirauth/keypin.h"
  7. #include "test/test.h"
  8. static void
  9. test_keypin_parse_line(void *arg)
  10. {
  11. (void)arg;
  12. keypin_ent_t *ent = NULL;
  13. /* Good line */
  14. ent = keypin_parse_journal_line(
  15. "aGVyZSBpcyBhIGdvb2Qgc2hhMSE "
  16. "VGhpcyBlZDI1NTE5IHNjb2ZmcyBhdCB0aGUgc2hhMS4");
  17. tt_assert(ent);
  18. tt_mem_op(ent->rsa_id, OP_EQ, "here is a good sha1!", 20);
  19. tt_mem_op(ent->ed25519_key, OP_EQ, "This ed25519 scoffs at the sha1.", 32);
  20. tor_free(ent); ent = NULL;
  21. /* Good line with extra stuff we will ignore. */
  22. ent = keypin_parse_journal_line(
  23. "aGVyZSBpcyBhIGdvb2Qgc2hhMSE "
  24. "VGhpcyBlZDI1NTE5IHNjb2ZmcyBhdCB0aGUgc2hhMS4helloworld");
  25. tt_assert(ent);
  26. tt_mem_op(ent->rsa_id, OP_EQ, "here is a good sha1!", 20);
  27. tt_mem_op(ent->ed25519_key, OP_EQ, "This ed25519 scoffs at the sha1.", 32);
  28. tor_free(ent); ent = NULL;
  29. /* Bad line: no space in the middle. */
  30. ent = keypin_parse_journal_line(
  31. "aGVyZSBpcyBhIGdvb2Qgc2hhMSE?"
  32. "VGhpcyBlZDI1NTE5IHNjb2ZmcyBhdCB0aGUgc2hhMS4");
  33. tt_ptr_op(ent, OP_EQ, NULL);
  34. /* Bad line: bad base64 in RSA ID */
  35. ent = keypin_parse_journal_line(
  36. "aGVyZSBpcyBhIGdv!2Qgc2hhMSE "
  37. "VGhpcyBlZDI1NTE5IHNjb2ZmcyBhdCB0aGUgc2hhMS4");
  38. tt_ptr_op(ent, OP_EQ, NULL);
  39. /* Bad line: bad base64 in Ed25519 */
  40. ent = keypin_parse_journal_line(
  41. "aGVyZSBpcyBhIGdvb2Qgc2hhMSE "
  42. "VGhpcyBlZDI1NTE5IHNjb2ZmcyB!dCB0aGUgc2hhMS4");
  43. tt_ptr_op(ent, OP_EQ, NULL);
  44. done:
  45. tor_free(ent);
  46. }
  47. static smartlist_t *mock_addent_got = NULL;
  48. static void
  49. mock_addent(keypin_ent_t *ent)
  50. {
  51. smartlist_add(mock_addent_got, ent);
  52. keypin_add_entry_to_map__real(ent);
  53. }
  54. static void
  55. test_keypin_parse_file(void *arg)
  56. {
  57. (void)arg;
  58. mock_addent_got = smartlist_new();
  59. MOCK(keypin_add_entry_to_map, mock_addent);
  60. /* Simple, minimal, correct example. */
  61. const char data1[] =
  62. "PT09PT09PT09PT09PT09PT09PT0 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0\n"
  63. "TG9yYXggaXBzdW0gZ3J1dnZ1bHU cyB0aG5lZWQgYW1ldCwgc25lcmdlbGx5IG9uY2UtbGU\n"
  64. "ciBsZXJraW0sIHNlZCBkbyBiYXI YmFsb290IHRlbXBvciBnbHVwcGl0dXMgdXQgbGFib3I\n"
  65. "ZSBldCB0cnVmZnVsYSBtYWduYSA YWxpcXVhLiBVdCBlbmltIGFkIGdyaWNrbGUtZ3Jhc3M\n"
  66. "dmVuaWFtLCBxdWlzIG1pZmYtbXU ZmZlcmVkIGdhLXp1bXBjbyBsYWJvcmlzIG5pc2kgdXQ\n"
  67. "Y3J1ZmZ1bHVzIGV4IGVhIHNjaGw b3BwaXR5IGNvbnNlcXVhdC4gRHVpcyBhdXRlIHNuYXI\n"
  68. "Z2dsZSBpbiBzd29tZWVzd2FucyA aW4gdm9sdXB0YXRlIGF4ZS1oYWNrZXIgZXNzZSByaXA\n"
  69. "cHVsdXMgY3J1bW1paSBldSBtb28 ZiBudWxsYSBzbnV2di5QTFVHSFBMT1ZFUlhZWlpZLi4\n";
  70. tt_int_op(0, OP_EQ, keypin_load_journal_impl(data1, strlen(data1)));
  71. tt_int_op(8, OP_EQ, smartlist_len(mock_addent_got));
  72. keypin_ent_t *ent = smartlist_get(mock_addent_got, 2);
  73. tt_mem_op(ent->rsa_id, OP_EQ, "r lerkim, sed do bar", 20);
  74. tt_mem_op(ent->ed25519_key, OP_EQ, "baloot tempor gluppitus ut labor", 32);
  75. /* More complex example: weird lines, bogus lines,
  76. duplicate/conflicting lines */
  77. const char data2[] =
  78. "PT09PT09PT09PT09PT09PT09PT0 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0\n"
  79. "# This is a comment.\n"
  80. " \n"
  81. "QXQgdGhlIGVuZCBvZiB0aGUgeWU YXIgS3VycmVta2FybWVycnVrIHNhaWQgdG8gaGltLCA\n"
  82. "IllvdSBoYXZlIG1hZGUgYSBnb28 ZCBiZWdpbm5pbmcuIiBCdXQgbm8gbW9yZS4gV2l6YXI\n"
  83. "\n"
  84. "ZHMgc3BlYWsgdHJ1dGgsIGFuZCA aXQgd2FzIHRydWUgdGhhdCBhbGwgdGhlIG1hc3Rlcgo\n"
  85. "@reserved for a future extension \n"
  86. "eSBvZiBOYW1lcyB0aGF0IEdlZCA aGFkIHRvaWxlZCbyB3aW4gdGhhdCB5ZWFyIHdhcyA\n"
  87. "eSBvZiBOYW1lcyB0aGF0IEdlZCA aGFkIHRvaWxlZCbyB3aW4gdGhhdCB5ZWFyIHdhcy"
  88. "A line too long\n"
  89. "dGhlIG1lcmUgc3RhcnQgb2Ygd2g YXQgaGUgbXVzdCBnbyBvb!BsZWFybmluZy4uLi4uLi4\n"
  90. "ZHMgc3BlYWsgdaJ1dGgsIGFuZCA aXQgd2FzIHRydWUgdGhhdCBhbGwgdGhlIG1hc3Rlcgo\n"
  91. "ZHMgc3BlYWsgdHJ1dGgsIGFuZCA aXQgd2FzIHRydaUgdGhhdCBhbGwgdGhlIG1hc3Rlcgo\n"
  92. ;
  93. tt_int_op(0, OP_EQ, keypin_load_journal_impl(data2, strlen(data2)));
  94. tt_int_op(13, OP_EQ, smartlist_len(mock_addent_got));
  95. ent = smartlist_get(mock_addent_got, 9);
  96. tt_mem_op(ent->rsa_id, OP_EQ, "\"You have made a goo", 20);
  97. tt_mem_op(ent->ed25519_key, OP_EQ, "d beginning.\" But no more. Wizar", 32);
  98. ent = smartlist_get(mock_addent_got, 12);
  99. tt_mem_op(ent->rsa_id, OP_EQ, "ds speak truth, and ", 20);
  100. tt_mem_op(ent->ed25519_key, OP_EQ,
  101. "it was tru\xa5 that all the master\n", 32);
  102. /* File truncated before NL */
  103. const char data3[] =
  104. "Tm8gZHJhZ29uIGNhbiByZXNpc3Q IHRoZSBmYXNjaW5hdGlvbiBvZiByaWRkbGluZyB0YWw";
  105. tt_int_op(0, OP_EQ, keypin_load_journal_impl(data3, strlen(data3)));
  106. tt_int_op(14, OP_EQ, smartlist_len(mock_addent_got));
  107. ent = smartlist_get(mock_addent_got, 13);
  108. tt_mem_op(ent->rsa_id, OP_EQ, "No dragon can resist", 20);
  109. tt_mem_op(ent->ed25519_key, OP_EQ, " the fascination of riddling tal", 32);
  110. done:
  111. keypin_clear();
  112. smartlist_free(mock_addent_got);
  113. }
  114. #define ADD(a,b) keypin_check_and_add((const uint8_t*)(a),\
  115. (const uint8_t*)(b),0)
  116. #define LONE_RSA(a) keypin_check_lone_rsa((const uint8_t*)(a))
  117. static void
  118. test_keypin_add_entry(void *arg)
  119. {
  120. (void)arg;
  121. keypin_clear();
  122. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("ambassadors-at-large",
  123. "bread-and-butter thing-in-itself"));
  124. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("gentleman-adventurer",
  125. "cloak-and-dagger what's-his-face"));
  126. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("ambassadors-at-large",
  127. "bread-and-butter thing-in-itself"));
  128. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("ambassadors-at-large",
  129. "bread-and-butter thing-in-itself"));
  130. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("gentleman-adventurer",
  131. "cloak-and-dagger what's-his-face"));
  132. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("Johnnies-come-lately",
  133. "run-of-the-mill root-mean-square"));
  134. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("gentleman-adventurer",
  135. "hypersentimental closefistedness"));
  136. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("disestablismentarian",
  137. "cloak-and-dagger what's-his-face"));
  138. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("gentleman-adventurer",
  139. "cloak-and-dagger what's-his-face"));
  140. tt_int_op(KEYPIN_NOT_FOUND, OP_EQ, LONE_RSA("Llanfairpwllgwyngyll"));
  141. tt_int_op(KEYPIN_MISMATCH, OP_EQ, LONE_RSA("Johnnies-come-lately"));
  142. done:
  143. keypin_clear();
  144. }
  145. static void
  146. test_keypin_journal(void *arg)
  147. {
  148. (void)arg;
  149. char *contents = NULL;
  150. const char *fname = get_fname("keypin-journal");
  151. tt_int_op(0, OP_EQ, keypin_load_journal(fname)); /* ENOENT is okay */
  152. update_approx_time(1217709000);
  153. tt_int_op(0, OP_EQ, keypin_open_journal(fname));
  154. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("king-of-the-herrings",
  155. "good-for-nothing attorney-at-law"));
  156. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("yellowish-red-yellow",
  157. "salt-and-pepper high-muck-a-muck"));
  158. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("yellowish-red-yellow",
  159. "salt-and-pepper high-muck-a-muck"));
  160. keypin_close_journal();
  161. keypin_clear();
  162. tt_int_op(0, OP_EQ, keypin_load_journal(fname));
  163. update_approx_time(1231041600);
  164. tt_int_op(0, OP_EQ, keypin_open_journal(fname));
  165. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("yellowish-red-yellow",
  166. "salt-and-pepper high-muck-a-muck"));
  167. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("theatre-in-the-round",
  168. "holier-than-thou jack-in-the-box"));
  169. tt_int_op(KEYPIN_ADDED, OP_EQ, ADD("no-deposit-no-return",
  170. "across-the-board will-o-the-wisp"));
  171. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("intellectualizations",
  172. "salt-and-pepper high-muck-a-muck"));
  173. keypin_close_journal();
  174. keypin_clear();
  175. tt_int_op(0, OP_EQ, keypin_load_journal(fname));
  176. update_approx_time(1412278354);
  177. tt_int_op(0, OP_EQ, keypin_open_journal(fname));
  178. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("yellowish-red-yellow",
  179. "salt-and-pepper high-muck-a-muck"));
  180. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("intellectualizations",
  181. "salt-and-pepper high-muck-a-muck"));
  182. tt_int_op(KEYPIN_FOUND, OP_EQ, ADD("theatre-in-the-round",
  183. "holier-than-thou jack-in-the-box"));
  184. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("counterrevolutionary",
  185. "holier-than-thou jack-in-the-box"));
  186. tt_int_op(KEYPIN_MISMATCH, OP_EQ, ADD("no-deposit-no-return",
  187. "floccinaucinihilipilificationism"));
  188. keypin_close_journal();
  189. contents = read_file_to_str(fname, RFTS_BIN, NULL);
  190. tt_assert(contents);
  191. tt_str_op(contents,OP_EQ,
  192. "\n"
  193. "@opened-at 2008-08-02 20:30:00\n"
  194. "a2luZy1vZi10aGUtaGVycmluZ3M Z29vZC1mb3Itbm90aGluZyBhdHRvcm5leS1hdC1sYXc\n"
  195. "eWVsbG93aXNoLXJlZC15ZWxsb3c c2FsdC1hbmQtcGVwcGVyIGhpZ2gtbXVjay1hLW11Y2s\n"
  196. "\n"
  197. "@opened-at 2009-01-04 04:00:00\n"
  198. "dGhlYXRyZS1pbi10aGUtcm91bmQ aG9saWVyLXRoYW4tdGhvdSBqYWNrLWluLXRoZS1ib3g\n"
  199. "bm8tZGVwb3NpdC1uby1yZXR1cm4 YWNyb3NzLXRoZS1ib2FyZCB3aWxsLW8tdGhlLXdpc3A\n"
  200. "\n"
  201. "@opened-at 2014-10-02 19:32:34\n");
  202. done:
  203. tor_free(contents);
  204. keypin_clear();
  205. }
  206. #undef ADD
  207. #undef LONE_RSA
  208. #define TEST(name, flags) \
  209. { #name , test_keypin_ ## name, (flags), NULL, NULL }
  210. struct testcase_t keypin_tests[] = {
  211. TEST( parse_line, 0 ),
  212. TEST( parse_file, TT_FORK ),
  213. TEST( add_entry, TT_FORK ),
  214. TEST( journal, TT_FORK ),
  215. END_OF_TESTCASES
  216. };