test_rendcache.c 42.3 KB
Newer Older
Ola Bini's avatar
Ola Bini committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright (c) 2010-2015, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "orconfig.h"
#include "or.h"

#include "test.h"
#define RENDCACHE_PRIVATE
#include "rendcache.h"
#include "router.h"
#include "routerlist.h"
#include "config.h"
#include <openssl/rsa.h>
#include "rend_test_helpers.h"

#define NS_MODULE rend_cache

static const int RECENT_TIME = -10;
Ola Bini's avatar
Ola Bini committed
19
20
static const int TIME_IN_THE_PAST = -(REND_CACHE_MAX_AGE + \
                                      REND_CACHE_MAX_SKEW + 10);
Ola Bini's avatar
Ola Bini committed
21
22
23
24
25
26
27
static const int TIME_IN_THE_FUTURE = REND_CACHE_MAX_SKEW + 10;

extern strmap_t *rend_cache;
extern digestmap_t *rend_cache_v2_dir;
extern strmap_t *rend_cache_failure;
extern size_t rend_cache_total_allocation;

28
29
static rend_data_t *
mock_rend_data(const char *onion_address)
Ola Bini's avatar
Ola Bini committed
30
{
31
  rend_data_t *rend_query = tor_malloc_zero(sizeof(rend_data_t));
Ola Bini's avatar
Ola Bini committed
32

33
34
  strlcpy(rend_query->onion_address, onion_address,
          sizeof(rend_query->onion_address));
35
36
37
  rend_query->auth_type = REND_NO_AUTH;
  rend_query->hsdirs_fp = smartlist_new();
  smartlist_add(rend_query->hsdirs_fp, tor_memdup("aaaaaaaaaaaaaaaaaaaaaaaa",
Ola Bini's avatar
Ola Bini committed
38
                                                 DIGEST_LEN));
Ola Bini's avatar
Ola Bini committed
39
40
41
42
43
44
45
46

  return rend_query;
}

static void
test_rend_cache_lookup_entry(void *data)
{
  int ret;
47
  rend_data_t *mock_rend_query = NULL;
Ola Bini's avatar
Ola Bini committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  rend_cache_entry_t *entry = NULL;
  rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  char *service_id = NULL;
  (void)data;

  rend_cache_init();

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);

  ret = rend_cache_lookup_entry("abababababababab", 0, NULL);
  tt_int_op(ret, OP_EQ, -ENOENT);

  ret = rend_cache_lookup_entry("invalid query", 2, NULL);
  tt_int_op(ret, OP_EQ, -EINVAL);

  ret = rend_cache_lookup_entry("abababababababab", 2, NULL);
  tt_int_op(ret, OP_EQ, -ENOENT);

  ret = rend_cache_lookup_entry("abababababababab", 4224, NULL);
  tt_int_op(ret, OP_EQ, -ENOENT);

  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
71
72
73
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
74
                                     mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
75
76
77
78
79
80
81
82
83
84

  ret = rend_cache_lookup_entry(service_id, 2, NULL);
  tt_int_op(ret, OP_EQ, 0);

  ret = rend_cache_lookup_entry(service_id, 2, &entry);
  tt_assert(entry);
  tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
  tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);

 done:
85
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
86
  tor_free(service_id);
87
88
  rend_cache_free_all();
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
89
90
91
92
93
94
}

static void
test_rend_cache_store_v2_desc_as_client(void *data)
{
  rend_cache_store_status_t ret;
95
  rend_data_t *mock_rend_query;
Ola Bini's avatar
Ola Bini committed
96
97
98
99
100
101
102
103
104
105
106
107
108
  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  rend_cache_entry_t *entry = NULL;
  rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  char *service_id = NULL;
  char client_cookie[REND_DESC_COOKIE_LEN];
  (void)data;

  rend_cache_init();

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);

  // Test success
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
109
110
111
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
112
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
113
                                           &entry);
Ola Bini's avatar
Ola Bini committed
114
115
116
117
118
119
120
121
122

  tt_int_op(ret, OP_EQ, RCS_OKAY);
  tt_assert(entry);
  tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
  tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);

  // Test various failure modes

  // TODO: a too long desc_id_base32 argument crashes the function
Ola Bini's avatar
Ola Bini committed
123
124
125
126
   /* ret = rend_cache_store_v2_desc_as_client( */
   /*                   desc_holder->desc_str, */
   /*                   "3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG", */
   /*                   &mock_rend_query, NULL); */
Ola Bini's avatar
Ola Bini committed
127
128
129
  /* tt_int_op(ret, OP_EQ, RCS_BADDESC); */

  // Test bad base32 failure
Ola Bini's avatar
Ola Bini committed
130
  // This causes an assertion failure if we're running with assertions.
131
132
  // But when building without asserts, we can test it.
#ifdef DISABLE_ASSERTS_IN_UNIT_TESTS
Ola Bini's avatar
Ola Bini committed
133
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
134
                   "!xqunszqnaolrrfmtzgaki7mxelgvkj", mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
135
136
137
138
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
#endif

  // Test invalid descriptor
Ola Bini's avatar
Ola Bini committed
139
  ret = rend_cache_store_v2_desc_as_client("invalid descriptor",
140
             "3xqunszqnaolrrfmtzgaki7mxelgvkje", mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
141
142
143
  tt_int_op(ret, OP_EQ, RCS_BADDESC);

  // TODO: it doesn't seem to be possible to test invalid service ID condition.
Ola Bini's avatar
Ola Bini committed
144
145
  // that means it is likely not possible to have that condition without
  // earlier conditions failing first (such as signature checking of the desc)
Ola Bini's avatar
Ola Bini committed
146

147
148
  rend_cache_free_all();

Ola Bini's avatar
Ola Bini committed
149
150
  // Test mismatch between service ID and onion address
  rend_cache_init();
151
  strncpy(mock_rend_query->onion_address, "abc", REND_SERVICE_ID_LEN_BASE32+1);
Ola Bini's avatar
Ola Bini committed
152
153
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
                                           desc_id_base32,
154
                                           mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
155
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
156
157
  rend_cache_free_all();
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
158
159
160
161
162

  // Test incorrect descriptor ID
  rend_cache_init();
  mock_rend_query = mock_rend_data(service_id);
  desc_id_base32[0]++;
Ola Bini's avatar
Ola Bini committed
163
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
164
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
165
                                           NULL);
Ola Bini's avatar
Ola Bini committed
166
167
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
  desc_id_base32[0]--;
168
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
169
170
171

  // Test too old descriptor
  rend_cache_init();
172
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
173
  tor_free(service_id);
174
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
175
176
177

  generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
178
179
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
Ola Bini's avatar
Ola Bini committed
180

Ola Bini's avatar
Ola Bini committed
181
182
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
                                           desc_id_base32,
183
                                           mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
184
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
185
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
186
187
188

  // Test too new descriptor (in the future)
  rend_cache_init();
189
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
190
  tor_free(service_id);
191
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
192
193
194

  generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
195
196
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
Ola Bini's avatar
Ola Bini committed
197

Ola Bini's avatar
Ola Bini committed
198
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
199
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
200
                                           NULL);
Ola Bini's avatar
Ola Bini committed
201
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
202
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
203
204
205

  // Test when a descriptor is already in the cache
  rend_cache_init();
206
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
207
  tor_free(service_id);
Nick Mathewson's avatar
Nick Mathewson committed
208
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
209
210
211

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
212
213
214
215
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);

  rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
216
                                     mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
217
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
218
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
219
                                           NULL);
Ola Bini's avatar
Ola Bini committed
220
221
  tt_int_op(ret, OP_EQ, RCS_OKAY);

Ola Bini's avatar
Ola Bini committed
222
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
223
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
224
                                           &entry);
Ola Bini's avatar
Ola Bini committed
225
226
  tt_int_op(ret, OP_EQ, RCS_OKAY);
  tt_assert(entry);
227
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
228
229
230

  // Test unsuccessful decrypting of introduction points
  rend_cache_init();
231
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
232
  tor_free(service_id);
Nick Mathewson's avatar
Nick Mathewson committed
233
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
234
235
236

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  mock_rend_query = mock_rend_data(service_id);
237
  mock_rend_query->auth_type = REND_BASIC_AUTH;
Ola Bini's avatar
Ola Bini committed
238
  client_cookie[0] = 'A';
239
  memcpy(mock_rend_query->descriptor_cookie, client_cookie,
Ola Bini's avatar
Ola Bini committed
240
241
242
243
         REND_DESC_COOKIE_LEN);
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
244
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
245
                                           NULL);
Ola Bini's avatar
Ola Bini committed
246
  tt_int_op(ret, OP_EQ, RCS_OKAY);
247
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
248
249
250

  // Test successful run when we have REND_BASIC_AUTH but not cookie
  rend_cache_init();
251
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
252
  tor_free(service_id);
253
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
254
255
256

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  mock_rend_query = mock_rend_data(service_id);
257
  mock_rend_query->auth_type = REND_BASIC_AUTH;
Ola Bini's avatar
Ola Bini committed
258
259
260
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
261
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
262
                                           NULL);
Ola Bini's avatar
Ola Bini committed
263
264
  tt_int_op(ret, OP_EQ, RCS_OKAY);

265
266
  rend_cache_free_all();

Ola Bini's avatar
Ola Bini committed
267
268
  // Test when we have no introduction points
  rend_cache_init();
269
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
270
  tor_free(service_id);
271
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
272
273
274

  generate_desc(RECENT_TIME, &desc_holder, &service_id, 0);
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
275
276
277
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
278
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
279
                                           NULL);
Ola Bini's avatar
Ola Bini committed
280
  tt_int_op(ret, OP_EQ, RCS_BADDESC);
281
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
282
283
284

  // Test when we have too many intro points
  rend_cache_init();
285
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
286
  tor_free(service_id);
287
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
288
289
290

  generate_desc(RECENT_TIME, &desc_holder, &service_id, MAX_INTRO_POINTS+1);
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
291
292
293
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
  ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
294
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
295
                                           NULL);
Ola Bini's avatar
Ola Bini committed
296
297
298
299
300
  tt_int_op(ret, OP_EQ, RCS_BADDESC);

 done:
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);
301
302
  rend_cache_free_all();
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
303
304
305
306
307
308
}

static void
test_rend_cache_store_v2_desc_as_client_with_different_time(void *data)
{
  rend_cache_store_status_t ret;
309
  rend_data_t *mock_rend_query;
Ola Bini's avatar
Ola Bini committed
310
311
312
313
314
315
316
317
318
  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  rend_service_descriptor_t *generated = NULL;
  smartlist_t *descs = smartlist_new();
  time_t t;
  char *service_id = NULL;
  rend_encoded_v2_service_descriptor_t *desc_holder_newer;
  rend_encoded_v2_service_descriptor_t *desc_holder_older;

  t = time(NULL);
319
  rend_cache_init();
Ola Bini's avatar
Ola Bini committed
320
321
322
323

  create_descriptor(&generated, &service_id, 3);

  generated->timestamp = t + RECENT_TIME;
Ola Bini's avatar
Ola Bini committed
324
325
326
327
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
                       smartlist_get(descs, 0));
328
  smartlist_set(descs, 0, NULL);
Ola Bini's avatar
Ola Bini committed
329

330
331
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
Ola Bini's avatar
Ola Bini committed
332
333
334
335
  smartlist_free(descs);
  descs = smartlist_new();

  generated->timestamp = (t + RECENT_TIME) - 20;
Ola Bini's avatar
Ola Bini committed
336
337
338
339
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
                       smartlist_get(descs, 0));
340
  smartlist_set(descs, 0, NULL);
Ola Bini's avatar
Ola Bini committed
341
342
  (void)data;

Ola Bini's avatar
Ola Bini committed
343
344
  // Test when a descriptor is already in the cache and it is newer than the
  // one we submit
Ola Bini's avatar
Ola Bini committed
345
  mock_rend_query = mock_rend_data(service_id);
Ola Bini's avatar
Ola Bini committed
346
347
348
  base32_encode(desc_id_base32, sizeof(desc_id_base32),
                desc_holder_newer->desc_id, DIGEST_LEN);
  rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
349
                                     desc_id_base32, mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
350
  ret = rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
351
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
352
                                           NULL);
Ola Bini's avatar
Ola Bini committed
353
354
  tt_int_op(ret, OP_EQ, RCS_OKAY);

355
356
  rend_cache_free_all();

Ola Bini's avatar
Ola Bini committed
357
358
  // Test when an old descriptor is in the cache and we submit a newer one
  rend_cache_init();
Ola Bini's avatar
Ola Bini committed
359
  rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
360
                                     desc_id_base32, mock_rend_query, NULL);
Ola Bini's avatar
Ola Bini committed
361
  ret = rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
362
                                           desc_id_base32, mock_rend_query,
Ola Bini's avatar
Ola Bini committed
363
                                           NULL);
Ola Bini's avatar
Ola Bini committed
364
365
366
367
368
  tt_int_op(ret, OP_EQ, RCS_OKAY);

 done:
  rend_encoded_v2_service_descriptor_free(desc_holder_newer);
  rend_encoded_v2_service_descriptor_free(desc_holder_older);
369
370
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
Ola Bini's avatar
Ola Bini committed
371
372
373
  smartlist_free(descs);
  rend_service_descriptor_free(generated);
  tor_free(service_id);
374
375
  rend_cache_free_all();
  rend_data_free(mock_rend_query);
Ola Bini's avatar
Ola Bini committed
376
377
378
379
380
381
382
383
384
385
386
387
}

#define NS_SUBMODULE lookup_v2_desc_as_dir
NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
NS_DECL(int, hid_serv_responsible_for_desc_id, (const char *id));

static routerinfo_t *mock_routerinfo;
static int hid_serv_responsible_for_desc_id_response;

static const routerinfo_t *
NS(router_get_my_routerinfo)(void)
{
Ola Bini's avatar
Ola Bini committed
388
  if (!mock_routerinfo) {
Ola Bini's avatar
Ola Bini committed
389
390
391
392
393
394
395
396
397
    mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  }

  return mock_routerinfo;
}

static int
NS(hid_serv_responsible_for_desc_id)(const char *id)
{
Ola Bini's avatar
Ola Bini committed
398
  (void)id;
Ola Bini's avatar
Ola Bini committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  return hid_serv_responsible_for_desc_id_response;
}

static void
test_rend_cache_lookup_v2_desc_as_dir(void *data)
{
  int ret;
  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  char *service_id = NULL;
  const char *ret_desc = NULL;

  (void)data;

  NS_MOCK(router_get_my_routerinfo);
  NS_MOCK(hid_serv_responsible_for_desc_id);

  rend_cache_init();

  // Test invalid base32
  ret = rend_cache_lookup_v2_desc_as_dir("!bababababababab", NULL);
  tt_int_op(ret, OP_EQ, -1);

  // Test non-existent descriptor but well formed
Ola Bini's avatar
Ola Bini committed
423
424
  ret = rend_cache_lookup_v2_desc_as_dir("3xqunszqnaolrrfmtzgaki7mxelgvkje",
                                         NULL);
Ola Bini's avatar
Ola Bini committed
425
426
427
428
429
430
  tt_int_op(ret, OP_EQ, 0);

  // Test existing descriptor
  hid_serv_responsible_for_desc_id_response = 1;
  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
Ola Bini's avatar
Ola Bini committed
431
432
  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);
Ola Bini's avatar
Ola Bini committed
433
434
435
436
437
438
439
440
  ret = rend_cache_lookup_v2_desc_as_dir(desc_id_base32, &ret_desc);
  tt_int_op(ret, OP_EQ, 1);
  tt_assert(ret_desc);

 done:
  NS_UNMOCK(router_get_my_routerinfo);
  NS_UNMOCK(hid_serv_responsible_for_desc_id);
  tor_free(mock_routerinfo);
441
  rend_cache_free_all();
442
443
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);
Ola Bini's avatar
Ola Bini committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
}

#undef NS_SUBMODULE

#define NS_SUBMODULE store_v2_desc_as_dir
NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
NS_DECL(int, hid_serv_responsible_for_desc_id, (const char *id));

static const routerinfo_t *
NS(router_get_my_routerinfo)(void)
{
  return mock_routerinfo;
}

static int
NS(hid_serv_responsible_for_desc_id)(const char *id)
{
Ola Bini's avatar
Ola Bini committed
461
  (void)id;
Ola Bini's avatar
Ola Bini committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  return hid_serv_responsible_for_desc_id_response;
}

static void
test_rend_cache_store_v2_desc_as_dir(void *data)
{
  (void)data;
  rend_cache_store_status_t ret;
  rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  char *service_id = NULL;

  NS_MOCK(router_get_my_routerinfo);
  NS_MOCK(hid_serv_responsible_for_desc_id);

  rend_cache_init();

  // Test when we are not an HS dir
  mock_routerinfo = NULL;
  ret = rend_cache_store_v2_desc_as_dir("");
  tt_int_op(ret, OP_EQ, RCS_NOTDIR);

  // Test when we can't parse the descriptor
  mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  hid_serv_responsible_for_desc_id_response = 1;
  ret = rend_cache_store_v2_desc_as_dir("unparseable");
  tt_int_op(ret, OP_EQ, RCS_BADDESC);

  // Test when we are not responsible for an HS
  hid_serv_responsible_for_desc_id_response = 0;
  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

495
496
497
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);

Ola Bini's avatar
Ola Bini committed
498
499
500
501
502
503
  // Test when we have an old descriptor
  hid_serv_responsible_for_desc_id_response = 1;
  generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

504
505
506
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);

Ola Bini's avatar
Ola Bini committed
507
508
509
510
511
  // Test when we have a descriptor in the future
  generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

512
513
514
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);

Ola Bini's avatar
Ola Bini committed
515
516
517
518
519
  // Test when two descriptors
  generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

520
521
522
  rend_encoded_v2_service_descriptor_free(desc_holder);
  tor_free(service_id);

Ola Bini's avatar
Ola Bini committed
523
524
525
526
527
528
529
530
531
532
  // Test when asking for hidden service statistics  HiddenServiceStatistics
  rend_cache_purge();
  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  get_options_mutable()->HiddenServiceStatistics = 1;
  ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

 done:
  NS_UNMOCK(router_get_my_routerinfo);
  NS_UNMOCK(hid_serv_responsible_for_desc_id);
533
  rend_encoded_v2_service_descriptor_free(desc_holder);
Ola Bini's avatar
Ola Bini committed
534
  tor_free(service_id);
535
  rend_cache_free_all();
536
  tor_free(mock_routerinfo);
Ola Bini's avatar
Ola Bini committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
}

static void
test_rend_cache_store_v2_desc_as_dir_with_different_time(void *data)
{
  (void)data;

  rend_cache_store_status_t ret;
  rend_service_descriptor_t *generated = NULL;
  smartlist_t *descs = smartlist_new();
  time_t t;
  char *service_id = NULL;
  rend_encoded_v2_service_descriptor_t *desc_holder_newer;
  rend_encoded_v2_service_descriptor_t *desc_holder_older;

  NS_MOCK(router_get_my_routerinfo);
  NS_MOCK(hid_serv_responsible_for_desc_id);

  rend_cache_init();

  t = time(NULL);

  create_descriptor(&generated, &service_id, 3);
  generated->timestamp = t + RECENT_TIME;
Ola Bini's avatar
Ola Bini committed
561
562
563
564
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
                       smartlist_get(descs, 0));
565
566
567
  smartlist_set(descs, 0, NULL);
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
Ola Bini's avatar
Ola Bini committed
568
569
570
571
  smartlist_free(descs);
  descs = smartlist_new();

  generated->timestamp = (t + RECENT_TIME) - 20;
Ola Bini's avatar
Ola Bini committed
572
573
574
575
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
                       smartlist_get(descs, 0));
576
  smartlist_set(descs, 0, NULL);
Ola Bini's avatar
Ola Bini committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

  // Test when we have a newer descriptor stored
  mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  hid_serv_responsible_for_desc_id_response = 1;
  rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

  // Test when we have an old descriptor stored
  rend_cache_purge();
  rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

 done:
  NS_UNMOCK(router_get_my_routerinfo);
  NS_UNMOCK(hid_serv_responsible_for_desc_id);
594
595
596
597
598
599
  rend_cache_free_all();
  rend_service_descriptor_free(generated);
  tor_free(service_id);
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
  smartlist_free(descs);
600
601
  rend_encoded_v2_service_descriptor_free(desc_holder_newer);
  rend_encoded_v2_service_descriptor_free(desc_holder_older);
602
  tor_free(mock_routerinfo);
Ola Bini's avatar
Ola Bini committed
603
604
605
606
607
608
609
610
611
612
613
614
}

static void
test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data)
{
  (void)data;

  rend_cache_store_status_t ret;
  rend_service_descriptor_t *generated = NULL;
  smartlist_t *descs = smartlist_new();
  time_t t;
  char *service_id = NULL;
Nick Mathewson's avatar
Nick Mathewson committed
615
616
  rend_encoded_v2_service_descriptor_t *desc_holder_one = NULL;
  rend_encoded_v2_service_descriptor_t *desc_holder_two = NULL;
Ola Bini's avatar
Ola Bini committed
617
618
619
620
621
622
623
624
625
626

  NS_MOCK(router_get_my_routerinfo);
  NS_MOCK(hid_serv_responsible_for_desc_id);

  rend_cache_init();

  t = time(NULL);

  create_descriptor(&generated, &service_id, 3);
  generated->timestamp = t + RECENT_TIME;
Ola Bini's avatar
Ola Bini committed
627
628
629
630
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_one = ((rend_encoded_v2_service_descriptor_t *)
                     smartlist_get(descs, 0));
631
  smartlist_set(descs, 0, NULL);
Ola Bini's avatar
Ola Bini committed
632

633
634
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
Ola Bini's avatar
Ola Bini committed
635
636
637
638
639
  smartlist_free(descs);
  descs = smartlist_new();

  generated->timestamp = t + RECENT_TIME;
  generated->protocols = 41;
Ola Bini's avatar
Ola Bini committed
640
641
642
643
  rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
                             REND_NO_AUTH, NULL, NULL);
  desc_holder_two = ((rend_encoded_v2_service_descriptor_t *)
                     smartlist_get(descs, 0));
644
  smartlist_set(descs, 0, NULL);
Ola Bini's avatar
Ola Bini committed
645
646
647
648
649
650
651
652
653
654
655

  // Test when we have another descriptor stored, with a different descriptor
  mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  hid_serv_responsible_for_desc_id_response = 1;
  rend_cache_store_v2_desc_as_dir(desc_holder_one->desc_str);
  ret = rend_cache_store_v2_desc_as_dir(desc_holder_two->desc_str);
  tt_int_op(ret, OP_EQ, RCS_OKAY);

 done:
  NS_UNMOCK(router_get_my_routerinfo);
  NS_UNMOCK(hid_serv_responsible_for_desc_id);
656
657
658
659
660
661
  rend_cache_free_all();
  rend_service_descriptor_free(generated);
  tor_free(service_id);
  SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
                    rend_encoded_v2_service_descriptor_free(d));
  smartlist_free(descs);
Nick Mathewson's avatar
Nick Mathewson committed
662
663
  rend_encoded_v2_service_descriptor_free(desc_holder_one);
  rend_encoded_v2_service_descriptor_free(desc_holder_two);
Ola Bini's avatar
Ola Bini committed
664
665
666
667
668
669
670
671
672
673
}

#undef NS_SUBMODULE

static void
test_rend_cache_init(void *data)
{
  (void)data;

  tt_assert_msg(!rend_cache, "rend_cache should be NULL when starting");
Ola Bini's avatar
Ola Bini committed
674
675
676
677
  tt_assert_msg(!rend_cache_v2_dir, "rend_cache_v2_dir should be NULL "
                "when starting");
  tt_assert_msg(!rend_cache_failure, "rend_cache_failure should be NULL when "
                "starting");
Ola Bini's avatar
Ola Bini committed
678
679
680
681

  rend_cache_init();

  tt_assert_msg(rend_cache, "rend_cache should not be NULL after initing");
Ola Bini's avatar
Ola Bini committed
682
683
684
685
  tt_assert_msg(rend_cache_v2_dir, "rend_cache_v2_dir should not be NULL "
                "after initing");
  tt_assert_msg(rend_cache_failure, "rend_cache_failure should not be NULL "
                "after initing");
Ola Bini's avatar
Ola Bini committed
686
687
688
689
690
691

  tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
  tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);

 done:
692
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
}

static void
test_rend_cache_decrement_allocation(void *data)
{
  (void)data;

  // Test when the cache has enough allocations
  rend_cache_total_allocation = 10;
  rend_cache_decrement_allocation(3);
  tt_int_op(rend_cache_total_allocation, OP_EQ, 7);

  // Test when there are not enough allocations
  rend_cache_total_allocation = 1;
  rend_cache_decrement_allocation(2);
  tt_int_op(rend_cache_total_allocation, OP_EQ, 0);

  // And again
  rend_cache_decrement_allocation(2);
  tt_int_op(rend_cache_total_allocation, OP_EQ, 0);

 done:
  (void)0;
}

static void
test_rend_cache_increment_allocation(void *data)
{
  (void)data;

  // Test when the cache is not overflowing
  rend_cache_total_allocation = 5;
  rend_cache_increment_allocation(3);
  tt_int_op(rend_cache_total_allocation, OP_EQ, 8);

  // Test when there are too many allocations
  rend_cache_total_allocation = SIZE_MAX-1;
  rend_cache_increment_allocation(2);
731
  tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
Ola Bini's avatar
Ola Bini committed
732
733
734

  // And again
  rend_cache_increment_allocation(2);
735
  tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
Ola Bini's avatar
Ola Bini committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

 done:
  (void)0;
}

static void
test_rend_cache_failure_intro_entry_new(void *data)
{
  time_t now;
  rend_cache_failure_intro_t *entry;
  rend_intro_point_failure_t failure;

  (void)data;

  failure = INTRO_POINT_FAILURE_TIMEOUT;
  now = time(NULL);
  entry = rend_cache_failure_intro_entry_new(failure);

  tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
  tt_int_op(entry->created_ts, OP_GE, now-5);
  tt_int_op(entry->created_ts, OP_LE, now+5);

 done:
  tor_free(entry);
}

static void
test_rend_cache_failure_intro_lookup(void *data)
{
  (void)data;
  int ret;
  rend_cache_failure_t *failure;
  rend_cache_failure_intro_t *ip;
  rend_cache_failure_intro_t *entry;
770
771
772
  const char key_ip_one[DIGEST_LEN] = "ip1";
  const char key_ip_two[DIGEST_LEN] = "ip2";
  const char key_foo[DIGEST_LEN] = "foo1";
Ola Bini's avatar
Ola Bini committed
773
774
775
776
777

  rend_cache_init();

  failure = rend_cache_failure_entry_new();
  ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
778
  digestmap_set(failure->intro_failures, key_ip_one, ip);
Ola Bini's avatar
Ola Bini committed
779
780
781
  strmap_set_lc(rend_cache_failure, "foo1", failure);

  // Test not found
782
  ret = cache_failure_intro_lookup((const uint8_t *) key_foo, "foo2", NULL);
Ola Bini's avatar
Ola Bini committed
783
784
785
  tt_int_op(ret, OP_EQ, 0);

  // Test found with no intro failures in it
786
  ret = cache_failure_intro_lookup((const uint8_t *) key_ip_two, "foo1", NULL);
Ola Bini's avatar
Ola Bini committed
787
788
789
  tt_int_op(ret, OP_EQ, 0);

  // Test found
790
  ret = cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", NULL);
Ola Bini's avatar
Ola Bini committed
791
792
793
  tt_int_op(ret, OP_EQ, 1);

  // Test found and asking for entry
794
  cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", &entry);
Ola Bini's avatar
Ola Bini committed
795
796
797
798
  tt_assert(entry);
  tt_assert(entry == ip);

 done:
799
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
}

static void
test_rend_cache_clean(void *data)
{
  rend_cache_entry_t *one, *two;
  rend_service_descriptor_t *desc_one, *desc_two;
  strmap_iter_t *iter = NULL;
  const char *key;
  void *val;

  (void)data;

  rend_cache_init();

  // Test with empty rendcache
816
  rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
Ola Bini's avatar
Ola Bini committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
  tt_int_op(strmap_size(rend_cache), OP_EQ, 0);

  // Test with two old entries
  one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  two = tor_malloc_zero(sizeof(rend_cache_entry_t));
  desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  one->parsed = desc_one;
  two->parsed = desc_two;

  desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
  desc_two->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
  desc_one->pk = pk_generate(0);
  desc_two->pk = pk_generate(1);

  strmap_set_lc(rend_cache, "foo1", one);
  strmap_set_lc(rend_cache, "foo2", two);

835
  rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
Ola Bini's avatar
Ola Bini committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
  tt_int_op(strmap_size(rend_cache), OP_EQ, 0);

  // Test with one old entry and one newer entry
  one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  two = tor_malloc_zero(sizeof(rend_cache_entry_t));
  desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  one->parsed = desc_one;
  two->parsed = desc_two;

  desc_one->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
  desc_two->timestamp = time(NULL) - 100;
  desc_one->pk = pk_generate(0);
  desc_two->pk = pk_generate(1);

  strmap_set_lc(rend_cache, "foo1", one);
  strmap_set_lc(rend_cache, "foo2", two);

854
  rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
Ola Bini's avatar
Ola Bini committed
855
856
857
858
859
860
861
  tt_int_op(strmap_size(rend_cache), OP_EQ, 1);

  iter = strmap_iter_init(rend_cache);
  strmap_iter_get(iter, &key, &val);
  tt_str_op(key, OP_EQ, "foo2");

 done:
862
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
}

static void
test_rend_cache_failure_entry_new(void *data)
{
  rend_cache_failure_t *failure;

  (void)data;

  failure = rend_cache_failure_entry_new();
  tt_assert(failure);
  tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 0);

 done:
877
  rend_cache_failure_entry_free(failure);
Ola Bini's avatar
Ola Bini committed
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
}

static void
test_rend_cache_failure_entry_free(void *data)
{
  (void)data;

  // Test that it can deal with a NULL argument
  rend_cache_failure_entry_free(NULL);

 /* done: */
 /*  (void)0; */
}

static void
test_rend_cache_failure_clean(void *data)
{
  rend_cache_failure_t *failure;
  rend_cache_failure_intro_t *ip_one, *ip_two;

898
899
900
  const char key_one[DIGEST_LEN] = "ip1";
  const char key_two[DIGEST_LEN] = "ip2";

Ola Bini's avatar
Ola Bini committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
  (void)data;

  rend_cache_init();

  // Test with empty failure cache
  rend_cache_failure_clean(time(NULL));
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);

  // Test with one empty failure entry
  failure = rend_cache_failure_entry_new();
  strmap_set_lc(rend_cache_failure, "foo1", failure);
  rend_cache_failure_clean(time(NULL));
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);

  // Test with one new intro point
  failure = rend_cache_failure_entry_new();
  ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
918
  digestmap_set(failure->intro_failures, key_one, ip_one);
Ola Bini's avatar
Ola Bini committed
919
920
921
922
923
924
925
926
927
  strmap_set_lc(rend_cache_failure, "foo1", failure);
  rend_cache_failure_clean(time(NULL));
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);

  // Test with one old intro point
  rend_cache_failure_purge();
  failure = rend_cache_failure_entry_new();
  ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  ip_one->created_ts = time(NULL) - 7*60;
928
  digestmap_set(failure->intro_failures, key_one, ip_one);
Ola Bini's avatar
Ola Bini committed
929
930
931
932
933
934
935
936
937
  strmap_set_lc(rend_cache_failure, "foo1", failure);
  rend_cache_failure_clean(time(NULL));
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);

  // Test with one old intro point and one new one
  rend_cache_failure_purge();
  failure = rend_cache_failure_entry_new();
  ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  ip_one->created_ts = time(NULL) - 7*60;
938
  digestmap_set(failure->intro_failures, key_one, ip_one);
Ola Bini's avatar
Ola Bini committed
939
940
  ip_two = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  ip_two->created_ts = time(NULL) - 2*60;
941
  digestmap_set(failure->intro_failures, key_two, ip_two);
Ola Bini's avatar
Ola Bini committed
942
943
944
945
946
947
  strmap_set_lc(rend_cache_failure, "foo1", failure);
  rend_cache_failure_clean(time(NULL));
  tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
  tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 1);

 done:
948
  rend_cache_free_all();
Ola Bini's avatar
Ola Bini committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
}

static void
test_rend_cache_failure_remove(void *data)
{
  rend_service_descriptor_t *desc;
  (void)data;

  rend_cache_init();

  // Test that it deals well with a NULL desc
  rend_cache_failure_remove(NULL);

  // Test a descriptor that isn't in the cache
  desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  desc->pk = pk_generate(0);
  rend_cache_failure_remove(desc);

Ola Bini's avatar
Ola Bini committed
967
968
  // There seems to not exist any way of getting rend_cache_failure_remove()
  // to fail because of a problem with rend_get_service_id from here
969
  rend_cache_free_all();
970
971

  rend_service_descriptor_free(desc);
Ola Bini's avatar
Ola Bini committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
 /* done: */
 /*  (void)0; */
}

static void
test_rend_cache_free_all(void *data)
{
  rend_cache_failure_t *failure;
  rend_cache_entry_t *one;
  rend_service_descriptor_t *desc_one;

  (void)data;

  rend_cache_init();

  failure = rend_cache_failure_entry_new();
  strmap_set_lc(rend_cache_failure, "foo1", failure);

  one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  one->parsed = desc_one;
  desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
  desc_one->pk = pk_generate(0);
  strmap_set_lc(rend_cache, "foo1", one);

  rend_cache_free_all();

  tt_assert(!rend_cache);
  tt_assert(!rend_cache_v2_dir);
For faster browsing, not all history is shown. View entire blame