0.2.6.5-rc will come out today, because why not.
[tor.git] / src / test / test.c
blob0524a6978f29e3de50ecbcb3add892a22ea11cfa
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file test.c
8 * \brief Unit tests for many pieces of the lower level Tor modules.
9 **/
11 #include "orconfig.h"
13 #include <stdio.h>
14 #ifdef HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif
18 #ifdef _WIN32
19 /* For mkdir() */
20 #include <direct.h>
21 #else
22 #include <dirent.h>
23 #endif
25 /* These macros pull in declarations for some functions and structures that
26 * are typically file-private. */
27 #define GEOIP_PRIVATE
28 #define ROUTER_PRIVATE
29 #define CIRCUITSTATS_PRIVATE
30 #define CIRCUITLIST_PRIVATE
31 #define STATEFILE_PRIVATE
34 * Linux doesn't provide lround in math.h by default, but mac os does...
35 * It's best just to leave math.h out of the picture entirely.
37 //#include <math.h>
38 long int lround(double x);
39 double fabs(double x);
41 #include "or.h"
42 #include "backtrace.h"
43 #include "buffers.h"
44 #include "circuitlist.h"
45 #include "circuitstats.h"
46 #include "config.h"
47 #include "connection_edge.h"
48 #include "geoip.h"
49 #include "rendcommon.h"
50 #include "test.h"
51 #include "torgzip.h"
52 #include "memarea.h"
53 #include "onion.h"
54 #include "onion_ntor.h"
55 #include "onion_tap.h"
56 #include "policies.h"
57 #include "rephist.h"
58 #include "routerparse.h"
59 #include "statefile.h"
60 #include "crypto_curve25519.h"
61 #include "onion_ntor.h"
63 /** Run unit tests for the onion handshake code. */
64 static void
65 test_onion_handshake(void *arg)
67 /* client-side */
68 crypto_dh_t *c_dh = NULL;
69 char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
70 char c_keys[40];
71 /* server-side */
72 char s_buf[TAP_ONIONSKIN_REPLY_LEN];
73 char s_keys[40];
74 int i;
75 /* shared */
76 crypto_pk_t *pk = NULL, *pk2 = NULL;
78 (void)arg;
79 pk = pk_generate(0);
80 pk2 = pk_generate(1);
82 /* client handshake 1. */
83 memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
84 tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
86 for (i = 1; i <= 3; ++i) {
87 crypto_pk_t *k1, *k2;
88 if (i==1) {
89 /* server handshake: only one key known. */
90 k1 = pk; k2 = NULL;
91 } else if (i==2) {
92 /* server handshake: try the right key first. */
93 k1 = pk; k2 = pk2;
94 } else {
95 /* server handshake: try the right key second. */
96 k1 = pk2; k2 = pk;
99 memset(s_buf, 0, TAP_ONIONSKIN_REPLY_LEN);
100 memset(s_keys, 0, 40);
101 tt_assert(! onion_skin_TAP_server_handshake(c_buf, k1, k2,
102 s_buf, s_keys, 40));
104 /* client handshake 2 */
105 memset(c_keys, 0, 40);
106 tt_assert(! onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys,
107 40, NULL));
109 tt_mem_op(c_keys,OP_EQ, s_keys, 40);
110 memset(s_buf, 0, 40);
111 tt_mem_op(c_keys,OP_NE, s_buf, 40);
113 done:
114 crypto_dh_free(c_dh);
115 crypto_pk_free(pk);
116 crypto_pk_free(pk2);
119 static void
120 test_bad_onion_handshake(void *arg)
122 char junk_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
123 char junk_buf2[TAP_ONIONSKIN_CHALLENGE_LEN];
124 /* client-side */
125 crypto_dh_t *c_dh = NULL;
126 char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
127 char c_keys[40];
128 /* server-side */
129 char s_buf[TAP_ONIONSKIN_REPLY_LEN];
130 char s_keys[40];
131 /* shared */
132 crypto_pk_t *pk = NULL, *pk2 = NULL;
134 (void)arg;
136 pk = pk_generate(0);
137 pk2 = pk_generate(1);
139 /* Server: Case 1: the encrypted data is degenerate. */
140 memset(junk_buf, 0, sizeof(junk_buf));
141 crypto_pk_public_hybrid_encrypt(pk, junk_buf2, TAP_ONIONSKIN_CHALLENGE_LEN,
142 junk_buf, DH_KEY_LEN, PK_PKCS1_OAEP_PADDING, 1);
143 tt_int_op(-1, OP_EQ,
144 onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
145 s_buf, s_keys, 40));
147 /* Server: Case 2: the encrypted data is not long enough. */
148 memset(junk_buf, 0, sizeof(junk_buf));
149 memset(junk_buf2, 0, sizeof(junk_buf2));
150 crypto_pk_public_encrypt(pk, junk_buf2, sizeof(junk_buf2),
151 junk_buf, 48, PK_PKCS1_OAEP_PADDING);
152 tt_int_op(-1, OP_EQ,
153 onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
154 s_buf, s_keys, 40));
156 /* client handshake 1: do it straight. */
157 memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
158 tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
160 /* Server: Case 3: we just don't have the right key. */
161 tt_int_op(-1, OP_EQ,
162 onion_skin_TAP_server_handshake(c_buf, pk2, NULL,
163 s_buf, s_keys, 40));
165 /* Server: Case 4: The RSA-encrypted portion is corrupt. */
166 c_buf[64] ^= 33;
167 tt_int_op(-1, OP_EQ,
168 onion_skin_TAP_server_handshake(c_buf, pk, NULL,
169 s_buf, s_keys, 40));
170 c_buf[64] ^= 33;
172 /* (Let the server procede) */
173 tt_int_op(0, OP_EQ,
174 onion_skin_TAP_server_handshake(c_buf, pk, NULL,
175 s_buf, s_keys, 40));
177 /* Client: Case 1: The server sent back junk. */
178 s_buf[64] ^= 33;
179 tt_int_op(-1, OP_EQ,
180 onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
181 s_buf[64] ^= 33;
183 /* Let the client finish; make sure it can. */
184 tt_int_op(0, OP_EQ,
185 onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
186 tt_mem_op(s_keys,OP_EQ, c_keys, 40);
188 /* Client: Case 2: The server sent back a degenerate DH. */
189 memset(s_buf, 0, sizeof(s_buf));
190 tt_int_op(-1, OP_EQ,
191 onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, NULL));
193 done:
194 crypto_dh_free(c_dh);
195 crypto_pk_free(pk);
196 crypto_pk_free(pk2);
199 static void
200 test_ntor_handshake(void *arg)
202 /* client-side */
203 ntor_handshake_state_t *c_state = NULL;
204 uint8_t c_buf[NTOR_ONIONSKIN_LEN];
205 uint8_t c_keys[400];
207 /* server-side */
208 di_digest256_map_t *s_keymap=NULL;
209 curve25519_keypair_t s_keypair;
210 uint8_t s_buf[NTOR_REPLY_LEN];
211 uint8_t s_keys[400];
213 /* shared */
214 const curve25519_public_key_t *server_pubkey;
215 uint8_t node_id[20] = "abcdefghijklmnopqrst";
217 (void) arg;
219 /* Make the server some keys */
220 curve25519_secret_key_generate(&s_keypair.seckey, 0);
221 curve25519_public_key_generate(&s_keypair.pubkey, &s_keypair.seckey);
222 dimap_add_entry(&s_keymap, s_keypair.pubkey.public_key, &s_keypair);
223 server_pubkey = &s_keypair.pubkey;
225 /* client handshake 1. */
226 memset(c_buf, 0, NTOR_ONIONSKIN_LEN);
227 tt_int_op(0, OP_EQ, onion_skin_ntor_create(node_id, server_pubkey,
228 &c_state, c_buf));
230 /* server handshake */
231 memset(s_buf, 0, NTOR_REPLY_LEN);
232 memset(s_keys, 0, 40);
233 tt_int_op(0, OP_EQ, onion_skin_ntor_server_handshake(c_buf, s_keymap, NULL,
234 node_id,
235 s_buf, s_keys, 400));
237 /* client handshake 2 */
238 memset(c_keys, 0, 40);
239 tt_int_op(0, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
240 c_keys, 400, NULL));
242 tt_mem_op(c_keys,OP_EQ, s_keys, 400);
243 memset(s_buf, 0, 40);
244 tt_mem_op(c_keys,OP_NE, s_buf, 40);
246 done:
247 ntor_handshake_state_free(c_state);
248 dimap_free(s_keymap, NULL);
251 /** Run unit tests for the onion queues. */
252 static void
253 test_onion_queues(void *arg)
255 uint8_t buf1[TAP_ONIONSKIN_CHALLENGE_LEN] = {0};
256 uint8_t buf2[NTOR_ONIONSKIN_LEN] = {0};
258 or_circuit_t *circ1 = or_circuit_new(0, NULL);
259 or_circuit_t *circ2 = or_circuit_new(0, NULL);
261 create_cell_t *onionskin = NULL, *create2_ptr;
262 create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
263 create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
264 (void)arg;
265 create2_ptr = create2; /* remember, but do not free */
267 create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
268 TAP_ONIONSKIN_CHALLENGE_LEN, buf1);
269 create_cell_init(create2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
270 NTOR_ONIONSKIN_LEN, buf2);
272 tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
273 tt_int_op(0,OP_EQ, onion_pending_add(circ1, create1));
274 create1 = NULL;
275 tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
277 tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
278 tt_int_op(0,OP_EQ, onion_pending_add(circ2, create2));
279 create2 = NULL;
280 tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
282 tt_ptr_op(circ2,OP_EQ, onion_next_task(&onionskin));
283 tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
284 tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
285 tt_ptr_op(onionskin, OP_EQ, create2_ptr);
287 clear_pending_onions();
288 tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
289 tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
291 done:
292 circuit_free(TO_CIRCUIT(circ1));
293 circuit_free(TO_CIRCUIT(circ2));
294 tor_free(create1);
295 tor_free(create2);
296 tor_free(onionskin);
299 static void
300 test_circuit_timeout(void *arg)
302 /* Plan:
303 * 1. Generate 1000 samples
304 * 2. Estimate parameters
305 * 3. If difference, repeat
306 * 4. Save state
307 * 5. load state
308 * 6. Estimate parameters
309 * 7. compare differences
311 circuit_build_times_t initial;
312 circuit_build_times_t estimate;
313 circuit_build_times_t final;
314 double timeout1, timeout2;
315 or_state_t *state=NULL;
316 int i, runs;
317 double close_ms;
318 (void)arg;
319 circuit_build_times_init(&initial);
320 circuit_build_times_init(&estimate);
321 circuit_build_times_init(&final);
323 state = or_state_new();
325 circuitbuild_running_unit_tests();
326 #define timeout0 (build_time_t)(30*1000.0)
327 initial.Xm = 3000;
328 circuit_build_times_initial_alpha(&initial,
329 CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
330 timeout0);
331 close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
332 CBT_DEFAULT_CLOSE_QUANTILE/100.0),
333 CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
334 do {
335 for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
336 build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
338 if (sample > close_ms) {
339 circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
340 } else {
341 circuit_build_times_add_time(&estimate, sample);
344 circuit_build_times_update_alpha(&estimate);
345 timeout1 = circuit_build_times_calculate_timeout(&estimate,
346 CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
347 circuit_build_times_set_timeout(&estimate);
348 log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
349 /* 2% error */
350 } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
351 circuit_build_times_cdf(&initial, timeout1)) > 0.02);
353 tt_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
355 circuit_build_times_update_state(&estimate, state);
356 circuit_build_times_free_timeouts(&final);
357 tt_assert(circuit_build_times_parse_state(&final, state) == 0);
359 circuit_build_times_update_alpha(&final);
360 timeout2 = circuit_build_times_calculate_timeout(&final,
361 CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
363 circuit_build_times_set_timeout(&final);
364 log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
366 /* 5% here because some accuracy is lost due to histogram conversion */
367 tt_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
368 circuit_build_times_cdf(&initial, timeout2)) < 0.05);
370 for (runs = 0; runs < 50; runs++) {
371 int build_times_idx = 0;
372 int total_build_times = 0;
374 final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
375 estimate.close_ms = estimate.timeout_ms
376 = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
378 for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
379 circuit_build_times_network_circ_success(&estimate);
380 circuit_build_times_add_time(&estimate,
381 circuit_build_times_generate_sample(&estimate, 0,
382 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
384 circuit_build_times_network_circ_success(&estimate);
385 circuit_build_times_add_time(&final,
386 circuit_build_times_generate_sample(&final, 0,
387 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
390 tt_assert(!circuit_build_times_network_check_changed(&estimate));
391 tt_assert(!circuit_build_times_network_check_changed(&final));
393 /* Reset liveness to be non-live */
394 final.liveness.network_last_live = 0;
395 estimate.liveness.network_last_live = 0;
397 build_times_idx = estimate.build_times_idx;
398 total_build_times = estimate.total_build_times;
400 tt_assert(circuit_build_times_network_check_live(&estimate));
401 tt_assert(circuit_build_times_network_check_live(&final));
403 circuit_build_times_count_close(&estimate, 0,
404 (time_t)(approx_time()-estimate.close_ms/1000.0-1));
405 circuit_build_times_count_close(&final, 0,
406 (time_t)(approx_time()-final.close_ms/1000.0-1));
408 tt_assert(!circuit_build_times_network_check_live(&estimate));
409 tt_assert(!circuit_build_times_network_check_live(&final));
411 log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
412 build_times_idx, estimate.build_times_idx,
413 total_build_times, estimate.total_build_times);
415 /* Check rollback index. Should match top of loop. */
416 tt_assert(build_times_idx == estimate.build_times_idx);
417 // This can fail if estimate.total_build_times == 1000, because
418 // in that case, rewind actually causes us to lose timeouts
419 if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
420 tt_assert(total_build_times == estimate.total_build_times);
422 /* Now simulate that the network has become live and we need
423 * a change */
424 circuit_build_times_network_is_live(&estimate);
425 circuit_build_times_network_is_live(&final);
427 for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
428 circuit_build_times_count_timeout(&estimate, 1);
430 if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
431 circuit_build_times_count_timeout(&final, 1);
435 tt_assert(estimate.liveness.after_firsthop_idx == 0);
436 tt_assert(final.liveness.after_firsthop_idx ==
437 CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
439 tt_assert(circuit_build_times_network_check_live(&estimate));
440 tt_assert(circuit_build_times_network_check_live(&final));
442 circuit_build_times_count_timeout(&final, 1);
444 /* Ensure return value for degenerate cases are clamped correctly */
445 initial.alpha = INT32_MAX;
446 tt_assert(circuit_build_times_calculate_timeout(&initial, .99999999) <=
447 INT32_MAX);
448 initial.alpha = 0;
449 tt_assert(circuit_build_times_calculate_timeout(&initial, .5) <=
450 INT32_MAX);
453 done:
454 circuit_build_times_free_timeouts(&initial);
455 circuit_build_times_free_timeouts(&estimate);
456 circuit_build_times_free_timeouts(&final);
457 or_state_free(state);
460 /** Test encoding and parsing of rendezvous service descriptors. */
461 static void
462 test_rend_fns(void *arg)
464 rend_service_descriptor_t *generated = NULL, *parsed = NULL;
465 char service_id[DIGEST_LEN];
466 char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
467 const char *next_desc;
468 smartlist_t *descs = smartlist_new();
469 char computed_desc_id[DIGEST_LEN];
470 char parsed_desc_id[DIGEST_LEN];
471 crypto_pk_t *pk1 = NULL, *pk2 = NULL;
472 time_t now;
473 char *intro_points_encrypted = NULL;
474 size_t intro_points_size;
475 size_t encoded_size;
476 int i;
477 char address1[] = "fooaddress.onion";
478 char address2[] = "aaaaaaaaaaaaaaaa.onion";
479 char address3[] = "fooaddress.exit";
480 char address4[] = "www.torproject.org";
481 char address5[] = "foo.abcdefghijklmnop.onion";
482 char address6[] = "foo.bar.abcdefghijklmnop.onion";
483 char address7[] = ".abcdefghijklmnop.onion";
485 (void)arg;
486 tt_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
487 tt_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
488 tt_str_op(address2,OP_EQ, "aaaaaaaaaaaaaaaa");
489 tt_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
490 tt_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
491 tt_assert(ONION_HOSTNAME == parse_extended_hostname(address5));
492 tt_str_op(address5,OP_EQ, "abcdefghijklmnop");
493 tt_assert(ONION_HOSTNAME == parse_extended_hostname(address6));
494 tt_str_op(address6,OP_EQ, "abcdefghijklmnop");
495 tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
497 pk1 = pk_generate(0);
498 pk2 = pk_generate(1);
499 generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
500 generated->pk = crypto_pk_dup_key(pk1);
501 crypto_pk_get_digest(generated->pk, service_id);
502 base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
503 service_id, REND_SERVICE_ID_LEN);
504 now = time(NULL);
505 generated->timestamp = now;
506 generated->version = 2;
507 generated->protocols = 42;
508 generated->intro_nodes = smartlist_new();
510 for (i = 0; i < 3; i++) {
511 rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
512 crypto_pk_t *okey = pk_generate(2 + i);
513 intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
514 intro->extend_info->onion_key = okey;
515 crypto_pk_get_digest(intro->extend_info->onion_key,
516 intro->extend_info->identity_digest);
517 //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
518 intro->extend_info->nickname[0] = '$';
519 base16_encode(intro->extend_info->nickname + 1,
520 sizeof(intro->extend_info->nickname) - 1,
521 intro->extend_info->identity_digest, DIGEST_LEN);
522 /* Does not cover all IP addresses. */
523 tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
524 intro->extend_info->port = 1 + crypto_rand_int(65535);
525 intro->intro_key = crypto_pk_dup_key(pk2);
526 smartlist_add(generated->intro_nodes, intro);
528 tt_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
529 REND_NO_AUTH, NULL, NULL) > 0);
530 tt_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
531 NULL, now, 0) == 0);
532 tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
533 smartlist_get(descs, 0))->desc_id, OP_EQ,
534 computed_desc_id, DIGEST_LEN);
535 tt_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
536 &intro_points_encrypted,
537 &intro_points_size,
538 &encoded_size,
539 &next_desc,
540 ((rend_encoded_v2_service_descriptor_t *)
541 smartlist_get(descs, 0))->desc_str, 1) == 0);
542 tt_assert(parsed);
543 tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
544 smartlist_get(descs, 0))->desc_id,OP_EQ, parsed_desc_id, DIGEST_LEN);
545 tt_int_op(rend_parse_introduction_points(parsed, intro_points_encrypted,
546 intro_points_size),OP_EQ, 3);
547 tt_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
548 tt_int_op(parsed->timestamp,OP_EQ, now);
549 tt_int_op(parsed->version,OP_EQ, 2);
550 tt_int_op(parsed->protocols,OP_EQ, 42);
551 tt_int_op(smartlist_len(parsed->intro_nodes),OP_EQ, 3);
552 for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
553 rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
554 *gen_intro = smartlist_get(generated->intro_nodes, i);
555 extend_info_t *par_info = par_intro->extend_info;
556 extend_info_t *gen_info = gen_intro->extend_info;
557 tt_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
558 tt_mem_op(gen_info->identity_digest,OP_EQ, par_info->identity_digest,
559 DIGEST_LEN);
560 tt_str_op(gen_info->nickname,OP_EQ, par_info->nickname);
561 tt_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
562 tt_int_op(gen_info->port,OP_EQ, par_info->port);
565 rend_service_descriptor_free(parsed);
566 rend_service_descriptor_free(generated);
567 parsed = generated = NULL;
569 done:
570 if (descs) {
571 for (i = 0; i < smartlist_len(descs); i++)
572 rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
573 smartlist_free(descs);
575 if (parsed)
576 rend_service_descriptor_free(parsed);
577 if (generated)
578 rend_service_descriptor_free(generated);
579 if (pk1)
580 crypto_pk_free(pk1);
581 if (pk2)
582 crypto_pk_free(pk2);
583 tor_free(intro_points_encrypted);
586 /* Record odd numbered fake-IPs using ipv6, even numbered fake-IPs
587 * using ipv4. Since our fake geoip database is the same between
588 * ipv4 and ipv6, we should get the same result no matter which
589 * address family we pick for each IP. */
590 #define SET_TEST_ADDRESS(i) do { \
591 if ((i) & 1) { \
592 SET_TEST_IPV6(i); \
593 tor_addr_from_in6(&addr, &in6); \
594 } else { \
595 tor_addr_from_ipv4h(&addr, (uint32_t) i); \
597 } while (0)
599 /* Make sure that country ID actually works. */
600 #define SET_TEST_IPV6(i) \
601 do { \
602 set_uint32(in6.s6_addr + 12, htonl((uint32_t) (i))); \
603 } while (0)
604 #define CHECK_COUNTRY(country, val) do { \
605 /* test ipv4 country lookup */ \
606 tt_str_op(country, OP_EQ, \
607 geoip_get_country_name(geoip_get_country_by_ipv4(val))); \
608 /* test ipv6 country lookup */ \
609 SET_TEST_IPV6(val); \
610 tt_str_op(country, OP_EQ, \
611 geoip_get_country_name(geoip_get_country_by_ipv6(&in6))); \
612 } while (0)
614 /** Run unit tests for GeoIP code. */
615 static void
616 test_geoip(void *arg)
618 int i, j;
619 time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
620 char *s = NULL, *v = NULL;
621 const char *bridge_stats_1 =
622 "bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
623 "bridge-ips zz=24,xy=8\n"
624 "bridge-ip-versions v4=16,v6=16\n"
625 "bridge-ip-transports <OR>=24\n",
626 *dirreq_stats_1 =
627 "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
628 "dirreq-v3-ips ab=8\n"
629 "dirreq-v3-reqs ab=8\n"
630 "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
631 "not-modified=0,busy=0\n"
632 "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
633 "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
634 *dirreq_stats_2 =
635 "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
636 "dirreq-v3-ips \n"
637 "dirreq-v3-reqs \n"
638 "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
639 "not-modified=0,busy=0\n"
640 "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
641 "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
642 *dirreq_stats_3 =
643 "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
644 "dirreq-v3-ips \n"
645 "dirreq-v3-reqs \n"
646 "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
647 "not-modified=0,busy=0\n"
648 "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
649 "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
650 *dirreq_stats_4 =
651 "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
652 "dirreq-v3-ips \n"
653 "dirreq-v3-reqs \n"
654 "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
655 "not-modified=0,busy=0\n"
656 "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
657 "dirreq-v3-tunneled-dl complete=0,timeout=0,running=4\n",
658 *entry_stats_1 =
659 "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
660 "entry-ips ab=8\n",
661 *entry_stats_2 =
662 "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
663 "entry-ips \n";
664 tor_addr_t addr;
665 struct in6_addr in6;
667 /* Populate the DB a bit. Add these in order, since we can't do the final
668 * 'sort' step. These aren't very good IP addresses, but they're perfectly
669 * fine uint32_t values. */
670 (void)arg;
671 tt_int_op(0,OP_EQ, geoip_parse_entry("10,50,AB", AF_INET));
672 tt_int_op(0,OP_EQ, geoip_parse_entry("52,90,XY", AF_INET));
673 tt_int_op(0,OP_EQ, geoip_parse_entry("95,100,AB", AF_INET));
674 tt_int_op(0,OP_EQ, geoip_parse_entry("\"105\",\"140\",\"ZZ\"", AF_INET));
675 tt_int_op(0,OP_EQ, geoip_parse_entry("\"150\",\"190\",\"XY\"", AF_INET));
676 tt_int_op(0,OP_EQ, geoip_parse_entry("\"200\",\"250\",\"AB\"", AF_INET));
678 /* Populate the IPv6 DB equivalently with fake IPs in the same range */
679 tt_int_op(0,OP_EQ, geoip_parse_entry("::a,::32,AB", AF_INET6));
680 tt_int_op(0,OP_EQ, geoip_parse_entry("::34,::5a,XY", AF_INET6));
681 tt_int_op(0,OP_EQ, geoip_parse_entry("::5f,::64,AB", AF_INET6));
682 tt_int_op(0,OP_EQ, geoip_parse_entry("::69,::8c,ZZ", AF_INET6));
683 tt_int_op(0,OP_EQ, geoip_parse_entry("::96,::be,XY", AF_INET6));
684 tt_int_op(0,OP_EQ, geoip_parse_entry("::c8,::fa,AB", AF_INET6));
686 /* We should have 4 countries: ??, ab, xy, zz. */
687 tt_int_op(4,OP_EQ, geoip_get_n_countries());
688 memset(&in6, 0, sizeof(in6));
690 CHECK_COUNTRY("??", 3);
691 CHECK_COUNTRY("ab", 32);
692 CHECK_COUNTRY("??", 5);
693 CHECK_COUNTRY("??", 51);
694 CHECK_COUNTRY("xy", 150);
695 CHECK_COUNTRY("xy", 190);
696 CHECK_COUNTRY("??", 2000);
698 tt_int_op(0,OP_EQ, geoip_get_country_by_ipv4(3));
699 SET_TEST_IPV6(3);
700 tt_int_op(0,OP_EQ, geoip_get_country_by_ipv6(&in6));
702 get_options_mutable()->BridgeRelay = 1;
703 get_options_mutable()->BridgeRecordUsageByCountry = 1;
704 /* Put 9 observations in AB... */
705 for (i=32; i < 40; ++i) {
706 SET_TEST_ADDRESS(i);
707 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
709 SET_TEST_ADDRESS(225);
710 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
711 /* and 3 observations in XY, several times. */
712 for (j=0; j < 10; ++j)
713 for (i=52; i < 55; ++i) {
714 SET_TEST_ADDRESS(i);
715 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-3600);
717 /* and 17 observations in ZZ... */
718 for (i=110; i < 127; ++i) {
719 SET_TEST_ADDRESS(i);
720 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
722 geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
723 tt_assert(s);
724 tt_assert(v);
725 tt_str_op("zz=24,ab=16,xy=8",OP_EQ, s);
726 tt_str_op("v4=16,v6=16",OP_EQ, v);
727 tor_free(s);
728 tor_free(v);
730 /* Now clear out all the AB observations. */
731 geoip_remove_old_clients(now-6000);
732 geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
733 tt_assert(s);
734 tt_assert(v);
735 tt_str_op("zz=24,xy=8",OP_EQ, s);
736 tt_str_op("v4=16,v6=16",OP_EQ, v);
737 tor_free(s);
738 tor_free(v);
740 /* Start testing bridge statistics by making sure that we don't output
741 * bridge stats without initializing them. */
742 s = geoip_format_bridge_stats(now + 86400);
743 tt_assert(!s);
745 /* Initialize stats and generate the bridge-stats history string out of
746 * the connecting clients added above. */
747 geoip_bridge_stats_init(now);
748 s = geoip_format_bridge_stats(now + 86400);
749 tt_assert(s);
750 tt_str_op(bridge_stats_1,OP_EQ, s);
751 tor_free(s);
753 /* Stop collecting bridge stats and make sure we don't write a history
754 * string anymore. */
755 geoip_bridge_stats_term();
756 s = geoip_format_bridge_stats(now + 86400);
757 tt_assert(!s);
759 /* Stop being a bridge and start being a directory mirror that gathers
760 * directory request statistics. */
761 geoip_bridge_stats_term();
762 get_options_mutable()->BridgeRelay = 0;
763 get_options_mutable()->BridgeRecordUsageByCountry = 0;
764 get_options_mutable()->DirReqStatistics = 1;
766 /* Start testing dirreq statistics by making sure that we don't collect
767 * dirreq stats without initializing them. */
768 SET_TEST_ADDRESS(100);
769 geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
770 s = geoip_format_dirreq_stats(now + 86400);
771 tt_assert(!s);
773 /* Initialize stats, note one connecting client, and generate the
774 * dirreq-stats history string. */
775 geoip_dirreq_stats_init(now);
776 SET_TEST_ADDRESS(100);
777 geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
778 s = geoip_format_dirreq_stats(now + 86400);
779 tt_str_op(dirreq_stats_1,OP_EQ, s);
780 tor_free(s);
782 /* Stop collecting stats, add another connecting client, and ensure we
783 * don't generate a history string. */
784 geoip_dirreq_stats_term();
785 SET_TEST_ADDRESS(101);
786 geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
787 s = geoip_format_dirreq_stats(now + 86400);
788 tt_assert(!s);
790 /* Re-start stats, add a connecting client, reset stats, and make sure
791 * that we get an all empty history string. */
792 geoip_dirreq_stats_init(now);
793 SET_TEST_ADDRESS(100);
794 geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
795 geoip_reset_dirreq_stats(now);
796 s = geoip_format_dirreq_stats(now + 86400);
797 tt_str_op(dirreq_stats_2,OP_EQ, s);
798 tor_free(s);
800 /* Note a successful network status response and make sure that it
801 * appears in the history string. */
802 geoip_note_ns_response(GEOIP_SUCCESS);
803 s = geoip_format_dirreq_stats(now + 86400);
804 tt_str_op(dirreq_stats_3,OP_EQ, s);
805 tor_free(s);
807 /* Start a tunneled directory request. */
808 geoip_start_dirreq((uint64_t) 1, 1024, DIRREQ_TUNNELED);
809 s = geoip_format_dirreq_stats(now + 86400);
810 tt_str_op(dirreq_stats_4,OP_EQ, s);
811 tor_free(s);
813 /* Stop collecting directory request statistics and start gathering
814 * entry stats. */
815 geoip_dirreq_stats_term();
816 get_options_mutable()->DirReqStatistics = 0;
817 get_options_mutable()->EntryStatistics = 1;
819 /* Start testing entry statistics by making sure that we don't collect
820 * anything without initializing entry stats. */
821 SET_TEST_ADDRESS(100);
822 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
823 s = geoip_format_entry_stats(now + 86400);
824 tt_assert(!s);
826 /* Initialize stats, note one connecting client, and generate the
827 * entry-stats history string. */
828 geoip_entry_stats_init(now);
829 SET_TEST_ADDRESS(100);
830 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
831 s = geoip_format_entry_stats(now + 86400);
832 tt_str_op(entry_stats_1,OP_EQ, s);
833 tor_free(s);
835 /* Stop collecting stats, add another connecting client, and ensure we
836 * don't generate a history string. */
837 geoip_entry_stats_term();
838 SET_TEST_ADDRESS(101);
839 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
840 s = geoip_format_entry_stats(now + 86400);
841 tt_assert(!s);
843 /* Re-start stats, add a connecting client, reset stats, and make sure
844 * that we get an all empty history string. */
845 geoip_entry_stats_init(now);
846 SET_TEST_ADDRESS(100);
847 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
848 geoip_reset_entry_stats(now);
849 s = geoip_format_entry_stats(now + 86400);
850 tt_str_op(entry_stats_2,OP_EQ, s);
851 tor_free(s);
853 /* Stop collecting entry statistics. */
854 geoip_entry_stats_term();
855 get_options_mutable()->EntryStatistics = 0;
857 done:
858 tor_free(s);
859 tor_free(v);
862 static void
863 test_geoip_with_pt(void *arg)
865 time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
866 char *s = NULL;
867 int i;
868 tor_addr_t addr;
869 struct in6_addr in6;
871 (void)arg;
872 get_options_mutable()->BridgeRelay = 1;
873 get_options_mutable()->BridgeRecordUsageByCountry = 1;
875 memset(&in6, 0, sizeof(in6));
877 /* No clients seen yet. */
878 s = geoip_get_transport_history();
879 tor_assert(!s);
881 /* 4 connections without a pluggable transport */
882 for (i=0; i < 4; ++i) {
883 SET_TEST_ADDRESS(i);
884 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
887 /* 9 connections with "alpha" */
888 for (i=4; i < 13; ++i) {
889 SET_TEST_ADDRESS(i);
890 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "alpha", now-7200);
893 /* one connection with "beta" */
894 SET_TEST_ADDRESS(13);
895 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "beta", now-7200);
897 /* 14 connections with "charlie" */
898 for (i=14; i < 28; ++i) {
899 SET_TEST_ADDRESS(i);
900 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "charlie", now-7200);
903 /* 131 connections with "ddr" */
904 for (i=28; i < 159; ++i) {
905 SET_TEST_ADDRESS(i);
906 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "ddr", now-7200);
909 /* 8 connections with "entropy" */
910 for (i=159; i < 167; ++i) {
911 SET_TEST_ADDRESS(i);
912 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "entropy", now-7200);
915 /* 2 connections from the same IP with two different transports. */
916 SET_TEST_ADDRESS(++i);
917 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "fire", now-7200);
918 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "google", now-7200);
920 /* Test the transport history string. */
921 s = geoip_get_transport_history();
922 tor_assert(s);
923 tt_str_op(s,OP_EQ, "<OR>=8,alpha=16,beta=8,charlie=16,ddr=136,"
924 "entropy=8,fire=8,google=8");
926 /* Stop collecting entry statistics. */
927 geoip_entry_stats_term();
928 get_options_mutable()->EntryStatistics = 0;
930 done:
931 tor_free(s);
934 #undef SET_TEST_ADDRESS
935 #undef SET_TEST_IPV6
936 #undef CHECK_COUNTRY
938 /** Run unit tests for stats code. */
939 static void
940 test_stats(void *arg)
942 time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
943 char *s = NULL;
944 int i;
946 /* Start with testing exit port statistics; we shouldn't collect exit
947 * stats without initializing them. */
948 (void)arg;
949 rep_hist_note_exit_stream_opened(80);
950 rep_hist_note_exit_bytes(80, 100, 10000);
951 s = rep_hist_format_exit_stats(now + 86400);
952 tt_assert(!s);
954 /* Initialize stats, note some streams and bytes, and generate history
955 * string. */
956 rep_hist_exit_stats_init(now);
957 rep_hist_note_exit_stream_opened(80);
958 rep_hist_note_exit_bytes(80, 100, 10000);
959 rep_hist_note_exit_stream_opened(443);
960 rep_hist_note_exit_bytes(443, 100, 10000);
961 rep_hist_note_exit_bytes(443, 100, 10000);
962 s = rep_hist_format_exit_stats(now + 86400);
963 tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
964 "exit-kibibytes-written 80=1,443=1,other=0\n"
965 "exit-kibibytes-read 80=10,443=20,other=0\n"
966 "exit-streams-opened 80=4,443=4,other=0\n",OP_EQ, s);
967 tor_free(s);
969 /* Add a few bytes on 10 more ports and ensure that only the top 10
970 * ports are contained in the history string. */
971 for (i = 50; i < 60; i++) {
972 rep_hist_note_exit_bytes(i, i, i);
973 rep_hist_note_exit_stream_opened(i);
975 s = rep_hist_format_exit_stats(now + 86400);
976 tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
977 "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
978 "59=1,80=1,443=1,other=1\n"
979 "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
980 "59=1,80=10,443=20,other=1\n"
981 "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
982 "59=4,80=4,443=4,other=4\n",OP_EQ, s);
983 tor_free(s);
985 /* Stop collecting stats, add some bytes, and ensure we don't generate
986 * a history string. */
987 rep_hist_exit_stats_term();
988 rep_hist_note_exit_bytes(80, 100, 10000);
989 s = rep_hist_format_exit_stats(now + 86400);
990 tt_assert(!s);
992 /* Re-start stats, add some bytes, reset stats, and see what history we
993 * get when observing no streams or bytes at all. */
994 rep_hist_exit_stats_init(now);
995 rep_hist_note_exit_stream_opened(80);
996 rep_hist_note_exit_bytes(80, 100, 10000);
997 rep_hist_reset_exit_stats(now);
998 s = rep_hist_format_exit_stats(now + 86400);
999 tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1000 "exit-kibibytes-written other=0\n"
1001 "exit-kibibytes-read other=0\n"
1002 "exit-streams-opened other=0\n",OP_EQ, s);
1003 tor_free(s);
1005 /* Continue with testing connection statistics; we shouldn't collect
1006 * conn stats without initializing them. */
1007 rep_hist_note_or_conn_bytes(1, 20, 400, now);
1008 s = rep_hist_format_conn_stats(now + 86400);
1009 tt_assert(!s);
1011 /* Initialize stats, note bytes, and generate history string. */
1012 rep_hist_conn_stats_init(now);
1013 rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
1014 rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
1015 rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
1016 rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
1017 s = rep_hist_format_conn_stats(now + 86400);
1018 tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n",OP_EQ, s);
1019 tor_free(s);
1021 /* Stop collecting stats, add some bytes, and ensure we don't generate
1022 * a history string. */
1023 rep_hist_conn_stats_term();
1024 rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
1025 s = rep_hist_format_conn_stats(now + 86400);
1026 tt_assert(!s);
1028 /* Re-start stats, add some bytes, reset stats, and see what history we
1029 * get when observing no bytes at all. */
1030 rep_hist_conn_stats_init(now);
1031 rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
1032 rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
1033 rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
1034 rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
1035 rep_hist_reset_conn_stats(now);
1036 s = rep_hist_format_conn_stats(now + 86400);
1037 tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",OP_EQ, s);
1038 tor_free(s);
1040 /* Continue with testing buffer statistics; we shouldn't collect buffer
1041 * stats without initializing them. */
1042 rep_hist_add_buffer_stats(2.0, 2.0, 20);
1043 s = rep_hist_format_buffer_stats(now + 86400);
1044 tt_assert(!s);
1046 /* Initialize stats, add statistics for a single circuit, and generate
1047 * the history string. */
1048 rep_hist_buffer_stats_init(now);
1049 rep_hist_add_buffer_stats(2.0, 2.0, 20);
1050 s = rep_hist_format_buffer_stats(now + 86400);
1051 tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1052 "cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
1053 "cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
1054 "0.00,0.00\n"
1055 "cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
1056 "cell-circuits-per-decile 1\n",OP_EQ, s);
1057 tor_free(s);
1059 /* Add nineteen more circuit statistics to the one that's already in the
1060 * history to see that the math works correctly. */
1061 for (i = 21; i < 30; i++)
1062 rep_hist_add_buffer_stats(2.0, 2.0, i);
1063 for (i = 20; i < 30; i++)
1064 rep_hist_add_buffer_stats(3.5, 3.5, i);
1065 s = rep_hist_format_buffer_stats(now + 86400);
1066 tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1067 "cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
1068 "cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
1069 "2.75,2.75\n"
1070 "cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
1071 "cell-circuits-per-decile 2\n",OP_EQ, s);
1072 tor_free(s);
1074 /* Stop collecting stats, add statistics for one circuit, and ensure we
1075 * don't generate a history string. */
1076 rep_hist_buffer_stats_term();
1077 rep_hist_add_buffer_stats(2.0, 2.0, 20);
1078 s = rep_hist_format_buffer_stats(now + 86400);
1079 tt_assert(!s);
1081 /* Re-start stats, add statistics for one circuit, reset stats, and make
1082 * sure that the history has all zeros. */
1083 rep_hist_buffer_stats_init(now);
1084 rep_hist_add_buffer_stats(2.0, 2.0, 20);
1085 rep_hist_reset_buffer_stats(now);
1086 s = rep_hist_format_buffer_stats(now + 86400);
1087 tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
1088 "cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
1089 "cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
1090 "0.00,0.00\n"
1091 "cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
1092 "cell-circuits-per-decile 0\n",OP_EQ, s);
1094 done:
1095 tor_free(s);
1098 #define ENT(name) \
1099 { #name, test_ ## name , 0, NULL, NULL }
1100 #define FORK(name) \
1101 { #name, test_ ## name , TT_FORK, NULL, NULL }
1103 static struct testcase_t test_array[] = {
1104 ENT(onion_handshake),
1105 { "bad_onion_handshake", test_bad_onion_handshake, 0, NULL, NULL },
1106 ENT(onion_queues),
1107 { "ntor_handshake", test_ntor_handshake, 0, NULL, NULL },
1108 ENT(circuit_timeout),
1109 ENT(rend_fns),
1110 ENT(geoip),
1111 FORK(geoip_with_pt),
1112 FORK(stats),
1114 END_OF_TESTCASES
1117 extern struct testcase_t accounting_tests[];
1118 extern struct testcase_t addr_tests[];
1119 extern struct testcase_t address_tests[];
1120 extern struct testcase_t buffer_tests[];
1121 extern struct testcase_t cell_format_tests[];
1122 extern struct testcase_t cell_queue_tests[];
1123 extern struct testcase_t channel_tests[];
1124 extern struct testcase_t channeltls_tests[];
1125 extern struct testcase_t checkdir_tests[];
1126 extern struct testcase_t circuitlist_tests[];
1127 extern struct testcase_t circuitmux_tests[];
1128 extern struct testcase_t config_tests[];
1129 extern struct testcase_t container_tests[];
1130 extern struct testcase_t controller_event_tests[];
1131 extern struct testcase_t crypto_tests[];
1132 extern struct testcase_t dir_tests[];
1133 extern struct testcase_t entryconn_tests[];
1134 extern struct testcase_t entrynodes_tests[];
1135 extern struct testcase_t guardfraction_tests[];
1136 extern struct testcase_t extorport_tests[];
1137 extern struct testcase_t hs_tests[];
1138 extern struct testcase_t introduce_tests[];
1139 extern struct testcase_t logging_tests[];
1140 extern struct testcase_t microdesc_tests[];
1141 extern struct testcase_t nodelist_tests[];
1142 extern struct testcase_t oom_tests[];
1143 extern struct testcase_t options_tests[];
1144 extern struct testcase_t policy_tests[];
1145 extern struct testcase_t pt_tests[];
1146 extern struct testcase_t relay_tests[];
1147 extern struct testcase_t relaycell_tests[];
1148 extern struct testcase_t replaycache_tests[];
1149 extern struct testcase_t router_tests[];
1150 extern struct testcase_t routerkeys_tests[];
1151 extern struct testcase_t routerlist_tests[];
1152 extern struct testcase_t routerset_tests[];
1153 extern struct testcase_t scheduler_tests[];
1154 extern struct testcase_t socks_tests[];
1155 extern struct testcase_t status_tests[];
1156 extern struct testcase_t thread_tests[];
1157 extern struct testcase_t util_tests[];
1159 struct testgroup_t testgroups[] = {
1160 { "", test_array },
1161 { "accounting/", accounting_tests },
1162 { "addr/", addr_tests },
1163 { "address/", address_tests },
1164 { "buffer/", buffer_tests },
1165 { "cellfmt/", cell_format_tests },
1166 { "cellqueue/", cell_queue_tests },
1167 { "channel/", channel_tests },
1168 { "channeltls/", channeltls_tests },
1169 { "checkdir/", checkdir_tests },
1170 { "circuitlist/", circuitlist_tests },
1171 { "circuitmux/", circuitmux_tests },
1172 { "config/", config_tests },
1173 { "container/", container_tests },
1174 { "control/", controller_event_tests },
1175 { "crypto/", crypto_tests },
1176 { "dir/", dir_tests },
1177 { "dir/md/", microdesc_tests },
1178 { "entryconn/", entryconn_tests },
1179 { "entrynodes/", entrynodes_tests },
1180 { "guardfraction/", guardfraction_tests },
1181 { "extorport/", extorport_tests },
1182 { "hs/", hs_tests },
1183 { "introduce/", introduce_tests },
1184 { "nodelist/", nodelist_tests },
1185 { "oom/", oom_tests },
1186 { "options/", options_tests },
1187 { "policy/" , policy_tests },
1188 { "pt/", pt_tests },
1189 { "relay/" , relay_tests },
1190 { "relaycell/", relaycell_tests },
1191 { "replaycache/", replaycache_tests },
1192 { "routerkeys/", routerkeys_tests },
1193 { "routerlist/", routerlist_tests },
1194 { "routerset/" , routerset_tests },
1195 { "scheduler/", scheduler_tests },
1196 { "socks/", socks_tests },
1197 { "status/" , status_tests },
1198 { "util/", util_tests },
1199 { "util/logging/", logging_tests },
1200 { "util/thread/", thread_tests },
1201 END_OF_GROUPS