test_hs_cell.c 3.3 KB

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