test_cell_queue.c 4.4 KB

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