[tor-commits] [tor/master] test: Add test_hs_cell unit tests

nickm at torproject.org nickm at torproject.org
Wed Aug 9 00:36:37 UTC 2017


commit 472835d6e94134d7e30e390c4e294bd3a3af5eaa
Author: David Goulet <dgoulet at torproject.org>
Date:   Thu Apr 20 09:58:21 2017 -0400

    test: Add test_hs_cell unit tests
    
    Move ESTABLISH_INTRO tests from test_hs_service.c to this new file.
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/test/include.am        |   1 +
 src/test/test.c            |   1 +
 src/test/test.h            |   1 +
 src/test/test_hs_cell.c    | 114 +++++++++++++++++++++++++++++++++++++++++++++
 src/test/test_hs_service.c |  87 ----------------------------------
 5 files changed, 117 insertions(+), 87 deletions(-)

diff --git a/src/test/include.am b/src/test/include.am
index 2e448c8b3..53c723df8 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -115,6 +115,7 @@ src_test_test_SOURCES = \
 	src/test/test_extorport.c \
 	src/test/test_hs.c \
 	src/test/test_hs_config.c \
+	src/test/test_hs_cell.c \
 	src/test/test_hs_service.c \
 	src/test/test_hs_client.c  \
 	src/test/test_hs_intropoint.c \
diff --git a/src/test/test.c b/src/test/test.c
index c5c394900..2a2d5ba64 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1214,6 +1214,7 @@ struct testgroup_t testgroups[] = {
   { "extorport/", extorport_tests },
   { "legacy_hs/", hs_tests },
   { "hs_cache/", hs_cache },
+  { "hs_cell/", hs_cell_tests },
   { "hs_config/", hs_config_tests },
   { "hs_descriptor/", hs_descriptor },
   { "hs_service/", hs_service_tests },
diff --git a/src/test/test.h b/src/test/test.h
index 9b2a0b842..b30301d06 100644
--- a/src/test/test.h
+++ b/src/test/test.h
@@ -207,6 +207,7 @@ extern struct testcase_t guardfraction_tests[];
 extern struct testcase_t extorport_tests[];
 extern struct testcase_t hs_tests[];
 extern struct testcase_t hs_cache[];
+extern struct testcase_t hs_cell_tests[];
 extern struct testcase_t hs_config_tests[];
 extern struct testcase_t hs_descriptor[];
 extern struct testcase_t hs_service_tests[];
diff --git a/src/test/test_hs_cell.c b/src/test/test_hs_cell.c
new file mode 100644
index 000000000..0bb8c8735
--- /dev/null
+++ b/src/test/test_hs_cell.c
@@ -0,0 +1,114 @@
+/* Copyright (c) 2017, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file test_hs_cell.c
+ * \brief Test hidden service cell functionality.
+ */
+
+#define HS_INTROPOINT_PRIVATE
+#define HS_SERVICE_PRIVATE
+
+#include "test.h"
+#include "test_helpers.h"
+#include "log_test_helpers.h"
+
+#include "crypto_ed25519.h"
+#include "hs_intropoint.h"
+#include "hs_service.h"
+
+/* Trunnel. */
+#include "hs/cell_establish_intro.h"
+
+/** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
+ *  parse it from the receiver side. */
+static void
+test_gen_establish_intro_cell(void *arg)
+{
+  (void) arg;
+  ssize_t retval;
+  uint8_t circuit_key_material[DIGEST_LEN] = {0};
+  uint8_t buf[RELAY_PAYLOAD_SIZE];
+  trn_cell_establish_intro_t *cell_out = NULL;
+  trn_cell_establish_intro_t *cell_in = NULL;
+
+  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
+
+  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
+     attempt to parse it. */
+  {
+    cell_out = generate_establish_intro_cell(circuit_key_material,
+                                             sizeof(circuit_key_material));
+    tt_assert(cell_out);
+
+    retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
+    tt_int_op(retval, >=, 0);
+  }
+
+  /* Parse it as the receiver */
+  {
+    ssize_t parse_result = trn_cell_establish_intro_parse(&cell_in,
+                                                         buf, sizeof(buf));
+    tt_int_op(parse_result, >=, 0);
+
+    retval = verify_establish_intro_cell(cell_in,
+                                         circuit_key_material,
+                                         sizeof(circuit_key_material));
+    tt_int_op(retval, >=, 0);
+  }
+
+ done:
+  trn_cell_establish_intro_free(cell_out);
+  trn_cell_establish_intro_free(cell_in);
+}
+
+/* Mocked ed25519_sign_prefixed() function that always fails :) */
+static int
+mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
+                           const uint8_t *msg, size_t msg_len,
+                           const char *prefix_str,
+                           const ed25519_keypair_t *keypair) {
+  (void) signature_out;
+  (void) msg;
+  (void) msg_len;
+  (void) prefix_str;
+  (void) keypair;
+  return -1;
+}
+
+/** We simulate a failure to create an ESTABLISH_INTRO cell */
+static void
+test_gen_establish_intro_cell_bad(void *arg)
+{
+  (void) arg;
+  trn_cell_establish_intro_t *cell = NULL;
+  uint8_t circuit_key_material[DIGEST_LEN] = {0};
+
+  MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
+
+  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
+
+  setup_full_capture_of_logs(LOG_WARN);
+  /* Easiest way to make that function fail is to mock the
+     ed25519_sign_prefixed() function and make it fail. */
+  cell = generate_establish_intro_cell(circuit_key_material,
+                                       sizeof(circuit_key_material));
+  expect_log_msg_containing("Unable to gen signature for "
+                            "ESTABLISH_INTRO cell.");
+  teardown_capture_of_logs();
+  tt_assert(!cell);
+
+ done:
+  trn_cell_establish_intro_free(cell);
+  UNMOCK(ed25519_sign_prefixed);
+}
+
+struct testcase_t hs_cell_tests[] = {
+  { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
+    NULL, NULL },
+  { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
+    NULL, NULL },
+
+  END_OF_TESTCASES
+};
+
diff --git a/src/test/test_hs_service.c b/src/test/test_hs_service.c
index fe4ce4233..277855f75 100644
--- a/src/test/test_hs_service.c
+++ b/src/test/test_hs_service.c
@@ -59,89 +59,6 @@ helper_config_service(const char *conf)
   return ret;
 }
 
-/** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
- *  parse it from the receiver side. */
-static void
-test_gen_establish_intro_cell(void *arg)
-{
-  (void) arg;
-  ssize_t retval;
-  uint8_t circuit_key_material[DIGEST_LEN] = {0};
-  uint8_t buf[RELAY_PAYLOAD_SIZE];
-  trn_cell_establish_intro_t *cell_out = NULL;
-  trn_cell_establish_intro_t *cell_in = NULL;
-
-  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
-
-  /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
-     attempt to parse it. */
-  {
-    cell_out = generate_establish_intro_cell(circuit_key_material,
-                                             sizeof(circuit_key_material));
-    tt_assert(cell_out);
-
-    retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
-    tt_int_op(retval, >=, 0);
-  }
-
-  /* Parse it as the receiver */
-  {
-    ssize_t parse_result = trn_cell_establish_intro_parse(&cell_in,
-                                                         buf, sizeof(buf));
-    tt_int_op(parse_result, >=, 0);
-
-    retval = verify_establish_intro_cell(cell_in,
-                                         circuit_key_material,
-                                         sizeof(circuit_key_material));
-    tt_int_op(retval, >=, 0);
-  }
-
- done:
-  trn_cell_establish_intro_free(cell_out);
-  trn_cell_establish_intro_free(cell_in);
-}
-
-/* Mocked ed25519_sign_prefixed() function that always fails :) */
-static int
-mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
-                           const uint8_t *msg, size_t msg_len,
-                           const char *prefix_str,
-                           const ed25519_keypair_t *keypair) {
-  (void) signature_out;
-  (void) msg;
-  (void) msg_len;
-  (void) prefix_str;
-  (void) keypair;
-  return -1;
-}
-
-/** We simulate a failure to create an ESTABLISH_INTRO cell */
-static void
-test_gen_establish_intro_cell_bad(void *arg)
-{
-  (void) arg;
-  trn_cell_establish_intro_t *cell = NULL;
-  uint8_t circuit_key_material[DIGEST_LEN] = {0};
-
-  MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
-
-  crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
-
-  setup_full_capture_of_logs(LOG_WARN);
-  /* Easiest way to make that function fail is to mock the
-     ed25519_sign_prefixed() function and make it fail. */
-  cell = generate_establish_intro_cell(circuit_key_material,
-                                       sizeof(circuit_key_material));
-  expect_log_msg_containing("Unable to gen signature for "
-                            "ESTABLISH_INTRO cell.");
-  teardown_capture_of_logs();
-  tt_assert(!cell);
-
- done:
-  trn_cell_establish_intro_free(cell);
-  UNMOCK(ed25519_sign_prefixed);
-}
-
 /** Test the HS ntor handshake. Simulate the sending of an encrypted INTRODUCE1
  *  cell, and verify the proper derivation of decryption keys on the other end.
  *  Then simulate the sending of an authenticated RENDEZVOUS1 cell and verify
@@ -601,10 +518,6 @@ test_desc_overlap_period(void *arg)
 }
 
 struct testcase_t hs_service_tests[] = {
-  { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
-    NULL, NULL },
-  { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
-    NULL, NULL },
   { "hs_ntor", test_hs_ntor, TT_FORK,
     NULL, NULL },
   { "time_period", test_time_period, TT_FORK,





More information about the tor-commits mailing list