test_dir_handle_get.c 76.6 KB
Newer Older
1
2
/* Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3
 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4
5
6
7
/* See LICENSE for licensing information */

#define RENDCOMMON_PRIVATE
#define GEOIP_PRIVATE
8
#define CONNECTION_PRIVATE
9
10
#define CONFIG_PRIVATE
#define RENDCACHE_PRIVATE
11
12
13

#include "or.h"
#include "config.h"
14
#include "connection.h"
15
#include "consdiffmgr.h"
16
17
#include "directory.h"
#include "test.h"
18
#include "compress.h"
19
20
21
22
23
24
25
26
27
28
29
30
#include "connection.h"
#include "rendcommon.h"
#include "rendcache.h"
#include "router.h"
#include "routerlist.h"
#include "rend_test_helpers.h"
#include "microdesc.h"
#include "test_helpers.h"
#include "nodelist.h"
#include "entrynodes.h"
#include "routerparse.h"
#include "networkstatus.h"
31
#include "proto_http.h"
32
33
34
#include "geoip.h"
#include "dirserv.h"
#include "dirvote.h"
35
#include "log_test_helpers.h"
36
37
38
39
40
41

#ifdef _WIN32
/* For mkdir() */
#include <direct.h>
#else
#include <dirent.h>
42
#endif /* defined(_WIN32) */
43

44
#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
45
46
47
48
DISABLE_GCC_WARNING(overlength-strings)
/* We allow huge string constants in the unit tests, but not in the code
 * at large. */
#endif
49
#include "vote_descriptors.inc"
50
#ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
51
52
ENABLE_GCC_WARNING(overlength-strings)
#endif
53
54
55
56
57
58

#define NS_MODULE dir_handle_get

#define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
#define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
#define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
59
#define TOO_OLD "HTTP/1.0 404 Consensus is too old\r\n\r\n"
60
61
62
#define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
  "Consensus not signed by sufficient number of requested authorities\r\n\r\n"

63
64
65
66
static dir_connection_t *
new_dir_conn(void)
{
  dir_connection_t *conn = dir_connection_new(AF_INET);
67
  tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
68
  TO_CONN(conn)->address = tor_strdup("127.0.0.1");
69
70
  return conn;
}
71
72
73
74
75
76
77
78
79
80

static void
test_dir_handle_get_bad_request(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

81
  conn = new_dir_conn();
82
83
84
85
86
87
88
89
90
  tt_int_op(directory_handle_command_get(conn, "", NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(header, OP_EQ, BAD_REQUEST);

  done:
    UNMOCK(connection_write_to_buf_impl_);
91
    connection_free_minimal(TO_CONN(conn));
92
93
94
95
96
97
98
99
100
101
102
103
    tor_free(header);
}

static void
test_dir_handle_get_v1_command_not_found(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

104
  conn = new_dir_conn();
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

  // no frontpage configured
  tt_ptr_op(get_dirportfrontpage(), OP_EQ, NULL);

  /* V1 path */
  tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
            OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(connection_write_to_buf_impl_);
120
    connection_free_minimal(TO_CONN(conn));
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    tor_free(header);
}

static const char*
mock_get_dirportfrontpage(void)
{
  return "HELLO FROM FRONTPAGE";
}

static void
test_dir_handle_get_v1_command(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0, body_len = 0;
  const char *exp_body = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  MOCK(get_dirportfrontpage, mock_get_dirportfrontpage);

  exp_body = get_dirportfrontpage();
  body_len = strlen(exp_body);

146
  conn = new_dir_conn();
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
            OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, body_len+1, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/html\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  tt_assert(strstr(header, "Content-Length: 20\r\n"));

  tt_int_op(body_used, OP_EQ, strlen(body));
  tt_str_op(body, OP_EQ, exp_body);

  done:
    UNMOCK(connection_write_to_buf_impl_);
    UNMOCK(get_dirportfrontpage);
167
    connection_free_minimal(TO_CONN(conn));
168
169
170
171
172
173
174
175
176
177
178
179
180
    tor_free(header);
    tor_free(body);
}

static void
test_dir_handle_get_not_found(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

181
  conn = new_dir_conn();
182
183
184
185
186
187
188
189
190
191
192

  /* Unrecognized path */
  tt_int_op(directory_handle_command_get(conn, GET("/anything"), NULL, 0),
            OP_EQ, 0);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(connection_write_to_buf_impl_);
193
    connection_free_minimal(TO_CONN(conn));
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    tor_free(header);
}

static void
test_dir_handle_get_robots_txt(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

208
  conn = new_dir_conn();
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

  tt_int_op(directory_handle_command_get(conn, GET("/tor/robots.txt"),
                                         NULL, 0), OP_EQ, 0);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, 29, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  tt_assert(strstr(header, "Content-Length: 28\r\n"));

  tt_int_op(body_used, OP_EQ, strlen(body));
  tt_str_op(body, OP_EQ, "User-agent: *\r\nDisallow: /\r\n");

  done:
    UNMOCK(connection_write_to_buf_impl_);
228
    connection_free_minimal(TO_CONN(conn));
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    tor_free(header);
    tor_free(body);
}

#define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
static void
test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

243
  conn = new_dir_conn();
244
245
246
247
248
249
250
251
252
253
254
255
256

  // connection is not encrypted
  tt_assert(!connection_dir_is_encrypted(conn))

  tt_int_op(directory_handle_command_get(conn, RENDEZVOUS2_GET(), NULL, 0),
            OP_EQ, 0);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(connection_write_to_buf_impl_);
257
    connection_free_minimal(TO_CONN(conn));
258
259
260
261
262
263
264
265
266
267
268
269
    tor_free(header);
}

static void
test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
  void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
270
  conn = new_dir_conn();
271
272
273
274
275
276
277
278
279
280
281
282
283
284

  // connection is encrypted
  TO_CONN(conn)->linked = 1;
  tt_assert(connection_dir_is_encrypted(conn));

  tt_int_op(directory_handle_command_get(conn,
            RENDEZVOUS2_GET("invalid-desc-id"), NULL, 0), OP_EQ, 0);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(header, OP_EQ, BAD_REQUEST);

  done:
    UNMOCK(connection_write_to_buf_impl_);
285
    connection_free_minimal(TO_CONN(conn));
286
287
288
289
290
291
292
293
294
295
296
    tor_free(header);
}

static void
test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
297
  conn = new_dir_conn();
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

  // connection is encrypted
  TO_CONN(conn)->linked = 1;
  tt_assert(connection_dir_is_encrypted(conn));

  //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
  //case to happen. This test is the same as
  //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
  //We should refactor to remove the case from the switch.

  const char *req = RENDEZVOUS2_GET("1bababababababababababababababab");
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(header, OP_EQ, BAD_REQUEST);

  done:
    UNMOCK(connection_write_to_buf_impl_);
318
    connection_free_minimal(TO_CONN(conn));
319
320
321
322
323
324
325
326
327
328
329
    tor_free(header);
}

static void
test_dir_handle_get_rendezvous2_not_found(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
330
  conn = new_dir_conn();
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

  rend_cache_init();

  // connection is encrypted
  TO_CONN(conn)->linked = 1;
  tt_assert(connection_dir_is_encrypted(conn));

  const char *req = RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(connection_write_to_buf_impl_);
347
    connection_free_minimal(TO_CONN(conn));
348
349
350
351
352
353
354
355
356
357
358
359
    tor_free(header);
    rend_cache_free_all();
}

NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));

static routerinfo_t *mock_routerinfo;

static const routerinfo_t *
NS(router_get_my_routerinfo)(void)
{
  if (!mock_routerinfo) {
360
    mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  }

  return mock_routerinfo;
}

static void
test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  char buff[30];
  char req[70];
  rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  char *service_id = NULL;
  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  size_t body_len = 0;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  NS_MOCK(router_get_my_routerinfo);

  rend_cache_init();

  /* create a valid rend service descriptor */
  #define RECENT_TIME -10
  generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);

  tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str),
391
            OP_EQ, 0);
392
393
394
395

  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
                DIGEST_LEN);

396
  conn = new_dir_conn();
397
398
399
400
401

  // connection is encrypted
  TO_CONN(conn)->linked = 1;
  tt_assert(connection_dir_is_encrypted(conn));

402
  tor_snprintf(req, sizeof(req), RENDEZVOUS2_GET("%s"), desc_id_base32);
403
404
405
406
407
408
409
410
411
412
413
414
415
416

  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  body_len = strlen(desc_holder->desc_str);
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, body_len+1, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  tt_assert(strstr(header, "Pragma: no-cache\r\n"));
417
  tor_snprintf(buff, sizeof(buff), "Content-Length: %ld\r\n", (long) body_len);
418
419
420
421
422
423
424
425
426
  tt_assert(strstr(header, buff));

  tt_int_op(body_used, OP_EQ, strlen(body));
  tt_str_op(body, OP_EQ, desc_holder->desc_str);

  done:
    UNMOCK(connection_write_to_buf_impl_);
    NS_UNMOCK(router_get_my_routerinfo);

427
    connection_free_minimal(TO_CONN(conn));
428
429
    tor_free(header);
    tor_free(body);
430
431
    rend_encoded_v2_service_descriptor_free(desc_holder);
    tor_free(service_id);
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    rend_cache_free_all();
}

#define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
static void
test_dir_handle_get_micro_d_not_found(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
447
  conn = new_dir_conn();
448
449
450
451
452
453
454
455
456
457
458
459

  const char *req = MICRODESC_GET(B64_256_1 "-" B64_256_2);
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(connection_write_to_buf_impl_);

460
    connection_free_minimal(TO_CONN(conn));
461
462
463
464
465
466
467
    tor_free(header);
}

static or_options_t *mock_options = NULL;
static void
init_mock_options(void)
{
468
  mock_options = tor_malloc(sizeof(or_options_t));
469
470
  memset(mock_options, 0, sizeof(or_options_t));
  mock_options->TestingTorNetwork = 1;
471
472
  mock_options->DataDirectory = tor_strdup(get_fname_rnd("datadir_tmp"));
  check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
}

static const or_options_t *
mock_get_options(void)
{
  tor_assert(mock_options);
  return mock_options;
}

static const char microdesc[] =
  "onion-key\n"
  "-----BEGIN RSA PUBLIC KEY-----\n"
  "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
  "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
  "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
  "-----END RSA PUBLIC KEY-----\n";

static void
test_dir_handle_get_micro_d(void *data)
{
  dir_connection_t *conn = NULL;
  microdesc_cache_t *mc = NULL ;
  smartlist_t *list = NULL;
  char digest[DIGEST256_LEN];
  char digest_base64[128];
  char path[80];
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  (void) data;

  MOCK(get_options, mock_get_options);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* SETUP */
  init_mock_options();

  /* Add microdesc to cache */
  crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  base64_encode_nopad(digest_base64, sizeof(digest_base64),
                      (uint8_t *) digest, DIGEST256_LEN);

  mc = get_microdesc_cache();
  list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
                                  time(NULL), NULL);
  tt_int_op(1, OP_EQ, smartlist_len(list));

  /* Make the request */
521
  conn = new_dir_conn();
522

523
  tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, strlen(microdesc)+1, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));

  tt_int_op(body_used, OP_EQ, strlen(body));
  tt_str_op(body, OP_EQ, microdesc);

  done:
    UNMOCK(get_options);
    UNMOCK(connection_write_to_buf_impl_);

543
    or_options_free(mock_options); mock_options = NULL;
544
    connection_free_minimal(TO_CONN(conn));
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
    tor_free(header);
    tor_free(body);
    smartlist_free(list);
    microdesc_free_all();
}

static void
test_dir_handle_get_micro_d_server_busy(void *data)
{
  dir_connection_t *conn = NULL;
  microdesc_cache_t *mc = NULL ;
  smartlist_t *list = NULL;
  char digest[DIGEST256_LEN];
  char digest_base64[128];
  char path[80];
  char *header = NULL;
  (void) data;

  MOCK(get_options, mock_get_options);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* SETUP */
  init_mock_options();

  /* Add microdesc to cache */
  crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  base64_encode_nopad(digest_base64, sizeof(digest_base64),
                      (uint8_t *) digest, DIGEST256_LEN);

  mc = get_microdesc_cache();
  list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
                                  time(NULL), NULL);
  tt_int_op(1, OP_EQ, smartlist_len(list));

  //Make it busy
  mock_options->CountPrivateBandwidth = 1;

  /* Make the request */
583
  conn = new_dir_conn();
584

585
  tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
586
587
588
589
590
591
592
593
594
595
596
  tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(SERVER_BUSY, OP_EQ, header);

  done:
    UNMOCK(get_options);
    UNMOCK(connection_write_to_buf_impl_);

597
    or_options_free(mock_options); mock_options = NULL;
598
    connection_free_minimal(TO_CONN(conn));
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
    tor_free(header);
    smartlist_free(list);
    microdesc_free_all();
}

#define BRIDGES_PATH "/tor/networkstatus-bridges"
static void
test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(get_options, mock_get_options);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* SETUP */
  init_mock_options();
  mock_options->BridgeAuthoritativeDir = 1;
Nick Mathewson's avatar
Nick Mathewson committed
618
  mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
619

620
  conn = new_dir_conn();
621
622
623
624
625
626
627
628
629
630
631
632
633
  TO_CONN(conn)->linked = 1;

  const char *req = GET(BRIDGES_PATH);
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(get_options);
    UNMOCK(connection_write_to_buf_impl_);
634
    or_options_free(mock_options); mock_options = NULL;
635
    connection_free_minimal(TO_CONN(conn));
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    tor_free(header);
}

static void
test_dir_handle_get_networkstatus_bridges(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(get_options, mock_get_options);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* SETUP */
  init_mock_options();
  mock_options->BridgeAuthoritativeDir = 1;
  mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  crypto_digest256(mock_options->BridgePassword_AuthDigest_,
                     "abcdefghijklm12345", 18, DIGEST_SHA256);

656
  conn = new_dir_conn();
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  TO_CONN(conn)->linked = 1;

  const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
                    "Authorization: Basic abcdefghijklm12345\r\n\r\n";
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  tt_assert(strstr(header, "Content-Length: 0\r\n"));

  done:
    UNMOCK(get_options);
    UNMOCK(connection_write_to_buf_impl_);
674
    or_options_free(mock_options); mock_options = NULL;
675
    connection_free_minimal(TO_CONN(conn));
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    tor_free(header);
}

static void
test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(get_options, mock_get_options);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* SETUP */
  init_mock_options();
  mock_options->BridgeAuthoritativeDir = 1;
  mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  crypto_digest256(mock_options->BridgePassword_AuthDigest_,
                     "abcdefghijklm12345", 18, DIGEST_SHA256);

696
  conn = new_dir_conn();
697
698
699
700
701
702
703
704
705
706
707
708
709
710
  TO_CONN(conn)->linked = 1;

  const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
                           "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);

  done:
    UNMOCK(get_options);
    UNMOCK(connection_write_to_buf_impl_);
711
    or_options_free(mock_options); mock_options = NULL;
712
    connection_free_minimal(TO_CONN(conn));
713
714
715
716
717
718
719
720
721
722
723
724
725
    tor_free(header);
}

#define SERVER_DESC_GET(id) GET("/tor/server/" id)
static void
test_dir_handle_get_server_descriptors_not_found(void* data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  (void) data;

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

726
  conn = new_dir_conn();
727
728
729
730
731
732
733
734

  const char *req = SERVER_DESC_GET("invalid");
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      NULL, NULL, 1, 0);

  tt_str_op(NOT_FOUND, OP_EQ, header);
735
  tt_ptr_op(conn->spool, OP_EQ, NULL);
736
737
738

  done:
    UNMOCK(connection_write_to_buf_impl_);
739
    or_options_free(mock_options); mock_options = NULL;
740
    connection_free_minimal(TO_CONN(conn));
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
    tor_free(header);
}

static void
test_dir_handle_get_server_descriptors_all(void* data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  (void) data;

  /* Setup fake routerlist. */
  helper_setup_fake_routerlist();

  //TODO: change to router_get_my_extrainfo when testing "extra" path
  NS_MOCK(router_get_my_routerinfo);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  // We are one of the routers
  routerlist_t *our_routerlist = router_get_routerlist();
  tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
  set_server_identity_key(mock_routerinfo->identity_pkey);
765
  mock_routerinfo->cache_info.published_on = time(NULL);
766
767
768
769

  /* Treat "all" requests as if they were unencrypted */
  mock_routerinfo->cache_info.send_unencrypted = 1;

770
  conn = new_dir_conn();
771
772
773
774
775
776
777
778
779

  const char *req = SERVER_DESC_GET("all");
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  //TODO: Is this a BUG?
  //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  //which is smaller than that by annotation_len bytes
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used,
780
                      1024*1024, 0);
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));

  //TODO: Is this a BUG?
  //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
  tt_int_op(body_used, OP_EQ,
            mock_routerinfo->cache_info.signed_descriptor_len);

  tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
                         mock_routerinfo->cache_info.annotations_len);
796
  tt_ptr_op(conn->spool, OP_EQ, NULL);
797
798
799
800

  done:
    NS_UNMOCK(router_get_my_routerinfo);
    UNMOCK(connection_write_to_buf_impl_);
801
    connection_free_minimal(TO_CONN(conn));
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
    tor_free(header);
    tor_free(body);

    routerlist_free_all();
    nodelist_free_all();
    entry_guards_free_all();
}

static char
TEST_DESCRIPTOR[] =
"@uploaded-at 2014-06-08 19:20:11\n"
"@source \"127.0.0.1\"\n"
"router test000a 127.0.0.1 5000 0 7000\n"
"platform Tor 0.2.5.3-alpha-dev on Linux\n"
"protocols Link 1 2 Circuit 1\n"
"published 2014-06-08 19:20:11\n"
"fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
"uptime 0\n"
"bandwidth 1073741824 1073741824 0\n"
"extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
"caches-extra-info\n"
"onion-key\n"
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
"HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
"DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
"-----END RSA PUBLIC KEY-----\n"
"signing-key\n"
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
"O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
"3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
"-----END RSA PUBLIC KEY-----\n"
"hidden-service-dir\n"
"contact auth0@test.test\n"
"ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
"reject *:*\n"
"router-signature\n"
"-----BEGIN SIGNATURE-----\n"
"rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
"aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
"eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
"-----END SIGNATURE-----\n";

static void
test_dir_handle_get_server_descriptors_authority(void* data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  crypto_pk_t *identity_pkey = pk_generate(0);
  (void) data;

  NS_MOCK(router_get_my_routerinfo);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* init mock */
  router_get_my_routerinfo();
  crypto_pk_get_digest(identity_pkey,
                       mock_routerinfo->cache_info.identity_digest);

  // the digest is mine (the channel is unnecrypted, so we must allow sending)
  set_server_identity_key(identity_pkey);
  mock_routerinfo->cache_info.send_unencrypted = 1;

  /* Setup descriptor */
Nick Mathewson's avatar
Nick Mathewson committed
869
870
871
  long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  mock_routerinfo->cache_info.signed_descriptor_body =
    tor_strdup(TEST_DESCRIPTOR);
872
  mock_routerinfo->cache_info.signed_descriptor_len =
873
    strlen(TEST_DESCRIPTOR) - annotation_len;
874
  mock_routerinfo->cache_info.annotations_len = annotation_len;
875
  mock_routerinfo->cache_info.published_on = time(NULL);
876

877
  conn = new_dir_conn();
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894

  const char *req = SERVER_DESC_GET("authority");
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  //TODO: Is this a BUG?
  //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  //is smaller than that by annotation_len bytes
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));

895
  tt_int_op(body_used, OP_EQ, strlen(body));
896
897

  tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
898
  tt_ptr_op(conn->spool, OP_EQ, NULL);
899
900
901
902

  done:
    NS_UNMOCK(router_get_my_routerinfo);
    UNMOCK(connection_write_to_buf_impl_);
903
    tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
904
    tor_free(mock_routerinfo);
905
    connection_free_minimal(TO_CONN(conn));
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
    tor_free(header);
    tor_free(body);
    crypto_pk_free(identity_pkey);
}

static void
test_dir_handle_get_server_descriptors_fp(void* data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  crypto_pk_t *identity_pkey = pk_generate(0);
  (void) data;

  NS_MOCK(router_get_my_routerinfo);
  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

  /* init mock */
  router_get_my_routerinfo();
  crypto_pk_get_digest(identity_pkey,
                       mock_routerinfo->cache_info.identity_digest);

  // the digest is mine (the channel is unnecrypted, so we must allow sending)
  set_server_identity_key(identity_pkey);
  mock_routerinfo->cache_info.send_unencrypted = 1;

  /* Setup descriptor */
Nick Mathewson's avatar
Nick Mathewson committed
934
935
936
  long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  mock_routerinfo->cache_info.signed_descriptor_body =
    tor_strdup(TEST_DESCRIPTOR);
937
938
  mock_routerinfo->cache_info.signed_descriptor_len =
    strlen(TEST_DESCRIPTOR) - annotation_len;
939
  mock_routerinfo->cache_info.annotations_len = annotation_len;
940
  mock_routerinfo->cache_info.published_on = time(NULL);
941

942
  conn = new_dir_conn();
943
944
945
946
947
948
949

  #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
                                   DIGEST_LEN);

  char req[155];
950
951
  tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
               hex_digest);
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
  tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);

  //TODO: Is this a BUG?
  //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  //is smaller than that by annotation_len bytes
  fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
                      &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);

  tt_assert(header);
  tt_assert(body);

  tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  tt_assert(strstr(header, "Content-Encoding: identity\r\n"));

967
  tt_int_op(body_used, OP_EQ, strlen(body));
968
969

  tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
970
  tt_ptr_op(conn->spool, OP_EQ, NULL);
971
972
973
974

  done:
    NS_UNMOCK(router_get_my_routerinfo);
    UNMOCK(connection_write_to_buf_impl_);
975
    tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
976
    tor_free(mock_routerinfo);
977
    connection_free_minimal(TO_CONN(conn));
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    tor_free(header);
    tor_free(body);
    crypto_pk_free(identity_pkey);
}

#define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
#define HEX2 "Deadbeef99999991111119999911111111f00ba4"

static void
test_dir_handle_get_server_descriptors_d(void* data)
{
  dir_connection_t *conn = NULL;
  char *header = NULL;
  char *body = NULL;
  size_t body_used = 0;
  crypto_pk_t *identity_pkey = pk_generate(0);
  (void) data;

  /* Setup fake routerlist. */
  helper_setup_fake_routerlist();

  MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);

For faster browsing, not all history is shown. View entire blame