[tor-commits] [tor/master] test: Add fakecircs.{h|c} helper

nickm at torproject.org nickm at torproject.org
Fri Nov 1 13:39:51 UTC 2019


commit dba249bc735b02b6cafe92e5be7e8df049c53ae6
Author: David Goulet <dgoulet at torproject.org>
Date:   Tue Oct 22 11:35:53 2019 -0400

    test: Add fakecircs.{h|c} helper
    
    Fake circuits are created everywhere in the unit tests. This is an attempt at
    centralizing a "fake circuit creation" API like fakechans.c does for channel.
    
    This commit introduces fakecircs.c and changes test_relay.c and
    test_circpadding.c which were using roughly the same code.
    
    This will allow easier OR circuit creation for the future tests in
    test_circuitmux.c
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/test/fakecircs.c           | 88 ++++++++++++++++++++++++++++++++++++++++++
 src/test/fakecircs.h           | 17 ++++++++
 src/test/include.am            |  2 +
 src/test/test_circuitpadding.c | 81 ++++++--------------------------------
 src/test/test_relay.c          | 38 ++----------------
 5 files changed, 122 insertions(+), 104 deletions(-)

diff --git a/src/test/fakecircs.c b/src/test/fakecircs.c
new file mode 100644
index 000000000..62027e033
--- /dev/null
+++ b/src/test/fakecircs.c
@@ -0,0 +1,88 @@
+/* Copyright (c) 2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file fakecircs.c
+ * \brief Fake circuits API for unit test.
+ **/
+
+#define CHANNEL_PRIVATE
+#define CIRCUITBUILD_PRIVATE
+#define CIRCUITLIST_PRIVATE
+#define CRYPT_PATH_PRIVATE
+
+#include "core/or/or.h"
+
+#include "core/crypto/relay_crypto.h"
+#include "core/or/channel.h"
+#include "core/or/circuitbuild.h"
+#include "core/or/circuitlist.h"
+#include "core/or/circuitpadding.h"
+#include "core/or/crypt_path.h"
+#include "core/or/relay.h"
+#include "core/or/relay_crypto_st.h"
+
+#include "fakecircs.h"
+
+/** Return newly allocated OR circuit using the given nchan and pchan. It must
+ * be freed with the free_fake_orcirc(). */
+or_circuit_t *
+new_fake_orcirc(channel_t *nchan, channel_t *pchan)
+{
+  or_circuit_t *orcirc = NULL;
+  circuit_t *circ = NULL;
+  crypt_path_t tmp_cpath;
+  char whatevs_key[CPATH_KEY_MATERIAL_LEN];
+
+  orcirc = tor_malloc_zero(sizeof(*orcirc));
+  circ = &(orcirc->base_);
+  circ->magic = OR_CIRCUIT_MAGIC;
+
+  circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan);
+  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;
+
+  circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan);
+  cell_queue_init(&(orcirc->p_chan_cells));
+
+  memset(&tmp_cpath, 0, sizeof(tmp_cpath));
+  if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key,
+                                sizeof(whatevs_key), 0, 0)<0) {
+    log_warn(LD_BUG,"Circuit initialization failed");
+    return NULL;
+  }
+  orcirc->crypto = tmp_cpath.pvt_crypto;
+
+  return orcirc;
+}
+
+/** Free fake OR circuit which MUST be created by new_fake_orcirc(). */
+void
+free_fake_orcirc(or_circuit_t *orcirc)
+{
+  circuit_t *circ = TO_CIRCUIT(orcirc);
+
+  relay_crypto_clear(&orcirc->crypto);
+
+  circpad_circuit_free_all_machineinfos(circ);
+
+  if (orcirc->p_chan && orcirc->p_chan->cmux) {
+    circuitmux_detach_circuit(orcirc->p_chan->cmux, circ);
+  }
+  if (circ->n_chan && circ->n_chan->cmux) {
+    circuitmux_detach_circuit(circ->n_chan->cmux, circ);
+  }
+
+  tor_free_(circ);
+}
diff --git a/src/test/fakecircs.h b/src/test/fakecircs.h
new file mode 100644
index 000000000..5fd02027f
--- /dev/null
+++ b/src/test/fakecircs.h
@@ -0,0 +1,17 @@
+/* Copyright (c) 2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file fakecircs.h
+ * \brief Declarations for fake circuits for test suite use.
+ **/
+
+#ifndef TOR_FAKECIRCS_H
+#define TOR_FAKECIRCS_H
+
+#include "core/or/or_circuit_st.h"
+
+or_circuit_t *new_fake_orcirc(channel_t *nchan, channel_t *pchan);
+void free_fake_orcirc(or_circuit_t *orcirc);
+
+#endif /* TOR_FAKECIRCS_H */
diff --git a/src/test/include.am b/src/test/include.am
index d8e25dea9..294b9f309 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -99,6 +99,7 @@ if UNITTESTS_ENABLED
 
 # ADD_C_FILE: INSERT SOURCES HERE.
 src_test_test_SOURCES += \
+	src/test/fakecircs.c \
 	src/test/log_test_helpers.c \
 	src/test/hs_test_helpers.c \
 	src/test/rend_test_helpers.c \
@@ -339,6 +340,7 @@ src_test_test_timers_LDFLAGS = $(src_test_test_LDFLAGS)
 # ADD_C_FILE: INSERT HEADERS HERE.
 noinst_HEADERS+= \
 	src/test/fakechans.h \
+	src/test/fakecircs.h \
 	src/test/hs_test_helpers.h \
 	src/test/log_test_helpers.h \
 	src/test/rend_test_helpers.h \
diff --git a/src/test/test_circuitpadding.c b/src/test/test_circuitpadding.c
index 934ddb020..70e2081c5 100644
--- a/src/test/test_circuitpadding.c
+++ b/src/test/test_circuitpadding.c
@@ -38,6 +38,7 @@
 #include "core/or/or_circuit_st.h"
 #include "core/or/origin_circuit_st.h"
 
+#include "test/fakecircs.h"
 #include "test/rng_test_helpers.h"
 
 /* Start our monotime mocking at 1 second past whatever monotime_init()
@@ -53,7 +54,6 @@ circid_t get_unique_circ_id_by_chan(channel_t *chan);
 void helper_create_basic_machine(void);
 static void helper_create_conditional_machines(void);
 
-static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
 channel_t *new_fake_channel(void);
 void test_circuitpadding_negotiation(void *arg);
 void test_circuitpadding_wronghop(void *arg);
@@ -67,7 +67,6 @@ void test_circuitpadding_state_length(void *arg);
 static void
 simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
                            int padding);
-void free_fake_orcirc(circuit_t *circ);
 void free_fake_origin_circuit(origin_circuit_t *circ);
 
 static int deliver_negotiated = 1;
@@ -127,62 +126,6 @@ circuit_get_nth_node_mock(origin_circuit_t *circ, int hop)
   return &padding_node;
 }
 
-static or_circuit_t *
-new_fake_orcirc(channel_t *nchan, channel_t *pchan)
-{
-  or_circuit_t *orcirc = NULL;
-  circuit_t *circ = NULL;
-  crypt_path_t tmp_cpath;
-  char whatevs_key[CPATH_KEY_MATERIAL_LEN];
-
-  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);
-  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));
-
-  circuit_set_p_circid_chan(orcirc, orcirc->p_circ_id, pchan);
-  circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
-
-  memset(&tmp_cpath, 0, sizeof(tmp_cpath));
-  if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key,
-                                sizeof(whatevs_key), 0, 0)<0) {
-    log_warn(LD_BUG,"Circuit initialization failed");
-    return NULL;
-  }
-  orcirc->crypto = tmp_cpath.pvt_crypto;
-
-  return orcirc;
-}
-
-void
-free_fake_orcirc(circuit_t *circ)
-{
-  or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
-
-  relay_crypto_clear(&orcirc->crypto);
-
-  circpad_circuit_free_all_machineinfos(circ);
-  tor_free(circ);
-}
-
 void
 free_fake_origin_circuit(origin_circuit_t *circ)
 {
@@ -413,7 +356,7 @@ test_circuitpadding_rtt(void *arg)
             circpad_machine_current_state(
                 client_side->padding_info[0])->histogram_edges[0]);
  done:
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   timers_shutdown();
@@ -1439,7 +1382,7 @@ test_circuitpadding_wronghop(void *arg)
 
   /* Test 2: Test no padding */
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
 
   client_side = TO_CIRCUIT(origin_circuit_new());
   relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel,
@@ -1484,7 +1427,7 @@ test_circuitpadding_wronghop(void *arg)
 
  done:
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   monotime_disable_test_mocking();
@@ -1553,7 +1496,7 @@ test_circuitpadding_negotiation(void *arg)
 
   /* Test 2: Test no padding */
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
 
   client_side = TO_CIRCUIT(origin_circuit_new());
   relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
@@ -1591,7 +1534,7 @@ test_circuitpadding_negotiation(void *arg)
 
   /* 3. Test failure to negotiate a machine due to desync */
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
 
   client_side = TO_CIRCUIT(origin_circuit_new());
   relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
@@ -1619,7 +1562,7 @@ test_circuitpadding_negotiation(void *arg)
 
  done:
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   monotime_disable_test_mocking();
@@ -1939,7 +1882,7 @@ test_circuitpadding_state_length(void *arg)
   tor_free(client_machine);
 
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
 
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
@@ -2312,7 +2255,7 @@ test_circuitpadding_circuitsetup_machine(void *arg)
   tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
             OP_NE, 0);
   circuit_mark_for_close(client_side, END_CIRC_REASON_FLAG_REMOTE);
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   timers_advance_and_run(5000);
 
   /* No cells sent */
@@ -2616,7 +2559,7 @@ test_circuitpadding_global_rate_limiting(void *arg)
   tt_int_op(retval, OP_EQ, 0);
 
  done:
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
@@ -2769,7 +2712,7 @@ test_circuitpadding_reduce_disable(void *arg)
   tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
 
  done:
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   testing_disable_reproducible_rng();
@@ -3075,7 +3018,7 @@ helper_test_hs_machines(bool test_intro_circs)
   }
 
  done:
-  free_fake_orcirc(relay_side);
+  free_fake_orcirc(TO_OR_CIRCUIT(relay_side));
   circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
   circuitmux_free(dummy_channel.cmux);
   free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
diff --git a/src/test/test_relay.c b/src/test/test_relay.c
index 0b7a7be33..f7809b47e 100644
--- a/src/test/test_relay.c
+++ b/src/test/test_relay.c
@@ -21,42 +21,10 @@
 /* Test suite stuff */
 #include "test/test.h"
 #include "test/fakechans.h"
-
-static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
+#include "test/fakecircs.h"
 
 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;
-
-  circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan);
-  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;
-
-  circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan);
-  cell_queue_init(&(orcirc->p_chan_cells));
-
-  return orcirc;
-}
-
 static void
 assert_circuit_ok_mock(const circuit_t *c)
 {
@@ -145,7 +113,7 @@ test_relay_close_circuit(void *arg)
     cell_queue_clear(&orcirc->base_.n_chan_cells);
     cell_queue_clear(&orcirc->p_chan_cells);
   }
-  tor_free(orcirc);
+  free_fake_orcirc(orcirc);
   free_fake_channel(nchan);
   free_fake_channel(pchan);
   UNMOCK(assert_circuit_ok);
@@ -218,7 +186,7 @@ test_relay_append_cell_to_circuit_queue(void *arg)
     cell_queue_clear(&orcirc->base_.n_chan_cells);
     cell_queue_clear(&orcirc->p_chan_cells);
   }
-  tor_free(orcirc);
+  free_fake_orcirc(orcirc);
   free_fake_channel(nchan);
   free_fake_channel(pchan);
 





More information about the tor-commits mailing list