tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestProtocolProxyService.cpp (5099B)


      1 #include "gtest/gtest.h"
      2 
      3 #include "nsCOMPtr.h"
      4 #include "nsNetCID.h"
      5 #include "nsString.h"
      6 #include "nsComponentManagerUtils.h"
      7 #include "../../base/nsProtocolProxyService.h"
      8 #include "nsServiceManagerUtils.h"
      9 #include "mozilla/Preferences.h"
     10 #include "nsNetUtil.h"
     11 
     12 namespace mozilla {
     13 namespace net {
     14 
     15 TEST(TestProtocolProxyService, LoadHostFilters)
     16 {
     17  nsCOMPtr<nsIProtocolProxyService2> ps =
     18      do_GetService(NS_PROTOCOLPROXYSERVICE_CID);
     19  ASSERT_TRUE(ps);
     20  mozilla::net::nsProtocolProxyService* pps =
     21      static_cast<mozilla::net::nsProtocolProxyService*>(ps.get());
     22 
     23  nsCOMPtr<nsIURI> url;
     24  nsAutoCString spec;
     25 
     26  auto CheckLoopbackURLs = [&](bool expected) {
     27    // loopback IPs are always filtered
     28    spec = "http://127.0.0.1";
     29    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     30    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     31    spec = "http://[::1]";
     32    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     33    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     34    spec = "http://localhost";
     35    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     36    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     37  };
     38 
     39  auto CheckURLs = [&](bool expected) {
     40    spec = "http://example.com";
     41    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     42    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     43 
     44    spec = "https://10.2.3.4";
     45    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     46    ASSERT_EQ(pps->CanUseProxy(url, 443), expected);
     47 
     48    spec = "http://1.2.3.4";
     49    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     50    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     51 
     52    spec = "http://1.2.3.4:8080";
     53    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     54    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     55 
     56    spec = "http://[2001::1]";
     57    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     58    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     59 
     60    spec = "http://2.3.4.5:7777";
     61    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     62    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     63 
     64    spec = "http://[abcd::2]:123";
     65    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     66    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     67 
     68    spec = "http://bla.test.com";
     69    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     70    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     71  };
     72 
     73  auto CheckPortDomain = [&](bool expected) {
     74    spec = "http://blabla.com:10";
     75    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     76    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     77  };
     78 
     79  auto CheckLocalDomain = [&](bool expected) {
     80    spec = "http://test";
     81    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
     82    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
     83  };
     84 
     85  // --------------------------------------------------------------------------
     86 
     87  nsAutoCString filter;
     88 
     89  // Anything is allowed when there are no filters set
     90  printf("Testing empty filter: %s\n", filter.get());
     91  pps->LoadHostFilters(filter);
     92 
     93  CheckLoopbackURLs(false);
     94  CheckLocalDomain(true);
     95  CheckURLs(true);
     96  CheckPortDomain(true);
     97 
     98  // --------------------------------------------------------------------------
     99 
    100  filter =
    101      "example.com, 1.2.3.4/16, [2001::1], 10.0.0.0/8, 2.3.0.0/16:7777, "
    102      "[abcd::1]/64:123, *.test.com";
    103  printf("Testing filter: %s\n", filter.get());
    104  pps->LoadHostFilters(filter);
    105 
    106  CheckLoopbackURLs(false);
    107  // Check URLs can no longer use filtered proxy
    108  CheckURLs(false);
    109  CheckLocalDomain(true);
    110  CheckPortDomain(true);
    111 
    112  // --------------------------------------------------------------------------
    113 
    114  // This is space separated. See bug 1346711 comment 4. We check this to keep
    115  // backwards compatibility.
    116  filter = "<local> blabla.com:10";
    117  printf("Testing filter: %s\n", filter.get());
    118  pps->LoadHostFilters(filter);
    119 
    120  CheckLoopbackURLs(false);
    121  CheckURLs(true);
    122  CheckLocalDomain(false);
    123  CheckPortDomain(false);
    124 
    125  // Check that we don't crash on weird input
    126  filter = "a b c abc:1x2, ,, * ** *.* *:10 :20 :40/12 */12:90";
    127  printf("Testing filter: %s\n", filter.get());
    128  pps->LoadHostFilters(filter);
    129 
    130  // Check that filtering works properly when the filter is set to "<local>"
    131  filter = "<local>";
    132  printf("Testing filter: %s\n", filter.get());
    133  pps->LoadHostFilters(filter);
    134 
    135  CheckLoopbackURLs(false);
    136  CheckURLs(true);
    137  CheckLocalDomain(false);
    138  CheckPortDomain(true);
    139 
    140  // Check that allow_hijacking_localhost works with empty filter
    141  Preferences::SetBool("network.proxy.allow_hijacking_localhost", true);
    142 
    143  filter = "";
    144  printf("Testing filter: %s\n", filter.get());
    145  pps->LoadHostFilters(filter);
    146 
    147  CheckLoopbackURLs(true);
    148  CheckLocalDomain(true);
    149  CheckURLs(true);
    150  CheckPortDomain(true);
    151 
    152  // Check that allow_hijacking_localhost works with non-trivial filter
    153  filter = "127.0.0.1, [::1], localhost, blabla.com:10";
    154  printf("Testing filter: %s\n", filter.get());
    155  pps->LoadHostFilters(filter);
    156 
    157  CheckLoopbackURLs(false);
    158  CheckLocalDomain(true);
    159  CheckURLs(true);
    160  CheckPortDomain(false);
    161 }
    162 
    163 }  // namespace net
    164 }  // namespace mozilla