test_proto_misc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "proto_cell.h"
  12. #include "proto_control0.h"
  13. #include "proto_http.h"
  14. #include "proto_socks.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. struct testcase_t proto_misc_tests[] = {
  123. { "var_cell", test_proto_var_cell, 0, NULL, NULL },
  124. { "control0", test_proto_control0, 0, NULL, NULL },
  125. END_OF_TESTCASES
  126. };