1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
5 const char test_c_id
[] =
8 const char tor_svn_revision
[] = "";
12 * \brief Unit tests for many pieces of the lower level Tor modules.
28 /* These macros pull in declarations for some functions and structures that
29 * are typically file-private. */
30 #define CONFIG_PRIVATE
31 #define CONTROL_PRIVATE
32 #define CRYPTO_PRIVATE
33 #define DIRSERV_PRIVATE
34 #define DIRVOTE_PRIVATE
35 #define MEMPOOL_PRIVATE
36 #define ROUTER_PRIVATE
45 static char temp_dir
[256];
50 static int is_setup
= 0;
56 tor_snprintf(temp_dir
, sizeof(temp_dir
),
57 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
60 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
61 r
= mkdir(temp_dir
, 0700);
64 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
72 get_fname(const char *name
)
74 static char buf
[1024];
76 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
81 remove_directory(void)
83 smartlist_t
*elements
= tor_listdir(temp_dir
);
85 SMARTLIST_FOREACH(elements
, const char *, cp
,
87 size_t len
= strlen(cp
)+strlen(temp_dir
)+16;
88 char *tmp
= tor_malloc(len
);
89 tor_snprintf(tmp
, len
, "%s"PATH_SEPARATOR
"%s", temp_dir
, cp
);
93 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
94 smartlist_free(elements
);
99 static crypto_pk_env_t
*
102 static crypto_pk_env_t
*pregen
[3] = {NULL
, NULL
, NULL
};
103 tor_assert(idx
< (int)(sizeof(pregen
)/sizeof(pregen
[0])));
105 pregen
[idx
] = crypto_new_pk_env();
106 tor_assert(!crypto_pk_generate_key(pregen
[idx
]));
108 return crypto_pk_dup_key(pregen
[idx
]);
125 if (!(buf
= buf_new()))
128 test_eq(buf_capacity(buf
), 4096);
129 test_eq(buf_datalen(buf
), 0);
132 * General pointer frobbing
134 for (j
=0;j
<256;++j
) {
137 write_to_buf(str
, 256, buf
);
138 write_to_buf(str
, 256, buf
);
139 test_eq(buf_datalen(buf
), 512);
140 fetch_from_buf(str2
, 200, buf
);
141 test_memeq(str
, str2
, 200);
142 test_eq(buf_datalen(buf
), 312);
143 memset(str2
, 0, sizeof(str2
));
145 fetch_from_buf(str2
, 256, buf
);
146 test_memeq(str
+200, str2
, 56);
147 test_memeq(str
, str2
+56, 200);
148 test_eq(buf_datalen(buf
), 56);
149 memset(str2
, 0, sizeof(str2
));
150 /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
151 * another 3584 bytes, we hit the end. */
153 write_to_buf(str
, 256, buf
);
156 test_eq(buf_datalen(buf
), 3896);
157 fetch_from_buf(str2
, 56, buf
);
158 test_eq(buf_datalen(buf
), 3840);
159 test_memeq(str
+200, str2
, 56);
161 memset(str2
, 0, sizeof(str2
));
162 fetch_from_buf(str2
, 256, buf
);
163 test_memeq(str
, str2
, 256);
165 test_eq(buf_datalen(buf
), 0);
168 /* Okay, now make sure growing can work. */
169 buf
= buf_new_with_capacity(16);
170 test_eq(buf_capacity(buf
), 16);
171 write_to_buf(str
+1, 255, buf
);
172 test_eq(buf_capacity(buf
), 256);
173 fetch_from_buf(str2
, 254, buf
);
174 test_memeq(str
+1, str2
, 254);
175 test_eq(buf_capacity(buf
), 256);
177 write_to_buf(str
, 32, buf
);
178 test_eq(buf_capacity(buf
), 256);
180 write_to_buf(str
, 256, buf
);
182 test_eq(buf_capacity(buf
), 512);
183 test_eq(buf_datalen(buf
), 33+256);
184 fetch_from_buf(str2
, 33, buf
);
185 test_eq(*str2
, str
[255]);
187 test_memeq(str2
+1, str
, 32);
188 test_eq(buf_capacity(buf
), 512);
189 test_eq(buf_datalen(buf
), 256);
190 fetch_from_buf(str2
, 256, buf
);
191 test_memeq(str
, str2
, 256);
193 /* now try shrinking: case 1. */
195 buf
= buf_new_with_capacity(33668);
197 write_to_buf(str
,255, buf
);
199 test_eq(buf_capacity(buf
), 33668);
200 test_eq(buf_datalen(buf
), 17085);
201 for (j
=0; j
< 40; ++j
) {
202 fetch_from_buf(str2
, 255,buf
);
203 test_memeq(str2
, str
, 255);
206 /* now try shrinking: case 2. */
208 buf
= buf_new_with_capacity(33668);
210 write_to_buf(str
,255, buf
);
212 for (j
=0; j
< 20; ++j
) {
213 fetch_from_buf(str2
, 255,buf
);
214 test_memeq(str2
, str
, 255);
217 write_to_buf(str
,255, buf
);
219 test_eq(buf_capacity(buf
),33668);
220 for (j
=0; j
< 120; ++j
) {
221 fetch_from_buf(str2
, 255,buf
);
222 test_memeq(str2
, str
, 255);
225 /* Move from buf to buf. */
227 buf
= buf_new_with_capacity(4096);
228 buf2
= buf_new_with_capacity(4096);
230 write_to_buf(str
, 255, buf
);
231 test_eq(buf_datalen(buf
), 25500);
232 for (j
=0;j
<100;++j
) {
234 move_buf_to_buf(buf2
, buf
, &r
);
237 test_eq(buf_datalen(buf
), 24500);
238 test_eq(buf_datalen(buf2
), 1000);
240 fetch_from_buf(str2
, 255, buf2
);
241 test_memeq(str2
, str
, 255);
243 r
= 8192; /*big move*/
244 move_buf_to_buf(buf2
, buf
, &r
);
246 r
= 30000; /* incomplete move */
247 move_buf_to_buf(buf2
, buf
, &r
);
250 fetch_from_buf(str2
, 255, buf2
);
251 test_memeq(str2
, str
, 255);
265 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
269 s
= open(get_fname("data"), O_RDONLY
, 0);
271 errno
= 0; /* XXXX */
272 i
= read_to_buf(s
, 10, buf
, &eof
);
273 printf("%s\n", strerror(errno
));
276 test_eq(buf_capacity(buf
), 4096);
277 test_eq(buf_datalen(buf
), 10);
279 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10);
281 /* Test reading 0 bytes. */
282 i
= read_to_buf(s
, 0, buf
, &eof
);
283 test_eq(buf_capacity(buf
), 512*1024);
284 test_eq(buf_datalen(buf
), 10);
288 /* Now test when buffer is filled exactly. */
289 buf2
= buf_new_with_capacity(6);
290 i
= read_to_buf(s
, 6, buf2
, &eof
);
291 test_eq(buf_capacity(buf2
), 6);
292 test_eq(buf_datalen(buf2
), 6);
295 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
298 /* Now test when buffer is filled with more data to read. */
299 buf2
= buf_new_with_capacity(32);
300 i
= read_to_buf(s
, 128, buf2
, &eof
);
301 test_eq(buf_capacity(buf2
), 128);
302 test_eq(buf_datalen(buf2
), 32);
307 /* Now read to eof. */
308 test_assert(buf_capacity(buf
) > 256);
309 i
= read_to_buf(s
, 1024, buf
, &eof
);
310 test_eq(i
, (256-32-10-6));
311 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
312 test_eq(buf_datalen(buf
), 256-6-32);
313 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10); /* XXX Check rest. */
316 i
= read_to_buf(s
, 1024, buf
, &eof
);
318 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
319 test_eq(buf_datalen(buf
), 256-6-32);
328 crypto_dh_env_t
*dh1
, *dh2
;
335 dh1
= crypto_dh_new();
336 dh2
= crypto_dh_new();
337 test_eq(crypto_dh_get_bytes(dh1
), DH_BYTES
);
338 test_eq(crypto_dh_get_bytes(dh2
), DH_BYTES
);
340 memset(p1
, 0, DH_BYTES
);
341 memset(p2
, 0, DH_BYTES
);
342 test_memeq(p1
, p2
, DH_BYTES
);
343 test_assert(! crypto_dh_get_public(dh1
, p1
, DH_BYTES
));
344 test_memneq(p1
, p2
, DH_BYTES
);
345 test_assert(! crypto_dh_get_public(dh2
, p2
, DH_BYTES
));
346 test_memneq(p1
, p2
, DH_BYTES
);
348 memset(s1
, 0, DH_BYTES
);
349 memset(s2
, 0xFF, DH_BYTES
);
350 s1len
= crypto_dh_compute_secret(dh1
, p2
, DH_BYTES
, s1
, 50);
351 s2len
= crypto_dh_compute_secret(dh2
, p1
, DH_BYTES
, s2
, 50);
352 test_assert(s1len
> 0);
353 test_eq(s1len
, s2len
);
354 test_memeq(s1
, s2
, s1len
);
363 crypto_cipher_env_t
*env1
, *env2
;
364 crypto_pk_env_t
*pk1
, *pk2
;
365 char *data1
, *data2
, *data3
, *cp
;
369 data1
= tor_malloc(1024);
370 data2
= tor_malloc(1024);
371 data3
= tor_malloc(1024);
372 test_assert(data1
&& data2
&& data3
);
375 test_assert(! crypto_seed_rng());
376 crypto_rand(data1
, 100);
377 crypto_rand(data2
, 100);
378 test_memneq(data1
,data2
,100);
380 /* Now, test encryption and decryption with stream cipher. */
382 for (i
= 1023; i
>0; i
-= 35)
383 strncat(data1
, "Now is the time for all good onions", i
);
385 memset(data2
, 0, 1024);
386 memset(data3
, 0, 1024);
387 env1
= crypto_new_cipher_env();
389 env2
= crypto_new_cipher_env();
391 j
= crypto_cipher_generate_key(env1
);
392 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
393 crypto_cipher_encrypt_init_cipher(env1
);
394 crypto_cipher_decrypt_init_cipher(env2
);
396 /* Try encrypting 512 chars. */
397 crypto_cipher_encrypt(env1
, data2
, data1
, 512);
398 crypto_cipher_decrypt(env2
, data3
, data2
, 512);
399 test_memeq(data1
, data3
, 512);
400 test_memneq(data1
, data2
, 512);
402 /* Now encrypt 1 at a time, and get 1 at a time. */
403 for (j
= 512; j
< 560; ++j
) {
404 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 1);
406 for (j
= 512; j
< 560; ++j
) {
407 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 1);
409 test_memeq(data1
, data3
, 560);
410 /* Now encrypt 3 at a time, and get 5 at a time. */
411 for (j
= 560; j
< 1024-5; j
+= 3) {
412 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 3);
414 for (j
= 560; j
< 1024-5; j
+= 5) {
415 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 5);
417 test_memeq(data1
, data3
, 1024-5);
418 /* Now make sure that when we encrypt with different chunk sizes, we get
420 crypto_free_cipher_env(env2
);
422 memset(data3
, 0, 1024);
423 env2
= crypto_new_cipher_env();
425 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
426 crypto_cipher_encrypt_init_cipher(env2
);
427 for (j
= 0; j
< 1024-16; j
+= 17) {
428 crypto_cipher_encrypt(env2
, data3
+j
, data1
+j
, 17);
430 for (j
= 0; j
< 1024-16; ++j
) {
431 if (data2
[j
] != data3
[j
]) {
432 printf("%d: %d\t%d\n", j
, (int) data2
[j
], (int) data3
[j
]);
435 test_memeq(data2
, data3
, 1024-16);
436 crypto_free_cipher_env(env1
);
437 crypto_free_cipher_env(env2
);
439 /* Test vectors for stream ciphers. */
440 /* XXXX Look up some test vectors for the ciphers and make sure we match. */
442 /* Test SHA-1 with a test vector from the specification. */
443 i
= crypto_digest(data1
, "abc", 3);
445 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
446 "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
448 /* Public-key ciphers */
449 pk1
= pk_generate(0);
450 pk2
= crypto_new_pk_env();
451 test_assert(pk1
&& pk2
);
452 test_assert(! crypto_pk_write_public_key_to_string(pk1
, &cp
, &size
));
453 test_assert(! crypto_pk_read_public_key_from_string(pk2
, cp
, size
));
454 test_eq(0, crypto_pk_cmp_keys(pk1
, pk2
));
457 test_eq(128, crypto_pk_keysize(pk1
));
458 test_eq(128, crypto_pk_keysize(pk2
));
460 test_eq(128, crypto_pk_public_encrypt(pk2
, data1
, "Hello whirled.", 15,
461 PK_PKCS1_OAEP_PADDING
));
462 test_eq(128, crypto_pk_public_encrypt(pk1
, data2
, "Hello whirled.", 15,
463 PK_PKCS1_OAEP_PADDING
));
464 /* oaep padding should make encryption not match */
465 test_memneq(data1
, data2
, 128);
466 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data1
, 128,
467 PK_PKCS1_OAEP_PADDING
,1));
468 test_streq(data3
, "Hello whirled.");
469 memset(data3
, 0, 1024);
470 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
471 PK_PKCS1_OAEP_PADDING
,1));
472 test_streq(data3
, "Hello whirled.");
473 /* Can't decrypt with public key. */
474 test_eq(-1, crypto_pk_private_decrypt(pk2
, data3
, data2
, 128,
475 PK_PKCS1_OAEP_PADDING
,1));
476 /* Try again with bad padding */
477 memcpy(data2
+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
478 test_eq(-1, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
479 PK_PKCS1_OAEP_PADDING
,1));
481 /* File operations: save and load private key */
482 test_assert(! crypto_pk_write_private_key_to_filename(pk1
,
483 get_fname("pkey1")));
485 test_assert(! crypto_pk_read_private_key_from_filename(pk2
,
486 get_fname("pkey1")));
487 test_eq(15, crypto_pk_private_decrypt(pk2
, data3
, data1
, 128,
488 PK_PKCS1_OAEP_PADDING
,1));
490 /* Now try signing. */
491 strlcpy(data1
, "Ossifrage", 1024);
492 test_eq(128, crypto_pk_private_sign(pk1
, data2
, data1
, 10));
493 test_eq(10, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
494 test_streq(data3
, "Ossifrage");
495 /* Try signing digests. */
496 test_eq(128, crypto_pk_private_sign_digest(pk1
, data2
, data1
, 10));
497 test_eq(20, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
498 test_eq(0, crypto_pk_public_checksig_digest(pk1
, data1
, 10, data2
, 128));
499 test_eq(-1, crypto_pk_public_checksig_digest(pk1
, data1
, 11, data2
, 128));
500 /*XXXX test failed signing*/
503 crypto_free_pk_env(pk2
);
505 i
= crypto_pk_asn1_encode(pk1
, data1
, 1024);
507 pk2
= crypto_pk_asn1_decode(data1
, i
);
508 test_assert(crypto_pk_cmp_keys(pk1
,pk2
) == 0);
510 /* Try with hybrid encryption wrappers. */
511 crypto_rand(data1
, 1024);
512 for (i
= 0; i
< 3; ++i
) {
513 for (j
= 85; j
< 140; ++j
) {
514 memset(data2
,0,1024);
515 memset(data3
,0,1024);
516 if (i
== 0 && j
< 129)
518 p
= (i
==0)?PK_NO_PADDING
:
519 (i
==1)?PK_PKCS1_PADDING
:PK_PKCS1_OAEP_PADDING
;
520 len
= crypto_pk_public_hybrid_encrypt(pk1
,data2
,data1
,j
,p
,0);
522 len
= crypto_pk_private_hybrid_decrypt(pk1
,data3
,data2
,len
,p
,1);
524 test_memeq(data1
,data3
,j
);
527 crypto_free_pk_env(pk1
);
528 crypto_free_pk_env(pk2
);
531 strlcpy(data1
, "Test string that contains 35 chars.", 1024);
532 strlcat(data1
, " 2nd string that contains 35 chars.", 1024);
534 i
= base64_encode(data2
, 1024, data1
, 71);
535 j
= base64_decode(data3
, 1024, data2
, i
);
536 test_streq(data3
, data1
);
538 test_assert(data2
[i
] == '\0');
540 crypto_rand(data1
, DIGEST_LEN
);
541 memset(data2
, 100, 1024);
542 digest_to_base64(data2
, data1
);
543 test_eq(BASE64_DIGEST_LEN
, strlen(data2
));
544 test_eq(100, data2
[BASE64_DIGEST_LEN
+2]);
545 memset(data3
, 99, 1024);
546 digest_from_base64(data3
, data2
);
547 test_memeq(data1
, data3
, DIGEST_LEN
);
548 test_eq(99, data3
[DIGEST_LEN
+1]);
551 strlcpy(data1
, "5chrs", 1024);
552 /* bit pattern is: [35 63 68 72 73] ->
553 * [00110101 01100011 01101000 01110010 01110011]
554 * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
556 base32_encode(data2
, 9, data1
, 5);
557 test_streq(data2
, "gvrwq4tt");
559 strlcpy(data1
, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
560 base32_encode(data2
, 30, data1
, 10);
561 test_streq(data2
, "772w2rfobvomsywe");
564 strlcpy(data1
, "6chrs\xff", 1024);
565 base16_encode(data2
, 13, data1
, 6);
566 test_streq(data2
, "3663687273FF");
568 strlcpy(data1
, "f0d678affc000100", 1024);
569 i
= base16_decode(data2
, 8, data1
, 16);
571 test_memeq(data2
, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
573 /* now try some failing base16 decodes */
574 test_eq(-1, base16_decode(data2
, 8, data1
, 15)); /* odd input len */
575 test_eq(-1, base16_decode(data2
, 7, data1
, 16)); /* dest too short */
576 strlcpy(data1
, "f0dz!8affc000100", 1024);
577 test_eq(-1, base16_decode(data2
, 8, data1
, 16));
585 test_crypto_s2k(void)
592 memset(buf
, 0, sizeof(buf
));
593 memset(buf2
, 0, sizeof(buf2
));
594 buf3
= tor_malloc(65536);
595 memset(buf3
, 0, 65536);
597 secret_to_key(buf
+9, 20, "", 0, buf
);
598 crypto_digest(buf2
+9, buf3
, 1024);
599 test_memeq(buf
, buf2
, 29);
601 memcpy(buf
,"vrbacrda",8);
602 memcpy(buf2
,"vrbacrda",8);
605 secret_to_key(buf
+9, 20, "12345678", 8, buf
);
606 for (i
= 0; i
< 65536; i
+= 16) {
607 memcpy(buf3
+i
, "vrbacrda12345678", 16);
609 crypto_digest(buf2
+9, buf3
, 65536);
610 test_memeq(buf
, buf2
, 29);
614 _compare_strs(const void **a
, const void **b
)
616 const char *s1
= *a
, *s2
= *b
;
617 return strcmp(s1
, s2
);
621 _compare_without_first_ch(const void *a
, const void **b
)
623 const char *s1
= a
, *s2
= *b
;
624 return strcasecmp(s1
+1, s2
);
630 struct timeval start
, end
;
632 char timestr
[RFC1123_TIME_LEN
+1];
641 start
.tv_usec
= 5000;
646 test_eq(0L, tv_udiff(&start
, &end
));
650 test_assert(tv_cmp(&start
, &end
)<0);
651 test_assert(tv_cmp(&end
, &start
)>0);
652 test_assert(tv_cmp(&end
, &end
)==0);
654 test_eq(2000L, tv_udiff(&start
, &end
));
658 test_eq(1002000L, tv_udiff(&start
, &end
));
662 test_eq(995000L, tv_udiff(&start
, &end
));
666 test_eq(-1005000L, tv_udiff(&start
, &end
));
668 tv_addms(&end
, 5090);
669 test_eq(end
.tv_sec
, 9);
670 test_eq(end
.tv_usec
, 90000);
672 end
.tv_usec
= 999990;
675 tv_add(&start
, &end
);
676 test_eq(start
.tv_sec
, 11);
677 test_eq(start
.tv_usec
, 490);
679 /* The test values here are confirmed to be correct on a platform
680 * with a working timegm. */
681 a_time
.tm_year
= 2003-1900;
687 test_eq((time_t) 1062224095UL, tor_timegm(&a_time
));
688 a_time
.tm_year
= 2004-1900; /* Try a leap year, after feb. */
689 test_eq((time_t) 1093846495UL, tor_timegm(&a_time
));
690 a_time
.tm_mon
= 1; /* Try a leap year, in feb. */
692 test_eq((time_t) 1076393695UL, tor_timegm(&a_time
));
694 format_rfc1123_time(timestr
, 0);
695 test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr
);
696 format_rfc1123_time(timestr
, (time_t)1091580502UL);
697 test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr
);
700 i
= parse_rfc1123_time(timestr
, &t_res
);
702 test_eq(t_res
, (time_t)1091580502UL);
703 test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res
));
704 tor_gettimeofday(&start
);
706 /* Test tor_strstrip() */
707 strlcpy(buf
, "Testing 1 2 3", sizeof(buf
));
708 test_eq(0, tor_strstrip(buf
, ",!"));
709 test_streq(buf
, "Testing 1 2 3");
710 strlcpy(buf
, "!Testing 1 2 3?", sizeof(buf
));
711 test_eq(5, tor_strstrip(buf
, "!? "));
712 test_streq(buf
, "Testing123");
714 /* Test tor_strpartition() */
715 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefghi", "##", 3));
716 test_streq(buf
, "abc##def##ghi");
718 /* Test parse_addr_port */
719 cp
= NULL
; u32
= 3; u16
= 3;
720 test_assert(!parse_addr_port(LOG_WARN
, "1.2.3.4", &cp
, &u32
, &u16
));
721 test_streq(cp
, "1.2.3.4");
722 test_eq(u32
, 0x01020304u
);
725 test_assert(!parse_addr_port(LOG_WARN
, "4.3.2.1:99", &cp
, &u32
, &u16
));
726 test_streq(cp
, "4.3.2.1");
727 test_eq(u32
, 0x04030201u
);
730 test_assert(!parse_addr_port(LOG_WARN
, "nonexistent.address:4040",
732 test_streq(cp
, "nonexistent.address");
735 test_assert(!parse_addr_port(LOG_WARN
, "localhost:9999", &cp
, &u32
, &u16
));
736 test_streq(cp
, "localhost");
737 test_eq(u32
, 0x7f000001u
);
741 test_assert(!parse_addr_port(LOG_WARN
, "localhost", NULL
, &u32
, &u16
));
743 test_eq(u32
, 0x7f000001u
);
746 test_eq(0, addr_mask_get_bits(0x0u
));
747 test_eq(32, addr_mask_get_bits(0xFFFFFFFFu
));
748 test_eq(16, addr_mask_get_bits(0xFFFF0000u
));
749 test_eq(31, addr_mask_get_bits(0xFFFFFFFEu
));
750 test_eq(1, addr_mask_get_bits(0x80000000u
));
752 /* Test tor_parse_long. */
753 test_eq(10L, tor_parse_long("10",10,0,100,NULL
,NULL
));
754 test_eq(0L, tor_parse_long("10",10,50,100,NULL
,NULL
));
755 test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL
,NULL
));
757 /* Test tor_parse_ulong */
758 test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL
,NULL
));
759 test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL
,NULL
));
761 /* Test tor_parse_uint64. */
762 test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i
, &cp
));
764 test_streq(cp
, " x");
765 test_assert(U64_LITERAL(12345678901) ==
766 tor_parse_uint64("12345678901",10,0,UINT64_MAX
, &i
, &cp
));
769 test_assert(U64_LITERAL(0) ==
770 tor_parse_uint64("12345678901",10,500,INT32_MAX
, &i
, &cp
));
773 /* Test printf with uint64 */
774 tor_snprintf(buf
, sizeof(buf
), "x!"U64_FORMAT
"!x",
775 U64_PRINTF_ARG(U64_LITERAL(12345678901)));
776 test_streq(buf
, "x!12345678901!x");
778 /* Test parse_line_from_str */
779 strlcpy(buf
, "k v\n" " key value with spaces \n" "keykey val\n"
781 "k3 \n" "\n" " \n" "#comment\n"
782 "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf
));
785 cp
= parse_line_from_str(cp
, &k
, &v
);
788 test_assert(!strcmpstart(cp
, " key value with"));
790 cp
= parse_line_from_str(cp
, &k
, &v
);
791 test_streq(k
, "key");
792 test_streq(v
, "value with spaces");
793 test_assert(!strcmpstart(cp
, "keykey"));
795 cp
= parse_line_from_str(cp
, &k
, &v
);
796 test_streq(k
, "keykey");
797 test_streq(v
, "val");
798 test_assert(!strcmpstart(cp
, "k2\n"));
800 cp
= parse_line_from_str(cp
, &k
, &v
);
803 test_assert(!strcmpstart(cp
, "k3 \n"));
805 cp
= parse_line_from_str(cp
, &k
, &v
);
808 test_assert(!strcmpstart(cp
, "\n \n"));
810 cp
= parse_line_from_str(cp
, &k
, &v
);
813 test_assert(!strcmpstart(cp
, "k5#abc"));
815 cp
= parse_line_from_str(cp
, &k
, &v
);
818 test_assert(!strcmpstart(cp
, "k6"));
820 cp
= parse_line_from_str(cp
, &k
, &v
);
822 test_streq(v
, "val");
825 /* Test for strcmpstart and strcmpend. */
826 test_assert(strcmpstart("abcdef", "abcdef")==0);
827 test_assert(strcmpstart("abcdef", "abc")==0);
828 test_assert(strcmpstart("abcdef", "abd")<0);
829 test_assert(strcmpstart("abcdef", "abb")>0);
830 test_assert(strcmpstart("ab", "abb")<0);
832 test_assert(strcmpend("abcdef", "abcdef")==0);
833 test_assert(strcmpend("abcdef", "def")==0);
834 test_assert(strcmpend("abcdef", "deg")<0);
835 test_assert(strcmpend("abcdef", "dee")>0);
836 test_assert(strcmpend("ab", "abb")<0);
838 test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
839 test_assert(strcasecmpend("abcdef", "dEF")==0);
840 test_assert(strcasecmpend("abcDEf", "deg")<0);
841 test_assert(strcasecmpend("abcdef", "DEE")>0);
842 test_assert(strcasecmpend("ab", "abB")<0);
844 /* Test mem_is_zero */
847 test_assert(tor_digest_is_zero(buf
));
848 test_assert(tor_mem_is_zero(buf
, 10));
849 test_assert(tor_mem_is_zero(buf
, 20));
850 test_assert(tor_mem_is_zero(buf
, 128));
851 test_assert(!tor_mem_is_zero(buf
, 129));
853 test_assert(!tor_mem_is_zero(buf
, 128));
855 test_assert(!tor_mem_is_zero(buf
, 10));
859 char tmpbuf
[TOR_ADDR_BUF_LEN
];
860 const char *ip
= "176.192.208.224";
862 tor_inet_pton(AF_INET
, ip
, &in
);
863 tor_inet_ntop(AF_INET
, &in
, tmpbuf
, sizeof(tmpbuf
));
864 test_streq(tmpbuf
, ip
);
868 test_streq("\"\"", escaped(""));
869 test_streq("\"abcd\"", escaped("abcd"));
870 test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
871 test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
872 test_assert(NULL
== escaped(NULL
));
874 /* Test strndup and memdup */
876 const char *s
= "abcdefghijklmnopqrstuvwxyz";
877 cp
= tor_strndup(s
, 30);
878 test_streq(cp
, s
); /* same string, */
879 test_neq(cp
, s
); /* but different pointers. */
882 cp
= tor_strndup(s
, 5);
883 test_streq(cp
, "abcde");
886 s
= "a\0b\0c\0d\0e\0";
887 cp
= tor_memdup(s
,10);
888 test_memeq(cp
, s
, 10); /* same ram, */
889 test_neq(cp
, s
); /* but different pointers. */
893 /* Test str-foo functions */
894 cp
= tor_strdup("abcdef");
895 test_assert(tor_strisnonupper(cp
));
897 test_assert(!tor_strisnonupper(cp
));
899 test_streq(cp
, "ABCDEF");
900 test_assert(tor_strisprint(cp
));
902 test_assert(!tor_strisprint(cp
));
905 /* Test eat_whitespace. */
907 const char *s
= " \n a";
908 test_eq_ptr(eat_whitespace(s
), s
+4);
910 test_eq_ptr(eat_whitespace(s
), s
);
912 test_eq_ptr(eat_whitespace(s
), s
+5);
917 const char *haystack
= "abcde";
918 tor_assert(!tor_memmem(haystack
, 5, "ef", 2));
919 test_eq_ptr(tor_memmem(haystack
, 5, "cd", 2), haystack
+ 2);
920 test_eq_ptr(tor_memmem(haystack
, 5, "cde", 3), haystack
+ 2);
921 haystack
= "ababcad";
922 test_eq_ptr(tor_memmem(haystack
, 7, "abc", 3), haystack
+ 2);
925 /* Test wrap_string */
927 smartlist_t
*sl
= smartlist_create();
928 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
930 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
932 "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
934 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
937 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
939 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
941 "### This is a\n# test of string\n# wrapping\n# functionality:\n"
945 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
949 /* now make sure time works. */
950 tor_gettimeofday(&end
);
951 /* We might've timewarped a little. */
952 test_assert(tv_udiff(&start
, &end
) >= -5000);
954 /* Test tor_log2(). */
955 test_eq(tor_log2(64), 6);
956 test_eq(tor_log2(65), 6);
957 test_eq(tor_log2(63), 5);
958 test_eq(tor_log2(1), 0);
959 test_eq(tor_log2(2), 1);
960 test_eq(tor_log2(3), 1);
961 test_eq(tor_log2(4), 2);
962 test_eq(tor_log2(5), 2);
963 test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
964 test_eq(tor_log2(UINT64_MAX
), 63);
966 /* Test round_to_power_of_2 */
967 test_eq(round_to_power_of_2(120), 128);
968 test_eq(round_to_power_of_2(128), 128);
969 test_eq(round_to_power_of_2(130), 128);
970 test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
972 test_eq(round_to_power_of_2(0), 2);
976 _test_eq_ip6(struct in6_addr
*a
, struct in6_addr
*b
, const char *e1
,
977 const char *e2
, int line
)
981 for (i
= 0; i
< 16; ++i
) {
982 if (a
->s6_addr
[i
] != b
->s6_addr
[i
]) {
988 printf("."); fflush(stdout
);
990 char buf1
[128], *cp1
;
991 char buf2
[128], *cp2
;
993 cp1
= buf1
; cp2
= buf2
;
994 for (i
=0; i
<16; ++i
) {
995 tor_snprintf(cp1
, sizeof(buf1
)-(cp1
-buf1
), "%02x", a
->s6_addr
[i
]);
996 tor_snprintf(cp2
, sizeof(buf2
)-(cp2
-buf2
), "%02x", b
->s6_addr
[i
]);
998 if ((i
%2)==1 && i
!= 15) {
1004 printf("Line %d: assertion failed: (%s == %s)\n"
1005 " %s != %s\n", line
, e1
, e2
, buf1
, buf2
);
1009 #define test_eq_ip6(a,b) _test_eq_ip6((a),(b),#a,#b,__LINE__)
1011 #define test_pton6_same(a,b) STMT_BEGIN \
1012 r = tor_inet_pton(AF_INET6, a, &a1); \
1013 test_assert(r==1); \
1014 r = tor_inet_pton(AF_INET6, b, &a2); \
1015 test_assert(r==1); \
1016 test_eq_ip6(&a1,&a2); \
1019 #define test_pton6_bad(a) \
1020 test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
1022 #define test_ntop6_reduces(a,b) STMT_BEGIN \
1023 r = tor_inet_pton(AF_INET6, a, &a1); \
1024 test_assert(r==1); \
1025 test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
1026 r = tor_inet_pton(AF_INET6, b, &a2); \
1027 test_assert(r==1); \
1028 test_eq_ip6(&a1, &a2); \
1031 /*XXXX020 make this macro give useful output on failure, and follow the
1032 * conventions of the other test macros. */
1033 #define test_internal_ip(a,b) STMT_BEGIN \
1034 r = tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \
1035 test_assert(r==1); \
1036 t1.sa6.sin6_family = AF_INET6; \
1037 r = tor_addr_is_internal(&t1, b); \
1038 test_assert(r==1); \
1041 /*XXXX020 make this macro give useful output on failure, and follow the
1042 * conventions of the other test macros. */
1043 #define test_external_ip(a,b) STMT_BEGIN \
1044 r = tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \
1045 test_assert(r==1); \
1046 t1.sa6.sin6_family = AF_INET6; \
1047 r = tor_addr_is_internal(&t1, b); \
1048 test_assert(r==0); \
1051 /*XXXX020 make this macro give useful output on failure, and follow the
1052 * conventions of the other test macros. */
1053 #define test_addr_convert6(a,b) STMT_BEGIN \
1054 tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \
1055 tor_inet_pton(AF_INET6, b, &t2.sa6.sin6_addr); \
1056 t1.sa6.sin6_family = AF_INET6; \
1057 t2.sa6.sin6_family = AF_INET6; \
1060 /*XXXX020 make this macro give useful output on failure, and follow the
1061 * conventions of the other test macros. */
1062 #define test_addr_parse(xx) STMT_BEGIN \
1063 r=tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2); \
1064 t2.sa6.sin6_family = AF_INET6; \
1065 p1=tor_inet_ntop(AF_INET6, &t1.sa6.sin6_addr, bug, sizeof(bug)); \
1068 /*XXXX020 make this macro give useful output on failure, and follow the
1069 * conventions of the other test macros. */
1070 #define test_addr_parse_check(ip1, ip2, ip3, ip4, mm, pt1, pt2) STMT_BEGIN \
1071 test_assert(r>=0); \
1072 test_eq(htonl(ip1), IN6_ADDRESS32(&t1)[0]); \
1073 test_eq(htonl(ip2), IN6_ADDRESS32(&t1)[1]); \
1074 test_eq(htonl(ip3), IN6_ADDRESS32(&t1)[2]); \
1075 test_eq(htonl(ip4), IN6_ADDRESS32(&t1)[3]); \
1076 test_eq(mask, mm); \
1077 test_eq(port1, pt1); \
1078 test_eq(port2, pt2); \
1082 test_util_ip6_helpers(void)
1084 char buf
[TOR_ADDR_BUF_LEN
], bug
[TOR_ADDR_BUF_LEN
];
1085 struct in6_addr a1
, a2
;
1088 uint16_t port1
, port2
;
1092 // struct in_addr b1, b2;
1093 /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
1095 /* === Test pton: valid af_inet6 */
1096 /* Simple, valid parsing. */
1097 r
= tor_inet_pton(AF_INET6
,
1098 "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1
);
1100 for (i
=0;i
<16;++i
) { test_eq(i
+1, (int)a1
.s6_addr
[i
]); }
1102 test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
1103 "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
1104 /* shortened words. */
1105 test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
1106 "1:99:BEEF:0:0123:FFFF:1:1");
1107 /* zeros at the beginning */
1108 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
1110 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
1111 "::9:c0a8:0.1.0.1");
1112 /* zeros in the middle. */
1113 test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
1114 "fe80::202:1111:1:1");
1115 /* zeros at the end. */
1116 test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
1119 /* === Test ntop: af_inet6 */
1120 test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
1122 test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
1123 "1:99:beef:6:123:ffff:1:1");
1125 //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
1126 test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
1127 test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
1128 test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
1129 test_ntop6_reduces("008:0::0", "8::");
1130 test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
1131 test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
1132 test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
1134 test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
1135 "fe80::202:1111:1:1");
1136 test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
1139 /* === Test pton: invalid in6. */
1140 test_pton6_bad("foobar.");
1141 test_pton6_bad("55555::");
1142 test_pton6_bad("9:-60::");
1143 test_pton6_bad("1:2:33333:4:0002:3::");
1144 //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
1145 test_pton6_bad("1:2:3333:4:fish:3::");
1146 test_pton6_bad("1:2:3:4:5:6:7:8:9");
1147 test_pton6_bad("1:2:3:4:5:6:7");
1148 test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
1149 test_pton6_bad("1:2:3:4:5:6:1.2.3");
1150 test_pton6_bad("::1.2.3");
1151 test_pton6_bad("::1.2.3.4.5");
1152 test_pton6_bad("99");
1154 test_pton6_bad("1::2::3:4");
1155 test_pton6_bad("a:::b:c");
1156 test_pton6_bad(":::a:b:c");
1157 test_pton6_bad("a:b:c:::");
1159 /* test internal checking */
1160 test_external_ip("fbff:ffff::2:7", 0);
1161 test_internal_ip("fc01::2:7", 0);
1162 test_internal_ip("fdff:ffff::f:f", 0);
1163 test_external_ip("fe00::3:f", 0);
1165 test_external_ip("fe7f:ffff::2:7", 0);
1166 test_internal_ip("fe80::2:7", 0);
1167 test_internal_ip("febf:ffff::f:f", 0);
1169 test_internal_ip("fec0::2:7:7", 0);
1170 test_internal_ip("feff:ffff::e:7:7", 0);
1171 test_external_ip("ff00::e:7:7", 0);
1173 test_internal_ip("::", 0);
1174 test_internal_ip("::1", 0);
1175 test_internal_ip("::1", 1);
1176 test_internal_ip("::", 0);
1177 test_external_ip("::", 1);
1178 test_external_ip("::2", 0);
1179 test_external_ip("2001::", 0);
1180 test_external_ip("ffff::", 0);
1182 test_external_ip("::ffff:0.0.0.0", 1);
1183 test_internal_ip("::ffff:0.0.0.0", 0);
1184 test_internal_ip("::ffff:0.255.255.255", 0);
1185 test_external_ip("::ffff:1.0.0.0", 0);
1187 test_external_ip("::ffff:9.255.255.255", 0);
1188 test_internal_ip("::ffff:10.0.0.0", 0);
1189 test_internal_ip("::ffff:10.255.255.255", 0);
1190 test_external_ip("::ffff:11.0.0.0", 0);
1192 test_external_ip("::ffff:126.255.255.255", 0);
1193 test_internal_ip("::ffff:127.0.0.0", 0);
1194 test_internal_ip("::ffff:127.255.255.255", 0);
1195 test_external_ip("::ffff:128.0.0.0", 0);
1197 test_external_ip("::ffff:172.15.255.255", 0);
1198 test_internal_ip("::ffff:172.16.0.0", 0);
1199 test_internal_ip("::ffff:172.31.255.255", 0);
1200 test_external_ip("::ffff:172.32.0.0", 0);
1202 test_external_ip("::ffff:192.167.255.255", 0);
1203 test_internal_ip("::ffff:192.168.0.0", 0);
1204 test_internal_ip("::ffff:192.168.255.255", 0);
1205 test_external_ip("::ffff:192.169.0.0", 0);
1207 test_external_ip("::ffff:169.253.255.255", 0);
1208 test_internal_ip("::ffff:169.254.0.0", 0);
1209 test_internal_ip("::ffff:169.254.255.255", 0);
1210 test_external_ip("::ffff:169.255.0.0", 0);
1212 /* tor_addr_compare(tor_addr_t x2) */
1213 test_addr_convert6("ffff::", "ffff::0");
1214 test_assert(tor_addr_compare(&t1
, &t2
) == 0);
1215 test_addr_convert6("0::3:2:1", "0::ffff:0.3.2.1");
1216 test_assert(tor_addr_compare(&t1
, &t2
) > 0);
1217 test_addr_convert6("0::2:2:1", "0::ffff:0.3.2.1");
1218 test_assert(tor_addr_compare(&t1
, &t2
) > 0);
1219 test_addr_convert6("0::ffff:0.3.2.1", "0::0:0:0");
1220 test_assert(tor_addr_compare(&t1
, &t2
) < 0);
1221 test_addr_convert6("0::ffff:5.2.2.1", "::ffff:6.0.0.0");
1222 test_assert(tor_addr_compare(&t1
, &t2
) < 0); /* XXXX wrong. */
1223 tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1
, NULL
, NULL
, NULL
);
1224 tor_addr_parse_mask_ports("2.3.4.5", &t2
, NULL
, NULL
, NULL
);
1225 test_assert(tor_addr_compare(&t1
, &t2
) == 0);
1226 tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1
, NULL
, NULL
, NULL
);
1227 tor_addr_parse_mask_ports("2.3.4.5", &t2
, NULL
, NULL
, NULL
);
1228 test_assert(tor_addr_compare(&t1
, &t2
) < 0);
1230 /* XXXX020 test compare_masked */
1232 /* test tor_addr_parse_mask_ports */
1233 test_addr_parse("[::f]/17:47-95");
1234 test_addr_parse_check(0, 0, 0, 0x0000000f, 17, 47, 95);
1235 //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
1236 //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
1237 test_addr_parse("[::ffff:4.1.1.7]/120:443");
1238 test_addr_parse_check(0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
1239 test_addr_parse("[abcd:2::44a:0]:2-65000");
1240 test_addr_parse_check(0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
1242 r
=tor_addr_parse_mask_ports("[fefef::]/112", &t1
, NULL
, NULL
, NULL
);
1243 test_assert(r
== -1);
1244 r
=tor_addr_parse_mask_ports("efef::/112", &t1
, NULL
, NULL
, NULL
);
1245 test_assert(r
== -1);
1246 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1
, NULL
, NULL
, NULL
);
1247 test_assert(r
== -1);
1248 r
=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1
, NULL
, NULL
, NULL
);
1249 test_assert(r
== -1);
1250 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1
, NULL
, NULL
, NULL
);
1251 test_assert(r
== -1);
1252 /* Test for V4-mapped address with mask < 96. (arguably not valid) */
1253 r
=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1
, &mask
, NULL
, NULL
);
1254 test_assert(r
== -1);
1255 r
=tor_addr_parse_mask_ports("1.1.2.2/33", &t1
, &mask
, NULL
, NULL
);
1256 test_assert(r
== -1);
1257 r
=tor_addr_parse_mask_ports("1.1.2.2/31", &t1
, &mask
, NULL
, NULL
);
1258 test_assert(r
== AF_INET
);
1259 r
=tor_addr_parse_mask_ports("[efef::]/112", &t1
, &mask
, &port1
, &port2
);
1260 test_assert(r
== AF_INET6
);
1261 test_assert(port1
== 1);
1262 test_assert(port2
== 65535);
1264 /* make sure inet address lengths >= max */
1265 test_assert(INET_NTOA_BUF_LEN
>= sizeof("255.255.255.255"));
1266 test_assert(TOR_ADDR_BUF_LEN
>=
1267 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
1269 test_assert(sizeof(tor_addr_t
) >= sizeof(struct sockaddr_in6
));
1271 /* get interface addresses */
1272 r
= get_interface_address6(0, AF_INET
, &t1
);
1273 i
= get_interface_address6(0, AF_INET6
, &t2
);
1275 tor_inet_ntop(AF_INET
, &t1
.sa
.sin_addr
, buf
, sizeof(buf
));
1276 printf("\nv4 address: %s (family=%i)", buf
, IN_FAMILY(&t1
));
1277 tor_inet_ntop(AF_INET6
, &t2
.sa6
.sin6_addr
, buf
, sizeof(buf
));
1278 printf("\nv6 address: %s (family=%i)", buf
, IN_FAMILY(&t2
));
1283 test_util_smartlist(void)
1288 /* XXXX test sort_digests, uniq_strings, uniq_digests */
1290 /* Test smartlist add, del_keeporder, insert, get. */
1291 sl
= smartlist_create();
1292 smartlist_add(sl
, (void*)1);
1293 smartlist_add(sl
, (void*)2);
1294 smartlist_add(sl
, (void*)3);
1295 smartlist_add(sl
, (void*)4);
1296 smartlist_del_keeporder(sl
, 1);
1297 smartlist_insert(sl
, 1, (void*)22);
1298 smartlist_insert(sl
, 0, (void*)0);
1299 smartlist_insert(sl
, 5, (void*)555);
1300 test_eq_ptr((void*)0, smartlist_get(sl
,0));
1301 test_eq_ptr((void*)1, smartlist_get(sl
,1));
1302 test_eq_ptr((void*)22, smartlist_get(sl
,2));
1303 test_eq_ptr((void*)3, smartlist_get(sl
,3));
1304 test_eq_ptr((void*)4, smartlist_get(sl
,4));
1305 test_eq_ptr((void*)555, smartlist_get(sl
,5));
1306 /* Try deleting in the middle. */
1307 smartlist_del(sl
, 1);
1308 test_eq_ptr((void*)555, smartlist_get(sl
, 1));
1309 /* Try deleting at the end. */
1310 smartlist_del(sl
, 4);
1311 test_eq(4, smartlist_len(sl
));
1314 test_assert(smartlist_isin(sl
, (void*)3));
1315 test_assert(!smartlist_isin(sl
, (void*)99));
1317 /* Test split and join */
1318 smartlist_clear(sl
);
1319 test_eq(0, smartlist_len(sl
));
1320 smartlist_split_string(sl
, "abc", ":", 0, 0);
1321 test_eq(1, smartlist_len(sl
));
1322 test_streq("abc", smartlist_get(sl
, 0));
1323 smartlist_split_string(sl
, "a::bc::", "::", 0, 0);
1324 test_eq(4, smartlist_len(sl
));
1325 test_streq("a", smartlist_get(sl
, 1));
1326 test_streq("bc", smartlist_get(sl
, 2));
1327 test_streq("", smartlist_get(sl
, 3));
1328 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
1329 test_streq(cp
, "abcabc");
1331 cp
= smartlist_join_strings(sl
, "!", 0, NULL
);
1332 test_streq(cp
, "abc!a!bc!");
1334 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
1335 test_streq(cp
, "abcXYaXYbcXY");
1337 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
1338 test_streq(cp
, "abcXYaXYbcXYXY");
1340 cp
= smartlist_join_strings(sl
, "", 1, NULL
);
1341 test_streq(cp
, "abcabc");
1344 smartlist_split_string(sl
, "/def/ /ghijk", "/", 0, 0);
1345 test_eq(8, smartlist_len(sl
));
1346 test_streq("", smartlist_get(sl
, 4));
1347 test_streq("def", smartlist_get(sl
, 5));
1348 test_streq(" ", smartlist_get(sl
, 6));
1349 test_streq("ghijk", smartlist_get(sl
, 7));
1350 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1351 smartlist_clear(sl
);
1353 smartlist_split_string(sl
, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE
, 0);
1354 test_eq(3, smartlist_len(sl
));
1355 test_streq("a", smartlist_get(sl
,0));
1356 test_streq("bbd", smartlist_get(sl
,1));
1357 test_streq("cdef", smartlist_get(sl
,2));
1358 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1359 SPLIT_SKIP_SPACE
, 0);
1360 test_eq(8, smartlist_len(sl
));
1361 test_streq("z", smartlist_get(sl
,3));
1362 test_streq("zhasd", smartlist_get(sl
,4));
1363 test_streq("", smartlist_get(sl
,5));
1364 test_streq("bnud", smartlist_get(sl
,6));
1365 test_streq("", smartlist_get(sl
,7));
1367 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1368 smartlist_clear(sl
);
1370 smartlist_split_string(sl
, " ab\tc \td ef ", NULL
,
1371 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1372 test_eq(4, smartlist_len(sl
));
1373 test_streq("ab", smartlist_get(sl
,0));
1374 test_streq("c", smartlist_get(sl
,1));
1375 test_streq("d", smartlist_get(sl
,2));
1376 test_streq("ef", smartlist_get(sl
,3));
1377 smartlist_split_string(sl
, "ghi\tj", NULL
,
1378 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1379 test_eq(6, smartlist_len(sl
));
1380 test_streq("ghi", smartlist_get(sl
,4));
1381 test_streq("j", smartlist_get(sl
,5));
1383 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1384 smartlist_clear(sl
);
1386 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
1389 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
1390 test_streq(cp
, "XY");
1393 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1394 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1395 test_eq(3, smartlist_len(sl
));
1396 test_streq("z", smartlist_get(sl
, 0));
1397 test_streq("zhasd", smartlist_get(sl
, 1));
1398 test_streq("bnud", smartlist_get(sl
, 2));
1399 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1400 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 2);
1401 test_eq(5, smartlist_len(sl
));
1402 test_streq("z", smartlist_get(sl
, 3));
1403 test_streq("zhasd <> <> bnud<>", smartlist_get(sl
, 4));
1404 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1405 smartlist_clear(sl
);
1407 smartlist_split_string(sl
, "abcd\n", "\n",
1408 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1409 test_eq(1, smartlist_len(sl
));
1410 test_streq("abcd", smartlist_get(sl
, 0));
1411 smartlist_split_string(sl
, "efgh", "\n",
1412 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1413 test_eq(2, smartlist_len(sl
));
1414 test_streq("efgh", smartlist_get(sl
, 1));
1416 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1417 smartlist_clear(sl
);
1419 /* Test swapping, shuffling, and sorting. */
1420 smartlist_split_string(sl
, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
1421 test_eq(7, smartlist_len(sl
));
1422 smartlist_sort(sl
, _compare_strs
);
1423 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1424 test_streq(cp
,"and,arma,by,nickm,onion,router,the");
1426 smartlist_swap(sl
, 1, 5);
1427 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1428 test_streq(cp
,"and,router,by,nickm,onion,arma,the");
1430 smartlist_shuffle(sl
);
1431 test_eq(7, smartlist_len(sl
));
1432 test_assert(smartlist_string_isin(sl
, "and"));
1433 test_assert(smartlist_string_isin(sl
, "router"));
1434 test_assert(smartlist_string_isin(sl
, "by"));
1435 test_assert(smartlist_string_isin(sl
, "nickm"));
1436 test_assert(smartlist_string_isin(sl
, "onion"));
1437 test_assert(smartlist_string_isin(sl
, "arma"));
1438 test_assert(smartlist_string_isin(sl
, "the"));
1441 smartlist_sort(sl
, _compare_strs
);
1442 test_streq("nickm", smartlist_bsearch(sl
, "zNicKM",
1443 _compare_without_first_ch
));
1444 test_streq("and", smartlist_bsearch(sl
, " AND", _compare_without_first_ch
));
1445 test_eq_ptr(NULL
, smartlist_bsearch(sl
, " ANz", _compare_without_first_ch
));
1447 /* Test reverse() and pop_last() */
1448 smartlist_reverse(sl
);
1449 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1450 test_streq(cp
,"the,router,onion,nickm,by,arma,and");
1452 cp
= smartlist_pop_last(sl
);
1453 test_streq(cp
, "and");
1455 test_eq(smartlist_len(sl
), 6);
1456 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1457 smartlist_clear(sl
);
1460 smartlist_split_string(sl
,
1461 "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
1463 smartlist_sort(sl
, _compare_strs
);
1464 smartlist_uniq(sl
, _compare_strs
, NULL
);
1465 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1466 test_streq(cp
, "50,a,canal,man,noon,panama,plan,radar");
1469 /* Test string_isin and isin_case and num_isin */
1470 test_assert(smartlist_string_isin(sl
, "noon"));
1471 test_assert(!smartlist_string_isin(sl
, "noonoon"));
1472 test_assert(smartlist_string_isin_case(sl
, "nOOn"));
1473 test_assert(!smartlist_string_isin_case(sl
, "nooNooN"));
1474 test_assert(smartlist_string_num_isin(sl
, 50));
1475 test_assert(!smartlist_string_num_isin(sl
, 60));
1476 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1477 smartlist_clear(sl
);
1479 /* Test string_remove and remove and join_strings2 */
1480 smartlist_split_string(sl
,
1481 "Some say the Earth will end in ice and some in fire",
1483 cp
= smartlist_get(sl
, 4);
1484 test_streq(cp
, "will");
1485 smartlist_add(sl
, cp
);
1486 smartlist_remove(sl
, cp
);
1487 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1488 test_streq(cp
, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
1490 smartlist_string_remove(sl
, "in");
1491 cp
= smartlist_join_strings2(sl
, "+XX", 1, 0, NULL
);
1492 test_streq(cp
, "Some+say+the+Earth+fire+end+some+ice+and");
1495 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1496 smartlist_clear(sl
);
1499 smartlist_t
*ints
= smartlist_create();
1500 smartlist_t
*odds
= smartlist_create();
1501 smartlist_t
*evens
= smartlist_create();
1502 smartlist_t
*primes
= smartlist_create();
1504 for (i
=1; i
< 10; i
+= 2)
1505 smartlist_add(odds
, (void*)(uintptr_t)i
);
1506 for (i
=0; i
< 10; i
+= 2)
1507 smartlist_add(evens
, (void*)(uintptr_t)i
);
1510 smartlist_add_all(ints
, odds
);
1511 smartlist_add_all(ints
, evens
);
1512 test_eq(smartlist_len(ints
), 10);
1514 smartlist_add(primes
, (void*)2);
1515 smartlist_add(primes
, (void*)3);
1516 smartlist_add(primes
, (void*)5);
1517 smartlist_add(primes
, (void*)7);
1520 test_assert(smartlist_overlap(ints
, odds
));
1521 test_assert(smartlist_overlap(odds
, primes
));
1522 test_assert(smartlist_overlap(evens
, primes
));
1523 test_assert(!smartlist_overlap(odds
, evens
));
1526 smartlist_add_all(sl
, odds
);
1527 smartlist_intersect(sl
, primes
);
1528 test_eq(smartlist_len(sl
), 3);
1529 test_assert(smartlist_isin(sl
, (void*)3));
1530 test_assert(smartlist_isin(sl
, (void*)5));
1531 test_assert(smartlist_isin(sl
, (void*)7));
1534 smartlist_add_all(sl
, primes
);
1535 smartlist_subtract(sl
, odds
);
1536 test_eq(smartlist_len(sl
), 1);
1537 test_assert(smartlist_isin(sl
, (void*)2));
1539 smartlist_free(odds
);
1540 smartlist_free(evens
);
1541 smartlist_free(ints
);
1542 smartlist_free(primes
);
1543 smartlist_clear(sl
);
1550 test_util_bitarray(void)
1555 ba
= bitarray_init_zero(1);
1556 test_assert(! bitarray_is_set(ba
, 0));
1557 bitarray_set(ba
, 0);
1558 test_assert(bitarray_is_set(ba
, 0));
1559 bitarray_clear(ba
, 0);
1560 test_assert(! bitarray_is_set(ba
, 0));
1563 ba
= bitarray_init_zero(1023);
1564 for (i
= 1; i
< 64; ) {
1565 for (j
= 0; j
< 1023; ++j
) {
1567 bitarray_set(ba
, j
);
1569 bitarray_clear(ba
, j
);
1571 for (j
= 0; j
< 1023; ++j
) {
1572 if (!bool_eq(bitarray_is_set(ba
, j
), j
%i
))
1586 /* stop threads running at once. */
1587 static tor_mutex_t
*_thread_test_mutex
= NULL
;
1588 /* make sure that threads have to run at the same time. */
1589 static tor_mutex_t
*_thread_test_start1
= NULL
;
1590 static tor_mutex_t
*_thread_test_start2
= NULL
;
1591 static strmap_t
*_thread_test_strmap
= NULL
;
1593 static void _thread_test_func(void* _s
) ATTR_NORETURN
;
1595 static int t1_count
= 0;
1596 static int t2_count
= 0;
1599 _thread_test_func(void* _s
)
1606 if (!strcmp(s
, "thread 1")) {
1607 m
= _thread_test_start1
;
1610 m
= _thread_test_start2
;
1613 tor_mutex_acquire(m
);
1615 tor_snprintf(buf
, sizeof(buf
), "%lu", tor_get_thread_id());
1616 cp
= tor_strdup(buf
);
1618 for (i
=0; i
<10000; ++i
) {
1619 tor_mutex_acquire(_thread_test_mutex
);
1620 strmap_set(_thread_test_strmap
, "last to run", cp
);
1622 tor_mutex_release(_thread_test_mutex
);
1624 tor_mutex_acquire(_thread_test_mutex
);
1625 strmap_set(_thread_test_strmap
, s
, tor_strdup(buf
));
1626 tor_mutex_release(_thread_test_mutex
);
1628 tor_mutex_release(m
);
1634 test_util_threads(void)
1637 int done
= 0, timedout
= 0;
1639 #ifndef TOR_IS_MULTITHREADED
1640 /* Skip this test if we aren't threading. We should be threading most
1641 * everywhere by now. */
1645 _thread_test_mutex
= tor_mutex_new();
1646 _thread_test_start1
= tor_mutex_new();
1647 _thread_test_start2
= tor_mutex_new();
1648 _thread_test_strmap
= strmap_new();
1649 s1
= tor_strdup("thread 1");
1650 s2
= tor_strdup("thread 2");
1651 tor_mutex_acquire(_thread_test_start1
);
1652 tor_mutex_acquire(_thread_test_start2
);
1653 spawn_func(_thread_test_func
, s1
);
1654 spawn_func(_thread_test_func
, s2
);
1655 tor_mutex_release(_thread_test_start2
);
1656 tor_mutex_release(_thread_test_start1
);
1657 started
= time(NULL
);
1659 tor_mutex_acquire(_thread_test_mutex
);
1660 strmap_assert_ok(_thread_test_strmap
);
1661 if (strmap_get(_thread_test_strmap
, "thread 1") &&
1662 strmap_get(_thread_test_strmap
, "thread 2")) {
1664 } else if (time(NULL
) > started
+ 25) {
1665 timedout
= done
= 1;
1667 tor_mutex_release(_thread_test_mutex
);
1669 tor_mutex_free(_thread_test_mutex
);
1672 printf("\nTimed out: %d %d", t1_count
, t2_count
);
1673 test_assert(strmap_get(_thread_test_strmap
, "thread 1"));
1674 test_assert(strmap_get(_thread_test_strmap
, "thread 2"));
1675 test_assert(!timedout
);
1678 /* different thread IDs. */
1679 test_assert(strcmp(strmap_get(_thread_test_strmap
, "thread 1"),
1680 strmap_get(_thread_test_strmap
, "thread 2")));
1681 test_assert(!strcmp(strmap_get(_thread_test_strmap
, "thread 1"),
1682 strmap_get(_thread_test_strmap
, "last to run")) ||
1683 !strcmp(strmap_get(_thread_test_strmap
, "thread 2"),
1684 strmap_get(_thread_test_strmap
, "last to run")));
1686 strmap_free(_thread_test_strmap
, _tor_free
);
1693 _compare_strings_for_pqueue(const void *s1
, const void *s2
)
1695 return strcmp((const char*)s1
, (const char*)s2
);
1699 test_util_pqueue(void)
1702 int (*cmp
)(const void *, const void*);
1703 #define OK() smartlist_pqueue_assert_ok(sl, cmp)
1705 cmp
= _compare_strings_for_pqueue
;
1707 sl
= smartlist_create();
1708 smartlist_pqueue_add(sl
, cmp
, (char*)"cows");
1709 smartlist_pqueue_add(sl
, cmp
, (char*)"zebras");
1710 smartlist_pqueue_add(sl
, cmp
, (char*)"fish");
1711 smartlist_pqueue_add(sl
, cmp
, (char*)"frogs");
1712 smartlist_pqueue_add(sl
, cmp
, (char*)"apples");
1713 smartlist_pqueue_add(sl
, cmp
, (char*)"squid");
1714 smartlist_pqueue_add(sl
, cmp
, (char*)"daschunds");
1715 smartlist_pqueue_add(sl
, cmp
, (char*)"eggplants");
1716 smartlist_pqueue_add(sl
, cmp
, (char*)"weissbier");
1717 smartlist_pqueue_add(sl
, cmp
, (char*)"lobsters");
1718 smartlist_pqueue_add(sl
, cmp
, (char*)"roquefort");
1722 test_eq(smartlist_len(sl
), 11);
1723 test_streq(smartlist_get(sl
, 0), "apples");
1724 test_streq(smartlist_pqueue_pop(sl
, cmp
), "apples");
1725 test_eq(smartlist_len(sl
), 10);
1727 test_streq(smartlist_pqueue_pop(sl
, cmp
), "cows");
1728 test_streq(smartlist_pqueue_pop(sl
, cmp
), "daschunds");
1729 smartlist_pqueue_add(sl
, cmp
, (char*)"chinchillas");
1731 smartlist_pqueue_add(sl
, cmp
, (char*)"fireflies");
1733 test_streq(smartlist_pqueue_pop(sl
, cmp
), "chinchillas");
1734 test_streq(smartlist_pqueue_pop(sl
, cmp
), "eggplants");
1735 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fireflies");
1737 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fish");
1738 test_streq(smartlist_pqueue_pop(sl
, cmp
), "frogs");
1739 test_streq(smartlist_pqueue_pop(sl
, cmp
), "lobsters");
1740 test_streq(smartlist_pqueue_pop(sl
, cmp
), "roquefort");
1742 test_eq(smartlist_len(sl
), 3);
1743 test_streq(smartlist_pqueue_pop(sl
, cmp
), "squid");
1744 test_streq(smartlist_pqueue_pop(sl
, cmp
), "weissbier");
1745 test_streq(smartlist_pqueue_pop(sl
, cmp
), "zebras");
1746 test_eq(smartlist_len(sl
), 0);
1753 test_util_gzip(void)
1755 char *buf1
, *buf2
=NULL
, *buf3
=NULL
, *cp1
, *cp2
;
1758 tor_zlib_state_t
*state
;
1760 buf1
= tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
1761 test_assert(detect_compression_method(buf1
, strlen(buf1
)) == UNKNOWN_METHOD
);
1762 if (is_gzip_supported()) {
1763 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1766 test_assert(!memcmp(buf2
, "\037\213", 2)); /* Gzip magic. */
1767 test_assert(detect_compression_method(buf2
, len1
) == GZIP_METHOD
);
1769 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1770 GZIP_METHOD
, 1, LOG_INFO
));
1772 test_streq(buf1
,buf3
);
1778 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1781 test_assert(!memcmp(buf2
, "\x78\xDA", 2)); /* deflate magic. */
1782 test_assert(detect_compression_method(buf2
, len1
) == ZLIB_METHOD
);
1784 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1785 ZLIB_METHOD
, 1, LOG_INFO
));
1787 test_streq(buf1
,buf3
);
1789 /* Check whether we can uncompress concatenated, compresed strings. */
1791 buf2
= tor_realloc(buf2
, len1
*2);
1792 memcpy(buf2
+len1
, buf2
, len1
);
1793 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
*2,
1794 ZLIB_METHOD
, 1, LOG_INFO
));
1795 test_eq(len2
, (strlen(buf1
)+1)*2);
1797 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
1798 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
1799 (strlen(buf1
)+1)*2);
1805 /* Check whether we can uncompress partial strings. */
1807 tor_strdup("String with low redundancy that won't be compressed much.");
1808 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1810 tor_assert(len1
>16);
1811 /* when we allow an uncomplete string, we should succeed.*/
1812 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1813 ZLIB_METHOD
, 0, LOG_INFO
));
1815 tor_assert(len2
> 5);
1816 tor_assert(!strcmpstart(buf1
, buf3
));
1818 /* when we demand a complete string, this must fail. */
1820 tor_assert(tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1821 ZLIB_METHOD
, 1, LOG_INFO
));
1824 /* Now, try streaming compression. */
1828 state
= tor_zlib_new(1, ZLIB_METHOD
);
1830 cp1
= buf1
= tor_malloc(1024);
1832 ccp2
= "ABCDEFGHIJABCDEFGHIJ";
1834 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 0)
1836 test_eq(len2
, 0); /* Make sure we compressed it all. */
1837 test_assert(cp1
> buf1
);
1841 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 1)
1844 test_assert(cp1
> cp2
); /* Make sure we really added something. */
1846 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf1
, 1024-len1
,
1847 ZLIB_METHOD
, 1, LOG_WARN
));
1848 test_streq(buf3
, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
1851 tor_zlib_free(state
);
1859 test_util_strmap(void)
1862 strmap_iter_t
*iter
;
1866 smartlist_t
*found_keys
;
1869 v
= strmap_set(map
, "K1", (void*)99);
1871 v
= strmap_set(map
, "K2", (void*)101);
1873 v
= strmap_set(map
, "K1", (void*)100);
1874 test_eq(v
, (void*)99);
1875 test_eq_ptr(strmap_get(map
,"K1"), (void*)100);
1876 test_eq_ptr(strmap_get(map
,"K2"), (void*)101);
1877 test_eq_ptr(strmap_get(map
,"K-not-there"), NULL
);
1878 strmap_assert_ok(map
);
1880 v
= strmap_remove(map
,"K2");
1881 strmap_assert_ok(map
);
1882 test_eq_ptr(v
, (void*)101);
1883 test_eq_ptr(strmap_get(map
,"K2"), NULL
);
1884 test_eq_ptr(strmap_remove(map
,"K2"), NULL
);
1886 strmap_set(map
, "K2", (void*)101);
1887 strmap_set(map
, "K3", (void*)102);
1888 strmap_set(map
, "K4", (void*)103);
1889 strmap_assert_ok(map
);
1890 strmap_set(map
, "K5", (void*)104);
1891 strmap_set(map
, "K6", (void*)105);
1892 strmap_assert_ok(map
);
1894 /* Test iterator. */
1895 iter
= strmap_iter_init(map
);
1896 found_keys
= smartlist_create();
1897 while (!strmap_iter_done(iter
)) {
1898 strmap_iter_get(iter
,&k
,&v
);
1899 smartlist_add(found_keys
, tor_strdup(k
));
1900 test_eq_ptr(v
, strmap_get(map
, k
));
1902 if (!strcmp(k
, "K2")) {
1903 iter
= strmap_iter_next_rmv(map
,iter
);
1905 iter
= strmap_iter_next(map
,iter
);
1909 /* Make sure we removed K2, but not the others. */
1910 test_eq_ptr(strmap_get(map
, "K2"), NULL
);
1911 test_eq_ptr(strmap_get(map
, "K5"), (void*)104);
1912 /* Make sure we visited everyone once */
1913 smartlist_sort_strings(found_keys
);
1914 visited
= smartlist_join_strings(found_keys
, ":", 0, NULL
);
1915 test_streq(visited
, "K1:K2:K3:K4:K5:K6");
1917 SMARTLIST_FOREACH(found_keys
, char *, cp
, tor_free(cp
));
1918 smartlist_free(found_keys
);
1920 strmap_assert_ok(map
);
1921 /* Clean up after ourselves. */
1922 strmap_free(map
, NULL
);
1924 /* Now try some lc functions. */
1926 strmap_set_lc(map
,"Ab.C", (void*)1);
1927 test_eq_ptr(strmap_get(map
,"ab.c"), (void*)1);
1928 strmap_assert_ok(map
);
1929 test_eq_ptr(strmap_get_lc(map
,"AB.C"), (void*)1);
1930 test_eq_ptr(strmap_get(map
,"AB.C"), NULL
);
1931 test_eq_ptr(strmap_remove_lc(map
,"aB.C"), (void*)1);
1932 strmap_assert_ok(map
);
1933 test_eq_ptr(strmap_get_lc(map
,"AB.C"), NULL
);
1934 strmap_free(map
,NULL
);
1938 test_util_mmap(void)
1940 char *fname1
= tor_strdup(get_fname("mapped_1"));
1941 char *fname2
= tor_strdup(get_fname("mapped_2"));
1942 char *fname3
= tor_strdup(get_fname("mapped_3"));
1943 const size_t buflen
= 17000;
1944 char *buf
= tor_malloc(17000);
1945 tor_mmap_t
*mapping
;
1947 crypto_rand(buf
, buflen
);
1949 write_str_to_file(fname1
, "Short file.", 1);
1950 write_bytes_to_file(fname2
, buf
, buflen
, 1);
1951 write_bytes_to_file(fname3
, buf
, 16384, 1);
1953 mapping
= tor_mmap_file(fname1
);
1954 test_assert(mapping
);
1955 test_eq(mapping
->size
, strlen("Short file."));
1956 test_streq(mapping
->data
, "Short file.");
1958 tor_munmap_file(mapping
);
1959 test_assert(unlink(fname1
) == 0);
1961 /* make sure we can unlink. */
1962 test_assert(unlink(fname1
) == 0);
1963 test_streq(mapping
->data
, "Short file.");
1964 tor_munmap_file(mapping
);
1967 /* Make sure that we fail to map a no-longer-existent file. */
1968 mapping
= tor_mmap_file(fname1
);
1969 test_assert(mapping
== NULL
);
1971 /* Now try a big file that stretches across a few pages and isn't aligned */
1972 mapping
= tor_mmap_file(fname2
);
1973 test_assert(mapping
);
1974 test_eq(mapping
->size
, buflen
);
1975 test_memeq(mapping
->data
, buf
, buflen
);
1976 tor_munmap_file(mapping
);
1978 /* Now try a big aligned file. */
1979 mapping
= tor_mmap_file(fname3
);
1980 test_assert(mapping
);
1981 test_eq(mapping
->size
, 16384);
1982 test_memeq(mapping
->data
, buf
, 16384);
1983 tor_munmap_file(mapping
);
1985 /* fname1 got unlinked above */
1996 test_util_control_formats(void)
2000 "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
2003 sz
= read_escaped_data(inp
, strlen(inp
), &out
);
2005 ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
2006 test_eq(sz
, strlen(out
));
2012 test_onion_handshake(void)
2015 crypto_dh_env_t
*c_dh
= NULL
;
2016 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
2020 char s_buf
[ONIONSKIN_REPLY_LEN
];
2024 crypto_pk_env_t
*pk
= NULL
;
2026 pk
= pk_generate(0);
2028 /* client handshake 1. */
2029 memset(c_buf
, 0, ONIONSKIN_CHALLENGE_LEN
);
2030 test_assert(! onion_skin_create(pk
, &c_dh
, c_buf
));
2032 /* server handshake */
2033 memset(s_buf
, 0, ONIONSKIN_REPLY_LEN
);
2034 memset(s_keys
, 0, 40);
2035 test_assert(! onion_skin_server_handshake(c_buf
, pk
, NULL
,
2036 s_buf
, s_keys
, 40));
2038 /* client handshake 2 */
2039 memset(c_keys
, 0, 40);
2040 test_assert(! onion_skin_client_handshake(c_dh
, s_buf
, c_keys
, 40));
2042 crypto_dh_free(c_dh
);
2044 if (memcmp(c_keys
, s_keys
, 40)) {
2048 test_memeq(c_keys
, s_keys
, 40);
2049 memset(s_buf
, 0, 40);
2050 test_memneq(c_keys
, s_buf
, 40);
2051 crypto_free_pk_env(pk
);
2054 extern smartlist_t
*fingerprint_list
;
2057 test_dir_format(void)
2059 char buf
[8192], buf2
[8192];
2061 char fingerprint
[FINGERPRINT_LEN
+1];
2062 char *pk1_str
= NULL
, *pk2_str
= NULL
, *pk3_str
= NULL
, *cp
;
2063 size_t pk1_str_len
, pk2_str_len
, pk3_str_len
;
2064 routerinfo_t r1
, r2
;
2065 crypto_pk_env_t
*pk1
= NULL
, *pk2
= NULL
, *pk3
= NULL
;
2066 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
2067 addr_policy_t ex1
, ex2
;
2068 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
2071 pk1
= pk_generate(0);
2072 pk2
= pk_generate(1);
2073 pk3
= pk_generate(2);
2075 test_assert( is_legal_nickname("a"));
2076 test_assert(!is_legal_nickname(""));
2077 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
2078 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
2079 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
2080 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2082 test_assert( is_legal_nickname_or_hexdigest(
2083 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2084 test_assert( is_legal_nickname_or_hexdigest(
2085 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
2086 test_assert( is_legal_nickname_or_hexdigest(
2087 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
2089 test_assert(!is_legal_nickname_or_hexdigest(
2090 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2092 test_assert(!is_legal_nickname_or_hexdigest(
2093 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2094 /* hex part too long */
2095 test_assert(!is_legal_nickname_or_hexdigest(
2096 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2097 test_assert(!is_legal_nickname_or_hexdigest(
2098 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
2100 test_assert(!is_legal_nickname_or_hexdigest(
2101 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
2102 test_assert(!is_legal_nickname_or_hexdigest(
2103 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
2104 test_assert(!is_legal_nickname_or_hexdigest(
2105 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
2106 test_assert(!is_legal_nickname_or_hexdigest(
2107 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
2108 "abcdefghijklmnoppqrst"));
2109 /* Bad extra char. */
2110 test_assert(!is_legal_nickname_or_hexdigest(
2111 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
2112 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
2113 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
2114 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
2116 get_platform_str(platform
, sizeof(platform
));
2117 memset(&r1
,0,sizeof(r1
));
2118 memset(&r2
,0,sizeof(r2
));
2119 r1
.address
= tor_strdup("18.244.0.1");
2120 r1
.addr
= 0xc0a80001u
; /* 192.168.0.1 */
2121 r1
.cache_info
.published_on
= 0;
2124 r1
.onion_pkey
= pk1
;
2125 r1
.identity_pkey
= pk2
;
2126 r1
.bandwidthrate
= 1000;
2127 r1
.bandwidthburst
= 5000;
2128 r1
.bandwidthcapacity
= 10000;
2129 r1
.exit_policy
= NULL
;
2130 r1
.nickname
= tor_strdup("Magri");
2131 r1
.platform
= tor_strdup(platform
);
2133 ex1
.policy_type
= ADDR_POLICY_ACCEPT
;
2137 ex1
.prt_min
= ex1
.prt_max
= 80;
2139 ex2
.policy_type
= ADDR_POLICY_REJECT
;
2140 ex2
.addr
= 18 << 24;
2142 ex2
.prt_min
= ex2
.prt_max
= 24;
2144 r2
.address
= tor_strdup("1.1.1.1");
2145 r2
.addr
= 0x0a030201u
; /* 10.3.2.1 */
2146 r2
.platform
= tor_strdup(platform
);
2147 r2
.cache_info
.published_on
= 5;
2150 r2
.onion_pkey
= pk2
;
2151 r2
.identity_pkey
= pk1
;
2152 r2
.bandwidthrate
= r2
.bandwidthburst
= r2
.bandwidthcapacity
= 3000;
2153 r2
.exit_policy
= &ex1
;
2154 r2
.nickname
= tor_strdup("Fred");
2156 test_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
2158 test_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
2160 test_assert(!crypto_pk_write_public_key_to_string(pk3
, &pk3_str
,
2163 memset(buf
, 0, 2048);
2164 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
2166 strlcpy(buf2
, "router Magri 18.244.0.1 9000 0 0\n"
2167 "platform Tor "VERSION
" on ", sizeof(buf2
));
2168 strlcat(buf2
, get_uname(), sizeof(buf2
));
2170 "published 1970-01-01 00:00:00\n"
2171 "opt fingerprint ", sizeof(buf2
));
2172 test_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
2173 strlcat(buf2
, fingerprint
, sizeof(buf2
));
2174 strlcat(buf2
, "\nuptime 0\n"
2175 /* XXX the "0" above is hardcoded, but even if we made it reflect
2176 * uptime, that still wouldn't make it right, because the two
2177 * descriptors might be made on different seconds... hm. */
2178 "bandwidth 1000 5000 10000\n"
2179 "opt extra-info-digest 0000000000000000000000000000000000000000\n"
2180 "onion-key\n", sizeof(buf2
));
2181 strlcat(buf2
, pk1_str
, sizeof(buf2
));
2182 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
2183 strlcat(buf2
, pk2_str
, sizeof(buf2
));
2184 strlcat(buf2
, "router-signature\n", sizeof(buf2
));
2185 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
2188 test_streq(buf
, buf2
);
2190 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
2192 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1);
2194 test_streq(rp1
->address
, r1
.address
);
2195 test_eq(rp1
->or_port
, r1
.or_port
);
2196 //test_eq(rp1->dir_port, r1.dir_port);
2197 test_eq(rp1
->bandwidthrate
, r1
.bandwidthrate
);
2198 test_eq(rp1
->bandwidthburst
, r1
.bandwidthburst
);
2199 test_eq(rp1
->bandwidthcapacity
, r1
.bandwidthcapacity
);
2200 test_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
2201 test_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
2202 test_assert(rp1
->exit_policy
== NULL
);
2205 /* XXX Once we have exit policies, test this again. XXX */
2206 strlcpy(buf2
, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2
));
2207 strlcat(buf2
, pk2_str
, sizeof(buf2
));
2208 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
2209 strlcat(buf2
, pk1_str
, sizeof(buf2
));
2210 strlcat(buf2
, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2
));
2211 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk2
)>0);
2212 test_streq(buf
, buf2
);
2215 rp2
= router_parse_entry_from_string(&cp
,1);
2217 test_streq(rp2
->address
, r2
.address
);
2218 test_eq(rp2
->or_port
, r2
.or_port
);
2219 test_eq(rp2
->dir_port
, r2
.dir_port
);
2220 test_eq(rp2
->bandwidth
, r2
.bandwidth
);
2221 test_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
2222 test_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
2223 test_eq(rp2
->exit_policy
->policy_type
, EXIT_POLICY_ACCEPT
);
2224 test_streq(rp2
->exit_policy
->string
, "accept *:80");
2225 test_streq(rp2
->exit_policy
->address
, "*");
2226 test_streq(rp2
->exit_policy
->port
, "80");
2227 test_eq(rp2
->exit_policy
->next
->policy_type
, EXIT_POLICY_REJECT
);
2228 test_streq(rp2
->exit_policy
->next
->string
, "reject 18.*:24");
2229 test_streq(rp2
->exit_policy
->next
->address
, "18.*");
2230 test_streq(rp2
->exit_policy
->next
->port
, "24");
2231 test_assert(rp2
->exit_policy
->next
->next
== NULL
);
2233 /* Okay, now for the directories. */
2235 fingerprint_list
= smartlist_create();
2236 crypto_pk_get_fingerprint(pk2
, buf
, 1);
2237 add_fingerprint_to_dir("Magri", buf
, fingerprint_list
);
2238 crypto_pk_get_fingerprint(pk1
, buf
, 1);
2239 add_fingerprint_to_dir("Fred", buf
, fingerprint_list
);
2245 /* XXXX NM re-enable. */
2246 /* Make sure routers aren't too far in the past any more. */
2247 r1
.cache_info
.published_on
= time(NULL
);
2248 r2
.cache_info
.published_on
= time(NULL
)-3*60*60;
2249 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
2250 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
2251 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk1
)>0);
2252 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
2253 get_options()->Nickname
= tor_strdup("DirServer");
2254 test_assert(!dirserv_dump_directory_to_string(&cp
,pk3
, 0));
2255 crypto_pk_get_digest(pk3
, d
);
2256 test_assert(!router_parse_directory(cp
));
2257 test_eq(2, smartlist_len(dir1
->routers
));
2261 dirserv_free_fingerprint_list();
2265 if (pk1
) crypto_free_pk_env(pk1
);
2266 if (pk2
) crypto_free_pk_env(pk2
);
2267 if (rp1
) routerinfo_free(rp1
);
2268 if (rp2
) routerinfo_free(rp2
);
2269 tor_free(dir1
); /* XXXX And more !*/
2270 tor_free(dir2
); /* And more !*/
2272 /* Try out version parsing functionality */
2273 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1
));
2274 test_eq(0, ver1
.major
);
2275 test_eq(3, ver1
.minor
);
2276 test_eq(4, ver1
.micro
);
2277 test_eq(VER_PRE
, ver1
.status
);
2278 test_eq(2, ver1
.patchlevel
);
2279 test_eq(0, tor_version_parse("0.3.4rc1", &ver1
));
2280 test_eq(0, ver1
.major
);
2281 test_eq(3, ver1
.minor
);
2282 test_eq(4, ver1
.micro
);
2283 test_eq(VER_RC
, ver1
.status
);
2284 test_eq(1, ver1
.patchlevel
);
2285 test_eq(0, tor_version_parse("1.3.4", &ver1
));
2286 test_eq(1, ver1
.major
);
2287 test_eq(3, ver1
.minor
);
2288 test_eq(4, ver1
.micro
);
2289 test_eq(VER_RELEASE
, ver1
.status
);
2290 test_eq(0, ver1
.patchlevel
);
2291 test_eq(0, tor_version_parse("1.3.4.999", &ver1
));
2292 test_eq(1, ver1
.major
);
2293 test_eq(3, ver1
.minor
);
2294 test_eq(4, ver1
.micro
);
2295 test_eq(VER_RELEASE
, ver1
.status
);
2296 test_eq(999, ver1
.patchlevel
);
2297 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1
));
2298 test_eq(0, ver1
.major
);
2299 test_eq(1, ver1
.minor
);
2300 test_eq(2, ver1
.micro
);
2301 test_eq(4, ver1
.patchlevel
);
2302 test_eq(VER_RELEASE
, ver1
.status
);
2303 test_streq("alpha", ver1
.status_tag
);
2304 test_eq(0, tor_version_parse("0.1.2.4", &ver1
));
2305 test_eq(0, ver1
.major
);
2306 test_eq(1, ver1
.minor
);
2307 test_eq(2, ver1
.micro
);
2308 test_eq(4, ver1
.patchlevel
);
2309 test_eq(VER_RELEASE
, ver1
.status
);
2310 test_streq("", ver1
.status_tag
);
2312 #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
2313 #define test_v_i_o(val, ver, lst) \
2314 test_eq_vs(val, tor_version_is_obsolete(ver, lst))
2316 /* make sure tor_version_is_obsolete() works */
2317 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
2318 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
2319 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
2320 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
2321 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
2322 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
2323 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
2324 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
2325 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
2326 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
2327 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
2328 /* Not on list, but newer than any in same series. */
2329 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
2330 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2331 /* Series newer than any on list. */
2332 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");
2333 /* Series older than any on list. */
2334 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");
2335 /* Not on list, not newer than any on same series. */
2336 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
2337 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2338 /* On list, not newer than any on same series. */
2339 test_v_i_o(VS_UNRECOMMENDED
,
2340 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2341 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
2342 test_eq(1, tor_version_as_new_as(
2343 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
2344 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
2346 test_eq(0, tor_version_as_new_as(
2347 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
2348 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
2350 /* Now try svn revisions. */
2351 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
2352 "Tor 0.2.1.0-dev (r99)"));
2353 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
2354 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
2355 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
2356 "Tor 0.2.1.0-dev on Colossus"));
2357 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
2358 "Tor 0.2.1.0-dev (r100)"));
2359 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
2360 "Tor 0.2.1.0-dev (r100) on AM"));
2361 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
2362 "Tor 0.2.1.0-dev (r99)"));
2363 test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
2364 "Tor 0.2.1.0-dev (r99)"));
2367 extern const char AUTHORITY_CERT_1
[];
2368 extern const char AUTHORITY_SIGNKEY_1
[];
2369 extern const char AUTHORITY_CERT_2
[];
2370 extern const char AUTHORITY_SIGNKEY_2
[];
2371 extern const char AUTHORITY_CERT_3
[];
2372 extern const char AUTHORITY_SIGNKEY_3
[];
2375 test_same_voter(networkstatus_voter_info_t
*v1
,
2376 networkstatus_voter_info_t
*v2
)
2378 test_streq(v1
->nickname
, v2
->nickname
);
2379 test_memeq(v1
->identity_digest
, v2
->identity_digest
, DIGEST_LEN
);
2380 test_streq(v1
->address
, v2
->address
);
2381 test_eq(v1
->addr
, v2
->addr
);
2382 test_eq(v1
->dir_port
, v2
->dir_port
);
2383 test_eq(v1
->or_port
, v2
->or_port
);
2384 test_streq(v1
->contact
, v2
->contact
);
2385 test_memeq(v1
->vote_digest
, v2
->vote_digest
, DIGEST_LEN
);
2389 test_util_dirvote_helpers(void)
2391 smartlist_t
*sl
= smartlist_create();
2392 int a
=12,b
=24,c
=25,d
=60,e
=77;
2393 time_t v
=99, w
=150, x
=700, y
=1000, z
=time(NULL
);
2396 smartlist_add(sl
, &a
);
2397 test_eq(a
, median_int(sl
)); /* a */
2398 smartlist_add(sl
, &e
);
2399 smartlist_shuffle(sl
);
2400 test_eq(a
, median_int(sl
)); /* a,e */
2401 smartlist_add(sl
, &e
);
2402 smartlist_shuffle(sl
);
2403 test_eq(e
, median_int(sl
)); /* a,e,e */
2404 smartlist_add(sl
, &b
);
2405 test_eq(b
, median_int(sl
)); /* a,b,e,e */
2406 smartlist_add(sl
, &d
);
2407 smartlist_add(sl
, &a
);
2408 smartlist_add(sl
, &c
);
2409 smartlist_shuffle(sl
);
2410 test_eq(c
, median_int(sl
)); /* a,a,b,c,d,e,e */
2412 smartlist_clear(sl
);
2413 smartlist_add(sl
, &y
);
2414 test_eq(y
, median_time(sl
)); /*y*/
2415 smartlist_add(sl
, &w
);
2416 test_eq(w
, median_time(sl
)); /*w,y*/
2417 smartlist_add(sl
, &x
);
2418 test_eq(x
, median_time(sl
)); /*w,x,y*/
2419 smartlist_add(sl
, &v
);
2420 test_eq(w
, median_time(sl
)); /*v,w,x,y*/
2421 smartlist_add(sl
, &z
);
2422 test_eq(x
, median_time(sl
)); /*v,w,x,y,z*/
2428 test_v3_networkstatus(void)
2430 authority_cert_t
*cert1
, *cert2
, *cert3
;
2431 crypto_pk_env_t
*sign_skey_1
, *sign_skey_2
, *sign_skey_3
;
2433 time_t now
= time(NULL
);
2434 networkstatus_voter_info_t
*voter
;
2435 networkstatus_vote_t
*vote
, *v1
, *v2
, *v3
, *con
;
2436 vote_routerstatus_t
*vrs
;
2438 char *v1_text
, *v2_text
, *v3_text
, *consensus_text
, *cp
;
2439 smartlist_t
*votes
= smartlist_create();
2441 /* Parse certificates and keys. */
2442 cert1
= authority_cert_parse_from_string(AUTHORITY_CERT_1
, NULL
);
2444 cert2
= authority_cert_parse_from_string(AUTHORITY_CERT_2
, NULL
);
2446 cert3
= authority_cert_parse_from_string(AUTHORITY_CERT_3
, NULL
);
2448 sign_skey_1
= crypto_new_pk_env();
2449 sign_skey_2
= crypto_new_pk_env();
2450 sign_skey_3
= crypto_new_pk_env();
2452 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1
,
2453 AUTHORITY_SIGNKEY_1
));
2454 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2
,
2455 AUTHORITY_SIGNKEY_2
));
2456 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3
,
2457 AUTHORITY_SIGNKEY_3
));
2459 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, cert1
->signing_key
));
2460 test_assert(!crypto_pk_cmp_keys(sign_skey_2
, cert2
->signing_key
));
2463 * Set up a vote; generate it; try to parse it.
2465 vote
= tor_malloc_zero(sizeof(networkstatus_vote_t
));
2467 vote
->published
= now
;
2468 vote
->valid_after
= now
+1000;
2469 vote
->fresh_until
= now
+2000;
2470 vote
->valid_until
= now
+3000;
2471 vote
->vote_seconds
= 100;
2472 vote
->dist_seconds
= 200;
2473 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.15");
2474 vote
->server_versions
= tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
2475 vote
->known_flags
= smartlist_create();
2476 smartlist_split_string(vote
->known_flags
,
2477 "Authority Exit Fast Guard Running Stable V2Dir Valid",
2478 0, SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2479 vote
->voters
= smartlist_create();
2480 voter
= tor_malloc_zero(sizeof(networkstatus_voter_info_t
));
2481 voter
->nickname
= tor_strdup("Voter1");
2482 voter
->address
= tor_strdup("1.2.3.4");
2483 voter
->addr
= 0x01020304;
2484 voter
->dir_port
= 80;
2485 voter
->or_port
= 9000;
2486 voter
->contact
= tor_strdup("voter@example.com");
2487 crypto_pk_get_digest(cert1
->identity_key
, voter
->identity_digest
);
2488 smartlist_add(vote
->voters
, voter
);
2489 vote
->cert
= authority_cert_dup(cert1
);
2490 vote
->routerstatus_list
= smartlist_create();
2491 /* add the first routerstatus. */
2492 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2494 vrs
->version
= tor_strdup("0.1.2.14");
2495 rs
->published_on
= now
-1500;
2496 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
2497 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
2498 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2499 rs
->addr
= 0x99008801;
2501 rs
->dir_port
= 8000;
2502 /* all flags cleared */
2503 smartlist_add(vote
->routerstatus_list
, vrs
);
2504 /* add the second routerstatus. */
2505 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2507 vrs
->version
= tor_strdup("0.2.0.5");
2508 rs
->published_on
= now
-1000;
2509 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
2510 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
2511 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
2512 rs
->addr
= 0x99009901;
2515 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_running
=
2516 rs
->is_valid
= rs
->is_v2_dir
= rs
->is_possible_guard
= 1;
2517 smartlist_add(vote
->routerstatus_list
, vrs
);
2518 /* add the third routerstatus. */
2519 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2521 vrs
->version
= tor_strdup("0.1.0.3");
2522 rs
->published_on
= now
-1000;
2523 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
2524 memset(rs
->identity_digest
, 33, DIGEST_LEN
);
2525 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2526 rs
->addr
= 0xAA009901;
2528 rs
->dir_port
= 9999;
2529 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
2530 rs
->is_running
= rs
->is_valid
= rs
->is_v2_dir
= rs
->is_possible_guard
= 1;
2531 smartlist_add(vote
->routerstatus_list
, vrs
);
2533 /* dump the vote and try to parse it. */
2534 v1_text
= format_networkstatus_vote(sign_skey_1
, vote
);
2535 test_assert(v1_text
);
2536 v1
= networkstatus_parse_vote_from_string(v1_text
, 1);
2539 /* Make sure the parsed thing was right. */
2540 test_eq(v1
->is_vote
, 1);
2541 test_eq(v1
->published
, vote
->published
);
2542 test_eq(v1
->valid_after
, vote
->valid_after
);
2543 test_eq(v1
->fresh_until
, vote
->fresh_until
);
2544 test_eq(v1
->valid_until
, vote
->valid_until
);
2545 test_eq(v1
->vote_seconds
, vote
->vote_seconds
);
2546 test_eq(v1
->dist_seconds
, vote
->dist_seconds
);
2547 test_streq(v1
->client_versions
, vote
->client_versions
);
2548 test_streq(v1
->server_versions
, vote
->server_versions
);
2549 test_assert(v1
->voters
&& smartlist_len(v1
->voters
));
2550 voter
= smartlist_get(v1
->voters
, 0);
2551 test_streq(voter
->nickname
, "Voter1");
2552 test_streq(voter
->address
, "1.2.3.4");
2553 test_eq(voter
->addr
, 0x01020304);
2554 test_eq(voter
->dir_port
, 80);
2555 test_eq(voter
->or_port
, 9000);
2556 test_streq(voter
->contact
, "voter@example.com");
2557 test_assert(v1
->cert
);
2558 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, v1
->cert
->signing_key
));
2559 cp
= smartlist_join_strings(v1
->known_flags
, ":", 0, NULL
);
2560 test_streq(cp
, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
2562 test_eq(smartlist_len(v1
->routerstatus_list
), 3);
2563 /* Check the first routerstatus. */
2564 vrs
= smartlist_get(v1
->routerstatus_list
, 0);
2566 test_streq(vrs
->version
, "0.1.2.14");
2567 test_eq(rs
->published_on
, now
-1500);
2568 test_streq(rs
->nickname
, "router2");
2569 test_memeq(rs
->identity_digest
,
2570 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2572 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2573 test_eq(rs
->addr
, 0x99008801);
2574 test_eq(rs
->or_port
, 443);
2575 test_eq(rs
->dir_port
, 8000);
2576 test_eq(vrs
->flags
, U64_LITERAL(0));
2577 /* Check the second routerstatus. */
2578 vrs
= smartlist_get(v1
->routerstatus_list
, 1);
2580 test_streq(vrs
->version
, "0.2.0.5");
2581 test_eq(rs
->published_on
, now
-1000);
2582 test_streq(rs
->nickname
, "router1");
2583 test_memeq(rs
->identity_digest
,
2584 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2586 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2587 test_eq(rs
->addr
, 0x99009901);
2588 test_eq(rs
->or_port
, 443);
2589 test_eq(rs
->dir_port
, 0);
2590 test_eq(vrs
->flags
, U64_LITERAL(254)); // all flags except "authority."
2592 /* Generate second vote. It disagrees on some of the times,
2593 * and doesn't list versions, and knows some crazy flags */
2594 vote
->published
= now
+1;
2595 vote
->fresh_until
= now
+3005;
2596 vote
->dist_seconds
= 300;
2597 authority_cert_free(vote
->cert
);
2598 vote
->cert
= authority_cert_dup(cert2
);
2599 tor_free(vote
->client_versions
);
2600 tor_free(vote
->server_versions
);
2601 voter
= smartlist_get(vote
->voters
, 0);
2602 tor_free(voter
->nickname
);
2603 tor_free(voter
->address
);
2604 voter
->nickname
= tor_strdup("Voter2");
2605 voter
->address
= tor_strdup("2.3.4.5");
2606 voter
->addr
= 0x02030405;
2607 crypto_pk_get_digest(cert2
->identity_key
, voter
->identity_digest
);
2608 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfCheese"));
2609 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfTin"));
2610 smartlist_sort_strings(vote
->known_flags
);
2611 vrs
= smartlist_get(vote
->routerstatus_list
, 2);
2612 smartlist_del_keeporder(vote
->routerstatus_list
, 2);
2613 tor_free(vrs
->version
);
2615 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2616 vrs
->status
.is_fast
= 1;
2617 /* generate and parse. */
2618 v2_text
= format_networkstatus_vote(sign_skey_2
, vote
);
2619 test_assert(v2_text
);
2620 v2
= networkstatus_parse_vote_from_string(v2_text
, 1);
2622 /* Check that flags come out right.*/
2623 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2624 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2625 "Running:Stable:V2Dir:Valid");
2627 vrs
= smartlist_get(v2
->routerstatus_list
, 1);
2628 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
2629 test_eq(vrs
->flags
, U64_LITERAL(974));
2631 /* Generate the third vote. */
2632 vote
->published
= now
;
2633 vote
->fresh_until
= now
+2003;
2634 vote
->dist_seconds
= 250;
2635 authority_cert_free(vote
->cert
);
2636 vote
->cert
= authority_cert_dup(cert3
);
2637 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.17");
2638 vote
->server_versions
= tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
2639 voter
= smartlist_get(vote
->voters
, 0);
2640 tor_free(voter
->nickname
);
2641 tor_free(voter
->address
);
2642 voter
->nickname
= tor_strdup("Voter3");
2643 voter
->address
= tor_strdup("3.4.5.6");
2644 voter
->addr
= 0x03040506;
2645 crypto_pk_get_digest(cert3
->identity_key
, voter
->identity_digest
);
2646 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2647 smartlist_del_keeporder(vote
->routerstatus_list
, 0);
2648 tor_free(vrs
->version
);
2650 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2651 memset(vrs
->status
.descriptor_digest
, (int)'Z', DIGEST_LEN
);
2653 v3_text
= format_networkstatus_vote(sign_skey_3
, vote
);
2654 test_assert(v3_text
);
2655 v3
= networkstatus_parse_vote_from_string(v3_text
, 1);
2658 /* Compute a consensus as voter 3. */
2659 smartlist_add(votes
, v3
);
2660 smartlist_add(votes
, v1
);
2661 smartlist_add(votes
, v2
);
2662 consensus_text
= networkstatus_compute_consensus(votes
, 3,
2663 cert3
->identity_key
,
2665 test_assert(consensus_text
);
2666 con
= networkstatus_parse_vote_from_string(consensus_text
, 0);
2668 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
2669 // v1_text, v2_text, v3_text);
2671 /* Check consensus contents. */
2672 test_assert(!con
->is_vote
);
2673 test_eq(con
->published
, 0); /* this field only appears in votes. */
2674 test_eq(con
->valid_after
, now
+1000);
2675 test_eq(con
->fresh_until
, now
+2003); /* median */
2676 test_eq(con
->valid_until
, now
+3000);
2677 test_eq(con
->vote_seconds
, 100);
2678 test_eq(con
->dist_seconds
, 250); /* median */
2679 test_streq(con
->client_versions
, "0.1.2.14");
2680 test_streq(con
->server_versions
, "0.1.2.15,0.1.2.16");
2681 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2682 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2683 "Running:Stable:V2Dir:Valid");
2685 test_eq(3, smartlist_len(con
->voters
));
2686 /* The voter id digests should be in this order. */
2687 test_assert(memcmp(cert2
->cache_info
.identity_digest
,
2688 cert3
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2689 test_assert(memcmp(cert3
->cache_info
.identity_digest
,
2690 cert1
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2691 test_same_voter(smartlist_get(con
->voters
, 0),
2692 smartlist_get(v2
->voters
, 0));
2693 test_same_voter(smartlist_get(con
->voters
, 1),
2694 smartlist_get(v3
->voters
, 0));
2695 test_same_voter(smartlist_get(con
->voters
, 2),
2696 smartlist_get(v1
->voters
, 0));
2698 test_assert(!con
->cert
);
2699 test_eq(2, smartlist_len(con
->routerstatus_list
));
2700 /* There should be two listed routers: one with identity 3, one with
2702 /* This one showed up in 2 digests. */
2703 rs
= smartlist_get(con
->routerstatus_list
, 0);
2704 test_memeq(rs
->identity_digest
,
2705 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2707 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2708 test_assert(!rs
->is_authority
);
2709 test_assert(!rs
->is_exit
);
2710 test_assert(!rs
->is_fast
);
2711 test_assert(!rs
->is_possible_guard
);
2712 test_assert(!rs
->is_stable
);
2713 test_assert(!rs
->is_running
);
2714 test_assert(!rs
->is_v2_dir
);
2715 test_assert(!rs
->is_valid
);
2716 test_assert(!rs
->is_named
);
2717 /* XXXX020 check version */
2719 rs
= smartlist_get(con
->routerstatus_list
, 1);
2720 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2721 test_memeq(rs
->identity_digest
,
2722 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2724 test_streq(rs
->nickname
, "router1");
2725 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2726 test_eq(rs
->published_on
, now
-1000);
2727 test_eq(rs
->addr
, 0x99009901);
2728 test_eq(rs
->or_port
, 443);
2729 test_eq(rs
->dir_port
, 0);
2730 test_assert(!rs
->is_authority
);
2731 test_assert(rs
->is_exit
);
2732 test_assert(rs
->is_fast
);
2733 test_assert(rs
->is_possible_guard
);
2734 test_assert(rs
->is_stable
);
2735 test_assert(rs
->is_running
);
2736 test_assert(rs
->is_v2_dir
);
2737 test_assert(rs
->is_valid
);
2738 test_assert(!rs
->is_named
);
2739 /* XXXX020 check version */
2741 /* Check signatures. the first voter hasn't got one. The second one
2742 * does: validate it. */
2743 voter
= smartlist_get(con
->voters
, 0);
2744 test_assert(!voter
->signature
);
2745 test_assert(!voter
->good_signature
);
2746 test_assert(!voter
->bad_signature
);
2748 voter
= smartlist_get(con
->voters
, 1);
2749 test_assert(voter
->signature
);
2750 test_assert(!voter
->good_signature
);
2751 test_assert(!voter
->bad_signature
);
2752 test_assert(!networkstatus_check_voter_signature(con
,
2753 smartlist_get(con
->voters
, 1),
2755 test_assert(voter
->signature
);
2756 test_assert(voter
->good_signature
);
2757 test_assert(!voter
->bad_signature
);
2760 char *consensus_text2
, *consensus_text3
;
2761 networkstatus_vote_t
*con2
, *con3
;
2762 char *detached_text1
, *detached_text2
;
2763 ns_detached_signatures_t
*dsig1
, *dsig2
;
2764 /* Compute the other two signed consensuses. */
2765 smartlist_shuffle(votes
);
2766 consensus_text2
= networkstatus_compute_consensus(votes
, 3,
2767 cert2
->identity_key
,
2769 smartlist_shuffle(votes
);
2770 consensus_text3
= networkstatus_compute_consensus(votes
, 3,
2771 cert1
->identity_key
,
2773 test_assert(consensus_text2
);
2774 test_assert(consensus_text3
);
2775 con2
= networkstatus_parse_vote_from_string(consensus_text2
, 0);
2776 con3
= networkstatus_parse_vote_from_string(consensus_text3
, 0);
2780 /* All three should have the same digest. */
2781 test_memeq(con
->networkstatus_digest
, con2
->networkstatus_digest
,
2783 test_memeq(con
->networkstatus_digest
, con3
->networkstatus_digest
,
2786 /* Extract a detached signature from con3. */
2787 detached_text1
= networkstatus_get_detached_signatures(con3
);
2788 tor_assert(detached_text1
);
2789 /* Try to parse it. */
2790 dsig1
= networkstatus_parse_detached_signatures(detached_text1
, NULL
);
2793 /* Are parsed values as expected? */
2794 test_eq(dsig1
->valid_after
, con3
->valid_after
);
2795 test_eq(dsig1
->fresh_until
, con3
->fresh_until
);
2796 test_eq(dsig1
->valid_until
, con3
->valid_until
);
2797 test_memeq(dsig1
->networkstatus_digest
, con3
->networkstatus_digest
,
2799 test_eq(1, smartlist_len(dsig1
->signatures
));
2800 voter
= smartlist_get(dsig1
->signatures
, 0);
2801 test_memeq(voter
->identity_digest
, cert1
->cache_info
.identity_digest
,
2804 /* Try adding it to con2. */
2805 detached_text2
= networkstatus_get_detached_signatures(con2
);
2806 test_eq(1, networkstatus_add_detached_signatures(con2
, dsig1
));
2807 tor_free(detached_text2
);
2808 detached_text2
= networkstatus_get_detached_signatures(con2
);
2809 //printf("\n<%s>\n", detached_text2);
2810 dsig2
= networkstatus_parse_detached_signatures(detached_text2
, NULL
);
2814 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2816 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2820 test_eq(2, smartlist_len(dsig2
->signatures
));
2822 /* Try adding to con2 twice; verify that nothing changes. */
2823 test_eq(0, networkstatus_add_detached_signatures(con2
, dsig1
));
2826 test_eq(2, networkstatus_add_detached_signatures(con
, dsig2
));
2827 /* Check signatures */
2828 test_assert(!networkstatus_check_voter_signature(con
,
2829 smartlist_get(con
->voters
, 0),
2831 test_assert(!networkstatus_check_voter_signature(con
,
2832 smartlist_get(con
->voters
, 2),
2835 networkstatus_vote_free(con2
);
2836 networkstatus_vote_free(con3
);
2837 tor_free(consensus_text2
);
2838 tor_free(consensus_text3
);
2839 tor_free(detached_text1
);
2840 tor_free(detached_text2
);
2841 ns_detached_signatures_free(dsig1
);
2842 ns_detached_signatures_free(dsig2
);
2845 smartlist_free(votes
);
2849 tor_free(consensus_text
);
2850 networkstatus_vote_free(vote
);
2851 networkstatus_vote_free(v1
);
2852 networkstatus_vote_free(v2
);
2853 networkstatus_vote_free(v3
);
2854 networkstatus_vote_free(con
);
2855 crypto_free_pk_env(sign_skey_1
);
2856 crypto_free_pk_env(sign_skey_2
);
2857 crypto_free_pk_env(sign_skey_3
);
2858 authority_cert_free(cert1
);
2859 authority_cert_free(cert2
);
2860 authority_cert_free(cert3
);
2866 addr_policy_t
*policy
, *policy2
;
2870 policy
= router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
2871 test_eq(NULL
, policy
->next
);
2872 test_eq(ADDR_POLICY_REJECT
, policy
->policy_type
);
2873 tor_addr_from_ipv4(&tar
, 0xc0a80000u
);
2874 test_assert(policy
->addr
== 0xc0a80000u
);
2875 test_eq(16, policy
->maskbits
);
2876 test_eq(1, policy
->prt_min
);
2877 test_eq(65535, policy
->prt_max
);
2878 test_streq("reject 192.168.0.0/16:*", policy
->string
);
2880 test_assert(ADDR_POLICY_ACCEPTED
==
2881 compare_addr_to_addr_policy(0x01020304u
, 2, policy
));
2882 test_assert(ADDR_POLICY_PROBABLY_ACCEPTED
==
2883 compare_addr_to_addr_policy(0, 2, policy
));
2884 test_assert(ADDR_POLICY_REJECTED
==
2885 compare_addr_to_addr_policy(0xc0a80102, 2, policy
));
2888 test_assert(0 == policies_parse_exit_policy(NULL
, &policy2
, 1));
2889 test_assert(policy2
);
2891 test_assert(!exit_policy_is_general_exit(policy
));
2892 test_assert(exit_policy_is_general_exit(policy2
));
2894 test_assert(cmp_addr_policies(policy
, policy2
));
2895 test_assert(!cmp_addr_policies(policy2
, policy2
));
2897 test_assert(!policy_is_reject_star(policy2
));
2898 test_assert(policy_is_reject_star(policy
));
2900 addr_policy_free(policy
);
2901 addr_policy_free(policy2
);
2903 /* make sure compacting logic works. */
2905 line
.key
= (char*)"foo";
2906 line
.value
= (char*)"accept *:80,reject private:*,reject *:*";
2908 test_assert(0 == policies_parse_exit_policy(&line
, &policy
, 0));
2909 test_assert(policy
);
2910 test_streq(policy
->string
, "accept *:80");
2911 test_streq(policy
->next
->string
, "reject *:*");
2912 test_eq_ptr(policy
->next
->next
, NULL
);
2914 addr_policy_free(policy
);
2920 char address1
[] = "fooaddress.onion";
2921 char address2
[] = "aaaaaaaaaaaaaaaa.onion";
2922 char address3
[] = "fooaddress.exit";
2923 char address4
[] = "tor.eff.org";
2924 rend_service_descriptor_t
*d1
, *d2
;
2927 crypto_pk_env_t
*pk1
, *pk2
;
2929 pk1
= pk_generate(0);
2930 pk2
= pk_generate(1);
2932 /* Test unversioned descriptor */
2933 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
2934 d1
->pk
= crypto_pk_dup_key(pk1
);
2936 d1
->timestamp
= now
;
2937 d1
->n_intro_points
= 3;
2939 d1
->intro_points
= tor_malloc(sizeof(char*)*3);
2940 d1
->intro_points
[0] = tor_strdup("tom");
2941 d1
->intro_points
[1] = tor_strdup("crow");
2942 d1
->intro_points
[2] = tor_strdup("joel");
2943 test_assert(! rend_encode_service_descriptor(d1
, 0, pk1
, &encoded
, &len
));
2944 d2
= rend_parse_service_descriptor(encoded
, len
);
2947 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
2948 test_eq(d2
->timestamp
, now
);
2949 test_eq(d2
->version
, 0);
2950 test_eq(d2
->protocols
, 1);
2951 test_eq(d2
->n_intro_points
, 3);
2952 test_streq(d2
->intro_points
[0], "tom");
2953 test_streq(d2
->intro_points
[1], "crow");
2954 test_streq(d2
->intro_points
[2], "joel");
2955 test_eq(NULL
, d2
->intro_point_extend_info
);
2957 rend_service_descriptor_free(d1
);
2958 rend_service_descriptor_free(d2
);
2961 /* Test versioned descriptor. */
2962 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
2963 d1
->pk
= crypto_pk_dup_key(pk1
);
2965 d1
->timestamp
= now
;
2966 d1
->n_intro_points
= 2;
2969 d1
->intro_points
= tor_malloc(sizeof(char*)*2);
2970 d1
->intro_point_extend_info
= tor_malloc(sizeof(extend_info_t
*)*2);
2971 d1
->intro_points
[0] = tor_strdup("tom");
2972 d1
->intro_points
[1] = tor_strdup("crow");
2973 d1
->intro_point_extend_info
[0] = tor_malloc_zero(sizeof(extend_info_t
));
2974 strlcpy(d1
->intro_point_extend_info
[0]->nickname
, "tom", 4);
2975 d1
->intro_point_extend_info
[0]->addr
= 1234;
2976 d1
->intro_point_extend_info
[0]->port
= 4567;
2977 d1
->intro_point_extend_info
[0]->onion_key
= crypto_pk_dup_key(pk1
);
2978 memset(d1
->intro_point_extend_info
[0]->identity_digest
, 'a', DIGEST_LEN
);
2980 d1
->intro_point_extend_info
[1] = tor_malloc_zero(sizeof(extend_info_t
));
2981 strlcpy(d1
->intro_point_extend_info
[1]->nickname
, "crow", 5);
2982 d1
->intro_point_extend_info
[1]->addr
= 6060842;
2983 d1
->intro_point_extend_info
[1]->port
= 8000;
2984 d1
->intro_point_extend_info
[1]->onion_key
= crypto_pk_dup_key(pk2
);
2985 memset(d1
->intro_point_extend_info
[1]->identity_digest
, 'b', DIGEST_LEN
);
2987 test_assert(! rend_encode_service_descriptor(d1
, 1, pk1
, &encoded
, &len
));
2988 d2
= rend_parse_service_descriptor(encoded
, len
);
2991 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
2992 test_eq(d2
->timestamp
, now
);
2993 test_eq(d2
->version
, 1);
2994 test_eq(d2
->protocols
, 60);
2995 test_eq(d2
->n_intro_points
, 2);
2996 test_streq(d2
->intro_points
[0], d2
->intro_point_extend_info
[0]->nickname
);
2997 test_streq(d2
->intro_points
[1], d2
->intro_point_extend_info
[1]->nickname
);
2998 test_eq(d2
->intro_point_extend_info
[0]->addr
, 1234);
2999 test_eq(d2
->intro_point_extend_info
[0]->port
, 4567);
3000 test_assert(!crypto_pk_cmp_keys(pk1
,
3001 d2
->intro_point_extend_info
[0]->onion_key
));
3002 test_memeq(d2
->intro_point_extend_info
[0]->identity_digest
,
3003 d1
->intro_point_extend_info
[0]->identity_digest
, DIGEST_LEN
);
3004 test_eq(d2
->intro_point_extend_info
[1]->addr
, 6060842);
3005 test_eq(d2
->intro_point_extend_info
[1]->port
, 8000);
3007 test_memeq(d2
->intro_point_extend_info
[1]->identity_digest
,
3008 d1
->intro_point_extend_info
[1]->identity_digest
, DIGEST_LEN
);
3010 test_assert(BAD_HOSTNAME
== parse_extended_hostname(address1
));
3011 test_assert(ONION_HOSTNAME
== parse_extended_hostname(address2
));
3012 test_assert(EXIT_HOSTNAME
== parse_extended_hostname(address3
));
3013 test_assert(NORMAL_HOSTNAME
== parse_extended_hostname(address4
));
3015 rend_service_descriptor_free(d1
);
3016 rend_service_descriptor_free(d2
);
3017 crypto_free_pk_env(pk1
);
3018 crypto_free_pk_env(pk2
);
3026 crypto_cipher_env_t
*c
;
3027 struct timeval start
, end
;
3028 const int iters
= 100000;
3030 c
= crypto_new_cipher_env();
3031 crypto_cipher_generate_key(c
);
3032 crypto_cipher_encrypt_init_cipher(c
);
3033 for (len
= 1; len
<= 8192; len
*= 2) {
3034 b1
= tor_malloc_zero(len
);
3035 b2
= tor_malloc_zero(len
);
3036 tor_gettimeofday(&start
);
3037 for (i
= 0; i
< iters
; ++i
) {
3038 crypto_cipher_encrypt(c
, b1
, b2
, len
);
3040 tor_gettimeofday(&end
);
3043 nsec
= (uint64_t) tv_udiff(&start
,&end
);
3045 nsec
/= (iters
*len
);
3046 printf("%d bytes: "U64_FORMAT
" nsec per byte\n", len
,
3047 U64_PRINTF_ARG(nsec
));
3049 crypto_free_cipher_env(c
);
3053 test_util_mempool(void)
3056 smartlist_t
*allocated
;
3059 pool
= mp_pool_new(1, 100);
3060 test_assert(pool
->new_chunk_capacity
>= 100);
3061 test_assert(pool
->item_alloc_size
>= sizeof(void*)+1);
3062 mp_pool_destroy(pool
);
3064 pool
= mp_pool_new(241, 2500);
3065 test_assert(pool
->new_chunk_capacity
>= 10);
3066 test_assert(pool
->item_alloc_size
>= sizeof(void*)+241);
3067 test_eq(pool
->item_alloc_size
& 0x03, 0);
3068 test_assert(pool
->new_chunk_capacity
< 60);
3070 allocated
= smartlist_create();
3071 for (i
= 0; i
< 100000; ++i
) {
3072 if (smartlist_len(allocated
) < 20 || crypto_rand_int(2)) {
3073 void *m
= mp_pool_get(pool
);
3074 memset(m
, 0x09, 241);
3075 smartlist_add(allocated
, m
);
3076 //printf("%d: %p\n", i, m);
3077 //mp_pool_assert_ok(pool);
3079 int idx
= crypto_rand_int(smartlist_len(allocated
));
3080 void *m
= smartlist_get(allocated
, idx
);
3081 //printf("%d: free %p\n", i, m);
3082 smartlist_del(allocated
, idx
);
3084 //mp_pool_assert_ok(pool);
3086 if (crypto_rand_int(777)==0)
3087 mp_pool_clean(pool
, -1);
3090 mp_pool_assert_ok(pool
);
3092 SMARTLIST_FOREACH(allocated
, void *, m
, mp_pool_release(m
));
3093 mp_pool_assert_ok(pool
);
3094 mp_pool_clean(pool
, 0);
3095 mp_pool_assert_ok(pool
);
3096 mp_pool_destroy(pool
);
3097 smartlist_free(allocated
);
3100 #define ENT(x) { #x, test_ ## x, 0, 0 }
3101 #define SUBENT(x,y) { #x "/" #y, test_ ## x ## _ ## y, 1, 0 }
3104 const char *test_name
;
3105 void (*test_fn
)(void);
3112 SUBENT(crypto
, s2k
),
3114 SUBENT(util
, ip6_helpers
),
3116 SUBENT(util
, smartlist
),
3117 SUBENT(util
, bitarray
),
3118 SUBENT(util
, mempool
),
3119 SUBENT(util
, strmap
),
3120 SUBENT(util
, control_formats
),
3121 SUBENT(util
, pqueue
),
3123 SUBENT(util
, threads
),
3124 SUBENT(util
, dirvote_helpers
),
3125 ENT(onion_handshake
),
3127 ENT(v3_networkstatus
),
3130 { NULL
, NULL
, 0, 0 },
3133 static void syntax(void) ATTR_NORETURN
;
3139 " test [-v|--verbose] [--warn|--notice|--info|--debug]\n"
3141 "Recognized tests are:\n");
3142 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3143 printf(" %s\n", test_array
[i
].test_name
);
3150 main(int c
, char**v
)
3152 or_options_t
*options
= options_new();
3153 char *errmsg
= NULL
;
3155 int verbose
= 0, any_selected
= 0;
3156 int loglevel
= LOG_ERR
;
3158 for (i
= 1; i
< c
; ++i
) {
3159 if (!strcmp(v
[i
], "-v") || !strcmp(v
[i
], "--verbose"))
3161 else if (!strcmp(v
[i
], "--warn"))
3162 loglevel
= LOG_WARN
;
3163 else if (!strcmp(v
[i
], "--notice"))
3164 loglevel
= LOG_NOTICE
;
3165 else if (!strcmp(v
[i
], "--info"))
3166 loglevel
= LOG_INFO
;
3167 else if (!strcmp(v
[i
], "--debug"))
3168 loglevel
= LOG_DEBUG
;
3169 else if (!strcmp(v
[i
], "--help") || !strcmp(v
[i
], "-h") || v
[i
][0] == '-')
3173 for (j
= 0; test_array
[j
].test_name
; ++j
) {
3174 if (!strcmp(v
[i
], test_array
[j
].test_name
) ||
3175 (test_array
[j
].is_subent
&&
3176 !strcmpstart(test_array
[j
].test_name
, v
[i
]) &&
3177 test_array
[j
].test_name
[strlen(v
[i
])] == '/') ||
3178 (v
[i
][0] == '=' && !strcmp(v
[i
]+1, test_array
[j
].test_name
))) {
3179 test_array
[j
].selected
= 1;
3185 printf("Unknown test: %s\n", v
[i
]);
3191 if (!any_selected
) {
3192 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3193 test_array
[i
].selected
= 1;
3197 add_stream_log(loglevel
, LOG_ERR
, "", stdout
);
3199 options
->command
= CMD_RUN_UNITTESTS
;
3203 options_init(options
);
3204 options
->DataDirectory
= tor_strdup(temp_dir
);
3205 if (set_options(options
, &errmsg
) < 0) {
3206 printf("Failed to set initial options: %s\n", errmsg
);
3218 atexit(remove_directory
);
3220 printf("Running Tor unit tests on %s\n", get_uname());
3222 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3223 if (!test_array
[i
].selected
)
3225 if (!test_array
[i
].is_subent
) {
3226 printf("\n============================== %s\n",test_array
[i
].test_name
);
3227 } else if (test_array
[i
].is_subent
&& verbose
) {
3228 printf("\n%s", test_array
[i
].test_name
);
3230 test_array
[i
].test_fn();