changelog typo fix
[tor.git] / src / test / test_dir.c
blobeca3082e4f05130dcd6dd0fab1eadccef8cf9f0c
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->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->signing_key_cert->encoded,
171 r2->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, &r2->signing_key_cert->signing_key)
283 >= 0);
284 strlcat(buf2, k, sizeof(buf2));
285 strlcat(buf2, "\n", sizeof(buf2));
287 strlcat(buf2, "platform Tor "VERSION" on ", sizeof(buf2));
288 strlcat(buf2, get_uname(), sizeof(buf2));
289 strlcat(buf2, "\n"
290 "protocols Link 1 2 Circuit 1\n"
291 "published 1970-01-01 00:00:05\n"
292 "fingerprint ", sizeof(buf2));
293 tt_assert(!crypto_pk_get_fingerprint(pk1, fingerprint, 1));
294 strlcat(buf2, fingerprint, sizeof(buf2));
295 strlcat(buf2, "\nuptime 0\n"
296 "bandwidth 3000 3000 3000\n", sizeof(buf2));
297 strlcat(buf2, "onion-key\n", sizeof(buf2));
298 strlcat(buf2, pk2_str, sizeof(buf2));
299 strlcat(buf2, "signing-key\n", sizeof(buf2));
300 strlcat(buf2, pk1_str, sizeof(buf2));
301 int rsa_cc_len;
302 rsa_cc = make_tap_onion_key_crosscert(pk2,
303 &kp1.pubkey,
304 pk1,
305 &rsa_cc_len);
306 tt_assert(rsa_cc);
307 base64_encode(cert_buf, sizeof(cert_buf), (char*)rsa_cc, rsa_cc_len,
308 BASE64_ENCODE_MULTILINE);
309 strlcat(buf2, "onion-key-crosscert\n"
310 "-----BEGIN CROSSCERT-----\n", sizeof(buf2));
311 strlcat(buf2, cert_buf, sizeof(buf2));
312 strlcat(buf2, "-----END CROSSCERT-----\n", sizeof(buf2));
313 int ntor_cc_sign;
315 tor_cert_t *ntor_cc = NULL;
316 ntor_cc = make_ntor_onion_key_crosscert(&r2_onion_keypair,
317 &kp1.pubkey,
318 r2->cache_info.published_on,
319 MIN_ONION_KEY_LIFETIME,
320 &ntor_cc_sign);
321 tt_assert(ntor_cc);
322 base64_encode(cert_buf, sizeof(cert_buf),
323 (char*)ntor_cc->encoded, ntor_cc->encoded_len,
324 BASE64_ENCODE_MULTILINE);
325 tor_cert_free(ntor_cc);
327 tor_snprintf(buf2+strlen(buf2), sizeof(buf2)-strlen(buf2),
328 "ntor-onion-key-crosscert %d\n"
329 "-----BEGIN ED25519 CERT-----\n"
330 "%s"
331 "-----END ED25519 CERT-----\n", ntor_cc_sign, cert_buf);
333 strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
334 strlcat(buf2, "ntor-onion-key ", sizeof(buf2));
335 base64_encode(cert_buf, sizeof(cert_buf),
336 (const char*)r2_onion_keypair.pubkey.public_key, 32,
337 BASE64_ENCODE_MULTILINE);
338 strlcat(buf2, cert_buf, sizeof(buf2));
339 strlcat(buf2, "accept *:80\nreject 18.0.0.0/8:24\n", sizeof(buf2));
340 strlcat(buf2, "tunnelled-dir-server\n", sizeof(buf2));
341 strlcat(buf2, "router-sig-ed25519 ", sizeof(buf2));
343 /* Fake just enough of an ORPort to get by */
344 MOCK(get_configured_ports, mock_get_configured_ports);
345 mocked_configured_ports = smartlist_new();
347 memset(&orport, 0, sizeof(orport));
348 orport.type = CONN_TYPE_OR_LISTENER;
349 orport.addr.family = AF_INET;
350 orport.port = 9005;
351 smartlist_add(mocked_configured_ports, &orport);
353 buf = router_dump_router_to_string(r2, pk1, pk2, &r2_onion_keypair, &kp2);
354 tt_assert(buf);
355 buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
356 * twice */
358 tt_str_op(buf, OP_EQ, buf2);
359 tor_free(buf);
361 buf = router_dump_router_to_string(r2, pk1, NULL, NULL, NULL);
363 UNMOCK(get_configured_ports);
364 smartlist_free(mocked_configured_ports);
365 mocked_configured_ports = NULL;
367 /* Reset for later */
368 cp = buf;
369 rp2 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL,NULL);
370 tt_assert(rp2);
371 tt_int_op(rp2->addr,OP_EQ, r2->addr);
372 tt_int_op(rp2->or_port,OP_EQ, r2->or_port);
373 tt_int_op(rp2->dir_port,OP_EQ, r2->dir_port);
374 tt_int_op(rp2->bandwidthrate,OP_EQ, r2->bandwidthrate);
375 tt_int_op(rp2->bandwidthburst,OP_EQ, r2->bandwidthburst);
376 tt_int_op(rp2->bandwidthcapacity,OP_EQ, r2->bandwidthcapacity);
377 tt_mem_op(rp2->onion_curve25519_pkey->public_key,OP_EQ,
378 r2->onion_curve25519_pkey->public_key,
379 CURVE25519_PUBKEY_LEN);
380 tt_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
381 tt_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
382 tt_assert(rp2->supports_tunnelled_dir_requests);
384 tt_int_op(smartlist_len(rp2->exit_policy),OP_EQ, 2);
386 p = smartlist_get(rp2->exit_policy, 0);
387 tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_ACCEPT);
388 tt_assert(tor_addr_is_null(&p->addr));
389 tt_int_op(p->maskbits,OP_EQ, 0);
390 tt_int_op(p->prt_min,OP_EQ, 80);
391 tt_int_op(p->prt_max,OP_EQ, 80);
393 p = smartlist_get(rp2->exit_policy, 1);
394 tt_int_op(p->policy_type,OP_EQ, ADDR_POLICY_REJECT);
395 tt_assert(tor_addr_eq(&p->addr, &ex2->addr));
396 tt_int_op(p->maskbits,OP_EQ, 8);
397 tt_int_op(p->prt_min,OP_EQ, 24);
398 tt_int_op(p->prt_max,OP_EQ, 24);
400 #if 0
401 /* Okay, now for the directories. */
403 fingerprint_list = smartlist_new();
404 crypto_pk_get_fingerprint(pk2, buf, 1);
405 add_fingerprint_to_dir(buf, fingerprint_list, 0);
406 crypto_pk_get_fingerprint(pk1, buf, 1);
407 add_fingerprint_to_dir(buf, fingerprint_list, 0);
410 #endif
411 dirserv_free_fingerprint_list();
413 done:
414 if (r1)
415 routerinfo_free(r1);
416 if (r2)
417 routerinfo_free(r2);
418 if (rp2)
419 routerinfo_free(rp2);
421 tor_free(rsa_cc);
422 tor_free(buf);
423 tor_free(pk1_str);
424 tor_free(pk2_str);
425 if (pk1) crypto_pk_free(pk1);
426 if (pk2) crypto_pk_free(pk2);
427 if (rp1) routerinfo_free(rp1);
428 tor_free(dir1); /* XXXX And more !*/
429 tor_free(dir2); /* And more !*/
432 #include "failing_routerdescs.inc"
434 static void
435 test_dir_routerinfo_parsing(void *arg)
437 (void) arg;
439 int again;
440 routerinfo_t *ri = NULL;
442 #define CHECK_OK(s) \
443 do { \
444 routerinfo_free(ri); \
445 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, NULL); \
446 tt_assert(ri); \
447 } while (0)
448 #define CHECK_FAIL(s, againval) \
449 do { \
450 routerinfo_free(ri); \
451 again = 999; \
452 ri = router_parse_entry_from_string((s), NULL, 0, 0, NULL, &again); \
453 tt_assert(ri == NULL); \
454 tt_int_op(again, OP_EQ, (againval)); \
455 } while (0)
457 CHECK_OK(EX_RI_MINIMAL);
458 CHECK_OK(EX_RI_MAXIMAL);
460 CHECK_OK(EX_RI_MINIMAL_ED);
462 /* good annotations prepended */
463 routerinfo_free(ri);
464 ri = router_parse_entry_from_string(EX_RI_MINIMAL, NULL, 0, 0,
465 "@purpose bridge\n", NULL);
466 tt_assert(ri != NULL);
467 tt_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
468 routerinfo_free(ri);
470 /* bad annotations prepended. */
471 ri = router_parse_entry_from_string(EX_RI_MINIMAL,
472 NULL, 0, 0, "@purpose\n", NULL);
473 tt_assert(ri == NULL);
475 /* bad annotations on router. */
476 ri = router_parse_entry_from_string("@purpose\nrouter x\n", NULL, 0, 1,
477 NULL, NULL);
478 tt_assert(ri == NULL);
480 /* unwanted annotations on router. */
481 ri = router_parse_entry_from_string("@purpose foo\nrouter x\n", NULL, 0, 0,
482 NULL, NULL);
483 tt_assert(ri == NULL);
485 /* No signature. */
486 ri = router_parse_entry_from_string("router x\n", NULL, 0, 0,
487 NULL, NULL);
488 tt_assert(ri == NULL);
490 /* Not a router */
491 routerinfo_free(ri);
492 ri = router_parse_entry_from_string("hello\n", NULL, 0, 0, NULL, NULL);
493 tt_assert(ri == NULL);
495 CHECK_FAIL(EX_RI_BAD_SIG1, 1);
496 CHECK_FAIL(EX_RI_BAD_SIG2, 1);
497 CHECK_FAIL(EX_RI_BAD_TOKENS, 0);
498 CHECK_FAIL(EX_RI_BAD_PUBLISHED, 0);
499 CHECK_FAIL(EX_RI_NEG_BANDWIDTH, 0);
500 CHECK_FAIL(EX_RI_BAD_BANDWIDTH, 0);
501 CHECK_FAIL(EX_RI_BAD_BANDWIDTH2, 0);
502 CHECK_FAIL(EX_RI_BAD_ONIONKEY1, 0);
503 CHECK_FAIL(EX_RI_BAD_ONIONKEY2, 0);
504 CHECK_FAIL(EX_RI_BAD_PORTS, 0);
505 CHECK_FAIL(EX_RI_BAD_IP, 0);
506 CHECK_FAIL(EX_RI_BAD_DIRPORT, 0);
507 CHECK_FAIL(EX_RI_BAD_NAME2, 0);
508 CHECK_FAIL(EX_RI_BAD_UPTIME, 0);
510 CHECK_FAIL(EX_RI_BAD_BANDWIDTH3, 0);
511 CHECK_FAIL(EX_RI_BAD_NTOR_KEY, 0);
512 CHECK_FAIL(EX_RI_BAD_FINGERPRINT, 0);
513 CHECK_FAIL(EX_RI_MISMATCHED_FINGERPRINT, 0);
514 CHECK_FAIL(EX_RI_BAD_HAS_ACCEPT6, 0);
515 CHECK_FAIL(EX_RI_BAD_NO_EXIT_POLICY, 0);
516 CHECK_FAIL(EX_RI_BAD_IPV6_EXIT_POLICY, 0);
517 CHECK_FAIL(EX_RI_BAD_FAMILY, 0);
518 CHECK_FAIL(EX_RI_ZERO_ORPORT, 0);
520 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT, 0);
521 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT2, 0);
522 CHECK_FAIL(EX_RI_ED_MISSING_CROSSCERT_SIGN, 0);
523 CHECK_FAIL(EX_RI_ED_BAD_SIG1, 0);
524 CHECK_FAIL(EX_RI_ED_BAD_SIG2, 0);
525 CHECK_FAIL(EX_RI_ED_BAD_SIG3, 0);
526 CHECK_FAIL(EX_RI_ED_BAD_SIG4, 0);
527 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT1, 0);
528 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT3, 0);
529 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT4, 0);
530 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT5, 0);
531 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT6, 0);
532 CHECK_FAIL(EX_RI_ED_BAD_CROSSCERT7, 0);
533 CHECK_FAIL(EX_RI_ED_MISPLACED1, 0);
534 CHECK_FAIL(EX_RI_ED_MISPLACED2, 0);
535 CHECK_FAIL(EX_RI_ED_BAD_CERT1, 0);
536 CHECK_FAIL(EX_RI_ED_BAD_CERT2, 0);
537 CHECK_FAIL(EX_RI_ED_BAD_CERT3, 0);
539 /* This is allowed; we just ignore it. */
540 CHECK_OK(EX_RI_BAD_EI_DIGEST);
541 CHECK_OK(EX_RI_BAD_EI_DIGEST2);
543 #undef CHECK_FAIL
544 #undef CHECK_OK
545 done:
546 routerinfo_free(ri);
549 #include "example_extrainfo.inc"
551 static void
552 routerinfo_free_wrapper_(void *arg)
554 routerinfo_free(arg);
557 static void
558 test_dir_extrainfo_parsing(void *arg)
560 (void) arg;
562 #define CHECK_OK(s) \
563 do { \
564 extrainfo_free(ei); \
565 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, NULL); \
566 tt_assert(ei); \
567 } while (0)
568 #define CHECK_FAIL(s, againval) \
569 do { \
570 extrainfo_free(ei); \
571 again = 999; \
572 ei = extrainfo_parse_entry_from_string((s), NULL, 0, map, &again); \
573 tt_assert(ei == NULL); \
574 tt_int_op(again, OP_EQ, (againval)); \
575 } while (0)
576 #define ADD(name) \
577 do { \
578 ri = tor_malloc_zero(sizeof(routerinfo_t)); \
579 crypto_pk_t *pk = ri->identity_pkey = crypto_pk_new(); \
580 tt_assert(! crypto_pk_read_public_key_from_string(pk, \
581 name##_KEY, strlen(name##_KEY))); \
582 tt_int_op(0,OP_EQ,base16_decode(d, 20, name##_FP, strlen(name##_FP))); \
583 digestmap_set((digestmap_t*)map, d, ri); \
584 ri = NULL; \
585 } while (0)
587 routerinfo_t *ri = NULL;
588 char d[20];
589 struct digest_ri_map_t *map = NULL;
590 extrainfo_t *ei = NULL;
591 int again;
593 CHECK_OK(EX_EI_MINIMAL);
594 tt_assert(ei->pending_sig);
595 CHECK_OK(EX_EI_MAXIMAL);
596 tt_assert(ei->pending_sig);
597 CHECK_OK(EX_EI_GOOD_ED_EI);
598 tt_assert(ei->pending_sig);
600 map = (struct digest_ri_map_t *)digestmap_new();
601 ADD(EX_EI_MINIMAL);
602 ADD(EX_EI_MAXIMAL);
603 ADD(EX_EI_GOOD_ED_EI);
604 ADD(EX_EI_BAD_FP);
605 ADD(EX_EI_BAD_NICKNAME);
606 ADD(EX_EI_BAD_TOKENS);
607 ADD(EX_EI_BAD_START);
608 ADD(EX_EI_BAD_PUBLISHED);
610 ADD(EX_EI_ED_MISSING_SIG);
611 ADD(EX_EI_ED_MISSING_CERT);
612 ADD(EX_EI_ED_BAD_CERT1);
613 ADD(EX_EI_ED_BAD_CERT2);
614 ADD(EX_EI_ED_BAD_SIG1);
615 ADD(EX_EI_ED_BAD_SIG2);
616 ADD(EX_EI_ED_MISPLACED_CERT);
617 ADD(EX_EI_ED_MISPLACED_SIG);
619 CHECK_OK(EX_EI_MINIMAL);
620 tt_assert(!ei->pending_sig);
621 CHECK_OK(EX_EI_MAXIMAL);
622 tt_assert(!ei->pending_sig);
623 CHECK_OK(EX_EI_GOOD_ED_EI);
624 tt_assert(!ei->pending_sig);
626 CHECK_FAIL(EX_EI_BAD_SIG1,1);
627 CHECK_FAIL(EX_EI_BAD_SIG2,1);
628 CHECK_FAIL(EX_EI_BAD_SIG3,1);
629 CHECK_FAIL(EX_EI_BAD_FP,0);
630 CHECK_FAIL(EX_EI_BAD_NICKNAME,0);
631 CHECK_FAIL(EX_EI_BAD_TOKENS,0);
632 CHECK_FAIL(EX_EI_BAD_START,0);
633 CHECK_FAIL(EX_EI_BAD_PUBLISHED,0);
635 CHECK_FAIL(EX_EI_ED_MISSING_SIG,0);
636 CHECK_FAIL(EX_EI_ED_MISSING_CERT,0);
637 CHECK_FAIL(EX_EI_ED_BAD_CERT1,0);
638 CHECK_FAIL(EX_EI_ED_BAD_CERT2,0);
639 CHECK_FAIL(EX_EI_ED_BAD_SIG1,0);
640 CHECK_FAIL(EX_EI_ED_BAD_SIG2,0);
641 CHECK_FAIL(EX_EI_ED_MISPLACED_CERT,0);
642 CHECK_FAIL(EX_EI_ED_MISPLACED_SIG,0);
644 #undef CHECK_OK
645 #undef CHECK_FAIL
647 done:
648 escaped(NULL);
649 extrainfo_free(ei);
650 routerinfo_free(ri);
651 digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
654 static void
655 test_dir_parse_router_list(void *arg)
657 (void) arg;
658 smartlist_t *invalid = smartlist_new();
659 smartlist_t *dest = smartlist_new();
660 smartlist_t *chunks = smartlist_new();
661 int dest_has_ri = 1;
662 char *list = NULL;
663 const char *cp;
664 digestmap_t *map = NULL;
665 char *mem_op_hex_tmp = NULL;
666 routerinfo_t *ri = NULL;
667 char d[DIGEST_LEN];
669 smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL)); // ri 0
670 smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS)); // bad ri 0
671 smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL)); // ei 0
672 smartlist_add(chunks, tor_strdup(EX_EI_BAD_SIG2)); // bad ei --
673 smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));// bad ei 0
674 smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG1)); // bad ri --
675 smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED)); // bad ei 1
676 smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL)); // ri 1
677 smartlist_add(chunks, tor_strdup(EX_RI_BAD_FAMILY)); // bad ri 1
678 smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL)); // ei 1
680 list = smartlist_join_strings(chunks, "", 0, NULL);
682 /* First, parse the routers. */
683 cp = list;
684 tt_int_op(0,OP_EQ,
685 router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
686 0, 0, NULL, invalid));
687 tt_int_op(2, OP_EQ, smartlist_len(dest));
688 tt_ptr_op(cp, OP_EQ, list + strlen(list));
690 routerinfo_t *r = smartlist_get(dest, 0);
691 tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
692 EX_RI_MINIMAL, strlen(EX_RI_MINIMAL));
693 r = smartlist_get(dest, 1);
694 tt_mem_op(r->cache_info.signed_descriptor_body, OP_EQ,
695 EX_RI_MAXIMAL, strlen(EX_RI_MAXIMAL));
697 tt_int_op(2, OP_EQ, smartlist_len(invalid));
698 test_memeq_hex(smartlist_get(invalid, 0),
699 "ab9eeaa95e7d45740185b4e519c76ead756277a9");
700 test_memeq_hex(smartlist_get(invalid, 1),
701 "9a651ee03b64325959e8f1b46f2b689b30750b4c");
703 /* Now tidy up */
704 SMARTLIST_FOREACH(dest, routerinfo_t *, ri, routerinfo_free(ri));
705 SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
706 smartlist_clear(dest);
707 smartlist_clear(invalid);
709 /* And check extrainfos. */
710 dest_has_ri = 0;
711 map = (digestmap_t*)router_get_routerlist()->identity_map;
712 ADD(EX_EI_MINIMAL);
713 ADD(EX_EI_MAXIMAL);
714 ADD(EX_EI_BAD_NICKNAME);
715 ADD(EX_EI_BAD_PUBLISHED);
716 cp = list;
717 tt_int_op(0,OP_EQ,
718 router_parse_list_from_string(&cp, NULL, dest, SAVED_NOWHERE,
719 1, 0, NULL, invalid));
720 tt_int_op(2, OP_EQ, smartlist_len(dest));
721 extrainfo_t *e = smartlist_get(dest, 0);
722 tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
723 EX_EI_MAXIMAL, strlen(EX_EI_MAXIMAL));
724 e = smartlist_get(dest, 1);
725 tt_mem_op(e->cache_info.signed_descriptor_body, OP_EQ,
726 EX_EI_MINIMAL, strlen(EX_EI_MINIMAL));
728 tt_int_op(2, OP_EQ, smartlist_len(invalid));
729 test_memeq_hex(smartlist_get(invalid, 0),
730 "d5df4aa62ee9ffc9543d41150c9864908e0390af");
731 test_memeq_hex(smartlist_get(invalid, 1),
732 "f61efd2a7f4531f3687a9043e0de90a862ec64ba");
734 done:
735 tor_free(list);
736 if (dest_has_ri)
737 SMARTLIST_FOREACH(dest, routerinfo_t *, rt, routerinfo_free(rt));
738 else
739 SMARTLIST_FOREACH(dest, extrainfo_t *, ei, extrainfo_free(ei));
740 smartlist_free(dest);
741 SMARTLIST_FOREACH(invalid, uint8_t *, d, tor_free(d));
742 smartlist_free(invalid);
743 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
744 smartlist_free(chunks);
745 routerinfo_free(ri);
746 if (map) {
747 digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
748 router_get_routerlist()->identity_map =
749 (struct digest_ri_map_t*)digestmap_new();
751 tor_free(mem_op_hex_tmp);
753 #undef ADD
756 static download_status_t dls_minimal;
757 static download_status_t dls_maximal;
758 static download_status_t dls_bad_fingerprint;
759 static download_status_t dls_bad_sig2;
760 static download_status_t dls_bad_ports;
761 static download_status_t dls_bad_tokens;
763 static int mock_router_get_dl_status_unrecognized = 0;
764 static int mock_router_get_dl_status_calls = 0;
766 static download_status_t *
767 mock_router_get_dl_status(const char *d)
769 ++mock_router_get_dl_status_calls;
770 char hex[HEX_DIGEST_LEN+1];
771 base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
772 if (!strcmp(hex, "3E31D19A69EB719C00B02EC60D13356E3F7A3452")) {
773 return &dls_minimal;
774 } else if (!strcmp(hex, "581D8A368A0FA854ECDBFAB841D88B3F1B004038")) {
775 return &dls_maximal;
776 } else if (!strcmp(hex, "2578AE227C6116CDE29B3F0E95709B9872DEE5F1")) {
777 return &dls_bad_fingerprint;
778 } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
779 return &dls_bad_sig2;
780 } else if (!strcmp(hex, "AB9EEAA95E7D45740185B4E519C76EAD756277A9")) {
781 return &dls_bad_ports;
782 } else if (!strcmp(hex, "A0CC2CEFAD59DBF19F468BFEE60E0868C804B422")) {
783 return &dls_bad_tokens;
784 } else {
785 ++mock_router_get_dl_status_unrecognized;
786 return NULL;
790 static void
791 test_dir_load_routers(void *arg)
793 (void) arg;
794 smartlist_t *chunks = smartlist_new();
795 smartlist_t *wanted = smartlist_new();
796 char buf[DIGEST_LEN];
797 char *mem_op_hex_tmp = NULL;
798 char *list = NULL;
800 #define ADD(str) \
801 do { \
802 tt_int_op(0,OP_EQ,router_get_router_hash(str, strlen(str), buf)); \
803 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
804 } while (0)
806 MOCK(router_get_dl_status_by_descriptor_digest, mock_router_get_dl_status);
808 update_approx_time(1412510400);
810 smartlist_add(chunks, tor_strdup(EX_RI_MINIMAL));
811 smartlist_add(chunks, tor_strdup(EX_RI_BAD_FINGERPRINT));
812 smartlist_add(chunks, tor_strdup(EX_RI_BAD_SIG2));
813 smartlist_add(chunks, tor_strdup(EX_RI_MAXIMAL));
814 smartlist_add(chunks, tor_strdup(EX_RI_BAD_PORTS));
815 smartlist_add(chunks, tor_strdup(EX_RI_BAD_TOKENS));
817 /* not ADDing MINIMIAL */
818 ADD(EX_RI_MAXIMAL);
819 ADD(EX_RI_BAD_FINGERPRINT);
820 ADD(EX_RI_BAD_SIG2);
821 /* Not ADDing BAD_PORTS */
822 ADD(EX_RI_BAD_TOKENS);
824 list = smartlist_join_strings(chunks, "", 0, NULL);
825 tt_int_op(1, OP_EQ,
826 router_load_routers_from_string(list, NULL, SAVED_IN_JOURNAL,
827 wanted, 1, NULL));
829 /* The "maximal" router was added. */
830 /* "minimal" was not. */
831 tt_int_op(smartlist_len(router_get_routerlist()->routers),OP_EQ,1);
832 routerinfo_t *r = smartlist_get(router_get_routerlist()->routers, 0);
833 test_memeq_hex(r->cache_info.signed_descriptor_digest,
834 "581D8A368A0FA854ECDBFAB841D88B3F1B004038");
835 tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
836 tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
838 /* "Bad fingerprint" and "Bad tokens" should have gotten marked
839 * non-retriable. */
840 tt_want_int_op(mock_router_get_dl_status_calls, OP_EQ, 2);
841 tt_want_int_op(mock_router_get_dl_status_unrecognized, OP_EQ, 0);
842 tt_int_op(dls_bad_fingerprint.n_download_failures, OP_EQ, 255);
843 tt_int_op(dls_bad_tokens.n_download_failures, OP_EQ, 255);
845 /* bad_sig2 and bad ports" are retriable -- one since only the signature
846 * was bad, and one because we didn't ask for it. */
847 tt_int_op(dls_bad_sig2.n_download_failures, OP_EQ, 0);
848 tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
850 /* Wanted still contains "BAD_SIG2" */
851 tt_int_op(smartlist_len(wanted), OP_EQ, 1);
852 tt_str_op(smartlist_get(wanted, 0), OP_EQ,
853 "E0A3753CEFD54128EAB239F294954121DB23D2EF");
855 #undef ADD
857 done:
858 tor_free(mem_op_hex_tmp);
859 UNMOCK(router_get_dl_status_by_descriptor_digest);
860 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
861 smartlist_free(chunks);
862 SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
863 smartlist_free(wanted);
864 tor_free(list);
867 static int mock_get_by_ei_dd_calls = 0;
868 static int mock_get_by_ei_dd_unrecognized = 0;
870 static signed_descriptor_t sd_ei_minimal;
871 static signed_descriptor_t sd_ei_bad_nickname;
872 static signed_descriptor_t sd_ei_maximal;
873 static signed_descriptor_t sd_ei_bad_tokens;
874 static signed_descriptor_t sd_ei_bad_sig2;
876 static signed_descriptor_t *
877 mock_get_by_ei_desc_digest(const char *d)
880 ++mock_get_by_ei_dd_calls;
881 char hex[HEX_DIGEST_LEN+1];
882 base16_encode(hex, sizeof(hex), d, DIGEST_LEN);
884 if (!strcmp(hex, "11E0EDF526950739F7769810FCACAB8C882FAEEE")) {
885 return &sd_ei_minimal;
886 } else if (!strcmp(hex, "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF")) {
887 return &sd_ei_maximal;
888 } else if (!strcmp(hex, "D5DF4AA62EE9FFC9543D41150C9864908E0390AF")) {
889 return &sd_ei_bad_nickname;
890 } else if (!strcmp(hex, "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C")) {
891 return &sd_ei_bad_sig2;
892 } else if (!strcmp(hex, "9D90F8C42955BBC57D54FB05E54A3F083AF42E8B")) {
893 return &sd_ei_bad_tokens;
894 } else {
895 ++mock_get_by_ei_dd_unrecognized;
896 return NULL;
900 static smartlist_t *mock_ei_insert_list = NULL;
901 static was_router_added_t
902 mock_ei_insert(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)
904 (void) rl;
905 (void) warn_if_incompatible;
906 smartlist_add(mock_ei_insert_list, ei);
907 return ROUTER_ADDED_SUCCESSFULLY;
910 static void
911 test_dir_load_extrainfo(void *arg)
913 (void) arg;
914 smartlist_t *chunks = smartlist_new();
915 smartlist_t *wanted = smartlist_new();
916 char buf[DIGEST_LEN];
917 char *mem_op_hex_tmp = NULL;
918 char *list = NULL;
920 #define ADD(str) \
921 do { \
922 tt_int_op(0,OP_EQ,router_get_extrainfo_hash(str, strlen(str), buf)); \
923 smartlist_add(wanted, tor_strdup(hex_str(buf, DIGEST_LEN))); \
924 } while (0)
926 mock_ei_insert_list = smartlist_new();
927 MOCK(router_get_by_extrainfo_digest, mock_get_by_ei_desc_digest);
928 MOCK(extrainfo_insert, mock_ei_insert);
930 smartlist_add(chunks, tor_strdup(EX_EI_MINIMAL));
931 smartlist_add(chunks, tor_strdup(EX_EI_BAD_NICKNAME));
932 smartlist_add(chunks, tor_strdup(EX_EI_MAXIMAL));
933 smartlist_add(chunks, tor_strdup(EX_EI_BAD_PUBLISHED));
934 smartlist_add(chunks, tor_strdup(EX_EI_BAD_TOKENS));
936 /* not ADDing MINIMIAL */
937 ADD(EX_EI_MAXIMAL);
938 ADD(EX_EI_BAD_NICKNAME);
939 /* Not ADDing BAD_PUBLISHED */
940 ADD(EX_EI_BAD_TOKENS);
941 ADD(EX_EI_BAD_SIG2);
943 list = smartlist_join_strings(chunks, "", 0, NULL);
944 router_load_extrainfo_from_string(list, NULL, SAVED_IN_JOURNAL, wanted, 1);
946 /* The "maximal" router was added. */
947 /* "minimal" was also added, even though we didn't ask for it, since
948 * that's what we do with extrainfos. */
949 tt_int_op(smartlist_len(mock_ei_insert_list),OP_EQ,2);
951 extrainfo_t *e = smartlist_get(mock_ei_insert_list, 0);
952 test_memeq_hex(e->cache_info.signed_descriptor_digest,
953 "11E0EDF526950739F7769810FCACAB8C882FAEEE");
955 e = smartlist_get(mock_ei_insert_list, 1);
956 test_memeq_hex(e->cache_info.signed_descriptor_digest,
957 "47803B02A0E70E9E8BDA226CB1D74DE354D67DFF");
958 tt_int_op(dls_minimal.n_download_failures, OP_EQ, 0);
959 tt_int_op(dls_maximal.n_download_failures, OP_EQ, 0);
961 /* "Bad nickname" and "Bad tokens" should have gotten marked
962 * non-retriable. */
963 tt_want_int_op(mock_get_by_ei_dd_calls, OP_EQ, 2);
964 tt_want_int_op(mock_get_by_ei_dd_unrecognized, OP_EQ, 0);
965 tt_int_op(sd_ei_bad_nickname.ei_dl_status.n_download_failures, OP_EQ, 255);
966 tt_int_op(sd_ei_bad_tokens.ei_dl_status.n_download_failures, OP_EQ, 255);
968 /* bad_ports is retriable -- because we didn't ask for it. */
969 tt_int_op(dls_bad_ports.n_download_failures, OP_EQ, 0);
971 /* Wanted still contains "BAD_SIG2" */
972 tt_int_op(smartlist_len(wanted), OP_EQ, 1);
973 tt_str_op(smartlist_get(wanted, 0), OP_EQ,
974 "16D387D3A58F7DB3CF46638F8D0B90C45C7D769C");
976 #undef ADD
978 done:
979 tor_free(mem_op_hex_tmp);
980 UNMOCK(router_get_by_extrainfo_digest);
981 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
982 smartlist_free(chunks);
983 SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp));
984 smartlist_free(wanted);
985 tor_free(list);
988 static void
989 test_dir_versions(void *arg)
991 tor_version_t ver1;
993 /* Try out version parsing functionality */
994 (void)arg;
995 tt_int_op(0,OP_EQ, tor_version_parse("0.3.4pre2-cvs", &ver1));
996 tt_int_op(0,OP_EQ, ver1.major);
997 tt_int_op(3,OP_EQ, ver1.minor);
998 tt_int_op(4,OP_EQ, ver1.micro);
999 tt_int_op(VER_PRE,OP_EQ, ver1.status);
1000 tt_int_op(2,OP_EQ, ver1.patchlevel);
1001 tt_int_op(0,OP_EQ, tor_version_parse("0.3.4rc1", &ver1));
1002 tt_int_op(0,OP_EQ, ver1.major);
1003 tt_int_op(3,OP_EQ, ver1.minor);
1004 tt_int_op(4,OP_EQ, ver1.micro);
1005 tt_int_op(VER_RC,OP_EQ, ver1.status);
1006 tt_int_op(1,OP_EQ, ver1.patchlevel);
1007 tt_int_op(0,OP_EQ, tor_version_parse("1.3.4", &ver1));
1008 tt_int_op(1,OP_EQ, ver1.major);
1009 tt_int_op(3,OP_EQ, ver1.minor);
1010 tt_int_op(4,OP_EQ, ver1.micro);
1011 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1012 tt_int_op(0,OP_EQ, ver1.patchlevel);
1013 tt_int_op(0,OP_EQ, tor_version_parse("1.3.4.999", &ver1));
1014 tt_int_op(1,OP_EQ, ver1.major);
1015 tt_int_op(3,OP_EQ, ver1.minor);
1016 tt_int_op(4,OP_EQ, ver1.micro);
1017 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1018 tt_int_op(999,OP_EQ, ver1.patchlevel);
1019 tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4-alpha", &ver1));
1020 tt_int_op(0,OP_EQ, ver1.major);
1021 tt_int_op(1,OP_EQ, ver1.minor);
1022 tt_int_op(2,OP_EQ, ver1.micro);
1023 tt_int_op(4,OP_EQ, ver1.patchlevel);
1024 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1025 tt_str_op("alpha",OP_EQ, ver1.status_tag);
1026 tt_int_op(0,OP_EQ, tor_version_parse("0.1.2.4", &ver1));
1027 tt_int_op(0,OP_EQ, ver1.major);
1028 tt_int_op(1,OP_EQ, ver1.minor);
1029 tt_int_op(2,OP_EQ, ver1.micro);
1030 tt_int_op(4,OP_EQ, ver1.patchlevel);
1031 tt_int_op(VER_RELEASE,OP_EQ, ver1.status);
1032 tt_str_op("",OP_EQ, ver1.status_tag);
1034 tt_int_op(0, OP_EQ, tor_version_parse("10.1", &ver1));
1035 tt_int_op(10, OP_EQ, ver1.major);
1036 tt_int_op(1, OP_EQ, ver1.minor);
1037 tt_int_op(0, OP_EQ, ver1.micro);
1038 tt_int_op(0, OP_EQ, ver1.patchlevel);
1039 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1040 tt_str_op("", OP_EQ, ver1.status_tag);
1041 tt_int_op(0, OP_EQ, tor_version_parse("5.99.999", &ver1));
1042 tt_int_op(5, OP_EQ, ver1.major);
1043 tt_int_op(99, OP_EQ, ver1.minor);
1044 tt_int_op(999, OP_EQ, ver1.micro);
1045 tt_int_op(0, OP_EQ, ver1.patchlevel);
1046 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1047 tt_str_op("", OP_EQ, ver1.status_tag);
1048 tt_int_op(0, OP_EQ, tor_version_parse("10.1-alpha", &ver1));
1049 tt_int_op(10, OP_EQ, ver1.major);
1050 tt_int_op(1, OP_EQ, ver1.minor);
1051 tt_int_op(0, OP_EQ, ver1.micro);
1052 tt_int_op(0, OP_EQ, ver1.patchlevel);
1053 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1054 tt_str_op("alpha", OP_EQ, ver1.status_tag);
1055 tt_int_op(0, OP_EQ, tor_version_parse("2.1.700-alpha", &ver1));
1056 tt_int_op(2, OP_EQ, ver1.major);
1057 tt_int_op(1, OP_EQ, ver1.minor);
1058 tt_int_op(700, OP_EQ, ver1.micro);
1059 tt_int_op(0, OP_EQ, ver1.patchlevel);
1060 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1061 tt_str_op("alpha", OP_EQ, ver1.status_tag);
1062 tt_int_op(0, OP_EQ, tor_version_parse("1.6.8-alpha-dev", &ver1));
1063 tt_int_op(1, OP_EQ, ver1.major);
1064 tt_int_op(6, OP_EQ, ver1.minor);
1065 tt_int_op(8, OP_EQ, ver1.micro);
1066 tt_int_op(0, OP_EQ, ver1.patchlevel);
1067 tt_int_op(VER_RELEASE, OP_EQ, ver1.status);
1068 tt_str_op("alpha-dev", OP_EQ, ver1.status_tag);
1070 #define tt_versionstatus_op(vs1, op, vs2) \
1071 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
1072 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
1073 #define test_v_i_o(val, ver, lst) \
1074 tt_versionstatus_op(val, OP_EQ, tor_version_is_obsolete(ver, lst))
1076 /* make sure tor_version_is_obsolete() works */
1077 test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
1078 test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
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.3,BetterTor 0.0.1");
1081 test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
1082 test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
1083 test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
1084 test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
1085 test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
1086 test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
1087 test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
1088 /* Not on list, but newer than any in same series. */
1089 test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
1090 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1091 /* Series newer than any on list. */
1092 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");
1093 /* Series older than any on list. */
1094 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");
1095 /* Not on list, not newer than any on same series. */
1096 test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
1097 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1098 /* On list, not newer than any on same series. */
1099 test_v_i_o(VS_UNRECOMMENDED,
1100 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1101 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1102 tt_int_op(1,OP_EQ, tor_version_as_new_as(
1103 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1104 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
1105 "0.0.8rc2"));
1106 tt_int_op(0,OP_EQ, tor_version_as_new_as(
1107 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1108 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1110 /* Now try svn revisions. */
1111 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1112 "Tor 0.2.1.0-dev (r99)"));
1113 tt_int_op(1,OP_EQ, tor_version_as_new_as(
1114 "Tor 0.2.1.0-dev (r100) on Banana Jr",
1115 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
1116 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
1117 "Tor 0.2.1.0-dev on Colossus"));
1118 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
1119 "Tor 0.2.1.0-dev (r100)"));
1120 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
1121 "Tor 0.2.1.0-dev (r100) on AM"));
1122 tt_int_op(0,OP_EQ, tor_version_as_new_as("Tor 0.2.1.0-dev",
1123 "Tor 0.2.1.0-dev (r99)"));
1124 tt_int_op(1,OP_EQ, tor_version_as_new_as("Tor 0.2.1.1",
1125 "Tor 0.2.1.0-dev (r99)"));
1127 /* Now try git revisions */
1128 tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
1129 tt_int_op(0,OP_EQ, ver1.major);
1130 tt_int_op(5,OP_EQ, ver1.minor);
1131 tt_int_op(6,OP_EQ, ver1.micro);
1132 tt_int_op(7,OP_EQ, ver1.patchlevel);
1133 tt_int_op(3,OP_EQ, ver1.git_tag_len);
1134 tt_mem_op(ver1.git_tag,OP_EQ, "\xff\x00\xff", 3);
1135 tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
1136 tt_int_op(-1,OP_EQ, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
1137 tt_int_op(0,OP_EQ, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
1138 done:
1142 /** Run unit tests for directory fp_pair functions. */
1143 static void
1144 test_dir_fp_pairs(void *arg)
1146 smartlist_t *sl = smartlist_new();
1147 fp_pair_t *pair;
1149 (void)arg;
1150 dir_split_resource_into_fingerprint_pairs(
1151 /* Two pairs, out of order, with one duplicate. */
1152 "73656372657420646174612E0000000000FFFFFF-"
1153 "557365204145532d32353620696e73746561642e+"
1154 "73656372657420646174612E0000000000FFFFFF-"
1155 "557365204145532d32353620696e73746561642e+"
1156 "48657861646563696d616c2069736e277420736f-"
1157 "676f6f6420666f7220686964696e6720796f7572.z", sl);
1159 tt_int_op(smartlist_len(sl),OP_EQ, 2);
1160 pair = smartlist_get(sl, 0);
1161 tt_mem_op(pair->first,OP_EQ, "Hexadecimal isn't so", DIGEST_LEN);
1162 tt_mem_op(pair->second,OP_EQ, "good for hiding your", DIGEST_LEN);
1163 pair = smartlist_get(sl, 1);
1164 tt_mem_op(pair->first,OP_EQ, "secret data.\0\0\0\0\0\xff\xff\xff",
1165 DIGEST_LEN);
1166 tt_mem_op(pair->second,OP_EQ, "Use AES-256 instead.", DIGEST_LEN);
1168 done:
1169 SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
1170 smartlist_free(sl);
1173 static void
1174 test_dir_split_fps(void *testdata)
1176 smartlist_t *sl = smartlist_new();
1177 char *mem_op_hex_tmp = NULL;
1178 (void)testdata;
1180 /* Some example hex fingerprints and their base64 equivalents */
1181 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1182 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1183 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
1184 #define HEX256_1 \
1185 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
1186 #define HEX256_2 \
1187 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
1188 #define HEX256_3 \
1189 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
1190 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
1191 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
1192 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
1193 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
1195 /* no flags set */
1196 dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
1197 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1198 tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
1199 tt_str_op(smartlist_get(sl, 1), OP_EQ, "C");
1200 tt_str_op(smartlist_get(sl, 2), OP_EQ, "B");
1201 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1202 smartlist_clear(sl);
1204 /* uniq strings. */
1205 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
1206 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1207 tt_str_op(smartlist_get(sl, 0), OP_EQ, "A");
1208 tt_str_op(smartlist_get(sl, 1), OP_EQ, "B");
1209 tt_str_op(smartlist_get(sl, 2), OP_EQ, "C");
1210 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1211 smartlist_clear(sl);
1213 /* Decode hex. */
1214 dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
1215 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1216 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1217 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1218 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1219 smartlist_clear(sl);
1221 /* decode hex and drop weirdness. */
1222 dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
1223 sl, NULL, DSR_HEX);
1224 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1225 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1226 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1227 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1228 smartlist_clear(sl);
1230 /* Decode long hex */
1231 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
1232 sl, NULL, DSR_HEX|DSR_DIGEST256);
1233 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1234 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1235 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1236 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_3);
1237 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1238 smartlist_clear(sl);
1240 /* Decode hex and sort. */
1241 dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
1242 sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
1243 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1244 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX3);
1245 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1246 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX1);
1247 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1248 smartlist_clear(sl);
1250 /* Decode long hex and sort */
1251 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
1252 "+"HEX256_1,
1253 sl, NULL,
1254 DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
1255 tt_int_op(smartlist_len(sl), OP_EQ, 3);
1256 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_3);
1257 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1258 test_mem_op_hex(smartlist_get(sl, 2), OP_EQ, HEX256_1);
1259 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1260 smartlist_clear(sl);
1262 /* Decode base64 */
1263 dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
1264 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1265 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX1);
1266 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX2);
1267 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1268 smartlist_clear(sl);
1270 /* Decode long base64 */
1271 dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
1272 sl, NULL, DSR_BASE64|DSR_DIGEST256);
1273 tt_int_op(smartlist_len(sl), OP_EQ, 2);
1274 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1275 test_mem_op_hex(smartlist_get(sl, 1), OP_EQ, HEX256_2);
1276 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1277 smartlist_clear(sl);
1279 dir_split_resource_into_fingerprints(B64_256_1,
1280 sl, NULL, DSR_BASE64|DSR_DIGEST256);
1281 tt_int_op(smartlist_len(sl), OP_EQ, 1);
1282 test_mem_op_hex(smartlist_get(sl, 0), OP_EQ, HEX256_1);
1283 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1284 smartlist_clear(sl);
1286 done:
1287 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
1288 smartlist_free(sl);
1289 tor_free(mem_op_hex_tmp);
1292 static void
1293 test_dir_measured_bw_kb(void *arg)
1295 measured_bw_line_t mbwl;
1296 int i;
1297 const char *lines_pass[] = {
1298 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1299 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
1300 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
1301 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
1302 "bw=1024 junk=007\n",
1303 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
1304 "bw=1024 junk=007\n",
1305 "end"
1307 const char *lines_fail[] = {
1308 /* Test possible python stupidity on input */
1309 "node_id=None bw=1024\n",
1310 "node_id=$None bw=1024\n",
1311 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
1312 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
1313 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
1314 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
1315 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
1316 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
1317 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
1318 /* Test incomplete writes due to race conditions, partial copies, etc */
1319 "node_i",
1320 "node_i\n",
1321 "node_id=",
1322 "node_id=\n",
1323 "node_id=$557365204145532d32353620696e73746561642e bw=",
1324 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
1325 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
1326 "node_id=$557365204145532d32353620696e7374",
1327 "node_id=$557365204145532d32353620696e7374\n",
1329 "\n",
1330 " \n ",
1331 " \n\n",
1332 /* Test assorted noise */
1333 " node_id= ",
1334 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
1335 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
1336 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
1337 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
1338 "end"
1341 (void)arg;
1342 for (i = 0; strcmp(lines_fail[i], "end"); i++) {
1343 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
1344 tt_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
1347 for (i = 0; strcmp(lines_pass[i], "end"); i++) {
1348 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
1349 tt_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
1350 tt_assert(mbwl.bw_kb == 1024);
1351 tt_assert(strcmp(mbwl.node_hex,
1352 "557365204145532d32353620696e73746561642e") == 0);
1355 done:
1356 return;
1359 #define MBWC_INIT_TIME 1000
1361 /** Do the measured bandwidth cache unit test */
1362 static void
1363 test_dir_measured_bw_kb_cache(void *arg)
1365 /* Initial fake time_t for testing */
1366 time_t curr = MBWC_INIT_TIME;
1367 /* Some measured_bw_line_ts */
1368 measured_bw_line_t mbwl[3];
1369 /* For receiving output on cache queries */
1370 long bw;
1371 time_t as_of;
1373 /* First, clear the cache and assert that it's empty */
1374 (void)arg;
1375 dirserv_clear_measured_bw_cache();
1376 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1378 * Set up test mbwls; none of the dirserv_cache_*() functions care about
1379 * the node_hex field.
1381 memset(mbwl[0].node_id, 0x01, DIGEST_LEN);
1382 mbwl[0].bw_kb = 20;
1383 memset(mbwl[1].node_id, 0x02, DIGEST_LEN);
1384 mbwl[1].bw_kb = 40;
1385 memset(mbwl[2].node_id, 0x03, DIGEST_LEN);
1386 mbwl[2].bw_kb = 80;
1387 /* Try caching something */
1388 dirserv_cache_measured_bw(&(mbwl[0]), curr);
1389 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1390 /* Okay, let's see if we can retrieve it */
1391 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, &as_of));
1392 tt_int_op(bw,OP_EQ, 20);
1393 tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
1394 /* Try retrieving it without some outputs */
1395 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL, NULL));
1396 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,&bw, NULL));
1397 tt_int_op(bw,OP_EQ, 20);
1398 tt_assert(dirserv_query_measured_bw_cache_kb(mbwl[0].node_id,NULL,&as_of));
1399 tt_int_op(as_of,OP_EQ, MBWC_INIT_TIME);
1400 /* Now expire it */
1401 curr += MAX_MEASUREMENT_AGE + 1;
1402 dirserv_expire_measured_bw_cache(curr);
1403 /* Check that the cache is empty */
1404 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1405 /* Check that we can't retrieve it */
1406 tt_assert(!dirserv_query_measured_bw_cache_kb(mbwl[0].node_id, NULL,NULL));
1407 /* Try caching a few things now */
1408 dirserv_cache_measured_bw(&(mbwl[0]), curr);
1409 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1410 curr += MAX_MEASUREMENT_AGE / 4;
1411 dirserv_cache_measured_bw(&(mbwl[1]), curr);
1412 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
1413 curr += MAX_MEASUREMENT_AGE / 4;
1414 dirserv_cache_measured_bw(&(mbwl[2]), curr);
1415 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
1416 curr += MAX_MEASUREMENT_AGE / 4 + 1;
1417 /* Do an expire that's too soon to get any of them */
1418 dirserv_expire_measured_bw_cache(curr);
1419 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 3);
1420 /* Push the oldest one off the cliff */
1421 curr += MAX_MEASUREMENT_AGE / 4;
1422 dirserv_expire_measured_bw_cache(curr);
1423 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 2);
1424 /* And another... */
1425 curr += MAX_MEASUREMENT_AGE / 4;
1426 dirserv_expire_measured_bw_cache(curr);
1427 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 1);
1428 /* This should empty it out again */
1429 curr += MAX_MEASUREMENT_AGE / 4;
1430 dirserv_expire_measured_bw_cache(curr);
1431 tt_int_op(dirserv_get_measured_bw_cache_size(),OP_EQ, 0);
1433 done:
1434 return;
1437 static void
1438 test_dir_param_voting(void *arg)
1440 networkstatus_t vote1, vote2, vote3, vote4;
1441 smartlist_t *votes = smartlist_new();
1442 char *res = NULL;
1444 /* dirvote_compute_params only looks at the net_params field of the votes,
1445 so that's all we need to set.
1447 (void)arg;
1448 memset(&vote1, 0, sizeof(vote1));
1449 memset(&vote2, 0, sizeof(vote2));
1450 memset(&vote3, 0, sizeof(vote3));
1451 memset(&vote4, 0, sizeof(vote4));
1452 vote1.net_params = smartlist_new();
1453 vote2.net_params = smartlist_new();
1454 vote3.net_params = smartlist_new();
1455 vote4.net_params = smartlist_new();
1456 smartlist_split_string(vote1.net_params,
1457 "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
1458 smartlist_split_string(vote2.net_params,
1459 "ab=27 cw=5 x-yz=88", NULL, 0, 0);
1460 smartlist_split_string(vote3.net_params,
1461 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
1462 smartlist_split_string(vote4.net_params,
1463 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
1464 tt_int_op(100,OP_EQ, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
1465 tt_int_op(222,OP_EQ, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
1466 tt_int_op(80,OP_EQ, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
1467 tt_int_op(-8,OP_EQ, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
1468 tt_int_op(0,OP_EQ, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
1470 smartlist_add(votes, &vote1);
1472 /* Do the first tests without adding all the other votes, for
1473 * networks without many dirauths. */
1475 res = dirvote_compute_params(votes, 12, 2);
1476 tt_str_op(res,OP_EQ, "");
1477 tor_free(res);
1479 res = dirvote_compute_params(votes, 12, 1);
1480 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-99");
1481 tor_free(res);
1483 smartlist_add(votes, &vote2);
1485 res = dirvote_compute_params(votes, 12, 2);
1486 tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
1487 tor_free(res);
1489 res = dirvote_compute_params(votes, 12, 3);
1490 tt_str_op(res,OP_EQ, "ab=27 cw=5 x-yz=-99");
1491 tor_free(res);
1493 res = dirvote_compute_params(votes, 12, 6);
1494 tt_str_op(res,OP_EQ, "");
1495 tor_free(res);
1497 smartlist_add(votes, &vote3);
1499 res = dirvote_compute_params(votes, 12, 3);
1500 tt_str_op(res,OP_EQ, "ab=27 abcd=20 cw=50 x-yz=-9");
1501 tor_free(res);
1503 res = dirvote_compute_params(votes, 12, 5);
1504 tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
1505 tor_free(res);
1507 res = dirvote_compute_params(votes, 12, 9);
1508 tt_str_op(res,OP_EQ, "cw=50 x-yz=-9");
1509 tor_free(res);
1511 smartlist_add(votes, &vote4);
1513 res = dirvote_compute_params(votes, 12, 4);
1514 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1515 tor_free(res);
1517 res = dirvote_compute_params(votes, 12, 5);
1518 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1519 tor_free(res);
1521 /* Test that the special-cased "at least three dirauths voted for
1522 * this param" logic works as expected. */
1523 res = dirvote_compute_params(votes, 12, 6);
1524 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1525 tor_free(res);
1527 res = dirvote_compute_params(votes, 12, 10);
1528 tt_str_op(res,OP_EQ, "ab=90 abcd=20 cw=50 x-yz=-9");
1529 tor_free(res);
1531 done:
1532 tor_free(res);
1533 SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
1534 SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
1535 SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
1536 SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
1537 smartlist_free(vote1.net_params);
1538 smartlist_free(vote2.net_params);
1539 smartlist_free(vote3.net_params);
1540 smartlist_free(vote4.net_params);
1541 smartlist_free(votes);
1543 return;
1546 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
1547 * same voting authority, and that they do in fact have all the same
1548 * information. */
1549 static void
1550 test_same_voter(networkstatus_voter_info_t *v1,
1551 networkstatus_voter_info_t *v2)
1553 tt_str_op(v1->nickname,OP_EQ, v2->nickname);
1554 tt_mem_op(v1->identity_digest,OP_EQ, v2->identity_digest, DIGEST_LEN);
1555 tt_str_op(v1->address,OP_EQ, v2->address);
1556 tt_int_op(v1->addr,OP_EQ, v2->addr);
1557 tt_int_op(v1->dir_port,OP_EQ, v2->dir_port);
1558 tt_int_op(v1->or_port,OP_EQ, v2->or_port);
1559 tt_str_op(v1->contact,OP_EQ, v2->contact);
1560 tt_mem_op(v1->vote_digest,OP_EQ, v2->vote_digest, DIGEST_LEN);
1561 done:
1565 /** Helper: get a detached signatures document for one or two
1566 * consensuses. */
1567 static char *
1568 get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
1570 char *r;
1571 smartlist_t *sl;
1572 tor_assert(ns && ns->flavor == FLAV_NS);
1573 sl = smartlist_new();
1574 smartlist_add(sl,ns);
1575 if (ns2)
1576 smartlist_add(sl,ns2);
1577 r = networkstatus_get_detached_signatures(sl);
1578 smartlist_free(sl);
1579 return r;
1582 /** Apply tweaks to the vote list for each voter */
1583 static int
1584 vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
1586 vote_routerstatus_t *vrs;
1587 const char *msg = NULL;
1589 tt_assert(v);
1590 (void)now;
1592 if (voter == 1) {
1593 measured_bw_line_t mbw;
1594 memset(mbw.node_id, 33, sizeof(mbw.node_id));
1595 mbw.bw_kb = 1024;
1596 tt_assert(measured_bw_line_apply(&mbw,
1597 v->routerstatus_list) == 1);
1598 } else if (voter == 2 || voter == 3) {
1599 /* Monkey around with the list a bit */
1600 vrs = smartlist_get(v->routerstatus_list, 2);
1601 smartlist_del_keeporder(v->routerstatus_list, 2);
1602 vote_routerstatus_free(vrs);
1603 vrs = smartlist_get(v->routerstatus_list, 0);
1604 vrs->status.is_fast = 1;
1606 if (voter == 3) {
1607 vrs = smartlist_get(v->routerstatus_list, 0);
1608 smartlist_del_keeporder(v->routerstatus_list, 0);
1609 vote_routerstatus_free(vrs);
1610 vrs = smartlist_get(v->routerstatus_list, 0);
1611 memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
1612 tt_assert(router_add_to_routerlist(
1613 dir_common_generate_ri_from_rs(vrs), &msg,0,0) >= 0);
1617 done:
1618 return 0;
1622 * Test a parsed vote_routerstatus_t for v3_networkstatus test
1624 static void
1625 test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
1627 routerstatus_t *rs;
1628 tor_addr_t addr_ipv6;
1630 tt_assert(vrs);
1631 rs = &(vrs->status);
1632 tt_assert(rs);
1634 /* Split out by digests to test */
1635 if (tor_memeq(rs->identity_digest,
1636 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1637 "\x3\x3\x3\x3",
1638 DIGEST_LEN) &&
1639 (voter == 1)) {
1640 /* Check the first routerstatus. */
1641 tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
1642 tt_int_op(rs->published_on,OP_EQ, now-1500);
1643 tt_str_op(rs->nickname,OP_EQ, "router2");
1644 tt_mem_op(rs->identity_digest,OP_EQ,
1645 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1646 "\x3\x3\x3\x3",
1647 DIGEST_LEN);
1648 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1649 tt_int_op(rs->addr,OP_EQ, 0x99008801);
1650 tt_int_op(rs->or_port,OP_EQ, 443);
1651 tt_int_op(rs->dir_port,OP_EQ, 8000);
1652 /* no flags except "running" (16) and "v2dir" (64) */
1653 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(80));
1654 } else if (tor_memeq(rs->identity_digest,
1655 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1656 "\x5\x5\x5\x5",
1657 DIGEST_LEN) &&
1658 (voter == 1 || voter == 2)) {
1659 tt_mem_op(rs->identity_digest,OP_EQ,
1660 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1661 "\x5\x5\x5\x5",
1662 DIGEST_LEN);
1664 if (voter == 1) {
1665 /* Check the second routerstatus. */
1666 tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
1667 tt_int_op(rs->published_on,OP_EQ, now-1000);
1668 tt_str_op(rs->nickname,OP_EQ, "router1");
1670 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1671 tt_int_op(rs->addr,OP_EQ, 0x99009901);
1672 tt_int_op(rs->or_port,OP_EQ, 443);
1673 tt_int_op(rs->dir_port,OP_EQ, 0);
1674 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1675 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1676 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
1677 if (voter == 1) {
1678 /* all except "authority" (1) */
1679 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(254));
1680 } else {
1681 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
1682 tt_u64_op(vrs->flags, OP_EQ, U64_LITERAL(974));
1684 } else if (tor_memeq(rs->identity_digest,
1685 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
1686 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
1687 DIGEST_LEN) &&
1688 (voter == 1 || voter == 2)) {
1689 /* Check the measured bandwidth bits */
1690 tt_assert(vrs->has_measured_bw &&
1691 vrs->measured_bw_kb == 1024);
1692 } else {
1694 * Didn't expect this, but the old unit test only checked some of them,
1695 * so don't assert.
1697 /* tt_assert(0); */
1700 done:
1701 return;
1705 * Test a consensus for v3_networkstatus_test
1707 static void
1708 test_consensus_for_v3ns(networkstatus_t *con, time_t now)
1710 (void)now;
1712 tt_assert(con);
1713 tt_assert(!con->cert);
1714 tt_int_op(2,OP_EQ, smartlist_len(con->routerstatus_list));
1715 /* There should be two listed routers: one with identity 3, one with
1716 * identity 5. */
1718 done:
1719 return;
1723 * Test a router list entry for v3_networkstatus test
1725 static void
1726 test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
1728 tor_addr_t addr_ipv6;
1730 tt_assert(rs);
1732 /* There should be two listed routers: one with identity 3, one with
1733 * identity 5. */
1734 /* This one showed up in 2 digests. */
1735 if (tor_memeq(rs->identity_digest,
1736 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1737 "\x3\x3",
1738 DIGEST_LEN)) {
1739 tt_mem_op(rs->identity_digest,OP_EQ,
1740 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1741 DIGEST_LEN);
1742 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1743 tt_assert(!rs->is_authority);
1744 tt_assert(!rs->is_exit);
1745 tt_assert(!rs->is_fast);
1746 tt_assert(!rs->is_possible_guard);
1747 tt_assert(!rs->is_stable);
1748 /* (If it wasn't running it wouldn't be here) */
1749 tt_assert(rs->is_flagged_running);
1750 tt_assert(!rs->is_valid);
1751 tt_assert(!rs->is_named);
1752 tt_assert(rs->is_v2_dir);
1753 /* XXXX check version */
1754 } else if (tor_memeq(rs->identity_digest,
1755 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1756 "\x5\x5\x5\x5",
1757 DIGEST_LEN)) {
1758 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
1759 tt_mem_op(rs->identity_digest,OP_EQ,
1760 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1761 DIGEST_LEN);
1762 tt_str_op(rs->nickname,OP_EQ, "router1");
1763 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1764 tt_int_op(rs->published_on,OP_EQ, now-1000);
1765 tt_int_op(rs->addr,OP_EQ, 0x99009901);
1766 tt_int_op(rs->or_port,OP_EQ, 443);
1767 tt_int_op(rs->dir_port,OP_EQ, 0);
1768 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1769 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1770 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
1771 tt_assert(!rs->is_authority);
1772 tt_assert(rs->is_exit);
1773 tt_assert(rs->is_fast);
1774 tt_assert(rs->is_possible_guard);
1775 tt_assert(rs->is_stable);
1776 tt_assert(rs->is_flagged_running);
1777 tt_assert(rs->is_valid);
1778 tt_assert(rs->is_v2_dir);
1779 tt_assert(!rs->is_named);
1780 /* XXXX check version */
1781 } else {
1782 /* Weren't expecting this... */
1783 tt_assert(0);
1786 done:
1787 return;
1790 /** Run a unit tests for generating and parsing networkstatuses, with
1791 * the supply test fns. */
1792 static void
1793 test_a_networkstatus(
1794 vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
1795 int (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
1796 void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
1797 void (*consensus_test)(networkstatus_t *con, time_t now),
1798 void (*rs_test)(routerstatus_t *rs, time_t now))
1800 authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
1801 crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
1802 crypto_pk_t *sign_skey_leg1=NULL;
1804 * Sum the non-zero returns from vote_tweaks() we've seen; if vote_tweaks()
1805 * returns non-zero, it changed net_params and we should skip the tests for
1806 * that later as they will fail.
1808 int params_tweaked = 0;
1810 time_t now = time(NULL);
1811 networkstatus_voter_info_t *voter;
1812 document_signature_t *sig;
1813 networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
1814 *con_md=NULL;
1815 vote_routerstatus_t *vrs;
1816 routerstatus_t *rs;
1817 int idx, n_rs, n_vrs;
1818 char *consensus_text=NULL, *cp=NULL;
1819 smartlist_t *votes = smartlist_new();
1821 /* For generating the two other consensuses. */
1822 char *detached_text1=NULL, *detached_text2=NULL;
1823 char *consensus_text2=NULL, *consensus_text3=NULL;
1824 char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
1825 char *consensus_text_md=NULL;
1826 networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
1827 ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
1829 tt_assert(vrs_gen);
1830 tt_assert(rs_test);
1831 tt_assert(vrs_test);
1833 tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
1834 &sign_skey_1, &sign_skey_2,
1835 &sign_skey_3));
1836 sign_skey_leg1 = pk_generate(4);
1838 tt_assert(!dir_common_construct_vote_1(&vote, cert1, sign_skey_1, vrs_gen,
1839 &v1, &n_vrs, now, 1));
1840 tt_assert(v1);
1842 /* Make sure the parsed thing was right. */
1843 tt_int_op(v1->type,OP_EQ, NS_TYPE_VOTE);
1844 tt_int_op(v1->published,OP_EQ, vote->published);
1845 tt_int_op(v1->valid_after,OP_EQ, vote->valid_after);
1846 tt_int_op(v1->fresh_until,OP_EQ, vote->fresh_until);
1847 tt_int_op(v1->valid_until,OP_EQ, vote->valid_until);
1848 tt_int_op(v1->vote_seconds,OP_EQ, vote->vote_seconds);
1849 tt_int_op(v1->dist_seconds,OP_EQ, vote->dist_seconds);
1850 tt_str_op(v1->client_versions,OP_EQ, vote->client_versions);
1851 tt_str_op(v1->server_versions,OP_EQ, vote->server_versions);
1852 tt_assert(v1->voters && smartlist_len(v1->voters));
1853 voter = smartlist_get(v1->voters, 0);
1854 tt_str_op(voter->nickname,OP_EQ, "Voter1");
1855 tt_str_op(voter->address,OP_EQ, "1.2.3.4");
1856 tt_int_op(voter->addr,OP_EQ, 0x01020304);
1857 tt_int_op(voter->dir_port,OP_EQ, 80);
1858 tt_int_op(voter->or_port,OP_EQ, 9000);
1859 tt_str_op(voter->contact,OP_EQ, "voter@example.com");
1860 tt_assert(v1->cert);
1861 tt_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
1862 cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
1863 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
1864 tor_free(cp);
1865 tt_int_op(smartlist_len(v1->routerstatus_list),OP_EQ, n_vrs);
1866 networkstatus_vote_free(vote);
1867 vote = NULL;
1869 if (vote_tweaks) params_tweaked += vote_tweaks(v1, 1, now);
1871 /* Check the routerstatuses. */
1872 for (idx = 0; idx < n_vrs; ++idx) {
1873 vrs = smartlist_get(v1->routerstatus_list, idx);
1874 tt_assert(vrs);
1875 vrs_test(vrs, 1, now);
1878 /* Generate second vote. It disagrees on some of the times,
1879 * and doesn't list versions, and knows some crazy flags.
1880 * Generate and parse v2. */
1881 tt_assert(!dir_common_construct_vote_2(&vote, cert2, sign_skey_2, vrs_gen,
1882 &v2, &n_vrs, now, 1));
1883 tt_assert(v2);
1885 if (vote_tweaks) params_tweaked += vote_tweaks(v2, 2, now);
1887 /* Check that flags come out right.*/
1888 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1889 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1890 "Running:Stable:V2Dir:Valid");
1891 tor_free(cp);
1893 /* Check the routerstatuses. */
1894 n_vrs = smartlist_len(v2->routerstatus_list);
1895 for (idx = 0; idx < n_vrs; ++idx) {
1896 vrs = smartlist_get(v2->routerstatus_list, idx);
1897 tt_assert(vrs);
1898 vrs_test(vrs, 2, now);
1900 networkstatus_vote_free(vote);
1901 vote = NULL;
1903 /* Generate the third vote with a legacy id. */
1904 tt_assert(!dir_common_construct_vote_3(&vote, cert3, sign_skey_3, vrs_gen,
1905 &v3, &n_vrs, now, 1));
1906 tt_assert(v3);
1908 if (vote_tweaks) params_tweaked += vote_tweaks(v3, 3, now);
1910 /* Compute a consensus as voter 3. */
1911 smartlist_add(votes, v3);
1912 smartlist_add(votes, v1);
1913 smartlist_add(votes, v2);
1914 consensus_text = networkstatus_compute_consensus(votes, 3,
1915 cert3->identity_key,
1916 sign_skey_3,
1917 "AAAAAAAAAAAAAAAAAAAA",
1918 sign_skey_leg1,
1919 FLAV_NS);
1920 tt_assert(consensus_text);
1921 con = networkstatus_parse_vote_from_string(consensus_text, NULL,
1922 NS_TYPE_CONSENSUS);
1923 tt_assert(con);
1924 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
1925 // v1_text, v2_text, v3_text);
1926 consensus_text_md = networkstatus_compute_consensus(votes, 3,
1927 cert3->identity_key,
1928 sign_skey_3,
1929 "AAAAAAAAAAAAAAAAAAAA",
1930 sign_skey_leg1,
1931 FLAV_MICRODESC);
1932 tt_assert(consensus_text_md);
1933 con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
1934 NS_TYPE_CONSENSUS);
1935 tt_assert(con_md);
1936 tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
1938 /* Check consensus contents. */
1939 tt_assert(con->type == NS_TYPE_CONSENSUS);
1940 tt_int_op(con->published,OP_EQ, 0); /* this field only appears in votes. */
1941 tt_int_op(con->valid_after,OP_EQ, now+1000);
1942 tt_int_op(con->fresh_until,OP_EQ, now+2003); /* median */
1943 tt_int_op(con->valid_until,OP_EQ, now+3000);
1944 tt_int_op(con->vote_seconds,OP_EQ, 100);
1945 tt_int_op(con->dist_seconds,OP_EQ, 250); /* median */
1946 tt_str_op(con->client_versions,OP_EQ, "0.1.2.14");
1947 tt_str_op(con->server_versions,OP_EQ, "0.1.2.15,0.1.2.16");
1948 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1949 tt_str_op(cp,OP_EQ, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1950 "Running:Stable:V2Dir:Valid");
1951 tor_free(cp);
1952 if (!params_tweaked) {
1953 /* Skip this one if vote_tweaks() messed with the param lists */
1954 cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
1955 tt_str_op(cp,OP_EQ, "circuitwindow=80:foo=660");
1956 tor_free(cp);
1959 tt_int_op(4,OP_EQ, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
1960 /* The voter id digests should be in this order. */
1961 tt_assert(memcmp(cert2->cache_info.identity_digest,
1962 cert1->cache_info.identity_digest,DIGEST_LEN)<0);
1963 tt_assert(memcmp(cert1->cache_info.identity_digest,
1964 cert3->cache_info.identity_digest,DIGEST_LEN)<0);
1965 test_same_voter(smartlist_get(con->voters, 1),
1966 smartlist_get(v2->voters, 0));
1967 test_same_voter(smartlist_get(con->voters, 2),
1968 smartlist_get(v1->voters, 0));
1969 test_same_voter(smartlist_get(con->voters, 3),
1970 smartlist_get(v3->voters, 0));
1972 consensus_test(con, now);
1974 /* Check the routerstatuses. */
1975 n_rs = smartlist_len(con->routerstatus_list);
1976 tt_assert(n_rs);
1977 for (idx = 0; idx < n_rs; ++idx) {
1978 rs = smartlist_get(con->routerstatus_list, idx);
1979 tt_assert(rs);
1980 rs_test(rs, now);
1983 n_rs = smartlist_len(con_md->routerstatus_list);
1984 tt_assert(n_rs);
1985 for (idx = 0; idx < n_rs; ++idx) {
1986 rs = smartlist_get(con_md->routerstatus_list, idx);
1987 tt_assert(rs);
1990 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
1991 * The second one hasn't signed. The fourth one has signed: validate it. */
1992 voter = smartlist_get(con->voters, 1);
1993 tt_int_op(smartlist_len(voter->sigs),OP_EQ, 0);
1995 voter = smartlist_get(con->voters, 3);
1996 tt_int_op(smartlist_len(voter->sigs),OP_EQ, 1);
1997 sig = smartlist_get(voter->sigs, 0);
1998 tt_assert(sig->signature);
1999 tt_assert(!sig->good_signature);
2000 tt_assert(!sig->bad_signature);
2002 tt_assert(!networkstatus_check_document_signature(con, sig, cert3));
2003 tt_assert(sig->signature);
2004 tt_assert(sig->good_signature);
2005 tt_assert(!sig->bad_signature);
2008 const char *msg=NULL;
2009 /* Compute the other two signed consensuses. */
2010 smartlist_shuffle(votes);
2011 consensus_text2 = networkstatus_compute_consensus(votes, 3,
2012 cert2->identity_key,
2013 sign_skey_2, NULL,NULL,
2014 FLAV_NS);
2015 consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
2016 cert2->identity_key,
2017 sign_skey_2, NULL,NULL,
2018 FLAV_MICRODESC);
2019 smartlist_shuffle(votes);
2020 consensus_text3 = networkstatus_compute_consensus(votes, 3,
2021 cert1->identity_key,
2022 sign_skey_1, NULL,NULL,
2023 FLAV_NS);
2024 consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
2025 cert1->identity_key,
2026 sign_skey_1, NULL,NULL,
2027 FLAV_MICRODESC);
2028 tt_assert(consensus_text2);
2029 tt_assert(consensus_text3);
2030 tt_assert(consensus_text_md2);
2031 tt_assert(consensus_text_md3);
2032 con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
2033 NS_TYPE_CONSENSUS);
2034 con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
2035 NS_TYPE_CONSENSUS);
2036 con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
2037 NS_TYPE_CONSENSUS);
2038 con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
2039 NS_TYPE_CONSENSUS);
2040 tt_assert(con2);
2041 tt_assert(con3);
2042 tt_assert(con_md2);
2043 tt_assert(con_md3);
2045 /* All three should have the same digest. */
2046 tt_mem_op(&con->digests,OP_EQ, &con2->digests, sizeof(common_digests_t));
2047 tt_mem_op(&con->digests,OP_EQ, &con3->digests, sizeof(common_digests_t));
2049 tt_mem_op(&con_md->digests,OP_EQ, &con_md2->digests,
2050 sizeof(common_digests_t));
2051 tt_mem_op(&con_md->digests,OP_EQ, &con_md3->digests,
2052 sizeof(common_digests_t));
2054 /* Extract a detached signature from con3. */
2055 detached_text1 = get_detached_sigs(con3, con_md3);
2056 tt_assert(detached_text1);
2057 /* Try to parse it. */
2058 dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
2059 tt_assert(dsig1);
2061 /* Are parsed values as expected? */
2062 tt_int_op(dsig1->valid_after,OP_EQ, con3->valid_after);
2063 tt_int_op(dsig1->fresh_until,OP_EQ, con3->fresh_until);
2064 tt_int_op(dsig1->valid_until,OP_EQ, con3->valid_until);
2066 common_digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
2067 tt_assert(dsig_digests);
2068 tt_mem_op(dsig_digests->d[DIGEST_SHA1], OP_EQ,
2069 con3->digests.d[DIGEST_SHA1], DIGEST_LEN);
2070 dsig_digests = strmap_get(dsig1->digests, "microdesc");
2071 tt_assert(dsig_digests);
2072 tt_mem_op(dsig_digests->d[DIGEST_SHA256],OP_EQ,
2073 con_md3->digests.d[DIGEST_SHA256],
2074 DIGEST256_LEN);
2077 smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
2078 tt_assert(dsig_signatures);
2079 tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
2080 sig = smartlist_get(dsig_signatures, 0);
2081 tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
2082 DIGEST_LEN);
2083 tt_int_op(sig->alg,OP_EQ, DIGEST_SHA1);
2085 dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
2086 tt_assert(dsig_signatures);
2087 tt_int_op(1,OP_EQ, smartlist_len(dsig_signatures));
2088 sig = smartlist_get(dsig_signatures, 0);
2089 tt_mem_op(sig->identity_digest,OP_EQ, cert1->cache_info.identity_digest,
2090 DIGEST_LEN);
2091 tt_int_op(sig->alg,OP_EQ, DIGEST_SHA256);
2094 /* Try adding it to con2. */
2095 detached_text2 = get_detached_sigs(con2,con_md2);
2096 tt_int_op(1,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
2097 "test", LOG_INFO, &msg));
2098 tor_free(detached_text2);
2099 tt_int_op(1,OP_EQ,
2100 networkstatus_add_detached_signatures(con_md2, dsig1, "test",
2101 LOG_INFO, &msg));
2102 tor_free(detached_text2);
2103 detached_text2 = get_detached_sigs(con2,con_md2);
2104 //printf("\n<%s>\n", detached_text2);
2105 dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
2106 tt_assert(dsig2);
2108 printf("\n");
2109 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2110 char hd[64];
2111 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2112 printf("%s\n", hd);
2115 tt_int_op(2,OP_EQ,
2116 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
2117 tt_int_op(2,OP_EQ,
2118 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
2119 "microdesc")));
2121 /* Try adding to con2 twice; verify that nothing changes. */
2122 tt_int_op(0,OP_EQ, networkstatus_add_detached_signatures(con2, dsig1,
2123 "test", LOG_INFO, &msg));
2125 /* Add to con. */
2126 tt_int_op(2,OP_EQ, networkstatus_add_detached_signatures(con, dsig2,
2127 "test", LOG_INFO, &msg));
2128 /* Check signatures */
2129 voter = smartlist_get(con->voters, 1);
2130 sig = smartlist_get(voter->sigs, 0);
2131 tt_assert(sig);
2132 tt_assert(!networkstatus_check_document_signature(con, sig, cert2));
2133 voter = smartlist_get(con->voters, 2);
2134 sig = smartlist_get(voter->sigs, 0);
2135 tt_assert(sig);
2136 tt_assert(!networkstatus_check_document_signature(con, sig, cert1));
2139 done:
2140 tor_free(cp);
2141 smartlist_free(votes);
2142 tor_free(consensus_text);
2143 tor_free(consensus_text_md);
2145 networkstatus_vote_free(vote);
2146 networkstatus_vote_free(v1);
2147 networkstatus_vote_free(v2);
2148 networkstatus_vote_free(v3);
2149 networkstatus_vote_free(con);
2150 networkstatus_vote_free(con_md);
2151 crypto_pk_free(sign_skey_1);
2152 crypto_pk_free(sign_skey_2);
2153 crypto_pk_free(sign_skey_3);
2154 crypto_pk_free(sign_skey_leg1);
2155 authority_cert_free(cert1);
2156 authority_cert_free(cert2);
2157 authority_cert_free(cert3);
2159 tor_free(consensus_text2);
2160 tor_free(consensus_text3);
2161 tor_free(consensus_text_md2);
2162 tor_free(consensus_text_md3);
2163 tor_free(detached_text1);
2164 tor_free(detached_text2);
2166 networkstatus_vote_free(con2);
2167 networkstatus_vote_free(con3);
2168 networkstatus_vote_free(con_md2);
2169 networkstatus_vote_free(con_md3);
2170 ns_detached_signatures_free(dsig1);
2171 ns_detached_signatures_free(dsig2);
2174 /** Run unit tests for generating and parsing V3 consensus networkstatus
2175 * documents. */
2176 static void
2177 test_dir_v3_networkstatus(void *arg)
2179 (void)arg;
2180 test_a_networkstatus(dir_common_gen_routerstatus_for_v3ns,
2181 vote_tweaks_for_v3ns,
2182 test_vrs_for_v3ns,
2183 test_consensus_for_v3ns,
2184 test_routerstatus_for_v3ns);
2187 static void
2188 test_dir_scale_bw(void *testdata)
2190 double v[8] = { 2.0/3,
2191 7.0,
2192 1.0,
2193 3.0,
2194 1.0/5,
2195 1.0/7,
2196 12.0,
2197 24.0 };
2198 u64_dbl_t vals[8];
2199 uint64_t total;
2200 int i;
2202 (void) testdata;
2204 for (i=0; i<8; ++i)
2205 vals[i].dbl = v[i];
2207 scale_array_elements_to_u64(vals, 8, &total);
2209 tt_int_op((int)total, OP_EQ, 48);
2210 total = 0;
2211 for (i=0; i<8; ++i) {
2212 total += vals[i].u64;
2214 tt_assert(total >= (U64_LITERAL(1)<<60));
2215 tt_assert(total <= (U64_LITERAL(1)<<62));
2217 for (i=0; i<8; ++i) {
2218 /* vals[2].u64 is the scaled value of 1.0 */
2219 double ratio = ((double)vals[i].u64) / vals[2].u64;
2220 tt_double_op(fabs(ratio - v[i]), OP_LT, .00001);
2223 /* test handling of no entries */
2224 total = 1;
2225 scale_array_elements_to_u64(vals, 0, &total);
2226 tt_assert(total == 0);
2228 /* make sure we don't read the array when we have no entries
2229 * may require compiler flags to catch NULL dereferences */
2230 total = 1;
2231 scale_array_elements_to_u64(NULL, 0, &total);
2232 tt_assert(total == 0);
2234 scale_array_elements_to_u64(NULL, 0, NULL);
2236 /* test handling of zero totals */
2237 total = 1;
2238 vals[0].dbl = 0.0;
2239 scale_array_elements_to_u64(vals, 1, &total);
2240 tt_assert(total == 0);
2241 tt_assert(vals[0].u64 == 0);
2243 vals[0].dbl = 0.0;
2244 vals[1].dbl = 0.0;
2245 scale_array_elements_to_u64(vals, 2, NULL);
2246 tt_assert(vals[0].u64 == 0);
2247 tt_assert(vals[1].u64 == 0);
2249 done:
2253 static void
2254 test_dir_random_weighted(void *testdata)
2256 int histogram[10];
2257 uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
2258 u64_dbl_t inp[10];
2259 int i, choice;
2260 const int n = 50000;
2261 double max_sq_error;
2262 (void) testdata;
2264 /* Try a ten-element array with values from 0 through 10. The values are
2265 * in a scrambled order to make sure we don't depend on order. */
2266 memset(histogram,0,sizeof(histogram));
2267 for (i=0; i<10; ++i) {
2268 inp[i].u64 = vals[i];
2269 total += vals[i];
2271 tt_u64_op(total, OP_EQ, 45);
2272 for (i=0; i<n; ++i) {
2273 choice = choose_array_element_by_weight(inp, 10);
2274 tt_int_op(choice, OP_GE, 0);
2275 tt_int_op(choice, OP_LT, 10);
2276 histogram[choice]++;
2279 /* Now see if we chose things about frequently enough. */
2280 max_sq_error = 0;
2281 for (i=0; i<10; ++i) {
2282 int expected = (int)(n*vals[i]/total);
2283 double frac_diff = 0, sq;
2284 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
2285 if (expected)
2286 frac_diff = (histogram[i] - expected) / ((double)expected);
2287 else
2288 tt_int_op(histogram[i], OP_EQ, 0);
2290 sq = frac_diff * frac_diff;
2291 if (sq > max_sq_error)
2292 max_sq_error = sq;
2294 /* It should almost always be much much less than this. If you want to
2295 * figure out the odds, please feel free. */
2296 tt_double_op(max_sq_error, OP_LT, .05);
2298 /* Now try a singleton; do we choose it? */
2299 for (i = 0; i < 100; ++i) {
2300 choice = choose_array_element_by_weight(inp, 1);
2301 tt_int_op(choice, OP_EQ, 0);
2304 /* Now try an array of zeros. We should choose randomly. */
2305 memset(histogram,0,sizeof(histogram));
2306 for (i = 0; i < 5; ++i)
2307 inp[i].u64 = 0;
2308 for (i = 0; i < n; ++i) {
2309 choice = choose_array_element_by_weight(inp, 5);
2310 tt_int_op(choice, OP_GE, 0);
2311 tt_int_op(choice, OP_LT, 5);
2312 histogram[choice]++;
2314 /* Now see if we chose things about frequently enough. */
2315 max_sq_error = 0;
2316 for (i=0; i<5; ++i) {
2317 int expected = n/5;
2318 double frac_diff = 0, sq;
2319 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
2320 frac_diff = (histogram[i] - expected) / ((double)expected);
2321 sq = frac_diff * frac_diff;
2322 if (sq > max_sq_error)
2323 max_sq_error = sq;
2325 /* It should almost always be much much less than this. If you want to
2326 * figure out the odds, please feel free. */
2327 tt_double_op(max_sq_error, OP_LT, .05);
2328 done:
2332 /* Function pointers for test_dir_clip_unmeasured_bw_kb() */
2334 static uint32_t alternate_clip_bw = 0;
2337 * Generate a routerstatus for clip_unmeasured_bw_kb test; based on the
2338 * v3_networkstatus ones.
2340 static vote_routerstatus_t *
2341 gen_routerstatus_for_umbw(int idx, time_t now)
2343 vote_routerstatus_t *vrs = NULL;
2344 routerstatus_t *rs;
2345 tor_addr_t addr_ipv6;
2346 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2347 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2349 switch (idx) {
2350 case 0:
2351 /* Generate the first routerstatus. */
2352 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2353 rs = &vrs->status;
2354 vrs->version = tor_strdup("0.1.2.14");
2355 rs->published_on = now-1500;
2356 strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
2357 memset(rs->identity_digest, 3, DIGEST_LEN);
2358 memset(rs->descriptor_digest, 78, DIGEST_LEN);
2359 rs->addr = 0x99008801;
2360 rs->or_port = 443;
2361 rs->dir_port = 8000;
2362 /* all flags but running cleared */
2363 rs->is_flagged_running = 1;
2365 * This one has measured bandwidth below the clip cutoff, and
2366 * so shouldn't be clipped; we'll have to test that it isn't
2367 * later.
2369 vrs->has_measured_bw = 1;
2370 rs->has_bandwidth = 1;
2371 vrs->measured_bw_kb = rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
2372 break;
2373 case 1:
2374 /* Generate the second routerstatus. */
2375 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2376 rs = &vrs->status;
2377 vrs->version = tor_strdup("0.2.0.5");
2378 rs->published_on = now-1000;
2379 strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
2380 memset(rs->identity_digest, 5, DIGEST_LEN);
2381 memset(rs->descriptor_digest, 77, DIGEST_LEN);
2382 rs->addr = 0x99009901;
2383 rs->or_port = 443;
2384 rs->dir_port = 0;
2385 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2386 tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
2387 rs->ipv6_orport = 4711;
2388 rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
2389 rs->is_valid = rs->is_possible_guard = 1;
2391 * This one has measured bandwidth above the clip cutoff, and
2392 * so shouldn't be clipped; we'll have to test that it isn't
2393 * later.
2395 vrs->has_measured_bw = 1;
2396 rs->has_bandwidth = 1;
2397 vrs->measured_bw_kb = rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
2398 break;
2399 case 2:
2400 /* Generate the third routerstatus. */
2401 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2402 rs = &vrs->status;
2403 vrs->version = tor_strdup("0.1.0.3");
2404 rs->published_on = now-1000;
2405 strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
2406 memset(rs->identity_digest, 0x33, DIGEST_LEN);
2407 memset(rs->descriptor_digest, 79, DIGEST_LEN);
2408 rs->addr = 0xAA009901;
2409 rs->or_port = 400;
2410 rs->dir_port = 9999;
2411 rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
2412 rs->is_flagged_running = rs->is_valid =
2413 rs->is_possible_guard = 1;
2415 * This one has unmeasured bandwidth above the clip cutoff, and
2416 * so should be clipped; we'll have to test that it isn't
2417 * later.
2419 vrs->has_measured_bw = 0;
2420 rs->has_bandwidth = 1;
2421 vrs->measured_bw_kb = 0;
2422 rs->bandwidth_kb = 2 * max_unmeasured_bw_kb;
2423 break;
2424 case 3:
2425 /* Generate a fourth routerstatus that is not running. */
2426 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
2427 rs = &vrs->status;
2428 vrs->version = tor_strdup("0.1.6.3");
2429 rs->published_on = now-1000;
2430 strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
2431 memset(rs->identity_digest, 0x34, DIGEST_LEN);
2432 memset(rs->descriptor_digest, 47, DIGEST_LEN);
2433 rs->addr = 0xC0000203;
2434 rs->or_port = 500;
2435 rs->dir_port = 1999;
2436 /* all flags but running cleared */
2437 rs->is_flagged_running = 1;
2439 * This one has unmeasured bandwidth below the clip cutoff, and
2440 * so shouldn't be clipped; we'll have to test that it isn't
2441 * later.
2443 vrs->has_measured_bw = 0;
2444 rs->has_bandwidth = 1;
2445 vrs->measured_bw_kb = 0;
2446 rs->bandwidth_kb = max_unmeasured_bw_kb / 2;
2447 break;
2448 case 4:
2449 /* No more for this test; return NULL */
2450 vrs = NULL;
2451 break;
2452 default:
2453 /* Shouldn't happen */
2454 tt_assert(0);
2456 if (vrs) {
2457 vrs->microdesc = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
2458 tor_asprintf(&vrs->microdesc->microdesc_hash_line,
2459 "m 9,10,11,12,13,14,15,16,17 "
2460 "sha256=xyzajkldsdsajdadlsdjaslsdksdjlsdjsdaskdaaa%d\n",
2461 idx);
2464 done:
2465 return vrs;
2468 /** Apply tweaks to the vote list for each voter; for the umbw test this is
2469 * just adding the right consensus methods to let clipping happen */
2470 static int
2471 vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
2473 char *maxbw_param = NULL;
2474 int rv = 0;
2476 tt_assert(v);
2477 (void)voter;
2478 (void)now;
2480 tt_assert(v->supported_methods);
2481 SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
2482 smartlist_clear(v->supported_methods);
2483 /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
2484 smartlist_split_string(v->supported_methods,
2485 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
2486 NULL, 0, -1);
2487 /* If we're using a non-default clip bandwidth, add it to net_params */
2488 if (alternate_clip_bw > 0) {
2489 tor_asprintf(&maxbw_param, "maxunmeasuredbw=%u", alternate_clip_bw);
2490 tt_assert(maxbw_param);
2491 if (maxbw_param) {
2492 smartlist_add(v->net_params, maxbw_param);
2493 rv = 1;
2497 done:
2498 return rv;
2502 * Test a parsed vote_routerstatus_t for umbw test.
2504 static void
2505 test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
2507 routerstatus_t *rs;
2508 tor_addr_t addr_ipv6;
2509 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2510 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2512 (void)voter;
2513 tt_assert(vrs);
2514 rs = &(vrs->status);
2515 tt_assert(rs);
2517 /* Split out by digests to test */
2518 if (tor_memeq(rs->identity_digest,
2519 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2520 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2521 DIGEST_LEN)) {
2523 * Check the first routerstatus - measured bandwidth below the clip
2524 * cutoff.
2526 tt_str_op(vrs->version,OP_EQ, "0.1.2.14");
2527 tt_int_op(rs->published_on,OP_EQ, now-1500);
2528 tt_str_op(rs->nickname,OP_EQ, "router2");
2529 tt_mem_op(rs->identity_digest,OP_EQ,
2530 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2531 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2532 DIGEST_LEN);
2533 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
2534 tt_int_op(rs->addr,OP_EQ, 0x99008801);
2535 tt_int_op(rs->or_port,OP_EQ, 443);
2536 tt_int_op(rs->dir_port,OP_EQ, 8000);
2537 tt_assert(rs->has_bandwidth);
2538 tt_assert(vrs->has_measured_bw);
2539 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2540 tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2541 } else if (tor_memeq(rs->identity_digest,
2542 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2543 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2544 DIGEST_LEN)) {
2547 * Check the second routerstatus - measured bandwidth above the clip
2548 * cutoff.
2550 tt_str_op(vrs->version,OP_EQ, "0.2.0.5");
2551 tt_int_op(rs->published_on,OP_EQ, now-1000);
2552 tt_str_op(rs->nickname,OP_EQ, "router1");
2553 tt_mem_op(rs->identity_digest,OP_EQ,
2554 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2555 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2556 DIGEST_LEN);
2557 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
2558 tt_int_op(rs->addr,OP_EQ, 0x99009901);
2559 tt_int_op(rs->or_port,OP_EQ, 443);
2560 tt_int_op(rs->dir_port,OP_EQ, 0);
2561 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2562 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
2563 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
2564 tt_assert(rs->has_bandwidth);
2565 tt_assert(vrs->has_measured_bw);
2566 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2567 tt_int_op(vrs->measured_bw_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2568 } else if (tor_memeq(rs->identity_digest,
2569 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2570 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2571 DIGEST_LEN)) {
2573 * Check the third routerstatus - unmeasured bandwidth above the clip
2574 * cutoff; this one should be clipped later on in the consensus, but
2575 * appears unclipped in the vote.
2577 tt_assert(rs->has_bandwidth);
2578 tt_assert(!(vrs->has_measured_bw));
2579 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2580 tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
2581 } else if (tor_memeq(rs->identity_digest,
2582 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2583 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2584 DIGEST_LEN)) {
2586 * Check the fourth routerstatus - unmeasured bandwidth below the clip
2587 * cutoff; this one should not be clipped.
2589 tt_assert(rs->has_bandwidth);
2590 tt_assert(!(vrs->has_measured_bw));
2591 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2592 tt_int_op(vrs->measured_bw_kb,OP_EQ, 0);
2593 } else {
2594 tt_assert(0);
2597 done:
2598 return;
2602 * Test a consensus for v3_networkstatus_test
2604 static void
2605 test_consensus_for_umbw(networkstatus_t *con, time_t now)
2607 (void)now;
2609 tt_assert(con);
2610 tt_assert(!con->cert);
2611 // tt_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB);
2612 tt_assert(con->consensus_method >= 16);
2613 tt_int_op(4,OP_EQ, smartlist_len(con->routerstatus_list));
2614 /* There should be four listed routers; all voters saw the same in this */
2616 done:
2617 return;
2621 * Test a router list entry for umbw test
2623 static void
2624 test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
2626 tor_addr_t addr_ipv6;
2627 uint32_t max_unmeasured_bw_kb = (alternate_clip_bw > 0) ?
2628 alternate_clip_bw : DEFAULT_MAX_UNMEASURED_BW_KB;
2630 tt_assert(rs);
2632 /* There should be four listed routers, as constructed above */
2633 if (tor_memeq(rs->identity_digest,
2634 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2635 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2636 DIGEST_LEN)) {
2637 tt_mem_op(rs->identity_digest,OP_EQ,
2638 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
2639 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2640 DIGEST_LEN);
2641 tt_mem_op(rs->descriptor_digest,OP_EQ, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
2642 tt_assert(!rs->is_authority);
2643 tt_assert(!rs->is_exit);
2644 tt_assert(!rs->is_fast);
2645 tt_assert(!rs->is_possible_guard);
2646 tt_assert(!rs->is_stable);
2647 /* (If it wasn't running it wouldn't be here) */
2648 tt_assert(rs->is_flagged_running);
2649 tt_assert(!rs->is_valid);
2650 tt_assert(!rs->is_named);
2651 /* This one should have measured bandwidth below the clip cutoff */
2652 tt_assert(rs->has_bandwidth);
2653 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2654 tt_assert(!(rs->bw_is_unmeasured));
2655 } else if (tor_memeq(rs->identity_digest,
2656 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2657 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2658 DIGEST_LEN)) {
2659 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2660 tt_mem_op(rs->identity_digest,OP_EQ,
2661 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2662 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2663 DIGEST_LEN);
2664 tt_str_op(rs->nickname,OP_EQ, "router1");
2665 tt_mem_op(rs->descriptor_digest,OP_EQ, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
2666 tt_int_op(rs->published_on,OP_EQ, now-1000);
2667 tt_int_op(rs->addr,OP_EQ, 0x99009901);
2668 tt_int_op(rs->or_port,OP_EQ, 443);
2669 tt_int_op(rs->dir_port,OP_EQ, 0);
2670 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2671 tt_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
2672 tt_int_op(rs->ipv6_orport,OP_EQ, 4711);
2673 tt_assert(!rs->is_authority);
2674 tt_assert(rs->is_exit);
2675 tt_assert(rs->is_fast);
2676 tt_assert(rs->is_possible_guard);
2677 tt_assert(rs->is_stable);
2678 tt_assert(rs->is_flagged_running);
2679 tt_assert(rs->is_valid);
2680 tt_assert(!rs->is_named);
2681 /* This one should have measured bandwidth above the clip cutoff */
2682 tt_assert(rs->has_bandwidth);
2683 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb * 2);
2684 tt_assert(!(rs->bw_is_unmeasured));
2685 } else if (tor_memeq(rs->identity_digest,
2686 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2687 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2688 DIGEST_LEN)) {
2690 * This one should have unmeasured bandwidth above the clip cutoff,
2691 * and so should be clipped
2693 tt_assert(rs->has_bandwidth);
2694 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb);
2695 tt_assert(rs->bw_is_unmeasured);
2696 } else if (tor_memeq(rs->identity_digest,
2697 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2698 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2699 DIGEST_LEN)) {
2701 * This one should have unmeasured bandwidth below the clip cutoff,
2702 * and so should not be clipped
2704 tt_assert(rs->has_bandwidth);
2705 tt_int_op(rs->bandwidth_kb,OP_EQ, max_unmeasured_bw_kb / 2);
2706 tt_assert(rs->bw_is_unmeasured);
2707 } else {
2708 /* Weren't expecting this... */
2709 tt_assert(0);
2712 done:
2713 return;
2717 * Compute a consensus involving clipping unmeasured bandwidth with consensus
2718 * method 17; this uses the same test_a_networkstatus() function that the
2719 * v3_networkstatus test uses.
2722 static void
2723 test_dir_clip_unmeasured_bw_kb(void *arg)
2725 /* Run the test with the default clip bandwidth */
2726 (void)arg;
2727 alternate_clip_bw = 0;
2728 test_a_networkstatus(gen_routerstatus_for_umbw,
2729 vote_tweaks_for_umbw,
2730 test_vrs_for_umbw,
2731 test_consensus_for_umbw,
2732 test_routerstatus_for_umbw);
2736 * This version of test_dir_clip_unmeasured_bw_kb() uses a non-default choice
2737 * of clip bandwidth.
2740 static void
2741 test_dir_clip_unmeasured_bw_kb_alt(void *arg)
2744 * Try a different one; this value is chosen so that the below-the-cutoff
2745 * unmeasured nodes the test uses, at alternate_clip_bw / 2, will be above
2746 * DEFAULT_MAX_UNMEASURED_BW_KB and if the consensus incorrectly uses that
2747 * cutoff it will fail the test.
2749 (void)arg;
2750 alternate_clip_bw = 3 * DEFAULT_MAX_UNMEASURED_BW_KB;
2751 test_a_networkstatus(gen_routerstatus_for_umbw,
2752 vote_tweaks_for_umbw,
2753 test_vrs_for_umbw,
2754 test_consensus_for_umbw,
2755 test_routerstatus_for_umbw);
2758 static void
2759 test_dir_fmt_control_ns(void *arg)
2761 char *s = NULL;
2762 routerstatus_t rs;
2763 (void)arg;
2765 memset(&rs, 0, sizeof(rs));
2766 rs.published_on = 1364925198;
2767 strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname));
2768 memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN);
2769 memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN);
2770 rs.addr = 0x20304050;
2771 rs.or_port = 9001;
2772 rs.dir_port = 9002;
2773 rs.is_exit = 1;
2774 rs.is_fast = 1;
2775 rs.is_flagged_running = 1;
2776 rs.has_bandwidth = 1;
2777 rs.is_v2_dir = 1;
2778 rs.bandwidth_kb = 1000;
2780 s = networkstatus_getinfo_helper_single(&rs);
2781 tt_assert(s);
2782 tt_str_op(s, OP_EQ,
2783 "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA "
2784 "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 "
2785 "32.48.64.80 9001 9002\n"
2786 "s Exit Fast Running V2Dir\n"
2787 "w Bandwidth=1000\n");
2789 done:
2790 tor_free(s);
2793 static int mock_get_options_calls = 0;
2794 static or_options_t *mock_options = NULL;
2796 static void
2797 reset_options(or_options_t *options, int *get_options_calls)
2799 memset(options, 0, sizeof(or_options_t));
2800 options->TestingTorNetwork = 1;
2802 *get_options_calls = 0;
2805 static const or_options_t *
2806 mock_get_options(void)
2808 ++mock_get_options_calls;
2809 tor_assert(mock_options);
2810 return mock_options;
2813 static void
2814 reset_routerstatus(routerstatus_t *rs,
2815 const char *hex_identity_digest,
2816 int32_t ipv4_addr)
2818 memset(rs, 0, sizeof(routerstatus_t));
2819 base16_decode(rs->identity_digest, sizeof(rs->identity_digest),
2820 hex_identity_digest, HEX_DIGEST_LEN);
2821 /* A zero address matches everything, so the address needs to be set.
2822 * But the specific value is irrelevant. */
2823 rs->addr = ipv4_addr;
2826 #define ROUTER_A_ID_STR "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
2827 #define ROUTER_A_IPV4 0xAA008801
2828 #define ROUTER_B_ID_STR "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
2829 #define ROUTER_B_IPV4 0xBB008801
2831 #define ROUTERSET_ALL_STR "*"
2832 #define ROUTERSET_A_STR ROUTER_A_ID_STR
2833 #define ROUTERSET_NONE_STR ""
2836 * Test that dirserv_set_routerstatus_testing sets router flags correctly
2837 * Using "*" sets flags on A and B
2838 * Using "A" sets flags on A
2839 * Using "" sets flags on Neither
2840 * If the router is not included:
2841 * - if *Strict is set, the flag is set to 0,
2842 * - otherwise, the flag is not modified. */
2843 static void
2844 test_dir_dirserv_set_routerstatus_testing(void *arg)
2846 (void)arg;
2848 /* Init options */
2849 mock_options = malloc(sizeof(or_options_t));
2850 reset_options(mock_options, &mock_get_options_calls);
2852 MOCK(get_options, mock_get_options);
2854 /* Init routersets */
2855 routerset_t *routerset_all = routerset_new();
2856 routerset_parse(routerset_all, ROUTERSET_ALL_STR, "All routers");
2858 routerset_t *routerset_a = routerset_new();
2859 routerset_parse(routerset_a, ROUTERSET_A_STR, "Router A only");
2861 routerset_t *routerset_none = routerset_new();
2862 /* Routersets are empty when provided by routerset_new(),
2863 * so this is not strictly necessary */
2864 routerset_parse(routerset_none, ROUTERSET_NONE_STR, "No routers");
2866 /* Init routerstatuses */
2867 routerstatus_t *rs_a = malloc(sizeof(routerstatus_t));
2868 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2870 routerstatus_t *rs_b = malloc(sizeof(routerstatus_t));
2871 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2873 /* Sanity check that routersets correspond to routerstatuses.
2874 * Return values are {2, 3, 4} */
2876 /* We want 3 ("*" means match all addresses) */
2877 tt_assert(routerset_contains_routerstatus(routerset_all, rs_a, 0) == 3);
2878 tt_assert(routerset_contains_routerstatus(routerset_all, rs_b, 0) == 3);
2880 /* We want 4 (match id_digest [or nickname]) */
2881 tt_assert(routerset_contains_routerstatus(routerset_a, rs_a, 0) == 4);
2882 tt_assert(routerset_contains_routerstatus(routerset_a, rs_b, 0) == 0);
2884 tt_assert(routerset_contains_routerstatus(routerset_none, rs_a, 0) == 0);
2885 tt_assert(routerset_contains_routerstatus(routerset_none, rs_b, 0) == 0);
2887 /* Check that "*" sets flags on all routers: Exit
2888 * Check the flags aren't being confused with each other */
2889 reset_options(mock_options, &mock_get_options_calls);
2890 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2891 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2893 mock_options->TestingDirAuthVoteExit = routerset_all;
2894 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2896 dirserv_set_routerstatus_testing(rs_a);
2897 tt_assert(mock_get_options_calls == 1);
2898 dirserv_set_routerstatus_testing(rs_b);
2899 tt_assert(mock_get_options_calls == 2);
2901 tt_assert(rs_a->is_exit == 1);
2902 tt_assert(rs_b->is_exit == 1);
2903 /* Be paranoid - check no other flags are set */
2904 tt_assert(rs_a->is_possible_guard == 0);
2905 tt_assert(rs_b->is_possible_guard == 0);
2906 tt_assert(rs_a->is_hs_dir == 0);
2907 tt_assert(rs_b->is_hs_dir == 0);
2909 /* Check that "*" sets flags on all routers: Guard & HSDir
2910 * Cover the remaining flags in one test */
2911 reset_options(mock_options, &mock_get_options_calls);
2912 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2913 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2915 mock_options->TestingDirAuthVoteGuard = routerset_all;
2916 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2917 mock_options->TestingDirAuthVoteHSDir = routerset_all;
2918 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2920 dirserv_set_routerstatus_testing(rs_a);
2921 tt_assert(mock_get_options_calls == 1);
2922 dirserv_set_routerstatus_testing(rs_b);
2923 tt_assert(mock_get_options_calls == 2);
2925 tt_assert(rs_a->is_possible_guard == 1);
2926 tt_assert(rs_b->is_possible_guard == 1);
2927 tt_assert(rs_a->is_hs_dir == 1);
2928 tt_assert(rs_b->is_hs_dir == 1);
2929 /* Be paranoid - check exit isn't set */
2930 tt_assert(rs_a->is_exit == 0);
2931 tt_assert(rs_b->is_exit == 0);
2933 /* Check routerset A sets all flags on router A,
2934 * but leaves router B unmodified */
2935 reset_options(mock_options, &mock_get_options_calls);
2936 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
2937 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2939 mock_options->TestingDirAuthVoteExit = routerset_a;
2940 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2941 mock_options->TestingDirAuthVoteGuard = routerset_a;
2942 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2943 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2944 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2946 dirserv_set_routerstatus_testing(rs_a);
2947 tt_assert(mock_get_options_calls == 1);
2948 dirserv_set_routerstatus_testing(rs_b);
2949 tt_assert(mock_get_options_calls == 2);
2951 tt_assert(rs_a->is_exit == 1);
2952 tt_assert(rs_b->is_exit == 0);
2953 tt_assert(rs_a->is_possible_guard == 1);
2954 tt_assert(rs_b->is_possible_guard == 0);
2955 tt_assert(rs_a->is_hs_dir == 1);
2956 tt_assert(rs_b->is_hs_dir == 0);
2958 /* Check routerset A unsets all flags on router B when Strict is set */
2959 reset_options(mock_options, &mock_get_options_calls);
2960 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2962 mock_options->TestingDirAuthVoteExit = routerset_a;
2963 mock_options->TestingDirAuthVoteExitIsStrict = 1;
2964 mock_options->TestingDirAuthVoteGuard = routerset_a;
2965 mock_options->TestingDirAuthVoteGuardIsStrict = 1;
2966 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2967 mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
2969 rs_b->is_exit = 1;
2970 rs_b->is_possible_guard = 1;
2971 rs_b->is_hs_dir = 1;
2973 dirserv_set_routerstatus_testing(rs_b);
2974 tt_assert(mock_get_options_calls == 1);
2976 tt_assert(rs_b->is_exit == 0);
2977 tt_assert(rs_b->is_possible_guard == 0);
2978 tt_assert(rs_b->is_hs_dir == 0);
2980 /* Check routerset A doesn't modify flags on router B without Strict set */
2981 reset_options(mock_options, &mock_get_options_calls);
2982 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
2984 mock_options->TestingDirAuthVoteExit = routerset_a;
2985 mock_options->TestingDirAuthVoteExitIsStrict = 0;
2986 mock_options->TestingDirAuthVoteGuard = routerset_a;
2987 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
2988 mock_options->TestingDirAuthVoteHSDir = routerset_a;
2989 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
2991 rs_b->is_exit = 1;
2992 rs_b->is_possible_guard = 1;
2993 rs_b->is_hs_dir = 1;
2995 dirserv_set_routerstatus_testing(rs_b);
2996 tt_assert(mock_get_options_calls == 1);
2998 tt_assert(rs_b->is_exit == 1);
2999 tt_assert(rs_b->is_possible_guard == 1);
3000 tt_assert(rs_b->is_hs_dir == 1);
3002 /* Check the empty routerset zeroes all flags
3003 * on routers A & B with Strict set */
3004 reset_options(mock_options, &mock_get_options_calls);
3005 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
3007 mock_options->TestingDirAuthVoteExit = routerset_none;
3008 mock_options->TestingDirAuthVoteExitIsStrict = 1;
3009 mock_options->TestingDirAuthVoteGuard = routerset_none;
3010 mock_options->TestingDirAuthVoteGuardIsStrict = 1;
3011 mock_options->TestingDirAuthVoteHSDir = routerset_none;
3012 mock_options->TestingDirAuthVoteHSDirIsStrict = 1;
3014 rs_b->is_exit = 1;
3015 rs_b->is_possible_guard = 1;
3016 rs_b->is_hs_dir = 1;
3018 dirserv_set_routerstatus_testing(rs_b);
3019 tt_assert(mock_get_options_calls == 1);
3021 tt_assert(rs_b->is_exit == 0);
3022 tt_assert(rs_b->is_possible_guard == 0);
3023 tt_assert(rs_b->is_hs_dir == 0);
3025 /* Check the empty routerset doesn't modify any flags
3026 * on A or B without Strict set */
3027 reset_options(mock_options, &mock_get_options_calls);
3028 reset_routerstatus(rs_a, ROUTER_A_ID_STR, ROUTER_A_IPV4);
3029 reset_routerstatus(rs_b, ROUTER_B_ID_STR, ROUTER_B_IPV4);
3031 mock_options->TestingDirAuthVoteExit = routerset_none;
3032 mock_options->TestingDirAuthVoteExitIsStrict = 0;
3033 mock_options->TestingDirAuthVoteGuard = routerset_none;
3034 mock_options->TestingDirAuthVoteGuardIsStrict = 0;
3035 mock_options->TestingDirAuthVoteHSDir = routerset_none;
3036 mock_options->TestingDirAuthVoteHSDirIsStrict = 0;
3038 rs_b->is_exit = 1;
3039 rs_b->is_possible_guard = 1;
3040 rs_b->is_hs_dir = 1;
3042 dirserv_set_routerstatus_testing(rs_a);
3043 tt_assert(mock_get_options_calls == 1);
3044 dirserv_set_routerstatus_testing(rs_b);
3045 tt_assert(mock_get_options_calls == 2);
3047 tt_assert(rs_a->is_exit == 0);
3048 tt_assert(rs_a->is_possible_guard == 0);
3049 tt_assert(rs_a->is_hs_dir == 0);
3050 tt_assert(rs_b->is_exit == 1);
3051 tt_assert(rs_b->is_possible_guard == 1);
3052 tt_assert(rs_b->is_hs_dir == 1);
3054 done:
3055 free(mock_options);
3056 mock_options = NULL;
3058 UNMOCK(get_options);
3060 routerset_free(routerset_all);
3061 routerset_free(routerset_a);
3062 routerset_free(routerset_none);
3064 free(rs_a);
3065 free(rs_b);
3068 static void
3069 test_dir_http_handling(void *args)
3071 char *url = NULL;
3072 (void)args;
3074 /* Parse http url tests: */
3075 /* Good headers */
3076 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1\r\n"
3077 "Host: example.com\r\n"
3078 "User-Agent: Mozilla/5.0 (Windows;"
3079 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3080 &url),OP_EQ, 0);
3081 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3082 tor_free(url);
3084 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.0\r\n", &url),OP_EQ, 0);
3085 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3086 tor_free(url);
3088 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.600\r\n", &url),
3089 OP_EQ, 0);
3090 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3091 tor_free(url);
3093 /* Should prepend '/tor/' to url if required */
3094 tt_int_op(parse_http_url("GET /a/b/c.txt HTTP/1.1\r\n"
3095 "Host: example.com\r\n"
3096 "User-Agent: Mozilla/5.0 (Windows;"
3097 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3098 &url),OP_EQ, 0);
3099 tt_str_op(url,OP_EQ, "/tor/a/b/c.txt");
3100 tor_free(url);
3102 /* Bad headers -- no HTTP/1.x*/
3103 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3104 "Host: example.com\r\n"
3105 "User-Agent: Mozilla/5.0 (Windows;"
3106 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3107 &url),OP_EQ, -1);
3108 tt_assert(!url);
3110 /* Bad headers */
3111 tt_int_op(parse_http_url("GET /a/b/c.txt\r\n"
3112 "Host: example.com\r\n"
3113 "User-Agent: Mozilla/5.0 (Windows;"
3114 " U; Windows NT 6.1; en-US; rv:1.9.1.5)\r\n",
3115 &url),OP_EQ, -1);
3116 tt_assert(!url);
3118 tt_int_op(parse_http_url("GET /tor/a/b/c.txt", &url),OP_EQ, -1);
3119 tt_assert(!url);
3121 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1", &url),OP_EQ, -1);
3122 tt_assert(!url);
3124 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.1x\r\n", &url),
3125 OP_EQ, -1);
3126 tt_assert(!url);
3128 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.", &url),OP_EQ, -1);
3129 tt_assert(!url);
3131 tt_int_op(parse_http_url("GET /tor/a/b/c.txt HTTP/1.\r", &url),OP_EQ, -1);
3132 tt_assert(!url);
3134 done:
3135 tor_free(url);
3138 static void
3139 test_dir_purpose_needs_anonymity(void *arg)
3141 (void)arg;
3142 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_BRIDGE));
3143 tt_int_op(1, ==, purpose_needs_anonymity(0, ROUTER_PURPOSE_GENERAL));
3144 tt_int_op(0, ==, purpose_needs_anonymity(DIR_PURPOSE_FETCH_MICRODESC,
3145 ROUTER_PURPOSE_GENERAL));
3146 done: ;
3149 static void
3150 test_dir_fetch_type(void *arg)
3152 (void)arg;
3153 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
3154 NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
3155 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
3156 NULL), OP_EQ, EXTRAINFO_DIRINFO | V3_DIRINFO);
3158 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC, ROUTER_PURPOSE_BRIDGE,
3159 NULL), OP_EQ, BRIDGE_DIRINFO);
3160 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_SERVERDESC,
3161 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3163 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_STATUS_VOTE,
3164 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3165 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
3166 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3167 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CERTIFICATE,
3168 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, V3_DIRINFO);
3170 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
3171 "microdesc"), OP_EQ, V3_DIRINFO|MICRODESC_DIRINFO);
3172 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_CONSENSUS, ROUTER_PURPOSE_GENERAL,
3173 NULL), OP_EQ, V3_DIRINFO);
3175 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
3176 NULL), OP_EQ, MICRODESC_DIRINFO);
3178 tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
3179 ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
3180 done: ;
3183 static void
3184 test_dir_packages(void *arg)
3186 smartlist_t *votes = smartlist_new();
3187 char *res = NULL;
3188 (void)arg;
3190 #define BAD(s) \
3191 tt_int_op(0, ==, validate_recommended_package_line(s));
3192 #define GOOD(s) \
3193 tt_int_op(1, ==, validate_recommended_package_line(s));
3194 GOOD("tor 0.2.6.3-alpha "
3195 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3196 "sha256=sssdlkfjdsklfjdskfljasdklfj");
3197 GOOD("tor 0.2.6.3-alpha "
3198 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3199 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b=fred");
3200 BAD("tor 0.2.6.3-alpha "
3201 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3202 "sha256=sssdlkfjdsklfjdskfljasdklfj=");
3203 BAD("tor 0.2.6.3-alpha "
3204 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz "
3205 "sha256=sssdlkfjdsklfjdskfljasdklfj blake2b");
3206 BAD("tor 0.2.6.3-alpha "
3207 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz ");
3208 BAD("tor 0.2.6.3-alpha "
3209 "http://torproject.example.com/dist/tor-0.2.6.3-alpha.tar.gz");
3210 BAD("tor 0.2.6.3-alpha ");
3211 BAD("tor 0.2.6.3-alpha");
3212 BAD("tor ");
3213 BAD("tor");
3214 BAD("");
3215 BAD("=foobar sha256="
3216 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3217 BAD("= = sha256="
3218 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3220 BAD("sha512= sha256="
3221 "3c179f46ca77069a6a0bac70212a9b3b838b2f66129cb52d568837fc79d8fcc7");
3223 smartlist_add(votes, tor_malloc_zero(sizeof(networkstatus_t)));
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_FOREACH(votes, networkstatus_t *, ns,
3230 ns->package_lines = smartlist_new());
3232 #define ADD(i, s) \
3233 smartlist_add(((networkstatus_t*)smartlist_get(votes, (i)))->package_lines, \
3234 (void*)(s));
3236 /* Only one vote for this one. */
3237 ADD(4, "cisco 99z http://foobar.example.com/ sha256=blahblah");
3239 /* Only two matching entries for this one, but 3 voters */
3240 ADD(1, "mystic 99y http://barfoo.example.com/ sha256=blahblah");
3241 ADD(3, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3242 ADD(4, "mystic 99y http://foobar.example.com/ sha256=blahblah");
3244 /* Only two matching entries for this one, but at least 4 voters */
3245 ADD(1, "mystic 99p http://barfoo.example.com/ sha256=ggggggg");
3246 ADD(3, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3247 ADD(4, "mystic 99p http://foobar.example.com/ sha256=blahblah");
3248 ADD(5, "mystic 99p http://foobar.example.com/ sha256=ggggggg");
3250 /* This one has only invalid votes. */
3251 ADD(0, "haffenreffer 1.2 http://foobar.example.com/ sha256");
3252 ADD(1, "haffenreffer 1.2 http://foobar.example.com/ ");
3253 ADD(2, "haffenreffer 1.2 ");
3254 ADD(3, "haffenreffer ");
3255 ADD(4, "haffenreffer");
3257 /* Three matching votes for this; it should actually go in! */
3258 ADD(2, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3259 ADD(3, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3260 ADD(4, "element 0.66.1 http://quux.example.com/ sha256=abcdef");
3261 ADD(1, "element 0.66.1 http://quum.example.com/ sha256=abcdef");
3262 ADD(0, "element 0.66.1 http://quux.example.com/ sha256=abcde");
3264 /* Three votes for A, three votes for B */
3265 ADD(0, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3266 ADD(1, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3267 ADD(2, "clownshoes 22alpha1 http://quumble.example.com/ blake2=foob");
3268 ADD(3, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3269 ADD(4, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3270 ADD(5, "clownshoes 22alpha1 http://quumble.example.com/ blake2=fooz");
3272 /* Three votes for A, two votes for B */
3273 ADD(1, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3274 ADD(2, "clownshoes 22alpha3 http://quumble.example.com/ blake2=foob");
3275 ADD(3, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3276 ADD(4, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3277 ADD(5, "clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz");
3279 /* Four votes for A, two for B. */
3280 ADD(0, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3281 ADD(1, "clownshoes 22alpha4 http://quumble.example.com/ blake2=foob");
3282 ADD(2, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3283 ADD(3, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3284 ADD(4, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3285 ADD(5, "clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa");
3287 /* Five votes for A ... all from the same authority. Three for B. */
3288 ADD(0, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3289 ADD(1, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3290 ADD(3, "cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m");
3291 ADD(2, "cbc 99.1.11.1.1 http://example.com/ cubehash=ahooy");
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");
3297 /* As above but new replaces old: no two match. */
3298 ADD(0, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3299 ADD(1, "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/x cubehash=ahooy sha512=m");
3301 ADD(2, "cbc 99.1.11.1.2 http://example.com/cbc/ cubehash=ahooy sha512=m");
3302 ADD(2, "cbc 99.1.11.1.2 http://example.com/ cubehash=ahooy");
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");
3308 res = compute_consensus_package_lines(votes);
3309 tt_assert(res);
3310 tt_str_op(res, ==,
3311 "package cbc 99.1.11.1.1 http://example.com/cbc/ cubehash=ahooy sha512=m\n"
3312 "package clownshoes 22alpha3 http://quumble.example.com/ blake2=fooz\n"
3313 "package clownshoes 22alpha4 http://quumble.example.cam/ blake2=fooa\n"
3314 "package element 0.66.1 http://quux.example.com/ sha256=abcdef\n"
3315 "package mystic 99y http://foobar.example.com/ sha256=blahblah\n"
3318 #undef ADD
3319 #undef BAD
3320 #undef GOOD
3321 done:
3322 SMARTLIST_FOREACH(votes, networkstatus_t *, ns,
3323 { smartlist_free(ns->package_lines); tor_free(ns); });
3324 smartlist_free(votes);
3325 tor_free(res);
3328 static void
3329 test_dir_download_status_schedule(void *arg)
3331 (void)arg;
3332 download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
3333 DL_WANT_AUTHORITY,
3334 DL_SCHED_INCREMENT_FAILURE };
3335 download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_CONSENSUS,
3336 DL_WANT_ANY_DIRSERVER,
3337 DL_SCHED_INCREMENT_ATTEMPT};
3338 download_status_t dls_bridge = { 0, 0, 0, DL_SCHED_BRIDGE,
3339 DL_WANT_AUTHORITY,
3340 DL_SCHED_INCREMENT_FAILURE};
3341 int increment = -1;
3342 int expected_increment = -1;
3343 time_t current_time = time(NULL);
3344 int delay1 = -1;
3345 int delay2 = -1;
3346 smartlist_t *schedule = smartlist_new();
3348 /* Make a dummy schedule */
3349 smartlist_add(schedule, (void *)&delay1);
3350 smartlist_add(schedule, (void *)&delay2);
3352 /* check a range of values */
3353 delay1 = 1000;
3354 increment = download_status_schedule_get_delay(&dls_failure,
3355 schedule,
3356 TIME_MIN);
3357 expected_increment = delay1;
3358 tt_assert(increment == expected_increment);
3359 tt_assert(dls_failure.next_attempt_at == TIME_MIN + expected_increment);
3361 delay1 = INT_MAX;
3362 increment = download_status_schedule_get_delay(&dls_failure,
3363 schedule,
3364 -1);
3365 expected_increment = delay1;
3366 tt_assert(increment == expected_increment);
3367 tt_assert(dls_failure.next_attempt_at == TIME_MAX);
3369 delay1 = 0;
3370 increment = download_status_schedule_get_delay(&dls_attempt,
3371 schedule,
3373 expected_increment = delay1;
3374 tt_assert(increment == expected_increment);
3375 tt_assert(dls_attempt.next_attempt_at == 0 + expected_increment);
3377 delay1 = 1000;
3378 increment = download_status_schedule_get_delay(&dls_attempt,
3379 schedule,
3381 expected_increment = delay1;
3382 tt_assert(increment == expected_increment);
3383 tt_assert(dls_attempt.next_attempt_at == 1 + expected_increment);
3385 delay1 = INT_MAX;
3386 increment = download_status_schedule_get_delay(&dls_bridge,
3387 schedule,
3388 current_time);
3389 expected_increment = delay1;
3390 tt_assert(increment == expected_increment);
3391 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3393 delay1 = 1;
3394 increment = download_status_schedule_get_delay(&dls_bridge,
3395 schedule,
3396 TIME_MAX);
3397 expected_increment = delay1;
3398 tt_assert(increment == expected_increment);
3399 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3401 /* see what happens when we reach the end */
3402 dls_attempt.n_download_attempts++;
3403 dls_bridge.n_download_failures++;
3405 delay2 = 100;
3406 increment = download_status_schedule_get_delay(&dls_attempt,
3407 schedule,
3408 current_time);
3409 expected_increment = delay2;
3410 tt_assert(increment == expected_increment);
3411 tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
3413 delay2 = 1;
3414 increment = download_status_schedule_get_delay(&dls_bridge,
3415 schedule,
3416 current_time);
3417 expected_increment = delay2;
3418 tt_assert(increment == expected_increment);
3419 tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
3421 /* see what happens when we try to go off the end */
3422 dls_attempt.n_download_attempts++;
3423 dls_bridge.n_download_failures++;
3425 delay2 = 5;
3426 increment = download_status_schedule_get_delay(&dls_attempt,
3427 schedule,
3428 current_time);
3429 expected_increment = delay2;
3430 tt_assert(increment == expected_increment);
3431 tt_assert(dls_attempt.next_attempt_at == current_time + delay2);
3433 delay2 = 17;
3434 increment = download_status_schedule_get_delay(&dls_bridge,
3435 schedule,
3436 current_time);
3437 expected_increment = delay2;
3438 tt_assert(increment == expected_increment);
3439 tt_assert(dls_bridge.next_attempt_at == current_time + delay2);
3441 /* see what happens when we reach IMPOSSIBLE_TO_DOWNLOAD */
3442 dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD;
3443 dls_bridge.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD;
3445 delay2 = 35;
3446 increment = download_status_schedule_get_delay(&dls_attempt,
3447 schedule,
3448 current_time);
3449 expected_increment = INT_MAX;
3450 tt_assert(increment == expected_increment);
3451 tt_assert(dls_attempt.next_attempt_at == TIME_MAX);
3453 delay2 = 99;
3454 increment = download_status_schedule_get_delay(&dls_bridge,
3455 schedule,
3456 current_time);
3457 expected_increment = INT_MAX;
3458 tt_assert(increment == expected_increment);
3459 tt_assert(dls_bridge.next_attempt_at == TIME_MAX);
3461 done:
3462 /* the pointers in schedule are allocated on the stack */
3463 smartlist_free(schedule);
3466 static void
3467 test_dir_download_status_increment(void *arg)
3469 (void)arg;
3470 download_status_t dls_failure = { 0, 0, 0, DL_SCHED_GENERIC,
3471 DL_WANT_AUTHORITY,
3472 DL_SCHED_INCREMENT_FAILURE };
3473 download_status_t dls_attempt = { 0, 0, 0, DL_SCHED_BRIDGE,
3474 DL_WANT_ANY_DIRSERVER,
3475 DL_SCHED_INCREMENT_ATTEMPT};
3476 int delay0 = -1;
3477 int delay1 = -1;
3478 int delay2 = -1;
3479 smartlist_t *schedule = smartlist_new();
3480 or_options_t test_options;
3481 time_t next_at = TIME_MAX;
3482 time_t current_time = time(NULL);
3484 /* Provide some values for the schedule */
3485 delay0 = 10;
3486 delay1 = 99;
3487 delay2 = 20;
3489 /* Make the schedule */
3490 smartlist_add(schedule, (void *)&delay0);
3491 smartlist_add(schedule, (void *)&delay1);
3492 smartlist_add(schedule, (void *)&delay2);
3494 /* Put it in the options */
3495 mock_options = &test_options;
3496 reset_options(mock_options, &mock_get_options_calls);
3497 mock_options->TestingClientDownloadSchedule = schedule;
3498 mock_options->TestingBridgeDownloadSchedule = schedule;
3500 MOCK(get_options, mock_get_options);
3502 /* Check that a failure reset works */
3503 mock_get_options_calls = 0;
3504 download_status_reset(&dls_failure);
3505 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3506 * an unrealiable test, because time(NULL) might change. */
3507 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3508 >= current_time + delay0);
3509 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3510 != TIME_MAX);
3511 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3512 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3513 tt_assert(mock_get_options_calls >= 1);
3515 /* avoid timing inconsistencies */
3516 dls_failure.next_attempt_at = current_time + delay0;
3518 /* check that a reset schedule becomes ready at the right time */
3519 tt_assert(download_status_is_ready(&dls_failure,
3520 current_time + delay0 - 1,
3521 1) == 0);
3522 tt_assert(download_status_is_ready(&dls_failure,
3523 current_time + delay0,
3524 1) == 1);
3525 tt_assert(download_status_is_ready(&dls_failure,
3526 current_time + delay0 + 1,
3527 1) == 1);
3529 /* Check that a failure increment works */
3530 mock_get_options_calls = 0;
3531 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3532 current_time);
3533 tt_assert(next_at == current_time + delay1);
3534 tt_assert(download_status_get_n_failures(&dls_failure) == 1);
3535 tt_assert(download_status_get_n_attempts(&dls_failure) == 1);
3536 tt_assert(mock_get_options_calls >= 1);
3538 /* check that an incremented schedule becomes ready at the right time */
3539 tt_assert(download_status_is_ready(&dls_failure,
3540 current_time + delay1 - 1,
3541 1) == 0);
3542 tt_assert(download_status_is_ready(&dls_failure,
3543 current_time + delay1,
3544 1) == 1);
3545 tt_assert(download_status_is_ready(&dls_failure,
3546 current_time + delay1 + 1,
3547 1) == 1);
3549 /* check that a schedule isn't ready if it's had too many failures */
3550 tt_assert(download_status_is_ready(&dls_failure,
3551 current_time + delay1 + 10,
3552 0) == 0);
3554 /* Check that failure increments don't happen on 503 for clients, but that
3555 * attempt increments do. */
3556 mock_get_options_calls = 0;
3557 next_at = download_status_increment_failure(&dls_failure, 503, "test", 0,
3558 current_time);
3559 tt_assert(next_at == current_time + delay1);
3560 tt_assert(download_status_get_n_failures(&dls_failure) == 1);
3561 tt_assert(download_status_get_n_attempts(&dls_failure) == 2);
3562 tt_assert(mock_get_options_calls >= 1);
3564 /* Check that failure increments do happen on 503 for servers */
3565 mock_get_options_calls = 0;
3566 next_at = download_status_increment_failure(&dls_failure, 503, "test", 1,
3567 current_time);
3568 tt_assert(next_at == current_time + delay2);
3569 tt_assert(download_status_get_n_failures(&dls_failure) == 2);
3570 tt_assert(download_status_get_n_attempts(&dls_failure) == 3);
3571 tt_assert(mock_get_options_calls >= 1);
3573 /* Check what happens when we run off the end of the schedule */
3574 mock_get_options_calls = 0;
3575 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3576 current_time);
3577 tt_assert(next_at == current_time + delay2);
3578 tt_assert(download_status_get_n_failures(&dls_failure) == 3);
3579 tt_assert(download_status_get_n_attempts(&dls_failure) == 4);
3580 tt_assert(mock_get_options_calls >= 1);
3582 /* Check what happens when we hit the failure limit */
3583 mock_get_options_calls = 0;
3584 download_status_mark_impossible(&dls_failure);
3585 next_at = download_status_increment_failure(&dls_failure, 404, "test", 0,
3586 current_time);
3587 tt_assert(next_at == TIME_MAX);
3588 tt_assert(download_status_get_n_failures(&dls_failure)
3589 == IMPOSSIBLE_TO_DOWNLOAD);
3590 tt_assert(download_status_get_n_attempts(&dls_failure)
3591 == IMPOSSIBLE_TO_DOWNLOAD);
3592 tt_assert(mock_get_options_calls >= 1);
3594 /* Check that a failure reset doesn't reset at the limit */
3595 mock_get_options_calls = 0;
3596 download_status_reset(&dls_failure);
3597 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3598 == TIME_MAX);
3599 tt_assert(download_status_get_n_failures(&dls_failure)
3600 == IMPOSSIBLE_TO_DOWNLOAD);
3601 tt_assert(download_status_get_n_attempts(&dls_failure)
3602 == IMPOSSIBLE_TO_DOWNLOAD);
3603 tt_assert(mock_get_options_calls == 0);
3605 /* Check that a failure reset resets just before the limit */
3606 mock_get_options_calls = 0;
3607 dls_failure.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
3608 dls_failure.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
3609 download_status_reset(&dls_failure);
3610 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3611 * an unrealiable test, because time(NULL) might change. */
3612 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3613 >= current_time + delay0);
3614 tt_assert(download_status_get_next_attempt_at(&dls_failure)
3615 != TIME_MAX);
3616 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3617 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3618 tt_assert(mock_get_options_calls >= 1);
3620 /* Check that failure increments do happen on attempt-based schedules,
3621 * but that the retry is set at the end of time */
3622 mock_get_options_calls = 0;
3623 next_at = download_status_increment_failure(&dls_attempt, 404, "test", 0,
3624 current_time);
3625 tt_assert(next_at == TIME_MAX);
3626 tt_assert(download_status_get_n_failures(&dls_attempt) == 1);
3627 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3628 tt_assert(mock_get_options_calls == 0);
3630 /* Check that an attempt reset works */
3631 mock_get_options_calls = 0;
3632 download_status_reset(&dls_attempt);
3633 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3634 * an unrealiable test, because time(NULL) might change. */
3635 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3636 >= current_time + delay0);
3637 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3638 != TIME_MAX);
3639 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3640 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3641 tt_assert(mock_get_options_calls >= 1);
3643 /* avoid timing inconsistencies */
3644 dls_attempt.next_attempt_at = current_time + delay0;
3646 /* check that a reset schedule becomes ready at the right time */
3647 tt_assert(download_status_is_ready(&dls_attempt,
3648 current_time + delay0 - 1,
3649 1) == 0);
3650 tt_assert(download_status_is_ready(&dls_attempt,
3651 current_time + delay0,
3652 1) == 1);
3653 tt_assert(download_status_is_ready(&dls_attempt,
3654 current_time + delay0 + 1,
3655 1) == 1);
3657 /* Check that an attempt increment works */
3658 mock_get_options_calls = 0;
3659 next_at = download_status_increment_attempt(&dls_attempt, "test",
3660 current_time);
3661 tt_assert(next_at == current_time + delay1);
3662 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3663 tt_assert(download_status_get_n_attempts(&dls_attempt) == 1);
3664 tt_assert(mock_get_options_calls >= 1);
3666 /* check that an incremented schedule becomes ready at the right time */
3667 tt_assert(download_status_is_ready(&dls_attempt,
3668 current_time + delay1 - 1,
3669 1) == 0);
3670 tt_assert(download_status_is_ready(&dls_attempt,
3671 current_time + delay1,
3672 1) == 1);
3673 tt_assert(download_status_is_ready(&dls_attempt,
3674 current_time + delay1 + 1,
3675 1) == 1);
3677 /* check that a schedule isn't ready if it's had too many attempts */
3678 tt_assert(download_status_is_ready(&dls_attempt,
3679 current_time + delay1 + 10,
3680 0) == 0);
3682 /* Check what happens when we reach then run off the end of the schedule */
3683 mock_get_options_calls = 0;
3684 next_at = download_status_increment_attempt(&dls_attempt, "test",
3685 current_time);
3686 tt_assert(next_at == current_time + delay2);
3687 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3688 tt_assert(download_status_get_n_attempts(&dls_attempt) == 2);
3689 tt_assert(mock_get_options_calls >= 1);
3691 mock_get_options_calls = 0;
3692 next_at = download_status_increment_attempt(&dls_attempt, "test",
3693 current_time);
3694 tt_assert(next_at == current_time + delay2);
3695 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3696 tt_assert(download_status_get_n_attempts(&dls_attempt) == 3);
3697 tt_assert(mock_get_options_calls >= 1);
3699 /* Check what happens when we hit the attempt limit */
3700 mock_get_options_calls = 0;
3701 download_status_mark_impossible(&dls_attempt);
3702 next_at = download_status_increment_attempt(&dls_attempt, "test",
3703 current_time);
3704 tt_assert(next_at == TIME_MAX);
3705 tt_assert(download_status_get_n_failures(&dls_attempt)
3706 == IMPOSSIBLE_TO_DOWNLOAD);
3707 tt_assert(download_status_get_n_attempts(&dls_attempt)
3708 == IMPOSSIBLE_TO_DOWNLOAD);
3709 tt_assert(mock_get_options_calls >= 1);
3711 /* Check that an attempt reset doesn't reset at the limit */
3712 mock_get_options_calls = 0;
3713 download_status_reset(&dls_attempt);
3714 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3715 == TIME_MAX);
3716 tt_assert(download_status_get_n_failures(&dls_attempt)
3717 == IMPOSSIBLE_TO_DOWNLOAD);
3718 tt_assert(download_status_get_n_attempts(&dls_attempt)
3719 == IMPOSSIBLE_TO_DOWNLOAD);
3720 tt_assert(mock_get_options_calls == 0);
3722 /* Check that an attempt reset resets just before the limit */
3723 mock_get_options_calls = 0;
3724 dls_attempt.n_download_failures = IMPOSSIBLE_TO_DOWNLOAD - 1;
3725 dls_attempt.n_download_attempts = IMPOSSIBLE_TO_DOWNLOAD - 1;
3726 download_status_reset(&dls_attempt);
3727 /* we really want to test that it's equal to time(NULL) + delay0, but that's
3728 * an unrealiable test, because time(NULL) might change. */
3729 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3730 >= current_time + delay0);
3731 tt_assert(download_status_get_next_attempt_at(&dls_attempt)
3732 != TIME_MAX);
3733 tt_assert(download_status_get_n_failures(&dls_attempt) == 0);
3734 tt_assert(download_status_get_n_attempts(&dls_attempt) == 0);
3735 tt_assert(mock_get_options_calls >= 1);
3737 /* Check that attempt increments don't happen on failure-based schedules,
3738 * and that the attempt is set at the end of time */
3739 mock_get_options_calls = 0;
3740 next_at = download_status_increment_attempt(&dls_failure, "test",
3741 current_time);
3742 tt_assert(next_at == TIME_MAX);
3743 tt_assert(download_status_get_n_failures(&dls_failure) == 0);
3744 tt_assert(download_status_get_n_attempts(&dls_failure) == 0);
3745 tt_assert(mock_get_options_calls == 0);
3747 done:
3748 /* the pointers in schedule are allocated on the stack */
3749 smartlist_free(schedule);
3750 UNMOCK(get_options);
3751 mock_options = NULL;
3752 mock_get_options_calls = 0;
3755 static void
3756 test_dir_authdir_type_to_string(void *data)
3758 (void)data;
3759 char *res;
3761 tt_str_op(res = authdir_type_to_string(NO_DIRINFO), OP_EQ,
3762 "[Not an authority]");
3763 tor_free(res);
3765 tt_str_op(res = authdir_type_to_string(EXTRAINFO_DIRINFO), OP_EQ,
3766 "[Not an authority]");
3767 tor_free(res);
3769 tt_str_op(res = authdir_type_to_string(MICRODESC_DIRINFO), OP_EQ,
3770 "[Not an authority]");
3771 tor_free(res);
3773 tt_str_op(res = authdir_type_to_string(V3_DIRINFO), OP_EQ, "V3");
3774 tor_free(res);
3776 tt_str_op(res = authdir_type_to_string(BRIDGE_DIRINFO), OP_EQ, "Bridge");
3777 tor_free(res);
3779 tt_str_op(res = authdir_type_to_string(
3780 V3_DIRINFO | BRIDGE_DIRINFO | EXTRAINFO_DIRINFO), OP_EQ,
3781 "V3, Bridge");
3782 done:
3783 tor_free(res);
3786 static void
3787 test_dir_conn_purpose_to_string(void *data)
3789 (void)data;
3791 #define EXPECT_CONN_PURPOSE(purpose, expected) \
3792 tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
3794 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_DIR, "server descriptor upload");
3795 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_VOTE, "server vote upload");
3796 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_SIGNATURES,
3797 "consensus signature upload");
3798 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_SERVERDESC, "server descriptor fetch");
3799 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_EXTRAINFO, "extra-info fetch");
3800 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CONSENSUS,
3801 "consensus network-status fetch");
3802 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_CERTIFICATE, "authority cert fetch");
3803 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_STATUS_VOTE, "status vote fetch");
3804 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
3805 "consensus signature fetch");
3806 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_RENDDESC_V2,
3807 "hidden-service v2 descriptor fetch");
3808 EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
3809 "hidden-service v2 descriptor upload");
3810 EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
3811 EXPECT_CONN_PURPOSE(1024, "(unknown)");
3813 done: ;
3816 NS_DECL(int,
3817 public_server_mode, (const or_options_t *options));
3819 static int
3820 NS(public_server_mode)(const or_options_t *options)
3822 (void)options;
3824 if (CALLED(public_server_mode)++ == 0) {
3825 return 1;
3828 return 0;
3831 static void
3832 test_dir_should_use_directory_guards(void *data)
3834 or_options_t *options;
3835 char *errmsg = NULL;
3836 (void)data;
3838 NS_MOCK(public_server_mode);
3840 options = options_new();
3841 options_init(options);
3843 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3844 tt_int_op(CALLED(public_server_mode), OP_EQ, 1);
3846 options->UseEntryGuardsAsDirGuards = 1;
3847 options->UseEntryGuards = 1;
3848 options->DownloadExtraInfo = 0;
3849 options->FetchDirInfoEarly = 0;
3850 options->FetchDirInfoExtraEarly = 0;
3851 options->FetchUselessDescriptors = 0;
3852 tt_int_op(should_use_directory_guards(options), OP_EQ, 1);
3853 tt_int_op(CALLED(public_server_mode), OP_EQ, 2);
3855 options->UseEntryGuards = 0;
3856 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3857 tt_int_op(CALLED(public_server_mode), OP_EQ, 3);
3858 options->UseEntryGuards = 1;
3860 options->UseEntryGuardsAsDirGuards = 0;
3861 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3862 tt_int_op(CALLED(public_server_mode), OP_EQ, 4);
3863 options->UseEntryGuardsAsDirGuards = 1;
3865 options->DownloadExtraInfo = 1;
3866 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3867 tt_int_op(CALLED(public_server_mode), OP_EQ, 5);
3868 options->DownloadExtraInfo = 0;
3870 options->FetchDirInfoEarly = 1;
3871 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3872 tt_int_op(CALLED(public_server_mode), OP_EQ, 6);
3873 options->FetchDirInfoEarly = 0;
3875 options->FetchDirInfoExtraEarly = 1;
3876 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3877 tt_int_op(CALLED(public_server_mode), OP_EQ, 7);
3878 options->FetchDirInfoExtraEarly = 0;
3880 options->FetchUselessDescriptors = 1;
3881 tt_int_op(should_use_directory_guards(options), OP_EQ, 0);
3882 tt_int_op(CALLED(public_server_mode), OP_EQ, 8);
3883 options->FetchUselessDescriptors = 0;
3885 done:
3886 NS_UNMOCK(public_server_mode);
3887 or_options_free(options);
3888 tor_free(errmsg);
3891 NS_DECL(void,
3892 directory_initiate_command_routerstatus, (const routerstatus_t *status,
3893 uint8_t dir_purpose,
3894 uint8_t router_purpose,
3895 dir_indirection_t indirection,
3896 const char *resource,
3897 const char *payload,
3898 size_t payload_len,
3899 time_t if_modified_since));
3901 static void
3902 test_dir_should_not_init_request_to_ourselves(void *data)
3904 char digest[DIGEST_LEN];
3905 dir_server_t *ourself = NULL;
3906 crypto_pk_t *key = pk_generate(2);
3907 (void) data;
3909 NS_MOCK(directory_initiate_command_routerstatus);
3911 clear_dir_servers();
3912 routerlist_free_all();
3914 set_server_identity_key(key);
3915 crypto_pk_get_digest(key, (char*) &digest);
3916 ourself = trusted_dir_server_new("ourself", "127.0.0.1", 9059, 9060,
3917 NULL, digest,
3918 NULL, V3_DIRINFO, 1.0);
3920 tt_assert(ourself);
3921 dir_server_add(ourself);
3923 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3924 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3926 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3927 NULL);
3929 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3931 done:
3932 NS_UNMOCK(directory_initiate_command_routerstatus);
3933 clear_dir_servers();
3934 routerlist_free_all();
3935 crypto_pk_free(key);
3938 static void
3939 test_dir_should_not_init_request_to_dir_auths_without_v3_info(void *data)
3941 dir_server_t *ds = NULL;
3942 dirinfo_type_t dirinfo_type = BRIDGE_DIRINFO | EXTRAINFO_DIRINFO \
3943 | MICRODESC_DIRINFO;
3944 (void) data;
3946 NS_MOCK(directory_initiate_command_routerstatus);
3948 clear_dir_servers();
3949 routerlist_free_all();
3951 ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
3952 "12345678901234567890", NULL, dirinfo_type, 1.0);
3953 tt_assert(ds);
3954 dir_server_add(ds);
3956 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3957 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3959 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3960 NULL);
3961 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 0);
3963 done:
3964 NS_UNMOCK(directory_initiate_command_routerstatus);
3965 clear_dir_servers();
3966 routerlist_free_all();
3969 static void
3970 test_dir_should_init_request_to_dir_auths(void *data)
3972 dir_server_t *ds = NULL;
3973 (void) data;
3975 NS_MOCK(directory_initiate_command_routerstatus);
3977 clear_dir_servers();
3978 routerlist_free_all();
3980 ds = trusted_dir_server_new("ds", "10.0.0.1", 9059, 9060, NULL,
3981 "12345678901234567890", NULL, V3_DIRINFO, 1.0);
3982 tt_assert(ds);
3983 dir_server_add(ds);
3985 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE, 0, NULL);
3986 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 1);
3988 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES, 0,
3989 NULL);
3990 tt_int_op(CALLED(directory_initiate_command_routerstatus), OP_EQ, 2);
3992 done:
3993 NS_UNMOCK(directory_initiate_command_routerstatus);
3994 clear_dir_servers();
3995 routerlist_free_all();
3998 void
3999 NS(directory_initiate_command_routerstatus)(const routerstatus_t *status,
4000 uint8_t dir_purpose,
4001 uint8_t router_purpose,
4002 dir_indirection_t indirection,
4003 const char *resource,
4004 const char *payload,
4005 size_t payload_len,
4006 time_t if_modified_since)
4008 (void)status;
4009 (void)dir_purpose;
4010 (void)router_purpose;
4011 (void)indirection;
4012 (void)resource;
4013 (void)payload;
4014 (void)payload_len;
4015 (void)if_modified_since;
4016 CALLED(directory_initiate_command_routerstatus)++;
4019 static void
4020 test_dir_choose_compression_level(void* data)
4022 (void)data;
4024 /* It starts under_memory_pressure */
4025 tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);
4027 tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
4028 tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
4029 tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
4030 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
4032 /* Reset under_memory_pressure timer */
4033 cell_queues_check_size();
4034 tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);
4036 tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
4037 tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
4038 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
4039 tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));
4041 done: ;
4044 static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
4045 static int
4046 mock_networkstatus_consensus_is_bootstrapping(time_t now)
4048 (void)now;
4049 return mock_networkstatus_consensus_is_bootstrapping_value;
4052 static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
4053 static int
4054 mock_networkstatus_consensus_can_use_extra_fallbacks(
4055 const or_options_t *options)
4057 (void)options;
4058 return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
4061 /* data is a 2 character nul-terminated string.
4062 * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
4063 * If data[1] is 'f', set extra fallbacks, anything else means no extra
4064 * fallbacks.
4066 static void
4067 test_dir_find_dl_schedule(void* data)
4069 const char *str = (const char *)data;
4071 tt_assert(strlen(data) == 2);
4073 if (str[0] == 'b') {
4074 mock_networkstatus_consensus_is_bootstrapping_value = 1;
4075 } else {
4076 mock_networkstatus_consensus_is_bootstrapping_value = 0;
4079 if (str[1] == 'f') {
4080 mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
4081 } else {
4082 mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
4085 MOCK(networkstatus_consensus_is_bootstrapping,
4086 mock_networkstatus_consensus_is_bootstrapping);
4087 MOCK(networkstatus_consensus_can_use_extra_fallbacks,
4088 mock_networkstatus_consensus_can_use_extra_fallbacks);
4090 download_status_t dls;
4091 smartlist_t server, client, server_cons, client_cons;
4092 smartlist_t client_boot_auth_only_cons, client_boot_auth_cons;
4093 smartlist_t client_boot_fallback_cons, bridge;
4095 mock_options = malloc(sizeof(or_options_t));
4096 reset_options(mock_options, &mock_get_options_calls);
4097 MOCK(get_options, mock_get_options);
4099 mock_options->TestingServerDownloadSchedule = &server;
4100 mock_options->TestingClientDownloadSchedule = &client;
4101 mock_options->TestingServerConsensusDownloadSchedule = &server_cons;
4102 mock_options->TestingClientConsensusDownloadSchedule = &client_cons;
4103 mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule =
4104 &client_boot_auth_only_cons;
4105 mock_options->ClientBootstrapConsensusAuthorityDownloadSchedule =
4106 &client_boot_auth_cons;
4107 mock_options->ClientBootstrapConsensusFallbackDownloadSchedule =
4108 &client_boot_fallback_cons;
4109 mock_options->TestingBridgeDownloadSchedule = &bridge;
4111 dls.schedule = DL_SCHED_GENERIC;
4112 /* client */
4113 mock_options->ClientOnly = 1;
4114 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client);
4115 mock_options->ClientOnly = 0;
4117 /* dir mode */
4118 mock_options->DirPort_set = 1;
4119 mock_options->DirCache = 1;
4120 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server);
4121 mock_options->DirPort_set = 0;
4122 mock_options->DirCache = 0;
4124 dls.schedule = DL_SCHED_CONSENSUS;
4125 /* public server mode */
4126 mock_options->ORPort_set = 1;
4127 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server_cons);
4128 mock_options->ORPort_set = 0;
4130 /* client and bridge modes */
4131 if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
4132 if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
4133 dls.want_authority = 1;
4134 /* client */
4135 mock_options->ClientOnly = 1;
4136 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4137 &client_boot_auth_cons);
4138 mock_options->ClientOnly = 0;
4140 /* bridge relay */
4141 mock_options->ORPort_set = 1;
4142 mock_options->BridgeRelay = 1;
4143 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4144 &client_boot_auth_cons);
4145 mock_options->ORPort_set = 0;
4146 mock_options->BridgeRelay = 0;
4148 dls.want_authority = 0;
4149 /* client */
4150 mock_options->ClientOnly = 1;
4151 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4152 &client_boot_fallback_cons);
4153 mock_options->ClientOnly = 0;
4155 /* bridge relay */
4156 mock_options->ORPort_set = 1;
4157 mock_options->BridgeRelay = 1;
4158 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4159 &client_boot_fallback_cons);
4160 mock_options->ORPort_set = 0;
4161 mock_options->BridgeRelay = 0;
4163 } else {
4164 /* dls.want_authority is ignored */
4165 /* client */
4166 mock_options->ClientOnly = 1;
4167 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4168 &client_boot_auth_only_cons);
4169 mock_options->ClientOnly = 0;
4171 /* bridge relay */
4172 mock_options->ORPort_set = 1;
4173 mock_options->BridgeRelay = 1;
4174 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4175 &client_boot_auth_only_cons);
4176 mock_options->ORPort_set = 0;
4177 mock_options->BridgeRelay = 0;
4179 } else {
4180 /* client */
4181 mock_options->ClientOnly = 1;
4182 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4183 &client_cons);
4184 mock_options->ClientOnly = 0;
4186 /* bridge relay */
4187 mock_options->ORPort_set = 1;
4188 mock_options->BridgeRelay = 1;
4189 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
4190 &client_cons);
4191 mock_options->ORPort_set = 0;
4192 mock_options->BridgeRelay = 0;
4195 dls.schedule = DL_SCHED_BRIDGE;
4196 /* client */
4197 mock_options->ClientOnly = 1;
4198 tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
4200 done:
4201 UNMOCK(networkstatus_consensus_is_bootstrapping);
4202 UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
4203 UNMOCK(get_options);
4204 free(mock_options);
4205 mock_options = NULL;
4208 #define DIR_LEGACY(name) \
4209 { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
4211 #define DIR(name,flags) \
4212 { #name, test_dir_##name, (flags), NULL, NULL }
4214 /* where arg is a string constant */
4215 #define DIR_ARG(name,flags,arg) \
4216 { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, (void*) arg }
4218 struct testcase_t dir_tests[] = {
4219 DIR_LEGACY(nicknames),
4220 DIR_LEGACY(formats),
4221 DIR(routerinfo_parsing, 0),
4222 DIR(extrainfo_parsing, 0),
4223 DIR(parse_router_list, TT_FORK),
4224 DIR(load_routers, TT_FORK),
4225 DIR(load_extrainfo, TT_FORK),
4226 DIR_LEGACY(versions),
4227 DIR_LEGACY(fp_pairs),
4228 DIR(split_fps, 0),
4229 DIR_LEGACY(measured_bw_kb),
4230 DIR_LEGACY(measured_bw_kb_cache),
4231 DIR_LEGACY(param_voting),
4232 DIR_LEGACY(v3_networkstatus),
4233 DIR(random_weighted, 0),
4234 DIR(scale_bw, 0),
4235 DIR_LEGACY(clip_unmeasured_bw_kb),
4236 DIR_LEGACY(clip_unmeasured_bw_kb_alt),
4237 DIR(fmt_control_ns, 0),
4238 DIR(dirserv_set_routerstatus_testing, 0),
4239 DIR(http_handling, 0),
4240 DIR(purpose_needs_anonymity, 0),
4241 DIR(fetch_type, 0),
4242 DIR(packages, 0),
4243 DIR(download_status_schedule, 0),
4244 DIR(download_status_increment, 0),
4245 DIR(authdir_type_to_string, 0),
4246 DIR(conn_purpose_to_string, 0),
4247 DIR(should_use_directory_guards, 0),
4248 DIR(should_not_init_request_to_ourselves, TT_FORK),
4249 DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
4250 DIR(should_init_request_to_dir_auths, 0),
4251 DIR(choose_compression_level, 0),
4252 DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
4253 DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
4254 DIR_ARG(find_dl_schedule, TT_FORK, "cf"),
4255 DIR_ARG(find_dl_schedule, TT_FORK, "ca"),
4256 END_OF_TESTCASES