1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 // Test values to replacethe values specified in preprocessor defines.
14 static const char kDefaultKey
[] = "test-key";
15 static const char kDefaultDevOrigin
[] = "https://dev.net:443/";
16 static const char kDefaultOrigin
[] = "https://origin.net:443/";
17 static const char kDefaultFallbackOrigin
[] = "http://fallback.net:80/";
18 static const char kDefaultSSLOrigin
[] = "http://ssl.net:1080/";
19 static const char kDefaultAltOrigin
[] = "https://alt.net:443/";
20 static const char kDefaultAltFallbackOrigin
[] = "http://altfallback.net:80/";
21 static const char kDefaultProbeURL
[] = "http://probe.net/";
23 static const char kFlagKey
[] = "test-flag-key";
24 static const char kFlagOrigin
[] = "https://origin.org:443/";
25 static const char kFlagFallbackOrigin
[] = "http://fallback.org:80/";
26 static const char kFlagSSLOrigin
[] = "http://ssl.org:1080/";
27 static const char kFlagAltOrigin
[] = "https://alt.org:443/";
28 static const char kFlagAltFallbackOrigin
[] = "http://altfallback.org:80/";
29 static const char kFlagProbeURL
[] = "http://probe.org/";
31 // Used to emulate having constants defined by the preprocessor.
32 static const unsigned int HAS_NOTHING
= 0x0;
33 static const unsigned int HAS_KEY
= 0x1;
34 static const unsigned int HAS_DEV_ORIGIN
= 0x2;
35 static const unsigned int HAS_ORIGIN
= 0x4;
36 static const unsigned int HAS_FALLBACK_ORIGIN
= 0x8;
37 static const unsigned int HAS_SSL_ORIGIN
= 0x10;
38 static const unsigned int HAS_ALT_ORIGIN
= 0x20;
39 static const unsigned int HAS_ALT_FALLBACK_ORIGIN
= 0x40;
40 static const unsigned int HAS_PROBE_URL
= 0x80;
41 static const unsigned int HAS_EVERYTHING
= 0xff;
44 namespace data_reduction_proxy
{
46 class TestDataReductionProxyParams
: public DataReductionProxyParams
{
49 TestDataReductionProxyParams(int flags
,
50 unsigned int has_definitions
)
51 : DataReductionProxyParams(flags
,
53 has_definitions_(has_definitions
) {
54 init_result_
= Init(flags
& DataReductionProxyParams::kAllowed
,
55 flags
& DataReductionProxyParams::kFallbackAllowed
,
56 flags
& DataReductionProxyParams::kAlternativeAllowed
);
59 bool init_result() const {
64 virtual std::string
GetDefaultKey() const OVERRIDE
{
65 return GetDefinition(HAS_KEY
, kDefaultKey
);
68 virtual std::string
GetDefaultDevOrigin() const OVERRIDE
{
69 return GetDefinition(HAS_DEV_ORIGIN
, kDefaultDevOrigin
);
72 virtual std::string
GetDefaultOrigin() const OVERRIDE
{
73 return GetDefinition(HAS_ORIGIN
, kDefaultOrigin
);
76 virtual std::string
GetDefaultFallbackOrigin() const OVERRIDE
{
77 return GetDefinition(HAS_FALLBACK_ORIGIN
, kDefaultFallbackOrigin
);
80 virtual std::string
GetDefaultSSLOrigin() const OVERRIDE
{
81 return GetDefinition(HAS_SSL_ORIGIN
, kDefaultSSLOrigin
);
84 virtual std::string
GetDefaultAltOrigin() const OVERRIDE
{
85 return GetDefinition(HAS_ALT_ORIGIN
, kDefaultAltOrigin
);
88 virtual std::string
GetDefaultAltFallbackOrigin() const OVERRIDE
{
89 return GetDefinition(HAS_ALT_FALLBACK_ORIGIN
, kDefaultAltFallbackOrigin
);
92 virtual std::string
GetDefaultProbeURL() const OVERRIDE
{
93 return GetDefinition(HAS_PROBE_URL
, kDefaultProbeURL
);
97 std::string
GetDefinition(unsigned int has_def
,
98 const std::string
& definition
) const {
99 return ((has_definitions_
& has_def
) ? definition
: std::string());
102 unsigned int has_definitions_
;
107 class DataReductionProxyParamsTest
: public testing::Test
{
109 void CheckParams(const TestDataReductionProxyParams
& params
,
110 bool expected_init_result
,
111 bool expected_allowed
,
112 bool expected_fallback_allowed
,
113 bool expected_alternative_allowed
,
114 bool expected_promo_allowed
) {
115 EXPECT_EQ(expected_init_result
, params
.init_result());
116 EXPECT_EQ(expected_allowed
, params
.allowed());
117 EXPECT_EQ(expected_fallback_allowed
, params
.fallback_allowed());
118 EXPECT_EQ(expected_alternative_allowed
, params
.alternative_allowed());
119 EXPECT_EQ(expected_promo_allowed
, params
.promo_allowed());
121 void CheckValues(const TestDataReductionProxyParams
& params
,
122 const std::string expected_key
,
123 const std::string
& expected_origin
,
124 const std::string
& expected_fallback_origin
,
125 const std::string
& expected_ssl_origin
,
126 const std::string
& expected_alt_origin
,
127 const std::string
& expected_alt_fallback_origin
,
128 const std::string
& expected_probe_url
) {
129 EXPECT_EQ(expected_key
, params
.key());
130 EXPECT_EQ(GURL(expected_origin
), params
.origin());
131 EXPECT_EQ(GURL(expected_fallback_origin
), params
.fallback_origin());
132 EXPECT_EQ(GURL(expected_ssl_origin
), params
.ssl_origin());
133 EXPECT_EQ(GURL(expected_alt_origin
), params
.alt_origin());
134 EXPECT_EQ(GURL(expected_alt_fallback_origin
), params
.alt_fallback_origin());
135 EXPECT_EQ(GURL(expected_probe_url
), params
.probe_url());
139 TEST_F(DataReductionProxyParamsTest
, EverythingDefined
) {
140 TestDataReductionProxyParams
params(
141 DataReductionProxyParams::kAllowed
|
142 DataReductionProxyParams::kFallbackAllowed
|
143 DataReductionProxyParams::kPromoAllowed
, HAS_EVERYTHING
);
144 CheckParams(params
, true, true, true, false, true);
148 kDefaultFallbackOrigin
,
151 kDefaultAltFallbackOrigin
,
155 TEST_F(DataReductionProxyParamsTest
, NoDevOrigin
) {
156 TestDataReductionProxyParams
params(
157 DataReductionProxyParams::kAllowed
|
158 DataReductionProxyParams::kFallbackAllowed
|
159 DataReductionProxyParams::kPromoAllowed
,
160 HAS_EVERYTHING
& ~HAS_DEV_ORIGIN
);
161 CheckParams(params
, true, true, true, false, true);
165 kDefaultFallbackOrigin
,
168 kDefaultAltFallbackOrigin
,
172 TEST_F(DataReductionProxyParamsTest
, Flags
) {
173 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
174 switches::kDataReductionProxyKey
, kFlagKey
);
175 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
176 switches::kDataReductionProxy
, kFlagOrigin
);
177 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
178 switches::kDataReductionProxyFallback
, kFlagFallbackOrigin
);
179 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
180 switches::kDataReductionSSLProxy
, kFlagSSLOrigin
);
181 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
182 switches::kDataReductionProxyAlt
, kFlagAltOrigin
);
183 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
184 switches::kDataReductionProxyAltFallback
, kFlagAltFallbackOrigin
);
185 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
186 switches::kDataReductionProxyProbeURL
, kFlagProbeURL
);
187 TestDataReductionProxyParams
params(
188 DataReductionProxyParams::kAllowed
|
189 DataReductionProxyParams::kFallbackAllowed
|
190 DataReductionProxyParams::kAlternativeAllowed
|
191 DataReductionProxyParams::kPromoAllowed
, HAS_EVERYTHING
);
192 CheckParams(params
, true, true, true, true, true);
199 kFlagAltFallbackOrigin
,
203 TEST_F(DataReductionProxyParamsTest
, InvalidConfigurations
) {
206 bool fallback_allowed
;
207 bool alternative_allowed
;
209 unsigned int missing_definitions
;
210 bool expected_result
;
212 { true, true, true, true, HAS_NOTHING
, true },
213 { true, true, true, true, HAS_KEY
, true },
214 { true, true, true, true, HAS_DEV_ORIGIN
, true },
215 { true, true, true, true, HAS_ORIGIN
, true },
216 { true, true, true, true, HAS_ORIGIN
| HAS_DEV_ORIGIN
, false },
217 { true, true, true, true, HAS_FALLBACK_ORIGIN
, false },
218 { true, true, true, true, HAS_SSL_ORIGIN
, false },
219 { true, true, true, true, HAS_ALT_ORIGIN
, false },
220 { true, true, true, true, HAS_ALT_FALLBACK_ORIGIN
, false },
221 { true, true, true, true, HAS_PROBE_URL
, false },
223 { true, false, true, true, HAS_NOTHING
, true },
224 { true, false, true, true, HAS_KEY
, true },
225 { true, false, true, true, HAS_ORIGIN
| HAS_DEV_ORIGIN
, false },
226 { true, false, true, true, HAS_FALLBACK_ORIGIN
, true },
227 { true, false, true, true, HAS_SSL_ORIGIN
, false },
228 { true, false, true, true, HAS_ALT_ORIGIN
, false },
229 { true, false, true, true, HAS_ALT_FALLBACK_ORIGIN
, true },
230 { true, false, true, true, HAS_PROBE_URL
, false },
232 { true, true, false, true, HAS_NOTHING
, true },
233 { true, true, false, true, HAS_KEY
, true },
234 { true, true, false, true, HAS_ORIGIN
| HAS_DEV_ORIGIN
, false },
235 { true, true, false, true, HAS_FALLBACK_ORIGIN
, false },
236 { true, true, false, true, HAS_SSL_ORIGIN
, true },
237 { true, true, false, true, HAS_ALT_ORIGIN
, true },
238 { true, true, false, true, HAS_ALT_FALLBACK_ORIGIN
, true },
239 { true, true, false, true, HAS_PROBE_URL
, false },
241 { true, false, false, true, HAS_KEY
, true },
242 { true, false, false, true, HAS_ORIGIN
| HAS_DEV_ORIGIN
, false },
243 { true, false, false, true, HAS_FALLBACK_ORIGIN
, true },
244 { true, false, false, true, HAS_SSL_ORIGIN
, true },
245 { true, false, false, true, HAS_ALT_ORIGIN
, true },
246 { true, false, false, true, HAS_ALT_FALLBACK_ORIGIN
, true },
247 { true, false, false, true, HAS_PROBE_URL
, false },
249 { false, true, true, true, HAS_NOTHING
, false },
250 { false, true, true, true, HAS_KEY
, false },
251 { false, true, true, true, HAS_ORIGIN
| HAS_DEV_ORIGIN
, false },
252 { false, true, true, true, HAS_FALLBACK_ORIGIN
, false },
253 { false, true, true, true, HAS_SSL_ORIGIN
, false },
254 { false, true, true, true, HAS_ALT_ORIGIN
, false },
255 { false, true, true, true, HAS_ALT_FALLBACK_ORIGIN
, false },
256 { false, true, true, true, HAS_PROBE_URL
, false },
259 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
261 if (tests
[i
].allowed
)
262 flags
|= DataReductionProxyParams::kAllowed
;
263 if (tests
[i
].fallback_allowed
)
264 flags
|= DataReductionProxyParams::kFallbackAllowed
;
265 if (tests
[i
].alternative_allowed
)
266 flags
|= DataReductionProxyParams::kAlternativeAllowed
;
267 if (tests
[i
].promo_allowed
)
268 flags
|= DataReductionProxyParams::kPromoAllowed
;
269 TestDataReductionProxyParams
params(
271 HAS_EVERYTHING
& ~(tests
[i
].missing_definitions
));
272 EXPECT_EQ(tests
[i
].expected_result
, params
.init_result());
276 TEST_F(DataReductionProxyParamsTest
, IsDataReductionProxy
) {
278 net::HostPortPair host_port_pair
;
279 bool fallback_allowed
;
280 bool expected_result
;
281 net::HostPortPair expected_first
;
282 net::HostPortPair expected_second
;
284 { net::HostPortPair::FromURL(GURL(kDefaultOrigin
)),
287 net::HostPortPair::FromURL(GURL(kDefaultOrigin
)),
288 net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin
))
290 { net::HostPortPair::FromURL(GURL(kDefaultOrigin
)),
293 net::HostPortPair::FromURL(GURL(kDefaultOrigin
)),
294 net::HostPortPair::FromURL(GURL())
296 { net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin
)),
299 net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin
)),
300 net::HostPortPair::FromURL(GURL())
302 { net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin
)),
305 net::HostPortPair::FromURL(GURL()),
306 net::HostPortPair::FromURL(GURL())
308 { net::HostPortPair::FromURL(GURL(kDefaultAltOrigin
)),
311 net::HostPortPair::FromURL(GURL(kDefaultAltOrigin
)),
312 net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin
))
314 { net::HostPortPair::FromURL(GURL(kDefaultAltOrigin
)),
317 net::HostPortPair::FromURL(GURL(kDefaultAltOrigin
)),
318 net::HostPortPair::FromURL(GURL())
320 { net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin
)),
323 net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin
)),
324 net::HostPortPair::FromURL(GURL())
326 { net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin
)),
329 net::HostPortPair::FromURL(GURL()),
330 net::HostPortPair::FromURL(GURL())
332 { net::HostPortPair::FromURL(GURL(kDefaultSSLOrigin
)),
335 net::HostPortPair::FromURL(GURL(kDefaultSSLOrigin
)),
336 net::HostPortPair::FromURL(GURL())
339 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
340 int flags
= DataReductionProxyParams::kAllowed
|
341 DataReductionProxyParams::kAlternativeAllowed
;
342 if (tests
[i
].fallback_allowed
)
343 flags
|= DataReductionProxyParams::kFallbackAllowed
;
344 TestDataReductionProxyParams
params(flags
,
345 HAS_EVERYTHING
& ~HAS_DEV_ORIGIN
);
346 std::pair
<GURL
, GURL
> proxy_servers
;
347 EXPECT_EQ(tests
[i
].expected_result
,
348 params
.IsDataReductionProxy(
349 tests
[i
].host_port_pair
, &proxy_servers
));
350 EXPECT_TRUE(tests
[i
].expected_first
.Equals(
351 net::HostPortPair::FromURL(proxy_servers
.first
)));
352 EXPECT_TRUE(tests
[i
].expected_second
.Equals(
353 net::HostPortPair::FromURL(proxy_servers
.second
)));
357 } // namespace data_reduction_proxy