test.c 32 KB

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