1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
10 #define CONTROL_PRIVATE
11 #define DIRSERV_PRIVATE
12 #define DIRVOTE_PRIVATE
13 #define ROUTER_PRIVATE
14 #define ROUTERLIST_PRIVATE
15 #define ROUTERPARSE_PRIVATE
16 #define HIBERNATE_PRIVATE
17 #define NETWORKSTATUS_PRIVATE
22 #include "confparse.h"
25 #include "crypto_ed25519.h"
26 #include "directory.h"
29 #include "entrynodes.h"
30 #include "hibernate.h"
32 #include "networkstatus.h"
34 #include "routerkeys.h"
35 #include "routerlist.h"
36 #include "routerparse.h"
37 #include "routerset.h"
38 #include "shared_random_state.h"
40 #include "test_dir_common.h"
43 #include "log_test_helpers.h"
48 test_dir_nicknames(void *arg
)
51 tt_assert( is_legal_nickname("a"));
52 tt_assert(!is_legal_nickname(""));
53 tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
54 tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
55 tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
56 tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
58 tt_assert( is_legal_nickname_or_hexdigest(
59 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
60 tt_assert( is_legal_nickname_or_hexdigest(
61 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
62 tt_assert( is_legal_nickname_or_hexdigest(
63 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
65 tt_assert(!is_legal_nickname_or_hexdigest(
66 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
68 tt_assert(!is_legal_nickname_or_hexdigest(
69 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
70 /* hex part too long */
71 tt_assert(!is_legal_nickname_or_hexdigest(
72 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
73 tt_assert(!is_legal_nickname_or_hexdigest(
74 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
76 tt_assert(!is_legal_nickname_or_hexdigest(
77 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
78 tt_assert(!is_legal_nickname_or_hexdigest(
79 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
80 tt_assert(!is_legal_nickname_or_hexdigest(
81 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
82 tt_assert(!is_legal_nickname_or_hexdigest(
83 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
84 "abcdefghijklmnoppqrst"));
86 tt_assert(!is_legal_nickname_or_hexdigest(
87 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
88 tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
89 tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
90 tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
95 static smartlist_t
*mocked_configured_ports
= NULL
;
97 /** Returns mocked_configured_ports */
98 static const smartlist_t
*
99 mock_get_configured_ports(void)
101 return mocked_configured_ports
;
104 /** Run unit tests for router descriptor generation logic. */
106 test_dir_formats(void *arg
)
111 char fingerprint
[FINGERPRINT_LEN
+1];
112 char *pk1_str
= NULL
, *pk2_str
= NULL
, *cp
;
113 size_t pk1_str_len
, pk2_str_len
;
114 routerinfo_t
*r1
=NULL
, *r2
=NULL
;
115 crypto_pk_t
*pk1
= NULL
, *pk2
= NULL
;
116 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
117 addr_policy_t
*ex1
, *ex2
;
118 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
119 uint8_t *rsa_cc
= NULL
;
120 or_options_t
*options
= get_options_mutable();
121 const addr_policy_t
*p
;
122 time_t now
= time(NULL
);
123 port_cfg_t orport
, dirport
;
127 pk1
= pk_generate(0);
128 pk2
= pk_generate(1);
130 tt_assert(pk1
&& pk2
);
132 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE
);
134 get_platform_str(platform
, sizeof(platform
));
135 r1
= tor_malloc_zero(sizeof(routerinfo_t
));
136 r1
->addr
= 0xc0a80001u
; /* 192.168.0.1 */
137 r1
->cache_info
.published_on
= 0;
140 r1
->supports_tunnelled_dir_requests
= 1;
141 tor_addr_parse(&r1
->ipv6_addr
, "1:2:3:4::");
142 r1
->ipv6_orport
= 9999;
143 r1
->onion_pkey
= crypto_pk_dup_key(pk1
);
144 /* Fake just enough of an ntor key to get by */
145 curve25519_keypair_t r1_onion_keypair
;
146 curve25519_keypair_generate(&r1_onion_keypair
, 0);
147 r1
->onion_curve25519_pkey
= tor_memdup(&r1_onion_keypair
.pubkey
,
148 sizeof(curve25519_public_key_t
));
149 r1
->identity_pkey
= crypto_pk_dup_key(pk2
);
150 r1
->bandwidthrate
= 1000;
151 r1
->bandwidthburst
= 5000;
152 r1
->bandwidthcapacity
= 10000;
153 r1
->exit_policy
= NULL
;
154 r1
->nickname
= tor_strdup("Magri");
155 r1
->platform
= tor_strdup(platform
);
157 ex1
= tor_malloc_zero(sizeof(addr_policy_t
));
158 ex2
= tor_malloc_zero(sizeof(addr_policy_t
));
159 ex1
->policy_type
= ADDR_POLICY_ACCEPT
;
160 tor_addr_from_ipv4h(&ex1
->addr
, 0);
162 ex1
->prt_min
= ex1
->prt_max
= 80;
163 ex2
->policy_type
= ADDR_POLICY_REJECT
;
164 tor_addr_from_ipv4h(&ex2
->addr
, 18<<24);
166 ex2
->prt_min
= ex2
->prt_max
= 24;
167 r2
= tor_malloc_zero(sizeof(routerinfo_t
));
168 r2
->addr
= 0x0a030201u
; /* 10.3.2.1 */
169 ed25519_keypair_t kp1
, kp2
;
170 ed25519_secret_key_from_seed(&kp1
.seckey
,
171 (const uint8_t*)"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
172 ed25519_public_key_generate(&kp1
.pubkey
, &kp1
.seckey
);
173 ed25519_secret_key_from_seed(&kp2
.seckey
,
174 (const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
175 ed25519_public_key_generate(&kp2
.pubkey
, &kp2
.seckey
);
176 r2
->cache_info
.signing_key_cert
= tor_cert_create(&kp1
,
177 CERT_TYPE_ID_SIGNING
,
180 CERT_FLAG_INCLUDE_SIGNING_KEY
);
181 r2
->platform
= tor_strdup(platform
);
182 r2
->cache_info
.published_on
= 5;
185 r2
->supports_tunnelled_dir_requests
= 1;
186 r2
->onion_pkey
= crypto_pk_dup_key(pk2
);
187 curve25519_keypair_t r2_onion_keypair
;
188 curve25519_keypair_generate(&r2_onion_keypair
, 0);
189 r2
->onion_curve25519_pkey
= tor_memdup(&r2_onion_keypair
.pubkey
,
190 sizeof(curve25519_public_key_t
));
191 r2
->identity_pkey
= crypto_pk_dup_key(pk1
);
192 r2
->bandwidthrate
= r2
->bandwidthburst
= r2
->bandwidthcapacity
= 3000;
193 r2
->exit_policy
= smartlist_new();
194 smartlist_add(r2
->exit_policy
, ex1
);
195 smartlist_add(r2
->exit_policy
, ex2
);
196 r2
->nickname
= tor_strdup("Fred");
198 tt_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
200 tt_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
203 /* XXXX+++ router_dump_to_string should really take this from ri.*/
204 options
->ContactInfo
= tor_strdup("Magri White "
205 "<magri@elsewhere.example.com>");
206 /* Skip reachability checks for DirPort and tunnelled-dir-server */
207 options
->AssumeReachable
= 1;
209 /* Fake just enough of an ORPort and DirPort to get by */
210 MOCK(get_configured_ports
, mock_get_configured_ports
);
211 mocked_configured_ports
= smartlist_new();
213 memset(&orport
, 0, sizeof(orport
));
214 orport
.type
= CONN_TYPE_OR_LISTENER
;
215 orport
.addr
.family
= AF_INET
;
217 smartlist_add(mocked_configured_ports
, &orport
);
219 memset(&dirport
, 0, sizeof(dirport
));
220 dirport
.type
= CONN_TYPE_DIR_LISTENER
;
221 dirport
.addr
.family
= AF_INET
;
223 smartlist_add(mocked_configured_ports
, &dirport
);
225 buf
= router_dump_router_to_string(r1
, pk2
, NULL
, NULL
, NULL
);
227 UNMOCK(get_configured_ports
);
228 smartlist_free(mocked_configured_ports
);
229 mocked_configured_ports
= NULL
;
231 tor_free(options
->ContactInfo
);
234 strlcpy(buf2
, "router Magri 192.168.0.1 9000 0 9003\n"
235 "or-address [1:2:3:4::]:9999\n"
236 "platform Tor "VERSION
" on ", sizeof(buf2
));
237 strlcat(buf2
, get_uname(), sizeof(buf2
));
239 "published 1970-01-01 00:00:00\n"
240 "fingerprint ", sizeof(buf2
));
241 tt_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
242 strlcat(buf2
, fingerprint
, sizeof(buf2
));
243 strlcat(buf2
, "\nuptime 0\n"
244 /* XXX the "0" above is hard-coded, but even if we made it reflect
245 * uptime, that still wouldn't make it right, because the two
246 * descriptors might be made on different seconds... hm. */
247 "bandwidth 1000 5000 10000\n"
248 "onion-key\n", sizeof(buf2
));
249 strlcat(buf2
, pk1_str
, sizeof(buf2
));
250 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
251 strlcat(buf2
, pk2_str
, sizeof(buf2
));
252 strlcat(buf2
, "hidden-service-dir\n", sizeof(buf2
));
253 strlcat(buf2
, "contact Magri White <magri@elsewhere.example.com>\n",
255 strlcat(buf2
, "ntor-onion-key ", sizeof(buf2
));
256 base64_encode(cert_buf
, sizeof(cert_buf
),
257 (const char*)r1_onion_keypair
.pubkey
.public_key
, 32,
258 BASE64_ENCODE_MULTILINE
);
259 strlcat(buf2
, cert_buf
, sizeof(buf2
));
260 strlcat(buf2
, "reject *:*\n", sizeof(buf2
));
261 strlcat(buf2
, "tunnelled-dir-server\nrouter-signature\n", sizeof(buf2
));
262 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
265 tt_str_op(buf
,OP_EQ
, buf2
);
268 buf
= router_dump_router_to_string(r1
, pk2
, NULL
, NULL
, NULL
);
271 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
,NULL
);
273 tt_int_op(rp1
->addr
,OP_EQ
, r1
->addr
);
274 tt_int_op(rp1
->or_port
,OP_EQ
, r1
->or_port
);
275 tt_int_op(rp1
->dir_port
,OP_EQ
, r1
->dir_port
);
276 tt_int_op(rp1
->bandwidthrate
,OP_EQ
, r1
->bandwidthrate
);
277 tt_int_op(rp1
->bandwidthburst
,OP_EQ
, r1
->bandwidthburst
);
278 tt_int_op(rp1
->bandwidthcapacity
,OP_EQ
, r1
->bandwidthcapacity
);
279 tt_int_op(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
), OP_EQ
, 0);
280 tt_int_op(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
), OP_EQ
, 0);
281 tt_assert(rp1
->supports_tunnelled_dir_requests
);
282 //tt_assert(rp1->exit_policy == NULL);
286 "router Fred 10.3.2.1 9005 0 0\n"
288 "-----BEGIN ED25519 CERT-----\n", sizeof(buf2
));
289 base64_encode(cert_buf
, sizeof(cert_buf
),
290 (const char*)r2
->cache_info
.signing_key_cert
->encoded
,
291 r2
->cache_info
.signing_key_cert
->encoded_len
,
292 BASE64_ENCODE_MULTILINE
);
293 strlcat(buf2
, cert_buf
, sizeof(buf2
));
294 strlcat(buf2
, "-----END ED25519 CERT-----\n", sizeof(buf2
));
295 strlcat(buf2
, "master-key-ed25519 ", sizeof(buf2
));
297 char k
[ED25519_BASE64_LEN
+1];
298 tt_int_op(ed25519_public_to_base64(k
,
299 &r2
->cache_info
.signing_key_cert
->signing_key
),
301 strlcat(buf2
, k
, sizeof(buf2
));
302 strlcat(buf2
, "\n", sizeof(buf2
));
304 strlcat(buf2
, "platform Tor "VERSION
" on ", sizeof(buf2
));
305 strlcat(buf2
, get_uname(), sizeof(buf2
));
307 "published 1970-01-01 00:00:05\n"
308 "fingerprint ", sizeof(buf2
));
309 tt_assert(!crypto_pk_get_fingerprint(pk1
, fingerprint
, 1));
310 strlcat(buf2
, fingerprint
, sizeof(buf2
));
311 strlcat(buf2
, "\nuptime 0\n"
312 "bandwidth 3000 3000 3000\n", sizeof(buf2
));
313 strlcat(buf2
, "onion-key\n", sizeof(buf2
));
314 strlcat(buf2
, pk2_str
, sizeof(buf2
));
315 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
316 strlcat(buf2
, pk1_str
, sizeof(buf2
));
318 rsa_cc
= make_tap_onion_key_crosscert(pk2
,
323 base64_encode(cert_buf
, sizeof(cert_buf
), (char*)rsa_cc
, rsa_cc_len
,
324 BASE64_ENCODE_MULTILINE
);
325 strlcat(buf2
, "onion-key-crosscert\n"
326 "-----BEGIN CROSSCERT-----\n", sizeof(buf2
));
327 strlcat(buf2
, cert_buf
, sizeof(buf2
));
328 strlcat(buf2
, "-----END CROSSCERT-----\n", sizeof(buf2
));
331 tor_cert_t
*ntor_cc
= NULL
;
332 ntor_cc
= make_ntor_onion_key_crosscert(&r2_onion_keypair
,
334 r2
->cache_info
.published_on
,
335 get_onion_key_lifetime(),
338 base64_encode(cert_buf
, sizeof(cert_buf
),
339 (char*)ntor_cc
->encoded
, ntor_cc
->encoded_len
,
340 BASE64_ENCODE_MULTILINE
);
341 tor_cert_free(ntor_cc
);
343 tor_snprintf(buf2
+strlen(buf2
), sizeof(buf2
)-strlen(buf2
),
344 "ntor-onion-key-crosscert %d\n"
345 "-----BEGIN ED25519 CERT-----\n"
347 "-----END ED25519 CERT-----\n", ntor_cc_sign
, cert_buf
);
349 strlcat(buf2
, "hidden-service-dir\n", sizeof(buf2
));
350 strlcat(buf2
, "ntor-onion-key ", sizeof(buf2
));
351 base64_encode(cert_buf
, sizeof(cert_buf
),
352 (const char*)r2_onion_keypair
.pubkey
.public_key
, 32,
353 BASE64_ENCODE_MULTILINE
);
354 strlcat(buf2
, cert_buf
, sizeof(buf2
));
355 strlcat(buf2
, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2
));
356 strlcat(buf2
, "tunnelled-dir-server\n", sizeof(buf2
));
357 strlcat(buf2
, "router-sig-ed25519 ", sizeof(buf2
));
359 /* Fake just enough of an ORPort to get by */
360 MOCK(get_configured_ports
, mock_get_configured_ports
);
361 mocked_configured_ports
= smartlist_new();
363 memset(&orport
, 0, sizeof(orport
));
364 orport
.type
= CONN_TYPE_OR_LISTENER
;
365 orport
.addr
.family
= AF_INET
;
367 smartlist_add(mocked_configured_ports
, &orport
);
369 buf
= router_dump_router_to_string(r2
, pk1
, pk2
, &r2_onion_keypair
, &kp2
);
371 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
374 tt_str_op(buf
, OP_EQ
, buf2
);
377 buf
= router_dump_router_to_string(r2
, pk1
, NULL
, NULL
, NULL
);
379 UNMOCK(get_configured_ports
);
380 smartlist_free(mocked_configured_ports
);
381 mocked_configured_ports
= NULL
;
383 /* Reset for later */
385 rp2
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
,NULL
);
387 tt_int_op(rp2
->addr
,OP_EQ
, r2
->addr
);
388 tt_int_op(rp2
->or_port
,OP_EQ
, r2
->or_port
);
389 tt_int_op(rp2
->dir_port
,OP_EQ
, r2
->dir_port
);
390 tt_int_op(rp2
->bandwidthrate
,OP_EQ
, r2
->bandwidthrate
);
391 tt_int_op(rp2
->bandwidthburst
,OP_EQ
, r2
->bandwidthburst
);
392 tt_int_op(rp2
->bandwidthcapacity
,OP_EQ
, r2
->bandwidthcapacity
);
393 tt_mem_op(rp2
->onion_curve25519_pkey
->public_key
,OP_EQ
,
394 r2
->onion_curve25519_pkey
->public_key
,
395 CURVE25519_PUBKEY_LEN
);
396 tt_int_op(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
), OP_EQ
, 0);
397 tt_int_op(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
), OP_EQ
, 0);
398 tt_assert(rp2
->supports_tunnelled_dir_requests
);
400 tt_int_op(smartlist_len(rp2
->exit_policy
),OP_EQ
, 2);
402 p
= smartlist_get(rp2
->exit_policy
, 0);
403 tt_int_op(p
->policy_type
,OP_EQ
, ADDR_POLICY_ACCEPT
);
404 tt_assert(tor_addr_is_null(&p
->addr
));
405 tt_int_op(p
->maskbits
,OP_EQ
, 0);
406 tt_int_op(p
->prt_min
,OP_EQ
, 80);
407 tt_int_op(p
->prt_max
,OP_EQ
, 80);
409 p
= smartlist_get(rp2
->exit_policy
, 1);
410 tt_int_op(p
->policy_type
,OP_EQ
, ADDR_POLICY_REJECT
);
411 tt_assert(tor_addr_eq(&p
->addr
, &ex2
->addr
));
412 tt_int_op(p
->maskbits
,OP_EQ
, 8);
413 tt_int_op(p
->prt_min
,OP_EQ
, 24);
414 tt_int_op(p
->prt_max
,OP_EQ
, 24);
417 /* Okay, now for the directories. */
419 fingerprint_list
= smartlist_new();
420 crypto_pk_get_fingerprint(pk2
, buf
, 1);
421 add_fingerprint_to_dir(buf
, fingerprint_list
, 0);
422 crypto_pk_get_fingerprint(pk1
, buf
, 1);
423 add_fingerprint_to_dir(buf
, fingerprint_list
, 0);
427 dirserv_free_fingerprint_list();
435 routerinfo_free(rp2
);
441 if (pk1
) crypto_pk_free(pk1
);
442 if (pk2
) crypto_pk_free(pk2
);
443 if (rp1
) routerinfo_free(rp1
);
444 tor_free(dir1
); /* XXXX And more !*/
445 tor_free(dir2
); /* And more !*/
448 #include "failing_routerdescs.inc"
451 test_dir_routerinfo_parsing(void *arg
)
456 routerinfo_t
*ri
= NULL
;
458 #define CHECK_OK(s) \
460 routerinfo_free(ri); \
461 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
464 #define CHECK_FAIL(s, againval) \
466 routerinfo_free(ri); \
468 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
469 tt_assert(ri == NULL); \
470 tt_int_op(again, OP_EQ, (againval)); \
473 CHECK_OK(EX_RI_MINIMAL
);
474 CHECK_OK(EX_RI_MAXIMAL
);
476 CHECK_OK(EX_RI_MINIMAL_ED
);
478 /* good annotations prepended */
480 ri
= router_parse_entry_from_string(EX_RI_MINIMAL
, NULL
, 0, 0,
481 "@purpose bridge\n", NULL
);
482 tt_ptr_op(ri
, OP_NE
, NULL
);
483 tt_assert(ri
->purpose
== ROUTER_PURPOSE_BRIDGE
);
486 /* bad annotations prepended. */
487 ri
= router_parse_entry_from_string(EX_RI_MINIMAL
,
488 NULL
, 0, 0, "@purpose\n", NULL
);
489 tt_ptr_op(ri
, OP_EQ
, NULL
);
491 /* bad annotations on router. */
492 ri
= router_parse_entry_from_string("@purpose\nrouter x\n", NULL
, 0, 1,
494 tt_ptr_op(ri
, OP_EQ
, NULL
);
496 /* unwanted annotations on router. */
497 ri
= router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL
, 0, 0,
499 tt_ptr_op(ri
, OP_EQ
, NULL
);
502 ri
= router_parse_entry_from_string("router x\n", NULL
, 0, 0,
504 tt_ptr_op(ri
, OP_EQ
, NULL
);
508 ri
= router_parse_entry_from_string("hello\n", NULL
, 0, 0, NULL
, NULL
);
509 tt_ptr_op(ri
, OP_EQ
, NULL
);
511 CHECK_FAIL(EX_RI_BAD_SIG1
, 1);
512 CHECK_FAIL(EX_RI_BAD_SIG2
, 1);
513 CHECK_FAIL(EX_RI_BAD_TOKENS
, 0);
514 CHECK_FAIL(EX_RI_BAD_PUBLISHED
, 0);
515 CHECK_FAIL(EX_RI_NEG_BANDWIDTH
, 0);
516 CHECK_FAIL(EX_RI_BAD_BANDWIDTH
, 0);
517 CHECK_FAIL(EX_RI_BAD_BANDWIDTH2
, 0);
518 CHECK_FAIL(EX_RI_BAD_ONIONKEY1
, 0);
519 CHECK_FAIL(EX_RI_BAD_ONIONKEY2
, 0);
520 CHECK_FAIL(EX_RI_BAD_PORTS
, 0);
521 CHECK_FAIL(EX_RI_BAD_IP
, 0);
522 CHECK_FAIL(EX_RI_BAD_DIRPORT
, 0);
523 CHECK_FAIL(EX_RI_BAD_NAME2
, 0);
524 CHECK_FAIL(EX_RI_BAD_UPTIME
, 0);
526 CHECK_FAIL(EX_RI_BAD_BANDWIDTH3
, 0);
527 CHECK_FAIL(EX_RI_BAD_NTOR_KEY
, 0);
528 CHECK_FAIL(EX_RI_BAD_FINGERPRINT
, 0);
529 CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT
, 0);
530 CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6
, 0);
531 CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY
, 0);
532 CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY
, 0);
533 CHECK_FAIL(EX_RI_BAD_FAMILY
, 0);
534 CHECK_FAIL(EX_RI_ZERO_ORPORT
, 0);
536 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT
, 0);
537 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2
, 0);
538 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN
, 0);
539 CHECK_FAIL(EX_RI_ED_BAD_SIG1
, 0);
540 CHECK_FAIL(EX_RI_ED_BAD_SIG2
, 0);
541 CHECK_FAIL(EX_RI_ED_BAD_SIG3
, 0);
542 CHECK_FAIL(EX_RI_ED_BAD_SIG4
, 0);
543 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1
, 0);
544 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3
, 0);
545 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4
, 0);
546 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5
, 0);
547 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6
, 0);
548 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7
, 0);
549 CHECK_FAIL(EX_RI_ED_MISPLACED1
, 0);
550 CHECK_FAIL(EX_RI_ED_MISPLACED2
, 0);
551 CHECK_FAIL(EX_RI_ED_BAD_CERT1
, 0);
552 CHECK_FAIL(EX_RI_ED_BAD_CERT2
, 0);
553 CHECK_FAIL(EX_RI_ED_BAD_CERT3
, 0);
555 /* This is allowed; we just ignore it. */
556 CHECK_OK(EX_RI_BAD_EI_DIGEST
);
557 CHECK_OK(EX_RI_BAD_EI_DIGEST2
);
565 #include "example_extrainfo.inc"
568 routerinfo_free_wrapper_(void *arg
)
570 routerinfo_free(arg
);
574 test_dir_extrainfo_parsing(void *arg
)
578 #define CHECK_OK(s) \
580 extrainfo_free(ei); \
581 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
584 #define CHECK_FAIL(s, againval) \
586 extrainfo_free(ei); \
588 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
589 tt_assert(ei == NULL); \
590 tt_int_op(again, OP_EQ, (againval)); \
594 ri = tor_malloc_zero(sizeof(routerinfo_t)); \
595 crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
596 tt_assert(! crypto_pk_read_public_key_from_string(pk, \
597 name##_KEY, strlen(name##_KEY))); \
598 tt_int_op(20,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
599 digestmap_set((digestmap_t*)map, d, ri); \
603 routerinfo_t
*ri
= NULL
;
605 struct digest_ri_map_t
*map
= NULL
;
606 extrainfo_t
*ei
= NULL
;
609 CHECK_OK(EX_EI_MINIMAL
);
610 tt_assert(ei
->pending_sig
);
611 CHECK_OK(EX_EI_MAXIMAL
);
612 tt_assert(ei
->pending_sig
);
613 CHECK_OK(EX_EI_GOOD_ED_EI
);
614 tt_assert(ei
->pending_sig
);
616 map
= (struct digest_ri_map_t
*)digestmap_new();
619 ADD(EX_EI_GOOD_ED_EI
);
621 ADD(EX_EI_BAD_NICKNAME
);
622 ADD(EX_EI_BAD_TOKENS
);
623 ADD(EX_EI_BAD_START
);
624 ADD(EX_EI_BAD_PUBLISHED
);
626 ADD(EX_EI_ED_MISSING_SIG
);
627 ADD(EX_EI_ED_MISSING_CERT
);
628 ADD(EX_EI_ED_BAD_CERT1
);
629 ADD(EX_EI_ED_BAD_CERT2
);
630 ADD(EX_EI_ED_BAD_SIG1
);
631 ADD(EX_EI_ED_BAD_SIG2
);
632 ADD(EX_EI_ED_MISPLACED_CERT
);
633 ADD(EX_EI_ED_MISPLACED_SIG
);
635 CHECK_OK(EX_EI_MINIMAL
);
636 tt_ptr_op(ei
->pending_sig
, OP_EQ
, NULL
);
637 CHECK_OK(EX_EI_MAXIMAL
);
638 tt_ptr_op(ei
->pending_sig
, OP_EQ
, NULL
);
639 CHECK_OK(EX_EI_GOOD_ED_EI
);
640 tt_ptr_op(ei
->pending_sig
, OP_EQ
, NULL
);
642 CHECK_FAIL(EX_EI_BAD_SIG1
,1);
643 CHECK_FAIL(EX_EI_BAD_SIG2
,1);
644 CHECK_FAIL(EX_EI_BAD_SIG3
,1);
645 CHECK_FAIL(EX_EI_BAD_FP
,0);
646 CHECK_FAIL(EX_EI_BAD_NICKNAME
,0);
647 CHECK_FAIL(EX_EI_BAD_TOKENS
,0);
648 CHECK_FAIL(EX_EI_BAD_START
,0);
649 CHECK_FAIL(EX_EI_BAD_PUBLISHED
,0);
651 CHECK_FAIL(EX_EI_ED_MISSING_SIG
,0);
652 CHECK_FAIL(EX_EI_ED_MISSING_CERT
,0);
653 CHECK_FAIL(EX_EI_ED_BAD_CERT1
,0);
654 CHECK_FAIL(EX_EI_ED_BAD_CERT2
,0);
655 CHECK_FAIL(EX_EI_ED_BAD_SIG1
,0);
656 CHECK_FAIL(EX_EI_ED_BAD_SIG2
,0);
657 CHECK_FAIL(EX_EI_ED_MISPLACED_CERT
,0);
658 CHECK_FAIL(EX_EI_ED_MISPLACED_SIG
,0);
667 digestmap_free((digestmap_t
*)map
, routerinfo_free_wrapper_
);
671 test_dir_parse_router_list(void *arg
)
674 smartlist_t
*invalid
= smartlist_new();
675 smartlist_t
*dest
= smartlist_new();
676 smartlist_t
*chunks
= smartlist_new();
680 digestmap_t
*map
= NULL
;
681 char *mem_op_hex_tmp
= NULL
;
682 routerinfo_t
*ri
= NULL
;
685 smartlist_add_strdup(chunks
, EX_RI_MINIMAL
); // ri 0
686 smartlist_add_strdup(chunks
, EX_RI_BAD_PORTS
); // bad ri 0
687 smartlist_add_strdup(chunks
, EX_EI_MAXIMAL
); // ei 0
688 smartlist_add_strdup(chunks
, EX_EI_BAD_SIG2
); // bad ei --
689 smartlist_add_strdup(chunks
, EX_EI_BAD_NICKNAME
);// bad ei 0
690 smartlist_add_strdup(chunks
, EX_RI_BAD_SIG1
); // bad ri --
691 smartlist_add_strdup(chunks
, EX_EI_BAD_PUBLISHED
); // bad ei 1
692 smartlist_add_strdup(chunks
, EX_RI_MAXIMAL
); // ri 1
693 smartlist_add_strdup(chunks
, EX_RI_BAD_FAMILY
); // bad ri 1
694 smartlist_add_strdup(chunks
, EX_EI_MINIMAL
); // ei 1
696 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
698 /* First, parse the routers. */
701 router_parse_list_from_string(&cp
, NULL
, dest
, SAVED_NOWHERE
,
702 0, 0, NULL
, invalid
));
703 tt_int_op(2, OP_EQ
, smartlist_len(dest
));
704 tt_ptr_op(cp
, OP_EQ
, list
+ strlen(list
));
706 routerinfo_t
*r
= smartlist_get(dest
, 0);
707 tt_mem_op(r
->cache_info
.signed_descriptor_body
, OP_EQ
,
708 EX_RI_MINIMAL
, strlen(EX_RI_MINIMAL
));
709 r
= smartlist_get(dest
, 1);
710 tt_mem_op(r
->cache_info
.signed_descriptor_body
, OP_EQ
,
711 EX_RI_MAXIMAL
, strlen(EX_RI_MAXIMAL
));
713 tt_int_op(2, OP_EQ
, smartlist_len(invalid
));
714 test_memeq_hex(smartlist_get(invalid
, 0),
715 "ab9eeaa95e7d45740185b4e519c76ead756277a9");
716 test_memeq_hex(smartlist_get(invalid
, 1),
717 "9a651ee03b64325959e8f1b46f2b689b30750b4c");
720 SMARTLIST_FOREACH(dest
, routerinfo_t
*, rinfo
, routerinfo_free(rinfo
));
721 SMARTLIST_FOREACH(invalid
, uint8_t *, dig
, tor_free(dig
));
722 smartlist_clear(dest
);
723 smartlist_clear(invalid
);
725 /* And check extrainfos. */
727 map
= (digestmap_t
*)router_get_routerlist()->identity_map
;
730 ADD(EX_EI_BAD_NICKNAME
);
731 ADD(EX_EI_BAD_PUBLISHED
);
734 router_parse_list_from_string(&cp
, NULL
, dest
, SAVED_NOWHERE
,
735 1, 0, NULL
, invalid
));
736 tt_int_op(2, OP_EQ
, smartlist_len(dest
));
737 extrainfo_t
*e
= smartlist_get(dest
, 0);
738 tt_mem_op(e
->cache_info
.signed_descriptor_body
, OP_EQ
,
739 EX_EI_MAXIMAL
, strlen(EX_EI_MAXIMAL
));
740 e
= smartlist_get(dest
, 1);
741 tt_mem_op(e
->cache_info
.signed_descriptor_body
, OP_EQ
,
742 EX_EI_MINIMAL
, strlen(EX_EI_MINIMAL
));
744 tt_int_op(2, OP_EQ
, smartlist_len(invalid
));
745 test_memeq_hex(smartlist_get(invalid
, 0),
746 "d5df4aa62ee9ffc9543d41150c9864908e0390af");
747 test_memeq_hex(smartlist_get(invalid
, 1),
748 "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
753 SMARTLIST_FOREACH(dest
, routerinfo_t
*, rt
, routerinfo_free(rt
));
755 SMARTLIST_FOREACH(dest
, extrainfo_t
*, ei
, extrainfo_free(ei
));
756 smartlist_free(dest
);
757 SMARTLIST_FOREACH(invalid
, uint8_t *, dig
, tor_free(dig
));
758 smartlist_free(invalid
);
759 SMARTLIST_FOREACH(chunks
, char *, chunk
, tor_free(chunk
));
760 smartlist_free(chunks
);
763 digestmap_free((digestmap_t
*)map
, routerinfo_free_wrapper_
);
764 router_get_routerlist()->identity_map
=
765 (struct digest_ri_map_t
*)digestmap_new();
767 tor_free(mem_op_hex_tmp
);
772 static download_status_t dls_minimal
;
773 static download_status_t dls_maximal
;
774 static download_status_t dls_bad_fingerprint
;
775 static download_status_t dls_bad_sig2
;
776 static download_status_t dls_bad_ports
;
777 static download_status_t dls_bad_tokens
;
779 static int mock_router_get_dl_status_unrecognized
= 0;
780 static int mock_router_get_dl_status_calls
= 0;
782 static download_status_t
*
783 mock_router_get_dl_status(const char *d
)
785 ++mock_router_get_dl_status_calls
;
786 char hex
[HEX_DIGEST_LEN
+1];
787 base16_encode(hex
, sizeof(hex
), d
, DIGEST_LEN
);
788 if (!strcmp(hex
, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
790 } else if (!strcmp(hex
, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
792 } else if (!strcmp(hex
, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
793 return &dls_bad_fingerprint
;
794 } else if (!strcmp(hex
, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
795 return &dls_bad_sig2
;
796 } else if (!strcmp(hex
, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
797 return &dls_bad_ports
;
798 } else if (!strcmp(hex
, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
799 return &dls_bad_tokens
;
801 ++mock_router_get_dl_status_unrecognized
;
807 test_dir_load_routers(void *arg
)
810 smartlist_t
*chunks
= smartlist_new();
811 smartlist_t
*wanted
= smartlist_new();
812 char buf
[DIGEST_LEN
];
813 char *mem_op_hex_tmp
= NULL
;
818 tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
819 smartlist_add_strdup(wanted, hex_str(buf, DIGEST_LEN)); \
822 MOCK(router_get_dl_status_by_descriptor_digest
, mock_router_get_dl_status
);
824 update_approx_time(1412510400);
826 smartlist_add_strdup(chunks
, EX_RI_MINIMAL
);
827 smartlist_add_strdup(chunks
, EX_RI_BAD_FINGERPRINT
);
828 smartlist_add_strdup(chunks
, EX_RI_BAD_SIG2
);
829 smartlist_add_strdup(chunks
, EX_RI_MAXIMAL
);
830 smartlist_add_strdup(chunks
, EX_RI_BAD_PORTS
);
831 smartlist_add_strdup(chunks
, EX_RI_BAD_TOKENS
);
833 /* not ADDing MINIMIAL */
835 ADD(EX_RI_BAD_FINGERPRINT
);
837 /* Not ADDing BAD_PORTS */
838 ADD(EX_RI_BAD_TOKENS
);
840 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
842 router_load_routers_from_string(list
, NULL
, SAVED_IN_JOURNAL
,
845 /* The "maximal" router was added. */
846 /* "minimal" was not. */
847 tt_int_op(smartlist_len(router_get_routerlist()->routers
),OP_EQ
,1);
848 routerinfo_t
*r
= smartlist_get(router_get_routerlist()->routers
, 0);
849 test_memeq_hex(r
->cache_info
.signed_descriptor_digest
,
850 "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
851 tt_int_op(dls_minimal
.n_download_failures
, OP_EQ
, 0);
852 tt_int_op(dls_maximal
.n_download_failures
, OP_EQ
, 0);
854 /* "Bad fingerprint" and "Bad tokens" should have gotten marked
856 tt_want_int_op(mock_router_get_dl_status_calls
, OP_EQ
, 2);
857 tt_want_int_op(mock_router_get_dl_status_unrecognized
, OP_EQ
, 0);
858 tt_int_op(dls_bad_fingerprint
.n_download_failures
, OP_EQ
, 255);
859 tt_int_op(dls_bad_tokens
.n_download_failures
, OP_EQ
, 255);
861 /* bad_sig2 and bad ports" are retriable -- one since only the signature
862 * was bad, and one because we didn't ask for it. */
863 tt_int_op(dls_bad_sig2
.n_download_failures
, OP_EQ
, 0);
864 tt_int_op(dls_bad_ports
.n_download_failures
, OP_EQ
, 0);
866 /* Wanted still contains "BAD_SIG2" */
867 tt_int_op(smartlist_len(wanted
), OP_EQ
, 1);
868 tt_str_op(smartlist_get(wanted
, 0), OP_EQ
,
869 "E0A3753CEFD54128EAB239F294954121DB23D2EF");
874 tor_free(mem_op_hex_tmp
);
875 UNMOCK(router_get_dl_status_by_descriptor_digest
);
876 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
877 smartlist_free(chunks
);
878 SMARTLIST_FOREACH(wanted
, char *, cp
, tor_free(cp
));
879 smartlist_free(wanted
);
883 static int mock_get_by_ei_dd_calls
= 0;
884 static int mock_get_by_ei_dd_unrecognized
= 0;
886 static signed_descriptor_t sd_ei_minimal
;
887 static signed_descriptor_t sd_ei_bad_nickname
;
888 static signed_descriptor_t sd_ei_maximal
;
889 static signed_descriptor_t sd_ei_bad_tokens
;
890 static signed_descriptor_t sd_ei_bad_sig2
;
892 static signed_descriptor_t
*
893 mock_get_by_ei_desc_digest(const char *d
)
896 ++mock_get_by_ei_dd_calls
;
897 char hex
[HEX_DIGEST_LEN
+1];
898 base16_encode(hex
, sizeof(hex
), d
, DIGEST_LEN
);
900 if (!strcmp(hex
, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
901 return &sd_ei_minimal
;
902 } else if (!strcmp(hex
, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
903 return &sd_ei_maximal
;
904 } else if (!strcmp(hex
, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
905 return &sd_ei_bad_nickname
;
906 } else if (!strcmp(hex
, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
907 return &sd_ei_bad_sig2
;
908 } else if (!strcmp(hex
, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
909 return &sd_ei_bad_tokens
;
911 ++mock_get_by_ei_dd_unrecognized
;
916 static signed_descriptor_t
*
917 mock_ei_get_by_ei_digest(const char *d
)
919 char hex
[HEX_DIGEST_LEN
+1];
920 base16_encode(hex
, sizeof(hex
), d
, DIGEST_LEN
);
921 signed_descriptor_t
*sd
= &sd_ei_minimal
;
923 if (!strcmp(hex
, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
924 sd
->signed_descriptor_body
= (char *)EX_EI_MINIMAL
;
925 sd
->signed_descriptor_len
= sizeof(EX_EI_MINIMAL
);
926 sd
->annotations_len
= 0;
927 sd
->saved_location
= SAVED_NOWHERE
;
933 static smartlist_t
*mock_ei_insert_list
= NULL
;
934 static was_router_added_t
935 mock_ei_insert(routerlist_t
*rl
, extrainfo_t
*ei
, int warn_if_incompatible
)
938 (void) warn_if_incompatible
;
939 smartlist_add(mock_ei_insert_list
, ei
);
940 return ROUTER_ADDED_SUCCESSFULLY
;
944 test_dir_load_extrainfo(void *arg
)
947 smartlist_t
*chunks
= smartlist_new();
948 smartlist_t
*wanted
= smartlist_new();
949 char buf
[DIGEST_LEN
];
950 char *mem_op_hex_tmp
= NULL
;
955 tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
956 smartlist_add_strdup(wanted, hex_str(buf, DIGEST_LEN)); \
959 mock_ei_insert_list
= smartlist_new();
960 MOCK(router_get_by_extrainfo_digest
, mock_get_by_ei_desc_digest
);
961 MOCK(extrainfo_insert
, mock_ei_insert
);
963 smartlist_add_strdup(chunks
, EX_EI_MINIMAL
);
964 smartlist_add_strdup(chunks
, EX_EI_BAD_NICKNAME
);
965 smartlist_add_strdup(chunks
, EX_EI_MAXIMAL
);
966 smartlist_add_strdup(chunks
, EX_EI_BAD_PUBLISHED
);
967 smartlist_add_strdup(chunks
, EX_EI_BAD_TOKENS
);
969 /* not ADDing MINIMIAL */
971 ADD(EX_EI_BAD_NICKNAME
);
972 /* Not ADDing BAD_PUBLISHED */
973 ADD(EX_EI_BAD_TOKENS
);
976 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
977 router_load_extrainfo_from_string(list
, NULL
, SAVED_IN_JOURNAL
, wanted
, 1);
979 /* The "maximal" router was added. */
980 /* "minimal" was also added, even though we didn't ask for it, since
981 * that's what we do with extrainfos. */
982 tt_int_op(smartlist_len(mock_ei_insert_list
),OP_EQ
,2);
984 extrainfo_t
*e
= smartlist_get(mock_ei_insert_list
, 0);
985 test_memeq_hex(e
->cache_info
.signed_descriptor_digest
,
986 "11E0EDF526950739F7769810FCACAB8C882FAEEE");
988 e
= smartlist_get(mock_ei_insert_list
, 1);
989 test_memeq_hex(e
->cache_info
.signed_descriptor_digest
,
990 "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
991 tt_int_op(dls_minimal
.n_download_failures
, OP_EQ
, 0);
992 tt_int_op(dls_maximal
.n_download_failures
, OP_EQ
, 0);
994 /* "Bad nickname" and "Bad tokens" should have gotten marked
996 tt_want_int_op(mock_get_by_ei_dd_calls
, OP_EQ
, 2);
997 tt_want_int_op(mock_get_by_ei_dd_unrecognized
, OP_EQ
, 0);
998 tt_int_op(sd_ei_bad_nickname
.ei_dl_status
.n_download_failures
, OP_EQ
, 255);
999 tt_int_op(sd_ei_bad_tokens
.ei_dl_status
.n_download_failures
, OP_EQ
, 255);
1001 /* bad_ports is retriable -- because we didn't ask for it. */
1002 tt_int_op(dls_bad_ports
.n_download_failures
, OP_EQ
, 0);
1004 /* Wanted still contains "BAD_SIG2" */
1005 tt_int_op(smartlist_len(wanted
), OP_EQ
, 1);
1006 tt_str_op(smartlist_get(wanted
, 0), OP_EQ
,
1007 "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
1012 tor_free(mem_op_hex_tmp
);
1013 UNMOCK(router_get_by_extrainfo_digest
);
1014 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
1015 smartlist_free(chunks
);
1016 SMARTLIST_FOREACH(wanted
, char *, cp
, tor_free(cp
));
1017 smartlist_free(wanted
);
1022 test_dir_getinfo_extra(void *arg
)
1025 char *answer
= NULL
;
1026 const char *errmsg
= NULL
;
1029 MOCK(extrainfo_get_by_descriptor_digest
, mock_ei_get_by_ei_digest
);
1030 r
= getinfo_helper_dir(NULL
, "extra-info/digest/"
1031 "11E0EDF526950739F7769810FCACAB8C882FAEEE", &answer
,
1033 tt_int_op(0, OP_EQ
, r
);
1034 tt_ptr_op(NULL
, OP_EQ
, errmsg
);
1035 tt_str_op(answer
, OP_EQ
, EX_EI_MINIMAL
);
1039 r
= getinfo_helper_dir(NULL
, "extra-info/digest/"
1040 "NOTAVALIDHEXSTRINGNOTAVALIDHEXSTRINGNOTA", &answer
,
1042 tt_int_op(0, OP_EQ
, r
);
1043 /* getinfo_helper_dir() should maybe return an error here but doesn't */
1044 tt_ptr_op(NULL
, OP_EQ
, errmsg
);
1045 /* In any case, there should be no answer for an invalid hex string. */
1046 tt_ptr_op(NULL
, OP_EQ
, answer
);
1049 UNMOCK(extrainfo_get_by_descriptor_digest
);
1053 test_dir_versions(void *arg
)
1057 /* Try out version parsing functionality */
1059 tt_int_op(0,OP_EQ
, tor_version_parse("0.3.4pre2-cvs", &ver1
));
1060 tt_int_op(0,OP_EQ
, ver1
.major
);
1061 tt_int_op(3,OP_EQ
, ver1
.minor
);
1062 tt_int_op(4,OP_EQ
, ver1
.micro
);
1063 tt_int_op(VER_PRE
,OP_EQ
, ver1
.status
);
1064 tt_int_op(2,OP_EQ
, ver1
.patchlevel
);
1065 tt_int_op(0,OP_EQ
, tor_version_parse("0.3.4rc1", &ver1
));
1066 tt_int_op(0,OP_EQ
, ver1
.major
);
1067 tt_int_op(3,OP_EQ
, ver1
.minor
);
1068 tt_int_op(4,OP_EQ
, ver1
.micro
);
1069 tt_int_op(VER_RC
,OP_EQ
, ver1
.status
);
1070 tt_int_op(1,OP_EQ
, ver1
.patchlevel
);
1071 tt_int_op(0,OP_EQ
, tor_version_parse("1.3.4", &ver1
));
1072 tt_int_op(1,OP_EQ
, ver1
.major
);
1073 tt_int_op(3,OP_EQ
, ver1
.minor
);
1074 tt_int_op(4,OP_EQ
, ver1
.micro
);
1075 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
1076 tt_int_op(0,OP_EQ
, ver1
.patchlevel
);
1077 tt_int_op(0,OP_EQ
, tor_version_parse("1.3.4.999", &ver1
));
1078 tt_int_op(1,OP_EQ
, ver1
.major
);
1079 tt_int_op(3,OP_EQ
, ver1
.minor
);
1080 tt_int_op(4,OP_EQ
, ver1
.micro
);
1081 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
1082 tt_int_op(999,OP_EQ
, ver1
.patchlevel
);
1083 tt_int_op(0,OP_EQ
, tor_version_parse("0.1.2.4-alpha", &ver1
));
1084 tt_int_op(0,OP_EQ
, ver1
.major
);
1085 tt_int_op(1,OP_EQ
, ver1
.minor
);
1086 tt_int_op(2,OP_EQ
, ver1
.micro
);
1087 tt_int_op(4,OP_EQ
, ver1
.patchlevel
);
1088 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
1089 tt_str_op("alpha",OP_EQ
, ver1
.status_tag
);
1090 tt_int_op(0,OP_EQ
, tor_version_parse("0.1.2.4", &ver1
));
1091 tt_int_op(0,OP_EQ
, ver1
.major
);
1092 tt_int_op(1,OP_EQ
, ver1
.minor
);
1093 tt_int_op(2,OP_EQ
, ver1
.micro
);
1094 tt_int_op(4,OP_EQ
, ver1
.patchlevel
);
1095 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
1096 tt_str_op("",OP_EQ
, ver1
.status_tag
);
1098 tt_int_op(0, OP_EQ
, tor_version_parse("10.1", &ver1
));
1099 tt_int_op(10, OP_EQ
, ver1
.major
);
1100 tt_int_op(1, OP_EQ
, ver1
.minor
);
1101 tt_int_op(0, OP_EQ
, ver1
.micro
);
1102 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1103 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1104 tt_str_op("", OP_EQ
, ver1
.status_tag
);
1105 tt_int_op(0, OP_EQ
, tor_version_parse("5.99.999", &ver1
));
1106 tt_int_op(5, OP_EQ
, ver1
.major
);
1107 tt_int_op(99, OP_EQ
, ver1
.minor
);
1108 tt_int_op(999, OP_EQ
, ver1
.micro
);
1109 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1110 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1111 tt_str_op("", OP_EQ
, ver1
.status_tag
);
1112 tt_int_op(0, OP_EQ
, tor_version_parse("10.1-alpha", &ver1
));
1113 tt_int_op(10, OP_EQ
, ver1
.major
);
1114 tt_int_op(1, OP_EQ
, ver1
.minor
);
1115 tt_int_op(0, OP_EQ
, ver1
.micro
);
1116 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1117 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1118 tt_str_op("alpha", OP_EQ
, ver1
.status_tag
);
1119 /* Go through the full set of status tags */
1120 tt_int_op(0, OP_EQ
, tor_version_parse("2.1.700-alpha", &ver1
));
1121 tt_int_op(2, OP_EQ
, ver1
.major
);
1122 tt_int_op(1, OP_EQ
, ver1
.minor
);
1123 tt_int_op(700, OP_EQ
, ver1
.micro
);
1124 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1125 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1126 tt_str_op("alpha", OP_EQ
, ver1
.status_tag
);
1127 tt_int_op(0, OP_EQ
, tor_version_parse("1.6.8-alpha-dev", &ver1
));
1128 tt_int_op(1, OP_EQ
, ver1
.major
);
1129 tt_int_op(6, OP_EQ
, ver1
.minor
);
1130 tt_int_op(8, OP_EQ
, ver1
.micro
);
1131 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1132 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1133 tt_str_op("alpha-dev", OP_EQ
, ver1
.status_tag
);
1134 tt_int_op(0, OP_EQ
, tor_version_parse("0.2.9.5-rc", &ver1
));
1135 tt_int_op(0, OP_EQ
, ver1
.major
);
1136 tt_int_op(2, OP_EQ
, ver1
.minor
);
1137 tt_int_op(9, OP_EQ
, ver1
.micro
);
1138 tt_int_op(5, OP_EQ
, ver1
.patchlevel
);
1139 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1140 tt_str_op("rc", OP_EQ
, ver1
.status_tag
);
1141 tt_int_op(0, OP_EQ
, tor_version_parse("0.2.9.6-rc-dev", &ver1
));
1142 tt_int_op(0, OP_EQ
, ver1
.major
);
1143 tt_int_op(2, OP_EQ
, ver1
.minor
);
1144 tt_int_op(9, OP_EQ
, ver1
.micro
);
1145 tt_int_op(6, OP_EQ
, ver1
.patchlevel
);
1146 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1147 tt_str_op("rc-dev", OP_EQ
, ver1
.status_tag
);
1148 tt_int_op(0, OP_EQ
, tor_version_parse("0.2.9.8", &ver1
));
1149 tt_int_op(0, OP_EQ
, ver1
.major
);
1150 tt_int_op(2, OP_EQ
, ver1
.minor
);
1151 tt_int_op(9, OP_EQ
, ver1
.micro
);
1152 tt_int_op(8, OP_EQ
, ver1
.patchlevel
);
1153 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1154 tt_str_op("", OP_EQ
, ver1
.status_tag
);
1155 tt_int_op(0, OP_EQ
, tor_version_parse("0.2.9.9-dev", &ver1
));
1156 tt_int_op(0, OP_EQ
, ver1
.major
);
1157 tt_int_op(2, OP_EQ
, ver1
.minor
);
1158 tt_int_op(9, OP_EQ
, ver1
.micro
);
1159 tt_int_op(9, OP_EQ
, ver1
.patchlevel
);
1160 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1161 tt_str_op("dev", OP_EQ
, ver1
.status_tag
);
1162 /* In #21450, we fixed an inconsistency in parsing versions > INT32_MAX
1163 * between i386 and x86_64, as we used tor_parse_long, and then cast to int
1165 tt_int_op(0, OP_EQ
, tor_version_parse("0.2147483647.0", &ver1
));
1166 tt_int_op(0, OP_EQ
, ver1
.major
);
1167 tt_int_op(2147483647, OP_EQ
, ver1
.minor
);
1168 tt_int_op(0, OP_EQ
, ver1
.micro
);
1169 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1170 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1171 tt_str_op("", OP_EQ
, ver1
.status_tag
);
1172 tt_int_op(-1, OP_EQ
, tor_version_parse("0.2147483648.0", &ver1
));
1173 tt_int_op(-1, OP_EQ
, tor_version_parse("0.4294967295.0", &ver1
));
1174 /* In #21278, we reject negative version components */
1175 tt_int_op(-1, OP_EQ
, tor_version_parse("0.-1.0", &ver1
));
1176 tt_int_op(-1, OP_EQ
, tor_version_parse("0.-2147483648.0", &ver1
));
1177 tt_int_op(-1, OP_EQ
, tor_version_parse("0.-4294967295.0", &ver1
));
1178 /* In #21507, we reject version components with non-numeric prefixes */
1179 tt_int_op(-1, OP_EQ
, tor_version_parse("0.-0.0", &ver1
));
1180 tt_int_op(-1, OP_EQ
, tor_version_parse("+1.0.0", &ver1
));
1181 /* use the list in isspace() */
1182 tt_int_op(-1, OP_EQ
, tor_version_parse("0.\t0.0", &ver1
));
1183 tt_int_op(-1, OP_EQ
, tor_version_parse("0.\n0.0", &ver1
));
1184 tt_int_op(-1, OP_EQ
, tor_version_parse("0.\v0.0", &ver1
));
1185 tt_int_op(-1, OP_EQ
, tor_version_parse("0.\f0.0", &ver1
));
1186 tt_int_op(-1, OP_EQ
, tor_version_parse("0.\r0.0", &ver1
));
1187 tt_int_op(-1, OP_EQ
, tor_version_parse("0. 0.0", &ver1
));
1189 #define tt_versionstatus_op(vs1, op, vs2) \
1190 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
1191 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
1192 #define test_v_i_o(val, ver, lst) \
1193 tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
1195 /* make sure tor_version_is_obsolete() works */
1196 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
1197 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
1198 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
1199 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
1200 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
1201 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
1202 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
1203 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
1204 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
1205 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
1206 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
1207 test_v_i_o(VS_NEW
, "0.2.9.9-dev", "0.2.9.9");
1208 /* Not on list, but newer than any in same series. */
1209 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
1210 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1211 /* Series newer than any on list. */
1212 test_v_i_o(VS_NEW
, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1213 /* Series older than any on list. */
1214 test_v_i_o(VS_OLD
, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1215 /* Not on list, not newer than any on same series. */
1216 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
1217 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1218 /* On list, not newer than any on same series. */
1219 test_v_i_o(VS_UNRECOMMENDED
,
1220 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1221 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1222 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1223 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1224 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
1226 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1227 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1228 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1230 /* Now try svn revisions. */
1231 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1232 "Tor 0.2.1.0-dev (r99)"));
1233 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1234 "Tor 0.2.1.0-dev (r100) on Banana Jr",
1235 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
1236 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1237 "Tor 0.2.1.0-dev on Colossus"));
1238 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
1239 "Tor 0.2.1.0-dev (r100)"));
1240 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
1241 "Tor 0.2.1.0-dev (r100) on AM"));
1242 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev",
1243 "Tor 0.2.1.0-dev (r99)"));
1244 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.1",
1245 "Tor 0.2.1.0-dev (r99)"));
1246 /* And git revisions */
1247 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1248 "Tor 0.2.9.9 (git-56788a2489127072)",
1249 "Tor 0.2.9.9 (git-56788a2489127072)"));
1250 /* a git revision is newer than no git revision */
1251 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1252 "Tor 0.2.9.9 (git-56788a2489127072)",
1254 /* a longer git revision is newer than a shorter git revision
1255 * this should be true if they prefix-match, but if they don't, they are
1256 * incomparable, because hashes aren't ordered (but we compare their bytes
1258 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1259 "Tor 0.2.9.9 (git-56788a2489127072d513cf4baf35a8ff475f3c7b)",
1260 "Tor 0.2.9.9 (git-56788a2489127072)"));
1261 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1262 "Tor 0.2.9.9 (git-0102)",
1263 "Tor 0.2.9.9 (git-03)"));
1264 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1265 "Tor 0.2.9.9 (git-0102)",
1266 "Tor 0.2.9.9 (git-00)"));
1267 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1268 "Tor 0.2.9.9 (git-01)",
1269 "Tor 0.2.9.9 (git-00)"));
1270 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1271 "Tor 0.2.9.9 (git-00)",
1272 "Tor 0.2.9.9 (git-01)"));
1273 /* In #21278, we comapre without integer overflows.
1274 * But since #21450 limits version components to [0, INT32_MAX], it is no
1275 * longer possible to cause an integer overflow in tor_version_compare() */
1276 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1278 "Tor 2147483647.0.0.0"));
1279 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1280 "Tor 2147483647.0.0.0",
1282 /* These versions used to cause an overflow, now they don't parse
1283 * (and authorities reject their descriptors), and log a BUG message */
1284 setup_full_capture_of_logs(LOG_WARN
);
1285 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1287 "Tor 0.-2147483648.0.0"));
1288 expect_single_log_msg_containing("unparseable");
1289 mock_clean_saved_logs();
1290 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1291 "Tor 0.2147483647.0.0",
1293 expect_single_log_msg_containing("unparseable");
1294 mock_clean_saved_logs();
1295 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1296 "Tor 0.2147483647.0.0",
1297 "Tor 0.-2147483648.0.0"));
1298 expect_single_log_msg_containing("unparseable");
1299 mock_clean_saved_logs();
1300 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1301 "Tor 4294967295.0.0.0",
1303 expect_no_log_entry();
1304 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1305 "Tor 0.4294967295.0.0",
1306 "Tor 0.-4294967295.0.0"));
1307 expect_single_log_msg_containing("unparseable");
1308 mock_clean_saved_logs();
1309 teardown_capture_of_logs();
1311 /* Now try git revisions */
1312 tt_int_op(0,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1
));
1313 tt_int_op(0,OP_EQ
, ver1
.major
);
1314 tt_int_op(5,OP_EQ
, ver1
.minor
);
1315 tt_int_op(6,OP_EQ
, ver1
.micro
);
1316 tt_int_op(7,OP_EQ
, ver1
.patchlevel
);
1317 tt_int_op(3,OP_EQ
, ver1
.git_tag_len
);
1318 tt_mem_op(ver1
.git_tag
,OP_EQ
, "\xff\x00\xff", 3);
1319 /* reject bad hex digits */
1320 tt_int_op(-1,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1
));
1321 /* reject odd hex digit count */
1322 tt_int_op(-1,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1
));
1324 tt_int_op(0,OP_EQ
, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1
));
1325 /* standard length is 16 hex digits */
1326 tt_int_op(0,OP_EQ
, tor_version_parse("0.5.6.7 (git-0010203040506070)",
1328 /* length limit is 40 hex digits */
1329 tt_int_op(0,OP_EQ
, tor_version_parse(
1330 "0.5.6.7 (git-000102030405060708090a0b0c0d0e0f10111213)",
1332 tt_int_op(-1,OP_EQ
, tor_version_parse(
1333 "0.5.6.7 (git-000102030405060708090a0b0c0d0e0f1011121314)",
1336 teardown_capture_of_logs();
1339 /** Run unit tests for directory fp_pair functions. */
1341 test_dir_fp_pairs(void *arg
)
1343 smartlist_t
*sl
= smartlist_new();
1347 dir_split_resource_into_fingerprint_pairs(
1348 /* Two pairs, out of order, with one duplicate. */
1349 "73656372657420646174612E0000000000FFFFFF-"
1350 "557365204145532d32353620696e73746561642e+"
1351 "73656372657420646174612E0000000000FFFFFF-"
1352 "557365204145532d32353620696e73746561642e+"
1353 "48657861646563696d616c2069736e277420736f-"
1354 "676f6f6420666f7220686964696e6720796f7572.z", sl
);
1356 tt_int_op(smartlist_len(sl
),OP_EQ
, 2);
1357 pair
= smartlist_get(sl
, 0);
1358 tt_mem_op(pair
->first
,OP_EQ
, "Hexadecimal isn't so", DIGEST_LEN
);
1359 tt_mem_op(pair
->second
,OP_EQ
, "good for hiding your", DIGEST_LEN
);
1360 pair
= smartlist_get(sl
, 1);
1361 tt_mem_op(pair
->first
,OP_EQ
, "secret data.\0\0\0\0\0\xff\xff\xff",
1363 tt_mem_op(pair
->second
,OP_EQ
, "Use AES-256 instead.", DIGEST_LEN
);
1366 SMARTLIST_FOREACH(sl
, fp_pair_t
*, pair_to_free
, tor_free(pair_to_free
));
1371 test_dir_split_fps(void *testdata
)
1373 smartlist_t
*sl
= smartlist_new();
1374 char *mem_op_hex_tmp
= NULL
;
1377 /* Some example hex fingerprints and their base64 equivalents */
1378 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1379 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1380 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
1382 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
1384 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
1386 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
1387 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
1388 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
1389 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
1390 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
1393 dir_split_resource_into_fingerprints("A+C+B", sl
, NULL
, 0);
1394 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1395 tt_str_op(smartlist_get(sl
, 0), OP_EQ
, "A");
1396 tt_str_op(smartlist_get(sl
, 1), OP_EQ
, "C");
1397 tt_str_op(smartlist_get(sl
, 2), OP_EQ
, "B");
1398 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1399 smartlist_clear(sl
);
1402 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl
, NULL
, DSR_SORT_UNIQ
);
1403 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1404 tt_str_op(smartlist_get(sl
, 0), OP_EQ
, "A");
1405 tt_str_op(smartlist_get(sl
, 1), OP_EQ
, "B");
1406 tt_str_op(smartlist_get(sl
, 2), OP_EQ
, "C");
1407 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1408 smartlist_clear(sl
);
1411 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
, sl
, NULL
, DSR_HEX
);
1412 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1413 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1414 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1415 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1416 smartlist_clear(sl
);
1418 /* decode hex and drop weirdness. */
1419 dir_split_resource_into_fingerprints(HEX1
"+bogus+"HEX2
"+"HEX256_1
,
1421 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1422 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1423 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1424 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1425 smartlist_clear(sl
);
1427 /* Decode long hex */
1428 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX2
"+"HEX256_3
,
1429 sl
, NULL
, DSR_HEX
|DSR_DIGEST256
);
1430 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1431 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1432 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1433 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX256_3
);
1434 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1435 smartlist_clear(sl
);
1437 /* Decode hex and sort. */
1438 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
"+"HEX3
"+"HEX2
,
1439 sl
, NULL
, DSR_HEX
|DSR_SORT_UNIQ
);
1440 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1441 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX3
);
1442 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1443 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX1
);
1444 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1445 smartlist_clear(sl
);
1447 /* Decode long hex and sort */
1448 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX256_3
1451 DSR_HEX
|DSR_DIGEST256
|DSR_SORT_UNIQ
);
1452 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1453 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_3
);
1454 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1455 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX256_1
);
1456 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1457 smartlist_clear(sl
);
1460 dir_split_resource_into_fingerprints(B64_1
"-"B64_2
, sl
, NULL
, DSR_BASE64
);
1461 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1462 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1463 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1464 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1465 smartlist_clear(sl
);
1467 /* Decode long base64 */
1468 dir_split_resource_into_fingerprints(B64_256_1
"-"B64_256_2
,
1469 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
1470 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1471 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1472 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1473 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1474 smartlist_clear(sl
);
1476 dir_split_resource_into_fingerprints(B64_256_1
,
1477 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
1478 tt_int_op(smartlist_len(sl
), OP_EQ
, 1);
1479 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1480 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1481 smartlist_clear(sl
);
1484 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1486 tor_free(mem_op_hex_tmp
);
1490 test_dir_measured_bw_kb(void *arg
)
1492 measured_bw_line_t mbwl
;
1494 const char *lines_pass
[] = {
1495 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1496 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
1497 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1498 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
1499 "bw=1024 junk=007\n",
1500 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
1501 "bw=1024 junk=007\n",
1504 const char *lines_fail
[] = {
1505 /* Test possible python stupidity on input */
1506 "node_id=None bw=1024\n",
1507 "node_id=$None bw=1024\n",
1508 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
1509 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
1510 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
1511 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
1512 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
1513 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
1514 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
1515 /* Test incomplete writes due to race conditions, partial copies, etc */
1520 "node_id=$557365204145532d32353620696e73746561642e bw=",
1521 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
1522 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
1523 "node_id=$557365204145532d32353620696e7374",
1524 "node_id=$557365204145532d32353620696e7374\n",
1529 /* Test assorted noise */
1531 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
1532 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
1533 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
1534 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
1539 for (i
= 0; strcmp(lines_fail
[i
], "end"); i
++) {
1540 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
1541 tt_int_op(measured_bw_line_parse(&mbwl
, lines_fail
[i
]), OP_EQ
, -1);
1544 for (i
= 0; strcmp(lines_pass
[i
], "end"); i
++) {
1545 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
1546 tt_int_op(measured_bw_line_parse(&mbwl
, lines_pass
[i
]), OP_EQ
, 0);
1547 tt_assert(mbwl
.bw_kb
== 1024);
1548 tt_assert(strcmp(mbwl
.node_hex
,
1549 "557365204145532d32353620696e73746561642e") == 0);
1556 #define MBWC_INIT_TIME 1000
1558 /** Do the measured bandwidth cache unit test */
1560 test_dir_measured_bw_kb_cache(void *arg
)
1562 /* Initial fake time_t for testing */
1563 time_t curr
= MBWC_INIT_TIME
;
1564 /* Some measured_bw_line_ts */
1565 measured_bw_line_t mbwl
[3];
1566 /* For receiving output on cache queries */
1570 /* First, clear the cache and assert that it's empty */
1572 dirserv_clear_measured_bw_cache();
1573 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1575 * Set up test mbwls; none of the dirserv_cache_*() functions care about
1576 * the node_hex field.
1578 memset(mbwl
[0].node_id
, 0x01, DIGEST_LEN
);
1580 memset(mbwl
[1].node_id
, 0x02, DIGEST_LEN
);
1582 memset(mbwl
[2].node_id
, 0x03, DIGEST_LEN
);
1584 /* Try caching something */
1585 dirserv_cache_measured_bw(&(mbwl
[0]), curr
);
1586 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1587 /* Okay, let's see if we can retrieve it */
1588 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,&bw
, &as_of
));
1589 tt_int_op(bw
,OP_EQ
, 20);
1590 tt_int_op(as_of
,OP_EQ
, MBWC_INIT_TIME
);
1591 /* Try retrieving it without some outputs */
1592 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,NULL
, NULL
));
1593 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,&bw
, NULL
));
1594 tt_int_op(bw
,OP_EQ
, 20);
1595 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,NULL
,&as_of
));
1596 tt_int_op(as_of
,OP_EQ
, MBWC_INIT_TIME
);
1598 curr
+= MAX_MEASUREMENT_AGE
+ 1;
1599 dirserv_expire_measured_bw_cache(curr
);
1600 /* Check that the cache is empty */
1601 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1602 /* Check that we can't retrieve it */
1603 tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
, NULL
,NULL
));
1604 /* Try caching a few things now */
1605 dirserv_cache_measured_bw(&(mbwl
[0]), curr
);
1606 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1607 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1608 dirserv_cache_measured_bw(&(mbwl
[1]), curr
);
1609 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 2);
1610 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1611 dirserv_cache_measured_bw(&(mbwl
[2]), curr
);
1612 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 3);
1613 curr
+= MAX_MEASUREMENT_AGE
/ 4 + 1;
1614 /* Do an expire that's too soon to get any of them */
1615 dirserv_expire_measured_bw_cache(curr
);
1616 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 3);
1617 /* Push the oldest one off the cliff */
1618 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1619 dirserv_expire_measured_bw_cache(curr
);
1620 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 2);
1621 /* And another... */
1622 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1623 dirserv_expire_measured_bw_cache(curr
);
1624 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1625 /* This should empty it out again */
1626 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1627 dirserv_expire_measured_bw_cache(curr
);
1628 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1635 my_dirvote_compute_params(smartlist_t
*votes
, int method
,
1636 int total_authorities
)
1638 smartlist_t
*s
= dirvote_compute_params(votes
, method
, total_authorities
);
1640 char *res
= smartlist_join_strings(s
, " ", 0, NULL
);
1641 SMARTLIST_FOREACH(s
, char *, cp
, tor_free(cp
));
1646 #define dirvote_compute_params my_dirvote_compute_params
1649 test_dir_param_voting(void *arg
)
1651 networkstatus_t vote1
, vote2
, vote3
, vote4
;
1652 smartlist_t
*votes
= smartlist_new();
1655 /* dirvote_compute_params only looks at the net_params field of the votes,
1656 so that's all we need to set.
1659 memset(&vote1
, 0, sizeof(vote1
));
1660 memset(&vote2
, 0, sizeof(vote2
));
1661 memset(&vote3
, 0, sizeof(vote3
));
1662 memset(&vote4
, 0, sizeof(vote4
));
1663 vote1
.net_params
= smartlist_new();
1664 vote2
.net_params
= smartlist_new();
1665 vote3
.net_params
= smartlist_new();
1666 vote4
.net_params
= smartlist_new();
1667 smartlist_split_string(vote1
.net_params
,
1668 "ab=90 abcd=20 cw=50 x-yz=-99", NULL
, 0, 0);
1669 smartlist_split_string(vote2
.net_params
,
1670 "ab=27 cw=5 x-yz=88", NULL
, 0, 0);
1671 smartlist_split_string(vote3
.net_params
,
1672 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL
, 0, 0);
1673 smartlist_split_string(vote4
.net_params
,
1674 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL
, 0, 0);
1675 tt_int_op(100,OP_EQ
, networkstatus_get_param(&vote4
, "x-yz", 50, 0, 300));
1676 tt_int_op(222,OP_EQ
, networkstatus_get_param(&vote4
, "foobar", 222, 0, 300));
1677 tt_int_op(80,OP_EQ
, networkstatus_get_param(&vote4
, "ab", 12, 0, 80));
1678 tt_int_op(-8,OP_EQ
, networkstatus_get_param(&vote4
, "ab", -12, -100, -8));
1679 tt_int_op(0,OP_EQ
, networkstatus_get_param(&vote4
, "foobar", 0, -100, 8));
1681 tt_int_op(100,OP_EQ
, networkstatus_get_overridable_param(
1682 &vote4
, -1, "x-yz", 50, 0, 300));
1683 tt_int_op(30,OP_EQ
, networkstatus_get_overridable_param(
1684 &vote4
, 30, "x-yz", 50, 0, 300));
1685 tt_int_op(0,OP_EQ
, networkstatus_get_overridable_param(
1686 &vote4
, -101, "foobar", 0, -100, 8));
1687 tt_int_op(-99,OP_EQ
, networkstatus_get_overridable_param(
1688 &vote4
, -99, "foobar", 0, -100, 8));
1690 smartlist_add(votes
, &vote1
);
1692 /* Do the first tests without adding all the other votes, for
1693 * networks without many dirauths. */
1695 res
= dirvote_compute_params(votes
, 12, 2);
1696 tt_str_op(res
,OP_EQ
, "");
1699 res
= dirvote_compute_params(votes
, 12, 1);
1700 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-99");
1703 smartlist_add(votes
, &vote2
);
1705 res
= dirvote_compute_params(votes
, 12, 2);
1706 tt_str_op(res
,OP_EQ
, "ab=27 cw=5 x-yz=-99");
1709 res
= dirvote_compute_params(votes
, 12, 3);
1710 tt_str_op(res
,OP_EQ
, "ab=27 cw=5 x-yz=-99");
1713 res
= dirvote_compute_params(votes
, 12, 6);
1714 tt_str_op(res
,OP_EQ
, "");
1717 smartlist_add(votes
, &vote3
);
1719 res
= dirvote_compute_params(votes
, 12, 3);
1720 tt_str_op(res
,OP_EQ
, "ab=27 abcd=20 cw=50 x-yz=-9");
1723 res
= dirvote_compute_params(votes
, 12, 5);
1724 tt_str_op(res
,OP_EQ
, "cw=50 x-yz=-9");
1727 res
= dirvote_compute_params(votes
, 12, 9);
1728 tt_str_op(res
,OP_EQ
, "cw=50 x-yz=-9");
1731 smartlist_add(votes
, &vote4
);
1733 res
= dirvote_compute_params(votes
, 12, 4);
1734 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1737 res
= dirvote_compute_params(votes
, 12, 5);
1738 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1741 /* Test that the special-cased "at least three dirauths voted for
1742 * this param" logic works as expected. */
1743 res
= dirvote_compute_params(votes
, 12, 6);
1744 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1747 res
= dirvote_compute_params(votes
, 12, 10);
1748 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1753 SMARTLIST_FOREACH(vote1
.net_params
, char *, cp
, tor_free(cp
));
1754 SMARTLIST_FOREACH(vote2
.net_params
, char *, cp
, tor_free(cp
));
1755 SMARTLIST_FOREACH(vote3
.net_params
, char *, cp
, tor_free(cp
));
1756 SMARTLIST_FOREACH(vote4
.net_params
, char *, cp
, tor_free(cp
));
1757 smartlist_free(vote1
.net_params
);
1758 smartlist_free(vote2
.net_params
);
1759 smartlist_free(vote3
.net_params
);
1760 smartlist_free(vote4
.net_params
);
1761 smartlist_free(votes
);
1767 test_dir_param_voting_lookup(void *arg
)
1770 smartlist_t
*lst
= smartlist_new();
1772 smartlist_split_string(lst
,
1773 "moomin=9 moomin=10 moomintroll=5 fred "
1774 "jack= electricity=sdk opa=6z abc=9 abcd=99",
1778 OP_EQ
, dirvote_get_intermediate_param_value(lst
, "ab", 1000));
1779 tt_int_op(9, OP_EQ
, dirvote_get_intermediate_param_value(lst
, "abc", 1000));
1780 tt_int_op(99, OP_EQ
,
1781 dirvote_get_intermediate_param_value(lst
, "abcd", 1000));
1783 /* moomin appears twice. That's a bug. */
1784 tor_capture_bugs_(1);
1785 tt_int_op(-100, OP_EQ
,
1786 dirvote_get_intermediate_param_value(lst
, "moomin", -100));
1787 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1788 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1790 tor_end_capture_bugs_();
1791 /* There is no 'fred=', so that is treated as not existing. */
1792 tt_int_op(-100, OP_EQ
,
1793 dirvote_get_intermediate_param_value(lst
, "fred", -100));
1794 /* jack is truncated */
1795 tor_capture_bugs_(1);
1796 tt_int_op(-100, OP_EQ
,
1797 dirvote_get_intermediate_param_value(lst
, "jack", -100));
1798 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1799 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1801 tor_end_capture_bugs_();
1802 /* electricity and opa aren't integers. */
1803 tor_capture_bugs_(1);
1804 tt_int_op(-100, OP_EQ
,
1805 dirvote_get_intermediate_param_value(lst
, "electricity", -100));
1806 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1807 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1809 tor_end_capture_bugs_();
1811 tor_capture_bugs_(1);
1812 tt_int_op(-100, OP_EQ
,
1813 dirvote_get_intermediate_param_value(lst
, "opa", -100));
1814 tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ
, 1);
1815 tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ
,
1817 tor_end_capture_bugs_();
1820 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
1821 smartlist_free(lst
);
1822 tor_end_capture_bugs_();
1825 #undef dirvote_compute_params
1827 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
1828 * same voting authority, and that they do in fact have all the same
1831 test_same_voter(networkstatus_voter_info_t
*v1
,
1832 networkstatus_voter_info_t
*v2
)
1834 tt_str_op(v1
->nickname
,OP_EQ
, v2
->nickname
);
1835 tt_mem_op(v1
->identity_digest
,OP_EQ
, v2
->identity_digest
, DIGEST_LEN
);
1836 tt_str_op(v1
->address
,OP_EQ
, v2
->address
);
1837 tt_int_op(v1
->addr
,OP_EQ
, v2
->addr
);
1838 tt_int_op(v1
->dir_port
,OP_EQ
, v2
->dir_port
);
1839 tt_int_op(v1
->or_port
,OP_EQ
, v2
->or_port
);
1840 tt_str_op(v1
->contact
,OP_EQ
, v2
->contact
);
1841 tt_mem_op(v1
->vote_digest
,OP_EQ
, v2
->vote_digest
, DIGEST_LEN
);
1846 /** Helper: get a detached signatures document for one or two
1849 get_detached_sigs(networkstatus_t
*ns
, networkstatus_t
*ns2
)
1853 tor_assert(ns
&& ns
->flavor
== FLAV_NS
);
1854 sl
= smartlist_new();
1855 smartlist_add(sl
,ns
);
1857 smartlist_add(sl
,ns2
);
1858 r
= networkstatus_get_detached_signatures(sl
);
1863 /** Apply tweaks to the vote list for each voter */
1865 vote_tweaks_for_v3ns(networkstatus_t
*v
, int voter
, time_t now
)
1867 vote_routerstatus_t
*vrs
;
1868 const char *msg
= NULL
;
1874 measured_bw_line_t mbw
;
1875 memset(mbw
.node_id
, 33, sizeof(mbw
.node_id
));
1877 tt_int_op(measured_bw_line_apply(&mbw
, v
->routerstatus_list
), OP_EQ
, 1);
1878 } else if (voter
== 2 || voter
== 3) {
1879 /* Monkey around with the list a bit */
1880 vrs
= smartlist_get(v
->routerstatus_list
, 2);
1881 smartlist_del_keeporder(v
->routerstatus_list
, 2);
1882 vote_routerstatus_free(vrs
);
1883 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1884 vrs
->status
.is_fast
= 1;
1887 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1888 smartlist_del_keeporder(v
->routerstatus_list
, 0);
1889 vote_routerstatus_free(vrs
);
1890 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1891 memset(vrs
->status
.descriptor_digest
, (int)'Z', DIGEST_LEN
);
1892 tt_assert(router_add_to_routerlist(
1893 dir_common_generate_ri_from_rs(vrs
), &msg
,0,0) >= 0);
1902 * Test a parsed vote_routerstatus_t for v3_networkstatus test
1905 test_vrs_for_v3ns(vote_routerstatus_t
*vrs
, int voter
, time_t now
)
1908 tor_addr_t addr_ipv6
;
1911 rs
= &(vrs
->status
);
1914 /* Split out by digests to test */
1915 if (tor_memeq(rs
->identity_digest
,
1916 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1920 /* Check the first routerstatus. */
1921 tt_str_op(vrs
->version
,OP_EQ
, "0.1.2.14");
1922 tt_int_op(rs
->published_on
,OP_EQ
, now
-1500);
1923 tt_str_op(rs
->nickname
,OP_EQ
, "router2");
1924 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1925 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1928 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
1929 tt_int_op(rs
->addr
,OP_EQ
, 0x99008801);
1930 tt_int_op(rs
->or_port
,OP_EQ
, 443);
1931 tt_int_op(rs
->dir_port
,OP_EQ
, 8000);
1932 /* no flags except "running" (16) and "v2dir" (64) and "valid" (128) */
1933 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(0xd0));
1934 } else if (tor_memeq(rs
->identity_digest
,
1935 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1938 (voter
== 1 || voter
== 2)) {
1939 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1940 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1945 /* Check the second routerstatus. */
1946 tt_str_op(vrs
->version
,OP_EQ
, "0.2.0.5");
1947 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
1948 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
1950 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
1951 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
1952 tt_int_op(rs
->or_port
,OP_EQ
, 443);
1953 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
1954 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
1955 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
1956 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
1958 /* all except "authority" (1) */
1959 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(254));
1961 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
1962 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(974));
1964 } else if (tor_memeq(rs
->identity_digest
,
1965 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
1966 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
1968 (voter
== 1 || voter
== 2)) {
1969 /* Check the measured bandwidth bits */
1970 tt_assert(vrs
->has_measured_bw
&&
1971 vrs
->measured_bw_kb
== 1024);
1974 * Didn't expect this, but the old unit test only checked some of them,
1985 * Test a consensus for v3_networkstatus_test
1988 test_consensus_for_v3ns(networkstatus_t
*con
, time_t now
)
1993 tt_ptr_op(con
->cert
, OP_EQ
, NULL
);
1994 tt_int_op(2,OP_EQ
, smartlist_len(con
->routerstatus_list
));
1995 /* There should be two listed routers: one with identity 3, one with
2003 * Test a router list entry for v3_networkstatus test
2006 test_routerstatus_for_v3ns(routerstatus_t
*rs
, time_t now
)
2008 tor_addr_t addr_ipv6
;
2012 /* There should be two listed routers: one with identity 3, one with
2014 /* This one showed up in 2 digests. */
2015 if (tor_memeq(rs
->identity_digest
,
2016 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2019 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2020 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2022 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2023 tt_assert(!rs
->is_authority
);
2024 tt_assert(!rs
->is_exit
);
2025 tt_assert(!rs
->is_fast
);
2026 tt_assert(!rs
->is_possible_guard
);
2027 tt_assert(!rs
->is_stable
);
2028 /* (If it wasn't running it wouldn't be here) */
2029 tt_assert(rs
->is_flagged_running
);
2030 tt_assert(rs
->is_valid
);
2031 tt_assert(!rs
->is_named
);
2032 tt_assert(rs
->is_v2_dir
);
2033 /* XXXX check version */
2034 } else if (tor_memeq(rs
->identity_digest
,
2035 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2038 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2039 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2040 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2042 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
2043 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2044 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
2045 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
2046 tt_int_op(rs
->or_port
,OP_EQ
, 443);
2047 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
2048 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
2049 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
2050 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
2051 tt_assert(!rs
->is_authority
);
2052 tt_assert(rs
->is_exit
);
2053 tt_assert(rs
->is_fast
);
2054 tt_assert(rs
->is_possible_guard
);
2055 tt_assert(rs
->is_stable
);
2056 tt_assert(rs
->is_flagged_running
);
2057 tt_assert(rs
->is_valid
);
2058 tt_assert(rs
->is_v2_dir
);
2059 tt_assert(!rs
->is_named
);
2060 /* XXXX check version */
2062 /* Weren't expecting this... */
2071 test_dir_networkstatus_compute_bw_weights_v10(void *arg
)
2074 smartlist_t
*chunks
= smartlist_new();
2075 int64_t G
, M
, E
, D
, T
, weight_scale
;
2077 weight_scale
= 10000;
2079 /* no case. one or more of the values is 0 */
2082 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2084 tt_int_op(ret
, OP_EQ
, 0);
2085 tt_int_op(smartlist_len(chunks
), OP_EQ
, 0);
2088 /* XXX dir-spec not followed? See #20272. If it isn't closed, then this is
2089 * testing current behavior, not spec. */
2093 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2095 tt_int_op(ret
, OP_EQ
, 1);
2096 tt_int_op(smartlist_len(chunks
), OP_EQ
, 1);
2097 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=3333 "
2098 "Wbe=3000 Wbg=3000 Wbm=10000 Wdb=10000 Web=10000 Wed=3333 Wee=7000 "
2099 "Weg=3333 Wem=7000 Wgb=10000 Wgd=3333 Wgg=7000 Wgm=7000 Wmb=10000 "
2100 "Wmd=3333 Wme=3000 Wmg=3000 Wmm=10000\n");
2101 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2102 smartlist_clear(chunks
);
2104 /* case 2a E scarce */
2109 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2111 tt_int_op(ret
, OP_EQ
, 1);
2112 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 Wbe=0 "
2113 "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
2114 "Wem=10000 Wgb=10000 Wgd=0 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 Wme=0 "
2115 "Wmg=0 Wmm=10000\n");
2116 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2117 smartlist_clear(chunks
);
2119 /* case 2a G scarce */
2124 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2126 tt_int_op(ret
, OP_EQ
, 1);
2127 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 Wbe=0 "
2128 "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=10000 Weg=0 Wem=10000 "
2129 "Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 Wme=0 Wmg=0 "
2131 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2132 smartlist_clear(chunks
);
2134 /* case 2b1 (Wgg=1, Wmd=Wgd) */
2140 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2142 tt_int_op(ret
, OP_EQ
, 1);
2143 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=4000 "
2144 "Wbe=0 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=2000 Wee=10000 Weg=2000 "
2145 "Wem=10000 Wgb=10000 Wgd=4000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=4000 "
2146 "Wme=0 Wmg=0 Wmm=10000\n");
2147 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2148 smartlist_clear(chunks
);
2156 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2158 tt_int_op(ret
, OP_EQ
, 1);
2159 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=666 Wbe=0 "
2160 "Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=3666 Wee=10000 Weg=3666 "
2161 "Wem=10000 Wgb=10000 Wgd=5668 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=666 "
2162 "Wme=0 Wmg=0 Wmm=10000\n");
2163 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2164 smartlist_clear(chunks
);
2167 /* XXX I can't get a combination of values that hits this case without error,
2168 * so this just tests that it fails. See #20285. Also see #20284 as 2b3 does
2169 * not follow dir-spec. */
2170 /* (E < T/3 && G < T/3) && (E+D>=G || G+D>=E) && (M > T/3) */
2176 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2178 tt_int_op(ret
, OP_EQ
, 0);
2179 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2180 smartlist_clear(chunks
);
2182 /* case 3a G scarce */
2188 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2190 tt_int_op(ret
, OP_EQ
, 1);
2191 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 "
2192 "Wbe=3333 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=6667 Weg=0 "
2193 "Wem=6667 Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 "
2194 "Wme=3333 Wmg=0 Wmm=10000\n");
2195 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2196 smartlist_clear(chunks
);
2198 /* case 3a E scarce */
2204 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2206 tt_int_op(ret
, OP_EQ
, 1);
2207 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 Wbe=0 "
2208 "Wbg=3333 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
2209 "Wem=10000 Wgb=10000 Wgd=0 Wgg=6667 Wgm=6667 Wmb=10000 Wmd=0 Wme=0 "
2210 "Wmg=3333 Wmm=10000\n");
2211 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2212 smartlist_clear(chunks
);
2220 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2222 tt_int_op(ret
, OP_EQ
, 1);
2223 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 "
2224 "Wbe=3334 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=0 Wee=6666 Weg=0 "
2225 "Wem=6666 Wgb=10000 Wgd=10000 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=0 "
2226 "Wme=3334 Wmg=0 Wmm=10000\n");
2227 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2228 smartlist_clear(chunks
);
2236 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2238 tt_int_op(ret
, OP_EQ
, 1);
2239 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 Wbe=0 "
2240 "Wbg=3334 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
2241 "Wem=10000 Wgb=10000 Wgd=0 Wgg=6666 Wgm=6666 Wmb=10000 Wmd=0 Wme=0 "
2242 "Wmg=3334 Wmm=10000\n");
2243 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2244 smartlist_clear(chunks
);
2246 /* case from 21 Jul 2013 (3be) */
2252 tt_i64_op(G
+M
+E
+D
, OP_EQ
, T
);
2253 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2256 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=883 Wbe=0 "
2257 "Wbg=3673 Wbm=10000 Wdb=10000 Web=10000 Wed=8233 Wee=10000 Weg=8233 "
2258 "Wem=10000 Wgb=10000 Wgd=883 Wgg=6327 Wgm=6327 Wmb=10000 Wmd=883 Wme=0 "
2259 "Wmg=3673 Wmm=10000\n");
2260 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2261 smartlist_clear(chunks
);
2263 /* case from 04 Oct 2016 (3a E scarce) */
2269 tt_i64_op(G
+M
+E
+D
, OP_EQ
, T
);
2270 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2273 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=0 Wbe=0 "
2274 "Wbg=4194 Wbm=10000 Wdb=10000 Web=10000 Wed=10000 Wee=10000 Weg=10000 "
2275 "Wem=10000 Wgb=10000 Wgd=0 Wgg=5806 Wgm=5806 Wmb=10000 Wmd=0 Wme=0 "
2276 "Wmg=4194 Wmm=10000\n");
2277 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2278 smartlist_clear(chunks
);
2280 /* case from 04 Sep 2013 (2b1) */
2286 tt_i64_op(G
+M
+E
+D
, OP_EQ
, T
);
2287 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2290 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=317 "
2291 "Wbe=5938 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=9366 Wee=4061 "
2292 "Weg=9366 Wem=4061 Wgb=10000 Wgd=317 Wgg=10000 Wgm=10000 Wmb=10000 "
2293 "Wmd=317 Wme=5938 Wmg=0 Wmm=10000\n");
2294 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2295 smartlist_clear(chunks
);
2297 /* explicitly test initializing weights to 1*/
2303 tt_i64_op(G
+M
+E
+D
, OP_EQ
, T
);
2304 ret
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
, T
,
2306 tt_str_op(smartlist_get(chunks
, 0), OP_EQ
, "bandwidth-weights Wbd=3333 "
2307 "Wbe=0 Wbg=0 Wbm=10000 Wdb=10000 Web=10000 Wed=3333 Wee=10000 Weg=3333 "
2308 "Wem=10000 Wgb=10000 Wgd=3333 Wgg=10000 Wgm=10000 Wmb=10000 Wmd=3333 "
2309 "Wme=0 Wmg=0 Wmm=10000\n");
2313 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2314 smartlist_free(chunks
);
2317 static authority_cert_t
*mock_cert
;
2319 static authority_cert_t
*
2320 get_my_v3_authority_cert_m(void)
2322 tor_assert(mock_cert
);
2326 /** Run a unit tests for generating and parsing networkstatuses, with
2327 * the supply test fns. */
2329 test_a_networkstatus(
2330 vote_routerstatus_t
* (*vrs_gen
)(int idx
, time_t now
),
2331 int (*vote_tweaks
)(networkstatus_t
*v
, int voter
, time_t now
),
2332 void (*vrs_test
)(vote_routerstatus_t
*vrs
, int voter
, time_t now
),
2333 void (*consensus_test
)(networkstatus_t
*con
, time_t now
),
2334 void (*rs_test
)(routerstatus_t
*rs
, time_t now
))
2336 authority_cert_t
*cert1
=NULL
, *cert2
=NULL
, *cert3
=NULL
;
2337 crypto_pk_t
*sign_skey_1
=NULL
, *sign_skey_2
=NULL
, *sign_skey_3
=NULL
;
2338 crypto_pk_t
*sign_skey_leg1
=NULL
;
2340 * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
2341 * returns non-zero, it changed net_params and we should skip the tests for
2342 * that later as they will fail.
2344 int params_tweaked
= 0;
2346 time_t now
= time(NULL
);
2347 networkstatus_voter_info_t
*voter
;
2348 document_signature_t
*sig
;
2349 networkstatus_t
*vote
=NULL
, *v1
=NULL
, *v2
=NULL
, *v3
=NULL
, *con
=NULL
,
2351 vote_routerstatus_t
*vrs
;
2353 int idx
, n_rs
, n_vrs
;
2354 char *consensus_text
=NULL
, *cp
=NULL
;
2355 smartlist_t
*votes
= smartlist_new();
2357 /* For generating the two other consensuses. */
2358 char *detached_text1
=NULL
, *detached_text2
=NULL
;
2359 char *consensus_text2
=NULL
, *consensus_text3
=NULL
;
2360 char *consensus_text_md2
=NULL
, *consensus_text_md3
=NULL
;
2361 char *consensus_text_md
=NULL
;
2362 networkstatus_t
*con2
=NULL
, *con_md2
=NULL
, *con3
=NULL
, *con_md3
=NULL
;
2363 ns_detached_signatures_t
*dsig1
=NULL
, *dsig2
=NULL
;
2367 tt_assert(vrs_test
);
2369 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
2371 /* Parse certificates and keys. */
2372 cert1
= mock_cert
= authority_cert_parse_from_string(AUTHORITY_CERT_1
, NULL
);
2374 cert2
= authority_cert_parse_from_string(AUTHORITY_CERT_2
, NULL
);
2376 cert3
= authority_cert_parse_from_string(AUTHORITY_CERT_3
, NULL
);
2378 sign_skey_1
= crypto_pk_new();
2379 sign_skey_2
= crypto_pk_new();
2380 sign_skey_3
= crypto_pk_new();
2381 sign_skey_leg1
= pk_generate(4);
2382 dirvote_recalculate_timing(get_options(), now
);
2383 sr_state_init(0, 0);
2385 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1
,
2386 AUTHORITY_SIGNKEY_1
, -1));
2387 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_2
,
2388 AUTHORITY_SIGNKEY_2
, -1));
2389 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_3
,
2390 AUTHORITY_SIGNKEY_3
, -1));
2392 tt_assert(!crypto_pk_cmp_keys(sign_skey_1
, cert1
->signing_key
));
2393 tt_assert(!crypto_pk_cmp_keys(sign_skey_2
, cert2
->signing_key
));
2395 tt_assert(!dir_common_construct_vote_1(&vote
, cert1
, sign_skey_1
, vrs_gen
,
2396 &v1
, &n_vrs
, now
, 1));
2399 /* Make sure the parsed thing was right. */
2400 tt_int_op(v1
->type
,OP_EQ
, NS_TYPE_VOTE
);
2401 tt_int_op(v1
->published
,OP_EQ
, vote
->published
);
2402 tt_int_op(v1
->valid_after
,OP_EQ
, vote
->valid_after
);
2403 tt_int_op(v1
->fresh_until
,OP_EQ
, vote
->fresh_until
);
2404 tt_int_op(v1
->valid_until
,OP_EQ
, vote
->valid_until
);
2405 tt_int_op(v1
->vote_seconds
,OP_EQ
, vote
->vote_seconds
);
2406 tt_int_op(v1
->dist_seconds
,OP_EQ
, vote
->dist_seconds
);
2407 tt_str_op(v1
->client_versions
,OP_EQ
, vote
->client_versions
);
2408 tt_str_op(v1
->server_versions
,OP_EQ
, vote
->server_versions
);
2409 tt_assert(v1
->voters
&& smartlist_len(v1
->voters
));
2410 voter
= smartlist_get(v1
->voters
, 0);
2411 tt_str_op(voter
->nickname
,OP_EQ
, "Voter1");
2412 tt_str_op(voter
->address
,OP_EQ
, "1.2.3.4");
2413 tt_int_op(voter
->addr
,OP_EQ
, 0x01020304);
2414 tt_int_op(voter
->dir_port
,OP_EQ
, 80);
2415 tt_int_op(voter
->or_port
,OP_EQ
, 9000);
2416 tt_str_op(voter
->contact
,OP_EQ
, "voter@example.com");
2417 tt_assert(v1
->cert
);
2418 tt_assert(!crypto_pk_cmp_keys(sign_skey_1
, v1
->cert
->signing_key
));
2419 cp
= smartlist_join_strings(v1
->known_flags
, ":", 0, NULL
);
2420 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
2422 tt_int_op(smartlist_len(v1
->routerstatus_list
),OP_EQ
, n_vrs
);
2423 networkstatus_vote_free(vote
);
2426 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v1
, 1, now
);
2428 /* Check the routerstatuses. */
2429 for (idx
= 0; idx
< n_vrs
; ++idx
) {
2430 vrs
= smartlist_get(v1
->routerstatus_list
, idx
);
2432 vrs_test(vrs
, 1, now
);
2435 /* Generate second vote. It disagrees on some of the times,
2436 * and doesn't list versions, and knows some crazy flags.
2437 * Generate and parse v2. */
2438 tt_assert(!dir_common_construct_vote_2(&vote
, cert2
, sign_skey_2
, vrs_gen
,
2439 &v2
, &n_vrs
, now
, 1));
2442 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v2
, 2, now
);
2444 /* Check that flags come out right.*/
2445 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2446 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2447 "Running:Stable:V2Dir:Valid");
2450 /* Check the routerstatuses. */
2451 n_vrs
= smartlist_len(v2
->routerstatus_list
);
2452 for (idx
= 0; idx
< n_vrs
; ++idx
) {
2453 vrs
= smartlist_get(v2
->routerstatus_list
, idx
);
2455 vrs_test(vrs
, 2, now
);
2457 networkstatus_vote_free(vote
);
2460 /* Generate the third vote with a legacy id. */
2461 tt_assert(!dir_common_construct_vote_3(&vote
, cert3
, sign_skey_3
, vrs_gen
,
2462 &v3
, &n_vrs
, now
, 1));
2465 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v3
, 3, now
);
2467 /* Compute a consensus as voter 3. */
2468 smartlist_add(votes
, v3
);
2469 smartlist_add(votes
, v1
);
2470 smartlist_add(votes
, v2
);
2471 consensus_text
= networkstatus_compute_consensus(votes
, 3,
2472 cert3
->identity_key
,
2474 "AAAAAAAAAAAAAAAAAAAA",
2477 tt_assert(consensus_text
);
2478 con
= networkstatus_parse_vote_from_string(consensus_text
, NULL
,
2481 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
2482 // v1_text, v2_text, v3_text);
2483 consensus_text_md
= networkstatus_compute_consensus(votes
, 3,
2484 cert3
->identity_key
,
2486 "AAAAAAAAAAAAAAAAAAAA",
2489 tt_assert(consensus_text_md
);
2490 con_md
= networkstatus_parse_vote_from_string(consensus_text_md
, NULL
,
2493 tt_int_op(con_md
->flavor
,OP_EQ
, FLAV_MICRODESC
);
2495 /* Check consensus contents. */
2496 tt_assert(con
->type
== NS_TYPE_CONSENSUS
);
2497 tt_int_op(con
->published
,OP_EQ
, 0); /* this field only appears in votes. */
2498 tt_int_op(con
->valid_after
,OP_EQ
, now
+1000);
2499 tt_int_op(con
->fresh_until
,OP_EQ
, now
+2003); /* median */
2500 tt_int_op(con
->valid_until
,OP_EQ
, now
+3000);
2501 tt_int_op(con
->vote_seconds
,OP_EQ
, 100);
2502 tt_int_op(con
->dist_seconds
,OP_EQ
, 250); /* median */
2503 tt_str_op(con
->client_versions
,OP_EQ
, "0.1.2.14");
2504 tt_str_op(con
->server_versions
,OP_EQ
, "0.1.2.15,0.1.2.16");
2505 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2506 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2507 "Running:Stable:V2Dir:Valid");
2509 if (!params_tweaked
) {
2510 /* Skip this one if vote_tweaks() messed with the param lists */
2511 cp
= smartlist_join_strings(con
->net_params
, ":", 0, NULL
);
2512 tt_str_op(cp
,OP_EQ
, "circuitwindow=80:foo=660");
2516 tt_int_op(4,OP_EQ
, smartlist_len(con
->voters
)); /*3 voters, 1 legacy key.*/
2517 /* The voter id digests should be in this order. */
2518 tt_assert(fast_memcmp(cert2
->cache_info
.identity_digest
,
2519 cert1
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2520 tt_assert(fast_memcmp(cert1
->cache_info
.identity_digest
,
2521 cert3
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2522 test_same_voter(smartlist_get(con
->voters
, 1),
2523 smartlist_get(v2
->voters
, 0));
2524 test_same_voter(smartlist_get(con
->voters
, 2),
2525 smartlist_get(v1
->voters
, 0));
2526 test_same_voter(smartlist_get(con
->voters
, 3),
2527 smartlist_get(v3
->voters
, 0));
2529 consensus_test(con
, now
);
2531 /* Check the routerstatuses. */
2532 n_rs
= smartlist_len(con
->routerstatus_list
);
2534 for (idx
= 0; idx
< n_rs
; ++idx
) {
2535 rs
= smartlist_get(con
->routerstatus_list
, idx
);
2540 n_rs
= smartlist_len(con_md
->routerstatus_list
);
2542 for (idx
= 0; idx
< n_rs
; ++idx
) {
2543 rs
= smartlist_get(con_md
->routerstatus_list
, idx
);
2547 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
2548 * The second one hasn't signed. The fourth one has signed: validate it. */
2549 voter
= smartlist_get(con
->voters
, 1);
2550 tt_int_op(smartlist_len(voter
->sigs
),OP_EQ
, 0);
2552 voter
= smartlist_get(con
->voters
, 3);
2553 tt_int_op(smartlist_len(voter
->sigs
),OP_EQ
, 1);
2554 sig
= smartlist_get(voter
->sigs
, 0);
2555 tt_assert(sig
->signature
);
2556 tt_assert(!sig
->good_signature
);
2557 tt_assert(!sig
->bad_signature
);
2559 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert3
));
2560 tt_assert(sig
->signature
);
2561 tt_assert(sig
->good_signature
);
2562 tt_assert(!sig
->bad_signature
);
2565 const char *msg
=NULL
;
2566 /* Compute the other two signed consensuses. */
2567 smartlist_shuffle(votes
);
2568 consensus_text2
= networkstatus_compute_consensus(votes
, 3,
2569 cert2
->identity_key
,
2570 sign_skey_2
, NULL
,NULL
,
2572 consensus_text_md2
= networkstatus_compute_consensus(votes
, 3,
2573 cert2
->identity_key
,
2574 sign_skey_2
, NULL
,NULL
,
2576 smartlist_shuffle(votes
);
2577 consensus_text3
= networkstatus_compute_consensus(votes
, 3,
2578 cert1
->identity_key
,
2579 sign_skey_1
, NULL
,NULL
,
2581 consensus_text_md3
= networkstatus_compute_consensus(votes
, 3,
2582 cert1
->identity_key
,
2583 sign_skey_1
, NULL
,NULL
,
2585 tt_assert(consensus_text2
);
2586 tt_assert(consensus_text3
);
2587 tt_assert(consensus_text_md2
);
2588 tt_assert(consensus_text_md3
);
2589 con2
= networkstatus_parse_vote_from_string(consensus_text2
, NULL
,
2591 con3
= networkstatus_parse_vote_from_string(consensus_text3
, NULL
,
2593 con_md2
= networkstatus_parse_vote_from_string(consensus_text_md2
, NULL
,
2595 con_md3
= networkstatus_parse_vote_from_string(consensus_text_md3
, NULL
,
2602 /* All three should have the same digest. */
2603 tt_mem_op(&con
->digests
,OP_EQ
, &con2
->digests
, sizeof(common_digests_t
));
2604 tt_mem_op(&con
->digests
,OP_EQ
, &con3
->digests
, sizeof(common_digests_t
));
2606 tt_mem_op(&con_md
->digests
,OP_EQ
, &con_md2
->digests
,
2607 sizeof(common_digests_t
));
2608 tt_mem_op(&con_md
->digests
,OP_EQ
, &con_md3
->digests
,
2609 sizeof(common_digests_t
));
2611 /* Extract a detached signature from con3. */
2612 detached_text1
= get_detached_sigs(con3
, con_md3
);
2613 tt_assert(detached_text1
);
2614 /* Try to parse it. */
2615 dsig1
= networkstatus_parse_detached_signatures(detached_text1
, NULL
);
2618 /* Are parsed values as expected? */
2619 tt_int_op(dsig1
->valid_after
,OP_EQ
, con3
->valid_after
);
2620 tt_int_op(dsig1
->fresh_until
,OP_EQ
, con3
->fresh_until
);
2621 tt_int_op(dsig1
->valid_until
,OP_EQ
, con3
->valid_until
);
2623 common_digests_t
*dsig_digests
= strmap_get(dsig1
->digests
, "ns");
2624 tt_assert(dsig_digests
);
2625 tt_mem_op(dsig_digests
->d
[DIGEST_SHA1
], OP_EQ
,
2626 con3
->digests
.d
[DIGEST_SHA1
], DIGEST_LEN
);
2627 dsig_digests
= strmap_get(dsig1
->digests
, "microdesc");
2628 tt_assert(dsig_digests
);
2629 tt_mem_op(dsig_digests
->d
[DIGEST_SHA256
],OP_EQ
,
2630 con_md3
->digests
.d
[DIGEST_SHA256
],
2634 smartlist_t
*dsig_signatures
= strmap_get(dsig1
->signatures
, "ns");
2635 tt_assert(dsig_signatures
);
2636 tt_int_op(1,OP_EQ
, smartlist_len(dsig_signatures
));
2637 sig
= smartlist_get(dsig_signatures
, 0);
2638 tt_mem_op(sig
->identity_digest
,OP_EQ
, cert1
->cache_info
.identity_digest
,
2640 tt_int_op(sig
->alg
,OP_EQ
, DIGEST_SHA1
);
2642 dsig_signatures
= strmap_get(dsig1
->signatures
, "microdesc");
2643 tt_assert(dsig_signatures
);
2644 tt_int_op(1,OP_EQ
, smartlist_len(dsig_signatures
));
2645 sig
= smartlist_get(dsig_signatures
, 0);
2646 tt_mem_op(sig
->identity_digest
,OP_EQ
, cert1
->cache_info
.identity_digest
,
2648 tt_int_op(sig
->alg
,OP_EQ
, DIGEST_SHA256
);
2651 /* Try adding it to con2. */
2652 detached_text2
= get_detached_sigs(con2
,con_md2
);
2653 tt_int_op(1,OP_EQ
, networkstatus_add_detached_signatures(con2
, dsig1
,
2654 "test", LOG_INFO
, &msg
));
2655 tor_free(detached_text2
);
2657 networkstatus_add_detached_signatures(con_md2
, dsig1
, "test",
2659 tor_free(detached_text2
);
2660 detached_text2
= get_detached_sigs(con2
,con_md2
);
2661 //printf("\n<%s>\n", detached_text2);
2662 dsig2
= networkstatus_parse_detached_signatures(detached_text2
, NULL
);
2666 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2668 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2673 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
, "ns")));
2675 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
,
2678 /* Try adding to con2 twice; verify that nothing changes. */
2679 tt_int_op(0,OP_EQ
, networkstatus_add_detached_signatures(con2
, dsig1
,
2680 "test", LOG_INFO
, &msg
));
2683 tt_int_op(2,OP_EQ
, networkstatus_add_detached_signatures(con
, dsig2
,
2684 "test", LOG_INFO
, &msg
));
2685 /* Check signatures */
2686 voter
= smartlist_get(con
->voters
, 1);
2687 sig
= smartlist_get(voter
->sigs
, 0);
2689 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert2
));
2690 voter
= smartlist_get(con
->voters
, 2);
2691 sig
= smartlist_get(voter
->sigs
, 0);
2693 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert1
));
2698 smartlist_free(votes
);
2699 tor_free(consensus_text
);
2700 tor_free(consensus_text_md
);
2702 networkstatus_vote_free(vote
);
2703 networkstatus_vote_free(v1
);
2704 networkstatus_vote_free(v2
);
2705 networkstatus_vote_free(v3
);
2706 networkstatus_vote_free(con
);
2707 networkstatus_vote_free(con_md
);
2708 crypto_pk_free(sign_skey_1
);
2709 crypto_pk_free(sign_skey_2
);
2710 crypto_pk_free(sign_skey_3
);
2711 crypto_pk_free(sign_skey_leg1
);
2712 authority_cert_free(cert1
);
2713 authority_cert_free(cert2
);
2714 authority_cert_free(cert3
);
2716 tor_free(consensus_text2
);
2717 tor_free(consensus_text3
);
2718 tor_free(consensus_text_md2
);
2719 tor_free(consensus_text_md3
);
2720 tor_free(detached_text1
);
2721 tor_free(detached_text2
);
2723 networkstatus_vote_free(con2
);
2724 networkstatus_vote_free(con3
);
2725 networkstatus_vote_free(con_md2
);
2726 networkstatus_vote_free(con_md3
);
2727 ns_detached_signatures_free(dsig1
);
2728 ns_detached_signatures_free(dsig2
);
2731 /** Run unit tests for generating and parsing V3 consensus networkstatus
2734 test_dir_v3_networkstatus(void *arg
)
2737 test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns
,
2738 vote_tweaks_for_v3ns
,
2740 test_consensus_for_v3ns
,
2741 test_routerstatus_for_v3ns
);
2745 test_dir_scale_bw(void *testdata
)
2747 double v
[8] = { 2.0/3,
2756 uint64_t vals_u64
[8];
2765 scale_array_elements_to_u64(vals_u64
, vals_dbl
, 8, &total
);
2767 tt_int_op((int)total
, OP_EQ
, 48);
2769 for (i
=0; i
<8; ++i
) {
2770 total
+= vals_u64
[i
];
2772 tt_assert(total
>= (U64_LITERAL(1)<<60));
2773 tt_assert(total
<= (U64_LITERAL(1)<<62));
2775 for (i
=0; i
<8; ++i
) {
2776 /* vals[2].u64 is the scaled value of 1.0 */
2777 double ratio
= ((double)vals_u64
[i
]) / vals_u64
[2];
2778 tt_double_op(fabs(ratio
- v
[i
]), OP_LT
, .00001);
2781 /* test handling of no entries */
2783 scale_array_elements_to_u64(vals_u64
, vals_dbl
, 0, &total
);
2784 tt_assert(total
== 0);
2786 /* make sure we don't read the array when we have no entries
2787 * may require compiler flags to catch NULL dereferences */
2789 scale_array_elements_to_u64(NULL
, NULL
, 0, &total
);
2790 tt_assert(total
== 0);
2792 scale_array_elements_to_u64(NULL
, NULL
, 0, NULL
);
2794 /* test handling of zero totals */
2797 scale_array_elements_to_u64(vals_u64
, vals_dbl
, 1, &total
);
2798 tt_assert(total
== 0);
2799 tt_assert(vals_u64
[0] == 0);
2803 scale_array_elements_to_u64(vals_u64
, vals_dbl
, 2, NULL
);
2804 tt_assert(vals_u64
[0] == 0);
2805 tt_assert(vals_u64
[1] == 0);
2812 test_dir_random_weighted(void *testdata
)
2815 uint64_t vals
[10] = {3,1,2,4,6,0,7,5,8,9}, total
=0;
2816 uint64_t inp_u64
[10];
2818 const int n
= 50000;
2819 double max_sq_error
;
2822 /* Try a ten-element array with values from 0 through 10. The values are
2823 * in a scrambled order to make sure we don't depend on order. */
2824 memset(histogram
,0,sizeof(histogram
));
2825 for (i
=0; i
<10; ++i
) {
2826 inp_u64
[i
] = vals
[i
];
2829 tt_u64_op(total
, OP_EQ
, 45);
2830 for (i
=0; i
<n
; ++i
) {
2831 choice
= choose_array_element_by_weight(inp_u64
, 10);
2832 tt_int_op(choice
, OP_GE
, 0);
2833 tt_int_op(choice
, OP_LT
, 10);
2834 histogram
[choice
]++;
2837 /* Now see if we chose things about frequently enough. */
2839 for (i
=0; i
<10; ++i
) {
2840 int expected
= (int)(n
*vals
[i
]/total
);
2841 double frac_diff
= 0, sq
;
2842 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals
[i
], histogram
[i
], expected
));
2844 frac_diff
= (histogram
[i
] - expected
) / ((double)expected
);
2846 tt_int_op(histogram
[i
], OP_EQ
, 0);
2848 sq
= frac_diff
* frac_diff
;
2849 if (sq
> max_sq_error
)
2852 /* It should almost always be much much less than this. If you want to
2853 * figure out the odds, please feel free. */
2854 tt_double_op(max_sq_error
, OP_LT
, .05);
2856 /* Now try a singleton; do we choose it? */
2857 for (i
= 0; i
< 100; ++i
) {
2858 choice
= choose_array_element_by_weight(inp_u64
, 1);
2859 tt_int_op(choice
, OP_EQ
, 0);
2862 /* Now try an array of zeros. We should choose randomly. */
2863 memset(histogram
,0,sizeof(histogram
));
2864 for (i
= 0; i
< 5; ++i
)
2866 for (i
= 0; i
< n
; ++i
) {
2867 choice
= choose_array_element_by_weight(inp_u64
, 5);
2868 tt_int_op(choice
, OP_GE
, 0);
2869 tt_int_op(choice
, OP_LT
, 5);
2870 histogram
[choice
]++;
2872 /* Now see if we chose things about frequently enough. */
2874 for (i
=0; i
<5; ++i
) {
2876 double frac_diff
= 0, sq
;
2877 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals
[i
], histogram
[i
], expected
));
2878 frac_diff
= (histogram
[i
] - expected
) / ((double)expected
);
2879 sq
= frac_diff
* frac_diff
;
2880 if (sq
> max_sq_error
)
2883 /* It should almost always be much much less than this. If you want to
2884 * figure out the odds, please feel free. */
2885 tt_double_op(max_sq_error
, OP_LT
, .05);
2890 /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
2892 static uint32_t alternate_clip_bw
= 0;
2895 * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
2896 * v3_networkstatus ones.
2898 static vote_routerstatus_t
*
2899 gen_routerstatus_for_umbw(int idx
, time_t now
)
2901 vote_routerstatus_t
*vrs
= NULL
;
2903 tor_addr_t addr_ipv6
;
2904 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
2905 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
2909 /* Generate the first routerstatus. */
2910 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2912 vrs
->version
= tor_strdup("0.1.2.14");
2913 rs
->published_on
= now
-1500;
2914 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
2915 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
2916 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2917 rs
->addr
= 0x99008801;
2919 rs
->dir_port
= 8000;
2920 /* all flags but running cleared */
2921 rs
->is_flagged_running
= 1;
2923 * This one has measured bandwidth below the clip cutoff, and
2924 * so shouldn't be clipped; we'll have to test that it isn't
2927 vrs
->has_measured_bw
= 1;
2928 rs
->has_bandwidth
= 1;
2929 vrs
->measured_bw_kb
= rs
->bandwidth_kb
= max_unmeasured_bw_kb
/ 2;
2932 /* Generate the second routerstatus. */
2933 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2935 vrs
->version
= tor_strdup("0.2.0.5");
2936 rs
->published_on
= now
-1000;
2937 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
2938 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
2939 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
2940 rs
->addr
= 0x99009901;
2943 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
2944 tor_addr_copy(&rs
->ipv6_addr
, &addr_ipv6
);
2945 rs
->ipv6_orport
= 4711;
2946 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_flagged_running
=
2947 rs
->is_valid
= rs
->is_possible_guard
= 1;
2949 * This one has measured bandwidth above the clip cutoff, and
2950 * so shouldn't be clipped; we'll have to test that it isn't
2953 vrs
->has_measured_bw
= 1;
2954 rs
->has_bandwidth
= 1;
2955 vrs
->measured_bw_kb
= rs
->bandwidth_kb
= 2 * max_unmeasured_bw_kb
;
2958 /* Generate the third routerstatus. */
2959 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2961 vrs
->version
= tor_strdup("0.1.0.3");
2962 rs
->published_on
= now
-1000;
2963 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
2964 memset(rs
->identity_digest
, 0x33, DIGEST_LEN
);
2965 memset(rs
->descriptor_digest
, 79, DIGEST_LEN
);
2966 rs
->addr
= 0xAA009901;
2968 rs
->dir_port
= 9999;
2969 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
2970 rs
->is_flagged_running
= rs
->is_valid
=
2971 rs
->is_possible_guard
= 1;
2973 * This one has unmeasured bandwidth above the clip cutoff, and
2974 * so should be clipped; we'll have to test that it isn't
2977 vrs
->has_measured_bw
= 0;
2978 rs
->has_bandwidth
= 1;
2979 vrs
->measured_bw_kb
= 0;
2980 rs
->bandwidth_kb
= 2 * max_unmeasured_bw_kb
;
2983 /* Generate a fourth routerstatus that is not running. */
2984 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2986 vrs
->version
= tor_strdup("0.1.6.3");
2987 rs
->published_on
= now
-1000;
2988 strlcpy(rs
->nickname
, "router4", sizeof(rs
->nickname
));
2989 memset(rs
->identity_digest
, 0x34, DIGEST_LEN
);
2990 memset(rs
->descriptor_digest
, 47, DIGEST_LEN
);
2991 rs
->addr
= 0xC0000203;
2993 rs
->dir_port
= 1999;
2994 /* all flags but running cleared */
2995 rs
->is_flagged_running
= 1;
2997 * This one has unmeasured bandwidth below the clip cutoff, and
2998 * so shouldn't be clipped; we'll have to test that it isn't
3001 vrs
->has_measured_bw
= 0;
3002 rs
->has_bandwidth
= 1;
3003 vrs
->measured_bw_kb
= 0;
3004 rs
->bandwidth_kb
= max_unmeasured_bw_kb
/ 2;
3007 /* No more for this test; return NULL */
3011 /* Shouldn't happen */
3015 vrs
->microdesc
= tor_malloc_zero(sizeof(vote_microdesc_hash_t
));
3016 tor_asprintf(&vrs
->microdesc
->microdesc_hash_line
,
3017 "m 9,10,11,12,13,14,15,16,17 "
3018 "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
3026 /** Apply tweaks to the vote list for each voter; for the umbw test this is
3027 * just adding the right consensus methods to let clipping happen */
3029 vote_tweaks_for_umbw(networkstatus_t
*v
, int voter
, time_t now
)
3031 char *maxbw_param
= NULL
;
3038 tt_assert(v
->supported_methods
);
3039 SMARTLIST_FOREACH(v
->supported_methods
, char *, c
, tor_free(c
));
3040 smartlist_clear(v
->supported_methods
);
3041 /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
3042 smartlist_split_string(v
->supported_methods
,
3043 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
3045 /* If we're using a non-default clip bandwidth, add it to net_params */
3046 if (alternate_clip_bw
> 0) {
3047 tor_asprintf(&maxbw_param
, "maxunmeasuredbw=%u", alternate_clip_bw
);
3048 tt_assert(maxbw_param
);
3050 smartlist_add(v
->net_params
, maxbw_param
);
3060 * Test a parsed vote_routerstatus_t for umbw test.
3063 test_vrs_for_umbw(vote_routerstatus_t
*vrs
, int voter
, time_t now
)
3066 tor_addr_t addr_ipv6
;
3067 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
3068 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
3072 rs
= &(vrs
->status
);
3075 /* Split out by digests to test */
3076 if (tor_memeq(rs
->identity_digest
,
3077 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
3078 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
3081 * Check the first routerstatus - measured bandwidth below the clip
3084 tt_str_op(vrs
->version
,OP_EQ
, "0.1.2.14");
3085 tt_int_op(rs
->published_on
,OP_EQ
, now
-1500);
3086 tt_str_op(rs
->nickname
,OP_EQ
, "router2");
3087 tt_mem_op(rs
->identity_digest
,OP_EQ
,
3088 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
3089 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
3091 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
3092 tt_int_op(rs
->addr
,OP_EQ
, 0x99008801);
3093 tt_int_op(rs
->or_port
,OP_EQ
, 443);
3094 tt_int_op(rs
->dir_port
,OP_EQ
, 8000);
3095 tt_assert(rs
->has_bandwidth
);
3096 tt_assert(vrs
->has_measured_bw
);
3097 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
3098 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
3099 } else if (tor_memeq(rs
->identity_digest
,
3100 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
3101 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
3105 * Check the second routerstatus - measured bandwidth above the clip
3108 tt_str_op(vrs
->version
,OP_EQ
, "0.2.0.5");
3109 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
3110 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
3111 tt_mem_op(rs
->identity_digest
,OP_EQ
,
3112 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
3113 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
3115 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
3116 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
3117 tt_int_op(rs
->or_port
,OP_EQ
, 443);
3118 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
3119 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
3120 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
3121 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
3122 tt_assert(rs
->has_bandwidth
);
3123 tt_assert(vrs
->has_measured_bw
);
3124 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
3125 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
3126 } else if (tor_memeq(rs
->identity_digest
,
3127 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
3128 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
3131 * Check the third routerstatus - unmeasured bandwidth above the clip
3132 * cutoff; this one should be clipped later on in the consensus, but
3133 * appears unclipped in the vote.
3135 tt_assert(rs
->has_bandwidth
);
3136 tt_assert(!(vrs
->has_measured_bw
));
3137 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
3138 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, 0);
3139 } else if (tor_memeq(rs
->identity_digest
,
3140 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
3141 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
3144 * Check the fourth routerstatus - unmeasured bandwidth below the clip
3145 * cutoff; this one should not be clipped.
3147 tt_assert(rs
->has_bandwidth
);
3148 tt_assert(!(vrs
->has_measured_bw
));
3149 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
3150 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, 0);
3160 * Test a consensus for v3_networkstatus_test
3163 test_consensus_for_umbw(networkstatus_t
*con
, time_t now
)
3168 tt_ptr_op(con
->cert
, OP_EQ
, NULL
);
3169 // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
3170 tt_int_op(con
->consensus_method
, OP_GE
, 16);
3171 tt_int_op(4,OP_EQ
, smartlist_len(con
->routerstatus_list
));
3172 /* There should be four listed routers; all voters saw the same in this */
3179 * Test a router list entry for umbw test
3182 test_routerstatus_for_umbw(routerstatus_t
*rs
, time_t now
)
3184 tor_addr_t addr_ipv6
;
3185 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
3186 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
3190 /* There should be four listed routers, as constructed above */
3191 if (tor_memeq(rs
->identity_digest
,
3192 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
3193 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
3195 tt_mem_op(rs
->identity_digest
,OP_EQ
,
3196 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
3197 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
3199 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
3200 tt_assert(!rs
->is_authority
);
3201 tt_assert(!rs
->is_exit
);
3202 tt_assert(!rs
->is_fast
);
3203 tt_assert(!rs
->is_possible_guard
);
3204 tt_assert(!rs
->is_stable
);
3205 /* (If it wasn't running it wouldn't be here) */
3206 tt_assert(rs
->is_flagged_running
);
3207 tt_assert(!rs
->is_valid
);
3208 tt_assert(!rs
->is_named
);
3209 /* This one should have measured bandwidth below the clip cutoff */
3210 tt_assert(rs
->has_bandwidth
);
3211 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
3212 tt_assert(!(rs
->bw_is_unmeasured
));
3213 } else if (tor_memeq(rs
->identity_digest
,
3214 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
3215 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
3217 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
3218 tt_mem_op(rs
->identity_digest
,OP_EQ
,
3219 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
3220 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
3222 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
3223 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
3224 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
3225 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
3226 tt_int_op(rs
->or_port
,OP_EQ
, 443);
3227 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
3228 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
3229 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
3230 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
3231 tt_assert(!rs
->is_authority
);
3232 tt_assert(rs
->is_exit
);
3233 tt_assert(rs
->is_fast
);
3234 tt_assert(rs
->is_possible_guard
);
3235 tt_assert(rs
->is_stable
);
3236 tt_assert(rs
->is_flagged_running
);
3237 tt_assert(rs
->is_valid
);
3238 tt_assert(!rs
->is_named
);
3239 /* This one should have measured bandwidth above the clip cutoff */
3240 tt_assert(rs
->has_bandwidth
);
3241 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
3242 tt_assert(!(rs
->bw_is_unmeasured
));
3243 } else if (tor_memeq(rs
->identity_digest
,
3244 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
3245 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
3248 * This one should have unmeasured bandwidth above the clip cutoff,
3249 * and so should be clipped
3251 tt_assert(rs
->has_bandwidth
);
3252 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
);
3253 tt_assert(rs
->bw_is_unmeasured
);
3254 } else if (tor_memeq(rs
->identity_digest
,
3255 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
3256 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
3259 * This one should have unmeasured bandwidth below the clip cutoff,
3260 * and so should not be clipped
3262 tt_assert(rs
->has_bandwidth
);
3263 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
3264 tt_assert(rs
->bw_is_unmeasured
);
3266 /* Weren't expecting this... */
3275 * Compute a consensus involving clipping unmeasured bandwidth with consensus
3276 * method 17; this uses the same test_a_networkstatus() function that the
3277 * v3_networkstatus test uses.
3281 test_dir_clip_unmeasured_bw_kb(void *arg
)
3283 /* Run the test with the default clip bandwidth */
3285 alternate_clip_bw
= 0;
3286 test_a_networkstatus(gen_routerstatus_for_umbw
,
3287 vote_tweaks_for_umbw
,
3289 test_consensus_for_umbw
,
3290 test_routerstatus_for_umbw
);
3294 * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
3295 * of clip bandwidth.
3299 test_dir_clip_unmeasured_bw_kb_alt(void *arg
)
3302 * Try a different one; this value is chosen so that the below-the-cutoff
3303 * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
3304 * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
3305 * cutoff it will fail the test.
3308 alternate_clip_bw
= 3 * DEFAULT_MAX_UNMEASURED_BW_KB
;
3309 test_a_networkstatus(gen_routerstatus_for_umbw
,
3310 vote_tweaks_for_umbw
,
3312 test_consensus_for_umbw
,
3313 test_routerstatus_for_umbw
);
3317 test_dir_fmt_control_ns(void *arg
)
3323 memset(&rs
, 0, sizeof(rs
));
3324 rs
.published_on
= 1364925198;
3325 strlcpy(rs
.nickname
, "TetsuoMilk", sizeof(rs
.nickname
));
3326 memcpy(rs
.identity_digest
, "Stately, plump Buck ", DIGEST_LEN
);
3327 memcpy(rs
.descriptor_digest
, "Mulligan came up fro", DIGEST_LEN
);
3328 rs
.addr
= 0x20304050;
3333 rs
.is_flagged_running
= 1;
3334 rs
.has_bandwidth
= 1;
3336 rs
.bandwidth_kb
= 1000;
3338 s
= networkstatus_getinfo_helper_single(&rs
);
3341 "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
3342 "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
3343 "32.48.64.80 9001 9002\n"
3344 "s Exit Fast Running V2Dir\n"
3345 "w Bandwidth=1000\n");
3351 static int mock_get_options_calls
= 0;
3352 static or_options_t
*mock_options
= NULL
;
3355 reset_options(or_options_t
*options
, int *get_options_calls
)
3357 memset(options
, 0, sizeof(or_options_t
));
3358 options
->TestingTorNetwork
= 1;
3360 *get_options_calls
= 0;
3363 static const or_options_t
*
3364 mock_get_options(void)
3366 ++mock_get_options_calls
;
3367 tor_assert(mock_options
);
3368 return mock_options
;
3372 reset_routerstatus(routerstatus_t
*rs
,
3373 const char *hex_identity_digest
,
3376 memset(rs
, 0, sizeof(routerstatus_t
));
3377 base16_decode(rs
->identity_digest
, sizeof(rs
->identity_digest
),
3378 hex_identity_digest
, HEX_DIGEST_LEN
);
3379 /* A zero address matches everything, so the address needs to be set.
3380 * But the specific value is irrelevant. */
3381 rs
->addr
= ipv4_addr
;
3384 #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
3385 #define ROUTER_A_IPV4 0xAA008801
3386 #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
3387 #define ROUTER_B_IPV4 0xBB008801
3389 #define ROUTERSET_ALL_STR "*"
3390 #define ROUTERSET_A_STR ROUTER_A_ID_STR
3391 #define ROUTERSET_NONE_STR ""
3394 * Test that dirserv_set_routerstatus_testing sets router flags correctly
3395 * Using "*" sets flags on A and B
3396 * Using "A" sets flags on A
3397 * Using "" sets flags on Neither
3398 * If the router is not included:
3399 * - if *Strict is set, the flag is set to 0,
3400 * - otherwise, the flag is not modified. */
3402 test_dir_dirserv_set_routerstatus_testing(void *arg
)
3407 mock_options
= tor_malloc(sizeof(or_options_t
));
3408 reset_options(mock_options
, &mock_get_options_calls
);
3410 MOCK(get_options
, mock_get_options
);
3412 /* Init routersets */
3413 routerset_t
*routerset_all
= routerset_new();
3414 routerset_parse(routerset_all
, ROUTERSET_ALL_STR
, "All routers");
3416 routerset_t
*routerset_a
= routerset_new();
3417 routerset_parse(routerset_a
, ROUTERSET_A_STR
, "Router A only");
3419 routerset_t
*routerset_none
= routerset_new();
3420 /* Routersets are empty when provided by routerset_new(),
3421 * so this is not strictly necessary */
3422 routerset_parse(routerset_none
, ROUTERSET_NONE_STR
, "No routers");
3424 /* Init routerstatuses */
3425 routerstatus_t
*rs_a
= tor_malloc(sizeof(routerstatus_t
));
3426 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3428 routerstatus_t
*rs_b
= tor_malloc(sizeof(routerstatus_t
));
3429 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3431 /* Sanity check that routersets correspond to routerstatuses.
3432 * Return values are {2, 3, 4} */
3434 /* We want 3 ("*" means match all addresses) */
3435 tt_int_op(routerset_contains_routerstatus(routerset_all
, rs_a
, 0), OP_EQ
, 3);
3436 tt_int_op(routerset_contains_routerstatus(routerset_all
, rs_b
, 0), OP_EQ
, 3);
3438 /* We want 4 (match id_digest [or nickname]) */
3439 tt_int_op(routerset_contains_routerstatus(routerset_a
, rs_a
, 0), OP_EQ
, 4);
3440 tt_int_op(routerset_contains_routerstatus(routerset_a
, rs_b
, 0), OP_EQ
, 0);
3442 tt_int_op(routerset_contains_routerstatus(routerset_none
, rs_a
, 0), OP_EQ
,
3444 tt_int_op(routerset_contains_routerstatus(routerset_none
, rs_b
, 0), OP_EQ
,
3447 /* Check that "*" sets flags on all routers: Exit
3448 * Check the flags aren't being confused with each other */
3449 reset_options(mock_options
, &mock_get_options_calls
);
3450 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3451 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3453 mock_options
->TestingDirAuthVoteExit
= routerset_all
;
3454 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3456 dirserv_set_routerstatus_testing(rs_a
);
3457 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3458 dirserv_set_routerstatus_testing(rs_b
);
3459 tt_int_op(mock_get_options_calls
, OP_EQ
, 2);
3461 tt_uint_op(rs_a
->is_exit
, OP_EQ
, 1);
3462 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 1);
3463 /* Be paranoid - check no other flags are set */
3464 tt_uint_op(rs_a
->is_possible_guard
, OP_EQ
, 0);
3465 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 0);
3466 tt_uint_op(rs_a
->is_hs_dir
, OP_EQ
, 0);
3467 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 0);
3469 /* Check that "*" sets flags on all routers: Guard & HSDir
3470 * Cover the remaining flags in one test */
3471 reset_options(mock_options
, &mock_get_options_calls
);
3472 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3473 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3475 mock_options
->TestingDirAuthVoteGuard
= routerset_all
;
3476 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3477 mock_options
->TestingDirAuthVoteHSDir
= routerset_all
;
3478 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3480 dirserv_set_routerstatus_testing(rs_a
);
3481 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3482 dirserv_set_routerstatus_testing(rs_b
);
3483 tt_int_op(mock_get_options_calls
, OP_EQ
, 2);
3485 tt_uint_op(rs_a
->is_possible_guard
, OP_EQ
, 1);
3486 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 1);
3487 tt_uint_op(rs_a
->is_hs_dir
, OP_EQ
, 1);
3488 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 1);
3489 /* Be paranoid - check exit isn't set */
3490 tt_uint_op(rs_a
->is_exit
, OP_EQ
, 0);
3491 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 0);
3493 /* Check routerset A sets all flags on router A,
3494 * but leaves router B unmodified */
3495 reset_options(mock_options
, &mock_get_options_calls
);
3496 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3497 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3499 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3500 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3501 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3502 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3503 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3504 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3506 dirserv_set_routerstatus_testing(rs_a
);
3507 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3508 dirserv_set_routerstatus_testing(rs_b
);
3509 tt_int_op(mock_get_options_calls
, OP_EQ
, 2);
3511 tt_uint_op(rs_a
->is_exit
, OP_EQ
, 1);
3512 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 0);
3513 tt_uint_op(rs_a
->is_possible_guard
, OP_EQ
, 1);
3514 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 0);
3515 tt_uint_op(rs_a
->is_hs_dir
, OP_EQ
, 1);
3516 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 0);
3518 /* Check routerset A unsets all flags on router B when Strict is set */
3519 reset_options(mock_options
, &mock_get_options_calls
);
3520 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3522 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3523 mock_options
->TestingDirAuthVoteExitIsStrict
= 1;
3524 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3525 mock_options
->TestingDirAuthVoteGuardIsStrict
= 1;
3526 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3527 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 1;
3530 rs_b
->is_possible_guard
= 1;
3531 rs_b
->is_hs_dir
= 1;
3533 dirserv_set_routerstatus_testing(rs_b
);
3534 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3536 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 0);
3537 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 0);
3538 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 0);
3540 /* Check routerset A doesn't modify flags on router B without Strict set */
3541 reset_options(mock_options
, &mock_get_options_calls
);
3542 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3544 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3545 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3546 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3547 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3548 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3549 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3552 rs_b
->is_possible_guard
= 1;
3553 rs_b
->is_hs_dir
= 1;
3555 dirserv_set_routerstatus_testing(rs_b
);
3556 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3558 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 1);
3559 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 1);
3560 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 1);
3562 /* Check the empty routerset zeroes all flags
3563 * on routers A & B with Strict set */
3564 reset_options(mock_options
, &mock_get_options_calls
);
3565 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3567 mock_options
->TestingDirAuthVoteExit
= routerset_none
;
3568 mock_options
->TestingDirAuthVoteExitIsStrict
= 1;
3569 mock_options
->TestingDirAuthVoteGuard
= routerset_none
;
3570 mock_options
->TestingDirAuthVoteGuardIsStrict
= 1;
3571 mock_options
->TestingDirAuthVoteHSDir
= routerset_none
;
3572 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 1;
3575 rs_b
->is_possible_guard
= 1;
3576 rs_b
->is_hs_dir
= 1;
3578 dirserv_set_routerstatus_testing(rs_b
);
3579 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3581 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 0);
3582 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 0);
3583 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 0);
3585 /* Check the empty routerset doesn't modify any flags
3586 * on A or B without Strict set */
3587 reset_options(mock_options
, &mock_get_options_calls
);
3588 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3589 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3591 mock_options
->TestingDirAuthVoteExit
= routerset_none
;
3592 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3593 mock_options
->TestingDirAuthVoteGuard
= routerset_none
;
3594 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3595 mock_options
->TestingDirAuthVoteHSDir
= routerset_none
;
3596 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3599 rs_b
->is_possible_guard
= 1;
3600 rs_b
->is_hs_dir
= 1;
3602 dirserv_set_routerstatus_testing(rs_a
);
3603 tt_int_op(mock_get_options_calls
, OP_EQ
, 1);
3604 dirserv_set_routerstatus_testing(rs_b
);
3605 tt_int_op(mock_get_options_calls
, OP_EQ
, 2);
3607 tt_uint_op(rs_a
->is_exit
, OP_EQ
, 0);
3608 tt_uint_op(rs_a
->is_possible_guard
, OP_EQ
, 0);
3609 tt_uint_op(rs_a
->is_hs_dir
, OP_EQ
, 0);
3610 tt_uint_op(rs_b
->is_exit
, OP_EQ
, 1);
3611 tt_uint_op(rs_b
->is_possible_guard
, OP_EQ
, 1);
3612 tt_uint_op(rs_b
->is_hs_dir
, OP_EQ
, 1);
3615 tor_free(mock_options
);
3616 mock_options
= NULL
;
3618 UNMOCK(get_options
);
3620 routerset_free(routerset_all
);
3621 routerset_free(routerset_a
);
3622 routerset_free(routerset_none
);
3629 test_dir_http_handling(void *args
)
3634 /* Parse http url tests: */
3636 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
3637 "Host: example.com\r\n"
3638 "User-Agent: Mozilla/5.0 (Windows;"
3639 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3641 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3644 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url
),OP_EQ
, 0);
3645 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3648 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url
),
3650 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3653 /* Should prepend '/tor/' to url if required */
3654 tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
3655 "Host: example.com\r\n"
3656 "User-Agent: Mozilla/5.0 (Windows;"
3657 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3659 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3662 /* Bad headers -- no HTTP/1.x*/
3663 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3664 "Host: example.com\r\n"
3665 "User-Agent: Mozilla/5.0 (Windows;"
3666 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3668 tt_ptr_op(url
, OP_EQ
, NULL
);
3671 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3672 "Host: example.com\r\n"
3673 "User-Agent: Mozilla/5.0 (Windows;"
3674 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3676 tt_ptr_op(url
, OP_EQ
, NULL
);
3678 tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url
),OP_EQ
, -1);
3679 tt_ptr_op(url
, OP_EQ
, NULL
);
3681 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url
),OP_EQ
, -1);
3682 tt_ptr_op(url
, OP_EQ
, NULL
);
3684 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url
),
3686 tt_ptr_op(url
, OP_EQ
, NULL
);
3688 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url
),OP_EQ
, -1);
3689 tt_ptr_op(url
, OP_EQ
, NULL
);
3691 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url
),OP_EQ
, -1);
3692 tt_ptr_op(url
, OP_EQ
, NULL
);
3699 test_dir_purpose_needs_anonymity_returns_true_by_default(void *arg
)
3703 tor_capture_bugs_(1);
3704 setup_full_capture_of_logs(LOG_WARN
);
3705 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(0, 0, NULL
));
3706 tt_int_op(1, OP_EQ
, smartlist_len(tor_get_captured_bug_log_()));
3707 expect_single_log_msg_containing("Called with dir_purpose=0");
3709 tor_end_capture_bugs_();
3711 tor_end_capture_bugs_();
3712 teardown_capture_of_logs();
3716 test_dir_purpose_needs_anonymity_returns_true_for_bridges(void *arg
)
3720 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE
, NULL
));
3721 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE
,
3724 purpose_needs_anonymity(DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2
,
3725 ROUTER_PURPOSE_BRIDGE
, NULL
));
3730 test_dir_purpose_needs_anonymity_returns_false_for_own_bridge_desc(void *arg
)
3733 tt_int_op(0, OP_EQ
, purpose_needs_anonymity(DIR_PURPOSE_FETCH_SERVERDESC
,
3734 ROUTER_PURPOSE_BRIDGE
,
3740 test_dir_purpose_needs_anonymity_returns_true_for_sensitive_purpose(void *arg
)
3744 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(
3745 DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2
,
3746 ROUTER_PURPOSE_GENERAL
, NULL
));
3747 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(
3748 DIR_PURPOSE_UPLOAD_RENDDESC_V2
, 0, NULL
));
3749 tt_int_op(1, OP_EQ
, purpose_needs_anonymity(
3750 DIR_PURPOSE_FETCH_RENDDESC_V2
, 0, NULL
));
3755 test_dir_purpose_needs_anonymity_ret_false_for_non_sensitive_conn(void *arg
)
3759 tt_int_op(0, OP_EQ
, purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_DIR
,
3760 ROUTER_PURPOSE_GENERAL
, NULL
));
3762 purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_VOTE
, 0, NULL
));
3764 purpose_needs_anonymity(DIR_PURPOSE_UPLOAD_SIGNATURES
, 0, NULL
));
3766 purpose_needs_anonymity(DIR_PURPOSE_FETCH_STATUS_VOTE
, 0, NULL
));
3767 tt_int_op(0, OP_EQ
, purpose_needs_anonymity(
3768 DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
, 0, NULL
));
3770 purpose_needs_anonymity(DIR_PURPOSE_FETCH_CONSENSUS
, 0, NULL
));
3772 purpose_needs_anonymity(DIR_PURPOSE_FETCH_CERTIFICATE
, 0, NULL
));
3774 purpose_needs_anonymity(DIR_PURPOSE_FETCH_SERVERDESC
, 0, NULL
));
3776 purpose_needs_anonymity(DIR_PURPOSE_FETCH_EXTRAINFO
, 0, NULL
));
3778 purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC
, 0, NULL
));
3783 test_dir_fetch_type(void *arg
)
3787 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO
, ROUTER_PURPOSE_BRIDGE
,
3788 NULL
), OP_EQ
, EXTRAINFO_DIRINFO
| BRIDGE_DIRINFO
);
3789 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO
, ROUTER_PURPOSE_GENERAL
,
3790 NULL
), OP_EQ
, EXTRAINFO_DIRINFO
| V3_DIRINFO
);
3792 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC
, ROUTER_PURPOSE_BRIDGE
,
3793 NULL
), OP_EQ
, BRIDGE_DIRINFO
);
3794 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC
,
3795 ROUTER_PURPOSE_GENERAL
, NULL
), OP_EQ
, V3_DIRINFO
);
3797 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE
,
3798 ROUTER_PURPOSE_GENERAL
, NULL
), OP_EQ
, V3_DIRINFO
);
3799 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
3800 ROUTER_PURPOSE_GENERAL
, NULL
), OP_EQ
, V3_DIRINFO
);
3801 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE
,
3802 ROUTER_PURPOSE_GENERAL
, NULL
), OP_EQ
, V3_DIRINFO
);
3804 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS
, ROUTER_PURPOSE_GENERAL
,
3805 "microdesc"), OP_EQ
, V3_DIRINFO
|MICRODESC_DIRINFO
);
3806 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS
, ROUTER_PURPOSE_GENERAL
,
3807 NULL
), OP_EQ
, V3_DIRINFO
);
3809 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC
, ROUTER_PURPOSE_GENERAL
,
3810 NULL
), OP_EQ
, MICRODESC_DIRINFO
);
3812 /* This will give a warning, because this function isn't supposed to be
3813 * used for HS descriptors. */
3814 setup_full_capture_of_logs(LOG_WARN
);
3815 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2
,
3816 ROUTER_PURPOSE_GENERAL
, NULL
), OP_EQ
, NO_DIRINFO
);
3817 expect_single_log_msg_containing("Unexpected purpose");
3819 teardown_capture_of_logs();
3823 test_dir_packages(void *arg
)
3825 smartlist_t
*votes
= smartlist_new();
3830 tt_int_op(0, OP_EQ, validate_recommended_package_line(s));
3832 tt_int_op(1, OP_EQ, validate_recommended_package_line(s));
3833 GOOD("tor 0.2.6.3-alpha "
3834 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3835 "sha256=sssdlkfjdsklfjdskfljasdklfj");
3836 GOOD("tor 0.2.6.3-alpha "
3837 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3838 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
3839 BAD("tor 0.2.6.3-alpha "
3840 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3841 "sha256=sssdlkfjdsklfjdskfljasdklfj=");
3842 BAD("tor 0.2.6.3-alpha "
3843 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3844 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
3845 BAD("tor 0.2.6.3-alpha "
3846 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
3847 BAD("tor 0.2.6.3-alpha "
3848 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
3849 BAD("tor 0.2.6.3-alpha ");
3850 BAD("tor 0.2.6.3-alpha");
3854 BAD("=foobar sha256="
3855 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3857 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3859 BAD("sha512= sha256="
3860 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3862 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3863 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3864 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3865 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3866 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3867 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3868 SMARTLIST_FOREACH(votes
, networkstatus_t
*, ns
,
3869 ns
->package_lines
= smartlist_new());
3872 smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
3875 /* Only one vote for this one. */
3876 ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
3878 /* Only two matching entries for this one, but 3 voters */
3879 ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
3880 ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3881 ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3883 /* Only two matching entries for this one, but at least 4 voters */
3884 ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
3885 ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3886 ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3887 ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
3889 /* This one has only invalid votes. */
3890 ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
3891 ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
3892 ADD(2, "haffenreffer 1.2 ");
3893 ADD(3, "haffenreffer ");
3894 ADD(4, "haffenreffer");
3896 /* Three matching votes for this; it should actually go in! */
3897 ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3898 ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3899 ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3900 ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
3901 ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
3903 /* Three votes for A, three votes for B */
3904 ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3905 ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3906 ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3907 ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3908 ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3909 ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3911 /* Three votes for A, two votes for B */
3912 ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3913 ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3914 ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3915 ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3916 ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3918 /* Four votes for A, two for B. */
3919 ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3920 ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3921 ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3922 ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3923 ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3924 ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3926 /* Five votes for A ... all from the same authority. Three for B. */
3927 ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3928 ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3929 ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3930 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3931 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3932 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3933 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3934 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3936 /* As above but new replaces old: no two match. */
3937 ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3938 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3939 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
3940 ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3941 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3942 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3943 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3944 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3945 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3947 res
= compute_consensus_package_lines(votes
);
3949 tt_str_op(res
, OP_EQ
,
3950 "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
3951 "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
3952 "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
3953 "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
3954 "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
3961 SMARTLIST_FOREACH(votes
, networkstatus_t
*, ns
,
3962 { smartlist_free(ns
->package_lines
); tor_free(ns
); });
3963 smartlist_free(votes
);
3968 test_dir_download_status_schedule(void *arg
)
3971 download_status_t dls_failure
= { 0, 0, 0, DL_SCHED_GENERIC
,
3973 DL_SCHED_INCREMENT_FAILURE
,
3974 DL_SCHED_DETERMINISTIC
, 0, 0 };
3975 download_status_t dls_attempt
= { 0, 0, 0, DL_SCHED_CONSENSUS
,
3976 DL_WANT_ANY_DIRSERVER
,
3977 DL_SCHED_INCREMENT_ATTEMPT
,
3978 DL_SCHED_DETERMINISTIC
, 0, 0 };
3979 download_status_t dls_bridge
= { 0, 0, 0, DL_SCHED_BRIDGE
,
3981 DL_SCHED_INCREMENT_FAILURE
,
3982 DL_SCHED_DETERMINISTIC
, 0, 0 };
3984 int expected_increment
= -1;
3985 time_t current_time
= time(NULL
);
3988 smartlist_t
*schedule
= smartlist_new();
3990 /* Make a dummy schedule */
3991 smartlist_add(schedule
, (void *)&delay1
);
3992 smartlist_add(schedule
, (void *)&delay2
);
3994 /* check a range of values */
3996 increment
= download_status_schedule_get_delay(&dls_failure
,
4000 expected_increment
= delay1
;
4001 tt_assert(increment
== expected_increment
);
4002 tt_assert(dls_failure
.next_attempt_at
== TIME_MIN
+ expected_increment
);
4005 increment
= download_status_schedule_get_delay(&dls_failure
,
4009 expected_increment
= delay1
;
4010 tt_assert(increment
== expected_increment
);
4011 tt_assert(dls_failure
.next_attempt_at
== TIME_MAX
);
4014 increment
= download_status_schedule_get_delay(&dls_attempt
,
4018 expected_increment
= delay1
;
4019 tt_assert(increment
== expected_increment
);
4020 tt_assert(dls_attempt
.next_attempt_at
== 0 + expected_increment
);
4023 increment
= download_status_schedule_get_delay(&dls_attempt
,
4027 expected_increment
= delay1
;
4028 tt_assert(increment
== expected_increment
);
4029 tt_assert(dls_attempt
.next_attempt_at
== 1 + expected_increment
);
4032 increment
= download_status_schedule_get_delay(&dls_bridge
,
4036 expected_increment
= delay1
;
4037 tt_assert(increment
== expected_increment
);
4038 tt_assert(dls_bridge
.next_attempt_at
== TIME_MAX
);
4041 increment
= download_status_schedule_get_delay(&dls_bridge
,
4045 expected_increment
= delay1
;
4046 tt_assert(increment
== expected_increment
);
4047 tt_assert(dls_bridge
.next_attempt_at
== TIME_MAX
);
4049 /* see what happens when we reach the end */
4050 dls_attempt
.n_download_attempts
++;
4051 dls_bridge
.n_download_failures
++;
4054 increment
= download_status_schedule_get_delay(&dls_attempt
,
4058 expected_increment
= delay2
;
4059 tt_assert(increment
== expected_increment
);
4060 tt_assert(dls_attempt
.next_attempt_at
== current_time
+ delay2
);
4063 increment
= download_status_schedule_get_delay(&dls_bridge
,
4067 expected_increment
= delay2
;
4068 tt_assert(increment
== expected_increment
);
4069 tt_assert(dls_bridge
.next_attempt_at
== current_time
+ delay2
);
4071 /* see what happens when we try to go off the end */
4072 dls_attempt
.n_download_attempts
++;
4073 dls_bridge
.n_download_failures
++;
4076 increment
= download_status_schedule_get_delay(&dls_attempt
,
4080 expected_increment
= delay2
;
4081 tt_assert(increment
== expected_increment
);
4082 tt_assert(dls_attempt
.next_attempt_at
== current_time
+ delay2
);
4085 increment
= download_status_schedule_get_delay(&dls_bridge
,
4089 expected_increment
= delay2
;
4090 tt_assert(increment
== expected_increment
);
4091 tt_assert(dls_bridge
.next_attempt_at
== current_time
+ delay2
);
4093 /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
4094 dls_attempt
.n_download_attempts
= IMPOSSIBLE_TO_DOWNLOAD
;
4095 dls_bridge
.n_download_failures
= IMPOSSIBLE_TO_DOWNLOAD
;
4098 increment
= download_status_schedule_get_delay(&dls_attempt
,
4102 expected_increment
= INT_MAX
;
4103 tt_assert(increment
== expected_increment
);
4104 tt_assert(dls_attempt
.next_attempt_at
== TIME_MAX
);
4107 increment
= download_status_schedule_get_delay(&dls_bridge
,
4111 expected_increment
= INT_MAX
;
4112 tt_assert(increment
== expected_increment
);
4113 tt_assert(dls_bridge
.next_attempt_at
== TIME_MAX
);
4116 /* the pointers in schedule are allocated on the stack */
4117 smartlist_free(schedule
);
4121 download_status_random_backoff_helper(int min_delay
, int max_delay
)
4123 download_status_t dls_random
=
4124 { 0, 0, 0, DL_SCHED_GENERIC
, DL_WANT_AUTHORITY
,
4125 DL_SCHED_INCREMENT_FAILURE
, DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 };
4127 int old_increment
= -1;
4128 time_t current_time
= time(NULL
);
4130 /* Check the random backoff cases */
4133 increment
= download_status_schedule_get_delay(&dls_random
,
4135 min_delay
, max_delay
,
4138 log_debug(LD_DIR
, "Min: %d, Max: %d, Inc: %d, Old Inc: %d",
4139 min_delay
, max_delay
, increment
, old_increment
);
4141 /* Regression test for 20534 and friends
4142 * increment must always increase after the first */
4143 if (dls_random
.last_backoff_position
> 0 && max_delay
> 0) {
4144 /* Always increment the exponential backoff */
4145 tt_int_op(increment
, OP_GE
, 1);
4149 tt_int_op(increment
, OP_GE
, min_delay
);
4150 tt_int_op(increment
, OP_LE
, max_delay
);
4153 if (dls_random
.n_download_attempts
< IMPOSSIBLE_TO_DOWNLOAD
- 1) {
4154 ++(dls_random
.n_download_attempts
);
4155 ++(dls_random
.n_download_failures
);
4158 /* Try another maybe */
4159 old_increment
= increment
;
4160 } while (increment
< max_delay
&& ++n_attempts
< 1000);
4167 test_dir_download_status_random_backoff(void *arg
)
4171 /* Do a standard test */
4172 download_status_random_backoff_helper(0, 1000000);
4173 /* Regression test for 20534 and friends:
4174 * try tighter bounds */
4175 download_status_random_backoff_helper(0, 100);
4176 /* regression tests for 17750: initial delay */
4177 download_status_random_backoff_helper(10, 1000);
4178 download_status_random_backoff_helper(20, 30);
4180 /* Pathological cases */
4181 download_status_random_backoff_helper(0, 0);
4182 download_status_random_backoff_helper(1, 1);
4183 download_status_random_backoff_helper(0, INT_MAX
);
4184 download_status_random_backoff_helper(INT_MAX
/2, INT_MAX
);
4188 test_dir_download_status_random_backoff_ranges(void *arg
)
4192 next_random_exponential_delay_range(&lo
, &hi
, 0, 10);
4193 tt_int_op(lo
, OP_EQ
, 10);
4194 tt_int_op(hi
, OP_EQ
, 11);
4196 next_random_exponential_delay_range(&lo
, &hi
, 6, 10);
4197 tt_int_op(lo
, OP_EQ
, 10);
4198 tt_int_op(hi
, OP_EQ
, 6*3);
4200 next_random_exponential_delay_range(&lo
, &hi
, 13, 10);
4201 tt_int_op(lo
, OP_EQ
, 10);
4202 tt_int_op(hi
, OP_EQ
, 13 * 3);
4204 next_random_exponential_delay_range(&lo
, &hi
, 37, 10);
4205 tt_int_op(lo
, OP_EQ
, 10);
4206 tt_int_op(hi
, OP_EQ
, 111);
4208 next_random_exponential_delay_range(&lo
, &hi
, 123, 10);
4209 tt_int_op(lo
, OP_EQ
, 10);
4210 tt_int_op(hi
, OP_EQ
, 369);
4212 next_random_exponential_delay_range(&lo
, &hi
, INT_MAX
-5, 10);
4213 tt_int_op(lo
, OP_EQ
, 10);
4214 tt_int_op(hi
, OP_EQ
, INT_MAX
);
4220 test_dir_download_status_increment(void *arg
)
4223 download_status_t dls_failure
= { 0, 0, 0, DL_SCHED_GENERIC
,
4225 DL_SCHED_INCREMENT_FAILURE
,
4226 DL_SCHED_DETERMINISTIC
, 0, 0 };
4227 download_status_t dls_attempt
= { 0, 0, 0, DL_SCHED_BRIDGE
,
4228 DL_WANT_ANY_DIRSERVER
,
4229 DL_SCHED_INCREMENT_ATTEMPT
,
4230 DL_SCHED_DETERMINISTIC
, 0, 0 };
4231 download_status_t dls_exp
= { 0, 0, 0, DL_SCHED_GENERIC
,
4232 DL_WANT_ANY_DIRSERVER
,
4233 DL_SCHED_INCREMENT_ATTEMPT
,
4234 DL_SCHED_RANDOM_EXPONENTIAL
, 0, 0 };
4239 smartlist_t
*schedule
= smartlist_new();
4240 smartlist_t
*schedule_no_initial_delay
= smartlist_new();
4241 or_options_t test_options
;
4242 time_t next_at
= TIME_MAX
;
4243 time_t current_time
= time(NULL
);
4245 /* Provide some values for the schedules */
4250 /* Make the schedules */
4251 smartlist_add(schedule
, (void *)&delay0
);
4252 smartlist_add(schedule
, (void *)&delay1
);
4253 smartlist_add(schedule
, (void *)&delay2
);
4255 smartlist_add(schedule_no_initial_delay
, (void *)&no_delay
);
4256 smartlist_add(schedule_no_initial_delay
, (void *)&delay1
);
4257 smartlist_add(schedule_no_initial_delay
, (void *)&delay2
);
4259 /* Put it in the options */
4260 mock_options
= &test_options
;
4261 reset_options(mock_options
, &mock_get_options_calls
);
4262 mock_options
->TestingBridgeBootstrapDownloadSchedule
= schedule
;
4263 mock_options
->TestingClientDownloadSchedule
= schedule
;
4265 MOCK(get_options
, mock_get_options
);
4267 /* Check that the initial value of the schedule is the first value used,
4268 * whether or not it was reset before being used */
4270 /* regression test for 17750: no initial delay */
4271 mock_options
->TestingClientDownloadSchedule
= schedule_no_initial_delay
;
4272 mock_get_options_calls
= 0;
4273 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4274 * an unrealiable test, because time(NULL) might change. */
4275 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4276 >= current_time
+ no_delay
);
4277 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4279 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 0);
4280 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 0);
4281 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4283 /* regression test for 17750: initial delay */
4284 mock_options
->TestingClientDownloadSchedule
= schedule
;
4285 mock_get_options_calls
= 0;
4286 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4287 * an unrealiable test, because time(NULL) might change. */
4288 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4289 >= current_time
+ delay0
);
4290 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4292 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 0);
4293 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 0);
4294 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4296 /* regression test for 17750: exponential, no initial delay */
4297 mock_options
->TestingClientDownloadSchedule
= schedule_no_initial_delay
;
4298 mock_get_options_calls
= 0;
4299 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4300 * an unrealiable test, because time(NULL) might change. */
4301 tt_assert(download_status_get_next_attempt_at(&dls_exp
)
4302 >= current_time
+ no_delay
);
4303 tt_assert(download_status_get_next_attempt_at(&dls_exp
)
4305 tt_int_op(download_status_get_n_failures(&dls_exp
), OP_EQ
, 0);
4306 tt_int_op(download_status_get_n_attempts(&dls_exp
), OP_EQ
, 0);
4307 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4309 /* regression test for 17750: exponential, initial delay */
4310 mock_options
->TestingClientDownloadSchedule
= schedule
;
4311 mock_get_options_calls
= 0;
4312 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4313 * an unrealiable test, because time(NULL) might change. */
4314 tt_assert(download_status_get_next_attempt_at(&dls_exp
)
4315 >= current_time
+ delay0
);
4316 tt_assert(download_status_get_next_attempt_at(&dls_exp
)
4318 tt_int_op(download_status_get_n_failures(&dls_exp
), OP_EQ
, 0);
4319 tt_int_op(download_status_get_n_attempts(&dls_exp
), OP_EQ
, 0);
4320 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4322 /* Check that a failure reset works */
4323 mock_get_options_calls
= 0;
4324 download_status_reset(&dls_failure
);
4325 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4326 * an unrealiable test, because time(NULL) might change. */
4327 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4328 >= current_time
+ delay0
);
4329 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4331 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 0);
4332 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 0);
4333 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4335 /* avoid timing inconsistencies */
4336 dls_failure
.next_attempt_at
= current_time
+ delay0
;
4338 /* check that a reset schedule becomes ready at the right time */
4339 tt_int_op(download_status_is_ready(&dls_failure
,
4340 current_time
+ delay0
- 1, 1),
4342 tt_int_op(download_status_is_ready(&dls_failure
,
4343 current_time
+ delay0
, 1),
4345 tt_int_op(download_status_is_ready(&dls_failure
,
4346 current_time
+ delay0
+ 1, 1),
4349 /* Check that a failure increment works */
4350 mock_get_options_calls
= 0;
4351 next_at
= download_status_increment_failure(&dls_failure
, 404, "test", 0,
4353 tt_assert(next_at
== current_time
+ delay1
);
4354 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 1);
4355 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 1);
4356 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4358 /* check that an incremented schedule becomes ready at the right time */
4359 tt_int_op(download_status_is_ready(&dls_failure
,
4360 current_time
+ delay1
- 1, 1),
4362 tt_int_op(download_status_is_ready(&dls_failure
,
4363 current_time
+ delay1
, 1),
4365 tt_int_op(download_status_is_ready(&dls_failure
,
4366 current_time
+ delay1
+ 1, 1),
4369 /* check that a schedule isn't ready if it's had too many failures */
4370 tt_int_op(download_status_is_ready(&dls_failure
,
4371 current_time
+ delay1
+ 10, 0),
4374 /* Check that failure increments do happen on 503 for clients, and
4375 * attempt increments do too. */
4376 mock_get_options_calls
= 0;
4377 next_at
= download_status_increment_failure(&dls_failure
, 503, "test", 0,
4379 tt_i64_op(next_at
, OP_EQ
, current_time
+ delay2
);
4380 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 2);
4381 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 2);
4382 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4384 /* Check that failure increments do happen on 503 for servers */
4385 mock_get_options_calls
= 0;
4386 next_at
= download_status_increment_failure(&dls_failure
, 503, "test", 1,
4388 tt_assert(next_at
== current_time
+ delay2
);
4389 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 3);
4390 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 3);
4391 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4393 /* Check what happens when we run off the end of the schedule */
4394 mock_get_options_calls
= 0;
4395 next_at
= download_status_increment_failure(&dls_failure
, 404, "test", 0,
4397 tt_assert(next_at
== current_time
+ delay2
);
4398 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 4);
4399 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 4);
4400 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4402 /* Check what happens when we hit the failure limit */
4403 mock_get_options_calls
= 0;
4404 download_status_mark_impossible(&dls_failure
);
4405 next_at
= download_status_increment_failure(&dls_failure
, 404, "test", 0,
4407 tt_assert(next_at
== TIME_MAX
);
4408 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
,
4409 IMPOSSIBLE_TO_DOWNLOAD
);
4410 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
,
4411 IMPOSSIBLE_TO_DOWNLOAD
);
4412 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4414 /* Check that a failure reset doesn't reset at the limit */
4415 mock_get_options_calls
= 0;
4416 download_status_reset(&dls_failure
);
4417 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4419 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
,
4420 IMPOSSIBLE_TO_DOWNLOAD
);
4421 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
,
4422 IMPOSSIBLE_TO_DOWNLOAD
);
4423 tt_int_op(mock_get_options_calls
, OP_EQ
, 0);
4425 /* Check that a failure reset resets just before the limit */
4426 mock_get_options_calls
= 0;
4427 dls_failure
.n_download_failures
= IMPOSSIBLE_TO_DOWNLOAD
- 1;
4428 dls_failure
.n_download_attempts
= IMPOSSIBLE_TO_DOWNLOAD
- 1;
4429 download_status_reset(&dls_failure
);
4430 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4431 * an unrealiable test, because time(NULL) might change. */
4432 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4433 >= current_time
+ delay0
);
4434 tt_assert(download_status_get_next_attempt_at(&dls_failure
)
4436 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 0);
4437 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 0);
4438 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4440 /* Check that failure increments do happen on attempt-based schedules,
4441 * but that the retry is set at the end of time */
4442 mock_options
->UseBridges
= 1;
4443 mock_get_options_calls
= 0;
4444 next_at
= download_status_increment_failure(&dls_attempt
, 404, "test", 0,
4446 tt_assert(next_at
== TIME_MAX
);
4447 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 1);
4448 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 0);
4449 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4451 /* Check that an attempt reset works */
4452 mock_get_options_calls
= 0;
4453 download_status_reset(&dls_attempt
);
4454 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4455 * an unrealiable test, because time(NULL) might change. */
4456 tt_assert(download_status_get_next_attempt_at(&dls_attempt
)
4457 >= current_time
+ delay0
);
4458 tt_assert(download_status_get_next_attempt_at(&dls_attempt
)
4460 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 0);
4461 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 0);
4462 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4464 /* avoid timing inconsistencies */
4465 dls_attempt
.next_attempt_at
= current_time
+ delay0
;
4467 /* check that a reset schedule becomes ready at the right time */
4468 tt_int_op(download_status_is_ready(&dls_attempt
,
4469 current_time
+ delay0
- 1, 1),
4471 tt_int_op(download_status_is_ready(&dls_attempt
,
4472 current_time
+ delay0
, 1),
4474 tt_int_op(download_status_is_ready(&dls_attempt
,
4475 current_time
+ delay0
+ 1, 1),
4478 /* Check that an attempt increment works */
4479 mock_get_options_calls
= 0;
4480 next_at
= download_status_increment_attempt(&dls_attempt
, "test",
4482 tt_assert(next_at
== current_time
+ delay1
);
4483 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 0);
4484 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 1);
4485 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4487 /* check that an incremented schedule becomes ready at the right time */
4488 tt_int_op(download_status_is_ready(&dls_attempt
,
4489 current_time
+ delay1
- 1, 1),
4491 tt_int_op(download_status_is_ready(&dls_attempt
,
4492 current_time
+ delay1
, 1),
4494 tt_int_op(download_status_is_ready(&dls_attempt
,
4495 current_time
+ delay1
+ 1, 1),
4498 /* check that a schedule isn't ready if it's had too many attempts */
4499 tt_int_op(download_status_is_ready(&dls_attempt
,
4500 current_time
+ delay1
+ 10, 0),
4503 /* Check what happens when we reach then run off the end of the schedule */
4504 mock_get_options_calls
= 0;
4505 next_at
= download_status_increment_attempt(&dls_attempt
, "test",
4507 tt_assert(next_at
== current_time
+ delay2
);
4508 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 0);
4509 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 2);
4510 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4512 mock_get_options_calls
= 0;
4513 next_at
= download_status_increment_attempt(&dls_attempt
, "test",
4515 tt_assert(next_at
== current_time
+ delay2
);
4516 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 0);
4517 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 3);
4518 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4520 /* Check what happens when we hit the attempt limit */
4521 mock_get_options_calls
= 0;
4522 download_status_mark_impossible(&dls_attempt
);
4523 next_at
= download_status_increment_attempt(&dls_attempt
, "test",
4525 tt_assert(next_at
== TIME_MAX
);
4526 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
,
4527 IMPOSSIBLE_TO_DOWNLOAD
);
4528 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
,
4529 IMPOSSIBLE_TO_DOWNLOAD
);
4530 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4532 /* Check that an attempt reset doesn't reset at the limit */
4533 mock_get_options_calls
= 0;
4534 download_status_reset(&dls_attempt
);
4535 tt_assert(download_status_get_next_attempt_at(&dls_attempt
)
4537 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
,
4538 IMPOSSIBLE_TO_DOWNLOAD
);
4539 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
,
4540 IMPOSSIBLE_TO_DOWNLOAD
);
4541 tt_int_op(mock_get_options_calls
, OP_EQ
, 0);
4543 /* Check that an attempt reset resets just before the limit */
4544 mock_get_options_calls
= 0;
4545 dls_attempt
.n_download_failures
= IMPOSSIBLE_TO_DOWNLOAD
- 1;
4546 dls_attempt
.n_download_attempts
= IMPOSSIBLE_TO_DOWNLOAD
- 1;
4547 download_status_reset(&dls_attempt
);
4548 /* we really want to test that it's equal to time(NULL) + delay0, but that's
4549 * an unrealiable test, because time(NULL) might change. */
4550 tt_assert(download_status_get_next_attempt_at(&dls_attempt
)
4551 >= current_time
+ delay0
);
4552 tt_assert(download_status_get_next_attempt_at(&dls_attempt
)
4554 tt_int_op(download_status_get_n_failures(&dls_attempt
), OP_EQ
, 0);
4555 tt_int_op(download_status_get_n_attempts(&dls_attempt
), OP_EQ
, 0);
4556 tt_int_op(mock_get_options_calls
, OP_GE
, 1);
4557 mock_options
->UseBridges
= 0;
4559 /* Check that attempt increments don't happen on failure-based schedules,
4560 * and that the attempt is set at the end of time */
4561 mock_get_options_calls
= 0;
4562 setup_full_capture_of_logs(LOG_WARN
);
4563 next_at
= download_status_increment_attempt(&dls_failure
, "test",
4565 expect_single_log_msg_containing(
4566 "Tried to launch an attempt-based connection on a failure-based "
4568 teardown_capture_of_logs();
4569 tt_assert(next_at
== TIME_MAX
);
4570 tt_int_op(download_status_get_n_failures(&dls_failure
), OP_EQ
, 0);
4571 tt_int_op(download_status_get_n_attempts(&dls_failure
), OP_EQ
, 0);
4572 tt_int_op(mock_get_options_calls
, OP_EQ
, 0);
4575 /* the pointers in schedule are allocated on the stack */
4576 smartlist_free(schedule
);
4577 smartlist_free(schedule_no_initial_delay
);
4578 UNMOCK(get_options
);
4579 mock_options
= NULL
;
4580 mock_get_options_calls
= 0;
4581 teardown_capture_of_logs();
4585 test_dir_authdir_type_to_string(void *data
)
4590 tt_str_op(res
= authdir_type_to_string(NO_DIRINFO
), OP_EQ
,
4591 "[Not an authority]");
4594 tt_str_op(res
= authdir_type_to_string(EXTRAINFO_DIRINFO
), OP_EQ
,
4595 "[Not an authority]");
4598 tt_str_op(res
= authdir_type_to_string(MICRODESC_DIRINFO
), OP_EQ
,
4599 "[Not an authority]");
4602 tt_str_op(res
= authdir_type_to_string(V3_DIRINFO
), OP_EQ
, "V3");
4605 tt_str_op(res
= authdir_type_to_string(BRIDGE_DIRINFO
), OP_EQ
, "Bridge");
4608 tt_str_op(res
= authdir_type_to_string(
4609 V3_DIRINFO
| BRIDGE_DIRINFO
| EXTRAINFO_DIRINFO
), OP_EQ
,
4616 test_dir_conn_purpose_to_string(void *data
)
4620 #define EXPECT_CONN_PURPOSE(purpose, expected) \
4621 tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
4623 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR
, "server descriptor upload");
4624 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE
, "server vote upload");
4625 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES
,
4626 "consensus signature upload");
4627 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC
, "server descriptor fetch");
4628 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO
, "extra-info fetch");
4629 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS
,
4630 "consensus network-status fetch");
4631 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE
, "authority cert fetch");
4632 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE
, "status vote fetch");
4633 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
4634 "consensus signature fetch");
4635 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2
,
4636 "hidden-service v2 descriptor fetch");
4637 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2
,
4638 "hidden-service v2 descriptor upload");
4639 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC
, "microdescriptor fetch");
4641 /* This will give a warning, because there is no purpose 1024. */
4642 setup_full_capture_of_logs(LOG_WARN
);
4643 EXPECT_CONN_PURPOSE(1024, "(unknown)");
4644 expect_single_log_msg_containing("Called with unknown purpose 1024");
4647 teardown_capture_of_logs();
4651 public_server_mode
, (const or_options_t
*options
));
4654 NS(public_server_mode
)(const or_options_t
*options
)
4658 if (CALLED(public_server_mode
)++ == 0) {
4666 test_dir_should_use_directory_guards(void *data
)
4668 or_options_t
*options
;
4669 char *errmsg
= NULL
;
4672 NS_MOCK(public_server_mode
);
4674 options
= options_new();
4675 options_init(options
);
4677 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4678 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 1);
4680 options
->UseEntryGuards
= 1;
4681 options
->DownloadExtraInfo
= 0;
4682 options
->FetchDirInfoEarly
= 0;
4683 options
->FetchDirInfoExtraEarly
= 0;
4684 options
->FetchUselessDescriptors
= 0;
4685 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 1);
4686 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 2);
4688 options
->UseEntryGuards
= 0;
4689 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4690 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 3);
4691 options
->UseEntryGuards
= 1;
4693 options
->DownloadExtraInfo
= 1;
4694 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4695 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 4);
4696 options
->DownloadExtraInfo
= 0;
4698 options
->FetchDirInfoEarly
= 1;
4699 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4700 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 5);
4701 options
->FetchDirInfoEarly
= 0;
4703 options
->FetchDirInfoExtraEarly
= 1;
4704 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4705 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 6);
4706 options
->FetchDirInfoExtraEarly
= 0;
4708 options
->FetchUselessDescriptors
= 1;
4709 tt_int_op(should_use_directory_guards(options
), OP_EQ
, 0);
4710 tt_int_op(CALLED(public_server_mode
), OP_EQ
, 7);
4711 options
->FetchUselessDescriptors
= 0;
4714 NS_UNMOCK(public_server_mode
);
4715 or_options_free(options
);
4720 directory_initiate_request
, (directory_request_t
*req
));
4723 test_dir_should_not_init_request_to_ourselves(void *data
)
4725 char digest
[DIGEST_LEN
];
4726 dir_server_t
*ourself
= NULL
;
4727 crypto_pk_t
*key
= pk_generate(2);
4730 NS_MOCK(directory_initiate_request
);
4732 clear_dir_servers();
4733 routerlist_free_all();
4735 set_server_identity_key(key
);
4736 crypto_pk_get_digest(key
, (char*) &digest
);
4737 ourself
= trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
4739 NULL
, V3_DIRINFO
, 1.0);
4742 dir_server_add(ourself
);
4744 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
, 0, NULL
);
4745 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 0);
4747 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
, 0,
4750 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 0);
4753 NS_UNMOCK(directory_initiate_request
);
4754 clear_dir_servers();
4755 routerlist_free_all();
4756 crypto_pk_free(key
);
4760 test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data
)
4762 dir_server_t
*ds
= NULL
;
4763 dirinfo_type_t dirinfo_type
= BRIDGE_DIRINFO
| EXTRAINFO_DIRINFO \
4764 | MICRODESC_DIRINFO
;
4767 NS_MOCK(directory_initiate_request
);
4769 clear_dir_servers();
4770 routerlist_free_all();
4772 ds
= trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL
,
4773 "12345678901234567890", NULL
, dirinfo_type
, 1.0);
4777 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
, 0, NULL
);
4778 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 0);
4780 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
, 0,
4782 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 0);
4785 NS_UNMOCK(directory_initiate_request
);
4786 clear_dir_servers();
4787 routerlist_free_all();
4791 test_dir_should_init_request_to_dir_auths(void *data
)
4793 dir_server_t
*ds
= NULL
;
4796 NS_MOCK(directory_initiate_request
);
4798 clear_dir_servers();
4799 routerlist_free_all();
4801 ds
= trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL
,
4802 "12345678901234567890", NULL
, V3_DIRINFO
, 1.0);
4806 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
, 0, NULL
);
4807 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 1);
4809 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
, 0,
4811 tt_int_op(CALLED(directory_initiate_request
), OP_EQ
, 2);
4814 NS_UNMOCK(directory_initiate_request
);
4815 clear_dir_servers();
4816 routerlist_free_all();
4820 NS(directory_initiate_request
)(directory_request_t
*req
)
4823 CALLED(directory_initiate_request
)++;
4827 test_dir_choose_compression_level(void* data
)
4831 /* It starts under_memory_pressure */
4832 tt_int_op(have_been_under_memory_pressure(), OP_EQ
, 1);
4834 tt_assert(HIGH_COMPRESSION
== choose_compression_level(-1));
4835 tt_assert(LOW_COMPRESSION
== choose_compression_level(1024-1));
4836 tt_assert(MEDIUM_COMPRESSION
== choose_compression_level(2048-1));
4837 tt_assert(HIGH_COMPRESSION
== choose_compression_level(2048));
4839 /* Reset under_memory_pressure timer */
4840 cell_queues_check_size();
4841 tt_int_op(have_been_under_memory_pressure(), OP_EQ
, 0);
4843 tt_assert(HIGH_COMPRESSION
== choose_compression_level(-1));
4844 tt_assert(HIGH_COMPRESSION
== choose_compression_level(1024-1));
4845 tt_assert(HIGH_COMPRESSION
== choose_compression_level(2048-1));
4846 tt_assert(HIGH_COMPRESSION
== choose_compression_level(2048));
4852 * Mock check_private_dir(), and always succeed - no need to actually
4853 * look at or create anything on the filesystem.
4857 mock_check_private_dir(const char *dirname
, cpd_check_t check
,
4858 const char *effective_user
)
4862 (void)effective_user
;
4868 * This really mocks options_get_datadir_fname2_suffix(), but for testing
4869 * dump_desc(), we only care about get_datadir_fname(sub1), which is defined
4872 * options_get_datadir_fname2_suffix(get_options(), sub1, NULL, NULL)
4876 mock_get_datadir_fname(const or_options_t
*options
,
4877 const char *sub1
, const char *sub2
,
4883 * Assert we were called like get_datadir_fname2() or get_datadir_fname(),
4884 * since that's all we implement here.
4886 tt_ptr_op(options
, OP_NE
, NULL
);
4887 tt_ptr_op(sub1
, OP_NE
, NULL
);
4889 * No particular assertions about sub2, since we could be in the
4890 * get_datadir_fname() or get_datadir_fname2() case.
4892 tt_ptr_op(suffix
, OP_EQ
, NULL
);
4894 /* Just duplicate the basename and return it for this mock */
4896 /* If we have sub2, it's the basename, otherwise sub1 */
4897 rv
= tor_strdup(sub2
);
4899 rv
= tor_strdup(sub1
);
4906 static char *last_unlinked_path
= NULL
;
4907 static int unlinked_count
= 0;
4910 mock_unlink_reset(void)
4912 tor_free(last_unlinked_path
);
4917 mock_unlink(const char *path
)
4919 tt_ptr_op(path
, OP_NE
, NULL
);
4921 tor_free(last_unlinked_path
);
4922 last_unlinked_path
= tor_strdup(path
);
4929 static char *last_write_str_path
= NULL
;
4930 static uint8_t last_write_str_hash
[DIGEST256_LEN
];
4931 static int write_str_count
= 0;
4934 mock_write_str_to_file_reset(void)
4936 tor_free(last_write_str_path
);
4937 write_str_count
= 0;
4941 mock_write_str_to_file(const char *path
, const char *str
, int bin
)
4944 uint8_t hash
[DIGEST256_LEN
];
4948 tt_ptr_op(path
, OP_NE
, NULL
);
4949 tt_ptr_op(str
, OP_NE
, NULL
);
4952 crypto_digest256((char *)hash
, str
, len
, DIGEST_SHA256
);
4954 tor_free(last_write_str_path
);
4955 last_write_str_path
= tor_strdup(path
);
4956 memcpy(last_write_str_hash
, hash
, sizeof(last_write_str_hash
));
4964 test_dir_dump_unparseable_descriptors(void *data
)
4967 * These bogus descriptors look nothing at all like real bogus descriptors
4968 * we might see, but we're only testing dump_desc() here, not the parser.
4970 const char *test_desc_type
= "squamous";
4971 /* strlen(test_desc_1) = 583 bytes */
4972 const char *test_desc_1
=
4973 "The most merciful thing in the world, I think, is the inability of the "
4974 "human mind to correlate all its contents. We live on a placid island of"
4975 " ignorance in the midst of black seas of infinity, and it was not meant"
4976 " that we should voyage far. The sciences, each straining in its own dir"
4977 "ection, have hitherto harmed us little; but some day the piecing togeth"
4978 "er of dissociated knowledge will open up such terrifying vistas of real"
4979 "ity, and of our frightful position therein, that we shall either go mad"
4980 "from the revelation or flee from the light into the peace and safety of"
4982 uint8_t test_desc_1_hash
[DIGEST256_LEN
];
4983 char test_desc_1_hash_str
[HEX_DIGEST256_LEN
+1];
4984 /* strlen(test_desc_2) = 650 bytes */
4985 const char *test_desc_2
=
4986 "I think their predominant colour was a greyish-green, though they had w"
4987 "hite bellies. They were mostly shiny and slippery, but the ridges of th"
4988 "eir backs were scaly. Their forms vaguely suggested the anthropoid, whi"
4989 "le their heads were the heads of fish, with prodigious bulging eyes tha"
4990 "t never closed. At the sides of their necks were palpitating gills, and"
4991 "their long paws were webbed. They hopped irregularly, sometimes on two "
4992 "legs and sometimes on four. I was somehow glad that they had no more th"
4993 "an four limbs. Their croaking, baying voices, clearly wed tar articulat"
4994 "e speech, held all the dark shades of expression which their staring fa"
4996 uint8_t test_desc_2_hash
[DIGEST256_LEN
];
4997 char test_desc_2_hash_str
[HEX_DIGEST256_LEN
+1];
4998 /* strlen(test_desc_3) = 700 bytes */
4999 const char *test_desc_3
=
5000 "Without knowing what futurism is like, Johansen achieved something very"
5001 "close to it when he spoke of the city; for instead of describing any de"
5002 "finite structure or building, he dwells only on broad impressions of va"
5003 "st angles and stone surfaces - surfaces too great to belong to anything"
5004 "right or proper for this earth, and impious with horrible images and hi"
5005 "eroglyphs. I mention his talk about angles because it suggests somethin"
5006 "g Wilcox had told me of his awful dreams. He said that the geometry of "
5007 "the dream-place he saw was abnormal, non-Euclidean, and loathsomely red"
5008 "olent of spheres and dimensions apart from ours. Now an unlettered seam"
5009 "an felt the same thing whilst gazing at the terrible reality.";
5010 uint8_t test_desc_3_hash
[DIGEST256_LEN
];
5011 char test_desc_3_hash_str
[HEX_DIGEST256_LEN
+1];
5012 /* strlen(test_desc_3) = 604 bytes */
5013 const char *test_desc_4
=
5014 "So we glanced back simultaneously, it would appear; though no doubt the"
5015 "incipient motion of one prompted the imitation of the other. As we did "
5016 "so we flashed both torches full strength at the momentarily thinned mis"
5017 "t; either from sheer primitive anxiety to see all we could, or in a les"
5018 "s primitive but equally unconscious effort to dazzle the entity before "
5019 "we dimmed our light and dodged among the penguins of the labyrinth cent"
5020 "er ahead. Unhappy act! Not Orpheus himself, or Lot's wife, paid much mo"
5021 "re dearly for a backward glance. And again came that shocking, wide-ran"
5022 "ged piping - \"Tekeli-li! Tekeli-li!\"";
5023 uint8_t test_desc_4_hash
[DIGEST256_LEN
];
5024 char test_desc_4_hash_str
[HEX_DIGEST256_LEN
+1];
5028 * Set up options mock so we can force a tiny FIFO size and generate
5031 mock_options
= tor_malloc(sizeof(or_options_t
));
5032 reset_options(mock_options
, &mock_get_options_calls
);
5033 mock_options
->MaxUnparseableDescSizeToLog
= 1536;
5034 MOCK(get_options
, mock_get_options
);
5035 MOCK(check_private_dir
, mock_check_private_dir
);
5036 MOCK(options_get_datadir_fname2_suffix
,
5037 mock_get_datadir_fname
);
5040 * Set up unlink and write mocks
5042 MOCK(tor_unlink
, mock_unlink
);
5043 mock_unlink_reset();
5044 MOCK(write_str_to_file
, mock_write_str_to_file
);
5045 mock_write_str_to_file_reset();
5048 * Compute hashes we'll need to recognize which descriptor is which
5050 crypto_digest256((char *)test_desc_1_hash
, test_desc_1
,
5051 strlen(test_desc_1
), DIGEST_SHA256
);
5052 base16_encode(test_desc_1_hash_str
, sizeof(test_desc_1_hash_str
),
5053 (const char *)test_desc_1_hash
,
5054 sizeof(test_desc_1_hash
));
5055 crypto_digest256((char *)test_desc_2_hash
, test_desc_2
,
5056 strlen(test_desc_2
), DIGEST_SHA256
);
5057 base16_encode(test_desc_2_hash_str
, sizeof(test_desc_2_hash_str
),
5058 (const char *)test_desc_2_hash
,
5059 sizeof(test_desc_2_hash
));
5060 crypto_digest256((char *)test_desc_3_hash
, test_desc_3
,
5061 strlen(test_desc_3
), DIGEST_SHA256
);
5062 base16_encode(test_desc_3_hash_str
, sizeof(test_desc_3_hash_str
),
5063 (const char *)test_desc_3_hash
,
5064 sizeof(test_desc_3_hash
));
5065 crypto_digest256((char *)test_desc_4_hash
, test_desc_4
,
5066 strlen(test_desc_4
), DIGEST_SHA256
);
5067 base16_encode(test_desc_4_hash_str
, sizeof(test_desc_4_hash_str
),
5068 (const char *)test_desc_4_hash
,
5069 sizeof(test_desc_4_hash
));
5072 * Reset the FIFO and check its state
5074 dump_desc_fifo_cleanup();
5075 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5076 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5079 * (1) Fire off dump_desc() once; these descriptors should all be safely
5080 * smaller than configured FIFO size.
5083 dump_desc(test_desc_1
, test_desc_type
);
5086 * Assert things about the FIFO state
5088 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_1
));
5089 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5092 * Assert things about the mocks
5094 tt_int_op(unlinked_count
, OP_EQ
, 0);
5095 tt_int_op(write_str_count
, OP_EQ
, 1);
5096 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_1_hash
, DIGEST_SHA256
);
5099 * Reset the FIFO and check its state
5101 dump_desc_fifo_cleanup();
5102 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5103 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5106 * Reset the mocks and check their state
5108 mock_unlink_reset();
5109 mock_write_str_to_file_reset();
5110 tt_int_op(unlinked_count
, OP_EQ
, 0);
5111 tt_int_op(write_str_count
, OP_EQ
, 0);
5114 * (2) Fire off dump_desc() twice; this still should trigger no cleanup.
5118 dump_desc(test_desc_2
, test_desc_type
);
5121 * Assert things about the FIFO state
5123 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_2
));
5124 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5127 * Assert things about the mocks
5129 tt_int_op(unlinked_count
, OP_EQ
, 0);
5130 tt_int_op(write_str_count
, OP_EQ
, 1);
5131 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_2_hash
, DIGEST_SHA256
);
5134 dump_desc(test_desc_3
, test_desc_type
);
5137 * Assert things about the FIFO state
5139 tt_u64_op(len_descs_dumped
, OP_EQ
,
5140 strlen(test_desc_2
) + strlen(test_desc_3
));
5141 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5144 * Assert things about the mocks
5146 tt_int_op(unlinked_count
, OP_EQ
, 0);
5147 tt_int_op(write_str_count
, OP_EQ
, 2);
5148 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_3_hash
, DIGEST_SHA256
);
5151 * Reset the FIFO and check its state
5153 dump_desc_fifo_cleanup();
5154 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5155 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5158 * Reset the mocks and check their state
5160 mock_unlink_reset();
5161 mock_write_str_to_file_reset();
5162 tt_int_op(unlinked_count
, OP_EQ
, 0);
5163 tt_int_op(write_str_count
, OP_EQ
, 0);
5166 * (3) Three calls to dump_desc cause a FIFO cleanup
5170 dump_desc(test_desc_4
, test_desc_type
);
5173 * Assert things about the FIFO state
5175 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_4
));
5176 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5179 * Assert things about the mocks
5181 tt_int_op(unlinked_count
, OP_EQ
, 0);
5182 tt_int_op(write_str_count
, OP_EQ
, 1);
5183 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5186 dump_desc(test_desc_1
, test_desc_type
);
5189 * Assert things about the FIFO state
5191 tt_u64_op(len_descs_dumped
, OP_EQ
,
5192 strlen(test_desc_4
) + strlen(test_desc_1
));
5193 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5196 * Assert things about the mocks
5198 tt_int_op(unlinked_count
, OP_EQ
, 0);
5199 tt_int_op(write_str_count
, OP_EQ
, 2);
5200 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_1_hash
, DIGEST_SHA256
);
5202 /* Third time - we should unlink the dump of test_desc_4 here */
5203 dump_desc(test_desc_2
, test_desc_type
);
5206 * Assert things about the FIFO state
5208 tt_u64_op(len_descs_dumped
, OP_EQ
,
5209 strlen(test_desc_1
) + strlen(test_desc_2
));
5210 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5213 * Assert things about the mocks
5215 tt_int_op(unlinked_count
, OP_EQ
, 1);
5216 tt_int_op(write_str_count
, OP_EQ
, 3);
5217 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_2_hash
, DIGEST_SHA256
);
5220 * Reset the FIFO and check its state
5222 dump_desc_fifo_cleanup();
5223 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5224 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5227 * Reset the mocks and check their state
5229 mock_unlink_reset();
5230 mock_write_str_to_file_reset();
5231 tt_int_op(unlinked_count
, OP_EQ
, 0);
5232 tt_int_op(write_str_count
, OP_EQ
, 0);
5235 * (4) But repeating one (A B B) doesn't overflow and cleanup
5239 dump_desc(test_desc_3
, test_desc_type
);
5242 * Assert things about the FIFO state
5244 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_3
));
5245 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5248 * Assert things about the mocks
5250 tt_int_op(unlinked_count
, OP_EQ
, 0);
5251 tt_int_op(write_str_count
, OP_EQ
, 1);
5252 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_3_hash
, DIGEST_SHA256
);
5255 dump_desc(test_desc_4
, test_desc_type
);
5258 * Assert things about the FIFO state
5260 tt_u64_op(len_descs_dumped
, OP_EQ
,
5261 strlen(test_desc_3
) + strlen(test_desc_4
));
5262 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5265 * Assert things about the mocks
5267 tt_int_op(unlinked_count
, OP_EQ
, 0);
5268 tt_int_op(write_str_count
, OP_EQ
, 2);
5269 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5272 dump_desc(test_desc_4
, test_desc_type
);
5275 * Assert things about the FIFO state
5277 tt_u64_op(len_descs_dumped
, OP_EQ
,
5278 strlen(test_desc_3
) + strlen(test_desc_4
));
5279 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5282 * Assert things about the mocks
5284 tt_int_op(unlinked_count
, OP_EQ
, 0);
5285 tt_int_op(write_str_count
, OP_EQ
, 2);
5286 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5289 * Reset the FIFO and check its state
5291 dump_desc_fifo_cleanup();
5292 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5293 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5296 * Reset the mocks and check their state
5298 mock_unlink_reset();
5299 mock_write_str_to_file_reset();
5300 tt_int_op(unlinked_count
, OP_EQ
, 0);
5301 tt_int_op(write_str_count
, OP_EQ
, 0);
5304 * (5) Same for the (A B A) repetition
5308 dump_desc(test_desc_1
, test_desc_type
);
5311 * Assert things about the FIFO state
5313 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_1
));
5314 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5317 * Assert things about the mocks
5319 tt_int_op(unlinked_count
, OP_EQ
, 0);
5320 tt_int_op(write_str_count
, OP_EQ
, 1);
5321 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_1_hash
, DIGEST_SHA256
);
5324 dump_desc(test_desc_2
, test_desc_type
);
5327 * Assert things about the FIFO state
5329 tt_u64_op(len_descs_dumped
, OP_EQ
,
5330 strlen(test_desc_1
) + strlen(test_desc_2
));
5331 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5334 * Assert things about the mocks
5336 tt_int_op(unlinked_count
, OP_EQ
, 0);
5337 tt_int_op(write_str_count
, OP_EQ
, 2);
5338 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_2_hash
, DIGEST_SHA256
);
5341 dump_desc(test_desc_1
, test_desc_type
);
5344 * Assert things about the FIFO state
5346 tt_u64_op(len_descs_dumped
, OP_EQ
,
5347 strlen(test_desc_1
) + strlen(test_desc_2
));
5348 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5351 * Assert things about the mocks
5353 tt_int_op(unlinked_count
, OP_EQ
, 0);
5354 tt_int_op(write_str_count
, OP_EQ
, 2);
5355 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_2_hash
, DIGEST_SHA256
);
5358 * Reset the FIFO and check its state
5360 dump_desc_fifo_cleanup();
5361 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5362 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5365 * Reset the mocks and check their state
5367 mock_unlink_reset();
5368 mock_write_str_to_file_reset();
5369 tt_int_op(unlinked_count
, OP_EQ
, 0);
5370 tt_int_op(write_str_count
, OP_EQ
, 0);
5373 * (6) (A B B C) triggering overflow on C causes A, not B to be unlinked
5377 dump_desc(test_desc_3
, test_desc_type
);
5380 * Assert things about the FIFO state
5382 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_3
));
5383 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5386 * Assert things about the mocks
5388 tt_int_op(unlinked_count
, OP_EQ
, 0);
5389 tt_int_op(write_str_count
, OP_EQ
, 1);
5390 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_3_hash
, DIGEST_SHA256
);
5393 dump_desc(test_desc_4
, test_desc_type
);
5396 * Assert things about the FIFO state
5398 tt_u64_op(len_descs_dumped
, OP_EQ
,
5399 strlen(test_desc_3
) + strlen(test_desc_4
));
5400 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5403 * Assert things about the mocks
5405 tt_int_op(unlinked_count
, OP_EQ
, 0);
5406 tt_int_op(write_str_count
, OP_EQ
, 2);
5407 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5410 dump_desc(test_desc_4
, test_desc_type
);
5413 * Assert things about the FIFO state
5415 tt_u64_op(len_descs_dumped
, OP_EQ
,
5416 strlen(test_desc_3
) + strlen(test_desc_4
));
5417 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5420 * Assert things about the mocks
5422 tt_int_op(unlinked_count
, OP_EQ
, 0);
5423 tt_int_op(write_str_count
, OP_EQ
, 2);
5424 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5426 /* Fourth time - we should unlink the dump of test_desc_3 here */
5427 dump_desc(test_desc_1
, test_desc_type
);
5430 * Assert things about the FIFO state
5432 tt_u64_op(len_descs_dumped
, OP_EQ
,
5433 strlen(test_desc_4
) + strlen(test_desc_1
));
5434 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5437 * Assert things about the mocks
5439 tt_int_op(unlinked_count
, OP_EQ
, 1);
5440 tt_int_op(write_str_count
, OP_EQ
, 3);
5441 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_1_hash
, DIGEST_SHA256
);
5444 * Reset the FIFO and check its state
5446 dump_desc_fifo_cleanup();
5447 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5448 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5451 * Reset the mocks and check their state
5453 mock_unlink_reset();
5454 mock_write_str_to_file_reset();
5455 tt_int_op(unlinked_count
, OP_EQ
, 0);
5456 tt_int_op(write_str_count
, OP_EQ
, 0);
5459 * (7) (A B A C) triggering overflow on C causes B, not A to be unlinked
5463 dump_desc(test_desc_2
, test_desc_type
);
5466 * Assert things about the FIFO state
5468 tt_u64_op(len_descs_dumped
, OP_EQ
, strlen(test_desc_2
));
5469 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 1);
5472 * Assert things about the mocks
5474 tt_int_op(unlinked_count
, OP_EQ
, 0);
5475 tt_int_op(write_str_count
, OP_EQ
, 1);
5476 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_2_hash
, DIGEST_SHA256
);
5479 dump_desc(test_desc_3
, test_desc_type
);
5482 * Assert things about the FIFO state
5484 tt_u64_op(len_descs_dumped
, OP_EQ
,
5485 strlen(test_desc_2
) + strlen(test_desc_3
));
5486 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5489 * Assert things about the mocks
5491 tt_int_op(unlinked_count
, OP_EQ
, 0);
5492 tt_int_op(write_str_count
, OP_EQ
, 2);
5493 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_3_hash
, DIGEST_SHA256
);
5496 dump_desc(test_desc_2
, test_desc_type
);
5499 * Assert things about the FIFO state
5501 tt_u64_op(len_descs_dumped
, OP_EQ
,
5502 strlen(test_desc_2
) + strlen(test_desc_3
));
5503 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5506 * Assert things about the mocks
5508 tt_int_op(unlinked_count
, OP_EQ
, 0);
5509 tt_int_op(write_str_count
, OP_EQ
, 2);
5510 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_3_hash
, DIGEST_SHA256
);
5512 /* Fourth time - we should unlink the dump of test_desc_3 here */
5513 dump_desc(test_desc_4
, test_desc_type
);
5516 * Assert things about the FIFO state
5518 tt_u64_op(len_descs_dumped
, OP_EQ
,
5519 strlen(test_desc_2
) + strlen(test_desc_4
));
5520 tt_assert(descs_dumped
!= NULL
&& smartlist_len(descs_dumped
) == 2);
5523 * Assert things about the mocks
5525 tt_int_op(unlinked_count
, OP_EQ
, 1);
5526 tt_int_op(write_str_count
, OP_EQ
, 3);
5527 tt_mem_op(last_write_str_hash
, OP_EQ
, test_desc_4_hash
, DIGEST_SHA256
);
5530 * Reset the FIFO and check its state
5532 dump_desc_fifo_cleanup();
5533 tt_u64_op(len_descs_dumped
, OP_EQ
, 0);
5534 tt_assert(descs_dumped
== NULL
|| smartlist_len(descs_dumped
) == 0);
5537 * Reset the mocks and check their state
5539 mock_unlink_reset();
5540 mock_write_str_to_file_reset();
5541 tt_int_op(unlinked_count
, OP_EQ
, 0);
5542 tt_int_op(write_str_count
, OP_EQ
, 0);
5546 /* Clean up the fifo */
5547 dump_desc_fifo_cleanup();
5551 mock_unlink_reset();
5552 UNMOCK(write_str_to_file
);
5553 mock_write_str_to_file_reset();
5554 UNMOCK(options_get_datadir_fname2_suffix
);
5555 UNMOCK(check_private_dir
);
5556 UNMOCK(get_options
);
5557 tor_free(mock_options
);
5558 mock_options
= NULL
;
5563 /* Variables for reset_read_file_to_str_mock() */
5565 static int enforce_expected_filename
= 0;
5566 static char *expected_filename
= NULL
;
5567 static char *file_content
= NULL
;
5568 static size_t file_content_len
= 0;
5569 static struct stat file_stat
;
5570 static int read_count
= 0, read_call_count
= 0;
5573 reset_read_file_to_str_mock(void)
5575 tor_free(expected_filename
);
5576 tor_free(file_content
);
5577 file_content_len
= 0;
5578 memset(&file_stat
, 0, sizeof(file_stat
));
5580 read_call_count
= 0;
5584 read_file_to_str_mock(const char *filename
, int flags
,
5585 struct stat
*stat_out
) {
5586 char *result
= NULL
;
5588 /* Insist we got a filename */
5589 tt_ptr_op(filename
, OP_NE
, NULL
);
5591 /* We ignore flags */
5594 /* Bump the call count */
5597 if (enforce_expected_filename
) {
5598 tt_assert(expected_filename
);
5599 tt_str_op(filename
, OP_EQ
, expected_filename
);
5602 if (expected_filename
!= NULL
&&
5603 file_content
!= NULL
&&
5604 strcmp(filename
, expected_filename
) == 0) {
5605 /* You asked for it, you got it */
5608 * This is the same behavior as the real read_file_to_str();
5609 * if there's a NUL, the real size ends up in stat_out.
5611 result
= tor_malloc(file_content_len
+ 1);
5612 if (file_content_len
> 0) {
5613 memcpy(result
, file_content
, file_content_len
);
5615 result
[file_content_len
] = '\0';
5617 /* Do we need to set up stat_out? */
5618 if (stat_out
!= NULL
) {
5619 memcpy(stat_out
, &file_stat
, sizeof(file_stat
));
5620 /* We always return the correct length here */
5621 stat_out
->st_size
= file_content_len
;
5624 /* Wooo, we have a return value - bump the counter */
5627 /* else no match, return NULL */
5633 /* This one tests dump_desc_populate_one_file() */
5635 test_dir_populate_dump_desc_fifo(void *data
)
5637 const char *dirname
= "foo";
5638 const char *fname
= NULL
;
5644 * Set up unlink and read_file_to_str mocks
5646 MOCK(tor_unlink
, mock_unlink
);
5647 mock_unlink_reset();
5648 MOCK(read_file_to_str
, read_file_to_str_mock
);
5649 reset_read_file_to_str_mock();
5651 /* Check state of unlink mock */
5652 tt_int_op(unlinked_count
, OP_EQ
, 0);
5654 /* Some cases that should fail before trying to read the file */
5655 ent
= dump_desc_populate_one_file(dirname
, "bar");
5656 tt_ptr_op(ent
, OP_EQ
, NULL
);
5657 tt_int_op(unlinked_count
, OP_EQ
, 1);
5658 tt_int_op(read_count
, OP_EQ
, 0);
5659 tt_int_op(read_call_count
, OP_EQ
, 0);
5661 ent
= dump_desc_populate_one_file(dirname
, "unparseable-desc");
5662 tt_ptr_op(ent
, OP_EQ
, NULL
);
5663 tt_int_op(unlinked_count
, OP_EQ
, 2);
5664 tt_int_op(read_count
, OP_EQ
, 0);
5665 tt_int_op(read_call_count
, OP_EQ
, 0);
5667 ent
= dump_desc_populate_one_file(dirname
, "unparseable-desc.baz");
5668 tt_ptr_op(ent
, OP_EQ
, NULL
);
5669 tt_int_op(unlinked_count
, OP_EQ
, 3);
5670 tt_int_op(read_count
, OP_EQ
, 0);
5671 tt_int_op(read_call_count
, OP_EQ
, 0);
5673 ent
= dump_desc_populate_one_file(
5675 "unparseable-desc.08AE85E90461F59E");
5676 tt_ptr_op(ent
, OP_EQ
, NULL
);
5677 tt_int_op(unlinked_count
, OP_EQ
, 4);
5678 tt_int_op(read_count
, OP_EQ
, 0);
5679 tt_int_op(read_call_count
, OP_EQ
, 0);
5681 ent
= dump_desc_populate_one_file(
5683 "unparseable-desc.08AE85E90461F59EDF0981323F3A70D02B55AB54B44B04F"
5684 "287D72F7B72F242E85C8CB0EDA8854A99");
5685 tt_ptr_op(ent
, OP_EQ
, NULL
);
5686 tt_int_op(unlinked_count
, OP_EQ
, 5);
5687 tt_int_op(read_count
, OP_EQ
, 0);
5688 tt_int_op(read_call_count
, OP_EQ
, 0);
5690 /* This is a correct-length digest but base16_decode() will fail */
5691 ent
= dump_desc_populate_one_file(
5693 "unparseable-desc.68219B8BGE64B705A6FFC728C069DC596216D60A7D7520C"
5694 "D5ECE250D912E686B");
5695 tt_ptr_op(ent
, OP_EQ
, NULL
);
5696 tt_int_op(unlinked_count
, OP_EQ
, 6);
5697 tt_int_op(read_count
, OP_EQ
, 0);
5698 tt_int_op(read_call_count
, OP_EQ
, 0);
5700 /* This one has a correctly formed filename and should try reading */
5703 ent
= dump_desc_populate_one_file(
5705 "unparseable-desc.DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E"
5706 "85C8CB0EDA8854A99");
5707 tt_ptr_op(ent
, OP_EQ
, NULL
);
5708 tt_int_op(unlinked_count
, OP_EQ
, 7);
5709 tt_int_op(read_count
, OP_EQ
, 0);
5710 tt_int_op(read_call_count
, OP_EQ
, 1);
5712 /* This read will succeed but the digest won't match the file content */
5715 "DF0981323F3A70D02B55AB54B44B04F287D72F7B72F242E85C8CB0EDA8854A99";
5716 enforce_expected_filename
= 1;
5717 tor_asprintf(&expected_filename
, "%s%s%s", dirname
, PATH_SEPARATOR
, fname
);
5718 file_content
= tor_strdup("hanc culpam maiorem an illam dicam?");
5719 file_content_len
= strlen(file_content
);
5720 file_stat
.st_mtime
= 123456;
5721 ent
= dump_desc_populate_one_file(dirname
, fname
);
5722 enforce_expected_filename
= 0;
5723 tt_ptr_op(ent
, OP_EQ
, NULL
);
5724 tt_int_op(unlinked_count
, OP_EQ
, 8);
5725 tt_int_op(read_count
, OP_EQ
, 1);
5726 tt_int_op(read_call_count
, OP_EQ
, 2);
5727 tor_free(expected_filename
);
5728 tor_free(file_content
);
5730 /* This one will match */
5733 "0786C7173447B7FB033FFCA2FC47C3CF71C30DD47CA8236D3FC7FF35853271C6";
5734 tor_asprintf(&expected_filename
, "%s%s%s", dirname
, PATH_SEPARATOR
, fname
);
5735 file_content
= tor_strdup("hanc culpam maiorem an illam dicam?");
5736 file_content_len
= strlen(file_content
);
5737 file_stat
.st_mtime
= 789012;
5738 ent
= dump_desc_populate_one_file(dirname
, fname
);
5739 tt_ptr_op(ent
, OP_NE
, NULL
);
5740 tt_int_op(unlinked_count
, OP_EQ
, 8);
5741 tt_int_op(read_count
, OP_EQ
, 2);
5742 tt_int_op(read_call_count
, OP_EQ
, 3);
5743 tt_str_op(ent
->filename
, OP_EQ
, expected_filename
);
5744 tt_int_op(ent
->len
, OP_EQ
, file_content_len
);
5745 tt_int_op(ent
->when
, OP_EQ
, file_stat
.st_mtime
);
5746 tor_free(ent
->filename
);
5748 tor_free(expected_filename
);
5751 * Reset the mocks and check their state
5753 mock_unlink_reset();
5754 tt_int_op(unlinked_count
, OP_EQ
, 0);
5755 reset_read_file_to_str_mock();
5756 tt_int_op(read_count
, OP_EQ
, 0);
5761 mock_unlink_reset();
5762 UNMOCK(read_file_to_str
);
5763 reset_read_file_to_str_mock();
5765 tor_free(file_content
);
5770 static smartlist_t
*
5771 listdir_mock(const char *dname
)
5775 /* Ignore the name, always return this list */
5778 l
= smartlist_new();
5779 smartlist_add_strdup(l
, "foo");
5780 smartlist_add_strdup(l
, "bar");
5781 smartlist_add_strdup(l
, "baz");
5786 static dumped_desc_t
*
5787 pop_one_mock(const char *dirname
, const char *f
)
5789 dumped_desc_t
*ent
= NULL
;
5791 if (dirname
!= NULL
&& strcmp(dirname
, "d") == 0) {
5792 if (f
!= NULL
&& strcmp(f
, "foo") == 0) {
5793 ent
= tor_malloc_zero(sizeof(*ent
));
5794 ent
->filename
= tor_strdup("d/foo");
5796 ent
->digest_sha256
[0] = 1;
5798 } else if (f
!= NULL
&& strcmp(f
, "bar") == 0) {
5799 ent
= tor_malloc_zero(sizeof(*ent
));
5800 ent
->filename
= tor_strdup("d/bar");
5802 ent
->digest_sha256
[0] = 2;
5804 * Note that the timestamps are in a different order than
5805 * listdir_mock() returns; we're testing the sort order.
5808 } else if (f
!= NULL
&& strcmp(f
, "baz") == 0) {
5809 ent
= tor_malloc_zero(sizeof(*ent
));
5810 ent
->filename
= tor_strdup("d/baz");
5812 ent
->digest_sha256
[0] = 3;
5820 /* This one tests dump_desc_populate_fifo_from_directory() */
5822 test_dir_populate_dump_desc_fifo_2(void *data
)
5824 dumped_desc_t
*ent
= NULL
;
5828 /* Set up the mocks */
5829 MOCK(tor_listdir
, listdir_mock
);
5830 MOCK(dump_desc_populate_one_file
, pop_one_mock
);
5832 /* Run dump_desc_populate_fifo_from_directory() */
5833 descs_dumped
= NULL
;
5834 len_descs_dumped
= 0;
5835 dump_desc_populate_fifo_from_directory("d");
5836 tt_assert(descs_dumped
!= NULL
);
5837 tt_int_op(smartlist_len(descs_dumped
), OP_EQ
, 3);
5838 tt_u64_op(len_descs_dumped
, OP_EQ
, 1368);
5839 ent
= smartlist_get(descs_dumped
, 0);
5840 tt_str_op(ent
->filename
, OP_EQ
, "d/bar");
5841 tt_int_op(ent
->len
, OP_EQ
, 456);
5842 tt_int_op(ent
->when
, OP_EQ
, 512);
5843 ent
= smartlist_get(descs_dumped
, 1);
5844 tt_str_op(ent
->filename
, OP_EQ
, "d/baz");
5845 tt_int_op(ent
->len
, OP_EQ
, 789);
5846 tt_int_op(ent
->when
, OP_EQ
, 768);
5847 ent
= smartlist_get(descs_dumped
, 2);
5848 tt_str_op(ent
->filename
, OP_EQ
, "d/foo");
5849 tt_int_op(ent
->len
, OP_EQ
, 123);
5850 tt_int_op(ent
->when
, OP_EQ
, 1024);
5853 dump_desc_fifo_cleanup();
5855 UNMOCK(dump_desc_populate_one_file
);
5856 UNMOCK(tor_listdir
);
5861 static int mock_networkstatus_consensus_is_bootstrapping_value
= 0;
5863 mock_networkstatus_consensus_is_bootstrapping(time_t now
)
5866 return mock_networkstatus_consensus_is_bootstrapping_value
;
5869 static int mock_networkstatus_consensus_can_use_extra_fallbacks_value
= 0;
5871 mock_networkstatus_consensus_can_use_extra_fallbacks(
5872 const or_options_t
*options
)
5875 return mock_networkstatus_consensus_can_use_extra_fallbacks_value
;
5878 static int mock_num_bridges_usable_value
= 0;
5880 mock_num_bridges_usable(void)
5882 return mock_num_bridges_usable_value
;
5885 /* data is a 3 character nul-terminated string.
5886 * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
5887 * If data[1] is 'f', set extra fallbacks, anything else means no extra
5888 * If data[2] is 'f', set running bridges, anything else means no extra
5892 test_dir_find_dl_schedule(void* data
)
5894 const char *str
= (const char *)data
;
5896 tt_assert(strlen(data
) == 3);
5898 if (str
[0] == 'b') {
5899 mock_networkstatus_consensus_is_bootstrapping_value
= 1;
5901 mock_networkstatus_consensus_is_bootstrapping_value
= 0;
5904 if (str
[1] == 'f') {
5905 mock_networkstatus_consensus_can_use_extra_fallbacks_value
= 1;
5907 mock_networkstatus_consensus_can_use_extra_fallbacks_value
= 0;
5910 if (str
[2] == 'r') {
5911 /* Any positive, non-zero value should work */
5912 mock_num_bridges_usable_value
= 2;
5914 mock_num_bridges_usable_value
= 0;
5917 MOCK(networkstatus_consensus_is_bootstrapping
,
5918 mock_networkstatus_consensus_is_bootstrapping
);
5919 MOCK(networkstatus_consensus_can_use_extra_fallbacks
,
5920 mock_networkstatus_consensus_can_use_extra_fallbacks
);
5921 MOCK(num_bridges_usable
,
5922 mock_num_bridges_usable
);
5924 download_status_t dls
;
5925 smartlist_t server
, client
, server_cons
, client_cons
;
5926 smartlist_t client_boot_auth_only_cons
, client_boot_auth_cons
;
5927 smartlist_t client_boot_fallback_cons
, bridge
, bridge_bootstrap
;
5929 mock_options
= tor_malloc(sizeof(or_options_t
));
5930 reset_options(mock_options
, &mock_get_options_calls
);
5931 MOCK(get_options
, mock_get_options
);
5933 mock_options
->TestingServerDownloadSchedule
= &server
;
5934 mock_options
->TestingClientDownloadSchedule
= &client
;
5935 mock_options
->TestingServerConsensusDownloadSchedule
= &server_cons
;
5936 mock_options
->TestingClientConsensusDownloadSchedule
= &client_cons
;
5937 mock_options
->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule
=
5938 &client_boot_auth_only_cons
;
5939 mock_options
->ClientBootstrapConsensusAuthorityDownloadSchedule
=
5940 &client_boot_auth_cons
;
5941 mock_options
->ClientBootstrapConsensusFallbackDownloadSchedule
=
5942 &client_boot_fallback_cons
;
5943 mock_options
->TestingBridgeDownloadSchedule
= &bridge
;
5944 mock_options
->TestingBridgeBootstrapDownloadSchedule
= &bridge_bootstrap
;
5946 dls
.schedule
= DL_SCHED_GENERIC
;
5948 mock_options
->ClientOnly
= 1;
5949 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
, &client
);
5950 mock_options
->ClientOnly
= 0;
5953 mock_options
->DirPort_set
= 1;
5954 mock_options
->DirCache
= 1;
5955 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
, &server
);
5956 mock_options
->DirPort_set
= 0;
5957 mock_options
->DirCache
= 0;
5959 dls
.schedule
= DL_SCHED_CONSENSUS
;
5960 /* public server mode */
5961 mock_options
->ORPort_set
= 1;
5962 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
, &server_cons
);
5963 mock_options
->ORPort_set
= 0;
5965 /* client and bridge modes */
5966 if (networkstatus_consensus_is_bootstrapping(time(NULL
))) {
5967 if (networkstatus_consensus_can_use_extra_fallbacks(mock_options
)) {
5968 dls
.want_authority
= 1;
5970 mock_options
->ClientOnly
= 1;
5971 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
5972 &client_boot_auth_cons
);
5973 mock_options
->ClientOnly
= 0;
5976 mock_options
->ORPort_set
= 1;
5977 mock_options
->BridgeRelay
= 1;
5978 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
5979 &client_boot_auth_cons
);
5980 mock_options
->ORPort_set
= 0;
5981 mock_options
->BridgeRelay
= 0;
5983 dls
.want_authority
= 0;
5985 mock_options
->ClientOnly
= 1;
5986 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
5987 &client_boot_fallback_cons
);
5988 mock_options
->ClientOnly
= 0;
5991 mock_options
->ORPort_set
= 1;
5992 mock_options
->BridgeRelay
= 1;
5993 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
5994 &client_boot_fallback_cons
);
5995 mock_options
->ORPort_set
= 0;
5996 mock_options
->BridgeRelay
= 0;
5999 /* dls.want_authority is ignored */
6001 mock_options
->ClientOnly
= 1;
6002 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
6003 &client_boot_auth_only_cons
);
6004 mock_options
->ClientOnly
= 0;
6007 mock_options
->ORPort_set
= 1;
6008 mock_options
->BridgeRelay
= 1;
6009 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
6010 &client_boot_auth_only_cons
);
6011 mock_options
->ORPort_set
= 0;
6012 mock_options
->BridgeRelay
= 0;
6016 mock_options
->ClientOnly
= 1;
6017 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
6019 mock_options
->ClientOnly
= 0;
6022 mock_options
->ORPort_set
= 1;
6023 mock_options
->BridgeRelay
= 1;
6024 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
,
6026 mock_options
->ORPort_set
= 0;
6027 mock_options
->BridgeRelay
= 0;
6030 dls
.schedule
= DL_SCHED_BRIDGE
;
6032 mock_options
->ClientOnly
= 1;
6033 mock_options
->UseBridges
= 1;
6034 if (num_bridges_usable() > 0) {
6035 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
, &bridge
);
6037 tt_ptr_op(find_dl_schedule(&dls
, mock_options
), OP_EQ
, &bridge_bootstrap
);
6041 UNMOCK(networkstatus_consensus_is_bootstrapping
);
6042 UNMOCK(networkstatus_consensus_can_use_extra_fallbacks
);
6043 UNMOCK(num_bridges_usable
);
6044 UNMOCK(get_options
);
6045 tor_free(mock_options
);
6046 mock_options
= NULL
;
6050 test_dir_assumed_flags(void *arg
)
6053 smartlist_t
*tokens
= smartlist_new();
6054 memarea_t
*area
= memarea_new();
6055 routerstatus_t
*rs
= NULL
;
6057 /* First, we should always assume that the Running flag is set, even
6058 * when it isn't listed, since the consensus method is always
6061 "r example hereiswhereyouridentitygoes 2015-08-30 12:00:00 "
6062 "192.168.0.1 9001 0\n"
6063 "m thisoneislongerbecauseitisa256bitmddigest33\n"
6064 "s Fast Guard Stable\n";
6066 const char *cp
= str1
;
6067 rs
= routerstatus_parse_entry_from_string(area
, &cp
, tokens
, NULL
, NULL
,
6068 23, FLAV_MICRODESC
);
6070 tt_assert(rs
->is_flagged_running
);
6071 tt_assert(! rs
->is_valid
);
6072 tt_assert(! rs
->is_exit
);
6073 tt_assert(rs
->is_fast
);
6074 routerstatus_free(rs
);
6076 /* With method 24 or later, we can assume "valid" is set. */
6078 rs
= routerstatus_parse_entry_from_string(area
, &cp
, tokens
, NULL
, NULL
,
6079 24, FLAV_MICRODESC
);
6081 tt_assert(rs
->is_flagged_running
);
6082 tt_assert(rs
->is_valid
);
6083 tt_assert(! rs
->is_exit
);
6084 tt_assert(rs
->is_fast
);
6087 smartlist_free(tokens
);
6088 memarea_drop_all(area
);
6089 routerstatus_free(rs
);
6093 test_dir_post_parsing(void *arg
)
6097 /* Test the version parsing from an HS descriptor publish request. */
6100 const char *prefix
= "/tor/hs/";
6101 int version
= parse_hs_version_from_post("/tor/hs//publish", prefix
, &end
);
6102 tt_int_op(version
, OP_EQ
, -1);
6103 tt_ptr_op(end
, OP_EQ
, NULL
);
6104 version
= parse_hs_version_from_post("/tor/hs/a/publish", prefix
, &end
);
6105 tt_int_op(version
, OP_EQ
, -1);
6106 tt_ptr_op(end
, OP_EQ
, NULL
);
6107 version
= parse_hs_version_from_post("/tor/hs/3/publish", prefix
, &end
);
6108 tt_int_op(version
, OP_EQ
, 3);
6109 tt_str_op(end
, OP_EQ
, "/publish");
6110 version
= parse_hs_version_from_post("/tor/hs/42/publish", prefix
, &end
);
6111 tt_int_op(version
, OP_EQ
, 42);
6112 tt_str_op(end
, OP_EQ
, "/publish");
6113 version
= parse_hs_version_from_post("/tor/hs/18163/publish",prefix
, &end
);
6114 tt_int_op(version
, OP_EQ
, 18163);
6115 tt_str_op(end
, OP_EQ
, "/publish");
6116 version
= parse_hs_version_from_post("JUNKJUNKJUNK", prefix
, &end
);
6117 tt_int_op(version
, OP_EQ
, -1);
6118 tt_ptr_op(end
, OP_EQ
, NULL
);
6119 version
= parse_hs_version_from_post("/tor/hs/3/publish", "blah", &end
);
6120 tt_int_op(version
, OP_EQ
, -1);
6121 tt_ptr_op(end
, OP_EQ
, NULL
);
6122 /* Missing the '/' at the end of the prefix. */
6123 version
= parse_hs_version_from_post("/tor/hs/3/publish", "/tor/hs", &end
);
6124 tt_int_op(version
, OP_EQ
, -1);
6125 tt_ptr_op(end
, OP_EQ
, NULL
);
6126 version
= parse_hs_version_from_post("/random/blah/tor/hs/3/publish",
6128 tt_int_op(version
, OP_EQ
, -1);
6129 tt_ptr_op(end
, OP_EQ
, NULL
);
6130 version
= parse_hs_version_from_post("/tor/hs/3/publish/random/junk",
6132 tt_int_op(version
, OP_EQ
, 3);
6133 tt_str_op(end
, OP_EQ
, "/publish/random/junk");
6134 version
= parse_hs_version_from_post("/tor/hs/-1/publish", prefix
, &end
);
6135 tt_int_op(version
, OP_EQ
, -1);
6136 tt_ptr_op(end
, OP_EQ
, NULL
);
6138 version
= parse_hs_version_from_post("/tor/hs/2147483647/publish",
6140 tt_int_op(version
, OP_EQ
, INT_MAX
);
6141 tt_str_op(end
, OP_EQ
, "/publish");
6143 version
= parse_hs_version_from_post("/tor/hs/2147483648/publish",
6145 tt_int_op(version
, OP_EQ
, -1);
6146 tt_ptr_op(end
, OP_EQ
, NULL
);
6154 test_dir_platform_str(void *arg
)
6159 get_platform_str(platform
, sizeof(platform
));
6160 tt_int_op((int)strlen(platform
), OP_GT
, 0);
6161 tt_assert(!strcmpstart(platform
, "Tor "));
6164 // make sure this is a tor version, a real actual tor version.
6165 tt_int_op(tor_version_parse_platform(platform
, &ver
, 1), OP_EQ
, 1);
6167 TT_BLATHER(("%d.%d.%d.%d", ver
.major
, ver
.minor
, ver
.micro
, ver
.patchlevel
));
6169 // Handle an example version.
6170 tt_int_op(tor_version_parse_platform(
6171 "Tor 0.3.3.3 (foo) (git-xyzzy) on a potato", &ver
, 1), OP_EQ
, 1);
6176 #define DIR_LEGACY(name) \
6177 { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
6179 #define DIR(name,flags) \
6180 { #name, test_dir_##name, (flags), NULL, NULL }
6182 /* where arg is a string constant */
6183 #define DIR_ARG(name,flags,arg) \
6184 { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
6186 struct testcase_t dir_tests
[] = {
6187 DIR_LEGACY(nicknames
),
6188 DIR_LEGACY(formats
),
6189 DIR(routerinfo_parsing
, 0),
6190 DIR(extrainfo_parsing
, 0),
6191 DIR(parse_router_list
, TT_FORK
),
6192 DIR(load_routers
, TT_FORK
),
6193 DIR(load_extrainfo
, TT_FORK
),
6194 DIR(getinfo_extra
, 0),
6195 DIR_LEGACY(versions
),
6196 DIR_LEGACY(fp_pairs
),
6198 DIR_LEGACY(measured_bw_kb
),
6199 DIR_LEGACY(measured_bw_kb_cache
),
6200 DIR_LEGACY(param_voting
),
6201 DIR(param_voting_lookup
, 0),
6202 DIR_LEGACY(v3_networkstatus
),
6203 DIR(random_weighted
, 0),
6205 DIR_LEGACY(clip_unmeasured_bw_kb
),
6206 DIR_LEGACY(clip_unmeasured_bw_kb_alt
),
6207 DIR(fmt_control_ns
, 0),
6208 DIR(dirserv_set_routerstatus_testing
, 0),
6209 DIR(http_handling
, 0),
6210 DIR(purpose_needs_anonymity_returns_true_for_bridges
, 0),
6211 DIR(purpose_needs_anonymity_returns_false_for_own_bridge_desc
, 0),
6212 DIR(purpose_needs_anonymity_returns_true_by_default
, 0),
6213 DIR(purpose_needs_anonymity_returns_true_for_sensitive_purpose
, 0),
6214 DIR(purpose_needs_anonymity_ret_false_for_non_sensitive_conn
, 0),
6215 DIR(post_parsing
, 0),
6218 DIR(download_status_schedule
, 0),
6219 DIR(download_status_random_backoff
, 0),
6220 DIR(download_status_random_backoff_ranges
, 0),
6221 DIR(download_status_increment
, 0),
6222 DIR(authdir_type_to_string
, 0),
6223 DIR(conn_purpose_to_string
, 0),
6224 DIR(should_use_directory_guards
, 0),
6225 DIR(should_not_init_request_to_ourselves
, TT_FORK
),
6226 DIR(should_not_init_request_to_dir_auths_without_v3_info
, 0),
6227 DIR(should_init_request_to_dir_auths
, 0),
6228 DIR(choose_compression_level
, 0),
6229 DIR(dump_unparseable_descriptors
, 0),
6230 DIR(populate_dump_desc_fifo
, 0),
6231 DIR(populate_dump_desc_fifo_2
, 0),
6232 DIR_ARG(find_dl_schedule
, TT_FORK
, "bfd"),
6233 DIR_ARG(find_dl_schedule
, TT_FORK
, "bad"),
6234 DIR_ARG(find_dl_schedule
, TT_FORK
, "cfd"),
6235 DIR_ARG(find_dl_schedule
, TT_FORK
, "cad"),
6236 DIR_ARG(find_dl_schedule
, TT_FORK
, "bfr"),
6237 DIR_ARG(find_dl_schedule
, TT_FORK
, "bar"),
6238 DIR_ARG(find_dl_schedule
, TT_FORK
, "cfr"),
6239 DIR_ARG(find_dl_schedule
, TT_FORK
, "car"),
6240 DIR(assumed_flags
, 0),
6241 DIR(networkstatus_compute_bw_weights_v10
, 0),
6242 DIR(platform_str
, 0),