test_circuitpadding.c 111 KB
Newer Older
teor's avatar
teor committed
1
#define CHANNEL_OBJECT_PRIVATE
George Kadianakis's avatar
George Kadianakis committed
2
3
#define TOR_TIMERS_PRIVATE
#define CIRCUITPADDING_PRIVATE
4
#define CIRCUITPADDING_MACHINES_PRIVATE
George Kadianakis's avatar
George Kadianakis committed
5
#define NETWORKSTATUS_PRIVATE
6
#define CRYPT_PATH_PRIVATE
7
#define RELAY_PRIVATE
George Kadianakis's avatar
George Kadianakis committed
8
9

#include "core/or/or.h"
Nick Mathewson's avatar
Nick Mathewson committed
10
#include "test/test.h"
11
#include "test/log_test_helpers.h"
George Kadianakis's avatar
George Kadianakis committed
12
13
14
15
#include "lib/testsupport/testsupport.h"
#include "core/or/connection_or.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
16
#include "core/or/crypt_path.h"
George Kadianakis's avatar
George Kadianakis committed
17
18
19
#include <event.h>
#include "lib/evloop/compat_libevent.h"
#include "lib/time/compat_time.h"
20
#include "lib/defs/time.h"
George Kadianakis's avatar
George Kadianakis committed
21
22
23
24
#include "core/or/relay.h"
#include "core/or/circuitlist.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitpadding.h"
25
#include "core/or/circuitpadding_machines.h"
26
#include "core/or/extendinfo.h"
27
#include "core/mainloop/netstatus.h"
George Kadianakis's avatar
George Kadianakis committed
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "core/crypto/relay_crypto.h"
#include "core/or/protover.h"
#include "feature/nodelist/nodelist.h"
#include "app/config/config.h"

#include "feature/nodelist/routerstatus_st.h"
#include "feature/nodelist/networkstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/cell_st.h"
#include "core/or/crypt_path_st.h"
#include "core/or/or_circuit_st.h"
#include "core/or/origin_circuit_st.h"

41
#include "test/fakecircs.h"
42
43
#include "test/rng_test_helpers.h"

44
45
46
47
48
49
/* Start our monotime mocking at 1 second past whatever monotime_init()
 * thought the actual wall clock time was, for platforms with bad resolution
 * and weird timevalues during monotime_init() before mocking. */
#define MONOTIME_MOCK_START   (monotime_absolute_nsec()+\
                               TOR_NSEC_PER_USEC*TOR_USEC_PER_SEC)

George Kadianakis's avatar
George Kadianakis committed
50
extern smartlist_t *connection_array;
Mike Perry's avatar
Mike Perry committed
51
void circuit_expire_old_circuits_clientside(void);
George Kadianakis's avatar
George Kadianakis committed
52
53
54
55
56
57
58
59
60
61
62
63
64

circid_t get_unique_circ_id_by_chan(channel_t *chan);
void helper_create_basic_machine(void);
static void helper_create_conditional_machines(void);

channel_t *new_fake_channel(void);
void test_circuitpadding_negotiation(void *arg);
void test_circuitpadding_wronghop(void *arg);
void test_circuitpadding_conditions(void *arg);

void test_circuitpadding_serialize(void *arg);
void test_circuitpadding_rtt(void *arg);
void test_circuitpadding_tokens(void *arg);
65
void test_circuitpadding_state_length(void *arg);
George Kadianakis's avatar
George Kadianakis committed
66
67
68
69
70
71
72
73
74
75
76
77
78

static void
simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
                           int padding);
void free_fake_origin_circuit(origin_circuit_t *circ);

static int deliver_negotiated = 1;
static int64_t curr_mocked_time;

static node_t padding_node;
static node_t non_padding_node;

static channel_t dummy_channel;
79
static circpad_machine_spec_t circ_client_machine;
George Kadianakis's avatar
George Kadianakis committed
80
81
82
83

static void
timers_advance_and_run(int64_t msec_update)
{
84
  curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC;
George Kadianakis's avatar
George Kadianakis committed
85
86
87
88
89
90
91
92
93
  monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  monotime_set_mock_time_nsec(curr_mocked_time);
  timers_run_pending();
}

static void
nodes_init(void)
{
  padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
94
  padding_node.rs->pv.supports_hs_setup_padding = 1;
George Kadianakis's avatar
George Kadianakis committed
95
96

  non_padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
97
  non_padding_node.rs->pv.supports_hs_setup_padding = 0;
George Kadianakis's avatar
George Kadianakis committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
}

static void
nodes_free(void)
{
  tor_free(padding_node.rs);

  tor_free(non_padding_node.rs);
}

static const node_t *
node_get_by_id_mock(const char *identity_digest)
{
  if (identity_digest[0] == 1) {
    return &padding_node;
  } else if (identity_digest[0] == 0) {
    return &non_padding_node;
  }

  return NULL;
}

120
121
122
123
124
125
126
127
128
static const node_t *
circuit_get_nth_node_mock(origin_circuit_t *circ, int hop)
{
  (void) circ;
  (void) hop;

  return &padding_node;
}

George Kadianakis's avatar
George Kadianakis committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
void
free_fake_origin_circuit(origin_circuit_t *circ)
{
  circpad_circuit_free_all_machineinfos(TO_CIRCUIT(circ));
  circuit_clear_cpath(circ);
  tor_free(circ);
}

void dummy_nop_timer(void);

//static int dont_stop_libevent = 0;

static circuit_t *client_side;
static circuit_t *relay_side;

static int n_client_cells = 0;
static int n_relay_cells = 0;

static int
circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
                           cell_direction_t cell_direction,
                           crypt_path_t *layer_hint, streamid_t on_stream,
                           const char *filename, int lineno);

static void
circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
                               cell_direction_t direction);

static void
circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
                               cell_direction_t direction)
{
  (void)cmux;
  (void)circ;
  (void)direction;

  return;
}

static int
circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
                           cell_direction_t cell_direction,
                           crypt_path_t *layer_hint, streamid_t on_stream,
                           const char *filename, int lineno)
{
  (void)cell; (void)on_stream; (void)filename; (void)lineno;

  if (circ == client_side) {
    if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
      // Deliver to relay
      circpad_handle_padding_negotiate(relay_side, cell);
    } else {

      int is_target_hop = circpad_padding_is_from_expected_hop(circ,
                                                             layer_hint);
      tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_OUT);
      tt_int_op(is_target_hop, OP_EQ, 1);

      // No need to pretend a padding cell was sent: This event is
      // now emitted internally when the circuitpadding code sends them.
      //circpad_cell_event_padding_sent(client_side);

      // Receive padding cell at middle
      circpad_deliver_recognized_relay_cell_events(relay_side,
              cell->payload[0], NULL);
    }
    n_client_cells++;
  } else if (circ == relay_side) {
    tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_IN);

    if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATED) {
      // XXX: blah need right layer_hint..
      if (deliver_negotiated)
        circpad_handle_padding_negotiated(client_side, cell,
                                          TO_ORIGIN_CIRCUIT(client_side)
                                             ->cpath->next);
    } else if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
      circpad_handle_padding_negotiate(client_side, cell);
    } else {
      // No need to pretend a padding cell was sent: This event is
      // now emitted internally when the circuitpadding code sends them.
      //circpad_cell_event_padding_sent(relay_side);

      // Receive padding cell at client
      circpad_deliver_recognized_relay_cell_events(client_side,
              cell->payload[0],
              TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
    }

    n_relay_cells++;
  }

 done:
  timers_advance_and_run(1);
  return 0;
}

// Test reading and writing padding to strings (or options_t + consensus)
void
test_circuitpadding_serialize(void *arg)
{
  (void)arg;
}

static signed_error_t
circpad_send_command_to_hop_mock(origin_circuit_t *circ, uint8_t hopnum,
                                 uint8_t relay_command, const uint8_t *payload,
                                 ssize_t payload_len)
{
  (void) circ;
  (void) hopnum;
  (void) relay_command;
  (void) payload;
  (void) payload_len;
  return 0;
}

void
test_circuitpadding_rtt(void *arg)
{
  /* Test Plan:
   *
   * 1. Test RTT measurement server side
   *    a. test usage of measured RTT
   * 2. Test termination of RTT measurement
   *    a. test non-update of RTT
   * 3. Test client side circuit and non-application of RTT..
   */
  circpad_delay_t rtt_estimate;
258
  int64_t actual_mocked_monotime_start;
George Kadianakis's avatar
George Kadianakis committed
259
260
261
262
  (void)arg;

  MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
263
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
264
265
266
267
268
269
270
271
272

  dummy_channel.cmux = circuitmux_alloc();
  relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  client_side = TO_CIRCUIT(origin_circuit_new());
  relay_side->purpose = CIRCUIT_PURPOSE_OR;
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;

  monotime_init();
  monotime_enable_test_mocking();
273
274
275
276
  actual_mocked_monotime_start = MONOTIME_MOCK_START;
  monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  curr_mocked_time = actual_mocked_monotime_start;
George Kadianakis's avatar
George Kadianakis committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

  timers_initialize();
  circpad_machines_init();
  helper_create_basic_machine();

  MOCK(circuit_package_relay_cell,
       circuit_package_relay_cell_mock);

  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
                                                                 0);

  relay_side->padding_machine[0] = &circ_client_machine;
  relay_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,0);

  /* Test 1: Test measuring RTT */
293
  circpad_cell_event_nonpadding_received(relay_side);
294
  tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
George Kadianakis's avatar
George Kadianakis committed
295
296
297

  timers_advance_and_run(20);

298
  circpad_cell_event_nonpadding_sent(relay_side);
299
  tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
George Kadianakis's avatar
George Kadianakis committed
300
301
302
303
304
305
306

  tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 19000);
  tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 30000);
  tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
            OP_EQ,
            relay_side->padding_info[0]->rtt_estimate_usec+
            circpad_machine_current_state(
307
             relay_side->padding_info[0])->histogram_edges[0]);
George Kadianakis's avatar
George Kadianakis committed
308

309
310
  circpad_cell_event_nonpadding_received(relay_side);
  circpad_cell_event_nonpadding_received(relay_side);
311
  tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
George Kadianakis's avatar
George Kadianakis committed
312
  timers_advance_and_run(20);
313
314
  circpad_cell_event_nonpadding_sent(relay_side);
  circpad_cell_event_nonpadding_sent(relay_side);
315
  tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
George Kadianakis's avatar
George Kadianakis committed
316
317
318
319
320
321
322

  tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 20000);
  tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 21000);
  tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
            OP_EQ,
            relay_side->padding_info[0]->rtt_estimate_usec+
            circpad_machine_current_state(
323
             relay_side->padding_info[0])->histogram_edges[0]);
George Kadianakis's avatar
George Kadianakis committed
324
325
326
327
328

  /* Test 2: Termination of RTT measurement (from the previous test) */
  tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  rtt_estimate = relay_side->padding_info[0]->rtt_estimate_usec;

329
  circpad_cell_event_nonpadding_received(relay_side);
George Kadianakis's avatar
George Kadianakis committed
330
  timers_advance_and_run(4);
331
  circpad_cell_event_nonpadding_sent(relay_side);
George Kadianakis's avatar
George Kadianakis committed
332
333
334

  tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_EQ,
            rtt_estimate);
335
  tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
George Kadianakis's avatar
George Kadianakis committed
336
337
338
339
340
  tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
            OP_EQ,
            relay_side->padding_info[0]->rtt_estimate_usec+
            circpad_machine_current_state(
341
             relay_side->padding_info[0])->histogram_edges[0]);
George Kadianakis's avatar
George Kadianakis committed
342
343

  /* Test 3: Make sure client side machine properly ignores RTT */
344
  circpad_cell_event_nonpadding_received(client_side);
345
  tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
George Kadianakis's avatar
George Kadianakis committed
346
347

  timers_advance_and_run(20);
348
  circpad_cell_event_nonpadding_sent(client_side);
349
  tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
George Kadianakis's avatar
George Kadianakis committed
350
351
352
353
354
355
356

  tt_int_op(client_side->padding_info[0]->rtt_estimate_usec, OP_EQ, 0);
  tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
            OP_NE, client_side->padding_info[0]->rtt_estimate_usec);
  tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
            OP_EQ,
            circpad_machine_current_state(
357
                client_side->padding_info[0])->histogram_edges[0]);
George Kadianakis's avatar
George Kadianakis committed
358
 done:
359
  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
George Kadianakis's avatar
George Kadianakis committed
360
361
362
363
364
365
366
  circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  circuitmux_free(dummy_channel.cmux);
  timers_shutdown();
  monotime_disable_test_mocking();
  UNMOCK(circuit_package_relay_cell);
  UNMOCK(circuitmux_attach_circuit);
  tor_free(circ_client_machine.states);
367
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
368
369
370
371
372
373
374
375
376
377

  return;
}

void
helper_create_basic_machine(void)
{
  /* Start, burst */
  circpad_machine_states_init(&circ_client_machine, 2);

378
379
  circ_client_machine.name = "basic";

George Kadianakis's avatar
George Kadianakis committed
380
381
  circ_client_machine.states[CIRCPAD_STATE_START].
      next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
382
  circ_client_machine.states[CIRCPAD_STATE_START].use_rtt_estimate = 1;
George Kadianakis's avatar
George Kadianakis committed
383
384
385
386
387
388
389
390
391
392
393
394
395

  circ_client_machine.states[CIRCPAD_STATE_BURST].
      next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  circ_client_machine.states[CIRCPAD_STATE_BURST].
      next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;

  circ_client_machine.states[CIRCPAD_STATE_BURST].
      next_state[CIRCPAD_EVENT_NONPADDING_SENT] = CIRCPAD_STATE_CANCEL;

  circ_client_machine.states[CIRCPAD_STATE_BURST].token_removal =
      CIRCPAD_TOKEN_REMOVAL_HIGHER;

  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_len = 5;
396
397
398
399
400
401
402

  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 500;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 2500;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 5000;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[4] = 20000;

George Kadianakis's avatar
George Kadianakis committed
403
404
405
406
407
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[0] = 1;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[2] = 2;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[3] = 2;
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[4] = 2;
408

George Kadianakis's avatar
George Kadianakis committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_total_tokens = 7;
  circ_client_machine.states[CIRCPAD_STATE_BURST].use_rtt_estimate = 1;

  return;
}

#define BIG_HISTOGRAM_LEN 10

/** Setup a machine with a big histogram */
static void
helper_create_machine_with_big_histogram(circpad_removal_t removal_strategy)
{
  const int tokens_per_bin = 2;

  /* Start, burst */
  circpad_machine_states_init(&circ_client_machine, 2);

  circpad_state_t *burst_state =
    &circ_client_machine.states[CIRCPAD_STATE_BURST];

  circ_client_machine.states[CIRCPAD_STATE_START].
    next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;

  burst_state->next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  burst_state->next_state[CIRCPAD_EVENT_NONPADDING_RECV] =CIRCPAD_STATE_BURST;

  burst_state->next_state[CIRCPAD_EVENT_NONPADDING_SENT] =CIRCPAD_STATE_CANCEL;

  burst_state->token_removal = CIRCPAD_TOKEN_REMOVAL_HIGHER;

  burst_state->histogram_len = BIG_HISTOGRAM_LEN;

  int n_tokens = 0;
442
443
  int i;
  for (i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
George Kadianakis's avatar
George Kadianakis committed
444
445
446
447
    burst_state->histogram[i] = tokens_per_bin;
    n_tokens += tokens_per_bin;
  }

448
449
450
451
452
453
454
455
456
457
458
  burst_state->histogram_edges[0] = 0;
  burst_state->histogram_edges[1] = 1;
  burst_state->histogram_edges[2] = 7;
  burst_state->histogram_edges[3] = 15;
  burst_state->histogram_edges[4] = 31;
  burst_state->histogram_edges[5] = 62;
  burst_state->histogram_edges[6] = 125;
  burst_state->histogram_edges[7] = 250;
  burst_state->histogram_edges[8] = 500;
  burst_state->histogram_edges[9] = 1000;

George Kadianakis's avatar
George Kadianakis committed
459
460
461
462
463
464
465
466
467
468
469
  burst_state->histogram_total_tokens = n_tokens;
  burst_state->length_dist.type = CIRCPAD_DIST_UNIFORM;
  burst_state->length_dist.param1 = n_tokens;
  burst_state->length_dist.param2 = n_tokens;
  burst_state->max_length = n_tokens;
  burst_state->length_includes_nonpadding = 1;
  burst_state->use_rtt_estimate = 0;
  burst_state->token_removal = removal_strategy;
}

static circpad_decision_t
470
circpad_machine_schedule_padding_mock(circpad_machine_runtime_t *mi)
George Kadianakis's avatar
George Kadianakis committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
{
  (void)mi;
  return 0;
}

static uint64_t
mock_monotime_absolute_usec(void)
{
  return 100;
}

/** Test higher token removal strategy by bin  */
static void
test_circuitpadding_token_removal_higher(void *arg)
{
486
  circpad_machine_runtime_t *mi;
George Kadianakis's avatar
George Kadianakis committed
487
488
489
490
491
  (void)arg;

  /* Mock it up */
  MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
492
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
493
494

  /* Setup test environment (time etc.) */
495
  client_side = TO_CIRCUIT(origin_circuit_new());
George Kadianakis's avatar
George Kadianakis committed
496
497
498
499
500
501
502
503
504
505
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  monotime_enable_test_mocking();

  /* Create test machine */
  helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_HIGHER);
  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] =
    circpad_circuit_machineinfo_new(client_side, 0);

  /* move the machine to the right state */
506
  circpad_cell_event_nonpadding_received(client_side);
George Kadianakis's avatar
George Kadianakis committed
507
508
509
510
511
512
513
514
515
516
517
518
519
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);

  /* Get the machine and setup tokens */
  mi = client_side->padding_info[0];
  tt_assert(mi);

  /*************************************************************************/

  uint64_t current_time = monotime_absolute_usec();

  /* Test left boundaries of each histogram bin: */
  const circpad_delay_t bin_left_bounds[] =
520
521
    {0, 1,    7,  15,  31,  62,  125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
George Kadianakis's avatar
George Kadianakis committed
522
523
524
525
    tt_uint_op(bin_left_bounds[i], OP_EQ,
               circpad_histogram_bin_to_usec(mi, i));
  }

526
527
528
529
530
531
532
533
  /* Test right boundaries of each histogram bin: */
  const circpad_delay_t bin_right_bounds[] =
    {0,    6,  14,  30,  61,  124, 249, 499, 999, CIRCPAD_DELAY_INFINITE-1};
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_uint_op(bin_right_bounds[i], OP_EQ,
               histogram_get_bin_upper_bound(mi, i));
  }

George Kadianakis's avatar
George Kadianakis committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  /* Check that all bins have two tokens right now */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* This is the right order to remove tokens from this histogram. That is, we
   * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
   * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
   * the same reason, then from the 5th, etc. */
  const int bin_removal_order[] = {4, 5, 6, 7, 8};
  unsigned i;

  /* Remove all tokens from all bins apart from the infinity bin */
  for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
    int bin_to_remove = bin_removal_order[i];
    log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
              i, bin_to_remove);

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);

    mi->padding_scheduled_at_usec = current_time - 57;
555
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
556
557
558
559

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);

    mi->padding_scheduled_at_usec = current_time - 57;
560
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
561
562
563
564
565
566
567
568

    /* Test that we cleaned out this bin. Don't do this in the case of the last
       bin since the tokens will get refilled */
    if (i != BIG_HISTOGRAM_LEN - 2) {
      tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
    }
  }

teor's avatar
teor committed
569
  /* Check that all lower bins are not touched */
George Kadianakis's avatar
George Kadianakis committed
570
571
572
573
574
575
576
  for (i=0; i < 4 ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* Test below the lowest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
577
578
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  mi->padding_scheduled_at_usec = current_time;
579
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
580
581
582
583
584
585
  tt_int_op(mi->histogram[0], OP_EQ, 1);

 done:
  free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  monotime_disable_test_mocking();
  tor_free(circ_client_machine.states);
586
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
587
588
589
590
591
592
}

/** Test lower token removal strategy by bin  */
static void
test_circuitpadding_token_removal_lower(void *arg)
{
593
  circpad_machine_runtime_t *mi;
George Kadianakis's avatar
George Kadianakis committed
594
595
596
597
598
  (void)arg;

  /* Mock it up */
  MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
599
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
600
601

  /* Setup test environment (time etc.) */
602
  client_side = TO_CIRCUIT(origin_circuit_new());
George Kadianakis's avatar
George Kadianakis committed
603
604
605
606
607
608
609
610
611
612
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  monotime_enable_test_mocking();

  /* Create test machine */
  helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_LOWER);
  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] =
    circpad_circuit_machineinfo_new(client_side, 0);

  /* move the machine to the right state */
613
  circpad_cell_event_nonpadding_received(client_side);
George Kadianakis's avatar
George Kadianakis committed
614
615
616
617
618
619
620
621
622
623
624
625
626
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);

  /* Get the machine and setup tokens */
  mi = client_side->padding_info[0];
  tt_assert(mi);

  /*************************************************************************/

  uint64_t current_time = monotime_absolute_usec();

  /* Test left boundaries of each histogram bin: */
  const circpad_delay_t bin_left_bounds[] =
627
628
    {0, 1,    7,  15,  31,  62,  125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
George Kadianakis's avatar
George Kadianakis committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    tt_uint_op(bin_left_bounds[i], OP_EQ,
               circpad_histogram_bin_to_usec(mi, i));
  }

  /* Check that all bins have two tokens right now */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* This is the right order to remove tokens from this histogram. That is, we
   * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
   * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
   * the same reason, then from the 5th, etc. */
  const int bin_removal_order[] = {4, 3, 2, 1, 0};
  unsigned i;

  /* Remove all tokens from all bins apart from the infinity bin */
  for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
    int bin_to_remove = bin_removal_order[i];
    log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
              i, bin_to_remove);

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);

    mi->padding_scheduled_at_usec = current_time - 57;
654
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
655
656
657
658

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);

    mi->padding_scheduled_at_usec = current_time - 57;
659
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

    /* Test that we cleaned out this bin. Don't do this in the case of the last
       bin since the tokens will get refilled */
    if (i != BIG_HISTOGRAM_LEN - 2) {
      tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
    }
  }

  /* Check that all higher bins are untouched */
  for (i = 5; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* Test above the highest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
676
677
  circ_client_machine.states[CIRCPAD_STATE_BURST].
    histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
George Kadianakis's avatar
George Kadianakis committed
678
  mi->padding_scheduled_at_usec = current_time - 29202;
679
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
680
681
682
683
684
685
  tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);

 done:
  free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  monotime_disable_test_mocking();
  tor_free(circ_client_machine.states);
686
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
687
688
689
690
691
692
}

/** Test closest token removal strategy by bin  */
static void
test_circuitpadding_closest_token_removal(void *arg)
{
693
  circpad_machine_runtime_t *mi;
George Kadianakis's avatar
George Kadianakis committed
694
695
696
697
698
  (void)arg;

  /* Mock it up */
  MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
699
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
700
701

  /* Setup test environment (time etc.) */
702
  client_side = TO_CIRCUIT(origin_circuit_new());
George Kadianakis's avatar
George Kadianakis committed
703
704
705
706
707
708
709
710
711
712
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  monotime_enable_test_mocking();

  /* Create test machine */
  helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST);
  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] =
    circpad_circuit_machineinfo_new(client_side, 0);

  /* move the machine to the right state */
713
  circpad_cell_event_nonpadding_received(client_side);
George Kadianakis's avatar
George Kadianakis committed
714
715
716
717
718
719
720
721
722
723
724
725
726
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);

  /* Get the machine and setup tokens */
  mi = client_side->padding_info[0];
  tt_assert(mi);

  /*************************************************************************/

  uint64_t current_time = monotime_absolute_usec();

  /* Test left boundaries of each histogram bin: */
  const circpad_delay_t bin_left_bounds[] =
727
728
    {0, 1,    7,  15,  31,  62,  125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
George Kadianakis's avatar
George Kadianakis committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
    tt_uint_op(bin_left_bounds[i], OP_EQ,
               circpad_histogram_bin_to_usec(mi, i));
  }

  /* Check that all bins have two tokens right now */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* This is the right order to remove tokens from this histogram. That is, we
   * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
   * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
   * the same reason, then from the 5th, etc. */
  const int bin_removal_order[] = {4, 3, 5, 2, 6, 1, 7, 0, 8, 9};

  /* Remove all tokens from all bins apart from the infinity bin */
  for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
    int bin_to_remove = bin_removal_order[i];
    log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
              i, bin_to_remove);

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);

    mi->padding_scheduled_at_usec = current_time - 57;
753
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
754
755
756
757

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);

    mi->padding_scheduled_at_usec = current_time - 57;
758
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

    /* Test that we cleaned out this bin. Don't do this in the case of the last
       bin since the tokens will get refilled */
    if (i != BIG_HISTOGRAM_LEN - 2) {
      tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
    }
  }

  /* Check that all bins have been refilled */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* Test below the lowest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
775
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
776
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
777
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
George Kadianakis's avatar
George Kadianakis committed
778
779
  mi->padding_scheduled_at_usec = current_time - 102;
  mi->histogram[0] = 0;
780
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
781
782
783
784
785
786
  tt_int_op(mi->histogram[1], OP_EQ, 1);

  /* Test above the highest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
  mi->padding_scheduled_at_usec = current_time - 29202;
787
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
788
789
790
791
792
793
  tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);

 done:
  free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  monotime_disable_test_mocking();
  tor_free(circ_client_machine.states);
794
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
795
796
797
798
799
800
}

/** Test closest token removal strategy with usec  */
static void
test_circuitpadding_closest_token_removal_usec(void *arg)
{
801
  circpad_machine_runtime_t *mi;
George Kadianakis's avatar
George Kadianakis committed
802
803
804
805
806
  (void)arg;

  /* Mock it up */
  MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
807
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
808
809

  /* Setup test environment (time etc.) */
810
  client_side = TO_CIRCUIT(origin_circuit_new());
George Kadianakis's avatar
George Kadianakis committed
811
812
813
814
815
816
817
818
819
820
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  monotime_enable_test_mocking();

  /* Create test machine */
  helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC);
  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] =
    circpad_circuit_machineinfo_new(client_side, 0);

  /* move the machine to the right state */
821
  circpad_cell_event_nonpadding_received(client_side);
George Kadianakis's avatar
George Kadianakis committed
822
823
824
825
826
827
828
829
830
831
832
833
834
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);

  /* Get the machine and setup tokens */
  mi = client_side->padding_info[0];
  tt_assert(mi);

  /*************************************************************************/

  uint64_t current_time = monotime_absolute_usec();

  /* Test left boundaries of each histogram bin: */
  const circpad_delay_t bin_left_bounds[] =
835
836
    {0, 1,    7,  15,  31,  62,  125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
George Kadianakis's avatar
George Kadianakis committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
    tt_uint_op(bin_left_bounds[i], OP_EQ,
               circpad_histogram_bin_to_usec(mi, i));
  }

  /* XXX we want to test remove_token_exact and
     circpad_machine_remove_closest_token() with usec */

  /* Check that all bins have two tokens right now */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* This is the right order to remove tokens from this histogram. That is, we
   * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
   * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
   * the same reason, then from the 5th, etc. */
  const int bin_removal_order[] = {4, 3, 5, 2, 1, 0, 6, 7, 8, 9};

  /* Remove all tokens from all bins apart from the infinity bin */
  for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
    int bin_to_remove = bin_removal_order[i];
    log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
              i, bin_to_remove);

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);

    mi->padding_scheduled_at_usec = current_time - 57;
864
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
865
866
867
868

    tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);

    mi->padding_scheduled_at_usec = current_time - 57;
869
    circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885

    /* Test that we cleaned out this bin. Don't do this in the case of the last
       bin since the tokens will get refilled */
    if (i != BIG_HISTOGRAM_LEN - 2) {
      tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
    }
  }

  /* Check that all bins have been refilled */
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    tt_int_op(mi->histogram[i], OP_EQ, 2);
  }

  /* Test below the lowest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
886
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
887
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
888
  circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
George Kadianakis's avatar
George Kadianakis committed
889
890
  mi->padding_scheduled_at_usec = current_time - 102;
  mi->histogram[0] = 0;
891
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
892
893
894
895
896
  tt_int_op(mi->histogram[1], OP_EQ, 1);

  /* Test above the highest bin, for coverage */
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);
897
898
  circ_client_machine.states[CIRCPAD_STATE_BURST].
    histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
George Kadianakis's avatar
George Kadianakis committed
899
  mi->padding_scheduled_at_usec = current_time - 29202;
900
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
901
902
903
904
905
906
  tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);

 done:
  free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  monotime_disable_test_mocking();
  tor_free(circ_client_machine.states);
907
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
908
909
910
911
912
913
}

/** Test closest token removal strategy with usec  */
static void
test_circuitpadding_token_removal_exact(void *arg)
{
914
  circpad_machine_runtime_t *mi;
George Kadianakis's avatar
George Kadianakis committed
915
916
917
918
919
  (void)arg;

  /* Mock it up */
  MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
920
  testing_enable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
921
922

  /* Setup test environment (time etc.) */
923
  client_side = TO_CIRCUIT(origin_circuit_new());
George Kadianakis's avatar
George Kadianakis committed
924
925
926
927
928
929
930
931
932
933
  client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  monotime_enable_test_mocking();

  /* Create test machine */
  helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_EXACT);
  client_side->padding_machine[0] = &circ_client_machine;
  client_side->padding_info[0] =
    circpad_circuit_machineinfo_new(client_side, 0);

  /* move the machine to the right state */
934
  circpad_cell_event_nonpadding_received(client_side);
George Kadianakis's avatar
George Kadianakis committed
935
936
937
938
939
940
941
942
943
944
945
946
947
  tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
            CIRCPAD_STATE_BURST);

  /* Get the machine and setup tokens */
  mi = client_side->padding_info[0];
  tt_assert(mi);

  /**********************************************************************/
  uint64_t current_time = monotime_absolute_usec();

  /* Ensure that we will clear out bin #4 with this usec */
  mi->padding_scheduled_at_usec = current_time - 57;
  tt_int_op(mi->histogram[4], OP_EQ, 2);
948
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
949
950
  mi->padding_scheduled_at_usec = current_time - 57;
  tt_int_op(mi->histogram[4], OP_EQ, 1);
951
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
952
953
954
955
956
  tt_int_op(mi->histogram[4], OP_EQ, 0);

  /* Ensure that we will not remove any other tokens even tho we try to, since
   * this is what the exact strategy dictates */
  mi->padding_scheduled_at_usec = current_time - 57;
957
  circpad_cell_event_nonpadding_sent(client_side);
George Kadianakis's avatar
George Kadianakis committed
958
959
960
961
962
963
964
965
966
967
  for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
    if (i != 4) {
      tt_int_op(mi->histogram[i], OP_EQ, 2);
    }
  }

 done:
  free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  monotime_disable_test_mocking();
  tor_free(circ_client_machine.states);
968
  testing_disable_reproducible_rng();
George Kadianakis's avatar
George Kadianakis committed
969
970
971
972
973
974
975
976
}

#undef BIG_HISTOGRAM_LEN

void
test_circuitpadding_tokens(void *arg)
{
  const circpad_state_t *state;
977
  circpad_machine_runtime_t *mi;
978
  int64_t actual_mocked_monotime_start;
George Kadianakis's avatar
George Kadianakis committed
979
980
  (void)arg;

981
982
  testing_enable_reproducible_rng();

George Kadianakis's avatar
George Kadianakis committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  /** Test plan:
   *
   * 1. Test symmetry between bin_to_usec and usec_to_bin
   *    a. Test conversion
   *    b. Test edge transitions (lower, upper)
   * 2. Test remove higher on an empty bin
   *    a. Normal bin
   *    b. Infinity bin
   *    c. Bin 0
   *    d. No higher
   * 3. Test remove lower
   *    a. Normal bin
   *    b. Bin 0
   *    c. No lower
   * 4. Test remove closest
   *    a. Closest lower
   *    b. Closest higher
   *    c. Closest 0
For faster browsing, not all history is shown. View entire blame