test.c 43 KB

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