aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_connection.c
blob: 6f716f5758e9502b503a5757dc862a501d7b8a24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/* Copyright (c) 2015-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "orconfig.h"

#define CONNECTION_PRIVATE
#define MAINLOOP_PRIVATE
#define CONNECTION_OR_PRIVATE

#include "core/or/or.h"
#include "test/test.h"

#include "core/mainloop/connection.h"
#include "core/or/connection_edge.h"
#include "feature/hs/hs_common.h"
#include "core/mainloop/mainloop.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/rend/rendcache.h"
#include "feature/dircommon/directory.h"
#include "core/or/connection_or.h"
#include "lib/net/resolve.h"

#include "test/test_connection.h"
#include "test/test_helpers.h"

#include "feature/dircommon/dir_connection_st.h"
#include "core/or/entry_connection_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/or_connection_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"

static void * test_conn_get_basic_setup(const struct testcase_t *tc);
static int test_conn_get_basic_teardown(const struct testcase_t *tc,
                                        void *arg);

static void * test_conn_get_rend_setup(const struct testcase_t *tc);
static int test_conn_get_rend_teardown(const struct testcase_t *tc,
                                        void *arg);

static void * test_conn_get_rsrc_setup(const struct testcase_t *tc);
static int test_conn_get_rsrc_teardown(const struct testcase_t *tc,
                                        void *arg);

/* Arbitrary choice - IPv4 Directory Connection to localhost */
#define TEST_CONN_TYPE          (CONN_TYPE_DIR)
/* We assume every machine has IPv4 localhost, is that ok? */
#define TEST_CONN_ADDRESS       "127.0.0.1"
#define TEST_CONN_PORT          (12345)
#define TEST_CONN_ADDRESS_PORT  "127.0.0.1:12345"
#define TEST_CONN_FAMILY        (AF_INET)
#define TEST_CONN_STATE         (DIR_CONN_STATE_MIN_)
#define TEST_CONN_ADDRESS_2     "127.0.0.2"

#define TEST_CONN_BASIC_PURPOSE (DIR_PURPOSE_MIN_)

#define TEST_CONN_REND_ADDR     "cfs3rltphxxvabci"
#define TEST_CONN_REND_PURPOSE  (DIR_PURPOSE_FETCH_RENDDESC_V2)
#define TEST_CONN_REND_PURPOSE_SUCCESSFUL (DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2)
#define TEST_CONN_REND_TYPE_2   (CONN_TYPE_AP)
#define TEST_CONN_REND_ADDR_2   "icbavxxhptlr3sfc"

#define TEST_CONN_RSRC          (networkstatus_get_flavor_name(FLAV_MICRODESC))
#define TEST_CONN_RSRC_PURPOSE  (DIR_PURPOSE_FETCH_CONSENSUS)
#define TEST_CONN_RSRC_STATE_SUCCESSFUL (DIR_CONN_STATE_CLIENT_FINISHED)
#define TEST_CONN_RSRC_2        (networkstatus_get_flavor_name(FLAV_NS))

#define TEST_CONN_DL_STATE      (DIR_CONN_STATE_CLIENT_READING)

/* see AP_CONN_STATE_IS_UNATTACHED() */
#define TEST_CONN_UNATTACHED_STATE (AP_CONN_STATE_CIRCUIT_WAIT)
#define TEST_CONN_ATTACHED_STATE   (AP_CONN_STATE_CONNECT_WAIT)

void
test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
{
  int rv = 0;

  tt_assert(addr);

  rv = tor_addr_lookup(address, family, addr);
  /* XXXX - should we retry on transient failure? */
  tt_int_op(rv, OP_EQ, 0);
  tt_assert(tor_addr_is_loopback(addr));
  tt_assert(tor_addr_is_v4(addr));

  return;

 done:
  tor_addr_make_null(addr, TEST_CONN_FAMILY);
}

static void *
test_conn_get_basic_setup(const struct testcase_t *tc)
{
  (void)tc;
  return test_conn_get_connection(TEST_CONN_STATE, TEST_CONN_TYPE,
                                  TEST_CONN_BASIC_PURPOSE);
}

static int
test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
{
  (void)tc;
  connection_t *conn = arg;

  tt_assert(conn);
  assert_connection_ok(conn, time(NULL));

  /* teardown the connection as fast as possible */
  if (conn->linked_conn) {
    assert_connection_ok(conn->linked_conn, time(NULL));

    /* We didn't call tor_libevent_initialize(), so event_base was NULL,
     * so we can't rely on connection_unregister_events() use of event_del().
     */
    if (conn->linked_conn->read_event) {
      tor_free(conn->linked_conn->read_event);
      conn->linked_conn->read_event = NULL;
    }
    if (conn->linked_conn->write_event) {
      tor_free(conn->linked_conn->write_event);
      conn->linked_conn->write_event = NULL;
    }

    if (!conn->linked_conn->marked_for_close) {
      connection_close_immediate(conn->linked_conn);
      if (CONN_IS_EDGE(conn->linked_conn)) {
        /* Suppress warnings about all the stuff we didn't do */
        TO_EDGE_CONN(conn->linked_conn)->edge_has_sent_end = 1;
        TO_EDGE_CONN(conn->linked_conn)->end_reason =
          END_STREAM_REASON_INTERNAL;
        if (conn->linked_conn->type == CONN_TYPE_AP) {
          TO_ENTRY_CONN(conn->linked_conn)->socks_request->has_finished = 1;
        }
      }
      connection_mark_for_close(conn->linked_conn);
    }

    close_closeable_connections();
  }

  /* We didn't set the events up properly, so we can't use event_del() in
   * close_closeable_connections() > connection_free()
   * > connection_unregister_events() */
  if (conn->read_event) {
    tor_free(conn->read_event);
    conn->read_event = NULL;
  }
  if (conn->write_event) {
    tor_free(conn->write_event);
    conn->write_event = NULL;
  }

  if (!conn->marked_for_close) {
    connection_close_immediate(conn);
    if (CONN_IS_EDGE(conn)) {
      /* Suppress warnings about all the stuff we didn't do */
      TO_EDGE_CONN(conn)->edge_has_sent_end = 1;
      TO_EDGE_CONN(conn)->end_reason = END_STREAM_REASON_INTERNAL;
      if (conn->type == CONN_TYPE_AP) {
        TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
      }
    }
    connection_mark_for_close(conn);
  }

  close_closeable_connections();

  /* The unit test will fail if we return 0 */
  return 1;

  /* When conn == NULL, we can't cleanup anything */
 done:
  return 0;
}

static void *
test_conn_get_rend_setup(const struct testcase_t *tc)
{
  dir_connection_t *conn = DOWNCAST(dir_connection_t,
                                    test_conn_get_connection(
                                                    TEST_CONN_STATE,
                                                    TEST_CONN_TYPE,
                                                    TEST_CONN_REND_PURPOSE));
  tt_assert(conn);
  assert_connection_ok(&conn->base_, time(NULL));

  rend_cache_init();

  /* TODO: use directory_initiate_request() to do this - maybe? */
  tor_assert(strlen(TEST_CONN_REND_ADDR) == REND_SERVICE_ID_LEN_BASE32);
  conn->rend_data = rend_data_client_create(TEST_CONN_REND_ADDR, NULL, NULL,
                                            REND_NO_AUTH);
  assert_connection_ok(&conn->base_, time(NULL));
  return conn;

  /* On failure */
 done:
  test_conn_get_rend_teardown(tc, conn);
  /* Returning NULL causes the unit test to fail */
  return NULL;
}

static int
test_conn_get_rend_teardown(const struct testcase_t *tc, void *arg)
{
  dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  int rv = 0;

  tt_assert(conn);
  assert_connection_ok(&conn->base_, time(NULL));

  /* avoid a last-ditch attempt to refetch the descriptor */
  conn->base_.purpose = TEST_CONN_REND_PURPOSE_SUCCESSFUL;

  /* connection_free_() cleans up rend_data */
  rv = test_conn_get_basic_teardown(tc, arg);
 done:
  rend_cache_free_all();
  return rv;
}

static dir_connection_t *
test_conn_download_status_add_a_connection(const char *resource)
{
  dir_connection_t *conn = DOWNCAST(dir_connection_t,
                                    test_conn_get_connection(
                                                      TEST_CONN_STATE,
                                                      TEST_CONN_TYPE,
                                                      TEST_CONN_RSRC_PURPOSE));

  tt_assert(conn);
  assert_connection_ok(&conn->base_, time(NULL));

  /* Replace the existing resource with the one we want */
  if (resource) {
    if (conn->requested_resource) {
      tor_free(conn->requested_resource);
    }
    conn->requested_resource = tor_strdup(resource);
    assert_connection_ok(&conn->base_, time(NULL));
  }

  return conn;

 done:
  test_conn_get_rsrc_teardown(NULL, conn);
  return NULL;
}

static void *
test_conn_get_rsrc_setup(const struct testcase_t *tc)
{
  (void)tc;
  return test_conn_download_status_add_a_connection(TEST_CONN_RSRC);
}

static int
test_conn_get_rsrc_teardown(const struct testcase_t *tc, void *arg)
{
  int rv = 0;

  connection_t *conn = (connection_t *)arg;
  tt_assert(conn);
  assert_connection_ok(conn, time(NULL));

  if (conn->type == CONN_TYPE_DIR) {
    dir_connection_t *dir_conn = DOWNCAST(dir_connection_t, arg);

    tt_assert(dir_conn);
    assert_connection_ok(&dir_conn->base_, time(NULL));

    /* avoid a last-ditch attempt to refetch the consensus */
    dir_conn->base_.state = TEST_CONN_RSRC_STATE_SUCCESSFUL;
    assert_connection_ok(&dir_conn->base_, time(NULL));
  }

  /* connection_free_() cleans up requested_resource */
  rv = test_conn_get_basic_teardown(tc, conn);

 done:
  return rv;
}

static void *
test_conn_download_status_setup(const struct testcase_t *tc)
{
  return (void*)tc;
}

static int
test_conn_download_status_teardown(const struct testcase_t *tc, void *arg)
{
  (void)arg;
  int rv = 0;

  /* Ignore arg, and just loop through the connection array */
  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
    if (conn) {
      assert_connection_ok(conn, time(NULL));

      /* connection_free_() cleans up requested_resource */
      rv = test_conn_get_rsrc_teardown(tc, conn);
      tt_int_op(rv, OP_EQ, 1);
    }
  } SMARTLIST_FOREACH_END(conn);

 done:
  return rv;
}

/* Like connection_ap_make_link(), but does much less */
static connection_t *
test_conn_get_linked_connection(connection_t *l_conn, uint8_t state)
{
  tt_assert(l_conn);
  assert_connection_ok(l_conn, time(NULL));

  /* AP connections don't seem to have purposes */
  connection_t *conn = test_conn_get_connection(state, CONN_TYPE_AP,
                                                       0);

  tt_assert(conn);
  assert_connection_ok(conn, time(NULL));

  conn->linked = 1;
  l_conn->linked = 1;
  conn->linked_conn = l_conn;
  l_conn->linked_conn = conn;
  /* we never opened a real socket, so we can just overwrite it */
  conn->s = TOR_INVALID_SOCKET;
  l_conn->s = TOR_INVALID_SOCKET;

  assert_connection_ok(conn, time(NULL));
  assert_connection_ok(l_conn, time(NULL));

  return conn;

 done:
  test_conn_download_status_teardown(NULL, NULL);
  return NULL;
}

static struct testcase_setup_t test_conn_get_basic_st = {
  test_conn_get_basic_setup, test_conn_get_basic_teardown
};

static struct testcase_setup_t test_conn_get_rend_st = {
  test_conn_get_rend_setup, test_conn_get_rend_teardown
};

static struct testcase_setup_t test_conn_get_rsrc_st = {
  test_conn_get_rsrc_setup, test_conn_get_rsrc_teardown
};

static struct testcase_setup_t test_conn_download_status_st = {
  test_conn_download_status_setup, test_conn_download_status_teardown
};

static void
test_conn_get_basic(void *arg)
{
  connection_t *conn = (connection_t*)arg;
  tor_addr_t addr, addr2;

  tt_assert(conn);
  assert_connection_ok(conn, time(NULL));

  test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  tt_assert(!tor_addr_is_null(&addr));
  test_conn_lookup_addr_helper(TEST_CONN_ADDRESS_2, TEST_CONN_FAMILY, &addr2);
  tt_assert(!tor_addr_is_null(&addr2));

  /* Check that we get this connection back when we search for it by
   * its attributes, but get NULL when we supply a different value. */

  tt_assert(connection_get_by_global_id(conn->global_identifier) == conn);
  tt_ptr_op(connection_get_by_global_id(!conn->global_identifier), OP_EQ,
            NULL);

  tt_assert(connection_get_by_type(conn->type) == conn);
  tt_assert(connection_get_by_type(TEST_CONN_TYPE) == conn);
  tt_ptr_op(connection_get_by_type(!conn->type), OP_EQ, NULL);
  tt_ptr_op(connection_get_by_type(!TEST_CONN_TYPE), OP_EQ, NULL);

  tt_assert(connection_get_by_type_state(conn->type, conn->state)
            == conn);
  tt_assert(connection_get_by_type_state(TEST_CONN_TYPE, TEST_CONN_STATE)
            == conn);
  tt_assert(connection_get_by_type_state(!conn->type, !conn->state)
            == NULL);
  tt_assert(connection_get_by_type_state(!TEST_CONN_TYPE, !TEST_CONN_STATE)
            == NULL);

  /* Match on the connection fields themselves */
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &conn->addr,
                                                     conn->port,
                                                     conn->purpose)
            == conn);
  /* Match on the original inputs to the connection */
  tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
                                                     &conn->addr,
                                                     conn->port,
                                                     conn->purpose)
            == conn);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &addr,
                                                     conn->port,
                                                     conn->purpose)
            == conn);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &conn->addr,
                                                     TEST_CONN_PORT,
                                                     conn->purpose)
            == conn);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &conn->addr,
                                                     conn->port,
                                                     TEST_CONN_BASIC_PURPOSE)
            == conn);
  tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
                                                     &addr,
                                                     TEST_CONN_PORT,
                                                     TEST_CONN_BASIC_PURPOSE)
            == conn);
  /* Then try each of the not-matching combinations */
  tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
                                                     &conn->addr,
                                                     conn->port,
                                                     conn->purpose)
            == NULL);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &addr2,
                                                     conn->port,
                                                     conn->purpose)
            == NULL);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &conn->addr,
                                                     !conn->port,
                                                     conn->purpose)
            == NULL);
  tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
                                                     &conn->addr,
                                                     conn->port,
                                                     !conn->purpose)
            == NULL);
  /* Then try everything not-matching */
  tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
                                                     &addr2,
                                                     !conn->port,
                                                     !conn->purpose)
            == NULL);
  tt_assert(connection_get_by_type_addr_port_purpose(!TEST_CONN_TYPE,
                                                     &addr2,
                                                     !TEST_CONN_PORT,
                                                     !TEST_CONN_BASIC_PURPOSE)
            == NULL);

 done:
  ;
}

static void
test_conn_get_rend(void *arg)
{
  dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  tt_assert(conn);
  assert_connection_ok(&conn->base_, time(NULL));

  tt_assert(connection_get_by_type_state_rendquery(
                                            conn->base_.type,
                                            conn->base_.state,
                                            rend_data_get_address(
                                                      conn->rend_data))
            == TO_CONN(conn));
  tt_assert(connection_get_by_type_state_rendquery(
                                            TEST_CONN_TYPE,
                                            TEST_CONN_STATE,
                                            TEST_CONN_REND_ADDR)
            == TO_CONN(conn));
  tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
                                                   !conn->base_.state,
                                                   "")
            == NULL);
  tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
                                                   !TEST_CONN_STATE,
                                                   TEST_CONN_REND_ADDR_2)
            == NULL);

 done:
  ;
}

#define sl_is_conn_assert(sl_input, conn) \
  do {                                               \
    the_sl = (sl_input);                             \
    tt_int_op(smartlist_len((the_sl)), OP_EQ, 1);         \
    tt_assert(smartlist_get((the_sl), 0) == (conn)); \
    smartlist_free(the_sl); the_sl = NULL;           \
  } while (0)

#define sl_no_conn_assert(sl_input)          \
  do {                                       \
    the_sl = (sl_input);                     \
    tt_int_op(smartlist_len((the_sl)), OP_EQ, 0); \
    smartlist_free(the_sl); the_sl = NULL;   \
  } while (0)

static void
test_conn_get_rsrc(void *arg)
{
  dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  smartlist_t *the_sl = NULL;
  tt_assert(conn);
  assert_connection_ok(&conn->base_, time(NULL));

  sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
                                                    conn->base_.purpose,
                                                    conn->requested_resource),
                    conn);
  sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
                                                    TEST_CONN_RSRC_PURPOSE,
                                                    TEST_CONN_RSRC),
                    conn);
  sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
                                                    !conn->base_.purpose,
                                                    ""));
  sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
                                                    !TEST_CONN_RSRC_PURPOSE,
                                                    TEST_CONN_RSRC_2));

  sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
                                                    conn->base_.purpose,
                                                    conn->requested_resource,
                                                    conn->base_.state),
                    conn);
  sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
                                                    TEST_CONN_RSRC_PURPOSE,
                                                    TEST_CONN_RSRC,
                                                    TEST_CONN_STATE),
                    conn);
  sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
                                                    !conn->base_.purpose,
                                                    "",
                                                    !conn->base_.state));
  sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
                                                    !TEST_CONN_RSRC_PURPOSE,
                                                    TEST_CONN_RSRC_2,
                                                    !TEST_CONN_STATE));

  tt_int_op(connection_dir_count_by_purpose_and_resource(
                 conn->base_.purpose, conn->requested_resource),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                 TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                 !conn->base_.purpose, ""),
            OP_EQ, 0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                 !TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC_2),
            OP_EQ, 0);

  tt_int_op(connection_dir_count_by_purpose_resource_and_state(
                 conn->base_.purpose, conn->requested_resource,
                 conn->base_.state),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_resource_and_state(
                 TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC, TEST_CONN_STATE),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_resource_and_state(
                 !conn->base_.purpose, "", !conn->base_.state),
            OP_EQ, 0);
  tt_int_op(connection_dir_count_by_purpose_resource_and_state(
                 !TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC_2, !TEST_CONN_STATE),
            OP_EQ, 0);

 done:
  smartlist_free(the_sl);
}

static void
test_conn_download_status(void *arg)
{
  dir_connection_t *conn = NULL;
  dir_connection_t *conn2 = NULL;
  dir_connection_t *conn4 = NULL;
  connection_t *ap_conn = NULL;

  const struct testcase_t *tc = arg;
  consensus_flavor_t usable_flavor =
    networkstatus_parse_flavor_name((const char*) tc->setup_data);

  /* The "other flavor" trick only works if there are two flavors */
  tor_assert(N_CONSENSUS_FLAVORS == 2);
  consensus_flavor_t other_flavor = ((usable_flavor == FLAV_NS)
                                     ? FLAV_MICRODESC
                                     : FLAV_NS);
  const char *res = networkstatus_get_flavor_name(usable_flavor);
  const char *other_res = networkstatus_get_flavor_name(other_flavor);

  /* no connections */
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* one connection, not downloading */
  conn = test_conn_download_status_add_a_connection(res);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* one connection, downloading but not linked (not possible on a client,
   * but possible on a relay) */
  conn->base_.state = TEST_CONN_DL_STATE;
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* one connection, downloading and linked, but not yet attached */
  ap_conn = test_conn_get_linked_connection(TO_CONN(conn),
                                            TEST_CONN_UNATTACHED_STATE);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

    /* one connection, downloading and linked and attached */
  ap_conn->state = TEST_CONN_ATTACHED_STATE;
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* one connection, linked and attached but not downloading */
  conn->base_.state = TEST_CONN_STATE;
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* two connections, both not downloading */
  conn2 = test_conn_download_status_add_a_connection(res);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 2);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* two connections, one downloading */
  conn->base_.state = TEST_CONN_DL_STATE;
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 2);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);
  conn->base_.state = TEST_CONN_STATE;

  /* more connections, all not downloading */
  /* ignore the return value, it's free'd using the connection list */
  (void)test_conn_download_status_add_a_connection(res);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* more connections, one downloading */
  conn->base_.state = TEST_CONN_DL_STATE;
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* more connections, two downloading (should never happen, but needs
   * to be tested for completeness) */
  conn2->base_.state = TEST_CONN_DL_STATE;
  /* ignore the return value, it's free'd using the connection list */
  (void)test_conn_get_linked_connection(TO_CONN(conn2),
                                        TEST_CONN_ATTACHED_STATE);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);
  conn->base_.state = TEST_CONN_STATE;

  /* more connections, a different one downloading */
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                           TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 0);

  /* a connection for the other flavor (could happen if a client is set to
   * cache directory documents), one preferred flavor downloading
   */
  conn4 = test_conn_download_status_add_a_connection(other_res);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            0);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                   TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                   TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 1);

  /* a connection for the other flavor (could happen if a client is set to
   * cache directory documents), both flavors downloading
   */
  conn4->base_.state = TEST_CONN_DL_STATE;
  /* ignore the return value, it's free'd using the connection list */
  (void)test_conn_get_linked_connection(TO_CONN(conn4),
                                        TEST_CONN_ATTACHED_STATE);
  tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
            1);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                   TEST_CONN_RSRC_PURPOSE, res),
            OP_EQ, 3);
  tt_int_op(connection_dir_count_by_purpose_and_resource(
                                   TEST_CONN_RSRC_PURPOSE, other_res),
            OP_EQ, 1);

 done:
  /* the teardown function removes all the connections in the global list*/;
}

static node_t test_node;

static node_t *
mock_node_get_mutable_by_id(const char *digest)
{
  (void) digest;
  static routerinfo_t node_ri;
  memset(&node_ri, 0, sizeof(node_ri));

  test_node.ri = &node_ri;
  memset(test_node.identity, 'c', sizeof(test_node.identity));

  tor_addr_t ipv4_addr;
  tor_addr_parse(&ipv4_addr, "18.0.0.1");
  node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  node_ri.or_port = 1;

  return &test_node;
}

static const node_t *
mock_node_get_by_id(const char *digest)
{
  (void) digest;
  memset(test_node.identity, 'c', sizeof(test_node.identity));
  return &test_node;
}

/* Test whether we correctly track failed connections between relays. */
static void
test_failed_orconn_tracker(void *arg)
{
  (void) arg;

  int can_connect;
  time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  (void) now;

  update_approx_time(now);

  /* Prepare the OR connection that will be used in this test */
  or_connection_t or_conn;
  tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.real_addr, "18.0.0.1"));
  tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.base_.addr, "18.0.0.1"));
  or_conn.base_.port = 1;
  memset(or_conn.identity_digest, 'c', sizeof(or_conn.identity_digest));

  /* Check whether we can connect with an empty failure cache:
   * this should succeed */
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 1);

  /* Now add the destination to the failure cache */
  note_or_connect_failed(&or_conn);

  /* Check again: now it shouldn't connect */
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 0);

  /* Move time forward and check again: the cache should have been cleared and
   * now it should connect */
  now += 3600;
  update_approx_time(now);
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 1);

  /* Now mock the node_get_*by_id() functions to start using the node subsystem
   * optimization. */
  MOCK(node_get_by_id, mock_node_get_by_id);
  MOCK(node_get_mutable_by_id, mock_node_get_mutable_by_id);

  /* Since we just started using the node subsystem it will allow connections
   * now */
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 1);

  /* Mark it as failed */
  note_or_connect_failed(&or_conn);

  /* Check that it shouldn't connect now */
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 0);

  /* Move time forward and check again: now it should connect  */
  now += 3600;
  update_approx_time(now);
  can_connect = should_connect_to_relay(&or_conn);
  tt_int_op(can_connect, OP_EQ, 1);

 done:
  ;
}

#define CONNECTION_TESTCASE(name, fork, setup)                           \
  { #name, test_conn_##name, fork, &setup, NULL }

/* where arg is an expression (constant, variable, compound expression) */
#define CONNECTION_TESTCASE_ARG(name, fork, setup, arg)                  \
  { #name "_" #arg, test_conn_##name, fork, &setup, (void *)arg }

struct testcase_t connection_tests[] = {
  CONNECTION_TESTCASE(get_basic, TT_FORK, test_conn_get_basic_st),
  CONNECTION_TESTCASE(get_rend,  TT_FORK, test_conn_get_rend_st),
  CONNECTION_TESTCASE(get_rsrc,  TT_FORK, test_conn_get_rsrc_st),
  CONNECTION_TESTCASE_ARG(download_status,  TT_FORK,
                          test_conn_download_status_st, "microdesc"),
  CONNECTION_TESTCASE_ARG(download_status,  TT_FORK,
                          test_conn_download_status_st, "ns"),
//CONNECTION_TESTCASE(func_suffix, TT_FORK, setup_func_pair),
  { "failed_orconn_tracker", test_failed_orconn_tracker, TT_FORK, NULL, NULL },
  END_OF_TESTCASES
};