Merge remote-tracking branch 'teor/fix18809-warnings' into maint-0.2.8
[tor.git] / src / test / test_dir.c
blob26b0e72a9a34873b8c80279a62ec58acb662d34c
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
7 #include <math.h>
9 #define CONFIG_PRIVATE
10 #define DIRSERV_PRIVATE
11 #define DIRVOTE_PRIVATE
12 #define ROUTER_PRIVATE
13 #define ROUTERLIST_PRIVATE
14 #define HIBERNATE_PRIVATE
15 #define NETWORKSTATUS_PRIVATE
16 #define RELAY_PRIVATE
18 #include "or.h"
19 #include "confparse.h"
20 #include "config.h"
21 #include "crypto_ed25519.h"
22 #include "directory.h"
23 #include "dirserv.h"
24 #include "dirvote.h"
25 #include "hibernate.h"
26 #include "memarea.h"
27 #include "networkstatus.h"
28 #include "router.h"
29 #include "routerkeys.h"
30 #include "routerlist.h"
31 #include "routerparse.h"
32 #include "routerset.h"
33 #include "test.h"
34 #include "test_dir_common.h"
35 #include "torcert.h"
36 #include "relay.h"
38 #define NS_MODULE dir
40 static void
41 test_dir_nicknames(void *arg)
43 (void)arg;
44 tt_assert( is_legal_nickname("a"));
45 tt_assert(!is_legal_nickname(""));
46 tt_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
47 tt_assert(!is_legal_nickname("hyphen-")); /* bad char */
48 tt_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
49 tt_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
50 /* valid */
51 tt_assert( is_legal_nickname_or_hexdigest(
52 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
53 tt_assert( is_legal_nickname_or_hexdigest(
54 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
55 tt_assert( is_legal_nickname_or_hexdigest(
56 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
57 /* too short */
58 tt_assert(!is_legal_nickname_or_hexdigest(
59 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
60 /* illegal char */
61 tt_assert(!is_legal_nickname_or_hexdigest(
62 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
63 /* hex part too long */
64 tt_assert(!is_legal_nickname_or_hexdigest(
65 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
66 tt_assert(!is_legal_nickname_or_hexdigest(
67 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
68 /* Bad nickname */
69 tt_assert(!is_legal_nickname_or_hexdigest(
70 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
71 tt_assert(!is_legal_nickname_or_hexdigest(
72 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
73 tt_assert(!is_legal_nickname_or_hexdigest(
74 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
75 tt_assert(!is_legal_nickname_or_hexdigest(
76 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
77 "abcdefghijklmnoppqrst"));
78 /* Bad extra char. */
79 tt_assert(!is_legal_nickname_or_hexdigest(
80 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
81 tt_assert(is_legal_nickname_or_hexdigest("xyzzy"));
82 tt_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
83 tt_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
84 done:
88 static smartlist_t *mocked_configured_ports = NULL;
90 /** Returns mocked_configured_ports */
91 static const smartlist_t *
92 mock_get_configured_ports(void)
94 return mocked_configured_ports;
97 /** Run unit tests for router descriptor generation logic. */
98 static void
99 test_dir_formats(void *arg)
101 char *buf = NULL;
102 char buf2[8192];
103 char platform[256];
104 char fingerprint[FINGERPRINT_LEN+1];
105 char *pk1_str = NULL, *pk2_str = NULL, *cp;
106 size_t pk1_str_len, pk2_str_len;
107 routerinfo_t *r1=NULL, *r2=NULL;
108 crypto_pk_t *pk1 = NULL, *pk2 = NULL;
109 routerinfo_t *rp1 = NULL, *rp2 = NULL;
110 addr_policy_t *ex1, *ex2;
111 routerlist_t *dir1 = NULL, *dir2 = NULL;
112 uint8_t *rsa_cc = NULL;
113 or_options_t *options = get_options_mutable();
114 const addr_policy_t *p;
115 time_t now = time(NULL);
116 port_cfg_t orport, dirport;
118 (void)arg;
119 pk1 = pk_generate(0);
120 pk2 = pk_generate(1);
122 tt_assert(pk1 && pk2);
124 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
126 get_platform_str(platform, sizeof(platform));
127 r1 = tor_malloc_zero(sizeof(routerinfo_t));
128 r1->addr = 0xc0a80001u; /* 192.168.0.1 */
129 r1->cache_info.published_on = 0;
130 r1->or_port = 9000;
131 r1->dir_port = 9003;
132 r1->supports_tunnelled_dir_requests = 1;
133 tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
134 r1->ipv6_orport = 9999;
135 r1->onion_pkey = crypto_pk_dup_key(pk1);
136 r1->identity_pkey = crypto_pk_dup_key(pk2);
137 r1->bandwidthrate = 1000;
138 r1->bandwidthburst = 5000;
139 r1->bandwidthcapacity = 10000;
140 r1->exit_policy = NULL;
141 r1->nickname = tor_strdup("Magri");
142 r1->platform = tor_strdup(platform);
144 ex1 = tor_malloc_zero(sizeof(addr_policy_t));
145 ex2 = tor_malloc_zero(sizeof(addr_policy_t));
146 ex1->policy_type = ADDR_POLICY_ACCEPT;
147 tor_addr_from_ipv4h(&ex1->addr, 0);
148 ex1->maskbits = 0;
149 ex1->prt_min = ex1->prt_max = 80;
150 ex2->policy_type = ADDR_POLICY_REJECT;
151 tor_addr_from_ipv4h(&ex2->addr, 18<<24);
152 ex2->maskbits = 8;
153 ex2->prt_min = ex2->prt_max = 24;
154 r2 = tor_malloc_zero(sizeof(routerinfo_t));
155 r2->addr = 0x0a030201u; /* 10.3.2.1 */
156 ed25519_keypair_t kp1, kp2;
157 ed25519_secret_key_from_seed(&kp1.seckey,
158 (const uint8_t*)"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
159 ed25519_public_key_generate(&kp1.pubkey, &kp1.seckey);
160 ed25519_secret_key_from_seed(&kp2.seckey,
161 (const uint8_t*)"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
162 ed25519_public_key_generate(&kp2.pubkey, &kp2.seckey);
163 r2->cache_info.signing_key_cert = tor_cert_create(&kp1,
164 CERT_TYPE_ID_SIGNING,
165 &kp2.pubkey,
166 now, 86400,
167 CERT_FLAG_INCLUDE_SIGNING_KEY);
168 char cert_buf[256];
169 base64_encode(cert_buf, sizeof(cert_buf),
170 (const char*)r2->cache_info.signing_key_cert->encoded,
171 r2->cache_info.signing_key_cert->encoded_len,
172 BASE64_ENCODE_MULTILINE);
173 r2->platform = tor_strdup(platform);
174 r2->cache_info.published_on = 5;
175 r2->or_port = 9005;
176 r2->dir_port = 0;
177 r2->supports_tunnelled_dir_requests = 1;
178 r2->onion_pkey = crypto_pk_dup_key(pk2);
179 curve25519_keypair_t r2_onion_keypair;
180 curve25519_keypair_generate(&r2_onion_keypair, 0);
181 r2->onion_curve25519_pkey = tor_memdup(&r2_onion_keypair.pubkey,
182 sizeof(curve25519_public_key_t));
183 r2->identity_pkey = crypto_pk_dup_key(pk1);
184 r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
185 r2->exit_policy = smartlist_new();
186 smartlist_add(r2->exit_policy, ex1);
187 smartlist_add(r2->exit_policy, ex2);
188 r2->nickname = tor_strdup("Fred");
190 tt_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
191 &pk1_str_len));
192 tt_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
193 &pk2_str_len));
195 /* XXXX025 router_dump_to_string should really take this from ri.*/
196 options->ContactInfo = tor_strdup("Magri White "
197 "<magri@elsewhere.example.com>");
198 /* Skip reachability checks for DirPort and tunnelled-dir-server */
199 options->AssumeReachable = 1;
201 /* Fake just enough of an ORPort and DirPort to get by */
202 MOCK(get_configured_ports, mock_get_configured_ports);
203 mocked_configured_ports = smartlist_new();
205 memset(&orport, 0, sizeof(orport));
206 orport.type = CONN_TYPE_OR_LISTENER;
207 orport.addr.family = AF_INET;
208 orport.port = 9000;
209 smartlist_add(mocked_configured_ports, &orport);
211 memset(&dirport, 0, sizeof(dirport));
212 dirport.type = CONN_TYPE_DIR_LISTENER;
213 dirport.addr.family = AF_INET;
214 dirport.port = 9003;
215 smartlist_add(mocked_configured_ports, &dirport);
217 buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
219 UNMOCK(get_configured_ports);
220 smartlist_free(mocked_configured_ports);
221 mocked_configured_ports = NULL;
223 tor_free(options->ContactInfo);
224 tt_assert(buf);
226 strlcpy(buf2, "router Magri 192.168.0.1 9000 0 9003\n"
227 "or-address [1:2:3:4::]:9999\n"
228 "platform Tor "VERSION" on ", sizeof(buf2));
229 strlcat(buf2, get_uname(), sizeof(buf2));
230 strlcat(buf2, "\n"
231 "protocols Link 1 2 Circuit 1\n"
232 "published 1970-01-01 00:00:00\n"
233 "fingerprint ", sizeof(buf2));
234 tt_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
235 strlcat(buf2, fingerprint, sizeof(buf2));
236 strlcat(buf2, "\nuptime 0\n"
237 /* XXX the "0" above is hard-coded, but even if we made it reflect
238 * uptime, that still wouldn't make it right, because the two
239 * descriptors might be made on different seconds... hm. */
240 "bandwidth 1000 5000 10000\n"
241 "onion-key\n", sizeof(buf2));
242 strlcat(buf2, pk1_str, sizeof(buf2));
243 strlcat(buf2, "signing-key\n", sizeof(buf2));
244 strlcat(buf2, pk2_str, sizeof(buf2));
245 strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
246 strlcat(buf2, "contact Magri White <magri@elsewhere.example.com>\n",
247 sizeof(buf2));
248 strlcat(buf2, "reject *:*\n", sizeof(buf2));
249 strlcat(buf2, "tunnelled-dir-server\nrouter-signature\n", sizeof(buf2));
250 buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
251 * twice */
253 tt_str_op(buf,OP_EQ, buf2);
254 tor_free(buf);
256 buf = router_dump_router_to_string(r1, pk2, NULL, NULL, NULL);
257 tt_assert(buf);
258 cp = buf;
259 rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
260 tt_assert(rp1);
261 tt_int_op(rp1->addr,OP_EQ, r1->addr);
262 tt_int_op(rp1->or_port,OP_EQ, r1->or_port);
263 tt_int_op(rp1->dir_port,OP_EQ, r1->dir_port);
264 tt_int_op(rp1->bandwidthrate,OP_EQ, r1->bandwidthrate);
265 tt_int_op(rp1->bandwidthburst,OP_EQ, r1->bandwidthburst);
266 tt_int_op(rp1->bandwidthcapacity,OP_EQ, r1->bandwidthcapacity);
267 tt_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
268 tt_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
269 tt_assert(rp1->supports_tunnelled_dir_requests);
270 //tt_assert(rp1->exit_policy == NULL);
271 tor_free(buf);
273 strlcpy(buf2,
274 "router Fred 10.3.2.1 9005 0 0\n"
275 "identity-ed25519\n"
276 "-----BEGIN ED25519 CERT-----\n", sizeof(buf2));
277 strlcat(buf2, cert_buf, sizeof(buf2));
278 strlcat(buf2, "-----END ED25519 CERT-----\n", sizeof(buf2));
279 strlcat(buf2, "master-key-ed25519 ", sizeof(buf2));
281 char k[ED25519_BASE64_LEN+1];
282 tt_assert(ed25519_public_to_base64(k,
283 &r2->cache_info.signing_key_cert->signing_key)
284 >= 0);
285 strlcat(buf2, k, sizeof(buf2));
286 strlcat(buf2, "\n", sizeof(buf2));
288 strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2));
289 strlcat(buf2, get_uname(), sizeof(buf2));
290 strlcat(buf2, "\n"
291 "protocols Link 1 2 Circuit 1\n"
292 "published 1970-01-01 00:00:05\n"
293 "fingerprint ", sizeof(buf2));
294 tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
295 strlcat(buf2, fingerprint, sizeof(buf2));
296 strlcat(buf2, "\nuptime 0\n"
297 "bandwidth 3000 3000 3000\n", sizeof(buf2));
298 strlcat(buf2, "onion-key\n", sizeof(buf2));
299 strlcat(buf2, pk2_str, sizeof(buf2));
300 strlcat(buf2, "signing-key\n", sizeof(buf2));
301 strlcat(buf2, pk1_str, sizeof(buf2));
302 int rsa_cc_len;
303 rsa_cc = make_tap_onion_key_crosscert(pk2,
304 &kp1.pubkey,
305 pk1,
306 &rsa_cc_len);
307 tt_assert(rsa_cc);
308 base64_encode(cert_buf, sizeof(cert_buf), (char*)rsa_cc, rsa_cc_len,
309 BASE64_ENCODE_MULTILINE);
310 strlcat(buf2, "onion-key-crosscert\n"
311 "-----BEGIN CROSSCERT-----\n", sizeof(buf2));
312 strlcat(buf2, cert_buf, sizeof(buf2));
313 strlcat(buf2, "-----END CROSSCERT-----\n", sizeof(buf2));
314 int ntor_cc_sign;
316 tor_cert_t *ntor_cc = NULL;
317 ntor_cc = make_ntor_onion_key_crosscert(&r2_onion_keypair,
318 &kp1.pubkey,
319 r2->cache_info.published_on,
320 MIN_ONION_KEY_LIFETIME,
321 &ntor_cc_sign);
322 tt_assert(ntor_cc);
323 base64_encode(cert_buf, sizeof(cert_buf),
324 (char*)ntor_cc->encoded, ntor_cc->encoded_len,
325 BASE64_ENCODE_MULTILINE);
326 tor_cert_free(ntor_cc);
328 tor_snprintf(buf2+strlen(buf2), sizeof(buf2)-strlen(buf2),
329 "ntor-onion-key-crosscert %d\n"
330 "-----BEGIN ED25519 CERT-----\n"
331 "%s"
332 "-----END ED25519 CERT-----\n", ntor_cc_sign, cert_buf);
334 strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
335 strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
336 base64_encode(cert_buf, sizeof(cert_buf),
337 (const char*)r2_onion_keypair.pubkey.public_key, 32,
338 BASE64_ENCODE_MULTILINE);
339 strlcat(buf2, cert_buf, sizeof(buf2));
340 strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
341 strlcat(buf2, "tunnelled-dir-server\n", sizeof(buf2));
342 strlcat(buf2, "router-sig-ed25519 ", sizeof(buf2));
344 /* Fake just enough of an ORPort to get by */
345 MOCK(get_configured_ports, mock_get_configured_ports);
346 mocked_configured_ports = smartlist_new();
348 memset(&orport, 0, sizeof(orport));
349 orport.type = CONN_TYPE_OR_LISTENER;
350 orport.addr.family = AF_INET;
351 orport.port = 9005;
352 smartlist_add(mocked_configured_ports, &orport);
354 buf = router_dump_router_to_string(r2, pk1, pk2, &r2_onion_keypair, &kp2);
355 tt_assert(buf);
356 buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
357 * twice */
359 tt_str_op(buf, OP_EQ, buf2);
360 tor_free(buf);
362 buf = router_dump_router_to_string(r2, pk1, NULL, NULL, NULL);
364 UNMOCK(get_configured_ports);
365 smartlist_free(mocked_configured_ports);
366 mocked_configured_ports = NULL;
368 /* Reset for later */
369 cp = buf;
370 rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
371 tt_assert(rp2);
372 tt_int_op(rp2->addr,OP_EQ, r2->addr);
373 tt_int_op(rp2->or_port,OP_EQ, r2->or_port);
374 tt_int_op(rp2->dir_port,OP_EQ, r2->dir_port);
375 tt_int_op(rp2->bandwidthrate,OP_EQ, r2->bandwidthrate);
376 tt_int_op(rp2->bandwidthburst,OP_EQ, r2->bandwidthburst);
377 tt_int_op(rp2->bandwidthcapacity,OP_EQ, r2->bandwidthcapacity);
378 tt_mem_op(rp2->onion_curve25519_pkey->public_key,OP_EQ,
379 r2->onion_curve25519_pkey->public_key,
380 CURVE25519_PUBKEY_LEN);
381 tt_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
382 tt_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
383 tt_assert(rp2->supports_tunnelled_dir_requests);
385 tt_int_op(smartlist_len(rp2->exit_policy),OP_EQ, 2);
387 p = smartlist_get(rp2->exit_policy, 0);
388 tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_ACCEPT);
389 tt_assert(tor_addr_is_null(&p->addr));
390 tt_int_op(p->maskbits,OP_EQ, 0);
391 tt_int_op(p->prt_min,OP_EQ, 80);
392 tt_int_op(p->prt_max,OP_EQ, 80);
394 p = smartlist_get(rp2->exit_policy, 1);
395 tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_REJECT);
396 tt_assert(tor_addr_eq(&p->addr, &ex2->addr));
397 tt_int_op(p->maskbits,OP_EQ, 8);
398 tt_int_op(p->prt_min,OP_EQ, 24);
399 tt_int_op(p->prt_max,OP_EQ, 24);
401 #if 0
402 /* Okay, now for the directories. */
404 fingerprint_list = smartlist_new();
405 crypto_pk_get_fingerprint(pk2, buf, 1);
406 add_fingerprint_to_dir(buf, fingerprint_list, 0);
407 crypto_pk_get_fingerprint(pk1, buf, 1);
408 add_fingerprint_to_dir(buf, fingerprint_list, 0);
411 #endif
412 dirserv_free_fingerprint_list();
414 done:
415 if (r1)
416 routerinfo_free(r1);
417 if (r2)
418 routerinfo_free(r2);
419 if (rp2)
420 routerinfo_free(rp2);
422 tor_free(rsa_cc);
423 tor_free(buf);
424 tor_free(pk1_str);
425 tor_free(pk2_str);
426 if (pk1) crypto_pk_free(pk1);
427 if (pk2) crypto_pk_free(pk2);
428 if (rp1) routerinfo_free(rp1);
429 tor_free(dir1); /* XXXX And more !*/
430 tor_free(dir2); /* And more !*/
433 #include "failing_routerdescs.inc"
435 static void
436 test_dir_routerinfo_parsing(void *arg)
438 (void) arg;
440 int again;
441 routerinfo_t *ri = NULL;
443 #define CHECK_OK(s) \
444 do { \
445 routerinfo_free(ri); \
446 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
447 tt_assert(ri); \
448 } while (0)
449 #define CHECK_FAIL(s, againval) \
450 do { \
451 routerinfo_free(ri); \
452 again = 999; \
453 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
454 tt_assert(ri == NULL); \
455 tt_int_op(again, OP_EQ, (againval)); \
456 } while (0)
458 CHECK_OK(EX_RI_MINIMAL);
459 CHECK_OK(EX_RI_MAXIMAL);
461 CHECK_OK(EX_RI_MINIMAL_ED);
463 /* good annotations prepended */
464 routerinfo_free(ri);
465 ri = router_parse_entry_from_string(EX_RI_MINIMAL, NULL, 0, 0,
466 "@purpose bridge\n", NULL);
467 tt_assert(ri != NULL);
468 tt_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
469 routerinfo_free(ri);
471 /* bad annotations prepended. */
472 ri = router_parse_entry_from_string(EX_RI_MINIMAL,
473 NULL, 0, 0, "@purpose\n", NULL);
474 tt_assert(ri == NULL);
476 /* bad annotations on router. */
477 ri = router_parse_entry_from_string("@purpose\nrouter x\n", NULL, 0, 1,
478 NULL, NULL);
479 tt_assert(ri == NULL);
481 /* unwanted annotations on router. */
482 ri = router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL, 0, 0,
483 NULL, NULL);
484 tt_assert(ri == NULL);
486 /* No signature. */
487 ri = router_parse_entry_from_string("router x\n", NULL, 0, 0,
488 NULL, NULL);
489 tt_assert(ri == NULL);
491 /* Not a router */
492 routerinfo_free(ri);
493 ri = router_parse_entry_from_string("hello\n", NULL, 0, 0, NULL, NULL);
494 tt_assert(ri == NULL);
496 CHECK_FAIL(EX_RI_BAD_SIG1, 1);
497 CHECK_FAIL(EX_RI_BAD_SIG2, 1);
498 CHECK_FAIL(EX_RI_BAD_TOKENS, 0);
499 CHECK_FAIL(EX_RI_BAD_PUBLISHED, 0);
500 CHECK_FAIL(EX_RI_NEG_BANDWIDTH, 0);
501 CHECK_FAIL(EX_RI_BAD_BANDWIDTH, 0);
502 CHECK_FAIL(EX_RI_BAD_BANDWIDTH2, 0);
503 CHECK_FAIL(EX_RI_BAD_ONIONKEY1, 0);
504 CHECK_FAIL(EX_RI_BAD_ONIONKEY2, 0);
505 CHECK_FAIL(EX_RI_BAD_PORTS, 0);
506 CHECK_FAIL(EX_RI_BAD_IP, 0);
507 CHECK_FAIL(EX_RI_BAD_DIRPORT, 0);
508 CHECK_FAIL(EX_RI_BAD_NAME2, 0);
509 CHECK_FAIL(EX_RI_BAD_UPTIME, 0);
511 CHECK_FAIL(EX_RI_BAD_BANDWIDTH3, 0);
512 CHECK_FAIL(EX_RI_BAD_NTOR_KEY, 0);
513 CHECK_FAIL(EX_RI_BAD_FINGERPRINT, 0);
514 CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT, 0);
515 CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6, 0);
516 CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY, 0);
517 CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY, 0);
518 CHECK_FAIL(EX_RI_BAD_FAMILY, 0);
519 CHECK_FAIL(EX_RI_ZERO_ORPORT, 0);
521 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT, 0);
522 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2, 0);
523 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN, 0);
524 CHECK_FAIL(EX_RI_ED_BAD_SIG1, 0);
525 CHECK_FAIL(EX_RI_ED_BAD_SIG2, 0);
526 CHECK_FAIL(EX_RI_ED_BAD_SIG3, 0);
527 CHECK_FAIL(EX_RI_ED_BAD_SIG4, 0);
528 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1, 0);
529 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3, 0);
530 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4, 0);
531 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5, 0);
532 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6, 0);
533 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7, 0);
534 CHECK_FAIL(EX_RI_ED_MISPLACED1, 0);
535 CHECK_FAIL(EX_RI_ED_MISPLACED2, 0);
536 CHECK_FAIL(EX_RI_ED_BAD_CERT1, 0);
537 CHECK_FAIL(EX_RI_ED_BAD_CERT2, 0);
538 CHECK_FAIL(EX_RI_ED_BAD_CERT3, 0);
540 /* This is allowed; we just ignore it. */
541 CHECK_OK(EX_RI_BAD_EI_DIGEST);
542 CHECK_OK(EX_RI_BAD_EI_DIGEST2);
544 #undef CHECK_FAIL
545 #undef CHECK_OK
546 done:
547 routerinfo_free(ri);
550 #include "example_extrainfo.inc"
552 static void
553 routerinfo_free_wrapper_(void *arg)
555 routerinfo_free(arg);
558 static void
559 test_dir_extrainfo_parsing(void *arg)
561 (void) arg;
563 #define CHECK_OK(s) \
564 do { \
565 extrainfo_free(ei); \
566 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
567 tt_assert(ei); \
568 } while (0)
569 #define CHECK_FAIL(s, againval) \
570 do { \
571 extrainfo_free(ei); \
572 again = 999; \
573 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
574 tt_assert(ei == NULL); \
575 tt_int_op(again, OP_EQ, (againval)); \
576 } while (0)
577 #define ADD(name) \
578 do { \
579 ri = tor_malloc_zero(sizeof(routerinfo_t)); \
580 crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
581 tt_assert(! crypto_pk_read_public_key_from_string(pk, \
582 name##_KEY, strlen(name##_KEY))); \
583 tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
584 digestmap_set((digestmap_t*)map, d, ri); \
585 ri = NULL; \
586 } while (0)
588 routerinfo_t *ri = NULL;
589 char d[20];
590 struct digest_ri_map_t *map = NULL;
591 extrainfo_t *ei = NULL;
592 int again;
594 CHECK_OK(EX_EI_MINIMAL);
595 tt_assert(ei->pending_sig);
596 CHECK_OK(EX_EI_MAXIMAL);
597 tt_assert(ei->pending_sig);
598 CHECK_OK(EX_EI_GOOD_ED_EI);
599 tt_assert(ei->pending_sig);
601 map = (struct digest_ri_map_t *)digestmap_new();
602 ADD(EX_EI_MINIMAL);
603 ADD(EX_EI_MAXIMAL);
604 ADD(EX_EI_GOOD_ED_EI);
605 ADD(EX_EI_BAD_FP);
606 ADD(EX_EI_BAD_NICKNAME);
607 ADD(EX_EI_BAD_TOKENS);
608 ADD(EX_EI_BAD_START);
609 ADD(EX_EI_BAD_PUBLISHED);
611 ADD(EX_EI_ED_MISSING_SIG);
612 ADD(EX_EI_ED_MISSING_CERT);
613 ADD(EX_EI_ED_BAD_CERT1);
614 ADD(EX_EI_ED_BAD_CERT2);
615 ADD(EX_EI_ED_BAD_SIG1);
616 ADD(EX_EI_ED_BAD_SIG2);
617 ADD(EX_EI_ED_MISPLACED_CERT);
618 ADD(EX_EI_ED_MISPLACED_SIG);
620 CHECK_OK(EX_EI_MINIMAL);
621 tt_assert(!ei->pending_sig);
622 CHECK_OK(EX_EI_MAXIMAL);
623 tt_assert(!ei->pending_sig);
624 CHECK_OK(EX_EI_GOOD_ED_EI);
625 tt_assert(!ei->pending_sig);
627 CHECK_FAIL(EX_EI_BAD_SIG1,1);
628 CHECK_FAIL(EX_EI_BAD_SIG2,1);
629 CHECK_FAIL(EX_EI_BAD_SIG3,1);
630 CHECK_FAIL(EX_EI_BAD_FP,0);
631 CHECK_FAIL(EX_EI_BAD_NICKNAME,0);
632 CHECK_FAIL(EX_EI_BAD_TOKENS,0);
633 CHECK_FAIL(EX_EI_BAD_START,0);
634 CHECK_FAIL(EX_EI_BAD_PUBLISHED,0);
636 CHECK_FAIL(EX_EI_ED_MISSING_SIG,0);
637 CHECK_FAIL(EX_EI_ED_MISSING_CERT,0);
638 CHECK_FAIL(EX_EI_ED_BAD_CERT1,0);
639 CHECK_FAIL(EX_EI_ED_BAD_CERT2,0);
640 CHECK_FAIL(EX_EI_ED_BAD_SIG1,0);
641 CHECK_FAIL(EX_EI_ED_BAD_SIG2,0);
642 CHECK_FAIL(EX_EI_ED_MISPLACED_CERT,0);
643 CHECK_FAIL(EX_EI_ED_MISPLACED_SIG,0);
645 #undef CHECK_OK
646 #undef CHECK_FAIL
648 done:
649 escaped(NULL);
650 extrainfo_free(ei);
651 routerinfo_free(ri);
652 digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
655 static void
656 test_dir_parse_router_list(void *arg)
658 (void) arg;
659 smartlist_t *invalid = smartlist_new();
660 smartlist_t *dest = smartlist_new();
661 smartlist_t *chunks = smartlist_new();
662 int dest_has_ri = 1;
663 char *list = NULL;
664 const char *cp;
665 digestmap_t *map = NULL;
666 char *mem_op_hex_tmp = NULL;
667 routerinfo_t *ri = NULL;
668 char d[DIGEST_LEN];
670 smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL)); // ri 0
671 smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS)); // bad ri 0
672 smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL)); // ei 0
673 smartlist_add(chunks, tor_strdup(EX_EI_BAD_SIG2)); // bad ei --
674 smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));// bad ei 0
675 smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG1)); // bad ri --
676 smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED)); // bad ei 1
677 smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL)); // ri 1
678 smartlist_add(chunks, tor_strdup(EX_RI_BAD_FAMILY)); // bad ri 1
679 smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL)); // ei 1
681 list = smartlist_join_strings(chunks, "", 0, NULL);
683 /* First, parse the routers. */
684 cp = list;
685 tt_int_op(0,OP_EQ,
686 router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
687 0, 0, NULL, invalid));
688 tt_int_op(2, OP_EQ, smartlist_len(dest));
689 tt_ptr_op(cp, OP_EQ, list + strlen(list));
691 routerinfo_t *r = smartlist_get(dest, 0);
692 tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
693 EX_RI_MINIMAL, strlen(EX_RI_MINIMAL));
694 r = smartlist_get(dest, 1);
695 tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
696 EX_RI_MAXIMAL, strlen(EX_RI_MAXIMAL));
698 tt_int_op(2, OP_EQ, smartlist_len(invalid));
699 test_memeq_hex(smartlist_get(invalid, 0),
700 "ab9eeaa95e7d45740185b4e519c76ead756277a9");
701 test_memeq_hex(smartlist_get(invalid, 1),
702 "9a651ee03b64325959e8f1b46f2b689b30750b4c");
704 /* Now tidy up */
705 SMARTLIST_FOREACH(dest, routerinfo_t *, ri, routerinfo_free(ri));
706 SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
707 smartlist_clear(dest);
708 smartlist_clear(invalid);
710 /* And check extrainfos. */
711 dest_has_ri = 0;
712 map = (digestmap_t*)router_get_routerlist()->identity_map;
713 ADD(EX_EI_MINIMAL);
714 ADD(EX_EI_MAXIMAL);
715 ADD(EX_EI_BAD_NICKNAME);
716 ADD(EX_EI_BAD_PUBLISHED);
717 cp = list;
718 tt_int_op(0,OP_EQ,
719 router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
720 1, 0, NULL, invalid));
721 tt_int_op(2, OP_EQ, smartlist_len(dest));
722 extrainfo_t *e = smartlist_get(dest, 0);
723 tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
724 EX_EI_MAXIMAL, strlen(EX_EI_MAXIMAL));
725 e = smartlist_get(dest, 1);
726 tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
727 EX_EI_MINIMAL, strlen(EX_EI_MINIMAL));
729 tt_int_op(2, OP_EQ, smartlist_len(invalid));
730 test_memeq_hex(smartlist_get(invalid, 0),
731 "d5df4aa62ee9ffc9543d41150c9864908e0390af");
732 test_memeq_hex(smartlist_get(invalid, 1),
733 "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
735 done:
736 tor_free(list);
737 if (dest_has_ri)
738 SMARTLIST_FOREACH(dest, routerinfo_t *, rt, routerinfo_free(rt));
739 else
740 SMARTLIST_FOREACH(dest, extrainfo_t *, ei, extrainfo_free(ei));
741 smartlist_free(dest);
742 SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
743 smartlist_free(invalid);
744 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
745 smartlist_free(chunks);
746 routerinfo_free(ri);
747 if (map) {
748 digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
749 router_get_routerlist()->identity_map =
750 (struct digest_ri_map_t*)digestmap_new();
752 tor_free(mem_op_hex_tmp);
754 #undef ADD
757 static download_status_t dls_minimal;
758 static download_status_t dls_maximal;
759 static download_status_t dls_bad_fingerprint;
760 static download_status_t dls_bad_sig2;
761 static download_status_t dls_bad_ports;
762 static download_status_t dls_bad_tokens;
764 static int mock_router_get_dl_status_unrecognized = 0;
765 static int mock_router_get_dl_status_calls = 0;
767 static download_status_t *
768 mock_router_get_dl_status(const char *d)
770 ++mock_router_get_dl_status_calls;
771 char hex[HEX_DIGEST_LEN+1];
772 base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
773 if (!strcmp(hex, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
774 return &dls_minimal;
775 } else if (!strcmp(hex, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
776 return &dls_maximal;
777 } else if (!strcmp(hex, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
778 return &dls_bad_fingerprint;
779 } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
780 return &dls_bad_sig2;
781 } else if (!strcmp(hex, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
782 return &dls_bad_ports;
783 } else if (!strcmp(hex, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
784 return &dls_bad_tokens;
785 } else {
786 ++mock_router_get_dl_status_unrecognized;
787 return NULL;
791 static void
792 test_dir_load_routers(void *arg)
794 (void) arg;
795 smartlist_t *chunks = smartlist_new();
796 smartlist_t *wanted = smartlist_new();
797 char buf[DIGEST_LEN];
798 char *mem_op_hex_tmp = NULL;
799 char *list = NULL;
801 #define ADD(str) \
802 do { \
803 tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
804 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
805 } while (0)
807 MOCK(router_get_dl_status_by_descriptor_digest, mock_router_get_dl_status);
809 update_approx_time(1412510400);
811 smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL));
812 smartlist_add(chunks, tor_strdup(EX_RI_BAD_FINGERPRINT));
813 smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG2));
814 smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL));
815 smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS));
816 smartlist_add(chunks, tor_strdup(EX_RI_BAD_TOKENS));
818 /* not ADDing MINIMIAL */
819 ADD(EX_RI_MAXIMAL);
820 ADD(EX_RI_BAD_FINGERPRINT);
821 ADD(EX_RI_BAD_SIG2);
822 /* Not ADDing BAD_PORTS */
823 ADD(EX_RI_BAD_TOKENS);
825 list = smartlist_join_strings(chunks, "", 0, NULL);
826 tt_int_op(1, OP_EQ,
827 router_load_routers_from_string(list, NULL, SAVED_IN_JOURNAL,
828 wanted, 1, NULL));
830 /* The "maximal" router was added. */
831 /* "minimal" was not. */
832 tt_int_op(smartlist_len(router_get_routerlist()->routers),OP_EQ,1);
833 routerinfo_t *r = smartlist_get(router_get_routerlist()->routers, 0);
834 test_memeq_hex(r->cache_info.signed_descriptor_digest,
835 "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
836 tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
837 tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
839 /* "Bad fingerprint" and "Bad tokens" should have gotten marked
840 * non-retriable. */
841 tt_want_int_op(mock_router_get_dl_status_calls, OP_EQ, 2);
842 tt_want_int_op(mock_router_get_dl_status_unrecognized, OP_EQ, 0);
843 tt_int_op(dls_bad_fingerprint.n_download_failures, OP_EQ, 255);
844 tt_int_op(dls_bad_tokens.n_download_failures, OP_EQ, 255);
846 /* bad_sig2 and bad ports" are retriable -- one since only the signature
847 * was bad, and one because we didn't ask for it. */
848 tt_int_op(dls_bad_sig2.n_download_failures, OP_EQ, 0);
849 tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
851 /* Wanted still contains "BAD_SIG2" */
852 tt_int_op(smartlist_len(wanted), OP_EQ, 1);
853 tt_str_op(smartlist_get(wanted, 0), OP_EQ,
854 "E0A3753CEFD54128EAB239F294954121DB23D2EF");
856 #undef ADD
858 done:
859 tor_free(mem_op_hex_tmp);
860 UNMOCK(router_get_dl_status_by_descriptor_digest);
861 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
862 smartlist_free(chunks);
863 SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
864 smartlist_free(wanted);
865 tor_free(list);
868 static int mock_get_by_ei_dd_calls = 0;
869 static int mock_get_by_ei_dd_unrecognized = 0;
871 static signed_descriptor_t sd_ei_minimal;
872 static signed_descriptor_t sd_ei_bad_nickname;
873 static signed_descriptor_t sd_ei_maximal;
874 static signed_descriptor_t sd_ei_bad_tokens;
875 static signed_descriptor_t sd_ei_bad_sig2;
877 static signed_descriptor_t *
878 mock_get_by_ei_desc_digest(const char *d)
881 ++mock_get_by_ei_dd_calls;
882 char hex[HEX_DIGEST_LEN+1];
883 base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
885 if (!strcmp(hex, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
886 return &sd_ei_minimal;
887 } else if (!strcmp(hex, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
888 return &sd_ei_maximal;
889 } else if (!strcmp(hex, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
890 return &sd_ei_bad_nickname;
891 } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
892 return &sd_ei_bad_sig2;
893 } else if (!strcmp(hex, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
894 return &sd_ei_bad_tokens;
895 } else {
896 ++mock_get_by_ei_dd_unrecognized;
897 return NULL;
901 static smartlist_t *mock_ei_insert_list = NULL;
902 static was_router_added_t
903 mock_ei_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
905 (void) rl;
906 (void) warn_if_incompatible;
907 smartlist_add(mock_ei_insert_list, ei);
908 return ROUTER_ADDED_SUCCESSFULLY;
911 static void
912 test_dir_load_extrainfo(void *arg)
914 (void) arg;
915 smartlist_t *chunks = smartlist_new();
916 smartlist_t *wanted = smartlist_new();
917 char buf[DIGEST_LEN];
918 char *mem_op_hex_tmp = NULL;
919 char *list = NULL;
921 #define ADD(str) \
922 do { \
923 tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
924 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
925 } while (0)
927 mock_ei_insert_list = smartlist_new();
928 MOCK(router_get_by_extrainfo_digest, mock_get_by_ei_desc_digest);
929 MOCK(extrainfo_insert, mock_ei_insert);
931 smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL));
932 smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));
933 smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL));
934 smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED));
935 smartlist_add(chunks, tor_strdup(EX_EI_BAD_TOKENS));
937 /* not ADDing MINIMIAL */
938 ADD(EX_EI_MAXIMAL);
939 ADD(EX_EI_BAD_NICKNAME);
940 /* Not ADDing BAD_PUBLISHED */
941 ADD(EX_EI_BAD_TOKENS);
942 ADD(EX_EI_BAD_SIG2);
944 list = smartlist_join_strings(chunks, "", 0, NULL);
945 router_load_extrainfo_from_string(list, NULL, SAVED_IN_JOURNAL, wanted, 1);
947 /* The "maximal" router was added. */
948 /* "minimal" was also added, even though we didn't ask for it, since
949 * that's what we do with extrainfos. */
950 tt_int_op(smartlist_len(mock_ei_insert_list),OP_EQ,2);
952 extrainfo_t *e = smartlist_get(mock_ei_insert_list, 0);
953 test_memeq_hex(e->cache_info.signed_descriptor_digest,
954 "11E0EDF526950739F7769810FCACAB8C882FAEEE");
956 e = smartlist_get(mock_ei_insert_list, 1);
957 test_memeq_hex(e->cache_info.signed_descriptor_digest,
958 "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
959 tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
960 tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
962 /* "Bad nickname" and "Bad tokens" should have gotten marked
963 * non-retriable. */
964 tt_want_int_op(mock_get_by_ei_dd_calls, OP_EQ, 2);
965 tt_want_int_op(mock_get_by_ei_dd_unrecognized, OP_EQ, 0);
966 tt_int_op(sd_ei_bad_nickname.ei_dl_status.n_download_failures, OP_EQ, 255);
967 tt_int_op(sd_ei_bad_tokens.ei_dl_status.n_download_failures, OP_EQ, 255);
969 /* bad_ports is retriable -- because we didn't ask for it. */
970 tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
972 /* Wanted still contains "BAD_SIG2" */
973 tt_int_op(smartlist_len(wanted), OP_EQ, 1);
974 tt_str_op(smartlist_get(wanted, 0), OP_EQ,
975 "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
977 #undef ADD
979 done:
980 tor_free(mem_op_hex_tmp);
981 UNMOCK(router_get_by_extrainfo_digest);
982 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
983 smartlist_free(chunks);
984 SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
985 smartlist_free(wanted);
986 tor_free(list);
989 static void
990 test_dir_versions(void *arg)
992 tor_version_t ver1;
994 /* Try out version parsing functionality */
995 (void)arg;
996 tt_int_op(0,OP_EQ, tor_version_parse("0.3.4pre2-cvs", &ver1));
997 tt_int_op(0,OP_EQ, ver1.major);
998 tt_int_op(3,OP_EQ, ver1.minor);
999 tt_int_op(4,OP_EQ, ver1.micro);
1000 tt_int_op(VER_PRE,OP_EQ, ver1.status);
1001 tt_int_op(2,OP_EQ, ver1.patchlevel);
1002 tt_int_op(0,OP_EQ, tor_version_parse("0.3.4rc1", &ver1));
1003 tt_int_op(0,OP_EQ, ver1.major);
1004 tt_int_op(3,OP_EQ, ver1.minor);
1005 tt_int_op(4,OP_EQ, ver1.micro);
1006 tt_int_op(VER_RC,OP_EQ, ver1.status);
1007 tt_int_op(1,OP_EQ, ver1.patchlevel);
1008 tt_int_op(0,OP_EQ, tor_version_parse("1.3.4", &ver1));
1009 tt_int_op(1,OP_EQ, ver1.major);
1010 tt_int_op(3,OP_EQ, ver1.minor);
1011 tt_int_op(4,OP_EQ, ver1.micro);
1012 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1013 tt_int_op(0,OP_EQ, ver1.patchlevel);
1014 tt_int_op(0,OP_EQ, tor_version_parse("1.3.4.999", &ver1));
1015 tt_int_op(1,OP_EQ, ver1.major);
1016 tt_int_op(3,OP_EQ, ver1.minor);
1017 tt_int_op(4,OP_EQ, ver1.micro);
1018 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1019 tt_int_op(999,OP_EQ, ver1.patchlevel);
1020 tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4-alpha", &ver1));
1021 tt_int_op(0,OP_EQ, ver1.major);
1022 tt_int_op(1,OP_EQ, ver1.minor);
1023 tt_int_op(2,OP_EQ, ver1.micro);
1024 tt_int_op(4,OP_EQ, ver1.patchlevel);
1025 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1026 tt_str_op("alpha",OP_EQ, ver1.status_tag);
1027 tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4", &ver1));
1028 tt_int_op(0,OP_EQ, ver1.major);
1029 tt_int_op(1,OP_EQ, ver1.minor);
1030 tt_int_op(2,OP_EQ, ver1.micro);
1031 tt_int_op(4,OP_EQ, ver1.patchlevel);
1032 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1033 tt_str_op("",OP_EQ, ver1.status_tag);
1035 tt_int_op(0, OP_EQ, tor_version_parse("10.1", &ver1));
1036 tt_int_op(10, OP_EQ, ver1.major);
1037 tt_int_op(1, OP_EQ, ver1.minor);
1038 tt_int_op(0, OP_EQ, ver1.micro);
1039 tt_int_op(0, OP_EQ, ver1.patchlevel);
1040 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1041 tt_str_op("", OP_EQ, ver1.status_tag);
1042 tt_int_op(0, OP_EQ, tor_version_parse("5.99.999", &ver1));
1043 tt_int_op(5, OP_EQ, ver1.major);
1044 tt_int_op(99, OP_EQ, ver1.minor);
1045 tt_int_op(999, OP_EQ, ver1.micro);
1046 tt_int_op(0, OP_EQ, ver1.patchlevel);
1047 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1048 tt_str_op("", OP_EQ, ver1.status_tag);
1049 tt_int_op(0, OP_EQ, tor_version_parse("10.1-alpha", &ver1));
1050 tt_int_op(10, OP_EQ, ver1.major);
1051 tt_int_op(1, OP_EQ, ver1.minor);
1052 tt_int_op(0, OP_EQ, ver1.micro);
1053 tt_int_op(0, OP_EQ, ver1.patchlevel);
1054 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1055 tt_str_op("alpha", OP_EQ, ver1.status_tag);
1056 tt_int_op(0, OP_EQ, tor_version_parse("2.1.700-alpha", &ver1));
1057 tt_int_op(2, OP_EQ, ver1.major);
1058 tt_int_op(1, OP_EQ, ver1.minor);
1059 tt_int_op(700, OP_EQ, ver1.micro);
1060 tt_int_op(0, OP_EQ, ver1.patchlevel);
1061 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1062 tt_str_op("alpha", OP_EQ, ver1.status_tag);
1063 tt_int_op(0, OP_EQ, tor_version_parse("1.6.8-alpha-dev", &ver1));
1064 tt_int_op(1, OP_EQ, ver1.major);
1065 tt_int_op(6, OP_EQ, ver1.minor);
1066 tt_int_op(8, OP_EQ, ver1.micro);
1067 tt_int_op(0, OP_EQ, ver1.patchlevel);
1068 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1069 tt_str_op("alpha-dev", OP_EQ, ver1.status_tag);
1071 #define tt_versionstatus_op(vs1, op, vs2) \
1072 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
1073 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
1074 #define test_v_i_o(val, ver, lst) \
1075 tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
1077 /* make sure tor_version_is_obsolete() works */
1078 test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
1079 test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
1080 test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
1081 test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
1082 test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
1083 test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
1084 test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
1085 test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
1086 test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
1087 test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
1088 test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
1089 /* Not on list, but newer than any in same series. */
1090 test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
1091 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1092 /* Series newer than any on list. */
1093 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");
1094 /* Series older than any on list. */
1095 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");
1096 /* Not on list, not newer than any on same series. */
1097 test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
1098 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1099 /* On list, not newer than any on same series. */
1100 test_v_i_o(VS_UNRECOMMENDED,
1101 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1102 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1103 tt_int_op(1,OP_EQ, tor_version_as_new_as(
1104 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1105 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
1106 "0.0.8rc2"));
1107 tt_int_op(0,OP_EQ, tor_version_as_new_as(
1108 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1109 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1111 /* Now try svn revisions. */
1112 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1113 "Tor 0.2.1.0-dev (r99)"));
1114 tt_int_op(1,OP_EQ, tor_version_as_new_as(
1115 "Tor 0.2.1.0-dev (r100) on Banana Jr",
1116 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
1117 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1118 "Tor 0.2.1.0-dev on Colossus"));
1119 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
1120 "Tor 0.2.1.0-dev (r100)"));
1121 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
1122 "Tor 0.2.1.0-dev (r100) on AM"));
1123 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev",
1124 "Tor 0.2.1.0-dev (r99)"));
1125 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.1",
1126 "Tor 0.2.1.0-dev (r99)"));
1128 /* Now try git revisions */
1129 tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
1130 tt_int_op(0,OP_EQ, ver1.major);
1131 tt_int_op(5,OP_EQ, ver1.minor);
1132 tt_int_op(6,OP_EQ, ver1.micro);
1133 tt_int_op(7,OP_EQ, ver1.patchlevel);
1134 tt_int_op(3,OP_EQ, ver1.git_tag_len);
1135 tt_mem_op(ver1.git_tag,OP_EQ, "\xff\x00\xff", 3);
1136 tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
1137 tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
1138 tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
1139 done:
1143 /** Run unit tests for directory fp_pair functions. */
1144 static void
1145 test_dir_fp_pairs(void *arg)
1147 smartlist_t *sl = smartlist_new();
1148 fp_pair_t *pair;
1150 (void)arg;
1151 dir_split_resource_into_fingerprint_pairs(
1152 /* Two pairs, out of order, with one duplicate. */
1153 "73656372657420646174612E0000000000FFFFFF-"
1154 "557365204145532d32353620696e73746561642e+"
1155 "73656372657420646174612E0000000000FFFFFF-"
1156 "557365204145532d32353620696e73746561642e+"
1157 "48657861646563696d616c2069736e277420736f-"
1158 "676f6f6420666f7220686964696e6720796f7572.z", sl);
1160 tt_int_op(smartlist_len(sl),OP_EQ, 2);
1161 pair = smartlist_get(sl, 0);
1162 tt_mem_op(pair->first,OP_EQ, "Hexadecimal isn't so", DIGEST_LEN);
1163 tt_mem_op(pair->second,OP_EQ, "good for hiding your", DIGEST_LEN);
1164 pair = smartlist_get(sl, 1);
1165 tt_mem_op(pair->first,OP_EQ, "secret data.\0\0\0\0\0\xff\xff\xff",
1166 DIGEST_LEN);
1167 tt_mem_op(pair->second,OP_EQ, "Use AES-256 instead.", DIGEST_LEN);
1169 done:
1170 SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
1171 smartlist_free(sl);
1174 static void
1175 test_dir_split_fps(void *testdata)
1177 smartlist_t *sl = smartlist_new();
1178 char *mem_op_hex_tmp = NULL;
1179 (void)testdata;
1181 /* Some example hex fingerprints and their base64 equivalents */
1182 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1183 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1184 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
1185 #define HEX256_1 \
1186 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
1187 #define HEX256_2 \
1188 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
1189 #define HEX256_3 \
1190 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
1191 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
1192 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
1193 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
1194 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
1196 /* no flags set */
1197 dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
1198 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1199 tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
1200 tt_str_op(smartlist_get(sl, 1), OP_EQ, "C");
1201 tt_str_op(smartlist_get(sl, 2), OP_EQ, "B");
1202 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1203 smartlist_clear(sl);
1205 /* uniq strings. */
1206 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
1207 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1208 tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
1209 tt_str_op(smartlist_get(sl, 1), OP_EQ, "B");
1210 tt_str_op(smartlist_get(sl, 2), OP_EQ, "C");
1211 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1212 smartlist_clear(sl);
1214 /* Decode hex. */
1215 dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
1216 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1217 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1218 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1219 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1220 smartlist_clear(sl);
1222 /* decode hex and drop weirdness. */
1223 dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
1224 sl, NULL, DSR_HEX);
1225 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1226 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1227 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1228 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1229 smartlist_clear(sl);
1231 /* Decode long hex */
1232 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
1233 sl, NULL, DSR_HEX|DSR_DIGEST256);
1234 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1235 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1236 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1237 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_3);
1238 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1239 smartlist_clear(sl);
1241 /* Decode hex and sort. */
1242 dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
1243 sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
1244 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1245 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX3);
1246 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1247 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX1);
1248 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1249 smartlist_clear(sl);
1251 /* Decode long hex and sort */
1252 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
1253 "+"HEX256_1,
1254 sl, NULL,
1255 DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
1256 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1257 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_3);
1258 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1259 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_1);
1260 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1261 smartlist_clear(sl);
1263 /* Decode base64 */
1264 dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
1265 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1266 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1267 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1268 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1269 smartlist_clear(sl);
1271 /* Decode long base64 */
1272 dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
1273 sl, NULL, DSR_BASE64|DSR_DIGEST256);
1274 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1275 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1276 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1277 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1278 smartlist_clear(sl);
1280 dir_split_resource_into_fingerprints(B64_256_1,
1281 sl, NULL, DSR_BASE64|DSR_DIGEST256);
1282 tt_int_op(smartlist_len(sl), OP_EQ, 1);
1283 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1284 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1285 smartlist_clear(sl);
1287 done:
1288 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1289 smartlist_free(sl);
1290 tor_free(mem_op_hex_tmp);
1293 static void
1294 test_dir_measured_bw_kb(void *arg)
1296 measured_bw_line_t mbwl;
1297 int i;
1298 const char *lines_pass[] = {
1299 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1300 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
1301 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1302 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
1303 "bw=1024 junk=007\n",
1304 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
1305 "bw=1024 junk=007\n",
1306 "end"
1308 const char *lines_fail[] = {
1309 /* Test possible python stupidity on input */
1310 "node_id=None bw=1024\n",
1311 "node_id=$None bw=1024\n",
1312 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
1313 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
1314 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
1315 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
1316 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
1317 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
1318 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
1319 /* Test incomplete writes due to race conditions, partial copies, etc */
1320 "node_i",
1321 "node_i\n",
1322 "node_id=",
1323 "node_id=\n",
1324 "node_id=$557365204145532d32353620696e73746561642e bw=",
1325 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
1326 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
1327 "node_id=$557365204145532d32353620696e7374",
1328 "node_id=$557365204145532d32353620696e7374\n",
1330 "\n",
1331 " \n ",
1332 " \n\n",
1333 /* Test assorted noise */
1334 " node_id= ",
1335 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
1336 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
1337 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
1338 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
1339 "end"
1342 (void)arg;
1343 for (i = 0; strcmp(lines_fail[i], "end"); i++) {
1344 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
1345 tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
1348 for (i = 0; strcmp(lines_pass[i], "end"); i++) {
1349 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
1350 tt_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
1351 tt_assert(mbwl.bw_kb == 1024);
1352 tt_assert(strcmp(mbwl.node_hex,
1353 "557365204145532d32353620696e73746561642e") == 0);
1356 done:
1357 return;
1360 #define MBWC_INIT_TIME 1000
1362 /** Do the measured bandwidth cache unit test */
1363 static void
1364 test_dir_measured_bw_kb_cache(void *arg)
1366 /* Initial fake time_t for testing */
1367 time_t curr = MBWC_INIT_TIME;
1368 /* Some measured_bw_line_ts */
1369 measured_bw_line_t mbwl[3];
1370 /* For receiving output on cache queries */
1371 long bw;
1372 time_t as_of;
1374 /* First, clear the cache and assert that it's empty */
1375 (void)arg;
1376 dirserv_clear_measured_bw_cache();
1377 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1379 * Set up test mbwls; none of the dirserv_cache_*() functions care about
1380 * the node_hex field.
1382 memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
1383 mbwl[0].bw_kb = 20;
1384 memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
1385 mbwl[1].bw_kb = 40;
1386 memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
1387 mbwl[2].bw_kb = 80;
1388 /* Try caching something */
1389 dirserv_cache_measured_bw(&(mbwl[0]), curr);
1390 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1391 /* Okay, let's see if we can retrieve it */
1392 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, &as_of));
1393 tt_int_op(bw,OP_EQ, 20);
1394 tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
1395 /* Try retrieving it without some outputs */
1396 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL, NULL));
1397 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, NULL));
1398 tt_int_op(bw,OP_EQ, 20);
1399 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL,&as_of));
1400 tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
1401 /* Now expire it */
1402 curr += MAX_MEASUREMENT_AGE + 1;
1403 dirserv_expire_measured_bw_cache(curr);
1404 /* Check that the cache is empty */
1405 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1406 /* Check that we can't retrieve it */
1407 tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl[0].node_id, NULL,NULL));
1408 /* Try caching a few things now */
1409 dirserv_cache_measured_bw(&(mbwl[0]), curr);
1410 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1411 curr += MAX_MEASUREMENT_AGE / 4;
1412 dirserv_cache_measured_bw(&(mbwl[1]), curr);
1413 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
1414 curr += MAX_MEASUREMENT_AGE / 4;
1415 dirserv_cache_measured_bw(&(mbwl[2]), curr);
1416 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
1417 curr += MAX_MEASUREMENT_AGE / 4 + 1;
1418 /* Do an expire that's too soon to get any of them */
1419 dirserv_expire_measured_bw_cache(curr);
1420 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
1421 /* Push the oldest one off the cliff */
1422 curr += MAX_MEASUREMENT_AGE / 4;
1423 dirserv_expire_measured_bw_cache(curr);
1424 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
1425 /* And another... */
1426 curr += MAX_MEASUREMENT_AGE / 4;
1427 dirserv_expire_measured_bw_cache(curr);
1428 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1429 /* This should empty it out again */
1430 curr += MAX_MEASUREMENT_AGE / 4;
1431 dirserv_expire_measured_bw_cache(curr);
1432 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1434 done:
1435 return;
1438 static void
1439 test_dir_param_voting(void *arg)
1441 networkstatus_t vote1, vote2, vote3, vote4;
1442 smartlist_t *votes = smartlist_new();
1443 char *res = NULL;
1445 /* dirvote_compute_params only looks at the net_params field of the votes,
1446 so that's all we need to set.
1448 (void)arg;
1449 memset(&vote1, 0, sizeof(vote1));
1450 memset(&vote2, 0, sizeof(vote2));
1451 memset(&vote3, 0, sizeof(vote3));
1452 memset(&vote4, 0, sizeof(vote4));
1453 vote1.net_params = smartlist_new();
1454 vote2.net_params = smartlist_new();
1455 vote3.net_params = smartlist_new();
1456 vote4.net_params = smartlist_new();
1457 smartlist_split_string(vote1.net_params,
1458 "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
1459 smartlist_split_string(vote2.net_params,
1460 "ab=27 cw=5 x-yz=88", NULL, 0, 0);
1461 smartlist_split_string(vote3.net_params,
1462 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
1463 smartlist_split_string(vote4.net_params,
1464 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
1465 tt_int_op(100,OP_EQ, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
1466 tt_int_op(222,OP_EQ, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
1467 tt_int_op(80,OP_EQ, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
1468 tt_int_op(-8,OP_EQ, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
1469 tt_int_op(0,OP_EQ, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
1471 smartlist_add(votes, &vote1);
1473 /* Do the first tests without adding all the other votes, for
1474 * networks without many dirauths. */
1476 res = dirvote_compute_params(votes, 12, 2);
1477 tt_str_op(res,OP_EQ, "");
1478 tor_free(res);
1480 res = dirvote_compute_params(votes, 12, 1);
1481 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-99");
1482 tor_free(res);
1484 smartlist_add(votes, &vote2);
1486 res = dirvote_compute_params(votes, 12, 2);
1487 tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
1488 tor_free(res);
1490 res = dirvote_compute_params(votes, 12, 3);
1491 tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
1492 tor_free(res);
1494 res = dirvote_compute_params(votes, 12, 6);
1495 tt_str_op(res,OP_EQ, "");
1496 tor_free(res);
1498 smartlist_add(votes, &vote3);
1500 res = dirvote_compute_params(votes, 12, 3);
1501 tt_str_op(res,OP_EQ, "ab=27 abcd=20 cw=50 x-yz=-9");
1502 tor_free(res);
1504 res = dirvote_compute_params(votes, 12, 5);
1505 tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
1506 tor_free(res);
1508 res = dirvote_compute_params(votes, 12, 9);
1509 tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
1510 tor_free(res);
1512 smartlist_add(votes, &vote4);
1514 res = dirvote_compute_params(votes, 12, 4);
1515 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1516 tor_free(res);
1518 res = dirvote_compute_params(votes, 12, 5);
1519 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1520 tor_free(res);
1522 /* Test that the special-cased "at least three dirauths voted for
1523 * this param" logic works as expected. */
1524 res = dirvote_compute_params(votes, 12, 6);
1525 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1526 tor_free(res);
1528 res = dirvote_compute_params(votes, 12, 10);
1529 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1530 tor_free(res);
1532 done:
1533 tor_free(res);
1534 SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
1535 SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
1536 SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
1537 SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
1538 smartlist_free(vote1.net_params);
1539 smartlist_free(vote2.net_params);
1540 smartlist_free(vote3.net_params);
1541 smartlist_free(vote4.net_params);
1542 smartlist_free(votes);
1544 return;
1547 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
1548 * same voting authority, and that they do in fact have all the same
1549 * information. */
1550 static void
1551 test_same_voter(networkstatus_voter_info_t *v1,
1552 networkstatus_voter_info_t *v2)
1554 tt_str_op(v1->nickname,OP_EQ, v2->nickname);
1555 tt_mem_op(v1->identity_digest,OP_EQ, v2->identity_digest, DIGEST_LEN);
1556 tt_str_op(v1->address,OP_EQ, v2->address);
1557 tt_int_op(v1->addr,OP_EQ, v2->addr);
1558 tt_int_op(v1->dir_port,OP_EQ, v2->dir_port);
1559 tt_int_op(v1->or_port,OP_EQ, v2->or_port);
1560 tt_str_op(v1->contact,OP_EQ, v2->contact);
1561 tt_mem_op(v1->vote_digest,OP_EQ, v2->vote_digest, DIGEST_LEN);
1562 done:
1566 /** Helper: get a detached signatures document for one or two
1567 * consensuses. */
1568 static char *
1569 get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
1571 char *r;
1572 smartlist_t *sl;
1573 tor_assert(ns && ns->flavor == FLAV_NS);
1574 sl = smartlist_new();
1575 smartlist_add(sl,ns);
1576 if (ns2)
1577 smartlist_add(sl,ns2);
1578 r = networkstatus_get_detached_signatures(sl);
1579 smartlist_free(sl);
1580 return r;
1583 /** Apply tweaks to the vote list for each voter */
1584 static int
1585 vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
1587 vote_routerstatus_t *vrs;
1588 const char *msg = NULL;
1590 tt_assert(v);
1591 (void)now;
1593 if (voter == 1) {
1594 measured_bw_line_t mbw;
1595 memset(mbw.node_id, 33, sizeof(mbw.node_id));
1596 mbw.bw_kb = 1024;
1597 tt_assert(measured_bw_line_apply(&mbw,
1598 v->routerstatus_list) == 1);
1599 } else if (voter == 2 || voter == 3) {
1600 /* Monkey around with the list a bit */
1601 vrs = smartlist_get(v->routerstatus_list, 2);
1602 smartlist_del_keeporder(v->routerstatus_list, 2);
1603 vote_routerstatus_free(vrs);
1604 vrs = smartlist_get(v->routerstatus_list, 0);
1605 vrs->status.is_fast = 1;
1607 if (voter == 3) {
1608 vrs = smartlist_get(v->routerstatus_list, 0);
1609 smartlist_del_keeporder(v->routerstatus_list, 0);
1610 vote_routerstatus_free(vrs);
1611 vrs = smartlist_get(v->routerstatus_list, 0);
1612 memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
1613 tt_assert(router_add_to_routerlist(
1614 dir_common_generate_ri_from_rs(vrs), &msg,0,0) >= 0);
1618 done:
1619 return 0;
1623 * Test a parsed vote_routerstatus_t for v3_networkstatus test
1625 static void
1626 test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
1628 routerstatus_t *rs;
1629 tor_addr_t addr_ipv6;
1631 tt_assert(vrs);
1632 rs = &(vrs->status);
1633 tt_assert(rs);
1635 /* Split out by digests to test */
1636 if (tor_memeq(rs->identity_digest,
1637 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1638 "\x3\x3\x3\x3",
1639 DIGEST_LEN) &&
1640 (voter == 1)) {
1641 /* Check the first routerstatus. */
1642 tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
1643 tt_int_op(rs->published_on,OP_EQ, now-1500);
1644 tt_str_op(rs->nickname,OP_EQ, "router2");
1645 tt_mem_op(rs->identity_digest,OP_EQ,
1646 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1647 "\x3\x3\x3\x3",
1648 DIGEST_LEN);
1649 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1650 tt_int_op(rs->addr,OP_EQ, 0x99008801);
1651 tt_int_op(rs->or_port,OP_EQ, 443);
1652 tt_int_op(rs->dir_port,OP_EQ, 8000);
1653 /* no flags except "running" (16) and "v2dir" (64) */
1654 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(80));
1655 } else if (tor_memeq(rs->identity_digest,
1656 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1657 "\x5\x5\x5\x5",
1658 DIGEST_LEN) &&
1659 (voter == 1 || voter == 2)) {
1660 tt_mem_op(rs->identity_digest,OP_EQ,
1661 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1662 "\x5\x5\x5\x5",
1663 DIGEST_LEN);
1665 if (voter == 1) {
1666 /* Check the second routerstatus. */
1667 tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
1668 tt_int_op(rs->published_on,OP_EQ, now-1000);
1669 tt_str_op(rs->nickname,OP_EQ, "router1");
1671 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1672 tt_int_op(rs->addr,OP_EQ, 0x99009901);
1673 tt_int_op(rs->or_port,OP_EQ, 443);
1674 tt_int_op(rs->dir_port,OP_EQ, 0);
1675 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1676 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1677 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
1678 if (voter == 1) {
1679 /* all except "authority" (1) */
1680 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(254));
1681 } else {
1682 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
1683 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(974));
1685 } else if (tor_memeq(rs->identity_digest,
1686 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
1687 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
1688 DIGEST_LEN) &&
1689 (voter == 1 || voter == 2)) {
1690 /* Check the measured bandwidth bits */
1691 tt_assert(vrs->has_measured_bw &&
1692 vrs->measured_bw_kb == 1024);
1693 } else {
1695 * Didn't expect this, but the old unit test only checked some of them,
1696 * so don't assert.
1698 /* tt_assert(0); */
1701 done:
1702 return;
1706 * Test a consensus for v3_networkstatus_test
1708 static void
1709 test_consensus_for_v3ns(networkstatus_t *con, time_t now)
1711 (void)now;
1713 tt_assert(con);
1714 tt_assert(!con->cert);
1715 tt_int_op(2,OP_EQ, smartlist_len(con->routerstatus_list));
1716 /* There should be two listed routers: one with identity 3, one with
1717 * identity 5. */
1719 done:
1720 return;
1724 * Test a router list entry for v3_networkstatus test
1726 static void
1727 test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
1729 tor_addr_t addr_ipv6;
1731 tt_assert(rs);
1733 /* There should be two listed routers: one with identity 3, one with
1734 * identity 5. */
1735 /* This one showed up in 2 digests. */
1736 if (tor_memeq(rs->identity_digest,
1737 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1738 "\x3\x3",
1739 DIGEST_LEN)) {
1740 tt_mem_op(rs->identity_digest,OP_EQ,
1741 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1742 DIGEST_LEN);
1743 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1744 tt_assert(!rs->is_authority);
1745 tt_assert(!rs->is_exit);
1746 tt_assert(!rs->is_fast);
1747 tt_assert(!rs->is_possible_guard);
1748 tt_assert(!rs->is_stable);
1749 /* (If it wasn't running it wouldn't be here) */
1750 tt_assert(rs->is_flagged_running);
1751 tt_assert(!rs->is_valid);
1752 tt_assert(!rs->is_named);
1753 tt_assert(rs->is_v2_dir);
1754 /* XXXX check version */
1755 } else if (tor_memeq(rs->identity_digest,
1756 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1757 "\x5\x5\x5\x5",
1758 DIGEST_LEN)) {
1759 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
1760 tt_mem_op(rs->identity_digest,OP_EQ,
1761 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1762 DIGEST_LEN);
1763 tt_str_op(rs->nickname,OP_EQ, "router1");
1764 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1765 tt_int_op(rs->published_on,OP_EQ, now-1000);
1766 tt_int_op(rs->addr,OP_EQ, 0x99009901);
1767 tt_int_op(rs->or_port,OP_EQ, 443);
1768 tt_int_op(rs->dir_port,OP_EQ, 0);
1769 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1770 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1771 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
1772 tt_assert(!rs->is_authority);
1773 tt_assert(rs->is_exit);
1774 tt_assert(rs->is_fast);
1775 tt_assert(rs->is_possible_guard);
1776 tt_assert(rs->is_stable);
1777 tt_assert(rs->is_flagged_running);
1778 tt_assert(rs->is_valid);
1779 tt_assert(rs->is_v2_dir);
1780 tt_assert(!rs->is_named);
1781 /* XXXX check version */
1782 } else {
1783 /* Weren't expecting this... */
1784 tt_assert(0);
1787 done:
1788 return;
1791 /** Run a unit tests for generating and parsing networkstatuses, with
1792 * the supply test fns. */
1793 static void
1794 test_a_networkstatus(
1795 vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
1796 int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
1797 void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
1798 void (*consensus_test)(networkstatus_t *con, time_t now),
1799 void (*rs_test)(routerstatus_t *rs, time_t now))
1801 authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
1802 crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
1803 crypto_pk_t *sign_skey_leg1=NULL;
1805 * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
1806 * returns non-zero, it changed net_params and we should skip the tests for
1807 * that later as they will fail.
1809 int params_tweaked = 0;
1811 time_t now = time(NULL);
1812 networkstatus_voter_info_t *voter;
1813 document_signature_t *sig;
1814 networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
1815 *con_md=NULL;
1816 vote_routerstatus_t *vrs;
1817 routerstatus_t *rs;
1818 int idx, n_rs, n_vrs;
1819 char *consensus_text=NULL, *cp=NULL;
1820 smartlist_t *votes = smartlist_new();
1822 /* For generating the two other consensuses. */
1823 char *detached_text1=NULL, *detached_text2=NULL;
1824 char *consensus_text2=NULL, *consensus_text3=NULL;
1825 char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
1826 char *consensus_text_md=NULL;
1827 networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
1828 ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
1830 tt_assert(vrs_gen);
1831 tt_assert(rs_test);
1832 tt_assert(vrs_test);
1834 tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
1835 &sign_skey_1, &sign_skey_2,
1836 &sign_skey_3));
1837 sign_skey_leg1 = pk_generate(4);
1839 tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
1840 &v1, &n_vrs, now, 1));
1841 tt_assert(v1);
1843 /* Make sure the parsed thing was right. */
1844 tt_int_op(v1->type,OP_EQ, NS_TYPE_VOTE);
1845 tt_int_op(v1->published,OP_EQ, vote->published);
1846 tt_int_op(v1->valid_after,OP_EQ, vote->valid_after);
1847 tt_int_op(v1->fresh_until,OP_EQ, vote->fresh_until);
1848 tt_int_op(v1->valid_until,OP_EQ, vote->valid_until);
1849 tt_int_op(v1->vote_seconds,OP_EQ, vote->vote_seconds);
1850 tt_int_op(v1->dist_seconds,OP_EQ, vote->dist_seconds);
1851 tt_str_op(v1->client_versions,OP_EQ, vote->client_versions);
1852 tt_str_op(v1->server_versions,OP_EQ, vote->server_versions);
1853 tt_assert(v1->voters && smartlist_len(v1->voters));
1854 voter = smartlist_get(v1->voters, 0);
1855 tt_str_op(voter->nickname,OP_EQ, "Voter1");
1856 tt_str_op(voter->address,OP_EQ, "1.2.3.4");
1857 tt_int_op(voter->addr,OP_EQ, 0x01020304);
1858 tt_int_op(voter->dir_port,OP_EQ, 80);
1859 tt_int_op(voter->or_port,OP_EQ, 9000);
1860 tt_str_op(voter->contact,OP_EQ, "voter@example.com");
1861 tt_assert(v1->cert);
1862 tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
1863 cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
1864 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
1865 tor_free(cp);
1866 tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
1867 networkstatus_vote_free(vote);
1868 vote = NULL;
1870 if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
1872 /* Check the routerstatuses. */
1873 for (idx = 0; idx < n_vrs; ++idx) {
1874 vrs = smartlist_get(v1->routerstatus_list, idx);
1875 tt_assert(vrs);
1876 vrs_test(vrs, 1, now);
1879 /* Generate second vote. It disagrees on some of the times,
1880 * and doesn't list versions, and knows some crazy flags.
1881 * Generate and parse v2. */
1882 tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
1883 &v2, &n_vrs, now, 1));
1884 tt_assert(v2);
1886 if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
1888 /* Check that flags come out right.*/
1889 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1890 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1891 "Running:Stable:V2Dir:Valid");
1892 tor_free(cp);
1894 /* Check the routerstatuses. */
1895 n_vrs = smartlist_len(v2->routerstatus_list);
1896 for (idx = 0; idx < n_vrs; ++idx) {
1897 vrs = smartlist_get(v2->routerstatus_list, idx);
1898 tt_assert(vrs);
1899 vrs_test(vrs, 2, now);
1901 networkstatus_vote_free(vote);
1902 vote = NULL;
1904 /* Generate the third vote with a legacy id. */
1905 tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
1906 &v3, &n_vrs, now, 1));
1907 tt_assert(v3);
1909 if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
1911 /* Compute a consensus as voter 3. */
1912 smartlist_add(votes, v3);
1913 smartlist_add(votes, v1);
1914 smartlist_add(votes, v2);
1915 consensus_text = networkstatus_compute_consensus(votes, 3,
1916 cert3->identity_key,
1917 sign_skey_3,
1918 "AAAAAAAAAAAAAAAAAAAA",
1919 sign_skey_leg1,
1920 FLAV_NS);
1921 tt_assert(consensus_text);
1922 con = networkstatus_parse_vote_from_string(consensus_text, NULL,
1923 NS_TYPE_CONSENSUS);
1924 tt_assert(con);
1925 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
1926 // v1_text, v2_text, v3_text);
1927 consensus_text_md = networkstatus_compute_consensus(votes, 3,
1928 cert3->identity_key,
1929 sign_skey_3,
1930 "AAAAAAAAAAAAAAAAAAAA",
1931 sign_skey_leg1,
1932 FLAV_MICRODESC);
1933 tt_assert(consensus_text_md);
1934 con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
1935 NS_TYPE_CONSENSUS);
1936 tt_assert(con_md);
1937 tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
1939 /* Check consensus contents. */
1940 tt_assert(con->type == NS_TYPE_CONSENSUS);
1941 tt_int_op(con->published,OP_EQ, 0); /* this field only appears in votes. */
1942 tt_int_op(con->valid_after,OP_EQ, now+1000);
1943 tt_int_op(con->fresh_until,OP_EQ, now+2003); /* median */
1944 tt_int_op(con->valid_until,OP_EQ, now+3000);
1945 tt_int_op(con->vote_seconds,OP_EQ, 100);
1946 tt_int_op(con->dist_seconds,OP_EQ, 250); /* median */
1947 tt_str_op(con->client_versions,OP_EQ, "0.1.2.14");
1948 tt_str_op(con->server_versions,OP_EQ, "0.1.2.15,0.1.2.16");
1949 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1950 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1951 "Running:Stable:V2Dir:Valid");
1952 tor_free(cp);
1953 if (!params_tweaked) {
1954 /* Skip this one if vote_tweaks() messed with the param lists */
1955 cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
1956 tt_str_op(cp,OP_EQ, "circuitwindow=80:foo=660");
1957 tor_free(cp);
1960 tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
1961 /* The voter id digests should be in this order. */
1962 tt_assert(memcmp(cert2->cache_info.identity_digest,
1963 cert1->cache_info.identity_digest,DIGEST_LEN)<0);
1964 tt_assert(memcmp(cert1->cache_info.identity_digest,
1965 cert3->cache_info.identity_digest,DIGEST_LEN)<0);
1966 test_same_voter(smartlist_get(con->voters, 1),
1967 smartlist_get(v2->voters, 0));
1968 test_same_voter(smartlist_get(con->voters, 2),
1969 smartlist_get(v1->voters, 0));
1970 test_same_voter(smartlist_get(con->voters, 3),
1971 smartlist_get(v3->voters, 0));
1973 consensus_test(con, now);
1975 /* Check the routerstatuses. */
1976 n_rs = smartlist_len(con->routerstatus_list);
1977 tt_assert(n_rs);
1978 for (idx = 0; idx < n_rs; ++idx) {
1979 rs = smartlist_get(con->routerstatus_list, idx);
1980 tt_assert(rs);
1981 rs_test(rs, now);
1984 n_rs = smartlist_len(con_md->routerstatus_list);
1985 tt_assert(n_rs);
1986 for (idx = 0; idx < n_rs; ++idx) {
1987 rs = smartlist_get(con_md->routerstatus_list, idx);
1988 tt_assert(rs);
1991 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
1992 * The second one hasn't signed. The fourth one has signed: validate it. */
1993 voter = smartlist_get(con->voters, 1);
1994 tt_int_op(smartlist_len(voter->sigs),OP_EQ, 0);
1996 voter = smartlist_get(con->voters, 3);
1997 tt_int_op(smartlist_len(voter->sigs),OP_EQ, 1);
1998 sig = smartlist_get(voter->sigs, 0);
1999 tt_assert(sig->signature);
2000 tt_assert(!sig->good_signature);
2001 tt_assert(!sig->bad_signature);
2003 tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
2004 tt_assert(sig->signature);
2005 tt_assert(sig->good_signature);
2006 tt_assert(!sig->bad_signature);
2009 const char *msg=NULL;
2010 /* Compute the other two signed consensuses. */
2011 smartlist_shuffle(votes);
2012 consensus_text2 = networkstatus_compute_consensus(votes, 3,
2013 cert2->identity_key,
2014 sign_skey_2, NULL,NULL,
2015 FLAV_NS);
2016 consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
2017 cert2->identity_key,
2018 sign_skey_2, NULL,NULL,
2019 FLAV_MICRODESC);
2020 smartlist_shuffle(votes);
2021 consensus_text3 = networkstatus_compute_consensus(votes, 3,
2022 cert1->identity_key,
2023 sign_skey_1, NULL,NULL,
2024 FLAV_NS);
2025 consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
2026 cert1->identity_key,
2027 sign_skey_1, NULL,NULL,
2028 FLAV_MICRODESC);
2029 tt_assert(consensus_text2);
2030 tt_assert(consensus_text3);
2031 tt_assert(consensus_text_md2);
2032 tt_assert(consensus_text_md3);
2033 con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
2034 NS_TYPE_CONSENSUS);
2035 con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
2036 NS_TYPE_CONSENSUS);
2037 con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
2038 NS_TYPE_CONSENSUS);
2039 con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
2040 NS_TYPE_CONSENSUS);
2041 tt_assert(con2);
2042 tt_assert(con3);
2043 tt_assert(con_md2);
2044 tt_assert(con_md3);
2046 /* All three should have the same digest. */
2047 tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(common_digests_t));
2048 tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(common_digests_t));
2050 tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests,
2051 sizeof(common_digests_t));
2052 tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests,
2053 sizeof(common_digests_t));
2055 /* Extract a detached signature from con3. */
2056 detached_text1 = get_detached_sigs(con3, con_md3);
2057 tt_assert(detached_text1);
2058 /* Try to parse it. */
2059 dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
2060 tt_assert(dsig1);
2062 /* Are parsed values as expected? */
2063 tt_int_op(dsig1->valid_after,OP_EQ, con3->valid_after);
2064 tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
2065 tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
2067 common_digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
2068 tt_assert(dsig_digests);
2069 tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
2070 con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
2071 dsig_digests = strmap_get(dsig1->digests, "microdesc");
2072 tt_assert(dsig_digests);
2073 tt_mem_op(dsig_digests->d[DIGEST_SHA256],OP_EQ,
2074 con_md3->digests.d[DIGEST_SHA256],
2075 DIGEST256_LEN);
2078 smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
2079 tt_assert(dsig_signatures);
2080 tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
2081 sig = smartlist_get(dsig_signatures, 0);
2082 tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
2083 DIGEST_LEN);
2084 tt_int_op(sig->alg,OP_EQ, DIGEST_SHA1);
2086 dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
2087 tt_assert(dsig_signatures);
2088 tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
2089 sig = smartlist_get(dsig_signatures, 0);
2090 tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
2091 DIGEST_LEN);
2092 tt_int_op(sig->alg,OP_EQ, DIGEST_SHA256);
2095 /* Try adding it to con2. */
2096 detached_text2 = get_detached_sigs(con2,con_md2);
2097 tt_int_op(1,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
2098 "test", LOG_INFO, &msg));
2099 tor_free(detached_text2);
2100 tt_int_op(1,OP_EQ,
2101 networkstatus_add_detached_signatures(con_md2, dsig1, "test",
2102 LOG_INFO, &msg));
2103 tor_free(detached_text2);
2104 detached_text2 = get_detached_sigs(con2,con_md2);
2105 //printf("\n<%s>\n", detached_text2);
2106 dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
2107 tt_assert(dsig2);
2109 printf("\n");
2110 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2111 char hd[64];
2112 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2113 printf("%s\n", hd);
2116 tt_int_op(2,OP_EQ,
2117 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
2118 tt_int_op(2,OP_EQ,
2119 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
2120 "microdesc")));
2122 /* Try adding to con2 twice; verify that nothing changes. */
2123 tt_int_op(0,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
2124 "test", LOG_INFO, &msg));
2126 /* Add to con. */
2127 tt_int_op(2,OP_EQ, networkstatus_add_detached_signatures(con, dsig2,
2128 "test", LOG_INFO, &msg));
2129 /* Check signatures */
2130 voter = smartlist_get(con->voters, 1);
2131 sig = smartlist_get(voter->sigs, 0);
2132 tt_assert(sig);
2133 tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
2134 voter = smartlist_get(con->voters, 2);
2135 sig = smartlist_get(voter->sigs, 0);
2136 tt_assert(sig);
2137 tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
2140 done:
2141 tor_free(cp);
2142 smartlist_free(votes);
2143 tor_free(consensus_text);
2144 tor_free(consensus_text_md);
2146 networkstatus_vote_free(vote);
2147 networkstatus_vote_free(v1);
2148 networkstatus_vote_free(v2);
2149 networkstatus_vote_free(v3);
2150 networkstatus_vote_free(con);
2151 networkstatus_vote_free(con_md);
2152 crypto_pk_free(sign_skey_1);
2153 crypto_pk_free(sign_skey_2);
2154 crypto_pk_free(sign_skey_3);
2155 crypto_pk_free(sign_skey_leg1);
2156 authority_cert_free(cert1);
2157 authority_cert_free(cert2);
2158 authority_cert_free(cert3);
2160 tor_free(consensus_text2);
2161 tor_free(consensus_text3);
2162 tor_free(consensus_text_md2);
2163 tor_free(consensus_text_md3);
2164 tor_free(detached_text1);
2165 tor_free(detached_text2);
2167 networkstatus_vote_free(con2);
2168 networkstatus_vote_free(con3);
2169 networkstatus_vote_free(con_md2);
2170 networkstatus_vote_free(con_md3);
2171 ns_detached_signatures_free(dsig1);
2172 ns_detached_signatures_free(dsig2);
2175 /** Run unit tests for generating and parsing V3 consensus networkstatus
2176 * documents. */
2177 static void
2178 test_dir_v3_networkstatus(void *arg)
2180 (void)arg;
2181 test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
2182 vote_tweaks_for_v3ns,
2183 test_vrs_for_v3ns,
2184 test_consensus_for_v3ns,
2185 test_routerstatus_for_v3ns);
2188 static void
2189 test_dir_scale_bw(void *testdata)
2191 double v[8] = { 2.0/3,
2192 7.0,
2193 1.0,
2194 3.0,
2195 1.0/5,
2196 1.0/7,
2197 12.0,
2198 24.0 };
2199 u64_dbl_t vals[8];
2200 uint64_t total;
2201 int i;
2203 (void) testdata;
2205 for (i=0; i<8; ++i)
2206 vals[i].dbl = v[i];
2208 scale_array_elements_to_u64(vals, 8, &total);
2210 tt_int_op((int)total, OP_EQ, 48);
2211 total = 0;
2212 for (i=0; i<8; ++i) {
2213 total += vals[i].u64;
2215 tt_assert(total >= (U64_LITERAL(1)<<60));
2216 tt_assert(total <= (U64_LITERAL(1)<<62));
2218 for (i=0; i<8; ++i) {
2219 /* vals[2].u64 is the scaled value of 1.0 */
2220 double ratio = ((double)vals[i].u64) / vals[2].u64;
2221 tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
2224 /* test handling of no entries */
2225 total = 1;
2226 scale_array_elements_to_u64(vals, 0, &total);
2227 tt_assert(total == 0);
2229 /* make sure we don't read the array when we have no entries
2230 * may require compiler flags to catch NULL dereferences */
2231 total = 1;
2232 scale_array_elements_to_u64(NULL, 0, &total);
2233 tt_assert(total == 0);
2235 scale_array_elements_to_u64(NULL, 0, NULL);
2237 /* test handling of zero totals */
2238 total = 1;
2239 vals[0].dbl = 0.0;
2240 scale_array_elements_to_u64(vals, 1, &total);
2241 tt_assert(total == 0);
2242 tt_assert(vals[0].u64 == 0);
2244 vals[0].dbl = 0.0;
2245 vals[1].dbl = 0.0;
2246 scale_array_elements_to_u64(vals, 2, NULL);
2247 tt_assert(vals[0].u64 == 0);
2248 tt_assert(vals[1].u64 == 0);
2250 done:
2254 static void
2255 test_dir_random_weighted(void *testdata)
2257 int histogram[10];
2258 uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
2259 u64_dbl_t inp[10];
2260 int i, choice;
2261 const int n = 50000;
2262 double max_sq_error;
2263 (void) testdata;
2265 /* Try a ten-element array with values from 0 through 10. The values are
2266 * in a scrambled order to make sure we don't depend on order. */
2267 memset(histogram,0,sizeof(histogram));
2268 for (i=0; i<10; ++i) {
2269 inp[i].u64 = vals[i];
2270 total += vals[i];
2272 tt_u64_op(total, OP_EQ, 45);
2273 for (i=0; i<n; ++i) {
2274 choice = choose_array_element_by_weight(inp, 10);
2275 tt_int_op(choice, OP_GE, 0);
2276 tt_int_op(choice, OP_LT, 10);
2277 histogram[choice]++;
2280 /* Now see if we chose things about frequently enough. */
2281 max_sq_error = 0;
2282 for (i=0; i<10; ++i) {
2283 int expected = (int)(n*vals[i]/total);
2284 double frac_diff = 0, sq;
2285 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
2286 if (expected)
2287 frac_diff = (histogram[i] - expected) / ((double)expected);
2288 else
2289 tt_int_op(histogram[i], OP_EQ, 0);
2291 sq = frac_diff * frac_diff;
2292 if (sq > max_sq_error)
2293 max_sq_error = sq;
2295 /* It should almost always be much much less than this. If you want to
2296 * figure out the odds, please feel free. */
2297 tt_double_op(max_sq_error, OP_LT, .05);
2299 /* Now try a singleton; do we choose it? */
2300 for (i = 0; i < 100; ++i) {
2301 choice = choose_array_element_by_weight(inp, 1);
2302 tt_int_op(choice, OP_EQ, 0);
2305 /* Now try an array of zeros. We should choose randomly. */
2306 memset(histogram,0,sizeof(histogram));
2307 for (i = 0; i < 5; ++i)
2308 inp[i].u64 = 0;
2309 for (i = 0; i < n; ++i) {
2310 choice = choose_array_element_by_weight(inp, 5);
2311 tt_int_op(choice, OP_GE, 0);
2312 tt_int_op(choice, OP_LT, 5);
2313 histogram[choice]++;
2315 /* Now see if we chose things about frequently enough. */
2316 max_sq_error = 0;
2317 for (i=0; i<5; ++i) {
2318 int expected = n/5;
2319 double frac_diff = 0, sq;
2320 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
2321 frac_diff = (histogram[i] - expected) / ((double)expected);
2322 sq = frac_diff * frac_diff;
2323 if (sq > max_sq_error)
2324 max_sq_error = sq;
2326 /* It should almost always be much much less than this. If you want to
2327 * figure out the odds, please feel free. */
2328 tt_double_op(max_sq_error, OP_LT, .05);
2329 done:
2333 /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
2335 static uint32_t alternate_clip_bw = 0;
2338 * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
2339 * v3_networkstatus ones.
2341 static vote_routerstatus_t *
2342 gen_routerstatus_for_umbw(int idx, time_t now)
2344 vote_routerstatus_t *vrs = NULL;
2345 routerstatus_t *rs;
2346 tor_addr_t addr_ipv6;
2347 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2348 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2350 switch (idx) {
2351 case 0:
2352 /* Generate the first routerstatus. */
2353 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2354 rs = &vrs->status;
2355 vrs->version = tor_strdup("0.1.2.14");
2356 rs->published_on = now-1500;
2357 strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
2358 memset(rs->identity_digest, 3, DIGEST_LEN);
2359 memset(rs->descriptor_digest, 78, DIGEST_LEN);
2360 rs->addr = 0x99008801;
2361 rs->or_port = 443;
2362 rs->dir_port = 8000;
2363 /* all flags but running cleared */
2364 rs->is_flagged_running = 1;
2366 * This one has measured bandwidth below the clip cutoff, and
2367 * so shouldn't be clipped; we'll have to test that it isn't
2368 * later.
2370 vrs->has_measured_bw = 1;
2371 rs->has_bandwidth = 1;
2372 vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
2373 break;
2374 case 1:
2375 /* Generate the second routerstatus. */
2376 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2377 rs = &vrs->status;
2378 vrs->version = tor_strdup("0.2.0.5");
2379 rs->published_on = now-1000;
2380 strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
2381 memset(rs->identity_digest, 5, DIGEST_LEN);
2382 memset(rs->descriptor_digest, 77, DIGEST_LEN);
2383 rs->addr = 0x99009901;
2384 rs->or_port = 443;
2385 rs->dir_port = 0;
2386 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2387 tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
2388 rs->ipv6_orport = 4711;
2389 rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
2390 rs->is_valid = rs->is_possible_guard = 1;
2392 * This one has measured bandwidth above the clip cutoff, and
2393 * so shouldn't be clipped; we'll have to test that it isn't
2394 * later.
2396 vrs->has_measured_bw = 1;
2397 rs->has_bandwidth = 1;
2398 vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
2399 break;
2400 case 2:
2401 /* Generate the third routerstatus. */
2402 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2403 rs = &vrs->status;
2404 vrs->version = tor_strdup("0.1.0.3");
2405 rs->published_on = now-1000;
2406 strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
2407 memset(rs->identity_digest, 0x33, DIGEST_LEN);
2408 memset(rs->descriptor_digest, 79, DIGEST_LEN);
2409 rs->addr = 0xAA009901;
2410 rs->or_port = 400;
2411 rs->dir_port = 9999;
2412 rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
2413 rs->is_flagged_running = rs->is_valid =
2414 rs->is_possible_guard = 1;
2416 * This one has unmeasured bandwidth above the clip cutoff, and
2417 * so should be clipped; we'll have to test that it isn't
2418 * later.
2420 vrs->has_measured_bw = 0;
2421 rs->has_bandwidth = 1;
2422 vrs->measured_bw_kb = 0;
2423 rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
2424 break;
2425 case 3:
2426 /* Generate a fourth routerstatus that is not running. */
2427 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2428 rs = &vrs->status;
2429 vrs->version = tor_strdup("0.1.6.3");
2430 rs->published_on = now-1000;
2431 strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
2432 memset(rs->identity_digest, 0x34, DIGEST_LEN);
2433 memset(rs->descriptor_digest, 47, DIGEST_LEN);
2434 rs->addr = 0xC0000203;
2435 rs->or_port = 500;
2436 rs->dir_port = 1999;
2437 /* all flags but running cleared */
2438 rs->is_flagged_running = 1;
2440 * This one has unmeasured bandwidth below the clip cutoff, and
2441 * so shouldn't be clipped; we'll have to test that it isn't
2442 * later.
2444 vrs->has_measured_bw = 0;
2445 rs->has_bandwidth = 1;
2446 vrs->measured_bw_kb = 0;
2447 rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
2448 break;
2449 case 4:
2450 /* No more for this test; return NULL */
2451 vrs = NULL;
2452 break;
2453 default:
2454 /* Shouldn't happen */
2455 tt_assert(0);
2457 if (vrs) {
2458 vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
2459 tor_asprintf(&vrs->microdesc->microdesc_hash_line,
2460 "m 9,10,11,12,13,14,15,16,17 "
2461 "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
2462 idx);
2465 done:
2466 return vrs;
2469 /** Apply tweaks to the vote list for each voter; for the umbw test this is
2470 * just adding the right consensus methods to let clipping happen */
2471 static int
2472 vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
2474 char *maxbw_param = NULL;
2475 int rv = 0;
2477 tt_assert(v);
2478 (void)voter;
2479 (void)now;
2481 tt_assert(v->supported_methods);
2482 SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
2483 smartlist_clear(v->supported_methods);
2484 /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
2485 smartlist_split_string(v->supported_methods,
2486 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
2487 NULL, 0, -1);
2488 /* If we're using a non-default clip bandwidth, add it to net_params */
2489 if (alternate_clip_bw > 0) {
2490 tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
2491 tt_assert(maxbw_param);
2492 if (maxbw_param) {
2493 smartlist_add(v->net_params, maxbw_param);
2494 rv = 1;
2498 done:
2499 return rv;
2503 * Test a parsed vote_routerstatus_t for umbw test.
2505 static void
2506 test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
2508 routerstatus_t *rs;
2509 tor_addr_t addr_ipv6;
2510 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2511 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2513 (void)voter;
2514 tt_assert(vrs);
2515 rs = &(vrs->status);
2516 tt_assert(rs);
2518 /* Split out by digests to test */
2519 if (tor_memeq(rs->identity_digest,
2520 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2521 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2522 DIGEST_LEN)) {
2524 * Check the first routerstatus - measured bandwidth below the clip
2525 * cutoff.
2527 tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
2528 tt_int_op(rs->published_on,OP_EQ, now-1500);
2529 tt_str_op(rs->nickname,OP_EQ, "router2");
2530 tt_mem_op(rs->identity_digest,OP_EQ,
2531 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2532 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2533 DIGEST_LEN);
2534 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
2535 tt_int_op(rs->addr,OP_EQ, 0x99008801);
2536 tt_int_op(rs->or_port,OP_EQ, 443);
2537 tt_int_op(rs->dir_port,OP_EQ, 8000);
2538 tt_assert(rs->has_bandwidth);
2539 tt_assert(vrs->has_measured_bw);
2540 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2541 tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2542 } else if (tor_memeq(rs->identity_digest,
2543 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2544 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2545 DIGEST_LEN)) {
2548 * Check the second routerstatus - measured bandwidth above the clip
2549 * cutoff.
2551 tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
2552 tt_int_op(rs->published_on,OP_EQ, now-1000);
2553 tt_str_op(rs->nickname,OP_EQ, "router1");
2554 tt_mem_op(rs->identity_digest,OP_EQ,
2555 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2556 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2557 DIGEST_LEN);
2558 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
2559 tt_int_op(rs->addr,OP_EQ, 0x99009901);
2560 tt_int_op(rs->or_port,OP_EQ, 443);
2561 tt_int_op(rs->dir_port,OP_EQ, 0);
2562 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2563 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
2564 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
2565 tt_assert(rs->has_bandwidth);
2566 tt_assert(vrs->has_measured_bw);
2567 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2568 tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2569 } else if (tor_memeq(rs->identity_digest,
2570 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2571 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2572 DIGEST_LEN)) {
2574 * Check the third routerstatus - unmeasured bandwidth above the clip
2575 * cutoff; this one should be clipped later on in the consensus, but
2576 * appears unclipped in the vote.
2578 tt_assert(rs->has_bandwidth);
2579 tt_assert(!(vrs->has_measured_bw));
2580 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2581 tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
2582 } else if (tor_memeq(rs->identity_digest,
2583 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2584 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2585 DIGEST_LEN)) {
2587 * Check the fourth routerstatus - unmeasured bandwidth below the clip
2588 * cutoff; this one should not be clipped.
2590 tt_assert(rs->has_bandwidth);
2591 tt_assert(!(vrs->has_measured_bw));
2592 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2593 tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
2594 } else {
2595 tt_assert(0);
2598 done:
2599 return;
2603 * Test a consensus for v3_networkstatus_test
2605 static void
2606 test_consensus_for_umbw(networkstatus_t *con, time_t now)
2608 (void)now;
2610 tt_assert(con);
2611 tt_assert(!con->cert);
2612 // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
2613 tt_assert(con->consensus_method >= 16);
2614 tt_int_op(4,OP_EQ, smartlist_len(con->routerstatus_list));
2615 /* There should be four listed routers; all voters saw the same in this */
2617 done:
2618 return;
2622 * Test a router list entry for umbw test
2624 static void
2625 test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
2627 tor_addr_t addr_ipv6;
2628 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2629 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2631 tt_assert(rs);
2633 /* There should be four listed routers, as constructed above */
2634 if (tor_memeq(rs->identity_digest,
2635 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2636 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2637 DIGEST_LEN)) {
2638 tt_mem_op(rs->identity_digest,OP_EQ,
2639 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2640 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2641 DIGEST_LEN);
2642 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
2643 tt_assert(!rs->is_authority);
2644 tt_assert(!rs->is_exit);
2645 tt_assert(!rs->is_fast);
2646 tt_assert(!rs->is_possible_guard);
2647 tt_assert(!rs->is_stable);
2648 /* (If it wasn't running it wouldn't be here) */
2649 tt_assert(rs->is_flagged_running);
2650 tt_assert(!rs->is_valid);
2651 tt_assert(!rs->is_named);
2652 /* This one should have measured bandwidth below the clip cutoff */
2653 tt_assert(rs->has_bandwidth);
2654 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2655 tt_assert(!(rs->bw_is_unmeasured));
2656 } else if (tor_memeq(rs->identity_digest,
2657 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2658 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2659 DIGEST_LEN)) {
2660 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2661 tt_mem_op(rs->identity_digest,OP_EQ,
2662 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2663 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2664 DIGEST_LEN);
2665 tt_str_op(rs->nickname,OP_EQ, "router1");
2666 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
2667 tt_int_op(rs->published_on,OP_EQ, now-1000);
2668 tt_int_op(rs->addr,OP_EQ, 0x99009901);
2669 tt_int_op(rs->or_port,OP_EQ, 443);
2670 tt_int_op(rs->dir_port,OP_EQ, 0);
2671 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2672 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
2673 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
2674 tt_assert(!rs->is_authority);
2675 tt_assert(rs->is_exit);
2676 tt_assert(rs->is_fast);
2677 tt_assert(rs->is_possible_guard);
2678 tt_assert(rs->is_stable);
2679 tt_assert(rs->is_flagged_running);
2680 tt_assert(rs->is_valid);
2681 tt_assert(!rs->is_named);
2682 /* This one should have measured bandwidth above the clip cutoff */
2683 tt_assert(rs->has_bandwidth);
2684 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2685 tt_assert(!(rs->bw_is_unmeasured));
2686 } else if (tor_memeq(rs->identity_digest,
2687 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2688 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2689 DIGEST_LEN)) {
2691 * This one should have unmeasured bandwidth above the clip cutoff,
2692 * and so should be clipped
2694 tt_assert(rs->has_bandwidth);
2695 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb);
2696 tt_assert(rs->bw_is_unmeasured);
2697 } else if (tor_memeq(rs->identity_digest,
2698 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2699 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2700 DIGEST_LEN)) {
2702 * This one should have unmeasured bandwidth below the clip cutoff,
2703 * and so should not be clipped
2705 tt_assert(rs->has_bandwidth);
2706 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2707 tt_assert(rs->bw_is_unmeasured);
2708 } else {
2709 /* Weren't expecting this... */
2710 tt_assert(0);
2713 done:
2714 return;
2718 * Compute a consensus involving clipping unmeasured bandwidth with consensus
2719 * method 17; this uses the same test_a_networkstatus() function that the
2720 * v3_networkstatus test uses.
2723 static void
2724 test_dir_clip_unmeasured_bw_kb(void *arg)
2726 /* Run the test with the default clip bandwidth */
2727 (void)arg;
2728 alternate_clip_bw = 0;
2729 test_a_networkstatus(gen_routerstatus_for_umbw,
2730 vote_tweaks_for_umbw,
2731 test_vrs_for_umbw,
2732 test_consensus_for_umbw,
2733 test_routerstatus_for_umbw);
2737 * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
2738 * of clip bandwidth.
2741 static void
2742 test_dir_clip_unmeasured_bw_kb_alt(void *arg)
2745 * Try a different one; this value is chosen so that the below-the-cutoff
2746 * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
2747 * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
2748 * cutoff it will fail the test.
2750 (void)arg;
2751 alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
2752 test_a_networkstatus(gen_routerstatus_for_umbw,
2753 vote_tweaks_for_umbw,
2754 test_vrs_for_umbw,
2755 test_consensus_for_umbw,
2756 test_routerstatus_for_umbw);
2759 static void
2760 test_dir_fmt_control_ns(void *arg)
2762 char *s = NULL;
2763 routerstatus_t rs;
2764 (void)arg;
2766 memset(&rs, 0, sizeof(rs));
2767 rs.published_on = 1364925198;
2768 strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
2769 memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
2770 memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
2771 rs.addr = 0x20304050;
2772 rs.or_port = 9001;
2773 rs.dir_port = 9002;
2774 rs.is_exit = 1;
2775 rs.is_fast = 1;
2776 rs.is_flagged_running = 1;
2777 rs.has_bandwidth = 1;
2778 rs.is_v2_dir = 1;
2779 rs.bandwidth_kb = 1000;
2781 s = networkstatus_getinfo_helper_single(&rs);
2782 tt_assert(s);
2783 tt_str_op(s, OP_EQ,
2784 "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
2785 "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
2786 "32.48.64.80 9001 9002\n"
2787 "s Exit Fast Running V2Dir\n"
2788 "w Bandwidth=1000\n");
2790 done:
2791 tor_free(s);
2794 static int mock_get_options_calls = 0;
2795 static or_options_t *mock_options = NULL;
2797 static void
2798 reset_options(or_options_t *options, int *get_options_calls)
2800 memset(options, 0, sizeof(or_options_t));
2801 options->TestingTorNetwork = 1;
2803 *get_options_calls = 0;
2806 static const or_options_t *
2807 mock_get_options(void)
2809 ++mock_get_options_calls;
2810 tor_assert(mock_options);
2811 return mock_options;
2814 static void
2815 reset_routerstatus(routerstatus_t *rs,
2816 const char *hex_identity_digest,
2817 int32_t ipv4_addr)
2819 memset(rs, 0, sizeof(routerstatus_t));
2820 base16_decode(rs->identity_digest, sizeof(rs->identity_digest),
2821 hex_identity_digest, HEX_DIGEST_LEN);
2822 /* A zero address matches everything, so the address needs to be set.
2823 * But the specific value is irrelevant. */
2824 rs->addr = ipv4_addr;
2827 #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
2828 #define ROUTER_A_IPV4 0xAA008801
2829 #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
2830 #define ROUTER_B_IPV4 0xBB008801
2832 #define ROUTERSET_ALL_STR "*"
2833 #define ROUTERSET_A_STR ROUTER_A_ID_STR
2834 #define ROUTERSET_NONE_STR ""
2837 * Test that dirserv_set_routerstatus_testing sets router flags correctly
2838 * Using "*" sets flags on A and B
2839 * Using "A" sets flags on A
2840 * Using "" sets flags on Neither
2841 * If the router is not included:
2842 * - if *Strict is set, the flag is set to 0,
2843 * - otherwise, the flag is not modified. */
2844 static void
2845 test_dir_dirserv_set_routerstatus_testing(void *arg)
2847 (void)arg;
2849 /* Init options */
2850 mock_options = malloc(sizeof(or_options_t));
2851 reset_options(mock_options, &mock_get_options_calls);
2853 MOCK(get_options, mock_get_options);
2855 /* Init routersets */
2856 routerset_t *routerset_all = routerset_new();
2857 routerset_parse(routerset_all, ROUTERSET_ALL_STR, "All routers");
2859 routerset_t *routerset_a = routerset_new();
2860 routerset_parse(routerset_a, ROUTERSET_A_STR, "Router A only");
2862 routerset_t *routerset_none = routerset_new();
2863 /* Routersets are empty when provided by routerset_new(),
2864 * so this is not strictly necessary */
2865 routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
2867 /* Init routerstatuses */
2868 routerstatus_t *rs_a = malloc(sizeof(routerstatus_t));
2869 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2871 routerstatus_t *rs_b = malloc(sizeof(routerstatus_t));
2872 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2874 /* Sanity check that routersets correspond to routerstatuses.
2875 * Return values are {2, 3, 4} */
2877 /* We want 3 ("*" means match all addresses) */
2878 tt_assert(routerset_contains_routerstatus(routerset_all, rs_a, 0) == 3);
2879 tt_assert(routerset_contains_routerstatus(routerset_all, rs_b, 0) == 3);
2881 /* We want 4 (match id_digest [or nickname]) */
2882 tt_assert(routerset_contains_routerstatus(routerset_a, rs_a, 0) == 4);
2883 tt_assert(routerset_contains_routerstatus(routerset_a, rs_b, 0) == 0);
2885 tt_assert(routerset_contains_routerstatus(routerset_none, rs_a, 0) == 0);
2886 tt_assert(routerset_contains_routerstatus(routerset_none, rs_b, 0) == 0);
2888 /* Check that "*" sets flags on all routers: Exit
2889 * Check the flags aren't being confused with each other */
2890 reset_options(mock_options, &mock_get_options_calls);
2891 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2892 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2894 mock_options->TestingDirAuthVoteExit = routerset_all;
2895 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2897 dirserv_set_routerstatus_testing(rs_a);
2898 tt_assert(mock_get_options_calls == 1);
2899 dirserv_set_routerstatus_testing(rs_b);
2900 tt_assert(mock_get_options_calls == 2);
2902 tt_assert(rs_a->is_exit == 1);
2903 tt_assert(rs_b->is_exit == 1);
2904 /* Be paranoid - check no other flags are set */
2905 tt_assert(rs_a->is_possible_guard == 0);
2906 tt_assert(rs_b->is_possible_guard == 0);
2907 tt_assert(rs_a->is_hs_dir == 0);
2908 tt_assert(rs_b->is_hs_dir == 0);
2910 /* Check that "*" sets flags on all routers: Guard & HSDir
2911 * Cover the remaining flags in one test */
2912 reset_options(mock_options, &mock_get_options_calls);
2913 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2914 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2916 mock_options->TestingDirAuthVoteGuard = routerset_all;
2917 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2918 mock_options->TestingDirAuthVoteHSDir = routerset_all;
2919 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2921 dirserv_set_routerstatus_testing(rs_a);
2922 tt_assert(mock_get_options_calls == 1);
2923 dirserv_set_routerstatus_testing(rs_b);
2924 tt_assert(mock_get_options_calls == 2);
2926 tt_assert(rs_a->is_possible_guard == 1);
2927 tt_assert(rs_b->is_possible_guard == 1);
2928 tt_assert(rs_a->is_hs_dir == 1);
2929 tt_assert(rs_b->is_hs_dir == 1);
2930 /* Be paranoid - check exit isn't set */
2931 tt_assert(rs_a->is_exit == 0);
2932 tt_assert(rs_b->is_exit == 0);
2934 /* Check routerset A sets all flags on router A,
2935 * but leaves router B unmodified */
2936 reset_options(mock_options, &mock_get_options_calls);
2937 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2938 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2940 mock_options->TestingDirAuthVoteExit = routerset_a;
2941 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2942 mock_options->TestingDirAuthVoteGuard = routerset_a;
2943 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2944 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2945 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2947 dirserv_set_routerstatus_testing(rs_a);
2948 tt_assert(mock_get_options_calls == 1);
2949 dirserv_set_routerstatus_testing(rs_b);
2950 tt_assert(mock_get_options_calls == 2);
2952 tt_assert(rs_a->is_exit == 1);
2953 tt_assert(rs_b->is_exit == 0);
2954 tt_assert(rs_a->is_possible_guard == 1);
2955 tt_assert(rs_b->is_possible_guard == 0);
2956 tt_assert(rs_a->is_hs_dir == 1);
2957 tt_assert(rs_b->is_hs_dir == 0);
2959 /* Check routerset A unsets all flags on router B when Strict is set */
2960 reset_options(mock_options, &mock_get_options_calls);
2961 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2963 mock_options->TestingDirAuthVoteExit = routerset_a;
2964 mock_options->TestingDirAuthVoteExitIsStrict = 1;
2965 mock_options->TestingDirAuthVoteGuard = routerset_a;
2966 mock_options->TestingDirAuthVoteGuardIsStrict = 1;
2967 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2968 mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
2970 rs_b->is_exit = 1;
2971 rs_b->is_possible_guard = 1;
2972 rs_b->is_hs_dir = 1;
2974 dirserv_set_routerstatus_testing(rs_b);
2975 tt_assert(mock_get_options_calls == 1);
2977 tt_assert(rs_b->is_exit == 0);
2978 tt_assert(rs_b->is_possible_guard == 0);
2979 tt_assert(rs_b->is_hs_dir == 0);
2981 /* Check routerset A doesn't modify flags on router B without Strict set */
2982 reset_options(mock_options, &mock_get_options_calls);
2983 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2985 mock_options->TestingDirAuthVoteExit = routerset_a;
2986 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2987 mock_options->TestingDirAuthVoteGuard = routerset_a;
2988 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2989 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2990 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2992 rs_b->is_exit = 1;
2993 rs_b->is_possible_guard = 1;
2994 rs_b->is_hs_dir = 1;
2996 dirserv_set_routerstatus_testing(rs_b);
2997 tt_assert(mock_get_options_calls == 1);
2999 tt_assert(rs_b->is_exit == 1);
3000 tt_assert(rs_b->is_possible_guard == 1);
3001 tt_assert(rs_b->is_hs_dir == 1);
3003 /* Check the empty routerset zeroes all flags
3004 * on routers A & B with Strict set */
3005 reset_options(mock_options, &mock_get_options_calls);
3006 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
3008 mock_options->TestingDirAuthVoteExit = routerset_none;
3009 mock_options->TestingDirAuthVoteExitIsStrict = 1;
3010 mock_options->TestingDirAuthVoteGuard = routerset_none;
3011 mock_options->TestingDirAuthVoteGuardIsStrict = 1;
3012 mock_options->TestingDirAuthVoteHSDir = routerset_none;
3013 mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
3015 rs_b->is_exit = 1;
3016 rs_b->is_possible_guard = 1;
3017 rs_b->is_hs_dir = 1;
3019 dirserv_set_routerstatus_testing(rs_b);
3020 tt_assert(mock_get_options_calls == 1);
3022 tt_assert(rs_b->is_exit == 0);
3023 tt_assert(rs_b->is_possible_guard == 0);
3024 tt_assert(rs_b->is_hs_dir == 0);
3026 /* Check the empty routerset doesn't modify any flags
3027 * on A or B without Strict set */
3028 reset_options(mock_options, &mock_get_options_calls);
3029 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
3030 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
3032 mock_options->TestingDirAuthVoteExit = routerset_none;
3033 mock_options->TestingDirAuthVoteExitIsStrict = 0;
3034 mock_options->TestingDirAuthVoteGuard = routerset_none;
3035 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
3036 mock_options->TestingDirAuthVoteHSDir = routerset_none;
3037 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
3039 rs_b->is_exit = 1;
3040 rs_b->is_possible_guard = 1;
3041 rs_b->is_hs_dir = 1;
3043 dirserv_set_routerstatus_testing(rs_a);
3044 tt_assert(mock_get_options_calls == 1);
3045 dirserv_set_routerstatus_testing(rs_b);
3046 tt_assert(mock_get_options_calls == 2);
3048 tt_assert(rs_a->is_exit == 0);
3049 tt_assert(rs_a->is_possible_guard == 0);
3050 tt_assert(rs_a->is_hs_dir == 0);
3051 tt_assert(rs_b->is_exit == 1);
3052 tt_assert(rs_b->is_possible_guard == 1);
3053 tt_assert(rs_b->is_hs_dir == 1);
3055 done:
3056 free(mock_options);
3057 mock_options = NULL;
3059 UNMOCK(get_options);
3061 routerset_free(routerset_all);
3062 routerset_free(routerset_a);
3063 routerset_free(routerset_none);
3065 free(rs_a);
3066 free(rs_b);
3069 static void
3070 test_dir_http_handling(void *args)
3072 char *url = NULL;
3073 (void)args;
3075 /* Parse http url tests: */
3076 /* Good headers */
3077 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
3078 "Host: example.com\r\n"
3079 "User-Agent: Mozilla/5.0 (Windows;"
3080 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3081 &url),OP_EQ, 0);
3082 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3083 tor_free(url);
3085 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),OP_EQ, 0);
3086 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3087 tor_free(url);
3089 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),
3090 OP_EQ, 0);
3091 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3092 tor_free(url);
3094 /* Should prepend '/tor/' to url if required */
3095 tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
3096 "Host: example.com\r\n"
3097 "User-Agent: Mozilla/5.0 (Windows;"
3098 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3099 &url),OP_EQ, 0);
3100 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3101 tor_free(url);
3103 /* Bad headers -- no HTTP/1.x*/
3104 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3105 "Host: example.com\r\n"
3106 "User-Agent: Mozilla/5.0 (Windows;"
3107 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3108 &url),OP_EQ, -1);
3109 tt_assert(!url);
3111 /* Bad headers */
3112 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3113 "Host: example.com\r\n"
3114 "User-Agent: Mozilla/5.0 (Windows;"
3115 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3116 &url),OP_EQ, -1);
3117 tt_assert(!url);
3119 tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),OP_EQ, -1);
3120 tt_assert(!url);
3122 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),OP_EQ, -1);
3123 tt_assert(!url);
3125 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),
3126 OP_EQ, -1);
3127 tt_assert(!url);
3129 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),OP_EQ, -1);
3130 tt_assert(!url);
3132 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),OP_EQ, -1);
3133 tt_assert(!url);
3135 done:
3136 tor_free(url);
3139 static void
3140 test_dir_purpose_needs_anonymity(void *arg)
3142 (void)arg;
3143 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE));
3144 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_GENERAL));
3145 tt_int_op(0, ==, purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC,
3146 ROUTER_PURPOSE_GENERAL));
3147 done: ;
3150 static void
3151 test_dir_fetch_type(void *arg)
3153 (void)arg;
3154 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
3155 NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
3156 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
3157 NULL), OP_EQ, EXTRAINFO_DIRINFO | V3_DIRINFO);
3159 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
3160 NULL), OP_EQ, BRIDGE_DIRINFO);
3161 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC,
3162 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3164 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE,
3165 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3166 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
3167 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3168 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE,
3169 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3171 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
3172 "microdesc"), OP_EQ, V3_DIRINFO|MICRODESC_DIRINFO);
3173 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
3174 NULL), OP_EQ, V3_DIRINFO);
3176 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
3177 NULL), OP_EQ, MICRODESC_DIRINFO);
3179 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
3180 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
3181 done: ;
3184 static void
3185 test_dir_packages(void *arg)
3187 smartlist_t *votes = smartlist_new();
3188 char *res = NULL;
3189 (void)arg;
3191 #define BAD(s) \
3192 tt_int_op(0, ==, validate_recommended_package_line(s));
3193 #define GOOD(s) \
3194 tt_int_op(1, ==, validate_recommended_package_line(s));
3195 GOOD("tor 0.2.6.3-alpha "
3196 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3197 "sha256=sssdlkfjdsklfjdskfljasdklfj");
3198 GOOD("tor 0.2.6.3-alpha "
3199 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3200 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
3201 BAD("tor 0.2.6.3-alpha "
3202 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3203 "sha256=sssdlkfjdsklfjdskfljasdklfj=");
3204 BAD("tor 0.2.6.3-alpha "
3205 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3206 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
3207 BAD("tor 0.2.6.3-alpha "
3208 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
3209 BAD("tor 0.2.6.3-alpha "
3210 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
3211 BAD("tor 0.2.6.3-alpha ");
3212 BAD("tor 0.2.6.3-alpha");
3213 BAD("tor ");
3214 BAD("tor");
3215 BAD("");
3216 BAD("=foobar sha256="
3217 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3218 BAD("= = sha256="
3219 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3221 BAD("sha512= sha256="
3222 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3224 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3225 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3226 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3227 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3228 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3229 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
3230 SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
3231 ns->package_lines = smartlist_new());
3233 #define ADD(i, s) \
3234 smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
3235 (void*)(s));
3237 /* Only one vote for this one. */
3238 ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
3240 /* Only two matching entries for this one, but 3 voters */
3241 ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
3242 ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3243 ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3245 /* Only two matching entries for this one, but at least 4 voters */
3246 ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
3247 ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3248 ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3249 ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
3251 /* This one has only invalid votes. */
3252 ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
3253 ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
3254 ADD(2, "haffenreffer 1.2 ");
3255 ADD(3, "haffenreffer ");
3256 ADD(4, "haffenreffer");
3258 /* Three matching votes for this; it should actually go in! */
3259 ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3260 ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3261 ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3262 ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
3263 ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
3265 /* Three votes for A, three votes for B */
3266 ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3267 ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3268 ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3269 ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3270 ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3271 ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3273 /* Three votes for A, two votes for B */
3274 ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3275 ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3276 ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3277 ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3278 ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3280 /* Four votes for A, two for B. */
3281 ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3282 ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3283 ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3284 ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3285 ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3286 ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3288 /* Five votes for A ... all from the same authority. Three for B. */
3289 ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3290 ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3291 ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3292 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3293 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3294 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3295 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3296 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
3298 /* As above but new replaces old: no two match. */
3299 ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3300 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3301 ADD(1, "cbc 99.1.11.1.2 http://example.com/cbc/x cubehash=ahooy sha512=m");
3302 ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3303 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3304 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3305 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3306 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3307 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
3309 res = compute_consensus_package_lines(votes);
3310 tt_assert(res);
3311 tt_str_op(res, ==,
3312 "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
3313 "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
3314 "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
3315 "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
3316 "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
3319 #undef ADD
3320 #undef BAD
3321 #undef GOOD
3322 done:
3323 SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
3324 { smartlist_free(ns->package_lines); tor_free(ns); });
3325 smartlist_free(votes);
3326 tor_free(res);
3329 static void
3330 test_dir_download_status_schedule(void *arg)
3332 (void)arg;
3333 download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
3334 DL_WANT_AUTHORITY,
3335 DL_SCHED_INCREMENT_FAILURE };
3336 download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_CONSENSUS,
3337 DL_WANT_ANY_DIRSERVER,
3338 DL_SCHED_INCREMENT_ATTEMPT};
3339 download_status_t dls_bridge = { 0, 0, 0, DL_SCHED_BRIDGE,
3340 DL_WANT_AUTHORITY,
3341 DL_SCHED_INCREMENT_FAILURE};
3342 int increment = -1;
3343 int expected_increment = -1;
3344 time_t current_time = time(NULL);
3345 int delay1 = -1;
3346 int delay2 = -1;
3347 smartlist_t *schedule = smartlist_new();
3349 /* Make a dummy schedule */
3350 smartlist_add(schedule, (void *)&delay1);
3351 smartlist_add(schedule, (void *)&delay2);
3353 /* check a range of values */
3354 delay1 = 1000;
3355 increment = download_status_schedule_get_delay(&dls_failure,
3356 schedule,
3357 TIME_MIN);
3358 expected_increment = delay1;
3359 tt_assert(increment == expected_increment);
3360 tt_assert(dls_failure.next_attempt_at == TIME_MIN + expected_increment);
3362 delay1 = INT_MAX;
3363 increment = download_status_schedule_get_delay(&dls_failure,
3364 schedule,
3365 -1);
3366 expected_increment = delay1;
3367 tt_assert(increment == expected_increment);
3368 tt_assert(dls_failure.next_attempt_at == TIME_MAX);
3370 delay1 = 0;
3371 increment = download_status_schedule_get_delay(&dls_attempt,
3372 schedule,
3374 expected_increment = delay1;
3375 tt_assert(increment == expected_increment);
3376 tt_assert(dls_attempt.next_attempt_at == 0 + expected_increment);
3378 delay1 = 1000;
3379 increment = download_status_schedule_get_delay(&dls_attempt,
3380 schedule,
3382 expected_increment = delay1;
3383 tt_assert(increment == expected_increment);
3384 tt_assert(dls_attempt.next_attempt_at == 1 + expected_increment);
3386 delay1 = INT_MAX;
3387 increment = download_status_schedule_get_delay(&dls_bridge,
3388 schedule,
3389 current_time);
3390 expected_increment = delay1;
3391 tt_assert(increment == expected_increment);
3392 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3394 delay1 = 1;
3395 increment = download_status_schedule_get_delay(&dls_bridge,
3396 schedule,
3397 TIME_MAX);
3398 expected_increment = delay1;
3399 tt_assert(increment == expected_increment);
3400 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3402 /* see what happens when we reach the end */
3403 dls_attempt.n_download_attempts++;
3404 dls_bridge.n_download_failures++;
3406 delay2 = 100;
3407 increment = download_status_schedule_get_delay(&dls_attempt,
3408 schedule,
3409 current_time);
3410 expected_increment = delay2;
3411 tt_assert(increment == expected_increment);
3412 tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
3414 delay2 = 1;
3415 increment = download_status_schedule_get_delay(&dls_bridge,
3416 schedule,
3417 current_time);
3418 expected_increment = delay2;
3419 tt_assert(increment == expected_increment);
3420 tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
3422 /* see what happens when we try to go off the end */
3423 dls_attempt.n_download_attempts++;
3424 dls_bridge.n_download_failures++;
3426 delay2 = 5;
3427 increment = download_status_schedule_get_delay(&dls_attempt,
3428 schedule,
3429 current_time);
3430 expected_increment = delay2;
3431 tt_assert(increment == expected_increment);
3432 tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
3434 delay2 = 17;
3435 increment = download_status_schedule_get_delay(&dls_bridge,
3436 schedule,
3437 current_time);
3438 expected_increment = delay2;
3439 tt_assert(increment == expected_increment);
3440 tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
3442 /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
3443 dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
3444 dls_bridge.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
3446 delay2 = 35;
3447 increment = download_status_schedule_get_delay(&dls_attempt,
3448 schedule,
3449 current_time);
3450 expected_increment = INT_MAX;
3451 tt_assert(increment == expected_increment);
3452 tt_assert(dls_attempt.next_attempt_at == TIME_MAX);
3454 delay2 = 99;
3455 increment = download_status_schedule_get_delay(&dls_bridge,
3456 schedule,
3457 current_time);
3458 expected_increment = INT_MAX;
3459 tt_assert(increment == expected_increment);
3460 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3462 done:
3463 /* the pointers in schedule are allocated on the stack */
3464 smartlist_free(schedule);
3467 static void
3468 test_dir_download_status_increment(void *arg)
3470 (void)arg;
3471 download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
3472 DL_WANT_AUTHORITY,
3473 DL_SCHED_INCREMENT_FAILURE };
3474 download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_BRIDGE,
3475 DL_WANT_ANY_DIRSERVER,
3476 DL_SCHED_INCREMENT_ATTEMPT};
3477 int delay0 = -1;
3478 int delay1 = -1;
3479 int delay2 = -1;
3480 smartlist_t *schedule = smartlist_new();
3481 or_options_t test_options;
3482 time_t next_at = TIME_MAX;
3483 time_t current_time = time(NULL);
3485 /* Provide some values for the schedule */
3486 delay0 = 10;
3487 delay1 = 99;
3488 delay2 = 20;
3490 /* Make the schedule */
3491 smartlist_add(schedule, (void *)&delay0);
3492 smartlist_add(schedule, (void *)&delay1);
3493 smartlist_add(schedule, (void *)&delay2);
3495 /* Put it in the options */
3496 mock_options = &test_options;
3497 reset_options(mock_options, &mock_get_options_calls);
3498 mock_options->TestingClientDownloadSchedule = schedule;
3499 mock_options->TestingBridgeDownloadSchedule = schedule;
3501 MOCK(get_options, mock_get_options);
3503 /* Check that a failure reset works */
3504 mock_get_options_calls = 0;
3505 download_status_reset(&dls_failure);
3506 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3507 * an unrealiable test, because time(NULL) might change. */
3508 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3509 >= current_time + delay0);
3510 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3511 != TIME_MAX);
3512 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3513 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3514 tt_assert(mock_get_options_calls >= 1);
3516 /* avoid timing inconsistencies */
3517 dls_failure.next_attempt_at = current_time + delay0;
3519 /* check that a reset schedule becomes ready at the right time */
3520 tt_assert(download_status_is_ready(&dls_failure,
3521 current_time + delay0 - 1,
3522 1) == 0);
3523 tt_assert(download_status_is_ready(&dls_failure,
3524 current_time + delay0,
3525 1) == 1);
3526 tt_assert(download_status_is_ready(&dls_failure,
3527 current_time + delay0 + 1,
3528 1) == 1);
3530 /* Check that a failure increment works */
3531 mock_get_options_calls = 0;
3532 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3533 current_time);
3534 tt_assert(next_at == current_time + delay1);
3535 tt_assert(download_status_get_n_failures(&dls_failure) == 1);
3536 tt_assert(download_status_get_n_attempts(&dls_failure) == 1);
3537 tt_assert(mock_get_options_calls >= 1);
3539 /* check that an incremented schedule becomes ready at the right time */
3540 tt_assert(download_status_is_ready(&dls_failure,
3541 current_time + delay1 - 1,
3542 1) == 0);
3543 tt_assert(download_status_is_ready(&dls_failure,
3544 current_time + delay1,
3545 1) == 1);
3546 tt_assert(download_status_is_ready(&dls_failure,
3547 current_time + delay1 + 1,
3548 1) == 1);
3550 /* check that a schedule isn't ready if it's had too many failures */
3551 tt_assert(download_status_is_ready(&dls_failure,
3552 current_time + delay1 + 10,
3553 0) == 0);
3555 /* Check that failure increments don't happen on 503 for clients, but that
3556 * attempt increments do. */
3557 mock_get_options_calls = 0;
3558 next_at = download_status_increment_failure(&dls_failure, 503, "test", 0,
3559 current_time);
3560 tt_assert(next_at == current_time + delay1);
3561 tt_assert(download_status_get_n_failures(&dls_failure) == 1);
3562 tt_assert(download_status_get_n_attempts(&dls_failure) == 2);
3563 tt_assert(mock_get_options_calls >= 1);
3565 /* Check that failure increments do happen on 503 for servers */
3566 mock_get_options_calls = 0;
3567 next_at = download_status_increment_failure(&dls_failure, 503, "test", 1,
3568 current_time);
3569 tt_assert(next_at == current_time + delay2);
3570 tt_assert(download_status_get_n_failures(&dls_failure) == 2);
3571 tt_assert(download_status_get_n_attempts(&dls_failure) == 3);
3572 tt_assert(mock_get_options_calls >= 1);
3574 /* Check what happens when we run off the end of the schedule */
3575 mock_get_options_calls = 0;
3576 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3577 current_time);
3578 tt_assert(next_at == current_time + delay2);
3579 tt_assert(download_status_get_n_failures(&dls_failure) == 3);
3580 tt_assert(download_status_get_n_attempts(&dls_failure) == 4);
3581 tt_assert(mock_get_options_calls >= 1);
3583 /* Check what happens when we hit the failure limit */
3584 mock_get_options_calls = 0;
3585 download_status_mark_impossible(&dls_failure);
3586 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3587 current_time);
3588 tt_assert(next_at == TIME_MAX);
3589 tt_assert(download_status_get_n_failures(&dls_failure)
3590 == IMPOSSIBLE_TO_DOWNLOAD);
3591 tt_assert(download_status_get_n_attempts(&dls_failure)
3592 == IMPOSSIBLE_TO_DOWNLOAD);
3593 tt_assert(mock_get_options_calls >= 1);
3595 /* Check that a failure reset doesn't reset at the limit */
3596 mock_get_options_calls = 0;
3597 download_status_reset(&dls_failure);
3598 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3599 == TIME_MAX);
3600 tt_assert(download_status_get_n_failures(&dls_failure)
3601 == IMPOSSIBLE_TO_DOWNLOAD);
3602 tt_assert(download_status_get_n_attempts(&dls_failure)
3603 == IMPOSSIBLE_TO_DOWNLOAD);
3604 tt_assert(mock_get_options_calls == 0);
3606 /* Check that a failure reset resets just before the limit */
3607 mock_get_options_calls = 0;
3608 dls_failure.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
3609 dls_failure.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
3610 download_status_reset(&dls_failure);
3611 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3612 * an unrealiable test, because time(NULL) might change. */
3613 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3614 >= current_time + delay0);
3615 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3616 != TIME_MAX);
3617 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3618 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3619 tt_assert(mock_get_options_calls >= 1);
3621 /* Check that failure increments do happen on attempt-based schedules,
3622 * but that the retry is set at the end of time */
3623 mock_get_options_calls = 0;
3624 next_at = download_status_increment_failure(&dls_attempt, 404, "test", 0,
3625 current_time);
3626 tt_assert(next_at == TIME_MAX);
3627 tt_assert(download_status_get_n_failures(&dls_attempt) == 1);
3628 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3629 tt_assert(mock_get_options_calls == 0);
3631 /* Check that an attempt reset works */
3632 mock_get_options_calls = 0;
3633 download_status_reset(&dls_attempt);
3634 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3635 * an unrealiable test, because time(NULL) might change. */
3636 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3637 >= current_time + delay0);
3638 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3639 != TIME_MAX);
3640 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3641 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3642 tt_assert(mock_get_options_calls >= 1);
3644 /* avoid timing inconsistencies */
3645 dls_attempt.next_attempt_at = current_time + delay0;
3647 /* check that a reset schedule becomes ready at the right time */
3648 tt_assert(download_status_is_ready(&dls_attempt,
3649 current_time + delay0 - 1,
3650 1) == 0);
3651 tt_assert(download_status_is_ready(&dls_attempt,
3652 current_time + delay0,
3653 1) == 1);
3654 tt_assert(download_status_is_ready(&dls_attempt,
3655 current_time + delay0 + 1,
3656 1) == 1);
3658 /* Check that an attempt increment works */
3659 mock_get_options_calls = 0;
3660 next_at = download_status_increment_attempt(&dls_attempt, "test",
3661 current_time);
3662 tt_assert(next_at == current_time + delay1);
3663 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3664 tt_assert(download_status_get_n_attempts(&dls_attempt) == 1);
3665 tt_assert(mock_get_options_calls >= 1);
3667 /* check that an incremented schedule becomes ready at the right time */
3668 tt_assert(download_status_is_ready(&dls_attempt,
3669 current_time + delay1 - 1,
3670 1) == 0);
3671 tt_assert(download_status_is_ready(&dls_attempt,
3672 current_time + delay1,
3673 1) == 1);
3674 tt_assert(download_status_is_ready(&dls_attempt,
3675 current_time + delay1 + 1,
3676 1) == 1);
3678 /* check that a schedule isn't ready if it's had too many attempts */
3679 tt_assert(download_status_is_ready(&dls_attempt,
3680 current_time + delay1 + 10,
3681 0) == 0);
3683 /* Check what happens when we reach then run off the end of the schedule */
3684 mock_get_options_calls = 0;
3685 next_at = download_status_increment_attempt(&dls_attempt, "test",
3686 current_time);
3687 tt_assert(next_at == current_time + delay2);
3688 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3689 tt_assert(download_status_get_n_attempts(&dls_attempt) == 2);
3690 tt_assert(mock_get_options_calls >= 1);
3692 mock_get_options_calls = 0;
3693 next_at = download_status_increment_attempt(&dls_attempt, "test",
3694 current_time);
3695 tt_assert(next_at == current_time + delay2);
3696 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3697 tt_assert(download_status_get_n_attempts(&dls_attempt) == 3);
3698 tt_assert(mock_get_options_calls >= 1);
3700 /* Check what happens when we hit the attempt limit */
3701 mock_get_options_calls = 0;
3702 download_status_mark_impossible(&dls_attempt);
3703 next_at = download_status_increment_attempt(&dls_attempt, "test",
3704 current_time);
3705 tt_assert(next_at == TIME_MAX);
3706 tt_assert(download_status_get_n_failures(&dls_attempt)
3707 == IMPOSSIBLE_TO_DOWNLOAD);
3708 tt_assert(download_status_get_n_attempts(&dls_attempt)
3709 == IMPOSSIBLE_TO_DOWNLOAD);
3710 tt_assert(mock_get_options_calls >= 1);
3712 /* Check that an attempt reset doesn't reset at the limit */
3713 mock_get_options_calls = 0;
3714 download_status_reset(&dls_attempt);
3715 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3716 == TIME_MAX);
3717 tt_assert(download_status_get_n_failures(&dls_attempt)
3718 == IMPOSSIBLE_TO_DOWNLOAD);
3719 tt_assert(download_status_get_n_attempts(&dls_attempt)
3720 == IMPOSSIBLE_TO_DOWNLOAD);
3721 tt_assert(mock_get_options_calls == 0);
3723 /* Check that an attempt reset resets just before the limit */
3724 mock_get_options_calls = 0;
3725 dls_attempt.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
3726 dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
3727 download_status_reset(&dls_attempt);
3728 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3729 * an unrealiable test, because time(NULL) might change. */
3730 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3731 >= current_time + delay0);
3732 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3733 != TIME_MAX);
3734 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3735 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3736 tt_assert(mock_get_options_calls >= 1);
3738 /* Check that attempt increments don't happen on failure-based schedules,
3739 * and that the attempt is set at the end of time */
3740 mock_get_options_calls = 0;
3741 next_at = download_status_increment_attempt(&dls_failure, "test",
3742 current_time);
3743 tt_assert(next_at == TIME_MAX);
3744 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3745 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3746 tt_assert(mock_get_options_calls == 0);
3748 done:
3749 /* the pointers in schedule are allocated on the stack */
3750 smartlist_free(schedule);
3751 UNMOCK(get_options);
3752 mock_options = NULL;
3753 mock_get_options_calls = 0;
3756 static void
3757 test_dir_authdir_type_to_string(void *data)
3759 (void)data;
3760 char *res;
3762 tt_str_op(res = authdir_type_to_string(NO_DIRINFO), OP_EQ,
3763 "[Not an authority]");
3764 tor_free(res);
3766 tt_str_op(res = authdir_type_to_string(EXTRAINFO_DIRINFO), OP_EQ,
3767 "[Not an authority]");
3768 tor_free(res);
3770 tt_str_op(res = authdir_type_to_string(MICRODESC_DIRINFO), OP_EQ,
3771 "[Not an authority]");
3772 tor_free(res);
3774 tt_str_op(res = authdir_type_to_string(V3_DIRINFO), OP_EQ, "V3");
3775 tor_free(res);
3777 tt_str_op(res = authdir_type_to_string(BRIDGE_DIRINFO), OP_EQ, "Bridge");
3778 tor_free(res);
3780 tt_str_op(res = authdir_type_to_string(
3781 V3_DIRINFO | BRIDGE_DIRINFO | EXTRAINFO_DIRINFO), OP_EQ,
3782 "V3, Bridge");
3783 done:
3784 tor_free(res);
3787 static void
3788 test_dir_conn_purpose_to_string(void *data)
3790 (void)data;
3792 #define EXPECT_CONN_PURPOSE(purpose, expected) \
3793 tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
3795 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR, "server descriptor upload");
3796 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE, "server vote upload");
3797 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES,
3798 "consensus signature upload");
3799 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC, "server descriptor fetch");
3800 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO, "extra-info fetch");
3801 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS,
3802 "consensus network-status fetch");
3803 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE, "authority cert fetch");
3804 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE, "status vote fetch");
3805 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
3806 "consensus signature fetch");
3807 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2,
3808 "hidden-service v2 descriptor fetch");
3809 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
3810 "hidden-service v2 descriptor upload");
3811 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
3812 EXPECT_CONN_PURPOSE(1024, "(unknown)");
3814 done: ;
3817 NS_DECL(int,
3818 public_server_mode, (const or_options_t *options));
3820 static int
3821 NS(public_server_mode)(const or_options_t *options)
3823 (void)options;
3825 if (CALLED(public_server_mode)++ == 0) {
3826 return 1;
3829 return 0;
3832 static void
3833 test_dir_should_use_directory_guards(void *data)
3835 or_options_t *options;
3836 char *errmsg = NULL;
3837 (void)data;
3839 NS_MOCK(public_server_mode);
3841 options = options_new();
3842 options_init(options);
3844 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3845 tt_int_op(CALLED(public_server_mode), OP_EQ, 1);
3847 options->UseEntryGuardsAsDirGuards = 1;
3848 options->UseEntryGuards = 1;
3849 options->DownloadExtraInfo = 0;
3850 options->FetchDirInfoEarly = 0;
3851 options->FetchDirInfoExtraEarly = 0;
3852 options->FetchUselessDescriptors = 0;
3853 tt_int_op(should_use_directory_guards(options), OP_EQ, 1);
3854 tt_int_op(CALLED(public_server_mode), OP_EQ, 2);
3856 options->UseEntryGuards = 0;
3857 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3858 tt_int_op(CALLED(public_server_mode), OP_EQ, 3);
3859 options->UseEntryGuards = 1;
3861 options->UseEntryGuardsAsDirGuards = 0;
3862 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3863 tt_int_op(CALLED(public_server_mode), OP_EQ, 4);
3864 options->UseEntryGuardsAsDirGuards = 1;
3866 options->DownloadExtraInfo = 1;
3867 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3868 tt_int_op(CALLED(public_server_mode), OP_EQ, 5);
3869 options->DownloadExtraInfo = 0;
3871 options->FetchDirInfoEarly = 1;
3872 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3873 tt_int_op(CALLED(public_server_mode), OP_EQ, 6);
3874 options->FetchDirInfoEarly = 0;
3876 options->FetchDirInfoExtraEarly = 1;
3877 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3878 tt_int_op(CALLED(public_server_mode), OP_EQ, 7);
3879 options->FetchDirInfoExtraEarly = 0;
3881 options->FetchUselessDescriptors = 1;
3882 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3883 tt_int_op(CALLED(public_server_mode), OP_EQ, 8);
3884 options->FetchUselessDescriptors = 0;
3886 done:
3887 NS_UNMOCK(public_server_mode);
3888 or_options_free(options);
3889 tor_free(errmsg);
3892 NS_DECL(void,
3893 directory_initiate_command_routerstatus, (const routerstatus_t *status,
3894 uint8_t dir_purpose,
3895 uint8_t router_purpose,
3896 dir_indirection_t indirection,
3897 const char *resource,
3898 const char *payload,
3899 size_t payload_len,
3900 time_t if_modified_since));
3902 static void
3903 test_dir_should_not_init_request_to_ourselves(void *data)
3905 char digest[DIGEST_LEN];
3906 dir_server_t *ourself = NULL;
3907 crypto_pk_t *key = pk_generate(2);
3908 (void) data;
3910 NS_MOCK(directory_initiate_command_routerstatus);
3912 clear_dir_servers();
3913 routerlist_free_all();
3915 set_server_identity_key(key);
3916 crypto_pk_get_digest(key, (char*) &digest);
3917 ourself = trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
3918 NULL, digest,
3919 NULL, V3_DIRINFO, 1.0);
3921 tt_assert(ourself);
3922 dir_server_add(ourself);
3924 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3925 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3927 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3928 NULL);
3930 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3932 done:
3933 NS_UNMOCK(directory_initiate_command_routerstatus);
3934 clear_dir_servers();
3935 routerlist_free_all();
3936 crypto_pk_free(key);
3939 static void
3940 test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data)
3942 dir_server_t *ds = NULL;
3943 dirinfo_type_t dirinfo_type = BRIDGE_DIRINFO | EXTRAINFO_DIRINFO \
3944 | MICRODESC_DIRINFO;
3945 (void) data;
3947 NS_MOCK(directory_initiate_command_routerstatus);
3949 clear_dir_servers();
3950 routerlist_free_all();
3952 ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
3953 "12345678901234567890", NULL, dirinfo_type, 1.0);
3954 tt_assert(ds);
3955 dir_server_add(ds);
3957 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3958 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3960 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3961 NULL);
3962 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3964 done:
3965 NS_UNMOCK(directory_initiate_command_routerstatus);
3966 clear_dir_servers();
3967 routerlist_free_all();
3970 static void
3971 test_dir_should_init_request_to_dir_auths(void *data)
3973 dir_server_t *ds = NULL;
3974 (void) data;
3976 NS_MOCK(directory_initiate_command_routerstatus);
3978 clear_dir_servers();
3979 routerlist_free_all();
3981 ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
3982 "12345678901234567890", NULL, V3_DIRINFO, 1.0);
3983 tt_assert(ds);
3984 dir_server_add(ds);
3986 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3987 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 1);
3989 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3990 NULL);
3991 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 2);
3993 done:
3994 NS_UNMOCK(directory_initiate_command_routerstatus);
3995 clear_dir_servers();
3996 routerlist_free_all();
3999 void
4000 NS(directory_initiate_command_routerstatus)(const routerstatus_t *status,
4001 uint8_t dir_purpose,
4002 uint8_t router_purpose,
4003 dir_indirection_t indirection,
4004 const char *resource,
4005 const char *payload,
4006 size_t payload_len,
4007 time_t if_modified_since)
4009 (void)status;
4010 (void)dir_purpose;
4011 (void)router_purpose;
4012 (void)indirection;
4013 (void)resource;
4014 (void)payload;
4015 (void)payload_len;
4016 (void)if_modified_since;
4017 CALLED(directory_initiate_command_routerstatus)++;
4020 static void
4021 test_dir_choose_compression_level(void* data)
4023 (void)data;
4025 /* It starts under_memory_pressure */
4026 tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);
4028 tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
4029 tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
4030 tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
4031 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
4033 /* Reset under_memory_pressure timer */
4034 cell_queues_check_size();
4035 tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);
4037 tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
4038 tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
4039 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
4040 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
4042 done: ;
4045 static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
4046 static int
4047 mock_networkstatus_consensus_is_bootstrapping(time_t now)
4049 (void)now;
4050 return mock_networkstatus_consensus_is_bootstrapping_value;
4053 static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
4054 static int
4055 mock_networkstatus_consensus_can_use_extra_fallbacks(
4056 const or_options_t *options)
4058 (void)options;
4059 return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
4062 /* data is a 2 character nul-terminated string.
4063 * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
4064 * If data[1] is 'f', set extra fallbacks, anything else means no extra
4065 * fallbacks.
4067 static void
4068 test_dir_find_dl_schedule(void* data)
4070 const char *str = (const char *)data;
4072 tt_assert(strlen(data) == 2);
4074 if (str[0] == 'b') {
4075 mock_networkstatus_consensus_is_bootstrapping_value = 1;
4076 } else {
4077 mock_networkstatus_consensus_is_bootstrapping_value = 0;
4080 if (str[1] == 'f') {
4081 mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
4082 } else {
4083 mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
4086 MOCK(networkstatus_consensus_is_bootstrapping,
4087 mock_networkstatus_consensus_is_bootstrapping);
4088 MOCK(networkstatus_consensus_can_use_extra_fallbacks,
4089 mock_networkstatus_consensus_can_use_extra_fallbacks);
4091 download_status_t dls;
4092 smartlist_t server, client, server_cons, client_cons;
4093 smartlist_t client_boot_auth_only_cons, client_boot_auth_cons;
4094 smartlist_t client_boot_fallback_cons, bridge;
4096 mock_options = malloc(sizeof(or_options_t));
4097 reset_options(mock_options, &mock_get_options_calls);
4098 MOCK(get_options, mock_get_options);
4100 mock_options->TestingServerDownloadSchedule = &server;
4101 mock_options->TestingClientDownloadSchedule = &client;
4102 mock_options->TestingServerConsensusDownloadSchedule = &server_cons;
4103 mock_options->TestingClientConsensusDownloadSchedule = &client_cons;
4104 mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule =
4105 &client_boot_auth_only_cons;
4106 mock_options->ClientBootstrapConsensusAuthorityDownloadSchedule =
4107 &client_boot_auth_cons;
4108 mock_options->ClientBootstrapConsensusFallbackDownloadSchedule =
4109 &client_boot_fallback_cons;
4110 mock_options->TestingBridgeDownloadSchedule = &bridge;
4112 dls.schedule = DL_SCHED_GENERIC;
4113 /* client */
4114 mock_options->ClientOnly = 1;
4115 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client);
4116 mock_options->ClientOnly = 0;
4118 /* dir mode */
4119 mock_options->DirPort_set = 1;
4120 mock_options->DirCache = 1;
4121 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server);
4122 mock_options->DirPort_set = 0;
4123 mock_options->DirCache = 0;
4125 dls.schedule = DL_SCHED_CONSENSUS;
4126 /* public server mode */
4127 mock_options->ORPort_set = 1;
4128 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server_cons);
4129 mock_options->ORPort_set = 0;
4131 /* client and bridge modes */
4132 if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
4133 if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
4134 dls.want_authority = 1;
4135 /* client */
4136 mock_options->ClientOnly = 1;
4137 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4138 &client_boot_auth_cons);
4139 mock_options->ClientOnly = 0;
4141 /* bridge relay */
4142 mock_options->ORPort_set = 1;
4143 mock_options->BridgeRelay = 1;
4144 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4145 &client_boot_auth_cons);
4146 mock_options->ORPort_set = 0;
4147 mock_options->BridgeRelay = 0;
4149 dls.want_authority = 0;
4150 /* client */
4151 mock_options->ClientOnly = 1;
4152 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4153 &client_boot_fallback_cons);
4154 mock_options->ClientOnly = 0;
4156 /* bridge relay */
4157 mock_options->ORPort_set = 1;
4158 mock_options->BridgeRelay = 1;
4159 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4160 &client_boot_fallback_cons);
4161 mock_options->ORPort_set = 0;
4162 mock_options->BridgeRelay = 0;
4164 } else {
4165 /* dls.want_authority is ignored */
4166 /* client */
4167 mock_options->ClientOnly = 1;
4168 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4169 &client_boot_auth_only_cons);
4170 mock_options->ClientOnly = 0;
4172 /* bridge relay */
4173 mock_options->ORPort_set = 1;
4174 mock_options->BridgeRelay = 1;
4175 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4176 &client_boot_auth_only_cons);
4177 mock_options->ORPort_set = 0;
4178 mock_options->BridgeRelay = 0;
4180 } else {
4181 /* client */
4182 mock_options->ClientOnly = 1;
4183 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4184 &client_cons);
4185 mock_options->ClientOnly = 0;
4187 /* bridge relay */
4188 mock_options->ORPort_set = 1;
4189 mock_options->BridgeRelay = 1;
4190 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4191 &client_cons);
4192 mock_options->ORPort_set = 0;
4193 mock_options->BridgeRelay = 0;
4196 dls.schedule = DL_SCHED_BRIDGE;
4197 /* client */
4198 mock_options->ClientOnly = 1;
4199 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
4201 done:
4202 UNMOCK(networkstatus_consensus_is_bootstrapping);
4203 UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
4204 UNMOCK(get_options);
4205 free(mock_options);
4206 mock_options = NULL;
4209 #define DIR_LEGACY(name) \
4210 { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
4212 #define DIR(name,flags) \
4213 { #name, test_dir_##name, (flags), NULL, NULL }
4215 /* where arg is a string constant */
4216 #define DIR_ARG(name,flags,arg) \
4217 { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
4219 struct testcase_t dir_tests[] = {
4220 DIR_LEGACY(nicknames),
4221 DIR_LEGACY(formats),
4222 DIR(routerinfo_parsing, 0),
4223 DIR(extrainfo_parsing, 0),
4224 DIR(parse_router_list, TT_FORK),
4225 DIR(load_routers, TT_FORK),
4226 DIR(load_extrainfo, TT_FORK),
4227 DIR_LEGACY(versions),
4228 DIR_LEGACY(fp_pairs),
4229 DIR(split_fps, 0),
4230 DIR_LEGACY(measured_bw_kb),
4231 DIR_LEGACY(measured_bw_kb_cache),
4232 DIR_LEGACY(param_voting),
4233 DIR_LEGACY(v3_networkstatus),
4234 DIR(random_weighted, 0),
4235 DIR(scale_bw, 0),
4236 DIR_LEGACY(clip_unmeasured_bw_kb),
4237 DIR_LEGACY(clip_unmeasured_bw_kb_alt),
4238 DIR(fmt_control_ns, 0),
4239 DIR(dirserv_set_routerstatus_testing, 0),
4240 DIR(http_handling, 0),
4241 DIR(purpose_needs_anonymity, 0),
4242 DIR(fetch_type, 0),
4243 DIR(packages, 0),
4244 DIR(download_status_schedule, 0),
4245 DIR(download_status_increment, 0),
4246 DIR(authdir_type_to_string, 0),
4247 DIR(conn_purpose_to_string, 0),
4248 DIR(should_use_directory_guards, 0),
4249 DIR(should_not_init_request_to_ourselves, TT_FORK),
4250 DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
4251 DIR(should_init_request_to_dir_auths, 0),
4252 DIR(choose_compression_level, 0),
4253 DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
4254 DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
4255 DIR_ARG(find_dl_schedule, TT_FORK, "cf"),
4256 DIR_ARG(find_dl_schedule, TT_FORK, "ca"),
4257 END_OF_TESTCASES