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