test_microdesc.c 31 KB

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