test_cell_queue.c 4.2 KB

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