Split the circuit timeout and close codepaths.
[tor.git] / src / test / test.c
blobfbcfedbc9a538c606ea7ce566651a49de2ddf55a
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /* Ordinarily defined in tor_main.c; this bit is just here to provide one
7 * since we're not linking to tor_main.c */
8 const char tor_git_revision[] = "";
10 /**
11 * \file test.c
12 * \brief Unit tests for many pieces of the lower level Tor modules.
13 **/
15 #include "orconfig.h"
17 #include <stdio.h>
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
22 #ifdef MS_WINDOWS
23 /* For mkdir() */
24 #include <direct.h>
25 #else
26 #include <dirent.h>
27 #endif
29 /* These macros pull in declarations for some functions and structures that
30 * are typically file-private. */
31 #define BUFFERS_PRIVATE
32 #define CONFIG_PRIVATE
33 #define GEOIP_PRIVATE
34 #define ROUTER_PRIVATE
35 #define CIRCUIT_PRIVATE
38 * Linux doesn't provide lround in math.h by default, but mac os does...
39 * It's best just to leave math.h out of the picture entirely.
41 //#include <math.h>
42 long int lround(double x);
43 double fabs(double x);
45 #include "or.h"
46 #include "test.h"
47 #include "torgzip.h"
48 #include "mempool.h"
49 #include "memarea.h"
51 #ifdef USE_DMALLOC
52 #include <dmalloc.h>
53 #include <openssl/crypto.h>
54 #endif
56 /** Set to true if any unit test has failed. Mostly, this is set by the macros
57 * in test.h */
58 int have_failed = 0;
60 /** Temporary directory (set up by setup_directory) under which we store all
61 * our files during testing. */
62 static char temp_dir[256];
64 /** Select and create the temporary directory we'll use to run our unit tests.
65 * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
66 * idempotent. */
67 static void
68 setup_directory(void)
70 static int is_setup = 0;
71 int r;
72 if (is_setup) return;
74 #ifdef MS_WINDOWS
75 // XXXX
76 tor_snprintf(temp_dir, sizeof(temp_dir),
77 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
78 r = mkdir(temp_dir);
79 #else
80 tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
81 r = mkdir(temp_dir, 0700);
82 #endif
83 if (r) {
84 fprintf(stderr, "Can't create directory %s:", temp_dir);
85 perror("");
86 exit(1);
88 is_setup = 1;
91 /** Return a filename relative to our testing temporary directory */
92 const char *
93 get_fname(const char *name)
95 static char buf[1024];
96 setup_directory();
97 tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
98 return buf;
101 /** Remove all files stored under the temporary directory, and the directory
102 * itself. */
103 static void
104 remove_directory(void)
106 smartlist_t *elements = tor_listdir(temp_dir);
107 if (elements) {
108 SMARTLIST_FOREACH(elements, const char *, cp,
110 size_t len = strlen(cp)+strlen(temp_dir)+16;
111 char *tmp = tor_malloc(len);
112 tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
113 unlink(tmp);
114 tor_free(tmp);
116 SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
117 smartlist_free(elements);
119 rmdir(temp_dir);
122 /** Define this if unit tests spend too much time generating public keys*/
123 #undef CACHE_GENERATED_KEYS
125 static crypto_pk_env_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
126 #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
128 /** Generate and return a new keypair for use in unit tests. If we're using
129 * the key cache optimization, we might reuse keys: we only guarantee that
130 * keys made with distinct values for <b>idx</b> are different. The value of
131 * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
132 crypto_pk_env_t *
133 pk_generate(int idx)
135 #ifdef CACHE_GENERATED_KEYS
136 tor_assert(idx < N_PREGEN_KEYS);
137 if (! pregen_keys[idx]) {
138 pregen_keys[idx] = crypto_new_pk_env();
139 tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
141 return crypto_pk_dup_key(pregen_keys[idx]);
142 #else
143 crypto_pk_env_t *result;
144 (void) idx;
145 result = crypto_new_pk_env();
146 tor_assert(!crypto_pk_generate_key(result));
147 return result;
148 #endif
151 /** Free all storage used for the cached key optimization. */
152 static void
153 free_pregenerated_keys(void)
155 unsigned idx;
156 for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
157 if (pregen_keys[idx]) {
158 crypto_free_pk_env(pregen_keys[idx]);
159 pregen_keys[idx] = NULL;
164 /** Run unit tests for buffers.c */
165 static void
166 test_buffers(void)
168 char str[256];
169 char str2[256];
171 buf_t *buf = NULL, *buf2 = NULL;
172 const char *cp;
174 int j;
175 size_t r;
177 /****
178 * buf_new
179 ****/
180 if (!(buf = buf_new()))
181 test_fail();
183 //test_eq(buf_capacity(buf), 4096);
184 test_eq(buf_datalen(buf), 0);
186 /****
187 * General pointer frobbing
189 for (j=0;j<256;++j) {
190 str[j] = (char)j;
192 write_to_buf(str, 256, buf);
193 write_to_buf(str, 256, buf);
194 test_eq(buf_datalen(buf), 512);
195 fetch_from_buf(str2, 200, buf);
196 test_memeq(str, str2, 200);
197 test_eq(buf_datalen(buf), 312);
198 memset(str2, 0, sizeof(str2));
200 fetch_from_buf(str2, 256, buf);
201 test_memeq(str+200, str2, 56);
202 test_memeq(str, str2+56, 200);
203 test_eq(buf_datalen(buf), 56);
204 memset(str2, 0, sizeof(str2));
205 /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
206 * another 3584 bytes, we hit the end. */
207 for (j=0;j<15;++j) {
208 write_to_buf(str, 256, buf);
210 assert_buf_ok(buf);
211 test_eq(buf_datalen(buf), 3896);
212 fetch_from_buf(str2, 56, buf);
213 test_eq(buf_datalen(buf), 3840);
214 test_memeq(str+200, str2, 56);
215 for (j=0;j<15;++j) {
216 memset(str2, 0, sizeof(str2));
217 fetch_from_buf(str2, 256, buf);
218 test_memeq(str, str2, 256);
220 test_eq(buf_datalen(buf), 0);
221 buf_free(buf);
222 buf = NULL;
224 /* Okay, now make sure growing can work. */
225 buf = buf_new_with_capacity(16);
226 //test_eq(buf_capacity(buf), 16);
227 write_to_buf(str+1, 255, buf);
228 //test_eq(buf_capacity(buf), 256);
229 fetch_from_buf(str2, 254, buf);
230 test_memeq(str+1, str2, 254);
231 //test_eq(buf_capacity(buf), 256);
232 assert_buf_ok(buf);
233 write_to_buf(str, 32, buf);
234 //test_eq(buf_capacity(buf), 256);
235 assert_buf_ok(buf);
236 write_to_buf(str, 256, buf);
237 assert_buf_ok(buf);
238 //test_eq(buf_capacity(buf), 512);
239 test_eq(buf_datalen(buf), 33+256);
240 fetch_from_buf(str2, 33, buf);
241 test_eq(*str2, str[255]);
243 test_memeq(str2+1, str, 32);
244 //test_eq(buf_capacity(buf), 512);
245 test_eq(buf_datalen(buf), 256);
246 fetch_from_buf(str2, 256, buf);
247 test_memeq(str, str2, 256);
249 /* now try shrinking: case 1. */
250 buf_free(buf);
251 buf = buf_new_with_capacity(33668);
252 for (j=0;j<67;++j) {
253 write_to_buf(str,255, buf);
255 //test_eq(buf_capacity(buf), 33668);
256 test_eq(buf_datalen(buf), 17085);
257 for (j=0; j < 40; ++j) {
258 fetch_from_buf(str2, 255,buf);
259 test_memeq(str2, str, 255);
262 /* now try shrinking: case 2. */
263 buf_free(buf);
264 buf = buf_new_with_capacity(33668);
265 for (j=0;j<67;++j) {
266 write_to_buf(str,255, buf);
268 for (j=0; j < 20; ++j) {
269 fetch_from_buf(str2, 255,buf);
270 test_memeq(str2, str, 255);
272 for (j=0;j<80;++j) {
273 write_to_buf(str,255, buf);
275 //test_eq(buf_capacity(buf),33668);
276 for (j=0; j < 120; ++j) {
277 fetch_from_buf(str2, 255,buf);
278 test_memeq(str2, str, 255);
281 /* Move from buf to buf. */
282 buf_free(buf);
283 buf = buf_new_with_capacity(4096);
284 buf2 = buf_new_with_capacity(4096);
285 for (j=0;j<100;++j)
286 write_to_buf(str, 255, buf);
287 test_eq(buf_datalen(buf), 25500);
288 for (j=0;j<100;++j) {
289 r = 10;
290 move_buf_to_buf(buf2, buf, &r);
291 test_eq(r, 0);
293 test_eq(buf_datalen(buf), 24500);
294 test_eq(buf_datalen(buf2), 1000);
295 for (j=0;j<3;++j) {
296 fetch_from_buf(str2, 255, buf2);
297 test_memeq(str2, str, 255);
299 r = 8192; /*big move*/
300 move_buf_to_buf(buf2, buf, &r);
301 test_eq(r, 0);
302 r = 30000; /* incomplete move */
303 move_buf_to_buf(buf2, buf, &r);
304 test_eq(r, 13692);
305 for (j=0;j<97;++j) {
306 fetch_from_buf(str2, 255, buf2);
307 test_memeq(str2, str, 255);
309 buf_free(buf);
310 buf_free(buf2);
311 buf = buf2 = NULL;
313 buf = buf_new_with_capacity(5);
314 cp = "Testing. This is a moderately long Testing string.";
315 for (j = 0; cp[j]; j++)
316 write_to_buf(cp+j, 1, buf);
317 test_eq(0, buf_find_string_offset(buf, "Testing", 7));
318 test_eq(1, buf_find_string_offset(buf, "esting", 6));
319 test_eq(1, buf_find_string_offset(buf, "est", 3));
320 test_eq(39, buf_find_string_offset(buf, "ing str", 7));
321 test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
322 test_eq(32, buf_find_string_offset(buf, "ng ", 3));
323 test_eq(43, buf_find_string_offset(buf, "string.", 7));
324 test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
325 test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
326 test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
327 buf_free(buf);
328 buf = NULL;
330 #if 0
332 int s;
333 int eof;
334 int i;
335 buf_t *buf2;
336 /****
337 * read_to_buf
338 ****/
339 s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
340 write(s, str, 256);
341 close(s);
343 s = open(get_fname("data"), O_RDONLY, 0);
344 eof = 0;
345 errno = 0; /* XXXX */
346 i = read_to_buf(s, 10, buf, &eof);
347 printf("%s\n", strerror(errno));
348 test_eq(i, 10);
349 test_eq(eof, 0);
350 //test_eq(buf_capacity(buf), 4096);
351 test_eq(buf_datalen(buf), 10);
353 test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
355 /* Test reading 0 bytes. */
356 i = read_to_buf(s, 0, buf, &eof);
357 //test_eq(buf_capacity(buf), 512*1024);
358 test_eq(buf_datalen(buf), 10);
359 test_eq(eof, 0);
360 test_eq(i, 0);
362 /* Now test when buffer is filled exactly. */
363 buf2 = buf_new_with_capacity(6);
364 i = read_to_buf(s, 6, buf2, &eof);
365 //test_eq(buf_capacity(buf2), 6);
366 test_eq(buf_datalen(buf2), 6);
367 test_eq(eof, 0);
368 test_eq(i, 6);
369 test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
370 buf_free(buf2);
371 buf2 = NULL;
373 /* Now test when buffer is filled with more data to read. */
374 buf2 = buf_new_with_capacity(32);
375 i = read_to_buf(s, 128, buf2, &eof);
376 //test_eq(buf_capacity(buf2), 128);
377 test_eq(buf_datalen(buf2), 32);
378 test_eq(eof, 0);
379 test_eq(i, 32);
380 buf_free(buf2);
381 buf2 = NULL;
383 /* Now read to eof. */
384 test_assert(buf_capacity(buf) > 256);
385 i = read_to_buf(s, 1024, buf, &eof);
386 test_eq(i, (256-32-10-6));
387 test_eq(buf_capacity(buf), MAX_BUF_SIZE);
388 test_eq(buf_datalen(buf), 256-6-32);
389 test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
390 test_eq(eof, 0);
392 i = read_to_buf(s, 1024, buf, &eof);
393 test_eq(i, 0);
394 test_eq(buf_capacity(buf), MAX_BUF_SIZE);
395 test_eq(buf_datalen(buf), 256-6-32);
396 test_eq(eof, 1);
398 #endif
400 done:
401 if (buf)
402 buf_free(buf);
403 if (buf2)
404 buf_free(buf2);
407 /** Run unit tests for the onion handshake code. */
408 static void
409 test_onion_handshake(void)
411 /* client-side */
412 crypto_dh_env_t *c_dh = NULL;
413 char c_buf[ONIONSKIN_CHALLENGE_LEN];
414 char c_keys[40];
416 /* server-side */
417 char s_buf[ONIONSKIN_REPLY_LEN];
418 char s_keys[40];
420 /* shared */
421 crypto_pk_env_t *pk = NULL;
423 pk = pk_generate(0);
425 /* client handshake 1. */
426 memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
427 test_assert(! onion_skin_create(pk, &c_dh, c_buf));
429 /* server handshake */
430 memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
431 memset(s_keys, 0, 40);
432 test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
433 s_buf, s_keys, 40));
435 /* client handshake 2 */
436 memset(c_keys, 0, 40);
437 test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
439 if (memcmp(c_keys, s_keys, 40)) {
440 puts("Aiiiie");
441 exit(1);
443 test_memeq(c_keys, s_keys, 40);
444 memset(s_buf, 0, 40);
445 test_memneq(c_keys, s_buf, 40);
447 done:
448 if (c_dh)
449 crypto_dh_free(c_dh);
450 if (pk)
451 crypto_free_pk_env(pk);
454 static void
455 test_circuit_timeout(void)
457 /* Plan:
458 * 1. Generate 1000 samples
459 * 2. Estimate parameters
460 * 3. If difference, repeat
461 * 4. Save state
462 * 5. load state
463 * 6. Estimate parameters
464 * 7. compare differences
466 circuit_build_times_t initial;
467 circuit_build_times_t estimate;
468 circuit_build_times_t final;
469 double timeout1, timeout2;
470 or_state_t state;
471 char *msg;
472 int i, runs;
473 circuit_build_times_init(&initial);
474 circuit_build_times_init(&estimate);
475 circuit_build_times_init(&final);
477 memset(&state, 0, sizeof(or_state_t));
479 circuitbuild_running_unit_tests();
480 #define timeout0 (build_time_t)(30*1000.0)
481 initial.Xm = 750;
482 circuit_build_times_initial_alpha(&initial,
483 CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
484 timeout0);
485 do {
486 int n = 0;
487 for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
488 if (circuit_build_times_add_time(&estimate,
489 circuit_build_times_generate_sample(&initial, 0, 1)) == 0) {
490 n++;
493 circuit_build_times_update_alpha(&estimate);
494 timeout1 = circuit_build_times_calculate_timeout(&estimate,
495 CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
496 circuit_build_times_set_timeout(&estimate);
497 log_warn(LD_CIRC, "Timeout1 is %lf, Xm is %d", timeout1, estimate.Xm);
498 } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
499 circuit_build_times_cdf(&initial, timeout1)) > 0.02
500 /* 2% error */
501 && estimate.total_build_times < CBT_NCIRCUITS_TO_OBSERVE);
503 test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
505 circuit_build_times_update_state(&estimate, &state);
506 test_assert(circuit_build_times_parse_state(&final, &state, &msg) == 0);
508 circuit_build_times_update_alpha(&final);
509 timeout2 = circuit_build_times_calculate_timeout(&final,
510 CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
512 circuit_build_times_set_timeout(&final);
513 log_warn(LD_CIRC, "Timeout2 is %lf, Xm is %d", timeout2, final.Xm);
515 /* 5% here because some accuracy is lost due to histogram conversion */
516 test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
517 circuit_build_times_cdf(&initial, timeout2)) < 0.05);
519 for (runs = 0; runs < 50; runs++) {
520 int build_times_idx = 0;
521 int total_build_times = 0;
523 final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
524 estimate.close_ms = estimate.timeout_ms
525 = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
527 for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
528 circuit_build_times_network_circ_success(&estimate);
529 circuit_build_times_add_time(&estimate,
530 circuit_build_times_generate_sample(&estimate, 0,
531 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
533 circuit_build_times_network_circ_success(&estimate);
534 circuit_build_times_add_time(&final,
535 circuit_build_times_generate_sample(&final, 0,
536 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
539 test_assert(!circuit_build_times_network_check_changed(&estimate));
540 test_assert(!circuit_build_times_network_check_changed(&final));
542 /* Reset liveness to be non-live */
543 final.liveness.network_last_live = 0;
544 estimate.liveness.network_last_live = 0;
546 build_times_idx = estimate.build_times_idx;
547 total_build_times = estimate.total_build_times;
548 for (i = 0; i < CBT_NETWORK_NONLIVE_TIMEOUT_COUNT; i++) {
549 test_assert(circuit_build_times_network_check_live(&estimate));
550 test_assert(circuit_build_times_network_check_live(&final));
552 circuit_build_times_count_close(&estimate, 0,
553 (time_t)(approx_time()-estimate.close_ms/1000.0-1));
554 circuit_build_times_count_close(&final, 0,
555 (time_t)(approx_time()-final.close_ms/1000.0-1));
558 test_assert(!circuit_build_times_network_check_live(&estimate));
559 test_assert(!circuit_build_times_network_check_live(&final));
561 for ( ; i < CBT_NETWORK_NONLIVE_DISCARD_COUNT; i++) {
562 circuit_build_times_count_close(&estimate, 0,
563 (time_t)(approx_time()-estimate.close_ms/1000.0-1));
565 if (i < CBT_NETWORK_NONLIVE_DISCARD_COUNT-1) {
566 circuit_build_times_count_close(&final, 0,
567 (time_t)(approx_time()-final.close_ms/1000.0-1));
571 test_assert(!circuit_build_times_network_check_live(&estimate));
572 test_assert(!circuit_build_times_network_check_live(&final));
574 log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
575 build_times_idx, estimate.build_times_idx,
576 total_build_times, estimate.total_build_times);
578 /* Check rollback index. Should match top of loop. */
579 test_assert(build_times_idx == estimate.build_times_idx);
580 // This can fail if estimate.total_build_times == 1000, because
581 // in that case, rewind actually causes us to lose timeouts
582 if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
583 test_assert(total_build_times == estimate.total_build_times);
585 /* Now simulate that the network has become live and we need
586 * a change */
587 circuit_build_times_network_is_live(&estimate);
588 circuit_build_times_network_is_live(&final);
590 for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
591 circuit_build_times_count_timeout(&estimate, 1);
593 if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
594 circuit_build_times_count_timeout(&final, 1);
598 test_assert(estimate.liveness.after_firsthop_idx == 0);
599 test_assert(final.liveness.after_firsthop_idx ==
600 CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
602 test_assert(circuit_build_times_network_check_live(&estimate));
603 test_assert(circuit_build_times_network_check_live(&final));
605 circuit_build_times_count_timeout(&final, 1);
608 done:
609 return;
612 /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
613 * that policies_summarize() produces the string <b>expected_summary</b> from
614 * it. */
615 static void
616 test_policy_summary_helper(const char *policy_str,
617 const char *expected_summary)
619 config_line_t line;
620 smartlist_t *policy = smartlist_create();
621 char *summary = NULL;
622 int r;
624 line.key = (char*)"foo";
625 line.value = (char *)policy_str;
626 line.next = NULL;
628 r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
629 test_eq(r, 0);
630 summary = policy_summarize(policy);
632 test_assert(summary != NULL);
633 test_streq(summary, expected_summary);
635 done:
636 tor_free(summary);
637 if (policy)
638 addr_policy_list_free(policy);
641 /** Run unit tests for generating summary lines of exit policies */
642 static void
643 test_policies(void)
645 int i;
646 smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
647 *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
648 *policy7 = NULL;
649 addr_policy_t *p;
650 tor_addr_t tar;
651 config_line_t line;
652 smartlist_t *sm = NULL;
653 char *policy_str = NULL;
655 policy = smartlist_create();
657 p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
658 test_assert(p != NULL);
659 test_eq(ADDR_POLICY_REJECT, p->policy_type);
660 tor_addr_from_ipv4h(&tar, 0xc0a80000u);
661 test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
662 test_eq(16, p->maskbits);
663 test_eq(1, p->prt_min);
664 test_eq(65535, p->prt_max);
666 smartlist_add(policy, p);
668 test_assert(ADDR_POLICY_ACCEPTED ==
669 compare_addr_to_addr_policy(0x01020304u, 2, policy));
670 test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
671 compare_addr_to_addr_policy(0, 2, policy));
672 test_assert(ADDR_POLICY_REJECTED ==
673 compare_addr_to_addr_policy(0xc0a80102, 2, policy));
675 test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
676 test_assert(policy2);
678 policy3 = smartlist_create();
679 p = router_parse_addr_policy_item_from_string("reject *:*",-1);
680 test_assert(p != NULL);
681 smartlist_add(policy3, p);
682 p = router_parse_addr_policy_item_from_string("accept *:*",-1);
683 test_assert(p != NULL);
684 smartlist_add(policy3, p);
686 policy4 = smartlist_create();
687 p = router_parse_addr_policy_item_from_string("accept *:443",-1);
688 test_assert(p != NULL);
689 smartlist_add(policy4, p);
690 p = router_parse_addr_policy_item_from_string("accept *:443",-1);
691 test_assert(p != NULL);
692 smartlist_add(policy4, p);
694 policy5 = smartlist_create();
695 p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
696 test_assert(p != NULL);
697 smartlist_add(policy5, p);
698 p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
699 test_assert(p != NULL);
700 smartlist_add(policy5, p);
701 p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
702 test_assert(p != NULL);
703 smartlist_add(policy5, p);
704 p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
705 test_assert(p != NULL);
706 smartlist_add(policy5, p);
707 p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
708 test_assert(p != NULL);
709 smartlist_add(policy5, p);
710 p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
711 test_assert(p != NULL);
712 smartlist_add(policy5, p);
713 p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
714 test_assert(p != NULL);
715 smartlist_add(policy5, p);
716 p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
717 test_assert(p != NULL);
718 smartlist_add(policy5, p);
719 p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
720 test_assert(p != NULL);
721 smartlist_add(policy5, p);
722 p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
723 test_assert(p != NULL);
724 smartlist_add(policy5, p);
726 policy6 = smartlist_create();
727 p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
728 test_assert(p != NULL);
729 smartlist_add(policy6, p);
731 policy7 = smartlist_create();
732 p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
733 test_assert(p != NULL);
734 smartlist_add(policy7, p);
736 test_assert(!exit_policy_is_general_exit(policy));
737 test_assert(exit_policy_is_general_exit(policy2));
738 test_assert(!exit_policy_is_general_exit(NULL));
739 test_assert(!exit_policy_is_general_exit(policy3));
740 test_assert(!exit_policy_is_general_exit(policy4));
741 test_assert(!exit_policy_is_general_exit(policy5));
742 test_assert(!exit_policy_is_general_exit(policy6));
743 test_assert(!exit_policy_is_general_exit(policy7));
745 test_assert(cmp_addr_policies(policy, policy2));
746 test_assert(cmp_addr_policies(policy, NULL));
747 test_assert(!cmp_addr_policies(policy2, policy2));
748 test_assert(!cmp_addr_policies(NULL, NULL));
750 test_assert(!policy_is_reject_star(policy2));
751 test_assert(policy_is_reject_star(policy));
752 test_assert(policy_is_reject_star(NULL));
754 addr_policy_list_free(policy);
755 policy = NULL;
757 /* make sure compacting logic works. */
758 policy = NULL;
759 line.key = (char*)"foo";
760 line.value = (char*)"accept *:80,reject private:*,reject *:*";
761 line.next = NULL;
762 test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
763 test_assert(policy);
764 //test_streq(policy->string, "accept *:80");
765 //test_streq(policy->next->string, "reject *:*");
766 test_eq(smartlist_len(policy), 2);
768 /* test policy summaries */
769 /* check if we properly ignore private IP addresses */
770 test_policy_summary_helper("reject 192.168.0.0/16:*,"
771 "reject 0.0.0.0/8:*,"
772 "reject 10.0.0.0/8:*,"
773 "accept *:10-30,"
774 "accept *:90,"
775 "reject *:*",
776 "accept 10-30,90");
777 /* check all accept policies, and proper counting of rejects */
778 test_policy_summary_helper("reject 11.0.0.0/9:80,"
779 "reject 12.0.0.0/9:80,"
780 "reject 13.0.0.0/9:80,"
781 "reject 14.0.0.0/9:80,"
782 "accept *:*", "accept 1-65535");
783 test_policy_summary_helper("reject 11.0.0.0/9:80,"
784 "reject 12.0.0.0/9:80,"
785 "reject 13.0.0.0/9:80,"
786 "reject 14.0.0.0/9:80,"
787 "reject 15.0.0.0:81,"
788 "accept *:*", "accept 1-65535");
789 test_policy_summary_helper("reject 11.0.0.0/9:80,"
790 "reject 12.0.0.0/9:80,"
791 "reject 13.0.0.0/9:80,"
792 "reject 14.0.0.0/9:80,"
793 "reject 15.0.0.0:80,"
794 "accept *:*",
795 "reject 80");
796 /* no exits */
797 test_policy_summary_helper("accept 11.0.0.0/9:80,"
798 "reject *:*",
799 "reject 1-65535");
800 /* port merging */
801 test_policy_summary_helper("accept *:80,"
802 "accept *:81,"
803 "accept *:100-110,"
804 "accept *:111,"
805 "reject *:*",
806 "accept 80-81,100-111");
807 /* border ports */
808 test_policy_summary_helper("accept *:1,"
809 "accept *:3,"
810 "accept *:65535,"
811 "reject *:*",
812 "accept 1,3,65535");
813 /* holes */
814 test_policy_summary_helper("accept *:1,"
815 "accept *:3,"
816 "accept *:5,"
817 "accept *:7,"
818 "reject *:*",
819 "accept 1,3,5,7");
820 test_policy_summary_helper("reject *:1,"
821 "reject *:3,"
822 "reject *:5,"
823 "reject *:7,"
824 "accept *:*",
825 "reject 1,3,5,7");
827 /* truncation ports */
828 sm = smartlist_create();
829 for (i=1; i<2000; i+=2) {
830 char buf[POLICY_BUF_LEN];
831 tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
832 smartlist_add(sm, tor_strdup(buf));
834 smartlist_add(sm, tor_strdup("accept *:*"));
835 policy_str = smartlist_join_strings(sm, ",", 0, NULL);
836 test_policy_summary_helper( policy_str,
837 "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
838 "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
839 "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
840 "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
841 "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
842 "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
843 "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
844 "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
845 "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
846 "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
847 "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
848 "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
849 "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
850 "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
852 done:
853 addr_policy_list_free(policy);
854 addr_policy_list_free(policy2);
855 addr_policy_list_free(policy3);
856 addr_policy_list_free(policy4);
857 addr_policy_list_free(policy5);
858 addr_policy_list_free(policy6);
859 addr_policy_list_free(policy7);
860 tor_free(policy_str);
861 if (sm) {
862 SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
863 smartlist_free(sm);
867 /** Run AES performance benchmarks. */
868 static void
869 bench_aes(void)
871 int len, i;
872 char *b1, *b2;
873 crypto_cipher_env_t *c;
874 struct timeval start, end;
875 const int iters = 100000;
876 uint64_t nsec;
877 c = crypto_new_cipher_env();
878 crypto_cipher_generate_key(c);
879 crypto_cipher_encrypt_init_cipher(c);
880 for (len = 1; len <= 8192; len *= 2) {
881 b1 = tor_malloc_zero(len);
882 b2 = tor_malloc_zero(len);
883 tor_gettimeofday(&start);
884 for (i = 0; i < iters; ++i) {
885 crypto_cipher_encrypt(c, b1, b2, len);
887 tor_gettimeofday(&end);
888 tor_free(b1);
889 tor_free(b2);
890 nsec = (uint64_t) tv_udiff(&start,&end);
891 nsec *= 1000;
892 nsec /= (iters*len);
893 printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
894 U64_PRINTF_ARG(nsec));
896 crypto_free_cipher_env(c);
899 /** Run digestmap_t performance benchmarks. */
900 static void
901 bench_dmap(void)
903 smartlist_t *sl = smartlist_create();
904 smartlist_t *sl2 = smartlist_create();
905 struct timeval start, end, pt2, pt3, pt4;
906 const int iters = 10000;
907 const int elts = 4000;
908 const int fpostests = 1000000;
909 char d[20];
910 int i,n=0, fp = 0;
911 digestmap_t *dm = digestmap_new();
912 digestset_t *ds = digestset_new(elts);
914 for (i = 0; i < elts; ++i) {
915 crypto_rand(d, 20);
916 smartlist_add(sl, tor_memdup(d, 20));
918 for (i = 0; i < elts; ++i) {
919 crypto_rand(d, 20);
920 smartlist_add(sl2, tor_memdup(d, 20));
922 printf("nbits=%d\n", ds->mask+1);
924 tor_gettimeofday(&start);
925 for (i = 0; i < iters; ++i) {
926 SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
928 tor_gettimeofday(&pt2);
929 for (i = 0; i < iters; ++i) {
930 SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
931 SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
933 tor_gettimeofday(&pt3);
934 for (i = 0; i < iters; ++i) {
935 SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
937 tor_gettimeofday(&pt4);
938 for (i = 0; i < iters; ++i) {
939 SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
940 SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
942 tor_gettimeofday(&end);
944 for (i = 0; i < fpostests; ++i) {
945 crypto_rand(d, 20);
946 if (digestset_isin(ds, d)) ++fp;
949 printf("%ld\n",(unsigned long)tv_udiff(&start, &pt2));
950 printf("%ld\n",(unsigned long)tv_udiff(&pt2, &pt3));
951 printf("%ld\n",(unsigned long)tv_udiff(&pt3, &pt4));
952 printf("%ld\n",(unsigned long)tv_udiff(&pt4, &end));
953 printf("-- %d\n", n);
954 printf("++ %f\n", fp/(double)fpostests);
955 digestmap_free(dm, NULL);
956 digestset_free(ds);
957 SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
958 SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
959 smartlist_free(sl);
960 smartlist_free(sl2);
963 /** Test encoding and parsing of rendezvous service descriptors. */
964 static void
965 test_rend_fns(void)
967 rend_service_descriptor_t *generated = NULL, *parsed = NULL;
968 char service_id[DIGEST_LEN];
969 char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
970 const char *next_desc;
971 smartlist_t *descs = smartlist_create();
972 char computed_desc_id[DIGEST_LEN];
973 char parsed_desc_id[DIGEST_LEN];
974 crypto_pk_env_t *pk1 = NULL, *pk2 = NULL;
975 time_t now;
976 char *intro_points_encrypted = NULL;
977 size_t intro_points_size;
978 size_t encoded_size;
979 int i;
980 char address1[] = "fooaddress.onion";
981 char address2[] = "aaaaaaaaaaaaaaaa.onion";
982 char address3[] = "fooaddress.exit";
983 char address4[] = "www.torproject.org";
985 test_assert(BAD_HOSTNAME == parse_extended_hostname(address1, 1));
986 test_assert(ONION_HOSTNAME == parse_extended_hostname(address2, 1));
987 test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3, 1));
988 test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4, 1));
990 pk1 = pk_generate(0);
991 pk2 = pk_generate(1);
992 generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
993 generated->pk = crypto_pk_dup_key(pk1);
994 crypto_pk_get_digest(generated->pk, service_id);
995 base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
996 service_id, REND_SERVICE_ID_LEN);
997 now = time(NULL);
998 generated->timestamp = now;
999 generated->version = 2;
1000 generated->protocols = 42;
1001 generated->intro_nodes = smartlist_create();
1003 for (i = 0; i < 3; i++) {
1004 rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
1005 crypto_pk_env_t *okey = pk_generate(2 + i);
1006 intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
1007 intro->extend_info->onion_key = okey;
1008 crypto_pk_get_digest(intro->extend_info->onion_key,
1009 intro->extend_info->identity_digest);
1010 //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
1011 intro->extend_info->nickname[0] = '$';
1012 base16_encode(intro->extend_info->nickname + 1,
1013 sizeof(intro->extend_info->nickname) - 1,
1014 intro->extend_info->identity_digest, DIGEST_LEN);
1015 /* Does not cover all IP addresses. */
1016 tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
1017 intro->extend_info->port = crypto_rand_int(65536);
1018 intro->intro_key = crypto_pk_dup_key(pk2);
1019 smartlist_add(generated->intro_nodes, intro);
1021 test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
1022 REND_NO_AUTH, NULL, NULL) > 0);
1023 test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
1024 NULL, now, 0) == 0);
1025 test_memeq(((rend_encoded_v2_service_descriptor_t *)
1026 smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
1027 test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
1028 &intro_points_encrypted,
1029 &intro_points_size,
1030 &encoded_size,
1031 &next_desc,
1032 ((rend_encoded_v2_service_descriptor_t *)
1033 smartlist_get(descs, 0))->desc_str) == 0);
1034 test_assert(parsed);
1035 test_memeq(((rend_encoded_v2_service_descriptor_t *)
1036 smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
1037 test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
1038 intro_points_size), 3);
1039 test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
1040 test_eq(parsed->timestamp, now);
1041 test_eq(parsed->version, 2);
1042 test_eq(parsed->protocols, 42);
1043 test_eq(smartlist_len(parsed->intro_nodes), 3);
1044 for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
1045 rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
1046 *gen_intro = smartlist_get(generated->intro_nodes, i);
1047 extend_info_t *par_info = par_intro->extend_info;
1048 extend_info_t *gen_info = gen_intro->extend_info;
1049 test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
1050 test_memeq(gen_info->identity_digest, par_info->identity_digest,
1051 DIGEST_LEN);
1052 test_streq(gen_info->nickname, par_info->nickname);
1053 test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
1054 test_eq(gen_info->port, par_info->port);
1057 rend_service_descriptor_free(parsed);
1058 rend_service_descriptor_free(generated);
1059 parsed = generated = NULL;
1061 done:
1062 if (descs) {
1063 for (i = 0; i < smartlist_len(descs); i++)
1064 rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
1065 smartlist_free(descs);
1067 if (parsed)
1068 rend_service_descriptor_free(parsed);
1069 if (generated)
1070 rend_service_descriptor_free(generated);
1071 if (pk1)
1072 crypto_free_pk_env(pk1);
1073 if (pk2)
1074 crypto_free_pk_env(pk2);
1075 tor_free(intro_points_encrypted);
1078 /** Run unit tests for GeoIP code. */
1079 static void
1080 test_geoip(void)
1082 int i, j;
1083 time_t now = time(NULL);
1084 char *s = NULL;
1086 /* Populate the DB a bit. Add these in order, since we can't do the final
1087 * 'sort' step. These aren't very good IP addresses, but they're perfectly
1088 * fine uint32_t values. */
1089 test_eq(0, geoip_parse_entry("10,50,AB"));
1090 test_eq(0, geoip_parse_entry("52,90,XY"));
1091 test_eq(0, geoip_parse_entry("95,100,AB"));
1092 test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
1093 test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
1094 test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
1096 /* We should have 3 countries: ab, xy, zz. */
1097 test_eq(3, geoip_get_n_countries());
1098 /* Make sure that country ID actually works. */
1099 #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
1100 test_streq("ab", NAMEFOR(32));
1101 test_streq("??", NAMEFOR(5));
1102 test_streq("??", NAMEFOR(51));
1103 test_streq("xy", NAMEFOR(150));
1104 test_streq("xy", NAMEFOR(190));
1105 test_streq("??", NAMEFOR(2000));
1106 #undef NAMEFOR
1108 get_options()->BridgeRelay = 1;
1109 get_options()->BridgeRecordUsageByCountry = 1;
1110 /* Put 9 observations in AB... */
1111 for (i=32; i < 40; ++i)
1112 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-7200);
1113 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, 225, now-7200);
1114 /* and 3 observations in XY, several times. */
1115 for (j=0; j < 10; ++j)
1116 for (i=52; i < 55; ++i)
1117 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-3600);
1118 /* and 17 observations in ZZ... */
1119 for (i=110; i < 127; ++i)
1120 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now);
1121 s = geoip_get_client_history_bridge(now+5*24*60*60,
1122 GEOIP_CLIENT_CONNECT);
1123 test_assert(s);
1124 test_streq("zz=24,ab=16,xy=8", s);
1125 tor_free(s);
1127 /* Now clear out all the AB observations. */
1128 geoip_remove_old_clients(now-6000);
1129 s = geoip_get_client_history_bridge(now+5*24*60*60,
1130 GEOIP_CLIENT_CONNECT);
1131 test_assert(s);
1132 test_streq("zz=24,xy=8", s);
1134 done:
1135 tor_free(s);
1138 static void *
1139 legacy_test_setup(const struct testcase_t *testcase)
1141 return testcase->setup_data;
1144 void
1145 legacy_test_helper(void *data)
1147 void (*fn)(void) = data;
1148 fn();
1151 static int
1152 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
1154 (void)ptr;
1155 (void)testcase;
1156 return 1;
1159 const struct testcase_setup_t legacy_setup = {
1160 legacy_test_setup, legacy_test_cleanup
1163 #define ENT(name) \
1164 { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
1165 #define SUBENT(group, name) \
1166 { #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
1167 test_ ## group ## _ ## name }
1168 #define DISABLED(name) \
1169 { #name, legacy_test_helper, TT_SKIP, &legacy_setup, name }
1171 static struct testcase_t test_array[] = {
1172 ENT(buffers),
1173 ENT(onion_handshake),
1174 ENT(circuit_timeout),
1175 ENT(policies),
1176 ENT(rend_fns),
1177 ENT(geoip),
1179 DISABLED(bench_aes),
1180 DISABLED(bench_dmap),
1181 END_OF_TESTCASES
1184 extern struct testcase_t addr_tests[];
1185 extern struct testcase_t crypto_tests[];
1186 extern struct testcase_t container_tests[];
1187 extern struct testcase_t util_tests[];
1188 extern struct testcase_t dir_tests[];
1190 static struct testgroup_t testgroups[] = {
1191 { "", test_array },
1192 { "addr/", addr_tests },
1193 { "crypto/", crypto_tests },
1194 { "container/", container_tests },
1195 { "util/", util_tests },
1196 { "dir/", dir_tests },
1197 END_OF_GROUPS
1200 /** Main entry point for unit test code: parse the command line, and run
1201 * some unit tests. */
1203 main(int c, const char **v)
1205 or_options_t *options;
1206 char *errmsg = NULL;
1207 int i, i_out;
1208 int loglevel = LOG_ERR;
1210 #ifdef USE_DMALLOC
1212 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
1213 tor_assert(r);
1215 #endif
1217 update_approx_time(time(NULL));
1218 options = options_new();
1219 tor_threads_init();
1220 init_logging();
1222 for (i_out = i = 1; i < c; ++i) {
1223 if (!strcmp(v[i], "--warn")) {
1224 loglevel = LOG_WARN;
1225 } else if (!strcmp(v[i], "--notice")) {
1226 loglevel = LOG_NOTICE;
1227 } else if (!strcmp(v[i], "--info")) {
1228 loglevel = LOG_INFO;
1229 } else if (!strcmp(v[i], "--debug")) {
1230 loglevel = LOG_DEBUG;
1231 } else {
1232 v[i_out++] = v[i];
1235 c = i_out;
1238 log_severity_list_t s;
1239 memset(&s, 0, sizeof(s));
1240 set_log_severity_config(loglevel, LOG_ERR, &s);
1241 add_stream_log(&s, "", fileno(stdout));
1244 options->command = CMD_RUN_UNITTESTS;
1245 crypto_global_init(0, NULL, NULL);
1246 rep_hist_init();
1247 network_init();
1248 setup_directory();
1249 options_init(options);
1250 options->DataDirectory = tor_strdup(temp_dir);
1251 options->EntryStatistics = 1;
1252 if (set_options(options, &errmsg) < 0) {
1253 printf("Failed to set initial options: %s\n", errmsg);
1254 tor_free(errmsg);
1255 return 1;
1258 crypto_seed_rng(1);
1260 atexit(remove_directory);
1262 have_failed = (tinytest_main(c, v, testgroups) != 0);
1264 free_pregenerated_keys();
1265 #ifdef USE_DMALLOC
1266 tor_free_all(0);
1267 dmalloc_log_unfreed();
1268 #endif
1270 if (have_failed)
1271 return 1;
1272 else
1273 return 0;