test_hs_common.c 68.7 KB
Newer Older
1
/* Copyright (c) 2017-2021, The Tor Project, Inc. */
2
3
4
5
6
7
8
/* See LICENSE for licensing information */

/**
 * \file test_hs_common.c
 * \brief Test hidden service common functionalities.
 */

9
#define CONNECTION_EDGE_PRIVATE
10
#define HS_COMMON_PRIVATE
11
#define HS_CLIENT_PRIVATE
12
#define HS_SERVICE_PRIVATE
13
#define NODELIST_PRIVATE
14

Nick Mathewson's avatar
Nick Mathewson committed
15
16
17
18
#include "test/test.h"
#include "test/test_helpers.h"
#include "test/log_test_helpers.h"
#include "test/hs_test_helpers.h"
19

20
#include "core/or/connection_edge.h"
21
#include "lib/crypt_ops/crypto_format.h"
22
#include "lib/crypt_ops/crypto_rand.h"
23
24
25
26
27
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_service.h"
#include "app/config/config.h"
#include "feature/nodelist/networkstatus.h"
28
#include "feature/dirclient/dirclient.h"
29
30
31
32
33
34
#include "feature/dirauth/dirvote.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "app/config/statefile.h"
#include "core/or/circuitlist.h"
#include "feature/dirauth/shared_random.h"
35
#include "feature/dirauth/voting_schedule.h"
36

37
38
39
40
41
42
#include "feature/nodelist/microdesc_st.h"
#include "feature/nodelist/networkstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "app/config/or_state_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerstatus_st.h"
43

44
/** Test the validation of HS v3 addresses */
45
46
47
48
49
50
51
52
53
54
55
static void
test_validate_address(void *arg)
{
  int ret;

  (void) arg;

  /* Address too short and too long. */
  setup_full_capture_of_logs(LOG_WARN);
  ret = hs_address_is_valid("blah");
  tt_int_op(ret, OP_EQ, 0);
56
  expect_log_msg_containing("Invalid length");
57
58
59
60
61
62
  teardown_capture_of_logs();

  setup_full_capture_of_logs(LOG_WARN);
  ret = hs_address_is_valid(
           "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  tt_int_op(ret, OP_EQ, 0);
63
  expect_log_msg_containing("Invalid length");
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  teardown_capture_of_logs();

  /* Invalid checksum (taken from prop224) */
  setup_full_capture_of_logs(LOG_WARN);
  ret = hs_address_is_valid(
           "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  tt_int_op(ret, OP_EQ, 0);
  expect_log_msg_containing("invalid checksum");
  teardown_capture_of_logs();

  setup_full_capture_of_logs(LOG_WARN);
  ret = hs_address_is_valid(
           "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  tt_int_op(ret, OP_EQ, 0);
  expect_log_msg_containing("invalid checksum");
  teardown_capture_of_logs();

  /* Non base32 decodable string. */
  setup_full_capture_of_logs(LOG_WARN);
  ret = hs_address_is_valid(
           "????????????????????????????????????????????????????????");
  tt_int_op(ret, OP_EQ, 0);
86
  expect_log_msg_containing("Unable to base32 decode");
87
88
89
90
  teardown_capture_of_logs();

  /* Valid address. */
  ret = hs_address_is_valid(
91
           "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
92
93
94
95
96
97
  tt_int_op(ret, OP_EQ, 1);

 done:
  ;
}

98
99
100
101
static int
mock_write_str_to_file(const char *path, const char *str, int bin)
{
  (void)bin;
102
  tt_str_op(path, OP_EQ, "/double/five"PATH_SEPARATOR"squared");
103
  tt_str_op(str, OP_EQ,
104
           "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion\n");
105
106
107
108
109

 done:
  return 0;
}

110
111
/** Test building HS v3 onion addresses. Uses test vectors from the
 *  ./hs_build_address.py script. */
112
113
114
115
116
117
static void
test_build_address(void *arg)
{
  int ret;
  char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  ed25519_public_key_t pubkey;
118
119
120
  /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  char pubkey_hex[] =
    "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
121
  hs_service_t *service = NULL;
122
123
124

  (void) arg;

125
126
  MOCK(write_str_to_file, mock_write_str_to_file);

127
128
129
  /* The following has been created with hs_build_address.py script that
   * follows proposal 224 specification to build an onion address. */
  static const char *test_addr =
130
    "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid";
131

132
133
134
  /* Let's try to build the same onion address as the script */
  base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
                pubkey_hex, strlen(pubkey_hex));
135
136
137
138
139
140
  hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  tt_str_op(test_addr, OP_EQ, onion_addr);
  /* Validate that address. */
  ret = hs_address_is_valid(onion_addr);
  tt_int_op(ret, OP_EQ, 1);

141
142
143
144
145
146
  service = tor_malloc_zero(sizeof(hs_service_t));
  memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  tor_asprintf(&service->config.directory_path, "/double/five");
  ret = write_address_to_file(service, "squared");
  tt_int_op(ret, OP_EQ, 0);

147
 done:
148
  hs_service_free(service);
149
150
151
152
153
154
155
156
157
}

/** Test that our HS time period calculation functions work properly */
static void
test_time_period(void *arg)
{
  (void) arg;
  uint64_t tn;
  int retval;
158
  time_t fake_time, correct_time, start_time;
159
160
161
162

  /* Let's do the example in prop224 section [TIME-PERIODS] */
  retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
                              &fake_time);
163
  tt_int_op(retval, OP_EQ, 0);
164
165
166

  /* Check that the time period number is right */
  tn = hs_get_time_period_num(fake_time);
167
  tt_u64_op(tn, OP_EQ, 16903);
168
169
170
171
172

  /* Increase current time to 11:59:59 UTC and check that the time period
     number is still the same */
  fake_time += 3599;
  tn = hs_get_time_period_num(fake_time);
173
  tt_u64_op(tn, OP_EQ, 16903);
174

175
176
177
  { /* Check start time of next time period */
    retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
                                &correct_time);
178
    tt_int_op(retval, OP_EQ, 0);
179
180
181
182
183

    start_time = hs_get_start_time_of_next_time_period(fake_time);
    tt_int_op(start_time, OP_EQ, correct_time);
  }

184
185
186
  /* Now take time to 12:00:00 UTC and check that the time period rotated */
  fake_time += 1;
  tn = hs_get_time_period_num(fake_time);
187
  tt_u64_op(tn, OP_EQ, 16904);
188
189
190

  /* Now also check our hs_get_next_time_period_num() function */
  tn = hs_get_next_time_period_num(fake_time);
191
  tt_u64_op(tn, OP_EQ, 16905);
192

193
194
195
  { /* Check start time of next time period again */
    retval = parse_rfc1123_time("Wed, 14 Apr 2016 12:00:00 UTC",
                                &correct_time);
196
    tt_int_op(retval, OP_EQ, 0);
197
198
199
200
201
202
203
204
205
206

    start_time = hs_get_start_time_of_next_time_period(fake_time);
    tt_int_op(start_time, OP_EQ, correct_time);
  }

  /* Now do another sanity check: The time period number at the start of the
   * next time period, must be the same time period number as the one returned
   * from hs_get_next_time_period_num() */
  {
    time_t next_tp_start = hs_get_start_time_of_next_time_period(fake_time);
Nick Mathewson's avatar
Nick Mathewson committed
207
    tt_u64_op(hs_get_time_period_num(next_tp_start), OP_EQ,
208
209
210
              hs_get_next_time_period_num(fake_time));
  }

211
212
213
214
 done:
  ;
}

215
/** Test that we can correctly find the start time of the next time period */
216
217
218
219
220
221
222
223
224
225
226
227
static void
test_start_time_of_next_time_period(void *arg)
{
  (void) arg;
  int retval;
  time_t fake_time;
  char tbuf[ISO_TIME_LEN + 1];
  time_t next_tp_start_time;

  /* Do some basic tests */
  retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
                              &fake_time);
228
  tt_int_op(retval, OP_EQ, 0);
229
230
231
232
233
234
235
236
  next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  /* Compare it with the correct result */
  format_iso_time(tbuf, next_tp_start_time);
  tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);

  /* Another test with an edge-case time (start of TP) */
  retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
                              &fake_time);
237
  tt_int_op(retval, OP_EQ, 0);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  format_iso_time(tbuf, next_tp_start_time);
  tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);

  {
    /* Now pretend we are on a testing network and alter the voting schedule to
       be every 10 seconds. This means that a time period has length 10*24
       seconds (4 minutes). It also means that we apply a rotational offset of
       120 seconds to the time period, so that it starts at 00:02:00 instead of
       00:00:00. */
    or_options_t *options = get_options_mutable();
    options->TestingTorNetwork = 1;
    options->V3AuthVotingInterval = 10;
    options->TestingV3AuthInitialVotingInterval = 10;

    retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
                                &fake_time);
255
    tt_int_op(retval, OP_EQ, 0);
256
257
258
259
260
261
262
    next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
    /* Compare it with the correct result */
    format_iso_time(tbuf, next_tp_start_time);
    tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);

    retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
                                &fake_time);
263
    tt_int_op(retval, OP_EQ, 0);
264
265
266
267
268
269
270
271
272
273
    next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
    /* Compare it with the correct result */
    format_iso_time(tbuf, next_tp_start_time);
    tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  }

 done:
  ;
}

274
275
276
277
278
/* Cleanup the global nodelist. It also frees the "md" in the node_t because
 * we allocate the memory in helper_add_hsdir_to_networkstatus(). */
static void
cleanup_nodelist(void)
{
279
  const smartlist_t *nodelist = nodelist_get_list();
280
281
282
283
284
285
286
  SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
    tor_free(node->md);
    node->md = NULL;
  } SMARTLIST_FOREACH_END(node);
  nodelist_free_all();
}

287
288
static void
helper_add_hsdir_to_networkstatus(networkstatus_t *ns,
289
                                  int identity_idx,
290
291
292
293
294
                                  const char *nickname,
                                  int is_hsdir)
{
  routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t));
  routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
295
  uint8_t identity[DIGEST_LEN];
296
  node_t *node = NULL;
297
298
299

  memset(identity, identity_idx, sizeof(identity));

300
301
  memcpy(rs->identity_digest, identity, DIGEST_LEN);
  rs->is_hs_dir = is_hsdir;
302
  rs->pv.supports_v3_hsdir = 1;
303
  strlcpy(rs->nickname, nickname, sizeof(rs->nickname));
304
305
  tor_addr_parse(&ri->ipv4_addr, "1.2.3.4");
  tor_addr_parse(&rs->ipv4_addr, "1.2.3.4");
306
307
308
  ri->nickname = tor_strdup(nickname);
  ri->protocol_list = tor_strdup("HSDir=1-2 LinkAuth=3");
  memcpy(ri->cache_info.identity_digest, identity, DIGEST_LEN);
309
310
311
312
  ri->cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  /* Needed for the HSDir index computation. */
  memset(&ri->cache_info.signing_key_cert->signing_key,
         identity_idx, ED25519_PUBKEY_LEN);
313
  tt_assert(nodelist_set_routerinfo(ri, NULL));
314
315

  node = node_get_mutable_by_id(ri->cache_info.identity_digest);
316
317
  tt_assert(node);
  node->rs = rs;
318
319
  /* We need this to exist for node_has_preferred_descriptor() to return
   * true. */
320
321
322
323
324
  node->md = tor_malloc_zero(sizeof(microdesc_t));
  /* Do this now the nodelist_set_routerinfo() function needs a "rs" to set
   * the indexes which it doesn't have when it is called. */
  node_set_hsdir_index(node, ns);
  node->ri = NULL;
325
326
327
  smartlist_add(ns->routerstatus_list, rs);

 done:
328
329
330
  if (node == NULL)
    routerstatus_free(rs);

331
  routerinfo_free(ri);
332
333
}

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
static networkstatus_t *mock_ns = NULL;

static networkstatus_t *
mock_networkstatus_get_latest_consensus(void)
{
  time_t now = approx_time();

  /* If initialized, return it */
  if (mock_ns) {
    return mock_ns;
  }

  /* Initialize fake consensus */
  mock_ns = tor_malloc_zero(sizeof(networkstatus_t));

  /* This consensus is live */
  mock_ns->valid_after = now-1;
  mock_ns->fresh_until = now+1;
  mock_ns->valid_until = now+2;
  /* Create routerstatus list */
  mock_ns->routerstatus_list = smartlist_new();
355
  mock_ns->type = NS_TYPE_CONSENSUS;
356
357
358
359

  return mock_ns;
}

360
static networkstatus_t *
361
mock_networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
362
363
{
  (void) now;
364
  (void) flavor;
365
366
367
368
369
370
371

  tt_assert(mock_ns);

 done:
  return mock_ns;
}

372
373
374
375
376
377
378
379
380
381
382
383
/** Test the responsible HSDirs calculation function */
static void
test_responsible_hsdirs(void *arg)
{
  smartlist_t *responsible_dirs = smartlist_new();
  networkstatus_t *ns = NULL;
  (void) arg;

  hs_init();

  MOCK(networkstatus_get_latest_consensus,
       mock_networkstatus_get_latest_consensus);
384
385
  MOCK(networkstatus_get_reasonably_live_consensus,
       mock_networkstatus_get_reasonably_live_consensus);
386
387
388
389

  ns = networkstatus_get_latest_consensus();

  { /* First router: HSdir */
390
    helper_add_hsdir_to_networkstatus(ns, 1, "igor", 1);
391
392
  }

393
  { /* Second HSDir */
394
    helper_add_hsdir_to_networkstatus(ns, 2, "victor", 1);
395
396
397
  }

  { /* Third relay but not HSDir */
398
    helper_add_hsdir_to_networkstatus(ns, 3, "spyro", 0);
399
400
  }

401
402
403
404
  /* Use a fixed time period and pub key so we always take the same path */
  ed25519_public_key_t pubkey;
  uint64_t time_period_num = 17653; // 2 May, 2018, 14:00.
  memset(&pubkey, 42, sizeof(pubkey));
405

406
  hs_get_responsible_hsdirs(&pubkey, time_period_num,
407
                            0, 0, responsible_dirs);
408
409
410
411

  /* Make sure that we only found 2 responsible HSDirs.
   * The third relay was not an hsdir! */
  tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 2);
412
413
414
415

  /** TODO: Build a bigger network and do more tests here */

 done:
416
417
  SMARTLIST_FOREACH(ns->routerstatus_list,
                    routerstatus_t *, rs, routerstatus_free(rs));
418
419
420
  smartlist_free(responsible_dirs);
  smartlist_clear(ns->routerstatus_list);
  networkstatus_vote_free(mock_ns);
421
  cleanup_nodelist();
422
423

  UNMOCK(networkstatus_get_reasonably_live_consensus);
424
425
}

426
427
428
429
430
431
432
433
434
static void
mock_directory_initiate_request(directory_request_t *req)
{
  (void)req;
  return;
}

static int
mock_hs_desc_encode_descriptor(const hs_descriptor_t *desc,
435
436
437
                               const ed25519_keypair_t *signing_kp,
                               const uint8_t *descriptor_cookie,
                               char **encoded_out)
438
439
440
{
  (void)desc;
  (void)signing_kp;
441
  (void)descriptor_cookie;
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472

  tor_asprintf(encoded_out, "lulu");
  return 0;
}

static or_state_t dummy_state;

/* Mock function to get fake or state (used for rev counters) */
static or_state_t *
get_or_state_replacement(void)
{
  return &dummy_state;
}

static int
mock_router_have_minimum_dir_info(void)
{
  return 1;
}

/** Test that we correctly detect when the HSDir hash ring changes so that we
 *  reupload our descriptor. */
static void
test_desc_reupload_logic(void *arg)
{
  networkstatus_t *ns = NULL;

  (void) arg;

  hs_init();

473
474
  MOCK(networkstatus_get_reasonably_live_consensus,
       mock_networkstatus_get_reasonably_live_consensus);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  MOCK(router_have_minimum_dir_info,
       mock_router_have_minimum_dir_info);
  MOCK(get_or_state,
       get_or_state_replacement);
  MOCK(networkstatus_get_latest_consensus,
       mock_networkstatus_get_latest_consensus);
  MOCK(directory_initiate_request,
       mock_directory_initiate_request);
  MOCK(hs_desc_encode_descriptor,
       mock_hs_desc_encode_descriptor);

  ns = networkstatus_get_latest_consensus();

  /** Test logic:
   *  1) Upload descriptor to HSDirs
   *     CHECK that previous_hsdirs list was populated.
   *  2) Then call router_dir_info_changed() without an HSDir set change.
Samanta Navarro's avatar
Samanta Navarro committed
492
   *     CHECK that no reupload occurs.
493
494
495
496
497
498
499
500
501
   *  3) Now change the HSDir set, and call dir_info_changed() again.
   *     CHECK that reupload occurs.
   *  4) Finally call service_desc_schedule_upload().
   *     CHECK that previous_hsdirs list was cleared.
   **/

  /* Let's start by building our descriptor and service */
  hs_service_descriptor_t *desc = service_descriptor_new();
  hs_service_t *service = NULL;
502
503
504
  /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  char pubkey_hex[] =
    "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
505
506
  char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  ed25519_public_key_t pubkey;
507
508
  base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
                pubkey_hex, strlen(pubkey_hex));
509
510
  hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  service = tor_malloc_zero(sizeof(hs_service_t));
511
  tt_assert(service);
512
513
514
515
516
517
518
519
520
521
522
523
524
  memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  ed25519_public_key_generate(&service->keys.identity_pk,
                              &service->keys.identity_sk);
  service->desc_current = desc;
  /* Also add service to service map */
  hs_service_ht *service_map = get_hs_service_map();
  tt_assert(service_map);
  tt_int_op(hs_service_get_num_services(), OP_EQ, 0);
  register_service(service_map, service);
  tt_int_op(hs_service_get_num_services(), OP_EQ, 1);

  /* Now let's create our hash ring: */
525
  {
526
527
    helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
    helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
528
529
530
531
    helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
    helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
    helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
    helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
532
533
534
  }

  /* Now let's upload our desc to all hsdirs */
535
  upload_descriptor_to_all(service, desc);
536
  /* Check that previous hsdirs were populated */
537
  tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
538
539
540
541
542
543
544
545

  /* Poison next upload time so that we can see if it was changed by
   * router_dir_info_changed(). No changes in hash ring so far, so the upload
   * time should stay as is. */
  desc->next_upload_time = 42;
  router_dir_info_changed();
  tt_int_op(desc->next_upload_time, OP_EQ, 42);

546
547
548
549
550
551
  /* Now change the HSDir hash ring by swapping nora for aaron.
   * Start by clearing the hash ring */
  {
    SMARTLIST_FOREACH(ns->routerstatus_list,
                      routerstatus_t *, rs, routerstatus_free(rs));
    smartlist_clear(ns->routerstatus_list);
552
    cleanup_nodelist();
553
554
    routerlist_free_all();
  }
555

556
557
558
559
560
561
562
  { /* Now add back all the nodes */
    helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
    helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
    helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
    helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
    helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
    helper_add_hsdir_to_networkstatus(ns, 7, "nora", 1);
563
564
  }

565
566
  /* Now call service_desc_hsdirs_changed() and see that it detected the hash
     ring change */
567
568
  time_t now = approx_time();
  tt_assert(now);
569
  tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
570
571
  tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);

572
  /* Now order another upload and see that we keep having 6 prev hsdirs */
573
  upload_descriptor_to_all(service, desc);
574
  /* Check that previous hsdirs were populated */
575
  tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
576
577
578
579
580
581
582
583

  /* Now restore the HSDir hash ring to its original state by swapping back
     aaron for nora */
  /* First clear up the hash ring */
  {
    SMARTLIST_FOREACH(ns->routerstatus_list,
                      routerstatus_t *, rs, routerstatus_free(rs));
    smartlist_clear(ns->routerstatus_list);
584
    cleanup_nodelist();
585
586
587
588
589
590
591
592
593
594
595
596
597
598
    routerlist_free_all();
  }

  { /* Now populate the hash ring again */
    helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
    helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
    helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
    helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
    helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
    helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  }

  /* Check that our algorithm catches this change of hsdirs */
  tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
599
600
601
602
603
604
605

  /* Now pretend that the descriptor changed, and order a reupload to all
     HSDirs. Make sure that the set of previous HSDirs was cleared. */
  service_desc_schedule_upload(desc, now, 1);
  tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 0);

  /* Now reupload again: see that the prev hsdir set got populated again. */
606
  upload_descriptor_to_all(service, desc);
607
  tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
608
609

 done:
610
611
612
  SMARTLIST_FOREACH(ns->routerstatus_list,
                    routerstatus_t *, rs, routerstatus_free(rs));
  smartlist_clear(ns->routerstatus_list);
613
614
615
616
  if (service) {
    remove_service(get_hs_service_map(), service);
    hs_service_free(service);
  }
617
  networkstatus_vote_free(ns);
618
  cleanup_nodelist();
619
620
621
  hs_free_all();
}

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/** Test disaster SRV computation and caching */
static void
test_disaster_srv(void *arg)
{
  uint8_t *cached_disaster_srv_one = NULL;
  uint8_t *cached_disaster_srv_two = NULL;
  uint8_t srv_one[DIGEST256_LEN] = {0};
  uint8_t srv_two[DIGEST256_LEN] = {0};
  uint8_t srv_three[DIGEST256_LEN] = {0};
  uint8_t srv_four[DIGEST256_LEN] = {0};
  uint8_t srv_five[DIGEST256_LEN] = {0};

  (void) arg;

  /* Get the cached SRVs: we gonna use them later for verification */
  cached_disaster_srv_one = get_first_cached_disaster_srv();
  cached_disaster_srv_two = get_second_cached_disaster_srv();

  /* Compute some srvs */
  get_disaster_srv(1, srv_one);
  get_disaster_srv(2, srv_two);

teor's avatar
teor committed
644
  /* Check that the cached ones were updated */
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);

  /* Ask for an SRV that has already been computed */
  get_disaster_srv(2, srv_two);
  /* and check that the cache entries have not changed */
  tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);

  /* Ask for a new SRV */
  get_disaster_srv(3, srv_three);
  tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);

  /* Ask for another SRV: none of the original SRVs should now be cached */
  get_disaster_srv(4, srv_four);
  tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);

  /* Ask for yet another SRV */
  get_disaster_srv(5, srv_five);
  tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);

 done:
  ;
}

673
674
675
676
677
678
679
/** Test our HS descriptor request tracker by making various requests and
 *  checking whether they get tracked properly. */
static void
test_hid_serv_request_tracker(void *arg)
{
  (void) arg;
  time_t retval;
680
  routerstatus_t *hsdir = NULL, *hsdir2 = NULL, *hsdir3 = NULL;
681
682
683
684
685
686
  time_t now = approx_time();

  const char *req_key_str_first =
 "vd4zb6zesaubtrjvdqcr2w7x7lhw2up4Xnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  const char *req_key_str_second =
 "g53o7iavcd62oihswhr24u6czmqws5kpXnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
687
  const char *req_key_str_small = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
688
689
690
691
692
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

  /*************************** basic test *******************************/

  /* Get request tracker and make sure it's empty */
  strmap_t *request_tracker = get_last_hid_serv_requests();
  tt_int_op(strmap_size(request_tracker),OP_EQ, 0);

  /* Let's register a hid serv request */
  hsdir = tor_malloc_zero(sizeof(routerstatus_t));
  memset(hsdir->identity_digest, 'Z', DIGEST_LEN);
  retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
                                           now, 1);
  tt_int_op(retval, OP_EQ, now);
  tt_int_op(strmap_size(request_tracker),OP_EQ, 1);

  /* Let's lookup a non-existent hidserv request */
  retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_second,
                                           now+1, 0);
  tt_int_op(retval, OP_EQ, 0);
  tt_int_op(strmap_size(request_tracker),OP_EQ, 1);

  /* Let's lookup a real hidserv request */
  retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
                                           now+2, 0);
  tt_int_op(retval, OP_EQ, now); /* we got it */
  tt_int_op(strmap_size(request_tracker),OP_EQ, 1);

  /**********************************************************************/

  /* Let's add another request for the same HS but on a different HSDir. */
  hsdir2 = tor_malloc_zero(sizeof(routerstatus_t));
719
  memset(hsdir2->identity_digest, 2, DIGEST_LEN);
720
721
722
723
724
725
726
727
728
729
730
731
732
  retval = hs_lookup_last_hid_serv_request(hsdir2, req_key_str_first,
                                           now+3, 1);
  tt_int_op(retval, OP_EQ, now+3);
  tt_int_op(strmap_size(request_tracker),OP_EQ, 2);

  /* Check that we can clean the first request based on time */
  hs_clean_last_hid_serv_requests(now+3+REND_HID_SERV_DIR_REQUERY_PERIOD);
  tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  /* Check that it doesn't exist anymore */
  retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
                                           now+2, 0);
  tt_int_op(retval, OP_EQ, 0);

733
734
735
736
737
738
739
740
  /* Now let's add a smaller req key str */
  hsdir3 = tor_malloc_zero(sizeof(routerstatus_t));
  memset(hsdir3->identity_digest, 3, DIGEST_LEN);
  retval = hs_lookup_last_hid_serv_request(hsdir3, req_key_str_small,
                                           now+4, 1);
  tt_int_op(retval, OP_EQ, now+4);
  tt_int_op(strmap_size(request_tracker),OP_EQ, 2);

741
742
743
744
  /*************************** deleting entries **************************/

  /* Add another request with very short key */
  retval = hs_lookup_last_hid_serv_request(hsdir, "l",  now, 1);
745
  tt_int_op(retval, OP_EQ, now);
746
  tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
747
748
749
750
751

  /* Try deleting entries with a dummy key. Check that our previous requests
   * are still there */
  tor_capture_bugs_(1);
  hs_purge_hid_serv_from_last_hid_serv_requests("a");
752
  tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
753
754
755
756
757
758
759
760
  tor_end_capture_bugs_();

  /* Try another dummy key. Check that requests are still there */
  {
    char dummy[2000];
    memset(dummy, 'Z', 2000);
    dummy[1999] = '\x00';
    hs_purge_hid_serv_from_last_hid_serv_requests(dummy);
761
    tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
762
763
764
765
  }

  /* Another dummy key! */
  hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_second);
766
  tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
767
768
769

  /* Now actually delete a request! */
  hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_first);
770
  tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
771
772
773
774
775
776
777
778
779

  /* Purge it all! */
  hs_purge_last_hid_serv_requests();
  request_tracker = get_last_hid_serv_requests();
  tt_int_op(strmap_size(request_tracker),OP_EQ, 0);

 done:
  tor_free(hsdir);
  tor_free(hsdir2);
780
  tor_free(hsdir3);
781
782
}

783
784
785
786
static void
test_parse_extended_hostname(void *arg)
{
  (void) arg;
787
  hostname_type_t type;
788
789
790
791

  char address1[] = "fooaddress.onion";
  char address3[] = "fooaddress.exit";
  char address4[] = "www.torproject.org";
792
793
  char address5[] = "foo.abcdefghijklmnop.onion";
  char address6[] = "foo.bar.abcdefghijklmnop.onion";
794
795
  char address7[] = ".abcdefghijklmnop.onion";
  char address8[] =
796
    "www.25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion";
797
798
  char address9[] =
    "www.15njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion";
799
800
  char address10[] =
    "15njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid7jdl.onion";
801

802
803
804
805
806
807
808
809
810
  tt_assert(!parse_extended_hostname(address1, &type));
  tt_int_op(type, OP_EQ, BAD_HOSTNAME);

  tt_assert(parse_extended_hostname(address3, &type));
  tt_int_op(type, OP_EQ, EXIT_HOSTNAME);

  tt_assert(parse_extended_hostname(address4, &type));
  tt_int_op(type, OP_EQ, NORMAL_HOSTNAME);

811
812
813
814
815
816
817
818
  tt_assert(parse_extended_hostname(address5, &type));
  tt_int_op(type, OP_EQ, ONION_V2_HOSTNAME);
  tt_str_op(address5, OP_EQ, "abcdefghijklmnop");

  tt_assert(parse_extended_hostname(address6, &type));
  tt_int_op(type, OP_EQ, ONION_V2_HOSTNAME);
  tt_str_op(address6, OP_EQ, "abcdefghijklmnop");

819
820
821
822
823
  tt_assert(!parse_extended_hostname(address7, &type));
  tt_int_op(type, OP_EQ, BAD_HOSTNAME);

  tt_assert(parse_extended_hostname(address8, &type));
  tt_int_op(type, OP_EQ, ONION_V3_HOSTNAME);
824
  tt_str_op(address8, OP_EQ,
825
            "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
826

827
828
  /* Invalid v3 address. */
  tt_assert(!parse_extended_hostname(address9, &type));
829
830
831
832
833
  tt_int_op(type, OP_EQ, BAD_HOSTNAME);

  /* Invalid v3 address: too long */
  tt_assert(!parse_extended_hostname(address10, &type));
  tt_int_op(type, OP_EQ, BAD_HOSTNAME);
834

835
836
837
 done: ;
}

838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
static void
test_time_between_tp_and_srv(void *arg)
{
  int ret;
  networkstatus_t ns;
  (void) arg;

  /* This function should be returning true where "^" are:
   *
   *    +------------------------------------------------------------------+
   *    |                                                                  |
   *    | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *    | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *    |                                                                  |
   *    |  $==========|-----------$===========|-----------$===========|    |
   *    |             ^^^^^^^^^^^^            ^^^^^^^^^^^^                 |
   *    |                                                                  |
   *    +------------------------------------------------------------------+
   */

  ret = parse_rfc1123_time("Sat, 26 Oct 1985 00:00:00 UTC", &ns.valid_after);
  tt_int_op(ret, OP_EQ, 0);
860
861
  ret = parse_rfc1123_time("Sat, 26 Oct 1985 01:00:00 UTC", &ns.fresh_until);
  tt_int_op(ret, OP_EQ, 0);
862
  dirauth_sched_recalculate_timing(get_options(), ns.valid_after);
863
  ret = hs_in_period_between_tp_and_srv(&ns, 0);
864
865
866
867
  tt_int_op(ret, OP_EQ, 0);

  ret = parse_rfc1123_time("Sat, 26 Oct 1985 11:00:00 UTC", &ns.valid_after);
  tt_int_op(ret, OP_EQ, 0);
868
869
  ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.fresh_until);
  tt_int_op(ret, OP_EQ, 0);
870
  dirauth_sched_recalculate_timing(get_options(), ns.valid_after);
871
  ret = hs_in_period_between_tp_and_srv(&ns, 0);
872
873
874
875
  tt_int_op(ret, OP_EQ, 0);

  ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.valid_after);
  tt_int_op(ret, OP_EQ, 0);
876
877
  ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC", &ns.fresh_until);
  tt_int_op(ret, OP_EQ, 0);
878
  dirauth_sched_recalculate_timing(get_options(), ns.valid_after);
879
  ret = hs_in_period_between_tp_and_srv(&ns, 0);
880
881
882
883
  tt_int_op(ret, OP_EQ, 1);

  ret = parse_rfc1123_time("Sat, 26 Oct 1985 23:00:00 UTC", &ns.valid_after);
  tt_int_op(ret, OP_EQ, 0);
884
885
  ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.fresh_until);
  tt_int_op(ret, OP_EQ, 0);
886
  dirauth_sched_recalculate_timing(get_options(), ns.valid_after);
887
  ret = hs_in_period_between_tp_and_srv(&ns, 0);
888
889
  tt_int_op(ret, OP_EQ, 1);

890
891
892
  ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.valid_after);
  tt_int_op(ret, OP_EQ, 0);
  ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC", &ns.fresh_until);
893
  tt_int_op(ret, OP_EQ, 0);
894
  dirauth_sched_recalculate_timing(get_options(), ns.valid_after);
895
  ret = hs_in_period_between_tp_and_srv(&ns, 0);
896
897
898
899
900
901
  tt_int_op(ret, OP_EQ, 0);

 done:
  ;
}

902
903
904
905
906
907
908
909
910
911
912
913
/************ Reachability Test (it is huge) ****************/

/* Simulate different consensus for client and service. Used by the
 * reachability test. The SRV and responsible HSDir list are used by all
 * reachability tests so make them common to simplify setup and teardown. */
static networkstatus_t *mock_service_ns = NULL;
static networkstatus_t *mock_client_ns = NULL;
static sr_srv_t current_srv, previous_srv;
static smartlist_t *service_responsible_hsdirs = NULL;
static smartlist_t *client_responsible_hsdirs = NULL;

static networkstatus_t *
914
915
mock_networkstatus_get_reasonably_live_consensus_service(time_t now,
                                                         int flavor)
916
917
{
  (void) now;
918
  (void) flavor;
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933

  if (mock_service_ns) {
    return mock_service_ns;
  }

  mock_service_ns = tor_malloc_zero(sizeof(networkstatus_t));
  mock_service_ns->routerstatus_list = smartlist_new();
  mock_service_ns->type = NS_TYPE_CONSENSUS;

  return mock_service_ns;
}

static networkstatus_t *
mock_networkstatus_get_latest_consensus_service(void)
{
934
  return mock_networkstatus_get_reasonably_live_consensus_service(0, 0);
935
936
937
}

static networkstatus_t *
938
mock_networkstatus_get_reasonably_live_consensus_client(time_t now, int flavor)
939
940
{
  (void) now;
941
  (void) flavor;
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956

  if (mock_client_ns) {
    return mock_client_ns;
  }

  mock_client_ns = tor_malloc_zero(sizeof(networkstatus_t));
  mock_client_ns->routerstatus_list = smartlist_new();
  mock_client_ns->type = NS_TYPE_CONSENSUS;

  return mock_client_ns;
}

static networkstatus_t *
mock_networkstatus_get_latest_consensus_client(void)
{
957
  return mock_networkstatus_get_reasonably_live_consensus_client(0, 0);
958
959
960
961
962
963
964
965
966
967
968
969
970
971
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
}

/* Mock function because we are not trying to test the close circuit that does
 * an awful lot of checks on the circuit object. */
static void
mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
                            const char *file)
{
  (void) circ;
  (void) reason;
  (void) line;
  (void) file;
  return;
}

/* Initialize a big HSDir V3 hash ring. */
static void
helper_initialize_big_hash_ring(networkstatus_t *ns)
{
  int ret;

  /* Generate 250 hsdirs! :) */
  for (int counter = 1 ; counter < 251 ; counter++) {
    /* Let's generate random nickname for each hsdir... */
    char nickname_binary[8];
    char nickname_str[13] = {0};
    crypto_rand(nickname_binary, sizeof(nickname_binary));
    ret = base64_encode(nickname_str, sizeof(nickname_str),
                        nickname_binary, sizeof(nickname_binary), 0);
    tt_int_op(ret, OP_EQ, 12);
    helper_add_hsdir_to_networkstatus(ns, counter, nickname_str, 1);
  }

  /* Make sure we have 200 hsdirs in our list */
  tt_int_op(smartlist_len(ns->routerstatus_list), OP_EQ, 250);

 done:
  ;
}

/** Initialize service and publish its descriptor as needed. Return the newly
 *  allocated service object to the caller. */
static hs_service_t *
helper_init_service(time_t now)
{
  int retval;
  hs_service_t *service = hs_service_new(get_options());
  tt_assert(service);
  service->config.version = HS_VERSION_THREE;
  ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  ed25519_public_key_generate(&service->keys.identity_pk,
                              &service->keys.identity_sk);
  /* Register service to global map. */
  retval = register_service(get_hs_service_map(), service);
  tt_int_op(retval, OP_EQ, 0);

  /* Initialize service descriptor */
  build_all_descriptors(now);
  tt_assert(service->desc_current);
  tt_assert(service->desc_next);

 done:
  return service;
}

/* Helper function to set the RFC 1123 time string into t. */
static void
1025
set_consensus_times(const char *timestr, time_t *t)
1026
{
1027
  tt_assert(timestr);
1028
1029
  tt_assert(t);

1030
  int ret = parse_rfc1123_time(timestr, t);
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
  tt_int_op(ret, OP_EQ, 0);

 done:
  return;
}

/* Helper function to cleanup the mock consensus (client and service) */
static void
cleanup_mock_ns(void)
{
  if (mock_service_ns) {
    SMARTLIST_FOREACH(mock_service_ns->routerstatus_list,
                      routerstatus_t *, rs, routerstatus_free(rs));
    smartlist_clear(mock_service_ns->routerstatus_list);
    mock_service_ns->sr_info.current_srv = NULL;
    mock_service_ns->sr_info.previous_srv = NULL;
    networkstatus_vote_free(mock_service_ns);
    mock_service_ns = NULL;
  }

  if (mock_client_ns) {
    SMARTLIST_FOREACH(mock_client_ns->routerstatus_list,
                      routerstatus_t *, rs, routerstatus_free(rs));
    smartlist_clear(mock_client_ns->routerstatus_list);
    mock_client_ns->sr_info.current_srv = NULL;
    mock_client_ns->sr_info.previous_srv = NULL;
    networkstatus_vote_free(mock_client_ns);
    mock_client_ns = NULL;
  }
}

/* Helper function to setup a reachability test. Once called, the
 * cleanup_reachability_test MUST be called at the end. */
static void
setup_reachability_test(void)
{
  MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  MOCK(get_or_state, get_or_state_replacement);

  hs_init();

  /* Baseline to start with. */
  memset(&current_srv, 0, sizeof(current_srv));
  memset(&previous_srv, 1, sizeof(previous_srv));

  /* Initialize the consensuses. */
  mock_networkstatus_get_latest_consensus_service();
  mock_networkstatus_get_latest_consensus_client();

  service_responsible_hsdirs = smartlist_new();
  client_responsible_hsdirs = smartlist_new();
}

/* Helper function to cleanup a reachability test initial setup. */
static void
cleanup_reachability_test(void)
{
  smartlist_free(service_responsible_hsdirs);
  service_responsible_hsdirs = NULL;
  smartlist_free(client_responsible_hsdirs);
  client_responsible_hsdirs = NULL;
  hs_free_all();
  cleanup_mock_ns();
  UNMOCK(get_or_state);
  UNMOCK(circuit_mark_for_close_);
}

/* A reachability test always check if the resulting service and client
 * responsible HSDir for the given parameters are equal.
 *
 * Return true iff the same exact nodes are in both list. */
static int
are_responsible_hsdirs_equal(void)
{
  int count = 0;
  tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);
1107
  tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156

  SMARTLIST_FOREACH_BEGIN(client_responsible_hsdirs,
                          const routerstatus_t *, c_rs) {
    SMARTLIST_FOREACH_BEGIN(service_responsible_hsdirs,
                            const routerstatus_t *, s_rs) {
      if (tor_memeq(c_rs->identity_digest, s_rs->identity_digest,
                    DIGEST_LEN)) {
        count++;
        break;
      }
    } SMARTLIST_FOREACH_END(s_rs);
  } SMARTLIST_FOREACH_END(c_rs);

 done:
  return (count == 6);
}

/* Tor doesn't use such a function to get the previous HSDir, it is only used
 * in node_set_hsdir_index(). We need it here so we can test the reachability
 * scenario 6 that requires the previous time period to compute the list of
 * responsible HSDir because of the client state timing. */
static uint64_t
get_previous_time_period(time_t now)
{
  return hs_get_time_period_num(now) - 1;
}

/* Configuration of a reachability test scenario. */
typedef struct reachability_cfg_t {
  /* Consensus timings to be set. They have to be compliant with
   * RFC 1123 time format. */
  const char *service_valid_after;
  const char *service_valid_until;
  const char *client_valid_after;
  const char *client_valid_until;

  /* SRVs that the service and client should use. */
  sr_srv_t *service_current_srv;
  sr_srv_t *service_previous_srv;
  sr_srv_t *client_current_srv;
  sr_srv_t *client_previous_srv;

  /* A time period function for the service to use for this scenario. For a
   * successful reachability test, the client always use the current time
   * period thus why no client function. */
  uint64_t (*service_time_period_fn)(time_t);

  /* Is the client and service expected to be in a new time period. After
   * setting the consensus time, the reachability test checks
1157
1158
   * hs_in_period_between_tp_and_srv() and test the returned value against
   * this. */
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
  unsigned int service_in_new_tp;
  unsigned int client_in_new_tp;

  /* Some scenario requires a hint that the client, because of its consensus
   * time, will request the "next" service descriptor so this indicates if it
   * is the case or not. */
  unsigned int client_fetch_next_desc;
} reachability_cfg_t;

/* Some defines to help with semantic while reading a configuration below. */
#define NOT_IN_NEW_TP 0
#define IN_NEW_TP 1
#define DONT_NEED_NEXT_DESC 0
#define NEED_NEXT_DESC 1

static reachability_cfg_t reachability_scenarios[] = {
  /* Scenario 1
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|-----------$===========|    |
   *  |              ^ ^                                                 |
   *  |              S C                                                 |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 13:00 and client to 15:00,
   *  both are after TP#1 thus have access to SRV#1. Service and client should
   *  be using TP#1.
   */

  { "Sat, 26 Oct 1985 13:00:00 UTC", /* Service valid_after */
    "Sat, 26 Oct 1985 14:00:00 UTC", /* Service valid_until */
    "Sat, 26 Oct 1985 15:00:00 UTC", /* Client valid_after */
    "Sat, 26 Oct 1985 16:00:00 UTC", /* Client valid_until. */
    &current_srv, NULL, /* Service current and previous SRV */
    &current_srv, NULL, /* Client current and previous SRV */
    hs_get_time_period_num, /* Service time period function. */
    IN_NEW_TP, /* Is service in new TP? */
    IN_NEW_TP, /* Is client in new TP? */
    NEED_NEXT_DESC },

  /* Scenario 2
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|-----------$===========|    |
   *  |                        ^ ^                                       |
   *  |                        S C                                       |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 23:00 and client to 01:00,
   *  which makes the client after the SRV#2 and the service just before. The
   *  service should only be using TP#1. The client should be using TP#1.
   */

  { "Sat, 26 Oct 1985 23:00:00 UTC", /* Service valid_after */
    "Sat, 27 Oct 1985 00:00:00 UTC", /* Service valid_until */
    "Sat, 27 Oct 1985 01:00:00 UTC", /* Client valid_after */
    "Sat, 27 Oct 1985 02:00:00 UTC", /* Client valid_until. */
    &previous_srv, NULL, /* Service current and previous SRV */
    &current_srv, &previous_srv, /* Client current and previous SRV */
    hs_get_time_period_num, /* Service time period function. */
    IN_NEW_TP, /* Is service in new TP? */
    NOT_IN_NEW_TP, /* Is client in new TP? */
    NEED_NEXT_DESC },

  /* Scenario 3
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|----------$===========|     |
   *  |                            ^ ^                                   |
   *  |                            S C                                   |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 03:00 and client to 05:00,
   *  which makes both after SRV#2. The service should be using TP#1 as its
   *  current time period. The client should be using TP#1.
   */

  { "Sat, 27 Oct 1985 03:00:00 UTC", /* Service valid_after */
    "Sat, 27 Oct 1985 04:00:00 UTC", /* Service valid_until */
    "Sat, 27 Oct 1985 05:00:00 UTC", /* Client valid_after */
    "Sat, 27 Oct 1985 06:00:00 UTC", /* Client valid_until. */
    &current_srv, &previous_srv, /* Service current and previous SRV */
    &current_srv, &previous_srv, /* Client current and previous SRV */
    hs_get_time_period_num, /* Service time period function. */
    NOT_IN_NEW_TP, /* Is service in new TP? */
    NOT_IN_NEW_TP, /* Is client in new TP? */
    DONT_NEED_NEXT_DESC },

  /* Scenario 4
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|-----------$===========|    |
   *  |                                    ^ ^                           |
   *  |                                    S C                           |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 11:00 and client to 13:00,
   *  which makes the service before TP#2 and the client just after. The
   *  service should be using TP#1 as its current time period and TP#2 as the
   *  next. The client should be using TP#2 time period.
   */

  { "Sat, 27 Oct 1985 11:00:00 UTC", /* Service valid_after */
    "Sat, 27 Oct 1985 12:00:00 UTC", /* Service valid_until */
    "Sat, 27 Oct 1985 13:00:00 UTC", /* Client valid_after */
    "Sat, 27 Oct 1985 14:00:00 UTC", /* Client valid_until. */
    &current_srv, &previous_srv, /* Service current and previous SRV */
    &current_srv, &previous_srv, /* Client current and previous SRV */
    hs_get_next_time_period_num, /* Service time period function. */
    NOT_IN_NEW_TP, /* Is service in new TP? */
    IN_NEW_TP, /* Is client in new TP? */
    NEED_NEXT_DESC },

  /* Scenario 5
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|-----------$===========|    |
   *  |                        ^ ^                                       |
   *  |                        C S                                       |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 01:00 and client to 23:00,
   *  which makes the service after SRV#2 and the client just before. The
   *  service should be using TP#1 as its current time period and TP#2 as the
   *  next. The client should be using TP#1 time period.
   */

  { "Sat, 27 Oct 1985 01:00:00 UTC", /* Service valid_after */
    "Sat, 27 Oct 1985 02:00:00 UTC", /* Service valid_until */
    "Sat, 26 Oct 1985 23:00:00 UTC", /* Client valid_after */
    "Sat, 27 Oct 1985 00:00:00 UTC", /* Client valid_until. */
    &current_srv, &previous_srv, /* Service current and previous SRV */
    &previous_srv, NULL, /* Client current and previous SRV */
    hs_get_time_period_num, /* Service time period function. */
    NOT_IN_NEW_TP, /* Is service in new TP? */
    IN_NEW_TP, /* Is client in new TP? */
    DONT_NEED_NEXT_DESC },

  /* Scenario 6
   *
   *  +------------------------------------------------------------------+
   *  |                                                                  |
   *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
   *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
   *  |                                                                  |
   *  |  $==========|-----------$===========|-----------$===========|    |
   *  |                                    ^ ^                           |
   *  |                                    C S                           |
   *  +------------------------------------------------------------------+
   *
   *  S: Service, C: Client
   *
   *  Service consensus valid_after time is set to 13:00 and client to 11:00,
   *  which makes the service outside after TP#2 and the client just before.
   *  The service should be using TP#1 as its current time period and TP#2 as
   *  its next. The client should be using TP#1 time period.
   */

  { "Sat, 27 Oct 1985 13:00:00 UTC", /* Service valid_after */
    "Sat, 27 Oct 1985 14:00:00 UTC", /* Service valid_until */
    "Sat, 27 Oct 1985 11:00:00 UTC", /* Client valid_after */
    "Sat, 27 Oct 1985 12:00:00 UTC", /* Client valid_until. */
    &current_srv, &previous_srv, /* Service current and previous SRV */
    &current_srv, &previous_srv, /* Client current and previous SRV */
    get_previous_time_period, /* Service time period function. */
    IN_NEW_TP, /* Is service in new TP? */
    NOT_IN_NEW_TP, /* Is client in new TP? */
    DONT_NEED_NEXT_DESC },

  /* End marker. */
  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0}
};

/* Run a single reachability scenario. num_scenario is the corresponding
 * scenario number from the documentation. It is used to log it in case of
 * failure so we know which scenario fails. */
static int
run_reachability_scenario(const reachability_cfg_t *cfg, int num_scenario)
{
  int ret = -1;
  hs_service_t *service;
  uint64_t service_tp, client_tp;
  ed25519_public_key_t service_blinded_pk, client_blinded_pk;

  setup_reachability_test();

  tt_assert(cfg);

  /* Set service consensus time. */
  set_consensus_times(cfg->service_valid_after,
                      &mock_service_ns->valid_after);
  set_consensus_times(cfg->service_valid_until,
                      &mock_service_ns->valid_until);
  set_consensus_times(cfg->service_valid_until,
                      &mock_service_ns->fresh_until);
1384
  dirauth_sched_recalculate_timing(get_options(),
1385
                                     mock_service_ns->valid_after);
1386
1387
1388
1389
  /* Check that service is in the right time period point */
  tt_int_op(hs_in_period_between_tp_and_srv(mock_service_ns, 0), OP_EQ,
            cfg->service_in_new_tp);

1390
1391
1392
1393
1394
1395
1396
  /* Set client consensus time. */
  set_consensus_times(cfg->client_valid_after,
                      &mock_client_ns->valid_after);
  set_consensus_times(cfg->client_valid_until,
                      &mock_client_ns->valid_until);
  set_consensus_times(cfg->client_valid_until,
                      &mock_client_ns->fresh_until);
1397
  dirauth_sched_recalculate_timing(get_options(),
1398
                                     mock_client_ns->valid_after);
1399
  /* Check that client is in the right time period point */
1400
  tt_int_op(hs_in_period_between_tp_and_srv(mock_client_ns, 0), OP_EQ,
1401
1402
1403
1404
1405
1406
1407
1408
1409
            cfg->client_in_new_tp);

  /* Set the SRVs for this scenario. */
  mock_client_ns->sr_info.current_srv = cfg->client_current_srv;
  mock_client_ns->sr_info.previous_srv = cfg->client_previous_srv;
  mock_service_ns->sr_info.current_srv = cfg->service_current_srv;
  mock_service_ns->sr_info.previous_srv = cfg->service_previous_srv;

  /* Initialize a service to get keys. */
1410
1411
  update_approx_time(mock_service_ns->valid_after);
  service = helper_init_service(mock_service_ns->valid_after+1);
1412
1413
1414
1415
1416

  /*
   * === Client setup ===
   */

1417
1418
  MOCK(networkstatus_get_reasonably_live_consensus,
       mock_networkstatus_get_reasonably_live_consensus_client);
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
  MOCK(networkstatus_get_latest_consensus,
       mock_networkstatus_get_latest_consensus_client);

  /* Make networkstatus_is_live() happy. */
  update_approx_time(mock_client_ns->valid_after);
  /* Initialize a big hashring for this consensus with the hsdir index set. */
  helper_initialize_big_hash_ring(mock_client_ns);

  /* Client ONLY use the current time period. This is the whole point of these
   * reachability test that is to make sure the client can always reach the
   * service using only its current time period. */
  client_tp = hs_get_time_period_num(0);

  hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
                          client_tp, &client_blinded_pk);
  hs_get_responsible_hsdirs(&client_blinded_pk, client_tp, 0, 1,
                            client_responsible_hsdirs);
  /* Cleanup the nodelist so we can let the service computes its own set of
   * node with its own hashring. */
  cleanup_nodelist();
  tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);

  UNMOCK(networkstatus_get_latest_consensus);
1442
  UNMOCK(networkstatus_get_reasonably_live_consensus);
1443
1444
1445
1446
1447

  /*
   * === Service setup ===
   */

1448
1449
  MOCK(networkstatus_get_reasonably_live_consensus,
       mock_networkstatus_get_reasonably_live_consensus_service);
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
  MOCK(networkstatus_get_latest_consensus,
       mock_networkstatus_get_latest_consensus_service);

  /* Make networkstatus_is_live() happy. */
  update_approx_time(mock_service_ns->valid_after);
  /* Initialize a big hashring for this consensus with the hsdir index set. */
  helper_initialize_big_hash_ring(mock_service_ns);

  service_tp = cfg->service_time_period_fn(0);

  hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
                          service_tp, &service_blinded_pk);

  /* A service builds two lists of responsible HSDir, for the current and the
   * next descriptor. Depending on the scenario, the client timing indicate if
   * it is fetching the current or the next descriptor so we use the
   * "client_fetch_next_desc" to know which one the client is trying to get to
   * confirm that the service computes the same hashring for the same blinded
   * key and service time period function. */
  hs_get_responsible_hsdirs(&service_blinded_pk, service_tp,
                            cfg->client_fetch_next_desc, 0,
                            service_responsible_hsdirs);
  cleanup_nodelist();
1473
  tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
1474
1475

  UNMOCK(networkstatus_get_latest_consensus);
1476
  UNMOCK(networkstatus_get_reasonably_live_consensus);
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513

  /* Some testing of the values we just got from the client and service. */
  tt_mem_op(&client_blinded_pk, OP_EQ, &service_blinded_pk,
            ED25519_PUBKEY_LEN);
  tt_int_op(are_responsible_hsdirs_equal(), OP_EQ, 1);

  /* Everything went well. */
  ret = 0;

 done:
  cleanup_reachability_test();
  if (ret == -1) {
    /* Do this so we can know which scenario failed. */
    char msg[32];
    tor_snprintf(msg, sizeof(msg), "Scenario %d failed", num_scenario);
    tt_fail_msg(msg);
  }
  return ret;
}

static void
test_reachability(void *arg)
{
  (void) arg;

  /* NOTE: An important axiom to understand here is that SRV#N must only be
   * used with TP#N value. For example, SRV#2 with TP#1 should NEVER be used
   * together. The HSDir index computation is based on this axiom.*/

  for (int i = 0; reachability_scenarios[i].service_valid_after; ++i) {
    int ret = run_reachability_scenario(&reachability_scenarios[i], i + 1);
    if (ret < 0) {
      return;
    }
  }
}

1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
/** Pick an HSDir for service with <b>onion_identity_pk</b> as a client. Put
 *  its identity digest in <b>hsdir_digest_out</b>. */
static void
helper_client_pick_hsdir(const ed25519_public_key_t *onion_identity_pk,
                        char *hsdir_digest_out)
{
  tt_assert(onion_identity_pk);

  routerstatus_t *client_hsdir = pick_hsdir_v3(onion_identity_pk);
  tt_assert(client_hsdir);
  digest_to_base64(hsdir_digest_out, client_hsdir->identity_digest);

 done:
  ;
}

1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
static void
test_hs_indexes(void *arg)
{
  int ret;
  uint64_t period_num = 42;
  ed25519_public_key_t pubkey;

  (void) arg;

  /* Build the hs_index */
  {
    uint8_t hs_index[DIGEST256_LEN];
    const char *b32_test_vector =
      "37e5cbbd56a22823714f18f1623ece5983a0d64c78495a8cfab854245e5f9a8a";
    char test_vector[DIGEST256_LEN];
    ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
                        strlen(b32_test_vector));
    tt_int_op(ret, OP_EQ, sizeof(test_vector));
    /* Our test vector uses a public key set to 32 bytes of \x42. */
    memset(&pubkey, '\x42', sizeof(pubkey));
    hs_build_hs_index(1, &pubkey, period_num, hs_index);
    tt_mem_op(hs_index, OP_EQ, test_vector, sizeof(hs_index));
  }

  /* Build the hsdir_index */
  {
    uint8_t srv[DIGEST256_LEN];
    uint8_t hsdir_index[DIGEST256_LEN];
    const char *b32_test_vector =
      "db475361014a09965e7e5e4d4a25b8f8d4b8f16cb1d8a7e95eed50249cc1a2d5";
    char test_vector[DIGEST256_LEN];
    ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
                        strlen(b32_test_vector));
    tt_int_op(ret, OP_EQ, sizeof(test_vector));
    /* Our test vector uses a public key set to 32 bytes of \x42. */
    memset(&pubkey, '\x42', sizeof(pubkey));
    memset(srv, '\x43', sizeof(srv));
    hs_build_hsdir_index(&pubkey, srv, period_num, hsdir_index);
    tt_mem_op(hsdir_index, OP_EQ, test_vector, sizeof(hsdir_index));
  }

 done:
  ;
}

1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
#define EARLY_IN_SRV_TO_TP 0
#define LATE_IN_SRV_TO_TP 1
#define EARLY_IN_TP_TO_SRV 2
#define LATE_IN_TP_TO_SRV 3

/** Set the consensus and system time based on <b>position</b>. See the
 *    following diagram for details:
 *
 *  +------------------------------------------------------------------+
 *  |                                                                  |
 *  | 00:00      12:00       00:00       12:00       00:00       12:00 |
 *  | SRV#1      TP#1        SRV#2       TP#2        SRV#3       TP#3  |
 *  |                                                                  |
 *  |  $==========|-----------$===========|----------$===========|     |
 *  |                                                                  |
 *  |                                                                  |
 *  +------------------------------------------------------------------+
 */
1593
static time_t
1594
helper_set_consensus_and_system_time(networkstatus_t *ns, int position)
1595
{
1596
  time_t real_time = 0;
1597
1598
1599
1600

  /* The period between SRV#N and TP#N is from 00:00 to 12:00 UTC. Consensus
   * valid_after is what matters here, the rest is just to specify the voting
   * period correctly. */
1601
  if (position == LATE_IN_SRV_TO_TP) {
1602
1603
1604
    parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC", &ns->valid_after);
    parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC", &ns->fresh_until);
    parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->valid_until);
1605
  } else if (position == EARLY_IN_TP_TO_SRV) {
1606
1607
1608
    parse_rfc1123_time("Wed, 13 Apr 2016 13:00:00 UTC", &ns->valid_after);
    parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->fresh_until);
    parse_rfc1123_time("Wed, 13 Apr 2016 16:00:00 UTC", &ns->valid_until);
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
  } else if (position == LATE_IN_TP_TO_SRV) {
    parse_rfc1123_time("Wed, 13 Apr 2016 23:00:00 UTC", &ns->valid_after);
    parse_rfc1123_time("Wed, 14 Apr 2016 00:00:00 UTC", &ns->fresh_until);
    parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->valid_until);
  } else if (position == EARLY_IN_SRV_TO_TP) {
    parse_rfc1123_time("Wed, 14 Apr 2016 01:00:00 UTC", &ns->valid_after);
    parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->fresh_until);
    parse_rfc1123_time("Wed, 14 Apr 2016 04:00:00 UTC", &ns->valid_until);
  } else {
    tt_assert(0);
1619
  }
1620
  dirauth_sched_recalculate_timing(get_options(), ns->valid_after);
1621
1622
1623
1624

  /* Set system time: pretend to be just 2 minutes before consensus expiry */
  real_time = ns->valid_until - 120;
  update_approx_time(real_time);
1625
1626

 done: