1 #include "gtest/gtest.h"
6 #include "nsComponentManagerUtils.h"
7 #include "../../base/nsProtocolProxyService.h"
8 #include "nsServiceManagerUtils.h"
9 #include "mozilla/Preferences.h"
10 #include "nsNetUtil.h"
15 TEST(TestProtocolProxyService
, LoadHostFilters
)
17 nsCOMPtr
<nsIProtocolProxyService2
> ps
=
18 do_GetService(NS_PROTOCOLPROXYSERVICE_CID
);
20 mozilla::net::nsProtocolProxyService
* pps
=
21 static_cast<mozilla::net::nsProtocolProxyService
*>(ps
.get());
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
);
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
);
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
);
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
);
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
);
56 spec
= "http://[2001::1]";
57 ASSERT_EQ(NS_NewURI(getter_AddRefs(url
), spec
), NS_OK
);
58 ASSERT_EQ(pps
->CanUseProxy(url
, 80), expected
);
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
);
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
);
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
);
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
);
79 auto CheckLocalDomain
= [&](bool expected
) {
81 ASSERT_EQ(NS_NewURI(getter_AddRefs(url
), spec
), NS_OK
);
82 ASSERT_EQ(pps
->CanUseProxy(url
, 80), expected
);
85 // --------------------------------------------------------------------------
89 // Anything is allowed when there are no filters set
90 printf("Testing empty filter: %s\n", filter
.get());
91 pps
->LoadHostFilters(filter
);
93 CheckLoopbackURLs(false);
94 CheckLocalDomain(true);
96 CheckPortDomain(true);
98 // --------------------------------------------------------------------------
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
);
106 CheckLoopbackURLs(false);
107 // Check URLs can no longer use filtered proxy
109 CheckLocalDomain(true);
110 CheckPortDomain(true);
112 // --------------------------------------------------------------------------
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
);
120 CheckLoopbackURLs(false);
122 CheckLocalDomain(false);
123 CheckPortDomain(false);
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
);
130 // Check that filtering works properly when the filter is set to "<local>"
132 printf("Testing filter: %s\n", filter
.get());
133 pps
->LoadHostFilters(filter
);
135 CheckLoopbackURLs(false);
137 CheckLocalDomain(false);
138 CheckPortDomain(true);
140 // Check that allow_hijacking_localhost works with empty filter
141 Preferences::SetBool("network.proxy.allow_hijacking_localhost", true);
144 printf("Testing filter: %s\n", filter
.get());
145 pps
->LoadHostFilters(filter
);
147 CheckLoopbackURLs(true);
148 CheckLocalDomain(true);
150 CheckPortDomain(true);
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
);
157 CheckLoopbackURLs(false);
158 CheckLocalDomain(true);
160 CheckPortDomain(false);
164 } // namespace mozilla