test_channel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_CHANNEL_INTERNAL_
  4. #include "or.h"
  5. #include "channel.h"
  6. /* For channel_note_destroy_not_pending */
  7. #include "circuitlist.h"
  8. /* For var_cell_free */
  9. #include "connection_or.h"
  10. /* For packed_cell stuff */
  11. #define RELAY_PRIVATE
  12. #include "relay.h"
  13. /* For init/free stuff */
  14. #include "scheduler.h"
  15. #include "test.h"
  16. static int test_chan_accept_cells = 0;
  17. static int test_cells_written = 0;
  18. static int test_destroy_not_pending_calls = 0;
  19. static void channel_note_destroy_not_pending_mock(channel_t *ch,
  20. circid_t circid);
  21. static void chan_test_close(channel_t *ch);
  22. static size_t chan_test_num_bytes_queued(channel_t *ch);
  23. static int chan_test_write_cell(channel_t *ch, cell_t *cell);
  24. static int chan_test_write_packed_cell(channel_t *ch,
  25. packed_cell_t *packed_cell);
  26. static int chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell);
  27. static void make_fake_cell(cell_t *c);
  28. static void make_fake_var_cell(var_cell_t *c);
  29. static channel_t * new_fake_channel(void);
  30. static void scheduler_release_channel_mock(channel_t *ch);
  31. static void test_channel_write(void *arg);
  32. static void
  33. channel_note_destroy_not_pending_mock(channel_t *ch,
  34. circid_t circid)
  35. {
  36. (void)ch;
  37. (void)circid;
  38. ++test_destroy_not_pending_calls;
  39. }
  40. static void
  41. chan_test_close(channel_t *ch)
  42. {
  43. test_assert(ch);
  44. done:
  45. return;
  46. }
  47. static size_t
  48. chan_test_num_bytes_queued(channel_t *ch)
  49. {
  50. test_assert(ch);
  51. done:
  52. return 0;
  53. }
  54. static int
  55. chan_test_write_cell(channel_t *ch, cell_t *cell)
  56. {
  57. int rv = 0;
  58. test_assert(ch);
  59. test_assert(cell);
  60. if (test_chan_accept_cells) {
  61. /* Free the cell and bump the counter */
  62. tor_free(cell);
  63. ++test_cells_written;
  64. rv = 1;
  65. }
  66. /* else return 0, we didn't accept it */
  67. done:
  68. return rv;
  69. }
  70. static int
  71. chan_test_write_packed_cell(channel_t *ch,
  72. packed_cell_t *packed_cell)
  73. {
  74. int rv = 0;
  75. test_assert(ch);
  76. test_assert(packed_cell);
  77. if (test_chan_accept_cells) {
  78. /* Free the cell and bump the counter */
  79. packed_cell_free(packed_cell);
  80. ++test_cells_written;
  81. rv = 1;
  82. }
  83. /* else return 0, we didn't accept it */
  84. done:
  85. return rv;
  86. }
  87. static int
  88. chan_test_write_var_cell(channel_t *ch, var_cell_t *var_cell)
  89. {
  90. int rv = 0;
  91. test_assert(ch);
  92. test_assert(var_cell);
  93. if (test_chan_accept_cells) {
  94. /* Free the cell and bump the counter */
  95. var_cell_free(var_cell);
  96. ++test_cells_written;
  97. rv = 1;
  98. }
  99. /* else return 0, we didn't accept it */
  100. done:
  101. return rv;
  102. }
  103. static void
  104. make_fake_cell(cell_t *c)
  105. {
  106. test_assert(c != NULL);
  107. c->circ_id = 1;
  108. c->command = CELL_RELAY;
  109. memset(c->payload, 0, CELL_PAYLOAD_SIZE);
  110. done:
  111. return;
  112. }
  113. static void
  114. make_fake_var_cell(var_cell_t *c)
  115. {
  116. test_assert(c != NULL);
  117. c->circ_id = 1;
  118. c->command = CELL_VERSIONS;
  119. c->payload_len = CELL_PAYLOAD_SIZE / 2;
  120. memset(c->payload, 0, c->payload_len);
  121. done:
  122. return;
  123. }
  124. static channel_t *
  125. new_fake_channel(void)
  126. {
  127. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  128. channel_init(chan);
  129. chan->close = chan_test_close;
  130. chan->num_bytes_queued = chan_test_num_bytes_queued;
  131. chan->write_cell = chan_test_write_cell;
  132. chan->write_packed_cell = chan_test_write_packed_cell;
  133. chan->write_var_cell = chan_test_write_var_cell;
  134. chan->state = CHANNEL_STATE_OPEN;
  135. return chan;
  136. }
  137. static void
  138. scheduler_release_channel_mock(channel_t *ch)
  139. {
  140. (void)ch;
  141. /* Increment counter */
  142. ++test_releases_count;
  143. return;
  144. }
  145. static void
  146. test_channel_write(void *arg)
  147. {
  148. channel_t *ch = NULL;
  149. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  150. packed_cell_t *packed_cell = NULL;
  151. var_cell_t *var_cell =
  152. tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  153. int old_count;
  154. (void)arg;
  155. init_cell_pool();
  156. packed_cell = packed_cell_new();
  157. test_assert(packed_cell);
  158. ch = new_fake_channel();
  159. test_assert(ch);
  160. make_fake_cell(cell);
  161. make_fake_var_cell(var_cell);
  162. /* Tell it to accept cells */
  163. test_chan_accept_cells = 1;
  164. old_count = test_cells_written;
  165. channel_write_cell(ch, cell);
  166. test_assert(test_cells_written == old_count + 1);
  167. channel_write_var_cell(ch, var_cell);
  168. test_assert(test_cells_written == old_count + 2);
  169. channel_write_packed_cell(ch, packed_cell);
  170. test_assert(test_cells_written == old_count + 3);
  171. /* Now we test queueing; tell it not to accept cells */
  172. test_chan_accept_cells = 0;
  173. /* ...and keep it from trying to flush the queue */
  174. ch->state = CHANNEL_STATE_MAINT;
  175. /* Get a fresh cell */
  176. cell = tor_malloc_zero(sizeof(cell_t));
  177. make_fake_cell(cell);
  178. old_count = test_cells_written;
  179. channel_write_cell(ch, cell);
  180. test_assert(test_cells_written == old_count);
  181. /*
  182. * Now change back to open with channel_change_state() and assert that it
  183. * gets drained from the queue.
  184. */
  185. test_chan_accept_cells = 1;
  186. channel_change_state(ch, CHANNEL_STATE_OPEN);
  187. test_assert(test_cells_written == old_count + 1);
  188. /*
  189. * Check the note destroy case
  190. */
  191. cell = tor_malloc_zero(sizeof(cell_t));
  192. make_fake_cell(cell);
  193. cell->command = CELL_DESTROY;
  194. /* Set up the mock */
  195. MOCK(channel_note_destroy_not_pending,
  196. channel_note_destroy_not_pending_mock);
  197. old_count = test_destroy_not_pending_calls;
  198. channel_write_cell(ch, cell);
  199. test_assert(test_destroy_not_pending_calls == old_count + 1);
  200. /* Now send a non-destroy and check we don't call it */
  201. cell = tor_malloc_zero(sizeof(cell_t));
  202. make_fake_cell(cell);
  203. channel_write_cell(ch, cell);
  204. test_assert(test_destroy_not_pending_calls == old_count + 1);
  205. UNMOCK(channel_note_destroy_not_pending);
  206. /*
  207. * Now switch it to CLOSING so we can test the discard-cells case
  208. * in the channel_write_*() functions.
  209. */
  210. MOCK(scheduler_release_channel, scheduler_release_channel_mock);
  211. channel_mark_for_close(ch);
  212. UNMOCK(scheduler_release_channel);
  213. /* Send cells that will drop in the closing state */
  214. old_count = test_cells_written;
  215. cell = tor_malloc_zero(sizeof(cell_t));
  216. make_fake_cell(cell);
  217. channel_write_cell(ch, cell);
  218. test_assert(test_cells_written == old_count);
  219. var_cell = tor_malloc_zero(sizeof(var_cell_t) + CELL_PAYLOAD_SIZE);
  220. make_fake_var_cell(var_cell);
  221. channel_write_var_cell(ch, var_cell);
  222. test_assert(test_cells_written == old_count);
  223. packed_cell = packed_cell_new();
  224. channel_write_packed_cell(ch, packed_cell);
  225. test_assert(test_cells_written == old_count);
  226. free_cell_pool();
  227. done:
  228. tor_free(ch);
  229. return;
  230. }
  231. struct testcase_t channel_tests[] = {
  232. { "write", test_channel_write, TT_FORK, NULL, NULL },
  233. END_OF_TESTCASES
  234. };