test_microdesc.c 31 KB

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