test_cell_queue.c 4.2 KB

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