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