Prop210: Add want_authority to directory_get_from_dirserver
[tor.git] / src / test / test_routerlist.c
blob1bc5e4bb16c7004435958e1ec99fc88b2c704ac2
1 /* Copyright (c) 2014, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define ROUTERLIST_PRIVATE
5 #include "or.h"
6 #include "routerlist.h"
7 #include "directory.h"
8 #include "test.h"
10 /* 4 digests + 3 sep + pre + post + NULL */
11 static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
13 static void
14 mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
15 const char *resource, int pds_flags,
16 download_want_authority_t want_authority)
18 (void)dir_purpose;
19 (void)router_purpose;
20 (void)pds_flags;
21 (void)want_authority;
22 tt_assert(resource);
23 strlcpy(output, resource, sizeof(output));
24 done:
28 static void
29 test_routerlist_initiate_descriptor_downloads(void *arg)
31 const char *prose = "unhurried and wise, we perceive.";
32 smartlist_t *digests = smartlist_new();
33 (void)arg;
35 for (int i = 0; i < 20; i++) {
36 smartlist_add(digests, (char*)prose);
39 MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
40 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
41 digests, 3, 7, 0);
42 UNMOCK(directory_get_from_dirserver);
44 tt_str_op(output, OP_EQ, "d/"
45 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
46 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
47 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
48 "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
49 ".z");
51 done:
52 smartlist_free(digests);
55 static int count = 0;
57 static void
58 mock_initiate_descriptor_downloads(const routerstatus_t *source,
59 int purpose, smartlist_t *digests,
60 int lo, int hi, int pds_flags)
62 (void)source;
63 (void)purpose;
64 (void)digests;
65 (void)pds_flags;
66 (void)hi;
67 (void)lo;
68 count += 1;
71 static void
72 test_routerlist_launch_descriptor_downloads(void *arg)
74 smartlist_t *downloadable = smartlist_new();
75 time_t now = time(NULL);
76 char *cp;
77 (void)arg;
79 for (int i = 0; i < 100; i++) {
80 cp = tor_malloc(DIGEST256_LEN);
81 tt_assert(cp);
82 crypto_rand(cp, DIGEST256_LEN);
83 smartlist_add(downloadable, cp);
86 MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
87 launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
88 NULL, now);
89 tt_int_op(3, ==, count);
90 UNMOCK(initiate_descriptor_downloads);
92 done:
93 SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
94 smartlist_free(downloadable);
97 #define NODE(name, flags) \
98 { #name, test_routerlist_##name, (flags), NULL, NULL }
100 struct testcase_t routerlist_tests[] = {
101 NODE(initiate_descriptor_downloads, 0),
102 NODE(launch_descriptor_downloads, 0),
103 END_OF_TESTCASES