Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / rend_test_helpers.c
blob8e40167aebf57515d4cc699b1b430d7ad0f404c0
1 /* Copyright (c) 2014-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
5 #include "core/or/extendinfo.h"
6 #include "lib/crypt_ops/crypto_rand.h"
7 #include "test/test.h"
8 #include "feature/rend/rendcommon.h"
9 #include "test/rend_test_helpers.h"
11 #include "core/or/extend_info_st.h"
12 #include "feature/rend/rend_intro_point_st.h"
13 #include "feature/rend/rend_service_descriptor_st.h"
15 void
16 generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
17 char **service_id, int intro_points)
19 rend_service_descriptor_t *generated = NULL;
20 smartlist_t *descs = smartlist_new();
21 time_t now;
23 now = time(NULL) + time_diff;
24 create_descriptor(&generated, service_id, intro_points);
25 generated->timestamp = now;
27 rend_encode_v2_descriptors(descs, generated, now, 0, REND_NO_AUTH, NULL,
28 NULL);
29 tor_assert(smartlist_len(descs) > 1);
30 *desc = smartlist_get(descs, 0);
31 smartlist_set(descs, 0, NULL);
33 SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
34 rend_encoded_v2_service_descriptor_free(d));
35 smartlist_free(descs);
36 rend_service_descriptor_free(generated);
39 void
40 create_descriptor(rend_service_descriptor_t **generated, char **service_id,
41 int intro_points)
43 crypto_pk_t *pk1 = NULL;
44 crypto_pk_t *pk2 = NULL;
45 int i;
47 *service_id = tor_malloc(REND_SERVICE_ID_LEN_BASE32+1);
48 pk1 = pk_generate(0);
49 pk2 = pk_generate(1);
51 *generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
52 (*generated)->pk = crypto_pk_dup_key(pk1);
53 rend_get_service_id((*generated)->pk, *service_id);
55 (*generated)->version = 2;
56 (*generated)->protocols = 42;
57 (*generated)->intro_nodes = smartlist_new();
59 for (i = 0; i < intro_points; i++) {
60 rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
61 crypto_pk_t *okey = pk_generate(2 + i);
62 intro->extend_info =
63 extend_info_new(NULL, NULL, NULL, NULL, NULL, NULL, 0);
64 intro->extend_info->onion_key = okey;
65 crypto_pk_get_digest(intro->extend_info->onion_key,
66 intro->extend_info->identity_digest);
67 intro->extend_info->nickname[0] = '$';
68 base16_encode(intro->extend_info->nickname + 1,
69 sizeof(intro->extend_info->nickname) - 1,
70 intro->extend_info->identity_digest, DIGEST_LEN);
71 tor_addr_t addr;
72 uint16_t port;
73 /* Does not cover all IP addresses. */
74 tor_addr_from_ipv4h(&addr, crypto_rand_int(65536) + 1);
75 port = 1 + crypto_rand_int(65535);
76 extend_info_add_orport(intro->extend_info, &addr, port);
77 intro->intro_key = crypto_pk_dup_key(pk2);
78 smartlist_add((*generated)->intro_nodes, intro);
81 crypto_pk_free(pk1);
82 crypto_pk_free(pk2);
85 rend_data_t *
86 mock_rend_data(const char *onion_address)
88 rend_data_v2_t *v2_data = tor_malloc_zero(sizeof(*v2_data));
89 rend_data_t *rend_query = &v2_data->base_;
90 rend_query->version = 2;
92 strlcpy(v2_data->onion_address, onion_address,
93 sizeof(v2_data->onion_address));
94 v2_data->auth_type = REND_NO_AUTH;
95 rend_query->hsdirs_fp = smartlist_new();
96 smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
97 DIGEST_LEN));
98 return rend_query;