rend_test_helpers.c 2.4 KB

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