1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
17 #include "../common/test.h"
18 #include "../common/torgzip.h"
22 /* These functions are file-local, but are exposed so we can test. */
23 void add_fingerprint_to_dir(const char *nickname
, const char *fp
);
24 void get_platform_str(char *platform
, size_t len
);
25 int is_obsolete_version(const char *myversion
, const char *start
);
27 static char temp_dir
[256];
32 static int is_setup
= 0;
36 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
40 r
= mkdir(temp_dir
, 0700);
43 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
51 get_fname(const char *name
)
53 static char buf
[1024];
55 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
60 remove_directory(void)
65 if (!(dirp
= opendir(temp_dir
))) {
66 perror("Can't open temporary directory to remove files");
69 while ((de
= readdir(dirp
)) != NULL
) {
70 /* Only "." and ".." start with ., since we don't create any dotfiles. */
71 if (de
->d_name
[0] == '.') continue;
72 if (unlink(get_fname(de
->d_name
))) {
73 perror("Error removing file");
76 printf("==%s\n", de
->d_name
);
85 #define MAX_BUF_SIZE 1024*1024
97 if (!(buf
= buf_new()))
100 test_eq(buf_capacity(buf
), 512*1024);
101 test_eq(buf_datalen(buf
), 0);
106 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
107 for (j
=0;j
<256;++j
) {
113 s
= open(get_fname("data"), O_RDONLY
, 0);
115 i
= read_to_buf(s
, 10, buf
, &eof
);
116 test_eq(buf_capacity(buf
), 512*1024);
117 test_eq(buf_datalen(buf
), 10);
120 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10);
122 /* Test reading 0 bytes. */
123 i
= read_to_buf(s
, 0, buf
, &eof
);
124 test_eq(buf_capacity(buf
), 512*1024);
125 test_eq(buf_datalen(buf
), 10);
129 /* Now test when buffer is filled exactly. */
130 buf2
= buf_new_with_capacity(6);
131 i
= read_to_buf(s
, 6, buf2
, &eof
);
132 test_eq(buf_capacity(buf2
), 6);
133 test_eq(buf_datalen(buf2
), 6);
136 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
139 /* Now test when buffer is filled with more data to read. */
140 buf2
= buf_new_with_capacity(32);
141 i
= read_to_buf(s
, 128, buf2
, &eof
);
142 test_eq(buf_capacity(buf2
), 128);
143 test_eq(buf_datalen(buf2
), 32);
148 /* Now read to eof. */
149 test_assert(buf_capacity(buf
) > 256);
150 i
= read_to_buf(s
, 1024, buf
, &eof
);
151 test_eq(i
, (256-32-10-6));
152 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
153 test_eq(buf_datalen(buf
), 256-6-32);
154 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10); /* XXX Check rest. */
157 i
= read_to_buf(s
, 1024, buf
, &eof
);
159 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
160 test_eq(buf_datalen(buf
), 256-6-32);
168 memset(str2
, 255, 256);
169 test_eq(246, fetch_from_buf(str2
, 10, buf
));
170 test_memeq(str2
, str
, 10);
171 test_memeq(str
+10,(char*)_buf_peek_raw_buffer(buf
),246);
172 test_eq(buf_datalen(buf
),246);
174 test_eq(0, fetch_from_buf(str2
, 246, buf
));
175 test_memeq(str2
, str
+10, 246);
176 test_eq(buf_capacity(buf
),MAX_BUF_SIZE
);
177 test_eq(buf_datalen(buf
),0);
182 memset((char *)_buf_peek_raw_buffer(buf
), (int)'-', 256);
183 i
= write_to_buf("Hello world", 11, buf
);
185 test_eq(buf_datalen(buf
), 11);
186 test_memeq((char*)_buf_peek_raw_buffer(buf
), "Hello world", 11);
187 i
= write_to_buf("XYZZY", 5, buf
);
189 test_eq(buf_datalen(buf
), 16);
190 test_memeq((char*)_buf_peek_raw_buffer(buf
), "Hello worldXYZZY", 16);
191 /* Test when buffer is overfull. */
194 test_eq(-1, write_to_buf("This string will not fit.", 25,
195 &buf
, &buflen
, &buf_datalen
));
196 test_eq(buf_datalen
, 16);
197 test_memeq(buf
, "Hello worldXYZZY--", 18);
198 buflen
= MAX_BUF_SIZE
;
204 /* XXXX Needs tests. */
212 crypto_dh_env_t
*dh1
, *dh2
;
219 dh1
= crypto_dh_new();
220 dh2
= crypto_dh_new();
221 test_eq(crypto_dh_get_bytes(dh1
), DH_BYTES
);
222 test_eq(crypto_dh_get_bytes(dh2
), DH_BYTES
);
224 memset(p1
, 0, DH_BYTES
);
225 memset(p2
, 0, DH_BYTES
);
226 test_memeq(p1
, p2
, DH_BYTES
);
227 test_assert(! crypto_dh_get_public(dh1
, p1
, DH_BYTES
));
228 test_memneq(p1
, p2
, DH_BYTES
);
229 test_assert(! crypto_dh_get_public(dh2
, p2
, DH_BYTES
));
230 test_memneq(p1
, p2
, DH_BYTES
);
232 memset(s1
, 0, DH_BYTES
);
233 memset(s2
, 0xFF, DH_BYTES
);
234 s1len
= crypto_dh_compute_secret(dh1
, p2
, DH_BYTES
, s1
, 50);
235 s2len
= crypto_dh_compute_secret(dh2
, p1
, DH_BYTES
, s2
, 50);
236 test_assert(s1len
> 0);
237 test_eq(s1len
, s2len
);
238 test_memeq(s1
, s2
, s1len
);
247 crypto_cipher_env_t
*env1
, *env2
;
248 crypto_pk_env_t
*pk1
, *pk2
;
249 char *data1
, *data2
, *data3
, *cp
;
253 data1
= tor_malloc(1024);
254 data2
= tor_malloc(1024);
255 data3
= tor_malloc(1024);
256 test_assert(data1
&& data2
&& data3
);
259 test_assert(! crypto_seed_rng());
260 crypto_rand(data1
, 100);
261 crypto_rand(data2
, 100);
262 test_memneq(data1
,data2
,100);
265 /* Try out identity ciphers. */
266 env1
= crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY
);
268 test_eq(crypto_cipher_generate_key(env1
), 0);
269 test_eq(crypto_cipher_encrypt_init_cipher(env1
), 0);
270 for (i
= 0; i
< 1024; ++i
) {
271 data1
[i
] = (char) i
*73;
273 crypto_cipher_encrypt(env1
, data2
, data1
, 1024);
274 test_memeq(data1
, data2
, 1024);
275 crypto_free_cipher_env(env1
);
278 /* Now, test encryption and decryption with stream cipher. */
280 for (i
= 1023; i
>0; i
-= 35)
281 strncat(data1
, "Now is the time for all good onions", i
);
283 memset(data2
, 0, 1024);
284 memset(data3
, 0, 1024);
285 env1
= crypto_new_cipher_env();
287 env2
= crypto_new_cipher_env();
289 j
= crypto_cipher_generate_key(env1
);
290 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
291 crypto_cipher_encrypt_init_cipher(env1
);
292 crypto_cipher_decrypt_init_cipher(env2
);
294 /* Try encrypting 512 chars. */
295 crypto_cipher_encrypt(env1
, data2
, data1
, 512);
296 crypto_cipher_decrypt(env2
, data3
, data2
, 512);
297 test_memeq(data1
, data3
, 512);
298 test_memneq(data1
, data2
, 512);
300 /* Now encrypt 1 at a time, and get 1 at a time. */
301 for (j
= 512; j
< 560; ++j
) {
302 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 1);
304 for (j
= 512; j
< 560; ++j
) {
305 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 1);
307 test_memeq(data1
, data3
, 560);
308 /* Now encrypt 3 at a time, and get 5 at a time. */
309 for (j
= 560; j
< 1024-5; j
+= 3) {
310 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 3);
312 for (j
= 560; j
< 1024-5; j
+= 5) {
313 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 5);
315 test_memeq(data1
, data3
, 1024-5);
316 /* Now make sure that when we encrypt with different chunk sizes, we get
318 crypto_free_cipher_env(env2
);
320 memset(data3
, 0, 1024);
321 env2
= crypto_new_cipher_env();
323 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
324 crypto_cipher_encrypt_init_cipher(env2
);
325 for (j
= 0; j
< 1024-16; j
+= 17) {
326 crypto_cipher_encrypt(env2
, data3
+j
, data1
+j
, 17);
328 for (j
= 0; j
< 1024-16; ++j
) {
329 if (data2
[j
] != data3
[j
]) {
330 printf("%d: %d\t%d\n", j
, (int) data2
[j
], (int) data3
[j
]);
333 test_memeq(data2
, data3
, 1024-16);
334 crypto_free_cipher_env(env1
);
335 crypto_free_cipher_env(env2
);
337 /* Test vectors for stream ciphers. */
338 /* XXXX Look up some test vectors for the ciphers and make sure we match. */
340 /* Test SHA-1 with a test vector from the specification. */
341 i
= crypto_digest(data1
, "abc", 3);
343 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
344 "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
346 /* Public-key ciphers */
347 pk1
= crypto_new_pk_env();
348 pk2
= crypto_new_pk_env();
349 test_assert(pk1
&& pk2
);
350 test_assert(! crypto_pk_generate_key(pk1
));
351 test_assert(! crypto_pk_write_public_key_to_string(pk1
, &cp
, &size
));
352 test_assert(! crypto_pk_read_public_key_from_string(pk2
, cp
, size
));
353 test_eq(0, crypto_pk_cmp_keys(pk1
, pk2
));
356 /* Check DER encoding */
357 i
=crypto_pk_DER64_encode_public_key(pk1
, &cp
);
360 test_assert(!strchr(cp
, ' '));
361 test_assert(!strchr(cp
, '\n'));
362 test_eq(0, crypto_pk_cmp_keys(pk1
, pk1
));
363 crypto_free_pk_env(pk2
);
364 pk2
= crypto_pk_DER64_decode_public_key(cp
);
366 test_eq(0, crypto_pk_cmp_keys(pk1
, pk2
));
369 test_eq(128, crypto_pk_keysize(pk1
));
370 test_eq(128, crypto_pk_keysize(pk2
));
372 test_eq(128, crypto_pk_public_encrypt(pk2
, data1
, "Hello whirled.", 15,
373 PK_PKCS1_OAEP_PADDING
));
374 test_eq(128, crypto_pk_public_encrypt(pk1
, data2
, "Hello whirled.", 15,
375 PK_PKCS1_OAEP_PADDING
));
376 /* oaep padding should make encryption not match */
377 test_memneq(data1
, data2
, 128);
378 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data1
, 128,
379 PK_PKCS1_OAEP_PADDING
,1));
380 test_streq(data3
, "Hello whirled.");
381 memset(data3
, 0, 1024);
382 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
383 PK_PKCS1_OAEP_PADDING
,1));
384 test_streq(data3
, "Hello whirled.");
385 /* Can't decrypt with public key. */
386 test_eq(-1, crypto_pk_private_decrypt(pk2
, data3
, data2
, 128,
387 PK_PKCS1_OAEP_PADDING
,1));
388 /* Try again with bad padding */
389 memcpy(data2
+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
390 test_eq(-1, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
391 PK_PKCS1_OAEP_PADDING
,1));
393 /* File operations: save and load private key */
394 test_assert(! crypto_pk_write_private_key_to_filename(pk1
,
395 get_fname("pkey1")));
397 test_assert(! crypto_pk_read_private_key_from_filename(pk2
,
398 get_fname("pkey1")));
399 test_eq(15, crypto_pk_private_decrypt(pk2
, data3
, data1
, 128,
400 PK_PKCS1_OAEP_PADDING
,1));
402 /* Now try signing. */
403 strcpy(data1
, "Ossifrage");
404 test_eq(128, crypto_pk_private_sign(pk1
, data2
, data1
, 10));
405 test_eq(10, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
406 test_streq(data3
, "Ossifrage");
407 /* Try signing digests. */
408 test_eq(128, crypto_pk_private_sign_digest(pk1
, data2
, data1
, 10));
409 test_eq(20, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
410 test_eq(0, crypto_pk_public_checksig_digest(pk1
, data1
, 10, data2
, 128));
411 test_eq(-1, crypto_pk_public_checksig_digest(pk1
, data1
, 11, data2
, 128));
412 /*XXXX test failed signing*/
415 crypto_free_pk_env(pk2
);
417 i
= crypto_pk_asn1_encode(pk1
, data1
, 1024);
419 pk2
= crypto_pk_asn1_decode(data1
, i
);
420 test_assert(crypto_pk_cmp_keys(pk1
,pk2
) == 0);
422 /* Try with hybrid encryption wrappers. */
423 crypto_rand(data1
, 1024);
424 for (i
= 0; i
< 3; ++i
) {
425 for (j
= 85; j
< 140; ++j
) {
426 memset(data2
,0,1024);
427 memset(data3
,0,1024);
428 if (i
== 0 && j
< 129)
430 p
= (i
==0)?PK_NO_PADDING
:
431 (i
==1)?PK_PKCS1_PADDING
:PK_PKCS1_OAEP_PADDING
;
432 len
= crypto_pk_public_hybrid_encrypt(pk1
,data2
,data1
,j
,p
,0);
434 len
= crypto_pk_private_hybrid_decrypt(pk1
,data3
,data2
,len
,p
,1);
436 test_memeq(data1
,data3
,j
);
439 crypto_free_pk_env(pk1
);
440 crypto_free_pk_env(pk2
);
443 strcpy(data1
, "Test string that contains 35 chars.");
444 strcat(data1
, " 2nd string that contains 35 chars.");
446 i
= base64_encode(data2
, 1024, data1
, 71);
447 j
= base64_decode(data3
, 1024, data2
, i
);
448 test_streq(data3
, data1
);
450 test_assert(data2
[i
] == '\0');
453 strcpy(data1
, "5chrs");
454 /* bit pattern is: [35 63 68 72 73] ->
455 * [00110101 01100011 01101000 01110010 01110011]
456 * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
458 base32_encode(data2
, 9, data1
, 5);
459 test_streq(data2
, "gvrwq4tt");
461 strcpy(data1
, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
462 base32_encode(data2
, 30, data1
, 10);
463 test_streq(data2
, "772w2rfobvomsywe");
466 strcpy(data1
, "6chrs\xff");
467 base16_encode(data2
, 13, data1
, 6);
468 test_streq(data2
, "3663687273FF");
470 strcpy(data1
, "f0d678affc000100");
471 i
= base16_decode(data2
, 8, data1
, 16);
473 test_memeq(data2
, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
482 struct timeval start
, end
;
485 char timestr
[RFC1123_TIME_LEN
+1];
494 start
.tv_usec
= 5000;
499 test_eq(0L, tv_udiff(&start
, &end
));
503 test_eq(2000L, tv_udiff(&start
, &end
));
507 test_eq(1002000L, tv_udiff(&start
, &end
));
511 test_eq(995000L, tv_udiff(&start
, &end
));
515 test_eq(-1005000L, tv_udiff(&start
, &end
));
517 /* The test values here are confirmed to be correct on a platform
518 * with a working timegm. */
519 a_time
.tm_year
= 2003-1900;
525 test_eq((time_t) 1062224095UL, tor_timegm(&a_time
));
526 a_time
.tm_year
= 2004-1900; /* Try a leap year, after feb. */
527 test_eq((time_t) 1093846495UL, tor_timegm(&a_time
));
528 a_time
.tm_mon
= 1; /* Try a leap year, in feb. */
530 test_eq((time_t) 1076393695UL, tor_timegm(&a_time
));
532 format_rfc1123_time(timestr
, 0);
533 test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr
);
534 format_rfc1123_time(timestr
, (time_t)1091580502UL);
535 test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr
);
538 i
= parse_rfc1123_time(timestr
, &t_res
);
540 test_eq(t_res
, (time_t)1091580502UL);
543 sl
= smartlist_create();
544 smartlist_add(sl
, (void*)1);
545 smartlist_add(sl
, (void*)2);
546 smartlist_add(sl
, (void*)3);
547 smartlist_add(sl
, (void*)4);
548 smartlist_del_keeporder(sl
, 1);
549 smartlist_insert(sl
, 1, (void*)22);
550 smartlist_insert(sl
, 0, (void*)0);
551 smartlist_insert(sl
, 5, (void*)555);
552 test_eq((void*)0, smartlist_get(sl
,0));
553 test_eq((void*)1, smartlist_get(sl
,1));
554 test_eq((void*)22, smartlist_get(sl
,2));
555 test_eq((void*)3, smartlist_get(sl
,3));
556 test_eq((void*)4, smartlist_get(sl
,4));
557 test_eq((void*)555, smartlist_get(sl
,5));
560 smartlist_split_string(sl
, "abc", ":", 0, 0);
561 test_eq(1, smartlist_len(sl
));
562 test_streq("abc", smartlist_get(sl
, 0));
563 smartlist_split_string(sl
, "a::bc::", "::", 0, 0);
564 test_eq(4, smartlist_len(sl
));
565 test_streq("a", smartlist_get(sl
, 1));
566 test_streq("bc", smartlist_get(sl
, 2));
567 test_streq("", smartlist_get(sl
, 3));
568 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
569 test_streq(cp
, "abcabc");
571 cp
= smartlist_join_strings(sl
, "!", 0, NULL
);
572 test_streq(cp
, "abc!a!bc!");
574 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
575 test_streq(cp
, "abcXYaXYbcXY");
577 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
578 test_streq(cp
, "abcXYaXYbcXYXY");
580 cp
= smartlist_join_strings(sl
, "", 1, NULL
);
581 test_streq(cp
, "abcabc");
584 smartlist_split_string(sl
, "/def/ /ghijk", "/", 0, 0);
585 test_eq(8, smartlist_len(sl
));
586 test_streq("", smartlist_get(sl
, 4));
587 test_streq("def", smartlist_get(sl
, 5));
588 test_streq(" ", smartlist_get(sl
, 6));
589 test_streq("ghijk", smartlist_get(sl
, 7));
590 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
593 smartlist_split_string(sl
, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE
, 0);
594 test_eq(3, smartlist_len(sl
));
595 test_streq("a", smartlist_get(sl
,0));
596 test_streq("bbd", smartlist_get(sl
,1));
597 test_streq("cdef", smartlist_get(sl
,2));
598 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE
, 0);
599 test_eq(8, smartlist_len(sl
));
600 test_streq("z", smartlist_get(sl
,3));
601 test_streq("zhasd", smartlist_get(sl
,4));
602 test_streq("", smartlist_get(sl
,5));
603 test_streq("bnud", smartlist_get(sl
,6));
604 test_streq("", smartlist_get(sl
,7));
606 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
609 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
610 test_eq(3, smartlist_len(sl
));
611 test_streq("z", smartlist_get(sl
, 0));
612 test_streq("zhasd", smartlist_get(sl
, 1));
613 test_streq("bnud", smartlist_get(sl
, 2));
614 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 2);
615 test_eq(5, smartlist_len(sl
));
616 test_streq("z", smartlist_get(sl
, 3));
617 test_streq("zhasd <> <> bnud<>", smartlist_get(sl
, 4));
618 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
621 smartlist_split_string(sl
, "abcd\n", "\n", SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
622 test_eq(1, smartlist_len(sl
));
623 test_streq("abcd", smartlist_get(sl
, 0));
624 smartlist_split_string(sl
, "efgh", "\n", SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
625 test_eq(2, smartlist_len(sl
));
626 test_streq("efgh", smartlist_get(sl
, 1));
628 /* Test tor_strstrip() */
629 strcpy(buf
, "Testing 1 2 3");
630 test_eq(0, tor_strstrip(buf
, ",!"));
631 test_streq(buf
, "Testing 1 2 3");
632 strcpy(buf
, "!Testing 1 2 3?");
633 test_eq(5, tor_strstrip(buf
, "!? "));
634 test_streq(buf
, "Testing123");
636 /* Test tor_strpartition() */
637 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefg", "##", 3,
639 test_streq(buf
, "abc##def##g");
640 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefg", "##", 3,
642 test_streq(buf
, "abc##def##g##");
643 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefghi", "##", 3,
645 test_streq(buf
, "abc##def##ghi##");
646 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefghi", "##", 3,
648 test_streq(buf
, "abc##def##ghi");
650 /* Test parse_addr_port */
651 cp
= NULL
; u32
= 3; u16
= 3;
652 test_assert(!parse_addr_port("1.2.3.4", &cp
, &u32
, &u16
));
653 test_streq(cp
, "1.2.3.4");
654 test_eq(u32
, 0x01020304u
);
657 test_assert(!parse_addr_port("4.3.2.1:99", &cp
, &u32
, &u16
));
658 test_streq(cp
, "4.3.2.1");
659 test_eq(u32
, 0x04030201u
);
662 test_assert(!parse_addr_port("nonexistent.address:4040", &cp
, NULL
, &u16
));
663 test_streq(cp
, "nonexistent.address");
666 test_assert(!parse_addr_port("localhost:9999", &cp
, &u32
, &u16
));
667 test_streq(cp
, "localhost");
668 test_eq(u32
, 0x7f000001u
);
672 test_assert(!parse_addr_port("localhost", NULL
, &u32
, &u16
));
674 test_eq(u32
, 0x7f000001u
);
678 /* Test tor_parse_long. */
679 test_eq(10L, tor_parse_long("10",10,0,100,NULL
,NULL
));
680 test_eq(0L, tor_parse_long("10",10,50,100,NULL
,NULL
));
682 /* Test parse_line_from_str */
683 strlcpy(buf
, "k v\n" " key value with spaces \n" "keykey val\n"
685 "k3 \n" "\n" " \n" "#comment\n"
686 "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf
));
689 cp
= parse_line_from_str(cp
, &k
, &v
);
692 test_assert(!strcmpstart(cp
, " key value with"));
694 cp
= parse_line_from_str(cp
, &k
, &v
);
695 test_streq(k
, "key");
696 test_streq(v
, "value with spaces");
697 test_assert(!strcmpstart(cp
, "keykey"));
699 cp
= parse_line_from_str(cp
, &k
, &v
);
700 test_streq(k
, "keykey");
701 test_streq(v
, "val");
702 test_assert(!strcmpstart(cp
, "k2\n"));
704 cp
= parse_line_from_str(cp
, &k
, &v
);
707 test_assert(!strcmpstart(cp
, "k3 \n"));
709 cp
= parse_line_from_str(cp
, &k
, &v
);
712 test_assert(!strcmpstart(cp
, "\n \n"));
714 cp
= parse_line_from_str(cp
, &k
, &v
);
717 test_assert(!strcmpstart(cp
, "k5#abc"));
719 cp
= parse_line_from_str(cp
, &k
, &v
);
722 test_assert(!strcmpstart(cp
, "k6"));
724 cp
= parse_line_from_str(cp
, &k
, &v
);
726 test_streq(v
, "val");
729 /* Test for strcmpstart and strcmpend. */
730 test_assert(strcmpstart("abcdef", "abcdef")==0);
731 test_assert(strcmpstart("abcdef", "abc")==0);
732 test_assert(strcmpstart("abcdef", "abd")<0);
733 test_assert(strcmpstart("abcdef", "abb")>0);
734 test_assert(strcmpstart("ab", "abb")<0);
736 test_assert(strcmpend("abcdef", "abcdef")==0);
737 test_assert(strcmpend("abcdef", "def")==0);
738 test_assert(strcmpend("abcdef", "deg")<0);
739 test_assert(strcmpend("abcdef", "dee")>0);
740 test_assert(strcmpend("ab", "abb")<0);
742 /* XXXX test older functions. */
749 char *buf1
, *buf2
=NULL
, *buf3
=NULL
;
752 buf1
= tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
753 if (is_gzip_supported()) {
754 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
757 test_assert(!memcmp(buf2
, "\037\213", 2)); /* Gzip magic. */
759 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
, GZIP_METHOD
));
761 test_streq(buf1
,buf3
);
767 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
770 test_assert(!memcmp(buf2
, "\x78\xDA", 2)); /* deflate magic. */
772 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
, ZLIB_METHOD
));
774 test_streq(buf1
,buf3
);
782 _squareAndRemoveK4(const char *key
, void*val
, void *data
)
784 int *ip
= (int*)data
;
786 if (strcmp(key
,"K4") == 0) {
804 v
= strmap_set(map
, "K1", (void*)99);
806 v
= strmap_set(map
, "K2", (void*)101);
808 v
= strmap_set(map
, "K1", (void*)100);
809 test_eq(v
, (void*)99);
810 test_eq(strmap_get(map
,"K1"), (void*)100);
811 test_eq(strmap_get(map
,"K2"), (void*)101);
812 test_eq(strmap_get(map
,"K-not-there"), NULL
);
814 v
= strmap_remove(map
,"K2");
815 test_eq(v
, (void*)101);
816 test_eq(strmap_get(map
,"K2"), NULL
);
817 test_eq(strmap_remove(map
,"K2"), NULL
);
819 strmap_set(map
, "K2", (void*)101);
820 strmap_set(map
, "K3", (void*)102);
821 strmap_set(map
, "K4", (void*)103);
822 strmap_set(map
, "K5", (void*)104);
823 strmap_set(map
, "K6", (void*)105);
826 strmap_foreach(map
, _squareAndRemoveK4
, &count
);
828 test_eq(strmap_get(map
, "K4"), NULL
);
829 test_eq(strmap_get(map
, "K1"), (void*)10000);
830 test_eq(strmap_get(map
, "K6"), (void*)11025);
832 iter
= strmap_iter_init(map
);
833 strmap_iter_get(iter
,&k
,&v
);
835 test_eq(v
, (void*)10000);
836 iter
= strmap_iter_next(map
,iter
);
837 strmap_iter_get(iter
,&k
,&v
);
839 test_eq(v
, (void*)10201);
840 iter
= strmap_iter_next_rmv(map
,iter
);
841 strmap_iter_get(iter
,&k
,&v
);
843 test_eq(v
, (void*)10404);
844 iter
= strmap_iter_next(map
,iter
); /* K5 */
845 test_assert(!strmap_iter_done(iter
));
846 iter
= strmap_iter_next(map
,iter
); /* K6 */
847 test_assert(!strmap_iter_done(iter
));
848 iter
= strmap_iter_next(map
,iter
); /* done */
849 test_assert(strmap_iter_done(iter
));
851 /* Make sure we removed K2, but not the others. */
852 test_eq(strmap_get(map
, "K2"), NULL
);
853 test_eq(strmap_get(map
, "K5"), (void*)10816);
855 /* Clean up after ourselves. */
856 strmap_free(map
, NULL
);
858 /* Now try some lc functions. */
860 strmap_set_lc(map
,"Ab.C", (void*)1);
861 test_eq(strmap_get(map
,"ab.c"), (void*)1);
862 test_eq(strmap_get_lc(map
,"AB.C"), (void*)1);
863 test_eq(strmap_get(map
,"AB.C"), NULL
);
864 test_eq(strmap_remove_lc(map
,"aB.C"), (void*)1);
865 test_eq(strmap_get_lc(map
,"AB.C"), NULL
);
866 strmap_free(map
,NULL
);
876 names
= parse_nickname_list(" foo bar\t baz quux ", &num
);
878 test_streq(names
[0],"foo");
879 test_streq(names
[1],"bar");
880 test_streq(names
[2],"baz");
881 test_streq(names
[3],"quux");
889 test_onion_handshake(void)
892 crypto_dh_env_t
*c_dh
= NULL
;
893 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
897 char s_buf
[ONIONSKIN_REPLY_LEN
];
901 crypto_pk_env_t
*pk
= NULL
;
903 pk
= crypto_new_pk_env();
904 test_assert(! crypto_pk_generate_key(pk
));
906 /* client handshake 1. */
907 memset(c_buf
, 0, ONIONSKIN_CHALLENGE_LEN
);
908 test_assert(! onion_skin_create(pk
, &c_dh
, c_buf
));
910 /* server handshake */
911 memset(s_buf
, 0, ONIONSKIN_REPLY_LEN
);
912 memset(s_keys
, 0, 40);
913 test_assert(! onion_skin_server_handshake(c_buf
, pk
, NULL
, s_buf
, s_keys
, 40));
915 /* client handshake 2 */
916 memset(c_keys
, 0, 40);
917 test_assert(! onion_skin_client_handshake(c_dh
, s_buf
, c_keys
, 40));
919 crypto_dh_free(c_dh
);
921 if (memcmp(c_keys
, s_keys
, 40)) {
925 test_memeq(c_keys
, s_keys
, 40);
926 memset(s_buf
, 0, 40);
927 test_memneq(c_keys
, s_buf
, 40);
928 crypto_free_pk_env(pk
);
932 test_dir_format(void)
934 char buf
[8192], buf2
[8192];
936 char fingerprint
[FINGERPRINT_LEN
+1];
937 char *pk1_str
= NULL
, *pk2_str
= NULL
, *pk3_str
= NULL
, *cp
;
938 size_t pk1_str_len
, pk2_str_len
, pk3_str_len
;
940 crypto_pk_env_t
*pk1
= NULL
, *pk2
= NULL
, *pk3
= NULL
;
941 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
942 struct addr_policy_t ex1
, ex2
;
943 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
945 char *bw_lines
= NULL
;
947 test_assert( (pk1
= crypto_new_pk_env()) );
948 test_assert( (pk2
= crypto_new_pk_env()) );
949 test_assert( (pk3
= crypto_new_pk_env()) );
950 test_assert(! crypto_pk_generate_key(pk1
));
951 test_assert(! crypto_pk_generate_key(pk2
));
952 test_assert(! crypto_pk_generate_key(pk3
));
954 test_assert( is_legal_nickname("a"));
955 test_assert(!is_legal_nickname(""));
956 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
957 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
958 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
959 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
960 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
962 test_assert( is_legal_nickname_or_hexdigest(
963 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
965 test_assert(!is_legal_nickname_or_hexdigest(
966 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
968 test_assert(!is_legal_nickname_or_hexdigest(
969 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
970 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
971 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
972 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
974 get_platform_str(platform
, sizeof(platform
));
975 memset(&r1
,0,sizeof(r1
));
976 memset(&r2
,0,sizeof(r2
));
977 r1
.address
= tor_strdup("testaddr1.foo.bar");
978 r1
.addr
= 0xc0a80001u
; /* 192.168.0.1 */
981 r1
.socks_port
= 9002;
984 r1
.identity_pkey
= pk2
;
985 r1
.bandwidthrate
= 1000;
986 r1
.bandwidthburst
= 5000;
987 r1
.bandwidthcapacity
= 10000;
988 r1
.exit_policy
= NULL
;
989 r1
.nickname
= tor_strdup("Magri");
990 r1
.platform
= tor_strdup(platform
);
992 ex1
.policy_type
= ADDR_POLICY_ACCEPT
;
996 ex1
.prt_min
= ex1
.prt_max
= 80;
998 ex2
.policy_type
= ADDR_POLICY_REJECT
;
1000 ex2
.msk
= 0xFF000000u
;
1001 ex2
.prt_min
= ex2
.prt_max
= 24;
1003 r2
.address
= tor_strdup("tor.tor.tor");
1004 r2
.addr
= 0x0a030201u
; /* 10.3.2.1 */
1005 r2
.platform
= tor_strdup(platform
);
1006 r2
.published_on
= 5;
1010 r2
.onion_pkey
= pk2
;
1011 r2
.identity_pkey
= pk1
;
1012 r2
.bandwidthrate
= r2
.bandwidthburst
= r2
.bandwidthcapacity
= 3000;
1013 r2
.exit_policy
= &ex1
;
1014 r2
.nickname
= tor_strdup("Fred");
1016 bw_lines
= rep_hist_get_bandwidth_lines();
1017 test_assert(bw_lines
);
1018 test_assert(!strcmpstart(bw_lines
, "opt write-history "));
1020 test_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
1022 test_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
1024 test_assert(!crypto_pk_write_public_key_to_string(pk3
, &pk3_str
,
1027 memset(buf
, 0, 2048);
1028 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1030 strcpy(buf2
, "router Magri testaddr1.foo.bar 9000 9002 9003\n"
1031 "platform Tor "VERSION
" on ");
1032 strcat(buf2
, get_uname());
1034 "published 1970-01-01 00:00:00\n"
1035 "opt fingerprint ");
1036 test_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
1037 strcat(buf2
, fingerprint
);
1038 strcat(buf2
, "\nopt uptime 0\n"
1039 /* XXX the "0" above is hardcoded, but even if we made it reflect
1040 * uptime, that still wouldn't make it right, because the two
1041 * descriptors might be made on different seconds... hm. */
1042 "bandwidth 1000 5000 10000\n"
1044 strcat(buf2
, pk1_str
);
1045 strcat(buf2
, "signing-key\n");
1046 strcat(buf2
, pk2_str
);
1047 strcat(buf2
, bw_lines
);
1048 strcat(buf2
, "router-signature\n");
1049 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same twice*/
1051 test_streq(buf
, buf2
);
1054 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1056 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
);
1058 test_streq(rp1
->address
, r1
.address
);
1059 test_eq(rp1
->or_port
, r1
.or_port
);
1060 test_eq(rp1
->socks_port
, r1
.socks_port
);
1061 test_eq(rp1
->dir_port
, r1
.dir_port
);
1062 test_eq(rp1
->bandwidthrate
, r1
.bandwidthrate
);
1063 test_eq(rp1
->bandwidthburst
, r1
.bandwidthburst
);
1064 test_eq(rp1
->bandwidthcapacity
, r1
.bandwidthcapacity
);
1065 test_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
1066 test_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
1067 test_assert(rp1
->exit_policy
== NULL
);
1070 /* XXX Once we have exit policies, test this again. XXX */
1071 strcpy(buf2
, "router tor.tor.tor 9005 0 0 3000\n");
1072 strcat(buf2
, pk2_str
);
1073 strcat(buf2
, "signing-key\n");
1074 strcat(buf2
, pk1_str
);
1075 strcat(buf2
, "accept *:80\nreject 18.*:24\n\n");
1076 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk2
)>0);
1077 test_streq(buf
, buf2
);
1080 rp2
= router_parse_entry_from_string(&cp
);
1082 test_streq(rp2
->address
, r2
.address
);
1083 test_eq(rp2
->or_port
, r2
.or_port
);
1084 test_eq(rp2
->socks_port
, r2
.socks_port
);
1085 test_eq(rp2
->dir_port
, r2
.dir_port
);
1086 test_eq(rp2
->bandwidth
, r2
.bandwidth
);
1087 test_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
1088 test_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
1089 test_eq(rp2
->exit_policy
->policy_type
, EXIT_POLICY_ACCEPT
);
1090 test_streq(rp2
->exit_policy
->string
, "accept *:80");
1091 test_streq(rp2
->exit_policy
->address
, "*");
1092 test_streq(rp2
->exit_policy
->port
, "80");
1093 test_eq(rp2
->exit_policy
->next
->policy_type
, EXIT_POLICY_REJECT
);
1094 test_streq(rp2
->exit_policy
->next
->string
, "reject 18.*:24");
1095 test_streq(rp2
->exit_policy
->next
->address
, "18.*");
1096 test_streq(rp2
->exit_policy
->next
->port
, "24");
1097 test_assert(rp2
->exit_policy
->next
->next
== NULL
);
1100 /* Okay, now for the directories. */
1101 crypto_pk_get_fingerprint(pk2
, buf
, 1);
1102 add_fingerprint_to_dir("Magri", buf
);
1103 crypto_pk_get_fingerprint(pk1
, buf
, 1);
1104 add_fingerprint_to_dir("Fred", buf
);
1105 /* Make sure routers aren't too far in the past any more. */
1106 r1
.published_on
= time(NULL
);
1107 r2
.published_on
= time(NULL
)-3*60*60;
1108 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1110 test_eq(dirserv_add_descriptor((const char**)&cp
), 1);
1111 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk1
)>0);
1113 test_eq(dirserv_add_descriptor((const char**)&cp
), 1);
1114 get_options()->Nickname
= tor_strdup("DirServer");
1115 test_assert(!dirserv_dump_directory_to_string(buf
,8192,pk3
));
1117 test_assert(!router_parse_routerlist_from_directory(buf
, &dir1
, pk3
, 1));
1118 test_eq(2, smartlist_len(dir1
->routers
));
1119 dirserv_free_fingerprint_list();
1123 if (pk1
) crypto_free_pk_env(pk1
);
1124 if (pk2
) crypto_free_pk_env(pk2
);
1125 if (rp1
) routerinfo_free(rp1
);
1126 if (rp2
) routerinfo_free(rp2
);
1127 tor_free(dir1
); /* XXXX And more !*/
1128 tor_free(dir2
); /* And more !*/
1130 /* Try out version parsing functionality */
1131 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1
));
1132 test_eq(0, ver1
.major
);
1133 test_eq(3, ver1
.minor
);
1134 test_eq(4, ver1
.micro
);
1135 test_eq(VER_PRE
, ver1
.status
);
1136 test_eq(2, ver1
.patchlevel
);
1137 test_eq(IS_CVS
, ver1
.cvs
);
1138 test_eq(0, tor_version_parse("0.3.4rc1", &ver1
));
1139 test_eq(0, ver1
.major
);
1140 test_eq(3, ver1
.minor
);
1141 test_eq(4, ver1
.micro
);
1142 test_eq(VER_RC
, ver1
.status
);
1143 test_eq(1, ver1
.patchlevel
);
1144 test_eq(IS_NOT_CVS
, ver1
.cvs
);
1145 test_eq(0, tor_version_parse("1.3.4", &ver1
));
1146 test_eq(1, ver1
.major
);
1147 test_eq(3, ver1
.minor
);
1148 test_eq(4, ver1
.micro
);
1149 test_eq(VER_RELEASE
, ver1
.status
);
1150 test_eq(0, ver1
.patchlevel
);
1151 test_eq(IS_NOT_CVS
, ver1
.cvs
);
1152 test_eq(0, tor_version_parse("1.3.4.999", &ver1
));
1153 test_eq(1, ver1
.major
);
1154 test_eq(3, ver1
.minor
);
1155 test_eq(4, ver1
.micro
);
1156 test_eq(VER_RELEASE
, ver1
.status
);
1157 test_eq(999, ver1
.patchlevel
);
1158 test_eq(IS_NOT_CVS
, ver1
.cvs
);
1160 /* make sure is_obsolete_version() works */
1161 test_eq(1, is_obsolete_version("0.0.1", "Tor 0.0.2"));
1162 test_eq(1, is_obsolete_version("0.0.1", "0.0.2, Tor 0.0.3"));
1163 test_eq(1, is_obsolete_version("0.0.1", "0.0.2,Tor 0.0.3"));
1164 test_eq(1, is_obsolete_version("0.0.1", "0.0.3,BetterTor 0.0.1"));
1165 test_eq(0, is_obsolete_version("0.0.2", "Tor 0.0.2,Tor 0.0.3"));
1166 test_eq(1, is_obsolete_version("0.0.2", "Tor 0.0.2pre1,Tor 0.0.3"));
1167 test_eq(0, is_obsolete_version("0.1.0", "Tor 0.0.2,Tor 0.0.3"));
1168 test_eq(0, is_obsolete_version("0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8"));
1169 test_eq(0, is_obsolete_version("0.0.5", "0.0.5-cvs"));
1170 test_eq(0, is_obsolete_version("0.0.5.1-cvs", "0.0.5"));
1172 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1173 test_eq(1, tor_version_as_new_as(
1174 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8rc2"));
1175 test_eq(0, tor_version_as_new_as(
1176 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1183 char address1
[] = "fooaddress.onion";
1184 char address2
[] = "aaaaaaaaaaaaaaaa.onion";
1185 rend_service_descriptor_t
*d1
, *d2
;
1188 crypto_pk_env_t
*pk1
;
1190 pk1
= crypto_new_pk_env();
1192 test_assert(!crypto_pk_generate_key(pk1
));
1193 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
1196 d1
->timestamp
= now
;
1197 d1
->n_intro_points
= 3;
1198 d1
->intro_points
= tor_malloc(sizeof(char*)*3);
1199 d1
->intro_points
[0] = tor_strdup("tom");
1200 d1
->intro_points
[1] = tor_strdup("crow");
1201 d1
->intro_points
[2] = tor_strdup("joel");
1202 test_assert(! rend_encode_service_descriptor(d1
, pk1
, &encoded
, &len
));
1203 d2
= rend_parse_service_descriptor(encoded
, len
);
1206 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
1207 test_eq(d2
->timestamp
, now
);
1208 test_eq(d2
->n_intro_points
, 3);
1209 test_streq(d2
->intro_points
[0], "tom");
1210 test_streq(d2
->intro_points
[1], "crow");
1211 test_streq(d2
->intro_points
[2], "joel");
1213 test_eq(-1, rend_parse_rendezvous_address(address1
));
1214 test_eq( 0, rend_parse_rendezvous_address(address2
));
1216 rend_service_descriptor_free(d1
);
1217 rend_service_descriptor_free(d2
);
1221 main(int c
, char**v
) {
1222 or_options_t
*options
= tor_malloc_zero(sizeof(or_options_t
));
1223 options_init(options
);
1224 set_options(options
);
1229 atexit(remove_directory
);
1231 // puts("========================== Buffers =========================");
1232 if (0) test_buffers();
1233 puts("\n========================== Crypto ==========================");
1234 // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
1237 puts("\n========================= Util ============================");
1241 puts("\n========================= Onion Skins =====================");
1243 test_onion_handshake();
1244 puts("\n========================= Directory Formats ===============");
1246 puts("\n========================= Rendezvous functionality ========");