[tor-commits] [tor/master] Add append_cell_to_circuit_queue() unit test

nickm at torproject.org nickm at torproject.org
Fri Nov 28 03:58:32 UTC 2014


commit 5992a69deee324fb91f5f2995e145edd2c0560d2
Author: Andrea Shepard <andrea at torproject.org>
Date:   Tue Jan 21 02:54:35 2014 -0800

    Add append_cell_to_circuit_queue() unit test
---
 src/test/Makefile.nmake |    4 +-
 src/test/include.am     |    1 +
 src/test/test.c         |    2 +
 src/test/test_relay.c   |  130 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 135 insertions(+), 2 deletions(-)

diff --git a/src/test/Makefile.nmake b/src/test/Makefile.nmake
index fceef99..2a347bd 100644
--- a/src/test/Makefile.nmake
+++ b/src/test/Makefile.nmake
@@ -14,8 +14,8 @@ LIBS = ..\..\..\build-alpha\lib\libevent.lib \
 TEST_OBJECTS = test.obj test_addr.obj test_channel.obj test_containers.obj \
 	test_controller_events.ogj test_crypto.obj test_data.obj test_dir.obj \
 	test_microdesc.obj test_pt.obj test_util.obj test_config.obj \
-	test_cell_formats.obj test_replay.obj test_introduce.obj tinytest.obj \
-	test_hs.obj
+	test_cell_formats.obj test_relay.obj test_replay.obj \
+	test_introduce.obj tinytest.obj test_hs.obj
 
 tinytest.obj: ..\ext\tinytest.c
 	$(CC) $(CFLAGS) /D snprintf=_snprintf /c ..\ext\tinytest.c
diff --git a/src/test/include.am b/src/test/include.am
index 547f23b..531b81e 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -38,6 +38,7 @@ src_test_test_SOURCES = \
 	src/test/test_options.c \
 	src/test/test_pt.c \
 	src/test/test_relaycell.c \
+	src/test/test_relay.c \
 	src/test/test_replay.c \
 	src/test/test_routerkeys.c \
 	src/test/test_socks.c \
diff --git a/src/test/test.c b/src/test/test.c
index 4d4f6f2..7ade83d 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1310,6 +1310,7 @@ extern struct testcase_t status_tests[];
 extern struct testcase_t routerset_tests[];
 extern struct testcase_t router_tests[];
 extern struct testcase_t channel_tests[];
+extern struct testcase_t relay_tests[];
 
 static struct testgroup_t testgroups[] = {
   { "", test_array },
@@ -1343,6 +1344,7 @@ static struct testgroup_t testgroups[] = {
   { "status/" , status_tests },
   { "routerset/" , routerset_tests },
   { "channel/", channel_tests },
+  { "relay/" , relay_tests },
   END_OF_GROUPS
 };
 
diff --git a/src/test/test_relay.c b/src/test/test_relay.c
new file mode 100644
index 0000000..6b9cb33
--- /dev/null
+++ b/src/test/test_relay.c
@@ -0,0 +1,130 @@
+/* Copyright (c) 2014, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "or.h"
+#define CIRCUITBUILD_PRIVATE
+#include "circuitbuild.h"
+#define RELAY_PRIVATE
+#include "relay.h"
+/* For init/free stuff */
+#include "scheduler.h"
+
+/* Test suite stuff */
+#include "test.h"
+#include "fakechans.h"
+
+static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
+
+static void test_relay_append_cell_to_circuit_queue(void *arg);
+
+static or_circuit_t *
+new_fake_orcirc(channel_t *nchan, channel_t *pchan)
+{
+  or_circuit_t *orcirc = NULL;
+  circuit_t *circ = NULL;
+
+  orcirc = tor_malloc_zero(sizeof(*orcirc));
+  circ = &(orcirc->base_);
+  circ->magic = OR_CIRCUIT_MAGIC;
+
+  circ->n_chan = nchan;
+  circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
+  circ->n_mux = NULL; /* ?? */
+  cell_queue_init(&(circ->n_chan_cells));
+  circ->n_hop = NULL;
+  circ->streams_blocked_on_n_chan = 0;
+  circ->streams_blocked_on_p_chan = 0;
+  circ->n_delete_pending = 0;
+  circ->p_delete_pending = 0;
+  circ->received_destroy = 0;
+  circ->state = CIRCUIT_STATE_OPEN;
+  circ->purpose = CIRCUIT_PURPOSE_OR;
+  circ->package_window = CIRCWINDOW_START_MAX;
+  circ->deliver_window = CIRCWINDOW_START_MAX;
+  circ->n_chan_create_cell = NULL;
+
+  orcirc->p_chan = pchan;
+  orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
+  cell_queue_init(&(orcirc->p_chan_cells));
+
+  return orcirc;
+}
+
+static void
+test_relay_append_cell_to_circuit_queue(void *arg)
+{
+  channel_t *nchan = NULL, *pchan = NULL;
+  or_circuit_t *orcirc = NULL;
+  cell_t *cell = NULL;
+  int old_count, new_count;
+
+  (void)arg;
+
+  /* We'll need the cell pool for append_cell_to_circuit_queue() to work */
+  init_cell_pool();
+
+  /* Make fake channels to be nchan and pchan for the circuit */
+  nchan = new_fake_channel();
+  test_assert(nchan);
+
+  pchan = new_fake_channel();
+  test_assert(pchan);
+
+  /* We'll need chans with working cmuxes */
+  nchan->cmux = circuitmux_alloc();
+  pchan->cmux = circuitmux_alloc();
+
+  /* Make a fake orcirc */
+  orcirc = new_fake_orcirc(nchan, pchan);
+  test_assert(orcirc);
+
+  /* Make a cell */
+  cell = tor_malloc_zero(sizeof(cell_t));
+  make_fake_cell(cell);
+
+  MOCK(scheduler_channel_has_waiting_cells,
+       scheduler_channel_has_waiting_cells_mock);
+
+  /* Append it */
+  old_count = get_mock_scheduler_has_waiting_cells_count();
+  append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
+                               CELL_DIRECTION_OUT, 0);
+  new_count = get_mock_scheduler_has_waiting_cells_count();
+  test_eq(new_count, old_count + 1);
+
+  /* Now try the reverse direction */
+  old_count = get_mock_scheduler_has_waiting_cells_count();
+  append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
+                               CELL_DIRECTION_IN, 0);
+  new_count = get_mock_scheduler_has_waiting_cells_count();
+  test_eq(new_count, old_count + 1);
+
+  UNMOCK(scheduler_channel_has_waiting_cells);
+
+  /* Get rid of the fake channels */
+  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
+  channel_mark_for_close(nchan);
+  channel_mark_for_close(pchan);
+  UNMOCK(scheduler_release_channel);
+
+  /* Shut down channels */
+  channel_free_all();
+  nchan = pchan = NULL;
+
+ done:
+  tor_free(orcirc);
+  if (nchan && nchan->cmux) circuitmux_free(nchan->cmux);
+  tor_free(nchan);
+  if (pchan && pchan->cmux) circuitmux_free(pchan->cmux);
+  tor_free(pchan);
+  free_cell_pool();
+
+  return;
+}
+
+struct testcase_t relay_tests[] = {
+  { "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue,
+    TT_FORK, NULL, NULL },
+  END_OF_TESTCASES
+};
+





More information about the tor-commits mailing list