test_circuitmux.c 1.9 KB

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