test_controller.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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/routerlist.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(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. done:
  307. rend_service_port_config_free(cfg);
  308. tor_free(err_msg);
  309. }
  310. static void
  311. test_add_onion_helper_clientauth(void *arg)
  312. {
  313. rend_authorized_client_t *client = NULL;
  314. char *err_msg = NULL;
  315. int created = 0;
  316. (void)arg;
  317. /* Test "ClientName" only. */
  318. client = add_onion_helper_clientauth("alice", &created, &err_msg);
  319. tt_assert(client);
  320. tt_assert(created);
  321. tt_ptr_op(err_msg, OP_EQ, NULL);
  322. rend_authorized_client_free(client);
  323. /* Test "ClientName:Blob" */
  324. client = add_onion_helper_clientauth("alice:475hGBHPlq7Mc0cRZitK/B",
  325. &created, &err_msg);
  326. tt_assert(client);
  327. tt_assert(!created);
  328. tt_ptr_op(err_msg, OP_EQ, NULL);
  329. rend_authorized_client_free(client);
  330. /* Test invalid client names */
  331. client = add_onion_helper_clientauth("no*asterisks*allowed", &created,
  332. &err_msg);
  333. tt_ptr_op(client, OP_EQ, NULL);
  334. tt_assert(err_msg);
  335. tor_free(err_msg);
  336. /* Test invalid auth cookie */
  337. client = add_onion_helper_clientauth("alice:12345", &created, &err_msg);
  338. tt_ptr_op(client, OP_EQ, NULL);
  339. tt_assert(err_msg);
  340. tor_free(err_msg);
  341. /* Test invalid syntax */
  342. client = add_onion_helper_clientauth(":475hGBHPlq7Mc0cRZitK/B", &created,
  343. &err_msg);
  344. tt_ptr_op(client, OP_EQ, NULL);
  345. tt_assert(err_msg);
  346. tor_free(err_msg);
  347. done:
  348. rend_authorized_client_free(client);
  349. tor_free(err_msg);
  350. }
  351. /* Mocks and data/variables used for GETINFO download status tests */
  352. static const download_status_t dl_status_default =
  353. { 0, 0, 0, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
  354. DL_SCHED_INCREMENT_FAILURE, 0, 0 };
  355. static download_status_t ns_dl_status[N_CONSENSUS_FLAVORS];
  356. static download_status_t ns_dl_status_bootstrap[N_CONSENSUS_FLAVORS];
  357. static download_status_t ns_dl_status_running[N_CONSENSUS_FLAVORS];
  358. /*
  359. * These should explore all the possible cases of download_status_to_string()
  360. * in control.c
  361. */
  362. static const download_status_t dls_sample_1 =
  363. { 1467163900, 0, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
  364. DL_SCHED_INCREMENT_FAILURE, 0, 0 };
  365. static const char * dls_sample_1_str =
  366. "next-attempt-at 2016-06-29 01:31:40\n"
  367. "n-download-failures 0\n"
  368. "n-download-attempts 0\n"
  369. "schedule DL_SCHED_GENERIC\n"
  370. "want-authority DL_WANT_ANY_DIRSERVER\n"
  371. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  372. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  373. "last-backoff-position 0\n"
  374. "last-delay-used 0\n";
  375. static const download_status_t dls_sample_2 =
  376. { 1467164400, 1, 2, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
  377. DL_SCHED_INCREMENT_FAILURE, 0, 0 };
  378. static const char * dls_sample_2_str =
  379. "next-attempt-at 2016-06-29 01:40:00\n"
  380. "n-download-failures 1\n"
  381. "n-download-attempts 2\n"
  382. "schedule DL_SCHED_CONSENSUS\n"
  383. "want-authority DL_WANT_AUTHORITY\n"
  384. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  385. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  386. "last-backoff-position 0\n"
  387. "last-delay-used 0\n";
  388. static const download_status_t dls_sample_3 =
  389. { 1467154400, 12, 25, DL_SCHED_BRIDGE, DL_WANT_ANY_DIRSERVER,
  390. DL_SCHED_INCREMENT_ATTEMPT, 0, 0 };
  391. static const char * dls_sample_3_str =
  392. "next-attempt-at 2016-06-28 22:53:20\n"
  393. "n-download-failures 12\n"
  394. "n-download-attempts 25\n"
  395. "schedule DL_SCHED_BRIDGE\n"
  396. "want-authority DL_WANT_ANY_DIRSERVER\n"
  397. "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
  398. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  399. "last-backoff-position 0\n"
  400. "last-delay-used 0\n";
  401. static const download_status_t dls_sample_4 =
  402. { 1467166600, 3, 0, DL_SCHED_GENERIC, DL_WANT_ANY_DIRSERVER,
  403. DL_SCHED_INCREMENT_FAILURE, 0, 0 };
  404. static const char * dls_sample_4_str =
  405. "next-attempt-at 2016-06-29 02:16:40\n"
  406. "n-download-failures 3\n"
  407. "n-download-attempts 0\n"
  408. "schedule DL_SCHED_GENERIC\n"
  409. "want-authority DL_WANT_ANY_DIRSERVER\n"
  410. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  411. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  412. "last-backoff-position 0\n"
  413. "last-delay-used 0\n";
  414. static const download_status_t dls_sample_5 =
  415. { 1467164600, 3, 7, DL_SCHED_CONSENSUS, DL_WANT_ANY_DIRSERVER,
  416. DL_SCHED_INCREMENT_FAILURE, 1, 2112, };
  417. static const char * dls_sample_5_str =
  418. "next-attempt-at 2016-06-29 01:43:20\n"
  419. "n-download-failures 3\n"
  420. "n-download-attempts 7\n"
  421. "schedule DL_SCHED_CONSENSUS\n"
  422. "want-authority DL_WANT_ANY_DIRSERVER\n"
  423. "increment-on DL_SCHED_INCREMENT_FAILURE\n"
  424. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  425. "last-backoff-position 1\n"
  426. "last-delay-used 2112\n";
  427. static const download_status_t dls_sample_6 =
  428. { 1467164200, 4, 9, DL_SCHED_CONSENSUS, DL_WANT_AUTHORITY,
  429. DL_SCHED_INCREMENT_ATTEMPT, 3, 432 };
  430. static const char * dls_sample_6_str =
  431. "next-attempt-at 2016-06-29 01:36:40\n"
  432. "n-download-failures 4\n"
  433. "n-download-attempts 9\n"
  434. "schedule DL_SCHED_CONSENSUS\n"
  435. "want-authority DL_WANT_AUTHORITY\n"
  436. "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
  437. "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
  438. "last-backoff-position 3\n"
  439. "last-delay-used 432\n";
  440. /* Simulated auth certs */
  441. static const char *auth_id_digest_1_str =
  442. "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061";
  443. static download_status_t auth_def_cert_download_status_1;
  444. static const char *auth_id_digest_2_str =
  445. "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99";
  446. static download_status_t auth_def_cert_download_status_2;
  447. /* Expected form of digest list returned for GETINFO downloads/cert/fps */
  448. static const char *auth_id_digest_expected_list =
  449. "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061\n"
  450. "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99\n";
  451. /* Signing keys for simulated auth 1 */
  452. static const char *auth_1_sk_1_str =
  453. "AA69566029B1F023BA09451B8F1B10952384EB58";
  454. static download_status_t auth_1_sk_1_dls;
  455. static const char *auth_1_sk_2_str =
  456. "710865C7F06B73C5292695A8C34F1C94F769FF72";
  457. static download_status_t auth_1_sk_2_dls;
  458. /*
  459. * Expected form of sk digest list for
  460. * GETINFO downloads/cert/<auth_id_digest_1_str>/sks
  461. */
  462. static const char *auth_1_sk_digest_expected_list =
  463. "AA69566029B1F023BA09451B8F1B10952384EB58\n"
  464. "710865C7F06B73C5292695A8C34F1C94F769FF72\n";
  465. /* Signing keys for simulated auth 2 */
  466. static const char *auth_2_sk_1_str =
  467. "4299047E00D070AD6703FE00BE7AA756DB061E62";
  468. static download_status_t auth_2_sk_1_dls;
  469. static const char *auth_2_sk_2_str =
  470. "9451B8F1B10952384EB58B5F230C0BB701626C9B";
  471. static download_status_t auth_2_sk_2_dls;
  472. /*
  473. * Expected form of sk digest list for
  474. * GETINFO downloads/cert/<auth_id_digest_2_str>/sks
  475. */
  476. static const char *auth_2_sk_digest_expected_list =
  477. "4299047E00D070AD6703FE00BE7AA756DB061E62\n"
  478. "9451B8F1B10952384EB58B5F230C0BB701626C9B\n";
  479. /* Simulated router descriptor digests or bridge identity digests */
  480. static const char *descbr_digest_1_str =
  481. "616408544C7345822696074A1A3DFA16AB381CBD";
  482. static download_status_t descbr_digest_1_dl;
  483. static const char *descbr_digest_2_str =
  484. "06E8067246967265DBCB6641631B530EFEC12DC3";
  485. static download_status_t descbr_digest_2_dl;
  486. /* Expected form of digest list returned for GETINFO downloads/desc/descs */
  487. static const char *descbr_expected_list =
  488. "616408544C7345822696074A1A3DFA16AB381CBD\n"
  489. "06E8067246967265DBCB6641631B530EFEC12DC3\n";
  490. /*
  491. * Flag to make all descbr queries fail, to simulate not being
  492. * configured such that such queries make sense.
  493. */
  494. static int disable_descbr = 0;
  495. static void
  496. reset_mocked_dl_statuses(void)
  497. {
  498. int i;
  499. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  500. memcpy(&(ns_dl_status[i]), &dl_status_default,
  501. sizeof(download_status_t));
  502. memcpy(&(ns_dl_status_bootstrap[i]), &dl_status_default,
  503. sizeof(download_status_t));
  504. memcpy(&(ns_dl_status_running[i]), &dl_status_default,
  505. sizeof(download_status_t));
  506. }
  507. memcpy(&auth_def_cert_download_status_1, &dl_status_default,
  508. sizeof(download_status_t));
  509. memcpy(&auth_def_cert_download_status_2, &dl_status_default,
  510. sizeof(download_status_t));
  511. memcpy(&auth_1_sk_1_dls, &dl_status_default,
  512. sizeof(download_status_t));
  513. memcpy(&auth_1_sk_2_dls, &dl_status_default,
  514. sizeof(download_status_t));
  515. memcpy(&auth_2_sk_1_dls, &dl_status_default,
  516. sizeof(download_status_t));
  517. memcpy(&auth_2_sk_2_dls, &dl_status_default,
  518. sizeof(download_status_t));
  519. memcpy(&descbr_digest_1_dl, &dl_status_default,
  520. sizeof(download_status_t));
  521. memcpy(&descbr_digest_2_dl, &dl_status_default,
  522. sizeof(download_status_t));
  523. }
  524. static download_status_t *
  525. ns_dl_status_mock(consensus_flavor_t flavor)
  526. {
  527. return &(ns_dl_status[flavor]);
  528. }
  529. static download_status_t *
  530. ns_dl_status_bootstrap_mock(consensus_flavor_t flavor)
  531. {
  532. return &(ns_dl_status_bootstrap[flavor]);
  533. }
  534. static download_status_t *
  535. ns_dl_status_running_mock(consensus_flavor_t flavor)
  536. {
  537. return &(ns_dl_status_running[flavor]);
  538. }
  539. static void
  540. setup_ns_mocks(void)
  541. {
  542. MOCK(networkstatus_get_dl_status_by_flavor, ns_dl_status_mock);
  543. MOCK(networkstatus_get_dl_status_by_flavor_bootstrap,
  544. ns_dl_status_bootstrap_mock);
  545. MOCK(networkstatus_get_dl_status_by_flavor_running,
  546. ns_dl_status_running_mock);
  547. reset_mocked_dl_statuses();
  548. }
  549. static void
  550. clear_ns_mocks(void)
  551. {
  552. UNMOCK(networkstatus_get_dl_status_by_flavor);
  553. UNMOCK(networkstatus_get_dl_status_by_flavor_bootstrap);
  554. UNMOCK(networkstatus_get_dl_status_by_flavor_running);
  555. }
  556. static smartlist_t *
  557. cert_dl_status_auth_ids_mock(void)
  558. {
  559. char digest[DIGEST_LEN], *tmp;
  560. int len;
  561. smartlist_t *list = NULL;
  562. /* Just pretend we have only the two hard-coded digests listed above */
  563. list = smartlist_new();
  564. len = base16_decode(digest, DIGEST_LEN,
  565. auth_id_digest_1_str, strlen(auth_id_digest_1_str));
  566. tt_int_op(len, OP_EQ, DIGEST_LEN);
  567. tmp = tor_malloc(DIGEST_LEN);
  568. memcpy(tmp, digest, DIGEST_LEN);
  569. smartlist_add(list, tmp);
  570. len = base16_decode(digest, DIGEST_LEN,
  571. auth_id_digest_2_str, strlen(auth_id_digest_2_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. done:
  577. return list;
  578. }
  579. static download_status_t *
  580. cert_dl_status_def_for_auth_mock(const char *digest)
  581. {
  582. download_status_t *dl = NULL;
  583. char digest_str[HEX_DIGEST_LEN+1];
  584. tt_ptr_op(digest, OP_NE, NULL);
  585. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  586. digest, DIGEST_LEN);
  587. digest_str[HEX_DIGEST_LEN] = '\0';
  588. if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
  589. dl = &auth_def_cert_download_status_1;
  590. } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
  591. dl = &auth_def_cert_download_status_2;
  592. }
  593. done:
  594. return dl;
  595. }
  596. static smartlist_t *
  597. cert_dl_status_sks_for_auth_id_mock(const char *digest)
  598. {
  599. smartlist_t *list = NULL;
  600. char sk[DIGEST_LEN];
  601. char digest_str[HEX_DIGEST_LEN+1];
  602. char *tmp;
  603. int len;
  604. tt_ptr_op(digest, OP_NE, NULL);
  605. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  606. digest, DIGEST_LEN);
  607. digest_str[HEX_DIGEST_LEN] = '\0';
  608. /*
  609. * Build a list of two hard-coded digests, depending on what we
  610. * were just passed.
  611. */
  612. if (strcmp(digest_str, auth_id_digest_1_str) == 0) {
  613. list = smartlist_new();
  614. len = base16_decode(sk, DIGEST_LEN,
  615. auth_1_sk_1_str, strlen(auth_1_sk_1_str));
  616. tt_int_op(len, OP_EQ, DIGEST_LEN);
  617. tmp = tor_malloc(DIGEST_LEN);
  618. memcpy(tmp, sk, DIGEST_LEN);
  619. smartlist_add(list, tmp);
  620. len = base16_decode(sk, DIGEST_LEN,
  621. auth_1_sk_2_str, strlen(auth_1_sk_2_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. } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) {
  627. list = smartlist_new();
  628. len = base16_decode(sk, DIGEST_LEN,
  629. auth_2_sk_1_str, strlen(auth_2_sk_1_str));
  630. tt_int_op(len, OP_EQ, DIGEST_LEN);
  631. tmp = tor_malloc(DIGEST_LEN);
  632. memcpy(tmp, sk, DIGEST_LEN);
  633. smartlist_add(list, tmp);
  634. len = base16_decode(sk, DIGEST_LEN,
  635. auth_2_sk_2_str, strlen(auth_2_sk_2_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. }
  641. done:
  642. return list;
  643. }
  644. static download_status_t *
  645. cert_dl_status_fp_sk_mock(const char *fp_digest, const char *sk_digest)
  646. {
  647. download_status_t *dl = NULL;
  648. char fp_digest_str[HEX_DIGEST_LEN+1], sk_digest_str[HEX_DIGEST_LEN+1];
  649. /*
  650. * Unpack the digests so we can compare them and figure out which
  651. * dl status we want.
  652. */
  653. tt_ptr_op(fp_digest, OP_NE, NULL);
  654. base16_encode(fp_digest_str, HEX_DIGEST_LEN + 1,
  655. fp_digest, DIGEST_LEN);
  656. fp_digest_str[HEX_DIGEST_LEN] = '\0';
  657. tt_ptr_op(sk_digest, OP_NE, NULL);
  658. base16_encode(sk_digest_str, HEX_DIGEST_LEN + 1,
  659. sk_digest, DIGEST_LEN);
  660. sk_digest_str[HEX_DIGEST_LEN] = '\0';
  661. if (strcmp(fp_digest_str, auth_id_digest_1_str) == 0) {
  662. if (strcmp(sk_digest_str, auth_1_sk_1_str) == 0) {
  663. dl = &auth_1_sk_1_dls;
  664. } else if (strcmp(sk_digest_str, auth_1_sk_2_str) == 0) {
  665. dl = &auth_1_sk_2_dls;
  666. }
  667. } else if (strcmp(fp_digest_str, auth_id_digest_2_str) == 0) {
  668. if (strcmp(sk_digest_str, auth_2_sk_1_str) == 0) {
  669. dl = &auth_2_sk_1_dls;
  670. } else if (strcmp(sk_digest_str, auth_2_sk_2_str) == 0) {
  671. dl = &auth_2_sk_2_dls;
  672. }
  673. }
  674. done:
  675. return dl;
  676. }
  677. static void
  678. setup_cert_mocks(void)
  679. {
  680. MOCK(list_authority_ids_with_downloads, cert_dl_status_auth_ids_mock);
  681. MOCK(id_only_download_status_for_authority_id,
  682. cert_dl_status_def_for_auth_mock);
  683. MOCK(list_sk_digests_for_authority_id,
  684. cert_dl_status_sks_for_auth_id_mock);
  685. MOCK(download_status_for_authority_id_and_sk,
  686. cert_dl_status_fp_sk_mock);
  687. reset_mocked_dl_statuses();
  688. }
  689. static void
  690. clear_cert_mocks(void)
  691. {
  692. UNMOCK(list_authority_ids_with_downloads);
  693. UNMOCK(id_only_download_status_for_authority_id);
  694. UNMOCK(list_sk_digests_for_authority_id);
  695. UNMOCK(download_status_for_authority_id_and_sk);
  696. }
  697. static smartlist_t *
  698. descbr_get_digests_mock(void)
  699. {
  700. char digest[DIGEST_LEN], *tmp;
  701. int len;
  702. smartlist_t *list = NULL;
  703. if (!disable_descbr) {
  704. /* Just pretend we have only the two hard-coded digests listed above */
  705. list = smartlist_new();
  706. len = base16_decode(digest, DIGEST_LEN,
  707. descbr_digest_1_str, strlen(descbr_digest_1_str));
  708. tt_int_op(len, OP_EQ, DIGEST_LEN);
  709. tmp = tor_malloc(DIGEST_LEN);
  710. memcpy(tmp, digest, DIGEST_LEN);
  711. smartlist_add(list, tmp);
  712. len = base16_decode(digest, DIGEST_LEN,
  713. descbr_digest_2_str, strlen(descbr_digest_2_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. }
  719. done:
  720. return list;
  721. }
  722. static download_status_t *
  723. descbr_get_dl_by_digest_mock(const char *digest)
  724. {
  725. download_status_t *dl = NULL;
  726. char digest_str[HEX_DIGEST_LEN+1];
  727. if (!disable_descbr) {
  728. tt_ptr_op(digest, OP_NE, NULL);
  729. base16_encode(digest_str, HEX_DIGEST_LEN + 1,
  730. digest, DIGEST_LEN);
  731. digest_str[HEX_DIGEST_LEN] = '\0';
  732. if (strcmp(digest_str, descbr_digest_1_str) == 0) {
  733. dl = &descbr_digest_1_dl;
  734. } else if (strcmp(digest_str, descbr_digest_2_str) == 0) {
  735. dl = &descbr_digest_2_dl;
  736. }
  737. }
  738. done:
  739. return dl;
  740. }
  741. static void
  742. setup_desc_mocks(void)
  743. {
  744. MOCK(router_get_descriptor_digests,
  745. descbr_get_digests_mock);
  746. MOCK(router_get_dl_status_by_descriptor_digest,
  747. descbr_get_dl_by_digest_mock);
  748. reset_mocked_dl_statuses();
  749. }
  750. static void
  751. clear_desc_mocks(void)
  752. {
  753. UNMOCK(router_get_descriptor_digests);
  754. UNMOCK(router_get_dl_status_by_descriptor_digest);
  755. }
  756. static void
  757. setup_bridge_mocks(void)
  758. {
  759. disable_descbr = 0;
  760. MOCK(list_bridge_identities,
  761. descbr_get_digests_mock);
  762. MOCK(get_bridge_dl_status_by_id,
  763. descbr_get_dl_by_digest_mock);
  764. reset_mocked_dl_statuses();
  765. }
  766. static void
  767. clear_bridge_mocks(void)
  768. {
  769. UNMOCK(list_bridge_identities);
  770. UNMOCK(get_bridge_dl_status_by_id);
  771. disable_descbr = 0;
  772. }
  773. static void
  774. test_download_status_consensus(void *arg)
  775. {
  776. /* We just need one of these to pass, it doesn't matter what's in it */
  777. control_connection_t dummy;
  778. /* Get results out */
  779. char *answer = NULL;
  780. const char *errmsg = NULL;
  781. (void)arg;
  782. /* Check that the unknown prefix case works; no mocks needed yet */
  783. getinfo_helper_downloads(&dummy, "downloads/foo", &answer, &errmsg);
  784. tt_ptr_op(answer, OP_EQ, NULL);
  785. tt_str_op(errmsg, OP_EQ, "Unknown download status query");
  786. setup_ns_mocks();
  787. /*
  788. * Check returning serialized dlstatuses, and implicitly also test
  789. * download_status_to_string().
  790. */
  791. /* Case 1 default/FLAV_NS*/
  792. memcpy(&(ns_dl_status[FLAV_NS]), &dls_sample_1,
  793. sizeof(download_status_t));
  794. getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns",
  795. &answer, &errmsg);
  796. tt_ptr_op(answer, OP_NE, NULL);
  797. tt_ptr_op(errmsg, OP_EQ, NULL);
  798. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  799. tor_free(answer);
  800. errmsg = NULL;
  801. /* Case 2 default/FLAV_MICRODESC */
  802. memcpy(&(ns_dl_status[FLAV_MICRODESC]), &dls_sample_2,
  803. sizeof(download_status_t));
  804. getinfo_helper_downloads(&dummy, "downloads/networkstatus/microdesc",
  805. &answer, &errmsg);
  806. tt_ptr_op(answer, OP_NE, NULL);
  807. tt_ptr_op(errmsg, OP_EQ, NULL);
  808. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  809. tor_free(answer);
  810. errmsg = NULL;
  811. /* Case 3 bootstrap/FLAV_NS */
  812. memcpy(&(ns_dl_status_bootstrap[FLAV_NS]), &dls_sample_3,
  813. sizeof(download_status_t));
  814. getinfo_helper_downloads(&dummy, "downloads/networkstatus/ns/bootstrap",
  815. &answer, &errmsg);
  816. tt_ptr_op(answer, OP_NE, NULL);
  817. tt_ptr_op(errmsg, OP_EQ, NULL);
  818. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  819. tor_free(answer);
  820. errmsg = NULL;
  821. /* Case 4 bootstrap/FLAV_MICRODESC */
  822. memcpy(&(ns_dl_status_bootstrap[FLAV_MICRODESC]), &dls_sample_4,
  823. sizeof(download_status_t));
  824. getinfo_helper_downloads(&dummy,
  825. "downloads/networkstatus/microdesc/bootstrap",
  826. &answer, &errmsg);
  827. tt_ptr_op(answer, OP_NE, NULL);
  828. tt_ptr_op(errmsg, OP_EQ, NULL);
  829. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  830. tor_free(answer);
  831. errmsg = NULL;
  832. /* Case 5 running/FLAV_NS */
  833. memcpy(&(ns_dl_status_running[FLAV_NS]), &dls_sample_5,
  834. sizeof(download_status_t));
  835. getinfo_helper_downloads(&dummy,
  836. "downloads/networkstatus/ns/running",
  837. &answer, &errmsg);
  838. tt_ptr_op(answer, OP_NE, NULL);
  839. tt_ptr_op(errmsg, OP_EQ, NULL);
  840. tt_str_op(answer, OP_EQ, dls_sample_5_str);
  841. tor_free(answer);
  842. errmsg = NULL;
  843. /* Case 6 running/FLAV_MICRODESC */
  844. memcpy(&(ns_dl_status_running[FLAV_MICRODESC]), &dls_sample_6,
  845. sizeof(download_status_t));
  846. getinfo_helper_downloads(&dummy,
  847. "downloads/networkstatus/microdesc/running",
  848. &answer, &errmsg);
  849. tt_ptr_op(answer, OP_NE, NULL);
  850. tt_ptr_op(errmsg, OP_EQ, NULL);
  851. tt_str_op(answer, OP_EQ, dls_sample_6_str);
  852. tor_free(answer);
  853. errmsg = NULL;
  854. /* Now check the error case */
  855. getinfo_helper_downloads(&dummy, "downloads/networkstatus/foo",
  856. &answer, &errmsg);
  857. tt_ptr_op(answer, OP_EQ, NULL);
  858. tt_ptr_op(errmsg, OP_NE, NULL);
  859. tt_str_op(errmsg, OP_EQ, "Unknown flavor");
  860. errmsg = NULL;
  861. done:
  862. clear_ns_mocks();
  863. tor_free(answer);
  864. return;
  865. }
  866. static void
  867. test_download_status_cert(void *arg)
  868. {
  869. /* We just need one of these to pass, it doesn't matter what's in it */
  870. control_connection_t dummy;
  871. /* Get results out */
  872. char *question = NULL;
  873. char *answer = NULL;
  874. const char *errmsg = NULL;
  875. (void)arg;
  876. setup_cert_mocks();
  877. /*
  878. * Check returning serialized dlstatuses and digest lists, and implicitly
  879. * also test download_status_to_string() and digest_list_to_string().
  880. */
  881. /* Case 1 - list of authority identity fingerprints */
  882. getinfo_helper_downloads(&dummy,
  883. "downloads/cert/fps",
  884. &answer, &errmsg);
  885. tt_ptr_op(answer, OP_NE, NULL);
  886. tt_ptr_op(errmsg, OP_EQ, NULL);
  887. tt_str_op(answer, OP_EQ, auth_id_digest_expected_list);
  888. tor_free(answer);
  889. errmsg = NULL;
  890. /* Case 2 - download status for default cert for 1st auth id */
  891. memcpy(&auth_def_cert_download_status_1, &dls_sample_1,
  892. sizeof(download_status_t));
  893. tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_1_str);
  894. tt_ptr_op(question, OP_NE, NULL);
  895. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  896. tt_ptr_op(answer, OP_NE, NULL);
  897. tt_ptr_op(errmsg, OP_EQ, NULL);
  898. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  899. tor_free(question);
  900. tor_free(answer);
  901. errmsg = NULL;
  902. /* Case 3 - download status for default cert for 2nd auth id */
  903. memcpy(&auth_def_cert_download_status_2, &dls_sample_2,
  904. sizeof(download_status_t));
  905. tor_asprintf(&question, "downloads/cert/fp/%s", auth_id_digest_2_str);
  906. tt_ptr_op(question, OP_NE, NULL);
  907. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  908. tt_ptr_op(answer, OP_NE, NULL);
  909. tt_ptr_op(errmsg, OP_EQ, NULL);
  910. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  911. tor_free(question);
  912. tor_free(answer);
  913. errmsg = NULL;
  914. /* Case 4 - list of signing key digests for 1st auth id */
  915. tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_1_str);
  916. tt_ptr_op(question, OP_NE, NULL);
  917. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  918. tt_ptr_op(answer, OP_NE, NULL);
  919. tt_ptr_op(errmsg, OP_EQ, NULL);
  920. tt_str_op(answer, OP_EQ, auth_1_sk_digest_expected_list);
  921. tor_free(question);
  922. tor_free(answer);
  923. errmsg = NULL;
  924. /* Case 5 - list of signing key digests for 2nd auth id */
  925. tor_asprintf(&question, "downloads/cert/fp/%s/sks", auth_id_digest_2_str);
  926. tt_ptr_op(question, OP_NE, NULL);
  927. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  928. tt_ptr_op(answer, OP_NE, NULL);
  929. tt_ptr_op(errmsg, OP_EQ, NULL);
  930. tt_str_op(answer, OP_EQ, auth_2_sk_digest_expected_list);
  931. tor_free(question);
  932. tor_free(answer);
  933. errmsg = NULL;
  934. /* Case 6 - download status for 1st auth id, 1st sk */
  935. memcpy(&auth_1_sk_1_dls, &dls_sample_3,
  936. sizeof(download_status_t));
  937. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  938. auth_id_digest_1_str, auth_1_sk_1_str);
  939. tt_ptr_op(question, OP_NE, NULL);
  940. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  941. tt_ptr_op(answer, OP_NE, NULL);
  942. tt_ptr_op(errmsg, OP_EQ, NULL);
  943. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  944. tor_free(question);
  945. tor_free(answer);
  946. errmsg = NULL;
  947. /* Case 7 - download status for 1st auth id, 2nd sk */
  948. memcpy(&auth_1_sk_2_dls, &dls_sample_4,
  949. sizeof(download_status_t));
  950. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  951. auth_id_digest_1_str, auth_1_sk_2_str);
  952. tt_ptr_op(question, OP_NE, NULL);
  953. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  954. tt_ptr_op(answer, OP_NE, NULL);
  955. tt_ptr_op(errmsg, OP_EQ, NULL);
  956. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  957. tor_free(question);
  958. tor_free(answer);
  959. errmsg = NULL;
  960. /* Case 8 - download status for 2nd auth id, 1st sk */
  961. memcpy(&auth_2_sk_1_dls, &dls_sample_5,
  962. sizeof(download_status_t));
  963. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  964. auth_id_digest_2_str, auth_2_sk_1_str);
  965. tt_ptr_op(question, OP_NE, NULL);
  966. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  967. tt_ptr_op(answer, OP_NE, NULL);
  968. tt_ptr_op(errmsg, OP_EQ, NULL);
  969. tt_str_op(answer, OP_EQ, dls_sample_5_str);
  970. tor_free(question);
  971. tor_free(answer);
  972. errmsg = NULL;
  973. /* Case 9 - download status for 2nd auth id, 2nd sk */
  974. memcpy(&auth_2_sk_2_dls, &dls_sample_6,
  975. sizeof(download_status_t));
  976. tor_asprintf(&question, "downloads/cert/fp/%s/%s",
  977. auth_id_digest_2_str, auth_2_sk_2_str);
  978. tt_ptr_op(question, OP_NE, NULL);
  979. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  980. tt_ptr_op(answer, OP_NE, NULL);
  981. tt_ptr_op(errmsg, OP_EQ, NULL);
  982. tt_str_op(answer, OP_EQ, dls_sample_6_str);
  983. tor_free(question);
  984. tor_free(answer);
  985. errmsg = NULL;
  986. /* Now check the error cases */
  987. /* Case 1 - query is garbage after downloads/cert/ part */
  988. getinfo_helper_downloads(&dummy, "downloads/cert/blahdeblah",
  989. &answer, &errmsg);
  990. tt_ptr_op(answer, OP_EQ, NULL);
  991. tt_ptr_op(errmsg, OP_NE, NULL);
  992. tt_str_op(errmsg, OP_EQ, "Unknown certificate download status query");
  993. errmsg = NULL;
  994. /*
  995. * Case 2 - looks like downloads/cert/fp/<fp>, but <fp> isn't even
  996. * the right length for a digest.
  997. */
  998. getinfo_helper_downloads(&dummy, "downloads/cert/fp/2B1D36D32B2942406",
  999. &answer, &errmsg);
  1000. tt_ptr_op(answer, OP_EQ, NULL);
  1001. tt_ptr_op(errmsg, OP_NE, NULL);
  1002. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1003. errmsg = NULL;
  1004. /*
  1005. * Case 3 - looks like downloads/cert/fp/<fp>, and <fp> is digest-sized,
  1006. * but not parseable as one.
  1007. */
  1008. getinfo_helper_downloads(&dummy,
  1009. "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1",
  1010. &answer, &errmsg);
  1011. tt_ptr_op(answer, OP_EQ, NULL);
  1012. tt_ptr_op(errmsg, OP_NE, NULL);
  1013. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1014. errmsg = NULL;
  1015. /*
  1016. * Case 4 - downloads/cert/fp/<fp>, and <fp> is not a known authority
  1017. * identity digest
  1018. */
  1019. getinfo_helper_downloads(&dummy,
  1020. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61",
  1021. &answer, &errmsg);
  1022. tt_ptr_op(answer, OP_EQ, NULL);
  1023. tt_ptr_op(errmsg, OP_NE, NULL);
  1024. tt_str_op(errmsg, OP_EQ,
  1025. "Failed to get download status for this authority identity digest");
  1026. errmsg = NULL;
  1027. /*
  1028. * Case 5 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
  1029. * parse as a sensible digest.
  1030. */
  1031. getinfo_helper_downloads(&dummy,
  1032. "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1/blah",
  1033. &answer, &errmsg);
  1034. tt_ptr_op(answer, OP_EQ, NULL);
  1035. tt_ptr_op(errmsg, OP_NE, NULL);
  1036. tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
  1037. errmsg = NULL;
  1038. /*
  1039. * Case 6 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
  1040. * parse as a sensible digest.
  1041. */
  1042. getinfo_helper_downloads(&dummy,
  1043. "downloads/cert/fp/82F52AF55D25/blah",
  1044. &answer, &errmsg);
  1045. tt_ptr_op(answer, OP_EQ, NULL);
  1046. tt_ptr_op(errmsg, OP_NE, NULL);
  1047. tt_str_op(errmsg, OP_EQ, "That didn't look like an identity digest");
  1048. errmsg = NULL;
  1049. /*
  1050. * Case 7 - downloads/cert/fp/<fp>/sks, and <fp> is not a known authority
  1051. * digest.
  1052. */
  1053. getinfo_helper_downloads(&dummy,
  1054. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/sks",
  1055. &answer, &errmsg);
  1056. tt_ptr_op(answer, OP_EQ, NULL);
  1057. tt_ptr_op(errmsg, OP_NE, NULL);
  1058. tt_str_op(errmsg, OP_EQ,
  1059. "Failed to get list of signing key digests for this authority "
  1060. "identity digest");
  1061. errmsg = NULL;
  1062. /*
  1063. * Case 8 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
  1064. * parse as a signing key digest.
  1065. */
  1066. getinfo_helper_downloads(&dummy,
  1067. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
  1068. "82F52AF55D250115FE44D3GC81D49643241D56A1",
  1069. &answer, &errmsg);
  1070. tt_ptr_op(answer, OP_EQ, NULL);
  1071. tt_ptr_op(errmsg, OP_NE, NULL);
  1072. tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
  1073. errmsg = NULL;
  1074. /*
  1075. * Case 9 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
  1076. * parse as a signing key digest.
  1077. */
  1078. getinfo_helper_downloads(&dummy,
  1079. "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
  1080. "82F52AF55D250115FE44D",
  1081. &answer, &errmsg);
  1082. tt_ptr_op(answer, OP_EQ, NULL);
  1083. tt_ptr_op(errmsg, OP_NE, NULL);
  1084. tt_str_op(errmsg, OP_EQ, "That didn't look like a signing key digest");
  1085. errmsg = NULL;
  1086. /*
  1087. * Case 10 - downloads/cert/fp/<fp>/<sk>, but <fp> isn't a known
  1088. * authority identity digest.
  1089. */
  1090. getinfo_helper_downloads(&dummy,
  1091. "downloads/cert/fp/C6B05DF332F74DB9A13498EE3BBC7AA2F69FCB45/"
  1092. "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
  1093. &answer, &errmsg);
  1094. tt_ptr_op(answer, OP_EQ, NULL);
  1095. tt_ptr_op(errmsg, OP_NE, NULL);
  1096. tt_str_op(errmsg, OP_EQ,
  1097. "Failed to get download status for this identity/"
  1098. "signing key digest pair");
  1099. errmsg = NULL;
  1100. /*
  1101. * Case 11 - downloads/cert/fp/<fp>/<sk>, but <sk> isn't a known
  1102. * signing key digest.
  1103. */
  1104. getinfo_helper_downloads(&dummy,
  1105. "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
  1106. "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
  1107. &answer, &errmsg);
  1108. tt_ptr_op(answer, OP_EQ, NULL);
  1109. tt_ptr_op(errmsg, OP_NE, NULL);
  1110. tt_str_op(errmsg, OP_EQ,
  1111. "Failed to get download status for this identity/"
  1112. "signing key digest pair");
  1113. errmsg = NULL;
  1114. /*
  1115. * Case 12 - downloads/cert/fp/<fp>/<sk>, but <sk> is on the list for
  1116. * a different authority identity digest.
  1117. */
  1118. getinfo_helper_downloads(&dummy,
  1119. "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
  1120. "9451B8F1B10952384EB58B5F230C0BB701626C9B",
  1121. &answer, &errmsg);
  1122. tt_ptr_op(answer, OP_EQ, NULL);
  1123. tt_ptr_op(errmsg, OP_NE, NULL);
  1124. tt_str_op(errmsg, OP_EQ,
  1125. "Failed to get download status for this identity/"
  1126. "signing key digest pair");
  1127. errmsg = NULL;
  1128. done:
  1129. clear_cert_mocks();
  1130. tor_free(answer);
  1131. return;
  1132. }
  1133. static void
  1134. test_download_status_desc(void *arg)
  1135. {
  1136. /* We just need one of these to pass, it doesn't matter what's in it */
  1137. control_connection_t dummy;
  1138. /* Get results out */
  1139. char *question = NULL;
  1140. char *answer = NULL;
  1141. const char *errmsg = NULL;
  1142. (void)arg;
  1143. setup_desc_mocks();
  1144. /*
  1145. * Check returning serialized dlstatuses and digest lists, and implicitly
  1146. * also test download_status_to_string() and digest_list_to_string().
  1147. */
  1148. /* Case 1 - list of router descriptor digests */
  1149. getinfo_helper_downloads(&dummy,
  1150. "downloads/desc/descs",
  1151. &answer, &errmsg);
  1152. tt_ptr_op(answer, OP_NE, NULL);
  1153. tt_ptr_op(errmsg, OP_EQ, NULL);
  1154. tt_str_op(answer, OP_EQ, descbr_expected_list);
  1155. tor_free(answer);
  1156. errmsg = NULL;
  1157. /* Case 2 - get download status for router descriptor 1 */
  1158. memcpy(&descbr_digest_1_dl, &dls_sample_1,
  1159. sizeof(download_status_t));
  1160. tor_asprintf(&question, "downloads/desc/%s", descbr_digest_1_str);
  1161. tt_ptr_op(question, OP_NE, NULL);
  1162. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1163. tt_ptr_op(answer, OP_NE, NULL);
  1164. tt_ptr_op(errmsg, OP_EQ, NULL);
  1165. tt_str_op(answer, OP_EQ, dls_sample_1_str);
  1166. tor_free(question);
  1167. tor_free(answer);
  1168. errmsg = NULL;
  1169. /* Case 3 - get download status for router descriptor 1 */
  1170. memcpy(&descbr_digest_2_dl, &dls_sample_2,
  1171. sizeof(download_status_t));
  1172. tor_asprintf(&question, "downloads/desc/%s", descbr_digest_2_str);
  1173. tt_ptr_op(question, OP_NE, NULL);
  1174. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1175. tt_ptr_op(answer, OP_NE, NULL);
  1176. tt_ptr_op(errmsg, OP_EQ, NULL);
  1177. tt_str_op(answer, OP_EQ, dls_sample_2_str);
  1178. tor_free(question);
  1179. tor_free(answer);
  1180. errmsg = NULL;
  1181. /* Now check the error cases */
  1182. /* Case 1 - non-digest-length garbage after downloads/desc */
  1183. getinfo_helper_downloads(&dummy, "downloads/desc/blahdeblah",
  1184. &answer, &errmsg);
  1185. tt_ptr_op(answer, OP_EQ, NULL);
  1186. tt_ptr_op(errmsg, OP_NE, NULL);
  1187. tt_str_op(errmsg, OP_EQ, "Unknown router descriptor download status query");
  1188. errmsg = NULL;
  1189. /* Case 2 - nonparseable digest-shaped thing */
  1190. getinfo_helper_downloads(
  1191. &dummy,
  1192. "downloads/desc/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
  1193. &answer, &errmsg);
  1194. tt_ptr_op(answer, OP_EQ, NULL);
  1195. tt_ptr_op(errmsg, OP_NE, NULL);
  1196. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1197. errmsg = NULL;
  1198. /* Case 3 - digest we have no descriptor for */
  1199. getinfo_helper_downloads(
  1200. &dummy,
  1201. "downloads/desc/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
  1202. &answer, &errmsg);
  1203. tt_ptr_op(answer, OP_EQ, NULL);
  1204. tt_ptr_op(errmsg, OP_NE, NULL);
  1205. tt_str_op(errmsg, OP_EQ, "No such descriptor digest found");
  1206. errmsg = NULL;
  1207. /* Case 4 - microdescs only */
  1208. disable_descbr = 1;
  1209. getinfo_helper_downloads(&dummy,
  1210. "downloads/desc/descs",
  1211. &answer, &errmsg);
  1212. tt_ptr_op(answer, OP_EQ, NULL);
  1213. tt_ptr_op(errmsg, OP_NE, NULL);
  1214. tt_str_op(errmsg, OP_EQ,
  1215. "We don't seem to have a networkstatus-flavored consensus");
  1216. errmsg = NULL;
  1217. disable_descbr = 0;
  1218. done:
  1219. clear_desc_mocks();
  1220. tor_free(answer);
  1221. return;
  1222. }
  1223. static void
  1224. test_download_status_bridge(void *arg)
  1225. {
  1226. /* We just need one of these to pass, it doesn't matter what's in it */
  1227. control_connection_t dummy;
  1228. /* Get results out */
  1229. char *question = NULL;
  1230. char *answer = NULL;
  1231. const char *errmsg = NULL;
  1232. (void)arg;
  1233. setup_bridge_mocks();
  1234. /*
  1235. * Check returning serialized dlstatuses and digest lists, and implicitly
  1236. * also test download_status_to_string() and digest_list_to_string().
  1237. */
  1238. /* Case 1 - list of bridge identity digests */
  1239. getinfo_helper_downloads(&dummy,
  1240. "downloads/bridge/bridges",
  1241. &answer, &errmsg);
  1242. tt_ptr_op(answer, OP_NE, NULL);
  1243. tt_ptr_op(errmsg, OP_EQ, NULL);
  1244. tt_str_op(answer, OP_EQ, descbr_expected_list);
  1245. tor_free(answer);
  1246. errmsg = NULL;
  1247. /* Case 2 - get download status for bridge descriptor 1 */
  1248. memcpy(&descbr_digest_1_dl, &dls_sample_3,
  1249. sizeof(download_status_t));
  1250. tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_1_str);
  1251. tt_ptr_op(question, OP_NE, NULL);
  1252. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1253. tt_ptr_op(answer, OP_NE, NULL);
  1254. tt_ptr_op(errmsg, OP_EQ, NULL);
  1255. tt_str_op(answer, OP_EQ, dls_sample_3_str);
  1256. tor_free(question);
  1257. tor_free(answer);
  1258. errmsg = NULL;
  1259. /* Case 3 - get download status for router descriptor 1 */
  1260. memcpy(&descbr_digest_2_dl, &dls_sample_4,
  1261. sizeof(download_status_t));
  1262. tor_asprintf(&question, "downloads/bridge/%s", descbr_digest_2_str);
  1263. tt_ptr_op(question, OP_NE, NULL);
  1264. getinfo_helper_downloads(&dummy, question, &answer, &errmsg);
  1265. tt_ptr_op(answer, OP_NE, NULL);
  1266. tt_ptr_op(errmsg, OP_EQ, NULL);
  1267. tt_str_op(answer, OP_EQ, dls_sample_4_str);
  1268. tor_free(question);
  1269. tor_free(answer);
  1270. errmsg = NULL;
  1271. /* Now check the error cases */
  1272. /* Case 1 - non-digest-length garbage after downloads/bridge */
  1273. getinfo_helper_downloads(&dummy, "downloads/bridge/blahdeblah",
  1274. &answer, &errmsg);
  1275. tt_ptr_op(answer, OP_EQ, NULL);
  1276. tt_ptr_op(errmsg, OP_NE, NULL);
  1277. tt_str_op(errmsg, OP_EQ, "Unknown bridge descriptor download status query");
  1278. errmsg = NULL;
  1279. /* Case 2 - nonparseable digest-shaped thing */
  1280. getinfo_helper_downloads(
  1281. &dummy,
  1282. "downloads/bridge/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
  1283. &answer, &errmsg);
  1284. tt_ptr_op(answer, OP_EQ, NULL);
  1285. tt_ptr_op(errmsg, OP_NE, NULL);
  1286. tt_str_op(errmsg, OP_EQ, "That didn't look like a digest");
  1287. errmsg = NULL;
  1288. /* Case 3 - digest we have no descriptor for */
  1289. getinfo_helper_downloads(
  1290. &dummy,
  1291. "downloads/bridge/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
  1292. &answer, &errmsg);
  1293. tt_ptr_op(answer, OP_EQ, NULL);
  1294. tt_ptr_op(errmsg, OP_NE, NULL);
  1295. tt_str_op(errmsg, OP_EQ, "No such bridge identity digest found");
  1296. errmsg = NULL;
  1297. /* Case 4 - bridges disabled */
  1298. disable_descbr = 1;
  1299. getinfo_helper_downloads(&dummy,
  1300. "downloads/bridge/bridges",
  1301. &answer, &errmsg);
  1302. tt_ptr_op(answer, OP_EQ, NULL);
  1303. tt_ptr_op(errmsg, OP_NE, NULL);
  1304. tt_str_op(errmsg, OP_EQ, "We don't seem to be using bridges");
  1305. errmsg = NULL;
  1306. disable_descbr = 0;
  1307. done:
  1308. clear_bridge_mocks();
  1309. tor_free(answer);
  1310. return;
  1311. }
  1312. /** Set timeval to a mock date and time. This is necessary
  1313. * to make tor_gettimeofday() mockable. */
  1314. static void
  1315. mock_tor_gettimeofday(struct timeval *timeval)
  1316. {
  1317. timeval->tv_sec = 1523405073;
  1318. timeval->tv_usec = 271645;
  1319. }
  1320. static void
  1321. test_current_time(void *arg)
  1322. {
  1323. /* We just need one of these to pass, it doesn't matter what's in it */
  1324. control_connection_t dummy;
  1325. /* Get results out */
  1326. char *answer = NULL;
  1327. const char *errmsg = NULL;
  1328. (void)arg;
  1329. /* We need these for storing the (mock) time. */
  1330. MOCK(tor_gettimeofday, mock_tor_gettimeofday);
  1331. struct timeval now;
  1332. tor_gettimeofday(&now);
  1333. char timebuf[ISO_TIME_LEN+1];
  1334. /* Case 1 - local time */
  1335. format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
  1336. getinfo_helper_current_time(&dummy,
  1337. "current-time/local",
  1338. &answer, &errmsg);
  1339. tt_ptr_op(answer, OP_NE, NULL);
  1340. tt_ptr_op(errmsg, OP_EQ, NULL);
  1341. tt_str_op(answer, OP_EQ, timebuf);
  1342. tor_free(answer);
  1343. errmsg = NULL;
  1344. /* Case 2 - UTC time */
  1345. format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
  1346. getinfo_helper_current_time(&dummy,
  1347. "current-time/utc",
  1348. &answer, &errmsg);
  1349. tt_ptr_op(answer, OP_NE, NULL);
  1350. tt_ptr_op(errmsg, OP_EQ, NULL);
  1351. tt_str_op(answer, OP_EQ, timebuf);
  1352. tor_free(answer);
  1353. errmsg = NULL;
  1354. done:
  1355. UNMOCK(tor_gettimeofday);
  1356. tor_free(answer);
  1357. return;
  1358. }
  1359. static size_t n_nodelist_get_list = 0;
  1360. static smartlist_t *nodes = NULL;
  1361. static smartlist_t *
  1362. mock_nodelist_get_list(void)
  1363. {
  1364. n_nodelist_get_list++;
  1365. tor_assert(nodes);
  1366. return nodes;
  1367. }
  1368. static void
  1369. test_getinfo_md_all(void *arg)
  1370. {
  1371. char *answer = NULL;
  1372. const char *errmsg = NULL;
  1373. int retval = 0;
  1374. (void)arg;
  1375. node_t *node1 = tor_malloc(sizeof(node_t));
  1376. memset(node1, 0, sizeof(node_t));
  1377. node1->md = tor_malloc(sizeof(microdesc_t));
  1378. memset(node1->md, 0, sizeof(microdesc_t));
  1379. node1->md->body = tor_strdup("md1\n");
  1380. node1->md->bodylen = 4;
  1381. node_t *node2 = tor_malloc(sizeof(node_t));
  1382. memset(node2, 0, sizeof(node_t));
  1383. node2->md = tor_malloc(sizeof(microdesc_t));
  1384. memset(node2->md, 0, sizeof(microdesc_t));
  1385. node2->md->body = tor_strdup("md2\n");
  1386. node2->md->bodylen = 4;
  1387. MOCK(nodelist_get_list, mock_nodelist_get_list);
  1388. nodes = smartlist_new();
  1389. retval = getinfo_helper_dir(NULL, "md/all", &answer, &errmsg);
  1390. tt_int_op(n_nodelist_get_list, OP_EQ, 1);
  1391. tt_int_op(retval, OP_EQ, 0);
  1392. tt_assert(answer != NULL);
  1393. tt_assert(errmsg == NULL);
  1394. tt_str_op(answer, OP_EQ, "");
  1395. tor_free(answer);
  1396. smartlist_add(nodes, node1);
  1397. smartlist_add(nodes, node2);
  1398. retval = getinfo_helper_dir(NULL, "md/all", &answer, &errmsg);
  1399. tt_int_op(n_nodelist_get_list, OP_EQ, 2);
  1400. tt_int_op(retval, OP_EQ, 0);
  1401. tt_assert(answer != NULL);
  1402. tt_assert(errmsg == NULL);
  1403. tt_str_op(answer, OP_EQ, "md1\nmd2\n");
  1404. done:
  1405. UNMOCK(nodelist_get_list);
  1406. tor_free(node1->md->body);
  1407. tor_free(node1->md);
  1408. tor_free(node1);
  1409. tor_free(node2->md->body);
  1410. tor_free(node2->md);
  1411. tor_free(node2);
  1412. tor_free(answer);
  1413. smartlist_free(nodes);
  1414. return;
  1415. }
  1416. struct testcase_t controller_tests[] = {
  1417. { "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2, 0,
  1418. NULL, NULL },
  1419. { "add_onion_helper_keyarg_v3", test_add_onion_helper_keyarg_v3, 0,
  1420. NULL, NULL },
  1421. { "getinfo_helper_onion", test_getinfo_helper_onion, 0, NULL, NULL },
  1422. { "rend_service_parse_port_config", test_rend_service_parse_port_config, 0,
  1423. NULL, NULL },
  1424. { "add_onion_helper_clientauth", test_add_onion_helper_clientauth, 0, NULL,
  1425. NULL },
  1426. { "download_status_consensus", test_download_status_consensus, 0, NULL,
  1427. NULL },
  1428. { "download_status_cert", test_download_status_cert, 0, NULL,
  1429. NULL },
  1430. { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
  1431. { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
  1432. { "current_time", test_current_time, 0, NULL, NULL },
  1433. { "getinfo_md_all", test_getinfo_md_all, 0, NULL, NULL },
  1434. END_OF_TESTCASES
  1435. };