test.c 42 KB

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