aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_buffers.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-07-17 17:51:21 -0400
committerNick Mathewson <nickm@torproject.org>2013-08-15 12:03:36 -0400
commit7da59721a9963862b7b19dbba4e55d010c296d34 (patch)
tree96aaff19962770309a0915937cc15ee4ca5a9b3c /src/test/test_buffers.c
parent7a12cbc03d8fda94c9387a1976ad2ffc30b4b5b7 (diff)
downloadtor-7da59721a9963862b7b19dbba4e55d010c296d34.tar
tor-7da59721a9963862b7b19dbba4e55d010c296d34.tar.gz
Unit tests for fetch_ext_or_cmd
Diffstat (limited to 'src/test/test_buffers.c')
-rw-r--r--src/test/test_buffers.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index 7a8cf1002..a009faa0b 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -6,6 +6,7 @@
#define BUFFERS_PRIVATE
#include "or.h"
#include "buffers.h"
+#include "ext_orport.h"
#include "test.h"
/** Run unit tests for buffers.c */
@@ -258,9 +259,84 @@ test_buffer_copy(void *arg)
generic_buffer_free(buf2);
}
+static void
+test_buffer_ext_or_cmd(void *arg)
+{
+ ext_or_cmd_t *cmd = NULL;
+ generic_buffer_t *buf = generic_buffer_new();
+ char *tmp = NULL;
+ (void) arg;
+
+ /* Empty -- should give "not there. */
+ tt_int_op(0, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, ==, cmd);
+
+ /* Three bytes: shouldn't work. */
+ generic_buffer_add(buf, "\x00\x20\x00", 3);
+ tt_int_op(0, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, ==, cmd);
+ tt_int_op(3, ==, generic_buffer_len(buf));
+
+ /* 0020 0000: That's a nil command. It should work. */
+ generic_buffer_add(buf, "\x00", 1);
+ tt_int_op(1, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, !=, cmd);
+ tt_int_op(0x20, ==, cmd->cmd);
+ tt_int_op(0, ==, cmd->len);
+ tt_int_op(0, ==, generic_buffer_len(buf));
+ ext_or_cmd_free(cmd);
+ cmd = NULL;
+
+ /* Now try a length-6 command with one byte missing. */
+ generic_buffer_add(buf, "\x10\x21\x00\x06""abcde", 9);
+ tt_int_op(0, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, ==, cmd);
+ generic_buffer_add(buf, "f", 1);
+ tt_int_op(1, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, !=, cmd);
+ tt_int_op(0x1021, ==, cmd->cmd);
+ tt_int_op(6, ==, cmd->len);
+ test_mem_op("abcdef", ==, cmd->body, 6);
+ tt_int_op(0, ==, generic_buffer_len(buf));
+ ext_or_cmd_free(cmd);
+ cmd = NULL;
+
+ /* Now try a length-10 command with 4 extra bytes. */
+ generic_buffer_add(buf, "\xff\xff\x00\x0a"
+ "loremipsum\x10\x00\xff\xff", 18);
+ tt_int_op(1, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, !=, cmd);
+ tt_int_op(0xffff, ==, cmd->cmd);
+ tt_int_op(10, ==, cmd->len);
+ test_mem_op("loremipsum", ==, cmd->body, 10);
+ tt_int_op(4, ==, generic_buffer_len(buf));
+ ext_or_cmd_free(cmd);
+ cmd = NULL;
+
+ /* Finally, let's try a maximum-length command. We already have the header
+ * waiting. */
+ tt_int_op(0, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tmp = tor_malloc_zero(65535);
+ generic_buffer_add(buf, tmp, 65535);
+ tt_int_op(1, ==, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
+ tt_ptr_op(NULL, !=, cmd);
+ tt_int_op(0x1000, ==, cmd->cmd);
+ tt_int_op(0xffff, ==, cmd->len);
+ test_mem_op(tmp, ==, cmd->body, 65535);
+ tt_int_op(0, ==, generic_buffer_len(buf));
+ ext_or_cmd_free(cmd);
+ cmd = NULL;
+
+ done:
+ ext_or_cmd_free(cmd);
+ generic_buffer_free(buf);
+ tor_free(tmp);
+}
+
struct testcase_t buffer_tests[] = {
{ "basic", test_buffers_basic, 0, NULL, NULL },
{ "copy", test_buffer_copy, 0, NULL, NULL },
+ { "ext_or_cmd", test_buffer_ext_or_cmd, 0, NULL, NULL },
END_OF_TESTCASES
};