fix typos from #28614
[tor.git] / src / test / rend_test_helpers.c
blobf12d193cc5d8be47fac3ff2356dd428c16cd3f39
1 /* Copyright (c) 2014-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
5 #include "lib/crypt_ops/crypto_rand.h"
6 #include "test/test.h"
7 #include "feature/rend/rendcommon.h"
8 #include "test/rend_test_helpers.h"
10 #include "core/or/extend_info_st.h"
11 #include "feature/rend/rend_intro_point_st.h"
12 #include "feature/rend/rend_service_descriptor_st.h"
14 void
15 generate_desc(int time_diff, rend_encoded_v2_service_descriptor_t **desc,
16 char **service_id, int intro_points)
18 rend_service_descriptor_t *generated = NULL;
19 smartlist_t *descs = smartlist_new();
20 time_t now;
22 now = time(NULL) + time_diff;
23 create_descriptor(&generated, service_id, intro_points);
24 generated->timestamp = now;
26 rend_encode_v2_descriptors(descs, generated, now, 0, REND_NO_AUTH, NULL,
27 NULL);
28 tor_assert(smartlist_len(descs) > 1);
29 *desc = smartlist_get(descs, 0);
30 smartlist_set(descs, 0, NULL);
32 SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
33 rend_encoded_v2_service_descriptor_free(d));
34 smartlist_free(descs);
35 rend_service_descriptor_free(generated);
38 void
39 create_descriptor(rend_service_descriptor_t **generated, char **service_id,
40 int intro_points)
42 crypto_pk_t *pk1 = NULL;
43 crypto_pk_t *pk2 = NULL;
44 int i;
46 *service_id = tor_malloc(REND_SERVICE_ID_LEN_BASE32+1);
47 pk1 = pk_generate(0);
48 pk2 = pk_generate(1);
50 *generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
51 (*generated)->pk = crypto_pk_dup_key(pk1);
52 rend_get_service_id((*generated)->pk, *service_id);
54 (*generated)->version = 2;
55 (*generated)->protocols = 42;
56 (*generated)->intro_nodes = smartlist_new();
58 for (i = 0; i < intro_points; i++) {
59 rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
60 crypto_pk_t *okey = pk_generate(2 + i);
61 intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
62 intro->extend_info->onion_key = okey;
63 crypto_pk_get_digest(intro->extend_info->onion_key,
64 intro->extend_info->identity_digest);
65 intro->extend_info->nickname[0] = '$';
66 base16_encode(intro->extend_info->nickname + 1,
67 sizeof(intro->extend_info->nickname) - 1,
68 intro->extend_info->identity_digest, DIGEST_LEN);
69 tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
70 intro->extend_info->port = 1 + crypto_rand_int(65535);
71 intro->intro_key = crypto_pk_dup_key(pk2);
72 smartlist_add((*generated)->intro_nodes, intro);
75 crypto_pk_free(pk1);
76 crypto_pk_free(pk2);
79 rend_data_t *
80 mock_rend_data(const char *onion_address)
82 rend_data_v2_t *v2_data = tor_malloc_zero(sizeof(*v2_data));
83 rend_data_t *rend_query = &v2_data->base_;
84 rend_query->version = 2;
86 strlcpy(v2_data->onion_address, onion_address,
87 sizeof(v2_data->onion_address));
88 v2_data->auth_type = REND_NO_AUTH;
89 rend_query->hsdirs_fp = smartlist_new();
90 smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
91 DIGEST_LEN));
92 return rend_query;