1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 #define DIRSERV_PRIVATE
10 #define DIRVOTE_PRIVATE
11 #define ROUTER_PRIVATE
12 #define ROUTERLIST_PRIVATE
13 #define HIBERNATE_PRIVATE
14 #define NETWORKSTATUS_PRIVATE
17 #include "crypto_ed25519.h"
18 #include "directory.h"
21 #include "hibernate.h"
22 #include "networkstatus.h"
24 #include "routerkeys.h"
25 #include "routerlist.h"
26 #include "routerparse.h"
27 #include "routerset.h"
32 test_dir_nicknames(void *arg
)
35 tt_assert( is_legal_nickname("a"));
36 tt_assert(!is_legal_nickname(""));
37 tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
38 tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
39 tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
40 tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
42 tt_assert( is_legal_nickname_or_hexdigest(
43 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
44 tt_assert( is_legal_nickname_or_hexdigest(
45 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
46 tt_assert( is_legal_nickname_or_hexdigest(
47 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
49 tt_assert(!is_legal_nickname_or_hexdigest(
50 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
52 tt_assert(!is_legal_nickname_or_hexdigest(
53 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
54 /* hex part too long */
55 tt_assert(!is_legal_nickname_or_hexdigest(
56 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
57 tt_assert(!is_legal_nickname_or_hexdigest(
58 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
60 tt_assert(!is_legal_nickname_or_hexdigest(
61 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
62 tt_assert(!is_legal_nickname_or_hexdigest(
63 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
64 tt_assert(!is_legal_nickname_or_hexdigest(
65 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
66 tt_assert(!is_legal_nickname_or_hexdigest(
67 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
68 "abcdefghijklmnoppqrst"));
70 tt_assert(!is_legal_nickname_or_hexdigest(
71 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
72 tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
73 tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
74 tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
79 /** Run unit tests for router descriptor generation logic. */
81 test_dir_formats(void *arg
)
86 char fingerprint
[FINGERPRINT_LEN
+1];
87 char *pk1_str
= NULL
, *pk2_str
= NULL
, *cp
;
88 size_t pk1_str_len
, pk2_str_len
;
89 routerinfo_t
*r1
=NULL
, *r2
=NULL
;
90 crypto_pk_t
*pk1
= NULL
, *pk2
= NULL
;
91 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
92 addr_policy_t
*ex1
, *ex2
;
93 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
94 uint8_t *rsa_cc
= NULL
;
95 or_options_t
*options
= get_options_mutable();
96 const addr_policy_t
*p
;
97 time_t now
= time(NULL
);
100 pk1
= pk_generate(0);
101 pk2
= pk_generate(1);
103 tt_assert(pk1
&& pk2
);
105 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE
);
107 get_platform_str(platform
, sizeof(platform
));
108 r1
= tor_malloc_zero(sizeof(routerinfo_t
));
109 r1
->addr
= 0xc0a80001u
; /* 192.168.0.1 */
110 r1
->cache_info
.published_on
= 0;
113 tor_addr_parse(&r1
->ipv6_addr
, "1:2:3:4::");
114 r1
->ipv6_orport
= 9999;
115 r1
->onion_pkey
= crypto_pk_dup_key(pk1
);
116 r1
->identity_pkey
= crypto_pk_dup_key(pk2
);
117 r1
->bandwidthrate
= 1000;
118 r1
->bandwidthburst
= 5000;
119 r1
->bandwidthcapacity
= 10000;
120 r1
->exit_policy
= NULL
;
121 r1
->nickname
= tor_strdup("Magri");
122 r1
->platform
= tor_strdup(platform
);
124 ex1
= tor_malloc_zero(sizeof(addr_policy_t
));
125 ex2
= tor_malloc_zero(sizeof(addr_policy_t
));
126 ex1
->policy_type
= ADDR_POLICY_ACCEPT
;
127 tor_addr_from_ipv4h(&ex1
->addr
, 0);
129 ex1
->prt_min
= ex1
->prt_max
= 80;
130 ex2
->policy_type
= ADDR_POLICY_REJECT
;
131 tor_addr_from_ipv4h(&ex2
->addr
, 18<<24);
133 ex2
->prt_min
= ex2
->prt_max
= 24;
134 r2
= tor_malloc_zero(sizeof(routerinfo_t
));
135 r2
->addr
= 0x0a030201u
; /* 10.3.2.1 */
136 ed25519_keypair_t kp1
, kp2
;
137 ed25519_secret_key_from_seed(&kp1
.seckey
,
138 (const uint8_t*)"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
139 ed25519_public_key_generate(&kp1
.pubkey
, &kp1
.seckey
);
140 ed25519_secret_key_from_seed(&kp2
.seckey
,
141 (const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
142 ed25519_public_key_generate(&kp2
.pubkey
, &kp2
.seckey
);
143 r2
->signing_key_cert
= tor_cert_create(&kp1
,
144 CERT_TYPE_ID_SIGNING
,
147 CERT_FLAG_INCLUDE_SIGNING_KEY
);
149 base64_encode(cert_buf
, sizeof(cert_buf
),
150 (const char*)r2
->signing_key_cert
->encoded
,
151 r2
->signing_key_cert
->encoded_len
,
152 BASE64_ENCODE_MULTILINE
);
153 r2
->platform
= tor_strdup(platform
);
154 r2
->cache_info
.published_on
= 5;
157 r2
->onion_pkey
= crypto_pk_dup_key(pk2
);
158 curve25519_keypair_t r2_onion_keypair
;
159 curve25519_keypair_generate(&r2_onion_keypair
, 0);
160 r2
->onion_curve25519_pkey
= tor_memdup(&r2_onion_keypair
.pubkey
,
161 sizeof(curve25519_public_key_t
));
162 r2
->identity_pkey
= crypto_pk_dup_key(pk1
);
163 r2
->bandwidthrate
= r2
->bandwidthburst
= r2
->bandwidthcapacity
= 3000;
164 r2
->exit_policy
= smartlist_new();
165 smartlist_add(r2
->exit_policy
, ex1
);
166 smartlist_add(r2
->exit_policy
, ex2
);
167 r2
->nickname
= tor_strdup("Fred");
169 tt_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
171 tt_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
174 /* XXXX025 router_dump_to_string should really take this from ri.*/
175 options
->ContactInfo
= tor_strdup("Magri White "
176 "<magri@elsewhere.example.com>");
177 buf
= router_dump_router_to_string(r1
, pk2
, NULL
, NULL
, NULL
);
178 tor_free(options
->ContactInfo
);
181 strlcpy(buf2
, "router Magri 192.168.0.1 9000 0 9003\n"
182 "or-address [1:2:3:4::]:9999\n"
183 "platform Tor "VERSION
" on ", sizeof(buf2
));
184 strlcat(buf2
, get_uname(), sizeof(buf2
));
186 "protocols Link 1 2 Circuit 1\n"
187 "published 1970-01-01 00:00:00\n"
188 "fingerprint ", sizeof(buf2
));
189 tt_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
190 strlcat(buf2
, fingerprint
, sizeof(buf2
));
191 strlcat(buf2
, "\nuptime 0\n"
192 /* XXX the "0" above is hard-coded, but even if we made it reflect
193 * uptime, that still wouldn't make it right, because the two
194 * descriptors might be made on different seconds... hm. */
195 "bandwidth 1000 5000 10000\n"
196 "onion-key\n", sizeof(buf2
));
197 strlcat(buf2
, pk1_str
, sizeof(buf2
));
198 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
199 strlcat(buf2
, pk2_str
, sizeof(buf2
));
200 strlcat(buf2
, "hidden-service-dir\n", sizeof(buf2
));
201 strlcat(buf2
, "contact Magri White <magri@elsewhere.example.com>\n",
203 strlcat(buf2
, "reject *:*\nrouter-signature\n", sizeof(buf2
));
204 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
207 tt_str_op(buf
,OP_EQ
, buf2
);
210 buf
= router_dump_router_to_string(r1
, pk2
, NULL
, NULL
, NULL
);
213 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
,NULL
);
215 tt_int_op(rp1
->addr
,OP_EQ
, r1
->addr
);
216 tt_int_op(rp1
->or_port
,OP_EQ
, r1
->or_port
);
217 //test_eq(rp1->dir_port, r1->dir_port);
218 tt_int_op(rp1
->bandwidthrate
,OP_EQ
, r1
->bandwidthrate
);
219 tt_int_op(rp1
->bandwidthburst
,OP_EQ
, r1
->bandwidthburst
);
220 tt_int_op(rp1
->bandwidthcapacity
,OP_EQ
, r1
->bandwidthcapacity
);
221 tt_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
222 tt_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
223 //tt_assert(rp1->exit_policy == NULL);
227 "router Fred 10.3.2.1 9005 0 0\n"
229 "-----BEGIN ED25519 CERT-----\n", sizeof(buf2
));
230 strlcat(buf2
, cert_buf
, sizeof(buf2
));
231 strlcat(buf2
, "-----END ED25519 CERT-----\n", sizeof(buf2
));
232 strlcat(buf2
, "master-key-ed25519 ", sizeof(buf2
));
234 char k
[ED25519_BASE64_LEN
+1];
235 tt_assert(ed25519_public_to_base64(k
, &r2
->signing_key_cert
->signing_key
)
237 strlcat(buf2
, k
, sizeof(buf2
));
238 strlcat(buf2
, "\n", sizeof(buf2
));
240 strlcat(buf2
, "platform Tor "VERSION
" on ", sizeof(buf2
));
241 strlcat(buf2
, get_uname(), sizeof(buf2
));
243 "protocols Link 1 2 Circuit 1\n"
244 "published 1970-01-01 00:00:05\n"
245 "fingerprint ", sizeof(buf2
));
246 tt_assert(!crypto_pk_get_fingerprint(pk1
, fingerprint
, 1));
247 strlcat(buf2
, fingerprint
, sizeof(buf2
));
248 strlcat(buf2
, "\nuptime 0\n"
249 "bandwidth 3000 3000 3000\n", sizeof(buf2
));
250 strlcat(buf2
, "onion-key\n", sizeof(buf2
));
251 strlcat(buf2
, pk2_str
, sizeof(buf2
));
252 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
253 strlcat(buf2
, pk1_str
, sizeof(buf2
));
255 rsa_cc
= make_tap_onion_key_crosscert(pk2
,
260 base64_encode(cert_buf
, sizeof(cert_buf
), (char*)rsa_cc
, rsa_cc_len
,
261 BASE64_ENCODE_MULTILINE
);
262 strlcat(buf2
, "onion-key-crosscert\n"
263 "-----BEGIN CROSSCERT-----\n", sizeof(buf2
));
264 strlcat(buf2
, cert_buf
, sizeof(buf2
));
265 strlcat(buf2
, "-----END CROSSCERT-----\n", sizeof(buf2
));
268 tor_cert_t
*ntor_cc
= NULL
;
269 ntor_cc
= make_ntor_onion_key_crosscert(&r2_onion_keypair
,
271 r2
->cache_info
.published_on
,
272 MIN_ONION_KEY_LIFETIME
,
275 base64_encode(cert_buf
, sizeof(cert_buf
),
276 (char*)ntor_cc
->encoded
, ntor_cc
->encoded_len
,
277 BASE64_ENCODE_MULTILINE
);
278 tor_cert_free(ntor_cc
);
280 tor_snprintf(buf2
+strlen(buf2
), sizeof(buf2
)-strlen(buf2
),
281 "ntor-onion-key-crosscert %d\n"
282 "-----BEGIN ED25519 CERT-----\n"
284 "-----END ED25519 CERT-----\n", ntor_cc_sign
, cert_buf
);
286 strlcat(buf2
, "hidden-service-dir\n", sizeof(buf2
));
287 strlcat(buf2
, "ntor-onion-key ", sizeof(buf2
));
288 base64_encode(cert_buf
, sizeof(cert_buf
),
289 (const char*)r2_onion_keypair
.pubkey
.public_key
, 32,
290 BASE64_ENCODE_MULTILINE
);
291 strlcat(buf2
, cert_buf
, sizeof(buf2
));
292 strlcat(buf2
, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2
));
293 strlcat(buf2
, "router-sig-ed25519 ", sizeof(buf2
));
295 buf
= router_dump_router_to_string(r2
, pk1
, pk2
, &r2_onion_keypair
, &kp2
);
297 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
300 tt_str_op(buf
, OP_EQ
, buf2
);
303 buf
= router_dump_router_to_string(r2
, pk1
, NULL
, NULL
, NULL
);
305 rp2
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
,NULL
);
307 tt_int_op(rp2
->addr
,OP_EQ
, r2
->addr
);
308 tt_int_op(rp2
->or_port
,OP_EQ
, r2
->or_port
);
309 tt_int_op(rp2
->dir_port
,OP_EQ
, r2
->dir_port
);
310 tt_int_op(rp2
->bandwidthrate
,OP_EQ
, r2
->bandwidthrate
);
311 tt_int_op(rp2
->bandwidthburst
,OP_EQ
, r2
->bandwidthburst
);
312 tt_int_op(rp2
->bandwidthcapacity
,OP_EQ
, r2
->bandwidthcapacity
);
313 tt_mem_op(rp2
->onion_curve25519_pkey
->public_key
,OP_EQ
,
314 r2
->onion_curve25519_pkey
->public_key
,
315 CURVE25519_PUBKEY_LEN
);
316 tt_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
317 tt_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
319 tt_int_op(smartlist_len(rp2
->exit_policy
),OP_EQ
, 2);
321 p
= smartlist_get(rp2
->exit_policy
, 0);
322 tt_int_op(p
->policy_type
,OP_EQ
, ADDR_POLICY_ACCEPT
);
323 tt_assert(tor_addr_is_null(&p
->addr
));
324 tt_int_op(p
->maskbits
,OP_EQ
, 0);
325 tt_int_op(p
->prt_min
,OP_EQ
, 80);
326 tt_int_op(p
->prt_max
,OP_EQ
, 80);
328 p
= smartlist_get(rp2
->exit_policy
, 1);
329 tt_int_op(p
->policy_type
,OP_EQ
, ADDR_POLICY_REJECT
);
330 tt_assert(tor_addr_eq(&p
->addr
, &ex2
->addr
));
331 tt_int_op(p
->maskbits
,OP_EQ
, 8);
332 tt_int_op(p
->prt_min
,OP_EQ
, 24);
333 tt_int_op(p
->prt_max
,OP_EQ
, 24);
336 /* Okay, now for the directories. */
338 fingerprint_list
= smartlist_new();
339 crypto_pk_get_fingerprint(pk2
, buf
, 1);
340 add_fingerprint_to_dir(buf
, fingerprint_list
, 0);
341 crypto_pk_get_fingerprint(pk1
, buf
, 1);
342 add_fingerprint_to_dir(buf
, fingerprint_list
, 0);
346 dirserv_free_fingerprint_list();
354 routerinfo_free(rp2
);
360 if (pk1
) crypto_pk_free(pk1
);
361 if (pk2
) crypto_pk_free(pk2
);
362 if (rp1
) routerinfo_free(rp1
);
363 tor_free(dir1
); /* XXXX And more !*/
364 tor_free(dir2
); /* And more !*/
367 #include "failing_routerdescs.inc"
370 test_dir_routerinfo_parsing(void *arg
)
375 routerinfo_t
*ri
= NULL
;
377 #define CHECK_OK(s) \
379 routerinfo_free(ri); \
380 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
383 #define CHECK_FAIL(s, againval) \
385 routerinfo_free(ri); \
387 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
388 tt_assert(ri == NULL); \
389 tt_int_op(again, OP_EQ, (againval)); \
392 CHECK_OK(EX_RI_MINIMAL
);
393 CHECK_OK(EX_RI_MAXIMAL
);
395 CHECK_OK(EX_RI_MINIMAL_ED
);
397 /* good annotations prepended */
399 ri
= router_parse_entry_from_string(EX_RI_MINIMAL
, NULL
, 0, 0,
400 "@purpose bridge\n", NULL
);
401 tt_assert(ri
!= NULL
);
402 tt_assert(ri
->purpose
== ROUTER_PURPOSE_BRIDGE
);
405 /* bad annotations prepended. */
406 ri
= router_parse_entry_from_string(EX_RI_MINIMAL
,
407 NULL
, 0, 0, "@purpose\n", NULL
);
408 tt_assert(ri
== NULL
);
410 /* bad annotations on router. */
411 ri
= router_parse_entry_from_string("@purpose\nrouter x\n", NULL
, 0, 1,
413 tt_assert(ri
== NULL
);
415 /* unwanted annotations on router. */
416 ri
= router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL
, 0, 0,
418 tt_assert(ri
== NULL
);
421 ri
= router_parse_entry_from_string("router x\n", NULL
, 0, 0,
423 tt_assert(ri
== NULL
);
427 ri
= router_parse_entry_from_string("hello\n", NULL
, 0, 0, NULL
, NULL
);
428 tt_assert(ri
== NULL
);
430 CHECK_FAIL(EX_RI_BAD_SIG1
, 1);
431 CHECK_FAIL(EX_RI_BAD_SIG2
, 1);
432 CHECK_FAIL(EX_RI_BAD_TOKENS
, 0);
433 CHECK_FAIL(EX_RI_BAD_PUBLISHED
, 0);
434 CHECK_FAIL(EX_RI_NEG_BANDWIDTH
, 0);
435 CHECK_FAIL(EX_RI_BAD_BANDWIDTH
, 0);
436 CHECK_FAIL(EX_RI_BAD_BANDWIDTH2
, 0);
437 CHECK_FAIL(EX_RI_BAD_ONIONKEY1
, 0);
438 CHECK_FAIL(EX_RI_BAD_ONIONKEY2
, 0);
439 CHECK_FAIL(EX_RI_BAD_PORTS
, 0);
440 CHECK_FAIL(EX_RI_BAD_IP
, 0);
441 CHECK_FAIL(EX_RI_BAD_DIRPORT
, 0);
442 CHECK_FAIL(EX_RI_BAD_NAME2
, 0);
443 CHECK_FAIL(EX_RI_BAD_UPTIME
, 0);
445 CHECK_FAIL(EX_RI_BAD_BANDWIDTH3
, 0);
446 CHECK_FAIL(EX_RI_BAD_NTOR_KEY
, 0);
447 CHECK_FAIL(EX_RI_BAD_FINGERPRINT
, 0);
448 CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT
, 0);
449 CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6
, 0);
450 CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY
, 0);
451 CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY
, 0);
452 CHECK_FAIL(EX_RI_BAD_FAMILY
, 0);
453 CHECK_FAIL(EX_RI_ZERO_ORPORT
, 0);
455 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT
, 0);
456 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2
, 0);
457 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN
, 0);
458 CHECK_FAIL(EX_RI_ED_BAD_SIG1
, 0);
459 CHECK_FAIL(EX_RI_ED_BAD_SIG2
, 0);
460 CHECK_FAIL(EX_RI_ED_BAD_SIG3
, 0);
461 CHECK_FAIL(EX_RI_ED_BAD_SIG4
, 0);
462 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1
, 0);
463 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3
, 0);
464 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4
, 0);
465 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5
, 0);
466 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6
, 0);
467 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7
, 0);
468 CHECK_FAIL(EX_RI_ED_MISPLACED1
, 0);
469 CHECK_FAIL(EX_RI_ED_MISPLACED2
, 0);
470 CHECK_FAIL(EX_RI_ED_BAD_CERT1
, 0);
471 CHECK_FAIL(EX_RI_ED_BAD_CERT2
, 0);
472 CHECK_FAIL(EX_RI_ED_BAD_CERT3
, 0);
474 /* This is allowed; we just ignore it. */
475 CHECK_OK(EX_RI_BAD_EI_DIGEST
);
476 CHECK_OK(EX_RI_BAD_EI_DIGEST2
);
484 #include "example_extrainfo.inc"
487 routerinfo_free_wrapper_(void *arg
)
489 routerinfo_free(arg
);
493 test_dir_extrainfo_parsing(void *arg
)
497 #define CHECK_OK(s) \
499 extrainfo_free(ei); \
500 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
503 #define CHECK_FAIL(s, againval) \
505 extrainfo_free(ei); \
507 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
508 tt_assert(ei == NULL); \
509 tt_int_op(again, OP_EQ, (againval)); \
513 ri = tor_malloc_zero(sizeof(routerinfo_t)); \
514 crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
515 tt_assert(! crypto_pk_read_public_key_from_string(pk, \
516 name##_KEY, strlen(name##_KEY))); \
517 tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
518 digestmap_set((digestmap_t*)map, d, ri); \
522 routerinfo_t
*ri
= NULL
;
524 struct digest_ri_map_t
*map
= NULL
;
525 extrainfo_t
*ei
= NULL
;
528 CHECK_OK(EX_EI_MINIMAL
);
529 tt_assert(ei
->pending_sig
);
530 CHECK_OK(EX_EI_MAXIMAL
);
531 tt_assert(ei
->pending_sig
);
532 CHECK_OK(EX_EI_GOOD_ED_EI
);
533 tt_assert(ei
->pending_sig
);
535 map
= (struct digest_ri_map_t
*)digestmap_new();
538 ADD(EX_EI_GOOD_ED_EI
);
540 ADD(EX_EI_BAD_NICKNAME
);
541 ADD(EX_EI_BAD_TOKENS
);
542 ADD(EX_EI_BAD_START
);
543 ADD(EX_EI_BAD_PUBLISHED
);
545 ADD(EX_EI_ED_MISSING_SIG
);
546 ADD(EX_EI_ED_MISSING_CERT
);
547 ADD(EX_EI_ED_BAD_CERT1
);
548 ADD(EX_EI_ED_BAD_CERT2
);
549 ADD(EX_EI_ED_BAD_SIG1
);
550 ADD(EX_EI_ED_BAD_SIG2
);
551 ADD(EX_EI_ED_MISPLACED_CERT
);
552 ADD(EX_EI_ED_MISPLACED_SIG
);
554 CHECK_OK(EX_EI_MINIMAL
);
555 tt_assert(!ei
->pending_sig
);
556 CHECK_OK(EX_EI_MAXIMAL
);
557 tt_assert(!ei
->pending_sig
);
558 CHECK_OK(EX_EI_GOOD_ED_EI
);
559 tt_assert(!ei
->pending_sig
);
561 CHECK_FAIL(EX_EI_BAD_SIG1
,1);
562 CHECK_FAIL(EX_EI_BAD_SIG2
,1);
563 CHECK_FAIL(EX_EI_BAD_SIG3
,1);
564 CHECK_FAIL(EX_EI_BAD_FP
,0);
565 CHECK_FAIL(EX_EI_BAD_NICKNAME
,0);
566 CHECK_FAIL(EX_EI_BAD_TOKENS
,0);
567 CHECK_FAIL(EX_EI_BAD_START
,0);
568 CHECK_FAIL(EX_EI_BAD_PUBLISHED
,0);
570 CHECK_FAIL(EX_EI_ED_MISSING_SIG
,0);
571 CHECK_FAIL(EX_EI_ED_MISSING_CERT
,0);
572 CHECK_FAIL(EX_EI_ED_BAD_CERT1
,0);
573 CHECK_FAIL(EX_EI_ED_BAD_CERT2
,0);
574 CHECK_FAIL(EX_EI_ED_BAD_SIG1
,0);
575 CHECK_FAIL(EX_EI_ED_BAD_SIG2
,0);
576 CHECK_FAIL(EX_EI_ED_MISPLACED_CERT
,0);
577 CHECK_FAIL(EX_EI_ED_MISPLACED_SIG
,0);
585 digestmap_free((digestmap_t
*)map
, routerinfo_free_wrapper_
);
589 test_dir_parse_router_list(void *arg
)
592 smartlist_t
*invalid
= smartlist_new();
593 smartlist_t
*dest
= smartlist_new();
594 smartlist_t
*chunks
= smartlist_new();
598 digestmap_t
*map
= NULL
;
599 char *mem_op_hex_tmp
= NULL
;
600 routerinfo_t
*ri
= NULL
;
603 smartlist_add(chunks
, tor_strdup(EX_RI_MINIMAL
)); // ri 0
604 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_PORTS
)); // bad ri 0
605 smartlist_add(chunks
, tor_strdup(EX_EI_MAXIMAL
)); // ei 0
606 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_SIG2
)); // bad ei --
607 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_NICKNAME
));// bad ei 0
608 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_SIG1
)); // bad ri --
609 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_PUBLISHED
)); // bad ei 1
610 smartlist_add(chunks
, tor_strdup(EX_RI_MAXIMAL
)); // ri 1
611 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_FAMILY
)); // bad ri 1
612 smartlist_add(chunks
, tor_strdup(EX_EI_MINIMAL
)); // ei 1
614 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
616 /* First, parse the routers. */
619 router_parse_list_from_string(&cp
, NULL
, dest
, SAVED_NOWHERE
,
620 0, 0, NULL
, invalid
));
621 tt_int_op(2, OP_EQ
, smartlist_len(dest
));
622 tt_ptr_op(cp
, OP_EQ
, list
+ strlen(list
));
624 routerinfo_t
*r
= smartlist_get(dest
, 0);
625 tt_mem_op(r
->cache_info
.signed_descriptor_body
, OP_EQ
,
626 EX_RI_MINIMAL
, strlen(EX_RI_MINIMAL
));
627 r
= smartlist_get(dest
, 1);
628 tt_mem_op(r
->cache_info
.signed_descriptor_body
, OP_EQ
,
629 EX_RI_MAXIMAL
, strlen(EX_RI_MAXIMAL
));
631 tt_int_op(2, OP_EQ
, smartlist_len(invalid
));
632 test_memeq_hex(smartlist_get(invalid
, 0),
633 "ab9eeaa95e7d45740185b4e519c76ead756277a9");
634 test_memeq_hex(smartlist_get(invalid
, 1),
635 "9a651ee03b64325959e8f1b46f2b689b30750b4c");
638 SMARTLIST_FOREACH(dest
, routerinfo_t
*, ri
, routerinfo_free(ri
));
639 SMARTLIST_FOREACH(invalid
, uint8_t *, d
, tor_free(d
));
640 smartlist_clear(dest
);
641 smartlist_clear(invalid
);
643 /* And check extrainfos. */
645 map
= (digestmap_t
*)router_get_routerlist()->identity_map
;
648 ADD(EX_EI_BAD_NICKNAME
);
649 ADD(EX_EI_BAD_PUBLISHED
);
652 router_parse_list_from_string(&cp
, NULL
, dest
, SAVED_NOWHERE
,
653 1, 0, NULL
, invalid
));
654 tt_int_op(2, OP_EQ
, smartlist_len(dest
));
655 extrainfo_t
*e
= smartlist_get(dest
, 0);
656 tt_mem_op(e
->cache_info
.signed_descriptor_body
, OP_EQ
,
657 EX_EI_MAXIMAL
, strlen(EX_EI_MAXIMAL
));
658 e
= smartlist_get(dest
, 1);
659 tt_mem_op(e
->cache_info
.signed_descriptor_body
, OP_EQ
,
660 EX_EI_MINIMAL
, strlen(EX_EI_MINIMAL
));
662 tt_int_op(2, OP_EQ
, smartlist_len(invalid
));
663 test_memeq_hex(smartlist_get(invalid
, 0),
664 "d5df4aa62ee9ffc9543d41150c9864908e0390af");
665 test_memeq_hex(smartlist_get(invalid
, 1),
666 "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
671 SMARTLIST_FOREACH(dest
, routerinfo_t
*, rt
, routerinfo_free(rt
));
673 SMARTLIST_FOREACH(dest
, extrainfo_t
*, ei
, extrainfo_free(ei
));
674 smartlist_free(dest
);
675 SMARTLIST_FOREACH(invalid
, uint8_t *, d
, tor_free(d
));
676 smartlist_free(invalid
);
677 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
678 smartlist_free(chunks
);
681 digestmap_free((digestmap_t
*)map
, routerinfo_free_wrapper_
);
682 router_get_routerlist()->identity_map
=
683 (struct digest_ri_map_t
*)digestmap_new();
685 tor_free(mem_op_hex_tmp
);
690 static download_status_t dls_minimal
;
691 static download_status_t dls_maximal
;
692 static download_status_t dls_bad_fingerprint
;
693 static download_status_t dls_bad_sig2
;
694 static download_status_t dls_bad_ports
;
695 static download_status_t dls_bad_tokens
;
697 static int mock_router_get_dl_status_unrecognized
= 0;
698 static int mock_router_get_dl_status_calls
= 0;
700 static download_status_t
*
701 mock_router_get_dl_status(const char *d
)
703 ++mock_router_get_dl_status_calls
;
704 char hex
[HEX_DIGEST_LEN
+1];
705 base16_encode(hex
, sizeof(hex
), d
, DIGEST_LEN
);
706 if (!strcmp(hex
, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
708 } else if (!strcmp(hex
, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
710 } else if (!strcmp(hex
, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
711 return &dls_bad_fingerprint
;
712 } else if (!strcmp(hex
, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
713 return &dls_bad_sig2
;
714 } else if (!strcmp(hex
, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
715 return &dls_bad_ports
;
716 } else if (!strcmp(hex
, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
717 return &dls_bad_tokens
;
719 ++mock_router_get_dl_status_unrecognized
;
725 test_dir_load_routers(void *arg
)
728 smartlist_t
*chunks
= smartlist_new();
729 smartlist_t
*wanted
= smartlist_new();
730 char buf
[DIGEST_LEN
];
731 char *mem_op_hex_tmp
= NULL
;
736 tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
737 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
740 MOCK(router_get_dl_status_by_descriptor_digest
, mock_router_get_dl_status
);
742 update_approx_time(1412510400);
744 smartlist_add(chunks
, tor_strdup(EX_RI_MINIMAL
));
745 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_FINGERPRINT
));
746 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_SIG2
));
747 smartlist_add(chunks
, tor_strdup(EX_RI_MAXIMAL
));
748 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_PORTS
));
749 smartlist_add(chunks
, tor_strdup(EX_RI_BAD_TOKENS
));
751 /* not ADDing MINIMIAL */
753 ADD(EX_RI_BAD_FINGERPRINT
);
755 /* Not ADDing BAD_PORTS */
756 ADD(EX_RI_BAD_TOKENS
);
758 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
760 router_load_routers_from_string(list
, NULL
, SAVED_IN_JOURNAL
,
763 /* The "maximal" router was added. */
764 /* "minimal" was not. */
765 tt_int_op(smartlist_len(router_get_routerlist()->routers
),OP_EQ
,1);
766 routerinfo_t
*r
= smartlist_get(router_get_routerlist()->routers
, 0);
767 test_memeq_hex(r
->cache_info
.signed_descriptor_digest
,
768 "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
769 tt_int_op(dls_minimal
.n_download_failures
, OP_EQ
, 0);
770 tt_int_op(dls_maximal
.n_download_failures
, OP_EQ
, 0);
772 /* "Bad fingerprint" and "Bad tokens" should have gotten marked
774 tt_want_int_op(mock_router_get_dl_status_calls
, OP_EQ
, 2);
775 tt_want_int_op(mock_router_get_dl_status_unrecognized
, OP_EQ
, 0);
776 tt_int_op(dls_bad_fingerprint
.n_download_failures
, OP_EQ
, 255);
777 tt_int_op(dls_bad_tokens
.n_download_failures
, OP_EQ
, 255);
779 /* bad_sig2 and bad ports" are retriable -- one since only the signature
780 * was bad, and one because we didn't ask for it. */
781 tt_int_op(dls_bad_sig2
.n_download_failures
, OP_EQ
, 0);
782 tt_int_op(dls_bad_ports
.n_download_failures
, OP_EQ
, 0);
784 /* Wanted still contains "BAD_SIG2" */
785 tt_int_op(smartlist_len(wanted
), OP_EQ
, 1);
786 tt_str_op(smartlist_get(wanted
, 0), OP_EQ
,
787 "E0A3753CEFD54128EAB239F294954121DB23D2EF");
792 tor_free(mem_op_hex_tmp
);
793 UNMOCK(router_get_dl_status_by_descriptor_digest
);
794 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
795 smartlist_free(chunks
);
796 SMARTLIST_FOREACH(wanted
, char *, cp
, tor_free(cp
));
797 smartlist_free(wanted
);
801 static int mock_get_by_ei_dd_calls
= 0;
802 static int mock_get_by_ei_dd_unrecognized
= 0;
804 static signed_descriptor_t sd_ei_minimal
;
805 static signed_descriptor_t sd_ei_bad_nickname
;
806 static signed_descriptor_t sd_ei_maximal
;
807 static signed_descriptor_t sd_ei_bad_tokens
;
808 static signed_descriptor_t sd_ei_bad_sig2
;
810 static signed_descriptor_t
*
811 mock_get_by_ei_desc_digest(const char *d
)
814 ++mock_get_by_ei_dd_calls
;
815 char hex
[HEX_DIGEST_LEN
+1];
816 base16_encode(hex
, sizeof(hex
), d
, DIGEST_LEN
);
818 if (!strcmp(hex
, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
819 return &sd_ei_minimal
;
820 } else if (!strcmp(hex
, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
821 return &sd_ei_maximal
;
822 } else if (!strcmp(hex
, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
823 return &sd_ei_bad_nickname
;
824 } else if (!strcmp(hex
, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
825 return &sd_ei_bad_sig2
;
826 } else if (!strcmp(hex
, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
827 return &sd_ei_bad_tokens
;
829 ++mock_get_by_ei_dd_unrecognized
;
834 static smartlist_t
*mock_ei_insert_list
= NULL
;
835 static was_router_added_t
836 mock_ei_insert(routerlist_t
*rl
, extrainfo_t
*ei
, int warn_if_incompatible
)
839 (void) warn_if_incompatible
;
840 smartlist_add(mock_ei_insert_list
, ei
);
841 return ROUTER_ADDED_SUCCESSFULLY
;
845 test_dir_load_extrainfo(void *arg
)
848 smartlist_t
*chunks
= smartlist_new();
849 smartlist_t
*wanted
= smartlist_new();
850 char buf
[DIGEST_LEN
];
851 char *mem_op_hex_tmp
= NULL
;
856 tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
857 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
860 mock_ei_insert_list
= smartlist_new();
861 MOCK(router_get_by_extrainfo_digest
, mock_get_by_ei_desc_digest
);
862 MOCK(extrainfo_insert
, mock_ei_insert
);
864 smartlist_add(chunks
, tor_strdup(EX_EI_MINIMAL
));
865 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_NICKNAME
));
866 smartlist_add(chunks
, tor_strdup(EX_EI_MAXIMAL
));
867 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_PUBLISHED
));
868 smartlist_add(chunks
, tor_strdup(EX_EI_BAD_TOKENS
));
870 /* not ADDing MINIMIAL */
872 ADD(EX_EI_BAD_NICKNAME
);
873 /* Not ADDing BAD_PUBLISHED */
874 ADD(EX_EI_BAD_TOKENS
);
877 list
= smartlist_join_strings(chunks
, "", 0, NULL
);
878 router_load_extrainfo_from_string(list
, NULL
, SAVED_IN_JOURNAL
, wanted
, 1);
880 /* The "maximal" router was added. */
881 /* "minimal" was also added, even though we didn't ask for it, since
882 * that's what we do with extrainfos. */
883 tt_int_op(smartlist_len(mock_ei_insert_list
),OP_EQ
,2);
885 extrainfo_t
*e
= smartlist_get(mock_ei_insert_list
, 0);
886 test_memeq_hex(e
->cache_info
.signed_descriptor_digest
,
887 "11E0EDF526950739F7769810FCACAB8C882FAEEE");
889 e
= smartlist_get(mock_ei_insert_list
, 1);
890 test_memeq_hex(e
->cache_info
.signed_descriptor_digest
,
891 "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
892 tt_int_op(dls_minimal
.n_download_failures
, OP_EQ
, 0);
893 tt_int_op(dls_maximal
.n_download_failures
, OP_EQ
, 0);
895 /* "Bad nickname" and "Bad tokens" should have gotten marked
897 tt_want_int_op(mock_get_by_ei_dd_calls
, OP_EQ
, 2);
898 tt_want_int_op(mock_get_by_ei_dd_unrecognized
, OP_EQ
, 0);
899 tt_int_op(sd_ei_bad_nickname
.ei_dl_status
.n_download_failures
, OP_EQ
, 255);
900 tt_int_op(sd_ei_bad_tokens
.ei_dl_status
.n_download_failures
, OP_EQ
, 255);
902 /* bad_ports is retriable -- because we didn't ask for it. */
903 tt_int_op(dls_bad_ports
.n_download_failures
, OP_EQ
, 0);
905 /* Wanted still contains "BAD_SIG2" */
906 tt_int_op(smartlist_len(wanted
), OP_EQ
, 1);
907 tt_str_op(smartlist_get(wanted
, 0), OP_EQ
,
908 "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
913 tor_free(mem_op_hex_tmp
);
914 UNMOCK(router_get_by_extrainfo_digest
);
915 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
916 smartlist_free(chunks
);
917 SMARTLIST_FOREACH(wanted
, char *, cp
, tor_free(cp
));
918 smartlist_free(wanted
);
923 test_dir_versions(void *arg
)
927 /* Try out version parsing functionality */
929 tt_int_op(0,OP_EQ
, tor_version_parse("0.3.4pre2-cvs", &ver1
));
930 tt_int_op(0,OP_EQ
, ver1
.major
);
931 tt_int_op(3,OP_EQ
, ver1
.minor
);
932 tt_int_op(4,OP_EQ
, ver1
.micro
);
933 tt_int_op(VER_PRE
,OP_EQ
, ver1
.status
);
934 tt_int_op(2,OP_EQ
, ver1
.patchlevel
);
935 tt_int_op(0,OP_EQ
, tor_version_parse("0.3.4rc1", &ver1
));
936 tt_int_op(0,OP_EQ
, ver1
.major
);
937 tt_int_op(3,OP_EQ
, ver1
.minor
);
938 tt_int_op(4,OP_EQ
, ver1
.micro
);
939 tt_int_op(VER_RC
,OP_EQ
, ver1
.status
);
940 tt_int_op(1,OP_EQ
, ver1
.patchlevel
);
941 tt_int_op(0,OP_EQ
, tor_version_parse("1.3.4", &ver1
));
942 tt_int_op(1,OP_EQ
, ver1
.major
);
943 tt_int_op(3,OP_EQ
, ver1
.minor
);
944 tt_int_op(4,OP_EQ
, ver1
.micro
);
945 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
946 tt_int_op(0,OP_EQ
, ver1
.patchlevel
);
947 tt_int_op(0,OP_EQ
, tor_version_parse("1.3.4.999", &ver1
));
948 tt_int_op(1,OP_EQ
, ver1
.major
);
949 tt_int_op(3,OP_EQ
, ver1
.minor
);
950 tt_int_op(4,OP_EQ
, ver1
.micro
);
951 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
952 tt_int_op(999,OP_EQ
, ver1
.patchlevel
);
953 tt_int_op(0,OP_EQ
, tor_version_parse("0.1.2.4-alpha", &ver1
));
954 tt_int_op(0,OP_EQ
, ver1
.major
);
955 tt_int_op(1,OP_EQ
, ver1
.minor
);
956 tt_int_op(2,OP_EQ
, ver1
.micro
);
957 tt_int_op(4,OP_EQ
, ver1
.patchlevel
);
958 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
959 tt_str_op("alpha",OP_EQ
, ver1
.status_tag
);
960 tt_int_op(0,OP_EQ
, tor_version_parse("0.1.2.4", &ver1
));
961 tt_int_op(0,OP_EQ
, ver1
.major
);
962 tt_int_op(1,OP_EQ
, ver1
.minor
);
963 tt_int_op(2,OP_EQ
, ver1
.micro
);
964 tt_int_op(4,OP_EQ
, ver1
.patchlevel
);
965 tt_int_op(VER_RELEASE
,OP_EQ
, ver1
.status
);
966 tt_str_op("",OP_EQ
, ver1
.status_tag
);
968 tt_int_op(0, OP_EQ
, tor_version_parse("10.1", &ver1
));
969 tt_int_op(10, OP_EQ
, ver1
.major
);
970 tt_int_op(1, OP_EQ
, ver1
.minor
);
971 tt_int_op(0, OP_EQ
, ver1
.micro
);
972 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
973 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
974 tt_str_op("", OP_EQ
, ver1
.status_tag
);
975 tt_int_op(0, OP_EQ
, tor_version_parse("5.99.999", &ver1
));
976 tt_int_op(5, OP_EQ
, ver1
.major
);
977 tt_int_op(99, OP_EQ
, ver1
.minor
);
978 tt_int_op(999, OP_EQ
, ver1
.micro
);
979 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
980 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
981 tt_str_op("", OP_EQ
, ver1
.status_tag
);
982 tt_int_op(0, OP_EQ
, tor_version_parse("10.1-alpha", &ver1
));
983 tt_int_op(10, OP_EQ
, ver1
.major
);
984 tt_int_op(1, OP_EQ
, ver1
.minor
);
985 tt_int_op(0, OP_EQ
, ver1
.micro
);
986 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
987 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
988 tt_str_op("alpha", OP_EQ
, ver1
.status_tag
);
989 tt_int_op(0, OP_EQ
, tor_version_parse("2.1.700-alpha", &ver1
));
990 tt_int_op(2, OP_EQ
, ver1
.major
);
991 tt_int_op(1, OP_EQ
, ver1
.minor
);
992 tt_int_op(700, OP_EQ
, ver1
.micro
);
993 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
994 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
995 tt_str_op("alpha", OP_EQ
, ver1
.status_tag
);
996 tt_int_op(0, OP_EQ
, tor_version_parse("1.6.8-alpha-dev", &ver1
));
997 tt_int_op(1, OP_EQ
, ver1
.major
);
998 tt_int_op(6, OP_EQ
, ver1
.minor
);
999 tt_int_op(8, OP_EQ
, ver1
.micro
);
1000 tt_int_op(0, OP_EQ
, ver1
.patchlevel
);
1001 tt_int_op(VER_RELEASE
, OP_EQ
, ver1
.status
);
1002 tt_str_op("alpha-dev", OP_EQ
, ver1
.status_tag
);
1004 #define tt_versionstatus_op(vs1, op, vs2) \
1005 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
1006 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
1007 #define test_v_i_o(val, ver, lst) \
1008 tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
1010 /* make sure tor_version_is_obsolete() works */
1011 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
1012 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
1013 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
1014 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
1015 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
1016 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
1017 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
1018 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
1019 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
1020 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
1021 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
1022 /* Not on list, but newer than any in same series. */
1023 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
1024 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1025 /* Series newer than any on list. */
1026 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");
1027 /* Series older than any on list. */
1028 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");
1029 /* Not on list, not newer than any on same series. */
1030 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
1031 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1032 /* On list, not newer than any on same series. */
1033 test_v_i_o(VS_UNRECOMMENDED
,
1034 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1035 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1036 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1037 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1038 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
1040 tt_int_op(0,OP_EQ
, tor_version_as_new_as(
1041 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1042 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1044 /* Now try svn revisions. */
1045 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1046 "Tor 0.2.1.0-dev (r99)"));
1047 tt_int_op(1,OP_EQ
, tor_version_as_new_as(
1048 "Tor 0.2.1.0-dev (r100) on Banana Jr",
1049 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
1050 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1051 "Tor 0.2.1.0-dev on Colossus"));
1052 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
1053 "Tor 0.2.1.0-dev (r100)"));
1054 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
1055 "Tor 0.2.1.0-dev (r100) on AM"));
1056 tt_int_op(0,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.0-dev",
1057 "Tor 0.2.1.0-dev (r99)"));
1058 tt_int_op(1,OP_EQ
, tor_version_as_new_as("Tor 0.2.1.1",
1059 "Tor 0.2.1.0-dev (r99)"));
1061 /* Now try git revisions */
1062 tt_int_op(0,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1
));
1063 tt_int_op(0,OP_EQ
, ver1
.major
);
1064 tt_int_op(5,OP_EQ
, ver1
.minor
);
1065 tt_int_op(6,OP_EQ
, ver1
.micro
);
1066 tt_int_op(7,OP_EQ
, ver1
.patchlevel
);
1067 tt_int_op(3,OP_EQ
, ver1
.git_tag_len
);
1068 tt_mem_op(ver1
.git_tag
,OP_EQ
, "\xff\x00\xff", 3);
1069 tt_int_op(-1,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1
));
1070 tt_int_op(-1,OP_EQ
, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1
));
1071 tt_int_op(0,OP_EQ
, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1
));
1076 /** Run unit tests for directory fp_pair functions. */
1078 test_dir_fp_pairs(void *arg
)
1080 smartlist_t
*sl
= smartlist_new();
1084 dir_split_resource_into_fingerprint_pairs(
1085 /* Two pairs, out of order, with one duplicate. */
1086 "73656372657420646174612E0000000000FFFFFF-"
1087 "557365204145532d32353620696e73746561642e+"
1088 "73656372657420646174612E0000000000FFFFFF-"
1089 "557365204145532d32353620696e73746561642e+"
1090 "48657861646563696d616c2069736e277420736f-"
1091 "676f6f6420666f7220686964696e6720796f7572.z", sl
);
1093 tt_int_op(smartlist_len(sl
),OP_EQ
, 2);
1094 pair
= smartlist_get(sl
, 0);
1095 tt_mem_op(pair
->first
,OP_EQ
, "Hexadecimal isn't so", DIGEST_LEN
);
1096 tt_mem_op(pair
->second
,OP_EQ
, "good for hiding your", DIGEST_LEN
);
1097 pair
= smartlist_get(sl
, 1);
1098 tt_mem_op(pair
->first
,OP_EQ
, "secret data.\0\0\0\0\0\xff\xff\xff",
1100 tt_mem_op(pair
->second
,OP_EQ
, "Use AES-256 instead.", DIGEST_LEN
);
1103 SMARTLIST_FOREACH(sl
, fp_pair_t
*, pair
, tor_free(pair
));
1108 test_dir_split_fps(void *testdata
)
1110 smartlist_t
*sl
= smartlist_new();
1111 char *mem_op_hex_tmp
= NULL
;
1114 /* Some example hex fingerprints and their base64 equivalents */
1115 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1116 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1117 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
1119 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
1121 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
1123 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
1124 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
1125 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
1126 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
1127 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
1130 dir_split_resource_into_fingerprints("A+C+B", sl
, NULL
, 0);
1131 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1132 tt_str_op(smartlist_get(sl
, 0), OP_EQ
, "A");
1133 tt_str_op(smartlist_get(sl
, 1), OP_EQ
, "C");
1134 tt_str_op(smartlist_get(sl
, 2), OP_EQ
, "B");
1135 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1136 smartlist_clear(sl
);
1139 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl
, NULL
, DSR_SORT_UNIQ
);
1140 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1141 tt_str_op(smartlist_get(sl
, 0), OP_EQ
, "A");
1142 tt_str_op(smartlist_get(sl
, 1), OP_EQ
, "B");
1143 tt_str_op(smartlist_get(sl
, 2), OP_EQ
, "C");
1144 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1145 smartlist_clear(sl
);
1148 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
, sl
, NULL
, DSR_HEX
);
1149 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1150 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1151 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1152 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1153 smartlist_clear(sl
);
1155 /* decode hex and drop weirdness. */
1156 dir_split_resource_into_fingerprints(HEX1
"+bogus+"HEX2
"+"HEX256_1
,
1158 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1159 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1160 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1161 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1162 smartlist_clear(sl
);
1164 /* Decode long hex */
1165 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX2
"+"HEX256_3
,
1166 sl
, NULL
, DSR_HEX
|DSR_DIGEST256
);
1167 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1168 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1169 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1170 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX256_3
);
1171 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1172 smartlist_clear(sl
);
1174 /* Decode hex and sort. */
1175 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
"+"HEX3
"+"HEX2
,
1176 sl
, NULL
, DSR_HEX
|DSR_SORT_UNIQ
);
1177 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1178 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX3
);
1179 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1180 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX1
);
1181 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1182 smartlist_clear(sl
);
1184 /* Decode long hex and sort */
1185 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX256_3
1188 DSR_HEX
|DSR_DIGEST256
|DSR_SORT_UNIQ
);
1189 tt_int_op(smartlist_len(sl
), OP_EQ
, 3);
1190 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_3
);
1191 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1192 test_mem_op_hex(smartlist_get(sl
, 2), OP_EQ
, HEX256_1
);
1193 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1194 smartlist_clear(sl
);
1197 dir_split_resource_into_fingerprints(B64_1
"-"B64_2
, sl
, NULL
, DSR_BASE64
);
1198 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1199 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX1
);
1200 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX2
);
1201 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1202 smartlist_clear(sl
);
1204 /* Decode long base64 */
1205 dir_split_resource_into_fingerprints(B64_256_1
"-"B64_256_2
,
1206 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
1207 tt_int_op(smartlist_len(sl
), OP_EQ
, 2);
1208 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1209 test_mem_op_hex(smartlist_get(sl
, 1), OP_EQ
, HEX256_2
);
1210 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1211 smartlist_clear(sl
);
1213 dir_split_resource_into_fingerprints(B64_256_1
,
1214 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
1215 tt_int_op(smartlist_len(sl
), OP_EQ
, 1);
1216 test_mem_op_hex(smartlist_get(sl
, 0), OP_EQ
, HEX256_1
);
1217 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1218 smartlist_clear(sl
);
1221 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1223 tor_free(mem_op_hex_tmp
);
1227 test_dir_measured_bw_kb(void *arg
)
1229 measured_bw_line_t mbwl
;
1231 const char *lines_pass
[] = {
1232 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1233 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
1234 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1235 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
1236 "bw=1024 junk=007\n",
1237 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
1238 "bw=1024 junk=007\n",
1241 const char *lines_fail
[] = {
1242 /* Test possible python stupidity on input */
1243 "node_id=None bw=1024\n",
1244 "node_id=$None bw=1024\n",
1245 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
1246 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
1247 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
1248 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
1249 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
1250 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
1251 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
1252 /* Test incomplete writes due to race conditions, partial copies, etc */
1257 "node_id=$557365204145532d32353620696e73746561642e bw=",
1258 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
1259 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
1260 "node_id=$557365204145532d32353620696e7374",
1261 "node_id=$557365204145532d32353620696e7374\n",
1266 /* Test assorted noise */
1268 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
1269 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
1270 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
1271 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
1276 for (i
= 0; strcmp(lines_fail
[i
], "end"); i
++) {
1277 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
1278 tt_assert(measured_bw_line_parse(&mbwl
, lines_fail
[i
]) == -1);
1281 for (i
= 0; strcmp(lines_pass
[i
], "end"); i
++) {
1282 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
1283 tt_assert(measured_bw_line_parse(&mbwl
, lines_pass
[i
]) == 0);
1284 tt_assert(mbwl
.bw_kb
== 1024);
1285 tt_assert(strcmp(mbwl
.node_hex
,
1286 "557365204145532d32353620696e73746561642e") == 0);
1293 #define MBWC_INIT_TIME 1000
1295 /** Do the measured bandwidth cache unit test */
1297 test_dir_measured_bw_kb_cache(void *arg
)
1299 /* Initial fake time_t for testing */
1300 time_t curr
= MBWC_INIT_TIME
;
1301 /* Some measured_bw_line_ts */
1302 measured_bw_line_t mbwl
[3];
1303 /* For receiving output on cache queries */
1307 /* First, clear the cache and assert that it's empty */
1309 dirserv_clear_measured_bw_cache();
1310 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1312 * Set up test mbwls; none of the dirserv_cache_*() functions care about
1313 * the node_hex field.
1315 memset(mbwl
[0].node_id
, 0x01, DIGEST_LEN
);
1317 memset(mbwl
[1].node_id
, 0x02, DIGEST_LEN
);
1319 memset(mbwl
[2].node_id
, 0x03, DIGEST_LEN
);
1321 /* Try caching something */
1322 dirserv_cache_measured_bw(&(mbwl
[0]), curr
);
1323 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1324 /* Okay, let's see if we can retrieve it */
1325 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,&bw
, &as_of
));
1326 tt_int_op(bw
,OP_EQ
, 20);
1327 tt_int_op(as_of
,OP_EQ
, MBWC_INIT_TIME
);
1328 /* Try retrieving it without some outputs */
1329 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,NULL
, NULL
));
1330 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,&bw
, NULL
));
1331 tt_int_op(bw
,OP_EQ
, 20);
1332 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
,NULL
,&as_of
));
1333 tt_int_op(as_of
,OP_EQ
, MBWC_INIT_TIME
);
1335 curr
+= MAX_MEASUREMENT_AGE
+ 1;
1336 dirserv_expire_measured_bw_cache(curr
);
1337 /* Check that the cache is empty */
1338 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1339 /* Check that we can't retrieve it */
1340 tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl
[0].node_id
, NULL
,NULL
));
1341 /* Try caching a few things now */
1342 dirserv_cache_measured_bw(&(mbwl
[0]), curr
);
1343 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1344 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1345 dirserv_cache_measured_bw(&(mbwl
[1]), curr
);
1346 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 2);
1347 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1348 dirserv_cache_measured_bw(&(mbwl
[2]), curr
);
1349 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 3);
1350 curr
+= MAX_MEASUREMENT_AGE
/ 4 + 1;
1351 /* Do an expire that's too soon to get any of them */
1352 dirserv_expire_measured_bw_cache(curr
);
1353 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 3);
1354 /* Push the oldest one off the cliff */
1355 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1356 dirserv_expire_measured_bw_cache(curr
);
1357 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 2);
1358 /* And another... */
1359 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1360 dirserv_expire_measured_bw_cache(curr
);
1361 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 1);
1362 /* This should empty it out again */
1363 curr
+= MAX_MEASUREMENT_AGE
/ 4;
1364 dirserv_expire_measured_bw_cache(curr
);
1365 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ
, 0);
1372 test_dir_param_voting(void *arg
)
1374 networkstatus_t vote1
, vote2
, vote3
, vote4
;
1375 smartlist_t
*votes
= smartlist_new();
1378 /* dirvote_compute_params only looks at the net_params field of the votes,
1379 so that's all we need to set.
1382 memset(&vote1
, 0, sizeof(vote1
));
1383 memset(&vote2
, 0, sizeof(vote2
));
1384 memset(&vote3
, 0, sizeof(vote3
));
1385 memset(&vote4
, 0, sizeof(vote4
));
1386 vote1
.net_params
= smartlist_new();
1387 vote2
.net_params
= smartlist_new();
1388 vote3
.net_params
= smartlist_new();
1389 vote4
.net_params
= smartlist_new();
1390 smartlist_split_string(vote1
.net_params
,
1391 "ab=90 abcd=20 cw=50 x-yz=-99", NULL
, 0, 0);
1392 smartlist_split_string(vote2
.net_params
,
1393 "ab=27 cw=5 x-yz=88", NULL
, 0, 0);
1394 smartlist_split_string(vote3
.net_params
,
1395 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL
, 0, 0);
1396 smartlist_split_string(vote4
.net_params
,
1397 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL
, 0, 0);
1398 tt_int_op(100,OP_EQ
, networkstatus_get_param(&vote4
, "x-yz", 50, 0, 300));
1399 tt_int_op(222,OP_EQ
, networkstatus_get_param(&vote4
, "foobar", 222, 0, 300));
1400 tt_int_op(80,OP_EQ
, networkstatus_get_param(&vote4
, "ab", 12, 0, 80));
1401 tt_int_op(-8,OP_EQ
, networkstatus_get_param(&vote4
, "ab", -12, -100, -8));
1402 tt_int_op(0,OP_EQ
, networkstatus_get_param(&vote4
, "foobar", 0, -100, 8));
1404 smartlist_add(votes
, &vote1
);
1406 /* Do the first tests without adding all the other votes, for
1407 * networks without many dirauths. */
1409 res
= dirvote_compute_params(votes
, 12, 2);
1410 tt_str_op(res
,OP_EQ
, "");
1413 res
= dirvote_compute_params(votes
, 12, 1);
1414 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-99");
1417 smartlist_add(votes
, &vote2
);
1419 res
= dirvote_compute_params(votes
, 12, 2);
1420 tt_str_op(res
,OP_EQ
, "ab=27 cw=5 x-yz=-99");
1423 res
= dirvote_compute_params(votes
, 12, 3);
1424 tt_str_op(res
,OP_EQ
, "ab=27 cw=5 x-yz=-99");
1427 res
= dirvote_compute_params(votes
, 12, 6);
1428 tt_str_op(res
,OP_EQ
, "");
1431 smartlist_add(votes
, &vote3
);
1433 res
= dirvote_compute_params(votes
, 12, 3);
1434 tt_str_op(res
,OP_EQ
, "ab=27 abcd=20 cw=50 x-yz=-9");
1437 res
= dirvote_compute_params(votes
, 12, 5);
1438 tt_str_op(res
,OP_EQ
, "cw=50 x-yz=-9");
1441 res
= dirvote_compute_params(votes
, 12, 9);
1442 tt_str_op(res
,OP_EQ
, "cw=50 x-yz=-9");
1445 smartlist_add(votes
, &vote4
);
1447 res
= dirvote_compute_params(votes
, 12, 4);
1448 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1451 res
= dirvote_compute_params(votes
, 12, 5);
1452 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1455 /* Test that the special-cased "at least three dirauths voted for
1456 * this param" logic works as expected. */
1457 res
= dirvote_compute_params(votes
, 12, 6);
1458 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1461 res
= dirvote_compute_params(votes
, 12, 10);
1462 tt_str_op(res
,OP_EQ
, "ab=90 abcd=20 cw=50 x-yz=-9");
1467 SMARTLIST_FOREACH(vote1
.net_params
, char *, cp
, tor_free(cp
));
1468 SMARTLIST_FOREACH(vote2
.net_params
, char *, cp
, tor_free(cp
));
1469 SMARTLIST_FOREACH(vote3
.net_params
, char *, cp
, tor_free(cp
));
1470 SMARTLIST_FOREACH(vote4
.net_params
, char *, cp
, tor_free(cp
));
1471 smartlist_free(vote1
.net_params
);
1472 smartlist_free(vote2
.net_params
);
1473 smartlist_free(vote3
.net_params
);
1474 smartlist_free(vote4
.net_params
);
1475 smartlist_free(votes
);
1480 extern const char AUTHORITY_CERT_1
[];
1481 extern const char AUTHORITY_SIGNKEY_1
[];
1482 extern const char AUTHORITY_CERT_2
[];
1483 extern const char AUTHORITY_SIGNKEY_2
[];
1484 extern const char AUTHORITY_CERT_3
[];
1485 extern const char AUTHORITY_SIGNKEY_3
[];
1487 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
1488 * same voting authority, and that they do in fact have all the same
1491 test_same_voter(networkstatus_voter_info_t
*v1
,
1492 networkstatus_voter_info_t
*v2
)
1494 tt_str_op(v1
->nickname
,OP_EQ
, v2
->nickname
);
1495 tt_mem_op(v1
->identity_digest
,OP_EQ
, v2
->identity_digest
, DIGEST_LEN
);
1496 tt_str_op(v1
->address
,OP_EQ
, v2
->address
);
1497 tt_int_op(v1
->addr
,OP_EQ
, v2
->addr
);
1498 tt_int_op(v1
->dir_port
,OP_EQ
, v2
->dir_port
);
1499 tt_int_op(v1
->or_port
,OP_EQ
, v2
->or_port
);
1500 tt_str_op(v1
->contact
,OP_EQ
, v2
->contact
);
1501 tt_mem_op(v1
->vote_digest
,OP_EQ
, v2
->vote_digest
, DIGEST_LEN
);
1506 /** Helper: Make a new routerinfo containing the right information for a
1507 * given vote_routerstatus_t. */
1508 static routerinfo_t
*
1509 generate_ri_from_rs(const vote_routerstatus_t
*vrs
)
1512 const routerstatus_t
*rs
= &vrs
->status
;
1513 static time_t published
= 0;
1515 r
= tor_malloc_zero(sizeof(routerinfo_t
));
1516 r
->cert_expiration_time
= TIME_MAX
;
1517 memcpy(r
->cache_info
.identity_digest
, rs
->identity_digest
, DIGEST_LEN
);
1518 memcpy(r
->cache_info
.signed_descriptor_digest
, rs
->descriptor_digest
,
1520 r
->cache_info
.do_not_cache
= 1;
1521 r
->cache_info
.routerlist_index
= -1;
1522 r
->cache_info
.signed_descriptor_body
=
1523 tor_strdup("123456789012345678901234567890123");
1524 r
->cache_info
.signed_descriptor_len
=
1525 strlen(r
->cache_info
.signed_descriptor_body
);
1526 r
->exit_policy
= smartlist_new();
1527 r
->cache_info
.published_on
= ++published
+ time(NULL
);
1528 if (rs
->has_bandwidth
) {
1530 * Multiply by 1000 because the routerinfo_t and the routerstatus_t
1531 * seem to use different units (*sigh*) and because we seem stuck on
1532 * icky and perverse decimal kilobytes (*double sigh*) - see
1533 * router_get_advertised_bandwidth_capped() of routerlist.c and
1534 * routerstatus_format_entry() of dirserv.c.
1536 r
->bandwidthrate
= rs
->bandwidth_kb
* 1000;
1537 r
->bandwidthcapacity
= rs
->bandwidth_kb
* 1000;
1542 /** Helper: get a detached signatures document for one or two
1545 get_detached_sigs(networkstatus_t
*ns
, networkstatus_t
*ns2
)
1549 tor_assert(ns
&& ns
->flavor
== FLAV_NS
);
1550 sl
= smartlist_new();
1551 smartlist_add(sl
,ns
);
1553 smartlist_add(sl
,ns2
);
1554 r
= networkstatus_get_detached_signatures(sl
);
1560 * Generate a routerstatus for v3_networkstatus test
1562 static vote_routerstatus_t
*
1563 gen_routerstatus_for_v3ns(int idx
, time_t now
)
1565 vote_routerstatus_t
*vrs
=NULL
;
1567 tor_addr_t addr_ipv6
;
1571 /* Generate the first routerstatus. */
1572 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
1574 vrs
->version
= tor_strdup("0.1.2.14");
1575 rs
->published_on
= now
-1500;
1576 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
1577 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
1578 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
1579 rs
->addr
= 0x99008801;
1581 rs
->dir_port
= 8000;
1582 /* all flags but running cleared */
1583 rs
->is_flagged_running
= 1;
1586 /* Generate the second routerstatus. */
1587 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
1589 vrs
->version
= tor_strdup("0.2.0.5");
1590 rs
->published_on
= now
-1000;
1591 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
1592 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
1593 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
1594 rs
->addr
= 0x99009901;
1597 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
1598 tor_addr_copy(&rs
->ipv6_addr
, &addr_ipv6
);
1599 rs
->ipv6_orport
= 4711;
1600 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_flagged_running
=
1601 rs
->is_valid
= rs
->is_possible_guard
= 1;
1604 /* Generate the third routerstatus. */
1605 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
1607 vrs
->version
= tor_strdup("0.1.0.3");
1608 rs
->published_on
= now
-1000;
1609 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
1610 memset(rs
->identity_digest
, 33, DIGEST_LEN
);
1611 memset(rs
->descriptor_digest
, 79, DIGEST_LEN
);
1612 rs
->addr
= 0xAA009901;
1614 rs
->dir_port
= 9999;
1615 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
1616 rs
->is_flagged_running
= rs
->is_valid
=
1617 rs
->is_possible_guard
= 1;
1620 /* Generate a fourth routerstatus that is not running. */
1621 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
1623 vrs
->version
= tor_strdup("0.1.6.3");
1624 rs
->published_on
= now
-1000;
1625 strlcpy(rs
->nickname
, "router4", sizeof(rs
->nickname
));
1626 memset(rs
->identity_digest
, 34, DIGEST_LEN
);
1627 memset(rs
->descriptor_digest
, 47, DIGEST_LEN
);
1628 rs
->addr
= 0xC0000203;
1630 rs
->dir_port
= 1999;
1631 /* Running flag (and others) cleared */
1634 /* No more for this test; return NULL */
1638 /* Shouldn't happen */
1642 vrs
->microdesc
= tor_malloc_zero(sizeof(vote_microdesc_hash_t
));
1643 tor_asprintf(&vrs
->microdesc
->microdesc_hash_line
,
1644 "m 9,10,11,12,13,14,15,16,17 "
1645 "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
1653 /** Apply tweaks to the vote list for each voter */
1655 vote_tweaks_for_v3ns(networkstatus_t
*v
, int voter
, time_t now
)
1657 vote_routerstatus_t
*vrs
;
1658 const char *msg
= NULL
;
1664 measured_bw_line_t mbw
;
1665 memset(mbw
.node_id
, 33, sizeof(mbw
.node_id
));
1667 tt_assert(measured_bw_line_apply(&mbw
,
1668 v
->routerstatus_list
) == 1);
1669 } else if (voter
== 2 || voter
== 3) {
1670 /* Monkey around with the list a bit */
1671 vrs
= smartlist_get(v
->routerstatus_list
, 2);
1672 smartlist_del_keeporder(v
->routerstatus_list
, 2);
1673 vote_routerstatus_free(vrs
);
1674 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1675 vrs
->status
.is_fast
= 1;
1678 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1679 smartlist_del_keeporder(v
->routerstatus_list
, 0);
1680 vote_routerstatus_free(vrs
);
1681 vrs
= smartlist_get(v
->routerstatus_list
, 0);
1682 memset(vrs
->status
.descriptor_digest
, (int)'Z', DIGEST_LEN
);
1683 tt_assert(router_add_to_routerlist(
1684 generate_ri_from_rs(vrs
), &msg
,0,0) >= 0);
1693 * Test a parsed vote_routerstatus_t for v3_networkstatus test
1696 test_vrs_for_v3ns(vote_routerstatus_t
*vrs
, int voter
, time_t now
)
1699 tor_addr_t addr_ipv6
;
1702 rs
= &(vrs
->status
);
1705 /* Split out by digests to test */
1706 if (tor_memeq(rs
->identity_digest
,
1707 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1711 /* Check the first routerstatus. */
1712 tt_str_op(vrs
->version
,OP_EQ
, "0.1.2.14");
1713 tt_int_op(rs
->published_on
,OP_EQ
, now
-1500);
1714 tt_str_op(rs
->nickname
,OP_EQ
, "router2");
1715 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1716 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1719 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
1720 tt_int_op(rs
->addr
,OP_EQ
, 0x99008801);
1721 tt_int_op(rs
->or_port
,OP_EQ
, 443);
1722 tt_int_op(rs
->dir_port
,OP_EQ
, 8000);
1723 /* no flags except "running" (16) and "v2dir" (64) */
1724 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(80));
1725 } else if (tor_memeq(rs
->identity_digest
,
1726 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1729 (voter
== 1 || voter
== 2)) {
1730 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1731 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1736 /* Check the second routerstatus. */
1737 tt_str_op(vrs
->version
,OP_EQ
, "0.2.0.5");
1738 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
1739 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
1741 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
1742 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
1743 tt_int_op(rs
->or_port
,OP_EQ
, 443);
1744 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
1745 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
1746 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
1747 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
1749 /* all except "authority" (1) and "v2dir" (64) */
1750 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(190));
1752 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) - v2dir(256) */
1753 tt_u64_op(vrs
->flags
, OP_EQ
, U64_LITERAL(718));
1755 } else if (tor_memeq(rs
->identity_digest
,
1756 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
1757 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
1759 (voter
== 1 || voter
== 2)) {
1760 /* Check the measured bandwidth bits */
1761 tt_assert(vrs
->has_measured_bw
&&
1762 vrs
->measured_bw_kb
== 1024);
1765 * Didn't expect this, but the old unit test only checked some of them,
1776 * Test a consensus for v3_networkstatus_test
1779 test_consensus_for_v3ns(networkstatus_t
*con
, time_t now
)
1784 tt_assert(!con
->cert
);
1785 tt_int_op(2,OP_EQ
, smartlist_len(con
->routerstatus_list
));
1786 /* There should be two listed routers: one with identity 3, one with
1794 * Test a router list entry for v3_networkstatus test
1797 test_routerstatus_for_v3ns(routerstatus_t
*rs
, time_t now
)
1799 tor_addr_t addr_ipv6
;
1803 /* There should be two listed routers: one with identity 3, one with
1805 /* This one showed up in 2 digests. */
1806 if (tor_memeq(rs
->identity_digest
,
1807 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1810 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1811 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1813 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
1814 tt_assert(!rs
->is_authority
);
1815 tt_assert(!rs
->is_exit
);
1816 tt_assert(!rs
->is_fast
);
1817 tt_assert(!rs
->is_possible_guard
);
1818 tt_assert(!rs
->is_stable
);
1819 /* (If it wasn't running it wouldn't be here) */
1820 tt_assert(rs
->is_flagged_running
);
1821 tt_assert(!rs
->is_valid
);
1822 tt_assert(!rs
->is_named
);
1823 /* XXXX check version */
1824 } else if (tor_memeq(rs
->identity_digest
,
1825 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1828 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
1829 tt_mem_op(rs
->identity_digest
,OP_EQ
,
1830 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1832 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
1833 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
1834 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
1835 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
1836 tt_int_op(rs
->or_port
,OP_EQ
, 443);
1837 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
1838 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
1839 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
1840 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
1841 tt_assert(!rs
->is_authority
);
1842 tt_assert(rs
->is_exit
);
1843 tt_assert(rs
->is_fast
);
1844 tt_assert(rs
->is_possible_guard
);
1845 tt_assert(rs
->is_stable
);
1846 tt_assert(rs
->is_flagged_running
);
1847 tt_assert(rs
->is_valid
);
1848 tt_assert(!rs
->is_named
);
1849 /* XXXX check version */
1851 /* Weren't expecting this... */
1859 /** Run a unit tests for generating and parsing networkstatuses, with
1860 * the supply test fns. */
1862 test_a_networkstatus(
1863 vote_routerstatus_t
* (*vrs_gen
)(int idx
, time_t now
),
1864 int (*vote_tweaks
)(networkstatus_t
*v
, int voter
, time_t now
),
1865 void (*vrs_test
)(vote_routerstatus_t
*vrs
, int voter
, time_t now
),
1866 void (*consensus_test
)(networkstatus_t
*con
, time_t now
),
1867 void (*rs_test
)(routerstatus_t
*rs
, time_t now
))
1869 authority_cert_t
*cert1
=NULL
, *cert2
=NULL
, *cert3
=NULL
;
1870 crypto_pk_t
*sign_skey_1
=NULL
, *sign_skey_2
=NULL
, *sign_skey_3
=NULL
;
1871 crypto_pk_t
*sign_skey_leg1
=NULL
;
1872 const char *msg
=NULL
;
1874 * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
1875 * returns non-zero, it changed net_params and we should skip the tests for
1876 * that later as they will fail.
1878 int params_tweaked
= 0;
1880 time_t now
= time(NULL
);
1881 networkstatus_voter_info_t
*voter
;
1882 document_signature_t
*sig
;
1883 networkstatus_t
*vote
=NULL
, *v1
=NULL
, *v2
=NULL
, *v3
=NULL
, *con
=NULL
,
1885 vote_routerstatus_t
*vrs
;
1887 int idx
, n_rs
, n_vrs
;
1888 char *v1_text
=NULL
, *v2_text
=NULL
, *v3_text
=NULL
, *consensus_text
=NULL
,
1890 smartlist_t
*votes
= smartlist_new();
1892 /* For generating the two other consensuses. */
1893 char *detached_text1
=NULL
, *detached_text2
=NULL
;
1894 char *consensus_text2
=NULL
, *consensus_text3
=NULL
;
1895 char *consensus_text_md2
=NULL
, *consensus_text_md3
=NULL
;
1896 char *consensus_text_md
=NULL
;
1897 networkstatus_t
*con2
=NULL
, *con_md2
=NULL
, *con3
=NULL
, *con_md3
=NULL
;
1898 ns_detached_signatures_t
*dsig1
=NULL
, *dsig2
=NULL
;
1902 tt_assert(vrs_test
);
1904 /* Parse certificates and keys. */
1905 cert1
= authority_cert_parse_from_string(AUTHORITY_CERT_1
, NULL
);
1907 cert2
= authority_cert_parse_from_string(AUTHORITY_CERT_2
, NULL
);
1909 cert3
= authority_cert_parse_from_string(AUTHORITY_CERT_3
, NULL
);
1911 sign_skey_1
= crypto_pk_new();
1912 sign_skey_2
= crypto_pk_new();
1913 sign_skey_3
= crypto_pk_new();
1914 sign_skey_leg1
= pk_generate(4);
1916 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_1
,
1917 AUTHORITY_SIGNKEY_1
, -1));
1918 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_2
,
1919 AUTHORITY_SIGNKEY_2
, -1));
1920 tt_assert(!crypto_pk_read_private_key_from_string(sign_skey_3
,
1921 AUTHORITY_SIGNKEY_3
, -1));
1923 tt_assert(!crypto_pk_cmp_keys(sign_skey_1
, cert1
->signing_key
));
1924 tt_assert(!crypto_pk_cmp_keys(sign_skey_2
, cert2
->signing_key
));
1927 * Set up a vote; generate it; try to parse it.
1929 vote
= tor_malloc_zero(sizeof(networkstatus_t
));
1930 vote
->type
= NS_TYPE_VOTE
;
1931 vote
->published
= now
;
1932 vote
->valid_after
= now
+1000;
1933 vote
->fresh_until
= now
+2000;
1934 vote
->valid_until
= now
+3000;
1935 vote
->vote_seconds
= 100;
1936 vote
->dist_seconds
= 200;
1937 vote
->supported_methods
= smartlist_new();
1938 smartlist_split_string(vote
->supported_methods
, "1 2 3", NULL
, 0, -1);
1939 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.15");
1940 vote
->server_versions
= tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
1941 vote
->known_flags
= smartlist_new();
1942 smartlist_split_string(vote
->known_flags
,
1943 "Authority Exit Fast Guard Running Stable V2Dir Valid",
1944 0, SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1945 vote
->voters
= smartlist_new();
1946 voter
= tor_malloc_zero(sizeof(networkstatus_voter_info_t
));
1947 voter
->nickname
= tor_strdup("Voter1");
1948 voter
->address
= tor_strdup("1.2.3.4");
1949 voter
->addr
= 0x01020304;
1950 voter
->dir_port
= 80;
1951 voter
->or_port
= 9000;
1952 voter
->contact
= tor_strdup("voter@example.com");
1953 crypto_pk_get_digest(cert1
->identity_key
, voter
->identity_digest
);
1954 smartlist_add(vote
->voters
, voter
);
1955 vote
->cert
= authority_cert_dup(cert1
);
1956 vote
->net_params
= smartlist_new();
1957 smartlist_split_string(vote
->net_params
, "circuitwindow=101 foo=990",
1959 vote
->routerstatus_list
= smartlist_new();
1960 /* add routerstatuses */
1963 vrs
= vrs_gen(idx
, now
);
1965 smartlist_add(vote
->routerstatus_list
, vrs
);
1966 tt_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
),
1973 /* dump the vote and try to parse it. */
1974 v1_text
= format_networkstatus_vote(sign_skey_1
, vote
);
1976 v1
= networkstatus_parse_vote_from_string(v1_text
, NULL
, NS_TYPE_VOTE
);
1979 /* Make sure the parsed thing was right. */
1980 tt_int_op(v1
->type
,OP_EQ
, NS_TYPE_VOTE
);
1981 tt_int_op(v1
->published
,OP_EQ
, vote
->published
);
1982 tt_int_op(v1
->valid_after
,OP_EQ
, vote
->valid_after
);
1983 tt_int_op(v1
->fresh_until
,OP_EQ
, vote
->fresh_until
);
1984 tt_int_op(v1
->valid_until
,OP_EQ
, vote
->valid_until
);
1985 tt_int_op(v1
->vote_seconds
,OP_EQ
, vote
->vote_seconds
);
1986 tt_int_op(v1
->dist_seconds
,OP_EQ
, vote
->dist_seconds
);
1987 tt_str_op(v1
->client_versions
,OP_EQ
, vote
->client_versions
);
1988 tt_str_op(v1
->server_versions
,OP_EQ
, vote
->server_versions
);
1989 tt_assert(v1
->voters
&& smartlist_len(v1
->voters
));
1990 voter
= smartlist_get(v1
->voters
, 0);
1991 tt_str_op(voter
->nickname
,OP_EQ
, "Voter1");
1992 tt_str_op(voter
->address
,OP_EQ
, "1.2.3.4");
1993 tt_int_op(voter
->addr
,OP_EQ
, 0x01020304);
1994 tt_int_op(voter
->dir_port
,OP_EQ
, 80);
1995 tt_int_op(voter
->or_port
,OP_EQ
, 9000);
1996 tt_str_op(voter
->contact
,OP_EQ
, "voter@example.com");
1997 tt_assert(v1
->cert
);
1998 tt_assert(!crypto_pk_cmp_keys(sign_skey_1
, v1
->cert
->signing_key
));
1999 cp
= smartlist_join_strings(v1
->known_flags
, ":", 0, NULL
);
2000 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
2002 tt_int_op(smartlist_len(v1
->routerstatus_list
),OP_EQ
, n_vrs
);
2004 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v1
, 1, now
);
2006 /* Check the routerstatuses. */
2007 for (idx
= 0; idx
< n_vrs
; ++idx
) {
2008 vrs
= smartlist_get(v1
->routerstatus_list
, idx
);
2010 vrs_test(vrs
, 1, now
);
2013 /* Generate second vote. It disagrees on some of the times,
2014 * and doesn't list versions, and knows some crazy flags */
2015 vote
->published
= now
+1;
2016 vote
->fresh_until
= now
+3005;
2017 vote
->dist_seconds
= 300;
2018 authority_cert_free(vote
->cert
);
2019 vote
->cert
= authority_cert_dup(cert2
);
2020 SMARTLIST_FOREACH(vote
->net_params
, char *, c
, tor_free(c
));
2021 smartlist_clear(vote
->net_params
);
2022 smartlist_split_string(vote
->net_params
, "bar=2000000000 circuitwindow=20",
2024 tor_free(vote
->client_versions
);
2025 tor_free(vote
->server_versions
);
2026 voter
= smartlist_get(vote
->voters
, 0);
2027 tor_free(voter
->nickname
);
2028 tor_free(voter
->address
);
2029 voter
->nickname
= tor_strdup("Voter2");
2030 voter
->address
= tor_strdup("2.3.4.5");
2031 voter
->addr
= 0x02030405;
2032 crypto_pk_get_digest(cert2
->identity_key
, voter
->identity_digest
);
2033 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfCheese"));
2034 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfTin"));
2035 smartlist_sort_strings(vote
->known_flags
);
2037 /* generate and parse v2. */
2038 v2_text
= format_networkstatus_vote(sign_skey_2
, vote
);
2040 v2
= networkstatus_parse_vote_from_string(v2_text
, NULL
, NS_TYPE_VOTE
);
2043 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v2
, 2, now
);
2045 /* Check that flags come out right.*/
2046 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2047 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2048 "Running:Stable:V2Dir:Valid");
2051 /* Check the routerstatuses. */
2052 n_vrs
= smartlist_len(v2
->routerstatus_list
);
2053 for (idx
= 0; idx
< n_vrs
; ++idx
) {
2054 vrs
= smartlist_get(v2
->routerstatus_list
, idx
);
2056 vrs_test(vrs
, 2, now
);
2059 /* Generate the third vote. */
2060 vote
->published
= now
;
2061 vote
->fresh_until
= now
+2003;
2062 vote
->dist_seconds
= 250;
2063 authority_cert_free(vote
->cert
);
2064 vote
->cert
= authority_cert_dup(cert3
);
2065 SMARTLIST_FOREACH(vote
->net_params
, char *, c
, tor_free(c
));
2066 smartlist_clear(vote
->net_params
);
2067 smartlist_split_string(vote
->net_params
, "circuitwindow=80 foo=660",
2069 smartlist_add(vote
->supported_methods
, tor_strdup("4"));
2070 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.17");
2071 vote
->server_versions
= tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
2072 voter
= smartlist_get(vote
->voters
, 0);
2073 tor_free(voter
->nickname
);
2074 tor_free(voter
->address
);
2075 voter
->nickname
= tor_strdup("Voter3");
2076 voter
->address
= tor_strdup("3.4.5.6");
2077 voter
->addr
= 0x03040506;
2078 crypto_pk_get_digest(cert3
->identity_key
, voter
->identity_digest
);
2079 /* This one has a legacy id. */
2080 memset(voter
->legacy_id_digest
, (int)'A', DIGEST_LEN
);
2082 v3_text
= format_networkstatus_vote(sign_skey_3
, vote
);
2085 v3
= networkstatus_parse_vote_from_string(v3_text
, NULL
, NS_TYPE_VOTE
);
2088 if (vote_tweaks
) params_tweaked
+= vote_tweaks(v3
, 3, now
);
2090 /* Compute a consensus as voter 3. */
2091 smartlist_add(votes
, v3
);
2092 smartlist_add(votes
, v1
);
2093 smartlist_add(votes
, v2
);
2094 consensus_text
= networkstatus_compute_consensus(votes
, 3,
2095 cert3
->identity_key
,
2097 "AAAAAAAAAAAAAAAAAAAA",
2100 tt_assert(consensus_text
);
2101 con
= networkstatus_parse_vote_from_string(consensus_text
, NULL
,
2104 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
2105 // v1_text, v2_text, v3_text);
2106 consensus_text_md
= networkstatus_compute_consensus(votes
, 3,
2107 cert3
->identity_key
,
2109 "AAAAAAAAAAAAAAAAAAAA",
2112 tt_assert(consensus_text_md
);
2113 con_md
= networkstatus_parse_vote_from_string(consensus_text_md
, NULL
,
2116 tt_int_op(con_md
->flavor
,OP_EQ
, FLAV_MICRODESC
);
2118 /* Check consensus contents. */
2119 tt_assert(con
->type
== NS_TYPE_CONSENSUS
);
2120 tt_int_op(con
->published
,OP_EQ
, 0); /* this field only appears in votes. */
2121 tt_int_op(con
->valid_after
,OP_EQ
, now
+1000);
2122 tt_int_op(con
->fresh_until
,OP_EQ
, now
+2003); /* median */
2123 tt_int_op(con
->valid_until
,OP_EQ
, now
+3000);
2124 tt_int_op(con
->vote_seconds
,OP_EQ
, 100);
2125 tt_int_op(con
->dist_seconds
,OP_EQ
, 250); /* median */
2126 tt_str_op(con
->client_versions
,OP_EQ
, "0.1.2.14");
2127 tt_str_op(con
->server_versions
,OP_EQ
, "0.1.2.15,0.1.2.16");
2128 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2129 tt_str_op(cp
,OP_EQ
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2130 "Running:Stable:V2Dir:Valid");
2132 if (!params_tweaked
) {
2133 /* Skip this one if vote_tweaks() messed with the param lists */
2134 cp
= smartlist_join_strings(con
->net_params
, ":", 0, NULL
);
2135 tt_str_op(cp
,OP_EQ
, "circuitwindow=80:foo=660");
2139 tt_int_op(4,OP_EQ
, smartlist_len(con
->voters
)); /*3 voters, 1 legacy key.*/
2140 /* The voter id digests should be in this order. */
2141 tt_assert(memcmp(cert2
->cache_info
.identity_digest
,
2142 cert1
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2143 tt_assert(memcmp(cert1
->cache_info
.identity_digest
,
2144 cert3
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2145 test_same_voter(smartlist_get(con
->voters
, 1),
2146 smartlist_get(v2
->voters
, 0));
2147 test_same_voter(smartlist_get(con
->voters
, 2),
2148 smartlist_get(v1
->voters
, 0));
2149 test_same_voter(smartlist_get(con
->voters
, 3),
2150 smartlist_get(v3
->voters
, 0));
2152 consensus_test(con
, now
);
2154 /* Check the routerstatuses. */
2155 n_rs
= smartlist_len(con
->routerstatus_list
);
2156 for (idx
= 0; idx
< n_rs
; ++idx
) {
2157 rs
= smartlist_get(con
->routerstatus_list
, idx
);
2162 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
2163 * The second one hasn't signed. The fourth one has signed: validate it. */
2164 voter
= smartlist_get(con
->voters
, 1);
2165 tt_int_op(smartlist_len(voter
->sigs
),OP_EQ
, 0);
2167 voter
= smartlist_get(con
->voters
, 3);
2168 tt_int_op(smartlist_len(voter
->sigs
),OP_EQ
, 1);
2169 sig
= smartlist_get(voter
->sigs
, 0);
2170 tt_assert(sig
->signature
);
2171 tt_assert(!sig
->good_signature
);
2172 tt_assert(!sig
->bad_signature
);
2174 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert3
));
2175 tt_assert(sig
->signature
);
2176 tt_assert(sig
->good_signature
);
2177 tt_assert(!sig
->bad_signature
);
2180 const char *msg
=NULL
;
2181 /* Compute the other two signed consensuses. */
2182 smartlist_shuffle(votes
);
2183 consensus_text2
= networkstatus_compute_consensus(votes
, 3,
2184 cert2
->identity_key
,
2185 sign_skey_2
, NULL
,NULL
,
2187 consensus_text_md2
= networkstatus_compute_consensus(votes
, 3,
2188 cert2
->identity_key
,
2189 sign_skey_2
, NULL
,NULL
,
2191 smartlist_shuffle(votes
);
2192 consensus_text3
= networkstatus_compute_consensus(votes
, 3,
2193 cert1
->identity_key
,
2194 sign_skey_1
, NULL
,NULL
,
2196 consensus_text_md3
= networkstatus_compute_consensus(votes
, 3,
2197 cert1
->identity_key
,
2198 sign_skey_1
, NULL
,NULL
,
2200 tt_assert(consensus_text2
);
2201 tt_assert(consensus_text3
);
2202 tt_assert(consensus_text_md2
);
2203 tt_assert(consensus_text_md3
);
2204 con2
= networkstatus_parse_vote_from_string(consensus_text2
, NULL
,
2206 con3
= networkstatus_parse_vote_from_string(consensus_text3
, NULL
,
2208 con_md2
= networkstatus_parse_vote_from_string(consensus_text_md2
, NULL
,
2210 con_md3
= networkstatus_parse_vote_from_string(consensus_text_md3
, NULL
,
2217 /* All three should have the same digest. */
2218 tt_mem_op(&con
->digests
,OP_EQ
, &con2
->digests
, sizeof(digests_t
));
2219 tt_mem_op(&con
->digests
,OP_EQ
, &con3
->digests
, sizeof(digests_t
));
2221 tt_mem_op(&con_md
->digests
,OP_EQ
, &con_md2
->digests
, sizeof(digests_t
));
2222 tt_mem_op(&con_md
->digests
,OP_EQ
, &con_md3
->digests
, sizeof(digests_t
));
2224 /* Extract a detached signature from con3. */
2225 detached_text1
= get_detached_sigs(con3
, con_md3
);
2226 tt_assert(detached_text1
);
2227 /* Try to parse it. */
2228 dsig1
= networkstatus_parse_detached_signatures(detached_text1
, NULL
);
2231 /* Are parsed values as expected? */
2232 tt_int_op(dsig1
->valid_after
,OP_EQ
, con3
->valid_after
);
2233 tt_int_op(dsig1
->fresh_until
,OP_EQ
, con3
->fresh_until
);
2234 tt_int_op(dsig1
->valid_until
,OP_EQ
, con3
->valid_until
);
2236 digests_t
*dsig_digests
= strmap_get(dsig1
->digests
, "ns");
2237 tt_assert(dsig_digests
);
2238 tt_mem_op(dsig_digests
->d
[DIGEST_SHA1
], OP_EQ
,
2239 con3
->digests
.d
[DIGEST_SHA1
], DIGEST_LEN
);
2240 dsig_digests
= strmap_get(dsig1
->digests
, "microdesc");
2241 tt_assert(dsig_digests
);
2242 tt_mem_op(dsig_digests
->d
[DIGEST_SHA256
],OP_EQ
,
2243 con_md3
->digests
.d
[DIGEST_SHA256
],
2247 smartlist_t
*dsig_signatures
= strmap_get(dsig1
->signatures
, "ns");
2248 tt_assert(dsig_signatures
);
2249 tt_int_op(1,OP_EQ
, smartlist_len(dsig_signatures
));
2250 sig
= smartlist_get(dsig_signatures
, 0);
2251 tt_mem_op(sig
->identity_digest
,OP_EQ
, cert1
->cache_info
.identity_digest
,
2253 tt_int_op(sig
->alg
,OP_EQ
, DIGEST_SHA1
);
2255 dsig_signatures
= strmap_get(dsig1
->signatures
, "microdesc");
2256 tt_assert(dsig_signatures
);
2257 tt_int_op(1,OP_EQ
, smartlist_len(dsig_signatures
));
2258 sig
= smartlist_get(dsig_signatures
, 0);
2259 tt_mem_op(sig
->identity_digest
,OP_EQ
, cert1
->cache_info
.identity_digest
,
2261 tt_int_op(sig
->alg
,OP_EQ
, DIGEST_SHA256
);
2264 /* Try adding it to con2. */
2265 detached_text2
= get_detached_sigs(con2
,con_md2
);
2266 tt_int_op(1,OP_EQ
, networkstatus_add_detached_signatures(con2
, dsig1
,
2267 "test", LOG_INFO
, &msg
));
2268 tor_free(detached_text2
);
2270 networkstatus_add_detached_signatures(con_md2
, dsig1
, "test",
2272 tor_free(detached_text2
);
2273 detached_text2
= get_detached_sigs(con2
,con_md2
);
2274 //printf("\n<%s>\n", detached_text2);
2275 dsig2
= networkstatus_parse_detached_signatures(detached_text2
, NULL
);
2279 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2281 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2286 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
, "ns")));
2288 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
,
2291 /* Try adding to con2 twice; verify that nothing changes. */
2292 tt_int_op(0,OP_EQ
, networkstatus_add_detached_signatures(con2
, dsig1
,
2293 "test", LOG_INFO
, &msg
));
2296 tt_int_op(2,OP_EQ
, networkstatus_add_detached_signatures(con
, dsig2
,
2297 "test", LOG_INFO
, &msg
));
2298 /* Check signatures */
2299 voter
= smartlist_get(con
->voters
, 1);
2300 sig
= smartlist_get(voter
->sigs
, 0);
2302 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert2
));
2303 voter
= smartlist_get(con
->voters
, 2);
2304 sig
= smartlist_get(voter
->sigs
, 0);
2306 tt_assert(!networkstatus_check_document_signature(con
, sig
, cert1
));
2311 smartlist_free(votes
);
2315 tor_free(consensus_text
);
2316 tor_free(consensus_text_md
);
2319 networkstatus_vote_free(vote
);
2321 networkstatus_vote_free(v1
);
2323 networkstatus_vote_free(v2
);
2325 networkstatus_vote_free(v3
);
2327 networkstatus_vote_free(con
);
2329 networkstatus_vote_free(con_md
);
2331 crypto_pk_free(sign_skey_1
);
2333 crypto_pk_free(sign_skey_2
);
2335 crypto_pk_free(sign_skey_3
);
2337 crypto_pk_free(sign_skey_leg1
);
2339 authority_cert_free(cert1
);
2341 authority_cert_free(cert2
);
2343 authority_cert_free(cert3
);
2345 tor_free(consensus_text2
);
2346 tor_free(consensus_text3
);
2347 tor_free(consensus_text_md2
);
2348 tor_free(consensus_text_md3
);
2349 tor_free(detached_text1
);
2350 tor_free(detached_text2
);
2352 networkstatus_vote_free(con2
);
2354 networkstatus_vote_free(con3
);
2356 networkstatus_vote_free(con_md2
);
2358 networkstatus_vote_free(con_md3
);
2360 ns_detached_signatures_free(dsig1
);
2362 ns_detached_signatures_free(dsig2
);
2365 /** Run unit tests for generating and parsing V3 consensus networkstatus
2368 test_dir_v3_networkstatus(void *arg
)
2371 test_a_networkstatus(gen_routerstatus_for_v3ns
,
2372 vote_tweaks_for_v3ns
,
2374 test_consensus_for_v3ns
,
2375 test_routerstatus_for_v3ns
);
2379 test_dir_scale_bw(void *testdata
)
2381 double v
[8] = { 2.0/3,
2398 scale_array_elements_to_u64(vals
, 8, &total
);
2400 tt_int_op((int)total
, OP_EQ
, 48);
2402 for (i
=0; i
<8; ++i
) {
2403 total
+= vals
[i
].u64
;
2405 tt_assert(total
>= (U64_LITERAL(1)<<60));
2406 tt_assert(total
<= (U64_LITERAL(1)<<62));
2408 for (i
=0; i
<8; ++i
) {
2409 /* vals[2].u64 is the scaled value of 1.0 */
2410 double ratio
= ((double)vals
[i
].u64
) / vals
[2].u64
;
2411 tt_double_op(fabs(ratio
- v
[i
]), OP_LT
, .00001);
2414 /* test handling of no entries */
2416 scale_array_elements_to_u64(vals
, 0, &total
);
2417 tt_assert(total
== 0);
2419 /* make sure we don't read the array when we have no entries
2420 * may require compiler flags to catch NULL dereferences */
2422 scale_array_elements_to_u64(NULL
, 0, &total
);
2423 tt_assert(total
== 0);
2425 scale_array_elements_to_u64(NULL
, 0, NULL
);
2427 /* test handling of zero totals */
2430 scale_array_elements_to_u64(vals
, 1, &total
);
2431 tt_assert(total
== 0);
2432 tt_assert(vals
[0].u64
== 0);
2436 scale_array_elements_to_u64(vals
, 2, NULL
);
2437 tt_assert(vals
[0].u64
== 0);
2438 tt_assert(vals
[1].u64
== 0);
2445 test_dir_random_weighted(void *testdata
)
2448 uint64_t vals
[10] = {3,1,2,4,6,0,7,5,8,9}, total
=0;
2451 const int n
= 50000;
2452 double max_sq_error
;
2455 /* Try a ten-element array with values from 0 through 10. The values are
2456 * in a scrambled order to make sure we don't depend on order. */
2457 memset(histogram
,0,sizeof(histogram
));
2458 for (i
=0; i
<10; ++i
) {
2459 inp
[i
].u64
= vals
[i
];
2462 tt_u64_op(total
, OP_EQ
, 45);
2463 for (i
=0; i
<n
; ++i
) {
2464 choice
= choose_array_element_by_weight(inp
, 10);
2465 tt_int_op(choice
, OP_GE
, 0);
2466 tt_int_op(choice
, OP_LT
, 10);
2467 histogram
[choice
]++;
2470 /* Now see if we chose things about frequently enough. */
2472 for (i
=0; i
<10; ++i
) {
2473 int expected
= (int)(n
*vals
[i
]/total
);
2474 double frac_diff
= 0, sq
;
2475 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals
[i
], histogram
[i
], expected
));
2477 frac_diff
= (histogram
[i
] - expected
) / ((double)expected
);
2479 tt_int_op(histogram
[i
], OP_EQ
, 0);
2481 sq
= frac_diff
* frac_diff
;
2482 if (sq
> max_sq_error
)
2485 /* It should almost always be much much less than this. If you want to
2486 * figure out the odds, please feel free. */
2487 tt_double_op(max_sq_error
, OP_LT
, .05);
2489 /* Now try a singleton; do we choose it? */
2490 for (i
= 0; i
< 100; ++i
) {
2491 choice
= choose_array_element_by_weight(inp
, 1);
2492 tt_int_op(choice
, OP_EQ
, 0);
2495 /* Now try an array of zeros. We should choose randomly. */
2496 memset(histogram
,0,sizeof(histogram
));
2497 for (i
= 0; i
< 5; ++i
)
2499 for (i
= 0; i
< n
; ++i
) {
2500 choice
= choose_array_element_by_weight(inp
, 5);
2501 tt_int_op(choice
, OP_GE
, 0);
2502 tt_int_op(choice
, OP_LT
, 5);
2503 histogram
[choice
]++;
2505 /* Now see if we chose things about frequently enough. */
2507 for (i
=0; i
<5; ++i
) {
2509 double frac_diff
= 0, sq
;
2510 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals
[i
], histogram
[i
], expected
));
2511 frac_diff
= (histogram
[i
] - expected
) / ((double)expected
);
2512 sq
= frac_diff
* frac_diff
;
2513 if (sq
> max_sq_error
)
2516 /* It should almost always be much much less than this. If you want to
2517 * figure out the odds, please feel free. */
2518 tt_double_op(max_sq_error
, OP_LT
, .05);
2523 /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
2525 static uint32_t alternate_clip_bw
= 0;
2528 * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
2529 * v3_networkstatus ones.
2531 static vote_routerstatus_t
*
2532 gen_routerstatus_for_umbw(int idx
, time_t now
)
2534 vote_routerstatus_t
*vrs
= NULL
;
2536 tor_addr_t addr_ipv6
;
2537 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
2538 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
2542 /* Generate the first routerstatus. */
2543 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2545 vrs
->version
= tor_strdup("0.1.2.14");
2546 rs
->published_on
= now
-1500;
2547 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
2548 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
2549 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2550 rs
->addr
= 0x99008801;
2552 rs
->dir_port
= 8000;
2553 /* all flags but running cleared */
2554 rs
->is_flagged_running
= 1;
2556 * This one has measured bandwidth below the clip cutoff, and
2557 * so shouldn't be clipped; we'll have to test that it isn't
2560 vrs
->has_measured_bw
= 1;
2561 rs
->has_bandwidth
= 1;
2562 vrs
->measured_bw_kb
= rs
->bandwidth_kb
= max_unmeasured_bw_kb
/ 2;
2565 /* Generate the second routerstatus. */
2566 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2568 vrs
->version
= tor_strdup("0.2.0.5");
2569 rs
->published_on
= now
-1000;
2570 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
2571 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
2572 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
2573 rs
->addr
= 0x99009901;
2576 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
2577 tor_addr_copy(&rs
->ipv6_addr
, &addr_ipv6
);
2578 rs
->ipv6_orport
= 4711;
2579 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_flagged_running
=
2580 rs
->is_valid
= rs
->is_possible_guard
= 1;
2582 * This one has measured bandwidth above the clip cutoff, and
2583 * so shouldn't be clipped; we'll have to test that it isn't
2586 vrs
->has_measured_bw
= 1;
2587 rs
->has_bandwidth
= 1;
2588 vrs
->measured_bw_kb
= rs
->bandwidth_kb
= 2 * max_unmeasured_bw_kb
;
2591 /* Generate the third routerstatus. */
2592 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2594 vrs
->version
= tor_strdup("0.1.0.3");
2595 rs
->published_on
= now
-1000;
2596 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
2597 memset(rs
->identity_digest
, 0x33, DIGEST_LEN
);
2598 memset(rs
->descriptor_digest
, 79, DIGEST_LEN
);
2599 rs
->addr
= 0xAA009901;
2601 rs
->dir_port
= 9999;
2602 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
2603 rs
->is_flagged_running
= rs
->is_valid
=
2604 rs
->is_possible_guard
= 1;
2606 * This one has unmeasured bandwidth above the clip cutoff, and
2607 * so should be clipped; we'll have to test that it isn't
2610 vrs
->has_measured_bw
= 0;
2611 rs
->has_bandwidth
= 1;
2612 vrs
->measured_bw_kb
= 0;
2613 rs
->bandwidth_kb
= 2 * max_unmeasured_bw_kb
;
2616 /* Generate a fourth routerstatus that is not running. */
2617 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2619 vrs
->version
= tor_strdup("0.1.6.3");
2620 rs
->published_on
= now
-1000;
2621 strlcpy(rs
->nickname
, "router4", sizeof(rs
->nickname
));
2622 memset(rs
->identity_digest
, 0x34, DIGEST_LEN
);
2623 memset(rs
->descriptor_digest
, 47, DIGEST_LEN
);
2624 rs
->addr
= 0xC0000203;
2626 rs
->dir_port
= 1999;
2627 /* all flags but running cleared */
2628 rs
->is_flagged_running
= 1;
2630 * This one has unmeasured bandwidth below the clip cutoff, and
2631 * so shouldn't be clipped; we'll have to test that it isn't
2634 vrs
->has_measured_bw
= 0;
2635 rs
->has_bandwidth
= 1;
2636 vrs
->measured_bw_kb
= 0;
2637 rs
->bandwidth_kb
= max_unmeasured_bw_kb
/ 2;
2640 /* No more for this test; return NULL */
2644 /* Shouldn't happen */
2648 vrs
->microdesc
= tor_malloc_zero(sizeof(vote_microdesc_hash_t
));
2649 tor_asprintf(&vrs
->microdesc
->microdesc_hash_line
,
2650 "m 9,10,11,12,13,14,15,16,17 "
2651 "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
2659 /** Apply tweaks to the vote list for each voter; for the umbw test this is
2660 * just adding the right consensus methods to let clipping happen */
2662 vote_tweaks_for_umbw(networkstatus_t
*v
, int voter
, time_t now
)
2664 char *maxbw_param
= NULL
;
2671 tt_assert(v
->supported_methods
);
2672 SMARTLIST_FOREACH(v
->supported_methods
, char *, c
, tor_free(c
));
2673 smartlist_clear(v
->supported_methods
);
2674 /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
2675 smartlist_split_string(v
->supported_methods
,
2676 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
2678 /* If we're using a non-default clip bandwidth, add it to net_params */
2679 if (alternate_clip_bw
> 0) {
2680 tor_asprintf(&maxbw_param
, "maxunmeasuredbw=%u", alternate_clip_bw
);
2681 tt_assert(maxbw_param
);
2683 smartlist_add(v
->net_params
, maxbw_param
);
2693 * Test a parsed vote_routerstatus_t for umbw test.
2696 test_vrs_for_umbw(vote_routerstatus_t
*vrs
, int voter
, time_t now
)
2699 tor_addr_t addr_ipv6
;
2700 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
2701 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
2705 rs
= &(vrs
->status
);
2708 /* Split out by digests to test */
2709 if (tor_memeq(rs
->identity_digest
,
2710 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2711 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2714 * Check the first routerstatus - measured bandwidth below the clip
2717 tt_str_op(vrs
->version
,OP_EQ
, "0.1.2.14");
2718 tt_int_op(rs
->published_on
,OP_EQ
, now
-1500);
2719 tt_str_op(rs
->nickname
,OP_EQ
, "router2");
2720 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2721 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2722 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2724 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2725 tt_int_op(rs
->addr
,OP_EQ
, 0x99008801);
2726 tt_int_op(rs
->or_port
,OP_EQ
, 443);
2727 tt_int_op(rs
->dir_port
,OP_EQ
, 8000);
2728 tt_assert(rs
->has_bandwidth
);
2729 tt_assert(vrs
->has_measured_bw
);
2730 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
2731 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
2732 } else if (tor_memeq(rs
->identity_digest
,
2733 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2734 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2738 * Check the second routerstatus - measured bandwidth above the clip
2741 tt_str_op(vrs
->version
,OP_EQ
, "0.2.0.5");
2742 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
2743 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
2744 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2745 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2746 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2748 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2749 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
2750 tt_int_op(rs
->or_port
,OP_EQ
, 443);
2751 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
2752 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
2753 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
2754 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
2755 tt_assert(rs
->has_bandwidth
);
2756 tt_assert(vrs
->has_measured_bw
);
2757 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
2758 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
2759 } else if (tor_memeq(rs
->identity_digest
,
2760 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2761 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2764 * Check the third routerstatus - unmeasured bandwidth above the clip
2765 * cutoff; this one should be clipped later on in the consensus, but
2766 * appears unclipped in the vote.
2768 tt_assert(rs
->has_bandwidth
);
2769 tt_assert(!(vrs
->has_measured_bw
));
2770 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
2771 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, 0);
2772 } else if (tor_memeq(rs
->identity_digest
,
2773 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2774 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2777 * Check the fourth routerstatus - unmeasured bandwidth below the clip
2778 * cutoff; this one should not be clipped.
2780 tt_assert(rs
->has_bandwidth
);
2781 tt_assert(!(vrs
->has_measured_bw
));
2782 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
2783 tt_int_op(vrs
->measured_bw_kb
,OP_EQ
, 0);
2793 * Test a consensus for v3_networkstatus_test
2796 test_consensus_for_umbw(networkstatus_t
*con
, time_t now
)
2801 tt_assert(!con
->cert
);
2802 // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
2803 tt_assert(con
->consensus_method
>= 16);
2804 tt_int_op(4,OP_EQ
, smartlist_len(con
->routerstatus_list
));
2805 /* There should be four listed routers; all voters saw the same in this */
2812 * Test a router list entry for umbw test
2815 test_routerstatus_for_umbw(routerstatus_t
*rs
, time_t now
)
2817 tor_addr_t addr_ipv6
;
2818 uint32_t max_unmeasured_bw_kb
= (alternate_clip_bw
> 0) ?
2819 alternate_clip_bw
: DEFAULT_MAX_UNMEASURED_BW_KB
;
2823 /* There should be four listed routers, as constructed above */
2824 if (tor_memeq(rs
->identity_digest
,
2825 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2826 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2828 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2829 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2830 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2832 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2833 tt_assert(!rs
->is_authority
);
2834 tt_assert(!rs
->is_exit
);
2835 tt_assert(!rs
->is_fast
);
2836 tt_assert(!rs
->is_possible_guard
);
2837 tt_assert(!rs
->is_stable
);
2838 /* (If it wasn't running it wouldn't be here) */
2839 tt_assert(rs
->is_flagged_running
);
2840 tt_assert(!rs
->is_valid
);
2841 tt_assert(!rs
->is_named
);
2842 /* This one should have measured bandwidth below the clip cutoff */
2843 tt_assert(rs
->has_bandwidth
);
2844 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
2845 tt_assert(!(rs
->bw_is_unmeasured
));
2846 } else if (tor_memeq(rs
->identity_digest
,
2847 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2848 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2850 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2851 tt_mem_op(rs
->identity_digest
,OP_EQ
,
2852 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2853 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2855 tt_str_op(rs
->nickname
,OP_EQ
, "router1");
2856 tt_mem_op(rs
->descriptor_digest
,OP_EQ
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2857 tt_int_op(rs
->published_on
,OP_EQ
, now
-1000);
2858 tt_int_op(rs
->addr
,OP_EQ
, 0x99009901);
2859 tt_int_op(rs
->or_port
,OP_EQ
, 443);
2860 tt_int_op(rs
->dir_port
,OP_EQ
, 0);
2861 tor_addr_parse(&addr_ipv6
, "[1:2:3::4]");
2862 tt_assert(tor_addr_eq(&rs
->ipv6_addr
, &addr_ipv6
));
2863 tt_int_op(rs
->ipv6_orport
,OP_EQ
, 4711);
2864 tt_assert(!rs
->is_authority
);
2865 tt_assert(rs
->is_exit
);
2866 tt_assert(rs
->is_fast
);
2867 tt_assert(rs
->is_possible_guard
);
2868 tt_assert(rs
->is_stable
);
2869 tt_assert(rs
->is_flagged_running
);
2870 tt_assert(rs
->is_valid
);
2871 tt_assert(!rs
->is_named
);
2872 /* This one should have measured bandwidth above the clip cutoff */
2873 tt_assert(rs
->has_bandwidth
);
2874 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
* 2);
2875 tt_assert(!(rs
->bw_is_unmeasured
));
2876 } else if (tor_memeq(rs
->identity_digest
,
2877 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2878 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2881 * This one should have unmeasured bandwidth above the clip cutoff,
2882 * and so should be clipped
2884 tt_assert(rs
->has_bandwidth
);
2885 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
);
2886 tt_assert(rs
->bw_is_unmeasured
);
2887 } else if (tor_memeq(rs
->identity_digest
,
2888 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2889 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2892 * This one should have unmeasured bandwidth below the clip cutoff,
2893 * and so should not be clipped
2895 tt_assert(rs
->has_bandwidth
);
2896 tt_int_op(rs
->bandwidth_kb
,OP_EQ
, max_unmeasured_bw_kb
/ 2);
2897 tt_assert(rs
->bw_is_unmeasured
);
2899 /* Weren't expecting this... */
2908 * Compute a consensus involving clipping unmeasured bandwidth with consensus
2909 * method 17; this uses the same test_a_networkstatus() function that the
2910 * v3_networkstatus test uses.
2914 test_dir_clip_unmeasured_bw_kb(void *arg
)
2916 /* Run the test with the default clip bandwidth */
2918 alternate_clip_bw
= 0;
2919 test_a_networkstatus(gen_routerstatus_for_umbw
,
2920 vote_tweaks_for_umbw
,
2922 test_consensus_for_umbw
,
2923 test_routerstatus_for_umbw
);
2927 * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
2928 * of clip bandwidth.
2932 test_dir_clip_unmeasured_bw_kb_alt(void *arg
)
2935 * Try a different one; this value is chosen so that the below-the-cutoff
2936 * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
2937 * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
2938 * cutoff it will fail the test.
2941 alternate_clip_bw
= 3 * DEFAULT_MAX_UNMEASURED_BW_KB
;
2942 test_a_networkstatus(gen_routerstatus_for_umbw
,
2943 vote_tweaks_for_umbw
,
2945 test_consensus_for_umbw
,
2946 test_routerstatus_for_umbw
);
2950 test_dir_fmt_control_ns(void *arg
)
2956 memset(&rs
, 0, sizeof(rs
));
2957 rs
.published_on
= 1364925198;
2958 strlcpy(rs
.nickname
, "TetsuoMilk", sizeof(rs
.nickname
));
2959 memcpy(rs
.identity_digest
, "Stately, plump Buck ", DIGEST_LEN
);
2960 memcpy(rs
.descriptor_digest
, "Mulligan came up fro", DIGEST_LEN
);
2961 rs
.addr
= 0x20304050;
2966 rs
.is_flagged_running
= 1;
2967 rs
.has_bandwidth
= 1;
2968 rs
.bandwidth_kb
= 1000;
2970 s
= networkstatus_getinfo_helper_single(&rs
);
2973 "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
2974 "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
2975 "32.48.64.80 9001 9002\n"
2976 "s Exit Fast Running V2Dir\n"
2977 "w Bandwidth=1000\n");
2983 static int mock_get_options_calls
= 0;
2984 static or_options_t
*mock_options
= NULL
;
2987 reset_options(or_options_t
*options
, int *get_options_calls
)
2989 memset(options
, 0, sizeof(or_options_t
));
2990 options
->TestingTorNetwork
= 1;
2992 *get_options_calls
= 0;
2995 static const or_options_t
*
2996 mock_get_options(void)
2998 ++mock_get_options_calls
;
2999 tor_assert(mock_options
);
3000 return mock_options
;
3004 reset_routerstatus(routerstatus_t
*rs
,
3005 const char *hex_identity_digest
,
3008 memset(rs
, 0, sizeof(routerstatus_t
));
3009 base16_decode(rs
->identity_digest
, sizeof(rs
->identity_digest
),
3010 hex_identity_digest
, HEX_DIGEST_LEN
);
3011 /* A zero address matches everything, so the address needs to be set.
3012 * But the specific value is irrelevant. */
3013 rs
->addr
= ipv4_addr
;
3016 #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
3017 #define ROUTER_A_IPV4 0xAA008801
3018 #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
3019 #define ROUTER_B_IPV4 0xBB008801
3021 #define ROUTERSET_ALL_STR "*"
3022 #define ROUTERSET_A_STR ROUTER_A_ID_STR
3023 #define ROUTERSET_NONE_STR ""
3026 * Test that dirserv_set_routerstatus_testing sets router flags correctly
3027 * Using "*" sets flags on A and B
3028 * Using "A" sets flags on A
3029 * Using "" sets flags on Neither
3030 * If the router is not included:
3031 * - if *Strict is set, the flag is set to 0,
3032 * - otherwise, the flag is not modified. */
3034 test_dir_dirserv_set_routerstatus_testing(void *arg
)
3039 mock_options
= malloc(sizeof(or_options_t
));
3040 reset_options(mock_options
, &mock_get_options_calls
);
3042 MOCK(get_options
, mock_get_options
);
3044 /* Init routersets */
3045 routerset_t
*routerset_all
= routerset_new();
3046 routerset_parse(routerset_all
, ROUTERSET_ALL_STR
, "All routers");
3048 routerset_t
*routerset_a
= routerset_new();
3049 routerset_parse(routerset_a
, ROUTERSET_A_STR
, "Router A only");
3051 routerset_t
*routerset_none
= routerset_new();
3052 /* Routersets are empty when provided by routerset_new(),
3053 * so this is not strictly necessary */
3054 routerset_parse(routerset_none
, ROUTERSET_NONE_STR
, "No routers");
3056 /* Init routerstatuses */
3057 routerstatus_t
*rs_a
= malloc(sizeof(routerstatus_t
));
3058 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3060 routerstatus_t
*rs_b
= malloc(sizeof(routerstatus_t
));
3061 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3063 /* Sanity check that routersets correspond to routerstatuses.
3064 * Return values are {2, 3, 4} */
3066 /* We want 3 ("*" means match all addresses) */
3067 tt_assert(routerset_contains_routerstatus(routerset_all
, rs_a
, 0) == 3);
3068 tt_assert(routerset_contains_routerstatus(routerset_all
, rs_b
, 0) == 3);
3070 /* We want 4 (match id_digest [or nickname]) */
3071 tt_assert(routerset_contains_routerstatus(routerset_a
, rs_a
, 0) == 4);
3072 tt_assert(routerset_contains_routerstatus(routerset_a
, rs_b
, 0) == 0);
3074 tt_assert(routerset_contains_routerstatus(routerset_none
, rs_a
, 0) == 0);
3075 tt_assert(routerset_contains_routerstatus(routerset_none
, rs_b
, 0) == 0);
3077 /* Check that "*" sets flags on all routers: Exit
3078 * Check the flags aren't being confused with each other */
3079 reset_options(mock_options
, &mock_get_options_calls
);
3080 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3081 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3083 mock_options
->TestingDirAuthVoteExit
= routerset_all
;
3084 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3086 dirserv_set_routerstatus_testing(rs_a
);
3087 tt_assert(mock_get_options_calls
== 1);
3088 dirserv_set_routerstatus_testing(rs_b
);
3089 tt_assert(mock_get_options_calls
== 2);
3091 tt_assert(rs_a
->is_exit
== 1);
3092 tt_assert(rs_b
->is_exit
== 1);
3093 /* Be paranoid - check no other flags are set */
3094 tt_assert(rs_a
->is_possible_guard
== 0);
3095 tt_assert(rs_b
->is_possible_guard
== 0);
3096 tt_assert(rs_a
->is_hs_dir
== 0);
3097 tt_assert(rs_b
->is_hs_dir
== 0);
3099 /* Check that "*" sets flags on all routers: Guard & HSDir
3100 * Cover the remaining flags in one test */
3101 reset_options(mock_options
, &mock_get_options_calls
);
3102 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3103 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3105 mock_options
->TestingDirAuthVoteGuard
= routerset_all
;
3106 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3107 mock_options
->TestingDirAuthVoteHSDir
= routerset_all
;
3108 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3110 dirserv_set_routerstatus_testing(rs_a
);
3111 tt_assert(mock_get_options_calls
== 1);
3112 dirserv_set_routerstatus_testing(rs_b
);
3113 tt_assert(mock_get_options_calls
== 2);
3115 tt_assert(rs_a
->is_possible_guard
== 1);
3116 tt_assert(rs_b
->is_possible_guard
== 1);
3117 tt_assert(rs_a
->is_hs_dir
== 1);
3118 tt_assert(rs_b
->is_hs_dir
== 1);
3119 /* Be paranoid - check exit isn't set */
3120 tt_assert(rs_a
->is_exit
== 0);
3121 tt_assert(rs_b
->is_exit
== 0);
3123 /* Check routerset A sets all flags on router A,
3124 * but leaves router B unmodified */
3125 reset_options(mock_options
, &mock_get_options_calls
);
3126 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3127 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3129 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3130 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3131 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3132 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3133 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3134 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3136 dirserv_set_routerstatus_testing(rs_a
);
3137 tt_assert(mock_get_options_calls
== 1);
3138 dirserv_set_routerstatus_testing(rs_b
);
3139 tt_assert(mock_get_options_calls
== 2);
3141 tt_assert(rs_a
->is_exit
== 1);
3142 tt_assert(rs_b
->is_exit
== 0);
3143 tt_assert(rs_a
->is_possible_guard
== 1);
3144 tt_assert(rs_b
->is_possible_guard
== 0);
3145 tt_assert(rs_a
->is_hs_dir
== 1);
3146 tt_assert(rs_b
->is_hs_dir
== 0);
3148 /* Check routerset A unsets all flags on router B when Strict is set */
3149 reset_options(mock_options
, &mock_get_options_calls
);
3150 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3152 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3153 mock_options
->TestingDirAuthVoteExitIsStrict
= 1;
3154 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3155 mock_options
->TestingDirAuthVoteGuardIsStrict
= 1;
3156 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3157 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 1;
3160 rs_b
->is_possible_guard
= 1;
3161 rs_b
->is_hs_dir
= 1;
3163 dirserv_set_routerstatus_testing(rs_b
);
3164 tt_assert(mock_get_options_calls
== 1);
3166 tt_assert(rs_b
->is_exit
== 0);
3167 tt_assert(rs_b
->is_possible_guard
== 0);
3168 tt_assert(rs_b
->is_hs_dir
== 0);
3170 /* Check routerset A doesn't modify flags on router B without Strict set */
3171 reset_options(mock_options
, &mock_get_options_calls
);
3172 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3174 mock_options
->TestingDirAuthVoteExit
= routerset_a
;
3175 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3176 mock_options
->TestingDirAuthVoteGuard
= routerset_a
;
3177 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3178 mock_options
->TestingDirAuthVoteHSDir
= routerset_a
;
3179 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3182 rs_b
->is_possible_guard
= 1;
3183 rs_b
->is_hs_dir
= 1;
3185 dirserv_set_routerstatus_testing(rs_b
);
3186 tt_assert(mock_get_options_calls
== 1);
3188 tt_assert(rs_b
->is_exit
== 1);
3189 tt_assert(rs_b
->is_possible_guard
== 1);
3190 tt_assert(rs_b
->is_hs_dir
== 1);
3192 /* Check the empty routerset zeroes all flags
3193 * on routers A & B with Strict set */
3194 reset_options(mock_options
, &mock_get_options_calls
);
3195 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3197 mock_options
->TestingDirAuthVoteExit
= routerset_none
;
3198 mock_options
->TestingDirAuthVoteExitIsStrict
= 1;
3199 mock_options
->TestingDirAuthVoteGuard
= routerset_none
;
3200 mock_options
->TestingDirAuthVoteGuardIsStrict
= 1;
3201 mock_options
->TestingDirAuthVoteHSDir
= routerset_none
;
3202 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 1;
3205 rs_b
->is_possible_guard
= 1;
3206 rs_b
->is_hs_dir
= 1;
3208 dirserv_set_routerstatus_testing(rs_b
);
3209 tt_assert(mock_get_options_calls
== 1);
3211 tt_assert(rs_b
->is_exit
== 0);
3212 tt_assert(rs_b
->is_possible_guard
== 0);
3213 tt_assert(rs_b
->is_hs_dir
== 0);
3215 /* Check the empty routerset doesn't modify any flags
3216 * on A or B without Strict set */
3217 reset_options(mock_options
, &mock_get_options_calls
);
3218 reset_routerstatus(rs_a
, ROUTER_A_ID_STR
, ROUTER_A_IPV4
);
3219 reset_routerstatus(rs_b
, ROUTER_B_ID_STR
, ROUTER_B_IPV4
);
3221 mock_options
->TestingDirAuthVoteExit
= routerset_none
;
3222 mock_options
->TestingDirAuthVoteExitIsStrict
= 0;
3223 mock_options
->TestingDirAuthVoteGuard
= routerset_none
;
3224 mock_options
->TestingDirAuthVoteGuardIsStrict
= 0;
3225 mock_options
->TestingDirAuthVoteHSDir
= routerset_none
;
3226 mock_options
->TestingDirAuthVoteHSDirIsStrict
= 0;
3229 rs_b
->is_possible_guard
= 1;
3230 rs_b
->is_hs_dir
= 1;
3232 dirserv_set_routerstatus_testing(rs_a
);
3233 tt_assert(mock_get_options_calls
== 1);
3234 dirserv_set_routerstatus_testing(rs_b
);
3235 tt_assert(mock_get_options_calls
== 2);
3237 tt_assert(rs_a
->is_exit
== 0);
3238 tt_assert(rs_a
->is_possible_guard
== 0);
3239 tt_assert(rs_a
->is_hs_dir
== 0);
3240 tt_assert(rs_b
->is_exit
== 1);
3241 tt_assert(rs_b
->is_possible_guard
== 1);
3242 tt_assert(rs_b
->is_hs_dir
== 1);
3246 mock_options
= NULL
;
3248 UNMOCK(get_options
);
3250 routerset_free(routerset_all
);
3251 routerset_free(routerset_a
);
3252 routerset_free(routerset_none
);
3259 test_dir_http_handling(void *args
)
3264 /* Parse http url tests: */
3266 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
3267 "Host: example.com\r\n"
3268 "User-Agent: Mozilla/5.0 (Windows;"
3269 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3271 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3274 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url
),OP_EQ
, 0);
3275 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3278 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url
),
3280 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3283 /* Should prepend '/tor/' to url if required */
3284 tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
3285 "Host: example.com\r\n"
3286 "User-Agent: Mozilla/5.0 (Windows;"
3287 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3289 tt_str_op(url
,OP_EQ
, "/tor/a/b/c.txt");
3292 /* Bad headers -- no HTTP/1.x*/
3293 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3294 "Host: example.com\r\n"
3295 "User-Agent: Mozilla/5.0 (Windows;"
3296 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3301 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3302 "Host: example.com\r\n"
3303 "User-Agent: Mozilla/5.0 (Windows;"
3304 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3308 tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url
),OP_EQ
, -1);
3311 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url
),OP_EQ
, -1);
3314 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url
),
3318 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url
),OP_EQ
, -1);
3321 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url
),OP_EQ
, -1);
3329 test_dir_purpose_needs_anonymity(void *arg
)
3332 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE
));
3333 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_GENERAL
));
3334 tt_int_op(0, ==, purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC
,
3335 ROUTER_PURPOSE_GENERAL
));
3340 test_dir_fetch_type(void *arg
)
3343 tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC
, ROUTER_PURPOSE_GENERAL
,
3344 NULL
) == MICRODESC_DIRINFO
);
3345 tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC
, ROUTER_PURPOSE_BRIDGE
,
3346 NULL
) == BRIDGE_DIRINFO
);
3347 tt_assert(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS
, ROUTER_PURPOSE_GENERAL
,
3348 "microdesc") == (V3_DIRINFO
| MICRODESC_DIRINFO
));
3353 test_dir_packages(void *arg
)
3355 smartlist_t
*votes
= smartlist_new();
3360 tt_int_op(0, ==, validate_recommended_package_line(s));
3362 tt_int_op(1, ==, validate_recommended_package_line(s));
3363 GOOD("tor 0.2.6.3-alpha "
3364 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3365 "sha256=sssdlkfjdsklfjdskfljasdklfj");
3366 GOOD("tor 0.2.6.3-alpha "
3367 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3368 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
3369 BAD("tor 0.2.6.3-alpha "
3370 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3371 "sha256=sssdlkfjdsklfjdskfljasdklfj=");
3372 BAD("tor 0.2.6.3-alpha "
3373 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3374 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
3375 BAD("tor 0.2.6.3-alpha "
3376 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
3377 BAD("tor 0.2.6.3-alpha "
3378 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
3379 BAD("tor 0.2.6.3-alpha ");
3380 BAD("tor 0.2.6.3-alpha");
3384 BAD("=foobar sha256="
3385 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3387 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3389 BAD("sha512= sha256="
3390 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3392 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3393 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3394 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3395 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3396 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3397 smartlist_add(votes
, tor_malloc_zero(sizeof(networkstatus_t
)));
3398 SMARTLIST_FOREACH(votes
, networkstatus_t
*, ns
,
3399 ns
->package_lines
= smartlist_new());
3402 smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
3405 /* Only one vote for this one. */
3406 ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
3408 /* Only two matching entries for this one, but 3 voters */
3409 ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
3410 ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3411 ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3413 /* Only two matching entries for this one, but at least 4 voters */
3414 ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
3415 ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3416 ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3417 ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
3419 /* This one has only invalid votes. */
3420 ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
3421 ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
3422 ADD(2, "haffenreffer 1.2 ");
3423 ADD(3, "haffenreffer ");
3424 ADD(4, "haffenreffer");
3426 /* Three matching votes for this; it should actually go in! */
3427 ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3428 ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3429 ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3430 ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
3431 ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
3433 /* Three votes for A, three votes for B */
3434 ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3435 ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3436 ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3437 ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3438 ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3439 ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3441 /* Three votes for A, two votes for B */
3442 ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3443 ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3444 ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3445 ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3446 ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3448 /* Four votes for A, two for B. */
3449 ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3450 ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3451 ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3452 ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3453 ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3454 ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3456 /* Five votes for A ... all from the same guy. Three for B. */
3457 ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3458 ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3459 ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3460 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3461 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3462 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3463 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3464 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3466 /* As above but new replaces old: no two match. */
3467 ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3468 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3469 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
3470 ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3471 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3472 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3473 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3474 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3475 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3477 res
= compute_consensus_package_lines(votes
);
3480 "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
3481 "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
3482 "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
3483 "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
3484 "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
3491 SMARTLIST_FOREACH(votes
, networkstatus_t
*, ns
,
3492 { smartlist_free(ns
->package_lines
); tor_free(ns
); });
3493 smartlist_free(votes
);
3497 #define DIR_LEGACY(name) \
3498 { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
3500 #define DIR(name,flags) \
3501 { #name, test_dir_##name, (flags), NULL, NULL }
3503 struct testcase_t dir_tests
[] = {
3504 DIR_LEGACY(nicknames
),
3505 DIR_LEGACY(formats
),
3506 DIR(routerinfo_parsing
, 0),
3507 DIR(extrainfo_parsing
, 0),
3508 DIR(parse_router_list
, TT_FORK
),
3509 DIR(load_routers
, TT_FORK
),
3510 DIR(load_extrainfo
, TT_FORK
),
3511 DIR_LEGACY(versions
),
3512 DIR_LEGACY(fp_pairs
),
3514 DIR_LEGACY(measured_bw_kb
),
3515 DIR_LEGACY(measured_bw_kb_cache
),
3516 DIR_LEGACY(param_voting
),
3517 DIR_LEGACY(v3_networkstatus
),
3518 DIR(random_weighted
, 0),
3520 DIR_LEGACY(clip_unmeasured_bw_kb
),
3521 DIR_LEGACY(clip_unmeasured_bw_kb_alt
),
3522 DIR(fmt_control_ns
, 0),
3523 DIR(dirserv_set_routerstatus_testing
, 0),
3524 DIR(http_handling
, 0),
3525 DIR(purpose_needs_anonymity
, 0),