test.c 42 KB

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