test_routerkeys.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define ROUTER_PRIVATE
  7. #include "or.h"
  8. #include "config.h"
  9. #include "router.h"
  10. #include "util.h"
  11. #include "crypto.h"
  12. #include "test.h"
  13. static void
  14. test_routerkeys_write_fingerprint(void *arg)
  15. {
  16. crypto_pk_t *key = pk_generate(2);
  17. or_options_t *options = get_options_mutable();
  18. const char *ddir = get_fname("write_fingerprint");
  19. char *cp = NULL, *cp2 = NULL;
  20. char fp[FINGERPRINT_LEN+1];
  21. (void)arg;
  22. tt_assert(key);
  23. options->ORPort_set = 1; /* So that we can get the server ID key */
  24. tor_free(options->DataDirectory);
  25. options->DataDirectory = tor_strdup(ddir);
  26. options->Nickname = tor_strdup("haflinger");
  27. set_server_identity_key(key);
  28. set_client_identity_key(crypto_pk_dup_key(key));
  29. tt_int_op(0, OP_EQ, check_private_dir(ddir, CPD_CREATE, NULL));
  30. tt_int_op(crypto_pk_cmp_keys(get_server_identity_key(),key),OP_EQ,0);
  31. /* Write fingerprint file */
  32. tt_int_op(0, OP_EQ, router_write_fingerprint(0));
  33. cp = read_file_to_str(get_fname("write_fingerprint/fingerprint"),
  34. 0, NULL);
  35. crypto_pk_get_fingerprint(key, fp, 0);
  36. tor_asprintf(&cp2, "haflinger %s\n", fp);
  37. tt_str_op(cp, OP_EQ, cp2);
  38. tor_free(cp);
  39. tor_free(cp2);
  40. /* Write hashed-fingerprint file */
  41. tt_int_op(0, OP_EQ, router_write_fingerprint(1));
  42. cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
  43. 0, NULL);
  44. crypto_pk_get_hashed_fingerprint(key, fp);
  45. tor_asprintf(&cp2, "haflinger %s\n", fp);
  46. tt_str_op(cp, OP_EQ, cp2);
  47. tor_free(cp);
  48. tor_free(cp2);
  49. /* Replace outdated file */
  50. write_str_to_file(get_fname("write_fingerprint/hashed-fingerprint"),
  51. "junk goes here", 0);
  52. tt_int_op(0, OP_EQ, router_write_fingerprint(1));
  53. cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"),
  54. 0, NULL);
  55. crypto_pk_get_hashed_fingerprint(key, fp);
  56. tor_asprintf(&cp2, "haflinger %s\n", fp);
  57. tt_str_op(cp, OP_EQ, cp2);
  58. tor_free(cp);
  59. tor_free(cp2);
  60. done:
  61. crypto_pk_free(key);
  62. set_client_identity_key(NULL);
  63. tor_free(cp);
  64. tor_free(cp2);
  65. }
  66. #define TEST(name, flags) \
  67. { #name , test_routerkeys_ ## name, (flags), NULL, NULL }
  68. struct testcase_t routerkeys_tests[] = {
  69. TEST(write_fingerprint, TT_FORK),
  70. END_OF_TESTCASES
  71. };