test.c 42 KB

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