1 /* Copyright (c) 2015-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define CONTROL_PRIVATE
8 #include "entrynodes.h"
10 #include "networkstatus.h"
11 #include "rendservice.h"
12 #include "routerlist.h"
14 #include "test_helpers.h"
17 test_add_onion_helper_keyarg_v3(void *arg
)
20 add_onion_secret_key_t pk
;
21 char *key_new_blob
= NULL
;
23 const char *key_new_alg
= NULL
;
27 memset(&pk
, 0, sizeof(pk
));
29 /* Test explicit ED25519-V3 key generation. */
30 ret
= add_onion_helper_keyarg("NEW:ED25519-V3", 0, &key_new_alg
,
31 &key_new_blob
, &pk
, &hs_version
,
33 tt_int_op(ret
, OP_EQ
, 0);
34 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_THREE
);
36 tt_str_op(key_new_alg
, OP_EQ
, "ED25519-V3");
37 tt_assert(key_new_blob
);
38 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
39 tor_free(pk
.v3
); pk
.v3
= NULL
;
40 tor_free(key_new_blob
);
42 /* Test discarding the private key. */
43 ret
= add_onion_helper_keyarg("NEW:ED25519-V3", 1, &key_new_alg
,
44 &key_new_blob
, &pk
, &hs_version
,
46 tt_int_op(ret
, OP_EQ
, 0);
47 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_THREE
);
49 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
50 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
51 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
52 tor_free(pk
.v3
); pk
.v3
= NULL
;
53 tor_free(key_new_blob
);
55 /* Test passing a key blob. */
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==";
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
;
69 tor_asprintf(&key_blob
, "ED25519-V3:%s", base64_sk
);
71 ret
= add_onion_helper_keyarg(key_blob
, 1, &key_new_alg
,
72 &key_new_blob
, &pk
, &hs_version
,
75 tt_int_op(ret
, OP_EQ
, 0);
76 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_THREE
);
78 tt_mem_op(pk
.v3
, OP_EQ
, hex_sk
, 64);
79 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
80 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
81 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
82 tor_free(pk
.v3
); pk
.v3
= NULL
;
83 tor_free(key_new_blob
);
88 tor_free(key_new_blob
);
93 test_add_onion_helper_keyarg_v2(void *arg
)
96 add_onion_secret_key_t pk
;
97 crypto_pk_t
*pk1
= NULL
;
98 const char *key_new_alg
= NULL
;
99 char *key_new_blob
= NULL
;
100 char *err_msg
= NULL
;
101 char *encoded
= NULL
;
102 char *arg_str
= NULL
;
106 memset(&pk
, 0, sizeof(pk
));
108 /* Test explicit RSA1024 key generation. */
109 ret
= add_onion_helper_keyarg("NEW:RSA1024", 0, &key_new_alg
, &key_new_blob
,
110 &pk
, &hs_version
, &err_msg
);
111 tt_int_op(ret
, OP_EQ
, 0);
112 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
114 tt_str_op(key_new_alg
, OP_EQ
, "RSA1024");
115 tt_assert(key_new_blob
);
116 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
118 /* Test "BEST" key generation (Assumes BEST = RSA1024). */
119 crypto_pk_free(pk
.v2
); pk
.v2
= NULL
;
120 tor_free(key_new_blob
);
121 ret
= add_onion_helper_keyarg("NEW:BEST", 0, &key_new_alg
, &key_new_blob
,
122 &pk
, &hs_version
, &err_msg
);
123 tt_int_op(ret
, OP_EQ
, 0);
124 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
126 tt_str_op(key_new_alg
, OP_EQ
, "RSA1024");
127 tt_assert(key_new_blob
);
128 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
130 /* Test discarding the private key. */
131 crypto_pk_free(pk
.v2
); pk
.v2
= NULL
;
132 tor_free(key_new_blob
);
133 ret
= add_onion_helper_keyarg("NEW:BEST", 1, &key_new_alg
, &key_new_blob
,
134 &pk
, &hs_version
, &err_msg
);
135 tt_int_op(ret
, OP_EQ
, 0);
136 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
138 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
139 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
140 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
142 /* Test generating a invalid key type. */
143 crypto_pk_free(pk
.v2
); pk
.v2
= NULL
;
144 ret
= add_onion_helper_keyarg("NEW:RSA512", 0, &key_new_alg
, &key_new_blob
,
145 &pk
, &hs_version
, &err_msg
);
146 tt_int_op(ret
, OP_EQ
, -1);
147 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
149 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
150 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
153 /* Test loading a RSA1024 key. */
155 pk1
= pk_generate(0);
156 tt_int_op(0, OP_EQ
, crypto_pk_base64_encode(pk1
, &encoded
));
157 tor_asprintf(&arg_str
, "RSA1024:%s", encoded
);
158 ret
= add_onion_helper_keyarg(arg_str
, 0, &key_new_alg
, &key_new_blob
,
159 &pk
, &hs_version
, &err_msg
);
160 tt_int_op(ret
, OP_EQ
, 0);
161 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
163 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
164 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
165 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
166 tt_int_op(crypto_pk_cmp_keys(pk1
, pk
.v2
), OP_EQ
, 0);
168 /* Test loading a invalid key type. */
170 crypto_pk_free(pk1
); pk1
= NULL
;
171 crypto_pk_free(pk
.v2
); pk
.v2
= NULL
;
172 tor_asprintf(&arg_str
, "RSA512:%s", encoded
);
173 ret
= add_onion_helper_keyarg(arg_str
, 0, &key_new_alg
, &key_new_blob
,
174 &pk
, &hs_version
, &err_msg
);
175 tt_int_op(ret
, OP_EQ
, -1);
176 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
178 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
179 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
182 /* Test loading a invalid key. */
184 crypto_pk_free(pk
.v2
); pk
.v2
= NULL
;
186 encoded
[strlen(encoded
)/2] = '\0';
187 tor_asprintf(&arg_str
, "RSA1024:%s", encoded
);
188 ret
= add_onion_helper_keyarg(arg_str
, 0, &key_new_alg
, &key_new_blob
,
189 &pk
, &hs_version
, &err_msg
);
190 tt_int_op(ret
, OP_EQ
, -1);
191 tt_int_op(hs_version
, OP_EQ
, HS_VERSION_TWO
);
193 tt_ptr_op(key_new_alg
, OP_EQ
, NULL
);
194 tt_ptr_op(key_new_blob
, OP_EQ
, NULL
);
199 crypto_pk_free(pk
.v2
);
200 tor_free(key_new_blob
);
207 test_getinfo_helper_onion(void *arg
)
210 control_connection_t dummy
;
211 /* Get results out */
213 const char *errmsg
= NULL
;
214 char *service_id
= NULL
;
217 dummy
.ephemeral_onion_services
= NULL
;
219 /* successfully get an empty answer */
220 rt
= getinfo_helper_onions(&dummy
, "onions/current", &answer
, &errmsg
);
221 tt_int_op(rt
, OP_EQ
, 0);
222 tt_str_op(answer
, OP_EQ
, "");
225 /* successfully get an empty answer */
226 rt
= getinfo_helper_onions(&dummy
, "onions/detached", &answer
, &errmsg
);
227 tt_int_op(rt
, OP_EQ
, 0);
228 tt_str_op(answer
, OP_EQ
, "");
231 /* get an answer for one onion service */
232 service_id
= tor_strdup("dummy_onion_id");
233 dummy
.ephemeral_onion_services
= smartlist_new();
234 smartlist_add(dummy
.ephemeral_onion_services
, service_id
);
235 rt
= getinfo_helper_onions(&dummy
, "onions/current", &answer
, &errmsg
);
236 tt_int_op(rt
, OP_EQ
, 0);
237 tt_str_op(answer
, OP_EQ
, "dummy_onion_id");
241 tor_free(service_id
);
242 smartlist_free(dummy
.ephemeral_onion_services
);
246 test_rend_service_parse_port_config(void *arg
)
248 const char *sep
= ",";
249 rend_service_port_config_t
*cfg
= NULL
;
250 char *err_msg
= NULL
;
254 /* Test "VIRTPORT" only. */
255 cfg
= rend_service_parse_port_config("80", sep
, &err_msg
);
257 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
259 /* Test "VIRTPORT,TARGET" (Target is port). */
260 rend_service_port_config_free(cfg
);
261 cfg
= rend_service_parse_port_config("80,8080", sep
, &err_msg
);
263 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
265 /* Test "VIRTPORT,TARGET" (Target is IPv4:port). */
266 rend_service_port_config_free(cfg
);
267 cfg
= rend_service_parse_port_config("80,192.0.2.1:8080", sep
, &err_msg
);
269 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
271 /* Test "VIRTPORT,TARGET" (Target is IPv6:port). */
272 rend_service_port_config_free(cfg
);
273 cfg
= rend_service_parse_port_config("80,[2001:db8::1]:8080", sep
, &err_msg
);
275 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
276 rend_service_port_config_free(cfg
);
279 /* XXX: Someone should add tests for AF_UNIX targets if supported. */
281 /* Test empty config. */
282 rend_service_port_config_free(cfg
);
283 cfg
= rend_service_parse_port_config("", sep
, &err_msg
);
284 tt_ptr_op(cfg
, OP_EQ
, NULL
);
287 /* Test invalid port. */
289 cfg
= rend_service_parse_port_config("90001", sep
, &err_msg
);
290 tt_ptr_op(cfg
, OP_EQ
, NULL
);
297 /* quoted unix port */
299 cfg
= rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
302 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
303 rend_service_port_config_free(cfg
);
306 /* quoted unix port */
308 cfg
= rend_service_parse_port_config("100 unix:\"/tmp/foo bar\"",
311 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
312 rend_service_port_config_free(cfg
);
315 /* quoted unix port, missing end quote */
316 cfg
= rend_service_parse_port_config("100 unix:\"/tmp/foo bar",
318 tt_ptr_op(cfg
, OP_EQ
, NULL
);
319 tt_str_op(err_msg
, OP_EQ
, "Couldn't process address <unix:\"/tmp/foo bar> "
320 "from hidden service configuration");
323 /* bogus IP address */
324 MOCK(tor_addr_lookup
, mock_tor_addr_lookup__fail_on_bad_addrs
);
325 cfg
= rend_service_parse_port_config("100 foo!!.example.com:9000",
327 UNMOCK(tor_addr_lookup
);
328 tt_ptr_op(cfg
, OP_EQ
, NULL
);
329 tt_str_op(err_msg
, OP_EQ
, "Unparseable address in hidden service port "
333 /* bogus port port */
334 cfg
= rend_service_parse_port_config("100 99999",
336 tt_ptr_op(cfg
, OP_EQ
, NULL
);
337 tt_str_op(err_msg
, OP_EQ
, "Unparseable or out-of-range port \"99999\" "
338 "in hidden service port configuration.");
342 rend_service_port_config_free(cfg
);
347 test_add_onion_helper_clientauth(void *arg
)
349 rend_authorized_client_t
*client
= NULL
;
350 char *err_msg
= NULL
;
355 /* Test "ClientName" only. */
356 client
= add_onion_helper_clientauth("alice", &created
, &err_msg
);
359 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
360 rend_authorized_client_free(client
);
362 /* Test "ClientName:Blob" */
363 client
= add_onion_helper_clientauth("alice:475hGBHPlq7Mc0cRZitK/B",
367 tt_ptr_op(err_msg
, OP_EQ
, NULL
);
368 rend_authorized_client_free(client
);
370 /* Test invalid client names */
371 client
= add_onion_helper_clientauth("no*asterisks*allowed", &created
,
373 tt_ptr_op(client
, OP_EQ
, NULL
);
377 /* Test invalid auth cookie */
378 client
= add_onion_helper_clientauth("alice:12345", &created
, &err_msg
);
379 tt_ptr_op(client
, OP_EQ
, NULL
);
383 /* Test invalid syntax */
384 client
= add_onion_helper_clientauth(":475hGBHPlq7Mc0cRZitK/B", &created
,
386 tt_ptr_op(client
, OP_EQ
, NULL
);
391 rend_authorized_client_free(client
);
395 /* Mocks and data/variables used for GETINFO download status tests */
397 static const download_status_t dl_status_default
=
398 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
399 DL_SCHED_INCREMENT_FAILURE
, 0, 0 };
400 static download_status_t ns_dl_status
[N_CONSENSUS_FLAVORS
];
401 static download_status_t ns_dl_status_bootstrap
[N_CONSENSUS_FLAVORS
];
402 static download_status_t ns_dl_status_running
[N_CONSENSUS_FLAVORS
];
405 * These should explore all the possible cases of download_status_to_string()
408 static const download_status_t dls_sample_1
=
409 { 1467163900, 0, 0, DL_SCHED_GENERIC
, DL_WANT_ANY_DIRSERVER
,
410 DL_SCHED_INCREMENT_FAILURE
, 0, 0 };
411 static const char * dls_sample_1_str
=
412 "next-attempt-at 2016-06-29 01:31:40\n"
413 "n-download-failures 0\n"
414 "n-download-attempts 0\n"
415 "schedule DL_SCHED_GENERIC\n"
416 "want-authority DL_WANT_ANY_DIRSERVER\n"
417 "increment-on DL_SCHED_INCREMENT_FAILURE\n"
418 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
419 "last-backoff-position 0\n"
420 "last-delay-used 0\n";
421 static const download_status_t dls_sample_2
=
422 { 1467164400, 1, 2, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
423 DL_SCHED_INCREMENT_FAILURE
, 0, 0 };
424 static const char * dls_sample_2_str
=
425 "next-attempt-at 2016-06-29 01:40:00\n"
426 "n-download-failures 1\n"
427 "n-download-attempts 2\n"
428 "schedule DL_SCHED_CONSENSUS\n"
429 "want-authority DL_WANT_AUTHORITY\n"
430 "increment-on DL_SCHED_INCREMENT_FAILURE\n"
431 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
432 "last-backoff-position 0\n"
433 "last-delay-used 0\n";
434 static const download_status_t dls_sample_3
=
435 { 1467154400, 12, 25, DL_SCHED_BRIDGE
, DL_WANT_ANY_DIRSERVER
,
436 DL_SCHED_INCREMENT_ATTEMPT
, 0, 0 };
437 static const char * dls_sample_3_str
=
438 "next-attempt-at 2016-06-28 22:53:20\n"
439 "n-download-failures 12\n"
440 "n-download-attempts 25\n"
441 "schedule DL_SCHED_BRIDGE\n"
442 "want-authority DL_WANT_ANY_DIRSERVER\n"
443 "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
444 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
445 "last-backoff-position 0\n"
446 "last-delay-used 0\n";
447 static const download_status_t dls_sample_4
=
448 { 1467166600, 3, 0, DL_SCHED_GENERIC
, DL_WANT_ANY_DIRSERVER
,
449 DL_SCHED_INCREMENT_FAILURE
, 0, 0 };
450 static const char * dls_sample_4_str
=
451 "next-attempt-at 2016-06-29 02:16:40\n"
452 "n-download-failures 3\n"
453 "n-download-attempts 0\n"
454 "schedule DL_SCHED_GENERIC\n"
455 "want-authority DL_WANT_ANY_DIRSERVER\n"
456 "increment-on DL_SCHED_INCREMENT_FAILURE\n"
457 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
458 "last-backoff-position 0\n"
459 "last-delay-used 0\n";
460 static const download_status_t dls_sample_5
=
461 { 1467164600, 3, 7, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
462 DL_SCHED_INCREMENT_FAILURE
, 1, 2112, };
463 static const char * dls_sample_5_str
=
464 "next-attempt-at 2016-06-29 01:43:20\n"
465 "n-download-failures 3\n"
466 "n-download-attempts 7\n"
467 "schedule DL_SCHED_CONSENSUS\n"
468 "want-authority DL_WANT_ANY_DIRSERVER\n"
469 "increment-on DL_SCHED_INCREMENT_FAILURE\n"
470 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
471 "last-backoff-position 1\n"
472 "last-delay-used 2112\n";
473 static const download_status_t dls_sample_6
=
474 { 1467164200, 4, 9, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
475 DL_SCHED_INCREMENT_ATTEMPT
, 3, 432 };
476 static const char * dls_sample_6_str
=
477 "next-attempt-at 2016-06-29 01:36:40\n"
478 "n-download-failures 4\n"
479 "n-download-attempts 9\n"
480 "schedule DL_SCHED_CONSENSUS\n"
481 "want-authority DL_WANT_AUTHORITY\n"
482 "increment-on DL_SCHED_INCREMENT_ATTEMPT\n"
483 "backoff DL_SCHED_RANDOM_EXPONENTIAL\n"
484 "last-backoff-position 3\n"
485 "last-delay-used 432\n";
487 /* Simulated auth certs */
488 static const char *auth_id_digest_1_str
=
489 "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061";
490 static download_status_t auth_def_cert_download_status_1
;
491 static const char *auth_id_digest_2_str
=
492 "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99";
493 static download_status_t auth_def_cert_download_status_2
;
494 /* Expected form of digest list returned for GETINFO downloads/cert/fps */
495 static const char *auth_id_digest_expected_list
=
496 "63CDD326DFEF0CA020BDD3FEB45A3286FE13A061\n"
497 "2C209FCDD8D48DC049777B8DC2C0F94A0408BE99\n";
499 /* Signing keys for simulated auth 1 */
500 static const char *auth_1_sk_1_str
=
501 "AA69566029B1F023BA09451B8F1B10952384EB58";
502 static download_status_t auth_1_sk_1_dls
;
503 static const char *auth_1_sk_2_str
=
504 "710865C7F06B73C5292695A8C34F1C94F769FF72";
505 static download_status_t auth_1_sk_2_dls
;
507 * Expected form of sk digest list for
508 * GETINFO downloads/cert/<auth_id_digest_1_str>/sks
510 static const char *auth_1_sk_digest_expected_list
=
511 "AA69566029B1F023BA09451B8F1B10952384EB58\n"
512 "710865C7F06B73C5292695A8C34F1C94F769FF72\n";
514 /* Signing keys for simulated auth 2 */
515 static const char *auth_2_sk_1_str
=
516 "4299047E00D070AD6703FE00BE7AA756DB061E62";
517 static download_status_t auth_2_sk_1_dls
;
518 static const char *auth_2_sk_2_str
=
519 "9451B8F1B10952384EB58B5F230C0BB701626C9B";
520 static download_status_t auth_2_sk_2_dls
;
522 * Expected form of sk digest list for
523 * GETINFO downloads/cert/<auth_id_digest_2_str>/sks
525 static const char *auth_2_sk_digest_expected_list
=
526 "4299047E00D070AD6703FE00BE7AA756DB061E62\n"
527 "9451B8F1B10952384EB58B5F230C0BB701626C9B\n";
529 /* Simulated router descriptor digests or bridge identity digests */
530 static const char *descbr_digest_1_str
=
531 "616408544C7345822696074A1A3DFA16AB381CBD";
532 static download_status_t descbr_digest_1_dl
;
533 static const char *descbr_digest_2_str
=
534 "06E8067246967265DBCB6641631B530EFEC12DC3";
535 static download_status_t descbr_digest_2_dl
;
536 /* Expected form of digest list returned for GETINFO downloads/desc/descs */
537 static const char *descbr_expected_list
=
538 "616408544C7345822696074A1A3DFA16AB381CBD\n"
539 "06E8067246967265DBCB6641631B530EFEC12DC3\n";
541 * Flag to make all descbr queries fail, to simulate not being
542 * configured such that such queries make sense.
544 static int disable_descbr
= 0;
547 reset_mocked_dl_statuses(void)
551 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
552 memcpy(&(ns_dl_status
[i
]), &dl_status_default
,
553 sizeof(download_status_t
));
554 memcpy(&(ns_dl_status_bootstrap
[i
]), &dl_status_default
,
555 sizeof(download_status_t
));
556 memcpy(&(ns_dl_status_running
[i
]), &dl_status_default
,
557 sizeof(download_status_t
));
560 memcpy(&auth_def_cert_download_status_1
, &dl_status_default
,
561 sizeof(download_status_t
));
562 memcpy(&auth_def_cert_download_status_2
, &dl_status_default
,
563 sizeof(download_status_t
));
564 memcpy(&auth_1_sk_1_dls
, &dl_status_default
,
565 sizeof(download_status_t
));
566 memcpy(&auth_1_sk_2_dls
, &dl_status_default
,
567 sizeof(download_status_t
));
568 memcpy(&auth_2_sk_1_dls
, &dl_status_default
,
569 sizeof(download_status_t
));
570 memcpy(&auth_2_sk_2_dls
, &dl_status_default
,
571 sizeof(download_status_t
));
573 memcpy(&descbr_digest_1_dl
, &dl_status_default
,
574 sizeof(download_status_t
));
575 memcpy(&descbr_digest_2_dl
, &dl_status_default
,
576 sizeof(download_status_t
));
579 static download_status_t
*
580 ns_dl_status_mock(consensus_flavor_t flavor
)
582 return &(ns_dl_status
[flavor
]);
585 static download_status_t
*
586 ns_dl_status_bootstrap_mock(consensus_flavor_t flavor
)
588 return &(ns_dl_status_bootstrap
[flavor
]);
591 static download_status_t
*
592 ns_dl_status_running_mock(consensus_flavor_t flavor
)
594 return &(ns_dl_status_running
[flavor
]);
600 MOCK(networkstatus_get_dl_status_by_flavor
, ns_dl_status_mock
);
601 MOCK(networkstatus_get_dl_status_by_flavor_bootstrap
,
602 ns_dl_status_bootstrap_mock
);
603 MOCK(networkstatus_get_dl_status_by_flavor_running
,
604 ns_dl_status_running_mock
);
605 reset_mocked_dl_statuses();
611 UNMOCK(networkstatus_get_dl_status_by_flavor
);
612 UNMOCK(networkstatus_get_dl_status_by_flavor_bootstrap
);
613 UNMOCK(networkstatus_get_dl_status_by_flavor_running
);
617 cert_dl_status_auth_ids_mock(void)
619 char digest
[DIGEST_LEN
], *tmp
;
621 smartlist_t
*list
= NULL
;
623 /* Just pretend we have only the two hard-coded digests listed above */
624 list
= smartlist_new();
625 len
= base16_decode(digest
, DIGEST_LEN
,
626 auth_id_digest_1_str
, strlen(auth_id_digest_1_str
));
627 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
628 tmp
= tor_malloc(DIGEST_LEN
);
629 memcpy(tmp
, digest
, DIGEST_LEN
);
630 smartlist_add(list
, tmp
);
631 len
= base16_decode(digest
, DIGEST_LEN
,
632 auth_id_digest_2_str
, strlen(auth_id_digest_2_str
));
633 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
634 tmp
= tor_malloc(DIGEST_LEN
);
635 memcpy(tmp
, digest
, DIGEST_LEN
);
636 smartlist_add(list
, tmp
);
642 static download_status_t
*
643 cert_dl_status_def_for_auth_mock(const char *digest
)
645 download_status_t
*dl
= NULL
;
646 char digest_str
[HEX_DIGEST_LEN
+1];
648 tt_ptr_op(digest
, OP_NE
, NULL
);
649 base16_encode(digest_str
, HEX_DIGEST_LEN
+ 1,
651 digest_str
[HEX_DIGEST_LEN
] = '\0';
653 if (strcmp(digest_str
, auth_id_digest_1_str
) == 0) {
654 dl
= &auth_def_cert_download_status_1
;
655 } else if (strcmp(digest_str
, auth_id_digest_2_str
) == 0) {
656 dl
= &auth_def_cert_download_status_2
;
664 cert_dl_status_sks_for_auth_id_mock(const char *digest
)
666 smartlist_t
*list
= NULL
;
668 char digest_str
[HEX_DIGEST_LEN
+1];
672 tt_ptr_op(digest
, OP_NE
, NULL
);
673 base16_encode(digest_str
, HEX_DIGEST_LEN
+ 1,
675 digest_str
[HEX_DIGEST_LEN
] = '\0';
678 * Build a list of two hard-coded digests, depending on what we
681 if (strcmp(digest_str
, auth_id_digest_1_str
) == 0) {
682 list
= smartlist_new();
683 len
= base16_decode(sk
, DIGEST_LEN
,
684 auth_1_sk_1_str
, strlen(auth_1_sk_1_str
));
685 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
686 tmp
= tor_malloc(DIGEST_LEN
);
687 memcpy(tmp
, sk
, DIGEST_LEN
);
688 smartlist_add(list
, tmp
);
689 len
= base16_decode(sk
, DIGEST_LEN
,
690 auth_1_sk_2_str
, strlen(auth_1_sk_2_str
));
691 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
692 tmp
= tor_malloc(DIGEST_LEN
);
693 memcpy(tmp
, sk
, DIGEST_LEN
);
694 smartlist_add(list
, tmp
);
695 } else if (strcmp(digest_str
, auth_id_digest_2_str
) == 0) {
696 list
= smartlist_new();
697 len
= base16_decode(sk
, DIGEST_LEN
,
698 auth_2_sk_1_str
, strlen(auth_2_sk_1_str
));
699 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
700 tmp
= tor_malloc(DIGEST_LEN
);
701 memcpy(tmp
, sk
, DIGEST_LEN
);
702 smartlist_add(list
, tmp
);
703 len
= base16_decode(sk
, DIGEST_LEN
,
704 auth_2_sk_2_str
, strlen(auth_2_sk_2_str
));
705 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
706 tmp
= tor_malloc(DIGEST_LEN
);
707 memcpy(tmp
, sk
, DIGEST_LEN
);
708 smartlist_add(list
, tmp
);
715 static download_status_t
*
716 cert_dl_status_fp_sk_mock(const char *fp_digest
, const char *sk_digest
)
718 download_status_t
*dl
= NULL
;
719 char fp_digest_str
[HEX_DIGEST_LEN
+1], sk_digest_str
[HEX_DIGEST_LEN
+1];
722 * Unpack the digests so we can compare them and figure out which
726 tt_ptr_op(fp_digest
, OP_NE
, NULL
);
727 base16_encode(fp_digest_str
, HEX_DIGEST_LEN
+ 1,
728 fp_digest
, DIGEST_LEN
);
729 fp_digest_str
[HEX_DIGEST_LEN
] = '\0';
730 tt_ptr_op(sk_digest
, OP_NE
, NULL
);
731 base16_encode(sk_digest_str
, HEX_DIGEST_LEN
+ 1,
732 sk_digest
, DIGEST_LEN
);
733 sk_digest_str
[HEX_DIGEST_LEN
] = '\0';
735 if (strcmp(fp_digest_str
, auth_id_digest_1_str
) == 0) {
736 if (strcmp(sk_digest_str
, auth_1_sk_1_str
) == 0) {
737 dl
= &auth_1_sk_1_dls
;
738 } else if (strcmp(sk_digest_str
, auth_1_sk_2_str
) == 0) {
739 dl
= &auth_1_sk_2_dls
;
741 } else if (strcmp(fp_digest_str
, auth_id_digest_2_str
) == 0) {
742 if (strcmp(sk_digest_str
, auth_2_sk_1_str
) == 0) {
743 dl
= &auth_2_sk_1_dls
;
744 } else if (strcmp(sk_digest_str
, auth_2_sk_2_str
) == 0) {
745 dl
= &auth_2_sk_2_dls
;
754 setup_cert_mocks(void)
756 MOCK(list_authority_ids_with_downloads
, cert_dl_status_auth_ids_mock
);
757 MOCK(id_only_download_status_for_authority_id
,
758 cert_dl_status_def_for_auth_mock
);
759 MOCK(list_sk_digests_for_authority_id
,
760 cert_dl_status_sks_for_auth_id_mock
);
761 MOCK(download_status_for_authority_id_and_sk
,
762 cert_dl_status_fp_sk_mock
);
763 reset_mocked_dl_statuses();
767 clear_cert_mocks(void)
769 UNMOCK(list_authority_ids_with_downloads
);
770 UNMOCK(id_only_download_status_for_authority_id
);
771 UNMOCK(list_sk_digests_for_authority_id
);
772 UNMOCK(download_status_for_authority_id_and_sk
);
776 descbr_get_digests_mock(void)
778 char digest
[DIGEST_LEN
], *tmp
;
780 smartlist_t
*list
= NULL
;
782 if (!disable_descbr
) {
783 /* Just pretend we have only the two hard-coded digests listed above */
784 list
= smartlist_new();
785 len
= base16_decode(digest
, DIGEST_LEN
,
786 descbr_digest_1_str
, strlen(descbr_digest_1_str
));
787 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
788 tmp
= tor_malloc(DIGEST_LEN
);
789 memcpy(tmp
, digest
, DIGEST_LEN
);
790 smartlist_add(list
, tmp
);
791 len
= base16_decode(digest
, DIGEST_LEN
,
792 descbr_digest_2_str
, strlen(descbr_digest_2_str
));
793 tt_int_op(len
, OP_EQ
, DIGEST_LEN
);
794 tmp
= tor_malloc(DIGEST_LEN
);
795 memcpy(tmp
, digest
, DIGEST_LEN
);
796 smartlist_add(list
, tmp
);
803 static download_status_t
*
804 descbr_get_dl_by_digest_mock(const char *digest
)
806 download_status_t
*dl
= NULL
;
807 char digest_str
[HEX_DIGEST_LEN
+1];
809 if (!disable_descbr
) {
810 tt_ptr_op(digest
, OP_NE
, NULL
);
811 base16_encode(digest_str
, HEX_DIGEST_LEN
+ 1,
813 digest_str
[HEX_DIGEST_LEN
] = '\0';
815 if (strcmp(digest_str
, descbr_digest_1_str
) == 0) {
816 dl
= &descbr_digest_1_dl
;
817 } else if (strcmp(digest_str
, descbr_digest_2_str
) == 0) {
818 dl
= &descbr_digest_2_dl
;
827 setup_desc_mocks(void)
829 MOCK(router_get_descriptor_digests
,
830 descbr_get_digests_mock
);
831 MOCK(router_get_dl_status_by_descriptor_digest
,
832 descbr_get_dl_by_digest_mock
);
833 reset_mocked_dl_statuses();
837 clear_desc_mocks(void)
839 UNMOCK(router_get_descriptor_digests
);
840 UNMOCK(router_get_dl_status_by_descriptor_digest
);
844 setup_bridge_mocks(void)
848 MOCK(list_bridge_identities
,
849 descbr_get_digests_mock
);
850 MOCK(get_bridge_dl_status_by_id
,
851 descbr_get_dl_by_digest_mock
);
852 reset_mocked_dl_statuses();
856 clear_bridge_mocks(void)
858 UNMOCK(list_bridge_identities
);
859 UNMOCK(get_bridge_dl_status_by_id
);
865 test_download_status_consensus(void *arg
)
867 /* We just need one of these to pass, it doesn't matter what's in it */
868 control_connection_t dummy
;
869 /* Get results out */
871 const char *errmsg
= NULL
;
875 /* Check that the unknown prefix case works; no mocks needed yet */
876 getinfo_helper_downloads(&dummy
, "downloads/foo", &answer
, &errmsg
);
877 tt_ptr_op(answer
, OP_EQ
, NULL
);
878 tt_str_op(errmsg
, OP_EQ
, "Unknown download status query");
883 * Check returning serialized dlstatuses, and implicitly also test
884 * download_status_to_string().
887 /* Case 1 default/FLAV_NS*/
888 memcpy(&(ns_dl_status
[FLAV_NS
]), &dls_sample_1
,
889 sizeof(download_status_t
));
890 getinfo_helper_downloads(&dummy
, "downloads/networkstatus/ns",
892 tt_ptr_op(answer
, OP_NE
, NULL
);
893 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
894 tt_str_op(answer
, OP_EQ
, dls_sample_1_str
);
898 /* Case 2 default/FLAV_MICRODESC */
899 memcpy(&(ns_dl_status
[FLAV_MICRODESC
]), &dls_sample_2
,
900 sizeof(download_status_t
));
901 getinfo_helper_downloads(&dummy
, "downloads/networkstatus/microdesc",
903 tt_ptr_op(answer
, OP_NE
, NULL
);
904 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
905 tt_str_op(answer
, OP_EQ
, dls_sample_2_str
);
909 /* Case 3 bootstrap/FLAV_NS */
910 memcpy(&(ns_dl_status_bootstrap
[FLAV_NS
]), &dls_sample_3
,
911 sizeof(download_status_t
));
912 getinfo_helper_downloads(&dummy
, "downloads/networkstatus/ns/bootstrap",
914 tt_ptr_op(answer
, OP_NE
, NULL
);
915 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
916 tt_str_op(answer
, OP_EQ
, dls_sample_3_str
);
920 /* Case 4 bootstrap/FLAV_MICRODESC */
921 memcpy(&(ns_dl_status_bootstrap
[FLAV_MICRODESC
]), &dls_sample_4
,
922 sizeof(download_status_t
));
923 getinfo_helper_downloads(&dummy
,
924 "downloads/networkstatus/microdesc/bootstrap",
926 tt_ptr_op(answer
, OP_NE
, NULL
);
927 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
928 tt_str_op(answer
, OP_EQ
, dls_sample_4_str
);
932 /* Case 5 running/FLAV_NS */
933 memcpy(&(ns_dl_status_running
[FLAV_NS
]), &dls_sample_5
,
934 sizeof(download_status_t
));
935 getinfo_helper_downloads(&dummy
,
936 "downloads/networkstatus/ns/running",
938 tt_ptr_op(answer
, OP_NE
, NULL
);
939 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
940 tt_str_op(answer
, OP_EQ
, dls_sample_5_str
);
944 /* Case 6 running/FLAV_MICRODESC */
945 memcpy(&(ns_dl_status_running
[FLAV_MICRODESC
]), &dls_sample_6
,
946 sizeof(download_status_t
));
947 getinfo_helper_downloads(&dummy
,
948 "downloads/networkstatus/microdesc/running",
950 tt_ptr_op(answer
, OP_NE
, NULL
);
951 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
952 tt_str_op(answer
, OP_EQ
, dls_sample_6_str
);
956 /* Now check the error case */
957 getinfo_helper_downloads(&dummy
, "downloads/networkstatus/foo",
959 tt_ptr_op(answer
, OP_EQ
, NULL
);
960 tt_ptr_op(errmsg
, OP_NE
, NULL
);
961 tt_str_op(errmsg
, OP_EQ
, "Unknown flavor");
972 test_download_status_cert(void *arg
)
974 /* We just need one of these to pass, it doesn't matter what's in it */
975 control_connection_t dummy
;
976 /* Get results out */
977 char *question
= NULL
;
979 const char *errmsg
= NULL
;
986 * Check returning serialized dlstatuses and digest lists, and implicitly
987 * also test download_status_to_string() and digest_list_to_string().
990 /* Case 1 - list of authority identity fingerprints */
991 getinfo_helper_downloads(&dummy
,
992 "downloads/cert/fps",
994 tt_ptr_op(answer
, OP_NE
, NULL
);
995 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
996 tt_str_op(answer
, OP_EQ
, auth_id_digest_expected_list
);
1000 /* Case 2 - download status for default cert for 1st auth id */
1001 memcpy(&auth_def_cert_download_status_1
, &dls_sample_1
,
1002 sizeof(download_status_t
));
1003 tor_asprintf(&question
, "downloads/cert/fp/%s", auth_id_digest_1_str
);
1004 tt_ptr_op(question
, OP_NE
, NULL
);
1005 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1006 tt_ptr_op(answer
, OP_NE
, NULL
);
1007 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1008 tt_str_op(answer
, OP_EQ
, dls_sample_1_str
);
1013 /* Case 3 - download status for default cert for 2nd auth id */
1014 memcpy(&auth_def_cert_download_status_2
, &dls_sample_2
,
1015 sizeof(download_status_t
));
1016 tor_asprintf(&question
, "downloads/cert/fp/%s", auth_id_digest_2_str
);
1017 tt_ptr_op(question
, OP_NE
, NULL
);
1018 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1019 tt_ptr_op(answer
, OP_NE
, NULL
);
1020 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1021 tt_str_op(answer
, OP_EQ
, dls_sample_2_str
);
1026 /* Case 4 - list of signing key digests for 1st auth id */
1027 tor_asprintf(&question
, "downloads/cert/fp/%s/sks", auth_id_digest_1_str
);
1028 tt_ptr_op(question
, OP_NE
, NULL
);
1029 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1030 tt_ptr_op(answer
, OP_NE
, NULL
);
1031 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1032 tt_str_op(answer
, OP_EQ
, auth_1_sk_digest_expected_list
);
1037 /* Case 5 - list of signing key digests for 2nd auth id */
1038 tor_asprintf(&question
, "downloads/cert/fp/%s/sks", auth_id_digest_2_str
);
1039 tt_ptr_op(question
, OP_NE
, NULL
);
1040 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1041 tt_ptr_op(answer
, OP_NE
, NULL
);
1042 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1043 tt_str_op(answer
, OP_EQ
, auth_2_sk_digest_expected_list
);
1048 /* Case 6 - download status for 1st auth id, 1st sk */
1049 memcpy(&auth_1_sk_1_dls
, &dls_sample_3
,
1050 sizeof(download_status_t
));
1051 tor_asprintf(&question
, "downloads/cert/fp/%s/%s",
1052 auth_id_digest_1_str
, auth_1_sk_1_str
);
1053 tt_ptr_op(question
, OP_NE
, NULL
);
1054 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1055 tt_ptr_op(answer
, OP_NE
, NULL
);
1056 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1057 tt_str_op(answer
, OP_EQ
, dls_sample_3_str
);
1062 /* Case 7 - download status for 1st auth id, 2nd sk */
1063 memcpy(&auth_1_sk_2_dls
, &dls_sample_4
,
1064 sizeof(download_status_t
));
1065 tor_asprintf(&question
, "downloads/cert/fp/%s/%s",
1066 auth_id_digest_1_str
, auth_1_sk_2_str
);
1067 tt_ptr_op(question
, OP_NE
, NULL
);
1068 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1069 tt_ptr_op(answer
, OP_NE
, NULL
);
1070 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1071 tt_str_op(answer
, OP_EQ
, dls_sample_4_str
);
1076 /* Case 8 - download status for 2nd auth id, 1st sk */
1077 memcpy(&auth_2_sk_1_dls
, &dls_sample_5
,
1078 sizeof(download_status_t
));
1079 tor_asprintf(&question
, "downloads/cert/fp/%s/%s",
1080 auth_id_digest_2_str
, auth_2_sk_1_str
);
1081 tt_ptr_op(question
, OP_NE
, NULL
);
1082 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1083 tt_ptr_op(answer
, OP_NE
, NULL
);
1084 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1085 tt_str_op(answer
, OP_EQ
, dls_sample_5_str
);
1090 /* Case 9 - download status for 2nd auth id, 2nd sk */
1091 memcpy(&auth_2_sk_2_dls
, &dls_sample_6
,
1092 sizeof(download_status_t
));
1093 tor_asprintf(&question
, "downloads/cert/fp/%s/%s",
1094 auth_id_digest_2_str
, auth_2_sk_2_str
);
1095 tt_ptr_op(question
, OP_NE
, NULL
);
1096 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1097 tt_ptr_op(answer
, OP_NE
, NULL
);
1098 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1099 tt_str_op(answer
, OP_EQ
, dls_sample_6_str
);
1104 /* Now check the error cases */
1106 /* Case 1 - query is garbage after downloads/cert/ part */
1107 getinfo_helper_downloads(&dummy
, "downloads/cert/blahdeblah",
1109 tt_ptr_op(answer
, OP_EQ
, NULL
);
1110 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1111 tt_str_op(errmsg
, OP_EQ
, "Unknown certificate download status query");
1115 * Case 2 - looks like downloads/cert/fp/<fp>, but <fp> isn't even
1116 * the right length for a digest.
1118 getinfo_helper_downloads(&dummy
, "downloads/cert/fp/2B1D36D32B2942406",
1120 tt_ptr_op(answer
, OP_EQ
, NULL
);
1121 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1122 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a digest");
1126 * Case 3 - looks like downloads/cert/fp/<fp>, and <fp> is digest-sized,
1127 * but not parseable as one.
1129 getinfo_helper_downloads(&dummy
,
1130 "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1",
1132 tt_ptr_op(answer
, OP_EQ
, NULL
);
1133 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1134 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a digest");
1138 * Case 4 - downloads/cert/fp/<fp>, and <fp> is not a known authority
1141 getinfo_helper_downloads(&dummy
,
1142 "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61",
1144 tt_ptr_op(answer
, OP_EQ
, NULL
);
1145 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1146 tt_str_op(errmsg
, OP_EQ
,
1147 "Failed to get download status for this authority identity digest");
1151 * Case 5 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
1152 * parse as a sensible digest.
1154 getinfo_helper_downloads(&dummy
,
1155 "downloads/cert/fp/82F52AF55D250115FE44D3GC81D49643241D56A1/blah",
1157 tt_ptr_op(answer
, OP_EQ
, NULL
);
1158 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1159 tt_str_op(errmsg
, OP_EQ
, "That didn't look like an identity digest");
1163 * Case 6 - looks like downloads/cert/fp/<fp>/<anything>, but <fp> doesn't
1164 * parse as a sensible digest.
1166 getinfo_helper_downloads(&dummy
,
1167 "downloads/cert/fp/82F52AF55D25/blah",
1169 tt_ptr_op(answer
, OP_EQ
, NULL
);
1170 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1171 tt_str_op(errmsg
, OP_EQ
, "That didn't look like an identity digest");
1175 * Case 7 - downloads/cert/fp/<fp>/sks, and <fp> is not a known authority
1178 getinfo_helper_downloads(&dummy
,
1179 "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/sks",
1181 tt_ptr_op(answer
, OP_EQ
, NULL
);
1182 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1183 tt_str_op(errmsg
, OP_EQ
,
1184 "Failed to get list of signing key digests for this authority "
1189 * Case 8 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
1190 * parse as a signing key digest.
1192 getinfo_helper_downloads(&dummy
,
1193 "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
1194 "82F52AF55D250115FE44D3GC81D49643241D56A1",
1196 tt_ptr_op(answer
, OP_EQ
, NULL
);
1197 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1198 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a signing key digest");
1202 * Case 9 - looks like downloads/cert/fp/<fp>/<sk>, but <sk> doesn't
1203 * parse as a signing key digest.
1205 getinfo_helper_downloads(&dummy
,
1206 "downloads/cert/fp/AC4F23B5745BDD2A77997B85B1FD85D05C2E0F61/"
1207 "82F52AF55D250115FE44D",
1209 tt_ptr_op(answer
, OP_EQ
, NULL
);
1210 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1211 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a signing key digest");
1215 * Case 10 - downloads/cert/fp/<fp>/<sk>, but <fp> isn't a known
1216 * authority identity digest.
1218 getinfo_helper_downloads(&dummy
,
1219 "downloads/cert/fp/C6B05DF332F74DB9A13498EE3BBC7AA2F69FCB45/"
1220 "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
1222 tt_ptr_op(answer
, OP_EQ
, NULL
);
1223 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1224 tt_str_op(errmsg
, OP_EQ
,
1225 "Failed to get download status for this identity/"
1226 "signing key digest pair");
1230 * Case 11 - downloads/cert/fp/<fp>/<sk>, but <sk> isn't a known
1231 * signing key digest.
1233 getinfo_helper_downloads(&dummy
,
1234 "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
1235 "3A214FC21AE25B012C2ECCB5F4EC8A3602D0545D",
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");
1245 * Case 12 - downloads/cert/fp/<fp>/<sk>, but <sk> is on the list for
1246 * a different authority identity digest.
1248 getinfo_helper_downloads(&dummy
,
1249 "downloads/cert/fp/63CDD326DFEF0CA020BDD3FEB45A3286FE13A061/"
1250 "9451B8F1B10952384EB58B5F230C0BB701626C9B",
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");
1267 test_download_status_desc(void *arg
)
1269 /* We just need one of these to pass, it doesn't matter what's in it */
1270 control_connection_t dummy
;
1271 /* Get results out */
1272 char *question
= NULL
;
1273 char *answer
= NULL
;
1274 const char *errmsg
= NULL
;
1281 * Check returning serialized dlstatuses and digest lists, and implicitly
1282 * also test download_status_to_string() and digest_list_to_string().
1285 /* Case 1 - list of router descriptor digests */
1286 getinfo_helper_downloads(&dummy
,
1287 "downloads/desc/descs",
1289 tt_ptr_op(answer
, OP_NE
, NULL
);
1290 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1291 tt_str_op(answer
, OP_EQ
, descbr_expected_list
);
1295 /* Case 2 - get download status for router descriptor 1 */
1296 memcpy(&descbr_digest_1_dl
, &dls_sample_1
,
1297 sizeof(download_status_t
));
1298 tor_asprintf(&question
, "downloads/desc/%s", descbr_digest_1_str
);
1299 tt_ptr_op(question
, OP_NE
, NULL
);
1300 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1301 tt_ptr_op(answer
, OP_NE
, NULL
);
1302 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1303 tt_str_op(answer
, OP_EQ
, dls_sample_1_str
);
1308 /* Case 3 - get download status for router descriptor 1 */
1309 memcpy(&descbr_digest_2_dl
, &dls_sample_2
,
1310 sizeof(download_status_t
));
1311 tor_asprintf(&question
, "downloads/desc/%s", descbr_digest_2_str
);
1312 tt_ptr_op(question
, OP_NE
, NULL
);
1313 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1314 tt_ptr_op(answer
, OP_NE
, NULL
);
1315 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1316 tt_str_op(answer
, OP_EQ
, dls_sample_2_str
);
1321 /* Now check the error cases */
1323 /* Case 1 - non-digest-length garbage after downloads/desc */
1324 getinfo_helper_downloads(&dummy
, "downloads/desc/blahdeblah",
1326 tt_ptr_op(answer
, OP_EQ
, NULL
);
1327 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1328 tt_str_op(errmsg
, OP_EQ
, "Unknown router descriptor download status query");
1331 /* Case 2 - nonparseable digest-shaped thing */
1332 getinfo_helper_downloads(
1334 "downloads/desc/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
1336 tt_ptr_op(answer
, OP_EQ
, NULL
);
1337 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1338 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a digest");
1341 /* Case 3 - digest we have no descriptor for */
1342 getinfo_helper_downloads(
1344 "downloads/desc/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
1346 tt_ptr_op(answer
, OP_EQ
, NULL
);
1347 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1348 tt_str_op(errmsg
, OP_EQ
, "No such descriptor digest found");
1351 /* Case 4 - microdescs only */
1353 getinfo_helper_downloads(&dummy
,
1354 "downloads/desc/descs",
1356 tt_ptr_op(answer
, OP_EQ
, NULL
);
1357 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1358 tt_str_op(errmsg
, OP_EQ
,
1359 "We don't seem to have a networkstatus-flavored consensus");
1371 test_download_status_bridge(void *arg
)
1373 /* We just need one of these to pass, it doesn't matter what's in it */
1374 control_connection_t dummy
;
1375 /* Get results out */
1376 char *question
= NULL
;
1377 char *answer
= NULL
;
1378 const char *errmsg
= NULL
;
1382 setup_bridge_mocks();
1385 * Check returning serialized dlstatuses and digest lists, and implicitly
1386 * also test download_status_to_string() and digest_list_to_string().
1389 /* Case 1 - list of bridge identity digests */
1390 getinfo_helper_downloads(&dummy
,
1391 "downloads/bridge/bridges",
1393 tt_ptr_op(answer
, OP_NE
, NULL
);
1394 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1395 tt_str_op(answer
, OP_EQ
, descbr_expected_list
);
1399 /* Case 2 - get download status for bridge descriptor 1 */
1400 memcpy(&descbr_digest_1_dl
, &dls_sample_3
,
1401 sizeof(download_status_t
));
1402 tor_asprintf(&question
, "downloads/bridge/%s", descbr_digest_1_str
);
1403 tt_ptr_op(question
, OP_NE
, NULL
);
1404 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1405 tt_ptr_op(answer
, OP_NE
, NULL
);
1406 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1407 tt_str_op(answer
, OP_EQ
, dls_sample_3_str
);
1412 /* Case 3 - get download status for router descriptor 1 */
1413 memcpy(&descbr_digest_2_dl
, &dls_sample_4
,
1414 sizeof(download_status_t
));
1415 tor_asprintf(&question
, "downloads/bridge/%s", descbr_digest_2_str
);
1416 tt_ptr_op(question
, OP_NE
, NULL
);
1417 getinfo_helper_downloads(&dummy
, question
, &answer
, &errmsg
);
1418 tt_ptr_op(answer
, OP_NE
, NULL
);
1419 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1420 tt_str_op(answer
, OP_EQ
, dls_sample_4_str
);
1425 /* Now check the error cases */
1427 /* Case 1 - non-digest-length garbage after downloads/bridge */
1428 getinfo_helper_downloads(&dummy
, "downloads/bridge/blahdeblah",
1430 tt_ptr_op(answer
, OP_EQ
, NULL
);
1431 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1432 tt_str_op(errmsg
, OP_EQ
, "Unknown bridge descriptor download status query");
1435 /* Case 2 - nonparseable digest-shaped thing */
1436 getinfo_helper_downloads(
1438 "downloads/bridge/774EC52FD9A5B80A6FACZE536616E8022E3470AG",
1440 tt_ptr_op(answer
, OP_EQ
, NULL
);
1441 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1442 tt_str_op(errmsg
, OP_EQ
, "That didn't look like a digest");
1445 /* Case 3 - digest we have no descriptor for */
1446 getinfo_helper_downloads(
1448 "downloads/bridge/B05B46135B0B2C04EBE1DD6A6AE4B12D7CD2226A",
1450 tt_ptr_op(answer
, OP_EQ
, NULL
);
1451 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1452 tt_str_op(errmsg
, OP_EQ
, "No such bridge identity digest found");
1455 /* Case 4 - bridges disabled */
1457 getinfo_helper_downloads(&dummy
,
1458 "downloads/bridge/bridges",
1460 tt_ptr_op(answer
, OP_EQ
, NULL
);
1461 tt_ptr_op(errmsg
, OP_NE
, NULL
);
1462 tt_str_op(errmsg
, OP_EQ
, "We don't seem to be using bridges");
1467 clear_bridge_mocks();
1473 /** Set timeval to a mock date and time. This is neccessary
1474 * to make tor_gettimeofday() mockable. */
1476 mock_tor_gettimeofday(struct timeval
*timeval
)
1478 timeval
->tv_sec
= 1523405073;
1479 timeval
->tv_usec
= 271645;
1483 test_current_time(void *arg
)
1485 /* We just need one of these to pass, it doesn't matter what's in it */
1486 control_connection_t dummy
;
1487 /* Get results out */
1488 char *answer
= NULL
;
1489 const char *errmsg
= NULL
;
1493 /* We need these for storing the (mock) time. */
1494 MOCK(tor_gettimeofday
, mock_tor_gettimeofday
);
1496 tor_gettimeofday(&now
);
1497 char timebuf
[ISO_TIME_LEN
+1];
1499 /* Case 1 - local time */
1500 format_local_iso_time_nospace(timebuf
, (time_t)now
.tv_sec
);
1501 getinfo_helper_current_time(&dummy
,
1502 "current-time/local",
1504 tt_ptr_op(answer
, OP_NE
, NULL
);
1505 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1506 tt_str_op(answer
, OP_EQ
, timebuf
);
1510 /* Case 2 - UTC time */
1511 format_iso_time_nospace(timebuf
, (time_t)now
.tv_sec
);
1512 getinfo_helper_current_time(&dummy
,
1515 tt_ptr_op(answer
, OP_NE
, NULL
);
1516 tt_ptr_op(errmsg
, OP_EQ
, NULL
);
1517 tt_str_op(answer
, OP_EQ
, timebuf
);
1522 UNMOCK(tor_gettimeofday
);
1528 struct testcase_t controller_tests
[] = {
1529 { "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2
, 0,
1531 { "add_onion_helper_keyarg_v3", test_add_onion_helper_keyarg_v3
, 0,
1533 { "getinfo_helper_onion", test_getinfo_helper_onion
, 0, NULL
, NULL
},
1534 { "rend_service_parse_port_config", test_rend_service_parse_port_config
, 0,
1536 { "add_onion_helper_clientauth", test_add_onion_helper_clientauth
, 0, NULL
,
1538 { "download_status_consensus", test_download_status_consensus
, 0, NULL
,
1540 { "download_status_cert", test_download_status_cert
, 0, NULL
,
1542 { "download_status_desc", test_download_status_desc
, 0, NULL
, NULL
},
1543 { "download_status_bridge", test_download_status_bridge
, 0, NULL
, NULL
},
1544 { "current_time", test_current_time
, 0, NULL
, NULL
},