test_circuitmux.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2013-2014, 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 "relay.h"
  10. #include "scheduler.h"
  11. #include "test.h"
  12. /* XXXX duplicated function from test_circuitlist.c */
  13. static channel_t *
  14. new_fake_channel(void)
  15. {
  16. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  17. channel_init(chan);
  18. return chan;
  19. }
  20. static int
  21. has_queued_writes(channel_t *c)
  22. {
  23. (void) c;
  24. return 1;
  25. }
  26. /** Test destroy cell queue with no interference from other queues. */
  27. static void
  28. test_cmux_destroy_cell_queue(void *arg)
  29. {
  30. circuitmux_t *cmux = NULL;
  31. channel_t *ch = NULL;
  32. circuit_t *circ = NULL;
  33. cell_queue_t *cq = NULL;
  34. packed_cell_t *pc = NULL;
  35. tor_libevent_cfg cfg;
  36. memset(&cfg, 0, sizeof(cfg));
  37. tor_libevent_initialize(&cfg);
  38. scheduler_init();
  39. #ifdef ENABLE_MEMPOOLS
  40. init_cell_pool();
  41. #endif /* ENABLE_MEMPOOLS */
  42. (void) arg;
  43. cmux = circuitmux_alloc();
  44. tt_assert(cmux);
  45. ch = new_fake_channel();
  46. ch->has_queued_writes = has_queued_writes;
  47. ch->wide_circ_ids = 1;
  48. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  49. tt_assert(!circ);
  50. tt_assert(!cq);
  51. circuitmux_append_destroy_cell(ch, cmux, 100, 10);
  52. circuitmux_append_destroy_cell(ch, cmux, 190, 6);
  53. circuitmux_append_destroy_cell(ch, cmux, 30, 1);
  54. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 3);
  55. circ = circuitmux_get_first_active_circuit(cmux, &cq);
  56. tt_assert(!circ);
  57. tt_assert(cq);
  58. tt_int_op(cq->n, OP_EQ, 3);
  59. pc = cell_queue_pop(cq);
  60. tt_assert(pc);
  61. tt_mem_op(pc->body, OP_EQ, "\x00\x00\x00\x64\x04\x0a\x00\x00\x00", 9);
  62. packed_cell_free(pc);
  63. pc = NULL;
  64. tt_int_op(circuitmux_num_cells(cmux), OP_EQ, 2);
  65. done:
  66. circuitmux_free(cmux);
  67. channel_free(ch);
  68. packed_cell_free(pc);
  69. #ifdef ENABLE_MEMPOOLS
  70. free_cell_pool();
  71. #endif /* ENABLE_MEMPOOLS */
  72. }
  73. struct testcase_t circuitmux_tests[] = {
  74. { "destroy_cell_queue", test_cmux_destroy_cell_queue, TT_FORK, NULL, NULL },
  75. END_OF_TESTCASES
  76. };