test.c 43 KB

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