Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/test/gtest/TestProtocolProxyService.cpp
Line
Count
Source (jump to first uncovered line)
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
0
TEST(TestProtocolProxyService, LoadHostFilters) {
16
0
  nsCOMPtr<nsIProtocolProxyService2> ps = do_GetService(NS_PROTOCOLPROXYSERVICE_CID);
17
0
  ASSERT_TRUE(ps);
18
0
  mozilla::net::nsProtocolProxyService* pps = static_cast<mozilla::net::nsProtocolProxyService*>(ps.get());
19
0
20
0
  nsCOMPtr<nsIURI> url;
21
0
  nsAutoCString spec;
22
0
23
0
  auto CheckLoopbackURLs = [&](bool expected)
24
0
  {
25
0
    // loopback IPs are always filtered
26
0
    spec = "http://127.0.0.1";
27
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
28
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
29
0
    spec = "http://[::1]";
30
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
31
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
32
0
  };
33
0
34
0
  auto CheckURLs = [&](bool expected)
35
0
  {
36
0
    spec = "http://example.com";
37
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
38
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
39
0
40
0
    spec = "https://10.2.3.4";
41
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
42
0
    ASSERT_EQ(pps->CanUseProxy(url, 443), expected);
43
0
44
0
    spec = "http://1.2.3.4";
45
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
46
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
47
0
48
0
    spec = "http://1.2.3.4:8080";
49
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
50
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
51
0
52
0
    spec = "http://[2001::1]";
53
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
54
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
55
0
56
0
    spec = "http://2.3.4.5:7777";
57
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
58
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
59
0
60
0
    spec = "http://[abcd::2]:123";
61
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
62
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
63
0
64
0
    spec = "http://bla.test.com";
65
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
66
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
67
0
  };
68
0
69
0
  auto CheckPortDomain = [&](bool expected)
70
0
  {
71
0
    spec = "http://blabla.com:10";
72
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
73
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
74
0
  };
75
0
76
0
  auto CheckLocalDomain = [&](bool expected)
77
0
  {
78
0
    spec = "http://test";
79
0
    ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
80
0
    ASSERT_EQ(pps->CanUseProxy(url, 80), expected);
81
0
  };
82
0
83
0
  // --------------------------------------------------------------------------
84
0
85
0
  nsAutoCString filter;
86
0
87
0
  // Anything is allowed when there are no filters set
88
0
  printf("Testing empty filter: %s\n", filter.get());
89
0
  pps->LoadHostFilters(filter);
90
0
91
0
  CheckLoopbackURLs(true); // only time when loopbacks can be proxied. bug?
92
0
  CheckLocalDomain(true);
93
0
  CheckURLs(true);
94
0
  CheckPortDomain(true);
95
0
96
0
  // --------------------------------------------------------------------------
97
0
98
0
  filter = "example.com, 1.2.3.4/16, [2001::1], 10.0.0.0/8, 2.3.0.0/16:7777, [abcd::1]/64:123, *.test.com";
99
0
  printf("Testing filter: %s\n", filter.get());
100
0
  pps->LoadHostFilters(filter);
101
0
  // Check URLs can no longer use filtered proxy
102
0
  CheckURLs(false);
103
0
  CheckLoopbackURLs(false);
104
0
  CheckLocalDomain(true);
105
0
  CheckPortDomain(true);
106
0
107
0
  // --------------------------------------------------------------------------
108
0
109
0
  // This is space separated. See bug 1346711 comment 4. We check this to keep
110
0
  // backwards compatibility.
111
0
  filter = "<local> blabla.com:10";
112
0
  printf("Testing filter: %s\n", filter.get());
113
0
  pps->LoadHostFilters(filter);
114
0
  CheckURLs(true);
115
0
  CheckLoopbackURLs(false);
116
0
  CheckLocalDomain(false);
117
0
  CheckPortDomain(false);
118
0
119
0
  // Check that we don't crash on weird input
120
0
  filter = "a b c abc:1x2, ,, * ** *.* *:10 :20 :40/12 */12:90";
121
0
  printf("Testing filter: %s\n", filter.get());
122
0
  pps->LoadHostFilters(filter);
123
0
124
0
  // Check that filtering works properly when the filter is set to "<local>"
125
0
  filter = "<local>";
126
0
  printf("Testing filter: %s\n", filter.get());
127
0
  pps->LoadHostFilters(filter);
128
0
  CheckURLs(true);
129
0
  CheckLoopbackURLs(false);
130
0
  CheckLocalDomain(false);
131
0
  CheckPortDomain(true);
132
0
}
133
134
} // namespace net
135
} // namespace mozila