tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test.c (25732B)


      1 /* Copyright (c) 2001-2004, Roger Dingledine.
      2 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      3 /* See LICENSE for licensing information */
      4 
      5 /**
      6 * \file test.c
      7 * \brief Unit tests for many pieces of the lower level Tor modules.
      8 **/
      9 
     10 #include "orconfig.h"
     11 #include "lib/crypt_ops/crypto_dh.h"
     12 #include "lib/crypt_ops/crypto_rand.h"
     13 #include "app/config/or_state_st.h"
     14 #include "test/rng_test_helpers.h"
     15 
     16 #include <stdio.h>
     17 #ifdef HAVE_FCNTL_H
     18 #include <fcntl.h>
     19 #endif
     20 
     21 #ifdef _WIN32
     22 /* For mkdir() */
     23 #include <direct.h>
     24 #else
     25 #include <dirent.h>
     26 #endif /* defined(_WIN32) */
     27 
     28 #include <math.h>
     29 
     30 /* These macros pull in declarations for some functions and structures that
     31 * are typically file-private. */
     32 #define ROUTER_PRIVATE
     33 #define CIRCUITSTATS_PRIVATE
     34 #define CIRCUITLIST_PRIVATE
     35 #define MAINLOOP_PRIVATE
     36 #define STATEFILE_PRIVATE
     37 
     38 #include "core/or/or.h"
     39 #include "lib/err/backtrace.h"
     40 #include "lib/buf/buffers.h"
     41 #include "core/or/circuitlist.h"
     42 #include "core/or/circuitstats.h"
     43 #include "lib/compress/compress.h"
     44 #include "app/config/config.h"
     45 #include "core/or/connection_edge.h"
     46 #include "core/or/extendinfo.h"
     47 #include "test/test.h"
     48 #include "core/mainloop/mainloop.h"
     49 #include "lib/memarea/memarea.h"
     50 #include "core/or/onion.h"
     51 #include "core/crypto/onion_ntor.h"
     52 #include "core/crypto/onion_fast.h"
     53 #include "core/or/policies.h"
     54 #include "lib/sandbox/sandbox.h"
     55 #include "app/config/statefile.h"
     56 #include "lib/crypt_ops/crypto_curve25519.h"
     57 #include "feature/nodelist/networkstatus.h"
     58 
     59 #include "core/or/extend_info_st.h"
     60 #include "core/or/or_circuit_st.h"
     61 #include "feature/relay/onion_queue.h"
     62 
     63 static void
     64 test_ntor_handshake(void *arg)
     65 {
     66  /* client-side */
     67  ntor_handshake_state_t *c_state = NULL;
     68  uint8_t c_buf[NTOR_ONIONSKIN_LEN];
     69  uint8_t c_keys[400];
     70 
     71  /* server-side */
     72  di_digest256_map_t *s_keymap=NULL;
     73  curve25519_keypair_t s_keypair;
     74  uint8_t s_buf[NTOR_REPLY_LEN];
     75  uint8_t s_keys[400];
     76 
     77  /* shared */
     78  const curve25519_public_key_t *server_pubkey;
     79 
     80  NONSTRING uint8_t node_id[20] = "abcdefghijklmnopqrst";
     81 
     82  (void) arg;
     83 
     84  /* Make the server some keys */
     85  curve25519_secret_key_generate(&s_keypair.seckey, 0);
     86  curve25519_public_key_generate(&s_keypair.pubkey, &s_keypair.seckey);
     87  dimap_add_entry(&s_keymap, s_keypair.pubkey.public_key, &s_keypair);
     88  server_pubkey = &s_keypair.pubkey;
     89 
     90  /* client handshake 1. */
     91  memset(c_buf, 0, NTOR_ONIONSKIN_LEN);
     92  tt_int_op(0, OP_EQ, onion_skin_ntor_create(node_id, server_pubkey,
     93                                          &c_state, c_buf));
     94 
     95  /* server handshake */
     96  memset(s_buf, 0, NTOR_REPLY_LEN);
     97  memset(s_keys, 0, 40);
     98  tt_int_op(0, OP_EQ, onion_skin_ntor_server_handshake(c_buf, s_keymap, NULL,
     99                                                    node_id,
    100                                                    s_buf, s_keys, 400));
    101 
    102  /* client handshake 2 */
    103  memset(c_keys, 0, 40);
    104  tt_int_op(0, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
    105                                                       c_keys, 400, NULL));
    106 
    107  tt_mem_op(c_keys,OP_EQ, s_keys, 400);
    108  memset(s_buf, 0, 40);
    109  tt_mem_op(c_keys,OP_NE, s_buf, 40);
    110 
    111  /* Now try with a bogus server response. Zero input should trigger
    112   * All The Problems. */
    113  memset(c_keys, 0, 400);
    114  memset(s_buf, 0, NTOR_REPLY_LEN);
    115  const char *msg = NULL;
    116  tt_int_op(-1, OP_EQ, onion_skin_ntor_client_handshake(c_state, s_buf,
    117                                                        c_keys, 400, &msg));
    118  tt_str_op(msg, OP_EQ, "Zero output from curve25519 handshake");
    119 
    120 done:
    121  ntor_handshake_state_free(c_state);
    122  dimap_free(s_keymap, NULL);
    123 }
    124 
    125 static void
    126 test_fast_handshake(void *arg)
    127 {
    128  /* tests for the obsolete "CREATE_FAST" handshake. */
    129  (void) arg;
    130  fast_handshake_state_t *state = NULL;
    131  uint8_t client_handshake[CREATE_FAST_LEN];
    132  uint8_t server_handshake[CREATED_FAST_LEN];
    133  uint8_t s_keys[100], c_keys[100];
    134 
    135  /* First, test an entire handshake. */
    136  memset(client_handshake, 0, sizeof(client_handshake));
    137  tt_int_op(0, OP_EQ, fast_onionskin_create(&state, client_handshake));
    138  tt_assert(! fast_mem_is_zero((char*)client_handshake,
    139                              sizeof(client_handshake)));
    140 
    141  tt_int_op(0, OP_EQ,
    142            fast_server_handshake(client_handshake, server_handshake,
    143                                  s_keys, 100));
    144  const char *msg = NULL;
    145  tt_int_op(0, OP_EQ,
    146            fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
    147  tt_ptr_op(msg, OP_EQ, NULL);
    148  tt_mem_op(s_keys, OP_EQ, c_keys, 100);
    149 
    150  /* Now test a failing handshake. */
    151  server_handshake[0] ^= 3;
    152  tt_int_op(-1, OP_EQ,
    153            fast_client_handshake(state, server_handshake, c_keys, 100, &msg));
    154  tt_str_op(msg, OP_EQ, "Digest DOES NOT MATCH on fast handshake. "
    155            "Bug or attack.");
    156 
    157 done:
    158  fast_handshake_state_free(state);
    159 }
    160 
    161 /** Run unit tests for the onion queues. */
    162 static void
    163 test_onion_queues(void *arg)
    164 {
    165  uint8_t buf1[NTOR_ONIONSKIN_LEN] = {0};
    166  uint8_t buf2[NTOR_ONIONSKIN_LEN] = {0};
    167 
    168  or_circuit_t *circ1 = or_circuit_new(0, NULL);
    169  or_circuit_t *circ2 = or_circuit_new(0, NULL);
    170 
    171  create_cell_t *onionskin = NULL, *create1_ptr;
    172  create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
    173  create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
    174  (void)arg;
    175  create1_ptr = create1; /* remember, but do not free */
    176 
    177  create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
    178                   NTOR_ONIONSKIN_LEN, buf1);
    179  create_cell_init(create2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
    180                   NTOR_ONIONSKIN_LEN, buf2);
    181 
    182  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    183  tt_int_op(0,OP_EQ, onion_pending_add(circ1, create1));
    184  create1 = NULL;
    185  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    186 
    187  tt_int_op(0,OP_EQ, onion_pending_add(circ2, create2));
    188  create2 = NULL;
    189  tt_int_op(2,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    190 
    191  tt_ptr_op(circ1,OP_EQ, onion_next_task(&onionskin));
    192  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    193  tt_ptr_op(onionskin, OP_EQ, create1_ptr);
    194 
    195  clear_pending_onions();
    196  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    197  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    198 
    199 done:
    200  circuit_free_(TO_CIRCUIT(circ1));
    201  circuit_free_(TO_CIRCUIT(circ2));
    202  tor_free(create1);
    203  tor_free(create2);
    204  tor_free(onionskin);
    205 }
    206 
    207 /**
    208 * Test onion queue priority, separation, and resulting
    209 * ordering.
    210 *
    211 * create and add a mix of TAP, NTOR2, and NTORv3. Ensure
    212 * they all end up in the right queue. In particular, ntorv2
    213 * and ntorv3 should share a queue, but TAP should be separate,
    214 * and lower prioritt.
    215 *
    216 * We test this by way of adding TAP first, and then an interleaving
    217 * order of ntor2 and ntor3, and check that the ntor2 and ntor3 are
    218 * still interleaved, but TAP comes last. */
    219 static void
    220 test_onion_queue_order(void *arg)
    221 {
    222  uint8_t buf_ntor[NTOR_ONIONSKIN_LEN] = {0};
    223  uint8_t buf_ntor3[CELL_PAYLOAD_SIZE] = {0};
    224 
    225  or_circuit_t *circ_ntor = or_circuit_new(0, NULL);
    226  or_circuit_t *circ_ntor3 = or_circuit_new(0, NULL);
    227 
    228  create_cell_t *onionskin = NULL;
    229  create_cell_t *create_ntor1 = tor_malloc_zero(sizeof(create_cell_t));
    230  create_cell_t *create_ntor2 = tor_malloc_zero(sizeof(create_cell_t));
    231  create_cell_t *create_v3ntor1 = tor_malloc_zero(sizeof(create_cell_t));
    232  create_cell_t *create_v3ntor2 = tor_malloc_zero(sizeof(create_cell_t));
    233  (void)arg;
    234 
    235  create_cell_init(create_ntor1, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
    236                   NTOR_ONIONSKIN_LEN, buf_ntor);
    237  create_cell_init(create_ntor2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
    238                   NTOR_ONIONSKIN_LEN, buf_ntor);
    239  create_cell_init(create_v3ntor1, CELL_CREATE2, ONION_HANDSHAKE_TYPE_NTOR_V3,
    240                   NTOR_ONIONSKIN_LEN, buf_ntor3);
    241  create_cell_init(create_v3ntor2, CELL_CREATE2, ONION_HANDSHAKE_TYPE_NTOR_V3,
    242                   NTOR_ONIONSKIN_LEN, buf_ntor3);
    243 
    244  /* sanity check queue init */
    245  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    246  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    247 
    248  /* Now add interleaving ntor2 and ntor3, to ensure they share
    249   * the same queue and come out in this order */
    250  tt_int_op(0,OP_EQ, onion_pending_add(circ_ntor, create_ntor1));
    251  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    252  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    253  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    254 
    255  tt_int_op(0,OP_EQ, onion_pending_add(circ_ntor3, create_v3ntor1));
    256  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    257  tt_int_op(2,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    258  tt_int_op(2,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    259 
    260  tt_int_op(0,OP_EQ, onion_pending_add(circ_ntor, create_ntor2));
    261  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    262  tt_int_op(3,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    263  tt_int_op(3,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    264 
    265  tt_int_op(0,OP_EQ, onion_pending_add(circ_ntor3, create_v3ntor2));
    266  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    267  tt_int_op(4,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    268  tt_int_op(4,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    269 
    270  /* Now remove 5 tasks, ensuring order and queue sizes */
    271  tt_ptr_op(circ_ntor, OP_EQ, onion_next_task(&onionskin));
    272  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    273  tt_int_op(3,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    274  tt_int_op(3,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    275  tt_ptr_op(onionskin, OP_EQ, create_ntor1);
    276 
    277  tt_ptr_op(circ_ntor3, OP_EQ, onion_next_task(&onionskin));
    278  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    279  tt_int_op(2,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    280  tt_int_op(2,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    281  tt_ptr_op(onionskin, OP_EQ, create_v3ntor1);
    282 
    283  tt_ptr_op(circ_ntor, OP_EQ, onion_next_task(&onionskin));
    284  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    285  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    286  tt_int_op(1,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    287  tt_ptr_op(onionskin, OP_EQ, create_ntor2);
    288 
    289  tt_ptr_op(circ_ntor3, OP_EQ, onion_next_task(&onionskin));
    290  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    291  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    292  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR_V3));
    293  tt_ptr_op(onionskin, OP_EQ, create_v3ntor2);
    294 
    295  clear_pending_onions();
    296  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
    297  tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
    298 
    299 done:
    300  circuit_free_(TO_CIRCUIT(circ_ntor));
    301  circuit_free_(TO_CIRCUIT(circ_ntor3));
    302  tor_free(create_ntor1);
    303  tor_free(create_ntor2);
    304  tor_free(create_v3ntor1);
    305  tor_free(create_v3ntor2);
    306 }
    307 
    308 static int32_t cbtnummodes = 10;
    309 
    310 static int32_t
    311 mock_xm_networkstatus_get_param(
    312    const networkstatus_t *ns, const char *param_name, int32_t default_val,
    313    int32_t min_val, int32_t max_val)
    314 {
    315  (void)ns;
    316  (void)default_val;
    317  (void)min_val;
    318  (void)max_val;
    319  // only support cbtnummodes right now
    320  tor_assert(strcmp(param_name, "cbtnummodes")==0);
    321  return cbtnummodes;
    322 }
    323 
    324 static void
    325 test_circuit_timeout_xm_alpha(void *arg)
    326 {
    327  circuit_build_times_t cbt;
    328  build_time_t Xm;
    329  int alpha_ret;
    330  circuit_build_times_init(&cbt);
    331  (void)arg;
    332 
    333  /* Plan:
    334   * 1. Create array of build times with 10 modes.
    335   * 2. Make sure Xm calc is sane for 1,3,5,10,15,20 modes.
    336   * 3. Make sure alpha calc is sane for 1,3,5,10,15,20 modes.
    337   */
    338 
    339  /* 110 build times, 9 modes, 8 mode ties, 10 abandoned */
    340  build_time_t circuit_build_times[] = {
    341    100, 20, 1000, 500, 200, 5000, 30, 600, 200, 300, CBT_BUILD_ABANDONED,
    342    101, 21, 1001, 501, 201, 5001, 31, 601, 201, 301, CBT_BUILD_ABANDONED,
    343    102, 22, 1002, 502, 202, 5002, 32, 602, 202, 302, CBT_BUILD_ABANDONED,
    344    103, 23, 1003, 503, 203, 5003, 33, 603, 203, 303, CBT_BUILD_ABANDONED,
    345    104, 24, 1004, 504, 204, 5004, 34, 604, 204, 304, CBT_BUILD_ABANDONED,
    346    105, 25, 1005, 505, 205, 5005, 35, 605, 205, 305, CBT_BUILD_ABANDONED,
    347    106, 26, 1006, 506, 206, 5006, 36, 606, 206, 306, CBT_BUILD_ABANDONED,
    348    107, 27, 1007, 507, 207, 5007, 37, 607, 207, 307, CBT_BUILD_ABANDONED,
    349    108, 28, 1008, 508, 208, 5008, 38, 608, 208, 308, CBT_BUILD_ABANDONED,
    350    109, 29, 1009, 509, 209, 5009, 39, 609, 209, 309, CBT_BUILD_ABANDONED
    351  };
    352 
    353  memcpy(cbt.circuit_build_times, circuit_build_times,
    354         sizeof(circuit_build_times));
    355  cbt.total_build_times = 110;
    356 
    357  MOCK(networkstatus_get_param, mock_xm_networkstatus_get_param);
    358 
    359 #define CBT_ALPHA_PRECISION 0.00001
    360  cbtnummodes = 1;
    361  Xm = circuit_build_times_get_xm(&cbt);
    362  alpha_ret = circuit_build_times_update_alpha(&cbt);
    363  tt_int_op(alpha_ret, OP_EQ, 1);
    364  tt_int_op(Xm, OP_EQ, 205);
    365  tt_assert(fabs(cbt.alpha - 1.394401) < CBT_ALPHA_PRECISION);
    366 
    367  cbtnummodes = 3;
    368  Xm = circuit_build_times_get_xm(&cbt);
    369  alpha_ret = circuit_build_times_update_alpha(&cbt);
    370  tt_int_op(alpha_ret, OP_EQ, 1);
    371  tt_int_op(Xm, OP_EQ, 117);
    372  tt_assert(fabs(cbt.alpha - 0.902313) < CBT_ALPHA_PRECISION);
    373 
    374  cbtnummodes = 5;
    375  Xm = circuit_build_times_get_xm(&cbt);
    376  alpha_ret = circuit_build_times_update_alpha(&cbt);
    377  tt_int_op(alpha_ret, OP_EQ, 1);
    378  tt_int_op(Xm, OP_EQ, 146);
    379  tt_assert(fabs(cbt.alpha - 1.049032) < CBT_ALPHA_PRECISION);
    380 
    381  cbtnummodes = 10;
    382  Xm = circuit_build_times_get_xm(&cbt);
    383  alpha_ret = circuit_build_times_update_alpha(&cbt);
    384  tt_int_op(alpha_ret, OP_EQ, 1);
    385  tt_int_op(Xm, OP_EQ, 800);
    386  tt_assert(fabs(cbt.alpha - 4.851754) < CBT_ALPHA_PRECISION);
    387 
    388  cbtnummodes = 15;
    389  Xm = circuit_build_times_get_xm(&cbt);
    390  alpha_ret = circuit_build_times_update_alpha(&cbt);
    391  tt_int_op(alpha_ret, OP_EQ, 1);
    392  tt_int_op(Xm, OP_EQ, 800);
    393  tt_assert(fabs(cbt.alpha - 4.851754) < CBT_ALPHA_PRECISION);
    394 
    395  cbtnummodes = 20;
    396  Xm = circuit_build_times_get_xm(&cbt);
    397  alpha_ret = circuit_build_times_update_alpha(&cbt);
    398  tt_int_op(alpha_ret, OP_EQ, 1);
    399  tt_int_op(Xm, OP_EQ, 800);
    400  tt_assert(fabs(cbt.alpha - 4.851754) < CBT_ALPHA_PRECISION);
    401 
    402 done:
    403 #undef CBT_ALPHA_PRECISION
    404  UNMOCK(networkstatus_get_param);
    405  circuit_build_times_free_timeouts(&cbt);
    406 }
    407 
    408 static void
    409 test_circuit_timeout(void *arg)
    410 {
    411  /* Plan:
    412   *  1. Generate 1000 samples
    413   *  2. Estimate parameters
    414   *  3. If difference, repeat
    415   *  4. Save state
    416   *  5. load state
    417   *  6. Estimate parameters
    418   *  7. compare differences
    419   */
    420  circuit_build_times_t initial;
    421  circuit_build_times_t estimate;
    422  circuit_build_times_t final;
    423  double timeout1, timeout2;
    424  or_state_t *state=NULL;
    425  int i, runs;
    426  (void)arg;
    427 
    428  initialize_periodic_events();
    429 
    430  circuit_build_times_init(&initial);
    431  circuit_build_times_init(&estimate);
    432  circuit_build_times_init(&final);
    433 
    434  state = or_state_new();
    435 
    436  // Use a deterministic RNG here, or else we'll get nondeterministic
    437  // coverage in some of the circuitstats functions.
    438  testing_enable_deterministic_rng();
    439 
    440  circuitbuild_running_unit_tests();
    441 #define timeout0 (build_time_t)(30*1000.0)
    442  initial.Xm = 3000;
    443  circuit_build_times_initial_alpha(&initial,
    444                                    CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
    445                                    timeout0);
    446  do {
    447    for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
    448      build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
    449 
    450      circuit_build_times_add_time(&estimate, sample);
    451    }
    452    circuit_build_times_update_alpha(&estimate);
    453    timeout1 = circuit_build_times_calculate_timeout(&estimate,
    454                                  CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
    455    circuit_build_times_set_timeout(&estimate);
    456    log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
    457           /* 2% error */
    458  } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
    459                circuit_build_times_cdf(&initial, timeout1)) > 0.02);
    460 
    461  tt_int_op(estimate.total_build_times, OP_LE, CBT_NCIRCUITS_TO_OBSERVE);
    462 
    463  circuit_build_times_update_state(&estimate, state);
    464  circuit_build_times_free_timeouts(&final);
    465  tt_int_op(circuit_build_times_parse_state(&final, state), OP_EQ, 0);
    466 
    467  circuit_build_times_update_alpha(&final);
    468  timeout2 = circuit_build_times_calculate_timeout(&final,
    469                                 CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
    470 
    471  circuit_build_times_set_timeout(&final);
    472  log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
    473 
    474  /* 5% here because some accuracy is lost due to histogram conversion */
    475  tt_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
    476                   circuit_build_times_cdf(&initial, timeout2)) < 0.05);
    477 
    478  for (runs = 0; runs < 50; runs++) {
    479    int build_times_idx = 0;
    480    int total_build_times = 0;
    481 
    482    final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
    483    estimate.close_ms = estimate.timeout_ms
    484                      = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
    485 
    486    for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
    487      circuit_build_times_network_circ_success(&estimate);
    488      circuit_build_times_add_time(&estimate,
    489            circuit_build_times_generate_sample(&estimate, 0,
    490                CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
    491 
    492      circuit_build_times_network_circ_success(&estimate);
    493      circuit_build_times_add_time(&final,
    494            circuit_build_times_generate_sample(&final, 0,
    495                CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
    496    }
    497 
    498    tt_assert(!circuit_build_times_network_check_changed(&estimate));
    499    tt_assert(!circuit_build_times_network_check_changed(&final));
    500 
    501    /* Reset liveness to be non-live */
    502    final.liveness.network_last_live = 0;
    503    estimate.liveness.network_last_live = 0;
    504 
    505    build_times_idx = estimate.build_times_idx;
    506    total_build_times = estimate.total_build_times;
    507 
    508    tt_assert(circuit_build_times_network_check_live(&estimate));
    509    tt_assert(circuit_build_times_network_check_live(&final));
    510 
    511    circuit_build_times_count_close(&estimate, 0,
    512            (time_t)(approx_time()-estimate.close_ms/1000.0-1));
    513    circuit_build_times_count_close(&final, 0,
    514            (time_t)(approx_time()-final.close_ms/1000.0-1));
    515 
    516    tt_assert(!circuit_build_times_network_check_live(&estimate));
    517    tt_assert(!circuit_build_times_network_check_live(&final));
    518 
    519    log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
    520             build_times_idx, estimate.build_times_idx,
    521             total_build_times, estimate.total_build_times);
    522 
    523    /* Check rollback index. Should match top of loop. */
    524    tt_assert(build_times_idx == estimate.build_times_idx);
    525    // This can fail if estimate.total_build_times == 1000, because
    526    // in that case, rewind actually causes us to lose timeouts
    527    if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
    528      tt_assert(total_build_times == estimate.total_build_times);
    529 
    530    /* Now simulate that the network has become live and we need
    531     * a change */
    532    circuit_build_times_network_is_live(&estimate);
    533    circuit_build_times_network_is_live(&final);
    534 
    535    for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
    536      circuit_build_times_count_timeout(&estimate, 1);
    537 
    538      if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
    539        circuit_build_times_count_timeout(&final, 1);
    540      }
    541    }
    542 
    543    tt_int_op(estimate.liveness.after_firsthop_idx, OP_EQ, 0);
    544    tt_assert(final.liveness.after_firsthop_idx ==
    545                CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
    546 
    547    tt_assert(circuit_build_times_network_check_live(&estimate));
    548    tt_assert(circuit_build_times_network_check_live(&final));
    549 
    550    circuit_build_times_count_timeout(&final, 1);
    551 
    552    /* Ensure return value for degenerate cases are clamped correctly */
    553    initial.alpha = INT32_MAX;
    554    tt_assert(circuit_build_times_calculate_timeout(&initial, .99999999) <=
    555              INT32_MAX);
    556    initial.alpha = 0;
    557    tt_assert(circuit_build_times_calculate_timeout(&initial, .5) <=
    558              INT32_MAX);
    559  }
    560 
    561 done:
    562  circuit_build_times_free_timeouts(&initial);
    563  circuit_build_times_free_timeouts(&estimate);
    564  circuit_build_times_free_timeouts(&final);
    565  or_state_free(state);
    566  teardown_periodic_events();
    567 
    568  testing_disable_deterministic_rng();
    569 }
    570 
    571 #define ENT(name)                                                       \
    572  { #name, test_ ## name , 0, NULL, NULL }
    573 #define FORK(name)                                                      \
    574  { #name, test_ ## name , TT_FORK, NULL, NULL }
    575 
    576 static struct testcase_t test_array[] = {
    577  ENT(onion_queues),
    578  ENT(onion_queue_order),
    579  { "ntor_handshake", test_ntor_handshake, 0, NULL, NULL },
    580  { "fast_handshake", test_fast_handshake, 0, NULL, NULL },
    581  FORK(circuit_timeout),
    582  FORK(circuit_timeout_xm_alpha),
    583 
    584  END_OF_TESTCASES
    585 };
    586 
    587 struct testgroup_t testgroups[] = {
    588  { "", test_array },
    589  { "accounting/", accounting_tests },
    590  { "addr/", addr_tests },
    591  { "address/", address_tests },
    592  { "address_set/", address_set_tests },
    593  { "bridges/", bridges_tests },
    594  { "buffer/", buffer_tests },
    595  { "bwmgt/", bwmgt_tests },
    596  { "cellfmt/", cell_format_tests },
    597  { "cellqueue/", cell_queue_tests },
    598  { "channel/", channel_tests },
    599  { "channelpadding/", channelpadding_tests },
    600  { "channeltls/", channeltls_tests },
    601  { "checkdir/", checkdir_tests },
    602  { "circuitbuild/", circuitbuild_tests },
    603  { "circuitpadding/", circuitpadding_tests },
    604  { "circuitlist/", circuitlist_tests },
    605  { "circuitmux/", circuitmux_tests },
    606  { "circuitmux_ewma/", circuitmux_ewma_tests },
    607  { "circuitstats/", circuitstats_tests },
    608  { "circuituse/", circuituse_tests },
    609  { "compat/libevent/", compat_libevent_tests },
    610  { "config/", config_tests },
    611  { "config/mgr/", confmgr_tests },
    612  { "config/parse/", confparse_tests },
    613  { "conflux/cell/", conflux_cell_tests },
    614  { "conflux/pool/", conflux_pool_tests },
    615  { "congestion_control/", congestion_control_tests },
    616  { "connection/", connection_tests },
    617  { "conscache/", conscache_tests },
    618  { "consdiff/", consdiff_tests },
    619  { "consdiffmgr/", consdiffmgr_tests },
    620  { "container/", container_tests },
    621  { "container/namemap/", namemap_tests },
    622  { "control/", controller_tests },
    623  { "control/btrack/", btrack_tests },
    624  { "control/event/", controller_event_tests },
    625  { "crypto/", crypto_tests },
    626  { "crypto/ope/", crypto_ope_tests },
    627 #ifdef ENABLE_OPENSSL
    628  { "crypto/openssl/", crypto_openssl_tests },
    629 #endif
    630  { "crypto/pem/", pem_tests },
    631  { "crypto/rng/", crypto_rng_tests },
    632  { "crypto/cgo/", crypto_cgo_tests },
    633  { "dir/", dir_tests },
    634  { "dir/auth/ports/", dirauth_port_tests },
    635  { "dir/auth/process_descs/", process_descs_tests },
    636  { "dir/md/", microdesc_tests },
    637  { "dirauth/dirvote/", dirvote_tests},
    638  { "dir/voting/flags/", voting_flags_tests },
    639  { "dir/voting/schedule/", voting_schedule_tests },
    640  { "dir_handle_get/", dir_handle_get_tests },
    641  { "dispatch/", dispatch_tests, },
    642  { "dns/", dns_tests },
    643  { "dos/", dos_tests },
    644  { "entryconn/", entryconn_tests },
    645  { "entrynodes/", entrynodes_tests },
    646  { "extorport/", extorport_tests },
    647  { "geoip/", geoip_tests },
    648  { "guardfraction/", guardfraction_tests },
    649  { "hs_cache/", hs_cache },
    650  { "hs_cell/", hs_cell_tests },
    651  { "hs_client/", hs_client_tests },
    652  { "hs_common/", hs_common_tests },
    653  { "hs_config/", hs_config_tests },
    654  { "hs_control/", hs_control_tests },
    655  { "hs_descriptor/", hs_descriptor },
    656  { "hs_dos/", hs_dos_tests },
    657  { "hs_intropoint/", hs_intropoint_tests },
    658  { "hs_metrics/", hs_metrics_tests },
    659  { "hs_ntor/", hs_ntor_tests },
    660  { "hs_ob/", hs_ob_tests },
    661  { "hs_pow/", hs_pow_tests },
    662  { "hs_service/", hs_service_tests },
    663  { "keypin/", keypin_tests },
    664  { "link-handshake/", link_handshake_tests },
    665  { "mainloop/", mainloop_tests },
    666  { "metrics/", metrics_tests },
    667  { "netinfo/", netinfo_tests },
    668  { "nodelist/", nodelist_tests },
    669  { "oom/", oom_tests },
    670  { "onion-handshake/ntor-v3/", ntor_v3_tests },
    671  { "oos/", oos_tests },
    672  { "options/", options_tests },
    673  { "options/act/", options_act_tests },
    674  { "parsecommon/", parsecommon_tests },
    675  { "periodic-event/" , periodic_event_tests },
    676  { "policy/" , policy_tests },
    677  { "prob_distr/", prob_distr_tests },
    678  { "procmon/", procmon_tests },
    679  { "process/", process_tests },
    680  { "proto/haproxy/", proto_haproxy_tests },
    681  { "proto/http/", proto_http_tests },
    682  { "proto/misc/", proto_misc_tests },
    683  { "protover/", protover_tests },
    684  { "pt/", pt_tests },
    685  { "pubsub/build/", pubsub_build_tests },
    686  { "pubsub/msg/", pubsub_msg_tests },
    687  { "relay/" , relay_tests },
    688  { "relaycell/", relaycell_tests },
    689  { "relaycrypt/", relaycrypt_tests },
    690  { "replaycache/", replaycache_tests },
    691  { "router/", router_tests },
    692  { "routerkeys/", routerkeys_tests },
    693  { "routerlist/", routerlist_tests },
    694  { "routerset/" , routerset_tests },
    695 #ifdef USE_LIBSECCOMP
    696  { "sandbox/" , sandbox_tests },
    697 #endif
    698  { "scheduler/", scheduler_tests },
    699  { "sendme/", sendme_tests },
    700  { "shared-random/", sr_tests },
    701  { "socks/", socks_tests },
    702  { "statefile/", statefile_tests },
    703  { "stats/", stats_tests },
    704  { "status/" , status_tests },
    705  { "storagedir/", storagedir_tests },
    706  { "token_bucket/", token_bucket_tests },
    707  { "tortls/", tortls_tests },
    708 #ifndef ENABLE_NSS
    709  { "tortls/openssl/", tortls_openssl_tests },
    710 #endif
    711  { "tortls/x509/", x509_tests },
    712  { "util/", util_tests },
    713  { "util/format/", util_format_tests },
    714  { "util/handle/", handle_tests },
    715  { "util/logging/", logging_tests },
    716  { "util/process/", util_process_tests },
    717  { "util/thread/", thread_tests },
    718  END_OF_GROUPS
    719 };