test_controller.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /* Copyright (c) 2015-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONTROL_PRIVATE
  4. #include "or.h"
  5. #include "bridges.h"
  6. #include "control.h"
  7. #include "entrynodes.h"
  8. #include "networkstatus.h"
  9. #include "rendservice.h"
  10. #include "routerlist.h"
  11. #include "test.h"
  12. #include "test_helpers.h"
  13. static void
  14. test_add_onion_helper_keyarg(void *arg)
  15. {
  16. crypto_pk_t *pk = NULL;
  17. crypto_pk_t *pk2 = NULL;
  18. const char *key_new_alg = NULL;
  19. char *key_new_blob = NULL;
  20. char *err_msg = NULL;
  21. char *encoded = NULL;
  22. char *arg_str = NULL;
  23. (void) arg;
  24. /* Test explicit RSA1024 key generation. */
  25. pk = add_onion_helper_keyarg("NEW:RSA1024", 0, &key_new_alg, &key_new_blob,
  26. &err_msg);
  27. tt_assert(pk);
  28. tt_str_op(key_new_alg, OP_EQ, "RSA1024");
  29. tt_assert(key_new_blob);
  30. tt_assert(!err_msg);
  31. /* Test "BEST" key generation (Assumes BEST = RSA1024). */
  32. crypto_pk_free(pk);
  33. tor_free(key_new_blob);
  34. pk = add_onion_helper_keyarg("NEW:BEST", 0, &key_new_alg, &key_new_blob,
  35. &err_msg);
  36. tt_assert(pk);
  37. tt_str_op(key_new_alg, OP_EQ, "RSA1024");
  38. tt_assert(key_new_blob);
  39. tt_assert(!err_msg);
  40. /* Test discarding the private key. */
  41. crypto_pk_free(pk);
  42. tor_free(key_new_blob);
  43. pk = add_onion_helper_keyarg("NEW:BEST", 1, &key_new_alg, &key_new_blob,
  44. &err_msg);
  45. tt_assert(pk);
  46. tt_assert(!key_new_alg);
  47. tt_assert(!key_new_blob);
  48. tt_assert(!err_msg);
  49. /* Test generating a invalid key type. */
  50. crypto_pk_free(pk);
  51. pk = add_onion_helper_keyarg("NEW:RSA512", 0, &key_new_alg, &key_new_blob,
  52. &err_msg);
  53. tt_assert(!pk);
  54. tt_assert(!key_new_alg);
  55. tt_assert(!key_new_blob);
  56. tt_assert(err_msg);
  57. /* Test loading a RSA1024 key. */
  58. tor_free(err_msg);
  59. pk = pk_generate(0);
  60. tt_int_op(0, OP_EQ, crypto_pk_base64_encode(pk, &encoded));
  61. tor_asprintf(&arg_str, "RSA1024:%s", encoded);
  62. pk2 = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
  63. &err_msg);
  64. tt_assert(pk2);
  65. tt_assert(!key_new_alg);
  66. tt_assert(!key_new_blob);
  67. tt_assert(!err_msg);
  68. tt_assert(crypto_pk_cmp_keys(pk, pk2) == 0);
  69. /* Test loading a invalid key type. */
  70. tor_free(arg_str);
  71. crypto_pk_free(pk); pk = NULL;
  72. tor_asprintf(&arg_str, "RSA512:%s", encoded);
  73. pk = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
  74. &err_msg);
  75. tt_assert(!pk);
  76. tt_assert(!key_new_alg);
  77. tt_assert(!key_new_blob);
  78. tt_assert(err_msg);
  79. /* Test loading a invalid key. */
  80. tor_free(arg_str);
  81. crypto_pk_free(pk); pk = NULL;
  82. tor_free(err_msg);
  83. encoded[strlen(encoded)/2] = '\0';
  84. tor_asprintf(&arg_str, "RSA1024:%s", encoded);
  85. pk = add_onion_helper_keyarg(arg_str, 0, &key_new_alg, &key_new_blob,
  86. &err_msg);
  87. tt_assert(!pk);
  88. tt_assert(!key_new_alg);
  89. tt_assert(!key_new_blob);
  90. tt_assert(err_msg);
  91. done:
  92. crypto_pk_free(pk);
  93. crypto_pk_free(pk2);
  94. tor_free(key_new_blob);
  95. tor_free(err_msg);
  96. tor_free(encoded);
  97. tor_free(arg_str);
  98. }
  99. static void
  100. test_getinfo_helper_onion(void *arg)
  101. {
  102. (void)arg;
  103. control_connection_t dummy;
  104. /* Get results out */
  105. char *answer = NULL;
  106. const char *errmsg = NULL;
  107. char *service_id = NULL;
  108. int rt = 0;
  109. dummy.ephemeral_onion_services = NULL;
  110. /* successfully get an empty answer */
  111. rt = getinfo_helper_onions(&dummy, "onions/current", &answer, &errmsg);
  112. tt_assert(rt == 0);
  113. tt_str_op(answer, OP_EQ, "");
  114. tor_free(answer);
  115. /* successfully get an empty answer */
  116. rt = getinfo_helper_onions(&dummy, "onions/detached", &answer, &errmsg);
  117. tt_assert(rt == 0);
  118. tt_str_op(answer, OP_EQ, "");
  119. tor_free(answer);
  120. /* get an answer for one onion service */
  121. service_id = tor_strdup("dummy_onion_id");
  122. dummy.ephemeral_onion_services = smartlist_new();
  123. smartlist_add(dummy.ephemeral_onion_services, service_id);
  124. rt = getinfo_helper_onions(&dummy, "onions/current", &answer, &errmsg);
  125. tt_assert(rt == 0);
  126. tt_str_op(answer, OP_EQ, "dummy_onion_id");
  127. done:
  128. tor_free(answer);
  129. tor_free(service_id);
  130. smartlist_free(dummy.ephemeral_onion_services);
  131. }
  132. static void
  133. test_rend_service_parse_port_config(void *arg)
  134. {
  135. const char *sep = ",";
  136. rend_service_port_config_t *cfg = NULL;
  137. char *err_msg = NULL;
  138. (void)arg;
  139. /* Test "VIRTPORT" only. */
  140. cfg = rend_service_parse_port_config("80", sep, &err_msg);
  141. tt_assert(cfg);
  142. tt_assert(!err_msg);
  143. /* Test "VIRTPORT,TARGET" (Target is port). */
  144. rend_service_port_config_free(cfg);
  145. cfg = rend_service_parse_port_config("80,8080", sep, &err_msg);
  146. tt_assert(cfg);
  147. tt_assert(!err_msg);
  148. /* Test "VIRTPORT,TARGET" (Target is IPv4:port). */
  149. rend_service_port_config_free(cfg);
  150. cfg = rend_service_parse_port_config("80,192.0.2.1:8080", sep, &err_msg);
  151. tt_assert(cfg);
  152. tt_assert(!err_msg);
  153. /* Test "VIRTPORT,TARGET" (Target is IPv6:port). */
  154. rend_service_port_config_free(cfg);
  155. cfg = rend_service_parse_port_config("80,[2001:db8::1]:8080", sep, &err_msg);
  156. tt_assert(cfg);
  157. tt_assert(!err_msg);
  158. rend_service_port_config_free(cfg);
  159. cfg = NULL;
  160. /* XXX: Someone should add tests for AF_UNIX targets if supported. */
  161. /* Test empty config. */
  162. rend_service_port_config_free(cfg);
  163. cfg = rend_service_parse_port_config("", sep, &err_msg);
  164. tt_assert(!cfg);
  165. tt_assert(err_msg);
  166. /* Test invalid port. */
  167. tor_free(err_msg);
  168. cfg = rend_service_parse_port_config("90001", sep, &err_msg);
  169. tt_assert(!cfg);
  170. tt_assert(err_msg);
  171. tor_free(err_msg);
  172. /* unix port */
  173. cfg = NULL;
  174. /* quoted unix port */
  175. tor_free(err_msg);
  176. cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
  177. " ", &err_msg);
  178. tt_assert(cfg);
  179. tt_assert(!err_msg);
  180. rend_service_port_config_free(cfg);
  181. cfg = NULL;
  182. /* quoted unix port */
  183. tor_free(err_msg);
  184. cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
  185. " ", &err_msg);
  186. tt_assert(cfg);
  187. tt_assert(!err_msg);
  188. rend_service_port_config_free(cfg);
  189. cfg = NULL;
  190. /* quoted unix port, missing end quote */
  191. cfg = rend_service_parse_port_config("100 unix:\"/tmp/foo bar",
  192. " ", &err_msg);
  193. tt_assert(!cfg);
  194. tt_str_op(err_msg, OP_EQ, "Couldn't process address <unix:\"/tmp/foo bar> "
  195. "from hidden service configuration");
  196. tor_free(err_msg);
  197. /* bogus IP address */
  198. MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
  199. cfg = rend_service_parse_port_config("100 foo!!.example.com:9000",
  200. " ", &err_msg);
  201. UNMOCK(tor_addr_lookup);
  202. tt_assert(!cfg);
  203. tt_str_op(err_msg, OP_EQ, "Unparseable address in hidden service port "
  204. "configuration.");
  205. tor_free(err_msg);
  206. /* bogus port port */
  207. cfg = rend_service_parse_port_config("100 99999",
  208. " ", &err_msg);
  209. tt_assert(!cfg);
  210. tt_str_op(err_msg, OP_EQ, "Unparseable or out-of-range port \"99999\" "
  211. "in hidden service port configuration.");
  212. tor_free(err_msg);
  213. done:
  214. rend_service_port_config_free(cfg);
  215. tor_free(err_msg);
  216. }
  217. static void
  218. test_add_onion_helper_clientauth(void *arg)
  219. {
  220. rend_authorized_client_t *client = NULL;
  221. char *err_msg = NULL;
  222. int created = 0;
  223. (void)arg;
  224. /* Test "ClientName" only. */
  225. client = add_onion_helper_clientauth("alice", &created, &err_msg);
  226. tt_assert(client);
  227. tt_assert(created);
  228. tt_assert(!err_msg);
  229. rend_authorized_client_free(client);
  230. /* Test "ClientName:Blob" */
  231. client = add_onion_helper_clientauth("alice:475hGBHPlq7Mc0cRZitK/B",
  232. &created, &err_msg);
  233. tt_assert(client);
  234. tt_assert(!created);
  235. tt_assert(!err_msg);
  236. rend_authorized_client_free(client);
  237. /* Test invalid client names */
  238. client = add_onion_helper_clientauth("no*asterisks*allowed", &created,
  239. &err_msg);
  240. tt_assert(!client);
  241. tt_assert(err_msg);
  242. tor_free(err_msg);
  243. /* Test invalid auth cookie */
  244. client = add_onion_helper_clientauth("alice:12345", &created, &err_msg);
  245. tt_assert(!client);
  246. tt_assert(err_msg);
  247. tor_free(err_msg);
  248. /* Test invalid syntax */
  249. client = add_onion_helper_clientauth(":475hGBHPlq7Mc0cRZitK/B", &created,
  250. &err_msg);
  251. tt_assert(!client);
  252. tt_assert(err_msg);
  253. tor_free(err_msg);
  254. done:
  255. rend_authorized_client_free(client);
  256. tor_free(err_msg);
  257. }
  258. /* Mocks and data/variables used for GETINFO download status tests */
  259. static const download_status_t dl_status_default =
  260. { 0, 0, 0, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
  261. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
  262. static download_status_t ns_dl_status[N_CONSENSUS_FLAVORS];
  263. static download_status_t ns_dl_status_bootstrap[N_CONSENSUS_FLAVORS];
  264. static download_status_t ns_dl_status_running[N_CONSENSUS_FLAVORS];
  265. /*
  266. * These should explore all the possible cases of download_status_to_string()
  267. * in control.c
  268. */
  269. static const download_status_t dls_sample_1 =
  270. { 1467163900, 0, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
  271. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_DETERMINISTIC, 0, 0 };
  272. static const char * dls_sample_1_str =
  273. "next-attempt-at 2016-06-29 01:31:40\n"
  274. "n-download-failures 0\n"
  275. "n-download-attempts 0\n"
  276. "schedule DL_SCHED_GENERIC\n"
  277. "want-authority DL_WANT_ANY_DIRSERVER\n"
  278. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  279. "backoff DL_SCHED_DETERMINISTIC\n";
  280. static const download_status_t dls_sample_2 =
  281. { 1467164400, 1, 2, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
  282. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_DETERMINISTIC, 0, 0 };
  283. static const char * dls_sample_2_str =
  284. "next-attempt-at 2016-06-29 01:40:00\n"
  285. "n-download-failures 1\n"
  286. "n-download-attempts 2\n"
  287. "schedule DL_SCHED_CONSENSUS\n"
  288. "want-authority DL_WANT_AUTHORITY\n"
  289. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  290. "backoff DL_SCHED_DETERMINISTIC\n";
  291. static const download_status_t dls_sample_3 =
  292. { 1467154400, 12, 25, DL_SCHED_BRIDGE, DL_WANT_ANY_DIRSERVER,
  293. DL_SCHED_INCREMENT_ATTEMPT, DL_SCHED_DETERMINISTIC, 0, 0 };
  294. static const char * dls_sample_3_str =
  295. "next-attempt-at 2016-06-28 22:53:20\n"
  296. "n-download-failures 12\n"
  297. "n-download-attempts 25\n"
  298. "schedule DL_SCHED_BRIDGE\n"
  299. "want-authority DL_WANT_ANY_DIRSERVER\n"
  300. "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
  301. "backoff DL_SCHED_DETERMINISTIC\n";
  302. static const download_status_t dls_sample_4 =
  303. { 1467166600, 3, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
  304. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
  305. static const char * dls_sample_4_str =
  306. "next-attempt-at 2016-06-29 02:16:40\n"
  307. "n-download-failures 3\n"
  308. "n-download-attempts 0\n"
  309. "schedule DL_SCHED_GENERIC\n"
  310. "want-authority DL_WANT_ANY_DIRSERVER\n"
  311. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  312. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  313. "last-backoff-position 0\n"
  314. "last-delay-used 0\n";
  315. static const download_status_t dls_sample_5 =
  316. { 1467164600, 3, 7, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
  317. DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 1, 2112, };
  318. static const char * dls_sample_5_str =
  319. "next-attempt-at 2016-06-29 01:43:20\n"
  320. "n-download-failures 3\n"
  321. "n-download-attempts 7\n"
  322. "schedule DL_SCHED_CONSENSUS\n"
  323. "want-authority DL_WANT_ANY_DIRSERVER\n"
  324. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  325. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  326. "last-backoff-position 1\n"
  327. "last-delay-used 2112\n";
  328. static const download_status_t dls_sample_6 =
  329. { 1467164200, 4, 9, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
  330. DL_SCHED_INCREMENT_ATTEMPT, DL_SCHED_RANDOM_EXPONENTIAL, 3, 432 };
  331. static const char * dls_sample_6_str =
  332. "next-attempt-at 2016-06-29 01:36:40\n"
  333. "n-download-failures 4\n"
  334. "n-download-attempts 9\n"
  335. "schedule DL_SCHED_CONSENSUS\n"
  336. "want-authority DL_WANT_AUTHORITY\n"
  337. "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
  338. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  339. "last-backoff-position 3\n"
  340. "last-delay-used 432\n";
  341. /* Simulated auth certs */
  342. static const char *auth_id_digest_1_str =
  343. "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061";
  344. static download_status_t auth_def_cert_download_status_1;
  345. static const char *auth_id_digest_2_str =
  346. "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99";
  347. static download_status_t auth_def_cert_download_status_2;
  348. /* Expected form of digest list returned for GETINFO downloads/cert/fps */
  349. static const char *auth_id_digest_expected_list =
  350. "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061\n"
  351. "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99\n";
  352. /* Signing keys for simulated auth 1 */
  353. static const char *auth_1_sk_1_str =
  354. "AA69566029B1F023BA09451B8F1B10952384EB58";
  355. static download_status_t auth_1_sk_1_dls;
  356. static const char *auth_1_sk_2_str =
  357. "710865C7F06B73C5292695A8C34F1C94F769FF72";
  358. static download_status_t auth_1_sk_2_dls;
  359. /*
  360. * Expected form of sk digest list for
  361. * GETINFO downloads/cert/<auth_id_digest_1_str>/sks
  362. */
  363. static const char *auth_1_sk_digest_expected_list =
  364. "AA69566029B1F023BA09451B8F1B10952384EB58\n"
  365. "710865C7F06B73C5292695A8C34F1C94F769FF72\n";
  366. /* Signing keys for simulated auth 2 */
  367. static const char *auth_2_sk_1_str =
  368. "4299047E00D070AD6703FE00BE7AA756DB061E62";
  369. static download_status_t auth_2_sk_1_dls;
  370. static const char *auth_2_sk_2_str =
  371. "9451B8F1B10952384EB58B5F230C0BB701626C9B";
  372. static download_status_t auth_2_sk_2_dls;
  373. /*
  374. * Expected form of sk digest list for
  375. * GETINFO downloads/cert/<auth_id_digest_2_str>/sks
  376. */
  377. static const char *auth_2_sk_digest_expected_list =
  378. "4299047E00D070AD6703FE00BE7AA756DB061E62\n"
  379. "9451B8F1B10952384EB58B5F230C0BB701626C9B\n";
  380. /* Simulated router descriptor digests or bridge identity digests */
  381. static const char *descbr_digest_1_str =
  382. "616408544C7345822696074A1A3DFA16AB381CBD";
  383. static download_status_t descbr_digest_1_dl;
  384. static const char *descbr_digest_2_str =
  385. "06E8067246967265DBCB6641631B530EFEC12DC3";
  386. static download_status_t descbr_digest_2_dl;
  387. /* Expected form of digest list returned for GETINFO downloads/desc/descs */
  388. static const char *descbr_expected_list =
  389. "616408544C7345822696074A1A3DFA16AB381CBD\n"
  390. "06E8067246967265DBCB6641631B530EFEC12DC3\n";
  391. /*
  392. * Flag to make all descbr queries fail, to simulate not being
  393. * configured such that such queries make sense.
  394. */
  395. static int disable_descbr = 0;
  396. static void
  397. reset_mocked_dl_statuses(void)
  398. {
  399. int i;
  400. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  401. memcpy(&(ns_dl_status[i]), &dl_status_default,
  402. sizeof(download_status_t));
  403. memcpy(&(ns_dl_status_bootstrap[i]), &dl_status_default,
  404. sizeof(download_status_t));
  405. memcpy(&(ns_dl_status_running[i]), &dl_status_default,
  406. sizeof(download_status_t));
  407. }
  408. memcpy(&auth_def_cert_download_status_1, &dl_status_default,
  409. sizeof(download_status_t));
  410. memcpy(&auth_def_cert_download_status_2, &dl_status_default,
  411. sizeof(download_status_t));
  412. memcpy(&auth_1_sk_1_dls, &dl_status_default,
  413. sizeof(download_status_t));
  414. memcpy(&auth_1_sk_2_dls, &dl_status_default,
  415. sizeof(download_status_t));
  416. memcpy(&auth_2_sk_1_dls, &dl_status_default,
  417. sizeof(download_status_t));
  418. memcpy(&auth_2_sk_2_dls, &dl_status_default,
  419. sizeof(download_status_t));
  420. memcpy(&descbr_digest_1_dl, &dl_status_default,
  421. sizeof(download_status_t));
  422. memcpy(&descbr_digest_2_dl, &dl_status_default,
  423. sizeof(download_status_t));
  424. }
  425. static download_status_t *
  426. ns_dl_status_mock(consensus_flavor_t flavor)
  427. {
  428. return &(ns_dl_status[flavor]);
  429. }
  430. static download_status_t *
  431. ns_dl_status_bootstrap_mock(consensus_flavor_t flavor)
  432. {
  433. return &(ns_dl_status_bootstrap[flavor]);
  434. }
  435. static download_status_t *
  436. ns_dl_status_running_mock(consensus_flavor_t flavor)
  437. {
  438. return &(ns_dl_status_running[flavor]);
  439. }
  440. static void
  441. setup_ns_mocks(void)
  442. {
  443. MOCK(networkstatus_get_dl_status_by_flavor, ns_dl_status_mock);
  444. MOCK(networkstatus_get_dl_status_by_flavor_bootstrap,
  445. ns_dl_status_bootstrap_mock);
  446. MOCK(networkstatus_get_dl_status_by_flavor_running,
  447. ns_dl_status_running_mock);
  448. reset_mocked_dl_statuses();
  449. }
  450. static void
  451. clear_ns_mocks(void)
  452. {
  453. UNMOCK(networkstatus_get_dl_status_by_flavor);
  454. UNMOCK(networkstatus_get_dl_status_by_flavor_bootstrap);
  455. UNMOCK(networkstatus_get_dl_status_by_flavor_running);
  456. }
  457. static smartlist_t *
  458. cert_dl_status_auth_ids_mock(void)
  459. {
  460. char digest[DIGEST_LEN], *tmp;
  461. int len;
  462. smartlist_t *list = NULL;
  463. /* Just pretend we have only the two hard-coded digests listed above */
  464. list = smartlist_new();
  465. len = base16_decode(digest, DIGEST_LEN,
  466. auth_id_digest_1_str, strlen(auth_id_digest_1_str));
  467. tt_int_op(len, OP_EQ, DIGEST_LEN);
  468. tmp = tor_malloc(DIGEST_LEN);
  469. memcpy(tmp, digest, DIGEST_LEN);
  470. smartlist_add(list, tmp);
  471. len = base16_decode(digest, DIGEST_LEN,
  472. auth_id_digest_2_str, strlen(auth_id_digest_2_str));
  473. tt_int_op(len, OP_EQ, DIGEST_LEN);
  474. tmp = tor_malloc(DIGEST_LEN);
  475. memcpy(tmp, digest, DIGEST_LEN);
  476. smartlist_add(list, tmp);
  477. done:
  478. return list;
  479. }
  480. static download_status_t *
  481. cert_dl_status_def_for_auth_mock(const char *digest)
  482. {
  483. download_status_t *dl = NULL;
  484. char digest_str[HEX_DIGEST_LEN+1];
  485. tt_assert(digest != NULL);
  486. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  487. digest, DIGEST_LEN);
  488. digest_str[HEX_DIGEST_LEN] = '\0';
  489. if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
  490. dl = &auth_def_cert_download_status_1;
  491. } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
  492. dl = &auth_def_cert_download_status_2;
  493. }
  494. done:
  495. return dl;
  496. }
  497. static smartlist_t *
  498. cert_dl_status_sks_for_auth_id_mock(const char *digest)
  499. {
  500. smartlist_t *list = NULL;
  501. char sk[DIGEST_LEN];
  502. char digest_str[HEX_DIGEST_LEN+1];
  503. char *tmp;
  504. int len;
  505. tt_assert(digest != NULL);
  506. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  507. digest, DIGEST_LEN);
  508. digest_str[HEX_DIGEST_LEN] = '\0';
  509. /*
  510. * Build a list of two hard-coded digests, depending on what we
  511. * were just passed.
  512. */
  513. if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
  514. list = smartlist_new();
  515. len = base16_decode(sk, DIGEST_LEN,
  516. auth_1_sk_1_str, strlen(auth_1_sk_1_str));
  517. tt_int_op(len, OP_EQ, DIGEST_LEN);
  518. tmp = tor_malloc(DIGEST_LEN);
  519. memcpy(tmp, sk, DIGEST_LEN);
  520. smartlist_add(list, tmp);
  521. len = base16_decode(sk, DIGEST_LEN,
  522. auth_1_sk_2_str, strlen(auth_1_sk_2_str));
  523. tt_int_op(len, OP_EQ, DIGEST_LEN);
  524. tmp = tor_malloc(DIGEST_LEN);
  525. memcpy(tmp, sk, DIGEST_LEN);
  526. smartlist_add(list, tmp);
  527. } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
  528. list = smartlist_new();
  529. len = base16_decode(sk, DIGEST_LEN,
  530. auth_2_sk_1_str, strlen(auth_2_sk_1_str));
  531. tt_int_op(len, OP_EQ, DIGEST_LEN);
  532. tmp = tor_malloc(DIGEST_LEN);
  533. memcpy(tmp, sk, DIGEST_LEN);
  534. smartlist_add(list, tmp);
  535. len = base16_decode(sk, DIGEST_LEN,
  536. auth_2_sk_2_str, strlen(auth_2_sk_2_str));
  537. tt_int_op(len, OP_EQ, DIGEST_LEN);
  538. tmp = tor_malloc(DIGEST_LEN);
  539. memcpy(tmp, sk, DIGEST_LEN);
  540. smartlist_add(list, tmp);
  541. }
  542. done:
  543. return list;
  544. }
  545. static download_status_t *
  546. cert_dl_status_fp_sk_mock(const char *fp_digest, const char *sk_digest)
  547. {
  548. download_status_t *dl = NULL;
  549. char fp_digest_str[HEX_DIGEST_LEN+1], sk_digest_str[HEX_DIGEST_LEN+1];
  550. /*
  551. * Unpack the digests so we can compare them and figure out which
  552. * dl status we want.
  553. */
  554. tt_assert(fp_digest != NULL);
  555. base16_encode(fp_digest_str, HEX_DIGEST_LEN + 1,
  556. fp_digest, DIGEST_LEN);
  557. fp_digest_str[HEX_DIGEST_LEN] = '\0';
  558. tt_assert(sk_digest != NULL);
  559. base16_encode(sk_digest_str, HEX_DIGEST_LEN + 1,
  560. sk_digest, DIGEST_LEN);
  561. sk_digest_str[HEX_DIGEST_LEN] = '\0';
  562. if (strcmp(fp_digest_str, auth_id_digest_1_str) == 0) {
  563. if (strcmp(sk_digest_str, auth_1_sk_1_str) == 0) {
  564. dl = &auth_1_sk_1_dls;
  565. } else if (strcmp(sk_digest_str, auth_1_sk_2_str) == 0) {
  566. dl = &auth_1_sk_2_dls;
  567. }
  568. } else if (strcmp(fp_digest_str, auth_id_digest_2_str) == 0) {
  569. if (strcmp(sk_digest_str, auth_2_sk_1_str) == 0) {
  570. dl = &auth_2_sk_1_dls;
  571. } else if (strcmp(sk_digest_str, auth_2_sk_2_str) == 0) {
  572. dl = &auth_2_sk_2_dls;
  573. }
  574. }
  575. done:
  576. return dl;
  577. }
  578. static void
  579. setup_cert_mocks(void)
  580. {
  581. MOCK(list_authority_ids_with_downloads, cert_dl_status_auth_ids_mock);
  582. MOCK(id_only_download_status_for_authority_id,
  583. cert_dl_status_def_for_auth_mock);
  584. MOCK(list_sk_digests_for_authority_id,
  585. cert_dl_status_sks_for_auth_id_mock);
  586. MOCK(download_status_for_authority_id_and_sk,
  587. cert_dl_status_fp_sk_mock);
  588. reset_mocked_dl_statuses();
  589. }
  590. static void
  591. clear_cert_mocks(void)
  592. {
  593. UNMOCK(list_authority_ids_with_downloads);
  594. UNMOCK(id_only_download_status_for_authority_id);
  595. UNMOCK(list_sk_digests_for_authority_id);
  596. UNMOCK(download_status_for_authority_id_and_sk);
  597. }
  598. static smartlist_t *
  599. descbr_get_digests_mock(void)
  600. {
  601. char digest[DIGEST_LEN], *tmp;
  602. int len;
  603. smartlist_t *list = NULL;
  604. if (!disable_descbr) {
  605. /* Just pretend we have only the two hard-coded digests listed above */
  606. list = smartlist_new();
  607. len = base16_decode(digest, DIGEST_LEN,
  608. descbr_digest_1_str, strlen(descbr_digest_1_str));
  609. tt_int_op(len, OP_EQ, DIGEST_LEN);
  610. tmp = tor_malloc(DIGEST_LEN);
  611. memcpy(tmp, digest, DIGEST_LEN);
  612. smartlist_add(list, tmp);
  613. len = base16_decode(digest, DIGEST_LEN,
  614. descbr_digest_2_str, strlen(descbr_digest_2_str));
  615. tt_int_op(len, OP_EQ, DIGEST_LEN);
  616. tmp = tor_malloc(DIGEST_LEN);
  617. memcpy(tmp, digest, DIGEST_LEN);
  618. smartlist_add(list, tmp);
  619. }
  620. done:
  621. return list;
  622. }
  623. static download_status_t *
  624. descbr_get_dl_by_digest_mock(const char *digest)
  625. {
  626. download_status_t *dl = NULL;
  627. char digest_str[HEX_DIGEST_LEN+1];
  628. if (!disable_descbr) {
  629. tt_assert(digest != NULL);
  630. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  631. digest, DIGEST_LEN);
  632. digest_str[HEX_DIGEST_LEN] = '\0';
  633. if (strcmp(digest_str, descbr_digest_1_str) == 0) {
  634. dl = &descbr_digest_1_dl;
  635. } else if (strcmp(digest_str, descbr_digest_2_str) == 0) {
  636. dl = &descbr_digest_2_dl;
  637. }
  638. }
  639. done:
  640. return dl;
  641. }
  642. static void
  643. setup_desc_mocks(void)
  644. {
  645. MOCK(router_get_descriptor_digests,
  646. descbr_get_digests_mock);
  647. MOCK(router_get_dl_status_by_descriptor_digest,
  648. descbr_get_dl_by_digest_mock);
  649. reset_mocked_dl_statuses();
  650. }
  651. static void
  652. clear_desc_mocks(void)
  653. {
  654. UNMOCK(router_get_descriptor_digests);
  655. UNMOCK(router_get_dl_status_by_descriptor_digest);
  656. }
  657. static void
  658. setup_bridge_mocks(void)
  659. {
  660. disable_descbr = 0;
  661. MOCK(list_bridge_identities,
  662. descbr_get_digests_mock);
  663. MOCK(get_bridge_dl_status_by_id,
  664. descbr_get_dl_by_digest_mock);
  665. reset_mocked_dl_statuses();
  666. }
  667. static void
  668. clear_bridge_mocks(void)
  669. {
  670. UNMOCK(list_bridge_identities);
  671. UNMOCK(get_bridge_dl_status_by_id);
  672. disable_descbr = 0;
  673. }
  674. static void
  675. test_download_status_consensus(void *arg)
  676. {
  677. /* We just need one of these to pass, it doesn't matter what's in it */
  678. control_connection_t dummy;
  679. /* Get results out */
  680. char *answer = NULL;
  681. const char *errmsg = NULL;
  682. (void)arg;
  683. /* Check that the unknown prefix case works; no mocks needed yet */
  684. getinfo_helper_downloads(&dummy, "downloads/foo", &answer, &errmsg);
  685. tt_assert(answer == NULL);
  686. tt_str_op(errmsg, OP_EQ, "Unknown download status query");
  687. setup_ns_mocks();
  688. /*
  689. * Check returning serialized dlstatuses, and implicitly also test
  690. * download_status_to_string().
  691. */
  692. /* Case 1 default/FLAV_NS*/
  693. memcpy(&(ns_dl_status[FLAV_NS]), &dls_sample_1,
  694. sizeof(download_status_t));
  695. getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns",
  696. &answer, &errmsg);
  697. tt_assert(answer != NULL);
  698. tt_assert(errmsg == NULL);
  699. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  700. tor_free(answer);
  701. errmsg = NULL;
  702. /* Case 2 default/FLAV_MICRODESC */
  703. memcpy(&(ns_dl_status[FLAV_MICRODESC]), &dls_sample_2,
  704. sizeof(download_status_t));
  705. getinfo_helper_downloads(&dummy, "downloads/networkstatus/microdesc",
  706. &answer, &errmsg);
  707. tt_assert(answer != NULL);
  708. tt_assert(errmsg == NULL);
  709. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  710. tor_free(answer);
  711. errmsg = NULL;
  712. /* Case 3 bootstrap/FLAV_NS */
  713. memcpy(&(ns_dl_status_bootstrap[FLAV_NS]), &dls_sample_3,
  714. sizeof(download_status_t));
  715. getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns/bootstrap",
  716. &answer, &errmsg);
  717. tt_assert(answer != NULL);
  718. tt_assert(errmsg == NULL);
  719. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  720. tor_free(answer);
  721. errmsg = NULL;
  722. /* Case 4 bootstrap/FLAV_MICRODESC */
  723. memcpy(&(ns_dl_status_bootstrap[FLAV_MICRODESC]), &dls_sample_4,
  724. sizeof(download_status_t));
  725. getinfo_helper_downloads(&dummy,
  726. "downloads/networkstatus/microdesc/bootstrap",
  727. &answer, &errmsg);
  728. tt_assert(answer != NULL);
  729. tt_assert(errmsg == NULL);
  730. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  731. tor_free(answer);
  732. errmsg = NULL;
  733. /* Case 5 running/FLAV_NS */
  734. memcpy(&(ns_dl_status_running[FLAV_NS]), &dls_sample_5,
  735. sizeof(download_status_t));
  736. getinfo_helper_downloads(&dummy,
  737. "downloads/networkstatus/ns/running",
  738. &answer, &errmsg);
  739. tt_assert(answer != NULL);
  740. tt_assert(errmsg == NULL);
  741. tt_str_op(answer, OP_EQ, dls_sample_5_str);
  742. tor_free(answer);
  743. errmsg = NULL;
  744. /* Case 6 running/FLAV_MICRODESC */
  745. memcpy(&(ns_dl_status_running[FLAV_MICRODESC]), &dls_sample_6,
  746. sizeof(download_status_t));
  747. getinfo_helper_downloads(&dummy,
  748. "downloads/networkstatus/microdesc/running",
  749. &answer, &errmsg);
  750. tt_assert(answer != NULL);
  751. tt_assert(errmsg == NULL);
  752. tt_str_op(answer, OP_EQ, dls_sample_6_str);
  753. tor_free(answer);
  754. errmsg = NULL;
  755. /* Now check the error case */
  756. getinfo_helper_downloads(&dummy, "downloads/networkstatus/foo",
  757. &answer, &errmsg);
  758. tt_assert(answer == NULL);
  759. tt_assert(errmsg != NULL);
  760. tt_str_op(errmsg, OP_EQ, "Unknown flavor");
  761. errmsg = NULL;
  762. done:
  763. clear_ns_mocks();
  764. tor_free(answer);
  765. return;
  766. }
  767. static void
  768. test_download_status_cert(void *arg)
  769. {
  770. /* We just need one of these to pass, it doesn't matter what's in it */
  771. control_connection_t dummy;
  772. /* Get results out */
  773. char *question = NULL;
  774. char *answer = NULL;
  775. const char *errmsg = NULL;
  776. (void)arg;
  777. setup_cert_mocks();
  778. /*
  779. * Check returning serialized dlstatuses and digest lists, and implicitly
  780. * also test download_status_to_string() and digest_list_to_string().
  781. */
  782. /* Case 1 - list of authority identity fingerprints */
  783. getinfo_helper_downloads(&dummy,
  784. "downloads/cert/fps",
  785. &answer, &errmsg);
  786. tt_assert(answer != NULL);
  787. tt_assert(errmsg == NULL);
  788. tt_str_op(answer, OP_EQ, auth_id_digest_expected_list);
  789. tor_free(answer);
  790. errmsg = NULL;
  791. /* Case 2 - download status for default cert for 1st auth id */
  792. memcpy(&auth_def_cert_download_status_1, &dls_sample_1,
  793. sizeof(download_status_t));
  794. tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_1_str);
  795. tt_assert(question != NULL);
  796. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  797. tt_assert(answer != NULL);
  798. tt_assert(errmsg == NULL);
  799. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  800. tor_free(question);
  801. tor_free(answer);
  802. errmsg = NULL;
  803. /* Case 3 - download status for default cert for 2nd auth id */
  804. memcpy(&auth_def_cert_download_status_2, &dls_sample_2,
  805. sizeof(download_status_t));
  806. tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_2_str);
  807. tt_assert(question != NULL);
  808. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  809. tt_assert(answer != NULL);
  810. tt_assert(errmsg == NULL);
  811. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  812. tor_free(question);
  813. tor_free(answer);
  814. errmsg = NULL;
  815. /* Case 4 - list of signing key digests for 1st auth id */
  816. tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_1_str);
  817. tt_assert(question != NULL);
  818. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  819. tt_assert(answer != NULL);
  820. tt_assert(errmsg == NULL);
  821. tt_str_op(answer, OP_EQ, auth_1_sk_digest_expected_list);
  822. tor_free(question);
  823. tor_free(answer);
  824. errmsg = NULL;
  825. /* Case 5 - list of signing key digests for 2nd auth id */
  826. tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_2_str);
  827. tt_assert(question != NULL);
  828. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  829. tt_assert(answer != NULL);
  830. tt_assert(errmsg == NULL);
  831. tt_str_op(answer, OP_EQ, auth_2_sk_digest_expected_list);
  832. tor_free(question);
  833. tor_free(answer);
  834. errmsg = NULL;
  835. /* Case 6 - download status for 1st auth id, 1st sk */
  836. memcpy(&auth_1_sk_1_dls, &dls_sample_3,
  837. sizeof(download_status_t));
  838. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  839. auth_id_digest_1_str, auth_1_sk_1_str);
  840. tt_assert(question != NULL);
  841. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  842. tt_assert(answer != NULL);
  843. tt_assert(errmsg == NULL);
  844. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  845. tor_free(question);
  846. tor_free(answer);
  847. errmsg = NULL;
  848. /* Case 7 - download status for 1st auth id, 2nd sk */
  849. memcpy(&auth_1_sk_2_dls, &dls_sample_4,
  850. sizeof(download_status_t));
  851. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  852. auth_id_digest_1_str, auth_1_sk_2_str);
  853. tt_assert(question != NULL);
  854. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  855. tt_assert(answer != NULL);
  856. tt_assert(errmsg == NULL);
  857. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  858. tor_free(question);
  859. tor_free(answer);
  860. errmsg = NULL;
  861. /* Case 8 - download status for 2nd auth id, 1st sk */
  862. memcpy(&auth_2_sk_1_dls, &dls_sample_5,
  863. sizeof(download_status_t));
  864. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  865. auth_id_digest_2_str, auth_2_sk_1_str);
  866. tt_assert(question != NULL);
  867. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  868. tt_assert(answer != NULL);
  869. tt_assert(errmsg == NULL);
  870. tt_str_op(answer, OP_EQ, dls_sample_5_str);
  871. tor_free(question);
  872. tor_free(answer);
  873. errmsg = NULL;
  874. /* Case 9 - download status for 2nd auth id, 2nd sk */
  875. memcpy(&auth_2_sk_2_dls, &dls_sample_6,
  876. sizeof(download_status_t));
  877. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  878. auth_id_digest_2_str, auth_2_sk_2_str);
  879. tt_assert(question != NULL);
  880. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  881. tt_assert(answer != NULL);
  882. tt_assert(errmsg == NULL);
  883. tt_str_op(answer, OP_EQ, dls_sample_6_str);
  884. tor_free(question);
  885. tor_free(answer);
  886. errmsg = NULL;
  887. /* Now check the error cases */
  888. /* Case 1 - query is garbage after downloads/cert/ part */
  889. getinfo_helper_downloads(&dummy, "downloads/cert/blahdeblah",
  890. &answer, &errmsg);
  891. tt_assert(answer == NULL);
  892. tt_assert(errmsg != NULL);
  893. tt_str_op(errmsg, OP_EQ, "Unknown certificate download status query");
  894. errmsg = NULL;
  895. /*
  896. * Case 2 - looks like downloads/cert/fp/<fp>, but <fp> isn't even
  897. * the right length for a digest.
  898. */
  899. getinfo_helper_downloads(&dummy, "downloads/cert/fp/2B1D36D32B2942406",
  900. &answer, &errmsg);
  901. tt_assert(answer == NULL);
  902. tt_assert(errmsg != NULL);
  903. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  904. errmsg = NULL;
  905. /*
  906. * Case 3 - looks like downloads/cert/fp/<fp>, and <fp> is digest-sized,
  907. * but not parseable as one.
  908. */
  909. getinfo_helper_downloads(&dummy,
  910. "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1",
  911. &answer, &errmsg);
  912. tt_assert(answer == NULL);
  913. tt_assert(errmsg != NULL);
  914. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  915. errmsg = NULL;
  916. /*
  917. * Case 4 - downloads/cert/fp/<fp>, and <fp> is not a known authority
  918. * identity digest
  919. */
  920. getinfo_helper_downloads(&dummy,
  921. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61",
  922. &answer, &errmsg);
  923. tt_assert(answer == NULL);
  924. tt_assert(errmsg != NULL);
  925. tt_str_op(errmsg, OP_EQ,
  926. "Failed to get download status for this authority identity digest");
  927. errmsg = NULL;
  928. /*
  929. * Case 5 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
  930. * parse as a sensible digest.
  931. */
  932. getinfo_helper_downloads(&dummy,
  933. "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1/blah",
  934. &answer, &errmsg);
  935. tt_assert(answer == NULL);
  936. tt_assert(errmsg != NULL);
  937. tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
  938. errmsg = NULL;
  939. /*
  940. * Case 6 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
  941. * parse as a sensible digest.
  942. */
  943. getinfo_helper_downloads(&dummy,
  944. "downloads/cert/fp/82F52AF55D25/blah",
  945. &answer, &errmsg);
  946. tt_assert(answer == NULL);
  947. tt_assert(errmsg != NULL);
  948. tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
  949. errmsg = NULL;
  950. /*
  951. * Case 7 - downloads/cert/fp/<fp>/sks, and <fp> is not a known authority
  952. * digest.
  953. */
  954. getinfo_helper_downloads(&dummy,
  955. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/sks",
  956. &answer, &errmsg);
  957. tt_assert(answer == NULL);
  958. tt_assert(errmsg != NULL);
  959. tt_str_op(errmsg, OP_EQ,
  960. "Failed to get list of signing key digests for this authority "
  961. "identity digest");
  962. errmsg = NULL;
  963. /*
  964. * Case 8 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
  965. * parse as a signing key digest.
  966. */
  967. getinfo_helper_downloads(&dummy,
  968. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
  969. "82F52AF55D250115FE44D3GC81D49643241D56A1",
  970. &answer, &errmsg);
  971. tt_assert(answer == NULL);
  972. tt_assert(errmsg != NULL);
  973. tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
  974. errmsg = NULL;
  975. /*
  976. * Case 9 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
  977. * parse as a signing key digest.
  978. */
  979. getinfo_helper_downloads(&dummy,
  980. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
  981. "82F52AF55D250115FE44D",
  982. &answer, &errmsg);
  983. tt_assert(answer == NULL);
  984. tt_assert(errmsg != NULL);
  985. tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
  986. errmsg = NULL;
  987. /*
  988. * Case 10 - downloads/cert/fp/<fp>/<sk>, but <fp> isn't a known
  989. * authority identity digest.
  990. */
  991. getinfo_helper_downloads(&dummy,
  992. "downloads/cert/fp/C6B05DF332F74DB9A13498EE3BBC7AA2F69FCB45/"
  993. "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
  994. &answer, &errmsg);
  995. tt_assert(answer == NULL);
  996. tt_assert(errmsg != NULL);
  997. tt_str_op(errmsg, OP_EQ,
  998. "Failed to get download status for this identity/"
  999. "signing key digest pair");
  1000. errmsg = NULL;
  1001. /*
  1002. * Case 11 - downloads/cert/fp/<fp>/<sk>, but <sk> isn't a known
  1003. * signing key digest.
  1004. */
  1005. getinfo_helper_downloads(&dummy,
  1006. "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
  1007. "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
  1008. &answer, &errmsg);
  1009. tt_assert(answer == NULL);
  1010. tt_assert(errmsg != NULL);
  1011. tt_str_op(errmsg, OP_EQ,
  1012. "Failed to get download status for this identity/"
  1013. "signing key digest pair");
  1014. errmsg = NULL;
  1015. /*
  1016. * Case 12 - downloads/cert/fp/<fp>/<sk>, but <sk> is on the list for
  1017. * a different authority identity digest.
  1018. */
  1019. getinfo_helper_downloads(&dummy,
  1020. "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
  1021. "9451B8F1B10952384EB58B5F230C0BB701626C9B",
  1022. &answer, &errmsg);
  1023. tt_assert(answer == NULL);
  1024. tt_assert(errmsg != NULL);
  1025. tt_str_op(errmsg, OP_EQ,
  1026. "Failed to get download status for this identity/"
  1027. "signing key digest pair");
  1028. errmsg = NULL;
  1029. done:
  1030. clear_cert_mocks();
  1031. tor_free(answer);
  1032. return;
  1033. }
  1034. static void
  1035. test_download_status_desc(void *arg)
  1036. {
  1037. /* We just need one of these to pass, it doesn't matter what's in it */
  1038. control_connection_t dummy;
  1039. /* Get results out */
  1040. char *question = NULL;
  1041. char *answer = NULL;
  1042. const char *errmsg = NULL;
  1043. (void)arg;
  1044. setup_desc_mocks();
  1045. /*
  1046. * Check returning serialized dlstatuses and digest lists, and implicitly
  1047. * also test download_status_to_string() and digest_list_to_string().
  1048. */
  1049. /* Case 1 - list of router descriptor digests */
  1050. getinfo_helper_downloads(&dummy,
  1051. "downloads/desc/descs",
  1052. &answer, &errmsg);
  1053. tt_assert(answer != NULL);
  1054. tt_assert(errmsg == NULL);
  1055. tt_str_op(answer, OP_EQ, descbr_expected_list);
  1056. tor_free(answer);
  1057. errmsg = NULL;
  1058. /* Case 2 - get download status for router descriptor 1 */
  1059. memcpy(&descbr_digest_1_dl, &dls_sample_1,
  1060. sizeof(download_status_t));
  1061. tor_asprintf(&question, "downloads/desc/%s", descbr_digest_1_str);
  1062. tt_assert(question != NULL);
  1063. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1064. tt_assert(answer != NULL);
  1065. tt_assert(errmsg == NULL);
  1066. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  1067. tor_free(question);
  1068. tor_free(answer);
  1069. errmsg = NULL;
  1070. /* Case 3 - get download status for router descriptor 1 */
  1071. memcpy(&descbr_digest_2_dl, &dls_sample_2,
  1072. sizeof(download_status_t));
  1073. tor_asprintf(&question, "downloads/desc/%s", descbr_digest_2_str);
  1074. tt_assert(question != NULL);
  1075. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1076. tt_assert(answer != NULL);
  1077. tt_assert(errmsg == NULL);
  1078. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  1079. tor_free(question);
  1080. tor_free(answer);
  1081. errmsg = NULL;
  1082. /* Now check the error cases */
  1083. /* Case 1 - non-digest-length garbage after downloads/desc */
  1084. getinfo_helper_downloads(&dummy, "downloads/desc/blahdeblah",
  1085. &answer, &errmsg);
  1086. tt_assert(answer == NULL);
  1087. tt_assert(errmsg != NULL);
  1088. tt_str_op(errmsg, OP_EQ, "Unknown router descriptor download status query");
  1089. errmsg = NULL;
  1090. /* Case 2 - nonparseable digest-shaped thing */
  1091. getinfo_helper_downloads(
  1092. &dummy,
  1093. "downloads/desc/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
  1094. &answer, &errmsg);
  1095. tt_assert(answer == NULL);
  1096. tt_assert(errmsg != NULL);
  1097. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1098. errmsg = NULL;
  1099. /* Case 3 - digest we have no descriptor for */
  1100. getinfo_helper_downloads(
  1101. &dummy,
  1102. "downloads/desc/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
  1103. &answer, &errmsg);
  1104. tt_assert(answer == NULL);
  1105. tt_assert(errmsg != NULL);
  1106. tt_str_op(errmsg, OP_EQ, "No such descriptor digest found");
  1107. errmsg = NULL;
  1108. /* Case 4 - microdescs only */
  1109. disable_descbr = 1;
  1110. getinfo_helper_downloads(&dummy,
  1111. "downloads/desc/descs",
  1112. &answer, &errmsg);
  1113. tt_assert(answer == NULL);
  1114. tt_assert(errmsg != NULL);
  1115. tt_str_op(errmsg, OP_EQ,
  1116. "We don't seem to have a networkstatus-flavored consensus");
  1117. errmsg = NULL;
  1118. disable_descbr = 0;
  1119. done:
  1120. clear_desc_mocks();
  1121. tor_free(answer);
  1122. return;
  1123. }
  1124. static void
  1125. test_download_status_bridge(void *arg)
  1126. {
  1127. /* We just need one of these to pass, it doesn't matter what's in it */
  1128. control_connection_t dummy;
  1129. /* Get results out */
  1130. char *question = NULL;
  1131. char *answer = NULL;
  1132. const char *errmsg = NULL;
  1133. (void)arg;
  1134. setup_bridge_mocks();
  1135. /*
  1136. * Check returning serialized dlstatuses and digest lists, and implicitly
  1137. * also test download_status_to_string() and digest_list_to_string().
  1138. */
  1139. /* Case 1 - list of bridge identity digests */
  1140. getinfo_helper_downloads(&dummy,
  1141. "downloads/bridge/bridges",
  1142. &answer, &errmsg);
  1143. tt_assert(answer != NULL);
  1144. tt_assert(errmsg == NULL);
  1145. tt_str_op(answer, OP_EQ, descbr_expected_list);
  1146. tor_free(answer);
  1147. errmsg = NULL;
  1148. /* Case 2 - get download status for bridge descriptor 1 */
  1149. memcpy(&descbr_digest_1_dl, &dls_sample_3,
  1150. sizeof(download_status_t));
  1151. tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_1_str);
  1152. tt_assert(question != NULL);
  1153. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1154. tt_assert(answer != NULL);
  1155. tt_assert(errmsg == NULL);
  1156. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  1157. tor_free(question);
  1158. tor_free(answer);
  1159. errmsg = NULL;
  1160. /* Case 3 - get download status for router descriptor 1 */
  1161. memcpy(&descbr_digest_2_dl, &dls_sample_4,
  1162. sizeof(download_status_t));
  1163. tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_2_str);
  1164. tt_assert(question != NULL);
  1165. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1166. tt_assert(answer != NULL);
  1167. tt_assert(errmsg == NULL);
  1168. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  1169. tor_free(question);
  1170. tor_free(answer);
  1171. errmsg = NULL;
  1172. /* Now check the error cases */
  1173. /* Case 1 - non-digest-length garbage after downloads/bridge */
  1174. getinfo_helper_downloads(&dummy, "downloads/bridge/blahdeblah",
  1175. &answer, &errmsg);
  1176. tt_assert(answer == NULL);
  1177. tt_assert(errmsg != NULL);
  1178. tt_str_op(errmsg, OP_EQ, "Unknown bridge descriptor download status query");
  1179. errmsg = NULL;
  1180. /* Case 2 - nonparseable digest-shaped thing */
  1181. getinfo_helper_downloads(
  1182. &dummy,
  1183. "downloads/bridge/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
  1184. &answer, &errmsg);
  1185. tt_assert(answer == NULL);
  1186. tt_assert(errmsg != NULL);
  1187. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1188. errmsg = NULL;
  1189. /* Case 3 - digest we have no descriptor for */
  1190. getinfo_helper_downloads(
  1191. &dummy,
  1192. "downloads/bridge/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
  1193. &answer, &errmsg);
  1194. tt_assert(answer == NULL);
  1195. tt_assert(errmsg != NULL);
  1196. tt_str_op(errmsg, OP_EQ, "No such bridge identity digest found");
  1197. errmsg = NULL;
  1198. /* Case 4 - bridges disabled */
  1199. disable_descbr = 1;
  1200. getinfo_helper_downloads(&dummy,
  1201. "downloads/bridge/bridges",
  1202. &answer, &errmsg);
  1203. tt_assert(answer == NULL);
  1204. tt_assert(errmsg != NULL);
  1205. tt_str_op(errmsg, OP_EQ, "We don't seem to be using bridges");
  1206. errmsg = NULL;
  1207. disable_descbr = 0;
  1208. done:
  1209. clear_bridge_mocks();
  1210. tor_free(answer);
  1211. return;
  1212. }
  1213. struct testcase_t controller_tests[] = {
  1214. { "add_onion_helper_keyarg", test_add_onion_helper_keyarg, 0, NULL, NULL },
  1215. { "getinfo_helper_onion", test_getinfo_helper_onion, 0, NULL, NULL },
  1216. { "rend_service_parse_port_config", test_rend_service_parse_port_config, 0,
  1217. NULL, NULL },
  1218. { "add_onion_helper_clientauth", test_add_onion_helper_clientauth, 0, NULL,
  1219. NULL },
  1220. { "download_status_consensus", test_download_status_consensus, 0, NULL,
  1221. NULL },
  1222. { "download_status_cert", test_download_status_cert, 0, NULL,
  1223. NULL },
  1224. { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
  1225. { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
  1226. END_OF_TESTCASES
  1227. };