test.c 33 KB

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