test_cell_queue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CIRCUITLIST_PRIVATE
  4. #define RELAY_PRIVATE
  5. #include "or.h"
  6. #include "circuitlist.h"
  7. #include "relay.h"
  8. #include "test.h"
  9. static void
  10. test_cq_manip(void *arg)
  11. {
  12. packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc_tmp=NULL;
  13. cell_queue_t cq;
  14. cell_t cell;
  15. (void) arg;
  16. #ifdef ENABLE_MEMPOOLS
  17. init_cell_pool();
  18. #endif /* ENABLE_MEMPOOLS */
  19. cell_queue_init(&cq);
  20. tt_int_op(cq.n, ==, 0);
  21. pc1 = packed_cell_new();
  22. pc2 = packed_cell_new();
  23. pc3 = packed_cell_new();
  24. pc4 = packed_cell_new();
  25. tt_assert(pc1 && pc2 && pc3 && pc4);
  26. tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
  27. /* Add and remove a singleton. */
  28. cell_queue_append(&cq, pc1);
  29. tt_int_op(cq.n, ==, 1);
  30. tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
  31. tt_int_op(cq.n, ==, 0);
  32. /* Add and remove four items */
  33. cell_queue_append(&cq, pc4);
  34. cell_queue_append(&cq, pc3);
  35. cell_queue_append(&cq, pc2);
  36. cell_queue_append(&cq, pc1);
  37. tt_int_op(cq.n, ==, 4);
  38. tt_ptr_op(pc4, ==, cell_queue_pop(&cq));
  39. tt_ptr_op(pc3, ==, cell_queue_pop(&cq));
  40. tt_ptr_op(pc2, ==, cell_queue_pop(&cq));
  41. tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
  42. tt_int_op(cq.n, ==, 0);
  43. tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
  44. /* Try a packed copy (wide, then narrow, which is a bit of a cheat, since a
  45. * real cell queue has only one type.) */
  46. memset(&cell, 0, sizeof(cell));
  47. cell.circ_id = 0x12345678;
  48. cell.command = 10;
  49. strlcpy((char*)cell.payload, "Lorax ipsum gruvvulus thneed amet, snergelly "
  50. "once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et "
  51. "truffula magna aliqua.",
  52. sizeof(cell.payload));
  53. cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
  54. 1 /*wide*/, 0 /*stats*/);
  55. cell.circ_id = 0x2013;
  56. cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
  57. 0 /*wide*/, 0 /*stats*/);
  58. tt_int_op(cq.n, ==, 2);
  59. pc_tmp = cell_queue_pop(&cq);
  60. tt_int_op(cq.n, ==, 1);
  61. tt_ptr_op(pc_tmp, !=, NULL);
  62. test_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
  63. test_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
  64. packed_cell_free(pc_tmp);
  65. pc_tmp = cell_queue_pop(&cq);
  66. tt_int_op(cq.n, ==, 0);
  67. tt_ptr_op(pc_tmp, !=, NULL);
  68. test_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
  69. test_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
  70. packed_cell_free(pc_tmp);
  71. pc_tmp = NULL;
  72. tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
  73. /* Now make sure cell_queue_clear works. */
  74. cell_queue_append(&cq, pc2);
  75. cell_queue_append(&cq, pc1);
  76. tt_int_op(cq.n, ==, 2);
  77. cell_queue_clear(&cq);
  78. pc2 = pc1 = NULL; /* prevent double-free */
  79. tt_int_op(cq.n, ==, 0);
  80. done:
  81. packed_cell_free(pc1);
  82. packed_cell_free(pc2);
  83. packed_cell_free(pc3);
  84. packed_cell_free(pc4);
  85. packed_cell_free(pc_tmp);
  86. cell_queue_clear(&cq);
  87. #ifdef ENABLE_MEMPOOLS
  88. free_cell_pool();
  89. #endif /* ENABLE_MEMPOOLS */
  90. }
  91. static void
  92. test_circuit_n_cells(void *arg)
  93. {
  94. packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc5=NULL;
  95. origin_circuit_t *origin_c=NULL;
  96. or_circuit_t *or_c=NULL;
  97. (void)arg;
  98. #ifdef ENABLE_MEMPOOLS
  99. init_cell_pool();
  100. #endif /* ENABLE_MEMPOOLS */
  101. pc1 = packed_cell_new();
  102. pc2 = packed_cell_new();
  103. pc3 = packed_cell_new();
  104. pc4 = packed_cell_new();
  105. pc5 = packed_cell_new();
  106. tt_assert(pc1 && pc2 && pc3 && pc4 && pc5);
  107. or_c = or_circuit_new(0, NULL);
  108. origin_c = origin_circuit_new();
  109. origin_c->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  110. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 0);
  111. cell_queue_append(&or_c->p_chan_cells, pc1);
  112. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 1);
  113. cell_queue_append(&or_c->base_.n_chan_cells, pc2);
  114. cell_queue_append(&or_c->base_.n_chan_cells, pc3);
  115. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 3);
  116. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 0);
  117. cell_queue_append(&origin_c->base_.n_chan_cells, pc4);
  118. cell_queue_append(&origin_c->base_.n_chan_cells, pc5);
  119. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 2);
  120. done:
  121. circuit_free(TO_CIRCUIT(or_c));
  122. circuit_free(TO_CIRCUIT(origin_c));
  123. #ifdef ENABLE_MEMPOOLS
  124. free_cell_pool();
  125. #endif /* ENABLE_MEMPOOLS */
  126. }
  127. struct testcase_t cell_queue_tests[] = {
  128. { "basic", test_cq_manip, TT_FORK, NULL, NULL, },
  129. { "circ_n_cells", test_circuit_n_cells, TT_FORK, NULL, NULL },
  130. END_OF_TESTCASES
  131. };