test_controller.c 41 KB

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