Add unit test for unmeasured bandwidth clipping in consensus
[tor.git] / src / test / test_dir.c
blob269da22fe87b24a5dd6c3a07b2daf66bec4f408a
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
7 #include <math.h>
9 #define DIRSERV_PRIVATE
10 #define DIRVOTE_PRIVATE
11 #define ROUTER_PRIVATE
12 #define ROUTERLIST_PRIVATE
13 #define HIBERNATE_PRIVATE
14 #include "or.h"
15 #include "directory.h"
16 #include "dirserv.h"
17 #include "dirvote.h"
18 #include "hibernate.h"
19 #include "networkstatus.h"
20 #include "router.h"
21 #include "routerlist.h"
22 #include "routerparse.h"
23 #include "test.h"
25 static void
26 test_dir_nicknames(void)
28 test_assert( is_legal_nickname("a"));
29 test_assert(!is_legal_nickname(""));
30 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
31 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
32 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
33 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
34 /* valid */
35 test_assert( is_legal_nickname_or_hexdigest(
36 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
37 test_assert( is_legal_nickname_or_hexdigest(
38 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
39 test_assert( is_legal_nickname_or_hexdigest(
40 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
41 /* too short */
42 test_assert(!is_legal_nickname_or_hexdigest(
43 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
44 /* illegal char */
45 test_assert(!is_legal_nickname_or_hexdigest(
46 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
47 /* hex part too long */
48 test_assert(!is_legal_nickname_or_hexdigest(
49 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
50 test_assert(!is_legal_nickname_or_hexdigest(
51 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
52 /* Bad nickname */
53 test_assert(!is_legal_nickname_or_hexdigest(
54 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
55 test_assert(!is_legal_nickname_or_hexdigest(
56 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
57 test_assert(!is_legal_nickname_or_hexdigest(
58 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
59 test_assert(!is_legal_nickname_or_hexdigest(
60 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
61 "abcdefghijklmnoppqrst"));
62 /* Bad extra char. */
63 test_assert(!is_legal_nickname_or_hexdigest(
64 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
65 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
66 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
67 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
68 done:
72 /** Run unit tests for router descriptor generation logic. */
73 static void
74 test_dir_formats(void)
76 char buf[8192], buf2[8192];
77 char platform[256];
78 char fingerprint[FINGERPRINT_LEN+1];
79 char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
80 size_t pk1_str_len, pk2_str_len, pk3_str_len;
81 routerinfo_t *r1=NULL, *r2=NULL;
82 crypto_pk_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
83 routerinfo_t *rp1 = NULL;
84 addr_policy_t *ex1, *ex2;
85 routerlist_t *dir1 = NULL, *dir2 = NULL;
87 pk1 = pk_generate(0);
88 pk2 = pk_generate(1);
89 pk3 = pk_generate(2);
91 test_assert(pk1 && pk2 && pk3);
93 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
95 get_platform_str(platform, sizeof(platform));
96 r1 = tor_malloc_zero(sizeof(routerinfo_t));
97 r1->address = tor_strdup("18.244.0.1");
98 r1->addr = 0xc0a80001u; /* 192.168.0.1 */
99 r1->cache_info.published_on = 0;
100 r1->or_port = 9000;
101 r1->dir_port = 9003;
102 tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
103 r1->ipv6_orport = 9999;
104 r1->onion_pkey = crypto_pk_dup_key(pk1);
105 r1->identity_pkey = crypto_pk_dup_key(pk2);
106 r1->bandwidthrate = 1000;
107 r1->bandwidthburst = 5000;
108 r1->bandwidthcapacity = 10000;
109 r1->exit_policy = NULL;
110 r1->nickname = tor_strdup("Magri");
111 r1->platform = tor_strdup(platform);
113 ex1 = tor_malloc_zero(sizeof(addr_policy_t));
114 ex2 = tor_malloc_zero(sizeof(addr_policy_t));
115 ex1->policy_type = ADDR_POLICY_ACCEPT;
116 tor_addr_from_ipv4h(&ex1->addr, 0);
117 ex1->maskbits = 0;
118 ex1->prt_min = ex1->prt_max = 80;
119 ex2->policy_type = ADDR_POLICY_REJECT;
120 tor_addr_from_ipv4h(&ex2->addr, 18<<24);
121 ex2->maskbits = 8;
122 ex2->prt_min = ex2->prt_max = 24;
123 r2 = tor_malloc_zero(sizeof(routerinfo_t));
124 r2->address = tor_strdup("1.1.1.1");
125 r2->addr = 0x0a030201u; /* 10.3.2.1 */
126 r2->platform = tor_strdup(platform);
127 r2->cache_info.published_on = 5;
128 r2->or_port = 9005;
129 r2->dir_port = 0;
130 r2->onion_pkey = crypto_pk_dup_key(pk2);
131 r2->identity_pkey = crypto_pk_dup_key(pk1);
132 r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
133 r2->exit_policy = smartlist_new();
134 smartlist_add(r2->exit_policy, ex2);
135 smartlist_add(r2->exit_policy, ex1);
136 r2->nickname = tor_strdup("Fred");
138 test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
139 &pk1_str_len));
140 test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
141 &pk2_str_len));
142 test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
143 &pk3_str_len));
145 memset(buf, 0, 2048);
146 test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
148 strlcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
149 "or-address [1:2:3:4::]:9999\n"
150 "platform Tor "VERSION" on ", sizeof(buf2));
151 strlcat(buf2, get_uname(), sizeof(buf2));
152 strlcat(buf2, "\n"
153 "protocols Link 1 2 Circuit 1\n"
154 "published 1970-01-01 00:00:00\n"
155 "fingerprint ", sizeof(buf2));
156 test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
157 strlcat(buf2, fingerprint, sizeof(buf2));
158 strlcat(buf2, "\nuptime 0\n"
159 /* XXX the "0" above is hard-coded, but even if we made it reflect
160 * uptime, that still wouldn't make it right, because the two
161 * descriptors might be made on different seconds... hm. */
162 "bandwidth 1000 5000 10000\n"
163 "onion-key\n", sizeof(buf2));
164 strlcat(buf2, pk1_str, sizeof(buf2));
165 strlcat(buf2, "signing-key\n", sizeof(buf2));
166 strlcat(buf2, pk2_str, sizeof(buf2));
167 strlcat(buf2, "hidden-service-dir\n", sizeof(buf2));
168 strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
169 buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
170 * twice */
172 test_streq(buf, buf2);
174 test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
175 cp = buf;
176 rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
177 test_assert(rp1);
178 test_streq(rp1->address, r1->address);
179 test_eq(rp1->or_port, r1->or_port);
180 //test_eq(rp1->dir_port, r1->dir_port);
181 test_eq(rp1->bandwidthrate, r1->bandwidthrate);
182 test_eq(rp1->bandwidthburst, r1->bandwidthburst);
183 test_eq(rp1->bandwidthcapacity, r1->bandwidthcapacity);
184 test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
185 test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
186 //test_assert(rp1->exit_policy == NULL);
188 #if 0
189 /* XXX Once we have exit policies, test this again. XXX */
190 strlcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2));
191 strlcat(buf2, pk2_str, sizeof(buf2));
192 strlcat(buf2, "signing-key\n", sizeof(buf2));
193 strlcat(buf2, pk1_str, sizeof(buf2));
194 strlcat(buf2, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2));
195 test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
196 test_streq(buf, buf2);
198 cp = buf;
199 rp2 = router_parse_entry_from_string(&cp,1);
200 test_assert(rp2);
201 test_streq(rp2->address, r2.address);
202 test_eq(rp2->or_port, r2.or_port);
203 test_eq(rp2->dir_port, r2.dir_port);
204 test_eq(rp2->bandwidth, r2.bandwidth);
205 test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
206 test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
207 test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
208 test_streq(rp2->exit_policy->string, "accept *:80");
209 test_streq(rp2->exit_policy->address, "*");
210 test_streq(rp2->exit_policy->port, "80");
211 test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
212 test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
213 test_streq(rp2->exit_policy->next->address, "18.*");
214 test_streq(rp2->exit_policy->next->port, "24");
215 test_assert(rp2->exit_policy->next->next == NULL);
217 /* Okay, now for the directories. */
219 fingerprint_list = smartlist_new();
220 crypto_pk_get_fingerprint(pk2, buf, 1);
221 add_fingerprint_to_dir("Magri", buf, fingerprint_list);
222 crypto_pk_get_fingerprint(pk1, buf, 1);
223 add_fingerprint_to_dir("Fred", buf, fingerprint_list);
226 #endif
227 dirserv_free_fingerprint_list();
229 done:
230 if (r1)
231 routerinfo_free(r1);
232 if (r2)
233 routerinfo_free(r2);
235 tor_free(pk1_str);
236 tor_free(pk2_str);
237 tor_free(pk3_str);
238 if (pk1) crypto_pk_free(pk1);
239 if (pk2) crypto_pk_free(pk2);
240 if (pk3) crypto_pk_free(pk3);
241 if (rp1) routerinfo_free(rp1);
242 tor_free(dir1); /* XXXX And more !*/
243 tor_free(dir2); /* And more !*/
246 static void
247 test_dir_versions(void)
249 tor_version_t ver1;
251 /* Try out version parsing functionality */
252 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
253 test_eq(0, ver1.major);
254 test_eq(3, ver1.minor);
255 test_eq(4, ver1.micro);
256 test_eq(VER_PRE, ver1.status);
257 test_eq(2, ver1.patchlevel);
258 test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
259 test_eq(0, ver1.major);
260 test_eq(3, ver1.minor);
261 test_eq(4, ver1.micro);
262 test_eq(VER_RC, ver1.status);
263 test_eq(1, ver1.patchlevel);
264 test_eq(0, tor_version_parse("1.3.4", &ver1));
265 test_eq(1, ver1.major);
266 test_eq(3, ver1.minor);
267 test_eq(4, ver1.micro);
268 test_eq(VER_RELEASE, ver1.status);
269 test_eq(0, ver1.patchlevel);
270 test_eq(0, tor_version_parse("1.3.4.999", &ver1));
271 test_eq(1, ver1.major);
272 test_eq(3, ver1.minor);
273 test_eq(4, ver1.micro);
274 test_eq(VER_RELEASE, ver1.status);
275 test_eq(999, ver1.patchlevel);
276 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
277 test_eq(0, ver1.major);
278 test_eq(1, ver1.minor);
279 test_eq(2, ver1.micro);
280 test_eq(4, ver1.patchlevel);
281 test_eq(VER_RELEASE, ver1.status);
282 test_streq("alpha", ver1.status_tag);
283 test_eq(0, tor_version_parse("0.1.2.4", &ver1));
284 test_eq(0, ver1.major);
285 test_eq(1, ver1.minor);
286 test_eq(2, ver1.micro);
287 test_eq(4, ver1.patchlevel);
288 test_eq(VER_RELEASE, ver1.status);
289 test_streq("", ver1.status_tag);
291 #define tt_versionstatus_op(vs1, op, vs2) \
292 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
293 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
294 #define test_v_i_o(val, ver, lst) \
295 tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
297 /* make sure tor_version_is_obsolete() works */
298 test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
299 test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
300 test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
301 test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
302 test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
303 test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
304 test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
305 test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
306 test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
307 test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
308 test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
309 /* Not on list, but newer than any in same series. */
310 test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
311 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
312 /* Series newer than any on list. */
313 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");
314 /* Series older than any on list. */
315 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");
316 /* Not on list, not newer than any on same series. */
317 test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
318 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
319 /* On list, not newer than any on same series. */
320 test_v_i_o(VS_UNRECOMMENDED,
321 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
322 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
323 test_eq(1, tor_version_as_new_as(
324 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
325 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
326 "0.0.8rc2"));
327 test_eq(0, tor_version_as_new_as(
328 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
329 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
331 /* Now try svn revisions. */
332 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
333 "Tor 0.2.1.0-dev (r99)"));
334 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
335 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
336 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
337 "Tor 0.2.1.0-dev on Colossus"));
338 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
339 "Tor 0.2.1.0-dev (r100)"));
340 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
341 "Tor 0.2.1.0-dev (r100) on AM"));
342 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
343 "Tor 0.2.1.0-dev (r99)"));
344 test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
345 "Tor 0.2.1.0-dev (r99)"));
347 /* Now try git revisions */
348 test_eq(0, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
349 test_eq(0, ver1.major);
350 test_eq(5, ver1.minor);
351 test_eq(6, ver1.micro);
352 test_eq(7, ver1.patchlevel);
353 test_eq(3, ver1.git_tag_len);
354 test_memeq(ver1.git_tag, "\xff\x00\xff", 3);
355 test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
356 test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
357 test_eq(0, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
358 done:
362 /** Run unit tests for directory fp_pair functions. */
363 static void
364 test_dir_fp_pairs(void)
366 smartlist_t *sl = smartlist_new();
367 fp_pair_t *pair;
369 dir_split_resource_into_fingerprint_pairs(
370 /* Two pairs, out of order, with one duplicate. */
371 "73656372657420646174612E0000000000FFFFFF-"
372 "557365204145532d32353620696e73746561642e+"
373 "73656372657420646174612E0000000000FFFFFF-"
374 "557365204145532d32353620696e73746561642e+"
375 "48657861646563696d616c2069736e277420736f-"
376 "676f6f6420666f7220686964696e6720796f7572.z", sl);
378 test_eq(smartlist_len(sl), 2);
379 pair = smartlist_get(sl, 0);
380 test_memeq(pair->first, "Hexadecimal isn't so", DIGEST_LEN);
381 test_memeq(pair->second, "good for hiding your", DIGEST_LEN);
382 pair = smartlist_get(sl, 1);
383 test_memeq(pair->first, "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN);
384 test_memeq(pair->second, "Use AES-256 instead.", DIGEST_LEN);
386 done:
387 SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
388 smartlist_free(sl);
391 static void
392 test_dir_split_fps(void *testdata)
394 smartlist_t *sl = smartlist_new();
395 char *mem_op_hex_tmp = NULL;
396 (void)testdata;
398 /* Some example hex fingerprints and their base64 equivalents */
399 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
400 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
401 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
402 #define HEX256_1 \
403 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
404 #define HEX256_2 \
405 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
406 #define HEX256_3 \
407 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
408 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
409 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
410 #define B64_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
411 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
412 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
413 #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
415 /* no flags set */
416 dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
417 tt_int_op(smartlist_len(sl), ==, 3);
418 tt_str_op(smartlist_get(sl, 0), ==, "A");
419 tt_str_op(smartlist_get(sl, 1), ==, "C");
420 tt_str_op(smartlist_get(sl, 2), ==, "B");
421 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
422 smartlist_clear(sl);
424 /* uniq strings. */
425 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
426 tt_int_op(smartlist_len(sl), ==, 3);
427 tt_str_op(smartlist_get(sl, 0), ==, "A");
428 tt_str_op(smartlist_get(sl, 1), ==, "B");
429 tt_str_op(smartlist_get(sl, 2), ==, "C");
430 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
431 smartlist_clear(sl);
433 /* Decode hex. */
434 dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
435 tt_int_op(smartlist_len(sl), ==, 2);
436 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
437 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
438 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
439 smartlist_clear(sl);
441 /* decode hex and drop weirdness. */
442 dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
443 sl, NULL, DSR_HEX);
444 tt_int_op(smartlist_len(sl), ==, 2);
445 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
446 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
447 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
448 smartlist_clear(sl);
450 /* Decode long hex */
451 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
452 sl, NULL, DSR_HEX|DSR_DIGEST256);
453 tt_int_op(smartlist_len(sl), ==, 3);
454 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
455 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
456 test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
457 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
458 smartlist_clear(sl);
460 /* Decode hex and sort. */
461 dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
462 sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
463 tt_int_op(smartlist_len(sl), ==, 3);
464 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
465 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
466 test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
467 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
468 smartlist_clear(sl);
470 /* Decode long hex and sort */
471 dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
472 "+"HEX256_1,
473 sl, NULL,
474 DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
475 tt_int_op(smartlist_len(sl), ==, 3);
476 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
477 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
478 test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
479 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
480 smartlist_clear(sl);
482 /* Decode base64 */
483 dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
484 tt_int_op(smartlist_len(sl), ==, 2);
485 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
486 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
487 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
488 smartlist_clear(sl);
490 /* Decode long base64 */
491 dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
492 sl, NULL, DSR_BASE64|DSR_DIGEST256);
493 tt_int_op(smartlist_len(sl), ==, 2);
494 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
495 test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
496 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
497 smartlist_clear(sl);
499 dir_split_resource_into_fingerprints(B64_256_1,
500 sl, NULL, DSR_BASE64|DSR_DIGEST256);
501 tt_int_op(smartlist_len(sl), ==, 1);
502 test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
503 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
504 smartlist_clear(sl);
506 done:
507 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
508 smartlist_free(sl);
509 tor_free(mem_op_hex_tmp);
512 static void
513 test_dir_measured_bw(void)
515 measured_bw_line_t mbwl;
516 int i;
517 const char *lines_pass[] = {
518 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
519 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
520 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
521 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
522 "bw=1024 junk=007\n",
523 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
524 "bw=1024 junk=007\n",
525 "end"
527 const char *lines_fail[] = {
528 /* Test possible python stupidity on input */
529 "node_id=None bw=1024\n",
530 "node_id=$None bw=1024\n",
531 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
532 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
533 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
534 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
535 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
536 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
537 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
538 /* Test incomplete writes due to race conditions, partial copies, etc */
539 "node_i",
540 "node_i\n",
541 "node_id=",
542 "node_id=\n",
543 "node_id=$557365204145532d32353620696e73746561642e bw=",
544 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
545 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
546 "node_id=$557365204145532d32353620696e7374",
547 "node_id=$557365204145532d32353620696e7374\n",
549 "\n",
550 " \n ",
551 " \n\n",
552 /* Test assorted noise */
553 " node_id= ",
554 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
555 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
556 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
557 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
558 "end"
561 for (i = 0; strcmp(lines_fail[i], "end"); i++) {
562 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
563 test_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
566 for (i = 0; strcmp(lines_pass[i], "end"); i++) {
567 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
568 test_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
569 test_assert(mbwl.bw == 1024);
570 test_assert(strcmp(mbwl.node_hex,
571 "557365204145532d32353620696e73746561642e") == 0);
574 done:
575 return;
578 static void
579 test_dir_param_voting(void)
581 networkstatus_t vote1, vote2, vote3, vote4;
582 smartlist_t *votes = smartlist_new();
583 char *res = NULL;
585 /* dirvote_compute_params only looks at the net_params field of the votes,
586 so that's all we need to set.
588 memset(&vote1, 0, sizeof(vote1));
589 memset(&vote2, 0, sizeof(vote2));
590 memset(&vote3, 0, sizeof(vote3));
591 memset(&vote4, 0, sizeof(vote4));
592 vote1.net_params = smartlist_new();
593 vote2.net_params = smartlist_new();
594 vote3.net_params = smartlist_new();
595 vote4.net_params = smartlist_new();
596 smartlist_split_string(vote1.net_params,
597 "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
598 smartlist_split_string(vote2.net_params,
599 "ab=27 cw=5 x-yz=88", NULL, 0, 0);
600 smartlist_split_string(vote3.net_params,
601 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
602 smartlist_split_string(vote4.net_params,
603 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
604 test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
605 test_eq(222, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
606 test_eq(80, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
607 test_eq(-8, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
608 test_eq(0, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
610 smartlist_add(votes, &vote1);
612 /* Do the first tests without adding all the other votes, for
613 * networks without many dirauths. */
615 res = dirvote_compute_params(votes, 11, 6);
616 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
617 tor_free(res);
619 res = dirvote_compute_params(votes, 12, 2);
620 test_streq(res, "");
621 tor_free(res);
623 res = dirvote_compute_params(votes, 12, 1);
624 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
625 tor_free(res);
627 smartlist_add(votes, &vote2);
629 res = dirvote_compute_params(votes, 11, 2);
630 test_streq(res, "ab=27 abcd=20 cw=5 x-yz=-99");
631 tor_free(res);
633 res = dirvote_compute_params(votes, 12, 2);
634 test_streq(res, "ab=27 cw=5 x-yz=-99");
635 tor_free(res);
637 res = dirvote_compute_params(votes, 12, 3);
638 test_streq(res, "ab=27 cw=5 x-yz=-99");
639 tor_free(res);
641 res = dirvote_compute_params(votes, 12, 6);
642 test_streq(res, "");
643 tor_free(res);
645 smartlist_add(votes, &vote3);
647 res = dirvote_compute_params(votes, 11, 3);
648 test_streq(res, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
649 tor_free(res);
651 res = dirvote_compute_params(votes, 12, 3);
652 test_streq(res, "ab=27 abcd=20 cw=50 x-yz=-9");
653 tor_free(res);
655 res = dirvote_compute_params(votes, 12, 5);
656 test_streq(res, "cw=50 x-yz=-9");
657 tor_free(res);
659 res = dirvote_compute_params(votes, 12, 9);
660 test_streq(res, "cw=50 x-yz=-9");
661 tor_free(res);
663 smartlist_add(votes, &vote4);
665 res = dirvote_compute_params(votes, 11, 4);
666 test_streq(res, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
667 tor_free(res);
669 res = dirvote_compute_params(votes, 12, 4);
670 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
671 tor_free(res);
673 res = dirvote_compute_params(votes, 12, 5);
674 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
675 tor_free(res);
677 /* Test that the special-cased "at least three dirauths voted for
678 * this param" logic works as expected. */
679 res = dirvote_compute_params(votes, 12, 6);
680 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
681 tor_free(res);
683 res = dirvote_compute_params(votes, 12, 10);
684 test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
685 tor_free(res);
687 done:
688 tor_free(res);
689 SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
690 SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
691 SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
692 SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
693 smartlist_free(vote1.net_params);
694 smartlist_free(vote2.net_params);
695 smartlist_free(vote3.net_params);
696 smartlist_free(vote4.net_params);
697 smartlist_free(votes);
699 return;
702 extern const char AUTHORITY_CERT_1[];
703 extern const char AUTHORITY_SIGNKEY_1[];
704 extern const char AUTHORITY_CERT_2[];
705 extern const char AUTHORITY_SIGNKEY_2[];
706 extern const char AUTHORITY_CERT_3[];
707 extern const char AUTHORITY_SIGNKEY_3[];
709 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
710 * same voting authority, and that they do in fact have all the same
711 * information. */
712 static void
713 test_same_voter(networkstatus_voter_info_t *v1,
714 networkstatus_voter_info_t *v2)
716 test_streq(v1->nickname, v2->nickname);
717 test_memeq(v1->identity_digest, v2->identity_digest, DIGEST_LEN);
718 test_streq(v1->address, v2->address);
719 test_eq(v1->addr, v2->addr);
720 test_eq(v1->dir_port, v2->dir_port);
721 test_eq(v1->or_port, v2->or_port);
722 test_streq(v1->contact, v2->contact);
723 test_memeq(v1->vote_digest, v2->vote_digest, DIGEST_LEN);
724 done:
728 /** Helper: Make a new routerinfo containing the right information for a
729 * given vote_routerstatus_t. */
730 static routerinfo_t *
731 generate_ri_from_rs(const vote_routerstatus_t *vrs)
733 routerinfo_t *r;
734 const routerstatus_t *rs = &vrs->status;
735 static time_t published = 0;
737 r = tor_malloc_zero(sizeof(routerinfo_t));
738 memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
739 memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
740 DIGEST_LEN);
741 r->cache_info.do_not_cache = 1;
742 r->cache_info.routerlist_index = -1;
743 r->cache_info.signed_descriptor_body =
744 tor_strdup("123456789012345678901234567890123");
745 r->cache_info.signed_descriptor_len =
746 strlen(r->cache_info.signed_descriptor_body);
747 r->exit_policy = smartlist_new();
748 r->cache_info.published_on = ++published + time(NULL);
749 if (rs->has_bandwidth) {
751 * Multiply by 1000 because the routerinfo_t and the routerstatus_t
752 * seem to use different units (*sigh*) and because we seem stuck on
753 * icky and perverse decimal kilobytes (*double sigh*) - see
754 * router_get_advertised_bandwidth_capped() of routerlist.c and
755 * routerstatus_format_entry() of dirserv.c.
757 r->bandwidthrate = rs->bandwidth * 1000;
758 r->bandwidthcapacity = rs->bandwidth * 1000;
760 return r;
763 /** Helper: get a detached signatures document for one or two
764 * consensuses. */
765 static char *
766 get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
768 char *r;
769 smartlist_t *sl;
770 tor_assert(ns && ns->flavor == FLAV_NS);
771 sl = smartlist_new();
772 smartlist_add(sl,ns);
773 if (ns2)
774 smartlist_add(sl,ns2);
775 r = networkstatus_get_detached_signatures(sl);
776 smartlist_free(sl);
777 return r;
781 * Generate a routerstatus for v3_networkstatus test
783 static vote_routerstatus_t *
784 gen_routerstatus_for_v3ns(int idx, time_t now)
786 vote_routerstatus_t *vrs;
787 routerstatus_t *rs;
788 tor_addr_t addr_ipv6;
790 switch (idx) {
791 case 0:
792 /* Generate the first routerstatus. */
793 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
794 rs = &vrs->status;
795 vrs->version = tor_strdup("0.1.2.14");
796 rs->published_on = now-1500;
797 strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
798 memset(rs->identity_digest, 3, DIGEST_LEN);
799 memset(rs->descriptor_digest, 78, DIGEST_LEN);
800 rs->addr = 0x99008801;
801 rs->or_port = 443;
802 rs->dir_port = 8000;
803 /* all flags but running cleared */
804 rs->is_flagged_running = 1;
805 break;
806 case 1:
807 /* Generate the second routerstatus. */
808 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
809 rs = &vrs->status;
810 vrs->version = tor_strdup("0.2.0.5");
811 rs->published_on = now-1000;
812 strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
813 memset(rs->identity_digest, 5, DIGEST_LEN);
814 memset(rs->descriptor_digest, 77, DIGEST_LEN);
815 rs->addr = 0x99009901;
816 rs->or_port = 443;
817 rs->dir_port = 0;
818 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
819 tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
820 rs->ipv6_orport = 4711;
821 rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
822 rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
823 break;
824 case 2:
825 /* Generate the third routerstatus. */
826 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
827 rs = &vrs->status;
828 vrs->version = tor_strdup("0.1.0.3");
829 rs->published_on = now-1000;
830 strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
831 memset(rs->identity_digest, 33, DIGEST_LEN);
832 memset(rs->descriptor_digest, 79, DIGEST_LEN);
833 rs->addr = 0xAA009901;
834 rs->or_port = 400;
835 rs->dir_port = 9999;
836 rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
837 rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
838 rs->is_possible_guard = 1;
839 break;
840 case 3:
841 /* Generate a fourth routerstatus that is not running. */
842 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
843 rs = &vrs->status;
844 vrs->version = tor_strdup("0.1.6.3");
845 rs->published_on = now-1000;
846 strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
847 memset(rs->identity_digest, 34, DIGEST_LEN);
848 memset(rs->descriptor_digest, 47, DIGEST_LEN);
849 rs->addr = 0xC0000203;
850 rs->or_port = 500;
851 rs->dir_port = 1999;
852 /* Running flag (and others) cleared */
853 break;
854 case 4:
855 /* No more for this test; return NULL */
856 vrs = NULL;
857 break;
858 default:
859 /* Shouldn't happen */
860 test_assert(0);
863 done:
864 return vrs;
867 /** Apply tweaks to the vote list for each voter */
868 static void
869 vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
871 vote_routerstatus_t *vrs;
872 const char *msg = NULL;
874 test_assert(v);
875 (void)now;
877 if (voter == 1) {
878 measured_bw_line_t mbw;
879 memset(mbw.node_id, 33, sizeof(mbw.node_id));
880 mbw.bw = 1024;
881 test_assert(measured_bw_line_apply(&mbw,
882 v->routerstatus_list) == 1);
883 } else if (voter == 2 || voter == 3) {
884 /* Monkey around with the list a bit */
885 vrs = smartlist_get(v->routerstatus_list, 2);
886 smartlist_del_keeporder(v->routerstatus_list, 2);
887 tor_free(vrs->version);
888 tor_free(vrs);
889 vrs = smartlist_get(v->routerstatus_list, 0);
890 vrs->status.is_fast = 1;
892 if (voter == 3) {
893 vrs = smartlist_get(v->routerstatus_list, 0);
894 smartlist_del_keeporder(v->routerstatus_list, 0);
895 tor_free(vrs->version);
896 tor_free(vrs);
897 vrs = smartlist_get(v->routerstatus_list, 0);
898 memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
899 test_assert(router_add_to_routerlist(
900 generate_ri_from_rs(vrs), &msg,0,0) >= 0);
904 done:
905 return;
909 * Test a parsed vote_routerstatus_t for v3_networkstatus test
911 static void
912 test_vrs_for_v3ns(vote_routerstatus_t *vrs, int voter, time_t now)
914 routerstatus_t *rs;
915 tor_addr_t addr_ipv6;
917 test_assert(vrs);
918 rs = &(vrs->status);
919 test_assert(rs);
921 /* Split out by digests to test */
922 if (tor_memeq(rs->identity_digest,
923 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
924 "\x3\x3\x3\x3",
925 DIGEST_LEN) &&
926 (voter == 1)) {
927 /* Check the first routerstatus. */
928 test_streq(vrs->version, "0.1.2.14");
929 test_eq(rs->published_on, now-1500);
930 test_streq(rs->nickname, "router2");
931 test_memeq(rs->identity_digest,
932 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
933 "\x3\x3\x3\x3",
934 DIGEST_LEN);
935 test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
936 test_eq(rs->addr, 0x99008801);
937 test_eq(rs->or_port, 443);
938 test_eq(rs->dir_port, 8000);
939 test_eq(vrs->flags, U64_LITERAL(16)); // no flags except "running"
940 } else if (tor_memeq(rs->identity_digest,
941 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
942 "\x5\x5\x5\x5",
943 DIGEST_LEN) &&
944 (voter == 1 || voter == 2)) {
945 test_memeq(rs->identity_digest,
946 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
947 "\x5\x5\x5\x5",
948 DIGEST_LEN);
950 if (voter == 1) {
951 /* Check the second routerstatus. */
952 test_streq(vrs->version, "0.2.0.5");
953 test_eq(rs->published_on, now-1000);
954 test_streq(rs->nickname, "router1");
956 test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
957 test_eq(rs->addr, 0x99009901);
958 test_eq(rs->or_port, 443);
959 test_eq(rs->dir_port, 0);
960 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
961 test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
962 test_eq(rs->ipv6_orport, 4711);
963 if (voter == 1) {
964 test_eq(vrs->flags, U64_LITERAL(254)); // all flags except "authority."
965 } else {
966 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
967 test_eq(vrs->flags, U64_LITERAL(974));
969 } else if (tor_memeq(rs->identity_digest,
970 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
971 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
972 DIGEST_LEN) &&
973 (voter == 1 || voter == 2)) {
974 /* Check the measured bandwidth bits */
975 test_assert(vrs->status.has_measured_bw &&
976 vrs->status.measured_bw == 1024);
977 } else {
979 * Didn't expect this, but the old unit test only checked some of them,
980 * so don't assert.
982 /* test_assert(0); */
985 done:
986 return;
990 * Test a consensus for v3_networkstatus_test
992 static void
993 test_consensus_for_v3ns(networkstatus_t *con, time_t now)
995 (void)now;
997 test_assert(con);
998 test_assert(!con->cert);
999 test_eq(2, smartlist_len(con->routerstatus_list));
1000 /* There should be two listed routers: one with identity 3, one with
1001 * identity 5. */
1003 done:
1004 return;
1008 * Test a router list entry for v3_networkstatus test
1010 static void
1011 test_routerstatus_for_v3ns(routerstatus_t *rs, time_t now)
1013 tor_addr_t addr_ipv6;
1015 test_assert(rs);
1017 /* There should be two listed routers: one with identity 3, one with
1018 * identity 5. */
1019 /* This one showed up in 2 digests. */
1020 if (tor_memeq(rs->identity_digest,
1021 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1022 "\x3\x3",
1023 DIGEST_LEN)) {
1024 test_memeq(rs->identity_digest,
1025 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1026 DIGEST_LEN);
1027 test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1028 test_assert(!rs->is_authority);
1029 test_assert(!rs->is_exit);
1030 test_assert(!rs->is_fast);
1031 test_assert(!rs->is_possible_guard);
1032 test_assert(!rs->is_stable);
1033 /* (If it wasn't running it wouldn't be here) */
1034 test_assert(rs->is_flagged_running);
1035 test_assert(!rs->is_v2_dir);
1036 test_assert(!rs->is_valid);
1037 test_assert(!rs->is_named);
1038 /* XXXX check version */
1039 } else if (tor_memeq(rs->identity_digest,
1040 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1041 "\x5\x5\x5\x5",
1042 DIGEST_LEN)) {
1043 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
1044 test_memeq(rs->identity_digest,
1045 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1046 DIGEST_LEN);
1047 test_streq(rs->nickname, "router1");
1048 test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1049 test_eq(rs->published_on, now-1000);
1050 test_eq(rs->addr, 0x99009901);
1051 test_eq(rs->or_port, 443);
1052 test_eq(rs->dir_port, 0);
1053 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1054 test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1055 test_eq(rs->ipv6_orport, 4711);
1056 test_assert(!rs->is_authority);
1057 test_assert(rs->is_exit);
1058 test_assert(rs->is_fast);
1059 test_assert(rs->is_possible_guard);
1060 test_assert(rs->is_stable);
1061 test_assert(rs->is_flagged_running);
1062 test_assert(rs->is_v2_dir);
1063 test_assert(rs->is_valid);
1064 test_assert(!rs->is_named);
1065 /* XXXX check version */
1066 } else {
1067 /* Weren't expecting this... */
1068 test_assert(0);
1071 done:
1072 return;
1075 /** Run a unit tests for generating and parsing networkstatuses, with
1076 * the supply test fns. */
1077 static void
1078 test_a_networkstatus(
1079 vote_routerstatus_t * (*vrs_gen)(int idx, time_t now),
1080 void (*vote_tweaks)(networkstatus_t *v, int voter, time_t now),
1081 void (*vrs_test)(vote_routerstatus_t *vrs, int voter, time_t now),
1082 void (*consensus_test)(networkstatus_t *con, time_t now),
1083 void (*rs_test)(routerstatus_t *rs, time_t now))
1085 authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
1086 crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
1087 crypto_pk_t *sign_skey_leg1=NULL;
1088 const char *msg=NULL;
1090 time_t now = time(NULL);
1091 networkstatus_voter_info_t *voter;
1092 document_signature_t *sig;
1093 networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
1094 *con_md=NULL;
1095 vote_routerstatus_t *vrs;
1096 routerstatus_t *rs;
1097 int idx, n_rs, n_vrs;
1098 char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL, *cp;
1099 smartlist_t *votes = smartlist_new();
1101 /* For generating the two other consensuses. */
1102 char *detached_text1=NULL, *detached_text2=NULL;
1103 char *consensus_text2=NULL, *consensus_text3=NULL;
1104 char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
1105 char *consensus_text_md=NULL;
1106 networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
1107 ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
1109 test_assert(vrs_gen);
1110 test_assert(rs_test);
1111 test_assert(vrs_test);
1113 /* Parse certificates and keys. */
1114 cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
1115 test_assert(cert1);
1116 test_assert(cert1->is_cross_certified);
1117 cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
1118 test_assert(cert2);
1119 cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
1120 test_assert(cert3);
1121 sign_skey_1 = crypto_pk_new();
1122 sign_skey_2 = crypto_pk_new();
1123 sign_skey_3 = crypto_pk_new();
1124 sign_skey_leg1 = pk_generate(4);
1126 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
1127 AUTHORITY_SIGNKEY_1, -1));
1128 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
1129 AUTHORITY_SIGNKEY_2, -1));
1130 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
1131 AUTHORITY_SIGNKEY_3, -1));
1133 test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
1134 test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
1137 * Set up a vote; generate it; try to parse it.
1139 vote = tor_malloc_zero(sizeof(networkstatus_t));
1140 vote->type = NS_TYPE_VOTE;
1141 vote->published = now;
1142 vote->valid_after = now+1000;
1143 vote->fresh_until = now+2000;
1144 vote->valid_until = now+3000;
1145 vote->vote_seconds = 100;
1146 vote->dist_seconds = 200;
1147 vote->supported_methods = smartlist_new();
1148 smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
1149 vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
1150 vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
1151 vote->known_flags = smartlist_new();
1152 smartlist_split_string(vote->known_flags,
1153 "Authority Exit Fast Guard Running Stable V2Dir Valid",
1154 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
1155 vote->voters = smartlist_new();
1156 voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
1157 voter->nickname = tor_strdup("Voter1");
1158 voter->address = tor_strdup("1.2.3.4");
1159 voter->addr = 0x01020304;
1160 voter->dir_port = 80;
1161 voter->or_port = 9000;
1162 voter->contact = tor_strdup("voter@example.com");
1163 crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
1164 smartlist_add(vote->voters, voter);
1165 vote->cert = authority_cert_dup(cert1);
1166 vote->net_params = smartlist_new();
1167 smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
1168 NULL, 0, 0);
1169 vote->routerstatus_list = smartlist_new();
1170 /* add routerstatuses */
1171 idx = 0;
1172 do {
1173 vrs = vrs_gen(idx, now);
1174 if (vrs) {
1175 smartlist_add(vote->routerstatus_list, vrs);
1176 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs),
1177 &msg,0,0)>=0);
1178 ++idx;
1180 } while (vrs);
1181 n_vrs = idx;
1183 /* dump the vote and try to parse it. */
1184 v1_text = format_networkstatus_vote(sign_skey_1, vote);
1185 test_assert(v1_text);
1186 v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
1187 test_assert(v1);
1189 /* Make sure the parsed thing was right. */
1190 test_eq(v1->type, NS_TYPE_VOTE);
1191 test_eq(v1->published, vote->published);
1192 test_eq(v1->valid_after, vote->valid_after);
1193 test_eq(v1->fresh_until, vote->fresh_until);
1194 test_eq(v1->valid_until, vote->valid_until);
1195 test_eq(v1->vote_seconds, vote->vote_seconds);
1196 test_eq(v1->dist_seconds, vote->dist_seconds);
1197 test_streq(v1->client_versions, vote->client_versions);
1198 test_streq(v1->server_versions, vote->server_versions);
1199 test_assert(v1->voters && smartlist_len(v1->voters));
1200 voter = smartlist_get(v1->voters, 0);
1201 test_streq(voter->nickname, "Voter1");
1202 test_streq(voter->address, "1.2.3.4");
1203 test_eq(voter->addr, 0x01020304);
1204 test_eq(voter->dir_port, 80);
1205 test_eq(voter->or_port, 9000);
1206 test_streq(voter->contact, "voter@example.com");
1207 test_assert(v1->cert);
1208 test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
1209 cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
1210 test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
1211 tor_free(cp);
1212 test_eq(smartlist_len(v1->routerstatus_list), n_vrs);
1214 if (vote_tweaks) vote_tweaks(v1, 1, now);
1216 /* Check the routerstatuses. */
1217 for (idx = 0; idx < n_vrs; ++idx) {
1218 vrs = smartlist_get(v1->routerstatus_list, idx);
1219 test_assert(vrs);
1220 vrs_test(vrs, 1, now);
1223 /* Generate second vote. It disagrees on some of the times,
1224 * and doesn't list versions, and knows some crazy flags */
1225 vote->published = now+1;
1226 vote->fresh_until = now+3005;
1227 vote->dist_seconds = 300;
1228 authority_cert_free(vote->cert);
1229 vote->cert = authority_cert_dup(cert2);
1230 vote->net_params = smartlist_new();
1231 smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
1232 NULL, 0, 0);
1233 tor_free(vote->client_versions);
1234 tor_free(vote->server_versions);
1235 voter = smartlist_get(vote->voters, 0);
1236 tor_free(voter->nickname);
1237 tor_free(voter->address);
1238 voter->nickname = tor_strdup("Voter2");
1239 voter->address = tor_strdup("2.3.4.5");
1240 voter->addr = 0x02030405;
1241 crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
1242 smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
1243 smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
1244 smartlist_sort_strings(vote->known_flags);
1246 /* generate and parse v2. */
1247 v2_text = format_networkstatus_vote(sign_skey_2, vote);
1248 test_assert(v2_text);
1249 v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
1250 test_assert(v2);
1252 if (vote_tweaks) vote_tweaks(v2, 2, now);
1254 /* Check that flags come out right.*/
1255 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1256 test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1257 "Running:Stable:V2Dir:Valid");
1258 tor_free(cp);
1260 /* Check the routerstatuses. */
1261 n_vrs = smartlist_len(v2->routerstatus_list);
1262 for (idx = 0; idx < n_vrs; ++idx) {
1263 vrs = smartlist_get(v2->routerstatus_list, idx);
1264 test_assert(vrs);
1265 vrs_test(vrs, 2, now);
1268 /* Generate the third vote. */
1269 vote->published = now;
1270 vote->fresh_until = now+2003;
1271 vote->dist_seconds = 250;
1272 authority_cert_free(vote->cert);
1273 vote->cert = authority_cert_dup(cert3);
1274 vote->net_params = smartlist_new();
1275 smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
1276 NULL, 0, 0);
1277 smartlist_add(vote->supported_methods, tor_strdup("4"));
1278 vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
1279 vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
1280 voter = smartlist_get(vote->voters, 0);
1281 tor_free(voter->nickname);
1282 tor_free(voter->address);
1283 voter->nickname = tor_strdup("Voter3");
1284 voter->address = tor_strdup("3.4.5.6");
1285 voter->addr = 0x03040506;
1286 crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
1287 /* This one has a legacy id. */
1288 memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
1290 v3_text = format_networkstatus_vote(sign_skey_3, vote);
1291 test_assert(v3_text);
1293 v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
1294 test_assert(v3);
1296 if (vote_tweaks) vote_tweaks(v3, 3, now);
1298 /* Compute a consensus as voter 3. */
1299 smartlist_add(votes, v3);
1300 smartlist_add(votes, v1);
1301 smartlist_add(votes, v2);
1302 consensus_text = networkstatus_compute_consensus(votes, 3,
1303 cert3->identity_key,
1304 sign_skey_3,
1305 "AAAAAAAAAAAAAAAAAAAA",
1306 sign_skey_leg1,
1307 FLAV_NS);
1308 test_assert(consensus_text);
1309 con = networkstatus_parse_vote_from_string(consensus_text, NULL,
1310 NS_TYPE_CONSENSUS);
1311 test_assert(con);
1312 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
1313 // v1_text, v2_text, v3_text);
1314 consensus_text_md = networkstatus_compute_consensus(votes, 3,
1315 cert3->identity_key,
1316 sign_skey_3,
1317 "AAAAAAAAAAAAAAAAAAAA",
1318 sign_skey_leg1,
1319 FLAV_MICRODESC);
1320 test_assert(consensus_text_md);
1321 con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
1322 NS_TYPE_CONSENSUS);
1323 test_assert(con_md);
1324 test_eq(con_md->flavor, FLAV_MICRODESC);
1326 /* Check consensus contents. */
1327 test_assert(con->type == NS_TYPE_CONSENSUS);
1328 test_eq(con->published, 0); /* this field only appears in votes. */
1329 test_eq(con->valid_after, now+1000);
1330 test_eq(con->fresh_until, now+2003); /* median */
1331 test_eq(con->valid_until, now+3000);
1332 test_eq(con->vote_seconds, 100);
1333 test_eq(con->dist_seconds, 250); /* median */
1334 test_streq(con->client_versions, "0.1.2.14");
1335 test_streq(con->server_versions, "0.1.2.15,0.1.2.16");
1336 cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
1337 test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1338 "Running:Stable:V2Dir:Valid");
1339 tor_free(cp);
1340 cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
1341 test_streq(cp, "circuitwindow=80:foo=660");
1342 tor_free(cp);
1344 test_eq(4, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
1345 /* The voter id digests should be in this order. */
1346 test_assert(memcmp(cert2->cache_info.identity_digest,
1347 cert1->cache_info.identity_digest,DIGEST_LEN)<0);
1348 test_assert(memcmp(cert1->cache_info.identity_digest,
1349 cert3->cache_info.identity_digest,DIGEST_LEN)<0);
1350 test_same_voter(smartlist_get(con->voters, 1),
1351 smartlist_get(v2->voters, 0));
1352 test_same_voter(smartlist_get(con->voters, 2),
1353 smartlist_get(v1->voters, 0));
1354 test_same_voter(smartlist_get(con->voters, 3),
1355 smartlist_get(v3->voters, 0));
1357 consensus_test(con, now);
1359 /* Check the routerstatuses. */
1360 n_rs = smartlist_len(con->routerstatus_list);
1361 for (idx = 0; idx < n_rs; ++idx) {
1362 rs = smartlist_get(con->routerstatus_list, idx);
1363 test_assert(rs);
1364 rs_test(rs, now);
1367 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
1368 * The second one hasn't signed. The fourth one has signed: validate it. */
1369 voter = smartlist_get(con->voters, 1);
1370 test_eq(smartlist_len(voter->sigs), 0);
1372 voter = smartlist_get(con->voters, 3);
1373 test_eq(smartlist_len(voter->sigs), 1);
1374 sig = smartlist_get(voter->sigs, 0);
1375 test_assert(sig->signature);
1376 test_assert(!sig->good_signature);
1377 test_assert(!sig->bad_signature);
1379 test_assert(!networkstatus_check_document_signature(con, sig, cert3));
1380 test_assert(sig->signature);
1381 test_assert(sig->good_signature);
1382 test_assert(!sig->bad_signature);
1385 const char *msg=NULL;
1386 /* Compute the other two signed consensuses. */
1387 smartlist_shuffle(votes);
1388 consensus_text2 = networkstatus_compute_consensus(votes, 3,
1389 cert2->identity_key,
1390 sign_skey_2, NULL,NULL,
1391 FLAV_NS);
1392 consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
1393 cert2->identity_key,
1394 sign_skey_2, NULL,NULL,
1395 FLAV_MICRODESC);
1396 smartlist_shuffle(votes);
1397 consensus_text3 = networkstatus_compute_consensus(votes, 3,
1398 cert1->identity_key,
1399 sign_skey_1, NULL,NULL,
1400 FLAV_NS);
1401 consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
1402 cert1->identity_key,
1403 sign_skey_1, NULL,NULL,
1404 FLAV_MICRODESC);
1405 test_assert(consensus_text2);
1406 test_assert(consensus_text3);
1407 test_assert(consensus_text_md2);
1408 test_assert(consensus_text_md3);
1409 con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
1410 NS_TYPE_CONSENSUS);
1411 con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
1412 NS_TYPE_CONSENSUS);
1413 con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
1414 NS_TYPE_CONSENSUS);
1415 con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
1416 NS_TYPE_CONSENSUS);
1417 test_assert(con2);
1418 test_assert(con3);
1419 test_assert(con_md2);
1420 test_assert(con_md3);
1422 /* All three should have the same digest. */
1423 test_memeq(&con->digests, &con2->digests, sizeof(digests_t));
1424 test_memeq(&con->digests, &con3->digests, sizeof(digests_t));
1426 test_memeq(&con_md->digests, &con_md2->digests, sizeof(digests_t));
1427 test_memeq(&con_md->digests, &con_md3->digests, sizeof(digests_t));
1429 /* Extract a detached signature from con3. */
1430 detached_text1 = get_detached_sigs(con3, con_md3);
1431 tt_assert(detached_text1);
1432 /* Try to parse it. */
1433 dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
1434 tt_assert(dsig1);
1436 /* Are parsed values as expected? */
1437 test_eq(dsig1->valid_after, con3->valid_after);
1438 test_eq(dsig1->fresh_until, con3->fresh_until);
1439 test_eq(dsig1->valid_until, con3->valid_until);
1441 digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
1442 test_assert(dsig_digests);
1443 test_memeq(dsig_digests->d[DIGEST_SHA1], con3->digests.d[DIGEST_SHA1],
1444 DIGEST_LEN);
1445 dsig_digests = strmap_get(dsig1->digests, "microdesc");
1446 test_assert(dsig_digests);
1447 test_memeq(dsig_digests->d[DIGEST_SHA256],
1448 con_md3->digests.d[DIGEST_SHA256],
1449 DIGEST256_LEN);
1452 smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
1453 test_assert(dsig_signatures);
1454 test_eq(1, smartlist_len(dsig_signatures));
1455 sig = smartlist_get(dsig_signatures, 0);
1456 test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
1457 DIGEST_LEN);
1458 test_eq(sig->alg, DIGEST_SHA1);
1460 dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
1461 test_assert(dsig_signatures);
1462 test_eq(1, smartlist_len(dsig_signatures));
1463 sig = smartlist_get(dsig_signatures, 0);
1464 test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
1465 DIGEST_LEN);
1466 test_eq(sig->alg, DIGEST_SHA256);
1469 /* Try adding it to con2. */
1470 detached_text2 = get_detached_sigs(con2,con_md2);
1471 test_eq(1, networkstatus_add_detached_signatures(con2, dsig1, "test",
1472 LOG_INFO, &msg));
1473 tor_free(detached_text2);
1474 test_eq(1, networkstatus_add_detached_signatures(con_md2, dsig1, "test",
1475 LOG_INFO, &msg));
1476 tor_free(detached_text2);
1477 detached_text2 = get_detached_sigs(con2,con_md2);
1478 //printf("\n<%s>\n", detached_text2);
1479 dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
1480 test_assert(dsig2);
1482 printf("\n");
1483 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
1484 char hd[64];
1485 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
1486 printf("%s\n", hd);
1489 test_eq(2,
1490 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
1491 test_eq(2,
1492 smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
1493 "microdesc")));
1495 /* Try adding to con2 twice; verify that nothing changes. */
1496 test_eq(0, networkstatus_add_detached_signatures(con2, dsig1, "test",
1497 LOG_INFO, &msg));
1499 /* Add to con. */
1500 test_eq(2, networkstatus_add_detached_signatures(con, dsig2, "test",
1501 LOG_INFO, &msg));
1502 /* Check signatures */
1503 voter = smartlist_get(con->voters, 1);
1504 sig = smartlist_get(voter->sigs, 0);
1505 test_assert(sig);
1506 test_assert(!networkstatus_check_document_signature(con, sig, cert2));
1507 voter = smartlist_get(con->voters, 2);
1508 sig = smartlist_get(voter->sigs, 0);
1509 test_assert(sig);
1510 test_assert(!networkstatus_check_document_signature(con, sig, cert1));
1513 done:
1514 smartlist_free(votes);
1515 tor_free(v1_text);
1516 tor_free(v2_text);
1517 tor_free(v3_text);
1518 tor_free(consensus_text);
1519 tor_free(consensus_text_md);
1521 if (vote)
1522 networkstatus_vote_free(vote);
1523 if (v1)
1524 networkstatus_vote_free(v1);
1525 if (v2)
1526 networkstatus_vote_free(v2);
1527 if (v3)
1528 networkstatus_vote_free(v3);
1529 if (con)
1530 networkstatus_vote_free(con);
1531 if (con_md)
1532 networkstatus_vote_free(con_md);
1533 if (sign_skey_1)
1534 crypto_pk_free(sign_skey_1);
1535 if (sign_skey_2)
1536 crypto_pk_free(sign_skey_2);
1537 if (sign_skey_3)
1538 crypto_pk_free(sign_skey_3);
1539 if (sign_skey_leg1)
1540 crypto_pk_free(sign_skey_leg1);
1541 if (cert1)
1542 authority_cert_free(cert1);
1543 if (cert2)
1544 authority_cert_free(cert2);
1545 if (cert3)
1546 authority_cert_free(cert3);
1548 tor_free(consensus_text2);
1549 tor_free(consensus_text3);
1550 tor_free(consensus_text_md2);
1551 tor_free(consensus_text_md3);
1552 tor_free(detached_text1);
1553 tor_free(detached_text2);
1554 if (con2)
1555 networkstatus_vote_free(con2);
1556 if (con3)
1557 networkstatus_vote_free(con3);
1558 if (con_md2)
1559 networkstatus_vote_free(con_md2);
1560 if (con_md3)
1561 networkstatus_vote_free(con_md3);
1562 if (dsig1)
1563 ns_detached_signatures_free(dsig1);
1564 if (dsig2)
1565 ns_detached_signatures_free(dsig2);
1568 /** Run unit tests for generating and parsing V3 consensus networkstatus
1569 * documents. */
1570 static void
1571 test_dir_v3_networkstatus(void)
1573 test_a_networkstatus(gen_routerstatus_for_v3ns,
1574 vote_tweaks_for_v3ns,
1575 test_vrs_for_v3ns,
1576 test_consensus_for_v3ns,
1577 test_routerstatus_for_v3ns);
1580 static void
1581 test_dir_scale_bw(void *testdata)
1583 double v[8] = { 2.0/3,
1584 7.0,
1585 1.0,
1586 3.0,
1587 1.0/5,
1588 1.0/7,
1589 12.0,
1590 24.0 };
1591 u64_dbl_t vals[8];
1592 uint64_t total;
1593 int i;
1595 (void) testdata;
1597 for (i=0; i<8; ++i)
1598 vals[i].dbl = v[i];
1600 scale_array_elements_to_u64(vals, 8, &total);
1602 tt_int_op((int)total, ==, 48);
1603 total = 0;
1604 for (i=0; i<8; ++i) {
1605 total += vals[i].u64;
1607 tt_assert(total >= (U64_LITERAL(1)<<60));
1608 tt_assert(total <= (U64_LITERAL(1)<<62));
1610 for (i=0; i<8; ++i) {
1611 double ratio = ((double)vals[i].u64) / vals[2].u64;
1612 tt_double_op(fabs(ratio - v[i]), <, .00001);
1615 done:
1619 static void
1620 test_dir_random_weighted(void *testdata)
1622 int histogram[10];
1623 uint64_t vals[10] = {3,1,2,4,6,0,7,5,8,9}, total=0;
1624 u64_dbl_t inp[10];
1625 int i, choice;
1626 const int n = 50000;
1627 double max_sq_error;
1628 (void) testdata;
1630 /* Try a ten-element array with values from 0 through 10. The values are
1631 * in a scrambled order to make sure we don't depend on order. */
1632 memset(histogram,0,sizeof(histogram));
1633 for (i=0; i<10; ++i) {
1634 inp[i].u64 = vals[i];
1635 total += vals[i];
1637 tt_int_op(total, ==, 45);
1638 for (i=0; i<n; ++i) {
1639 choice = choose_array_element_by_weight(inp, 10);
1640 tt_int_op(choice, >=, 0);
1641 tt_int_op(choice, <, 10);
1642 histogram[choice]++;
1645 /* Now see if we chose things about frequently enough. */
1646 max_sq_error = 0;
1647 for (i=0; i<10; ++i) {
1648 int expected = (int)(n*vals[i]/total);
1649 double frac_diff = 0, sq;
1650 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
1651 if (expected)
1652 frac_diff = (histogram[i] - expected) / ((double)expected);
1653 else
1654 tt_int_op(histogram[i], ==, 0);
1656 sq = frac_diff * frac_diff;
1657 if (sq > max_sq_error)
1658 max_sq_error = sq;
1660 /* It should almost always be much much less than this. If you want to
1661 * figure out the odds, please feel free. */
1662 tt_double_op(max_sq_error, <, .05);
1664 /* Now try a singleton; do we choose it? */
1665 for (i = 0; i < 100; ++i) {
1666 choice = choose_array_element_by_weight(inp, 1);
1667 tt_int_op(choice, ==, 0);
1670 /* Now try an array of zeros. We should choose randomly. */
1671 memset(histogram,0,sizeof(histogram));
1672 for (i = 0; i < 5; ++i)
1673 inp[i].u64 = 0;
1674 for (i = 0; i < n; ++i) {
1675 choice = choose_array_element_by_weight(inp, 5);
1676 tt_int_op(choice, >=, 0);
1677 tt_int_op(choice, <, 5);
1678 histogram[choice]++;
1680 /* Now see if we chose things about frequently enough. */
1681 max_sq_error = 0;
1682 for (i=0; i<5; ++i) {
1683 int expected = n/5;
1684 double frac_diff = 0, sq;
1685 TT_BLATHER((" %d : %5d vs %5d\n", (int)vals[i], histogram[i], expected));
1686 frac_diff = (histogram[i] - expected) / ((double)expected);
1687 sq = frac_diff * frac_diff;
1688 if (sq > max_sq_error)
1689 max_sq_error = sq;
1691 /* It should almost always be much much less than this. If you want to
1692 * figure out the odds, please feel free. */
1693 tt_double_op(max_sq_error, <, .05);
1694 done:
1698 /* Function pointers for test_dir_clip_unmeasured_bw() */
1701 * Generate a routerstatus for clip_unmeasured_bw test; based on the
1702 * v3_networkstatus ones.
1704 static vote_routerstatus_t *
1705 gen_routerstatus_for_umbw(int idx, time_t now)
1707 vote_routerstatus_t *vrs;
1708 routerstatus_t *rs;
1709 tor_addr_t addr_ipv6;
1711 switch (idx) {
1712 case 0:
1713 /* Generate the first routerstatus. */
1714 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
1715 rs = &vrs->status;
1716 vrs->version = tor_strdup("0.1.2.14");
1717 rs->published_on = now-1500;
1718 strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
1719 memset(rs->identity_digest, 3, DIGEST_LEN);
1720 memset(rs->descriptor_digest, 78, DIGEST_LEN);
1721 rs->addr = 0x99008801;
1722 rs->or_port = 443;
1723 rs->dir_port = 8000;
1724 /* all flags but running cleared */
1725 rs->is_flagged_running = 1;
1727 * This one has measured bandwidth below the clip cutoff, and
1728 * so shouldn't be clipped; we'll have to test that it isn't
1729 * later.
1731 rs->has_measured_bw = 1;
1732 rs->has_bandwidth = 1;
1733 rs->measured_bw = rs->bandwidth = DEFAULT_MAX_UNMEASURED_BW / 2;
1734 break;
1735 case 1:
1736 /* Generate the second routerstatus. */
1737 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
1738 rs = &vrs->status;
1739 vrs->version = tor_strdup("0.2.0.5");
1740 rs->published_on = now-1000;
1741 strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
1742 memset(rs->identity_digest, 5, DIGEST_LEN);
1743 memset(rs->descriptor_digest, 77, DIGEST_LEN);
1744 rs->addr = 0x99009901;
1745 rs->or_port = 443;
1746 rs->dir_port = 0;
1747 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1748 tor_addr_copy(&rs->ipv6_addr, &addr_ipv6);
1749 rs->ipv6_orport = 4711;
1750 rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
1751 rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
1753 * This one has measured bandwidth above the clip cutoff, and
1754 * so shouldn't be clipped; we'll have to test that it isn't
1755 * later.
1757 rs->has_measured_bw = 1;
1758 rs->has_bandwidth = 1;
1759 rs->measured_bw = rs->bandwidth = 2 * DEFAULT_MAX_UNMEASURED_BW;
1760 break;
1761 case 2:
1762 /* Generate the third routerstatus. */
1763 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
1764 rs = &vrs->status;
1765 vrs->version = tor_strdup("0.1.0.3");
1766 rs->published_on = now-1000;
1767 strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
1768 memset(rs->identity_digest, 0x33, DIGEST_LEN);
1769 memset(rs->descriptor_digest, 79, DIGEST_LEN);
1770 rs->addr = 0xAA009901;
1771 rs->or_port = 400;
1772 rs->dir_port = 9999;
1773 rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
1774 rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
1775 rs->is_possible_guard = 1;
1777 * This one has unmeasured bandwidth above the clip cutoff, and
1778 * so should be clipped; we'll have to test that it isn't
1779 * later.
1781 rs->has_measured_bw = 0;
1782 rs->has_bandwidth = 1;
1783 rs->measured_bw = 0;
1784 rs->bandwidth = 2 * DEFAULT_MAX_UNMEASURED_BW;
1785 break;
1786 case 3:
1787 /* Generate a fourth routerstatus that is not running. */
1788 vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
1789 rs = &vrs->status;
1790 vrs->version = tor_strdup("0.1.6.3");
1791 rs->published_on = now-1000;
1792 strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
1793 memset(rs->identity_digest, 0x34, DIGEST_LEN);
1794 memset(rs->descriptor_digest, 47, DIGEST_LEN);
1795 rs->addr = 0xC0000203;
1796 rs->or_port = 500;
1797 rs->dir_port = 1999;
1798 /* all flags but running cleared */
1799 rs->is_flagged_running = 1;
1801 * This one has unmeasured bandwidth below the clip cutoff, and
1802 * so shouldn't be clipped; we'll have to test that it isn't
1803 * later.
1805 rs->has_measured_bw = 0;
1806 rs->has_bandwidth = 1;
1807 rs->measured_bw = 0;
1808 rs->bandwidth = DEFAULT_MAX_UNMEASURED_BW / 2;
1809 break;
1810 case 4:
1811 /* No more for this test; return NULL */
1812 vrs = NULL;
1813 break;
1814 default:
1815 /* Shouldn't happen */
1816 test_assert(0);
1819 done:
1820 return vrs;
1823 /** Apply tweaks to the vote list for each voter; for the umbw test this is
1824 * just adding the right consensus methods to let clipping happen */
1825 static void
1826 vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
1828 test_assert(v);
1829 (void)voter;
1830 (void)now;
1832 test_assert(v->supported_methods);
1833 smartlist_clear(v->supported_methods);
1834 /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW */
1835 smartlist_split_string(v->supported_methods,
1836 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17",
1837 NULL, 0, -1);
1839 done:
1840 return;
1844 * Test a parsed vote_routerstatus_t for umbw test.
1846 static void
1847 test_vrs_for_umbw(vote_routerstatus_t *vrs, int voter, time_t now)
1849 routerstatus_t *rs;
1850 tor_addr_t addr_ipv6;
1852 (void)voter;
1853 test_assert(vrs);
1854 rs = &(vrs->status);
1855 test_assert(rs);
1857 /* Split out by digests to test */
1858 if (tor_memeq(rs->identity_digest,
1859 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1860 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1861 DIGEST_LEN)) {
1863 * Check the first routerstatus - measured bandwidth below the clip
1864 * cutoff.
1866 test_streq(vrs->version, "0.1.2.14");
1867 test_eq(rs->published_on, now-1500);
1868 test_streq(rs->nickname, "router2");
1869 test_memeq(rs->identity_digest,
1870 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1871 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1872 DIGEST_LEN);
1873 test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1874 test_eq(rs->addr, 0x99008801);
1875 test_eq(rs->or_port, 443);
1876 test_eq(rs->dir_port, 8000);
1877 test_assert(rs->has_bandwidth);
1878 test_assert(rs->has_measured_bw);
1879 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
1880 test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW / 2);
1881 } else if (tor_memeq(rs->identity_digest,
1882 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1883 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1884 DIGEST_LEN)) {
1887 * Check the second routerstatus - measured bandwidth above the clip
1888 * cutoff.
1890 test_streq(vrs->version, "0.2.0.5");
1891 test_eq(rs->published_on, now-1000);
1892 test_streq(rs->nickname, "router1");
1893 test_memeq(rs->identity_digest,
1894 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1895 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1896 DIGEST_LEN);
1897 test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
1898 test_eq(rs->addr, 0x99009901);
1899 test_eq(rs->or_port, 443);
1900 test_eq(rs->dir_port, 0);
1901 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
1902 test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
1903 test_eq(rs->ipv6_orport, 4711);
1904 test_assert(rs->has_bandwidth);
1905 test_assert(rs->has_measured_bw);
1906 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
1907 test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW * 2);
1908 } else if (tor_memeq(rs->identity_digest,
1909 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
1910 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
1911 DIGEST_LEN)) {
1913 * Check the third routerstatus - unmeasured bandwidth above the clip
1914 * cutoff; this one should be clipped later on in the consensus, but
1915 * appears unclipped in the vote.
1917 test_assert(rs->has_bandwidth);
1918 test_assert(!(rs->has_measured_bw));
1919 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
1920 test_eq(rs->measured_bw, 0);
1921 } else if (tor_memeq(rs->identity_digest,
1922 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
1923 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
1924 DIGEST_LEN)) {
1926 * Check the fourth routerstatus - unmeasured bandwidth below the clip
1927 * cutoff; this one should not be clipped.
1929 test_assert(rs->has_bandwidth);
1930 test_assert(!(rs->has_measured_bw));
1931 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
1932 test_eq(rs->measured_bw, 0);
1933 } else {
1934 test_assert(0);
1937 done:
1938 return;
1942 * Test a consensus for v3_networkstatus_test
1944 static void
1945 test_consensus_for_umbw(networkstatus_t *con, time_t now)
1947 (void)now;
1949 test_assert(con);
1950 test_assert(!con->cert);
1951 /* test_assert(con->consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW); */
1952 test_assert(con->consensus_method >= 16);
1953 test_eq(4, smartlist_len(con->routerstatus_list));
1954 /* There should be four listed routers; all voters saw the same in this */
1956 done:
1957 return;
1961 * Test a router list entry for umbw test
1963 static void
1964 test_routerstatus_for_umbw(routerstatus_t *rs, time_t now)
1966 tor_addr_t addr_ipv6;
1968 test_assert(rs);
1970 /* There should be four listed routers, as constructed above */
1971 if (tor_memeq(rs->identity_digest,
1972 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1973 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1974 DIGEST_LEN)) {
1975 test_memeq(rs->identity_digest,
1976 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3"
1977 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1978 DIGEST_LEN);
1979 test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
1980 test_assert(!rs->is_authority);
1981 test_assert(!rs->is_exit);
1982 test_assert(!rs->is_fast);
1983 test_assert(!rs->is_possible_guard);
1984 test_assert(!rs->is_stable);
1985 /* (If it wasn't running it wouldn't be here) */
1986 test_assert(rs->is_flagged_running);
1987 test_assert(!rs->is_v2_dir);
1988 test_assert(!rs->is_valid);
1989 test_assert(!rs->is_named);
1990 /* This one should have measured bandwidth below the clip cutoff */
1991 test_assert(rs->has_bandwidth);
1992 test_assert(rs->has_measured_bw);
1993 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
1994 test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW / 2);
1995 } else if (tor_memeq(rs->identity_digest,
1996 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
1997 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1998 DIGEST_LEN)) {
1999 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2000 test_memeq(rs->identity_digest,
2001 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5"
2002 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2003 DIGEST_LEN);
2004 test_streq(rs->nickname, "router1");
2005 test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
2006 test_eq(rs->published_on, now-1000);
2007 test_eq(rs->addr, 0x99009901);
2008 test_eq(rs->or_port, 443);
2009 test_eq(rs->dir_port, 0);
2010 tor_addr_parse(&addr_ipv6, "[1:2:3::4]");
2011 test_assert(tor_addr_eq(&rs->ipv6_addr, &addr_ipv6));
2012 test_eq(rs->ipv6_orport, 4711);
2013 test_assert(!rs->is_authority);
2014 test_assert(rs->is_exit);
2015 test_assert(rs->is_fast);
2016 test_assert(rs->is_possible_guard);
2017 test_assert(rs->is_stable);
2018 test_assert(rs->is_flagged_running);
2019 test_assert(rs->is_v2_dir);
2020 test_assert(rs->is_valid);
2021 test_assert(!rs->is_named);
2022 /* This one should have measured bandwidth above the clip cutoff */
2023 test_assert(rs->has_bandwidth);
2024 test_assert(rs->has_measured_bw);
2025 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW * 2);
2026 test_eq(rs->measured_bw, DEFAULT_MAX_UNMEASURED_BW * 2);
2027 } else if (tor_memeq(rs->identity_digest,
2028 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33"
2029 "\x33\x33\x33\x33\x33\x33\x33\x33\x33\x33",
2030 DIGEST_LEN)) {
2032 * This one should have unmeasured bandwidth above the clip cutoff,
2033 * and so should be clipped
2035 test_assert(rs->has_bandwidth);
2036 test_assert(!(rs->has_measured_bw));
2037 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW);
2038 test_eq(rs->measured_bw, 0);
2039 } else if (tor_memeq(rs->identity_digest,
2040 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34"
2041 "\x34\x34\x34\x34\x34\x34\x34\x34\x34\x34",
2042 DIGEST_LEN)) {
2044 * This one should have unmeasured bandwidth below the clip cutoff,
2045 * and so should not be clipped
2047 test_assert(rs->has_bandwidth);
2048 test_assert(!(rs->has_measured_bw));
2049 test_eq(rs->bandwidth, DEFAULT_MAX_UNMEASURED_BW / 2);
2050 test_eq(rs->measured_bw, 0);
2051 } else {
2052 /* Weren't expecting this... */
2053 test_assert(0);
2056 done:
2057 return;
2061 * Compute a consensus involving clipping unmeasured bandwidth with consensus
2062 * method 17; this uses the same test_a_networkstatus() function that the
2063 * v3_networkstatus test uses.
2066 static void
2067 test_dir_clip_unmeasured_bw(void)
2069 test_a_networkstatus(gen_routerstatus_for_umbw,
2070 vote_tweaks_for_umbw,
2071 test_vrs_for_umbw,
2072 test_consensus_for_umbw,
2073 test_routerstatus_for_umbw);
2076 #define DIR_LEGACY(name) \
2077 { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
2079 #define DIR(name) \
2080 { #name, test_dir_##name, 0, NULL, NULL }
2082 struct testcase_t dir_tests[] = {
2083 DIR_LEGACY(nicknames),
2084 DIR_LEGACY(formats),
2085 DIR_LEGACY(versions),
2086 DIR_LEGACY(fp_pairs),
2087 DIR(split_fps),
2088 DIR_LEGACY(measured_bw),
2089 DIR_LEGACY(param_voting),
2090 DIR_LEGACY(v3_networkstatus),
2091 DIR(random_weighted),
2092 DIR(scale_bw),
2093 DIR_LEGACY(clip_unmeasured_bw),
2094 END_OF_TESTCASES