rend_test_helpers.c 2.9 KB

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