test.c 42 KB

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