test_microdesc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* Copyright (c) 2010-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "or.h"
  5. #include "config.h"
  6. #include "dirvote.h"
  7. #include "microdesc.h"
  8. #include "networkstatus.h"
  9. #include "routerlist.h"
  10. #include "routerparse.h"
  11. #include "torcert.h"
  12. #include "test.h"
  13. #ifdef _WIN32
  14. /* For mkdir() */
  15. #include <direct.h>
  16. #else
  17. #include <dirent.h>
  18. #endif /* defined(_WIN32) */
  19. static const char test_md1[] =
  20. "onion-key\n"
  21. "-----BEGIN RSA PUBLIC KEY-----\n"
  22. "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
  23. "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
  24. "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
  25. "-----END RSA PUBLIC KEY-----\n";
  26. static const char test_md2[] =
  27. "onion-key\n"
  28. "-----BEGIN RSA PUBLIC KEY-----\n"
  29. "MIGJAoGBAMIixIowh2DyPmDNMDwBX2DHcYcqdcH1zdIQJZkyV6c6rQHnvbcaDoSg\n"
  30. "jgFSLJKpnGmh71FVRqep+yVB0zI1JY43kuEnXry2HbZCD9UDo3d3n7t015X5S7ON\n"
  31. "bSSYtQGPwOr6Epf96IF6DoQxy4iDnPUAlejuhAG51s1y6/rZQ3zxAgMBAAE=\n"
  32. "-----END RSA PUBLIC KEY-----\n";
  33. static const char test_md3[] =
  34. "@last-listed 2009-06-22\n"
  35. "onion-key\n"
  36. "-----BEGIN RSA PUBLIC KEY-----\n"
  37. "MIGJAoGBAMH3340d4ENNGrqx7UxT+lB7x6DNUKOdPEOn4teceE11xlMyZ9TPv41c\n"
  38. "qj2fRZzfxlc88G/tmiaHshmdtEpklZ740OFqaaJVj4LjPMKFNE+J7Xc1142BE9Ci\n"
  39. "KgsbjGYe2RY261aADRWLetJ8T9QDMm+JngL4288hc8pq1uB/3TAbAgMBAAE=\n"
  40. "-----END RSA PUBLIC KEY-----\n"
  41. "p accept 1-700,800-1000\n"
  42. "family nodeX nodeY nodeZ\n";
  43. static void
  44. test_md_cache(void *data)
  45. {
  46. or_options_t *options = NULL;
  47. microdesc_cache_t *mc = NULL ;
  48. smartlist_t *added = NULL, *wanted = NULL;
  49. microdesc_t *md1, *md2, *md3;
  50. char d1[DIGEST256_LEN], d2[DIGEST256_LEN], d3[DIGEST256_LEN];
  51. const char *test_md3_noannotation = strchr(test_md3, '\n')+1;
  52. time_t time1, time2, time3;
  53. char *fn = NULL, *s = NULL;
  54. (void)data;
  55. options = get_options_mutable();
  56. tt_assert(options);
  57. time1 = time(NULL);
  58. time2 = time(NULL) - 2*24*60*60;
  59. time3 = time(NULL) - 15*24*60*60;
  60. /* Possibly, turn this into a test setup/cleanup pair */
  61. tor_free(options->CacheDirectory);
  62. options->CacheDirectory = tor_strdup(get_fname("md_datadir_test"));
  63. #ifdef _WIN32
  64. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory));
  65. #else
  66. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory, 0700));
  67. #endif
  68. tt_assert(!strcmpstart(test_md3_noannotation, "onion-key"));
  69. crypto_digest256(d1, test_md1, strlen(test_md1), DIGEST_SHA256);
  70. crypto_digest256(d2, test_md2, strlen(test_md1), DIGEST_SHA256);
  71. crypto_digest256(d3, test_md3_noannotation, strlen(test_md3_noannotation),
  72. DIGEST_SHA256);
  73. mc = get_microdesc_cache();
  74. added = microdescs_add_to_cache(mc, test_md1, NULL, SAVED_NOWHERE, 0,
  75. time1, NULL);
  76. tt_int_op(1, OP_EQ, smartlist_len(added));
  77. md1 = smartlist_get(added, 0);
  78. smartlist_free(added);
  79. added = NULL;
  80. wanted = smartlist_new();
  81. added = microdescs_add_to_cache(mc, test_md2, NULL, SAVED_NOWHERE, 0,
  82. time2, wanted);
  83. /* Should fail, since we didn't list test_md2's digest in wanted */
  84. tt_int_op(0, OP_EQ, smartlist_len(added));
  85. smartlist_free(added);
  86. added = NULL;
  87. smartlist_add(wanted, tor_memdup(d2, DIGEST256_LEN));
  88. smartlist_add(wanted, tor_memdup(d3, DIGEST256_LEN));
  89. added = microdescs_add_to_cache(mc, test_md2, NULL, SAVED_NOWHERE, 0,
  90. time2, wanted);
  91. /* Now it can work. md2 should have been added */
  92. tt_int_op(1, OP_EQ, smartlist_len(added));
  93. md2 = smartlist_get(added, 0);
  94. /* And it should have gotten removed from 'wanted' */
  95. tt_int_op(smartlist_len(wanted), OP_EQ, 1);
  96. tt_mem_op(smartlist_get(wanted, 0), OP_EQ, d3, DIGEST256_LEN);
  97. smartlist_free(added);
  98. added = NULL;
  99. added = microdescs_add_to_cache(mc, test_md3, NULL,
  100. SAVED_NOWHERE, 0, -1, NULL);
  101. /* Must fail, since SAVED_NOWHERE precludes annotations */
  102. tt_int_op(0, OP_EQ, smartlist_len(added));
  103. smartlist_free(added);
  104. added = NULL;
  105. added = microdescs_add_to_cache(mc, test_md3_noannotation, NULL,
  106. SAVED_NOWHERE, 0, time3, NULL);
  107. /* Now it can work */
  108. tt_int_op(1, OP_EQ, smartlist_len(added));
  109. md3 = smartlist_get(added, 0);
  110. smartlist_free(added);
  111. added = NULL;
  112. /* Okay. We added 1...3. Let's poke them to see how they look, and make
  113. * sure they're really in the journal. */
  114. tt_ptr_op(md1, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d1));
  115. tt_ptr_op(md2, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d2));
  116. tt_ptr_op(md3, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d3));
  117. tt_int_op(md1->last_listed, OP_EQ, time1);
  118. tt_int_op(md2->last_listed, OP_EQ, time2);
  119. tt_int_op(md3->last_listed, OP_EQ, time3);
  120. tt_int_op(md1->saved_location, OP_EQ, SAVED_IN_JOURNAL);
  121. tt_int_op(md2->saved_location, OP_EQ, SAVED_IN_JOURNAL);
  122. tt_int_op(md3->saved_location, OP_EQ, SAVED_IN_JOURNAL);
  123. tt_int_op(md1->bodylen, OP_EQ, strlen(test_md1));
  124. tt_int_op(md2->bodylen, OP_EQ, strlen(test_md2));
  125. tt_int_op(md3->bodylen, OP_EQ, strlen(test_md3_noannotation));
  126. tt_mem_op(md1->body, OP_EQ, test_md1, strlen(test_md1));
  127. tt_mem_op(md2->body, OP_EQ, test_md2, strlen(test_md2));
  128. tt_mem_op(md3->body, OP_EQ, test_md3_noannotation,
  129. strlen(test_md3_noannotation));
  130. tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs.new",
  131. options->CacheDirectory);
  132. s = read_file_to_str(fn, RFTS_BIN, NULL);
  133. tt_assert(s);
  134. tt_mem_op(md1->body, OP_EQ, s + md1->off, md1->bodylen);
  135. tt_mem_op(md2->body, OP_EQ, s + md2->off, md2->bodylen);
  136. tt_mem_op(md3->body, OP_EQ, s + md3->off, md3->bodylen);
  137. tt_ptr_op(md1->family, OP_EQ, NULL);
  138. tt_ptr_op(md3->family, OP_NE, NULL);
  139. tt_int_op(smartlist_len(md3->family), OP_EQ, 3);
  140. tt_str_op(smartlist_get(md3->family, 0), OP_EQ, "nodeX");
  141. /* Now rebuild the cache! */
  142. tt_int_op(microdesc_cache_rebuild(mc, 1), OP_EQ, 0);
  143. tt_int_op(md1->saved_location, OP_EQ, SAVED_IN_CACHE);
  144. tt_int_op(md2->saved_location, OP_EQ, SAVED_IN_CACHE);
  145. tt_int_op(md3->saved_location, OP_EQ, SAVED_IN_CACHE);
  146. /* The journal should be empty now */
  147. tor_free(s);
  148. s = read_file_to_str(fn, RFTS_BIN, NULL);
  149. tt_str_op(s, OP_EQ, "");
  150. tor_free(s);
  151. tor_free(fn);
  152. /* read the cache. */
  153. tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs",
  154. options->CacheDirectory);
  155. s = read_file_to_str(fn, RFTS_BIN, NULL);
  156. tt_mem_op(md1->body, OP_EQ, s + md1->off, strlen(test_md1));
  157. tt_mem_op(md2->body, OP_EQ, s + md2->off, strlen(test_md2));
  158. tt_mem_op(md3->body, OP_EQ, s + md3->off, strlen(test_md3_noannotation));
  159. /* Okay, now we are going to forget about the cache entirely, and reload it
  160. * from the disk. */
  161. microdesc_free_all();
  162. mc = get_microdesc_cache();
  163. md1 = microdesc_cache_lookup_by_digest256(mc, d1);
  164. md2 = microdesc_cache_lookup_by_digest256(mc, d2);
  165. md3 = microdesc_cache_lookup_by_digest256(mc, d3);
  166. tt_assert(md1);
  167. tt_assert(md2);
  168. tt_assert(md3);
  169. tt_mem_op(md1->body, OP_EQ, s + md1->off, strlen(test_md1));
  170. tt_mem_op(md2->body, OP_EQ, s + md2->off, strlen(test_md2));
  171. tt_mem_op(md3->body, OP_EQ, s + md3->off, strlen(test_md3_noannotation));
  172. tt_int_op(md1->last_listed, OP_EQ, time1);
  173. tt_int_op(md2->last_listed, OP_EQ, time2);
  174. tt_int_op(md3->last_listed, OP_EQ, time3);
  175. /* Okay, now we are going to clear out everything older than a week old.
  176. * In practice, that means md3 */
  177. microdesc_cache_clean(mc, time(NULL)-7*24*60*60, 1/*force*/);
  178. tt_ptr_op(md1, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d1));
  179. tt_ptr_op(md2, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d2));
  180. tt_ptr_op(NULL, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d3));
  181. md3 = NULL; /* it's history now! */
  182. /* rebuild again, make sure it stays gone. */
  183. tt_int_op(microdesc_cache_rebuild(mc, 1), OP_EQ, 0);
  184. tt_ptr_op(md1, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d1));
  185. tt_ptr_op(md2, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d2));
  186. tt_ptr_op(NULL, OP_EQ, microdesc_cache_lookup_by_digest256(mc, d3));
  187. /* Re-add md3, and make sure we can rebuild the cache. */
  188. added = microdescs_add_to_cache(mc, test_md3_noannotation, NULL,
  189. SAVED_NOWHERE, 0, time3, NULL);
  190. tt_int_op(1, OP_EQ, smartlist_len(added));
  191. md3 = smartlist_get(added, 0);
  192. smartlist_free(added);
  193. added = NULL;
  194. tt_int_op(md1->saved_location, OP_EQ, SAVED_IN_CACHE);
  195. tt_int_op(md2->saved_location, OP_EQ, SAVED_IN_CACHE);
  196. tt_int_op(md3->saved_location, OP_EQ, SAVED_IN_JOURNAL);
  197. tt_int_op(microdesc_cache_rebuild(mc, 1), OP_EQ, 0);
  198. tt_int_op(md3->saved_location, OP_EQ, SAVED_IN_CACHE);
  199. done:
  200. if (options)
  201. tor_free(options->CacheDirectory);
  202. microdesc_free_all();
  203. smartlist_free(added);
  204. if (wanted)
  205. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  206. smartlist_free(wanted);
  207. tor_free(s);
  208. tor_free(fn);
  209. }
  210. static const char truncated_md[] =
  211. "@last-listed 2013-08-08 19:02:59\n"
  212. "onion-key\n"
  213. "-----BEGIN RSA PUBLIC KEY-----\n"
  214. "MIGJAoGBAM91vLFNaM+gGhnRIdz2Cm/Kl7Xz0cOobIdVzhS3cKUJfk867hCuTipS\n"
  215. "NveLBzNopvgXKruAAzEj3cACxk6Q8lv5UWOGCD1UolkgsWSE62RBjap44g+oc9J1\n"
  216. "RI9968xOTZw0VaBQg9giEILNXl0djoikQ+5tQRUvLDDa67gpa5Q1AgMBAAE=\n"
  217. "-----END RSA PUBLIC KEY-----\n"
  218. "family @\n";
  219. static void
  220. test_md_cache_broken(void *data)
  221. {
  222. or_options_t *options;
  223. char *fn=NULL;
  224. microdesc_cache_t *mc = NULL;
  225. (void)data;
  226. options = get_options_mutable();
  227. tt_assert(options);
  228. tor_free(options->CacheDirectory);
  229. options->CacheDirectory = tor_strdup(get_fname("md_datadir_test2"));
  230. #ifdef _WIN32
  231. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory));
  232. #else
  233. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory, 0700));
  234. #endif
  235. tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs",
  236. options->CacheDirectory);
  237. write_str_to_file(fn, truncated_md, 1);
  238. mc = get_microdesc_cache();
  239. tt_assert(mc);
  240. done:
  241. if (options)
  242. tor_free(options->CacheDirectory);
  243. tor_free(fn);
  244. microdesc_free_all();
  245. }
  246. /* Generated by chutney. */
  247. static const char test_ri[] =
  248. "router test005r 127.0.0.1 5005 0 7005\n"
  249. "platform Tor 0.2.5.4-alpha-dev on Linux\n"
  250. "protocols Link 1 2 Circuit 1\n"
  251. "published 2014-05-06 22:57:55\n"
  252. "fingerprint 09DE 3BA2 48C2 1C3F 3760 6CD3 8460 43A6 D5EC F59E\n"
  253. "uptime 0\n"
  254. "bandwidth 1073741824 1073741824 0\n"
  255. "extra-info-digest 361F9428F9FA4DD854C03DDBCC159D0D9FA996C9\n"
  256. "onion-key\n"
  257. "-----BEGIN RSA PUBLIC KEY-----\n"
  258. "MIGJAoGBANBJz8Vldl12aFeSMPLiA4nOetLDN0oxU8bB1SDhO7Uu2zdWYVYAF5J0\n"
  259. "st7WvrVy/jA9v/fsezNAPskBanecHRSkdMTpkcgRPMHE7CTGEwIy1Yp1X4bPgDlC\n"
  260. "VCnbs5Pcts5HnWEYNK7qHDAUn+IlmjOO+pTUY8uyq+GQVz6H9wFlAgMBAAE=\n"
  261. "-----END RSA PUBLIC KEY-----\n"
  262. "signing-key\n"
  263. "-----BEGIN RSA PUBLIC KEY-----\n"
  264. "MIGJAoGBANbGUC4802Ke6C3nOVxN0U0HhIRrs32cQFEL4v+UUMJPgjbistHBvOax\n"
  265. "CWVR/sMXM2kKJeGThJ9ZUs2p9dDG4WHPUXgkMqzTTEeeFa7pQKU0brgbmLaJq0Pi\n"
  266. "mxmqC5RkTHa5bQvq6QlSFprAEoovV27cWqBM9jVdV9hyc//6kwPzAgMBAAE=\n"
  267. "-----END RSA PUBLIC KEY-----\n"
  268. "hidden-service-dir\n"
  269. "ntor-onion-key Gg73xH7+kTfT6bi1uNVx9gwQdQas9pROIfmc4NpAdC4=\n"
  270. "reject *:25\n"
  271. "reject *:119\n"
  272. "reject *:135-139\n"
  273. "reject *:445\n"
  274. "reject *:563\n"
  275. "reject *:1214\n"
  276. "reject *:4661-4666\n"
  277. "reject *:6346-6429\n"
  278. "reject *:6699\n"
  279. "reject *:6881-6999\n"
  280. "accept *:*\n"
  281. "router-signature\n"
  282. "-----BEGIN SIGNATURE-----\n"
  283. "ImzX5PF2vRCrG1YzGToyjoxYhgh1vtHEDjmP+tIS/iil1DSnHZNpHSuHp0L1jE9S\n"
  284. "yZyrtKaqpBE/aecAM3j4CWCn/ipnAAQkHcyRLin1bYvqBtRzyopVCRlUhF+uWrLq\n"
  285. "t0xkIE39ss/EwmQr7iIgkdVH4oRIMsjYnFFJBG26nYY=\n"
  286. "-----END SIGNATURE-----\n";
  287. static const char test_ri2[] =
  288. "router test001a 127.0.0.1 5001 0 7001\n"
  289. "identity-ed25519\n"
  290. "-----BEGIN ED25519 CERT-----\n"
  291. "AQQABf/FAf5iDuKCZP2VxnAaQWdklilAh6kaEeFX4z8261Yx2T1/AQAgBADCp8vO\n"
  292. "B8K1F9g2DzwuwvVCnPFLSK1qknVqPpNucHLH9DY7fuIYogBAdz4zHv1qC7RKaMNG\n"
  293. "Jux/tMO2tzPcm62Ky5PjClMQplKUOnZNQ+RIpA3wYCIfUDy/cQnY7XWgNQ0=\n"
  294. "-----END ED25519 CERT-----\n"
  295. "platform Tor 0.2.6.0-alpha-dev on Darwin\n"
  296. "protocols Link 1 2 Circuit 1\n"
  297. "published 2014-10-08 12:58:04\n"
  298. "fingerprint B7E2 7F10 4213 C36F 13E7 E982 9182 845E 4959 97A0\n"
  299. "uptime 0\n"
  300. "bandwidth 1073741824 1073741824 0\n"
  301. "extra-info-digest 568F27331B6D8C73E7024F1EF5D097B90DFC7CDB\n"
  302. "caches-extra-info\n"
  303. "onion-key\n"
  304. "-----BEGIN RSA PUBLIC KEY-----\n"
  305. "MIGJAoGBAL2R8EfubUcahxha4u02P4VAR0llQIMwFAmrHPjzcK7apcQgDOf2ovOA\n"
  306. "+YQnJFxlpBmCoCZC6ssCi+9G0mqo650lFuTMP5I90BdtjotfzESfTykHLiChyvhd\n"
  307. "l0dlqclb2SU/GKem/fLRXH16aNi72CdSUu/1slKs/70ILi34QixRAgMBAAE=\n"
  308. "-----END RSA PUBLIC KEY-----\n"
  309. "signing-key\n"
  310. "-----BEGIN RSA PUBLIC KEY-----\n"
  311. "MIGJAoGBAN8+78KUVlgHXdMMkYJxcwh1Zv2y+Gb5eWUyltUaQRajhrT9ij2T5JZs\n"
  312. "M0g85xTcuM3jNVVpV79+33hiTohdC6UZ+Bk4USQ7WBFzRbVFSXoVKLBJFkCOIexg\n"
  313. "SMGNd5WEDtHWrXl58mizmPFu1eG6ZxHzt7RuLSol5cwBvawXPNkFAgMBAAE=\n"
  314. "-----END RSA PUBLIC KEY-----\n"
  315. "onion-key-crosscert\n"
  316. "-----BEGIN CROSSCERT-----\n"
  317. "ETFDzU49bvNfoZnKK1j6JeBP2gDirgj6bBCgWpUYs663OO9ypbZRO0JwWANssKl6\n"
  318. "oaq9vKTsKGRsaNnqnz/JGMhehymakjjNtqg7crWwsahe8+7Pw9GKmW+YjFtcOkUf\n"
  319. "KfOn2bmKBa1FoJb4yW3oXzHcdlLSRuCciKqPn+Hky5o=\n"
  320. "-----END CROSSCERT-----\n"
  321. "ntor-onion-key-crosscert 0\n"
  322. "-----BEGIN ED25519 CERT-----\n"
  323. "AQoABf2dAcKny84HwrUX2DYPPC7C9UKc8UtIrWqSdWo+k25wcsf0AFohutG+xI06\n"
  324. "Ef21c5Zl1j8Hw6DzHDjYyJevXLFuOneaL3zcH2Ldn4sjrG3kc5UuVvRfTvV120UO\n"
  325. "xk4f5s5LGwY=\n"
  326. "-----END ED25519 CERT-----\n"
  327. "hidden-service-dir\n"
  328. "contact auth1@test.test\n"
  329. "ntor-onion-key hbxdRnfVUJJY7+KcT4E3Rs7/zuClbN3hJrjSBiEGMgI=\n"
  330. "reject *:*\n"
  331. "router-sig-ed25519 5aQXyTif7PExIuL2di37UvktmJECKnils2OWz2vDi"
  332. "hFxi+5TTAAPxYkS5clhc/Pjvw34itfjGmTKFic/8httAQ\n"
  333. "router-signature\n"
  334. "-----BEGIN SIGNATURE-----\n"
  335. "BaUB+aFPQbb3BwtdzKsKqV3+6cRlSqJF5bI3UTmwRoJk+Z5Pz+W5NWokNI0xArHM\n"
  336. "T4T5FZCCP9350jXsUCIvzyIyktU6aVRCGFt76rFlo1OETpN8GWkMnQU0w18cxvgS\n"
  337. "cf34GXHv61XReJF3AlzNHFpbrPOYmowmhrTULKyMqow=\n"
  338. "-----END SIGNATURE-----\n";
  339. static const char test_md_8[] =
  340. "onion-key\n"
  341. "-----BEGIN RSA PUBLIC KEY-----\n"
  342. "MIGJAoGBANBJz8Vldl12aFeSMPLiA4nOetLDN0oxU8bB1SDhO7Uu2zdWYVYAF5J0\n"
  343. "st7WvrVy/jA9v/fsezNAPskBanecHRSkdMTpkcgRPMHE7CTGEwIy1Yp1X4bPgDlC\n"
  344. "VCnbs5Pcts5HnWEYNK7qHDAUn+IlmjOO+pTUY8uyq+GQVz6H9wFlAgMBAAE=\n"
  345. "-----END RSA PUBLIC KEY-----\n"
  346. "p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999\n";
  347. static const char test_md_16[] =
  348. "onion-key\n"
  349. "-----BEGIN RSA PUBLIC KEY-----\n"
  350. "MIGJAoGBANBJz8Vldl12aFeSMPLiA4nOetLDN0oxU8bB1SDhO7Uu2zdWYVYAF5J0\n"
  351. "st7WvrVy/jA9v/fsezNAPskBanecHRSkdMTpkcgRPMHE7CTGEwIy1Yp1X4bPgDlC\n"
  352. "VCnbs5Pcts5HnWEYNK7qHDAUn+IlmjOO+pTUY8uyq+GQVz6H9wFlAgMBAAE=\n"
  353. "-----END RSA PUBLIC KEY-----\n"
  354. "ntor-onion-key Gg73xH7+kTfT6bi1uNVx9gwQdQas9pROIfmc4NpAdC4=\n"
  355. "p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999\n";
  356. static const char test_md_18[] =
  357. "onion-key\n"
  358. "-----BEGIN RSA PUBLIC KEY-----\n"
  359. "MIGJAoGBANBJz8Vldl12aFeSMPLiA4nOetLDN0oxU8bB1SDhO7Uu2zdWYVYAF5J0\n"
  360. "st7WvrVy/jA9v/fsezNAPskBanecHRSkdMTpkcgRPMHE7CTGEwIy1Yp1X4bPgDlC\n"
  361. "VCnbs5Pcts5HnWEYNK7qHDAUn+IlmjOO+pTUY8uyq+GQVz6H9wFlAgMBAAE=\n"
  362. "-----END RSA PUBLIC KEY-----\n"
  363. "ntor-onion-key Gg73xH7+kTfT6bi1uNVx9gwQdQas9pROIfmc4NpAdC4=\n"
  364. "p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999\n"
  365. "id rsa1024 Cd47okjCHD83YGzThGBDptXs9Z4\n";
  366. static const char test_md2_18[] =
  367. "onion-key\n"
  368. "-----BEGIN RSA PUBLIC KEY-----\n"
  369. "MIGJAoGBAL2R8EfubUcahxha4u02P4VAR0llQIMwFAmrHPjzcK7apcQgDOf2ovOA\n"
  370. "+YQnJFxlpBmCoCZC6ssCi+9G0mqo650lFuTMP5I90BdtjotfzESfTykHLiChyvhd\n"
  371. "l0dlqclb2SU/GKem/fLRXH16aNi72CdSUu/1slKs/70ILi34QixRAgMBAAE=\n"
  372. "-----END RSA PUBLIC KEY-----\n"
  373. "ntor-onion-key hbxdRnfVUJJY7+KcT4E3Rs7/zuClbN3hJrjSBiEGMgI=\n"
  374. "id rsa1024 t+J/EEITw28T5+mCkYKEXklZl6A\n";
  375. static const char test_md2_21[] =
  376. "onion-key\n"
  377. "-----BEGIN RSA PUBLIC KEY-----\n"
  378. "MIGJAoGBAL2R8EfubUcahxha4u02P4VAR0llQIMwFAmrHPjzcK7apcQgDOf2ovOA\n"
  379. "+YQnJFxlpBmCoCZC6ssCi+9G0mqo650lFuTMP5I90BdtjotfzESfTykHLiChyvhd\n"
  380. "l0dlqclb2SU/GKem/fLRXH16aNi72CdSUu/1slKs/70ILi34QixRAgMBAAE=\n"
  381. "-----END RSA PUBLIC KEY-----\n"
  382. "ntor-onion-key hbxdRnfVUJJY7+KcT4E3Rs7/zuClbN3hJrjSBiEGMgI=\n"
  383. "id ed25519 wqfLzgfCtRfYNg88LsL1QpzxS0itapJ1aj6TbnByx/Q\n";
  384. static void
  385. test_md_generate(void *arg)
  386. {
  387. routerinfo_t *ri;
  388. microdesc_t *md = NULL;
  389. (void)arg;
  390. ri = router_parse_entry_from_string(test_ri, NULL, 0, 0, NULL, NULL);
  391. tt_assert(ri);
  392. md = dirvote_create_microdescriptor(ri, 8);
  393. tt_str_op(md->body, OP_EQ, test_md_8);
  394. /* XXXX test family lines. */
  395. /* XXXX test method 14 for A lines. */
  396. /* XXXX test method 15 for P6 lines. */
  397. microdesc_free(md);
  398. md = NULL;
  399. md = dirvote_create_microdescriptor(ri, 16);
  400. tt_str_op(md->body, OP_EQ, test_md_16);
  401. microdesc_free(md);
  402. md = NULL;
  403. md = dirvote_create_microdescriptor(ri, 18);
  404. tt_str_op(md->body, OP_EQ, test_md_18);
  405. microdesc_free(md);
  406. md = NULL;
  407. md = dirvote_create_microdescriptor(ri, 21);
  408. tt_str_op(md->body, OP_EQ, test_md_18);
  409. routerinfo_free(ri);
  410. ri = router_parse_entry_from_string(test_ri2, NULL, 0, 0, NULL, NULL);
  411. microdesc_free(md);
  412. md = NULL;
  413. md = dirvote_create_microdescriptor(ri, 18);
  414. tt_str_op(md->body, OP_EQ, test_md2_18);
  415. microdesc_free(md);
  416. md = NULL;
  417. md = dirvote_create_microdescriptor(ri, 21);
  418. tt_str_op(md->body, OP_EQ, test_md2_21);
  419. tt_assert(ed25519_pubkey_eq(md->ed25519_identity_pkey,
  420. &ri->cache_info.signing_key_cert->signing_key));
  421. done:
  422. microdesc_free(md);
  423. routerinfo_free(ri);
  424. }
  425. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  426. DISABLE_GCC_WARNING(overlength-strings)
  427. /* We allow huge string constants in the unit tests, but not in the code
  428. * at large. */
  429. #endif
  430. /* Taken at random from my ~/.tor/cached-microdescs file and then
  431. * hand-munged */
  432. static const char MD_PARSE_TEST_DATA[] =
  433. /* Good 0 */
  434. "onion-key\n"
  435. "-----BEGIN RSA PUBLIC KEY-----\n"
  436. "MIGJAoGBANsKd1GRfOuSR1MkcwKqs6SVy4Gi/JXplt/bHDkIGm6Q96TeJ5uyVgUL\n"
  437. "DBr/ij6+JqgVFeriuiMzHKREytzjdaTuKsKBFFpLwb+Ppcjr5nMIH/AR6/aHO8hW\n"
  438. "T3B9lx5T6Kl7CqZ4yqXxYRHzn50EPTIZuz0y9se4J4gi9mLmL+pHAgMBAAE=\n"
  439. "-----END RSA PUBLIC KEY-----\n"
  440. "p accept 20-23,43,53,79-81,88,110,143,194,220,443,464,531,543-544\n"
  441. "id rsa1024 GEo59/iR1GWSIWZDzXTd5QxtqnU\n"
  442. /* Bad 0: I've messed with the onion-key in the second one. */
  443. "onion-key\n"
  444. "-----BEGIN RSA PUBLIC KEY-----\n"
  445. "MIGJAoGBAMr4o/pflVwscx11vC1AKEADlKEqnhpvCIjAEzNEenMhvGQHRlA0EXLC\n"
  446. "7G7O5bhnCwEHqK8Pvg8cuX/fD8v08TF1EVPhwPa0UI6ab8KnPP2F!!!!!!b92DG7EQIk3q\n"
  447. "d68Uxp7E9/t3v1WWZjzDqvEe0par6ul+DKW6HMlTGebFo5Q4e8R1AgMBAAE=\n"
  448. "-----END RSA PUBLIC KEY-----\n"
  449. "ntor-onion-key 761Dmm27via7lXygNHM3l+oJLrYU2Nye0Uz4pkpipyY=\n"
  450. "p accept 53\n"
  451. "id rsa1024 3Y4fwXhtgkdGDZ5ef5mtb6TJRQQ\n"
  452. /* Good 1 */
  453. "onion-key\n"
  454. "-----BEGIN RSA PUBLIC KEY-----\n"
  455. "MIGJAoGBANsMSjVi3EX8ZHfm/dvPF6KdVR66k1tVul7Jp+dDbDajBYNhgKRzVCxy\n"
  456. "Yac1CBuQjOqK89tKap9PQBnhF087eDrfaZDqYTLwB2W2sBJncVej15WEPXPRBifo\n"
  457. "iFZ8337kgczkaY+IOfSuhtbOUyDOoDpRJheIKBNq0ZiTqtLbbadVAgMBAAE=\n"
  458. "-----END RSA PUBLIC KEY-----\n"
  459. "ntor-onion-key ncfiHJjSgdDEW/gc6q6/7idac7j+x7ejQrRm6i75pGA=\n"
  460. "p accept 443,6660-6669,6697,7000-7001\n"
  461. "id rsa1024 XXuLzw3mfBELEq3veXoNhdehwD4\n"
  462. /* Good 2 */
  463. "onion-key\n"
  464. "-----BEGIN RSA PUBLIC KEY-----\n"
  465. "MIGJAoGBANQfBlrHrh9F/CAOytrNFgi0ikWMW/HZxuoszF9X+AQ+MudR8bcxxOGl\n"
  466. "1RFwb74s8E3uuzrCkNFvSw9Ar1L02F2DOX0gLsxEGuYC4Ave9NUteGqSqDyEJQUJ\n"
  467. "KlfxCPn2qC9nvNT7wR/Dg2WRvAEKnJmkpb57N3+WSAOPLjKOFEz3AgMBAAE=\n"
  468. "-----END RSA PUBLIC KEY-----\n"
  469. "ntor-onion-key AppBt6CSeb1kKid/36ototmFA24ddfW5JpjWPLuoJgs=\n"
  470. "id rsa1024 6y60AEI9a1PUUlRPO0YQT9WzrjI\n"
  471. /* Bad 1: Here I've messed with the ntor key */
  472. "onion-key\n"
  473. "-----BEGIN RSA PUBLIC KEY-----\n"
  474. "MIGJAoGBAPjy2HacU3jDNO5nTOFGSwNa0qKCNn4yhtrDVcAJ5alIQeBWZZGJLZ0q\n"
  475. "Cqylw1vYqxu8E09g+QXXFbAgBv1U9TICaATxrIJhIJzc8TJPhqJemp1kq0DvHLDx\n"
  476. "mxwlkNnCD/P5NS+JYB3EjOlU9EnSKUWNU61+Co344m2JqhEau40vAgMBAAE=\n"
  477. "-----END RSA PUBLIC KEY-----\n"
  478. "ntor-onion-key 4i2Fp9JHTUr1uQs0pxD5j5spl4/RG56S2P0gQxU=\n"
  479. "id rsa1024 nMRmNEGysA0NmlALVaUmI7D5jLU\n"
  480. /* Good 3: I've added a weird token in this one. This shouldn't prevent
  481. * it parsing */
  482. "onion-key\n"
  483. "-----BEGIN RSA PUBLIC KEY-----\n"
  484. "MIGJAoGBAKmosxudyNA/yJNz3S890VqV/ebylzoD11Sc0b/d5tyNNaNZjcYy5vRD\n"
  485. "kwyxFRMbP2TLZQ1zRfNwY7IDnYjU2SbW0pxuM6M8WRtsmx/YOE3kHMVAFJNrTUqU\n"
  486. "6D1zB3IiRDS5q5+NoRxwqo+hYUck60O3WTwEoqb+l3lvXeu7z9rFAgMBAAE=\n"
  487. "-----END RSA PUBLIC KEY-----\n"
  488. "flux-capacitor 1.21 GW\n"
  489. "ntor-onion-key MWBoEkl+RlBiGX44XKIvTSqbznTNZStOmUYtcYRQQyY=\n"
  490. "id rsa1024 R+A5O9qRvRac4FT3C4L2QnFyxsc\n"
  491. /* Good 4: Here I've made the 'id rsa' token odd. It should still parse
  492. * just fine. */
  493. "onion-key\n"
  494. "-----BEGIN RSA PUBLIC KEY-----\n"
  495. "MIGJAoGBAOh+WMkdNe/Pkjb8UjQyfLOlFgpuVFrxAIGnJsmWWx0yBE97DQxGyh2n\n"
  496. "h8G5OJZHRarJQyCIf7vpZQAi0oP0OkGGaCaDQsM+D8TnqhnU++RWGnMqY/cXxPrL\n"
  497. "MEq+n6aGiLmzkO7ah8yorZpoREk4GqLUIN89/tHHGOhJL3c4CPGjAgMBAAE=\n"
  498. "-----END RSA PUBLIC KEY-----\n"
  499. "p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999\n"
  500. "id rsa1234 jlqAKFD2E7uMKv+8TmKSeo7NBho\n"
  501. /* Good 5: Extra id type. */
  502. "onion-key\n"
  503. "-----BEGIN RSA PUBLIC KEY-----\n"
  504. "MIGJAoGBAMdgPPc5uaw4y/q+SUTN/I8Y+Gvdx9kKgWV4dmDGJ0mxsVZmo1v6+v3F\n"
  505. "12M2f9m99G3WB8F8now29C+9XyEv8MBHj1lHRdUFHSQes3YTFvDNlgj+FjLqO5TJ\n"
  506. "adOOmfu4DCUUtUEDyQKbNVL4EkMTXY73omTVsjcH3xxFjTx5wixhAgMBAAE=\n"
  507. "-----END RSA PUBLIC KEY-----\n"
  508. "ntor-onion-key AAVnWZcnDbxasdZwKqb4fL6O9sZV+XsRNHTpNd1YMz8=\n"
  509. "id rsa1024 72EfBL11QuwX2vU8y+p9ExGfGEg\n"
  510. "id expolding hedgehog 0+A5O9qRvRac4FT3C4L2QnFyxsc\n"
  511. /* Good 6: I've given this a bogus policy. It should parse. */
  512. "onion-key\n"
  513. "-----BEGIN RSA PUBLIC KEY-----\n"
  514. "MIGJAoGBALNuufwhPMF8BooxYMNvhYJMPqUB8hQDt8wGmPKphJcD1sVD1i4gAZM2\n"
  515. "HIo+zUBlljDrRWL5NzVzd1yxUJAiQxvXS5dRRFY3B70M7wTVpXw53xe0/BM5t1AX\n"
  516. "n0MFk7Jl6XIKMlzRalZvmMvE/odtyWXkP4Nd1MyZ1QcIwrQ2iwyrAgMBAAE=\n"
  517. "-----END RSA PUBLIC KEY-----\n"
  518. "p condone 1-10\n"
  519. "ntor-onion-key 2/nMJ+L4dd/2GpMyTYjz3zC59MvQy4MIzJZhdzKHekg=\n"
  520. "id rsa1024 FHyh10glEMA6MCmBb5R9Y+X/MhQ\n"
  521. /* Good 7: I've given this one another sort of odd policy. Should parse. */
  522. "onion-key\n"
  523. "-----BEGIN RSA PUBLIC KEY-----\n"
  524. "MIGJAoGBAKcd3FmQ8iAADghyvX8eca0ePqtJ2w1IDdUdTlf5Y/8+OMdp//sD01yC\n"
  525. "YmiX45LK5ge1O3AzcakYCO6fb3pyIqvXdvm24OjyYZELQ40cmKSLjdhcSf4Fr/N9\n"
  526. "uR/CkknR9cEePu1wZ5WBIGmGdXI6s7t3LB+e7XFyBYAx6wMGlnX7AgMBAAE=\n"
  527. "-----END RSA PUBLIC KEY-----\n"
  528. "p accept frogs-mice\n"
  529. "ntor-onion-key AMxvhaQ1Qg7jBJFoyHuPRgETvLbFmJ194hExV24FuAI=\n"
  530. "family $D8CFEA0D996F5D1473D2063C041B7910DB23981E\n"
  531. "id rsa1024 d0VVZC/cHh1P3y4MMbfKlQHFycc\n"
  532. /* Good 8: This one has the ntor-onion-key without terminating =. That's
  533. * allowed. */
  534. "onion-key\n"
  535. "-----BEGIN RSA PUBLIC KEY-----\n"
  536. "MIGJAoGBAL438YfjrJE2SPqkkXeQwICygu8KNO54Juj6sjqk5hgsiazIWMOBgbaX\n"
  537. "LIRqPNGaiSq01xSqwjwCBCfwZYT/nSdDBqj1h9aoR8rnjxZjyQ+m3rWpdDqeCDMx\n"
  538. "I3NgZ5w4bNX4poRb42lrV6NmQiFdjzpqszVbv5Lpn2CSKu32CwKVAgMBAAE=\n"
  539. "-----END RSA PUBLIC KEY-----\n"
  540. "ntor-onion-key UKL6Dnj2KwYsFlkCvOkXVatxvOPB4MaxqwPQQgZMTwI\n"
  541. "id rsa1024 FPIXc6k++JnKCtSKWUxaR6oXEKs\n"
  542. /* Good 9: Another totally normal one.*/
  543. "onion-key\n"
  544. "-----BEGIN RSA PUBLIC KEY-----\n"
  545. "MIGJAoGBANNGIKRd8PFNXkJ2JPV1ohDMFNbJwKbwybeieaQFjtU9KWedHCbr+QD4\n"
  546. "B6zNY5ysguNjHNnlq2f6D09+uhnfDBON8tAz0mPQH/6JqnOXm+EiUn+8bN0E8Nke\n"
  547. "/i3GEgDeaxJJMNQcpsJvmmSmKFOlYy9Fy7ejAjTGqtAnqOte7BnTAgMBAAE=\n"
  548. "-----END RSA PUBLIC KEY-----\n"
  549. "ntor-onion-key gUsq3e5iYgsQQvyxINtLzBpHxmIt5rtuFlEbKfI4gFk=\n"
  550. "id rsa1024 jv+LdatDzsMfEW6pLBeL/5uzwCc\n"
  551. /* Bad 2: RSA key has bad exponent of 3. */
  552. "onion-key\n"
  553. "-----BEGIN RSA PUBLIC KEY-----\n"
  554. "MIGHAoGBAMMTWtvPxYnUNJ5Y7B+XENcpxzPoGstrdiUszCBS+/42xvluLJ+JDSdR\n"
  555. "qJaMD6ax8vKAeLS5C6O17MNdG2VldlPRbtgl41MXsOoUqEJ+nY9e3WG9Snjp47xC\n"
  556. "zmWIfeduXSavIsb3a43/MLIz/9qO0TkgAAiuQr79JlwKhLdzCqTLAgED\n"
  557. "-----END RSA PUBLIC KEY-----\n"
  558. "ntor-onion-key NkRB4wTUFogiVp5jYmjGORe2ffb/y5Kk8Itw8jdzMjA=\n"
  559. "p reject 25,119,135-139,445,563,1214,4661-4666,6346-6429,6699,6881-6999\n"
  560. "id rsa1024 fKvYjP7TAjCC1FzYee5bYAwYkoDg\n"
  561. /* Bad 3: Bogus annotation */
  562. "@last-listed with strange aeons\n"
  563. "onion-key\n"
  564. "-----BEGIN RSA PUBLIC KEY-----\n"
  565. "MIGJAoGBALcRBFNCZtpd2TFJysU77/fJMFzKisRQEBOtDGtTZ2Bg4aEGosssa0Id\n"
  566. "YtUagRLYle08QVGvGB+EHBI5qf6Ah2yPH7k5QiN2a3Sq+nyh85dXKPazBGBBbM+C\n"
  567. "DOfDauV02CAnADNMLJEf1voY3oBVvYyIsmHxn5i1R19ZYIiR8NX5AgMBAAE=\n"
  568. "-----END RSA PUBLIC KEY-----\n"
  569. "ntor-onion-key m4xcFXMWMjCvZDXq8FT3XmS0EHYseGOeu+fV+6FYDlk=\n"
  570. "p accept 20-23,43,53,79-81,88,110,143,194,220,389,443,464,531,543-544\n"
  571. "id rsa1024 SSbfNE9vmaiwRKH+eqNAkiKQhds\n"
  572. /* Good 10: Normal, with added ipv6 address and added other address */
  573. "onion-key\n"
  574. "-----BEGIN RSA PUBLIC KEY-----\n"
  575. "MIGJAoGBAM7uUtq5F6h63QNYIvC+4NcWaD0DjtnrOORZMkdpJhinXUOwce3cD5Dj\n"
  576. "sgdN1wJpWpTQMXJ2DssfSgmOVXETP7qJuZyRprxalQhaEATMDNJA/66Ml1jSO9mZ\n"
  577. "+8Xb7m/4q778lNtkSbsvMaYD2Dq6k2QQ3kMhr9z8oUtX0XA23+pfAgMBAAE=\n"
  578. "-----END RSA PUBLIC KEY-----\n"
  579. "a [::1:2:3:4]:9090\n"
  580. "a 18.0.0.1:9999\n"
  581. "ntor-onion-key k2yFqTU2vzMCQDEiE/j9UcEHxKrXMLpB3IL0or09sik=\n"
  582. "id rsa1024 2A8wYpHxnkKJ92orocvIQBzeHlE\n"
  583. "p6 allow 80\n"
  584. ;
  585. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  586. ENABLE_GCC_WARNING(overlength-strings)
  587. #endif
  588. /** More tests for parsing different kinds of microdescriptors, and getting
  589. * invalid digests trackd from them. */
  590. static void
  591. test_md_parse(void *arg)
  592. {
  593. (void) arg;
  594. char *mem_op_hex_tmp = NULL;
  595. smartlist_t *invalid = smartlist_new();
  596. smartlist_t *mds = microdescs_parse_from_string(MD_PARSE_TEST_DATA,
  597. NULL, 1, SAVED_NOWHERE,
  598. invalid);
  599. tt_int_op(smartlist_len(mds), OP_EQ, 11);
  600. tt_int_op(smartlist_len(invalid), OP_EQ, 4);
  601. test_memeq_hex(smartlist_get(invalid,0),
  602. "5d76bf1c6614e885614a1e0ad074e1ab"
  603. "4ea14655ebeefb1736a71b5ed8a15a51");
  604. test_memeq_hex(smartlist_get(invalid,1),
  605. "2fde0ee3343669c2444cd9d53cbd39c6"
  606. "a7d1fc0513513e840ca7f6e68864b36c");
  607. test_memeq_hex(smartlist_get(invalid,2),
  608. "20d1576c5ab11bbcff0dedb1db4a3cfc"
  609. "c8bc8dd839d8cbfef92d00a1a7d7b294");
  610. test_memeq_hex(smartlist_get(invalid,3),
  611. "074770f394c73dbde7b44412e9692add"
  612. "691a478d4727f9804b77646c95420a96");
  613. /* Spot-check the valid ones. */
  614. const microdesc_t *md = smartlist_get(mds, 5);
  615. test_memeq_hex(md->digest,
  616. "54bb6d733ddeb375d2456c79ae103961"
  617. "da0cae29620375ac4cf13d54da4d92b3");
  618. tt_int_op(md->last_listed, OP_EQ, 0);
  619. tt_int_op(md->saved_location, OP_EQ, SAVED_NOWHERE);
  620. tt_int_op(md->no_save, OP_EQ, 0);
  621. tt_uint_op(md->held_in_map, OP_EQ, 0);
  622. tt_uint_op(md->held_by_nodes, OP_EQ, 0);
  623. tt_assert(md->onion_curve25519_pkey);
  624. md = smartlist_get(mds, 6);
  625. test_memeq_hex(md->digest,
  626. "53f740bd222ab37f19f604b1d3759aa6"
  627. "5eff1fbce9ac254bd0fa50d4af9b1bae");
  628. tt_assert(! md->exit_policy);
  629. md = smartlist_get(mds, 8);
  630. test_memeq_hex(md->digest,
  631. "a0a155562d8093d8fd0feb7b93b7226e"
  632. "17f056c2142aab7a4ea8c5867a0376d5");
  633. tt_assert(md->onion_curve25519_pkey);
  634. md = smartlist_get(mds, 10);
  635. test_memeq_hex(md->digest,
  636. "409ebd87d23925a2732bd467a92813c9"
  637. "21ca378fcb9ca193d354c51550b6d5e9");
  638. tt_assert(tor_addr_family(&md->ipv6_addr) == AF_INET6);
  639. tt_int_op(md->ipv6_orport, OP_EQ, 9090);
  640. done:
  641. SMARTLIST_FOREACH(mds, microdesc_t *, mdsc, microdesc_free(mdsc));
  642. smartlist_free(mds);
  643. SMARTLIST_FOREACH(invalid, char *, cp, tor_free(cp));
  644. smartlist_free(invalid);
  645. tor_free(mem_op_hex_tmp);
  646. }
  647. static int mock_rgsbd_called = 0;
  648. static routerstatus_t *mock_rgsbd_val_a = NULL;
  649. static routerstatus_t *mock_rgsbd_val_b = NULL;
  650. static routerstatus_t *
  651. mock_router_get_status_by_digest(networkstatus_t *c, const char *d)
  652. {
  653. (void) c;
  654. ++mock_rgsbd_called;
  655. if (fast_memeq(d, "\x5d\x76", 2)) {
  656. memcpy(mock_rgsbd_val_a->descriptor_digest, d, 32);
  657. return mock_rgsbd_val_a;
  658. } else if (fast_memeq(d, "\x20\xd1", 2)) {
  659. memcpy(mock_rgsbd_val_b->descriptor_digest, d, 32);
  660. return mock_rgsbd_val_b;
  661. } else {
  662. return NULL;
  663. }
  664. }
  665. static networkstatus_t *mock_ns_val = NULL;
  666. static networkstatus_t *
  667. mock_ns_get_by_flavor(consensus_flavor_t f)
  668. {
  669. (void)f;
  670. return mock_ns_val;
  671. }
  672. static void
  673. test_md_reject_cache(void *arg)
  674. {
  675. (void) arg;
  676. microdesc_cache_t *mc = NULL ;
  677. smartlist_t *added = NULL, *wanted = smartlist_new();
  678. or_options_t *options = get_options_mutable();
  679. char buf[DIGEST256_LEN];
  680. tor_free(options->CacheDirectory);
  681. options->CacheDirectory = tor_strdup(get_fname("md_datadir_test_rej"));
  682. mock_rgsbd_val_a = tor_malloc_zero(sizeof(routerstatus_t));
  683. mock_rgsbd_val_b = tor_malloc_zero(sizeof(routerstatus_t));
  684. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  685. mock_ns_val->valid_after = time(NULL) - 86400;
  686. mock_ns_val->valid_until = time(NULL) + 86400;
  687. mock_ns_val->flavor = FLAV_MICRODESC;
  688. #ifdef _WIN32
  689. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory));
  690. #else
  691. tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory, 0700));
  692. #endif
  693. MOCK(router_get_mutable_consensus_status_by_descriptor_digest,
  694. mock_router_get_status_by_digest);
  695. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  696. mc = get_microdesc_cache();
  697. #define ADD(hex) \
  698. do { \
  699. tt_int_op(sizeof(buf),OP_EQ,base16_decode(buf,sizeof(buf), \
  700. hex,strlen(hex)));\
  701. smartlist_add(wanted, tor_memdup(buf, DIGEST256_LEN)); \
  702. } while (0)
  703. /* invalid,0 */
  704. ADD("5d76bf1c6614e885614a1e0ad074e1ab4ea14655ebeefb1736a71b5ed8a15a51");
  705. /* invalid,2 */
  706. ADD("20d1576c5ab11bbcff0dedb1db4a3cfcc8bc8dd839d8cbfef92d00a1a7d7b294");
  707. /* valid, 6 */
  708. ADD("53f740bd222ab37f19f604b1d3759aa65eff1fbce9ac254bd0fa50d4af9b1bae");
  709. /* valid, 8 */
  710. ADD("a0a155562d8093d8fd0feb7b93b7226e17f056c2142aab7a4ea8c5867a0376d5");
  711. added = microdescs_add_to_cache(mc, MD_PARSE_TEST_DATA, NULL,
  712. SAVED_NOWHERE, 0, time(NULL), wanted);
  713. tt_int_op(smartlist_len(added), OP_EQ, 2);
  714. tt_int_op(mock_rgsbd_called, OP_EQ, 2);
  715. tt_int_op(mock_rgsbd_val_a->dl_status.n_download_failures, OP_EQ, 255);
  716. tt_int_op(mock_rgsbd_val_b->dl_status.n_download_failures, OP_EQ, 255);
  717. done:
  718. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  719. UNMOCK(router_get_mutable_consensus_status_by_descriptor_digest);
  720. tor_free(options->CacheDirectory);
  721. microdesc_free_all();
  722. smartlist_free(added);
  723. SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
  724. smartlist_free(wanted);
  725. tor_free(mock_rgsbd_val_a);
  726. tor_free(mock_rgsbd_val_b);
  727. tor_free(mock_ns_val);
  728. }
  729. static void
  730. test_md_corrupt_desc(void *arg)
  731. {
  732. char *cp = NULL;
  733. smartlist_t *sl = NULL;
  734. (void) arg;
  735. sl = microdescs_add_to_cache(get_microdesc_cache(),
  736. "@last-listed 2015-06-22 10:00:00\n"
  737. "onion-k\n",
  738. NULL, SAVED_IN_JOURNAL, 0, time(NULL), NULL);
  739. tt_int_op(smartlist_len(sl), OP_EQ, 0);
  740. smartlist_free(sl);
  741. sl = microdescs_add_to_cache(get_microdesc_cache(),
  742. "@last-listed 2015-06-22 10:00:00\n"
  743. "wiggly\n",
  744. NULL, SAVED_IN_JOURNAL, 0, time(NULL), NULL);
  745. tt_int_op(smartlist_len(sl), OP_EQ, 0);
  746. smartlist_free(sl);
  747. tor_asprintf(&cp, "%s\n%s", test_md1, "@foobar\nonion-wobble\n");
  748. sl = microdescs_add_to_cache(get_microdesc_cache(),
  749. cp, cp+strlen(cp),
  750. SAVED_IN_JOURNAL, 0, time(NULL), NULL);
  751. tt_int_op(smartlist_len(sl), OP_EQ, 0);
  752. done:
  753. tor_free(cp);
  754. smartlist_free(sl);
  755. }
  756. struct testcase_t microdesc_tests[] = {
  757. { "cache", test_md_cache, TT_FORK, NULL, NULL },
  758. { "broken_cache", test_md_cache_broken, TT_FORK, NULL, NULL },
  759. { "generate", test_md_generate, 0, NULL, NULL },
  760. { "parse", test_md_parse, 0, NULL, NULL },
  761. { "reject_cache", test_md_reject_cache, TT_FORK, NULL, NULL },
  762. { "corrupt_desc", test_md_corrupt_desc, TT_FORK, NULL, NULL },
  763. END_OF_TESTCASES
  764. };