[tor-commits] [tor/master] test: Series of fixes for hs_dos.c unit tests

nickm at torproject.org nickm at torproject.org
Wed Aug 7 13:52:13 UTC 2019


commit 90b5422e8ee9dd7c7d04507291668e5870f2e351
Author: David Goulet <dgoulet at torproject.org>
Date:   Thu Jun 27 14:06:42 2019 -0400

    test: Series of fixes for hs_dos.c unit tests
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/test/test_hs_dos.c        | 28 +++++++++++++++++++
 src/test/test_hs_intropoint.c | 63 ++++++++++++++++++++++---------------------
 2 files changed, 61 insertions(+), 30 deletions(-)

diff --git a/src/test/test_hs_dos.c b/src/test/test_hs_dos.c
index 6b8261053..3dfa057a4 100644
--- a/src/test/test_hs_dos.c
+++ b/src/test/test_hs_dos.c
@@ -7,16 +7,37 @@
  */
 
 #define CIRCUITLIST_PRIVATE
+#define NETWORKSTATUS_PRIVATE
 
 #include "test/test.h"
 #include "test/test_helpers.h"
 #include "test/log_test_helpers.h"
 
+#include "app/config/config.h"
+
 #include "core/or/circuitlist.h"
 #include "core/or/circuituse.h"
 #include "core/or/or_circuit_st.h"
 
 #include "feature/hs/hs_dos.h"
+#include "feature/nodelist/networkstatus.h"
+
+static void
+setup_mock_consensus(void)
+{
+  current_ns_consensus = tor_malloc_zero(sizeof(networkstatus_t));
+  current_ns_consensus->net_params = smartlist_new();
+  smartlist_add(current_ns_consensus->net_params,
+                (void *) "HiddenServiceEnableIntroDoSDefense=1");
+  hs_dos_consensus_has_changed(current_ns_consensus);
+}
+
+static void
+free_mock_consensus(void)
+{
+  smartlist_free(current_ns_consensus->net_params);
+  tor_free(current_ns_consensus);
+}
 
 static void
 test_can_send_intro2(void *arg)
@@ -26,8 +47,12 @@ test_can_send_intro2(void *arg)
 
   (void) arg;
 
+  hs_init();
   hs_dos_init();
 
+  get_options_mutable()->ORPort_set = 1;
+  setup_mock_consensus();
+
   or_circ =  or_circuit_new(1, NULL);
 
   /* Make that circuit a service intro point. */
@@ -95,6 +120,9 @@ test_can_send_intro2(void *arg)
 
  done:
   circuit_free_(TO_CIRCUIT(or_circ));
+
+  hs_free_all();
+  free_mock_consensus();
 }
 
 struct testcase_t hs_dos_tests[] = {
diff --git a/src/test/test_hs_intropoint.c b/src/test/test_hs_intropoint.c
index 1d472397c..7b01809f9 100644
--- a/src/test/test_hs_intropoint.c
+++ b/src/test/test_hs_intropoint.c
@@ -186,8 +186,6 @@ test_establish_intro_wrong_purpose(void *arg)
 
   (void)arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   memcpy(intro_circ->rend_circ_nonce, circ_nonce, DIGEST_LEN);
@@ -230,8 +228,6 @@ test_establish_intro_wrong_keytype(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -259,8 +255,6 @@ test_establish_intro_wrong_keytype2(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -297,8 +291,6 @@ test_establish_intro_wrong_mac(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -371,8 +363,6 @@ test_establish_intro_wrong_auth_key_len(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -418,8 +408,6 @@ test_establish_intro_wrong_sig_len(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -463,8 +451,6 @@ test_establish_intro_wrong_sig(void *arg)
 
   (void) arg;
 
-  hs_dos_init();
-
   /* Get the auth key of the intro point */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -502,8 +488,6 @@ helper_establish_intro_v3(or_circuit_t *intro_circ)
 
   tt_assert(intro_circ);
 
-  hs_dos_init();
-
   /* Prepare the circuit for the incoming ESTABLISH_INTRO */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -539,8 +523,6 @@ helper_establish_intro_v2(or_circuit_t *intro_circ)
 
   tt_assert(intro_circ);
 
-  hs_dos_init();
-
   /* Prepare the circuit for the incoming ESTABLISH_INTRO */
   crypto_rand(circ_nonce, sizeof(circ_nonce));
   helper_prepare_circ_for_intro(intro_circ, circ_nonce);
@@ -921,42 +903,63 @@ test_received_introduce1_handling(void *arg)
   UNMOCK(relay_send_command_from_edge_);
 }
 
+static void *
+hs_subsystem_setup_fn(const struct testcase_t *tc)
+{
+  (void) tc;
+
+  return NULL;
+}
+
+static int
+hs_subsystem_cleanup_fn(const struct testcase_t *tc, void *arg)
+{
+  (void) tc;
+  (void) arg;
+
+  return 1;
+}
+
+static struct testcase_setup_t test_setup = {
+  hs_subsystem_setup_fn, hs_subsystem_cleanup_fn
+};
+
 struct testcase_t hs_intropoint_tests[] = {
   { "intro_point_registration",
-    test_intro_point_registration, TT_FORK, NULL, NULL },
+    test_intro_point_registration, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_keytype",
-    test_establish_intro_wrong_keytype, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_keytype, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_keytype2",
-    test_establish_intro_wrong_keytype2, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_keytype2, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_purpose",
-    test_establish_intro_wrong_purpose, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_purpose, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_sig",
-    test_establish_intro_wrong_sig, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_sig, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_sig_len",
-    test_establish_intro_wrong_sig_len, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_sig_len, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_auth_key_len",
-    test_establish_intro_wrong_auth_key_len, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_auth_key_len, TT_FORK, NULL, &test_setup},
 
   { "receive_establish_intro_wrong_mac",
-    test_establish_intro_wrong_mac, TT_FORK, NULL, NULL },
+    test_establish_intro_wrong_mac, TT_FORK, NULL, &test_setup},
 
   { "introduce1_suitable_circuit",
-    test_introduce1_suitable_circuit, TT_FORK, NULL, NULL },
+    test_introduce1_suitable_circuit, TT_FORK, NULL, &test_setup},
 
   { "introduce1_is_legacy",
-    test_introduce1_is_legacy, TT_FORK, NULL, NULL },
+    test_introduce1_is_legacy, TT_FORK, NULL, &test_setup},
 
   { "introduce1_validation",
-    test_introduce1_validation, TT_FORK, NULL, NULL },
+    test_introduce1_validation, TT_FORK, NULL, &test_setup},
 
   { "received_introduce1_handling",
-    test_received_introduce1_handling, TT_FORK, NULL, NULL },
+    test_received_introduce1_handling, TT_FORK, NULL, &test_setup},
 
   END_OF_TESTCASES
 };





More information about the tor-commits mailing list