test_cell_queue.c 4.3 KB

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