test.c 44 KB

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