1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2012, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 #define DIRSERV_PRIVATE
8 #define DIRVOTE_PRIVATE
10 #define HIBERNATE_PRIVATE
12 #include "directory.h"
15 #include "hibernate.h"
16 #include "networkstatus.h"
18 #include "routerlist.h"
19 #include "routerparse.h"
23 test_dir_nicknames(void)
25 test_assert( is_legal_nickname("a"));
26 test_assert(!is_legal_nickname(""));
27 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
28 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
29 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
30 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
32 test_assert( is_legal_nickname_or_hexdigest(
33 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
34 test_assert( is_legal_nickname_or_hexdigest(
35 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
36 test_assert( is_legal_nickname_or_hexdigest(
37 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
39 test_assert(!is_legal_nickname_or_hexdigest(
40 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
42 test_assert(!is_legal_nickname_or_hexdigest(
43 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
44 /* hex part too long */
45 test_assert(!is_legal_nickname_or_hexdigest(
46 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
47 test_assert(!is_legal_nickname_or_hexdigest(
48 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
50 test_assert(!is_legal_nickname_or_hexdigest(
51 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
52 test_assert(!is_legal_nickname_or_hexdigest(
53 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
54 test_assert(!is_legal_nickname_or_hexdigest(
55 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
56 test_assert(!is_legal_nickname_or_hexdigest(
57 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
58 "abcdefghijklmnoppqrst"));
60 test_assert(!is_legal_nickname_or_hexdigest(
61 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
62 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
63 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
64 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
69 /** Run unit tests for router descriptor generation logic. */
71 test_dir_formats(void)
73 char buf
[8192], buf2
[8192];
75 char fingerprint
[FINGERPRINT_LEN
+1];
76 char *pk1_str
= NULL
, *pk2_str
= NULL
, *pk3_str
= NULL
, *cp
;
77 size_t pk1_str_len
, pk2_str_len
, pk3_str_len
;
78 routerinfo_t
*r1
=NULL
, *r2
=NULL
;
79 crypto_pk_t
*pk1
= NULL
, *pk2
= NULL
, *pk3
= NULL
;
80 routerinfo_t
*rp1
= NULL
;
81 addr_policy_t
*ex1
, *ex2
;
82 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
88 test_assert(pk1
&& pk2
&& pk3
);
90 hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE
);
92 get_platform_str(platform
, sizeof(platform
));
93 r1
= tor_malloc_zero(sizeof(routerinfo_t
));
94 r1
->address
= tor_strdup("18.244.0.1");
95 r1
->addr
= 0xc0a80001u
; /* 192.168.0.1 */
96 r1
->cache_info
.published_on
= 0;
99 tor_addr_parse(&r1
->ipv6_addr
, "1:2:3:4::");
100 r1
->ipv6_orport
= 9999;
101 r1
->onion_pkey
= crypto_pk_dup_key(pk1
);
102 r1
->identity_pkey
= crypto_pk_dup_key(pk2
);
103 r1
->bandwidthrate
= 1000;
104 r1
->bandwidthburst
= 5000;
105 r1
->bandwidthcapacity
= 10000;
106 r1
->exit_policy
= NULL
;
107 r1
->nickname
= tor_strdup("Magri");
108 r1
->platform
= tor_strdup(platform
);
110 ex1
= tor_malloc_zero(sizeof(addr_policy_t
));
111 ex2
= tor_malloc_zero(sizeof(addr_policy_t
));
112 ex1
->policy_type
= ADDR_POLICY_ACCEPT
;
113 tor_addr_from_ipv4h(&ex1
->addr
, 0);
115 ex1
->prt_min
= ex1
->prt_max
= 80;
116 ex2
->policy_type
= ADDR_POLICY_REJECT
;
117 tor_addr_from_ipv4h(&ex2
->addr
, 18<<24);
119 ex2
->prt_min
= ex2
->prt_max
= 24;
120 r2
= tor_malloc_zero(sizeof(routerinfo_t
));
121 r2
->address
= tor_strdup("1.1.1.1");
122 r2
->addr
= 0x0a030201u
; /* 10.3.2.1 */
123 r2
->platform
= tor_strdup(platform
);
124 r2
->cache_info
.published_on
= 5;
127 r2
->onion_pkey
= crypto_pk_dup_key(pk2
);
128 r2
->identity_pkey
= crypto_pk_dup_key(pk1
);
129 r2
->bandwidthrate
= r2
->bandwidthburst
= r2
->bandwidthcapacity
= 3000;
130 r2
->exit_policy
= smartlist_new();
131 smartlist_add(r2
->exit_policy
, ex2
);
132 smartlist_add(r2
->exit_policy
, ex1
);
133 r2
->nickname
= tor_strdup("Fred");
135 test_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
137 test_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
139 test_assert(!crypto_pk_write_public_key_to_string(pk3
, &pk3_str
,
142 memset(buf
, 0, 2048);
143 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
145 strlcpy(buf2
, "router Magri 18.244.0.1 9000 0 9003\n"
146 "or-address [1:2:3:4::]:9999\n"
147 "platform Tor "VERSION
" on ", sizeof(buf2
));
148 strlcat(buf2
, get_uname(), sizeof(buf2
));
150 "opt protocols Link 1 2 Circuit 1\n"
151 "published 1970-01-01 00:00:00\n"
152 "opt fingerprint ", sizeof(buf2
));
153 test_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
154 strlcat(buf2
, fingerprint
, sizeof(buf2
));
155 strlcat(buf2
, "\nuptime 0\n"
156 /* XXX the "0" above is hard-coded, but even if we made it reflect
157 * uptime, that still wouldn't make it right, because the two
158 * descriptors might be made on different seconds... hm. */
159 "bandwidth 1000 5000 10000\n"
160 "onion-key\n", sizeof(buf2
));
161 strlcat(buf2
, pk1_str
, sizeof(buf2
));
162 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
163 strlcat(buf2
, pk2_str
, sizeof(buf2
));
164 strlcat(buf2
, "opt hidden-service-dir\n", sizeof(buf2
));
165 strlcat(buf2
, "reject *:*\nrouter-signature\n", sizeof(buf2
));
166 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
169 test_streq(buf
, buf2
);
171 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
173 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
);
175 test_streq(rp1
->address
, r1
->address
);
176 test_eq(rp1
->or_port
, r1
->or_port
);
177 //test_eq(rp1->dir_port, r1->dir_port);
178 test_eq(rp1
->bandwidthrate
, r1
->bandwidthrate
);
179 test_eq(rp1
->bandwidthburst
, r1
->bandwidthburst
);
180 test_eq(rp1
->bandwidthcapacity
, r1
->bandwidthcapacity
);
181 test_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
182 test_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
183 //test_assert(rp1->exit_policy == NULL);
186 /* XXX Once we have exit policies, test this again. XXX */
187 strlcpy(buf2
, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2
));
188 strlcat(buf2
, pk2_str
, sizeof(buf2
));
189 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
190 strlcat(buf2
, pk1_str
, sizeof(buf2
));
191 strlcat(buf2
, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2
));
192 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk2
)>0);
193 test_streq(buf
, buf2
);
196 rp2
= router_parse_entry_from_string(&cp
,1);
198 test_streq(rp2
->address
, r2
.address
);
199 test_eq(rp2
->or_port
, r2
.or_port
);
200 test_eq(rp2
->dir_port
, r2
.dir_port
);
201 test_eq(rp2
->bandwidth
, r2
.bandwidth
);
202 test_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
203 test_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
204 test_eq(rp2
->exit_policy
->policy_type
, EXIT_POLICY_ACCEPT
);
205 test_streq(rp2
->exit_policy
->string
, "accept *:80");
206 test_streq(rp2
->exit_policy
->address
, "*");
207 test_streq(rp2
->exit_policy
->port
, "80");
208 test_eq(rp2
->exit_policy
->next
->policy_type
, EXIT_POLICY_REJECT
);
209 test_streq(rp2
->exit_policy
->next
->string
, "reject 18.*:24");
210 test_streq(rp2
->exit_policy
->next
->address
, "18.*");
211 test_streq(rp2
->exit_policy
->next
->port
, "24");
212 test_assert(rp2
->exit_policy
->next
->next
== NULL
);
214 /* Okay, now for the directories. */
216 fingerprint_list
= smartlist_new();
217 crypto_pk_get_fingerprint(pk2
, buf
, 1);
218 add_fingerprint_to_dir("Magri", buf
, fingerprint_list
);
219 crypto_pk_get_fingerprint(pk1
, buf
, 1);
220 add_fingerprint_to_dir("Fred", buf
, fingerprint_list
);
226 /* XXXX NM re-enable. */
227 /* Make sure routers aren't too far in the past any more. */
228 r1
->cache_info
.published_on
= time(NULL
);
229 r2
->cache_info
.published_on
= time(NULL
)-3*60*60;
230 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
231 test_eq(dirserv_add_descriptor(buf
,&m
,""), ROUTER_ADDED_NOTIFY_GENERATOR
);
232 test_assert(router_dump_router_to_string(buf
, 2048, r2
, pk1
)>0);
233 test_eq(dirserv_add_descriptor(buf
,&m
,""), ROUTER_ADDED_NOTIFY_GENERATOR
);
234 get_options()->Nickname
= tor_strdup("DirServer");
235 test_assert(!dirserv_dump_directory_to_string(&cp
,pk3
, 0));
236 crypto_pk_get_digest(pk3
, d
);
237 test_assert(!router_parse_directory(cp
));
238 test_eq(2, smartlist_len(dir1
->routers
));
242 dirserv_free_fingerprint_list();
253 if (pk1
) crypto_pk_free(pk1
);
254 if (pk2
) crypto_pk_free(pk2
);
255 if (pk3
) crypto_pk_free(pk3
);
256 if (rp1
) routerinfo_free(rp1
);
257 tor_free(dir1
); /* XXXX And more !*/
258 tor_free(dir2
); /* And more !*/
262 test_dir_versions(void)
266 /* Try out version parsing functionality */
267 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1
));
268 test_eq(0, ver1
.major
);
269 test_eq(3, ver1
.minor
);
270 test_eq(4, ver1
.micro
);
271 test_eq(VER_PRE
, ver1
.status
);
272 test_eq(2, ver1
.patchlevel
);
273 test_eq(0, tor_version_parse("0.3.4rc1", &ver1
));
274 test_eq(0, ver1
.major
);
275 test_eq(3, ver1
.minor
);
276 test_eq(4, ver1
.micro
);
277 test_eq(VER_RC
, ver1
.status
);
278 test_eq(1, ver1
.patchlevel
);
279 test_eq(0, tor_version_parse("1.3.4", &ver1
));
280 test_eq(1, ver1
.major
);
281 test_eq(3, ver1
.minor
);
282 test_eq(4, ver1
.micro
);
283 test_eq(VER_RELEASE
, ver1
.status
);
284 test_eq(0, ver1
.patchlevel
);
285 test_eq(0, tor_version_parse("1.3.4.999", &ver1
));
286 test_eq(1, ver1
.major
);
287 test_eq(3, ver1
.minor
);
288 test_eq(4, ver1
.micro
);
289 test_eq(VER_RELEASE
, ver1
.status
);
290 test_eq(999, ver1
.patchlevel
);
291 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1
));
292 test_eq(0, ver1
.major
);
293 test_eq(1, ver1
.minor
);
294 test_eq(2, ver1
.micro
);
295 test_eq(4, ver1
.patchlevel
);
296 test_eq(VER_RELEASE
, ver1
.status
);
297 test_streq("alpha", ver1
.status_tag
);
298 test_eq(0, tor_version_parse("0.1.2.4", &ver1
));
299 test_eq(0, ver1
.major
);
300 test_eq(1, ver1
.minor
);
301 test_eq(2, ver1
.micro
);
302 test_eq(4, ver1
.patchlevel
);
303 test_eq(VER_RELEASE
, ver1
.status
);
304 test_streq("", ver1
.status_tag
);
306 #define tt_versionstatus_op(vs1, op, vs2) \
307 tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t, \
308 (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
309 #define test_v_i_o(val, ver, lst) \
310 tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
312 /* make sure tor_version_is_obsolete() works */
313 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
314 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
315 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
316 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
317 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
318 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
319 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
320 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
321 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
322 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
323 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
324 /* Not on list, but newer than any in same series. */
325 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
326 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
327 /* Series newer than any on list. */
328 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");
329 /* Series older than any on list. */
330 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");
331 /* Not on list, not newer than any on same series. */
332 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
333 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
334 /* On list, not newer than any on same series. */
335 test_v_i_o(VS_UNRECOMMENDED
,
336 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
337 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
338 test_eq(1, tor_version_as_new_as(
339 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
340 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
342 test_eq(0, tor_version_as_new_as(
343 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
344 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
346 /* Now try svn revisions. */
347 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
348 "Tor 0.2.1.0-dev (r99)"));
349 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
350 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
351 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
352 "Tor 0.2.1.0-dev on Colossus"));
353 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
354 "Tor 0.2.1.0-dev (r100)"));
355 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
356 "Tor 0.2.1.0-dev (r100) on AM"));
357 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
358 "Tor 0.2.1.0-dev (r99)"));
359 test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
360 "Tor 0.2.1.0-dev (r99)"));
362 /* Now try git revisions */
363 test_eq(0, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1
));
364 test_eq(0, ver1
.major
);
365 test_eq(5, ver1
.minor
);
366 test_eq(6, ver1
.micro
);
367 test_eq(7, ver1
.patchlevel
);
368 test_eq(3, ver1
.git_tag_len
);
369 test_memeq(ver1
.git_tag
, "\xff\x00\xff", 3);
370 test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1
));
371 test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1
));
372 test_eq(0, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1
));
377 /** Run unit tests for directory fp_pair functions. */
379 test_dir_fp_pairs(void)
381 smartlist_t
*sl
= smartlist_new();
384 dir_split_resource_into_fingerprint_pairs(
385 /* Two pairs, out of order, with one duplicate. */
386 "73656372657420646174612E0000000000FFFFFF-"
387 "557365204145532d32353620696e73746561642e+"
388 "73656372657420646174612E0000000000FFFFFF-"
389 "557365204145532d32353620696e73746561642e+"
390 "48657861646563696d616c2069736e277420736f-"
391 "676f6f6420666f7220686964696e6720796f7572.z", sl
);
393 test_eq(smartlist_len(sl
), 2);
394 pair
= smartlist_get(sl
, 0);
395 test_memeq(pair
->first
, "Hexadecimal isn't so", DIGEST_LEN
);
396 test_memeq(pair
->second
, "good for hiding your", DIGEST_LEN
);
397 pair
= smartlist_get(sl
, 1);
398 test_memeq(pair
->first
, "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN
);
399 test_memeq(pair
->second
, "Use AES-256 instead.", DIGEST_LEN
);
402 SMARTLIST_FOREACH(sl
, fp_pair_t
*, pair
, tor_free(pair
));
407 test_dir_split_fps(void *testdata
)
409 smartlist_t
*sl
= smartlist_new();
410 char *mem_op_hex_tmp
= NULL
;
413 /* Some example hex fingerprints and their base64 equivalents */
414 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
415 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
416 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
418 "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
420 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
422 "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
423 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
424 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
425 #define B64_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
426 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
427 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
428 #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
431 dir_split_resource_into_fingerprints("A+C+B", sl
, NULL
, 0);
432 tt_int_op(smartlist_len(sl
), ==, 3);
433 tt_str_op(smartlist_get(sl
, 0), ==, "A");
434 tt_str_op(smartlist_get(sl
, 1), ==, "C");
435 tt_str_op(smartlist_get(sl
, 2), ==, "B");
436 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
440 dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl
, NULL
, DSR_SORT_UNIQ
);
441 tt_int_op(smartlist_len(sl
), ==, 3);
442 tt_str_op(smartlist_get(sl
, 0), ==, "A");
443 tt_str_op(smartlist_get(sl
, 1), ==, "B");
444 tt_str_op(smartlist_get(sl
, 2), ==, "C");
445 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
449 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
, sl
, NULL
, DSR_HEX
);
450 tt_int_op(smartlist_len(sl
), ==, 2);
451 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX1
);
452 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX2
);
453 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
456 /* decode hex and drop weirdness. */
457 dir_split_resource_into_fingerprints(HEX1
"+bogus+"HEX2
"+"HEX256_1
,
459 tt_int_op(smartlist_len(sl
), ==, 2);
460 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX1
);
461 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX2
);
462 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
465 /* Decode long hex */
466 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX2
"+"HEX256_3
,
467 sl
, NULL
, DSR_HEX
|DSR_DIGEST256
);
468 tt_int_op(smartlist_len(sl
), ==, 3);
469 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX256_1
);
470 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX256_2
);
471 test_mem_op_hex(smartlist_get(sl
, 2), ==, HEX256_3
);
472 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
475 /* Decode hex and sort. */
476 dir_split_resource_into_fingerprints(HEX1
"+"HEX2
"+"HEX3
"+"HEX2
,
477 sl
, NULL
, DSR_HEX
|DSR_SORT_UNIQ
);
478 tt_int_op(smartlist_len(sl
), ==, 3);
479 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX3
);
480 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX2
);
481 test_mem_op_hex(smartlist_get(sl
, 2), ==, HEX1
);
482 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
485 /* Decode long hex and sort */
486 dir_split_resource_into_fingerprints(HEX256_1
"+"HEX256_2
"+"HEX256_3
489 DSR_HEX
|DSR_DIGEST256
|DSR_SORT_UNIQ
);
490 tt_int_op(smartlist_len(sl
), ==, 3);
491 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX256_3
);
492 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX256_2
);
493 test_mem_op_hex(smartlist_get(sl
, 2), ==, HEX256_1
);
494 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
498 dir_split_resource_into_fingerprints(B64_1
"-"B64_2
, sl
, NULL
, DSR_BASE64
);
499 tt_int_op(smartlist_len(sl
), ==, 2);
500 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX1
);
501 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX2
);
502 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
505 /* Decode long base64 */
506 dir_split_resource_into_fingerprints(B64_256_1
"-"B64_256_2
,
507 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
508 tt_int_op(smartlist_len(sl
), ==, 2);
509 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX256_1
);
510 test_mem_op_hex(smartlist_get(sl
, 1), ==, HEX256_2
);
511 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
514 dir_split_resource_into_fingerprints(B64_256_1
,
515 sl
, NULL
, DSR_BASE64
|DSR_DIGEST256
);
516 tt_int_op(smartlist_len(sl
), ==, 1);
517 test_mem_op_hex(smartlist_get(sl
, 0), ==, HEX256_1
);
518 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
522 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
524 tor_free(mem_op_hex_tmp
);
528 test_dir_measured_bw(void)
530 measured_bw_line_t mbwl
;
532 const char *lines_pass
[] = {
533 "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
534 "node_id=$557365204145532d32353620696e73746561642e\t bw=1024 \n",
535 " node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
536 "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e "
537 "bw=1024 junk=007\n",
538 "misc=junk node_id=$557365204145532d32353620696e73746561642e "
539 "bw=1024 junk=007\n",
542 const char *lines_fail
[] = {
543 /* Test possible python stupidity on input */
544 "node_id=None bw=1024\n",
545 "node_id=$None bw=1024\n",
546 "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
547 "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
548 "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
549 "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
550 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
551 "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
552 "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
553 /* Test incomplete writes due to race conditions, partial copies, etc */
558 "node_id=$557365204145532d32353620696e73746561642e bw=",
559 "node_id=$557365204145532d32353620696e73746561642e bw=1024",
560 "node_id=$557365204145532d32353620696e73746561642e bw=\n",
561 "node_id=$557365204145532d32353620696e7374",
562 "node_id=$557365204145532d32353620696e7374\n",
567 /* Test assorted noise */
569 "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
570 "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
571 "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
572 "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
576 for (i
= 0; strcmp(lines_fail
[i
], "end"); i
++) {
577 //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
578 test_assert(measured_bw_line_parse(&mbwl
, lines_fail
[i
]) == -1);
581 for (i
= 0; strcmp(lines_pass
[i
], "end"); i
++) {
582 //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
583 test_assert(measured_bw_line_parse(&mbwl
, lines_pass
[i
]) == 0);
584 test_assert(mbwl
.bw
== 1024);
585 test_assert(strcmp(mbwl
.node_hex
,
586 "557365204145532d32353620696e73746561642e") == 0);
594 test_dir_param_voting(void)
596 networkstatus_t vote1
, vote2
, vote3
, vote4
;
597 smartlist_t
*votes
= smartlist_new();
600 /* dirvote_compute_params only looks at the net_params field of the votes,
601 so that's all we need to set.
603 memset(&vote1
, 0, sizeof(vote1
));
604 memset(&vote2
, 0, sizeof(vote2
));
605 memset(&vote3
, 0, sizeof(vote3
));
606 memset(&vote4
, 0, sizeof(vote4
));
607 vote1
.net_params
= smartlist_new();
608 vote2
.net_params
= smartlist_new();
609 vote3
.net_params
= smartlist_new();
610 vote4
.net_params
= smartlist_new();
611 smartlist_split_string(vote1
.net_params
,
612 "ab=90 abcd=20 cw=50 x-yz=-99", NULL
, 0, 0);
613 smartlist_split_string(vote2
.net_params
,
614 "ab=27 cw=5 x-yz=88", NULL
, 0, 0);
615 smartlist_split_string(vote3
.net_params
,
616 "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL
, 0, 0);
617 smartlist_split_string(vote4
.net_params
,
618 "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL
, 0, 0);
619 test_eq(100, networkstatus_get_param(&vote4
, "x-yz", 50, 0, 300));
620 test_eq(222, networkstatus_get_param(&vote4
, "foobar", 222, 0, 300));
621 test_eq(80, networkstatus_get_param(&vote4
, "ab", 12, 0, 80));
622 test_eq(-8, networkstatus_get_param(&vote4
, "ab", -12, -100, -8));
623 test_eq(0, networkstatus_get_param(&vote4
, "foobar", 0, -100, 8));
625 smartlist_add(votes
, &vote1
);
627 /* Do the first tests without adding all the other votes, for
628 * networks without many dirauths. */
630 res
= dirvote_compute_params(votes
, 11, 6);
631 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-99");
634 res
= dirvote_compute_params(votes
, 12, 2);
638 res
= dirvote_compute_params(votes
, 12, 1);
639 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-99");
642 smartlist_add(votes
, &vote2
);
644 res
= dirvote_compute_params(votes
, 11, 2);
645 test_streq(res
, "ab=27 abcd=20 cw=5 x-yz=-99");
648 res
= dirvote_compute_params(votes
, 12, 2);
649 test_streq(res
, "ab=27 cw=5 x-yz=-99");
652 res
= dirvote_compute_params(votes
, 12, 3);
653 test_streq(res
, "ab=27 cw=5 x-yz=-99");
656 res
= dirvote_compute_params(votes
, 12, 6);
660 smartlist_add(votes
, &vote3
);
662 res
= dirvote_compute_params(votes
, 11, 3);
663 test_streq(res
, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
666 res
= dirvote_compute_params(votes
, 12, 3);
667 test_streq(res
, "ab=27 abcd=20 cw=50 x-yz=-9");
670 res
= dirvote_compute_params(votes
, 12, 5);
671 test_streq(res
, "cw=50 x-yz=-9");
674 res
= dirvote_compute_params(votes
, 12, 9);
675 test_streq(res
, "cw=50 x-yz=-9");
678 smartlist_add(votes
, &vote4
);
680 res
= dirvote_compute_params(votes
, 11, 4);
681 test_streq(res
, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
684 res
= dirvote_compute_params(votes
, 12, 4);
685 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-9");
688 res
= dirvote_compute_params(votes
, 12, 5);
689 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-9");
692 /* Test that the special-cased "at least three dirauths voted for
693 * this param" logic works as expected. */
694 res
= dirvote_compute_params(votes
, 12, 6);
695 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-9");
698 res
= dirvote_compute_params(votes
, 12, 10);
699 test_streq(res
, "ab=90 abcd=20 cw=50 x-yz=-9");
704 SMARTLIST_FOREACH(vote1
.net_params
, char *, cp
, tor_free(cp
));
705 SMARTLIST_FOREACH(vote2
.net_params
, char *, cp
, tor_free(cp
));
706 SMARTLIST_FOREACH(vote3
.net_params
, char *, cp
, tor_free(cp
));
707 SMARTLIST_FOREACH(vote4
.net_params
, char *, cp
, tor_free(cp
));
708 smartlist_free(vote1
.net_params
);
709 smartlist_free(vote2
.net_params
);
710 smartlist_free(vote3
.net_params
);
711 smartlist_free(vote4
.net_params
);
712 smartlist_free(votes
);
717 extern const char AUTHORITY_CERT_1
[];
718 extern const char AUTHORITY_SIGNKEY_1
[];
719 extern const char AUTHORITY_CERT_2
[];
720 extern const char AUTHORITY_SIGNKEY_2
[];
721 extern const char AUTHORITY_CERT_3
[];
722 extern const char AUTHORITY_SIGNKEY_3
[];
724 /** Helper: Test that two networkstatus_voter_info_t do in fact represent the
725 * same voting authority, and that they do in fact have all the same
728 test_same_voter(networkstatus_voter_info_t
*v1
,
729 networkstatus_voter_info_t
*v2
)
731 test_streq(v1
->nickname
, v2
->nickname
);
732 test_memeq(v1
->identity_digest
, v2
->identity_digest
, DIGEST_LEN
);
733 test_streq(v1
->address
, v2
->address
);
734 test_eq(v1
->addr
, v2
->addr
);
735 test_eq(v1
->dir_port
, v2
->dir_port
);
736 test_eq(v1
->or_port
, v2
->or_port
);
737 test_streq(v1
->contact
, v2
->contact
);
738 test_memeq(v1
->vote_digest
, v2
->vote_digest
, DIGEST_LEN
);
743 /** Helper: Make a new routerinfo containing the right information for a
744 * given vote_routerstatus_t. */
745 static routerinfo_t
*
746 generate_ri_from_rs(const vote_routerstatus_t
*vrs
)
749 const routerstatus_t
*rs
= &vrs
->status
;
750 static time_t published
= 0;
752 r
= tor_malloc_zero(sizeof(routerinfo_t
));
753 memcpy(r
->cache_info
.identity_digest
, rs
->identity_digest
, DIGEST_LEN
);
754 memcpy(r
->cache_info
.signed_descriptor_digest
, rs
->descriptor_digest
,
756 r
->cache_info
.do_not_cache
= 1;
757 r
->cache_info
.routerlist_index
= -1;
758 r
->cache_info
.signed_descriptor_body
=
759 tor_strdup("123456789012345678901234567890123");
760 r
->cache_info
.signed_descriptor_len
=
761 strlen(r
->cache_info
.signed_descriptor_body
);
762 r
->exit_policy
= smartlist_new();
763 r
->cache_info
.published_on
= ++published
+ time(NULL
);
767 /** Helper: get a detached signatures document for one or two
770 get_detached_sigs(networkstatus_t
*ns
, networkstatus_t
*ns2
)
774 tor_assert(ns
&& ns
->flavor
== FLAV_NS
);
775 sl
= smartlist_new();
776 smartlist_add(sl
,ns
);
778 smartlist_add(sl
,ns2
);
779 r
= networkstatus_get_detached_signatures(sl
);
784 /** Run unit tests for generating and parsing V3 consensus networkstatus
787 test_dir_v3_networkstatus(void)
789 authority_cert_t
*cert1
=NULL
, *cert2
=NULL
, *cert3
=NULL
;
790 crypto_pk_t
*sign_skey_1
=NULL
, *sign_skey_2
=NULL
, *sign_skey_3
=NULL
;
791 crypto_pk_t
*sign_skey_leg1
=NULL
;
792 const char *msg
=NULL
;
794 time_t now
= time(NULL
);
795 networkstatus_voter_info_t
*voter
;
796 document_signature_t
*sig
;
797 networkstatus_t
*vote
=NULL
, *v1
=NULL
, *v2
=NULL
, *v3
=NULL
, *con
=NULL
,
799 vote_routerstatus_t
*vrs
;
801 char *v1_text
=NULL
, *v2_text
=NULL
, *v3_text
=NULL
, *consensus_text
=NULL
, *cp
;
802 smartlist_t
*votes
= smartlist_new();
804 /* For generating the two other consensuses. */
805 char *detached_text1
=NULL
, *detached_text2
=NULL
;
806 char *consensus_text2
=NULL
, *consensus_text3
=NULL
;
807 char *consensus_text_md2
=NULL
, *consensus_text_md3
=NULL
;
808 char *consensus_text_md
=NULL
;
809 networkstatus_t
*con2
=NULL
, *con_md2
=NULL
, *con3
=NULL
, *con_md3
=NULL
;
810 ns_detached_signatures_t
*dsig1
=NULL
, *dsig2
=NULL
;
812 /* Parse certificates and keys. */
813 cert1
= authority_cert_parse_from_string(AUTHORITY_CERT_1
, NULL
);
815 test_assert(cert1
->is_cross_certified
);
816 cert2
= authority_cert_parse_from_string(AUTHORITY_CERT_2
, NULL
);
818 cert3
= authority_cert_parse_from_string(AUTHORITY_CERT_3
, NULL
);
820 sign_skey_1
= crypto_pk_new();
821 sign_skey_2
= crypto_pk_new();
822 sign_skey_3
= crypto_pk_new();
823 sign_skey_leg1
= pk_generate(4);
825 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1
,
826 AUTHORITY_SIGNKEY_1
, -1));
827 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2
,
828 AUTHORITY_SIGNKEY_2
, -1));
829 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3
,
830 AUTHORITY_SIGNKEY_3
, -1));
832 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, cert1
->signing_key
));
833 test_assert(!crypto_pk_cmp_keys(sign_skey_2
, cert2
->signing_key
));
836 * Set up a vote; generate it; try to parse it.
838 vote
= tor_malloc_zero(sizeof(networkstatus_t
));
839 vote
->type
= NS_TYPE_VOTE
;
840 vote
->published
= now
;
841 vote
->valid_after
= now
+1000;
842 vote
->fresh_until
= now
+2000;
843 vote
->valid_until
= now
+3000;
844 vote
->vote_seconds
= 100;
845 vote
->dist_seconds
= 200;
846 vote
->supported_methods
= smartlist_new();
847 smartlist_split_string(vote
->supported_methods
, "1 2 3", NULL
, 0, -1);
848 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.15");
849 vote
->server_versions
= tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
850 vote
->known_flags
= smartlist_new();
851 smartlist_split_string(vote
->known_flags
,
852 "Authority Exit Fast Guard Running Stable V2Dir Valid",
853 0, SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
854 vote
->voters
= smartlist_new();
855 voter
= tor_malloc_zero(sizeof(networkstatus_voter_info_t
));
856 voter
->nickname
= tor_strdup("Voter1");
857 voter
->address
= tor_strdup("1.2.3.4");
858 voter
->addr
= 0x01020304;
859 voter
->dir_port
= 80;
860 voter
->or_port
= 9000;
861 voter
->contact
= tor_strdup("voter@example.com");
862 crypto_pk_get_digest(cert1
->identity_key
, voter
->identity_digest
);
863 smartlist_add(vote
->voters
, voter
);
864 vote
->cert
= authority_cert_dup(cert1
);
865 vote
->net_params
= smartlist_new();
866 smartlist_split_string(vote
->net_params
, "circuitwindow=101 foo=990",
868 vote
->routerstatus_list
= smartlist_new();
869 /* add the first routerstatus. */
870 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
872 vrs
->version
= tor_strdup("0.1.2.14");
873 rs
->published_on
= now
-1500;
874 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
875 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
876 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
877 rs
->addr
= 0x99008801;
880 /* all flags but running cleared */
881 rs
->is_flagged_running
= 1;
882 smartlist_add(vote
->routerstatus_list
, vrs
);
883 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
), &msg
,0,0)>=0);
885 /* add the second routerstatus. */
886 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
888 vrs
->version
= tor_strdup("0.2.0.5");
889 rs
->published_on
= now
-1000;
890 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
891 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
892 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
893 rs
->addr
= 0x99009901;
896 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_flagged_running
=
897 rs
->is_valid
= rs
->is_v2_dir
= rs
->is_possible_guard
= 1;
898 smartlist_add(vote
->routerstatus_list
, vrs
);
899 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
), &msg
,0,0)>=0);
901 /* add the third routerstatus. */
902 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
904 vrs
->version
= tor_strdup("0.1.0.3");
905 rs
->published_on
= now
-1000;
906 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
907 memset(rs
->identity_digest
, 33, DIGEST_LEN
);
908 memset(rs
->descriptor_digest
, 79, DIGEST_LEN
);
909 rs
->addr
= 0xAA009901;
912 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
913 rs
->is_flagged_running
= rs
->is_valid
= rs
->is_v2_dir
=
914 rs
->is_possible_guard
= 1;
915 smartlist_add(vote
->routerstatus_list
, vrs
);
916 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
), &msg
,0,0)>=0);
918 /* add a fourth routerstatus that is not running. */
919 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
921 vrs
->version
= tor_strdup("0.1.6.3");
922 rs
->published_on
= now
-1000;
923 strlcpy(rs
->nickname
, "router4", sizeof(rs
->nickname
));
924 memset(rs
->identity_digest
, 34, DIGEST_LEN
);
925 memset(rs
->descriptor_digest
, 47, DIGEST_LEN
);
926 rs
->addr
= 0xC0000203;
929 /* Running flag (and others) cleared */
930 smartlist_add(vote
->routerstatus_list
, vrs
);
931 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
), &msg
,0,0)>=0);
933 /* dump the vote and try to parse it. */
934 v1_text
= format_networkstatus_vote(sign_skey_1
, vote
);
935 test_assert(v1_text
);
936 v1
= networkstatus_parse_vote_from_string(v1_text
, NULL
, NS_TYPE_VOTE
);
939 /* Make sure the parsed thing was right. */
940 test_eq(v1
->type
, NS_TYPE_VOTE
);
941 test_eq(v1
->published
, vote
->published
);
942 test_eq(v1
->valid_after
, vote
->valid_after
);
943 test_eq(v1
->fresh_until
, vote
->fresh_until
);
944 test_eq(v1
->valid_until
, vote
->valid_until
);
945 test_eq(v1
->vote_seconds
, vote
->vote_seconds
);
946 test_eq(v1
->dist_seconds
, vote
->dist_seconds
);
947 test_streq(v1
->client_versions
, vote
->client_versions
);
948 test_streq(v1
->server_versions
, vote
->server_versions
);
949 test_assert(v1
->voters
&& smartlist_len(v1
->voters
));
950 voter
= smartlist_get(v1
->voters
, 0);
951 test_streq(voter
->nickname
, "Voter1");
952 test_streq(voter
->address
, "1.2.3.4");
953 test_eq(voter
->addr
, 0x01020304);
954 test_eq(voter
->dir_port
, 80);
955 test_eq(voter
->or_port
, 9000);
956 test_streq(voter
->contact
, "voter@example.com");
957 test_assert(v1
->cert
);
958 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, v1
->cert
->signing_key
));
959 cp
= smartlist_join_strings(v1
->known_flags
, ":", 0, NULL
);
960 test_streq(cp
, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
962 test_eq(smartlist_len(v1
->routerstatus_list
), 4);
963 /* Check the first routerstatus. */
964 vrs
= smartlist_get(v1
->routerstatus_list
, 0);
966 test_streq(vrs
->version
, "0.1.2.14");
967 test_eq(rs
->published_on
, now
-1500);
968 test_streq(rs
->nickname
, "router2");
969 test_memeq(rs
->identity_digest
,
970 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
972 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
973 test_eq(rs
->addr
, 0x99008801);
974 test_eq(rs
->or_port
, 443);
975 test_eq(rs
->dir_port
, 8000);
976 test_eq(vrs
->flags
, U64_LITERAL(16)); // no flags except "running"
977 /* Check the second routerstatus. */
978 vrs
= smartlist_get(v1
->routerstatus_list
, 1);
980 test_streq(vrs
->version
, "0.2.0.5");
981 test_eq(rs
->published_on
, now
-1000);
982 test_streq(rs
->nickname
, "router1");
983 test_memeq(rs
->identity_digest
,
984 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
986 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
987 test_eq(rs
->addr
, 0x99009901);
988 test_eq(rs
->or_port
, 443);
989 test_eq(rs
->dir_port
, 0);
990 test_eq(vrs
->flags
, U64_LITERAL(254)); // all flags except "authority."
993 measured_bw_line_t mbw
;
994 memset(mbw
.node_id
, 33, sizeof(mbw
.node_id
));
996 test_assert(measured_bw_line_apply(&mbw
,
997 v1
->routerstatus_list
) == 1);
998 vrs
= smartlist_get(v1
->routerstatus_list
, 2);
999 test_assert(vrs
->status
.has_measured_bw
&&
1000 vrs
->status
.measured_bw
== 1024);
1003 /* Generate second vote. It disagrees on some of the times,
1004 * and doesn't list versions, and knows some crazy flags */
1005 vote
->published
= now
+1;
1006 vote
->fresh_until
= now
+3005;
1007 vote
->dist_seconds
= 300;
1008 authority_cert_free(vote
->cert
);
1009 vote
->cert
= authority_cert_dup(cert2
);
1010 vote
->net_params
= smartlist_new();
1011 smartlist_split_string(vote
->net_params
, "bar=2000000000 circuitwindow=20",
1013 tor_free(vote
->client_versions
);
1014 tor_free(vote
->server_versions
);
1015 voter
= smartlist_get(vote
->voters
, 0);
1016 tor_free(voter
->nickname
);
1017 tor_free(voter
->address
);
1018 voter
->nickname
= tor_strdup("Voter2");
1019 voter
->address
= tor_strdup("2.3.4.5");
1020 voter
->addr
= 0x02030405;
1021 crypto_pk_get_digest(cert2
->identity_key
, voter
->identity_digest
);
1022 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfCheese"));
1023 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfTin"));
1024 smartlist_sort_strings(vote
->known_flags
);
1025 vrs
= smartlist_get(vote
->routerstatus_list
, 2);
1026 smartlist_del_keeporder(vote
->routerstatus_list
, 2);
1027 tor_free(vrs
->version
);
1029 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
1030 vrs
->status
.is_fast
= 1;
1031 /* generate and parse. */
1032 v2_text
= format_networkstatus_vote(sign_skey_2
, vote
);
1033 test_assert(v2_text
);
1034 v2
= networkstatus_parse_vote_from_string(v2_text
, NULL
, NS_TYPE_VOTE
);
1036 /* Check that flags come out right.*/
1037 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
1038 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1039 "Running:Stable:V2Dir:Valid");
1041 vrs
= smartlist_get(v2
->routerstatus_list
, 1);
1042 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
1043 test_eq(vrs
->flags
, U64_LITERAL(974));
1045 /* Generate the third vote. */
1046 vote
->published
= now
;
1047 vote
->fresh_until
= now
+2003;
1048 vote
->dist_seconds
= 250;
1049 authority_cert_free(vote
->cert
);
1050 vote
->cert
= authority_cert_dup(cert3
);
1051 vote
->net_params
= smartlist_new();
1052 smartlist_split_string(vote
->net_params
, "circuitwindow=80 foo=660",
1054 smartlist_add(vote
->supported_methods
, tor_strdup("4"));
1055 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.17");
1056 vote
->server_versions
= tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
1057 voter
= smartlist_get(vote
->voters
, 0);
1058 tor_free(voter
->nickname
);
1059 tor_free(voter
->address
);
1060 voter
->nickname
= tor_strdup("Voter3");
1061 voter
->address
= tor_strdup("3.4.5.6");
1062 voter
->addr
= 0x03040506;
1063 crypto_pk_get_digest(cert3
->identity_key
, voter
->identity_digest
);
1064 /* This one has a legacy id. */
1065 memset(voter
->legacy_id_digest
, (int)'A', DIGEST_LEN
);
1066 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
1067 smartlist_del_keeporder(vote
->routerstatus_list
, 0);
1068 tor_free(vrs
->version
);
1070 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
1071 memset(vrs
->status
.descriptor_digest
, (int)'Z', DIGEST_LEN
);
1072 test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs
), &msg
,0,0)>=0);
1074 v3_text
= format_networkstatus_vote(sign_skey_3
, vote
);
1075 test_assert(v3_text
);
1077 v3
= networkstatus_parse_vote_from_string(v3_text
, NULL
, NS_TYPE_VOTE
);
1080 /* Compute a consensus as voter 3. */
1081 smartlist_add(votes
, v3
);
1082 smartlist_add(votes
, v1
);
1083 smartlist_add(votes
, v2
);
1084 consensus_text
= networkstatus_compute_consensus(votes
, 3,
1085 cert3
->identity_key
,
1087 "AAAAAAAAAAAAAAAAAAAA",
1090 test_assert(consensus_text
);
1091 con
= networkstatus_parse_vote_from_string(consensus_text
, NULL
,
1094 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
1095 // v1_text, v2_text, v3_text);
1096 consensus_text_md
= networkstatus_compute_consensus(votes
, 3,
1097 cert3
->identity_key
,
1099 "AAAAAAAAAAAAAAAAAAAA",
1102 test_assert(consensus_text_md
);
1103 con_md
= networkstatus_parse_vote_from_string(consensus_text_md
, NULL
,
1105 test_assert(con_md
);
1106 test_eq(con_md
->flavor
, FLAV_MICRODESC
);
1108 /* Check consensus contents. */
1109 test_assert(con
->type
== NS_TYPE_CONSENSUS
);
1110 test_eq(con
->published
, 0); /* this field only appears in votes. */
1111 test_eq(con
->valid_after
, now
+1000);
1112 test_eq(con
->fresh_until
, now
+2003); /* median */
1113 test_eq(con
->valid_until
, now
+3000);
1114 test_eq(con
->vote_seconds
, 100);
1115 test_eq(con
->dist_seconds
, 250); /* median */
1116 test_streq(con
->client_versions
, "0.1.2.14");
1117 test_streq(con
->server_versions
, "0.1.2.15,0.1.2.16");
1118 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
1119 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
1120 "Running:Stable:V2Dir:Valid");
1122 cp
= smartlist_join_strings(con
->net_params
, ":", 0, NULL
);
1123 test_streq(cp
, "circuitwindow=80:foo=660");
1126 test_eq(4, smartlist_len(con
->voters
)); /*3 voters, 1 legacy key.*/
1127 /* The voter id digests should be in this order. */
1128 test_assert(memcmp(cert2
->cache_info
.identity_digest
,
1129 cert1
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
1130 test_assert(memcmp(cert1
->cache_info
.identity_digest
,
1131 cert3
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
1132 test_same_voter(smartlist_get(con
->voters
, 1),
1133 smartlist_get(v2
->voters
, 0));
1134 test_same_voter(smartlist_get(con
->voters
, 2),
1135 smartlist_get(v1
->voters
, 0));
1136 test_same_voter(smartlist_get(con
->voters
, 3),
1137 smartlist_get(v3
->voters
, 0));
1139 test_assert(!con
->cert
);
1140 test_eq(2, smartlist_len(con
->routerstatus_list
));
1141 /* There should be two listed routers: one with identity 3, one with
1143 /* This one showed up in 2 digests. */
1144 rs
= smartlist_get(con
->routerstatus_list
, 0);
1145 test_memeq(rs
->identity_digest
,
1146 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
1148 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
1149 test_assert(!rs
->is_authority
);
1150 test_assert(!rs
->is_exit
);
1151 test_assert(!rs
->is_fast
);
1152 test_assert(!rs
->is_possible_guard
);
1153 test_assert(!rs
->is_stable
);
1154 /* (If it wasn't running it wouldn't be here) */
1155 test_assert(rs
->is_flagged_running
);
1156 test_assert(!rs
->is_v2_dir
);
1157 test_assert(!rs
->is_valid
);
1158 test_assert(!rs
->is_named
);
1159 /* XXXX check version */
1161 rs
= smartlist_get(con
->routerstatus_list
, 1);
1162 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
1163 test_memeq(rs
->identity_digest
,
1164 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
1166 test_streq(rs
->nickname
, "router1");
1167 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
1168 test_eq(rs
->published_on
, now
-1000);
1169 test_eq(rs
->addr
, 0x99009901);
1170 test_eq(rs
->or_port
, 443);
1171 test_eq(rs
->dir_port
, 0);
1172 test_assert(!rs
->is_authority
);
1173 test_assert(rs
->is_exit
);
1174 test_assert(rs
->is_fast
);
1175 test_assert(rs
->is_possible_guard
);
1176 test_assert(rs
->is_stable
);
1177 test_assert(rs
->is_flagged_running
);
1178 test_assert(rs
->is_v2_dir
);
1179 test_assert(rs
->is_valid
);
1180 test_assert(!rs
->is_named
);
1181 /* XXXX check version */
1183 /* Check signatures. the first voter is a pseudo-entry with a legacy key.
1184 * The second one hasn't signed. The fourth one has signed: validate it. */
1185 voter
= smartlist_get(con
->voters
, 1);
1186 test_eq(smartlist_len(voter
->sigs
), 0);
1188 voter
= smartlist_get(con
->voters
, 3);
1189 test_eq(smartlist_len(voter
->sigs
), 1);
1190 sig
= smartlist_get(voter
->sigs
, 0);
1191 test_assert(sig
->signature
);
1192 test_assert(!sig
->good_signature
);
1193 test_assert(!sig
->bad_signature
);
1195 test_assert(!networkstatus_check_document_signature(con
, sig
, cert3
));
1196 test_assert(sig
->signature
);
1197 test_assert(sig
->good_signature
);
1198 test_assert(!sig
->bad_signature
);
1201 const char *msg
=NULL
;
1202 /* Compute the other two signed consensuses. */
1203 smartlist_shuffle(votes
);
1204 consensus_text2
= networkstatus_compute_consensus(votes
, 3,
1205 cert2
->identity_key
,
1206 sign_skey_2
, NULL
,NULL
,
1208 consensus_text_md2
= networkstatus_compute_consensus(votes
, 3,
1209 cert2
->identity_key
,
1210 sign_skey_2
, NULL
,NULL
,
1212 smartlist_shuffle(votes
);
1213 consensus_text3
= networkstatus_compute_consensus(votes
, 3,
1214 cert1
->identity_key
,
1215 sign_skey_1
, NULL
,NULL
,
1217 consensus_text_md3
= networkstatus_compute_consensus(votes
, 3,
1218 cert1
->identity_key
,
1219 sign_skey_1
, NULL
,NULL
,
1221 test_assert(consensus_text2
);
1222 test_assert(consensus_text3
);
1223 test_assert(consensus_text_md2
);
1224 test_assert(consensus_text_md3
);
1225 con2
= networkstatus_parse_vote_from_string(consensus_text2
, NULL
,
1227 con3
= networkstatus_parse_vote_from_string(consensus_text3
, NULL
,
1229 con_md2
= networkstatus_parse_vote_from_string(consensus_text_md2
, NULL
,
1231 con_md3
= networkstatus_parse_vote_from_string(consensus_text_md3
, NULL
,
1235 test_assert(con_md2
);
1236 test_assert(con_md3
);
1238 /* All three should have the same digest. */
1239 test_memeq(&con
->digests
, &con2
->digests
, sizeof(digests_t
));
1240 test_memeq(&con
->digests
, &con3
->digests
, sizeof(digests_t
));
1242 test_memeq(&con_md
->digests
, &con_md2
->digests
, sizeof(digests_t
));
1243 test_memeq(&con_md
->digests
, &con_md3
->digests
, sizeof(digests_t
));
1245 /* Extract a detached signature from con3. */
1246 detached_text1
= get_detached_sigs(con3
, con_md3
);
1247 tt_assert(detached_text1
);
1248 /* Try to parse it. */
1249 dsig1
= networkstatus_parse_detached_signatures(detached_text1
, NULL
);
1252 /* Are parsed values as expected? */
1253 test_eq(dsig1
->valid_after
, con3
->valid_after
);
1254 test_eq(dsig1
->fresh_until
, con3
->fresh_until
);
1255 test_eq(dsig1
->valid_until
, con3
->valid_until
);
1257 digests_t
*dsig_digests
= strmap_get(dsig1
->digests
, "ns");
1258 test_assert(dsig_digests
);
1259 test_memeq(dsig_digests
->d
[DIGEST_SHA1
], con3
->digests
.d
[DIGEST_SHA1
],
1261 dsig_digests
= strmap_get(dsig1
->digests
, "microdesc");
1262 test_assert(dsig_digests
);
1263 test_memeq(dsig_digests
->d
[DIGEST_SHA256
],
1264 con_md3
->digests
.d
[DIGEST_SHA256
],
1268 smartlist_t
*dsig_signatures
= strmap_get(dsig1
->signatures
, "ns");
1269 test_assert(dsig_signatures
);
1270 test_eq(1, smartlist_len(dsig_signatures
));
1271 sig
= smartlist_get(dsig_signatures
, 0);
1272 test_memeq(sig
->identity_digest
, cert1
->cache_info
.identity_digest
,
1274 test_eq(sig
->alg
, DIGEST_SHA1
);
1276 dsig_signatures
= strmap_get(dsig1
->signatures
, "microdesc");
1277 test_assert(dsig_signatures
);
1278 test_eq(1, smartlist_len(dsig_signatures
));
1279 sig
= smartlist_get(dsig_signatures
, 0);
1280 test_memeq(sig
->identity_digest
, cert1
->cache_info
.identity_digest
,
1282 test_eq(sig
->alg
, DIGEST_SHA256
);
1285 /* Try adding it to con2. */
1286 detached_text2
= get_detached_sigs(con2
,con_md2
);
1287 test_eq(1, networkstatus_add_detached_signatures(con2
, dsig1
, "test",
1289 tor_free(detached_text2
);
1290 test_eq(1, networkstatus_add_detached_signatures(con_md2
, dsig1
, "test",
1292 tor_free(detached_text2
);
1293 detached_text2
= get_detached_sigs(con2
,con_md2
);
1294 //printf("\n<%s>\n", detached_text2);
1295 dsig2
= networkstatus_parse_detached_signatures(detached_text2
, NULL
);
1299 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
1301 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
1306 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
, "ns")));
1308 smartlist_len((smartlist_t
*)strmap_get(dsig2
->signatures
,
1311 /* Try adding to con2 twice; verify that nothing changes. */
1312 test_eq(0, networkstatus_add_detached_signatures(con2
, dsig1
, "test",
1316 test_eq(2, networkstatus_add_detached_signatures(con
, dsig2
, "test",
1318 /* Check signatures */
1319 voter
= smartlist_get(con
->voters
, 1);
1320 sig
= smartlist_get(voter
->sigs
, 0);
1322 test_assert(!networkstatus_check_document_signature(con
, sig
, cert2
));
1323 voter
= smartlist_get(con
->voters
, 2);
1324 sig
= smartlist_get(voter
->sigs
, 0);
1326 test_assert(!networkstatus_check_document_signature(con
, sig
, cert1
));
1330 smartlist_free(votes
);
1334 tor_free(consensus_text
);
1335 tor_free(consensus_text_md
);
1338 networkstatus_vote_free(vote
);
1340 networkstatus_vote_free(v1
);
1342 networkstatus_vote_free(v2
);
1344 networkstatus_vote_free(v3
);
1346 networkstatus_vote_free(con
);
1348 networkstatus_vote_free(con_md
);
1350 crypto_pk_free(sign_skey_1
);
1352 crypto_pk_free(sign_skey_2
);
1354 crypto_pk_free(sign_skey_3
);
1356 crypto_pk_free(sign_skey_leg1
);
1358 authority_cert_free(cert1
);
1360 authority_cert_free(cert2
);
1362 authority_cert_free(cert3
);
1364 tor_free(consensus_text2
);
1365 tor_free(consensus_text3
);
1366 tor_free(consensus_text_md2
);
1367 tor_free(consensus_text_md3
);
1368 tor_free(detached_text1
);
1369 tor_free(detached_text2
);
1371 networkstatus_vote_free(con2
);
1373 networkstatus_vote_free(con3
);
1375 networkstatus_vote_free(con_md2
);
1377 networkstatus_vote_free(con_md3
);
1379 ns_detached_signatures_free(dsig1
);
1381 ns_detached_signatures_free(dsig2
);
1384 #define DIR_LEGACY(name) \
1385 { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
1388 { #name, test_dir_##name, 0, NULL, NULL }
1390 struct testcase_t dir_tests
[] = {
1391 DIR_LEGACY(nicknames
),
1392 DIR_LEGACY(formats
),
1393 DIR_LEGACY(versions
),
1394 DIR_LEGACY(fp_pairs
),
1396 DIR_LEGACY(measured_bw
),
1397 DIR_LEGACY(param_voting
),
1398 DIR_LEGACY(v3_networkstatus
),