test_controller.c 51 KB

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