test_cell_queue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(&cq, &cell, 1 /*wide*/, 0 /*stats*/);
  52. cell.circ_id = 0x2013;
  53. cell_queue_append_packed_copy(&cq, &cell, 0 /*wide*/, 0 /*stats*/);
  54. tt_int_op(cq.n, ==, 2);
  55. pc_tmp = cell_queue_pop(&cq);
  56. tt_int_op(cq.n, ==, 1);
  57. tt_ptr_op(pc_tmp, !=, NULL);
  58. test_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
  59. test_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
  60. packed_cell_free(pc_tmp);
  61. pc_tmp = cell_queue_pop(&cq);
  62. tt_int_op(cq.n, ==, 0);
  63. tt_ptr_op(pc_tmp, !=, NULL);
  64. test_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
  65. test_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
  66. packed_cell_free(pc_tmp);
  67. pc_tmp = NULL;
  68. tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
  69. /* Now make sure cell_queue_clear works. */
  70. cell_queue_append(&cq, pc2);
  71. cell_queue_append(&cq, pc1);
  72. tt_int_op(cq.n, ==, 2);
  73. cell_queue_clear(&cq);
  74. pc2 = pc1 = NULL; /* prevent double-free */
  75. tt_int_op(cq.n, ==, 0);
  76. done:
  77. packed_cell_free(pc1);
  78. packed_cell_free(pc2);
  79. packed_cell_free(pc3);
  80. packed_cell_free(pc4);
  81. packed_cell_free(pc_tmp);
  82. cell_queue_clear(&cq);
  83. free_cell_pool();
  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. init_cell_pool();
  93. pc1 = packed_cell_new();
  94. pc2 = packed_cell_new();
  95. pc3 = packed_cell_new();
  96. pc4 = packed_cell_new();
  97. pc5 = packed_cell_new();
  98. tt_assert(pc1 && pc2 && pc3 && pc4 && pc5);
  99. or_c = or_circuit_new(0, NULL);
  100. origin_c = origin_circuit_new();
  101. origin_c->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  102. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 0);
  103. cell_queue_append(&or_c->p_chan_cells, pc1);
  104. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 1);
  105. cell_queue_append(&or_c->base_.n_chan_cells, pc2);
  106. cell_queue_append(&or_c->base_.n_chan_cells, pc3);
  107. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 3);
  108. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 0);
  109. cell_queue_append(&origin_c->base_.n_chan_cells, pc4);
  110. cell_queue_append(&origin_c->base_.n_chan_cells, pc5);
  111. tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 2);
  112. done:
  113. circuit_free(TO_CIRCUIT(or_c));
  114. circuit_free(TO_CIRCUIT(origin_c));
  115. free_cell_pool();
  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. };