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
[] = "";
12 * \brief Unit tests for many pieces of the lower level Tor modules.
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
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.
42 long int lround(double x
);
43 double fabs(double x
);
53 #include <openssl/crypto.h>
56 /** Set to true if any unit test has failed. Mostly, this is set by the macros
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.
70 static int is_setup
= 0;
76 tor_snprintf(temp_dir
, sizeof(temp_dir
),
77 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
80 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
81 r
= mkdir(temp_dir
, 0700);
84 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
91 /** Return a filename relative to our testing temporary directory */
93 get_fname(const char *name
)
95 static char buf
[1024];
97 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
101 /** Remove all files stored under the temporary directory, and the directory
104 remove_directory(void)
106 smartlist_t
*elements
= tor_listdir(temp_dir
);
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
);
116 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
117 smartlist_free(elements
);
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. */
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
]);
143 crypto_pk_env_t
*result
;
145 result
= crypto_new_pk_env();
146 tor_assert(!crypto_pk_generate_key(result
));
151 /** Free all storage used for the cached key optimization. */
153 free_pregenerated_keys(void)
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 */
171 buf_t
*buf
= NULL
, *buf2
= NULL
;
180 if (!(buf
= buf_new()))
183 //test_eq(buf_capacity(buf), 4096);
184 test_eq(buf_datalen(buf
), 0);
187 * General pointer frobbing
189 for (j
=0;j
<256;++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. */
208 write_to_buf(str
, 256, 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);
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);
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);
233 write_to_buf(str
, 32, buf
);
234 //test_eq(buf_capacity(buf), 256);
236 write_to_buf(str
, 256, 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. */
251 buf
= buf_new_with_capacity(33668);
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. */
264 buf
= buf_new_with_capacity(33668);
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);
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. */
283 buf
= buf_new_with_capacity(4096);
284 buf2
= buf_new_with_capacity(4096);
286 write_to_buf(str
, 255, buf
);
287 test_eq(buf_datalen(buf
), 25500);
288 for (j
=0;j
<100;++j
) {
290 move_buf_to_buf(buf2
, buf
, &r
);
293 test_eq(buf_datalen(buf
), 24500);
294 test_eq(buf_datalen(buf2
), 1000);
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
);
302 r
= 30000; /* incomplete move */
303 move_buf_to_buf(buf2
, buf
, &r
);
306 fetch_from_buf(str2
, 255, buf2
);
307 test_memeq(str2
, str
, 255);
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));
339 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
343 s
= open(get_fname("data"), O_RDONLY
, 0);
345 errno
= 0; /* XXXX */
346 i
= read_to_buf(s
, 10, buf
, &eof
);
347 printf("%s\n", strerror(errno
));
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);
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);
369 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
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);
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. */
392 i
= read_to_buf(s
, 1024, buf
, &eof
);
394 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
395 test_eq(buf_datalen(buf
), 256-6-32);
407 /** Run unit tests for the onion handshake code. */
409 test_onion_handshake(void)
412 crypto_dh_env_t
*c_dh
= NULL
;
413 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
417 char s_buf
[ONIONSKIN_REPLY_LEN
];
421 crypto_pk_env_t
*pk
= NULL
;
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
,
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)) {
443 test_memeq(c_keys
, s_keys
, 40);
444 memset(s_buf
, 0, 40);
445 test_memneq(c_keys
, s_buf
, 40);
449 crypto_dh_free(c_dh
);
451 crypto_free_pk_env(pk
);
455 test_circuit_timeout(void)
458 * 1. Generate 1000 samples
459 * 2. Estimate parameters
460 * 3. If difference, repeat
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
;
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)
482 circuit_build_times_initial_alpha(&initial
,
483 CBT_DEFAULT_QUANTILE_CUTOFF
/100.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) {
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
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
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);
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
616 test_policy_summary_helper(const char *policy_str
,
617 const char *expected_summary
)
620 smartlist_t
*policy
= smartlist_create();
621 char *summary
= NULL
;
624 line
.key
= (char*)"foo";
625 line
.value
= (char *)policy_str
;
628 r
= policies_parse_exit_policy(&line
, &policy
, 0, NULL
, 1);
630 summary
= policy_summarize(policy
);
632 test_assert(summary
!= NULL
);
633 test_streq(summary
, expected_summary
);
638 addr_policy_list_free(policy
);
641 /** Run unit tests for generating summary lines of exit policies */
646 smartlist_t
*policy
= NULL
, *policy2
= NULL
, *policy3
= NULL
,
647 *policy4
= NULL
, *policy5
= NULL
, *policy6
= NULL
,
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
);
757 /* make sure compacting logic works. */
759 line
.key
= (char*)"foo";
760 line
.value
= (char*)"accept *:80,reject private:*,reject *:*";
762 test_assert(0 == policies_parse_exit_policy(&line
, &policy
, 0, NULL
, 1));
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:*,"
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,"
797 test_policy_summary_helper("accept 11.0.0.0/9:80,"
801 test_policy_summary_helper("accept *:80,"
806 "accept 80-81,100-111");
808 test_policy_summary_helper("accept *:1,"
814 test_policy_summary_helper("accept *:1,"
820 test_policy_summary_helper("reject *:1,"
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");
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
);
862 SMARTLIST_FOREACH(sm
, char *, s
, tor_free(s
));
867 /** Run AES performance benchmarks. */
873 crypto_cipher_env_t
*c
;
874 struct timeval start
, end
;
875 const int iters
= 100000;
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
);
890 nsec
= (uint64_t) tv_udiff(&start
,&end
);
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. */
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;
911 digestmap_t
*dm
= digestmap_new();
912 digestset_t
*ds
= digestset_new(elts
);
914 for (i
= 0; i
< elts
; ++i
) {
916 smartlist_add(sl
, tor_memdup(d
, 20));
918 for (i
= 0; i
< elts
; ++i
) {
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
) {
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
);
957 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
958 SMARTLIST_FOREACH(sl2
, char *, cp
, tor_free(cp
));
963 /** Test encoding and parsing of rendezvous service descriptors. */
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
;
976 char *intro_points_encrypted
= NULL
;
977 size_t intro_points_size
;
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
);
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
,
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
,
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
;
1063 for (i
= 0; i
< smartlist_len(descs
); i
++)
1064 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1065 smartlist_free(descs
);
1068 rend_service_descriptor_free(parsed
);
1070 rend_service_descriptor_free(generated
);
1072 crypto_free_pk_env(pk1
);
1074 crypto_free_pk_env(pk2
);
1075 tor_free(intro_points_encrypted
);
1078 /** Run unit tests for GeoIP code. */
1083 time_t now
= time(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));
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
);
1124 test_streq("zz=24,ab=16,xy=8", 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
);
1132 test_streq("zz=24,xy=8", s
);
1139 legacy_test_setup(const struct testcase_t
*testcase
)
1141 return testcase
->setup_data
;
1145 legacy_test_helper(void *data
)
1147 void (*fn
)(void) = data
;
1152 legacy_test_cleanup(const struct testcase_t
*testcase
, void *ptr
)
1159 const struct testcase_setup_t legacy_setup
= {
1160 legacy_test_setup
, legacy_test_cleanup
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
[] = {
1173 ENT(onion_handshake
),
1174 ENT(circuit_timeout
),
1179 DISABLED(bench_aes
),
1180 DISABLED(bench_dmap
),
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
[] = {
1192 { "addr/", addr_tests
},
1193 { "crypto/", crypto_tests
},
1194 { "container/", container_tests
},
1195 { "util/", util_tests
},
1196 { "dir/", dir_tests
},
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
;
1208 int loglevel
= LOG_ERR
;
1212 int r
= CRYPTO_set_mem_ex_functions(_tor_malloc
, _tor_realloc
, _tor_free
);
1217 update_approx_time(time(NULL
));
1218 options
= options_new();
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
;
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
);
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
);
1260 atexit(remove_directory
);
1262 have_failed
= (tinytest_main(c
, v
, testgroups
) != 0);
1264 free_pregenerated_keys();
1267 dmalloc_log_unfreed();