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
);
47 #include "circuitbuild.h"
49 #include "connection_edge.h"
51 #include "rendcommon.h"
59 #include "routerparse.h"
63 #include <openssl/crypto.h>
66 /** Set to true if any unit test has failed. Mostly, this is set by the macros
70 /** Temporary directory (set up by setup_directory) under which we store all
71 * our files during testing. */
72 static char temp_dir
[256];
74 /** Select and create the temporary directory we'll use to run our unit tests.
75 * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
80 static int is_setup
= 0;
86 tor_snprintf(temp_dir
, sizeof(temp_dir
),
87 "c:\\windows\\temp\\tor_test_%d", (int)getpid());
90 tor_snprintf(temp_dir
, sizeof(temp_dir
), "/tmp/tor_test_%d", (int) getpid());
91 r
= mkdir(temp_dir
, 0700);
94 fprintf(stderr
, "Can't create directory %s:", temp_dir
);
101 /** Return a filename relative to our testing temporary directory */
103 get_fname(const char *name
)
105 static char buf
[1024];
107 tor_snprintf(buf
,sizeof(buf
),"%s/%s",temp_dir
,name
);
111 /** Remove all files stored under the temporary directory, and the directory
114 remove_directory(void)
116 smartlist_t
*elements
= tor_listdir(temp_dir
);
118 SMARTLIST_FOREACH(elements
, const char *, cp
,
120 size_t len
= strlen(cp
)+strlen(temp_dir
)+16;
121 char *tmp
= tor_malloc(len
);
122 tor_snprintf(tmp
, len
, "%s"PATH_SEPARATOR
"%s", temp_dir
, cp
);
126 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
127 smartlist_free(elements
);
132 /** Define this if unit tests spend too much time generating public keys*/
133 #undef CACHE_GENERATED_KEYS
135 static crypto_pk_env_t
*pregen_keys
[5] = {NULL
, NULL
, NULL
, NULL
, NULL
};
136 #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
138 /** Generate and return a new keypair for use in unit tests. If we're using
139 * the key cache optimization, we might reuse keys: we only guarantee that
140 * keys made with distinct values for <b>idx</b> are different. The value of
141 * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
145 #ifdef CACHE_GENERATED_KEYS
146 tor_assert(idx
< N_PREGEN_KEYS
);
147 if (! pregen_keys
[idx
]) {
148 pregen_keys
[idx
] = crypto_new_pk_env();
149 tor_assert(!crypto_pk_generate_key(pregen_keys
[idx
]));
151 return crypto_pk_dup_key(pregen_keys
[idx
]);
153 crypto_pk_env_t
*result
;
155 result
= crypto_new_pk_env();
156 tor_assert(!crypto_pk_generate_key(result
));
161 /** Free all storage used for the cached key optimization. */
163 free_pregenerated_keys(void)
166 for (idx
= 0; idx
< N_PREGEN_KEYS
; ++idx
) {
167 if (pregen_keys
[idx
]) {
168 crypto_free_pk_env(pregen_keys
[idx
]);
169 pregen_keys
[idx
] = NULL
;
174 /** Run unit tests for buffers.c */
181 buf_t
*buf
= NULL
, *buf2
= NULL
;
190 if (!(buf
= buf_new()))
193 //test_eq(buf_capacity(buf), 4096);
194 test_eq(buf_datalen(buf
), 0);
197 * General pointer frobbing
199 for (j
=0;j
<256;++j
) {
202 write_to_buf(str
, 256, buf
);
203 write_to_buf(str
, 256, buf
);
204 test_eq(buf_datalen(buf
), 512);
205 fetch_from_buf(str2
, 200, buf
);
206 test_memeq(str
, str2
, 200);
207 test_eq(buf_datalen(buf
), 312);
208 memset(str2
, 0, sizeof(str2
));
210 fetch_from_buf(str2
, 256, buf
);
211 test_memeq(str
+200, str2
, 56);
212 test_memeq(str
, str2
+56, 200);
213 test_eq(buf_datalen(buf
), 56);
214 memset(str2
, 0, sizeof(str2
));
215 /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
216 * another 3584 bytes, we hit the end. */
218 write_to_buf(str
, 256, buf
);
221 test_eq(buf_datalen(buf
), 3896);
222 fetch_from_buf(str2
, 56, buf
);
223 test_eq(buf_datalen(buf
), 3840);
224 test_memeq(str
+200, str2
, 56);
226 memset(str2
, 0, sizeof(str2
));
227 fetch_from_buf(str2
, 256, buf
);
228 test_memeq(str
, str2
, 256);
230 test_eq(buf_datalen(buf
), 0);
234 /* Okay, now make sure growing can work. */
235 buf
= buf_new_with_capacity(16);
236 //test_eq(buf_capacity(buf), 16);
237 write_to_buf(str
+1, 255, buf
);
238 //test_eq(buf_capacity(buf), 256);
239 fetch_from_buf(str2
, 254, buf
);
240 test_memeq(str
+1, str2
, 254);
241 //test_eq(buf_capacity(buf), 256);
243 write_to_buf(str
, 32, buf
);
244 //test_eq(buf_capacity(buf), 256);
246 write_to_buf(str
, 256, buf
);
248 //test_eq(buf_capacity(buf), 512);
249 test_eq(buf_datalen(buf
), 33+256);
250 fetch_from_buf(str2
, 33, buf
);
251 test_eq(*str2
, str
[255]);
253 test_memeq(str2
+1, str
, 32);
254 //test_eq(buf_capacity(buf), 512);
255 test_eq(buf_datalen(buf
), 256);
256 fetch_from_buf(str2
, 256, buf
);
257 test_memeq(str
, str2
, 256);
259 /* now try shrinking: case 1. */
261 buf
= buf_new_with_capacity(33668);
263 write_to_buf(str
,255, buf
);
265 //test_eq(buf_capacity(buf), 33668);
266 test_eq(buf_datalen(buf
), 17085);
267 for (j
=0; j
< 40; ++j
) {
268 fetch_from_buf(str2
, 255,buf
);
269 test_memeq(str2
, str
, 255);
272 /* now try shrinking: case 2. */
274 buf
= buf_new_with_capacity(33668);
276 write_to_buf(str
,255, buf
);
278 for (j
=0; j
< 20; ++j
) {
279 fetch_from_buf(str2
, 255,buf
);
280 test_memeq(str2
, str
, 255);
283 write_to_buf(str
,255, buf
);
285 //test_eq(buf_capacity(buf),33668);
286 for (j
=0; j
< 120; ++j
) {
287 fetch_from_buf(str2
, 255,buf
);
288 test_memeq(str2
, str
, 255);
291 /* Move from buf to buf. */
293 buf
= buf_new_with_capacity(4096);
294 buf2
= buf_new_with_capacity(4096);
296 write_to_buf(str
, 255, buf
);
297 test_eq(buf_datalen(buf
), 25500);
298 for (j
=0;j
<100;++j
) {
300 move_buf_to_buf(buf2
, buf
, &r
);
303 test_eq(buf_datalen(buf
), 24500);
304 test_eq(buf_datalen(buf2
), 1000);
306 fetch_from_buf(str2
, 255, buf2
);
307 test_memeq(str2
, str
, 255);
309 r
= 8192; /*big move*/
310 move_buf_to_buf(buf2
, buf
, &r
);
312 r
= 30000; /* incomplete move */
313 move_buf_to_buf(buf2
, buf
, &r
);
316 fetch_from_buf(str2
, 255, buf2
);
317 test_memeq(str2
, str
, 255);
323 buf
= buf_new_with_capacity(5);
324 cp
= "Testing. This is a moderately long Testing string.";
325 for (j
= 0; cp
[j
]; j
++)
326 write_to_buf(cp
+j
, 1, buf
);
327 test_eq(0, buf_find_string_offset(buf
, "Testing", 7));
328 test_eq(1, buf_find_string_offset(buf
, "esting", 6));
329 test_eq(1, buf_find_string_offset(buf
, "est", 3));
330 test_eq(39, buf_find_string_offset(buf
, "ing str", 7));
331 test_eq(35, buf_find_string_offset(buf
, "Testing str", 11));
332 test_eq(32, buf_find_string_offset(buf
, "ng ", 3));
333 test_eq(43, buf_find_string_offset(buf
, "string.", 7));
334 test_eq(-1, buf_find_string_offset(buf
, "shrdlu", 6));
335 test_eq(-1, buf_find_string_offset(buf
, "Testing thing", 13));
336 test_eq(-1, buf_find_string_offset(buf
, "ngx", 3));
349 s
= open(get_fname("data"), O_WRONLY
|O_CREAT
|O_TRUNC
, 0600);
353 s
= open(get_fname("data"), O_RDONLY
, 0);
355 errno
= 0; /* XXXX */
356 i
= read_to_buf(s
, 10, buf
, &eof
);
357 printf("%s\n", strerror(errno
));
360 //test_eq(buf_capacity(buf), 4096);
361 test_eq(buf_datalen(buf
), 10);
363 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10);
365 /* Test reading 0 bytes. */
366 i
= read_to_buf(s
, 0, buf
, &eof
);
367 //test_eq(buf_capacity(buf), 512*1024);
368 test_eq(buf_datalen(buf
), 10);
372 /* Now test when buffer is filled exactly. */
373 buf2
= buf_new_with_capacity(6);
374 i
= read_to_buf(s
, 6, buf2
, &eof
);
375 //test_eq(buf_capacity(buf2), 6);
376 test_eq(buf_datalen(buf2
), 6);
379 test_memeq(str
+10, (char*)_buf_peek_raw_buffer(buf2
), 6);
383 /* Now test when buffer is filled with more data to read. */
384 buf2
= buf_new_with_capacity(32);
385 i
= read_to_buf(s
, 128, buf2
, &eof
);
386 //test_eq(buf_capacity(buf2), 128);
387 test_eq(buf_datalen(buf2
), 32);
393 /* Now read to eof. */
394 test_assert(buf_capacity(buf
) > 256);
395 i
= read_to_buf(s
, 1024, buf
, &eof
);
396 test_eq(i
, (256-32-10-6));
397 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
398 test_eq(buf_datalen(buf
), 256-6-32);
399 test_memeq(str
, (char*)_buf_peek_raw_buffer(buf
), 10); /* XXX Check rest. */
402 i
= read_to_buf(s
, 1024, buf
, &eof
);
404 test_eq(buf_capacity(buf
), MAX_BUF_SIZE
);
405 test_eq(buf_datalen(buf
), 256-6-32);
417 /** Run unit tests for the onion handshake code. */
419 test_onion_handshake(void)
422 crypto_dh_env_t
*c_dh
= NULL
;
423 char c_buf
[ONIONSKIN_CHALLENGE_LEN
];
427 char s_buf
[ONIONSKIN_REPLY_LEN
];
431 crypto_pk_env_t
*pk
= NULL
;
435 /* client handshake 1. */
436 memset(c_buf
, 0, ONIONSKIN_CHALLENGE_LEN
);
437 test_assert(! onion_skin_create(pk
, &c_dh
, c_buf
));
439 /* server handshake */
440 memset(s_buf
, 0, ONIONSKIN_REPLY_LEN
);
441 memset(s_keys
, 0, 40);
442 test_assert(! onion_skin_server_handshake(c_buf
, pk
, NULL
,
445 /* client handshake 2 */
446 memset(c_keys
, 0, 40);
447 test_assert(! onion_skin_client_handshake(c_dh
, s_buf
, c_keys
, 40));
449 if (memcmp(c_keys
, s_keys
, 40)) {
453 test_memeq(c_keys
, s_keys
, 40);
454 memset(s_buf
, 0, 40);
455 test_memneq(c_keys
, s_buf
, 40);
459 crypto_dh_free(c_dh
);
461 crypto_free_pk_env(pk
);
465 test_circuit_timeout(void)
468 * 1. Generate 1000 samples
469 * 2. Estimate parameters
470 * 3. If difference, repeat
473 * 6. Estimate parameters
474 * 7. compare differences
476 circuit_build_times_t initial
;
477 circuit_build_times_t estimate
;
478 circuit_build_times_t final
;
479 double timeout1
, timeout2
;
484 circuit_build_times_init(&initial
);
485 circuit_build_times_init(&estimate
);
486 circuit_build_times_init(&final
);
488 memset(&state
, 0, sizeof(or_state_t
));
490 circuitbuild_running_unit_tests();
491 #define timeout0 (build_time_t)(30*1000.0)
493 circuit_build_times_initial_alpha(&initial
,
494 CBT_DEFAULT_QUANTILE_CUTOFF
/100.0,
496 close_ms
= MAX(circuit_build_times_calculate_timeout(&initial
,
497 CBT_DEFAULT_CLOSE_QUANTILE
/100.0),
498 CBT_DEFAULT_TIMEOUT_INITIAL_VALUE
);
500 for (i
=0; i
< CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE
; i
++) {
501 build_time_t sample
= circuit_build_times_generate_sample(&initial
,0,1);
503 if (sample
> close_ms
) {
504 circuit_build_times_add_time(&estimate
, CBT_BUILD_ABANDONED
);
506 circuit_build_times_add_time(&estimate
, sample
);
509 circuit_build_times_update_alpha(&estimate
);
510 timeout1
= circuit_build_times_calculate_timeout(&estimate
,
511 CBT_DEFAULT_QUANTILE_CUTOFF
/100.0);
512 circuit_build_times_set_timeout(&estimate
);
513 log_notice(LD_CIRC
, "Timeout1 is %lf, Xm is %d", timeout1
, estimate
.Xm
);
515 } while (fabs(circuit_build_times_cdf(&initial
, timeout0
) -
516 circuit_build_times_cdf(&initial
, timeout1
)) > 0.02);
518 test_assert(estimate
.total_build_times
<= CBT_NCIRCUITS_TO_OBSERVE
);
520 circuit_build_times_update_state(&estimate
, &state
);
521 test_assert(circuit_build_times_parse_state(&final
, &state
, &msg
) == 0);
523 circuit_build_times_update_alpha(&final
);
524 timeout2
= circuit_build_times_calculate_timeout(&final
,
525 CBT_DEFAULT_QUANTILE_CUTOFF
/100.0);
527 circuit_build_times_set_timeout(&final
);
528 log_notice(LD_CIRC
, "Timeout2 is %lf, Xm is %d", timeout2
, final
.Xm
);
530 /* 5% here because some accuracy is lost due to histogram conversion */
531 test_assert(fabs(circuit_build_times_cdf(&initial
, timeout0
) -
532 circuit_build_times_cdf(&initial
, timeout2
)) < 0.05);
534 for (runs
= 0; runs
< 50; runs
++) {
535 int build_times_idx
= 0;
536 int total_build_times
= 0;
538 final
.close_ms
= final
.timeout_ms
= CBT_DEFAULT_TIMEOUT_INITIAL_VALUE
;
539 estimate
.close_ms
= estimate
.timeout_ms
540 = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE
;
542 for (i
= 0; i
< CBT_DEFAULT_RECENT_CIRCUITS
*2; i
++) {
543 circuit_build_times_network_circ_success(&estimate
);
544 circuit_build_times_add_time(&estimate
,
545 circuit_build_times_generate_sample(&estimate
, 0,
546 CBT_DEFAULT_QUANTILE_CUTOFF
/100.0));
548 circuit_build_times_network_circ_success(&estimate
);
549 circuit_build_times_add_time(&final
,
550 circuit_build_times_generate_sample(&final
, 0,
551 CBT_DEFAULT_QUANTILE_CUTOFF
/100.0));
554 test_assert(!circuit_build_times_network_check_changed(&estimate
));
555 test_assert(!circuit_build_times_network_check_changed(&final
));
557 /* Reset liveness to be non-live */
558 final
.liveness
.network_last_live
= 0;
559 estimate
.liveness
.network_last_live
= 0;
561 build_times_idx
= estimate
.build_times_idx
;
562 total_build_times
= estimate
.total_build_times
;
563 for (i
= 0; i
< CBT_NETWORK_NONLIVE_TIMEOUT_COUNT
; i
++) {
564 test_assert(circuit_build_times_network_check_live(&estimate
));
565 test_assert(circuit_build_times_network_check_live(&final
));
567 circuit_build_times_count_close(&estimate
, 0,
568 (time_t)(approx_time()-estimate
.close_ms
/1000.0-1));
569 circuit_build_times_count_close(&final
, 0,
570 (time_t)(approx_time()-final
.close_ms
/1000.0-1));
573 test_assert(!circuit_build_times_network_check_live(&estimate
));
574 test_assert(!circuit_build_times_network_check_live(&final
));
576 for ( ; i
< CBT_NETWORK_NONLIVE_DISCARD_COUNT
; i
++) {
577 circuit_build_times_count_close(&estimate
, 0,
578 (time_t)(approx_time()-estimate
.close_ms
/1000.0-1));
580 if (i
< CBT_NETWORK_NONLIVE_DISCARD_COUNT
-1) {
581 circuit_build_times_count_close(&final
, 0,
582 (time_t)(approx_time()-final
.close_ms
/1000.0-1));
586 test_assert(!circuit_build_times_network_check_live(&estimate
));
587 test_assert(!circuit_build_times_network_check_live(&final
));
589 log_info(LD_CIRC
, "idx: %d %d, tot: %d %d",
590 build_times_idx
, estimate
.build_times_idx
,
591 total_build_times
, estimate
.total_build_times
);
593 /* Check rollback index. Should match top of loop. */
594 test_assert(build_times_idx
== estimate
.build_times_idx
);
595 // This can fail if estimate.total_build_times == 1000, because
596 // in that case, rewind actually causes us to lose timeouts
597 if (total_build_times
!= CBT_NCIRCUITS_TO_OBSERVE
)
598 test_assert(total_build_times
== estimate
.total_build_times
);
600 /* Now simulate that the network has become live and we need
602 circuit_build_times_network_is_live(&estimate
);
603 circuit_build_times_network_is_live(&final
);
605 for (i
= 0; i
< CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT
; i
++) {
606 circuit_build_times_count_timeout(&estimate
, 1);
608 if (i
< CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT
-1) {
609 circuit_build_times_count_timeout(&final
, 1);
613 test_assert(estimate
.liveness
.after_firsthop_idx
== 0);
614 test_assert(final
.liveness
.after_firsthop_idx
==
615 CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT
-1);
617 test_assert(circuit_build_times_network_check_live(&estimate
));
618 test_assert(circuit_build_times_network_check_live(&final
));
620 circuit_build_times_count_timeout(&final
, 1);
627 /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
628 * that policies_summarize() produces the string <b>expected_summary</b> from
631 test_policy_summary_helper(const char *policy_str
,
632 const char *expected_summary
)
635 smartlist_t
*policy
= smartlist_create();
636 char *summary
= NULL
;
639 line
.key
= (char*)"foo";
640 line
.value
= (char *)policy_str
;
643 r
= policies_parse_exit_policy(&line
, &policy
, 0, NULL
, 1);
645 summary
= policy_summarize(policy
);
647 test_assert(summary
!= NULL
);
648 test_streq(summary
, expected_summary
);
653 addr_policy_list_free(policy
);
656 /** Run unit tests for generating summary lines of exit policies */
661 smartlist_t
*policy
= NULL
, *policy2
= NULL
, *policy3
= NULL
,
662 *policy4
= NULL
, *policy5
= NULL
, *policy6
= NULL
,
667 smartlist_t
*sm
= NULL
;
668 char *policy_str
= NULL
;
670 policy
= smartlist_create();
672 p
= router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
673 test_assert(p
!= NULL
);
674 test_eq(ADDR_POLICY_REJECT
, p
->policy_type
);
675 tor_addr_from_ipv4h(&tar
, 0xc0a80000u
);
676 test_eq(0, tor_addr_compare(&p
->addr
, &tar
, CMP_EXACT
));
677 test_eq(16, p
->maskbits
);
678 test_eq(1, p
->prt_min
);
679 test_eq(65535, p
->prt_max
);
681 smartlist_add(policy
, p
);
683 test_assert(ADDR_POLICY_ACCEPTED
==
684 compare_addr_to_addr_policy(0x01020304u
, 2, policy
));
685 test_assert(ADDR_POLICY_PROBABLY_ACCEPTED
==
686 compare_addr_to_addr_policy(0, 2, policy
));
687 test_assert(ADDR_POLICY_REJECTED
==
688 compare_addr_to_addr_policy(0xc0a80102, 2, policy
));
690 test_assert(0 == policies_parse_exit_policy(NULL
, &policy2
, 1, NULL
, 1));
691 test_assert(policy2
);
693 policy3
= smartlist_create();
694 p
= router_parse_addr_policy_item_from_string("reject *:*",-1);
695 test_assert(p
!= NULL
);
696 smartlist_add(policy3
, p
);
697 p
= router_parse_addr_policy_item_from_string("accept *:*",-1);
698 test_assert(p
!= NULL
);
699 smartlist_add(policy3
, p
);
701 policy4
= smartlist_create();
702 p
= router_parse_addr_policy_item_from_string("accept *:443",-1);
703 test_assert(p
!= NULL
);
704 smartlist_add(policy4
, p
);
705 p
= router_parse_addr_policy_item_from_string("accept *:443",-1);
706 test_assert(p
!= NULL
);
707 smartlist_add(policy4
, p
);
709 policy5
= smartlist_create();
710 p
= router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
711 test_assert(p
!= NULL
);
712 smartlist_add(policy5
, p
);
713 p
= router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
714 test_assert(p
!= NULL
);
715 smartlist_add(policy5
, p
);
716 p
= router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
717 test_assert(p
!= NULL
);
718 smartlist_add(policy5
, p
);
719 p
= router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
720 test_assert(p
!= NULL
);
721 smartlist_add(policy5
, p
);
722 p
= router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
723 test_assert(p
!= NULL
);
724 smartlist_add(policy5
, p
);
725 p
= router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
726 test_assert(p
!= NULL
);
727 smartlist_add(policy5
, p
);
728 p
= router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
729 test_assert(p
!= NULL
);
730 smartlist_add(policy5
, p
);
731 p
= router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
732 test_assert(p
!= NULL
);
733 smartlist_add(policy5
, p
);
734 p
= router_parse_addr_policy_item_from_string("reject *:65535",-1);
735 test_assert(p
!= NULL
);
736 smartlist_add(policy5
, p
);
737 p
= router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
738 test_assert(p
!= NULL
);
739 smartlist_add(policy5
, p
);
741 policy6
= smartlist_create();
742 p
= router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
743 test_assert(p
!= NULL
);
744 smartlist_add(policy6
, p
);
746 policy7
= smartlist_create();
747 p
= router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
748 test_assert(p
!= NULL
);
749 smartlist_add(policy7
, p
);
751 test_assert(!exit_policy_is_general_exit(policy
));
752 test_assert(exit_policy_is_general_exit(policy2
));
753 test_assert(!exit_policy_is_general_exit(NULL
));
754 test_assert(!exit_policy_is_general_exit(policy3
));
755 test_assert(!exit_policy_is_general_exit(policy4
));
756 test_assert(!exit_policy_is_general_exit(policy5
));
757 test_assert(!exit_policy_is_general_exit(policy6
));
758 test_assert(!exit_policy_is_general_exit(policy7
));
760 test_assert(cmp_addr_policies(policy
, policy2
));
761 test_assert(cmp_addr_policies(policy
, NULL
));
762 test_assert(!cmp_addr_policies(policy2
, policy2
));
763 test_assert(!cmp_addr_policies(NULL
, NULL
));
765 test_assert(!policy_is_reject_star(policy2
));
766 test_assert(policy_is_reject_star(policy
));
767 test_assert(policy_is_reject_star(NULL
));
769 addr_policy_list_free(policy
);
772 /* make sure compacting logic works. */
774 line
.key
= (char*)"foo";
775 line
.value
= (char*)"accept *:80,reject private:*,reject *:*";
777 test_assert(0 == policies_parse_exit_policy(&line
, &policy
, 0, NULL
, 1));
779 //test_streq(policy->string, "accept *:80");
780 //test_streq(policy->next->string, "reject *:*");
781 test_eq(smartlist_len(policy
), 2);
783 /* test policy summaries */
784 /* check if we properly ignore private IP addresses */
785 test_policy_summary_helper("reject 192.168.0.0/16:*,"
786 "reject 0.0.0.0/8:*,"
787 "reject 10.0.0.0/8:*,"
792 /* check all accept policies, and proper counting of rejects */
793 test_policy_summary_helper("reject 11.0.0.0/9:80,"
794 "reject 12.0.0.0/9:80,"
795 "reject 13.0.0.0/9:80,"
796 "reject 14.0.0.0/9:80,"
797 "accept *:*", "accept 1-65535");
798 test_policy_summary_helper("reject 11.0.0.0/9:80,"
799 "reject 12.0.0.0/9:80,"
800 "reject 13.0.0.0/9:80,"
801 "reject 14.0.0.0/9:80,"
802 "reject 15.0.0.0:81,"
803 "accept *:*", "accept 1-65535");
804 test_policy_summary_helper("reject 11.0.0.0/9:80,"
805 "reject 12.0.0.0/9:80,"
806 "reject 13.0.0.0/9:80,"
807 "reject 14.0.0.0/9:80,"
808 "reject 15.0.0.0:80,"
812 test_policy_summary_helper("accept 11.0.0.0/9:80,"
816 test_policy_summary_helper("accept *:80,"
821 "accept 80-81,100-111");
823 test_policy_summary_helper("accept *:1,"
829 test_policy_summary_helper("accept *:1,"
835 test_policy_summary_helper("reject *:1,"
842 /* truncation ports */
843 sm
= smartlist_create();
844 for (i
=1; i
<2000; i
+=2) {
845 char buf
[POLICY_BUF_LEN
];
846 tor_snprintf(buf
, sizeof(buf
), "reject *:%d", i
);
847 smartlist_add(sm
, tor_strdup(buf
));
849 smartlist_add(sm
, tor_strdup("accept *:*"));
850 policy_str
= smartlist_join_strings(sm
, ",", 0, NULL
);
851 test_policy_summary_helper( policy_str
,
852 "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
853 "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
854 "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
855 "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
856 "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
857 "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
858 "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
859 "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
860 "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
861 "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
862 "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
863 "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
864 "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
865 "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
868 addr_policy_list_free(policy
);
869 addr_policy_list_free(policy2
);
870 addr_policy_list_free(policy3
);
871 addr_policy_list_free(policy4
);
872 addr_policy_list_free(policy5
);
873 addr_policy_list_free(policy6
);
874 addr_policy_list_free(policy7
);
875 tor_free(policy_str
);
877 SMARTLIST_FOREACH(sm
, char *, s
, tor_free(s
));
882 /** Run AES performance benchmarks. */
888 crypto_cipher_env_t
*c
;
889 struct timeval start
, end
;
890 const int iters
= 100000;
892 c
= crypto_new_cipher_env();
893 crypto_cipher_generate_key(c
);
894 crypto_cipher_encrypt_init_cipher(c
);
895 for (len
= 1; len
<= 8192; len
*= 2) {
896 b1
= tor_malloc_zero(len
);
897 b2
= tor_malloc_zero(len
);
898 tor_gettimeofday(&start
);
899 for (i
= 0; i
< iters
; ++i
) {
900 crypto_cipher_encrypt(c
, b1
, b2
, len
);
902 tor_gettimeofday(&end
);
905 nsec
= (uint64_t) tv_udiff(&start
,&end
);
908 printf("%d bytes: "U64_FORMAT
" nsec per byte\n", len
,
909 U64_PRINTF_ARG(nsec
));
911 crypto_free_cipher_env(c
);
914 /** Run digestmap_t performance benchmarks. */
918 smartlist_t
*sl
= smartlist_create();
919 smartlist_t
*sl2
= smartlist_create();
920 struct timeval start
, end
, pt2
, pt3
, pt4
;
921 const int iters
= 10000;
922 const int elts
= 4000;
923 const int fpostests
= 1000000;
926 digestmap_t
*dm
= digestmap_new();
927 digestset_t
*ds
= digestset_new(elts
);
929 for (i
= 0; i
< elts
; ++i
) {
931 smartlist_add(sl
, tor_memdup(d
, 20));
933 for (i
= 0; i
< elts
; ++i
) {
935 smartlist_add(sl2
, tor_memdup(d
, 20));
937 printf("nbits=%d\n", ds
->mask
+1);
939 tor_gettimeofday(&start
);
940 for (i
= 0; i
< iters
; ++i
) {
941 SMARTLIST_FOREACH(sl
, const char *, cp
, digestmap_set(dm
, cp
, (void*)1));
943 tor_gettimeofday(&pt2
);
944 for (i
= 0; i
< iters
; ++i
) {
945 SMARTLIST_FOREACH(sl
, const char *, cp
, digestmap_get(dm
, cp
));
946 SMARTLIST_FOREACH(sl2
, const char *, cp
, digestmap_get(dm
, cp
));
948 tor_gettimeofday(&pt3
);
949 for (i
= 0; i
< iters
; ++i
) {
950 SMARTLIST_FOREACH(sl
, const char *, cp
, digestset_add(ds
, cp
));
952 tor_gettimeofday(&pt4
);
953 for (i
= 0; i
< iters
; ++i
) {
954 SMARTLIST_FOREACH(sl
, const char *, cp
, n
+= digestset_isin(ds
, cp
));
955 SMARTLIST_FOREACH(sl2
, const char *, cp
, n
+= digestset_isin(ds
, cp
));
957 tor_gettimeofday(&end
);
959 for (i
= 0; i
< fpostests
; ++i
) {
961 if (digestset_isin(ds
, d
)) ++fp
;
964 printf("%ld\n",(unsigned long)tv_udiff(&start
, &pt2
));
965 printf("%ld\n",(unsigned long)tv_udiff(&pt2
, &pt3
));
966 printf("%ld\n",(unsigned long)tv_udiff(&pt3
, &pt4
));
967 printf("%ld\n",(unsigned long)tv_udiff(&pt4
, &end
));
968 printf("-- %d\n", n
);
969 printf("++ %f\n", fp
/(double)fpostests
);
970 digestmap_free(dm
, NULL
);
972 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
973 SMARTLIST_FOREACH(sl2
, char *, cp
, tor_free(cp
));
978 /** Test encoding and parsing of rendezvous service descriptors. */
982 rend_service_descriptor_t
*generated
= NULL
, *parsed
= NULL
;
983 char service_id
[DIGEST_LEN
];
984 char service_id_base32
[REND_SERVICE_ID_LEN_BASE32
+1];
985 const char *next_desc
;
986 smartlist_t
*descs
= smartlist_create();
987 char computed_desc_id
[DIGEST_LEN
];
988 char parsed_desc_id
[DIGEST_LEN
];
989 crypto_pk_env_t
*pk1
= NULL
, *pk2
= NULL
;
991 char *intro_points_encrypted
= NULL
;
992 size_t intro_points_size
;
995 char address1
[] = "fooaddress.onion";
996 char address2
[] = "aaaaaaaaaaaaaaaa.onion";
997 char address3
[] = "fooaddress.exit";
998 char address4
[] = "www.torproject.org";
1000 test_assert(BAD_HOSTNAME
== parse_extended_hostname(address1
, 1));
1001 test_assert(ONION_HOSTNAME
== parse_extended_hostname(address2
, 1));
1002 test_assert(EXIT_HOSTNAME
== parse_extended_hostname(address3
, 1));
1003 test_assert(NORMAL_HOSTNAME
== parse_extended_hostname(address4
, 1));
1005 pk1
= pk_generate(0);
1006 pk2
= pk_generate(1);
1007 generated
= tor_malloc_zero(sizeof(rend_service_descriptor_t
));
1008 generated
->pk
= crypto_pk_dup_key(pk1
);
1009 crypto_pk_get_digest(generated
->pk
, service_id
);
1010 base32_encode(service_id_base32
, REND_SERVICE_ID_LEN_BASE32
+1,
1011 service_id
, REND_SERVICE_ID_LEN
);
1013 generated
->timestamp
= now
;
1014 generated
->version
= 2;
1015 generated
->protocols
= 42;
1016 generated
->intro_nodes
= smartlist_create();
1018 for (i
= 0; i
< 3; i
++) {
1019 rend_intro_point_t
*intro
= tor_malloc_zero(sizeof(rend_intro_point_t
));
1020 crypto_pk_env_t
*okey
= pk_generate(2 + i
);
1021 intro
->extend_info
= tor_malloc_zero(sizeof(extend_info_t
));
1022 intro
->extend_info
->onion_key
= okey
;
1023 crypto_pk_get_digest(intro
->extend_info
->onion_key
,
1024 intro
->extend_info
->identity_digest
);
1025 //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
1026 intro
->extend_info
->nickname
[0] = '$';
1027 base16_encode(intro
->extend_info
->nickname
+ 1,
1028 sizeof(intro
->extend_info
->nickname
) - 1,
1029 intro
->extend_info
->identity_digest
, DIGEST_LEN
);
1030 /* Does not cover all IP addresses. */
1031 tor_addr_from_ipv4h(&intro
->extend_info
->addr
, crypto_rand_int(65536));
1032 intro
->extend_info
->port
= 1 + crypto_rand_int(65535);
1033 intro
->intro_key
= crypto_pk_dup_key(pk2
);
1034 smartlist_add(generated
->intro_nodes
, intro
);
1036 test_assert(rend_encode_v2_descriptors(descs
, generated
, now
, 0,
1037 REND_NO_AUTH
, NULL
, NULL
) > 0);
1038 test_assert(rend_compute_v2_desc_id(computed_desc_id
, service_id_base32
,
1039 NULL
, now
, 0) == 0);
1040 test_memeq(((rend_encoded_v2_service_descriptor_t
*)
1041 smartlist_get(descs
, 0))->desc_id
, computed_desc_id
, DIGEST_LEN
);
1042 test_assert(rend_parse_v2_service_descriptor(&parsed
, parsed_desc_id
,
1043 &intro_points_encrypted
,
1047 ((rend_encoded_v2_service_descriptor_t
*)
1048 smartlist_get(descs
, 0))->desc_str
) == 0);
1049 test_assert(parsed
);
1050 test_memeq(((rend_encoded_v2_service_descriptor_t
*)
1051 smartlist_get(descs
, 0))->desc_id
, parsed_desc_id
, DIGEST_LEN
);
1052 test_eq(rend_parse_introduction_points(parsed
, intro_points_encrypted
,
1053 intro_points_size
), 3);
1054 test_assert(!crypto_pk_cmp_keys(generated
->pk
, parsed
->pk
));
1055 test_eq(parsed
->timestamp
, now
);
1056 test_eq(parsed
->version
, 2);
1057 test_eq(parsed
->protocols
, 42);
1058 test_eq(smartlist_len(parsed
->intro_nodes
), 3);
1059 for (i
= 0; i
< smartlist_len(parsed
->intro_nodes
); i
++) {
1060 rend_intro_point_t
*par_intro
= smartlist_get(parsed
->intro_nodes
, i
),
1061 *gen_intro
= smartlist_get(generated
->intro_nodes
, i
);
1062 extend_info_t
*par_info
= par_intro
->extend_info
;
1063 extend_info_t
*gen_info
= gen_intro
->extend_info
;
1064 test_assert(!crypto_pk_cmp_keys(gen_info
->onion_key
, par_info
->onion_key
));
1065 test_memeq(gen_info
->identity_digest
, par_info
->identity_digest
,
1067 test_streq(gen_info
->nickname
, par_info
->nickname
);
1068 test_assert(tor_addr_eq(&gen_info
->addr
, &par_info
->addr
));
1069 test_eq(gen_info
->port
, par_info
->port
);
1072 rend_service_descriptor_free(parsed
);
1073 rend_service_descriptor_free(generated
);
1074 parsed
= generated
= NULL
;
1078 for (i
= 0; i
< smartlist_len(descs
); i
++)
1079 rend_encoded_v2_service_descriptor_free(smartlist_get(descs
, i
));
1080 smartlist_free(descs
);
1083 rend_service_descriptor_free(parsed
);
1085 rend_service_descriptor_free(generated
);
1087 crypto_free_pk_env(pk1
);
1089 crypto_free_pk_env(pk2
);
1090 tor_free(intro_points_encrypted
);
1093 /** Run unit tests for GeoIP code. */
1098 time_t now
= time(NULL
);
1101 /* Populate the DB a bit. Add these in order, since we can't do the final
1102 * 'sort' step. These aren't very good IP addresses, but they're perfectly
1103 * fine uint32_t values. */
1104 test_eq(0, geoip_parse_entry("10,50,AB"));
1105 test_eq(0, geoip_parse_entry("52,90,XY"));
1106 test_eq(0, geoip_parse_entry("95,100,AB"));
1107 test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
1108 test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
1109 test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
1111 /* We should have 4 countries: ??, ab, xy, zz. */
1112 test_eq(4, geoip_get_n_countries());
1113 /* Make sure that country ID actually works. */
1114 #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
1115 test_streq("??", NAMEFOR(3));
1116 test_eq(0, geoip_get_country_by_ip(3));
1117 test_streq("ab", NAMEFOR(32));
1118 test_streq("??", NAMEFOR(5));
1119 test_streq("??", NAMEFOR(51));
1120 test_streq("xy", NAMEFOR(150));
1121 test_streq("xy", NAMEFOR(190));
1122 test_streq("??", NAMEFOR(2000));
1125 get_options()->BridgeRelay
= 1;
1126 get_options()->BridgeRecordUsageByCountry
= 1;
1127 /* Put 9 observations in AB... */
1128 for (i
=32; i
< 40; ++i
)
1129 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
, i
, now
-7200);
1130 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
, 225, now
-7200);
1131 /* and 3 observations in XY, several times. */
1132 for (j
=0; j
< 10; ++j
)
1133 for (i
=52; i
< 55; ++i
)
1134 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
, i
, now
-3600);
1135 /* and 17 observations in ZZ... */
1136 for (i
=110; i
< 127; ++i
)
1137 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
, i
, now
);
1138 s
= geoip_get_client_history(GEOIP_CLIENT_CONNECT
);
1140 test_streq("zz=24,ab=16,xy=8", s
);
1143 /* Now clear out all the AB observations. */
1144 geoip_remove_old_clients(now
-6000);
1145 s
= geoip_get_client_history(GEOIP_CLIENT_CONNECT
);
1147 test_streq("zz=24,xy=8", s
);
1153 /** Run unit tests for stats code. */
1157 time_t now
= 1281533250; /* 2010-08-11 13:27:30 UTC */
1160 /* We shouldn't collect exit stats without initializing them. */
1161 rep_hist_note_exit_stream_opened(80);
1162 rep_hist_note_exit_bytes(80, 100, 10000);
1163 s
= rep_hist_exit_stats_history(now
+ 86400);
1166 /* Initialize stats, note some streams and bytes, and generate history
1168 rep_hist_exit_stats_init(now
);
1169 rep_hist_note_exit_stream_opened(80);
1170 rep_hist_note_exit_bytes(80, 100, 10000);
1171 rep_hist_note_exit_stream_opened(443);
1172 rep_hist_note_exit_bytes(443, 100, 10000);
1173 rep_hist_note_exit_bytes(443, 100, 10000);
1174 s
= rep_hist_exit_stats_history(now
+ 86400);
1175 test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1176 "exit-kibibytes-written 80=1,443=1,other=0\n"
1177 "exit-kibibytes-read 80=10,443=20,other=0\n"
1178 "exit-streams-opened 80=4,443=4,other=0\n", s
);
1181 /* Stop collecting stats, add some bytes, and ensure we don't generate
1182 * a history string. */
1183 rep_hist_exit_stats_term();
1184 rep_hist_note_exit_bytes(80, 100, 10000);
1185 s
= rep_hist_exit_stats_history(now
+ 86400);
1188 /* Re-start stats, add some bytes, reset stats, and see what history we
1189 * get when observing no streams or bytes at all. */
1190 rep_hist_exit_stats_init(now
);
1191 rep_hist_note_exit_stream_opened(80);
1192 rep_hist_note_exit_bytes(80, 100, 10000);
1193 rep_hist_reset_exit_stats(now
);
1194 s
= rep_hist_exit_stats_history(now
+ 86400);
1195 test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1196 "exit-kibibytes-written other=0\n"
1197 "exit-kibibytes-read other=0\n"
1198 "exit-streams-opened other=0\n", s
);
1205 legacy_test_setup(const struct testcase_t
*testcase
)
1207 return testcase
->setup_data
;
1211 legacy_test_helper(void *data
)
1213 void (*fn
)(void) = data
;
1218 legacy_test_cleanup(const struct testcase_t
*testcase
, void *ptr
)
1225 const struct testcase_setup_t legacy_setup
= {
1226 legacy_test_setup
, legacy_test_cleanup
1230 { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
1231 #define SUBENT(group, name) \
1232 { #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
1233 test_ ## group ## _ ## name }
1234 #define DISABLED(name) \
1235 { #name, legacy_test_helper, TT_SKIP, &legacy_setup, name }
1237 static struct testcase_t test_array
[] = {
1239 ENT(onion_handshake
),
1240 ENT(circuit_timeout
),
1246 DISABLED(bench_aes
),
1247 DISABLED(bench_dmap
),
1251 extern struct testcase_t addr_tests
[];
1252 extern struct testcase_t crypto_tests
[];
1253 extern struct testcase_t container_tests
[];
1254 extern struct testcase_t util_tests
[];
1255 extern struct testcase_t dir_tests
[];
1257 static struct testgroup_t testgroups
[] = {
1259 { "addr/", addr_tests
},
1260 { "crypto/", crypto_tests
},
1261 { "container/", container_tests
},
1262 { "util/", util_tests
},
1263 { "dir/", dir_tests
},
1267 /** Main entry point for unit test code: parse the command line, and run
1268 * some unit tests. */
1270 main(int c
, const char **v
)
1272 or_options_t
*options
;
1273 char *errmsg
= NULL
;
1275 int loglevel
= LOG_ERR
;
1279 int r
= CRYPTO_set_mem_ex_functions(_tor_malloc
, _tor_realloc
, _tor_free
);
1284 update_approx_time(time(NULL
));
1285 options
= options_new();
1289 for (i_out
= i
= 1; i
< c
; ++i
) {
1290 if (!strcmp(v
[i
], "--warn")) {
1291 loglevel
= LOG_WARN
;
1292 } else if (!strcmp(v
[i
], "--notice")) {
1293 loglevel
= LOG_NOTICE
;
1294 } else if (!strcmp(v
[i
], "--info")) {
1295 loglevel
= LOG_INFO
;
1296 } else if (!strcmp(v
[i
], "--debug")) {
1297 loglevel
= LOG_DEBUG
;
1305 log_severity_list_t s
;
1306 memset(&s
, 0, sizeof(s
));
1307 set_log_severity_config(loglevel
, LOG_ERR
, &s
);
1308 add_stream_log(&s
, "", fileno(stdout
));
1311 options
->command
= CMD_RUN_UNITTESTS
;
1312 crypto_global_init(0, NULL
, NULL
);
1316 options_init(options
);
1317 options
->DataDirectory
= tor_strdup(temp_dir
);
1318 options
->EntryStatistics
= 1;
1319 if (set_options(options
, &errmsg
) < 0) {
1320 printf("Failed to set initial options: %s\n", errmsg
);
1327 atexit(remove_directory
);
1329 have_failed
= (tinytest_main(c
, v
, testgroups
) != 0);
1331 free_pregenerated_keys();
1334 dmalloc_log_unfreed();