aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_channel.c
blob: acbfca1c7d75645539b0b3cf0285f9768e226c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* Copyright (c) 2013, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#define TOR_CHANNEL_INTERNAL_
#include "or.h"
#include "channel.h"
/* For channel_note_destroy_not_pending */
#include "circuitlist.h"
/* For var_cell_free */
#include "connection_or.h"
/* For packed_cell stuff */
#define RELAY_PRIVATE
#include "relay.h"
/* For init/free stuff */
#include "scheduler.h"
#include "test.h"

static int test_chan_accept_cells = 0;
static int test_cells_written = 0;
static int test_destroy_not_pending_calls = 0;
static double test_overhead_estimate = 1.0f;

static void channel_note_destroy_not_pending_mock(channel_t *ch,
                                                  circid_t circid);
static void chan_test_close(channel_t *ch);
static size_t chan_test_num_bytes_queued(channel_t *ch);
static int chan_test_num_cells_writeable(channel_t *ch);
static int chan_test_write_cell(channel_t *ch, cell_t *cell);
static int chan_test_write_packed_cell(channel_t *ch,
                                       packed_cell_t *packed_cell);
static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
static void make_fake_cell(cell_t *c);
static void make_fake_var_cell(var_cell_t *c);
static channel_t * new_fake_channel(void);
static void scheduler_release_channel_mock(channel_t *ch);

static void test_channel_multi(void *arg);
static void test_channel_queue_size(void *arg);
static void test_channel_write(void *arg);

static void
channel_note_destroy_not_pending_mock(channel_t *ch,
                                      circid_t circid)
{
  (void)ch;
  (void)circid;

  ++test_destroy_not_pending_calls;
}

static void
chan_test_close(channel_t *ch)
{
  test_assert(ch);

 done:
  return;
}

static double
chan_test_get_overhead_estimate(channel_t *ch)
{
  test_assert(ch);

 done:
  return test_overhead_estimate;
}

static size_t
chan_test_num_bytes_queued(channel_t *ch)
{
  test_assert(ch);

 done:
  return 0;
}

static int
chan_test_num_cells_writeable(channel_t *ch)
{
  test_assert(ch);

 done:
  return 32;
}

static int
chan_test_write_cell(channel_t *ch, cell_t *cell)
{
  int rv = 0;

  test_assert(ch);
  test_assert(cell);

  if (test_chan_accept_cells) {
    /* Free the cell and bump the counter */
    tor_free(cell);
    ++test_cells_written;
    rv = 1;
  }
  /* else return 0, we didn't accept it */

 done:
  return rv;
}

static int
chan_test_write_packed_cell(channel_t *ch,
                            packed_cell_t *packed_cell)
{
  int rv = 0;

  test_assert(ch);
  test_assert(packed_cell);

  if (test_chan_accept_cells) {
    /* Free the cell and bump the counter */
    packed_cell_free(packed_cell);
    ++test_cells_written;
    rv = 1;
  }
  /* else return 0, we didn't accept it */

 done:
  return rv;
}

static int
chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
{
  int rv = 0;

  test_assert(ch);
  test_assert(var_cell);

  if (test_chan_accept_cells) {
    /* Free the cell and bump the counter */
    var_cell_free(var_cell);
    ++test_cells_written;
    rv = 1;
  }
  /* else return 0, we didn't accept it */

 done:
  return rv;
}

static void
make_fake_cell(cell_t *c)
{
  test_assert(c != NULL);

  c->circ_id = 1;
  c->command = CELL_RELAY;
  memset(c->payload, 0, CELL_PAYLOAD_SIZE);

 done:
  return;
}

static void
make_fake_var_cell(var_cell_t *c)
{
  test_assert(c != NULL);

  c->circ_id = 1;
  c->command = CELL_VERSIONS;
  c->payload_len = CELL_PAYLOAD_SIZE / 2;
  memset(c->payload, 0, c->payload_len);

 done:
  return;
}

static channel_t *
new_fake_channel(void)
{
  channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  channel_init(chan);

  chan->close = chan_test_close;
  chan->get_overhead_estimate = chan_test_get_overhead_estimate;
  chan->num_bytes_queued = chan_test_num_bytes_queued;
  chan->num_cells_writeable = chan_test_num_cells_writeable;
  chan->write_cell = chan_test_write_cell;
  chan->write_packed_cell = chan_test_write_packed_cell;
  chan->write_var_cell = chan_test_write_var_cell;
  chan->state = CHANNEL_STATE_OPEN;

  return chan;
}

static void
scheduler_release_channel_mock(channel_t *ch)
{
  (void)ch;

  /* Increment counter */
  ++test_releases_count;

  return;
}

static void
test_channel_multi(void *arg)
{
  channel_t *ch1 = NULL, *ch2 = NULL;
  uint64_t global_queue_estimate;
  cell_t *cell = NULL;

  (void)arg;

  /* Accept cells to lower layer */
  test_chan_accept_cells = 1;
  /* Use default overhead factor */
  test_overhead_estimate = 1.0f;

  ch1 = new_fake_channel();
  test_assert(ch1);
  ch2 = new_fake_channel();
  test_assert(ch2);

  /* Initial queue size update */
  channel_update_xmit_queue_size(ch1);
  test_eq(ch1->bytes_queued_for_xmit, 0);
  channel_update_xmit_queue_size(ch2);
  test_eq(ch2->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 0);

  /* Queue some cells, check queue estimates */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch1, cell);

  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch2, cell);

  channel_update_xmit_queue_size(ch1);
  channel_update_xmit_queue_size(ch2);
  test_eq(ch1->bytes_queued_for_xmit, 0);
  test_eq(ch2->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 0);

  /* Stop accepting cells at lower layer */
  test_chan_accept_cells = 0;

  /* Queue some cells and check queue estimates */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch1, cell);

  channel_update_xmit_queue_size(ch1);
  test_eq(ch1->bytes_queued_for_xmit, 512);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 512);

  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch2, cell);

  channel_update_xmit_queue_size(ch2);
  test_eq(ch2->bytes_queued_for_xmit, 512);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 1024);

  /* Allow cells through again */
  test_chan_accept_cells = 1;

  /* Flush chan 2 */
  channel_flush_cells(ch2);

  /* Update and check queue sizes */
  channel_update_xmit_queue_size(ch1);
  channel_update_xmit_queue_size(ch2);
  test_eq(ch1->bytes_queued_for_xmit, 512);
  test_eq(ch2->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 512);

  /* Flush chan 1 */
  channel_flush_cells(ch1);

  /* Update and check queue sizes */
  channel_update_xmit_queue_size(ch1);
  channel_update_xmit_queue_size(ch2);
  test_eq(ch1->bytes_queued_for_xmit, 0);
  test_eq(ch2->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 0);

  /* Now block again */
  test_chan_accept_cells = 0;

  /* Queue some cells */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch1, cell);

  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch2, cell);

  /* Check the estimates */
  channel_update_xmit_queue_size(ch1);
  channel_update_xmit_queue_size(ch2);
  test_eq(ch1->bytes_queued_for_xmit, 512);
  test_eq(ch2->bytes_queued_for_xmit, 512);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 1024);

  /* Now close channel 2; it should be subtracted from the global queue */
  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  channel_mark_for_close(ch2);
  UNMOCK(scheduler_release_channel);

  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 512);

  /* Now free everything */
  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  channel_free_all();
  UNMOCK(scheduler_release_channel);

 done:
  tor_free(ch1);
  tor_free(ch2);

  return;
}

static void
test_channel_queue_size(void *arg)
{
  channel_t *ch = NULL;
  cell_t *cell = NULL;
  int n, old_count;
  uint64_t global_queue_estimate;

  (void)arg;

  ch = new_fake_channel();
  test_assert(ch);

  /* Initial queue size update */
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 0);

  /* Test the call-through to our fake lower layer */
  n = channel_num_cells_writeable(ch);
  /* chan_test_num_cells_writeable() always returns 32 */
  test_eq(n, 32);

  /*
   * Now we queue some cells and check that channel_num_cells_writeable()
   * adjusts properly
   */

  /* tell it not to accept cells */
  test_chan_accept_cells = 0;
  /* ...and keep it from trying to flush the queue */
  ch->state = CHANNEL_STATE_MAINT;

  /* Get a fresh cell */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);

  old_count = test_cells_written;
  channel_write_cell(ch, cell);
  /* Assert that it got queued, not written through, correctly */
  test_eq(test_cells_written, old_count);

  /* Now check chan_test_num_cells_writeable() again */
  n = channel_num_cells_writeable(ch);
  test_eq(n, 0); /* Should return 0 since we're in CHANNEL_STATE_MAINT */

  /* Update queue size estimates */
  channel_update_xmit_queue_size(ch);
  /* One cell, times an overhead factor of 1.0 */
  test_eq(ch->bytes_queued_for_xmit, 512);
  /* Try a different overhead factor */
  test_overhead_estimate = 0.5f;
  /* This one should be ignored since it's below 1.0 */
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 512);
  /* Now try a larger one */
  test_overhead_estimate = 2.0f;
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 1024);
  /* Go back to 1.0 */
  test_overhead_estimate = 1.0f;
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 512);
  /* Check the global estimate too */
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 512);

  /* Go to open */
  old_count = test_cells_written;
  channel_change_state(ch, CHANNEL_STATE_OPEN);

  /*
   * It should try to write, but we aren't accepting cells right now, so
   * it'll requeue
   */
  test_eq(test_cells_written, old_count);

  /* Check the queue size again */
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 512);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 512);

  /*
   * Now the cell is in the queue, and we're open, so we should get 31
   * writeable cells.
   */
  n = channel_num_cells_writeable(ch);
  test_eq(n, 31);

  /* Accept cells again */
  test_chan_accept_cells = 1;
  /* ...and re-process the queue */
  old_count = test_cells_written;
  channel_flush_cells(ch);
  test_eq(test_cells_written, old_count + 1);

  /* Should have 32 writeable now */
  n = channel_num_cells_writeable(ch);
  test_eq(n, 32);

  /* Should have queue size estimate of zero */
  channel_update_xmit_queue_size(ch);
  test_eq(ch->bytes_queued_for_xmit, 0);
  global_queue_estimate = channel_get_global_queue_estimate();
  test_eq(global_queue_estimate, 0);

  /* Okay, now we're done with this one */
  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  channel_mark_for_close(ch);
  UNMOCK(scheduler_release_channel);

 done:
  tor_free(ch);

  return;
}

static void
test_channel_write(void *arg)
{
  channel_t *ch = NULL;
  cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  packed_cell_t *packed_cell = NULL;
  var_cell_t *var_cell =
    tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  int old_count;

  (void)arg;

  init_cell_pool();

  packed_cell = packed_cell_new();
  test_assert(packed_cell);

  ch = new_fake_channel();
  test_assert(ch);
  make_fake_cell(cell);
  make_fake_var_cell(var_cell);

  /* Tell it to accept cells */
  test_chan_accept_cells = 1;

  old_count = test_cells_written;
  channel_write_cell(ch, cell);
  test_assert(test_cells_written == old_count + 1);

  channel_write_var_cell(ch, var_cell);
  test_assert(test_cells_written == old_count + 2);

  channel_write_packed_cell(ch, packed_cell);
  test_assert(test_cells_written == old_count + 3);

  /* Now we test queueing; tell it not to accept cells */
  test_chan_accept_cells = 0;
  /* ...and keep it from trying to flush the queue */
  ch->state = CHANNEL_STATE_MAINT;

  /* Get a fresh cell */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);

  old_count = test_cells_written;
  channel_write_cell(ch, cell);
  test_assert(test_cells_written == old_count);

  /*
   * Now change back to open with channel_change_state() and assert that it
   * gets drained from the queue.
   */
  test_chan_accept_cells = 1;
  channel_change_state(ch, CHANNEL_STATE_OPEN);
  test_assert(test_cells_written == old_count + 1);

  /*
   * Check the note destroy case
   */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  cell->command = CELL_DESTROY;

  /* Set up the mock */
  MOCK(channel_note_destroy_not_pending,
       channel_note_destroy_not_pending_mock);

  old_count = test_destroy_not_pending_calls;
  channel_write_cell(ch, cell);
  test_assert(test_destroy_not_pending_calls == old_count + 1);

  /* Now send a non-destroy and check we don't call it */
  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch, cell);
  test_assert(test_destroy_not_pending_calls == old_count + 1);

  UNMOCK(channel_note_destroy_not_pending);

  /*
   * Now switch it to CLOSING so we can test the discard-cells case
   * in the channel_write_*() functions.
   */
  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  channel_mark_for_close(ch);
  UNMOCK(scheduler_release_channel);

  /* Send cells that will drop in the closing state */
  old_count = test_cells_written;

  cell = tor_malloc_zero(sizeof(cell_t));
  make_fake_cell(cell);
  channel_write_cell(ch, cell);
  test_assert(test_cells_written == old_count);

  var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  make_fake_var_cell(var_cell);
  channel_write_var_cell(ch, var_cell);
  test_assert(test_cells_written == old_count);

  packed_cell = packed_cell_new();
  channel_write_packed_cell(ch, packed_cell);
  test_assert(test_cells_written == old_count);

  free_cell_pool();

 done:
  tor_free(ch);

  return;
}

struct testcase_t channel_tests[] = {
  { "multi", test_channel_multi, TT_FORK, NULL, NULL },
  { "queue_size", test_channel_queue_size, TT_FORK, NULL, NULL },
  { "write", test_channel_write, TT_FORK, NULL, NULL },
  END_OF_TESTCASES
};