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