rend_test_helpers.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "crypto_rand.h"
  5. #include "test.h"
  6. #include "rendcommon.h"
  7. #include "rend_test_helpers.h"
  8. #include "rend_intro_point_st.h"
  9. #include "rend_service_descriptor_st.h"
  10. void
  11. generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
  12. char **service_id, int intro_points)
  13. {
  14. rend_service_descriptor_t *generated = NULL;
  15. smartlist_t *descs = smartlist_new();
  16. time_t now;
  17. now = time(NULL) + time_diff;
  18. create_descriptor(&generated, service_id, intro_points);
  19. generated->timestamp = now;
  20. rend_encode_v2_descriptors(descs, generated, now, 0, REND_NO_AUTH, NULL,
  21. NULL);
  22. tor_assert(smartlist_len(descs) > 1);
  23. *desc = smartlist_get(descs, 0);
  24. smartlist_set(descs, 0, NULL);
  25. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  26. rend_encoded_v2_service_descriptor_free(d));
  27. smartlist_free(descs);
  28. rend_service_descriptor_free(generated);
  29. }
  30. void
  31. create_descriptor(rend_service_descriptor_t **generated, char **service_id,
  32. int intro_points)
  33. {
  34. crypto_pk_t *pk1 = NULL;
  35. crypto_pk_t *pk2 = NULL;
  36. int i;
  37. *service_id = tor_malloc(REND_SERVICE_ID_LEN_BASE32+1);
  38. pk1 = pk_generate(0);
  39. pk2 = pk_generate(1);
  40. *generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  41. (*generated)->pk = crypto_pk_dup_key(pk1);
  42. rend_get_service_id((*generated)->pk, *service_id);
  43. (*generated)->version = 2;
  44. (*generated)->protocols = 42;
  45. (*generated)->intro_nodes = smartlist_new();
  46. for (i = 0; i < intro_points; i++) {
  47. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  48. crypto_pk_t *okey = pk_generate(2 + i);
  49. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  50. intro->extend_info->onion_key = okey;
  51. crypto_pk_get_digest(intro->extend_info->onion_key,
  52. intro->extend_info->identity_digest);
  53. intro->extend_info->nickname[0] = '$';
  54. base16_encode(intro->extend_info->nickname + 1,
  55. sizeof(intro->extend_info->nickname) - 1,
  56. intro->extend_info->identity_digest, DIGEST_LEN);
  57. tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
  58. intro->extend_info->port = 1 + crypto_rand_int(65535);
  59. intro->intro_key = crypto_pk_dup_key(pk2);
  60. smartlist_add((*generated)->intro_nodes, intro);
  61. }
  62. crypto_pk_free(pk1);
  63. crypto_pk_free(pk2);
  64. }
  65. rend_data_t *
  66. mock_rend_data(const char *onion_address)
  67. {
  68. rend_data_v2_t *v2_data = tor_malloc_zero(sizeof(*v2_data));
  69. rend_data_t *rend_query = &v2_data->base_;
  70. rend_query->version = 2;
  71. strlcpy(v2_data->onion_address, onion_address,
  72. sizeof(v2_data->onion_address));
  73. v2_data->auth_type = REND_NO_AUTH;
  74. rend_query->hsdirs_fp = smartlist_new();
  75. smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
  76. DIGEST_LEN));
  77. return rend_query;
  78. }