test.c 33 KB

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