test_circuitmux.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (c) 2013-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #define CIRCUITMUX_PRIVATE
  5. #define RELAY_PRIVATE
  6. #include "or.h"
  7. #include "channel.h"
  8. #include "circuitmux.h"
  9. #include "circuitmux_ewma.h"
  10. #include "relay.h"
  11. #include "scheduler.h"
  12. #include "test.h"
  13. /* XXXX duplicated function from test_circuitlist.c */
  14. static channel_t *
  15. new_fake_channel(void)
  16. {
  17. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  18. channel_init(chan);
  19. return chan;
  20. }
  21. static int
  22. has_queued_writes(channel_t *c)
  23. {
  24. (void) c;
  25. return 1;
  26. }
  27. /** Test destroy cell queue with no interference from other queues. */
  28. static void
  29. test_cmux_destroy_cell_queue(void *arg)
  30. {
  31. circuitmux_t *cmux = NULL;
  32. channel_t *ch = NULL;
  33. circuit_t *circ = NULL;
  34. destroy_cell_queue_t *cq = NULL;
  35. packed_cell_t *pc = NULL;
  36. destroy_cell_t *dc = NULL;
  37. scheduler_init();
  38. (void) arg;
  39. cmux = circuitmux_alloc();
  40. tt_assert(cmux);
  41. ch = new_fake_channel();
  42. circuitmux_set_policy(cmux, &ewma_policy);
  43. ch->has_queued_writes = has_queued_writes;
  44. ch->wide_circ_ids = 1;
  45. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  46. tt_ptr_op(circ, OP_EQ, NULL);
  47. tt_ptr_op(cq, OP_EQ, NULL);
  48. circuitmux_append_destroy_cell(ch, cmux, 100, 10);
  49. circuitmux_append_destroy_cell(ch, cmux, 190, 6);
  50. circuitmux_append_destroy_cell(ch, cmux, 30, 1);
  51. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 3);
  52. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  53. tt_ptr_op(circ, OP_EQ, NULL);
  54. tt_assert(cq);
  55. tt_int_op(cq->n, OP_EQ, 3);
  56. dc = destroy_cell_queue_pop(cq);
  57. tt_assert(dc);
  58. tt_uint_op(dc->circid, OP_EQ, 100);
  59. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 2);
  60. done:
  61. circuitmux_free(cmux);
  62. channel_free(ch);
  63. packed_cell_free(pc);
  64. tor_free(dc);
  65. }
  66. struct testcase_t circuitmux_tests[] = {
  67. { "destroy_cell_queue", test_cmux_destroy_cell_queue, TT_FORK, NULL, NULL },
  68. END_OF_TESTCASES
  69. };