1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
5 const char test_c_id
[] =
10 * \brief Unit tests for many pieces of the lower level Tor modules.
27 #include "../common/test.h"
28 #include "../common/torgzip.h"
32 /* These functions are file-local, but are exposed so we can test. */
33 void get_platform_str(char *platform
, size_t len
);
34 size_t read_escaped_data(const char *data
, size_t len
, int translate_newlines
,
36 or_options_t
*options_new(void);
37 int parse_addr_policy(config_line_t
*cfg
, addr_policy_t
**dest
,
40 static char temp_dir
[256];
45 static int is_setup
= 0;
51 tor_snprintf(temp_dir
, sizeof(temp_dir
),
52 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
55 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
56 r
= mkdir(temp_dir
, 0700);
59 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
67 get_fname(const char *name
)
69 static char buf
[1024];
71 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
76 remove_directory(void)
78 smartlist_t
*elements
= tor_listdir(temp_dir
);
80 SMARTLIST_FOREACH(elements
, const char *, cp
,
82 size_t len
= strlen(cp
)+strlen(temp_dir
)+16;
83 char *tmp
= tor_malloc(len
);
84 tor_snprintf(tmp
, len
, "%s"PATH_SEPARATOR
"%s", temp_dir
, cp
);
88 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
89 smartlist_free(elements
);
94 static crypto_pk_env_t
*
97 static crypto_pk_env_t
*pregen
[3] = {NULL
, NULL
, NULL
};
98 tor_assert(idx
< (int)(sizeof(pregen
)/sizeof(pregen
[0])));
100 pregen
[idx
] = crypto_new_pk_env();
101 tor_assert(!crypto_pk_generate_key(pregen
[idx
]));
103 return crypto_pk_dup_key(pregen
[idx
]);
119 if (!(buf
= buf_new()))
122 test_eq(buf_capacity(buf
), 4096);
123 test_eq(buf_datalen(buf
), 0);
126 * General pointer frobbing
128 for (j
=0;j
<256;++j
) {
131 write_to_buf(str
, 256, buf
);
132 write_to_buf(str
, 256, buf
);
133 test_eq(buf_datalen(buf
), 512);
134 fetch_from_buf(str2
, 200, buf
);
135 test_memeq(str
, str2
, 200);
136 test_eq(buf_datalen(buf
), 312);
137 memset(str2
, 0, sizeof(str2
));
139 fetch_from_buf(str2
, 256, buf
);
140 test_memeq(str
+200, str2
, 56);
141 test_memeq(str
, str2
+56, 200);
142 test_eq(buf_datalen(buf
), 56);
143 memset(str2
, 0, sizeof(str2
));
144 /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
145 * another 3584 bytes, we hit the end. */
147 write_to_buf(str
, 256, buf
);
150 test_eq(buf_datalen(buf
), 3896);
151 fetch_from_buf(str2
, 56, buf
);
152 test_eq(buf_datalen(buf
), 3840);
153 test_memeq(str
+200, str2
, 56);
155 memset(str2
, 0, sizeof(str2
));
156 fetch_from_buf(str2
, 256, buf
);
157 test_memeq(str
, str2
, 256);
159 test_eq(buf_datalen(buf
), 0);
162 /* Okay, now make sure growing can work. */
163 buf
= buf_new_with_capacity(16);
164 test_eq(buf_capacity(buf
), 16);
165 write_to_buf(str
+1, 255, buf
);
166 test_eq(buf_capacity(buf
), 256);
167 fetch_from_buf(str2
, 254, buf
);
168 test_memeq(str
+1, str2
, 254);
169 test_eq(buf_capacity(buf
), 256);
171 write_to_buf(str
, 32, buf
);
172 test_eq(buf_capacity(buf
), 256);
174 write_to_buf(str
, 256, buf
);
176 test_eq(buf_capacity(buf
), 512);
177 test_eq(buf_datalen(buf
), 33+256);
178 fetch_from_buf(str2
, 33, buf
);
179 test_eq(*str2
, str
[255]);
181 test_memeq(str2
+1, str
, 32);
182 test_eq(buf_capacity(buf
), 512);
183 test_eq(buf_datalen(buf
), 256);
184 fetch_from_buf(str2
, 256, buf
);
185 test_memeq(str
, str2
, 256);
187 /* now try shrinking: case 1. */
189 buf
= buf_new_with_capacity(33668);
191 write_to_buf(str
,255, buf
);
193 test_eq(buf_capacity(buf
), 33668);
194 test_eq(buf_datalen(buf
), 17085);
195 for (j
=0; j
< 40; ++j
) {
196 fetch_from_buf(str2
, 255,buf
);
197 test_memeq(str2
, str
, 255);
200 /* now try shrinking: case 2. */
202 buf
= buf_new_with_capacity(33668);
204 write_to_buf(str
,255, buf
);
206 for (j
=0; j
< 20; ++j
) {
207 fetch_from_buf(str2
, 255,buf
);
208 test_memeq(str2
, str
, 255);
211 write_to_buf(str
,255, buf
);
213 test_eq(buf_capacity(buf
),33668);
214 for (j
=0; j
< 120; ++j
) {
215 fetch_from_buf(str2
, 255,buf
);
216 test_memeq(str2
, str
, 255);
228 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
232 s
= open(get_fname("data"), O_RDONLY
, 0);
234 errno
= 0; /* XXXX */
235 i
= read_to_buf(s
, 10, buf
, &eof
);
236 printf("%s\n", strerror(errno
));
239 test_eq(buf_capacity(buf
), 4096);
240 test_eq(buf_datalen(buf
), 10);
242 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10);
244 /* Test reading 0 bytes. */
245 i
= read_to_buf(s
, 0, buf
, &eof
);
246 test_eq(buf_capacity(buf
), 512*1024);
247 test_eq(buf_datalen(buf
), 10);
251 /* Now test when buffer is filled exactly. */
252 buf2
= buf_new_with_capacity(6);
253 i
= read_to_buf(s
, 6, buf2
, &eof
);
254 test_eq(buf_capacity(buf2
), 6);
255 test_eq(buf_datalen(buf2
), 6);
258 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
261 /* Now test when buffer is filled with more data to read. */
262 buf2
= buf_new_with_capacity(32);
263 i
= read_to_buf(s
, 128, buf2
, &eof
);
264 test_eq(buf_capacity(buf2
), 128);
265 test_eq(buf_datalen(buf2
), 32);
270 /* Now read to eof. */
271 test_assert(buf_capacity(buf
) > 256);
272 i
= read_to_buf(s
, 1024, buf
, &eof
);
273 test_eq(i
, (256-32-10-6));
274 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
275 test_eq(buf_datalen(buf
), 256-6-32);
276 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10); /* XXX Check rest. */
279 i
= read_to_buf(s
, 1024, buf
, &eof
);
281 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
282 test_eq(buf_datalen(buf
), 256-6-32);
293 crypto_dh_env_t
*dh1
, *dh2
;
300 dh1
= crypto_dh_new();
301 dh2
= crypto_dh_new();
302 test_eq(crypto_dh_get_bytes(dh1
), DH_BYTES
);
303 test_eq(crypto_dh_get_bytes(dh2
), DH_BYTES
);
305 memset(p1
, 0, DH_BYTES
);
306 memset(p2
, 0, DH_BYTES
);
307 test_memeq(p1
, p2
, DH_BYTES
);
308 test_assert(! crypto_dh_get_public(dh1
, p1
, DH_BYTES
));
309 test_memneq(p1
, p2
, DH_BYTES
);
310 test_assert(! crypto_dh_get_public(dh2
, p2
, DH_BYTES
));
311 test_memneq(p1
, p2
, DH_BYTES
);
313 memset(s1
, 0, DH_BYTES
);
314 memset(s2
, 0xFF, DH_BYTES
);
315 s1len
= crypto_dh_compute_secret(dh1
, p2
, DH_BYTES
, s1
, 50);
316 s2len
= crypto_dh_compute_secret(dh2
, p1
, DH_BYTES
, s2
, 50);
317 test_assert(s1len
> 0);
318 test_eq(s1len
, s2len
);
319 test_memeq(s1
, s2
, s1len
);
328 crypto_cipher_env_t
*env1
, *env2
;
329 crypto_pk_env_t
*pk1
, *pk2
;
330 char *data1
, *data2
, *data3
, *cp
;
334 data1
= tor_malloc(1024);
335 data2
= tor_malloc(1024);
336 data3
= tor_malloc(1024);
337 test_assert(data1
&& data2
&& data3
);
340 test_assert(! crypto_seed_rng());
341 crypto_rand(data1
, 100);
342 crypto_rand(data2
, 100);
343 test_memneq(data1
,data2
,100);
345 /* Now, test encryption and decryption with stream cipher. */
347 for (i
= 1023; i
>0; i
-= 35)
348 strncat(data1
, "Now is the time for all good onions", i
);
350 memset(data2
, 0, 1024);
351 memset(data3
, 0, 1024);
352 env1
= crypto_new_cipher_env();
354 env2
= crypto_new_cipher_env();
356 j
= crypto_cipher_generate_key(env1
);
357 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
358 crypto_cipher_encrypt_init_cipher(env1
);
359 crypto_cipher_decrypt_init_cipher(env2
);
361 /* Try encrypting 512 chars. */
362 crypto_cipher_encrypt(env1
, data2
, data1
, 512);
363 crypto_cipher_decrypt(env2
, data3
, data2
, 512);
364 test_memeq(data1
, data3
, 512);
365 test_memneq(data1
, data2
, 512);
367 /* Now encrypt 1 at a time, and get 1 at a time. */
368 for (j
= 512; j
< 560; ++j
) {
369 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 1);
371 for (j
= 512; j
< 560; ++j
) {
372 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 1);
374 test_memeq(data1
, data3
, 560);
375 /* Now encrypt 3 at a time, and get 5 at a time. */
376 for (j
= 560; j
< 1024-5; j
+= 3) {
377 crypto_cipher_encrypt(env1
, data2
+j
, data1
+j
, 3);
379 for (j
= 560; j
< 1024-5; j
+= 5) {
380 crypto_cipher_decrypt(env2
, data3
+j
, data2
+j
, 5);
382 test_memeq(data1
, data3
, 1024-5);
383 /* Now make sure that when we encrypt with different chunk sizes, we get
385 crypto_free_cipher_env(env2
);
387 memset(data3
, 0, 1024);
388 env2
= crypto_new_cipher_env();
390 crypto_cipher_set_key(env2
, crypto_cipher_get_key(env1
));
391 crypto_cipher_encrypt_init_cipher(env2
);
392 for (j
= 0; j
< 1024-16; j
+= 17) {
393 crypto_cipher_encrypt(env2
, data3
+j
, data1
+j
, 17);
395 for (j
= 0; j
< 1024-16; ++j
) {
396 if (data2
[j
] != data3
[j
]) {
397 printf("%d: %d\t%d\n", j
, (int) data2
[j
], (int) data3
[j
]);
400 test_memeq(data2
, data3
, 1024-16);
401 crypto_free_cipher_env(env1
);
402 crypto_free_cipher_env(env2
);
404 /* Test vectors for stream ciphers. */
405 /* XXXX Look up some test vectors for the ciphers and make sure we match. */
407 /* Test SHA-1 with a test vector from the specification. */
408 i
= crypto_digest(data1
, "abc", 3);
410 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
411 "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
413 /* Public-key ciphers */
414 pk1
= pk_generate(0);
415 pk2
= crypto_new_pk_env();
416 test_assert(pk1
&& pk2
);
417 test_assert(! crypto_pk_write_public_key_to_string(pk1
, &cp
, &size
));
418 test_assert(! crypto_pk_read_public_key_from_string(pk2
, cp
, size
));
419 test_eq(0, crypto_pk_cmp_keys(pk1
, pk2
));
422 test_eq(128, crypto_pk_keysize(pk1
));
423 test_eq(128, crypto_pk_keysize(pk2
));
425 test_eq(128, crypto_pk_public_encrypt(pk2
, data1
, "Hello whirled.", 15,
426 PK_PKCS1_OAEP_PADDING
));
427 test_eq(128, crypto_pk_public_encrypt(pk1
, data2
, "Hello whirled.", 15,
428 PK_PKCS1_OAEP_PADDING
));
429 /* oaep padding should make encryption not match */
430 test_memneq(data1
, data2
, 128);
431 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data1
, 128,
432 PK_PKCS1_OAEP_PADDING
,1));
433 test_streq(data3
, "Hello whirled.");
434 memset(data3
, 0, 1024);
435 test_eq(15, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
436 PK_PKCS1_OAEP_PADDING
,1));
437 test_streq(data3
, "Hello whirled.");
438 /* Can't decrypt with public key. */
439 test_eq(-1, crypto_pk_private_decrypt(pk2
, data3
, data2
, 128,
440 PK_PKCS1_OAEP_PADDING
,1));
441 /* Try again with bad padding */
442 memcpy(data2
+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
443 test_eq(-1, crypto_pk_private_decrypt(pk1
, data3
, data2
, 128,
444 PK_PKCS1_OAEP_PADDING
,1));
446 /* File operations: save and load private key */
447 test_assert(! crypto_pk_write_private_key_to_filename(pk1
,
448 get_fname("pkey1")));
450 test_assert(! crypto_pk_read_private_key_from_filename(pk2
,
451 get_fname("pkey1")));
452 test_eq(15, crypto_pk_private_decrypt(pk2
, data3
, data1
, 128,
453 PK_PKCS1_OAEP_PADDING
,1));
455 /* Now try signing. */
456 strlcpy(data1
, "Ossifrage", 1024);
457 test_eq(128, crypto_pk_private_sign(pk1
, data2
, data1
, 10));
458 test_eq(10, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
459 test_streq(data3
, "Ossifrage");
460 /* Try signing digests. */
461 test_eq(128, crypto_pk_private_sign_digest(pk1
, data2
, data1
, 10));
462 test_eq(20, crypto_pk_public_checksig(pk1
, data3
, data2
, 128));
463 test_eq(0, crypto_pk_public_checksig_digest(pk1
, data1
, 10, data2
, 128));
464 test_eq(-1, crypto_pk_public_checksig_digest(pk1
, data1
, 11, data2
, 128));
465 /*XXXX test failed signing*/
468 crypto_free_pk_env(pk2
);
470 i
= crypto_pk_asn1_encode(pk1
, data1
, 1024);
472 pk2
= crypto_pk_asn1_decode(data1
, i
);
473 test_assert(crypto_pk_cmp_keys(pk1
,pk2
) == 0);
475 /* Try with hybrid encryption wrappers. */
476 crypto_rand(data1
, 1024);
477 for (i
= 0; i
< 3; ++i
) {
478 for (j
= 85; j
< 140; ++j
) {
479 memset(data2
,0,1024);
480 memset(data3
,0,1024);
481 if (i
== 0 && j
< 129)
483 p
= (i
==0)?PK_NO_PADDING
:
484 (i
==1)?PK_PKCS1_PADDING
:PK_PKCS1_OAEP_PADDING
;
485 len
= crypto_pk_public_hybrid_encrypt(pk1
,data2
,data1
,j
,p
,0);
487 len
= crypto_pk_private_hybrid_decrypt(pk1
,data3
,data2
,len
,p
,1);
489 test_memeq(data1
,data3
,j
);
492 crypto_free_pk_env(pk1
);
493 crypto_free_pk_env(pk2
);
496 strlcpy(data1
, "Test string that contains 35 chars.", 1024);
497 strlcat(data1
, " 2nd string that contains 35 chars.", 1024);
499 i
= base64_encode(data2
, 1024, data1
, 71);
500 j
= base64_decode(data3
, 1024, data2
, i
);
501 test_streq(data3
, data1
);
503 test_assert(data2
[i
] == '\0');
505 crypto_rand(data1
, DIGEST_LEN
);
506 memset(data2
, 100, 1024);
507 digest_to_base64(data2
, data1
);
508 test_eq(BASE64_DIGEST_LEN
, strlen(data2
));
509 test_eq(100, data2
[BASE64_DIGEST_LEN
+2]);
510 memset(data3
, 99, 1024);
511 digest_from_base64(data3
, data2
);
512 test_memeq(data1
, data3
, DIGEST_LEN
);
513 test_eq(99, data3
[DIGEST_LEN
+1]);
516 strlcpy(data1
, "5chrs", 1024);
517 /* bit pattern is: [35 63 68 72 73] ->
518 * [00110101 01100011 01101000 01110010 01110011]
519 * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
521 base32_encode(data2
, 9, data1
, 5);
522 test_streq(data2
, "gvrwq4tt");
524 strlcpy(data1
, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
525 base32_encode(data2
, 30, data1
, 10);
526 test_streq(data2
, "772w2rfobvomsywe");
529 strlcpy(data1
, "6chrs\xff", 1024);
530 base16_encode(data2
, 13, data1
, 6);
531 test_streq(data2
, "3663687273FF");
533 strlcpy(data1
, "f0d678affc000100", 1024);
534 i
= base16_decode(data2
, 8, data1
, 16);
536 test_memeq(data2
, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
544 test_crypto_s2k(void)
551 memset(buf
, 0, sizeof(buf
));
552 memset(buf2
, 0, sizeof(buf2
));
553 buf3
= tor_malloc(65536);
554 memset(buf3
, 0, 65536);
556 secret_to_key(buf
+9, 20, "", 0, buf
);
557 crypto_digest(buf2
+9, buf3
, 1024);
558 test_memeq(buf
, buf2
, 29);
560 memcpy(buf
,"vrbacrda",8);
561 memcpy(buf2
,"vrbacrda",8);
564 secret_to_key(buf
+9, 20, "12345678", 8, buf
);
565 for (i
= 0; i
< 65536; i
+= 16) {
566 memcpy(buf3
+i
, "vrbacrda12345678", 16);
568 crypto_digest(buf2
+9, buf3
, 65536);
569 test_memeq(buf
, buf2
, 29);
573 _compare_strs(const void **a
, const void **b
)
575 const char *s1
= *a
, *s2
= *b
;
576 return strcmp(s1
, s2
);
580 _compare_without_first_ch(const void *a
, const void **b
)
582 const char *s1
= a
, *s2
= *b
;
583 return strcasecmp(s1
+1, s2
);
589 struct timeval start
, end
;
591 char timestr
[RFC1123_TIME_LEN
+1];
600 start
.tv_usec
= 5000;
605 test_eq(0L, tv_udiff(&start
, &end
));
609 test_eq(2000L, tv_udiff(&start
, &end
));
613 test_eq(1002000L, tv_udiff(&start
, &end
));
617 test_eq(995000L, tv_udiff(&start
, &end
));
621 test_eq(-1005000L, tv_udiff(&start
, &end
));
623 /* The test values here are confirmed to be correct on a platform
624 * with a working timegm. */
625 a_time
.tm_year
= 2003-1900;
631 test_eq((time_t) 1062224095UL, tor_timegm(&a_time
));
632 a_time
.tm_year
= 2004-1900; /* Try a leap year, after feb. */
633 test_eq((time_t) 1093846495UL, tor_timegm(&a_time
));
634 a_time
.tm_mon
= 1; /* Try a leap year, in feb. */
636 test_eq((time_t) 1076393695UL, tor_timegm(&a_time
));
638 format_rfc1123_time(timestr
, 0);
639 test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr
);
640 format_rfc1123_time(timestr
, (time_t)1091580502UL);
641 test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr
);
644 i
= parse_rfc1123_time(timestr
, &t_res
);
646 test_eq(t_res
, (time_t)1091580502UL);
648 /* Test tor_strstrip() */
649 strlcpy(buf
, "Testing 1 2 3", sizeof(buf
));
650 test_eq(0, tor_strstrip(buf
, ",!"));
651 test_streq(buf
, "Testing 1 2 3");
652 strlcpy(buf
, "!Testing 1 2 3?", sizeof(buf
));
653 test_eq(5, tor_strstrip(buf
, "!? "));
654 test_streq(buf
, "Testing123");
656 /* Test tor_strpartition() */
657 test_assert(! tor_strpartition(buf
, sizeof(buf
), "abcdefghi", "##", 3));
658 test_streq(buf
, "abc##def##ghi");
660 /* Test parse_addr_port */
661 cp
= NULL
; u32
= 3; u16
= 3;
662 test_assert(!parse_addr_port(LOG_WARN
, "1.2.3.4", &cp
, &u32
, &u16
));
663 test_streq(cp
, "1.2.3.4");
664 test_eq(u32
, 0x01020304u
);
667 test_assert(!parse_addr_port(LOG_WARN
, "4.3.2.1:99", &cp
, &u32
, &u16
));
668 test_streq(cp
, "4.3.2.1");
669 test_eq(u32
, 0x04030201u
);
672 test_assert(!parse_addr_port(LOG_WARN
, "nonexistent.address:4040",
674 test_streq(cp
, "nonexistent.address");
677 test_assert(!parse_addr_port(LOG_WARN
, "localhost:9999", &cp
, &u32
, &u16
));
678 test_streq(cp
, "localhost");
679 test_eq(u32
, 0x7f000001u
);
683 test_assert(!parse_addr_port(LOG_WARN
, "localhost", NULL
, &u32
, &u16
));
685 test_eq(u32
, 0x7f000001u
);
688 test_eq(0, addr_mask_get_bits(0x0u
));
689 test_eq(32, addr_mask_get_bits(0xFFFFFFFFu
));
690 test_eq(16, addr_mask_get_bits(0xFFFF0000u
));
691 test_eq(31, addr_mask_get_bits(0xFFFFFFFEu
));
692 test_eq(1, addr_mask_get_bits(0x80000000u
));
694 /* Test tor_parse_long. */
695 test_eq(10L, tor_parse_long("10",10,0,100,NULL
,NULL
));
696 test_eq(0L, tor_parse_long("10",10,50,100,NULL
,NULL
));
698 /* Test tor_parse_uint64. */
699 test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i
, &cp
));
701 test_streq(cp
, " x");
702 test_assert(U64_LITERAL(12345678901) ==
703 tor_parse_uint64("12345678901",10,0,UINT64_MAX
, &i
, &cp
));
706 test_assert(U64_LITERAL(0) ==
707 tor_parse_uint64("12345678901",10,500,INT32_MAX
, &i
, &cp
));
710 /* Test printf with uint64 */
711 tor_snprintf(buf
, sizeof(buf
), "x!"U64_FORMAT
"!x",
712 U64_PRINTF_ARG(U64_LITERAL(12345678901)));
713 test_streq(buf
, "x!12345678901!x");
715 /* Test parse_line_from_str */
716 strlcpy(buf
, "k v\n" " key value with spaces \n" "keykey val\n"
718 "k3 \n" "\n" " \n" "#comment\n"
719 "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf
));
722 cp
= parse_line_from_str(cp
, &k
, &v
);
725 test_assert(!strcmpstart(cp
, " key value with"));
727 cp
= parse_line_from_str(cp
, &k
, &v
);
728 test_streq(k
, "key");
729 test_streq(v
, "value with spaces");
730 test_assert(!strcmpstart(cp
, "keykey"));
732 cp
= parse_line_from_str(cp
, &k
, &v
);
733 test_streq(k
, "keykey");
734 test_streq(v
, "val");
735 test_assert(!strcmpstart(cp
, "k2\n"));
737 cp
= parse_line_from_str(cp
, &k
, &v
);
740 test_assert(!strcmpstart(cp
, "k3 \n"));
742 cp
= parse_line_from_str(cp
, &k
, &v
);
745 test_assert(!strcmpstart(cp
, "\n \n"));
747 cp
= parse_line_from_str(cp
, &k
, &v
);
750 test_assert(!strcmpstart(cp
, "k5#abc"));
752 cp
= parse_line_from_str(cp
, &k
, &v
);
755 test_assert(!strcmpstart(cp
, "k6"));
757 cp
= parse_line_from_str(cp
, &k
, &v
);
759 test_streq(v
, "val");
762 /* Test for strcmpstart and strcmpend. */
763 test_assert(strcmpstart("abcdef", "abcdef")==0);
764 test_assert(strcmpstart("abcdef", "abc")==0);
765 test_assert(strcmpstart("abcdef", "abd")<0);
766 test_assert(strcmpstart("abcdef", "abb")>0);
767 test_assert(strcmpstart("ab", "abb")<0);
769 test_assert(strcmpend("abcdef", "abcdef")==0);
770 test_assert(strcmpend("abcdef", "def")==0);
771 test_assert(strcmpend("abcdef", "deg")<0);
772 test_assert(strcmpend("abcdef", "dee")>0);
773 test_assert(strcmpend("ab", "abb")<0);
776 char tmpbuf
[INET_NTOA_BUF_LEN
];
778 tor_inet_aton("18.244.0.188",&in
);
779 tor_inet_ntoa(&in
, tmpbuf
, sizeof(tmpbuf
));
780 test_streq(tmpbuf
, "18.244.0.188");
784 test_streq("\"\"", escaped(""));
785 test_streq("\"abcd\"", escaped("abcd"));
786 test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
787 test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
789 /* Test wrap_string */
791 smartlist_t
*sl
= smartlist_create();
792 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
794 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
796 "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
798 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
801 wrap_string(sl
, "This is a test of string wrapping functionality: woot.",
803 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
805 "### This is a\n# test of string\n# wrapping\n# functionality:\n"
809 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
820 /* XXXX test sort_strings, sort_digests, uniq_strings, uniq_digests */
822 /* Test smartlist add, del_keeporder, insert, get. */
823 sl
= smartlist_create();
824 smartlist_add(sl
, (void*)1);
825 smartlist_add(sl
, (void*)2);
826 smartlist_add(sl
, (void*)3);
827 smartlist_add(sl
, (void*)4);
828 smartlist_del_keeporder(sl
, 1);
829 smartlist_insert(sl
, 1, (void*)22);
830 smartlist_insert(sl
, 0, (void*)0);
831 smartlist_insert(sl
, 5, (void*)555);
832 test_eq_ptr((void*)0, smartlist_get(sl
,0));
833 test_eq_ptr((void*)1, smartlist_get(sl
,1));
834 test_eq_ptr((void*)22, smartlist_get(sl
,2));
835 test_eq_ptr((void*)3, smartlist_get(sl
,3));
836 test_eq_ptr((void*)4, smartlist_get(sl
,4));
837 test_eq_ptr((void*)555, smartlist_get(sl
,5));
838 /* Try deleting in the middle. */
839 smartlist_del(sl
, 1);
840 test_eq_ptr((void*)555, smartlist_get(sl
, 1));
841 /* Try deleting at the end. */
842 smartlist_del(sl
, 4);
843 test_eq(4, smartlist_len(sl
));
846 test_assert(smartlist_isin(sl
, (void*)3));
847 test_assert(!smartlist_isin(sl
, (void*)99));
849 /* Test split and join */
851 test_eq(0, smartlist_len(sl
));
852 smartlist_split_string(sl
, "abc", ":", 0, 0);
853 test_eq(1, smartlist_len(sl
));
854 test_streq("abc", smartlist_get(sl
, 0));
855 smartlist_split_string(sl
, "a::bc::", "::", 0, 0);
856 test_eq(4, smartlist_len(sl
));
857 test_streq("a", smartlist_get(sl
, 1));
858 test_streq("bc", smartlist_get(sl
, 2));
859 test_streq("", smartlist_get(sl
, 3));
860 cp
= smartlist_join_strings(sl
, "", 0, NULL
);
861 test_streq(cp
, "abcabc");
863 cp
= smartlist_join_strings(sl
, "!", 0, NULL
);
864 test_streq(cp
, "abc!a!bc!");
866 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
867 test_streq(cp
, "abcXYaXYbcXY");
869 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
870 test_streq(cp
, "abcXYaXYbcXYXY");
872 cp
= smartlist_join_strings(sl
, "", 1, NULL
);
873 test_streq(cp
, "abcabc");
876 smartlist_split_string(sl
, "/def/ /ghijk", "/", 0, 0);
877 test_eq(8, smartlist_len(sl
));
878 test_streq("", smartlist_get(sl
, 4));
879 test_streq("def", smartlist_get(sl
, 5));
880 test_streq(" ", smartlist_get(sl
, 6));
881 test_streq("ghijk", smartlist_get(sl
, 7));
882 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
885 smartlist_split_string(sl
, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE
, 0);
886 test_eq(3, smartlist_len(sl
));
887 test_streq("a", smartlist_get(sl
,0));
888 test_streq("bbd", smartlist_get(sl
,1));
889 test_streq("cdef", smartlist_get(sl
,2));
890 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
891 SPLIT_SKIP_SPACE
, 0);
892 test_eq(8, smartlist_len(sl
));
893 test_streq("z", smartlist_get(sl
,3));
894 test_streq("zhasd", smartlist_get(sl
,4));
895 test_streq("", smartlist_get(sl
,5));
896 test_streq("bnud", smartlist_get(sl
,6));
897 test_streq("", smartlist_get(sl
,7));
899 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
902 smartlist_split_string(sl
, " ab\tc \td ef ", NULL
,
903 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
904 test_eq(4, smartlist_len(sl
));
905 test_streq("ab", smartlist_get(sl
,0));
906 test_streq("c", smartlist_get(sl
,1));
907 test_streq("d", smartlist_get(sl
,2));
908 test_streq("ef", smartlist_get(sl
,3));
909 smartlist_split_string(sl
, "ghi\tj", NULL
,
910 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
911 test_eq(6, smartlist_len(sl
));
912 test_streq("ghi", smartlist_get(sl
,4));
913 test_streq("j", smartlist_get(sl
,5));
915 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
918 cp
= smartlist_join_strings(sl
, "XY", 0, NULL
);
921 cp
= smartlist_join_strings(sl
, "XY", 1, NULL
);
922 test_streq(cp
, "XY");
925 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
926 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
927 test_eq(3, smartlist_len(sl
));
928 test_streq("z", smartlist_get(sl
, 0));
929 test_streq("zhasd", smartlist_get(sl
, 1));
930 test_streq("bnud", smartlist_get(sl
, 2));
931 smartlist_split_string(sl
, " z <> zhasd <> <> bnud<> ", "<>",
932 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 2);
933 test_eq(5, smartlist_len(sl
));
934 test_streq("z", smartlist_get(sl
, 3));
935 test_streq("zhasd <> <> bnud<>", smartlist_get(sl
, 4));
936 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
939 smartlist_split_string(sl
, "abcd\n", "\n",
940 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
941 test_eq(1, smartlist_len(sl
));
942 test_streq("abcd", smartlist_get(sl
, 0));
943 smartlist_split_string(sl
, "efgh", "\n",
944 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
945 test_eq(2, smartlist_len(sl
));
946 test_streq("efgh", smartlist_get(sl
, 1));
948 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
951 /* Test smartlist sorting. */
952 smartlist_split_string(sl
, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
953 test_eq(7, smartlist_len(sl
));
954 smartlist_sort(sl
, _compare_strs
);
955 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
956 test_streq(cp
,"and,arma,by,nickm,onion,router,the");
959 test_streq("nickm", smartlist_bsearch(sl
, "zNicKM",
960 _compare_without_first_ch
));
961 test_streq("and", smartlist_bsearch(sl
, " AND", _compare_without_first_ch
));
962 test_eq_ptr(NULL
, smartlist_bsearch(sl
, " ANz", _compare_without_first_ch
));
964 /* Test reverse() and pop_last() */
965 smartlist_reverse(sl
);
966 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
967 test_streq(cp
,"the,router,onion,nickm,by,arma,and");
969 cp
= smartlist_pop_last(sl
);
970 test_streq(cp
, "and");
972 test_eq(smartlist_len(sl
), 6);
973 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
977 smartlist_split_string(sl
,
978 "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
980 smartlist_sort(sl
, _compare_strs
);
981 smartlist_uniq(sl
, _compare_strs
, NULL
);
982 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
983 test_streq(cp
, "50,a,canal,man,noon,panama,plan,radar");
986 /* Test string_isin. */
987 test_assert(smartlist_string_isin(sl
, "noon"));
988 test_assert(!smartlist_string_isin(sl
, "noonoon"));
989 test_assert(smartlist_string_num_isin(sl
, 50));
990 test_assert(!smartlist_string_num_isin(sl
, 60));
991 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
994 /* Test string_remove and remove and join_strings2 */
995 smartlist_split_string(sl
,
996 "Some say the Earth will end in ice and some in fire",
998 cp
= smartlist_get(sl
, 4);
999 test_streq(cp
, "will");
1000 smartlist_add(sl
, cp
);
1001 smartlist_remove(sl
, cp
);
1002 cp
= smartlist_join_strings(sl
, ",", 0, NULL
);
1003 test_streq(cp
, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
1005 smartlist_string_remove(sl
, "in");
1006 cp
= smartlist_join_strings2(sl
, "+XX", 1, 0, NULL
);
1007 test_streq(cp
, "Some+say+the+Earth+fire+end+some+ice+and");
1010 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1011 smartlist_clear(sl
);
1014 smartlist_t
*ints
= smartlist_create();
1015 smartlist_t
*odds
= smartlist_create();
1016 smartlist_t
*evens
= smartlist_create();
1017 smartlist_t
*primes
= smartlist_create();
1019 for (i
=1; i
< 10; i
+= 2)
1020 smartlist_add(odds
, (void*)(uintptr_t)i
);
1021 for (i
=0; i
< 10; i
+= 2)
1022 smartlist_add(evens
, (void*)(uintptr_t)i
);
1025 smartlist_add_all(ints
, odds
);
1026 smartlist_add_all(ints
, evens
);
1027 test_eq(smartlist_len(ints
), 10);
1029 smartlist_add(primes
, (void*)2);
1030 smartlist_add(primes
, (void*)3);
1031 smartlist_add(primes
, (void*)5);
1032 smartlist_add(primes
, (void*)7);
1035 test_assert(smartlist_overlap(ints
, odds
));
1036 test_assert(smartlist_overlap(odds
, primes
));
1037 test_assert(smartlist_overlap(evens
, primes
));
1038 test_assert(!smartlist_overlap(odds
, evens
));
1041 smartlist_add_all(sl
, odds
);
1042 smartlist_intersect(sl
, primes
);
1043 test_eq(smartlist_len(sl
), 3);
1044 test_assert(smartlist_isin(sl
, (void*)3));
1045 test_assert(smartlist_isin(sl
, (void*)5));
1046 test_assert(smartlist_isin(sl
, (void*)7));
1049 smartlist_add_all(sl
, primes
);
1050 smartlist_subtract(sl
, odds
);
1051 test_eq(smartlist_len(sl
), 1);
1052 test_assert(smartlist_isin(sl
, (void*)2));
1054 smartlist_free(odds
);
1055 smartlist_free(evens
);
1056 smartlist_free(ints
);
1057 smartlist_free(primes
);
1058 smartlist_clear(sl
);
1065 _compare_strings_for_pqueue(const void *s1
, const void *s2
)
1067 return strcmp((const char*)s1
, (const char*)s2
);
1074 int (*cmp
)(const void *, const void*);
1075 #define OK() smartlist_pqueue_assert_ok(sl, cmp)
1077 cmp
= _compare_strings_for_pqueue
;
1079 sl
= smartlist_create();
1080 smartlist_pqueue_add(sl
, cmp
, (char*)"cows");
1081 smartlist_pqueue_add(sl
, cmp
, (char*)"zebras");
1082 smartlist_pqueue_add(sl
, cmp
, (char*)"fish");
1083 smartlist_pqueue_add(sl
, cmp
, (char*)"frogs");
1084 smartlist_pqueue_add(sl
, cmp
, (char*)"apples");
1085 smartlist_pqueue_add(sl
, cmp
, (char*)"squid");
1086 smartlist_pqueue_add(sl
, cmp
, (char*)"daschunds");
1087 smartlist_pqueue_add(sl
, cmp
, (char*)"eggplants");
1088 smartlist_pqueue_add(sl
, cmp
, (char*)"weissbier");
1089 smartlist_pqueue_add(sl
, cmp
, (char*)"lobsters");
1090 smartlist_pqueue_add(sl
, cmp
, (char*)"roquefort");
1094 test_eq(smartlist_len(sl
), 11);
1095 test_streq(smartlist_get(sl
, 0), "apples");
1096 test_streq(smartlist_pqueue_pop(sl
, cmp
), "apples");
1097 test_eq(smartlist_len(sl
), 10);
1099 test_streq(smartlist_pqueue_pop(sl
, cmp
), "cows");
1100 test_streq(smartlist_pqueue_pop(sl
, cmp
), "daschunds");
1101 smartlist_pqueue_add(sl
, cmp
, (char*)"chinchillas");
1103 smartlist_pqueue_add(sl
, cmp
, (char*)"fireflies");
1105 test_streq(smartlist_pqueue_pop(sl
, cmp
), "chinchillas");
1106 test_streq(smartlist_pqueue_pop(sl
, cmp
), "eggplants");
1107 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fireflies");
1109 test_streq(smartlist_pqueue_pop(sl
, cmp
), "fish");
1110 test_streq(smartlist_pqueue_pop(sl
, cmp
), "frogs");
1111 test_streq(smartlist_pqueue_pop(sl
, cmp
), "lobsters");
1112 test_streq(smartlist_pqueue_pop(sl
, cmp
), "roquefort");
1114 test_eq(smartlist_len(sl
), 3);
1115 test_streq(smartlist_pqueue_pop(sl
, cmp
), "squid");
1116 test_streq(smartlist_pqueue_pop(sl
, cmp
), "weissbier");
1117 test_streq(smartlist_pqueue_pop(sl
, cmp
), "zebras");
1118 test_eq(smartlist_len(sl
), 0);
1127 char *buf1
, *buf2
=NULL
, *buf3
=NULL
, *cp1
, *cp2
;
1130 tor_zlib_state_t
*state
;
1132 buf1
= tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
1133 test_assert(detect_compression_method(buf1
, strlen(buf1
)) == UNKNOWN_METHOD
);
1134 if (is_gzip_supported()) {
1135 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1138 test_assert(!memcmp(buf2
, "\037\213", 2)); /* Gztip magic. */
1139 test_assert(detect_compression_method(buf2
, len1
) == GZIP_METHOD
);
1141 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1142 GZIP_METHOD
, 1, LOG_INFO
));
1144 test_streq(buf1
,buf3
);
1150 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1153 test_assert(!memcmp(buf2
, "\x78\xDA", 2)); /* deflate magic. */
1154 test_assert(detect_compression_method(buf2
, len1
) == ZLIB_METHOD
);
1156 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
,
1157 ZLIB_METHOD
, 1, LOG_INFO
));
1159 test_streq(buf1
,buf3
);
1161 /* Check whether we can uncompress concatenated, compresed strings. */
1163 buf2
= tor_realloc(buf2
, len1
*2);
1164 memcpy(buf2
+len1
, buf2
, len1
);
1165 test_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
*2,
1166 ZLIB_METHOD
, 1, LOG_INFO
));
1167 test_eq(len2
, (strlen(buf1
)+1)*2);
1169 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
1170 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
1171 (strlen(buf1
)+1)*2);
1177 /* Check whether we can uncompress partial strings. */
1179 tor_strdup("String with low redundancy that won't be compressed much.");
1180 test_assert(!tor_gzip_compress(&buf2
, &len1
, buf1
, strlen(buf1
)+1,
1182 tor_assert(len1
>16);
1183 /* when we allow an uncomplete string, we should succeed.*/
1184 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1185 ZLIB_METHOD
, 0, LOG_INFO
));
1187 tor_assert(len2
> 5);
1188 tor_assert(!strcmpstart(buf1
, buf3
));
1190 /* when we demand a complete string, this must fail. */
1192 tor_assert(tor_gzip_uncompress(&buf3
, &len2
, buf2
, len1
-16,
1193 ZLIB_METHOD
, 1, LOG_INFO
));
1196 /* Now, try streaming compression. */
1200 state
= tor_zlib_new(1, ZLIB_METHOD
);
1202 cp1
= buf1
= tor_malloc(1024);
1204 ccp2
= "ABCDEFGHIJABCDEFGHIJ";
1206 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 0)
1208 test_eq(len2
, 0); /* Make sure we compressed it all. */
1209 test_assert(cp1
> buf1
);
1213 test_assert(tor_zlib_process(state
, &cp1
, &len1
, &ccp2
, &len2
, 1)
1216 test_assert(cp1
> cp2
); /* Make sure we really added something. */
1218 tor_assert(!tor_gzip_uncompress(&buf3
, &len2
, buf1
, 1024-len1
,
1219 ZLIB_METHOD
, 1, LOG_WARN
));
1220 test_streq(buf3
, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
1223 tor_zlib_free(state
);
1234 strmap_iter_t
*iter
;
1238 smartlist_t
*found_keys
;
1241 v
= strmap_set(map
, "K1", (void*)99);
1243 v
= strmap_set(map
, "K2", (void*)101);
1245 v
= strmap_set(map
, "K1", (void*)100);
1246 test_eq(v
, (void*)99);
1247 test_eq_ptr(strmap_get(map
,"K1"), (void*)100);
1248 test_eq_ptr(strmap_get(map
,"K2"), (void*)101);
1249 test_eq_ptr(strmap_get(map
,"K-not-there"), NULL
);
1250 strmap_assert_ok(map
);
1252 v
= strmap_remove(map
,"K2");
1253 strmap_assert_ok(map
);
1254 test_eq_ptr(v
, (void*)101);
1255 test_eq_ptr(strmap_get(map
,"K2"), NULL
);
1256 test_eq_ptr(strmap_remove(map
,"K2"), NULL
);
1258 strmap_set(map
, "K2", (void*)101);
1259 strmap_set(map
, "K3", (void*)102);
1260 strmap_set(map
, "K4", (void*)103);
1261 strmap_assert_ok(map
);
1262 strmap_set(map
, "K5", (void*)104);
1263 strmap_set(map
, "K6", (void*)105);
1264 strmap_assert_ok(map
);
1266 /* Test iterator. */
1267 iter
= strmap_iter_init(map
);
1268 found_keys
= smartlist_create();
1269 while (!strmap_iter_done(iter
)) {
1270 strmap_iter_get(iter
,&k
,&v
);
1271 smartlist_add(found_keys
, tor_strdup(k
));
1272 test_eq_ptr(v
, strmap_get(map
, k
));
1274 if (!strcmp(k
, "K2")) {
1275 iter
= strmap_iter_next_rmv(map
,iter
);
1277 iter
= strmap_iter_next(map
,iter
);
1281 /* Make sure we removed K2, but not the others. */
1282 test_eq_ptr(strmap_get(map
, "K2"), NULL
);
1283 test_eq_ptr(strmap_get(map
, "K5"), (void*)104);
1284 /* Make sure we visited everyone once */
1285 smartlist_sort_strings(found_keys
);
1286 visited
= smartlist_join_strings(found_keys
, ":", 0, NULL
);
1287 test_streq(visited
, "K1:K2:K3:K4:K5:K6");
1289 SMARTLIST_FOREACH(found_keys
, char *, cp
, tor_free(cp
));
1290 smartlist_free(found_keys
);
1292 strmap_assert_ok(map
);
1293 /* Clean up after ourselves. */
1294 strmap_free(map
, NULL
);
1296 /* Now try some lc functions. */
1298 strmap_set_lc(map
,"Ab.C", (void*)1);
1299 test_eq_ptr(strmap_get(map
,"ab.c"), (void*)1);
1300 strmap_assert_ok(map
);
1301 test_eq_ptr(strmap_get_lc(map
,"AB.C"), (void*)1);
1302 test_eq_ptr(strmap_get(map
,"AB.C"), NULL
);
1303 test_eq_ptr(strmap_remove_lc(map
,"aB.C"), (void*)1);
1304 strmap_assert_ok(map
);
1305 test_eq_ptr(strmap_get_lc(map
,"AB.C"), NULL
);
1306 strmap_free(map
,NULL
);
1312 char *fname1
= tor_strdup(get_fname("mapped_1"));
1313 char *fname2
= tor_strdup(get_fname("mapped_2"));
1314 char *fname3
= tor_strdup(get_fname("mapped_3"));
1315 const size_t buflen
= 17000;
1316 char *buf
= tor_malloc(17000);
1317 tor_mmap_t
*mapping
;
1319 crypto_rand(buf
, buflen
);
1321 write_str_to_file(fname1
, "Short file.", 1);
1322 write_bytes_to_file(fname2
, buf
, buflen
, 1);
1323 write_bytes_to_file(fname3
, buf
, 16384, 1);
1325 mapping
= tor_mmap_file(fname1
);
1326 test_assert(mapping
);
1327 test_eq(mapping
->size
, strlen("Short file."));
1328 test_streq(mapping
->data
, "Short file.");
1330 tor_munmap_file(mapping
);
1331 test_assert(unlink(fname1
) == 0);
1333 /* make sure we can unlink. */
1334 test_assert(unlink(fname1
) == 0);
1335 test_streq(mapping
->data
, "Short file.");
1336 tor_munmap_file(mapping
);
1339 /* Make sure that we fail to map a no-longer-existent file. */
1340 mapping
= tor_mmap_file(fname1
);
1341 test_assert(mapping
== NULL
);
1343 /* Now try a big file that stretches across a few pages and isn't aligned */
1344 mapping
= tor_mmap_file(fname2
);
1345 test_assert(mapping
);
1346 test_eq(mapping
->size
, buflen
);
1347 test_memeq(mapping
->data
, buf
, buflen
);
1348 tor_munmap_file(mapping
);
1350 /* Now try a big aligned file. */
1351 mapping
= tor_mmap_file(fname3
);
1352 test_assert(mapping
);
1353 test_eq(mapping
->size
, 16384);
1354 test_memeq(mapping
->data
, buf
, 16384);
1355 tor_munmap_file(mapping
);
1357 /* fname1 got unlinked above */
1368 test_control_formats(void)
1372 "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
1375 sz
= read_escaped_data(inp
, strlen(inp
), 1, &out
);
1377 ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
1378 test_eq(sz
, strlen(out
));
1384 test_onion_handshake(void)
1387 crypto_dh_env_t
*c_dh
= NULL
;
1388 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
1392 char s_buf
[ONIONSKIN_REPLY_LEN
];
1396 crypto_pk_env_t
*pk
= NULL
;
1398 pk
= pk_generate(0);
1400 /* client handshake 1. */
1401 memset(c_buf
, 0, ONIONSKIN_CHALLENGE_LEN
);
1402 test_assert(! onion_skin_create(pk
, &c_dh
, c_buf
));
1404 /* server handshake */
1405 memset(s_buf
, 0, ONIONSKIN_REPLY_LEN
);
1406 memset(s_keys
, 0, 40);
1407 test_assert(! onion_skin_server_handshake(c_buf
, pk
, NULL
,
1408 s_buf
, s_keys
, 40));
1410 /* client handshake 2 */
1411 memset(c_keys
, 0, 40);
1412 test_assert(! onion_skin_client_handshake(c_dh
, s_buf
, c_keys
, 40));
1414 crypto_dh_free(c_dh
);
1416 if (memcmp(c_keys
, s_keys
, 40)) {
1420 test_memeq(c_keys
, s_keys
, 40);
1421 memset(s_buf
, 0, 40);
1422 test_memneq(c_keys
, s_buf
, 40);
1423 crypto_free_pk_env(pk
);
1426 extern smartlist_t
*fingerprint_list
;
1429 test_dir_format(void)
1431 char buf
[8192], buf2
[8192];
1433 char fingerprint
[FINGERPRINT_LEN
+1];
1434 char *pk1_str
= NULL
, *pk2_str
= NULL
, *pk3_str
= NULL
, *cp
;
1435 size_t pk1_str_len
, pk2_str_len
, pk3_str_len
;
1436 routerinfo_t r1
, r2
;
1437 crypto_pk_env_t
*pk1
= NULL
, *pk2
= NULL
, *pk3
= NULL
;
1438 routerinfo_t
*rp1
= NULL
, *rp2
= NULL
;
1439 addr_policy_t ex1
, ex2
;
1440 routerlist_t
*dir1
= NULL
, *dir2
= NULL
;
1442 char *bw_lines
= NULL
;
1444 pk1
= pk_generate(0);
1445 pk2
= pk_generate(1);
1446 pk3
= pk_generate(2);
1448 test_assert( is_legal_nickname("a"));
1449 test_assert(!is_legal_nickname(""));
1450 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
1451 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
1452 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
1453 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1455 test_assert( is_legal_nickname_or_hexdigest(
1456 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1457 test_assert( is_legal_nickname_or_hexdigest(
1458 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
1459 test_assert( is_legal_nickname_or_hexdigest(
1460 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
1462 test_assert(!is_legal_nickname_or_hexdigest(
1463 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1465 test_assert(!is_legal_nickname_or_hexdigest(
1466 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1467 /* hex part too long */
1468 test_assert(!is_legal_nickname_or_hexdigest(
1469 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1470 test_assert(!is_legal_nickname_or_hexdigest(
1471 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
1473 test_assert(!is_legal_nickname_or_hexdigest(
1474 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
1475 test_assert(!is_legal_nickname_or_hexdigest(
1476 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
1477 test_assert(!is_legal_nickname_or_hexdigest(
1478 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
1479 test_assert(!is_legal_nickname_or_hexdigest(
1480 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
1481 "abcdefghijklmnoppqrst"));
1482 /* Bad extra char. */
1483 test_assert(!is_legal_nickname_or_hexdigest(
1484 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
1485 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
1486 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
1487 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
1489 get_platform_str(platform
, sizeof(platform
));
1490 memset(&r1
,0,sizeof(r1
));
1491 memset(&r2
,0,sizeof(r2
));
1492 r1
.address
= tor_strdup("18.244.0.1");
1493 r1
.addr
= 0xc0a80001u
; /* 192.168.0.1 */
1494 r1
.cache_info
.published_on
= 0;
1497 r1
.onion_pkey
= pk1
;
1498 r1
.identity_pkey
= pk2
;
1499 r1
.bandwidthrate
= 1000;
1500 r1
.bandwidthburst
= 5000;
1501 r1
.bandwidthcapacity
= 10000;
1502 r1
.exit_policy
= NULL
;
1503 r1
.nickname
= tor_strdup("Magri");
1504 r1
.platform
= tor_strdup(platform
);
1506 ex1
.policy_type
= ADDR_POLICY_ACCEPT
;
1510 ex1
.prt_min
= ex1
.prt_max
= 80;
1512 ex2
.policy_type
= ADDR_POLICY_REJECT
;
1513 ex2
.addr
= 18 << 24;
1514 ex2
.msk
= 0xFF000000u
;
1515 ex2
.prt_min
= ex2
.prt_max
= 24;
1517 r2
.address
= tor_strdup("1.1.1.1");
1518 r2
.addr
= 0x0a030201u
; /* 10.3.2.1 */
1519 r2
.platform
= tor_strdup(platform
);
1520 r2
.cache_info
.published_on
= 5;
1523 r2
.onion_pkey
= pk2
;
1524 r2
.identity_pkey
= pk1
;
1525 r2
.bandwidthrate
= r2
.bandwidthburst
= r2
.bandwidthcapacity
= 3000;
1526 r2
.exit_policy
= &ex1
;
1527 r2
.nickname
= tor_strdup("Fred");
1529 bw_lines
= rep_hist_get_bandwidth_lines();
1530 test_assert(bw_lines
);
1531 test_assert(!strcmpstart(bw_lines
, "opt write-history "));
1533 test_assert(!crypto_pk_write_public_key_to_string(pk1
, &pk1_str
,
1535 test_assert(!crypto_pk_write_public_key_to_string(pk2
, &pk2_str
,
1537 test_assert(!crypto_pk_write_public_key_to_string(pk3
, &pk3_str
,
1540 memset(buf
, 0, 2048);
1541 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1543 strlcpy(buf2
, "router Magri 18.244.0.1 9000 0 0\n"
1544 "platform Tor "VERSION
" on ", sizeof(buf2
));
1545 strlcat(buf2
, get_uname(), sizeof(buf2
));
1547 "published 1970-01-01 00:00:00\n"
1548 "opt fingerprint ", sizeof(buf2
));
1549 test_assert(!crypto_pk_get_fingerprint(pk2
, fingerprint
, 1));
1550 strlcat(buf2
, fingerprint
, sizeof(buf2
));
1551 strlcat(buf2
, "\nuptime 0\n"
1552 /* XXX the "0" above is hardcoded, but even if we made it reflect
1553 * uptime, that still wouldn't make it right, because the two
1554 * descriptors might be made on different seconds... hm. */
1555 "bandwidth 1000 5000 10000\n"
1556 "onion-key\n", sizeof(buf2
));
1557 strlcat(buf2
, pk1_str
, sizeof(buf2
));
1558 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
1559 strlcat(buf2
, pk2_str
, sizeof(buf2
));
1560 #ifndef USE_EVENTDNS
1561 strlcat(buf2
, "opt eventdns 0\n", sizeof(buf2
));
1563 strlcat(buf2
, bw_lines
, sizeof(buf2
));
1564 strlcat(buf2
, "router-signature\n", sizeof(buf2
));
1565 buf
[strlen(buf2
)] = '\0'; /* Don't compare the sig; it's never the same
1568 test_streq(buf
, buf2
);
1571 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1573 rp1
= router_parse_entry_from_string((const char*)cp
,NULL
,1);
1575 test_streq(rp1
->address
, r1
.address
);
1576 test_eq(rp1
->or_port
, r1
.or_port
);
1577 //test_eq(rp1->dir_port, r1.dir_port);
1578 test_eq(rp1
->bandwidthrate
, r1
.bandwidthrate
);
1579 test_eq(rp1
->bandwidthburst
, r1
.bandwidthburst
);
1580 test_eq(rp1
->bandwidthcapacity
, r1
.bandwidthcapacity
);
1581 test_assert(crypto_pk_cmp_keys(rp1
->onion_pkey
, pk1
) == 0);
1582 test_assert(crypto_pk_cmp_keys(rp1
->identity_pkey
, pk2
) == 0);
1583 test_assert(rp1
->exit_policy
== NULL
);
1586 /* XXX Once we have exit policies, test this again. XXX */
1587 strlcpy(buf2
, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2
));
1588 strlcat(buf2
, pk2_str
, sizeof(buf2
));
1589 strlcat(buf2
, "signing-key\n", sizeof(buf2
));
1590 strlcat(buf2
, pk1_str
, sizeof(buf2
));
1591 strlcat(buf2
, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2
));
1592 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk2
)>0);
1593 test_streq(buf
, buf2
);
1596 rp2
= router_parse_entry_from_string(&cp
,1);
1598 test_streq(rp2
->address
, r2
.address
);
1599 test_eq(rp2
->or_port
, r2
.or_port
);
1600 test_eq(rp2
->dir_port
, r2
.dir_port
);
1601 test_eq(rp2
->bandwidth
, r2
.bandwidth
);
1602 test_assert(crypto_pk_cmp_keys(rp2
->onion_pkey
, pk2
) == 0);
1603 test_assert(crypto_pk_cmp_keys(rp2
->identity_pkey
, pk1
) == 0);
1604 test_eq(rp2
->exit_policy
->policy_type
, EXIT_POLICY_ACCEPT
);
1605 test_streq(rp2
->exit_policy
->string
, "accept *:80");
1606 test_streq(rp2
->exit_policy
->address
, "*");
1607 test_streq(rp2
->exit_policy
->port
, "80");
1608 test_eq(rp2
->exit_policy
->next
->policy_type
, EXIT_POLICY_REJECT
);
1609 test_streq(rp2
->exit_policy
->next
->string
, "reject 18.*:24");
1610 test_streq(rp2
->exit_policy
->next
->address
, "18.*");
1611 test_streq(rp2
->exit_policy
->next
->port
, "24");
1612 test_assert(rp2
->exit_policy
->next
->next
== NULL
);
1614 /* Okay, now for the directories. */
1616 fingerprint_list
= smartlist_create();
1617 crypto_pk_get_fingerprint(pk2
, buf
, 1);
1618 add_fingerprint_to_dir("Magri", buf
, fingerprint_list
);
1619 crypto_pk_get_fingerprint(pk1
, buf
, 1);
1620 add_fingerprint_to_dir("Fred", buf
, fingerprint_list
);
1626 /* XXXX NM re-enable. */
1627 /* Make sure routers aren't too far in the past any more. */
1628 r1
.cache_info
.published_on
= time(NULL
);
1629 r2
.cache_info
.published_on
= time(NULL
)-3*60*60;
1630 test_assert(router_dump_router_to_string(buf
, 2048, &r1
, pk2
)>0);
1631 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
1632 test_assert(router_dump_router_to_string(buf
, 2048, &r2
, pk1
)>0);
1633 test_eq(dirserv_add_descriptor(buf
,&m
), 2);
1634 get_options()->Nickname
= tor_strdup("DirServer");
1635 test_assert(!dirserv_dump_directory_to_string(&cp
,pk3
, 0));
1636 crypto_pk_get_digest(pk3
, d
);
1637 test_assert(!router_parse_directory(cp
));
1638 test_eq(2, smartlist_len(dir1
->routers
));
1642 dirserv_free_fingerprint_list();
1646 if (pk1
) crypto_free_pk_env(pk1
);
1647 if (pk2
) crypto_free_pk_env(pk2
);
1648 if (rp1
) routerinfo_free(rp1
);
1649 if (rp2
) routerinfo_free(rp2
);
1650 tor_free(dir1
); /* XXXX And more !*/
1651 tor_free(dir2
); /* And more !*/
1653 /* Try out version parsing functionality */
1654 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1
));
1655 test_eq(0, ver1
.major
);
1656 test_eq(3, ver1
.minor
);
1657 test_eq(4, ver1
.micro
);
1658 test_eq(VER_PRE
, ver1
.status
);
1659 test_eq(2, ver1
.patchlevel
);
1660 test_eq(0, tor_version_parse("0.3.4rc1", &ver1
));
1661 test_eq(0, ver1
.major
);
1662 test_eq(3, ver1
.minor
);
1663 test_eq(4, ver1
.micro
);
1664 test_eq(VER_RC
, ver1
.status
);
1665 test_eq(1, ver1
.patchlevel
);
1666 test_eq(0, tor_version_parse("1.3.4", &ver1
));
1667 test_eq(1, ver1
.major
);
1668 test_eq(3, ver1
.minor
);
1669 test_eq(4, ver1
.micro
);
1670 test_eq(VER_RELEASE
, ver1
.status
);
1671 test_eq(0, ver1
.patchlevel
);
1672 test_eq(0, tor_version_parse("1.3.4.999", &ver1
));
1673 test_eq(1, ver1
.major
);
1674 test_eq(3, ver1
.minor
);
1675 test_eq(4, ver1
.micro
);
1676 test_eq(VER_RELEASE
, ver1
.status
);
1677 test_eq(999, ver1
.patchlevel
);
1678 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1
));
1679 test_eq(0, ver1
.major
);
1680 test_eq(1, ver1
.minor
);
1681 test_eq(2, ver1
.micro
);
1682 test_eq(4, ver1
.patchlevel
);
1683 test_eq(VER_RELEASE
, ver1
.status
);
1684 test_streq("alpha", ver1
.status_tag
);
1685 test_eq(0, tor_version_parse("0.1.2.4", &ver1
));
1686 test_eq(0, ver1
.major
);
1687 test_eq(1, ver1
.minor
);
1688 test_eq(2, ver1
.micro
);
1689 test_eq(4, ver1
.patchlevel
);
1690 test_eq(VER_RELEASE
, ver1
.status
);
1691 test_streq("", ver1
.status_tag
);
1693 #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
1694 #define test_v_i_o(val, ver, lst) \
1695 test_eq_vs(val, tor_version_is_obsolete(ver, lst))
1697 /* make sure tor_version_is_obsolete() works */
1698 test_v_i_o(VS_OLD
, "0.0.1", "Tor 0.0.2");
1699 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2, Tor 0.0.3");
1700 test_v_i_o(VS_OLD
, "0.0.1", "0.0.2,Tor 0.0.3");
1701 test_v_i_o(VS_OLD
, "0.0.1","0.0.3,BetterTor 0.0.1");
1702 test_v_i_o(VS_RECOMMENDED
, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
1703 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
1704 test_v_i_o(VS_OLD
, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
1705 test_v_i_o(VS_NEW
, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
1706 test_v_i_o(VS_RECOMMENDED
, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
1707 test_v_i_o(VS_OLD
, "0.0.5.0", "0.0.5.1-cvs");
1708 test_v_i_o(VS_NEW_IN_SERIES
, "0.0.5.1-cvs", "0.0.5, 0.0.6");
1709 /* Not on list, but newer than any in same series. */
1710 test_v_i_o(VS_NEW_IN_SERIES
, "0.1.0.3",
1711 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1712 /* Series newer than any on list. */
1713 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");
1714 /* Series older than any on list. */
1715 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");
1716 /* Not on list, not newer than any on same series. */
1717 test_v_i_o(VS_UNRECOMMENDED
, "0.1.0.1",
1718 "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1719 /* On list, not newer than any on same series. */
1720 test_v_i_o(VS_UNRECOMMENDED
,
1721 "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
1722 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1723 test_eq(1, tor_version_as_new_as(
1724 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1725 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
1727 test_eq(0, tor_version_as_new_as(
1728 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
1729 "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1736 addr_policy_t
*policy
, *policy2
;
1739 policy
= router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
1740 test_eq(NULL
, policy
->next
);
1741 test_eq(ADDR_POLICY_REJECT
, policy
->policy_type
);
1742 test_eq(0xc0a80000u
, policy
->addr
);
1743 test_eq(0xffff0000u
, policy
->msk
);
1744 test_eq(1, policy
->prt_min
);
1745 test_eq(65535, policy
->prt_max
);
1746 test_streq("reject 192.168.0.0/16:*", policy
->string
);
1748 test_assert(ADDR_POLICY_ACCEPTED
==
1749 compare_addr_to_addr_policy(0x01020304u
, 2, policy
));
1750 test_assert(ADDR_POLICY_PROBABLY_ACCEPTED
==
1751 compare_addr_to_addr_policy(0, 2, policy
));
1752 test_assert(ADDR_POLICY_REJECTED
==
1753 compare_addr_to_addr_policy(0xc0a80102, 2, policy
));
1756 test_assert(0 == policies_parse_exit_policy(NULL
, &policy2
, 1));
1757 test_assert(policy2
);
1759 test_assert(!exit_policy_is_general_exit(policy
));
1760 test_assert(exit_policy_is_general_exit(policy2
));
1762 test_assert(cmp_addr_policies(policy
, policy2
));
1763 test_assert(!cmp_addr_policies(policy2
, policy2
));
1765 test_assert(!policy_is_reject_star(policy2
));
1766 test_assert(policy_is_reject_star(policy
));
1768 addr_policy_free(policy
);
1769 addr_policy_free(policy2
);
1771 /* make sure compacting logic works. */
1773 line
.key
= (char*)"foo";
1774 line
.value
= (char*)"accept *:80,reject private:*,reject *:*";
1776 test_assert(0 == policies_parse_exit_policy(&line
, &policy
, 0));
1777 test_assert(policy
);
1778 test_streq(policy
->string
, "accept *:80");
1779 test_streq(policy
->next
->string
, "reject *:*");
1780 test_eq_ptr(policy
->next
->next
, NULL
);
1782 addr_policy_free(policy
);
1788 char address1
[] = "fooaddress.onion";
1789 char address2
[] = "aaaaaaaaaaaaaaaa.onion";
1790 char address3
[] = "fooaddress.exit";
1791 char address4
[] = "tor.eff.org";
1792 rend_service_descriptor_t
*d1
, *d2
;
1795 crypto_pk_env_t
*pk1
, *pk2
;
1797 pk1
= pk_generate(0);
1798 pk2
= pk_generate(1);
1800 /* Test unversioned descriptor */
1801 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
1802 d1
->pk
= crypto_pk_dup_key(pk1
);
1804 d1
->timestamp
= now
;
1805 d1
->n_intro_points
= 3;
1807 d1
->intro_points
= tor_malloc(sizeof(char*)*3);
1808 d1
->intro_points
[0] = tor_strdup("tom");
1809 d1
->intro_points
[1] = tor_strdup("crow");
1810 d1
->intro_points
[2] = tor_strdup("joel");
1811 test_assert(! rend_encode_service_descriptor(d1
, 0, pk1
, &encoded
, &len
));
1812 d2
= rend_parse_service_descriptor(encoded
, len
);
1815 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
1816 test_eq(d2
->timestamp
, now
);
1817 test_eq(d2
->version
, 0);
1818 test_eq(d2
->protocols
, 4);
1819 test_eq(d2
->n_intro_points
, 3);
1820 test_streq(d2
->intro_points
[0], "tom");
1821 test_streq(d2
->intro_points
[1], "crow");
1822 test_streq(d2
->intro_points
[2], "joel");
1823 test_eq(NULL
, d2
->intro_point_extend_info
);
1825 rend_service_descriptor_free(d1
);
1826 rend_service_descriptor_free(d2
);
1829 /* Test versioned descriptor. */
1830 d1
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
1831 d1
->pk
= crypto_pk_dup_key(pk1
);
1833 d1
->timestamp
= now
;
1834 d1
->n_intro_points
= 2;
1837 d1
->intro_points
= tor_malloc(sizeof(char*)*2);
1838 d1
->intro_point_extend_info
= tor_malloc(sizeof(extend_info_t
*)*2);
1839 d1
->intro_points
[0] = tor_strdup("tom");
1840 d1
->intro_points
[1] = tor_strdup("crow");
1841 d1
->intro_point_extend_info
[0] = tor_malloc_zero(sizeof(extend_info_t
));
1842 strlcpy(d1
->intro_point_extend_info
[0]->nickname
, "tom", 4);
1843 d1
->intro_point_extend_info
[0]->addr
= 1234;
1844 d1
->intro_point_extend_info
[0]->port
= 4567;
1845 d1
->intro_point_extend_info
[0]->onion_key
= crypto_pk_dup_key(pk1
);
1846 memset(d1
->intro_point_extend_info
[0]->identity_digest
, 'a', DIGEST_LEN
);
1848 d1
->intro_point_extend_info
[1] = tor_malloc_zero(sizeof(extend_info_t
));
1849 strlcpy(d1
->intro_point_extend_info
[1]->nickname
, "crow", 5);
1850 d1
->intro_point_extend_info
[1]->addr
= 6060842;
1851 d1
->intro_point_extend_info
[1]->port
= 8000;
1852 d1
->intro_point_extend_info
[1]->onion_key
= crypto_pk_dup_key(pk2
);
1853 memset(d1
->intro_point_extend_info
[1]->identity_digest
, 'b', DIGEST_LEN
);
1855 test_assert(! rend_encode_service_descriptor(d1
, 1, pk1
, &encoded
, &len
));
1856 d2
= rend_parse_service_descriptor(encoded
, len
);
1859 test_assert(!crypto_pk_cmp_keys(d1
->pk
, d2
->pk
));
1860 test_eq(d2
->timestamp
, now
);
1861 test_eq(d2
->version
, 1);
1862 test_eq(d2
->protocols
, 60);
1863 test_eq(d2
->n_intro_points
, 2);
1864 test_streq(d2
->intro_points
[0], d2
->intro_point_extend_info
[0]->nickname
);
1865 test_streq(d2
->intro_points
[1], d2
->intro_point_extend_info
[1]->nickname
);
1866 test_eq(d2
->intro_point_extend_info
[0]->addr
, 1234);
1867 test_eq(d2
->intro_point_extend_info
[0]->port
, 4567);
1868 test_assert(!crypto_pk_cmp_keys(pk1
,
1869 d2
->intro_point_extend_info
[0]->onion_key
));
1870 test_memeq(d2
->intro_point_extend_info
[0]->identity_digest
,
1871 d1
->intro_point_extend_info
[0]->identity_digest
, DIGEST_LEN
);
1872 test_eq(d2
->intro_point_extend_info
[1]->addr
, 6060842);
1873 test_eq(d2
->intro_point_extend_info
[1]->port
, 8000);
1875 test_memeq(d2
->intro_point_extend_info
[1]->identity_digest
,
1876 d1
->intro_point_extend_info
[1]->identity_digest
, DIGEST_LEN
);
1878 test_assert(BAD_HOSTNAME
== parse_extended_hostname(address1
));
1879 test_assert(ONION_HOSTNAME
== parse_extended_hostname(address2
));
1880 test_assert(EXIT_HOSTNAME
== parse_extended_hostname(address3
));
1881 test_assert(NORMAL_HOSTNAME
== parse_extended_hostname(address4
));
1883 rend_service_descriptor_free(d1
);
1884 rend_service_descriptor_free(d2
);
1885 crypto_free_pk_env(pk1
);
1886 crypto_free_pk_env(pk2
);
1894 crypto_cipher_env_t
*c
;
1895 struct timeval start
, end
;
1896 const int iters
= 100000;
1898 c
= crypto_new_cipher_env();
1899 crypto_cipher_generate_key(c
);
1900 crypto_cipher_encrypt_init_cipher(c
);
1901 for (len
= 1; len
<= 8192; len
*= 2) {
1902 b1
= tor_malloc_zero(len
);
1903 b2
= tor_malloc_zero(len
);
1904 tor_gettimeofday(&start
);
1905 for (i
= 0; i
< iters
; ++i
) {
1906 crypto_cipher_encrypt(c
, b1
, b2
, len
);
1908 tor_gettimeofday(&end
);
1911 nsec
= (uint64_t) tv_udiff(&start
,&end
);
1913 nsec
/= (iters
*len
);
1914 printf("%d bytes: "U64_FORMAT
" nsec per byte\n", len
,
1915 U64_PRINTF_ARG(nsec
));
1917 crypto_free_cipher_env(c
);
1921 main(int c
, char**v
)
1923 or_options_t
*options
= options_new();
1924 char *errmsg
= NULL
;
1927 options
->command
= CMD_RUN_UNITTESTS
;
1931 options_init(options
);
1932 options
->DataDirectory
= tor_strdup(temp_dir
);
1933 if (set_options(options
, &errmsg
) < 0) {
1934 printf("Failed to set initial options: %s\n", errmsg
);
1946 atexit(remove_directory
);
1948 printf("Running Tor unit tests on %s\n", get_uname());
1950 puts("========================== Buffers =========================");
1952 puts("\n========================== Crypto ==========================");
1953 // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
1957 puts("\n========================= Util ============================");
1962 test_control_formats();
1965 puts("\n========================= Onion Skins =====================");
1966 test_onion_handshake();
1967 puts("\n========================= Directory Formats ===============");
1969 puts("\n========================= Policies ===================");
1971 puts("\n========================= Rendezvous functionality ========");