Forward port changelog
[tor.git] / src / or / test.c
blobc6a3b88e80ef09c575f352359ce8c615bf3a2b94
1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
5 const char test_c_id[] = "$Id$";
7 #include "orconfig.h"
8 #include <stdio.h>
9 #ifdef HAVE_FCNTL_H
10 #include <fcntl.h>
11 #endif
13 #ifdef MS_WINDOWS
14 /* For mkdir() */
15 #include <direct.h>
16 #else
17 #include <dirent.h>
18 #endif
20 #include "or.h"
21 #include "../common/test.h"
22 #include "../common/torgzip.h"
24 int have_failed = 0;
26 /* These functions are file-local, but are exposed so we can test. */
27 void add_fingerprint_to_dir(const char *nickname, const char *fp);
28 void get_platform_str(char *platform, size_t len);
29 int is_obsolete_version(const char *myversion, const char *start);
31 static char temp_dir[256];
33 static void
34 setup_directory(void)
36 static int is_setup = 0;
37 int r;
38 if (is_setup) return;
40 #ifdef MS_WINDOWS
41 // XXXX
42 tor_snprintf(temp_dir, sizeof(temp_dir), "c:\\windows\\temp\\tor_test_%d", (int)getpid());
43 r = mkdir(temp_dir);
44 #else
45 tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
46 r = mkdir(temp_dir, 0700);
47 #endif
48 if (r) {
49 fprintf(stderr, "Can't create directory %s:", temp_dir);
50 perror("");
51 exit(1);
53 is_setup = 1;
56 static const char *
57 get_fname(const char *name)
59 static char buf[1024];
60 setup_directory();
61 tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
62 return buf;
65 static void
66 remove_directory(void)
68 #ifdef MS_WINDOWS
69 char *pattern;
70 HANDLE handle;
71 WIN32_FIND_DATA findData;
73 setup_directory();
74 pattern = tor_malloc(strlen(temp_dir)+16);
75 tor_snprintf(pattern, strlen(temp_dir)+16, "%s\\*", temp_dir);
76 handle = FindFirstFile(pattern, &findData);
77 if (handle == INVALID_HANDLE_VALUE) {
78 perror("Can't remove");
79 return;
81 while (1) {
82 size_t dlen = strlen(findData.cFileName)+strlen(temp_dir)+16;
83 char *deleteable = tor_malloc(dlen);
84 tor_snprintf(deleteable, dlen, "%s\\%s", temp_dir, findData.cFileName);
85 unlink(deleteable);
86 tor_free(deleteable);
87 if (!FindNextFile(handle, &findData)) {
88 if (GetLastError() != ERROR_NO_MORE_FILES) {
89 perror("error reading dir");
91 break;
94 FindClose(handle);
95 tor_free(pattern);
96 #else
97 DIR *dirp;
98 struct dirent *de;
99 setup_directory();
100 if (!(dirp = opendir(temp_dir))) {
101 perror("Can't open temporary directory to remove files");
102 return;
104 while ((de = readdir(dirp)) != NULL) {
105 /* Only "." and ".." start with ., since we don't create any dotfiles. */
106 if (de->d_name[0] == '.') continue;
107 if (unlink(get_fname(de->d_name))) {
108 perror("Error removing file");
110 #if 0
111 printf("==%s\n", de->d_name);
112 #endif
114 closedir(dirp);
115 #endif
116 rmdir(temp_dir);
119 static void
120 test_buffers(void) {
121 #define MAX_BUF_SIZE 1024*1024
122 char str[256];
123 char str2[256];
125 buf_t *buf;
126 buf_t *buf2;
128 int s, i, j, eof;
130 /****
131 * buf_new
132 ****/
133 if (!(buf = buf_new()))
134 test_fail();
136 test_eq(buf_capacity(buf), 512*1024);
137 test_eq(buf_datalen(buf), 0);
139 /****
140 * read_to_buf
141 ****/
142 s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
143 for (j=0;j<256;++j) {
144 str[j] = (char)j;
146 write(s, str, 256);
147 close(s);
149 s = open(get_fname("data"), O_RDONLY, 0);
150 eof = 0;
151 i = read_to_buf(s, 10, buf, &eof);
152 test_eq(buf_capacity(buf), 512*1024);
153 test_eq(buf_datalen(buf), 10);
154 test_eq(eof, 0);
155 test_eq(i, 10);
156 test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
158 /* Test reading 0 bytes. */
159 i = read_to_buf(s, 0, buf, &eof);
160 test_eq(buf_capacity(buf), 512*1024);
161 test_eq(buf_datalen(buf), 10);
162 test_eq(eof, 0);
163 test_eq(i, 0);
165 /* Now test when buffer is filled exactly. */
166 buf2 = buf_new_with_capacity(6);
167 i = read_to_buf(s, 6, buf2, &eof);
168 test_eq(buf_capacity(buf2), 6);
169 test_eq(buf_datalen(buf2), 6);
170 test_eq(eof, 0);
171 test_eq(i, 6);
172 test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
173 buf_free(buf2);
175 /* Now test when buffer is filled with more data to read. */
176 buf2 = buf_new_with_capacity(32);
177 i = read_to_buf(s, 128, buf2, &eof);
178 test_eq(buf_capacity(buf2), 128);
179 test_eq(buf_datalen(buf2), 32);
180 test_eq(eof, 0);
181 test_eq(i, 32);
182 buf_free(buf2);
184 /* Now read to eof. */
185 test_assert(buf_capacity(buf) > 256);
186 i = read_to_buf(s, 1024, buf, &eof);
187 test_eq(i, (256-32-10-6));
188 test_eq(buf_capacity(buf), MAX_BUF_SIZE);
189 test_eq(buf_datalen(buf), 256-6-32);
190 test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
191 test_eq(eof, 0);
193 i = read_to_buf(s, 1024, buf, &eof);
194 test_eq(i, 0);
195 test_eq(buf_capacity(buf), MAX_BUF_SIZE);
196 test_eq(buf_datalen(buf), 256-6-32);
197 test_eq(eof, 1);
199 close(s);
201 /****
202 * fetch_from_buf
203 ****/
204 memset(str2, 255, 256);
205 test_eq(246, fetch_from_buf(str2, 10, buf));
206 test_memeq(str2, str, 10);
207 test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246);
208 test_eq(buf_datalen(buf),246);
210 test_eq(0, fetch_from_buf(str2, 246, buf));
211 test_memeq(str2, str+10, 246);
212 test_eq(buf_capacity(buf),MAX_BUF_SIZE);
213 test_eq(buf_datalen(buf),0);
215 /****
216 * write_to_buf
217 ****/
218 memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256);
219 i = write_to_buf("Hello world", 11, buf);
220 test_eq(i, 11);
221 test_eq(buf_datalen(buf), 11);
222 test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11);
223 i = write_to_buf("XYZZY", 5, buf);
224 test_eq(i, 16);
225 test_eq(buf_datalen(buf), 16);
226 test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16);
227 /* Test when buffer is overfull. */
228 #if 0
229 buflen = 18;
230 test_eq(-1, write_to_buf("This string will not fit.", 25,
231 &buf, &buflen, &buf_datalen));
232 test_eq(buf_datalen, 16);
233 test_memeq(buf, "Hello worldXYZZY--", 18);
234 buflen = MAX_BUF_SIZE;
235 #endif
237 /****
238 * flush_buf
239 ****/
240 /* XXXX Needs tests. */
242 buf_free(buf);
245 static void
246 test_crypto_dh(void)
248 crypto_dh_env_t *dh1, *dh2;
249 char p1[DH_BYTES];
250 char p2[DH_BYTES];
251 char s1[DH_BYTES];
252 char s2[DH_BYTES];
253 int s1len, s2len;
255 dh1 = crypto_dh_new();
256 dh2 = crypto_dh_new();
257 test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
258 test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
260 memset(p1, 0, DH_BYTES);
261 memset(p2, 0, DH_BYTES);
262 test_memeq(p1, p2, DH_BYTES);
263 test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
264 test_memneq(p1, p2, DH_BYTES);
265 test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
266 test_memneq(p1, p2, DH_BYTES);
268 memset(s1, 0, DH_BYTES);
269 memset(s2, 0xFF, DH_BYTES);
270 s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
271 s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
272 test_assert(s1len > 0);
273 test_eq(s1len, s2len);
274 test_memeq(s1, s2, s1len);
276 crypto_dh_free(dh1);
277 crypto_dh_free(dh2);
280 static void
281 test_crypto(void)
283 crypto_cipher_env_t *env1, *env2;
284 crypto_pk_env_t *pk1, *pk2;
285 char *data1, *data2, *data3, *cp;
286 int i, j, p, len;
287 size_t size;
289 data1 = tor_malloc(1024);
290 data2 = tor_malloc(1024);
291 data3 = tor_malloc(1024);
292 test_assert(data1 && data2 && data3);
294 /* Try out RNG. */
295 test_assert(! crypto_seed_rng());
296 crypto_rand(data1, 100);
297 crypto_rand(data2, 100);
298 test_memneq(data1,data2,100);
300 #if 0
301 /* Try out identity ciphers. */
302 env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
303 test_neq(env1, 0);
304 test_eq(crypto_cipher_generate_key(env1), 0);
305 test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
306 for (i = 0; i < 1024; ++i) {
307 data1[i] = (char) i*73;
309 crypto_cipher_encrypt(env1, data2, data1, 1024);
310 test_memeq(data1, data2, 1024);
311 crypto_free_cipher_env(env1);
312 #endif
314 /* Now, test encryption and decryption with stream cipher. */
315 data1[0]='\0';
316 for (i = 1023; i>0; i -= 35)
317 strncat(data1, "Now is the time for all good onions", i);
319 memset(data2, 0, 1024);
320 memset(data3, 0, 1024);
321 env1 = crypto_new_cipher_env();
322 test_neq(env1, 0);
323 env2 = crypto_new_cipher_env();
324 test_neq(env2, 0);
325 j = crypto_cipher_generate_key(env1);
326 crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
327 crypto_cipher_encrypt_init_cipher(env1);
328 crypto_cipher_decrypt_init_cipher(env2);
330 /* Try encrypting 512 chars. */
331 crypto_cipher_encrypt(env1, data2, data1, 512);
332 crypto_cipher_decrypt(env2, data3, data2, 512);
333 test_memeq(data1, data3, 512);
334 test_memneq(data1, data2, 512);
336 /* Now encrypt 1 at a time, and get 1 at a time. */
337 for (j = 512; j < 560; ++j) {
338 crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
340 for (j = 512; j < 560; ++j) {
341 crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
343 test_memeq(data1, data3, 560);
344 /* Now encrypt 3 at a time, and get 5 at a time. */
345 for (j = 560; j < 1024-5; j += 3) {
346 crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
348 for (j = 560; j < 1024-5; j += 5) {
349 crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
351 test_memeq(data1, data3, 1024-5);
352 /* Now make sure that when we encrypt with different chunk sizes, we get
353 the same results. */
354 crypto_free_cipher_env(env2);
356 memset(data3, 0, 1024);
357 env2 = crypto_new_cipher_env();
358 test_neq(env2, 0);
359 crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
360 crypto_cipher_encrypt_init_cipher(env2);
361 for (j = 0; j < 1024-16; j += 17) {
362 crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
364 for (j= 0; j < 1024-16; ++j) {
365 if (data2[j] != data3[j]) {
366 printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
369 test_memeq(data2, data3, 1024-16);
370 crypto_free_cipher_env(env1);
371 crypto_free_cipher_env(env2);
373 /* Test vectors for stream ciphers. */
374 /* XXXX Look up some test vectors for the ciphers and make sure we match. */
376 /* Test SHA-1 with a test vector from the specification. */
377 i = crypto_digest(data1, "abc", 3);
378 test_memeq(data1,
379 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
380 "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
382 /* Public-key ciphers */
383 pk1 = crypto_new_pk_env();
384 pk2 = crypto_new_pk_env();
385 test_assert(pk1 && pk2);
386 test_assert(! crypto_pk_generate_key(pk1));
387 test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
388 test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
389 test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
390 tor_free(cp);
392 /* Check DER encoding */
393 i=crypto_pk_DER64_encode_public_key(pk1, &cp);
394 test_assert(i>0);
395 test_assert(cp);
396 test_assert(!strchr(cp, ' '));
397 test_assert(!strchr(cp, '\n'));
398 test_eq(0, crypto_pk_cmp_keys(pk1, pk1));
399 crypto_free_pk_env(pk2);
400 pk2 = crypto_pk_DER64_decode_public_key(cp);
401 test_assert(pk2);
402 test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
403 tor_free(cp);
405 test_eq(128, crypto_pk_keysize(pk1));
406 test_eq(128, crypto_pk_keysize(pk2));
408 test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
409 PK_PKCS1_OAEP_PADDING));
410 test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
411 PK_PKCS1_OAEP_PADDING));
412 /* oaep padding should make encryption not match */
413 test_memneq(data1, data2, 128);
414 test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
415 PK_PKCS1_OAEP_PADDING,1));
416 test_streq(data3, "Hello whirled.");
417 memset(data3, 0, 1024);
418 test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
419 PK_PKCS1_OAEP_PADDING,1));
420 test_streq(data3, "Hello whirled.");
421 /* Can't decrypt with public key. */
422 test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
423 PK_PKCS1_OAEP_PADDING,1));
424 /* Try again with bad padding */
425 memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
426 test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
427 PK_PKCS1_OAEP_PADDING,1));
429 /* File operations: save and load private key */
430 test_assert(! crypto_pk_write_private_key_to_filename(pk1,
431 get_fname("pkey1")));
433 test_assert(! crypto_pk_read_private_key_from_filename(pk2,
434 get_fname("pkey1")));
435 test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
436 PK_PKCS1_OAEP_PADDING,1));
438 /* Now try signing. */
439 strcpy(data1, "Ossifrage");
440 test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
441 test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
442 test_streq(data3, "Ossifrage");
443 /* Try signing digests. */
444 test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
445 test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
446 test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
447 test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
448 /*XXXX test failed signing*/
450 /* Try encoding */
451 crypto_free_pk_env(pk2);
452 pk2 = NULL;
453 i = crypto_pk_asn1_encode(pk1, data1, 1024);
454 test_assert(i>0);
455 pk2 = crypto_pk_asn1_decode(data1, i);
456 test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
458 /* Try with hybrid encryption wrappers. */
459 crypto_rand(data1, 1024);
460 for (i = 0; i < 3; ++i) {
461 for (j = 85; j < 140; ++j) {
462 memset(data2,0,1024);
463 memset(data3,0,1024);
464 if (i == 0 && j < 129)
465 continue;
466 p = (i==0)?PK_NO_PADDING:
467 (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
468 len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
469 test_assert(len>=0);
470 len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
471 test_eq(len,j);
472 test_memeq(data1,data3,j);
475 crypto_free_pk_env(pk1);
476 crypto_free_pk_env(pk2);
478 /* Base64 tests */
479 strcpy(data1, "Test string that contains 35 chars.");
480 strcat(data1, " 2nd string that contains 35 chars.");
482 i = base64_encode(data2, 1024, data1, 71);
483 j = base64_decode(data3, 1024, data2, i);
484 test_streq(data3, data1);
485 test_eq(j, 71);
486 test_assert(data2[i] == '\0');
488 /* Base32 tests */
489 strcpy(data1, "5chrs");
490 /* bit pattern is: [35 63 68 72 73] ->
491 * [00110101 01100011 01101000 01110010 01110011]
492 * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
494 base32_encode(data2, 9, data1, 5);
495 test_streq(data2, "gvrwq4tt");
497 strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
498 base32_encode(data2, 30, data1, 10);
499 test_streq(data2, "772w2rfobvomsywe");
501 /* Base16 tests */
502 strcpy(data1, "6chrs\xff");
503 base16_encode(data2, 13, data1, 6);
504 test_streq(data2, "3663687273FF");
506 strcpy(data1, "f0d678affc000100");
507 i = base16_decode(data2, 8, data1, 16);
508 test_eq(i,0);
509 test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
511 free(data1);
512 free(data2);
513 free(data3);
516 static void
517 test_util(void) {
518 struct timeval start, end;
519 struct tm a_time;
520 smartlist_t *sl;
521 char timestr[RFC1123_TIME_LEN+1];
522 char buf[1024];
523 time_t t_res;
524 int i;
525 uint32_t u32;
526 uint16_t u16;
527 char *cp, *k, *v;
529 start.tv_sec = 5;
530 start.tv_usec = 5000;
532 end.tv_sec = 5;
533 end.tv_usec = 5000;
535 test_eq(0L, tv_udiff(&start, &end));
537 end.tv_usec = 7000;
539 test_eq(2000L, tv_udiff(&start, &end));
541 end.tv_sec = 6;
543 test_eq(1002000L, tv_udiff(&start, &end));
545 end.tv_usec = 0;
547 test_eq(995000L, tv_udiff(&start, &end));
549 end.tv_sec = 4;
551 test_eq(-1005000L, tv_udiff(&start, &end));
553 /* The test values here are confirmed to be correct on a platform
554 * with a working timegm. */
555 a_time.tm_year = 2003-1900;
556 a_time.tm_mon = 7;
557 a_time.tm_mday = 30;
558 a_time.tm_hour = 6;
559 a_time.tm_min = 14;
560 a_time.tm_sec = 55;
561 test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
562 a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
563 test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
564 a_time.tm_mon = 1; /* Try a leap year, in feb. */
565 a_time.tm_mday = 10;
566 test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
568 format_rfc1123_time(timestr, 0);
569 test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
570 format_rfc1123_time(timestr, (time_t)1091580502UL);
571 test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
573 t_res = 0;
574 i = parse_rfc1123_time(timestr, &t_res);
575 test_eq(i,0);
576 test_eq(t_res, (time_t)1091580502UL);
578 /* Test smartlist */
579 sl = smartlist_create();
580 smartlist_add(sl, (void*)1);
581 smartlist_add(sl, (void*)2);
582 smartlist_add(sl, (void*)3);
583 smartlist_add(sl, (void*)4);
584 smartlist_del_keeporder(sl, 1);
585 smartlist_insert(sl, 1, (void*)22);
586 smartlist_insert(sl, 0, (void*)0);
587 smartlist_insert(sl, 5, (void*)555);
588 test_eq((void*)0, smartlist_get(sl,0));
589 test_eq((void*)1, smartlist_get(sl,1));
590 test_eq((void*)22, smartlist_get(sl,2));
591 test_eq((void*)3, smartlist_get(sl,3));
592 test_eq((void*)4, smartlist_get(sl,4));
593 test_eq((void*)555, smartlist_get(sl,5));
595 smartlist_clear(sl);
596 smartlist_split_string(sl, "abc", ":", 0, 0);
597 test_eq(1, smartlist_len(sl));
598 test_streq("abc", smartlist_get(sl, 0));
599 smartlist_split_string(sl, "a::bc::", "::", 0, 0);
600 test_eq(4, smartlist_len(sl));
601 test_streq("a", smartlist_get(sl, 1));
602 test_streq("bc", smartlist_get(sl, 2));
603 test_streq("", smartlist_get(sl, 3));
604 cp = smartlist_join_strings(sl, "", 0, NULL);
605 test_streq(cp, "abcabc");
606 tor_free(cp);
607 cp = smartlist_join_strings(sl, "!", 0, NULL);
608 test_streq(cp, "abc!a!bc!");
609 tor_free(cp);
610 cp = smartlist_join_strings(sl, "XY", 0, NULL);
611 test_streq(cp, "abcXYaXYbcXY");
612 tor_free(cp);
613 cp = smartlist_join_strings(sl, "XY", 1, NULL);
614 test_streq(cp, "abcXYaXYbcXYXY");
615 tor_free(cp);
616 cp = smartlist_join_strings(sl, "", 1, NULL);
617 test_streq(cp, "abcabc");
618 tor_free(cp);
620 smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
621 test_eq(8, smartlist_len(sl));
622 test_streq("", smartlist_get(sl, 4));
623 test_streq("def", smartlist_get(sl, 5));
624 test_streq(" ", smartlist_get(sl, 6));
625 test_streq("ghijk", smartlist_get(sl, 7));
626 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
627 smartlist_clear(sl);
629 smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
630 test_eq(3, smartlist_len(sl));
631 test_streq("a", smartlist_get(sl,0));
632 test_streq("bbd", smartlist_get(sl,1));
633 test_streq("cdef", smartlist_get(sl,2));
634 smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE, 0);
635 test_eq(8, smartlist_len(sl));
636 test_streq("z", smartlist_get(sl,3));
637 test_streq("zhasd", smartlist_get(sl,4));
638 test_streq("", smartlist_get(sl,5));
639 test_streq("bnud", smartlist_get(sl,6));
640 test_streq("", smartlist_get(sl,7));
642 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
643 smartlist_clear(sl);
645 smartlist_split_string(sl, " ab\tc \td ef ", NULL,
646 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
647 test_eq(4, smartlist_len(sl));
648 test_streq("ab", smartlist_get(sl,0));
649 test_streq("c", smartlist_get(sl,1));
650 test_streq("d", smartlist_get(sl,2));
651 test_streq("ef", smartlist_get(sl,3));
652 smartlist_split_string(sl, "ghi\tj", NULL,
653 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
654 test_eq(6, smartlist_len(sl));
655 test_streq("ghi", smartlist_get(sl,4));
656 test_streq("j", smartlist_get(sl,5));
658 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
659 smartlist_clear(sl);
661 smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
662 test_eq(3, smartlist_len(sl));
663 test_streq("z", smartlist_get(sl, 0));
664 test_streq("zhasd", smartlist_get(sl, 1));
665 test_streq("bnud", smartlist_get(sl, 2));
666 smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
667 test_eq(5, smartlist_len(sl));
668 test_streq("z", smartlist_get(sl, 3));
669 test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
670 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
671 smartlist_clear(sl);
673 smartlist_split_string(sl, "abcd\n", "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
674 test_eq(1, smartlist_len(sl));
675 test_streq("abcd", smartlist_get(sl, 0));
676 smartlist_split_string(sl, "efgh", "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
677 test_eq(2, smartlist_len(sl));
678 test_streq("efgh", smartlist_get(sl, 1));
680 /* Test tor_strstrip() */
681 strcpy(buf, "Testing 1 2 3");
682 test_eq(0, tor_strstrip(buf, ",!"));
683 test_streq(buf, "Testing 1 2 3");
684 strcpy(buf, "!Testing 1 2 3?");
685 test_eq(5, tor_strstrip(buf, "!? "));
686 test_streq(buf, "Testing123");
688 /* Test tor_strpartition() */
689 test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
690 TERMINATE_IF_EVEN));
691 test_streq(buf, "abc##def##g");
692 test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
693 ALWAYS_TERMINATE));
694 test_streq(buf, "abc##def##g##");
695 test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
696 TERMINATE_IF_EVEN));
697 test_streq(buf, "abc##def##ghi##");
698 test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
699 NEVER_TERMINATE));
700 test_streq(buf, "abc##def##ghi");
702 /* Test parse_addr_port */
703 cp = NULL; u32 = 3; u16 = 3;
704 test_assert(!parse_addr_port("1.2.3.4", &cp, &u32, &u16));
705 test_streq(cp, "1.2.3.4");
706 test_eq(u32, 0x01020304u);
707 test_eq(u16, 0);
708 tor_free(cp);
709 test_assert(!parse_addr_port("4.3.2.1:99", &cp, &u32, &u16));
710 test_streq(cp, "4.3.2.1");
711 test_eq(u32, 0x04030201u);
712 test_eq(u16, 99);
713 tor_free(cp);
714 test_assert(!parse_addr_port("nonexistent.address:4040", &cp, NULL, &u16));
715 test_streq(cp, "nonexistent.address");
716 test_eq(u16, 4040);
717 tor_free(cp);
718 test_assert(!parse_addr_port("localhost:9999", &cp, &u32, &u16));
719 test_streq(cp, "localhost");
720 test_eq(u32, 0x7f000001u);
721 test_eq(u16, 9999);
722 tor_free(cp);
723 u32 = 3;
724 test_assert(!parse_addr_port("localhost", NULL, &u32, &u16));
725 test_eq(cp, NULL);
726 test_eq(u32, 0x7f000001u);
727 test_eq(u16, 0);
728 tor_free(cp);
730 /* Test tor_parse_long. */
731 test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
732 test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
734 /* Test parse_line_from_str */
735 strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
736 "k2\n"
737 "k3 \n" "\n" " \n" "#comment\n"
738 "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
739 cp = buf;
741 cp = parse_line_from_str(cp, &k, &v);
742 test_streq(k, "k");
743 test_streq(v, "v");
744 test_assert(!strcmpstart(cp, " key value with"));
746 cp = parse_line_from_str(cp, &k, &v);
747 test_streq(k, "key");
748 test_streq(v, "value with spaces");
749 test_assert(!strcmpstart(cp, "keykey"));
751 cp = parse_line_from_str(cp, &k, &v);
752 test_streq(k, "keykey");
753 test_streq(v, "val");
754 test_assert(!strcmpstart(cp, "k2\n"));
756 cp = parse_line_from_str(cp, &k, &v);
757 test_streq(k, "k2");
758 test_streq(v, "");
759 test_assert(!strcmpstart(cp, "k3 \n"));
761 cp = parse_line_from_str(cp, &k, &v);
762 test_streq(k, "k3");
763 test_streq(v, "");
764 test_assert(!strcmpstart(cp, "\n \n"));
766 cp = parse_line_from_str(cp, &k, &v);
767 test_streq(k, "k4");
768 test_streq(v, "");
769 test_assert(!strcmpstart(cp, "k5#abc"));
771 cp = parse_line_from_str(cp, &k, &v);
772 test_streq(k, "k5");
773 test_streq(v, "");
774 test_assert(!strcmpstart(cp, "k6"));
776 cp = parse_line_from_str(cp, &k, &v);
777 test_streq(k, "k6");
778 test_streq(v, "val");
779 test_streq(cp, "");
781 /* Test for strcmpstart and strcmpend. */
782 test_assert(strcmpstart("abcdef", "abcdef")==0);
783 test_assert(strcmpstart("abcdef", "abc")==0);
784 test_assert(strcmpstart("abcdef", "abd")<0);
785 test_assert(strcmpstart("abcdef", "abb")>0);
786 test_assert(strcmpstart("ab", "abb")<0);
788 test_assert(strcmpend("abcdef", "abcdef")==0);
789 test_assert(strcmpend("abcdef", "def")==0);
790 test_assert(strcmpend("abcdef", "deg")<0);
791 test_assert(strcmpend("abcdef", "dee")>0);
792 test_assert(strcmpend("ab", "abb")<0);
794 /* XXXX test older functions. */
795 smartlist_free(sl);
798 static void
799 test_gzip(void)
801 char *buf1, *buf2=NULL, *buf3=NULL;
802 size_t len1, len2;
804 buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
805 test_eq(detect_compression_method(buf1, strlen(buf1)), 0);
806 if (is_gzip_supported()) {
807 test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
808 GZIP_METHOD));
809 test_assert(buf2);
810 test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
811 test_eq(detect_compression_method(buf2, len1), GZIP_METHOD);
813 test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
814 test_assert(buf3);
815 test_streq(buf1,buf3);
817 tor_free(buf2);
818 tor_free(buf3);
821 test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
822 ZLIB_METHOD));
823 test_assert(buf2);
824 test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
825 test_eq(detect_compression_method(buf2, len1), ZLIB_METHOD);
827 test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
828 test_assert(buf3);
829 test_streq(buf1,buf3);
831 tor_free(buf2);
832 tor_free(buf3);
833 tor_free(buf1);
836 static void *
837 _squareAndRemoveK4(const char *key, void*val, void *data)
839 int *ip = (int*)data;
840 intptr_t v;
841 if (strcmp(key,"K4") == 0) {
842 ++(*ip);
843 return NULL;
845 v = (intptr_t)val;
846 return (void*)(v*v);
849 static void
850 test_strmap(void)
852 strmap_t *map;
853 strmap_iter_t *iter;
854 const char *k;
855 void *v;
856 int count;
858 map = strmap_new();
859 v = strmap_set(map, "K1", (void*)99);
860 test_eq(v, NULL);
861 v = strmap_set(map, "K2", (void*)101);
862 test_eq(v, NULL);
863 v = strmap_set(map, "K1", (void*)100);
864 test_eq(v, (void*)99);
865 test_eq(strmap_get(map,"K1"), (void*)100);
866 test_eq(strmap_get(map,"K2"), (void*)101);
867 test_eq(strmap_get(map,"K-not-there"), NULL);
869 v = strmap_remove(map,"K2");
870 test_eq(v, (void*)101);
871 test_eq(strmap_get(map,"K2"), NULL);
872 test_eq(strmap_remove(map,"K2"), NULL);
874 strmap_set(map, "K2", (void*)101);
875 strmap_set(map, "K3", (void*)102);
876 strmap_set(map, "K4", (void*)103);
877 strmap_set(map, "K5", (void*)104);
878 strmap_set(map, "K6", (void*)105);
880 count = 0;
881 strmap_foreach(map, _squareAndRemoveK4, &count);
882 test_eq(count, 1);
883 test_eq(strmap_get(map, "K4"), NULL);
884 test_eq(strmap_get(map, "K1"), (void*)10000);
885 test_eq(strmap_get(map, "K6"), (void*)11025);
887 iter = strmap_iter_init(map);
888 strmap_iter_get(iter,&k,&v);
889 test_streq(k, "K1");
890 test_eq(v, (void*)10000);
891 iter = strmap_iter_next(map,iter);
892 strmap_iter_get(iter,&k,&v);
893 test_streq(k, "K2");
894 test_eq(v, (void*)10201);
895 iter = strmap_iter_next_rmv(map,iter);
896 strmap_iter_get(iter,&k,&v);
897 test_streq(k, "K3");
898 test_eq(v, (void*)10404);
899 iter = strmap_iter_next(map,iter); /* K5 */
900 test_assert(!strmap_iter_done(iter));
901 iter = strmap_iter_next(map,iter); /* K6 */
902 test_assert(!strmap_iter_done(iter));
903 iter = strmap_iter_next(map,iter); /* done */
904 test_assert(strmap_iter_done(iter));
906 /* Make sure we removed K2, but not the others. */
907 test_eq(strmap_get(map, "K2"), NULL);
908 test_eq(strmap_get(map, "K5"), (void*)10816);
910 /* Clean up after ourselves. */
911 strmap_free(map, NULL);
913 /* Now try some lc functions. */
914 map = strmap_new();
915 strmap_set_lc(map,"Ab.C", (void*)1);
916 test_eq(strmap_get(map,"ab.c"), (void*)1);
917 test_eq(strmap_get_lc(map,"AB.C"), (void*)1);
918 test_eq(strmap_get(map,"AB.C"), NULL);
919 test_eq(strmap_remove_lc(map,"aB.C"), (void*)1);
920 test_eq(strmap_get_lc(map,"AB.C"), NULL);
921 strmap_free(map,NULL);
924 static void
925 test_onion(void)
927 #if 0
928 char **names;
929 int i,num;
931 names = parse_nickname_list(" foo bar\t baz quux ", &num);
932 test_eq(num,4);
933 test_streq(names[0],"foo");
934 test_streq(names[1],"bar");
935 test_streq(names[2],"baz");
936 test_streq(names[3],"quux");
937 for (i=0;i<num;i++)
938 tor_free(names[i]);
939 tor_free(names);
940 #endif
943 static void
944 test_onion_handshake(void)
946 /* client-side */
947 crypto_dh_env_t *c_dh = NULL;
948 char c_buf[ONIONSKIN_CHALLENGE_LEN];
949 char c_keys[40];
951 /* server-side */
952 char s_buf[ONIONSKIN_REPLY_LEN];
953 char s_keys[40];
955 /* shared */
956 crypto_pk_env_t *pk = NULL;
958 pk = crypto_new_pk_env();
959 test_assert(! crypto_pk_generate_key(pk));
961 /* client handshake 1. */
962 memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
963 test_assert(! onion_skin_create(pk, &c_dh, c_buf));
965 /* server handshake */
966 memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
967 memset(s_keys, 0, 40);
968 test_assert(! onion_skin_server_handshake(c_buf, pk, NULL, s_buf, s_keys, 40));
970 /* client handshake 2 */
971 memset(c_keys, 0, 40);
972 test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
974 crypto_dh_free(c_dh);
976 if (memcmp(c_keys, s_keys, 40)) {
977 puts("Aiiiie");
978 exit(1);
980 test_memeq(c_keys, s_keys, 40);
981 memset(s_buf, 0, 40);
982 test_memneq(c_keys, s_buf, 40);
983 crypto_free_pk_env(pk);
986 static void
987 test_dir_format(void)
989 char buf[8192], buf2[8192];
990 char platform[256];
991 char fingerprint[FINGERPRINT_LEN+1];
992 char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
993 size_t pk1_str_len, pk2_str_len, pk3_str_len;
994 routerinfo_t r1, r2;
995 crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
996 routerinfo_t *rp1 = NULL, *rp2 = NULL;
997 addr_policy_t ex1, ex2;
998 routerlist_t *dir1 = NULL, *dir2 = NULL;
999 tor_version_t ver1;
1000 char *bw_lines = NULL;
1001 const char *m;
1003 test_assert( (pk1 = crypto_new_pk_env()) );
1004 test_assert( (pk2 = crypto_new_pk_env()) );
1005 test_assert( (pk3 = crypto_new_pk_env()) );
1006 test_assert(! crypto_pk_generate_key(pk1));
1007 test_assert(! crypto_pk_generate_key(pk2));
1008 test_assert(! crypto_pk_generate_key(pk3));
1010 test_assert( is_legal_nickname("a"));
1011 test_assert(!is_legal_nickname(""));
1012 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
1013 test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
1014 test_assert(!is_legal_nickname("hyphen-")); /* bad char */
1015 test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
1016 test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1017 /* valid */
1018 test_assert( is_legal_nickname_or_hexdigest(
1019 "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1020 /* too short */
1021 test_assert(!is_legal_nickname_or_hexdigest(
1022 "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1023 /* illegal char */
1024 test_assert(!is_legal_nickname_or_hexdigest(
1025 "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
1026 test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
1027 test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
1028 test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
1030 get_platform_str(platform, sizeof(platform));
1031 memset(&r1,0,sizeof(r1));
1032 memset(&r2,0,sizeof(r2));
1033 r1.address = tor_strdup("18.244.0.1");
1034 r1.addr = 0xc0a80001u; /* 192.168.0.1 */
1035 r1.published_on = 0;
1036 r1.or_port = 9000;
1037 r1.dir_port = 9003;
1038 r1.onion_pkey = pk1;
1039 r1.identity_pkey = pk2;
1040 r1.bandwidthrate = 1000;
1041 r1.bandwidthburst = 5000;
1042 r1.bandwidthcapacity = 10000;
1043 r1.exit_policy = NULL;
1044 r1.nickname = tor_strdup("Magri");
1045 r1.platform = tor_strdup(platform);
1047 ex1.policy_type = ADDR_POLICY_ACCEPT;
1048 ex1.string = NULL;
1049 ex1.addr = 0;
1050 ex1.msk = 0;
1051 ex1.prt_min = ex1.prt_max = 80;
1052 ex1.next = &ex2;
1053 ex2.policy_type = ADDR_POLICY_REJECT;
1054 ex2.addr = 18 << 24;
1055 ex2.msk = 0xFF000000u;
1056 ex2.prt_min = ex2.prt_max = 24;
1057 ex2.next = NULL;
1058 r2.address = tor_strdup("1.1.1.1");
1059 r2.addr = 0x0a030201u; /* 10.3.2.1 */
1060 r2.platform = tor_strdup(platform);
1061 r2.published_on = 5;
1062 r2.or_port = 9005;
1063 r2.dir_port = 0;
1064 r2.onion_pkey = pk2;
1065 r2.identity_pkey = pk1;
1066 r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
1067 r2.exit_policy = &ex1;
1068 r2.nickname = tor_strdup("Fred");
1070 bw_lines = rep_hist_get_bandwidth_lines();
1071 test_assert(bw_lines);
1072 test_assert(!strcmpstart(bw_lines, "opt write-history "));
1074 test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
1075 &pk1_str_len));
1076 test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
1077 &pk2_str_len));
1078 test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
1079 &pk3_str_len));
1081 memset(buf, 0, 2048);
1082 test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
1084 strcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
1085 "platform Tor "VERSION" on ");
1086 strcat(buf2, get_uname());
1087 strcat(buf2, "\n"
1088 "published 1970-01-01 00:00:00\n"
1089 "opt fingerprint ");
1090 test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
1091 strcat(buf2, fingerprint);
1092 strcat(buf2, "\nopt uptime 0\n"
1093 /* XXX the "0" above is hardcoded, but even if we made it reflect
1094 * uptime, that still wouldn't make it right, because the two
1095 * descriptors might be made on different seconds... hm. */
1096 "bandwidth 1000 5000 10000\n"
1097 "onion-key\n");
1098 strcat(buf2, pk1_str);
1099 strcat(buf2, "signing-key\n");
1100 strcat(buf2, pk2_str);
1101 strcat(buf2, bw_lines);
1102 strcat(buf2, "router-signature\n");
1103 buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same twice*/
1105 test_streq(buf, buf2);
1106 tor_free(bw_lines);
1108 test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
1109 cp = buf;
1110 rp1 = router_parse_entry_from_string((const char*)cp,NULL);
1111 test_assert(rp1);
1112 test_streq(rp1->address, r1.address);
1113 test_eq(rp1->or_port, r1.or_port);
1114 test_eq(rp1->dir_port, r1.dir_port);
1115 test_eq(rp1->bandwidthrate, r1.bandwidthrate);
1116 test_eq(rp1->bandwidthburst, r1.bandwidthburst);
1117 test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
1118 test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
1119 test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
1120 test_assert(rp1->exit_policy == NULL);
1122 #if 0
1123 /* XXX Once we have exit policies, test this again. XXX */
1124 strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
1125 strcat(buf2, pk2_str);
1126 strcat(buf2, "signing-key\n");
1127 strcat(buf2, pk1_str);
1128 strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
1129 test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
1130 test_streq(buf, buf2);
1132 cp = buf;
1133 rp2 = router_parse_entry_from_string(&cp);
1134 test_assert(rp2);
1135 test_streq(rp2->address, r2.address);
1136 test_eq(rp2->or_port, r2.or_port);
1137 test_eq(rp2->dir_port, r2.dir_port);
1138 test_eq(rp2->bandwidth, r2.bandwidth);
1139 test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
1140 test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
1141 test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
1142 test_streq(rp2->exit_policy->string, "accept *:80");
1143 test_streq(rp2->exit_policy->address, "*");
1144 test_streq(rp2->exit_policy->port, "80");
1145 test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
1146 test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
1147 test_streq(rp2->exit_policy->next->address, "18.*");
1148 test_streq(rp2->exit_policy->next->port, "24");
1149 test_assert(rp2->exit_policy->next->next == NULL);
1150 #endif
1152 /* Okay, now for the directories. */
1153 crypto_pk_get_fingerprint(pk2, buf, 1);
1154 add_fingerprint_to_dir("Magri", buf);
1155 crypto_pk_get_fingerprint(pk1, buf, 1);
1156 add_fingerprint_to_dir("Fred", buf);
1157 /* Make sure routers aren't too far in the past any more. */
1158 r1.published_on = time(NULL);
1159 r2.published_on = time(NULL)-3*60*60;
1160 test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
1161 cp = buf;
1162 test_eq(dirserv_add_descriptor((const char**)&cp,&m), 1);
1163 test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
1164 cp = buf;
1165 test_eq(dirserv_add_descriptor((const char**)&cp,&m), 1);
1166 get_options()->Nickname = tor_strdup("DirServer");
1167 test_assert(!dirserv_dump_directory_to_string(&cp,pk3));
1168 test_assert(!router_parse_routerlist_from_directory(cp, &dir1, pk3, 1, 0));
1169 test_eq(2, smartlist_len(dir1->routers));
1170 dirserv_free_fingerprint_list();
1171 tor_free(cp);
1173 tor_free(pk1_str);
1174 tor_free(pk2_str);
1175 if (pk1) crypto_free_pk_env(pk1);
1176 if (pk2) crypto_free_pk_env(pk2);
1177 if (rp1) routerinfo_free(rp1);
1178 if (rp2) routerinfo_free(rp2);
1179 tor_free(dir1); /* XXXX And more !*/
1180 tor_free(dir2); /* And more !*/
1182 /* Try out version parsing functionality */
1183 test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
1184 test_eq(0, ver1.major);
1185 test_eq(3, ver1.minor);
1186 test_eq(4, ver1.micro);
1187 test_eq(VER_PRE, ver1.status);
1188 test_eq(2, ver1.patchlevel);
1189 test_eq(IS_CVS, ver1.cvs);
1190 test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
1191 test_eq(0, ver1.major);
1192 test_eq(3, ver1.minor);
1193 test_eq(4, ver1.micro);
1194 test_eq(VER_RC, ver1.status);
1195 test_eq(1, ver1.patchlevel);
1196 test_eq(IS_NOT_CVS, ver1.cvs);
1197 test_eq(0, tor_version_parse("1.3.4", &ver1));
1198 test_eq(1, ver1.major);
1199 test_eq(3, ver1.minor);
1200 test_eq(4, ver1.micro);
1201 test_eq(VER_RELEASE, ver1.status);
1202 test_eq(0, ver1.patchlevel);
1203 test_eq(IS_NOT_CVS, ver1.cvs);
1204 test_eq(0, tor_version_parse("1.3.4.999", &ver1));
1205 test_eq(1, ver1.major);
1206 test_eq(3, ver1.minor);
1207 test_eq(4, ver1.micro);
1208 test_eq(VER_RELEASE, ver1.status);
1209 test_eq(999, ver1.patchlevel);
1210 test_eq(IS_NOT_CVS, ver1.cvs);
1211 test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
1212 test_eq(0, ver1.major);
1213 test_eq(1, ver1.minor);
1214 test_eq(2, ver1.micro);
1215 test_eq(4, ver1.patchlevel);
1216 test_eq(VER_RELEASE, ver1.status);
1217 test_eq(IS_NOT_CVS, ver1.cvs);
1218 test_streq("alpha", ver1.status_tag);
1219 test_eq(0, tor_version_parse("0.1.2.4", &ver1));
1220 test_eq(0, ver1.major);
1221 test_eq(1, ver1.minor);
1222 test_eq(2, ver1.micro);
1223 test_eq(4, ver1.patchlevel);
1224 test_eq(VER_RELEASE, ver1.status);
1225 test_eq(IS_NOT_CVS, ver1.cvs);
1226 test_streq("", ver1.status_tag);
1228 /* make sure is_obsolete_version() works */
1229 test_eq(1, is_obsolete_version("0.0.1", "Tor 0.0.2"));
1230 test_eq(1, is_obsolete_version("0.0.1", "0.0.2, Tor 0.0.3"));
1231 test_eq(1, is_obsolete_version("0.0.1", "0.0.2,Tor 0.0.3"));
1232 test_eq(1, is_obsolete_version("0.0.1", "0.0.3,BetterTor 0.0.1"));
1233 test_eq(0, is_obsolete_version("0.0.2", "Tor 0.0.2,Tor 0.0.3"));
1234 test_eq(1, is_obsolete_version("0.0.2", "Tor 0.0.2pre1,Tor 0.0.3"));
1235 test_eq(0, is_obsolete_version("0.1.0", "Tor 0.0.2,Tor 0.0.3"));
1236 test_eq(0, is_obsolete_version("0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8"));
1237 test_eq(0, is_obsolete_version("0.0.5", "0.0.5-cvs"));
1238 test_eq(0, is_obsolete_version("0.0.5.1-cvs", "0.0.5"));
1240 test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
1241 test_eq(1, tor_version_as_new_as(
1242 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8rc2"));
1243 test_eq(0, tor_version_as_new_as(
1244 "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
1248 static void
1249 test_rend_fns(void)
1251 char address1[] = "fooaddress.onion";
1252 char address2[] = "aaaaaaaaaaaaaaaa.onion";
1253 char address3[] = "fooaddress.exit";
1254 rend_service_descriptor_t *d1, *d2;
1255 char *encoded;
1256 size_t len;
1257 crypto_pk_env_t *pk1;
1258 time_t now;
1259 pk1 = crypto_new_pk_env();
1261 test_assert(!crypto_pk_generate_key(pk1));
1262 d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
1263 d1->pk = pk1;
1264 now = time(NULL);
1265 d1->timestamp = now;
1266 d1->n_intro_points = 3;
1267 d1->intro_points = tor_malloc(sizeof(char*)*3);
1268 d1->intro_points[0] = tor_strdup("tom");
1269 d1->intro_points[1] = tor_strdup("crow");
1270 d1->intro_points[2] = tor_strdup("joel");
1271 test_assert(! rend_encode_service_descriptor(d1, pk1, &encoded, &len));
1272 d2 = rend_parse_service_descriptor(encoded, len);
1273 test_assert(d2);
1275 test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
1276 test_eq(d2->timestamp, now);
1277 test_eq(d2->n_intro_points, 3);
1278 test_streq(d2->intro_points[0], "tom");
1279 test_streq(d2->intro_points[1], "crow");
1280 test_streq(d2->intro_points[2], "joel");
1282 test_eq(NORMAL_HOSTNAME, parse_extended_hostname(address1));
1283 test_eq(ONION_HOSTNAME, parse_extended_hostname(address2));
1284 test_eq(EXIT_HOSTNAME, parse_extended_hostname(address3));
1286 rend_service_descriptor_free(d1);
1287 rend_service_descriptor_free(d2);
1291 main(int c, char**v) {
1292 or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
1293 network_init();
1294 options_init(options);
1295 set_options(options);
1297 crypto_seed_rng();
1298 setup_directory();
1299 rep_hist_init();
1300 atexit(remove_directory);
1302 // puts("========================== Buffers =========================");
1303 if (0) test_buffers();
1304 puts("\n========================== Crypto ==========================");
1305 // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
1306 test_crypto();
1307 test_crypto_dh();
1308 puts("\n========================= Util ============================");
1309 test_gzip();
1310 test_util();
1311 test_strmap();
1312 puts("\n========================= Onion Skins =====================");
1313 test_onion();
1314 test_onion_handshake();
1315 puts("\n========================= Directory Formats ===============");
1316 test_dir_format();
1317 puts("\n========================= Rendezvous functionality ========");
1318 test_rend_fns();
1319 puts("");
1321 if (have_failed)
1322 return 1;
1323 else
1324 return 0;