test.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, 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 "lib/crypt_ops/crypto_rand.h"
  11. #include <stdio.h>
  12. #ifdef HAVE_FCNTL_H
  13. #include <fcntl.h>
  14. #endif
  15. #ifdef _WIN32
  16. /* For mkdir() */
  17. #include <direct.h>
  18. #else
  19. #include <dirent.h>
  20. #endif /* defined(_WIN32) */
  21. /* These macros pull in declarations for some functions and structures that
  22. * are typically file-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/or.h"
  36. #include "lib/err/backtrace.h"
  37. #include "lib/container/buffers.h"
  38. #include "or/circuitlist.h"
  39. #include "or/circuitstats.h"
  40. #include "lib/compress/compress.h"
  41. #include "or/config.h"
  42. #include "or/connection_edge.h"
  43. #include "or/rendcommon.h"
  44. #include "or/rendcache.h"
  45. #include "test/test.h"
  46. #include "or/main.h"
  47. #include "lib/memarea/memarea.h"
  48. #include "or/onion.h"
  49. #include "or/onion_ntor.h"
  50. #include "or/onion_fast.h"
  51. #include "or/onion_tap.h"
  52. #include "or/policies.h"
  53. #include "or/rephist.h"
  54. #include "or/routerparse.h"
  55. #include "or/statefile.h"
  56. #include "lib/crypt_ops/crypto_curve25519.h"
  57. #include "or/extend_info_st.h"
  58. #include "or/or_circuit_st.h"
  59. #include "or/rend_encoded_v2_service_descriptor_st.h"
  60. #include "or/rend_intro_point_st.h"
  61. #include "or/rend_service_descriptor_st.h"
  62. /** Run unit tests for the onion handshake code. */
  63. static void
  64. test_onion_handshake(void *arg)
  65. {
  66. /* client-side */
  67. crypto_dh_t *c_dh = NULL;
  68. char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  69. char c_keys[40];
  70. /* server-side */
  71. char s_buf[TAP_ONIONSKIN_REPLY_LEN];
  72. char s_keys[40];
  73. int i;
  74. /* shared */
  75. crypto_pk_t *pk = NULL, *pk2 = NULL;
  76. (void)arg;
  77. pk = pk_generate(0);
  78. pk2 = pk_generate(1);
  79. /* client handshake 1. */
  80. memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
  81. tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
  82. for (i = 1; i <= 3; ++i) {
  83. crypto_pk_t *k1, *k2;
  84. if (i==1) {
  85. /* server handshake: only one key known. */
  86. k1 = pk; k2 = NULL;
  87. } else if (i==2) {
  88. /* server handshake: try the right key first. */
  89. k1 = pk; k2 = pk2;
  90. } else {
  91. /* server handshake: try the right key second. */
  92. k1 = pk2; k2 = pk;
  93. }
  94. memset(s_buf, 0, TAP_ONIONSKIN_REPLY_LEN);
  95. memset(s_keys, 0, 40);
  96. tt_assert(! onion_skin_TAP_server_handshake(c_buf, k1, k2,
  97. s_buf, s_keys, 40));
  98. /* client handshake 2 */
  99. memset(c_keys, 0, 40);
  100. tt_assert(! onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys,
  101. 40, NULL));
  102. tt_mem_op(c_keys,OP_EQ, s_keys, 40);
  103. memset(s_buf, 0, 40);
  104. tt_mem_op(c_keys,OP_NE, s_buf, 40);
  105. }
  106. done:
  107. crypto_dh_free(c_dh);
  108. crypto_pk_free(pk);
  109. crypto_pk_free(pk2);
  110. }
  111. static void
  112. test_bad_onion_handshake(void *arg)
  113. {
  114. char junk_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  115. char junk_buf2[TAP_ONIONSKIN_CHALLENGE_LEN];
  116. /* client-side */
  117. crypto_dh_t *c_dh = NULL;
  118. char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  119. char c_keys[40];
  120. /* server-side */
  121. char s_buf[TAP_ONIONSKIN_REPLY_LEN];
  122. char s_keys[40];
  123. /* shared */
  124. crypto_pk_t *pk = NULL, *pk2 = NULL;
  125. (void)arg;
  126. pk = pk_generate(0);
  127. pk2 = pk_generate(1);
  128. /* Server: Case 1: the encrypted data is degenerate. */
  129. memset(junk_buf, 0, sizeof(junk_buf));
  130. crypto_pk_obsolete_public_hybrid_encrypt(pk,
  131. junk_buf2, TAP_ONIONSKIN_CHALLENGE_LEN,
  132. junk_buf, DH1024_KEY_LEN,
  133. PK_PKCS1_OAEP_PADDING, 1);
  134. tt_int_op(-1, OP_EQ,
  135. onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
  136. s_buf, s_keys, 40));
  137. /* Server: Case 2: the encrypted data is not long enough. */
  138. memset(junk_buf, 0, sizeof(junk_buf));
  139. memset(junk_buf2, 0, sizeof(junk_buf2));
  140. crypto_pk_public_encrypt(pk, junk_buf2, sizeof(junk_buf2),
  141. junk_buf, 48, PK_PKCS1_OAEP_PADDING);
  142. tt_int_op(-1, OP_EQ,
  143. onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
  144. s_buf, s_keys, 40));
  145. /* client handshake 1: do it straight. */
  146. memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
  147. tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
  148. /* Server: Case 3: we just don't have the right key. */
  149. tt_int_op(-1, OP_EQ,
  150. onion_skin_TAP_server_handshake(c_buf, pk2, NULL,
  151. s_buf, s_keys, 40));
  152. /* Server: Case 4: The RSA-encrypted portion is corrupt. */
  153. c_buf[64] ^= 33;
  154. tt_int_op(-1, OP_EQ,
  155. onion_skin_TAP_server_handshake(c_buf, pk, NULL,
  156. s_buf, s_keys, 40));
  157. c_buf[64] ^= 33;
  158. /* (Let the server proceed) */
  159. tt_int_op(0, OP_EQ,
  160. onion_skin_TAP_server_handshake(c_buf, pk, NULL,
  161. s_buf, s_keys, 40));
  162. /* Client: Case 1: The server sent back junk. */
  163. const char *msg = NULL;
  164. s_buf[64] ^= 33;
  165. tt_int_op(-1, OP_EQ,
  166. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
  167. s_buf[64] ^= 33;
  168. tt_str_op(msg, OP_EQ, "Digest DOES NOT MATCH on onion handshake. "
  169. "Bug or attack.");
  170. /* Let the client finish; make sure it can. */
  171. msg = NULL;
  172. tt_int_op(0, OP_EQ,
  173. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
  174. tt_mem_op(s_keys,OP_EQ, c_keys, 40);
  175. tt_ptr_op(msg, OP_EQ, NULL);
  176. /* Client: Case 2: The server sent back a degenerate DH. */
  177. memset(s_buf, 0, sizeof(s_buf));
  178. tt_int_op(-1, OP_EQ,
  179. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40, &msg));
  180. tt_str_op(msg, OP_EQ, "DH computation failed.");
  181. done:
  182. crypto_dh_free(c_dh);
  183. crypto_pk_free(pk);
  184. crypto_pk_free(pk2);
  185. }
  186. static void
  187. test_ntor_handshake(void *arg)
  188. {
  189. /* client-side */
  190. ntor_handshake_state_t *c_state = NULL;
  191. uint8_t c_buf[NTOR_ONIONSKIN_LEN];
  192. uint8_t c_keys[400];
  193. /* server-side */
  194. di_digest256_map_t *s_keymap=NULL;
  195. curve25519_keypair_t s_keypair;
  196. uint8_t s_buf[NTOR_REPLY_LEN];
  197. uint8_t s_keys[400];
  198. /* shared */
  199. const curve25519_public_key_t *server_pubkey;
  200. uint8_t node_id[20] = "abcdefghijklmnopqrst";
  201. (void) arg;
  202. /* Make the server some keys */
  203. curve25519_secret_key_generate(&s_keypair.seckey, 0);
  204. curve25519_public_key_generate(&s_keypair.pubkey, &s_keypair.seckey);
  205. dimap_add_entry(&s_keymap, s_keypair.pubkey.public_key, &s_keypair);
  206. server_pubkey = &s_keypair.pubkey;
  207. /* client handshake 1. */
  208. memset(c_buf, 0, NTOR_ONIONSKIN_LEN);
  209. tt_int_op(0, OP_EQ, onion_skin_ntor_create(node_id, server_pubkey,
  210. &c_state, c_buf));
  211. /* server handshake */
  212. memset(s_buf, 0, NTOR_REPLY_LEN);
  213. memset(s_keys, 0, 40);
  214. tt_int_op(0, OP_EQ, onion_skin_ntor_server_handshake(c_buf, s_keymap, NULL,
  215. node_id,
  216. s_buf, s_keys, 400));
  217. /* client handshake 2 */
  218. memset(c_keys, 0, 40);
  219. tt_int_op(0, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
  220. c_keys, 400, NULL));
  221. tt_mem_op(c_keys,OP_EQ, s_keys, 400);
  222. memset(s_buf, 0, 40);
  223. tt_mem_op(c_keys,OP_NE, s_buf, 40);
  224. /* Now try with a bogus server response. Zero input should trigger
  225. * All The Problems. */
  226. memset(c_keys, 0, 400);
  227. memset(s_buf, 0, NTOR_REPLY_LEN);
  228. const char *msg = NULL;
  229. tt_int_op(-1, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
  230. c_keys, 400, &msg));
  231. tt_str_op(msg, OP_EQ, "Zero output from curve25519 handshake");
  232. done:
  233. ntor_handshake_state_free(c_state);
  234. dimap_free(s_keymap, NULL);
  235. }
  236. static void
  237. test_fast_handshake(void *arg)
  238. {
  239. /* tests for the obsolete "CREATE_FAST" handshake. */
  240. (void) arg;
  241. fast_handshake_state_t *state = NULL;
  242. uint8_t client_handshake[CREATE_FAST_LEN];
  243. uint8_t server_handshake[CREATED_FAST_LEN];
  244. uint8_t s_keys[100], c_keys[100];
  245. /* First, test an entire handshake. */
  246. memset(client_handshake, 0, sizeof(client_handshake));
  247. tt_int_op(0, OP_EQ, fast_onionskin_create(&state, client_handshake));
  248. tt_assert(! tor_mem_is_zero((char*)client_handshake,
  249. sizeof(client_handshake)));
  250. tt_int_op(0, OP_EQ,
  251. fast_server_handshake(client_handshake, server_handshake,
  252. s_keys, 100));
  253. const char *msg = NULL;
  254. tt_int_op(0, OP_EQ,
  255. fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
  256. tt_ptr_op(msg, OP_EQ, NULL);
  257. tt_mem_op(s_keys, OP_EQ, c_keys, 100);
  258. /* Now test a failing handshake. */
  259. server_handshake[0] ^= 3;
  260. tt_int_op(-1, OP_EQ,
  261. fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
  262. tt_str_op(msg, OP_EQ, "Digest DOES NOT MATCH on fast handshake. "
  263. "Bug or attack.");
  264. done:
  265. fast_handshake_state_free(state);
  266. }
  267. /** Run unit tests for the onion queues. */
  268. static void
  269. test_onion_queues(void *arg)
  270. {
  271. uint8_t buf1[TAP_ONIONSKIN_CHALLENGE_LEN] = {0};
  272. uint8_t buf2[NTOR_ONIONSKIN_LEN] = {0};
  273. or_circuit_t *circ1 = or_circuit_new(0, NULL);
  274. or_circuit_t *circ2 = or_circuit_new(0, NULL);
  275. create_cell_t *onionskin = NULL, *create2_ptr;
  276. create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
  277. create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
  278. (void)arg;
  279. create2_ptr = create2; /* remember, but do not free */
  280. create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  281. TAP_ONIONSKIN_CHALLENGE_LEN, buf1);
  282. create_cell_init(create2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  283. NTOR_ONIONSKIN_LEN, buf2);
  284. tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  285. tt_int_op(0,OP_EQ, onion_pending_add(circ1, create1));
  286. create1 = NULL;
  287. tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  288. tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  289. tt_int_op(0,OP_EQ, onion_pending_add(circ2, create2));
  290. create2 = NULL;
  291. tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  292. tt_ptr_op(circ2,OP_EQ, onion_next_task(&onionskin));
  293. tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  294. tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  295. tt_ptr_op(onionskin, OP_EQ, create2_ptr);
  296. clear_pending_onions();
  297. tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  298. tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  299. done:
  300. circuit_free_(TO_CIRCUIT(circ1));
  301. circuit_free_(TO_CIRCUIT(circ2));
  302. tor_free(create1);
  303. tor_free(create2);
  304. tor_free(onionskin);
  305. }
  306. static crypto_cipher_t *crypto_rand_aes_cipher = NULL;
  307. // Mock replacement for crypto_rand: Generates bytes from a provided AES_CTR
  308. // cipher in <b>crypto_rand_aes_cipher</b>.
  309. static void
  310. crypto_rand_deterministic_aes(char *out, size_t n)
  311. {
  312. tor_assert(crypto_rand_aes_cipher);
  313. memset(out, 0, n);
  314. crypto_cipher_crypt_inplace(crypto_rand_aes_cipher, out, n);
  315. }
  316. static void
  317. test_circuit_timeout(void *arg)
  318. {
  319. /* Plan:
  320. * 1. Generate 1000 samples
  321. * 2. Estimate parameters
  322. * 3. If difference, repeat
  323. * 4. Save state
  324. * 5. load state
  325. * 6. Estimate parameters
  326. * 7. compare differences
  327. */
  328. circuit_build_times_t initial;
  329. circuit_build_times_t estimate;
  330. circuit_build_times_t final;
  331. double timeout1, timeout2;
  332. or_state_t *state=NULL;
  333. int i, runs;
  334. double close_ms;
  335. (void)arg;
  336. initialize_periodic_events();
  337. circuit_build_times_init(&initial);
  338. circuit_build_times_init(&estimate);
  339. circuit_build_times_init(&final);
  340. state = or_state_new();
  341. // Use a deterministic RNG here, or else we'll get nondeterministic
  342. // coverage in some of the circuitstats functions.
  343. MOCK(crypto_rand, crypto_rand_deterministic_aes);
  344. crypto_rand_aes_cipher = crypto_cipher_new("xyzzyplughplover");
  345. circuitbuild_running_unit_tests();
  346. #define timeout0 (build_time_t)(30*1000.0)
  347. initial.Xm = 3000;
  348. circuit_build_times_initial_alpha(&initial,
  349. CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
  350. timeout0);
  351. close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
  352. CBT_DEFAULT_CLOSE_QUANTILE/100.0),
  353. CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
  354. do {
  355. for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
  356. build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
  357. if (sample > close_ms) {
  358. circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
  359. } else {
  360. circuit_build_times_add_time(&estimate, sample);
  361. }
  362. }
  363. circuit_build_times_update_alpha(&estimate);
  364. timeout1 = circuit_build_times_calculate_timeout(&estimate,
  365. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  366. circuit_build_times_set_timeout(&estimate);
  367. log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
  368. /* 2% error */
  369. } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
  370. circuit_build_times_cdf(&initial, timeout1)) > 0.02);
  371. tt_int_op(estimate.total_build_times, OP_LE, CBT_NCIRCUITS_TO_OBSERVE);
  372. circuit_build_times_update_state(&estimate, state);
  373. circuit_build_times_free_timeouts(&final);
  374. tt_int_op(circuit_build_times_parse_state(&final, state), OP_EQ, 0);
  375. circuit_build_times_update_alpha(&final);
  376. timeout2 = circuit_build_times_calculate_timeout(&final,
  377. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  378. circuit_build_times_set_timeout(&final);
  379. log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
  380. /* 5% here because some accuracy is lost due to histogram conversion */
  381. tt_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
  382. circuit_build_times_cdf(&initial, timeout2)) < 0.05);
  383. for (runs = 0; runs < 50; runs++) {
  384. int build_times_idx = 0;
  385. int total_build_times = 0;
  386. final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  387. estimate.close_ms = estimate.timeout_ms
  388. = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  389. for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
  390. circuit_build_times_network_circ_success(&estimate);
  391. circuit_build_times_add_time(&estimate,
  392. circuit_build_times_generate_sample(&estimate, 0,
  393. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  394. circuit_build_times_network_circ_success(&estimate);
  395. circuit_build_times_add_time(&final,
  396. circuit_build_times_generate_sample(&final, 0,
  397. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  398. }
  399. tt_assert(!circuit_build_times_network_check_changed(&estimate));
  400. tt_assert(!circuit_build_times_network_check_changed(&final));
  401. /* Reset liveness to be non-live */
  402. final.liveness.network_last_live = 0;
  403. estimate.liveness.network_last_live = 0;
  404. build_times_idx = estimate.build_times_idx;
  405. total_build_times = estimate.total_build_times;
  406. tt_assert(circuit_build_times_network_check_live(&estimate));
  407. tt_assert(circuit_build_times_network_check_live(&final));
  408. circuit_build_times_count_close(&estimate, 0,
  409. (time_t)(approx_time()-estimate.close_ms/1000.0-1));
  410. circuit_build_times_count_close(&final, 0,
  411. (time_t)(approx_time()-final.close_ms/1000.0-1));
  412. tt_assert(!circuit_build_times_network_check_live(&estimate));
  413. tt_assert(!circuit_build_times_network_check_live(&final));
  414. log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
  415. build_times_idx, estimate.build_times_idx,
  416. total_build_times, estimate.total_build_times);
  417. /* Check rollback index. Should match top of loop. */
  418. tt_assert(build_times_idx == estimate.build_times_idx);
  419. // This can fail if estimate.total_build_times == 1000, because
  420. // in that case, rewind actually causes us to lose timeouts
  421. if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
  422. tt_assert(total_build_times == estimate.total_build_times);
  423. /* Now simulate that the network has become live and we need
  424. * a change */
  425. circuit_build_times_network_is_live(&estimate);
  426. circuit_build_times_network_is_live(&final);
  427. for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
  428. circuit_build_times_count_timeout(&estimate, 1);
  429. if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
  430. circuit_build_times_count_timeout(&final, 1);
  431. }
  432. }
  433. tt_int_op(estimate.liveness.after_firsthop_idx, OP_EQ, 0);
  434. tt_assert(final.liveness.after_firsthop_idx ==
  435. CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
  436. tt_assert(circuit_build_times_network_check_live(&estimate));
  437. tt_assert(circuit_build_times_network_check_live(&final));
  438. circuit_build_times_count_timeout(&final, 1);
  439. /* Ensure return value for degenerate cases are clamped correctly */
  440. initial.alpha = INT32_MAX;
  441. tt_assert(circuit_build_times_calculate_timeout(&initial, .99999999) <=
  442. INT32_MAX);
  443. initial.alpha = 0;
  444. tt_assert(circuit_build_times_calculate_timeout(&initial, .5) <=
  445. INT32_MAX);
  446. }
  447. done:
  448. circuit_build_times_free_timeouts(&initial);
  449. circuit_build_times_free_timeouts(&estimate);
  450. circuit_build_times_free_timeouts(&final);
  451. or_state_free(state);
  452. teardown_periodic_events();
  453. UNMOCK(crypto_rand);
  454. crypto_cipher_free(crypto_rand_aes_cipher);
  455. }
  456. /** Test encoding and parsing of rendezvous service descriptors. */
  457. static void
  458. test_rend_fns(void *arg)
  459. {
  460. rend_service_descriptor_t *generated = NULL, *parsed = NULL;
  461. char service_id[DIGEST_LEN];
  462. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  463. const char *next_desc;
  464. smartlist_t *descs = smartlist_new();
  465. char computed_desc_id[DIGEST_LEN];
  466. char parsed_desc_id[DIGEST_LEN];
  467. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  468. time_t now;
  469. char *intro_points_encrypted = NULL;
  470. size_t intro_points_size;
  471. size_t encoded_size;
  472. int i;
  473. (void)arg;
  474. /* Initialize the service cache. */
  475. rend_cache_init();
  476. pk1 = pk_generate(0);
  477. pk2 = pk_generate(1);
  478. generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  479. generated->pk = crypto_pk_dup_key(pk1);
  480. crypto_pk_get_digest(generated->pk, service_id);
  481. base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
  482. service_id, REND_SERVICE_ID_LEN);
  483. now = time(NULL);
  484. generated->timestamp = now;
  485. generated->version = 2;
  486. generated->protocols = 42;
  487. generated->intro_nodes = smartlist_new();
  488. for (i = 0; i < 3; i++) {
  489. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  490. crypto_pk_t *okey = pk_generate(2 + i);
  491. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  492. intro->extend_info->onion_key = okey;
  493. crypto_pk_get_digest(intro->extend_info->onion_key,
  494. intro->extend_info->identity_digest);
  495. //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
  496. intro->extend_info->nickname[0] = '$';
  497. base16_encode(intro->extend_info->nickname + 1,
  498. sizeof(intro->extend_info->nickname) - 1,
  499. intro->extend_info->identity_digest, DIGEST_LEN);
  500. /* Does not cover all IP addresses. */
  501. tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
  502. intro->extend_info->port = 1 + crypto_rand_int(65535);
  503. intro->intro_key = crypto_pk_dup_key(pk2);
  504. smartlist_add(generated->intro_nodes, intro);
  505. }
  506. int rv = rend_encode_v2_descriptors(descs, generated, now, 0,
  507. REND_NO_AUTH, NULL, NULL);
  508. tt_int_op(rv, OP_GT, 0);
  509. rv = rend_compute_v2_desc_id(computed_desc_id, service_id_base32, NULL,
  510. now, 0);
  511. tt_int_op(rv, OP_EQ, 0);
  512. tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
  513. smartlist_get(descs, 0))->desc_id, OP_EQ,
  514. computed_desc_id, DIGEST_LEN);
  515. rv = rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
  516. &intro_points_encrypted, &intro_points_size, &encoded_size,
  517. &next_desc,
  518. ((rend_encoded_v2_service_descriptor_t *)smartlist_get(descs, 0))
  519. ->desc_str, 1);
  520. tt_int_op(rv, OP_EQ, 0);
  521. tt_assert(parsed);
  522. tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
  523. smartlist_get(descs, 0))->desc_id,OP_EQ, parsed_desc_id, DIGEST_LEN);
  524. tt_int_op(rend_parse_introduction_points(parsed, intro_points_encrypted,
  525. intro_points_size),OP_EQ, 3);
  526. tt_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
  527. tt_int_op(parsed->timestamp,OP_EQ, now);
  528. tt_int_op(parsed->version,OP_EQ, 2);
  529. tt_int_op(parsed->protocols,OP_EQ, 42);
  530. tt_int_op(smartlist_len(parsed->intro_nodes),OP_EQ, 3);
  531. for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
  532. rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
  533. *gen_intro = smartlist_get(generated->intro_nodes, i);
  534. extend_info_t *par_info = par_intro->extend_info;
  535. extend_info_t *gen_info = gen_intro->extend_info;
  536. tt_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
  537. tt_mem_op(gen_info->identity_digest,OP_EQ, par_info->identity_digest,
  538. DIGEST_LEN);
  539. tt_str_op(gen_info->nickname,OP_EQ, par_info->nickname);
  540. tt_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
  541. tt_int_op(gen_info->port,OP_EQ, par_info->port);
  542. }
  543. rend_service_descriptor_free(parsed);
  544. rend_service_descriptor_free(generated);
  545. parsed = generated = NULL;
  546. done:
  547. if (descs) {
  548. for (i = 0; i < smartlist_len(descs); i++)
  549. rend_encoded_v2_service_descriptor_free_(smartlist_get(descs, i));
  550. smartlist_free(descs);
  551. }
  552. if (parsed)
  553. rend_service_descriptor_free(parsed);
  554. if (generated)
  555. rend_service_descriptor_free(generated);
  556. if (pk1)
  557. crypto_pk_free(pk1);
  558. if (pk2)
  559. crypto_pk_free(pk2);
  560. tor_free(intro_points_encrypted);
  561. }
  562. /** Run unit tests for stats code. */
  563. static void
  564. test_stats(void *arg)
  565. {
  566. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  567. char *s = NULL;
  568. int i;
  569. /* Start with testing exit port statistics; we shouldn't collect exit
  570. * stats without initializing them. */
  571. (void)arg;
  572. rep_hist_note_exit_stream_opened(80);
  573. rep_hist_note_exit_bytes(80, 100, 10000);
  574. s = rep_hist_format_exit_stats(now + 86400);
  575. tt_ptr_op(s, OP_EQ, NULL);
  576. /* Initialize stats, note some streams and bytes, and generate history
  577. * string. */
  578. rep_hist_exit_stats_init(now);
  579. rep_hist_note_exit_stream_opened(80);
  580. rep_hist_note_exit_bytes(80, 100, 10000);
  581. rep_hist_note_exit_stream_opened(443);
  582. rep_hist_note_exit_bytes(443, 100, 10000);
  583. rep_hist_note_exit_bytes(443, 100, 10000);
  584. s = rep_hist_format_exit_stats(now + 86400);
  585. tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  586. "exit-kibibytes-written 80=1,443=1,other=0\n"
  587. "exit-kibibytes-read 80=10,443=20,other=0\n"
  588. "exit-streams-opened 80=4,443=4,other=0\n",OP_EQ, s);
  589. tor_free(s);
  590. /* Add a few bytes on 10 more ports and ensure that only the top 10
  591. * ports are contained in the history string. */
  592. for (i = 50; i < 60; i++) {
  593. rep_hist_note_exit_bytes(i, i, i);
  594. rep_hist_note_exit_stream_opened(i);
  595. }
  596. s = rep_hist_format_exit_stats(now + 86400);
  597. tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  598. "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  599. "59=1,80=1,443=1,other=1\n"
  600. "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  601. "59=1,80=10,443=20,other=1\n"
  602. "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
  603. "59=4,80=4,443=4,other=4\n",OP_EQ, s);
  604. tor_free(s);
  605. /* Stop collecting stats, add some bytes, and ensure we don't generate
  606. * a history string. */
  607. rep_hist_exit_stats_term();
  608. rep_hist_note_exit_bytes(80, 100, 10000);
  609. s = rep_hist_format_exit_stats(now + 86400);
  610. tt_ptr_op(s, OP_EQ, NULL);
  611. /* Re-start stats, add some bytes, reset stats, and see what history we
  612. * get when observing no streams or bytes at all. */
  613. rep_hist_exit_stats_init(now);
  614. rep_hist_note_exit_stream_opened(80);
  615. rep_hist_note_exit_bytes(80, 100, 10000);
  616. rep_hist_reset_exit_stats(now);
  617. s = rep_hist_format_exit_stats(now + 86400);
  618. tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  619. "exit-kibibytes-written other=0\n"
  620. "exit-kibibytes-read other=0\n"
  621. "exit-streams-opened other=0\n",OP_EQ, s);
  622. tor_free(s);
  623. /* Continue with testing connection statistics; we shouldn't collect
  624. * conn stats without initializing them. */
  625. rep_hist_note_or_conn_bytes(1, 20, 400, now);
  626. s = rep_hist_format_conn_stats(now + 86400);
  627. tt_ptr_op(s, OP_EQ, NULL);
  628. /* Initialize stats, note bytes, and generate history string. */
  629. rep_hist_conn_stats_init(now);
  630. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  631. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  632. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  633. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  634. s = rep_hist_format_conn_stats(now + 86400);
  635. tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n",OP_EQ, s);
  636. tor_free(s);
  637. /* Stop collecting stats, add some bytes, and ensure we don't generate
  638. * a history string. */
  639. rep_hist_conn_stats_term();
  640. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  641. s = rep_hist_format_conn_stats(now + 86400);
  642. tt_ptr_op(s, OP_EQ, NULL);
  643. /* Re-start stats, add some bytes, reset stats, and see what history we
  644. * get when observing no bytes at all. */
  645. rep_hist_conn_stats_init(now);
  646. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  647. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  648. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  649. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  650. rep_hist_reset_conn_stats(now);
  651. s = rep_hist_format_conn_stats(now + 86400);
  652. tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",OP_EQ, s);
  653. tor_free(s);
  654. /* Continue with testing buffer statistics; we shouldn't collect buffer
  655. * stats without initializing them. */
  656. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  657. s = rep_hist_format_buffer_stats(now + 86400);
  658. tt_ptr_op(s, OP_EQ, NULL);
  659. /* Initialize stats, add statistics for a single circuit, and generate
  660. * the history string. */
  661. rep_hist_buffer_stats_init(now);
  662. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  663. s = rep_hist_format_buffer_stats(now + 86400);
  664. tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  665. "cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
  666. "cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  667. "0.00,0.00\n"
  668. "cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
  669. "cell-circuits-per-decile 1\n",OP_EQ, s);
  670. tor_free(s);
  671. /* Add nineteen more circuit statistics to the one that's already in the
  672. * history to see that the math works correctly. */
  673. for (i = 21; i < 30; i++)
  674. rep_hist_add_buffer_stats(2.0, 2.0, i);
  675. for (i = 20; i < 30; i++)
  676. rep_hist_add_buffer_stats(3.5, 3.5, i);
  677. s = rep_hist_format_buffer_stats(now + 86400);
  678. tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  679. "cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
  680. "cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
  681. "2.75,2.75\n"
  682. "cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
  683. "cell-circuits-per-decile 2\n",OP_EQ, s);
  684. tor_free(s);
  685. /* Stop collecting stats, add statistics for one circuit, and ensure we
  686. * don't generate a history string. */
  687. rep_hist_buffer_stats_term();
  688. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  689. s = rep_hist_format_buffer_stats(now + 86400);
  690. tt_ptr_op(s, OP_EQ, NULL);
  691. /* Re-start stats, add statistics for one circuit, reset stats, and make
  692. * sure that the history has all zeros. */
  693. rep_hist_buffer_stats_init(now);
  694. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  695. rep_hist_reset_buffer_stats(now);
  696. s = rep_hist_format_buffer_stats(now + 86400);
  697. tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  698. "cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
  699. "cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  700. "0.00,0.00\n"
  701. "cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
  702. "cell-circuits-per-decile 0\n",OP_EQ, s);
  703. done:
  704. tor_free(s);
  705. }
  706. #define ENT(name) \
  707. { #name, test_ ## name , 0, NULL, NULL }
  708. #define FORK(name) \
  709. { #name, test_ ## name , TT_FORK, NULL, NULL }
  710. static struct testcase_t test_array[] = {
  711. ENT(onion_handshake),
  712. { "bad_onion_handshake", test_bad_onion_handshake, 0, NULL, NULL },
  713. ENT(onion_queues),
  714. { "ntor_handshake", test_ntor_handshake, 0, NULL, NULL },
  715. { "fast_handshake", test_fast_handshake, 0, NULL, NULL },
  716. FORK(circuit_timeout),
  717. FORK(rend_fns),
  718. FORK(stats),
  719. END_OF_TESTCASES
  720. };
  721. struct testgroup_t testgroups[] = {
  722. { "", test_array },
  723. { "accounting/", accounting_tests },
  724. { "addr/", addr_tests },
  725. { "address/", address_tests },
  726. { "address_set/", address_set_tests },
  727. { "bridges/", bridges_tests },
  728. { "buffer/", buffer_tests },
  729. { "bwmgt/", bwmgt_tests },
  730. { "cellfmt/", cell_format_tests },
  731. { "cellqueue/", cell_queue_tests },
  732. { "channel/", channel_tests },
  733. { "channelpadding/", channelpadding_tests },
  734. { "channeltls/", channeltls_tests },
  735. { "checkdir/", checkdir_tests },
  736. { "circuitbuild/", circuitbuild_tests },
  737. { "circuitlist/", circuitlist_tests },
  738. { "circuitmux/", circuitmux_tests },
  739. { "circuituse/", circuituse_tests },
  740. { "circuitstats/", circuitstats_tests },
  741. { "compat/libevent/", compat_libevent_tests },
  742. { "config/", config_tests },
  743. { "connection/", connection_tests },
  744. { "conscache/", conscache_tests },
  745. { "consdiff/", consdiff_tests },
  746. { "consdiffmgr/", consdiffmgr_tests },
  747. { "container/", container_tests },
  748. { "control/", controller_tests },
  749. { "control/event/", controller_event_tests },
  750. { "crypto/", crypto_tests },
  751. { "crypto/openssl/", crypto_openssl_tests },
  752. { "dir/", dir_tests },
  753. { "dir_handle_get/", dir_handle_get_tests },
  754. { "dir/md/", microdesc_tests },
  755. { "dir/voting-schedule/", voting_schedule_tests },
  756. { "dos/", dos_tests },
  757. { "entryconn/", entryconn_tests },
  758. { "entrynodes/", entrynodes_tests },
  759. { "guardfraction/", guardfraction_tests },
  760. { "extorport/", extorport_tests },
  761. { "geoip/", geoip_tests },
  762. { "legacy_hs/", hs_tests },
  763. { "hs_cache/", hs_cache },
  764. { "hs_cell/", hs_cell_tests },
  765. { "hs_common/", hs_common_tests },
  766. { "hs_config/", hs_config_tests },
  767. { "hs_control/", hs_control_tests },
  768. { "hs_descriptor/", hs_descriptor },
  769. { "hs_ntor/", hs_ntor_tests },
  770. { "hs_service/", hs_service_tests },
  771. { "hs_client/", hs_client_tests },
  772. { "hs_intropoint/", hs_intropoint_tests },
  773. { "introduce/", introduce_tests },
  774. { "keypin/", keypin_tests },
  775. { "link-handshake/", link_handshake_tests },
  776. { "mainloop/", mainloop_tests },
  777. { "nodelist/", nodelist_tests },
  778. { "oom/", oom_tests },
  779. { "oos/", oos_tests },
  780. { "options/", options_tests },
  781. { "periodic-event/" , periodic_event_tests },
  782. { "policy/" , policy_tests },
  783. { "procmon/", procmon_tests },
  784. { "proto/http/", proto_http_tests },
  785. { "proto/misc/", proto_misc_tests },
  786. { "protover/", protover_tests },
  787. { "pt/", pt_tests },
  788. { "relay/" , relay_tests },
  789. { "relaycell/", relaycell_tests },
  790. { "relaycrypt/", relaycrypt_tests },
  791. { "rend_cache/", rend_cache_tests },
  792. { "replaycache/", replaycache_tests },
  793. { "router/", router_tests },
  794. { "routerkeys/", routerkeys_tests },
  795. { "routerlist/", routerlist_tests },
  796. { "routerset/" , routerset_tests },
  797. { "scheduler/", scheduler_tests },
  798. { "socks/", socks_tests },
  799. { "shared-random/", sr_tests },
  800. { "status/" , status_tests },
  801. { "storagedir/", storagedir_tests },
  802. { "tortls/", tortls_tests },
  803. { "util/", util_tests },
  804. { "util/format/", util_format_tests },
  805. { "util/logging/", logging_tests },
  806. { "util/process/", util_process_tests },
  807. { "util/thread/", thread_tests },
  808. { "util/handle/", handle_tests },
  809. { "dns/", dns_tests },
  810. END_OF_GROUPS
  811. };