test_proto_misc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_proto_misc.c
  5. * \brief Test our smaller buffer-based protocol functions
  6. */
  7. #include "or.h"
  8. #include "test.h"
  9. #include "buffers.h"
  10. #include "connection_or.h"
  11. #include "ext_orport.h"
  12. #include "proto_cell.h"
  13. #include "proto_control0.h"
  14. #include "proto_ext_or.h"
  15. static void
  16. test_proto_var_cell(void *arg)
  17. {
  18. (void)arg;
  19. char *mem_op_hex_tmp = NULL;
  20. char tmp[1024];
  21. buf_t *buf = NULL;
  22. var_cell_t *cell = NULL;
  23. buf = buf_new();
  24. memset(tmp, 0xf0, sizeof(tmp));
  25. /* Short little commands will make us say "no cell yet." */
  26. tt_int_op(0, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  27. tt_ptr_op(cell, OP_EQ, NULL);
  28. buf_add(buf, "\x01\x02\x02\0x2", 4);
  29. tt_int_op(0, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  30. /* An incomplete fixed-length cell makes us say "no cell yet". */
  31. buf_add(buf, "\x03", 1);
  32. tt_int_op(0, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  33. /* A complete fixed length-cell makes us say "not a variable-length cell" */
  34. buf_add(buf, tmp, 509);
  35. tt_int_op(0, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  36. buf_clear(buf);
  37. /* An incomplete versions cell is a variable-length cell that isn't ready
  38. * yet. */
  39. buf_add(buf,
  40. "\x01\x02\x03\x04" /* circid */
  41. "\x07" /* VERSIONS */
  42. "\x00\x04" /* 4 bytes long */
  43. "\x00" /* incomplete */, 8);
  44. tt_int_op(1, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  45. tt_ptr_op(cell, OP_EQ, NULL);
  46. /* Complete it, and it's a variable-length cell. Leave a byte on the end for
  47. * fun. */
  48. buf_add(buf, "\x09\x00\x25\ff", 4);
  49. tt_int_op(1, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 4));
  50. tt_ptr_op(cell, OP_NE, NULL);
  51. tt_int_op(cell->command, OP_EQ, CELL_VERSIONS);
  52. tt_uint_op(cell->circ_id, OP_EQ, 0x01020304);
  53. tt_int_op(cell->payload_len, OP_EQ, 4);
  54. test_mem_op_hex(cell->payload, OP_EQ, "00090025");
  55. var_cell_free(cell);
  56. cell = NULL;
  57. tt_int_op(buf_datalen(buf), OP_EQ, 1);
  58. buf_clear(buf);
  59. /* In link protocol 3 and earlier, circid fields were two bytes long. Let's
  60. * ensure that gets handled correctly. */
  61. buf_add(buf,
  62. "\x23\x45\x81\x00\x06" /* command 81; 6 bytes long */
  63. "coraje", 11);
  64. tt_int_op(1, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 3));
  65. tt_ptr_op(cell, OP_NE, NULL);
  66. tt_int_op(cell->command, OP_EQ, 129);
  67. tt_uint_op(cell->circ_id, OP_EQ, 0x2345);
  68. tt_int_op(cell->payload_len, OP_EQ, 6);
  69. tt_mem_op(cell->payload, OP_EQ, "coraje", 6);
  70. var_cell_free(cell);
  71. cell = NULL;
  72. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  73. /* In link protocol 2, only VERSIONS cells counted as variable-length */
  74. buf_add(buf,
  75. "\x23\x45\x81\x00\x06"
  76. "coraje", 11); /* As above */
  77. tt_int_op(0, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 2));
  78. buf_clear(buf);
  79. buf_add(buf,
  80. "\x23\x45\x07\x00\x06"
  81. "futuro", 11);
  82. tt_int_op(1, OP_EQ, fetch_var_cell_from_buf(buf, &cell, 2));
  83. tt_ptr_op(cell, OP_NE, NULL);
  84. tt_int_op(cell->command, OP_EQ, 7);
  85. tt_uint_op(cell->circ_id, OP_EQ, 0x2345);
  86. tt_int_op(cell->payload_len, OP_EQ, 6);
  87. tt_mem_op(cell->payload, OP_EQ, "futuro", 6);
  88. var_cell_free(cell);
  89. cell = NULL;
  90. done:
  91. buf_free(buf);
  92. var_cell_free(cell);
  93. tor_free(mem_op_hex_tmp);
  94. }
  95. static void
  96. test_proto_control0(void *arg)
  97. {
  98. (void)arg;
  99. buf_t *buf = buf_new();
  100. /* The only remaining function for the v0 control protocol is the function
  101. that detects whether the user has stumbled across an old controller
  102. that's using it. The format was:
  103. u16 length;
  104. u16 command;
  105. u8 body[length];
  106. */
  107. /* Empty buffer -- nothing to do. */
  108. tt_int_op(0, OP_EQ, peek_buf_has_control0_command(buf));
  109. /* 3 chars in buf -- can't tell */
  110. buf_add(buf, "AUT", 3);
  111. tt_int_op(0, OP_EQ, peek_buf_has_control0_command(buf));
  112. /* command in buf -- easy to tell */
  113. buf_add(buf, "HENTICATE ", 10);
  114. tt_int_op(0, OP_EQ, peek_buf_has_control0_command(buf));
  115. /* Control0 command header in buf: make sure we detect it. */
  116. buf_clear(buf);
  117. buf_add(buf, "\x09\x05" "\x00\x05" "blah", 8);
  118. tt_int_op(1, OP_EQ, peek_buf_has_control0_command(buf));
  119. done:
  120. buf_free(buf);
  121. }
  122. static void
  123. test_proto_ext_or_cmd(void *arg)
  124. {
  125. ext_or_cmd_t *cmd = NULL;
  126. buf_t *buf = buf_new();
  127. char *tmp = NULL;
  128. (void) arg;
  129. /* Empty -- should give "not there. */
  130. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  131. tt_ptr_op(NULL, OP_EQ, cmd);
  132. /* Three bytes: shouldn't work. */
  133. buf_add(buf, "\x00\x20\x00", 3);
  134. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  135. tt_ptr_op(NULL, OP_EQ, cmd);
  136. tt_int_op(3, OP_EQ, buf_datalen(buf));
  137. /* 0020 0000: That's a nil command. It should work. */
  138. buf_add(buf, "\x00", 1);
  139. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  140. tt_ptr_op(NULL, OP_NE, cmd);
  141. tt_int_op(0x20, OP_EQ, cmd->cmd);
  142. tt_int_op(0, OP_EQ, cmd->len);
  143. tt_int_op(0, OP_EQ, buf_datalen(buf));
  144. ext_or_cmd_free(cmd);
  145. cmd = NULL;
  146. /* Now try a length-6 command with one byte missing. */
  147. buf_add(buf, "\x10\x21\x00\x06""abcde", 9);
  148. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  149. tt_ptr_op(NULL, OP_EQ, cmd);
  150. buf_add(buf, "f", 1);
  151. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  152. tt_ptr_op(NULL, OP_NE, cmd);
  153. tt_int_op(0x1021, OP_EQ, cmd->cmd);
  154. tt_int_op(6, OP_EQ, cmd->len);
  155. tt_mem_op("abcdef", OP_EQ, cmd->body, 6);
  156. tt_int_op(0, OP_EQ, buf_datalen(buf));
  157. ext_or_cmd_free(cmd);
  158. cmd = NULL;
  159. /* Now try a length-10 command with 4 extra bytes. */
  160. buf_add(buf, "\xff\xff\x00\x0aloremipsum\x10\x00\xff\xff", 18);
  161. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  162. tt_ptr_op(NULL, OP_NE, cmd);
  163. tt_int_op(0xffff, OP_EQ, cmd->cmd);
  164. tt_int_op(10, OP_EQ, cmd->len);
  165. tt_mem_op("loremipsum", OP_EQ, cmd->body, 10);
  166. tt_int_op(4, OP_EQ, buf_datalen(buf));
  167. ext_or_cmd_free(cmd);
  168. cmd = NULL;
  169. /* Finally, let's try a maximum-length command. We already have the header
  170. * waiting. */
  171. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  172. tmp = tor_malloc_zero(65535);
  173. buf_add(buf, tmp, 65535);
  174. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  175. tt_ptr_op(NULL, OP_NE, cmd);
  176. tt_int_op(0x1000, OP_EQ, cmd->cmd);
  177. tt_int_op(0xffff, OP_EQ, cmd->len);
  178. tt_mem_op(tmp, OP_EQ, cmd->body, 65535);
  179. tt_int_op(0, OP_EQ, buf_datalen(buf));
  180. ext_or_cmd_free(cmd);
  181. cmd = NULL;
  182. done:
  183. ext_or_cmd_free(cmd);
  184. buf_free(buf);
  185. tor_free(tmp);
  186. }
  187. static void
  188. test_proto_line(void *arg)
  189. {
  190. (void)arg;
  191. char tmp[60];
  192. buf_t *buf = buf_new();
  193. #define S(str) str, sizeof(str)-1
  194. const struct {
  195. const char *input;
  196. size_t input_len;
  197. size_t line_len;
  198. const char *output;
  199. int returnval;
  200. } cases[] = {
  201. { S("Hello world"), 0, NULL, 0 },
  202. { S("Hello world\n"), 12, "Hello world\n", 1 },
  203. { S("Hello world\nMore"), 12, "Hello world\n", 1 },
  204. { S("\n oh hello world\nMore"), 1, "\n", 1 },
  205. { S("Hello worpd\n\nMore"), 12, "Hello worpd\n", 1 },
  206. { S("------------------------------------------------------------\n"), 0,
  207. NULL, -1 },
  208. };
  209. unsigned i;
  210. for (i = 0; i < ARRAY_LENGTH(cases); ++i) {
  211. buf_add(buf, cases[i].input, cases[i].input_len);
  212. memset(tmp, 0xfe, sizeof(tmp));
  213. size_t sz = sizeof(tmp);
  214. int rv = buf_get_line(buf, tmp, &sz);
  215. tt_int_op(rv, OP_EQ, cases[i].returnval);
  216. if (rv == 1) {
  217. tt_int_op(sz, OP_LT, sizeof(tmp));
  218. tt_mem_op(cases[i].output, OP_EQ, tmp, sz+1);
  219. tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len - strlen(tmp));
  220. tt_int_op(sz, OP_EQ, cases[i].line_len);
  221. } else {
  222. tt_int_op(buf_datalen(buf), OP_EQ, cases[i].input_len);
  223. // tt_int_op(sz, OP_EQ, sizeof(tmp));
  224. }
  225. buf_clear(buf);
  226. }
  227. done:
  228. buf_free(buf);
  229. }
  230. struct testcase_t proto_misc_tests[] = {
  231. { "var_cell", test_proto_var_cell, 0, NULL, NULL },
  232. { "control0", test_proto_control0, 0, NULL, NULL },
  233. { "ext_or_cmd", test_proto_ext_or_cmd, TT_FORK, NULL, NULL },
  234. { "line", test_proto_line, 0, NULL, NULL },
  235. END_OF_TESTCASES
  236. };