1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2008, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 const char test_c_id
[] =
9 const char tor_svn_revision
[] = "";
13 * \brief Unit tests for many pieces of the lower level Tor modules.
29 /* These macros pull in declarations for some functions and structures that
30 * are typically file-private. */
31 #define CONFIG_PRIVATE
32 #define CONTROL_PRIVATE
33 #define CRYPTO_PRIVATE
34 #define DIRSERV_PRIVATE
35 #define DIRVOTE_PRIVATE
37 #define MEMPOOL_PRIVATE
38 #define ROUTER_PRIVATE
47 static char temp_dir
[256];
52 static int is_setup
= 0;
58 tor_snprintf(temp_dir
, sizeof(temp_dir
),
59 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
62 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
63 r
= mkdir(temp_dir
, 0700);
66 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
74 get_fname(const char *name
)
76 static char buf
[1024];
78 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
83 remove_directory(void)
85 smartlist_t
*elements
= tor_listdir(temp_dir
);
87 SMARTLIST_FOREACH(elements
, const char *, cp
,
89 size_t len
= strlen(cp
)+strlen(temp_dir
)+16;
90 char *tmp
= tor_malloc(len
);
91 tor_snprintf(tmp
, len
, "%s"PATH_SEPARATOR
"%s", temp_dir
, cp
);
95 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
96 smartlist_free(elements
);
101 static crypto_pk_env_t
*
104 static crypto_pk_env_t
*pregen
[5] = {NULL
, NULL
, NULL
, NULL
, NULL
};
105 tor_assert(idx
< (int)(sizeof(pregen
)/sizeof(pregen
[0])));
107 pregen
[idx
] = crypto_new_pk_env();
108 tor_assert(!crypto_pk_generate_key(pregen
[idx
]));
110 return crypto_pk_dup_key(pregen
[idx
]);
127 if (!(buf
= buf_new()))
130 //test_eq(buf_capacity(buf), 4096);
131 test_eq(buf_datalen(buf
), 0);
134 * General pointer frobbing
136 for (j
=0;j
<256;++j
) {
139 write_to_buf(str
, 256, buf
);
140 write_to_buf(str
, 256, buf
);
141 test_eq(buf_datalen(buf
), 512);
142 fetch_from_buf(str2
, 200, buf
);
143 test_memeq(str
, str2
, 200);
144 test_eq(buf_datalen(buf
), 312);
145 memset(str2
, 0, sizeof(str2
));
147 fetch_from_buf(str2
, 256, buf
);
148 test_memeq(str
+200, str2
, 56);
149 test_memeq(str
, str2
+56, 200);
150 test_eq(buf_datalen(buf
), 56);
151 memset(str2
, 0, sizeof(str2
));
152 /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
153 * another 3584 bytes, we hit the end. */
155 write_to_buf(str
, 256, buf
);
158 test_eq(buf_datalen(buf
), 3896);
159 fetch_from_buf(str2
, 56, buf
);
160 test_eq(buf_datalen(buf
), 3840);
161 test_memeq(str
+200, str2
, 56);
163 memset(str2
, 0, sizeof(str2
));
164 fetch_from_buf(str2
, 256, buf
);
165 test_memeq(str
, str2
, 256);
167 test_eq(buf_datalen(buf
), 0);
170 /* Okay, now make sure growing can work. */
171 buf
= buf_new_with_capacity(16);
172 //test_eq(buf_capacity(buf), 16);
173 write_to_buf(str
+1, 255, buf
);
174 //test_eq(buf_capacity(buf), 256);
175 fetch_from_buf(str2
, 254, buf
);
176 test_memeq(str
+1, str2
, 254);
177 //test_eq(buf_capacity(buf), 256);
179 write_to_buf(str
, 32, buf
);
180 //test_eq(buf_capacity(buf), 256);
182 write_to_buf(str
, 256, buf
);
184 //test_eq(buf_capacity(buf), 512);
185 test_eq(buf_datalen(buf
), 33+256);
186 fetch_from_buf(str2
, 33, buf
);
187 test_eq(*str2
, str
[255]);
189 test_memeq(str2
+1, str
, 32);
190 //test_eq(buf_capacity(buf), 512);
191 test_eq(buf_datalen(buf
), 256);
192 fetch_from_buf(str2
, 256, buf
);
193 test_memeq(str
, str2
, 256);
195 /* now try shrinking: case 1. */
197 buf
= buf_new_with_capacity(33668);
199 write_to_buf(str
,255, buf
);
201 //test_eq(buf_capacity(buf), 33668);
202 test_eq(buf_datalen(buf
), 17085);
203 for (j
=0; j
< 40; ++j
) {
204 fetch_from_buf(str2
, 255,buf
);
205 test_memeq(str2
, str
, 255);
208 /* now try shrinking: case 2. */
210 buf
= buf_new_with_capacity(33668);
212 write_to_buf(str
,255, buf
);
214 for (j
=0; j
< 20; ++j
) {
215 fetch_from_buf(str2
, 255,buf
);
216 test_memeq(str2
, str
, 255);
219 write_to_buf(str
,255, buf
);
221 //test_eq(buf_capacity(buf),33668);
222 for (j
=0; j
< 120; ++j
) {
223 fetch_from_buf(str2
, 255,buf
);
224 test_memeq(str2
, str
, 255);
227 /* Move from buf to buf. */
229 buf
= buf_new_with_capacity(4096);
230 buf2
= buf_new_with_capacity(4096);
232 write_to_buf(str
, 255, buf
);
233 test_eq(buf_datalen(buf
), 25500);
234 for (j
=0;j
<100;++j
) {
236 move_buf_to_buf(buf2
, buf
, &r
);
239 test_eq(buf_datalen(buf
), 24500);
240 test_eq(buf_datalen(buf2
), 1000);
242 fetch_from_buf(str2
, 255, buf2
);
243 test_memeq(str2
, str
, 255);
245 r
= 8192; /*big move*/
246 move_buf_to_buf(buf2
, buf
, &r
);
248 r
= 30000; /* incomplete move */
249 move_buf_to_buf(buf2
, buf
, &r
);
252 fetch_from_buf(str2
, 255, buf2
);
253 test_memeq(str2
, str
, 255);
267 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
271 s
= open(get_fname("data"), O_RDONLY
, 0);
273 errno
= 0; /* XXXX */
274 i
= read_to_buf(s
, 10, buf
, &eof
);
275 printf("%s\n", strerror(errno
));
278 //test_eq(buf_capacity(buf), 4096);
279 test_eq(buf_datalen(buf
), 10);
281 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10);
283 /* Test reading 0 bytes. */
284 i
= read_to_buf(s
, 0, buf
, &eof
);
285 //test_eq(buf_capacity(buf), 512*1024);
286 test_eq(buf_datalen(buf
), 10);
290 /* Now test when buffer is filled exactly. */
291 buf2
= buf_new_with_capacity(6);
292 i
= read_to_buf(s
, 6, buf2
, &eof
);
293 //test_eq(buf_capacity(buf2), 6);
294 test_eq(buf_datalen(buf2
), 6);
297 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
300 /* Now test when buffer is filled with more data to read. */
301 buf2
= buf_new_with_capacity(32);
302 i
= read_to_buf(s
, 128, buf2
, &eof
);
303 //test_eq(buf_capacity(buf2), 128);
304 test_eq(buf_datalen(buf2
), 32);
309 /* Now read to eof. */
310 test_assert(buf_capacity(buf
) > 256);
311 i
= read_to_buf(s
, 1024, buf
, &eof
);
312 test_eq(i
, (256-32-10-6));
313 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
314 test_eq(buf_datalen(buf
), 256-6-32);
315 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10); /* XXX Check rest. */
318 i
= read_to_buf(s
, 1024, buf
, &eof
);
320 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
321 test_eq(buf_datalen(buf
), 256-6-32);
330 crypto_dh_env_t
*dh1
, *dh2
;
337 dh1
= crypto_dh_new();
338 dh2
= crypto_dh_new();
339 test_eq(crypto_dh_get_bytes(dh1
), DH_BYTES
);
340 test_eq(crypto_dh_get_bytes(dh2
), DH_BYTES
);
342 memset(p1
, 0, DH_BYTES
);
343 memset(p2
, 0, DH_BYTES
);
344 test_memeq(p1
, p2
, DH_BYTES
);
345 test_assert(! crypto_dh_get_public(dh1
, p1
, DH_BYTES
));
346 test_memneq(p1
, p2
, DH_BYTES
);
347 test_assert(! crypto_dh_get_public(dh2
, p2
, DH_BYTES
));
348 test_memneq(p1
, p2
, DH_BYTES
);
350 memset(s1
, 0, DH_BYTES
);
351 memset(s2
, 0xFF, DH_BYTES
);
352 s1len
= crypto_dh_compute_secret(dh1
, p2
, DH_BYTES
, s1
, 50);
353 s2len
= crypto_dh_compute_secret(dh2
, p1
, DH_BYTES
, s2
, 50);
354 test_assert(s1len
> 0);
355 test_eq(s1len
, s2len
);
356 test_memeq(s1
, s2
, s1len
);
365 crypto_cipher_env_t
*env1
, *env2
;
366 crypto_pk_env_t
*pk1
, *pk2
;
367 char *data1
, *data2
, *data3
, *cp
;
368 int i
, j
, p
, len
, idx
;
371 data1
= tor_malloc(1024);
372 data2
= tor_malloc(1024);
373 data3
= tor_malloc(1024);
374 test_assert(data1
&& data2
&& data3
);
377 test_assert(! crypto_seed_rng());
378 crypto_rand(data1
, 100);
379 crypto_rand(data2
, 100);
380 test_memneq(data1
,data2
,100);
382 /* Now, test encryption and decryption with stream cipher. */
384 for (i
= 1023; i
>0; i
-= 35)
385 strncat(data1
, "Now is the time for all good onions", i
);
387 memset(data2
, 0, 1024);
388 memset(data3
, 0, 1024);
389 env1
= crypto_new_cipher_env();
391 env2
= crypto_new_cipher_env();
393 j
= crypto_cipher_generate_key(env1
);
394 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
395 crypto_cipher_encrypt_init_cipher(env1
);
396 crypto_cipher_decrypt_init_cipher(env2
);
398 /* Try encrypting 512 chars. */
399 crypto_cipher_encrypt(env1
, data2
, data1
, 512);
400 crypto_cipher_decrypt(env2
, data3
, data2
, 512);
401 test_memeq(data1
, data3
, 512);
402 test_memneq(data1
, data2
, 512);
404 /* Now encrypt 1 at a time, and get 1 at a time. */
405 for (j
= 512; j
< 560; ++j
) {
406 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 1);
408 for (j
= 512; j
< 560; ++j
) {
409 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 1);
411 test_memeq(data1
, data3
, 560);
412 /* Now encrypt 3 at a time, and get 5 at a time. */
413 for (j
= 560; j
< 1024-5; j
+= 3) {
414 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 3);
416 for (j
= 560; j
< 1024-5; j
+= 5) {
417 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 5);
419 test_memeq(data1
, data3
, 1024-5);
420 /* Now make sure that when we encrypt with different chunk sizes, we get
422 crypto_free_cipher_env(env2
);
424 memset(data3
, 0, 1024);
425 env2
= crypto_new_cipher_env();
427 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
428 crypto_cipher_encrypt_init_cipher(env2
);
429 for (j
= 0; j
< 1024-16; j
+= 17) {
430 crypto_cipher_encrypt(env2
, data3
+j
, data1
+j
, 17);
432 for (j
= 0; j
< 1024-16; ++j
) {
433 if (data2
[j
] != data3
[j
]) {
434 printf("%d: %d\t%d\n", j
, (int) data2
[j
], (int) data3
[j
]);
437 test_memeq(data2
, data3
, 1024-16);
438 crypto_free_cipher_env(env1
);
439 crypto_free_cipher_env(env2
);
441 /* Test vectors for stream ciphers. */
442 /* XXXX Look up some test vectors for the ciphers and make sure we match. */
444 /* Test SHA-1 with a test vector from the specification. */
445 i
= crypto_digest(data1
, "abc", 3);
447 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
448 "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
450 /* Test HMAC-SHA-1 with test cases from RFC2202. */
457 memset(key
, 0x0b, 20);
458 crypto_hmac_sha1(digest
, key
, 20, "Hi There", 8);
459 test_streq(hex_str(digest
, 20),
460 "B617318655057264E28BC0B6FB378C8EF146BE00");
463 crypto_hmac_sha1(digest
, "Jefe", 4, "what do ya want for nothing?", 28);
464 test_streq(hex_str(digest
, 20),
465 "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79");
468 base16_decode(key
, 25,
469 "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
470 memset(data
, 0xcd, 50);
471 crypto_hmac_sha1(digest
, key
, 25, data
, 50);
472 test_streq(hex_str(digest
, 20),
473 "4C9007F4026250C6BC8414F9BF50C86C2D7235DA");
476 memset(key
, 0xaa, 80);
477 crypto_hmac_sha1(digest
, key
, 80,
478 "Test Using Larger Than Block-Size Key - Hash Key First",
480 test_streq(hex_str(digest
, 20),
481 "AA4AE5E15272D00E95705637CE8A3B55ED402112");
485 /* Public-key ciphers */
486 pk1
= pk_generate(0);
487 pk2
= crypto_new_pk_env();
488 test_assert(pk1
&& pk2
);
489 test_assert(! crypto_pk_write_public_key_to_string(pk1
, &cp
, &size
));
490 test_assert(! crypto_pk_read_public_key_from_string(pk2
, cp
, size
));
491 test_eq(0, crypto_pk_cmp_keys(pk1
, pk2
));
494 test_eq(128, crypto_pk_keysize(pk1
));
495 test_eq(128, crypto_pk_keysize(pk2
));
497 test_eq(128, crypto_pk_public_encrypt(pk2
, data1
, "Hello whirled.", 15,
498 PK_PKCS1_OAEP_PADDING
));
499 test_eq(128, crypto_pk_public_encrypt(pk1
, data2
, "Hello whirled.", 15,
500 PK_PKCS1_OAEP_PADDING
));
501 /* oaep padding should make encryption not match */
502 test_memneq(data1
, data2
, 128);
503 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data1
, 128,
504 PK_PKCS1_OAEP_PADDING
,1));
505 test_streq(data3
, "Hello whirled.");
506 memset(data3
, 0, 1024);
507 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
508 PK_PKCS1_OAEP_PADDING
,1));
509 test_streq(data3
, "Hello whirled.");
510 /* Can't decrypt with public key. */
511 test_eq(-1, crypto_pk_private_decrypt(pk2
, data3
, data2
, 128,
512 PK_PKCS1_OAEP_PADDING
,1));
513 /* Try again with bad padding */
514 memcpy(data2
+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
515 test_eq(-1, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
516 PK_PKCS1_OAEP_PADDING
,1));
518 /* File operations: save and load private key */
519 test_assert(! crypto_pk_write_private_key_to_filename(pk1
,
520 get_fname("pkey1")));
522 test_assert(! crypto_pk_read_private_key_from_filename(pk2
,
523 get_fname("pkey1")));
524 test_eq(15, crypto_pk_private_decrypt(pk2
, data3
, data1
, 128,
525 PK_PKCS1_OAEP_PADDING
,1));
527 /* Now try signing. */
528 strlcpy(data1
, "Ossifrage", 1024);
529 test_eq(128, crypto_pk_private_sign(pk1
, data2
, data1
, 10));
530 test_eq(10, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
531 test_streq(data3
, "Ossifrage");
532 /* Try signing digests. */
533 test_eq(128, crypto_pk_private_sign_digest(pk1
, data2
, data1
, 10));
534 test_eq(20, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
535 test_eq(0, crypto_pk_public_checksig_digest(pk1
, data1
, 10, data2
, 128));
536 test_eq(-1, crypto_pk_public_checksig_digest(pk1
, data1
, 11, data2
, 128));
537 /*XXXX test failed signing*/
540 crypto_free_pk_env(pk2
);
542 i
= crypto_pk_asn1_encode(pk1
, data1
, 1024);
544 pk2
= crypto_pk_asn1_decode(data1
, i
);
545 test_assert(crypto_pk_cmp_keys(pk1
,pk2
) == 0);
547 /* Try with hybrid encryption wrappers. */
548 crypto_rand(data1
, 1024);
549 for (i
= 0; i
< 3; ++i
) {
550 for (j
= 85; j
< 140; ++j
) {
551 memset(data2
,0,1024);
552 memset(data3
,0,1024);
553 if (i
== 0 && j
< 129)
555 p
= (i
==0)?PK_NO_PADDING
:
556 (i
==1)?PK_PKCS1_PADDING
:PK_PKCS1_OAEP_PADDING
;
557 len
= crypto_pk_public_hybrid_encrypt(pk1
,data2
,data1
,j
,p
,0);
559 len
= crypto_pk_private_hybrid_decrypt(pk1
,data3
,data2
,len
,p
,1);
561 test_memeq(data1
,data3
,j
);
564 crypto_free_pk_env(pk1
);
565 crypto_free_pk_env(pk2
);
568 memset(data1
, 6, 1024);
569 for (idx
= 0; idx
< 10; ++idx
) {
570 i
= base64_encode(data2
, 1024, data1
, idx
);
571 j
= base64_decode(data3
, 1024, data2
, i
);
573 test_memeq(data3
, data1
, idx
);
576 strlcpy(data1
, "Test string that contains 35 chars.", 1024);
577 strlcat(data1
, " 2nd string that contains 35 chars.", 1024);
579 i
= base64_encode(data2
, 1024, data1
, 71);
580 j
= base64_decode(data3
, 1024, data2
, i
);
582 test_streq(data3
, data1
);
583 test_assert(data2
[i
] == '\0');
585 crypto_rand(data1
, DIGEST_LEN
);
586 memset(data2
, 100, 1024);
587 digest_to_base64(data2
, data1
);
588 test_eq(BASE64_DIGEST_LEN
, strlen(data2
));
589 test_eq(100, data2
[BASE64_DIGEST_LEN
+2]);
590 memset(data3
, 99, 1024);
591 test_eq(digest_from_base64(data3
, data2
), 0);
592 test_memeq(data1
, data3
, DIGEST_LEN
);
593 test_eq(99, data3
[DIGEST_LEN
+1]);
596 strlcpy(data1
, "5chrs", 1024);
597 /* bit pattern is: [35 63 68 72 73] ->
598 * [00110101 01100011 01101000 01110010 01110011]
599 * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
601 base32_encode(data2
, 9, data1
, 5);
602 test_streq(data2
, "gvrwq4tt");
604 strlcpy(data1
, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
605 base32_encode(data2
, 30, data1
, 10);
606 test_streq(data2
, "772w2rfobvomsywe");
609 strlcpy(data1
, "6chrs\xff", 1024);
610 base16_encode(data2
, 13, data1
, 6);
611 test_streq(data2
, "3663687273FF");
613 strlcpy(data1
, "f0d678affc000100", 1024);
614 i
= base16_decode(data2
, 8, data1
, 16);
616 test_memeq(data2
, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
618 /* now try some failing base16 decodes */
619 test_eq(-1, base16_decode(data2
, 8, data1
, 15)); /* odd input len */
620 test_eq(-1, base16_decode(data2
, 7, data1
, 16)); /* dest too short */
621 strlcpy(data1
, "f0dz!8affc000100", 1024);
622 test_eq(-1, base16_decode(data2
, 8, data1
, 16));
630 test_crypto_s2k(void)
637 memset(buf
, 0, sizeof(buf
));
638 memset(buf2
, 0, sizeof(buf2
));
639 buf3
= tor_malloc(65536);
640 memset(buf3
, 0, 65536);
642 secret_to_key(buf
+9, 20, "", 0, buf
);
643 crypto_digest(buf2
+9, buf3
, 1024);
644 test_memeq(buf
, buf2
, 29);
646 memcpy(buf
,"vrbacrda",8);
647 memcpy(buf2
,"vrbacrda",8);
650 secret_to_key(buf
+9, 20, "12345678", 8, buf
);
651 for (i
= 0; i
< 65536; i
+= 16) {
652 memcpy(buf3
+i
, "vrbacrda12345678", 16);
654 crypto_digest(buf2
+9, buf3
, 65536);
655 test_memeq(buf
, buf2
, 29);
660 _compare_strs(const void **a
, const void **b
)
662 const char *s1
= *a
, *s2
= *b
;
663 return strcmp(s1
, s2
);
667 _compare_without_first_ch(const void *a
, const void **b
)
669 const char *s1
= a
, *s2
= *b
;
670 return strcasecmp(s1
+1, s2
);
676 struct timeval start
, end
;
678 char timestr
[RFC1123_TIME_LEN
+1];
688 start
.tv_usec
= 5000;
693 test_eq(0L, tv_udiff(&start
, &end
));
697 test_assert(tv_cmp(&start
, &end
)<0);
698 test_assert(tv_cmp(&end
, &start
)>0);
699 test_assert(tv_cmp(&end
, &end
)==0);
701 test_eq(2000L, tv_udiff(&start
, &end
));
705 test_eq(1002000L, tv_udiff(&start
, &end
));
709 test_eq(995000L, tv_udiff(&start
, &end
));
713 test_eq(-1005000L, tv_udiff(&start
, &end
));
715 tv_addms(&end
, 5090);
716 test_eq(end
.tv_sec
, 9);
717 test_eq(end
.tv_usec
, 90000);
719 end
.tv_usec
= 999990;
722 tv_add(&start
, &end
);
723 test_eq(start
.tv_sec
, 11);
724 test_eq(start
.tv_usec
, 490);
726 /* The test values here are confirmed to be correct on a platform
727 * with a working timegm. */
728 a_time
.tm_year
= 2003-1900;
734 test_eq((time_t) 1062224095UL, tor_timegm(&a_time
));
735 a_time
.tm_year
= 2004-1900; /* Try a leap year, after feb. */
736 test_eq((time_t) 1093846495UL, tor_timegm(&a_time
));
737 a_time
.tm_mon
= 1; /* Try a leap year, in feb. */
739 test_eq((time_t) 1076393695UL, tor_timegm(&a_time
));
741 format_rfc1123_time(timestr
, 0);
742 test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr
);
743 format_rfc1123_time(timestr
, (time_t)1091580502UL);
744 test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr
);
747 i
= parse_rfc1123_time(timestr
, &t_res
);
749 test_eq(t_res
, (time_t)1091580502UL);
750 test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res
));
751 tor_gettimeofday(&start
);
753 /* Test tor_strstrip() */
754 strlcpy(buf
, "Testing 1 2 3", sizeof(buf
));
755 test_eq(0, tor_strstrip(buf
, ",!"));
756 test_streq(buf
, "Testing 1 2 3");
757 strlcpy(buf
, "!Testing 1 2 3?", sizeof(buf
));
758 test_eq(5, tor_strstrip(buf
, "!? "));
759 test_streq(buf
, "Testing123");
761 /* Test tor_strpartition() */
762 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefghi", "##", 3));
763 test_streq(buf
, "abc##def##ghi");
765 /* Test parse_addr_port */
766 cp
= NULL
; u32
= 3; u16
= 3;
767 test_assert(!parse_addr_port(LOG_WARN
, "1.2.3.4", &cp
, &u32
, &u16
));
768 test_streq(cp
, "1.2.3.4");
769 test_eq(u32
, 0x01020304u
);
772 test_assert(!parse_addr_port(LOG_WARN
, "4.3.2.1:99", &cp
, &u32
, &u16
));
773 test_streq(cp
, "4.3.2.1");
774 test_eq(u32
, 0x04030201u
);
777 test_assert(!parse_addr_port(LOG_WARN
, "nonexistent.address:4040",
779 test_streq(cp
, "nonexistent.address");
782 test_assert(!parse_addr_port(LOG_WARN
, "localhost:9999", &cp
, &u32
, &u16
));
783 test_streq(cp
, "localhost");
784 test_eq(u32
, 0x7f000001u
);
788 test_assert(!parse_addr_port(LOG_WARN
, "localhost", NULL
, &u32
, &u16
));
790 test_eq(u32
, 0x7f000001u
);
793 test_eq(0, addr_mask_get_bits(0x0u
));
794 test_eq(32, addr_mask_get_bits(0xFFFFFFFFu
));
795 test_eq(16, addr_mask_get_bits(0xFFFF0000u
));
796 test_eq(31, addr_mask_get_bits(0xFFFFFFFEu
));
797 test_eq(1, addr_mask_get_bits(0x80000000u
));
799 /* Test tor_parse_long. */
800 test_eq(10L, tor_parse_long("10",10,0,100,NULL
,NULL
));
801 test_eq(0L, tor_parse_long("10",10,50,100,NULL
,NULL
));
802 test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL
,NULL
));
804 /* Test tor_parse_ulong */
805 test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL
,NULL
));
806 test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL
,NULL
));
808 /* Test tor_parse_uint64. */
809 test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i
, &cp
));
811 test_streq(cp
, " x");
812 test_assert(U64_LITERAL(12345678901) ==
813 tor_parse_uint64("12345678901",10,0,UINT64_MAX
, &i
, &cp
));
816 test_assert(U64_LITERAL(0) ==
817 tor_parse_uint64("12345678901",10,500,INT32_MAX
, &i
, &cp
));
820 /* Test printf with uint64 */
821 tor_snprintf(buf
, sizeof(buf
), "x!"U64_FORMAT
"!x",
822 U64_PRINTF_ARG(U64_LITERAL(12345678901)));
823 test_streq(buf
, "x!12345678901!x");
825 /* Test parse_config_line_from_str */
826 strlcpy(buf
, "k v\n" " key value with spaces \n" "keykey val\n"
828 "k3 \n" "\n" " \n" "#comment\n"
829 "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
830 "kseven \"a quoted 'string\"\n"
831 "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
835 str
= parse_config_line_from_str(str
, &k
, &v
);
838 tor_free(k
); tor_free(v
);
839 test_assert(!strcmpstart(str
, "key value with"));
841 str
= parse_config_line_from_str(str
, &k
, &v
);
842 test_streq(k
, "key");
843 test_streq(v
, "value with spaces");
844 tor_free(k
); tor_free(v
);
845 test_assert(!strcmpstart(str
, "keykey"));
847 str
= parse_config_line_from_str(str
, &k
, &v
);
848 test_streq(k
, "keykey");
849 test_streq(v
, "val");
850 tor_free(k
); tor_free(v
);
851 test_assert(!strcmpstart(str
, "k2\n"));
853 str
= parse_config_line_from_str(str
, &k
, &v
);
856 tor_free(k
); tor_free(v
);
857 test_assert(!strcmpstart(str
, "k3 \n"));
859 str
= parse_config_line_from_str(str
, &k
, &v
);
862 tor_free(k
); tor_free(v
);
863 test_assert(!strcmpstart(str
, "#comment"));
865 str
= parse_config_line_from_str(str
, &k
, &v
);
868 tor_free(k
); tor_free(v
);
869 test_assert(!strcmpstart(str
, "k5#abc"));
871 str
= parse_config_line_from_str(str
, &k
, &v
);
874 tor_free(k
); tor_free(v
);
875 test_assert(!strcmpstart(str
, "k6"));
877 str
= parse_config_line_from_str(str
, &k
, &v
);
879 test_streq(v
, "val");
880 tor_free(k
); tor_free(v
);
881 test_assert(!strcmpstart(str
, "kseven"));
883 str
= parse_config_line_from_str(str
, &k
, &v
);
884 test_streq(k
, "kseven");
885 test_streq(v
, "a quoted 'string");
886 tor_free(k
); tor_free(v
);
887 test_assert(!strcmpstart(str
, "k8 "));
889 str
= parse_config_line_from_str(str
, &k
, &v
);
891 test_streq(v
, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
892 tor_free(k
); tor_free(v
);
895 /* Test for strcmpstart and strcmpend. */
896 test_assert(strcmpstart("abcdef", "abcdef")==0);
897 test_assert(strcmpstart("abcdef", "abc")==0);
898 test_assert(strcmpstart("abcdef", "abd")<0);
899 test_assert(strcmpstart("abcdef", "abb")>0);
900 test_assert(strcmpstart("ab", "abb")<0);
902 test_assert(strcmpend("abcdef", "abcdef")==0);
903 test_assert(strcmpend("abcdef", "def")==0);
904 test_assert(strcmpend("abcdef", "deg")<0);
905 test_assert(strcmpend("abcdef", "dee")>0);
906 test_assert(strcmpend("ab", "abb")<0);
908 test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
909 test_assert(strcasecmpend("abcdef", "dEF")==0);
910 test_assert(strcasecmpend("abcDEf", "deg")<0);
911 test_assert(strcasecmpend("abcdef", "DEE")>0);
912 test_assert(strcasecmpend("ab", "abB")<0);
914 /* Test mem_is_zero */
917 test_assert(tor_digest_is_zero(buf
));
918 test_assert(tor_mem_is_zero(buf
, 10));
919 test_assert(tor_mem_is_zero(buf
, 20));
920 test_assert(tor_mem_is_zero(buf
, 128));
921 test_assert(!tor_mem_is_zero(buf
, 129));
923 test_assert(!tor_mem_is_zero(buf
, 128));
925 test_assert(!tor_mem_is_zero(buf
, 10));
929 char tmpbuf
[TOR_ADDR_BUF_LEN
];
930 const char *ip
= "176.192.208.224";
932 tor_inet_pton(AF_INET
, ip
, &in
);
933 tor_inet_ntop(AF_INET
, &in
, tmpbuf
, sizeof(tmpbuf
));
934 test_streq(tmpbuf
, ip
);
938 test_streq("\"\"", escaped(""));
939 test_streq("\"abcd\"", escaped("abcd"));
940 test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
941 test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
942 test_assert(NULL
== escaped(NULL
));
944 /* Test strndup and memdup */
946 const char *s
= "abcdefghijklmnopqrstuvwxyz";
947 cp
= tor_strndup(s
, 30);
948 test_streq(cp
, s
); /* same string, */
949 test_neq(cp
, s
); /* but different pointers. */
952 cp
= tor_strndup(s
, 5);
953 test_streq(cp
, "abcde");
956 s
= "a\0b\0c\0d\0e\0";
957 cp
= tor_memdup(s
,10);
958 test_memeq(cp
, s
, 10); /* same ram, */
959 test_neq(cp
, s
); /* but different pointers. */
963 /* Test str-foo functions */
964 cp
= tor_strdup("abcdef");
965 test_assert(tor_strisnonupper(cp
));
967 test_assert(!tor_strisnonupper(cp
));
969 test_streq(cp
, "ABCDEF");
970 test_assert(tor_strisprint(cp
));
972 test_assert(!tor_strisprint(cp
));
975 /* Test eat_whitespace. */
977 const char *s
= " \n a";
978 test_eq_ptr(eat_whitespace(s
), s
+4);
980 test_eq_ptr(eat_whitespace(s
), s
);
982 test_eq_ptr(eat_whitespace(s
), s
+5);
987 const char *haystack
= "abcde";
988 tor_assert(!tor_memmem(haystack
, 5, "ef", 2));
989 test_eq_ptr(tor_memmem(haystack
, 5, "cd", 2), haystack
+ 2);
990 test_eq_ptr(tor_memmem(haystack
, 5, "cde", 3), haystack
+ 2);
991 haystack
= "ababcad";
992 test_eq_ptr(tor_memmem(haystack
, 7, "abc", 3), haystack
+ 2);
995 /* Test wrap_string */
997 smartlist_t
*sl
= smartlist_create();
998 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
1000 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
1002 "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
1004 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1005 smartlist_clear(sl
);
1007 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
1009 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
1011 "### This is a\n# test of string\n# wrapping\n# functionality:\n"
1015 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1019 /* now make sure time works. */
1020 tor_gettimeofday(&end
);
1021 /* We might've timewarped a little. */
1022 test_assert(tv_udiff(&start
, &end
) >= -5000);
1024 /* Test tor_log2(). */
1025 test_eq(tor_log2(64), 6);
1026 test_eq(tor_log2(65), 6);
1027 test_eq(tor_log2(63), 5);
1028 test_eq(tor_log2(1), 0);
1029 test_eq(tor_log2(2), 1);
1030 test_eq(tor_log2(3), 1);
1031 test_eq(tor_log2(4), 2);
1032 test_eq(tor_log2(5), 2);
1033 test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
1034 test_eq(tor_log2(UINT64_MAX
), 63);
1036 /* Test round_to_power_of_2 */
1037 test_eq(round_to_power_of_2(120), 128);
1038 test_eq(round_to_power_of_2(128), 128);
1039 test_eq(round_to_power_of_2(130), 128);
1040 test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
1041 U64_LITERAL(1)<<55);
1042 test_eq(round_to_power_of_2(0), 2);
1047 _test_eq_ip6(struct in6_addr
*a
, struct in6_addr
*b
, const char *e1
,
1048 const char *e2
, int line
)
1052 for (i
= 0; i
< 16; ++i
) {
1053 if (a
->s6_addr
[i
] != b
->s6_addr
[i
]) {
1059 printf("."); fflush(stdout
);
1061 char buf1
[128], *cp1
;
1062 char buf2
[128], *cp2
;
1064 cp1
= buf1
; cp2
= buf2
;
1065 for (i
=0; i
<16; ++i
) {
1066 tor_snprintf(cp1
, sizeof(buf1
)-(cp1
-buf1
), "%02x", a
->s6_addr
[i
]);
1067 tor_snprintf(cp2
, sizeof(buf2
)-(cp2
-buf2
), "%02x", b
->s6_addr
[i
]);
1069 if ((i
%2)==1 && i
!= 15) {
1075 printf("Line %d: assertion failed: (%s == %s)\n"
1076 " %s != %s\n", line
, e1
, e2
, buf1
, buf2
);
1081 #define test_eq_ip6(a,b) _test_eq_ip6((a),(b),#a,#b,__LINE__)
1083 /** Helper: Assert that two strings both decode as IPv6 addresses with
1084 * tor_inet_pton(), and both decode to the same address. */
1085 #define test_pton6_same(a,b) STMT_BEGIN \
1086 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
1087 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
1088 _test_eq_ip6(&a1,&a2,#a,#b,__LINE__); \
1091 /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
1092 * tor_inet_pton(). */
1093 #define test_pton6_bad(a) \
1094 test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
1096 /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
1097 * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
1098 * the same value as <b>a</b>. */
1099 #define test_ntop6_reduces(a,b) STMT_BEGIN \
1100 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
1101 test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
1102 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
1103 _test_eq_ip6(&a1, &a2, a, b, __LINE__); \
1106 /** Helper: assert that <b>a</a> parses by tor_inet_pton() into a address that
1107 * passes tor_addr_is_internal() with <b>for_listening</b> */
1108 #define test_internal_ip(a,for_listening) STMT_BEGIN \
1109 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
1110 t1.family = AF_INET6; \
1111 if (!tor_addr_is_internal(&t1, for_listening)) \
1112 test_fail_msg( a "was not internal."); \
1115 /** Helper: assert that <b>a</a> parses by tor_inet_pton() into a address that
1116 * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
1117 #define test_external_ip(a,for_listening) STMT_BEGIN \
1118 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
1119 t1.family = AF_INET6; \
1120 if (tor_addr_is_internal(&t1, for_listening)) \
1121 test_fail_msg(a "was not external."); \
1124 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
1125 * tor_inet_pton(), give addresses that compare in the order defined by
1126 * <b>op</b> with tor_addr_compare(). */
1127 #define test_addr_compare(a, op, b) STMT_BEGIN \
1128 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
1129 test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
1130 t1.family = t2.family = AF_INET6; \
1131 r = tor_addr_compare(&t1,&t2); \
1133 test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
1137 #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
1139 test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f); \
1140 p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
1141 test_eq(htonl(ip1), IN6_ADDRESS32(&t1)[0]); \
1142 test_eq(htonl(ip2), IN6_ADDRESS32(&t1)[1]); \
1143 test_eq(htonl(ip3), IN6_ADDRESS32(&t1)[2]); \
1144 test_eq(htonl(ip4), IN6_ADDRESS32(&t1)[3]); \
1145 test_eq(mask, mm); \
1146 test_eq(port1, pt1); \
1147 test_eq(port2, pt2); \
1151 test_util_ip6_helpers(void)
1153 char buf
[TOR_ADDR_BUF_LEN
], bug
[TOR_ADDR_BUF_LEN
];
1154 struct in6_addr a1
, a2
;
1157 uint16_t port1
, port2
;
1161 // struct in_addr b1, b2;
1162 /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
1164 /* === Test pton: valid af_inet6 */
1165 /* Simple, valid parsing. */
1166 r
= tor_inet_pton(AF_INET6
,
1167 "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1
);
1169 for (i
=0;i
<16;++i
) { test_eq(i
+1, (int)a1
.s6_addr
[i
]); }
1171 test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
1172 "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
1173 /* shortened words. */
1174 test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
1175 "1:99:BEEF:0:0123:FFFF:1:1");
1176 /* zeros at the beginning */
1177 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
1179 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
1180 "::9:c0a8:0.1.0.1");
1181 /* zeros in the middle. */
1182 test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
1183 "fe80::202:1111:1:1");
1184 /* zeros at the end. */
1185 test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
1188 /* === Test ntop: af_inet6 */
1189 test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
1191 test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
1192 "1:99:beef:6:123:ffff:1:1");
1194 //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
1195 test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
1196 test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
1197 test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
1198 test_ntop6_reduces("008:0::0", "8::");
1199 test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
1200 test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
1201 test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
1203 test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
1204 "fe80::202:1111:1:1");
1205 test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
1208 /* === Test pton: invalid in6. */
1209 test_pton6_bad("foobar.");
1210 test_pton6_bad("55555::");
1211 test_pton6_bad("9:-60::");
1212 test_pton6_bad("1:2:33333:4:0002:3::");
1213 //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
1214 test_pton6_bad("1:2:3333:4:fish:3::");
1215 test_pton6_bad("1:2:3:4:5:6:7:8:9");
1216 test_pton6_bad("1:2:3:4:5:6:7");
1217 test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
1218 test_pton6_bad("1:2:3:4:5:6:1.2.3");
1219 test_pton6_bad("::1.2.3");
1220 test_pton6_bad("::1.2.3.4.5");
1221 test_pton6_bad("99");
1223 test_pton6_bad("1::2::3:4");
1224 test_pton6_bad("a:::b:c");
1225 test_pton6_bad(":::a:b:c");
1226 test_pton6_bad("a:b:c:::");
1228 /* test internal checking */
1229 test_external_ip("fbff:ffff::2:7", 0);
1230 test_internal_ip("fc01::2:7", 0);
1231 test_internal_ip("fdff:ffff::f:f", 0);
1232 test_external_ip("fe00::3:f", 0);
1234 test_external_ip("fe7f:ffff::2:7", 0);
1235 test_internal_ip("fe80::2:7", 0);
1236 test_internal_ip("febf:ffff::f:f", 0);
1238 test_internal_ip("fec0::2:7:7", 0);
1239 test_internal_ip("feff:ffff::e:7:7", 0);
1240 test_external_ip("ff00::e:7:7", 0);
1242 test_internal_ip("::", 0);
1243 test_internal_ip("::1", 0);
1244 test_internal_ip("::1", 1);
1245 test_internal_ip("::", 0);
1246 test_external_ip("::", 1);
1247 test_external_ip("::2", 0);
1248 test_external_ip("2001::", 0);
1249 test_external_ip("ffff::", 0);
1251 test_external_ip("::ffff:0.0.0.0", 1);
1252 test_internal_ip("::ffff:0.0.0.0", 0);
1253 test_internal_ip("::ffff:0.255.255.255", 0);
1254 test_external_ip("::ffff:1.0.0.0", 0);
1256 test_external_ip("::ffff:9.255.255.255", 0);
1257 test_internal_ip("::ffff:10.0.0.0", 0);
1258 test_internal_ip("::ffff:10.255.255.255", 0);
1259 test_external_ip("::ffff:11.0.0.0", 0);
1261 test_external_ip("::ffff:126.255.255.255", 0);
1262 test_internal_ip("::ffff:127.0.0.0", 0);
1263 test_internal_ip("::ffff:127.255.255.255", 0);
1264 test_external_ip("::ffff:128.0.0.0", 0);
1266 test_external_ip("::ffff:172.15.255.255", 0);
1267 test_internal_ip("::ffff:172.16.0.0", 0);
1268 test_internal_ip("::ffff:172.31.255.255", 0);
1269 test_external_ip("::ffff:172.32.0.0", 0);
1271 test_external_ip("::ffff:192.167.255.255", 0);
1272 test_internal_ip("::ffff:192.168.0.0", 0);
1273 test_internal_ip("::ffff:192.168.255.255", 0);
1274 test_external_ip("::ffff:192.169.0.0", 0);
1276 test_external_ip("::ffff:169.253.255.255", 0);
1277 test_internal_ip("::ffff:169.254.0.0", 0);
1278 test_internal_ip("::ffff:169.254.255.255", 0);
1279 test_external_ip("::ffff:169.255.0.0", 0);
1281 /* tor_addr_compare(tor_addr_t x2) */
1282 test_addr_compare("ffff::", ==, "ffff::0");
1283 test_addr_compare("0::3:2:1", >, "0::ffff:0.3.2.1");
1284 test_addr_compare("0::2:2:1", >, "0::ffff:0.3.2.1");
1285 test_addr_compare("0::ffff:0.3.2.1", <, "0::0:0:0");
1286 test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
1287 tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1
, NULL
, NULL
, NULL
);
1288 tor_addr_parse_mask_ports("2.3.4.5", &t2
, NULL
, NULL
, NULL
);
1289 test_assert(tor_addr_compare(&t1
, &t2
) == 0);
1290 tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1
, NULL
, NULL
, NULL
);
1291 tor_addr_parse_mask_ports("2.3.4.5", &t2
, NULL
, NULL
, NULL
);
1292 test_assert(tor_addr_compare(&t1
, &t2
) < 0);
1294 /* XXXX020 test compare_masked */
1296 /* test tor_addr_parse_mask_ports */
1297 test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6
,
1298 0, 0, 0, 0x0000000f, 17, 47, 95);
1299 //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
1300 //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
1301 test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6
,
1302 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
1303 test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6
,
1304 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
1306 r
=tor_addr_parse_mask_ports("[fefef::]/112", &t1
, NULL
, NULL
, NULL
);
1307 test_assert(r
== -1);
1308 r
=tor_addr_parse_mask_ports("efef::/112", &t1
, NULL
, NULL
, NULL
);
1309 test_assert(r
== -1);
1310 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1
, NULL
, NULL
, NULL
);
1311 test_assert(r
== -1);
1312 r
=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1
, NULL
, NULL
, NULL
);
1313 test_assert(r
== -1);
1314 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1
, NULL
, NULL
, NULL
);
1315 test_assert(r
== -1);
1316 /* Test for V4-mapped address with mask < 96. (arguably not valid) */
1317 r
=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1
, &mask
, NULL
, NULL
);
1318 test_assert(r
== -1);
1319 r
=tor_addr_parse_mask_ports("1.1.2.2/33", &t1
, &mask
, NULL
, NULL
);
1320 test_assert(r
== -1);
1321 r
=tor_addr_parse_mask_ports("1.1.2.2/31", &t1
, &mask
, NULL
, NULL
);
1322 test_assert(r
== AF_INET
);
1323 r
=tor_addr_parse_mask_ports("[efef::]/112", &t1
, &mask
, &port1
, &port2
);
1324 test_assert(r
== AF_INET6
);
1325 test_assert(port1
== 1);
1326 test_assert(port2
== 65535);
1328 /* make sure inet address lengths >= max */
1329 test_assert(INET_NTOA_BUF_LEN
>= sizeof("255.255.255.255"));
1330 test_assert(TOR_ADDR_BUF_LEN
>=
1331 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
1333 test_assert(sizeof(tor_addr_t
) >= sizeof(struct in6_addr
));
1335 /* get interface addresses */
1336 r
= get_interface_address6(0, AF_INET
, &t1
);
1337 i
= get_interface_address6(0, AF_INET6
, &t2
);
1339 tor_inet_ntop(AF_INET
, &t1
.sa
.sin_addr
, buf
, sizeof(buf
));
1340 printf("\nv4 address: %s (family=%i)", buf
, IN_FAMILY(&t1
));
1341 tor_inet_ntop(AF_INET6
, &t2
.sa6
.sin6_addr
, buf
, sizeof(buf
));
1342 printf("\nv6 address: %s (family=%i)", buf
, IN_FAMILY(&t2
));
1347 test_util_smartlist(void)
1352 /* XXXX test sort_digests, uniq_strings, uniq_digests */
1354 /* Test smartlist add, del_keeporder, insert, get. */
1355 sl
= smartlist_create();
1356 smartlist_add(sl
, (void*)1);
1357 smartlist_add(sl
, (void*)2);
1358 smartlist_add(sl
, (void*)3);
1359 smartlist_add(sl
, (void*)4);
1360 smartlist_del_keeporder(sl
, 1);
1361 smartlist_insert(sl
, 1, (void*)22);
1362 smartlist_insert(sl
, 0, (void*)0);
1363 smartlist_insert(sl
, 5, (void*)555);
1364 test_eq_ptr((void*)0, smartlist_get(sl
,0));
1365 test_eq_ptr((void*)1, smartlist_get(sl
,1));
1366 test_eq_ptr((void*)22, smartlist_get(sl
,2));
1367 test_eq_ptr((void*)3, smartlist_get(sl
,3));
1368 test_eq_ptr((void*)4, smartlist_get(sl
,4));
1369 test_eq_ptr((void*)555, smartlist_get(sl
,5));
1370 /* Try deleting in the middle. */
1371 smartlist_del(sl
, 1);
1372 test_eq_ptr((void*)555, smartlist_get(sl
, 1));
1373 /* Try deleting at the end. */
1374 smartlist_del(sl
, 4);
1375 test_eq(4, smartlist_len(sl
));
1378 test_assert(smartlist_isin(sl
, (void*)3));
1379 test_assert(!smartlist_isin(sl
, (void*)99));
1381 /* Test split and join */
1382 smartlist_clear(sl
);
1383 test_eq(0, smartlist_len(sl
));
1384 smartlist_split_string(sl
, "abc", ":", 0, 0);
1385 test_eq(1, smartlist_len(sl
));
1386 test_streq("abc", smartlist_get(sl
, 0));
1387 smartlist_split_string(sl
, "a::bc::", "::", 0, 0);
1388 test_eq(4, smartlist_len(sl
));
1389 test_streq("a", smartlist_get(sl
, 1));
1390 test_streq("bc", smartlist_get(sl
, 2));
1391 test_streq("", smartlist_get(sl
, 3));
1392 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
1393 test_streq(cp
, "abcabc");
1395 cp
= smartlist_join_strings(sl
, "!", 0, NULL
);
1396 test_streq(cp
, "abc!a!bc!");
1398 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
1399 test_streq(cp
, "abcXYaXYbcXY");
1401 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
1402 test_streq(cp
, "abcXYaXYbcXYXY");
1404 cp
= smartlist_join_strings(sl
, "", 1, NULL
);
1405 test_streq(cp
, "abcabc");
1408 smartlist_split_string(sl
, "/def/ /ghijk", "/", 0, 0);
1409 test_eq(8, smartlist_len(sl
));
1410 test_streq("", smartlist_get(sl
, 4));
1411 test_streq("def", smartlist_get(sl
, 5));
1412 test_streq(" ", smartlist_get(sl
, 6));
1413 test_streq("ghijk", smartlist_get(sl
, 7));
1414 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1415 smartlist_clear(sl
);
1417 smartlist_split_string(sl
, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE
, 0);
1418 test_eq(3, smartlist_len(sl
));
1419 test_streq("a", smartlist_get(sl
,0));
1420 test_streq("bbd", smartlist_get(sl
,1));
1421 test_streq("cdef", smartlist_get(sl
,2));
1422 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1423 SPLIT_SKIP_SPACE
, 0);
1424 test_eq(8, smartlist_len(sl
));
1425 test_streq("z", smartlist_get(sl
,3));
1426 test_streq("zhasd", smartlist_get(sl
,4));
1427 test_streq("", smartlist_get(sl
,5));
1428 test_streq("bnud", smartlist_get(sl
,6));
1429 test_streq("", smartlist_get(sl
,7));
1431 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1432 smartlist_clear(sl
);
1434 smartlist_split_string(sl
, " ab\tc \td ef ", NULL
,
1435 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1436 test_eq(4, smartlist_len(sl
));
1437 test_streq("ab", smartlist_get(sl
,0));
1438 test_streq("c", smartlist_get(sl
,1));
1439 test_streq("d", smartlist_get(sl
,2));
1440 test_streq("ef", smartlist_get(sl
,3));
1441 smartlist_split_string(sl
, "ghi\tj", NULL
,
1442 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1443 test_eq(6, smartlist_len(sl
));
1444 test_streq("ghi", smartlist_get(sl
,4));
1445 test_streq("j", smartlist_get(sl
,5));
1447 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1448 smartlist_clear(sl
);
1450 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
1453 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
1454 test_streq(cp
, "XY");
1457 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1458 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1459 test_eq(3, smartlist_len(sl
));
1460 test_streq("z", smartlist_get(sl
, 0));
1461 test_streq("zhasd", smartlist_get(sl
, 1));
1462 test_streq("bnud", smartlist_get(sl
, 2));
1463 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
1464 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 2);
1465 test_eq(5, smartlist_len(sl
));
1466 test_streq("z", smartlist_get(sl
, 3));
1467 test_streq("zhasd <> <> bnud<>", smartlist_get(sl
, 4));
1468 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1469 smartlist_clear(sl
);
1471 smartlist_split_string(sl
, "abcd\n", "\n",
1472 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1473 test_eq(1, smartlist_len(sl
));
1474 test_streq("abcd", smartlist_get(sl
, 0));
1475 smartlist_split_string(sl
, "efgh", "\n",
1476 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1477 test_eq(2, smartlist_len(sl
));
1478 test_streq("efgh", smartlist_get(sl
, 1));
1480 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1481 smartlist_clear(sl
);
1483 /* Test swapping, shuffling, and sorting. */
1484 smartlist_split_string(sl
, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
1485 test_eq(7, smartlist_len(sl
));
1486 smartlist_sort(sl
, _compare_strs
);
1487 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1488 test_streq(cp
,"and,arma,by,nickm,onion,router,the");
1490 smartlist_swap(sl
, 1, 5);
1491 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1492 test_streq(cp
,"and,router,by,nickm,onion,arma,the");
1494 smartlist_shuffle(sl
);
1495 test_eq(7, smartlist_len(sl
));
1496 test_assert(smartlist_string_isin(sl
, "and"));
1497 test_assert(smartlist_string_isin(sl
, "router"));
1498 test_assert(smartlist_string_isin(sl
, "by"));
1499 test_assert(smartlist_string_isin(sl
, "nickm"));
1500 test_assert(smartlist_string_isin(sl
, "onion"));
1501 test_assert(smartlist_string_isin(sl
, "arma"));
1502 test_assert(smartlist_string_isin(sl
, "the"));
1505 smartlist_sort(sl
, _compare_strs
);
1506 test_streq("nickm", smartlist_bsearch(sl
, "zNicKM",
1507 _compare_without_first_ch
));
1508 test_streq("and", smartlist_bsearch(sl
, " AND", _compare_without_first_ch
));
1509 test_eq_ptr(NULL
, smartlist_bsearch(sl
, " ANz", _compare_without_first_ch
));
1511 /* Test bsearch_idx */
1514 test_eq(0, smartlist_bsearch_idx(sl
," aaa",_compare_without_first_ch
,&f
));
1516 test_eq(0, smartlist_bsearch_idx(sl
," and",_compare_without_first_ch
,&f
));
1518 test_eq(1, smartlist_bsearch_idx(sl
," arm",_compare_without_first_ch
,&f
));
1520 test_eq(1, smartlist_bsearch_idx(sl
," arma",_compare_without_first_ch
,&f
));
1522 test_eq(2, smartlist_bsearch_idx(sl
," armb",_compare_without_first_ch
,&f
));
1524 test_eq(7, smartlist_bsearch_idx(sl
," zzzz",_compare_without_first_ch
,&f
));
1528 /* Test reverse() and pop_last() */
1529 smartlist_reverse(sl
);
1530 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1531 test_streq(cp
,"the,router,onion,nickm,by,arma,and");
1533 cp
= smartlist_pop_last(sl
);
1534 test_streq(cp
, "and");
1536 test_eq(smartlist_len(sl
), 6);
1537 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1538 smartlist_clear(sl
);
1541 smartlist_split_string(sl
,
1542 "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
1544 smartlist_sort(sl
, _compare_strs
);
1545 smartlist_uniq(sl
, _compare_strs
, _tor_free
);
1546 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1547 test_streq(cp
, "50,a,canal,man,noon,panama,plan,radar");
1550 /* Test string_isin and isin_case and num_isin */
1551 test_assert(smartlist_string_isin(sl
, "noon"));
1552 test_assert(!smartlist_string_isin(sl
, "noonoon"));
1553 test_assert(smartlist_string_isin_case(sl
, "nOOn"));
1554 test_assert(!smartlist_string_isin_case(sl
, "nooNooN"));
1555 test_assert(smartlist_string_num_isin(sl
, 50));
1556 test_assert(!smartlist_string_num_isin(sl
, 60));
1557 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1558 smartlist_clear(sl
);
1560 /* Test string_remove and remove and join_strings2 */
1561 smartlist_split_string(sl
,
1562 "Some say the Earth will end in ice and some in fire",
1564 cp
= smartlist_get(sl
, 4);
1565 test_streq(cp
, "will");
1566 smartlist_add(sl
, cp
);
1567 smartlist_remove(sl
, cp
);
1569 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1570 test_streq(cp
, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
1572 smartlist_string_remove(sl
, "in");
1573 cp
= smartlist_join_strings2(sl
, "+XX", 1, 0, NULL
);
1574 test_streq(cp
, "Some+say+the+Earth+fire+end+some+ice+and");
1577 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1578 smartlist_clear(sl
);
1581 smartlist_t
*ints
= smartlist_create();
1582 smartlist_t
*odds
= smartlist_create();
1583 smartlist_t
*evens
= smartlist_create();
1584 smartlist_t
*primes
= smartlist_create();
1586 for (i
=1; i
< 10; i
+= 2)
1587 smartlist_add(odds
, (void*)(uintptr_t)i
);
1588 for (i
=0; i
< 10; i
+= 2)
1589 smartlist_add(evens
, (void*)(uintptr_t)i
);
1592 smartlist_add_all(ints
, odds
);
1593 smartlist_add_all(ints
, evens
);
1594 test_eq(smartlist_len(ints
), 10);
1596 smartlist_add(primes
, (void*)2);
1597 smartlist_add(primes
, (void*)3);
1598 smartlist_add(primes
, (void*)5);
1599 smartlist_add(primes
, (void*)7);
1602 test_assert(smartlist_overlap(ints
, odds
));
1603 test_assert(smartlist_overlap(odds
, primes
));
1604 test_assert(smartlist_overlap(evens
, primes
));
1605 test_assert(!smartlist_overlap(odds
, evens
));
1608 smartlist_add_all(sl
, odds
);
1609 smartlist_intersect(sl
, primes
);
1610 test_eq(smartlist_len(sl
), 3);
1611 test_assert(smartlist_isin(sl
, (void*)3));
1612 test_assert(smartlist_isin(sl
, (void*)5));
1613 test_assert(smartlist_isin(sl
, (void*)7));
1616 smartlist_add_all(sl
, primes
);
1617 smartlist_subtract(sl
, odds
);
1618 test_eq(smartlist_len(sl
), 1);
1619 test_assert(smartlist_isin(sl
, (void*)2));
1621 smartlist_free(odds
);
1622 smartlist_free(evens
);
1623 smartlist_free(ints
);
1624 smartlist_free(primes
);
1625 smartlist_clear(sl
);
1632 test_util_bitarray(void)
1637 ba
= bitarray_init_zero(1);
1638 test_assert(! bitarray_is_set(ba
, 0));
1639 bitarray_set(ba
, 0);
1640 test_assert(bitarray_is_set(ba
, 0));
1641 bitarray_clear(ba
, 0);
1642 test_assert(! bitarray_is_set(ba
, 0));
1645 ba
= bitarray_init_zero(1023);
1646 for (i
= 1; i
< 64; ) {
1647 for (j
= 0; j
< 1023; ++j
) {
1649 bitarray_set(ba
, j
);
1651 bitarray_clear(ba
, j
);
1653 for (j
= 0; j
< 1023; ++j
) {
1654 if (!bool_eq(bitarray_is_set(ba
, j
), j
%i
))
1668 /* stop threads running at once. */
1669 static tor_mutex_t
*_thread_test_mutex
= NULL
;
1670 /* make sure that threads have to run at the same time. */
1671 static tor_mutex_t
*_thread_test_start1
= NULL
;
1672 static tor_mutex_t
*_thread_test_start2
= NULL
;
1673 static strmap_t
*_thread_test_strmap
= NULL
;
1675 static void _thread_test_func(void* _s
) ATTR_NORETURN
;
1677 static int t1_count
= 0;
1678 static int t2_count
= 0;
1681 _thread_test_func(void* _s
)
1688 if (!strcmp(s
, "thread 1")) {
1689 m
= _thread_test_start1
;
1692 m
= _thread_test_start2
;
1695 tor_mutex_acquire(m
);
1697 tor_snprintf(buf
, sizeof(buf
), "%lu", tor_get_thread_id());
1698 cp
= tor_strdup(buf
);
1700 for (i
=0; i
<10000; ++i
) {
1701 tor_mutex_acquire(_thread_test_mutex
);
1702 strmap_set(_thread_test_strmap
, "last to run", cp
);
1704 tor_mutex_release(_thread_test_mutex
);
1706 tor_mutex_acquire(_thread_test_mutex
);
1707 strmap_set(_thread_test_strmap
, s
, tor_strdup(buf
));
1708 tor_mutex_release(_thread_test_mutex
);
1710 tor_mutex_release(m
);
1716 test_util_threads(void)
1719 int done
= 0, timedout
= 0;
1721 #ifndef TOR_IS_MULTITHREADED
1722 /* Skip this test if we aren't threading. We should be threading most
1723 * everywhere by now. */
1727 _thread_test_mutex
= tor_mutex_new();
1728 _thread_test_start1
= tor_mutex_new();
1729 _thread_test_start2
= tor_mutex_new();
1730 _thread_test_strmap
= strmap_new();
1731 s1
= tor_strdup("thread 1");
1732 s2
= tor_strdup("thread 2");
1733 tor_mutex_acquire(_thread_test_start1
);
1734 tor_mutex_acquire(_thread_test_start2
);
1735 spawn_func(_thread_test_func
, s1
);
1736 spawn_func(_thread_test_func
, s2
);
1737 tor_mutex_release(_thread_test_start2
);
1738 tor_mutex_release(_thread_test_start1
);
1739 started
= time(NULL
);
1741 tor_mutex_acquire(_thread_test_mutex
);
1742 strmap_assert_ok(_thread_test_strmap
);
1743 if (strmap_get(_thread_test_strmap
, "thread 1") &&
1744 strmap_get(_thread_test_strmap
, "thread 2")) {
1746 } else if (time(NULL
) > started
+ 25) {
1747 timedout
= done
= 1;
1749 tor_mutex_release(_thread_test_mutex
);
1751 tor_mutex_free(_thread_test_mutex
);
1754 printf("\nTimed out: %d %d", t1_count
, t2_count
);
1755 test_assert(strmap_get(_thread_test_strmap
, "thread 1"));
1756 test_assert(strmap_get(_thread_test_strmap
, "thread 2"));
1757 test_assert(!timedout
);
1760 /* different thread IDs. */
1761 test_assert(strcmp(strmap_get(_thread_test_strmap
, "thread 1"),
1762 strmap_get(_thread_test_strmap
, "thread 2")));
1763 test_assert(!strcmp(strmap_get(_thread_test_strmap
, "thread 1"),
1764 strmap_get(_thread_test_strmap
, "last to run")) ||
1765 !strcmp(strmap_get(_thread_test_strmap
, "thread 2"),
1766 strmap_get(_thread_test_strmap
, "last to run")));
1768 strmap_free(_thread_test_strmap
, _tor_free
);
1775 _compare_strings_for_pqueue(const void *s1
, const void *s2
)
1777 return strcmp((const char*)s1
, (const char*)s2
);
1781 test_util_pqueue(void)
1784 int (*cmp
)(const void *, const void*);
1785 #define OK() smartlist_pqueue_assert_ok(sl, cmp)
1787 cmp
= _compare_strings_for_pqueue
;
1789 sl
= smartlist_create();
1790 smartlist_pqueue_add(sl
, cmp
, (char*)"cows");
1791 smartlist_pqueue_add(sl
, cmp
, (char*)"zebras");
1792 smartlist_pqueue_add(sl
, cmp
, (char*)"fish");
1793 smartlist_pqueue_add(sl
, cmp
, (char*)"frogs");
1794 smartlist_pqueue_add(sl
, cmp
, (char*)"apples");
1795 smartlist_pqueue_add(sl
, cmp
, (char*)"squid");
1796 smartlist_pqueue_add(sl
, cmp
, (char*)"daschunds");
1797 smartlist_pqueue_add(sl
, cmp
, (char*)"eggplants");
1798 smartlist_pqueue_add(sl
, cmp
, (char*)"weissbier");
1799 smartlist_pqueue_add(sl
, cmp
, (char*)"lobsters");
1800 smartlist_pqueue_add(sl
, cmp
, (char*)"roquefort");
1804 test_eq(smartlist_len(sl
), 11);
1805 test_streq(smartlist_get(sl
, 0), "apples");
1806 test_streq(smartlist_pqueue_pop(sl
, cmp
), "apples");
1807 test_eq(smartlist_len(sl
), 10);
1809 test_streq(smartlist_pqueue_pop(sl
, cmp
), "cows");
1810 test_streq(smartlist_pqueue_pop(sl
, cmp
), "daschunds");
1811 smartlist_pqueue_add(sl
, cmp
, (char*)"chinchillas");
1813 smartlist_pqueue_add(sl
, cmp
, (char*)"fireflies");
1815 test_streq(smartlist_pqueue_pop(sl
, cmp
), "chinchillas");
1816 test_streq(smartlist_pqueue_pop(sl
, cmp
), "eggplants");
1817 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fireflies");
1819 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fish");
1820 test_streq(smartlist_pqueue_pop(sl
, cmp
), "frogs");
1821 test_streq(smartlist_pqueue_pop(sl
, cmp
), "lobsters");
1822 test_streq(smartlist_pqueue_pop(sl
, cmp
), "roquefort");
1824 test_eq(smartlist_len(sl
), 3);
1825 test_streq(smartlist_pqueue_pop(sl
, cmp
), "squid");
1826 test_streq(smartlist_pqueue_pop(sl
, cmp
), "weissbier");
1827 test_streq(smartlist_pqueue_pop(sl
, cmp
), "zebras");
1828 test_eq(smartlist_len(sl
), 0);
1835 test_util_gzip(void)
1837 char *buf1
, *buf2
=NULL
, *buf3
=NULL
, *cp1
, *cp2
;
1840 tor_zlib_state_t
*state
;
1842 buf1
= tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
1843 test_assert(detect_compression_method(buf1
, strlen(buf1
)) == UNKNOWN_METHOD
);
1844 if (is_gzip_supported()) {
1845 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1848 test_assert(!memcmp(buf2
, "\037\213", 2)); /* Gzip magic. */
1849 test_assert(detect_compression_method(buf2
, len1
) == GZIP_METHOD
);
1851 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1852 GZIP_METHOD
, 1, LOG_INFO
));
1854 test_streq(buf1
,buf3
);
1860 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1863 test_assert(!memcmp(buf2
, "\x78\xDA", 2)); /* deflate magic. */
1864 test_assert(detect_compression_method(buf2
, len1
) == ZLIB_METHOD
);
1866 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1867 ZLIB_METHOD
, 1, LOG_INFO
));
1869 test_streq(buf1
,buf3
);
1871 /* Check whether we can uncompress concatenated, compresed strings. */
1873 buf2
= tor_realloc(buf2
, len1
*2);
1874 memcpy(buf2
+len1
, buf2
, len1
);
1875 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
*2,
1876 ZLIB_METHOD
, 1, LOG_INFO
));
1877 test_eq(len2
, (strlen(buf1
)+1)*2);
1879 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
1880 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
1881 (strlen(buf1
)+1)*2);
1887 /* Check whether we can uncompress partial strings. */
1889 tor_strdup("String with low redundancy that won't be compressed much.");
1890 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1892 tor_assert(len1
>16);
1893 /* when we allow an uncomplete string, we should succeed.*/
1894 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1895 ZLIB_METHOD
, 0, LOG_INFO
));
1897 tor_assert(len2
> 5);
1898 tor_assert(!strcmpstart(buf1
, buf3
));
1900 /* when we demand a complete string, this must fail. */
1902 tor_assert(tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1903 ZLIB_METHOD
, 1, LOG_INFO
));
1906 /* Now, try streaming compression. */
1910 state
= tor_zlib_new(1, ZLIB_METHOD
);
1912 cp1
= buf1
= tor_malloc(1024);
1914 ccp2
= "ABCDEFGHIJABCDEFGHIJ";
1916 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 0)
1918 test_eq(len2
, 0); /* Make sure we compressed it all. */
1919 test_assert(cp1
> buf1
);
1923 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 1)
1926 test_assert(cp1
> cp2
); /* Make sure we really added something. */
1928 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf1
, 1024-len1
,
1929 ZLIB_METHOD
, 1, LOG_WARN
));
1930 test_streq(buf3
, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
1933 tor_zlib_free(state
);
1941 test_util_strmap(void)
1944 strmap_iter_t
*iter
;
1948 smartlist_t
*found_keys
;
1951 v
= strmap_set(map
, "K1", (void*)99);
1953 v
= strmap_set(map
, "K2", (void*)101);
1955 v
= strmap_set(map
, "K1", (void*)100);
1956 test_eq(v
, (void*)99);
1957 test_eq_ptr(strmap_get(map
,"K1"), (void*)100);
1958 test_eq_ptr(strmap_get(map
,"K2"), (void*)101);
1959 test_eq_ptr(strmap_get(map
,"K-not-there"), NULL
);
1960 strmap_assert_ok(map
);
1962 v
= strmap_remove(map
,"K2");
1963 strmap_assert_ok(map
);
1964 test_eq_ptr(v
, (void*)101);
1965 test_eq_ptr(strmap_get(map
,"K2"), NULL
);
1966 test_eq_ptr(strmap_remove(map
,"K2"), NULL
);
1968 strmap_set(map
, "K2", (void*)101);
1969 strmap_set(map
, "K3", (void*)102);
1970 strmap_set(map
, "K4", (void*)103);
1971 strmap_assert_ok(map
);
1972 strmap_set(map
, "K5", (void*)104);
1973 strmap_set(map
, "K6", (void*)105);
1974 strmap_assert_ok(map
);
1976 /* Test iterator. */
1977 iter
= strmap_iter_init(map
);
1978 found_keys
= smartlist_create();
1979 while (!strmap_iter_done(iter
)) {
1980 strmap_iter_get(iter
,&k
,&v
);
1981 smartlist_add(found_keys
, tor_strdup(k
));
1982 test_eq_ptr(v
, strmap_get(map
, k
));
1984 if (!strcmp(k
, "K2")) {
1985 iter
= strmap_iter_next_rmv(map
,iter
);
1987 iter
= strmap_iter_next(map
,iter
);
1991 /* Make sure we removed K2, but not the others. */
1992 test_eq_ptr(strmap_get(map
, "K2"), NULL
);
1993 test_eq_ptr(strmap_get(map
, "K5"), (void*)104);
1994 /* Make sure we visited everyone once */
1995 smartlist_sort_strings(found_keys
);
1996 visited
= smartlist_join_strings(found_keys
, ":", 0, NULL
);
1997 test_streq(visited
, "K1:K2:K3:K4:K5:K6");
1999 SMARTLIST_FOREACH(found_keys
, char *, cp
, tor_free(cp
));
2000 smartlist_free(found_keys
);
2002 strmap_assert_ok(map
);
2003 /* Clean up after ourselves. */
2004 strmap_free(map
, NULL
);
2006 /* Now try some lc functions. */
2008 strmap_set_lc(map
,"Ab.C", (void*)1);
2009 test_eq_ptr(strmap_get(map
,"ab.c"), (void*)1);
2010 strmap_assert_ok(map
);
2011 test_eq_ptr(strmap_get_lc(map
,"AB.C"), (void*)1);
2012 test_eq_ptr(strmap_get(map
,"AB.C"), NULL
);
2013 test_eq_ptr(strmap_remove_lc(map
,"aB.C"), (void*)1);
2014 strmap_assert_ok(map
);
2015 test_eq_ptr(strmap_get_lc(map
,"AB.C"), NULL
);
2016 strmap_free(map
,NULL
);
2020 test_util_mmap(void)
2022 char *fname1
= tor_strdup(get_fname("mapped_1"));
2023 char *fname2
= tor_strdup(get_fname("mapped_2"));
2024 char *fname3
= tor_strdup(get_fname("mapped_3"));
2025 const size_t buflen
= 17000;
2026 char *buf
= tor_malloc(17000);
2027 tor_mmap_t
*mapping
;
2029 crypto_rand(buf
, buflen
);
2031 write_str_to_file(fname1
, "Short file.", 1);
2032 write_bytes_to_file(fname2
, buf
, buflen
, 1);
2033 write_bytes_to_file(fname3
, buf
, 16384, 1);
2035 mapping
= tor_mmap_file(fname1
);
2036 test_assert(mapping
);
2037 test_eq(mapping
->size
, strlen("Short file."));
2038 test_streq(mapping
->data
, "Short file.");
2040 tor_munmap_file(mapping
);
2041 test_assert(unlink(fname1
) == 0);
2043 /* make sure we can unlink. */
2044 test_assert(unlink(fname1
) == 0);
2045 test_streq(mapping
->data
, "Short file.");
2046 tor_munmap_file(mapping
);
2049 /* Make sure that we fail to map a no-longer-existent file. */
2050 mapping
= tor_mmap_file(fname1
);
2051 test_assert(mapping
== NULL
);
2053 /* Now try a big file that stretches across a few pages and isn't aligned */
2054 mapping
= tor_mmap_file(fname2
);
2055 test_assert(mapping
);
2056 test_eq(mapping
->size
, buflen
);
2057 test_memeq(mapping
->data
, buf
, buflen
);
2058 tor_munmap_file(mapping
);
2060 /* Now try a big aligned file. */
2061 mapping
= tor_mmap_file(fname3
);
2062 test_assert(mapping
);
2063 test_eq(mapping
->size
, 16384);
2064 test_memeq(mapping
->data
, buf
, 16384);
2065 tor_munmap_file(mapping
);
2067 /* fname1 got unlinked above */
2078 test_util_control_formats(void)
2082 "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
2085 sz
= read_escaped_data(inp
, strlen(inp
), &out
);
2087 ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
2088 test_eq(sz
, strlen(out
));
2094 test_onion_handshake(void)
2097 crypto_dh_env_t
*c_dh
= NULL
;
2098 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
2102 char s_buf
[ONIONSKIN_REPLY_LEN
];
2106 crypto_pk_env_t
*pk
= NULL
;
2108 pk
= pk_generate(0);
2110 /* client handshake 1. */
2111 memset(c_buf
, 0, ONIONSKIN_CHALLENGE_LEN
);
2112 test_assert(! onion_skin_create(pk
, &c_dh
, c_buf
));
2114 /* server handshake */
2115 memset(s_buf
, 0, ONIONSKIN_REPLY_LEN
);
2116 memset(s_keys
, 0, 40);
2117 test_assert(! onion_skin_server_handshake(c_buf
, pk
, NULL
,
2118 s_buf
, s_keys
, 40));
2120 /* client handshake 2 */
2121 memset(c_keys
, 0, 40);
2122 test_assert(! onion_skin_client_handshake(c_dh
, s_buf
, c_keys
, 40));
2124 crypto_dh_free(c_dh
);
2126 if (memcmp(c_keys
, s_keys
, 40)) {
2130 test_memeq(c_keys
, s_keys
, 40);
2131 memset(s_buf
, 0, 40);
2132 test_memneq(c_keys
, s_buf
, 40);
2133 crypto_free_pk_env(pk
);
2136 extern smartlist_t
*fingerprint_list
;
2139 test_dir_format(void)
2141 char buf
[8192], buf2
[8192];
2143 char fingerprint
[FINGERPRINT_LEN
+1];
2144 char *pk1_str
= NULL
, *pk2_str
= NULL
, *pk3_str
= NULL
, *cp
;
2145 size_t pk1_str_len
, pk2_str_len
, pk3_str_len
;
2146 routerinfo_t
*r1
, *r2
;
2147 crypto_pk_env_t
*pk1
= NULL
, *pk2
= NULL
, *pk3
= NULL
;
2148 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
2149 addr_policy_t
*ex1
, *ex2
;
2150 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
2153 pk1
= pk_generate(0);
2154 pk2
= pk_generate(1);
2155 pk3
= pk_generate(2);
2157 test_assert( is_legal_nickname("a"));
2158 test_assert(!is_legal_nickname(""));
2159 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
2160 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
2161 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
2162 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2164 test_assert( is_legal_nickname_or_hexdigest(
2165 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2166 test_assert( is_legal_nickname_or_hexdigest(
2167 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
2168 test_assert( is_legal_nickname_or_hexdigest(
2169 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
2171 test_assert(!is_legal_nickname_or_hexdigest(
2172 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2174 test_assert(!is_legal_nickname_or_hexdigest(
2175 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2176 /* hex part too long */
2177 test_assert(!is_legal_nickname_or_hexdigest(
2178 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
2179 test_assert(!is_legal_nickname_or_hexdigest(
2180 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
2182 test_assert(!is_legal_nickname_or_hexdigest(
2183 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
2184 test_assert(!is_legal_nickname_or_hexdigest(
2185 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
2186 test_assert(!is_legal_nickname_or_hexdigest(
2187 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
2188 test_assert(!is_legal_nickname_or_hexdigest(
2189 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
2190 "abcdefghijklmnoppqrst"));
2191 /* Bad extra char. */
2192 test_assert(!is_legal_nickname_or_hexdigest(
2193 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
2194 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
2195 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
2196 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
2198 get_platform_str(platform
, sizeof(platform
));
2199 r1
= tor_malloc_zero(sizeof(routerinfo_t
));
2200 r1
->address
= tor_strdup("18.244.0.1");
2201 r1
->addr
= 0xc0a80001u
; /* 192.168.0.1 */
2202 r1
->cache_info
.published_on
= 0;
2204 r1
->dir_port
= 9003;
2205 r1
->onion_pkey
= crypto_pk_dup_key(pk1
);
2206 r1
->identity_pkey
= crypto_pk_dup_key(pk2
);
2207 r1
->bandwidthrate
= 1000;
2208 r1
->bandwidthburst
= 5000;
2209 r1
->bandwidthcapacity
= 10000;
2210 r1
->exit_policy
= NULL
;
2211 r1
->nickname
= tor_strdup("Magri");
2212 r1
->platform
= tor_strdup(platform
);
2214 ex1
= tor_malloc_zero(sizeof(addr_policy_t
));
2215 ex2
= tor_malloc_zero(sizeof(addr_policy_t
));
2216 ex1
->policy_type
= ADDR_POLICY_ACCEPT
;
2219 ex1
->prt_min
= ex1
->prt_max
= 80;
2220 ex2
->policy_type
= ADDR_POLICY_REJECT
;
2221 ex2
->addr
= 18 << 24;
2223 ex2
->prt_min
= ex2
->prt_max
= 24;
2224 r2
= tor_malloc_zero(sizeof(routerinfo_t
));
2225 r2
->address
= tor_strdup("1.1.1.1");
2226 r2
->addr
= 0x0a030201u
; /* 10.3.2.1 */
2227 r2
->platform
= tor_strdup(platform
);
2228 r2
->cache_info
.published_on
= 5;
2231 r2
->onion_pkey
= crypto_pk_dup_key(pk2
);
2232 r2
->identity_pkey
= crypto_pk_dup_key(pk1
);
2233 r2
->bandwidthrate
= r2
->bandwidthburst
= r2
->bandwidthcapacity
= 3000;
2234 r2
->exit_policy
= smartlist_create();
2235 smartlist_add(r2
->exit_policy
, &ex2
);
2236 smartlist_add(r2
->exit_policy
, &ex1
);
2237 r2
->nickname
= tor_strdup("Fred");
2239 test_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
2241 test_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
2243 test_assert(!crypto_pk_write_public_key_to_string(pk3
, &pk3_str
,
2246 memset(buf
, 0, 2048);
2247 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
2249 strlcpy(buf2
, "router Magri 18.244.0.1 9000 0 9003\n"
2250 "platform Tor "VERSION
" on ", sizeof(buf2
));
2251 strlcat(buf2
, get_uname(), sizeof(buf2
));
2253 "opt protocols Link 1 Circuit 1\n"
2254 "published 1970-01-01 00:00:00\n"
2255 "opt fingerprint ", sizeof(buf2
));
2256 test_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
2257 strlcat(buf2
, fingerprint
, sizeof(buf2
));
2258 strlcat(buf2
, "\nuptime 0\n"
2259 /* XXX the "0" above is hardcoded, but even if we made it reflect
2260 * uptime, that still wouldn't make it right, because the two
2261 * descriptors might be made on different seconds... hm. */
2262 "bandwidth 1000 5000 10000\n"
2263 "opt extra-info-digest 0000000000000000000000000000000000000000\n"
2264 "onion-key\n", sizeof(buf2
));
2265 strlcat(buf2
, pk1_str
, sizeof(buf2
));
2266 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
2267 strlcat(buf2
, pk2_str
, sizeof(buf2
));
2268 strlcat(buf2
, "router-signature\n", sizeof(buf2
));
2269 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
2272 test_streq(buf
, buf2
);
2274 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
2276 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1,0,NULL
);
2278 test_streq(rp1
->address
, r1
->address
);
2279 test_eq(rp1
->or_port
, r1
->or_port
);
2280 //test_eq(rp1->dir_port, r1->dir_port);
2281 test_eq(rp1
->bandwidthrate
, r1
->bandwidthrate
);
2282 test_eq(rp1
->bandwidthburst
, r1
->bandwidthburst
);
2283 test_eq(rp1
->bandwidthcapacity
, r1
->bandwidthcapacity
);
2284 test_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
2285 test_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
2286 test_assert(rp1
->exit_policy
== NULL
);
2289 /* XXX Once we have exit policies, test this again. XXX */
2290 strlcpy(buf2
, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2
));
2291 strlcat(buf2
, pk2_str
, sizeof(buf2
));
2292 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
2293 strlcat(buf2
, pk1_str
, sizeof(buf2
));
2294 strlcat(buf2
, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2
));
2295 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk2
)>0);
2296 test_streq(buf
, buf2
);
2299 rp2
= router_parse_entry_from_string(&cp
,1);
2301 test_streq(rp2
->address
, r2
.address
);
2302 test_eq(rp2
->or_port
, r2
.or_port
);
2303 test_eq(rp2
->dir_port
, r2
.dir_port
);
2304 test_eq(rp2
->bandwidth
, r2
.bandwidth
);
2305 test_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
2306 test_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
2307 test_eq(rp2
->exit_policy
->policy_type
, EXIT_POLICY_ACCEPT
);
2308 test_streq(rp2
->exit_policy
->string
, "accept *:80");
2309 test_streq(rp2
->exit_policy
->address
, "*");
2310 test_streq(rp2
->exit_policy
->port
, "80");
2311 test_eq(rp2
->exit_policy
->next
->policy_type
, EXIT_POLICY_REJECT
);
2312 test_streq(rp2
->exit_policy
->next
->string
, "reject 18.*:24");
2313 test_streq(rp2
->exit_policy
->next
->address
, "18.*");
2314 test_streq(rp2
->exit_policy
->next
->port
, "24");
2315 test_assert(rp2
->exit_policy
->next
->next
== NULL
);
2317 /* Okay, now for the directories. */
2319 fingerprint_list
= smartlist_create();
2320 crypto_pk_get_fingerprint(pk2
, buf
, 1);
2321 add_fingerprint_to_dir("Magri", buf
, fingerprint_list
);
2322 crypto_pk_get_fingerprint(pk1
, buf
, 1);
2323 add_fingerprint_to_dir("Fred", buf
, fingerprint_list
);
2329 /* XXXX NM re-enable. */
2330 /* Make sure routers aren't too far in the past any more. */
2331 r1
->cache_info
.published_on
= time(NULL
);
2332 r2
->cache_info
.published_on
= time(NULL
)-3*60*60;
2333 test_assert(router_dump_router_to_string(buf
, 2048, r1
, pk2
)>0);
2334 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
2335 test_assert(router_dump_router_to_string(buf
, 2048, r2
, pk1
)>0);
2336 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
2337 get_options()->Nickname
= tor_strdup("DirServer");
2338 test_assert(!dirserv_dump_directory_to_string(&cp
,pk3
, 0));
2339 crypto_pk_get_digest(pk3
, d
);
2340 test_assert(!router_parse_directory(cp
));
2341 test_eq(2, smartlist_len(dir1
->routers
));
2345 dirserv_free_fingerprint_list();
2350 if (pk1
) crypto_free_pk_env(pk1
);
2351 if (pk2
) crypto_free_pk_env(pk2
);
2352 if (pk3
) crypto_free_pk_env(pk3
);
2353 if (rp1
) routerinfo_free(rp1
);
2354 if (rp2
) routerinfo_free(rp2
);
2355 tor_free(dir1
); /* XXXX And more !*/
2356 tor_free(dir2
); /* And more !*/
2357 routerinfo_free(r1
);
2358 // routerinfo_free(r2); XXX020 this line crashes on openbsd and netbsd
2360 /* Try out version parsing functionality */
2361 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1
));
2362 test_eq(0, ver1
.major
);
2363 test_eq(3, ver1
.minor
);
2364 test_eq(4, ver1
.micro
);
2365 test_eq(VER_PRE
, ver1
.status
);
2366 test_eq(2, ver1
.patchlevel
);
2367 test_eq(0, tor_version_parse("0.3.4rc1", &ver1
));
2368 test_eq(0, ver1
.major
);
2369 test_eq(3, ver1
.minor
);
2370 test_eq(4, ver1
.micro
);
2371 test_eq(VER_RC
, ver1
.status
);
2372 test_eq(1, ver1
.patchlevel
);
2373 test_eq(0, tor_version_parse("1.3.4", &ver1
));
2374 test_eq(1, ver1
.major
);
2375 test_eq(3, ver1
.minor
);
2376 test_eq(4, ver1
.micro
);
2377 test_eq(VER_RELEASE
, ver1
.status
);
2378 test_eq(0, ver1
.patchlevel
);
2379 test_eq(0, tor_version_parse("1.3.4.999", &ver1
));
2380 test_eq(1, ver1
.major
);
2381 test_eq(3, ver1
.minor
);
2382 test_eq(4, ver1
.micro
);
2383 test_eq(VER_RELEASE
, ver1
.status
);
2384 test_eq(999, ver1
.patchlevel
);
2385 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1
));
2386 test_eq(0, ver1
.major
);
2387 test_eq(1, ver1
.minor
);
2388 test_eq(2, ver1
.micro
);
2389 test_eq(4, ver1
.patchlevel
);
2390 test_eq(VER_RELEASE
, ver1
.status
);
2391 test_streq("alpha", ver1
.status_tag
);
2392 test_eq(0, tor_version_parse("0.1.2.4", &ver1
));
2393 test_eq(0, ver1
.major
);
2394 test_eq(1, ver1
.minor
);
2395 test_eq(2, ver1
.micro
);
2396 test_eq(4, ver1
.patchlevel
);
2397 test_eq(VER_RELEASE
, ver1
.status
);
2398 test_streq("", ver1
.status_tag
);
2400 #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
2401 #define test_v_i_o(val, ver, lst) \
2402 test_eq_vs(val, tor_version_is_obsolete(ver, lst))
2404 /* make sure tor_version_is_obsolete() works */
2405 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
2406 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
2407 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
2408 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
2409 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
2410 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
2411 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
2412 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
2413 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
2414 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
2415 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
2416 /* Not on list, but newer than any in same series. */
2417 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
2418 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2419 /* Series newer than any on list. */
2420 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");
2421 /* Series older than any on list. */
2422 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");
2423 /* Not on list, not newer than any on same series. */
2424 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
2425 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2426 /* On list, not newer than any on same series. */
2427 test_v_i_o(VS_UNRECOMMENDED
,
2428 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
2429 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
2430 test_eq(1, tor_version_as_new_as(
2431 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
2432 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
2434 test_eq(0, tor_version_as_new_as(
2435 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
2436 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
2438 /* Now try svn revisions. */
2439 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
2440 "Tor 0.2.1.0-dev (r99)"));
2441 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
2442 "Tor 0.2.1.0-dev (r99) on Hal 9000"));
2443 test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
2444 "Tor 0.2.1.0-dev on Colossus"));
2445 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
2446 "Tor 0.2.1.0-dev (r100)"));
2447 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
2448 "Tor 0.2.1.0-dev (r100) on AM"));
2449 test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
2450 "Tor 0.2.1.0-dev (r99)"));
2451 test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
2452 "Tor 0.2.1.0-dev (r99)"));
2455 extern const char AUTHORITY_CERT_1
[];
2456 extern const char AUTHORITY_SIGNKEY_1
[];
2457 extern const char AUTHORITY_CERT_2
[];
2458 extern const char AUTHORITY_SIGNKEY_2
[];
2459 extern const char AUTHORITY_CERT_3
[];
2460 extern const char AUTHORITY_SIGNKEY_3
[];
2463 test_same_voter(networkstatus_voter_info_t
*v1
,
2464 networkstatus_voter_info_t
*v2
)
2466 test_streq(v1
->nickname
, v2
->nickname
);
2467 test_memeq(v1
->identity_digest
, v2
->identity_digest
, DIGEST_LEN
);
2468 test_streq(v1
->address
, v2
->address
);
2469 test_eq(v1
->addr
, v2
->addr
);
2470 test_eq(v1
->dir_port
, v2
->dir_port
);
2471 test_eq(v1
->or_port
, v2
->or_port
);
2472 test_streq(v1
->contact
, v2
->contact
);
2473 test_memeq(v1
->vote_digest
, v2
->vote_digest
, DIGEST_LEN
);
2477 test_util_order_functions(void)
2480 // int a=12,b=24,c=25,d=60,e=77;
2482 #define median() median_int(lst, n)
2485 test_eq(12, median()); /* 12 */
2487 //smartlist_shuffle(sl);
2488 test_eq(12, median()); /* 12, 77 */
2490 //smartlist_shuffle(sl);
2491 test_eq(77, median()); /* 12, 77, 77 */
2493 test_eq(24, median()); /* 12,24,77,77 */
2497 //smartlist_shuffle(sl);
2498 test_eq(25, median()); /* 12,12,24,25,60,77,77 */
2503 test_v3_networkstatus(void)
2505 authority_cert_t
*cert1
, *cert2
, *cert3
;
2506 crypto_pk_env_t
*sign_skey_1
, *sign_skey_2
, *sign_skey_3
;
2508 time_t now
= time(NULL
);
2509 networkstatus_voter_info_t
*voter
;
2510 networkstatus_t
*vote
, *v1
, *v2
, *v3
, *con
;
2511 vote_routerstatus_t
*vrs
;
2513 char *v1_text
, *v2_text
, *v3_text
, *consensus_text
, *cp
;
2514 smartlist_t
*votes
= smartlist_create();
2516 /* Parse certificates and keys. */
2517 cert1
= authority_cert_parse_from_string(AUTHORITY_CERT_1
, NULL
);
2519 cert2
= authority_cert_parse_from_string(AUTHORITY_CERT_2
, NULL
);
2521 cert3
= authority_cert_parse_from_string(AUTHORITY_CERT_3
, NULL
);
2523 sign_skey_1
= crypto_new_pk_env();
2524 sign_skey_2
= crypto_new_pk_env();
2525 sign_skey_3
= crypto_new_pk_env();
2527 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1
,
2528 AUTHORITY_SIGNKEY_1
));
2529 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2
,
2530 AUTHORITY_SIGNKEY_2
));
2531 test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3
,
2532 AUTHORITY_SIGNKEY_3
));
2534 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, cert1
->signing_key
));
2535 test_assert(!crypto_pk_cmp_keys(sign_skey_2
, cert2
->signing_key
));
2538 * Set up a vote; generate it; try to parse it.
2540 vote
= tor_malloc_zero(sizeof(networkstatus_t
));
2542 vote
->published
= now
;
2543 vote
->valid_after
= now
+1000;
2544 vote
->fresh_until
= now
+2000;
2545 vote
->valid_until
= now
+3000;
2546 vote
->vote_seconds
= 100;
2547 vote
->dist_seconds
= 200;
2548 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.15");
2549 vote
->server_versions
= tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
2550 vote
->known_flags
= smartlist_create();
2551 smartlist_split_string(vote
->known_flags
,
2552 "Authority Exit Fast Guard Running Stable V2Dir Valid",
2553 0, SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2554 vote
->voters
= smartlist_create();
2555 voter
= tor_malloc_zero(sizeof(networkstatus_voter_info_t
));
2556 voter
->nickname
= tor_strdup("Voter1");
2557 voter
->address
= tor_strdup("1.2.3.4");
2558 voter
->addr
= 0x01020304;
2559 voter
->dir_port
= 80;
2560 voter
->or_port
= 9000;
2561 voter
->contact
= tor_strdup("voter@example.com");
2562 crypto_pk_get_digest(cert1
->identity_key
, voter
->identity_digest
);
2563 smartlist_add(vote
->voters
, voter
);
2564 vote
->cert
= authority_cert_dup(cert1
);
2565 vote
->routerstatus_list
= smartlist_create();
2566 /* add the first routerstatus. */
2567 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2569 vrs
->version
= tor_strdup("0.1.2.14");
2570 rs
->published_on
= now
-1500;
2571 strlcpy(rs
->nickname
, "router2", sizeof(rs
->nickname
));
2572 memset(rs
->identity_digest
, 3, DIGEST_LEN
);
2573 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2574 rs
->addr
= 0x99008801;
2576 rs
->dir_port
= 8000;
2577 /* all flags cleared */
2578 smartlist_add(vote
->routerstatus_list
, vrs
);
2579 /* add the second routerstatus. */
2580 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2582 vrs
->version
= tor_strdup("0.2.0.5");
2583 rs
->published_on
= now
-1000;
2584 strlcpy(rs
->nickname
, "router1", sizeof(rs
->nickname
));
2585 memset(rs
->identity_digest
, 5, DIGEST_LEN
);
2586 memset(rs
->descriptor_digest
, 77, DIGEST_LEN
);
2587 rs
->addr
= 0x99009901;
2590 rs
->is_exit
= rs
->is_stable
= rs
->is_fast
= rs
->is_running
=
2591 rs
->is_valid
= rs
->is_v2_dir
= rs
->is_possible_guard
= 1;
2592 smartlist_add(vote
->routerstatus_list
, vrs
);
2593 /* add the third routerstatus. */
2594 vrs
= tor_malloc_zero(sizeof(vote_routerstatus_t
));
2596 vrs
->version
= tor_strdup("0.1.0.3");
2597 rs
->published_on
= now
-1000;
2598 strlcpy(rs
->nickname
, "router3", sizeof(rs
->nickname
));
2599 memset(rs
->identity_digest
, 33, DIGEST_LEN
);
2600 memset(rs
->descriptor_digest
, 78, DIGEST_LEN
);
2601 rs
->addr
= 0xAA009901;
2603 rs
->dir_port
= 9999;
2604 rs
->is_authority
= rs
->is_exit
= rs
->is_stable
= rs
->is_fast
=
2605 rs
->is_running
= rs
->is_valid
= rs
->is_v2_dir
= rs
->is_possible_guard
= 1;
2606 smartlist_add(vote
->routerstatus_list
, vrs
);
2608 /* dump the vote and try to parse it. */
2609 v1_text
= format_networkstatus_vote(sign_skey_1
, vote
);
2610 test_assert(v1_text
);
2611 v1
= networkstatus_parse_vote_from_string(v1_text
, NULL
, 1);
2614 /* Make sure the parsed thing was right. */
2615 test_eq(v1
->is_vote
, 1);
2616 test_eq(v1
->published
, vote
->published
);
2617 test_eq(v1
->valid_after
, vote
->valid_after
);
2618 test_eq(v1
->fresh_until
, vote
->fresh_until
);
2619 test_eq(v1
->valid_until
, vote
->valid_until
);
2620 test_eq(v1
->vote_seconds
, vote
->vote_seconds
);
2621 test_eq(v1
->dist_seconds
, vote
->dist_seconds
);
2622 test_streq(v1
->client_versions
, vote
->client_versions
);
2623 test_streq(v1
->server_versions
, vote
->server_versions
);
2624 test_assert(v1
->voters
&& smartlist_len(v1
->voters
));
2625 voter
= smartlist_get(v1
->voters
, 0);
2626 test_streq(voter
->nickname
, "Voter1");
2627 test_streq(voter
->address
, "1.2.3.4");
2628 test_eq(voter
->addr
, 0x01020304);
2629 test_eq(voter
->dir_port
, 80);
2630 test_eq(voter
->or_port
, 9000);
2631 test_streq(voter
->contact
, "voter@example.com");
2632 test_assert(v1
->cert
);
2633 test_assert(!crypto_pk_cmp_keys(sign_skey_1
, v1
->cert
->signing_key
));
2634 cp
= smartlist_join_strings(v1
->known_flags
, ":", 0, NULL
);
2635 test_streq(cp
, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
2637 test_eq(smartlist_len(v1
->routerstatus_list
), 3);
2638 /* Check the first routerstatus. */
2639 vrs
= smartlist_get(v1
->routerstatus_list
, 0);
2641 test_streq(vrs
->version
, "0.1.2.14");
2642 test_eq(rs
->published_on
, now
-1500);
2643 test_streq(rs
->nickname
, "router2");
2644 test_memeq(rs
->identity_digest
,
2645 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2647 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2648 test_eq(rs
->addr
, 0x99008801);
2649 test_eq(rs
->or_port
, 443);
2650 test_eq(rs
->dir_port
, 8000);
2651 test_eq(vrs
->flags
, U64_LITERAL(0));
2652 /* Check the second routerstatus. */
2653 vrs
= smartlist_get(v1
->routerstatus_list
, 1);
2655 test_streq(vrs
->version
, "0.2.0.5");
2656 test_eq(rs
->published_on
, now
-1000);
2657 test_streq(rs
->nickname
, "router1");
2658 test_memeq(rs
->identity_digest
,
2659 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2661 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2662 test_eq(rs
->addr
, 0x99009901);
2663 test_eq(rs
->or_port
, 443);
2664 test_eq(rs
->dir_port
, 0);
2665 test_eq(vrs
->flags
, U64_LITERAL(254)); // all flags except "authority."
2667 /* Generate second vote. It disagrees on some of the times,
2668 * and doesn't list versions, and knows some crazy flags */
2669 vote
->published
= now
+1;
2670 vote
->fresh_until
= now
+3005;
2671 vote
->dist_seconds
= 300;
2672 authority_cert_free(vote
->cert
);
2673 vote
->cert
= authority_cert_dup(cert2
);
2674 tor_free(vote
->client_versions
);
2675 tor_free(vote
->server_versions
);
2676 voter
= smartlist_get(vote
->voters
, 0);
2677 tor_free(voter
->nickname
);
2678 tor_free(voter
->address
);
2679 voter
->nickname
= tor_strdup("Voter2");
2680 voter
->address
= tor_strdup("2.3.4.5");
2681 voter
->addr
= 0x02030405;
2682 crypto_pk_get_digest(cert2
->identity_key
, voter
->identity_digest
);
2683 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfCheese"));
2684 smartlist_add(vote
->known_flags
, tor_strdup("MadeOfTin"));
2685 smartlist_sort_strings(vote
->known_flags
);
2686 vrs
= smartlist_get(vote
->routerstatus_list
, 2);
2687 smartlist_del_keeporder(vote
->routerstatus_list
, 2);
2688 tor_free(vrs
->version
);
2690 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2691 vrs
->status
.is_fast
= 1;
2692 /* generate and parse. */
2693 v2_text
= format_networkstatus_vote(sign_skey_2
, vote
);
2694 test_assert(v2_text
);
2695 v2
= networkstatus_parse_vote_from_string(v2_text
, NULL
, 1);
2697 /* Check that flags come out right.*/
2698 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2699 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2700 "Running:Stable:V2Dir:Valid");
2702 vrs
= smartlist_get(v2
->routerstatus_list
, 1);
2703 /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
2704 test_eq(vrs
->flags
, U64_LITERAL(974));
2706 /* Generate the third vote. */
2707 vote
->published
= now
;
2708 vote
->fresh_until
= now
+2003;
2709 vote
->dist_seconds
= 250;
2710 authority_cert_free(vote
->cert
);
2711 vote
->cert
= authority_cert_dup(cert3
);
2712 vote
->client_versions
= tor_strdup("0.1.2.14,0.1.2.17");
2713 vote
->server_versions
= tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
2714 voter
= smartlist_get(vote
->voters
, 0);
2715 tor_free(voter
->nickname
);
2716 tor_free(voter
->address
);
2717 voter
->nickname
= tor_strdup("Voter3");
2718 voter
->address
= tor_strdup("3.4.5.6");
2719 voter
->addr
= 0x03040506;
2720 crypto_pk_get_digest(cert3
->identity_key
, voter
->identity_digest
);
2721 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2722 smartlist_del_keeporder(vote
->routerstatus_list
, 0);
2723 tor_free(vrs
->version
);
2725 vrs
= smartlist_get(vote
->routerstatus_list
, 0);
2726 memset(vrs
->status
.descriptor_digest
, (int)'Z', DIGEST_LEN
);
2728 v3_text
= format_networkstatus_vote(sign_skey_3
, vote
);
2729 test_assert(v3_text
);
2731 v3
= networkstatus_parse_vote_from_string(v3_text
, NULL
, 1);
2734 /* Compute a consensus as voter 3. */
2735 smartlist_add(votes
, v3
);
2736 smartlist_add(votes
, v1
);
2737 smartlist_add(votes
, v2
);
2738 consensus_text
= networkstatus_compute_consensus(votes
, 3,
2739 cert3
->identity_key
,
2741 test_assert(consensus_text
);
2742 con
= networkstatus_parse_vote_from_string(consensus_text
, NULL
, 0);
2744 //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
2745 // v1_text, v2_text, v3_text);
2747 /* Check consensus contents. */
2748 test_assert(!con
->is_vote
);
2749 test_eq(con
->published
, 0); /* this field only appears in votes. */
2750 test_eq(con
->valid_after
, now
+1000);
2751 test_eq(con
->fresh_until
, now
+2003); /* median */
2752 test_eq(con
->valid_until
, now
+3000);
2753 test_eq(con
->vote_seconds
, 100);
2754 test_eq(con
->dist_seconds
, 250); /* median */
2755 test_streq(con
->client_versions
, "0.1.2.14");
2756 test_streq(con
->server_versions
, "0.1.2.15,0.1.2.16");
2757 cp
= smartlist_join_strings(v2
->known_flags
, ":", 0, NULL
);
2758 test_streq(cp
, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
2759 "Running:Stable:V2Dir:Valid");
2761 test_eq(3, smartlist_len(con
->voters
));
2762 /* The voter id digests should be in this order. */
2763 test_assert(memcmp(cert2
->cache_info
.identity_digest
,
2764 cert3
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2765 test_assert(memcmp(cert3
->cache_info
.identity_digest
,
2766 cert1
->cache_info
.identity_digest
,DIGEST_LEN
)<0);
2767 test_same_voter(smartlist_get(con
->voters
, 0),
2768 smartlist_get(v2
->voters
, 0));
2769 test_same_voter(smartlist_get(con
->voters
, 1),
2770 smartlist_get(v3
->voters
, 0));
2771 test_same_voter(smartlist_get(con
->voters
, 2),
2772 smartlist_get(v1
->voters
, 0));
2774 test_assert(!con
->cert
);
2775 test_eq(2, smartlist_len(con
->routerstatus_list
));
2776 /* There should be two listed routers: one with identity 3, one with
2778 /* This one showed up in 2 digests. */
2779 rs
= smartlist_get(con
->routerstatus_list
, 0);
2780 test_memeq(rs
->identity_digest
,
2781 "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
2783 test_memeq(rs
->descriptor_digest
, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN
);
2784 test_assert(!rs
->is_authority
);
2785 test_assert(!rs
->is_exit
);
2786 test_assert(!rs
->is_fast
);
2787 test_assert(!rs
->is_possible_guard
);
2788 test_assert(!rs
->is_stable
);
2789 test_assert(!rs
->is_running
);
2790 test_assert(!rs
->is_v2_dir
);
2791 test_assert(!rs
->is_valid
);
2792 test_assert(!rs
->is_named
);
2793 /* XXXX check version */
2795 rs
= smartlist_get(con
->routerstatus_list
, 1);
2796 /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'. */
2797 test_memeq(rs
->identity_digest
,
2798 "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
2800 test_streq(rs
->nickname
, "router1");
2801 test_memeq(rs
->descriptor_digest
, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN
);
2802 test_eq(rs
->published_on
, now
-1000);
2803 test_eq(rs
->addr
, 0x99009901);
2804 test_eq(rs
->or_port
, 443);
2805 test_eq(rs
->dir_port
, 0);
2806 test_assert(!rs
->is_authority
);
2807 test_assert(rs
->is_exit
);
2808 test_assert(rs
->is_fast
);
2809 test_assert(rs
->is_possible_guard
);
2810 test_assert(rs
->is_stable
);
2811 test_assert(rs
->is_running
);
2812 test_assert(rs
->is_v2_dir
);
2813 test_assert(rs
->is_valid
);
2814 test_assert(!rs
->is_named
);
2815 /* XXXX check version */
2817 /* Check signatures. the first voter hasn't got one. The second one
2818 * does: validate it. */
2819 voter
= smartlist_get(con
->voters
, 0);
2820 test_assert(!voter
->signature
);
2821 test_assert(!voter
->good_signature
);
2822 test_assert(!voter
->bad_signature
);
2824 voter
= smartlist_get(con
->voters
, 1);
2825 test_assert(voter
->signature
);
2826 test_assert(!voter
->good_signature
);
2827 test_assert(!voter
->bad_signature
);
2828 test_assert(!networkstatus_check_voter_signature(con
,
2829 smartlist_get(con
->voters
, 1),
2831 test_assert(voter
->signature
);
2832 test_assert(voter
->good_signature
);
2833 test_assert(!voter
->bad_signature
);
2836 char *consensus_text2
, *consensus_text3
;
2837 networkstatus_t
*con2
, *con3
;
2838 char *detached_text1
, *detached_text2
;
2839 ns_detached_signatures_t
*dsig1
, *dsig2
;
2840 const char *msg
=NULL
;
2841 /* Compute the other two signed consensuses. */
2842 smartlist_shuffle(votes
);
2843 consensus_text2
= networkstatus_compute_consensus(votes
, 3,
2844 cert2
->identity_key
,
2846 smartlist_shuffle(votes
);
2847 consensus_text3
= networkstatus_compute_consensus(votes
, 3,
2848 cert1
->identity_key
,
2850 test_assert(consensus_text2
);
2851 test_assert(consensus_text3
);
2852 con2
= networkstatus_parse_vote_from_string(consensus_text2
, NULL
, 0);
2853 con3
= networkstatus_parse_vote_from_string(consensus_text3
, NULL
, 0);
2857 /* All three should have the same digest. */
2858 test_memeq(con
->networkstatus_digest
, con2
->networkstatus_digest
,
2860 test_memeq(con
->networkstatus_digest
, con3
->networkstatus_digest
,
2863 /* Extract a detached signature from con3. */
2864 detached_text1
= networkstatus_get_detached_signatures(con3
);
2865 tor_assert(detached_text1
);
2866 /* Try to parse it. */
2867 dsig1
= networkstatus_parse_detached_signatures(detached_text1
, NULL
);
2870 /* Are parsed values as expected? */
2871 test_eq(dsig1
->valid_after
, con3
->valid_after
);
2872 test_eq(dsig1
->fresh_until
, con3
->fresh_until
);
2873 test_eq(dsig1
->valid_until
, con3
->valid_until
);
2874 test_memeq(dsig1
->networkstatus_digest
, con3
->networkstatus_digest
,
2876 test_eq(1, smartlist_len(dsig1
->signatures
));
2877 voter
= smartlist_get(dsig1
->signatures
, 0);
2878 test_memeq(voter
->identity_digest
, cert1
->cache_info
.identity_digest
,
2881 /* Try adding it to con2. */
2882 detached_text2
= networkstatus_get_detached_signatures(con2
);
2883 test_eq(1, networkstatus_add_detached_signatures(con2
, dsig1
, &msg
));
2884 tor_free(detached_text2
);
2885 detached_text2
= networkstatus_get_detached_signatures(con2
);
2886 //printf("\n<%s>\n", detached_text2);
2887 dsig2
= networkstatus_parse_detached_signatures(detached_text2
, NULL
);
2891 SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
2893 base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
2897 test_eq(2, smartlist_len(dsig2
->signatures
));
2899 /* Try adding to con2 twice; verify that nothing changes. */
2900 test_eq(0, networkstatus_add_detached_signatures(con2
, dsig1
, &msg
));
2903 test_eq(2, networkstatus_add_detached_signatures(con
, dsig2
, &msg
));
2904 /* Check signatures */
2905 test_assert(!networkstatus_check_voter_signature(con
,
2906 smartlist_get(con
->voters
, 0),
2908 test_assert(!networkstatus_check_voter_signature(con
,
2909 smartlist_get(con
->voters
, 2),
2912 networkstatus_vote_free(con2
);
2913 networkstatus_vote_free(con3
);
2914 tor_free(consensus_text2
);
2915 tor_free(consensus_text3
);
2916 tor_free(detached_text1
);
2917 tor_free(detached_text2
);
2918 ns_detached_signatures_free(dsig1
);
2919 ns_detached_signatures_free(dsig2
);
2922 smartlist_free(votes
);
2926 tor_free(consensus_text
);
2927 networkstatus_vote_free(vote
);
2928 networkstatus_vote_free(v1
);
2929 networkstatus_vote_free(v2
);
2930 networkstatus_vote_free(v3
);
2931 networkstatus_vote_free(con
);
2932 crypto_free_pk_env(sign_skey_1
);
2933 crypto_free_pk_env(sign_skey_2
);
2934 crypto_free_pk_env(sign_skey_3
);
2935 authority_cert_free(cert1
);
2936 authority_cert_free(cert2
);
2937 authority_cert_free(cert3
);
2943 smartlist_t
*policy
, *policy2
;
2948 policy
= smartlist_create();
2950 p
= router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
2951 test_eq(ADDR_POLICY_REJECT
, p
->policy_type
);
2952 tor_addr_from_ipv4(&tar
, 0xc0a80000u
);
2953 test_assert(p
->addr
== 0xc0a80000u
);
2954 test_eq(16, p
->maskbits
);
2955 test_eq(1, p
->prt_min
);
2956 test_eq(65535, p
->prt_max
);
2958 smartlist_add(policy
, p
);
2960 test_assert(ADDR_POLICY_ACCEPTED
==
2961 compare_addr_to_addr_policy(0x01020304u
, 2, policy
));
2962 test_assert(ADDR_POLICY_PROBABLY_ACCEPTED
==
2963 compare_addr_to_addr_policy(0, 2, policy
));
2964 test_assert(ADDR_POLICY_REJECTED
==
2965 compare_addr_to_addr_policy(0xc0a80102, 2, policy
));
2968 test_assert(0 == policies_parse_exit_policy(NULL
, &policy2
, 1, NULL
));
2969 test_assert(policy2
);
2971 test_assert(!exit_policy_is_general_exit(policy
));
2972 test_assert(exit_policy_is_general_exit(policy2
));
2974 test_assert(cmp_addr_policies(policy
, policy2
));
2975 test_assert(!cmp_addr_policies(policy2
, policy2
));
2977 test_assert(!policy_is_reject_star(policy2
));
2978 test_assert(policy_is_reject_star(policy
));
2980 addr_policy_list_free(policy
);
2981 addr_policy_list_free(policy2
);
2983 /* make sure compacting logic works. */
2985 line
.key
= (char*)"foo";
2986 line
.value
= (char*)"accept *:80,reject private:*,reject *:*";
2988 test_assert(0 == policies_parse_exit_policy(&line
, &policy
, 0, NULL
));
2989 test_assert(policy
);
2990 //test_streq(policy->string, "accept *:80");
2991 //test_streq(policy->next->string, "reject *:*");
2992 test_eq(smartlist_len(policy
), 2);
2994 addr_policy_list_free(policy
);
3000 char address1
[] = "fooaddress.onion";
3001 char address2
[] = "aaaaaaaaaaaaaaaa.onion";
3002 char address3
[] = "fooaddress.exit";
3003 char address4
[] = "www.torproject.org";
3004 rend_service_descriptor_t
*d1
, *d2
;
3007 crypto_pk_env_t
*pk1
, *pk2
;
3010 pk1
= pk_generate(0);
3011 pk2
= pk_generate(1);
3013 /* Test unversioned (v0) descriptor */
3014 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
3015 d1
->pk
= crypto_pk_dup_key(pk1
);
3017 d1
->timestamp
= now
;
3019 d1
->intro_nodes
= smartlist_create();
3020 for (i
= 0; i
< 3; i
++) {
3021 rend_intro_point_t
*intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
3022 intro
->extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
3023 crypto_rand(intro
->extend_info
->identity_digest
, DIGEST_LEN
);
3024 intro
->extend_info
->nickname
[0] = '$';
3025 base16_encode(intro
->extend_info
->nickname
+1, HEX_DIGEST_LEN
+1,
3026 intro
->extend_info
->identity_digest
, DIGEST_LEN
);
3027 smartlist_add(d1
->intro_nodes
, intro
);
3029 test_assert(! rend_encode_service_descriptor(d1
, pk1
, &encoded
, &len
));
3030 d2
= rend_parse_service_descriptor(encoded
, len
);
3033 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
3034 test_eq(d2
->timestamp
, now
);
3035 test_eq(d2
->version
, 0);
3036 test_eq(d2
->protocols
, 1<<2);
3037 test_eq(smartlist_len(d2
->intro_nodes
), 3);
3038 for (i
= 0; i
< 3; i
++) {
3039 rend_intro_point_t
*intro1
= smartlist_get(d1
->intro_nodes
, i
);
3040 rend_intro_point_t
*intro2
= smartlist_get(d2
->intro_nodes
, i
);
3041 test_streq(intro1
->extend_info
->nickname
,
3042 intro2
->extend_info
->nickname
);
3045 rend_service_descriptor_free(d1
);
3046 rend_service_descriptor_free(d2
);
3049 test_assert(BAD_HOSTNAME
== parse_extended_hostname(address1
));
3050 test_assert(ONION_HOSTNAME
== parse_extended_hostname(address2
));
3051 test_assert(EXIT_HOSTNAME
== parse_extended_hostname(address3
));
3052 test_assert(NORMAL_HOSTNAME
== parse_extended_hostname(address4
));
3054 crypto_free_pk_env(pk1
);
3055 crypto_free_pk_env(pk2
);
3063 crypto_cipher_env_t
*c
;
3064 struct timeval start
, end
;
3065 const int iters
= 100000;
3067 c
= crypto_new_cipher_env();
3068 crypto_cipher_generate_key(c
);
3069 crypto_cipher_encrypt_init_cipher(c
);
3070 for (len
= 1; len
<= 8192; len
*= 2) {
3071 b1
= tor_malloc_zero(len
);
3072 b2
= tor_malloc_zero(len
);
3073 tor_gettimeofday(&start
);
3074 for (i
= 0; i
< iters
; ++i
) {
3075 crypto_cipher_encrypt(c
, b1
, b2
, len
);
3077 tor_gettimeofday(&end
);
3080 nsec
= (uint64_t) tv_udiff(&start
,&end
);
3082 nsec
/= (iters
*len
);
3083 printf("%d bytes: "U64_FORMAT
" nsec per byte\n", len
,
3084 U64_PRINTF_ARG(nsec
));
3086 crypto_free_cipher_env(c
);
3090 test_util_mempool(void)
3093 smartlist_t
*allocated
;
3096 pool
= mp_pool_new(1, 100);
3097 test_assert(pool
->new_chunk_capacity
>= 100);
3098 test_assert(pool
->item_alloc_size
>= sizeof(void*)+1);
3099 mp_pool_destroy(pool
);
3101 pool
= mp_pool_new(241, 2500);
3102 test_assert(pool
->new_chunk_capacity
>= 10);
3103 test_assert(pool
->item_alloc_size
>= sizeof(void*)+241);
3104 test_eq(pool
->item_alloc_size
& 0x03, 0);
3105 test_assert(pool
->new_chunk_capacity
< 60);
3107 allocated
= smartlist_create();
3108 for (i
= 0; i
< 100000; ++i
) {
3109 if (smartlist_len(allocated
) < 20 || crypto_rand_int(2)) {
3110 void *m
= mp_pool_get(pool
);
3111 memset(m
, 0x09, 241);
3112 smartlist_add(allocated
, m
);
3113 //printf("%d: %p\n", i, m);
3114 //mp_pool_assert_ok(pool);
3116 int idx
= crypto_rand_int(smartlist_len(allocated
));
3117 void *m
= smartlist_get(allocated
, idx
);
3118 //printf("%d: free %p\n", i, m);
3119 smartlist_del(allocated
, idx
);
3121 //mp_pool_assert_ok(pool);
3123 if (crypto_rand_int(777)==0)
3124 mp_pool_clean(pool
, -1, 0);
3127 mp_pool_assert_ok(pool
);
3129 SMARTLIST_FOREACH(allocated
, void *, m
, mp_pool_release(m
));
3130 mp_pool_assert_ok(pool
);
3131 mp_pool_clean(pool
, 0, 0);
3132 mp_pool_assert_ok(pool
);
3133 mp_pool_destroy(pool
);
3134 smartlist_free(allocated
);
3138 test_util_datadir(void)
3143 f
= get_datadir_fname(NULL
);
3144 test_streq(f
, temp_dir
);
3146 f
= get_datadir_fname("state");
3147 tor_snprintf(buf
, sizeof(buf
), "%s"PATH_SEPARATOR
"state", temp_dir
);
3150 f
= get_datadir_fname2("cache", "thingy");
3151 tor_snprintf(buf
, sizeof(buf
),
3152 "%s"PATH_SEPARATOR
"cache"PATH_SEPARATOR
"thingy", temp_dir
);
3155 f
= get_datadir_fname2_suffix("cache", "thingy", ".foo");
3156 tor_snprintf(buf
, sizeof(buf
),
3157 "%s"PATH_SEPARATOR
"cache"PATH_SEPARATOR
"thingy.foo", temp_dir
);
3160 f
= get_datadir_fname_suffix("cache", ".foo");
3161 tor_snprintf(buf
, sizeof(buf
), "%s"PATH_SEPARATOR
"cache.foo",
3167 /* Test AES-CTR encryption and decryption with IV. */
3169 test_crypto_aes_iv(void)
3171 crypto_cipher_env_t
*cipher
;
3172 char *plain
, *encrypted1
, *encrypted2
, *decrypted1
, *decrypted2
;
3173 char plain_1
[1], plain_15
[15], plain_16
[16], plain_17
[17];
3174 char key1
[16], key2
[16];
3175 size_t encrypted_size
, decrypted_size
;
3176 plain
= tor_malloc(4095);
3177 encrypted1
= tor_malloc(4095 + 1 + 16);
3178 encrypted2
= tor_malloc(4095 + 1 + 16);
3179 decrypted1
= tor_malloc(4095 + 1);
3180 decrypted2
= tor_malloc(4095 + 1);
3181 crypto_rand(plain
, 4095);
3182 crypto_rand(key1
, 16);
3183 crypto_rand(key2
, 16);
3184 crypto_rand(plain_1
, 1);
3185 crypto_rand(plain_15
, 15);
3186 crypto_rand(plain_16
, 16);
3187 crypto_rand(plain_17
, 17);
3188 key1
[0] = key2
[0] + 128; /* Make sure that contents are different. */
3189 /* Encrypt and decrypt with the same key. */
3190 cipher
= crypto_create_init_cipher(key1
, 1);
3191 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted1
, 16 + 4095,
3193 crypto_free_cipher_env(cipher
);
3194 test_eq(encrypted_size
, 16 + 4095);
3195 cipher
= crypto_create_init_cipher(key1
, 0);
3196 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 4095,
3197 encrypted1
, encrypted_size
);
3198 crypto_free_cipher_env(cipher
);
3199 test_eq(decrypted_size
, 4095);
3200 test_memeq(plain
, decrypted1
, 4095);
3201 /* Encrypt a second time (with a new random initialization vector). */
3202 cipher
= crypto_create_init_cipher(key1
, 1);
3203 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted2
, 16 + 4095,
3205 crypto_free_cipher_env(cipher
);
3206 test_eq(encrypted_size
, 16 + 4095);
3207 cipher
= crypto_create_init_cipher(key1
, 0);
3208 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted2
, 4095,
3209 encrypted2
, encrypted_size
);
3210 crypto_free_cipher_env(cipher
);
3211 test_eq(decrypted_size
, 4095);
3212 test_memeq(plain
, decrypted2
, 4095);
3213 test_memneq(encrypted1
, encrypted2
, encrypted_size
);
3214 /* Decrypt with the wrong key. */
3215 cipher
= crypto_create_init_cipher(key2
, 0);
3216 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted2
, 4095,
3217 encrypted1
, encrypted_size
);
3218 crypto_free_cipher_env(cipher
);
3219 test_memneq(plain
, decrypted2
, encrypted_size
);
3220 /* Alter the initialization vector. */
3221 encrypted1
[0] += 42;
3222 cipher
= crypto_create_init_cipher(key1
, 0);
3223 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 4095,
3224 encrypted1
, encrypted_size
);
3225 crypto_free_cipher_env(cipher
);
3226 test_memneq(plain
, decrypted2
, 4095);
3227 /* Special length case: 1. */
3228 cipher
= crypto_create_init_cipher(key1
, 1);
3229 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted1
, 16 + 1,
3231 crypto_free_cipher_env(cipher
);
3232 test_eq(encrypted_size
, 16 + 1);
3233 cipher
= crypto_create_init_cipher(key1
, 0);
3234 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 1,
3235 encrypted1
, encrypted_size
);
3236 crypto_free_cipher_env(cipher
);
3237 test_eq(decrypted_size
, 1);
3238 test_memeq(plain_1
, decrypted1
, 1);
3239 /* Special length case: 15. */
3240 cipher
= crypto_create_init_cipher(key1
, 1);
3241 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted1
, 16 + 15,
3243 crypto_free_cipher_env(cipher
);
3244 test_eq(encrypted_size
, 16 + 15);
3245 cipher
= crypto_create_init_cipher(key1
, 0);
3246 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 15,
3247 encrypted1
, encrypted_size
);
3248 crypto_free_cipher_env(cipher
);
3249 test_eq(decrypted_size
, 15);
3250 test_memeq(plain_15
, decrypted1
, 15);
3251 /* Special length case: 16. */
3252 cipher
= crypto_create_init_cipher(key1
, 1);
3253 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted1
, 16 + 16,
3255 crypto_free_cipher_env(cipher
);
3256 test_eq(encrypted_size
, 16 + 16);
3257 cipher
= crypto_create_init_cipher(key1
, 0);
3258 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 16,
3259 encrypted1
, encrypted_size
);
3260 crypto_free_cipher_env(cipher
);
3261 test_eq(decrypted_size
, 16);
3262 test_memeq(plain_16
, decrypted1
, 16);
3263 /* Special length case: 17. */
3264 cipher
= crypto_create_init_cipher(key1
, 1);
3265 encrypted_size
= crypto_cipher_encrypt_with_iv(cipher
, encrypted1
, 16 + 17,
3267 crypto_free_cipher_env(cipher
);
3268 test_eq(encrypted_size
, 16 + 17);
3269 cipher
= crypto_create_init_cipher(key1
, 0);
3270 decrypted_size
= crypto_cipher_decrypt_with_iv(cipher
, decrypted1
, 17,
3271 encrypted1
, encrypted_size
);
3272 crypto_free_cipher_env(cipher
);
3273 test_eq(decrypted_size
, 17);
3274 test_memeq(plain_17
, decrypted1
, 17);
3277 tor_free(encrypted1
);
3278 tor_free(encrypted2
);
3279 tor_free(decrypted1
);
3280 tor_free(decrypted2
);
3283 /* Test base32 decoding. */
3285 test_crypto_base32_decode(void)
3287 char plain
[60], encoded
[96 + 1], decoded
[60];
3289 crypto_rand(plain
, 60);
3290 /* Encode and decode a random string. */
3291 base32_encode(encoded
, 96 + 1, plain
, 60);
3292 res
= base32_decode(decoded
, 60, encoded
, 96);
3294 test_memeq(plain
, decoded
, 60);
3295 /* Encode, uppercase, and decode a random string. */
3296 base32_encode(encoded
, 96 + 1, plain
, 60);
3297 tor_strupper(encoded
);
3298 res
= base32_decode(decoded
, 60, encoded
, 96);
3300 test_memeq(plain
, decoded
, 60);
3301 /* Change encoded string and decode. */
3302 if (encoded
[0] == 'A' || encoded
[0] == 'a')
3306 res
= base32_decode(decoded
, 60, encoded
, 96);
3308 test_memneq(plain
, decoded
, 60);
3311 /* Test encoding and parsing of v2 rendezvous service descriptors. */
3313 test_rend_fns_v2(void)
3315 rend_service_descriptor_t
*generated
, *parsed
;
3316 char service_id
[DIGEST_LEN
];
3317 char service_id_base32
[REND_SERVICE_ID_LEN_BASE32
+1];
3318 const char *next_desc
;
3319 smartlist_t
*descs
= smartlist_create();
3320 char computed_desc_id
[DIGEST_LEN
];
3321 char parsed_desc_id
[DIGEST_LEN
];
3322 crypto_pk_env_t
*pk1
, *pk2
;
3324 char *intro_points_encrypted
;
3325 size_t intro_points_size
;
3326 size_t encoded_size
;
3328 pk1
= pk_generate(0);
3329 pk2
= pk_generate(1);
3330 generated
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
3331 generated
->pk
= crypto_pk_dup_key(pk1
);
3332 crypto_pk_get_digest(generated
->pk
, service_id
);
3333 base32_encode(service_id_base32
, REND_SERVICE_ID_LEN_BASE32
+1,
3334 service_id
, REND_SERVICE_ID_LEN
);
3336 generated
->timestamp
= now
;
3337 generated
->version
= 2;
3338 generated
->protocols
= 42;
3339 generated
->intro_nodes
= smartlist_create();
3340 for (i
= 0; i
< 3; i
++) {
3341 rend_intro_point_t
*intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
3342 crypto_pk_env_t
*okey
= pk_generate(2 + i
);
3343 intro
->extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
3344 intro
->extend_info
->onion_key
= crypto_pk_dup_key(okey
);
3345 crypto_pk_get_digest(intro
->extend_info
->onion_key
,
3346 intro
->extend_info
->identity_digest
);
3347 //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
3348 intro
->extend_info
->nickname
[0] = '$';
3349 base16_encode(intro
->extend_info
->nickname
+ 1,
3350 sizeof(intro
->extend_info
->nickname
) - 1,
3351 intro
->extend_info
->identity_digest
, DIGEST_LEN
);
3352 intro
->extend_info
->addr
= crypto_rand_int(65536); /* Does not cover all
3354 intro
->extend_info
->port
= crypto_rand_int(65536);
3355 intro
->intro_key
= crypto_pk_dup_key(pk2
);
3356 smartlist_add(generated
->intro_nodes
, intro
);
3358 test_assert(rend_encode_v2_descriptors(descs
, generated
, now
,
3360 test_assert(rend_compute_v2_desc_id(computed_desc_id
, service_id_base32
,
3361 NULL
, now
, 0) == 0);
3362 test_memeq(((rend_encoded_v2_service_descriptor_t
*)
3363 smartlist_get(descs
, 0))->desc_id
, computed_desc_id
, DIGEST_LEN
);
3364 test_assert(rend_parse_v2_service_descriptor(&parsed
, parsed_desc_id
,
3365 &intro_points_encrypted
,
3369 ((rend_encoded_v2_service_descriptor_t
*)
3370 smartlist_get(descs
, 0))->desc_str
) == 0);
3371 test_assert(parsed
);
3372 test_memeq(((rend_encoded_v2_service_descriptor_t
*)
3373 smartlist_get(descs
, 0))->desc_id
, parsed_desc_id
, DIGEST_LEN
);
3374 test_assert(rend_decrypt_introduction_points(parsed
, NULL
,
3375 intro_points_encrypted
,
3376 intro_points_size
) == 3);
3377 test_assert(!crypto_pk_cmp_keys(generated
->pk
, parsed
->pk
));
3378 test_eq(parsed
->timestamp
, now
);
3379 test_eq(parsed
->version
, 2);
3380 test_eq(parsed
->protocols
, 42);
3381 test_eq(smartlist_len(parsed
->intro_nodes
), 3);
3382 for (i
= 0; i
< smartlist_len(parsed
->intro_nodes
); i
++) {
3383 rend_intro_point_t
*par_intro
= smartlist_get(parsed
->intro_nodes
, i
),
3384 *gen_intro
= smartlist_get(generated
->intro_nodes
, i
);
3385 extend_info_t
*par_info
= par_intro
->extend_info
;
3386 extend_info_t
*gen_info
= gen_intro
->extend_info
;
3387 test_assert(!crypto_pk_cmp_keys(gen_info
->onion_key
, par_info
->onion_key
));
3388 test_memeq(gen_info
->identity_digest
, par_info
->identity_digest
,
3390 test_streq(gen_info
->nickname
, par_info
->nickname
);
3391 test_eq(gen_info
->addr
, par_info
->addr
);
3392 test_eq(gen_info
->port
, par_info
->port
);
3394 tor_free(intro_points_encrypted
);
3395 for (i
= 0; i
< smartlist_len(descs
); i
++)
3396 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
3397 smartlist_free(descs
);
3398 rend_service_descriptor_free(parsed
);
3399 rend_service_descriptor_free(generated
);
3406 time_t now
= time(NULL
);
3409 /* Populate the DB a bit. Add these in order, since we can't do the final
3410 * 'sort' step. These aren't very good IP addresses, but they're perfectly
3411 * fine uint32_t values. */
3412 test_eq(0, geoip_parse_entry("10,50,AB"));
3413 test_eq(0, geoip_parse_entry("52,90,XY"));
3414 test_eq(0, geoip_parse_entry("95,100,AB"));
3415 test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
3416 test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
3417 test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
3419 /* We should have 3 countries: ab, xy, zz. */
3420 test_eq(3, geoip_get_n_countries());
3421 /* Make sure that country ID actually works. */
3422 #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
3423 test_streq("ab", NAMEFOR(32));
3424 test_streq("??", NAMEFOR(5));
3425 test_streq("??", NAMEFOR(51));
3426 test_streq("xy", NAMEFOR(150));
3427 test_streq("xy", NAMEFOR(190));
3428 test_streq("??", NAMEFOR(2000));
3431 get_options()->BridgeRelay
= 1;
3432 get_options()->BridgeRecordUsageByCountry
= 1;
3433 /* Put 9 observations in AB... */
3434 for (i
=32; i
< 40; ++i
)
3435 geoip_note_client_seen(i
, now
);
3436 geoip_note_client_seen(225, now
);
3437 /* and 3 observations in XY, several times. */
3438 for (j
=0; j
< 10; ++j
)
3439 for (i
=52; i
< 55; ++i
)
3440 geoip_note_client_seen(i
, now
-3600);
3441 /* and 17 observations in ZZ... */
3442 for (i
=110; i
< 127; ++i
)
3443 geoip_note_client_seen(i
, now
-7200);
3444 s
= geoip_get_client_history(now
+5*24*60*60);
3446 test_streq("zz=24,ab=16", s
);
3449 /* Now clear out all the zz observations. */
3450 geoip_remove_old_clients(now
-6000);
3451 s
= geoip_get_client_history(now
+5*24*60*60);
3452 test_assert(! s
); /* There are only 12 observations left. Not enough to
3453 build an answer. Add 4 more in XY... */
3454 for (i
=55; i
< 59; ++i
)
3455 geoip_note_client_seen(i
, now
-3600);
3456 s
= geoip_get_client_history(now
+5*24*60*60);
3458 test_streq("ab=16", s
);
3462 #define ENT(x) { #x, test_ ## x, 0, 0 }
3463 #define SUBENT(x,y) { #x "/" #y, test_ ## x ## _ ## y, 1, 0 }
3466 const char *test_name
;
3467 void (*test_fn
)(void);
3474 SUBENT(crypto
, s2k
),
3475 SUBENT(crypto
, aes_iv
),
3476 SUBENT(crypto
, base32_decode
),
3478 SUBENT(util
, ip6_helpers
),
3480 SUBENT(util
, datadir
),
3481 SUBENT(util
, smartlist
),
3482 SUBENT(util
, bitarray
),
3483 SUBENT(util
, mempool
),
3484 SUBENT(util
, strmap
),
3485 SUBENT(util
, control_formats
),
3486 SUBENT(util
, pqueue
),
3488 SUBENT(util
, threads
),
3489 SUBENT(util
, order_functions
),
3490 ENT(onion_handshake
),
3492 ENT(v3_networkstatus
),
3495 SUBENT(rend_fns
, v2
),
3497 { NULL
, NULL
, 0, 0 },
3500 static void syntax(void) ATTR_NORETURN
;
3506 " test [-v|--verbose] [--warn|--notice|--info|--debug]\n"
3508 "Recognized tests are:\n");
3509 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3510 printf(" %s\n", test_array
[i
].test_name
);
3517 main(int c
, char**v
)
3519 or_options_t
*options
= options_new();
3520 char *errmsg
= NULL
;
3522 int verbose
= 0, any_selected
= 0;
3523 int loglevel
= LOG_ERR
;
3527 for (i
= 1; i
< c
; ++i
) {
3528 if (!strcmp(v
[i
], "-v") || !strcmp(v
[i
], "--verbose"))
3530 else if (!strcmp(v
[i
], "--warn"))
3531 loglevel
= LOG_WARN
;
3532 else if (!strcmp(v
[i
], "--notice"))
3533 loglevel
= LOG_NOTICE
;
3534 else if (!strcmp(v
[i
], "--info"))
3535 loglevel
= LOG_INFO
;
3536 else if (!strcmp(v
[i
], "--debug"))
3537 loglevel
= LOG_DEBUG
;
3538 else if (!strcmp(v
[i
], "--help") || !strcmp(v
[i
], "-h") || v
[i
][0] == '-')
3542 for (j
= 0; test_array
[j
].test_name
; ++j
) {
3543 if (!strcmp(v
[i
], test_array
[j
].test_name
) ||
3544 (test_array
[j
].is_subent
&&
3545 !strcmpstart(test_array
[j
].test_name
, v
[i
]) &&
3546 test_array
[j
].test_name
[strlen(v
[i
])] == '/') ||
3547 (v
[i
][0] == '=' && !strcmp(v
[i
]+1, test_array
[j
].test_name
))) {
3548 test_array
[j
].selected
= 1;
3554 printf("Unknown test: %s\n", v
[i
]);
3560 if (!any_selected
) {
3561 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3562 test_array
[i
].selected
= 1;
3566 add_stream_log(loglevel
, LOG_ERR
, "", stdout
);
3568 options
->command
= CMD_RUN_UNITTESTS
;
3572 options_init(options
);
3573 options
->DataDirectory
= tor_strdup(temp_dir
);
3574 if (set_options(options
, &errmsg
) < 0) {
3575 printf("Failed to set initial options: %s\n", errmsg
);
3587 atexit(remove_directory
);
3589 printf("Running Tor unit tests on %s\n", get_uname());
3591 for (i
= 0; test_array
[i
].test_name
; ++i
) {
3592 if (!test_array
[i
].selected
)
3594 if (!test_array
[i
].is_subent
) {
3595 printf("\n============================== %s\n",test_array
[i
].test_name
);
3596 } else if (test_array
[i
].is_subent
&& verbose
) {
3597 printf("\n%s", test_array
[i
].test_name
);
3599 test_array
[i
].test_fn();