LCOV - code coverage report
Current view: top level - test - test_bridges.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 342 342 100.0 %
Date: 2021-11-24 03:28:48 Functions: 19 19 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2018-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information     */
       3             : 
       4             : /**
       5             :  * \file test_bridges.c
       6             :  * \brief Unittests for code in bridges.c
       7             :  **/
       8             : 
       9             : #define TOR_BRIDGES_PRIVATE
      10             : #define PT_PRIVATE /* Only needed for the mock_* items below */
      11             : 
      12             : #include <stdbool.h>
      13             : 
      14             : #include "core/or/or.h"
      15             : #include "lib/net/address.h"
      16             : #include "feature/client/bridges.h"
      17             : #include "app/config/config.h"
      18             : #include "feature/client/transports.h"
      19             : #include "feature/nodelist/node_st.h"
      20             : #include "feature/nodelist/routerinfo_st.h"
      21             : #include "feature/nodelist/routerstatus_st.h"
      22             : #include "feature/nodelist/microdesc_st.h"
      23             : 
      24             : /* Test suite stuff */
      25             : #include "test/test.h"
      26             : 
      27             : /**
      28             :  * A mocked transport_t, constructed via mock_transport_get_by_name().
      29             :  */
      30             : static transport_t *mock_transport = NULL;
      31             : 
      32             : /**
      33             :  * Mock transport_get_by_name() to simply return a transport_t for the
      34             :  * transport name that was input to it.
      35             :  */
      36             : static transport_t *
      37           1 : mock_transport_get_by_name(const char *name)
      38             : {
      39           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
      40           1 :   uint16_t port = 9999;
      41           1 :   int socksv = 9;
      42           1 :   char *args = tor_strdup("foo=bar");
      43             : 
      44           1 :   if (!mock_transport) {
      45           1 :     tor_addr_parse(addr, "99.99.99.99");
      46           1 :     mock_transport = transport_new(addr, port, name, socksv, args);
      47             :   }
      48             : 
      49           1 :   tor_free(addr);
      50           1 :   tor_free(args);
      51             : 
      52           1 :   return mock_transport;
      53             : }
      54             : 
      55             : #undef PT_PRIVATE /* defined(PT_PRIVATE) */
      56             : 
      57             : /**
      58             :  * Test helper: Add a variety of bridges to our global bridgelist.
      59             :  */
      60             : static void
      61          16 : helper_add_bridges_to_bridgelist(void *arg)
      62             : {
      63             :   /* Note: the two bridges which do not have specified fingerprints will be
      64             :    * internally stored as both having the same fingerprint of all-zero bytes.
      65             :    */
      66             : 
      67          16 :   (void)arg;
      68          16 :   char *bridge0 = tor_strdup("6.6.6.6:6666");
      69          16 :   char *bridge1 = tor_strdup("6.6.6.7:6667 "
      70             :                              "A10C4F666D27364036B562823E5830BC448E046A");
      71          16 :   char *bridge2 = tor_strdup("obfs4 198.245.60.51:443 "
      72             :                              "752CF7825B3B9EA6A98C83AC41F7099D67007EA5 "
      73             :                   "cert=xpmQtKUqQ/6v5X7ijgYE/f03+l2/EuQ1dexjyUhh16wQlu/"
      74             :                              "cpXUGalmhDIlhuiQPNEKmKw iat-mode=0");
      75          16 :   char *bridge3 = tor_strdup("banana 5.5.5.5:5555 "
      76             :                              "9D6AE1BD4FDF39721CE908966E79E16F9BFCCF2F");
      77          16 :   char *bridge4 = tor_strdup("obfs4 1.2.3.4:1234 "
      78             :                              "foo=abcdefghijklmnopqrstuvwxyz");
      79          16 :   char *bridge5 = tor_strdup("apple 4.4.4.4:4444 "
      80             :                              "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA "
      81             :                              "foo=abcdefghijklmnopqrstuvwxyz");
      82          16 :   char *bridge6 = tor_strdup("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:6666");
      83             : 
      84          16 :   mark_bridge_list();
      85             : 
      86             : #define ADD_BRIDGE(bridge)                                          \
      87             :   bridge_line_t *bridge_line_ ##bridge = parse_bridge_line(bridge); \
      88             :   if (!bridge_line_ ##bridge) {                                     \
      89             :     printf("Unparseable bridge line: '%s'", #bridge);               \
      90             :   } else {                                                          \
      91             :     bridge_add_from_config(bridge_line_ ##bridge);                  \
      92             :   }                                                                 \
      93             :   tor_free(bridge);
      94             : 
      95          16 :   ADD_BRIDGE(bridge0);
      96          16 :   ADD_BRIDGE(bridge1);
      97          16 :   ADD_BRIDGE(bridge2);
      98          16 :   ADD_BRIDGE(bridge3);
      99          16 :   ADD_BRIDGE(bridge4);
     100          16 :   ADD_BRIDGE(bridge5);
     101          16 :   ADD_BRIDGE(bridge6);
     102             : #undef ADD_BRIDGES
     103             : 
     104          16 :   sweep_bridge_list();
     105          16 : }
     106             : 
     107             : /**
     108             :  * Make sure our test helper works too.
     109             :  */
     110             : static void
     111           1 : test_bridges_helper_func_add_bridges_to_bridgelist(void *arg)
     112             : {
     113           1 :   helper_add_bridges_to_bridgelist(arg);
     114           1 :   tt_finished();
     115             : 
     116           1 :  done:
     117           1 :   mark_bridge_list();
     118           1 :   sweep_bridge_list();
     119           1 : }
     120             : 
     121             : /**
     122             :  * Calling bridge_list_get() should create a new bridgelist if we
     123             :  * didn't have one before.
     124             :  */
     125             : static void
     126           1 : test_bridges_bridge_list_get_creates_new_bridgelist(void *arg)
     127             : {
     128           1 :   const smartlist_t *bridgelist = bridge_list_get();
     129             : 
     130           1 :   (void)arg;
     131             : 
     132           1 :   tt_ptr_op(bridgelist, OP_NE, NULL);
     133             : 
     134           1 :  done:
     135           1 :   return;
     136             : }
     137             : 
     138             : /**
     139             :  * Calling clear_bridge_list() should remove all bridges from the bridgelist.
     140             :  */
     141             : static void
     142           1 : test_bridges_clear_bridge_list(void *arg)
     143             : {
     144           1 :   const smartlist_t *bridgelist;
     145           1 :   const smartlist_t *bridgelist_after;
     146           1 :   const bridge_info_t *bridge;
     147             : 
     148           1 :   helper_add_bridges_to_bridgelist(arg);
     149           1 :   bridgelist = bridge_list_get();
     150           1 :   tt_ptr_op(bridgelist, OP_NE, NULL);
     151             : 
     152           1 :   bridge = smartlist_get(bridgelist, 0);
     153           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     154             : 
     155           1 :   clear_bridge_list();
     156           1 :   bridgelist_after = bridge_list_get();
     157           1 :   tt_ptr_op(bridgelist_after, OP_NE, NULL);
     158           1 :   tt_int_op(smartlist_len(bridgelist_after), OP_EQ, 0);
     159             : 
     160           1 :  done:
     161           1 :   return;
     162             : }
     163             : 
     164             : /**
     165             :  * Calling bridge_get_addrport() should give me the address and port
     166             :  * of the bridge.  In this case, we sort the smartlist of bridges on
     167             :  * fingerprints and choose the first one.
     168             :  */
     169             : static void
     170           1 : test_bridges_bridge_get_addrport(void *arg)
     171             : {
     172           1 :   smartlist_t *bridgelist;
     173           1 :   const bridge_info_t *bridge;
     174           1 :   const tor_addr_port_t *addrport;
     175             : 
     176           1 :   helper_add_bridges_to_bridgelist(arg);
     177           1 :   bridgelist = (smartlist_t*)bridge_list_get();
     178           1 :   tt_ptr_op(bridgelist, OP_NE, NULL);
     179             : 
     180             :   // This should be the bridge at 6.6.6.6:6666 with fingerprint
     181             :   // 0000000000000000000000000000000000000000
     182           1 :   bridge = smartlist_get(bridgelist, 0);
     183           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     184             : 
     185           1 :   addrport = bridge_get_addr_port(bridge);
     186           1 :   tt_int_op(addrport->port, OP_EQ, 6666);
     187             : 
     188           1 :  done:
     189           1 :   mark_bridge_list();
     190           1 :   sweep_bridge_list();
     191           1 : }
     192             : 
     193             : /**
     194             :  * Calling get_configured_bridge_by_orports_digest() with two
     195             :  * configured bridge orports and an invalid digest should return the
     196             :  * bridge of the first addrport in the list.
     197             :  */
     198             : static void
     199           1 : test_bridges_get_configured_bridge_by_orports_digest(void *arg)
     200             : {
     201           1 :   smartlist_t *orports = NULL;
     202           1 :   const smartlist_t *bridgelist;
     203           1 :   const bridge_info_t *bridge1;
     204           1 :   const bridge_info_t *bridge2;
     205           1 :   const bridge_info_t *ret;
     206           1 :   tor_addr_port_t *addrport1;
     207           1 :   tor_addr_port_t *addrport2;
     208           1 :   const char *digest;
     209             : 
     210           1 :   helper_add_bridges_to_bridgelist(arg);
     211           1 :   bridgelist = bridge_list_get();
     212           1 :   tt_ptr_op(bridgelist, OP_NE, NULL);
     213             : 
     214             :   // This should be the bridge at 6.6.6.6:6666 with fingerprint
     215             :   // 0000000000000000000000000000000000000000
     216           1 :   bridge1 = smartlist_get(bridgelist, 0);
     217           1 :   tt_ptr_op(bridge1, OP_NE, NULL);
     218             :   // This should be the bridge at 6.6.6.7:6667 with fingerprint
     219             :   // A10C4F666D27364036B562823E5830BC448E046A
     220           1 :   bridge2 = smartlist_get(bridgelist, 1);
     221           1 :   tt_ptr_op(bridge2, OP_NE, NULL);
     222             : 
     223           1 :   addrport1 = (tor_addr_port_t*)bridge_get_addr_port(bridge1);
     224           1 :   tt_int_op(addrport1->port, OP_EQ, 6666);
     225           1 :   addrport2 = (tor_addr_port_t*)bridge_get_addr_port(bridge2);
     226           1 :   tt_int_op(addrport2->port, OP_EQ, 6667);
     227             : 
     228           1 :   orports = smartlist_new();
     229           1 :   smartlist_add(orports, addrport1);
     230           1 :   smartlist_add(orports, addrport2);
     231             : 
     232           1 :   digest = "zzzzzzzzzzzzzzzz";
     233             : 
     234           1 :   ret = get_configured_bridge_by_orports_digest(digest, orports);
     235           1 :   tt_ptr_op(ret, OP_NE, NULL);
     236             : 
     237           1 :   tt_assert(tor_addr_port_eq(addrport1, bridge_get_addr_port(ret)));
     238             : 
     239           1 :  done:
     240           1 :   smartlist_free(orports);
     241             : 
     242           1 :   mark_bridge_list();
     243           1 :   sweep_bridge_list();
     244           1 : }
     245             : 
     246             : /**
     247             :  * Calling get_configured_bridge_by_addr_port_digest() with a digest that we do
     248             :  * have and an addr:port pair we don't should return the bridge for that
     249             :  * digest.
     250             :  */
     251             : static void
     252           1 : test_bridges_get_configured_bridge_by_addr_port_digest_digest_only(void *arg)
     253             : {
     254           1 :   char digest[DIGEST_LEN];
     255           1 :   bridge_info_t *bridge;
     256           1 :   const char fingerprint[HEX_DIGEST_LEN] =
     257             :     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     258           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     259           1 :   char ret_addr[16];
     260           1 :   uint16_t port = 11111;
     261           1 :   int ret;
     262             : 
     263           1 :   helper_add_bridges_to_bridgelist(arg);
     264             : 
     265             :   // We don't actually have a bridge with this addr:port pair
     266           1 :   base16_decode(digest, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN);
     267           1 :   ret = tor_addr_parse(addr, "111.111.111.111");
     268           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     269             : 
     270           1 :   bridge = get_configured_bridge_by_addr_port_digest(addr, port, digest);
     271           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     272             : 
     273           1 :   tor_addr_to_str(ret_addr, &bridge_get_addr_port(bridge)->addr, 16, 0);
     274           1 :   tt_str_op("4.4.4.4", OP_EQ, ret_addr);
     275             : 
     276           1 :  done:
     277           1 :   tor_free(addr);
     278             : 
     279           1 :   mark_bridge_list();
     280           1 :   sweep_bridge_list();
     281           1 : }
     282             : 
     283             : /**
     284             :  * Calling get_configured_bridge_by_addr_port_digest() with only an
     285             :  * addr:port (i.e. digest set to NULL) should return the bridge for
     286             :  * that digest when there is such a bridge.
     287             :  */
     288             : static void
     289           1 : test_bridges_get_configured_bridge_by_addr_port_digest_address_only(void *arg)
     290             : {
     291           1 :   bridge_info_t *bridge;
     292           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     293           1 :   char ret_addr[16];
     294           1 :   uint16_t port = 6666;
     295           1 :   int ret;
     296             : 
     297           1 :   helper_add_bridges_to_bridgelist(arg);
     298             : 
     299           1 :   ret = tor_addr_parse(addr, "6.6.6.6");
     300           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     301             : 
     302           1 :   bridge = get_configured_bridge_by_addr_port_digest(addr, port, NULL);
     303           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     304             : 
     305           1 :   tor_addr_to_str(ret_addr, &bridge_get_addr_port(bridge)->addr, 16, 0);
     306           1 :   tt_str_op("6.6.6.6", OP_EQ, ret_addr);
     307             : 
     308           1 :  done:
     309           1 :   tor_free(addr);
     310             : 
     311           1 :   mark_bridge_list();
     312           1 :   sweep_bridge_list();
     313           1 : }
     314             : 
     315             : /**
     316             :  * Calling get_configured_bridge_by_exact_addr_port_digest() with a digest that
     317             :  * we do have, and an addr:port pair we don't have, should return NULL.
     318             :  */
     319             : static void
     320           1 : test_bridges_get_configured_bridge_by_exact_addr_port_digest_donly(void *arg)
     321             : {
     322           1 :   char digest[DIGEST_LEN];
     323           1 :   bridge_info_t *bridge;
     324           1 :   const char fingerprint[HEX_DIGEST_LEN] =
     325             :     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     326           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     327           1 :   uint16_t port = 11111;
     328           1 :   int ret;
     329             : 
     330           1 :   helper_add_bridges_to_bridgelist(arg);
     331             : 
     332             :   // We don't actually have a bridge with this addr:port pair
     333           1 :   base16_decode(digest, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN);
     334           1 :   ret = tor_addr_parse(addr, "111.111.111.111");
     335           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     336             : 
     337           1 :   bridge = get_configured_bridge_by_exact_addr_port_digest(addr, port, digest);
     338           1 :   tt_ptr_op(bridge, OP_EQ, NULL);
     339             : 
     340           1 :  done:
     341           1 :   tor_free(addr);
     342             : 
     343           1 :   mark_bridge_list();
     344           1 :   sweep_bridge_list();
     345           1 : }
     346             : 
     347             : /**
     348             :  * Calling get_configured_bridge_by_exact_addr_port_digest() with a digest that
     349             :  * we do have, and an addr:port pair we do have, should return the bridge.
     350             :  */
     351             : static void
     352           1 : test_bridges_get_configured_bridge_by_exact_addr_port_digest_both(void *arg)
     353             : {
     354           1 :   char digest[DIGEST_LEN];
     355           1 :   bridge_info_t *bridge;
     356           1 :   const char fingerprint[HEX_DIGEST_LEN] =
     357             :     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     358           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     359           1 :   uint16_t port = 4444;
     360           1 :   char ret_addr[16];
     361           1 :   int ret;
     362             : 
     363           1 :   helper_add_bridges_to_bridgelist(arg);
     364             : 
     365           1 :   base16_decode(digest, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN);
     366           1 :   ret = tor_addr_parse(addr, "4.4.4.4");
     367           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     368             : 
     369           1 :   bridge = get_configured_bridge_by_exact_addr_port_digest(addr, port, digest);
     370           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     371             : 
     372           1 :   tor_addr_to_str(ret_addr, &bridge_get_addr_port(bridge)->addr, 16, 0);
     373           1 :   tt_str_op("4.4.4.4", OP_EQ, ret_addr);
     374             : 
     375           1 :  done:
     376           1 :   tor_free(addr);
     377             : 
     378           1 :   mark_bridge_list();
     379           1 :   sweep_bridge_list();
     380           1 : }
     381             : 
     382             : /**
     383             :  * Calling get_configured_bridge_by_exact_addr_port_digest() with no digest,
     384             :  * and an addr:port pair we do have, should return the bridge.
     385             :  */
     386             : static void
     387           1 : test_bridges_get_configured_bridge_by_exact_addr_port_digest_aonly(void *arg)
     388             : {
     389           1 :   bridge_info_t *bridge;
     390           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     391           1 :   uint16_t port = 4444;
     392           1 :   char ret_addr[16];
     393           1 :   int ret;
     394             : 
     395           1 :   helper_add_bridges_to_bridgelist(arg);
     396             : 
     397           1 :   ret = tor_addr_parse(addr, "4.4.4.4");
     398           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     399             : 
     400           1 :   bridge = get_configured_bridge_by_exact_addr_port_digest(addr, port, NULL);
     401           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     402             : 
     403           1 :   tor_addr_to_str(ret_addr, &bridge_get_addr_port(bridge)->addr, 16, 0);
     404           1 :   tt_str_op("4.4.4.4", OP_EQ, ret_addr);
     405             : 
     406           1 :  done:
     407           1 :   tor_free(addr);
     408             : 
     409           1 :   mark_bridge_list();
     410           1 :   sweep_bridge_list();
     411           1 : }
     412             : 
     413             : /**
     414             :  * Calling find_bridge_by_digest() when we have a bridge with a known
     415             :  * identity digest should return the bridge's information.
     416             :  */
     417             : static void
     418           1 : test_bridges_find_bridge_by_digest_known(void *arg)
     419             : {
     420           1 :   char digest1[DIGEST_LEN];
     421           1 :   bridge_info_t *bridge;
     422           1 :   const char fingerprint[HEX_DIGEST_LEN] =
     423             :     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     424             : 
     425           1 :   helper_add_bridges_to_bridgelist(arg);
     426             : 
     427           1 :   base16_decode(digest1, DIGEST_LEN, fingerprint, HEX_DIGEST_LEN);
     428           1 :   bridge = find_bridge_by_digest(digest1);
     429             : 
     430           1 :   tt_ptr_op(bridge, OP_NE, NULL);
     431             : 
     432             :   /* We have to call bridge_get_rsa_id_digest() here because the bridge_info_t
     433             :    * struct is opaquely defined in bridges.h. */
     434           1 :   const uint8_t *digest2 = bridge_get_rsa_id_digest(bridge);
     435             : 
     436           1 :   tt_mem_op((char*)digest2, OP_EQ, digest1, DIGEST_LEN);
     437             : 
     438           1 :  done:
     439           1 :   mark_bridge_list();
     440           1 :   sweep_bridge_list();
     441           1 : }
     442             : 
     443             : /**
     444             :  * Calling find_bridge_by_digest() when we do NOT have a bridge with that
     445             :  * identity digest should return NULL.
     446             :  */
     447             : static void
     448           1 : test_bridges_find_bridge_by_digest_unknown(void *arg)
     449             : {
     450           1 :   const char *fingerprint = "cccccccccccccccccccccccccccccccccccccccc";
     451           1 :   bridge_info_t *bridge;
     452             : 
     453           1 :   helper_add_bridges_to_bridgelist(arg);
     454             : 
     455           1 :   bridge = find_bridge_by_digest(fingerprint);
     456             : 
     457           1 :   tt_ptr_op(bridge, OP_EQ, NULL);
     458             : 
     459           1 :  done:
     460           1 :   mark_bridge_list();
     461           1 :   sweep_bridge_list();
     462           1 : }
     463             : 
     464             : /**
     465             :  * Calling bridge_resolve_conflicts() with an identical bridge to one we've
     466             :  * already configure should mark the pre-configured bridge for removal.
     467             :  */
     468             : static void
     469           1 : test_bridges_bridge_resolve_conflicts(void *arg)
     470             : {
     471           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     472           1 :   uint16_t port = 4444;
     473           1 :   const char *digest = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
     474           1 :   const char *transport = "apple";
     475           1 :   int ret;
     476             : 
     477           1 :   helper_add_bridges_to_bridgelist(arg);
     478             : 
     479           1 :   ret = tor_addr_parse(addr, "4.4.4.4");
     480           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success
     481             : 
     482           1 :   bridge_resolve_conflicts((const tor_addr_t*)addr, port, digest, transport);
     483             : 
     484             :   /* The bridge should now be marked for removal, and removed when we sweep the
     485             :    * bridge_list */
     486           1 :   sweep_bridge_list();
     487           1 :   ret = addr_is_a_configured_bridge((const tor_addr_t*)addr, port, digest);
     488           1 :   tt_int_op(ret, OP_EQ, 0);
     489             : 
     490           1 :  done:
     491           1 :   tor_free(addr);
     492             : 
     493           1 :   mark_bridge_list();
     494           1 :   sweep_bridge_list();
     495           1 : }
     496             : 
     497             : /**
     498             :  * Calling transport_is_needed() with a transport we do need ("obfs4") and a
     499             :  * bogus transport that we don't need should return 1 and 0, respectively.
     500             :  */
     501             : static void
     502           1 : test_bridges_transport_is_needed(void *arg)
     503             : {
     504           1 :   int ret;
     505             : 
     506           1 :   helper_add_bridges_to_bridgelist(arg);
     507             : 
     508           1 :   ret = transport_is_needed("obfs4");
     509           1 :   tt_int_op(ret, OP_EQ, 1);
     510             : 
     511           1 :   ret = transport_is_needed("apowefjaoewpaief");
     512           1 :   tt_int_op(ret, OP_EQ, 0);
     513             : 
     514           1 :  done:
     515           1 :   mark_bridge_list();
     516           1 :   sweep_bridge_list();
     517           1 : }
     518             : 
     519             : /**
     520             :  * Calling get_transport_by_bridge_addrport() with the address and port of a
     521             :  * configured bridge which uses a pluggable transport when there is no global
     522             :  * transport_list should return -1 and the transport_t should be NULL.
     523             :  */
     524             : static void
     525           1 : test_bridges_get_transport_by_bridge_addrport_no_ptlist(void *arg)
     526             : {
     527           1 :   transport_t *transport = NULL;
     528           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     529           1 :   uint16_t port = 1234;
     530           1 :   int ret;
     531             : 
     532           1 :   helper_add_bridges_to_bridgelist(arg);
     533             : 
     534           1 :   ret = tor_addr_parse(addr, "1.2.3.4");
     535           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success?
     536             : 
     537             :   /* This will fail because the global transport_list has nothing in it, and so
     538             :    * transport_get_by_name() has nothing to return, even the the bridge *did*
     539             :    * say it had an obfs4 transport.
     540             :    */
     541           1 :   ret = get_transport_by_bridge_addrport((const tor_addr_t*)addr, port,
     542             :                                          (const transport_t**)&transport);
     543           1 :   tt_int_op(ret, OP_EQ, -1); // returns -1 on failure
     544           1 :   tt_ptr_op(transport, OP_EQ, NULL);
     545             : 
     546           1 :  done:
     547           1 :   tor_free(addr);
     548             : 
     549           1 :   mark_bridge_list();
     550           1 :   sweep_bridge_list();
     551           1 : }
     552             : 
     553             : /**
     554             :  * Calling get_transport_by_bridge_addrport() with the address and port of a
     555             :  * configured bridge which uses a pluggable transport should return 0 and set
     556             :  * appropriate transport_t.
     557             :  */
     558             : static void
     559           1 : test_bridges_get_transport_by_bridge_addrport(void *arg)
     560             : {
     561           1 :   transport_t *transport = NULL;
     562           1 :   tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t));
     563           1 :   uint16_t port = 1234;
     564           1 :   int ret;
     565             : 
     566           1 :   helper_add_bridges_to_bridgelist(arg);
     567           1 :   mark_transport_list(); // Also initialise our transport_list
     568             : 
     569           1 :   ret = tor_addr_parse(addr, "1.2.3.4");
     570           1 :   tt_int_op(ret, OP_EQ, 2); // it returns the address family on success?
     571             : 
     572             :   /* After we mock transport_get_by_name() to return a bogus transport_t with
     573             :    * the name it was asked for, the call should succeed.
     574             :    */
     575           1 :   MOCK(transport_get_by_name, mock_transport_get_by_name);
     576           1 :   ret = get_transport_by_bridge_addrport((const tor_addr_t*)addr, port,
     577             :                                          (const transport_t**)&transport);
     578           1 :   tt_int_op(ret, OP_EQ, 0); // returns 0 on success
     579           1 :   tt_ptr_op(transport, OP_NE, NULL);
     580           1 :   tt_str_op(transport->name, OP_EQ, "obfs4");
     581             : 
     582           1 :  done:
     583           1 :   UNMOCK(transport_get_by_name);
     584             : 
     585           1 :   tor_free(addr);
     586           1 :   transport_free(transport);
     587             : 
     588           1 :   mark_bridge_list();
     589           1 :   sweep_bridge_list();
     590           1 : }
     591             : 
     592             : static void
     593           1 : test_bridges_node_is_a_configured_bridge(void *arg)
     594             : {
     595             : 
     596           1 :   routerinfo_t ri_ipv4 = { .ipv4_orport = 6666 };
     597           1 :   tor_addr_parse(&ri_ipv4.ipv4_addr, "6.6.6.6");
     598             : 
     599           1 :   routerstatus_t rs_ipv4 = { .ipv4_orport = 6666 };
     600           1 :   tor_addr_parse(&rs_ipv4.ipv4_addr, "6.6.6.6");
     601             : 
     602           1 :   routerinfo_t ri_ipv6 = { .ipv6_orport = 6666 };
     603           1 :   tor_addr_parse(&(ri_ipv6.ipv6_addr),
     604             :                  "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
     605             : 
     606           1 :   routerstatus_t rs_ipv6 = { .ipv6_orport = 6666 };
     607           1 :   tor_addr_parse(&(rs_ipv6.ipv6_addr),
     608             :                  "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
     609             : 
     610           1 :   microdesc_t md_ipv6 = { .ipv6_orport = 6666 };
     611           1 :   tor_addr_parse(&(md_ipv6.ipv6_addr),
     612             :                  "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
     613             : 
     614           1 :   helper_add_bridges_to_bridgelist(arg);
     615             : 
     616           1 :   node_t node_with_digest;
     617           1 :   memset(&node_with_digest, 0, sizeof(node_with_digest));
     618             : 
     619           1 :   const char fingerprint[HEX_DIGEST_LEN] =
     620             :     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     621             : 
     622           1 :   const char fingerprint2[HEX_DIGEST_LEN] =
     623             :     "ffffffffffffffffffffffffffffffffffffffff";
     624             : 
     625           1 :   base16_decode(node_with_digest.identity, DIGEST_LEN,
     626             :                 fingerprint, HEX_DIGEST_LEN);
     627             : 
     628           1 :   node_t node_ri_ipv4 = { .ri = &ri_ipv4 };
     629           1 :   base16_decode(node_ri_ipv4.identity, DIGEST_LEN,
     630             :                 fingerprint2, HEX_DIGEST_LEN);
     631           1 :   tt_assert(node_is_a_configured_bridge(&node_ri_ipv4));
     632             : 
     633             :   /* This will still match bridge0, since bridge0 has no digest set. */
     634           1 :   memset(node_ri_ipv4.identity, 0x3f, DIGEST_LEN);
     635           1 :   tt_assert(node_is_a_configured_bridge(&node_ri_ipv4));
     636             : 
     637             :   /* It won't match bridge1, though, since bridge1 has a digest, and this
     638             :      isn't it! */
     639           1 :   tor_addr_parse(&node_ri_ipv4.ri->ipv4_addr, "6.6.6.7");
     640           1 :   node_ri_ipv4.ri->ipv4_orport = 6667;
     641           1 :   tt_assert(! node_is_a_configured_bridge(&node_ri_ipv4));
     642             :   /* If we set the fingerprint right, though, it will match. */
     643           1 :   base16_decode(node_ri_ipv4.identity, DIGEST_LEN,
     644             :                 "A10C4F666D27364036B562823E5830BC448E046A", HEX_DIGEST_LEN);
     645           1 :   tt_assert(node_is_a_configured_bridge(&node_ri_ipv4));
     646             : 
     647           1 :   node_t node_rs_ipv4 = { .rs = &rs_ipv4 };
     648           1 :   base16_decode(node_rs_ipv4.identity, DIGEST_LEN,
     649             :                 fingerprint2, HEX_DIGEST_LEN);
     650           1 :   tt_assert(node_is_a_configured_bridge(&node_rs_ipv4));
     651             : 
     652           1 :   node_t node_ri_ipv6 = { .ri = &ri_ipv6 };
     653           1 :   base16_decode(node_ri_ipv6.identity, DIGEST_LEN,
     654             :                 fingerprint2, HEX_DIGEST_LEN);
     655           1 :   tt_assert(node_is_a_configured_bridge(&node_ri_ipv6));
     656             : 
     657           1 :   node_t node_rs_ipv6 = { .rs = &rs_ipv6 };
     658           1 :   base16_decode(node_rs_ipv6.identity, DIGEST_LEN,
     659             :                 fingerprint2, HEX_DIGEST_LEN);
     660           1 :   tt_assert(node_is_a_configured_bridge(&node_rs_ipv6));
     661             : 
     662           1 :   node_t node_md_ipv6 = { .md = &md_ipv6 };
     663           1 :   base16_decode(node_md_ipv6.identity, DIGEST_LEN,
     664             :                 fingerprint2, HEX_DIGEST_LEN);
     665           1 :   tt_assert(node_is_a_configured_bridge(&node_md_ipv6));
     666             : 
     667           1 :   mark_bridge_list();
     668           1 :   sweep_bridge_list();
     669             : 
     670           1 :   tt_assert(!node_is_a_configured_bridge(&node_with_digest));
     671           1 :   tt_assert(!node_is_a_configured_bridge(&node_ri_ipv4));
     672           1 :   tt_assert(!node_is_a_configured_bridge(&node_ri_ipv6));
     673           1 :   tt_assert(!node_is_a_configured_bridge(&node_rs_ipv4));
     674           1 :   tt_assert(!node_is_a_configured_bridge(&node_rs_ipv6));
     675           1 :   tt_assert(!node_is_a_configured_bridge(&node_md_ipv6));
     676             : 
     677           1 :  done:
     678           1 :   mark_bridge_list();
     679           1 :   sweep_bridge_list();
     680           1 : }
     681             : 
     682             : #undef PT_PRIVATE /* defined(PT_PRIVATE) */
     683             : 
     684             : #define B_TEST(name, flags) \
     685             :   { #name, test_bridges_ ##name, (flags), NULL, NULL }
     686             : 
     687             : struct testcase_t bridges_tests[] = {
     688             :   B_TEST(helper_func_add_bridges_to_bridgelist, 0),
     689             :   B_TEST(bridge_list_get_creates_new_bridgelist, 0),
     690             :   B_TEST(clear_bridge_list, 0),
     691             :   B_TEST(bridge_get_addrport, 0),
     692             :   B_TEST(get_configured_bridge_by_orports_digest, 0),
     693             :   B_TEST(get_configured_bridge_by_addr_port_digest_digest_only, 0),
     694             :   B_TEST(get_configured_bridge_by_addr_port_digest_address_only, 0),
     695             :   B_TEST(get_configured_bridge_by_exact_addr_port_digest_donly, 0),
     696             :   B_TEST(get_configured_bridge_by_exact_addr_port_digest_both, 0),
     697             :   B_TEST(get_configured_bridge_by_exact_addr_port_digest_aonly, 0),
     698             :   B_TEST(find_bridge_by_digest_known, 0),
     699             :   B_TEST(find_bridge_by_digest_unknown, 0),
     700             :   B_TEST(bridge_resolve_conflicts, 0),
     701             :   B_TEST(get_transport_by_bridge_addrport_no_ptlist, 0),
     702             :   B_TEST(get_transport_by_bridge_addrport, 0),
     703             :   B_TEST(transport_is_needed, 0),
     704             :   B_TEST(node_is_a_configured_bridge, 0),
     705             :   END_OF_TESTCASES
     706             : };

Generated by: LCOV version 1.14