Browse Source

Unit tests for test_routerkeys_write_fingerprint

Nick Mathewson 10 years ago
parent
commit
886d4be149
5 changed files with 89 additions and 1 deletions
  1. 1 1
      src/or/router.c
  2. 1 0
      src/or/router.h
  3. 1 0
      src/test/include.am
  4. 2 0
      src/test/test.c
  5. 84 0
      src/test/test_routerkeys.c

+ 1 - 1
src/or/router.c

@@ -688,7 +688,7 @@ router_initialize_tls_context(void)
  * it to 'fingerprint' (or 'hashed-fingerprint'). Return 0 on success, or
  * -1 if Tor should die,
  */
-static int
+STATIC int
 router_write_fingerprint(int hashed)
 {
   char *keydir = NULL, *cp = NULL;

+ 1 - 0
src/or/router.h

@@ -147,6 +147,7 @@ smartlist_t *router_get_all_orports(const routerinfo_t *ri);
 #ifdef ROUTER_PRIVATE
 /* Used only by router.c and test.c */
 STATIC void get_platform_str(char *platform, size_t len);
+STATIC int router_write_fingerprint(int hashed);
 #endif
 
 #endif

+ 1 - 0
src/test/include.am

@@ -35,6 +35,7 @@ src_test_test_SOURCES = \
 	src/test/test_options.c \
 	src/test/test_pt.c \
 	src/test/test_replay.c \
+	src/test/test_routerkeys.c \
 	src/test/test_socks.c \
 	src/test/test_util.c \
 	src/test/test_config.c \

+ 2 - 0
src/test/test.c

@@ -1629,6 +1629,7 @@ extern struct testcase_t logging_tests[];
 extern struct testcase_t backtrace_tests[];
 extern struct testcase_t hs_tests[];
 extern struct testcase_t nodelist_tests[];
+extern struct testcase_t routerkeys_tests[];
 
 static struct testgroup_t testgroups[] = {
   { "", test_array },
@@ -1654,6 +1655,7 @@ static struct testgroup_t testgroups[] = {
   { "control/", controller_event_tests },
   { "hs/", hs_tests },
   { "nodelist/", nodelist_tests },
+  { "routerkeys/", routerkeys_tests },
   END_OF_GROUPS
 };
 

+ 84 - 0
src/test/test_routerkeys.c

@@ -0,0 +1,84 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#define ROUTER_PRIVATE
+#include "or.h"
+#include "config.h"
+#include "router.h"
+#include "util.h"
+#include "crypto.h"
+
+#include "test.h"
+
+static void
+test_routerkeys_write_fingerprint(void *arg)
+{
+  crypto_pk_t *key = pk_generate(2);
+  or_options_t *options = get_options_mutable();
+  const char *ddir = get_fname("write_fingerprint");
+  char *cp = NULL, *cp2 = NULL;
+  char fp[FINGERPRINT_LEN+1];
+
+  (void)arg;
+
+  tt_assert(key);
+
+  options->ORPort_set = 1; /* So that we can get the server ID key */
+  options->DataDirectory = tor_strdup(ddir);
+  options->Nickname = tor_strdup("haflinger");
+  set_server_identity_key(key);
+  set_client_identity_key(crypto_pk_dup_key(key));
+
+  check_private_dir(ddir, CPD_CREATE, NULL);
+  tt_int_op(crypto_pk_cmp_keys(get_server_identity_key(),key),==,0);
+
+  /* Write fingerprint file */
+  tt_int_op(0, ==, router_write_fingerprint(0));
+  cp = read_file_to_str(get_fname("write_fingerprint/fingerprint"),
+                        0, NULL);
+  crypto_pk_get_fingerprint(key, fp, 0);
+  tor_asprintf(&cp2, "haflinger %s\n", fp);
+  tt_str_op(cp, ==, cp2);
+  tor_free(cp);
+  tor_free(cp2);
+
+  /* Write hashed-fingerprint file */
+  tt_int_op(0, ==, router_write_fingerprint(1));
+  cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
+                        0, NULL);
+  crypto_pk_get_hashed_fingerprint(key, fp);
+  tor_asprintf(&cp2, "haflinger %s\n", fp);
+  tt_str_op(cp, ==, cp2);
+  tor_free(cp);
+  tor_free(cp2);
+
+  /* Replace outdated file */
+  write_str_to_file(get_fname("write_fingerprint/hashed-fingerprint"),
+                    "junk goes here", 0);
+  tt_int_op(0, ==, router_write_fingerprint(1));
+  cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
+                        0, NULL);
+  crypto_pk_get_hashed_fingerprint(key, fp);
+  tor_asprintf(&cp2, "haflinger %s\n", fp);
+  tt_str_op(cp, ==, cp2);
+  tor_free(cp);
+  tor_free(cp2);
+
+ done:
+  crypto_pk_free(key);
+  set_client_identity_key(NULL);
+  tor_free(cp);
+  tor_free(cp2);
+}
+
+#define TEST(name, flags)                                       \
+  { #name , test_routerkeys_ ## name, (flags), NULL, NULL }
+
+struct testcase_t routerkeys_tests[] = {
+  TEST(write_fingerprint, TT_FORK),
+  END_OF_TESTCASES
+};
+