test_hs_service.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define HS_SERVICE_PRIVATE
  8. #define HS_INTROPOINT_PRIVATE
  9. #include "test.h"
  10. #include "log_test_helpers.h"
  11. #include "crypto.h"
  12. #include "hs/cell_establish_intro.h"
  13. #include "hs_service.h"
  14. #include "hs_intropoint.h"
  15. /** We simulate the creation of an outgoing ESTABLISH_INTRO cell, and then we
  16. * parse it from the receiver side. */
  17. static void
  18. test_gen_establish_intro_cell(void *arg)
  19. {
  20. (void) arg;
  21. ssize_t retval;
  22. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  23. uint8_t buf[RELAY_PAYLOAD_SIZE];
  24. trn_cell_establish_intro_t *cell_out = NULL;
  25. trn_cell_establish_intro_t *cell_in = NULL;
  26. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  27. /* Create outgoing ESTABLISH_INTRO cell and extract its payload so that we
  28. attempt to parse it. */
  29. {
  30. cell_out = generate_establish_intro_cell(circuit_key_material,
  31. sizeof(circuit_key_material));
  32. tt_assert(cell_out);
  33. retval = get_establish_intro_payload(buf, sizeof(buf), cell_out);
  34. tt_int_op(retval, >=, 0);
  35. }
  36. /* Parse it as the receiver */
  37. {
  38. ssize_t parse_result = trn_cell_establish_intro_parse(&cell_in,
  39. buf, sizeof(buf));
  40. tt_int_op(parse_result, >=, 0);
  41. retval = verify_establish_intro_cell(cell_in,
  42. circuit_key_material,
  43. sizeof(circuit_key_material));
  44. tt_int_op(retval, >=, 0);
  45. }
  46. done:
  47. trn_cell_establish_intro_free(cell_out);
  48. trn_cell_establish_intro_free(cell_in);
  49. }
  50. /* Mocked ed25519_sign_prefixed() function that always fails :) */
  51. static int
  52. mock_ed25519_sign_prefixed(ed25519_signature_t *signature_out,
  53. const uint8_t *msg, size_t msg_len,
  54. const char *prefix_str,
  55. const ed25519_keypair_t *keypair) {
  56. (void) signature_out;
  57. (void) msg;
  58. (void) msg_len;
  59. (void) prefix_str;
  60. (void) keypair;
  61. return -1;
  62. }
  63. /** We simulate a failure to create an ESTABLISH_INTRO cell */
  64. static void
  65. test_gen_establish_intro_cell_bad(void *arg)
  66. {
  67. (void) arg;
  68. trn_cell_establish_intro_t *cell = NULL;
  69. uint8_t circuit_key_material[DIGEST_LEN] = {0};
  70. MOCK(ed25519_sign_prefixed, mock_ed25519_sign_prefixed);
  71. crypto_rand((char *) circuit_key_material, sizeof(circuit_key_material));
  72. setup_full_capture_of_logs(LOG_WARN);
  73. /* Easiest way to make that function fail is to mock the
  74. ed25519_sign_prefixed() function and make it fail. */
  75. cell = generate_establish_intro_cell(circuit_key_material,
  76. sizeof(circuit_key_material));
  77. expect_log_msg_containing("Unable to gen signature for "
  78. "ESTABLISH_INTRO cell.");
  79. teardown_capture_of_logs();
  80. tt_assert(!cell);
  81. done:
  82. trn_cell_establish_intro_free(cell);
  83. UNMOCK(ed25519_sign_prefixed);
  84. }
  85. struct testcase_t hs_service_tests[] = {
  86. { "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
  87. NULL, NULL },
  88. { "gen_establish_intro_cell_bad", test_gen_establish_intro_cell_bad, TT_FORK,
  89. NULL, NULL },
  90. END_OF_TESTCASES
  91. };