r15841@catbus: nickm | 2007-10-16 09:49:19 -0400
[tor.git] / src / or / test.c
bloba5c00b753acd200e74952ea23e07fda40ecd7e6d
1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
5 const char test_c_id[] =
6 "$Id$";
8 /**
9 * \file test.c
10 * \brief Unit tests for many pieces of the lower level Tor modules.
11 **/
13 #include "orconfig.h"
14 #include <stdio.h>
15 #ifdef HAVE_FCNTL_H
16 #include <fcntl.h>
17 #endif
19 #ifdef MS_WINDOWS
20 /* For mkdir() */
21 #include <direct.h>
22 #else
23 #include <dirent.h>
24 #endif
26 #include "or.h"
27 #include "../common/test.h"
28 #include "../common/torgzip.h"
30 int have_failed = 0;
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,
35 char **out);
36 or_options_t *options_new(void);
37 int parse_addr_policy(config_line_t *cfg, addr_policy_t **dest,
38 int assume_action);
40 static char temp_dir[256];
42 static void
43 setup_directory(void)
45 static int is_setup = 0;
46 int r;
47 if (is_setup) return;
49 #ifdef MS_WINDOWS
50 // XXXX
51 tor_snprintf(temp_dir, sizeof(temp_dir),
52 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
53 r = mkdir(temp_dir);
54 #else
55 tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
56 r = mkdir(temp_dir, 0700);
57 #endif
58 if (r) {
59 fprintf(stderr, "Can't create directory %s:", temp_dir);
60 perror("");
61 exit(1);
63 is_setup = 1;
66 static const char *
67 get_fname(const char *name)
69 static char buf[1024];
70 setup_directory();
71 tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
72 return buf;
75 static void
76 remove_directory(void)
78 smartlist_t *elements = tor_listdir(temp_dir);
79 if (elements) {
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);
85 unlink(tmp);
86 tor_free(tmp);
87 });
88 SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
89 smartlist_free(elements);
91 rmdir(temp_dir);
94 static crypto_pk_env_t *
95 pk_generate(int idx)
97 static crypto_pk_env_t *pregen[3] = {NULL, NULL, NULL};
98 tor_assert(idx < (int)(sizeof(pregen)/sizeof(pregen[0])));
99 if (! pregen[idx]) {
100 pregen[idx] = crypto_new_pk_env();
101 tor_assert(!crypto_pk_generate_key(pregen[idx]));
103 return crypto_pk_dup_key(pregen[idx]);
106 static void
107 test_buffers(void)
109 char str[256];
110 char str2[256];
112 buf_t *buf;
114 int j;
116 /****
117 * buf_new
118 ****/
119 if (!(buf = buf_new()))
120 test_fail();
122 test_eq(buf_capacity(buf), 4096);
123 test_eq(buf_datalen(buf), 0);
125 /****
126 * General pointer frobbing
128 for (j=0;j<256;++j) {
129 str[j] = (char)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. */
146 for (j=0;j<15;++j) {
147 write_to_buf(str, 256, buf);
149 assert_buf_ok(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);
154 for (j=0;j<15;++j) {
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);
160 buf_free(buf);
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);
170 assert_buf_ok(buf);
171 write_to_buf(str, 32, buf);
172 test_eq(buf_capacity(buf), 256);
173 assert_buf_ok(buf);
174 write_to_buf(str, 256, buf);
175 assert_buf_ok(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. */
188 buf_free(buf);
189 buf = buf_new_with_capacity(33668);
190 for (j=0;j<67;++j) {
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. */
201 buf_free(buf);
202 buf = buf_new_with_capacity(33668);
203 for (j=0;j<67;++j) {
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);
210 for (j=0;j<80;++j) {
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);
219 #if 0
221 int s;
222 int eof;
223 int i;
224 buf_t *buf2;
225 /****
226 * read_to_buf
227 ****/
228 s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
229 write(s, str, 256);
230 close(s);
232 s = open(get_fname("data"), O_RDONLY, 0);
233 eof = 0;
234 errno = 0; /* XXXX */
235 i = read_to_buf(s, 10, buf, &eof);
236 printf("%s\n", strerror(errno));
237 test_eq(i, 10);
238 test_eq(eof, 0);
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);
248 test_eq(eof, 0);
249 test_eq(i, 0);
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);
256 test_eq(eof, 0);
257 test_eq(i, 6);
258 test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
259 buf_free(buf2);
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);
266 test_eq(eof, 0);
267 test_eq(i, 32);
268 buf_free(buf2);
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. */
277 test_eq(eof, 0);
279 i = read_to_buf(s, 1024, buf, &eof);
280 test_eq(i, 0);
281 test_eq(buf_capacity(buf), MAX_BUF_SIZE);
282 test_eq(buf_datalen(buf), 256-6-32);
283 test_eq(eof, 1);
285 #endif
287 buf_free(buf);
290 static void
291 test_crypto_dh(void)
293 crypto_dh_env_t *dh1, *dh2;
294 char p1[DH_BYTES];
295 char p2[DH_BYTES];
296 char s1[DH_BYTES];
297 char s2[DH_BYTES];
298 int s1len, s2len;
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);
321 crypto_dh_free(dh1);
322 crypto_dh_free(dh2);
325 static void
326 test_crypto(void)
328 crypto_cipher_env_t *env1, *env2;
329 crypto_pk_env_t *pk1, *pk2;
330 char *data1, *data2, *data3, *cp;
331 int i, j, p, len;
332 size_t size;
334 data1 = tor_malloc(1024);
335 data2 = tor_malloc(1024);
336 data3 = tor_malloc(1024);
337 test_assert(data1 && data2 && data3);
339 /* Try out RNG. */
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. */
346 data1[0]='\0';
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();
353 test_neq(env1, 0);
354 env2 = crypto_new_cipher_env();
355 test_neq(env2, 0);
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
384 the same results. */
385 crypto_free_cipher_env(env2);
387 memset(data3, 0, 1024);
388 env2 = crypto_new_cipher_env();
389 test_neq(env2, 0);
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);
409 test_memeq(data1,
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));
420 tor_free(cp);
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*/
467 /* Try encoding */
468 crypto_free_pk_env(pk2);
469 pk2 = NULL;
470 i = crypto_pk_asn1_encode(pk1, data1, 1024);
471 test_assert(i>0);
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)
482 continue;
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);
486 test_assert(len>=0);
487 len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
488 test_eq(len,j);
489 test_memeq(data1,data3,j);
492 crypto_free_pk_env(pk1);
493 crypto_free_pk_env(pk2);
495 /* Base64 tests */
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);
502 test_eq(j, 71);
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]);
515 /* Base32 tests */
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");
528 /* Base16 tests */
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);
535 test_eq(i,0);
536 test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
538 tor_free(data1);
539 tor_free(data2);
540 tor_free(data3);
543 static void
544 test_crypto_s2k(void)
546 char buf[29];
547 char buf2[29];
548 char *buf3;
549 int i;
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);
562 buf[8] = 96;
563 buf2[8] = 96;
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);
572 static int
573 _compare_strs(const void **a, const void **b)
575 const char *s1 = *a, *s2 = *b;
576 return strcmp(s1, s2);
579 static int
580 _compare_without_first_ch(const void *a, const void **b)
582 const char *s1 = a, *s2 = *b;
583 return strcasecmp(s1+1, s2);
586 static void
587 test_util(void)
589 struct timeval start, end;
590 struct tm a_time;
591 char timestr[RFC1123_TIME_LEN+1];
592 char buf[1024];
593 time_t t_res;
594 int i;
595 uint32_t u32;
596 uint16_t u16;
597 char *cp, *k, *v;
599 start.tv_sec = 5;
600 start.tv_usec = 5000;
602 end.tv_sec = 5;
603 end.tv_usec = 5000;
605 test_eq(0L, tv_udiff(&start, &end));
607 end.tv_usec = 7000;
609 test_eq(2000L, tv_udiff(&start, &end));
611 end.tv_sec = 6;
613 test_eq(1002000L, tv_udiff(&start, &end));
615 end.tv_usec = 0;
617 test_eq(995000L, tv_udiff(&start, &end));
619 end.tv_sec = 4;
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;
626 a_time.tm_mon = 7;
627 a_time.tm_mday = 30;
628 a_time.tm_hour = 6;
629 a_time.tm_min = 14;
630 a_time.tm_sec = 55;
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. */
635 a_time.tm_mday = 10;
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);
643 t_res = 0;
644 i = parse_rfc1123_time(timestr, &t_res);
645 test_eq(i,0);
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);
665 test_eq(u16, 0);
666 tor_free(cp);
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);
670 test_eq(u16, 99);
671 tor_free(cp);
672 test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
673 &cp, NULL, &u16));
674 test_streq(cp, "nonexistent.address");
675 test_eq(u16, 4040);
676 tor_free(cp);
677 test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
678 test_streq(cp, "localhost");
679 test_eq(u32, 0x7f000001u);
680 test_eq(u16, 9999);
681 tor_free(cp);
682 u32 = 3;
683 test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
684 test_eq(cp, NULL);
685 test_eq(u32, 0x7f000001u);
686 test_eq(u16, 0);
687 tor_free(cp);
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));
700 test_assert(i == 1);
701 test_streq(cp, " x");
702 test_assert(U64_LITERAL(12345678901) ==
703 tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
704 test_assert(i == 1);
705 test_streq(cp, "");
706 test_assert(U64_LITERAL(0) ==
707 tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
708 test_assert(i == 0);
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"
717 "k2\n"
718 "k3 \n" "\n" " \n" "#comment\n"
719 "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
720 cp = buf;
722 cp = parse_line_from_str(cp, &k, &v);
723 test_streq(k, "k");
724 test_streq(v, "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);
738 test_streq(k, "k2");
739 test_streq(v, "");
740 test_assert(!strcmpstart(cp, "k3 \n"));
742 cp = parse_line_from_str(cp, &k, &v);
743 test_streq(k, "k3");
744 test_streq(v, "");
745 test_assert(!strcmpstart(cp, "\n \n"));
747 cp = parse_line_from_str(cp, &k, &v);
748 test_streq(k, "k4");
749 test_streq(v, "");
750 test_assert(!strcmpstart(cp, "k5#abc"));
752 cp = parse_line_from_str(cp, &k, &v);
753 test_streq(k, "k5");
754 test_streq(v, "");
755 test_assert(!strcmpstart(cp, "k6"));
757 cp = parse_line_from_str(cp, &k, &v);
758 test_streq(k, "k6");
759 test_streq(v, "val");
760 test_streq(cp, "");
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];
777 struct in_addr in;
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");
783 /* Test 'escaped' */
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.",
793 10, "", "");
794 cp = smartlist_join_strings(sl, "", 0, NULL);
795 test_streq(cp,
796 "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
797 tor_free(cp);
798 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
799 smartlist_clear(sl);
801 wrap_string(sl, "This is a test of string wrapping functionality: woot.",
802 16, "### ", "# ");
803 cp = smartlist_join_strings(sl, "", 0, NULL);
804 test_streq(cp,
805 "### This is a\n# test of string\n# wrapping\n# functionality:\n"
806 "# woot.\n");
808 tor_free(cp);
809 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
810 smartlist_clear(sl);
814 static void
815 test_smartlist(void)
817 smartlist_t *sl;
818 char *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));
845 /* test isin. */
846 test_assert(smartlist_isin(sl, (void*)3));
847 test_assert(!smartlist_isin(sl, (void*)99));
849 /* Test split and join */
850 smartlist_clear(sl);
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");
862 tor_free(cp);
863 cp = smartlist_join_strings(sl, "!", 0, NULL);
864 test_streq(cp, "abc!a!bc!");
865 tor_free(cp);
866 cp = smartlist_join_strings(sl, "XY", 0, NULL);
867 test_streq(cp, "abcXYaXYbcXY");
868 tor_free(cp);
869 cp = smartlist_join_strings(sl, "XY", 1, NULL);
870 test_streq(cp, "abcXYaXYbcXYXY");
871 tor_free(cp);
872 cp = smartlist_join_strings(sl, "", 1, NULL);
873 test_streq(cp, "abcabc");
874 tor_free(cp);
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));
883 smartlist_clear(sl);
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));
900 smartlist_clear(sl);
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));
916 smartlist_clear(sl);
918 cp = smartlist_join_strings(sl, "XY", 0, NULL);
919 test_streq(cp, "");
920 tor_free(cp);
921 cp = smartlist_join_strings(sl, "XY", 1, NULL);
922 test_streq(cp, "XY");
923 tor_free(cp);
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));
937 smartlist_clear(sl);
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));
949 smartlist_clear(sl);
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");
957 tor_free(cp);
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");
968 tor_free(cp);
969 cp = smartlist_pop_last(sl);
970 test_streq(cp, "and");
971 tor_free(cp);
972 test_eq(smartlist_len(sl), 6);
973 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
974 smartlist_clear(sl);
976 /* Test uniq() */
977 smartlist_split_string(sl,
978 "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
979 ",", 0, 0);
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");
984 tor_free(cp);
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));
992 smartlist_clear(sl);
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",
997 " ", 0, 0);
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");
1004 tor_free(cp);
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");
1008 tor_free(cp);
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();
1018 int i;
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);
1024 /* add_all */
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);
1034 /* overlap */
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));
1040 /* intersect */
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));
1048 /* subtract */
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);
1061 smartlist_free(sl);
1064 static int
1065 _compare_strings_for_pqueue(const void *s1, const void *s2)
1067 return strcmp((const char*)s1, (const char*)s2);
1070 static void
1071 test_pqueue(void)
1073 smartlist_t *sl;
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");
1092 OK();
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);
1098 OK();
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");
1102 OK();
1103 smartlist_pqueue_add(sl, cmp, (char*)"fireflies");
1104 OK();
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");
1108 OK();
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");
1113 OK();
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);
1119 OK();
1120 #undef OK
1121 smartlist_free(sl);
1124 static void
1125 test_gzip(void)
1127 char *buf1, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
1128 const char *ccp2;
1129 size_t len1, len2;
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,
1136 GZIP_METHOD));
1137 test_assert(buf2);
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));
1143 test_assert(buf3);
1144 test_streq(buf1,buf3);
1146 tor_free(buf2);
1147 tor_free(buf3);
1150 test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
1151 ZLIB_METHOD));
1152 test_assert(buf2);
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));
1158 test_assert(buf3);
1159 test_streq(buf1,buf3);
1161 /* Check whether we can uncompress concatenated, compresed strings. */
1162 tor_free(buf3);
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);
1168 test_memeq(buf3,
1169 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
1170 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
1171 (strlen(buf1)+1)*2);
1173 tor_free(buf1);
1174 tor_free(buf2);
1175 tor_free(buf3);
1177 /* Check whether we can uncompress partial strings. */
1178 buf1 =
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,
1181 ZLIB_METHOD));
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));
1186 buf3[len2]='\0';
1187 tor_assert(len2 > 5);
1188 tor_assert(!strcmpstart(buf1, buf3));
1190 /* when we demand a complete string, this must fail. */
1191 tor_free(buf3);
1192 tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
1193 ZLIB_METHOD, 1, LOG_INFO));
1194 tor_assert(!buf3);
1196 /* Now, try streaming compression. */
1197 tor_free(buf1);
1198 tor_free(buf2);
1199 tor_free(buf3);
1200 state = tor_zlib_new(1, ZLIB_METHOD);
1201 tor_assert(state);
1202 cp1 = buf1 = tor_malloc(1024);
1203 len1 = 1024;
1204 ccp2 = "ABCDEFGHIJABCDEFGHIJ";
1205 len2 = 21;
1206 test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
1207 == TOR_ZLIB_OK);
1208 test_eq(len2, 0); /* Make sure we compressed it all. */
1209 test_assert(cp1 > buf1);
1211 len2 = 0;
1212 cp2 = cp1;
1213 test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
1214 == TOR_ZLIB_DONE);
1215 test_eq(len2, 0);
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.*/
1221 tor_free(buf3);
1223 tor_zlib_free(state);
1225 tor_free(buf2);
1226 tor_free(buf3);
1227 tor_free(buf1);
1230 static void
1231 test_strmap(void)
1233 strmap_t *map;
1234 strmap_iter_t *iter;
1235 const char *k;
1236 void *v;
1237 char *visited;
1238 smartlist_t *found_keys;
1240 map = strmap_new();
1241 v = strmap_set(map, "K1", (void*)99);
1242 test_eq(v, NULL);
1243 v = strmap_set(map, "K2", (void*)101);
1244 test_eq(v, NULL);
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);
1276 } else {
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");
1288 tor_free(visited);
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. */
1297 map = strmap_new();
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);
1309 static void
1310 test_mmap(void)
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.");
1329 #ifdef MS_WINDOWS
1330 tor_munmap_file(mapping);
1331 test_assert(unlink(fname1) == 0);
1332 #else
1333 /* make sure we can unlink. */
1334 test_assert(unlink(fname1) == 0);
1335 test_streq(mapping->data, "Short file.");
1336 tor_munmap_file(mapping);
1337 #endif
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 */
1358 unlink(fname2);
1359 unlink(fname3);
1361 tor_free(fname1);
1362 tor_free(fname2);
1363 tor_free(fname3);
1364 tor_free(buf);
1367 static void
1368 test_control_formats(void)
1370 char *out;
1371 const char *inp =
1372 "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
1373 size_t sz;
1375 sz = read_escaped_data(inp, strlen(inp), 1, &out);
1376 test_streq(out,
1377 ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
1378 test_eq(sz, strlen(out));
1380 tor_free(out);
1383 static void
1384 test_onion_handshake(void)
1386 /* client-side */
1387 crypto_dh_env_t *c_dh = NULL;
1388 char c_buf[ONIONSKIN_CHALLENGE_LEN];
1389 char c_keys[40];
1391 /* server-side */
1392 char s_buf[ONIONSKIN_REPLY_LEN];
1393 char s_keys[40];
1395 /* shared */
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)) {
1417 puts("Aiiiie");
1418 exit(1);
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;
1428 static void
1429 test_dir_format(void)
1431 char buf[8192], buf2[8192];
1432 char platform[256];
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;
1441 tor_version_t ver1;
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"));
1454 /* valid */
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"));
1461 /* too short */
1462 test_assert(!is_legal_nickname_or_hexdigest(
1463 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1464 /* illegal char */
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"));
1472 /* Bad nickname */
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;
1495 r1.or_port = 9000;
1496 r1.dir_port = 9003;
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;
1507 ex1.string = NULL;
1508 ex1.addr = 0;
1509 ex1.msk = 0;
1510 ex1.prt_min = ex1.prt_max = 80;
1511 ex1.next = &ex2;
1512 ex2.policy_type = ADDR_POLICY_REJECT;
1513 ex2.addr = 18 << 24;
1514 ex2.msk = 0xFF000000u;
1515 ex2.prt_min = ex2.prt_max = 24;
1516 ex2.next = NULL;
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;
1521 r2.or_port = 9005;
1522 r2.dir_port = 0;
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,
1534 &pk1_str_len));
1535 test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
1536 &pk2_str_len));
1537 test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
1538 &pk3_str_len));
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));
1546 strlcat(buf2, "\n"
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));
1562 #endif
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
1566 * twice */
1568 test_streq(buf, buf2);
1569 tor_free(bw_lines);
1571 test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
1572 cp = buf;
1573 rp1 = router_parse_entry_from_string((const char*)cp,NULL,1);
1574 test_assert(rp1);
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);
1585 #if 0
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);
1595 cp = buf;
1596 rp2 = router_parse_entry_from_string(&cp,1);
1597 test_assert(rp2);
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);
1624 char d[DIGEST_LEN];
1625 const char *m;
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));
1639 tor_free(cp);
1641 #endif
1642 dirserv_free_fingerprint_list();
1644 tor_free(pk1_str);
1645 tor_free(pk2_str);
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",
1726 "0.0.8rc2"));
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"));
1733 static void
1734 test_policies(void)
1736 addr_policy_t *policy, *policy2;
1737 config_line_t line;
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));
1755 policy2 = NULL;
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. */
1772 policy = NULL;
1773 line.key = (char*)"foo";
1774 line.value = (char*)"accept *:80,reject private:*,reject *:*";
1775 line.next = NULL;
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);
1785 static void
1786 test_rend_fns(void)
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;
1793 char *encoded;
1794 size_t len;
1795 crypto_pk_env_t *pk1, *pk2;
1796 time_t now;
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);
1803 now = time(NULL);
1804 d1->timestamp = now;
1805 d1->n_intro_points = 3;
1806 d1->version = 0;
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);
1813 test_assert(d2);
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);
1827 tor_free(encoded);
1829 /* Test versioned descriptor. */
1830 d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
1831 d1->pk = crypto_pk_dup_key(pk1);
1832 now = time(NULL);
1833 d1->timestamp = now;
1834 d1->n_intro_points = 2;
1835 d1->version = 1;
1836 d1->protocols = 60;
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);
1857 test_assert(d2);
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);
1889 static void
1890 bench_aes(void)
1892 int len, i;
1893 char *b1, *b2;
1894 crypto_cipher_env_t *c;
1895 struct timeval start, end;
1896 const int iters = 100000;
1897 uint64_t nsec;
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);
1909 tor_free(b1);
1910 tor_free(b2);
1911 nsec = (uint64_t) tv_udiff(&start,&end);
1912 nsec *= 1000;
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;
1925 (void) c;
1926 (void) v;
1927 options->command = CMD_RUN_UNITTESTS;
1928 rep_hist_init();
1929 network_init();
1930 setup_directory();
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);
1935 tor_free(errmsg);
1936 return 1;
1939 crypto_seed_rng();
1941 if (0) {
1942 bench_aes();
1943 return 0;
1946 atexit(remove_directory);
1948 printf("Running Tor unit tests on %s\n", get_uname());
1950 puts("========================== Buffers =========================");
1951 test_buffers();
1952 puts("\n========================== Crypto ==========================");
1953 // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
1954 test_crypto();
1955 test_crypto_dh();
1956 test_crypto_s2k();
1957 puts("\n========================= Util ============================");
1958 test_gzip();
1959 test_util();
1960 test_smartlist();
1961 test_strmap();
1962 test_control_formats();
1963 test_pqueue();
1964 test_mmap();
1965 puts("\n========================= Onion Skins =====================");
1966 test_onion_handshake();
1967 puts("\n========================= Directory Formats ===============");
1968 test_dir_format();
1969 puts("\n========================= Policies ===================");
1970 test_policies();
1971 puts("\n========================= Rendezvous functionality ========");
1972 test_rend_fns();
1973 puts("");
1975 if (have_failed)
1976 return 1;
1977 else
1978 return 0;