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_settings.h"
7 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h"
13 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
14 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
15 #include "net/http/http_auth.h"
16 #include "net/http/http_auth_cache.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 const char kProbeURLWithOKResponse
[] = "http://ok.org/";
24 const char kProbeURLWithBadResponse
[] = "http://bad.org/";
25 const char kProbeURLWithNoResponse
[] = "http://no.org/";
26 const char kWarmupURLWithNoContentResponse
[] = "http://warm.org/";
30 namespace data_reduction_proxy
{
32 class DataReductionProxySettingsTest
33 : public ConcreteDataReductionProxySettingsTest
<
34 DataReductionProxySettings
> {
37 TEST_F(DataReductionProxySettingsTest
, TestGetDataReductionProxyOrigin
) {
38 // SetUp() adds the origin to the command line, which should be returned here.
40 settings_
->params()->origin().spec();
41 EXPECT_EQ(GURL(expected_params_
->DefaultOrigin()), GURL(result
));
44 TEST_F(DataReductionProxySettingsTest
, TestGetDataReductionProxyDevOrigin
) {
45 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
46 switches::kDataReductionProxyDev
, expected_params_
->DefaultDevOrigin());
47 ResetSettings(true, true, false, true, false);
49 settings_
->params()->origin().spec();
50 EXPECT_EQ(GURL(expected_params_
->DefaultDevOrigin()), GURL(result
));
54 TEST_F(DataReductionProxySettingsTest
, TestGetDataReductionProxies
) {
55 DataReductionProxyParams::DataReductionProxyList proxies
=
56 expected_params_
->GetAllowedProxies();
58 unsigned int expected_proxy_size
= 2u;
59 EXPECT_EQ(expected_proxy_size
, proxies
.size());
61 net::HostPortPair expected_origin
=
62 net::HostPortPair::FromURL(GURL(expected_params_
->DefaultOrigin()));
63 net::HostPortPair expected_fallback_origin
=
64 net::HostPortPair::FromURL(
65 GURL(expected_params_
->DefaultFallbackOrigin()));
66 EXPECT_EQ(expected_origin
.host(), proxies
[0].host());
67 EXPECT_EQ(expected_origin
.port() ,proxies
[0].EffectiveIntPort());
68 EXPECT_EQ(expected_fallback_origin
.host(), proxies
[1].host());
69 EXPECT_EQ(expected_fallback_origin
.port(), proxies
[1].EffectiveIntPort());
72 TEST_F(DataReductionProxySettingsTest
, TestSetProxyConfigs
) {
73 TestDataReductionProxyParams
drp_params(
74 DataReductionProxyParams::kAllowed
|
75 DataReductionProxyParams::kFallbackAllowed
|
76 DataReductionProxyParams::kPromoAllowed
,
77 TestDataReductionProxyParams::HAS_EVERYTHING
&
78 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN
);
79 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
80 switches::kDataReductionProxyAlt
, drp_params
.DefaultAltOrigin());
81 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
82 switches::kDataReductionProxyAltFallback
,
83 drp_params
.DefaultAltFallbackOrigin());
84 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
85 switches::kDataReductionSSLProxy
, drp_params
.DefaultSSLOrigin());
86 ResetSettings(true, true, true, true, false);
87 TestDataReductionProxyConfig
* config
=
88 static_cast<TestDataReductionProxyConfig
*>(
89 settings_
->configurator());
91 settings_
->SetProxyConfigs(true, true, false, false);
92 EXPECT_TRUE(config
->enabled_
);
93 EXPECT_TRUE(net::HostPortPair::FromString(
94 expected_params_
->DefaultAltOrigin()).Equals(
95 net::HostPortPair::FromString(config
->origin_
)));
96 EXPECT_TRUE(net::HostPortPair::FromString(
97 expected_params_
->DefaultAltFallbackOrigin()).Equals(
98 net::HostPortPair::FromString(config
->fallback_origin_
)));
99 EXPECT_TRUE(net::HostPortPair::FromString(
100 expected_params_
->DefaultSSLOrigin()).Equals(
101 net::HostPortPair::FromString(config
->ssl_origin_
)));
103 settings_
->SetProxyConfigs(true, false, false, false);
104 EXPECT_TRUE(config
->enabled_
);
105 EXPECT_TRUE(net::HostPortPair::FromString(drp_params
.DefaultOrigin()).Equals(
106 net::HostPortPair::FromString(config
->origin_
)));
107 EXPECT_TRUE(net::HostPortPair::FromString(
108 drp_params
.DefaultFallbackOrigin()).Equals(
109 net::HostPortPair::FromString(config
->fallback_origin_
)));
110 EXPECT_EQ("", config
->ssl_origin_
);
112 settings_
->SetProxyConfigs(false, true, false, false);
113 EXPECT_FALSE(config
->enabled_
);
114 EXPECT_EQ("", config
->origin_
);
115 EXPECT_EQ("", config
->fallback_origin_
);
116 EXPECT_EQ("", config
->ssl_origin_
);
118 settings_
->SetProxyConfigs(false, false, false, false);
119 EXPECT_FALSE(config
->enabled_
);
120 EXPECT_EQ("", config
->origin_
);
121 EXPECT_EQ("", config
->fallback_origin_
);
122 EXPECT_EQ("", config
->ssl_origin_
);
125 TEST_F(DataReductionProxySettingsTest
, TestSetProxyConfigsHoldback
) {
126 ResetSettings(true, true, true, true, true);
127 TestDataReductionProxyConfig
* config
=
128 static_cast<TestDataReductionProxyConfig
*>(
129 settings_
->configurator());
132 settings_
->SetProxyConfigs(true, true, false, false);
133 EXPECT_FALSE(config
->enabled_
);
134 EXPECT_EQ("", config
->origin_
);
135 EXPECT_EQ("", config
->fallback_origin_
);
136 EXPECT_EQ("", config
->ssl_origin_
);
139 TEST_F(DataReductionProxySettingsTest
, TestIsProxyEnabledOrManaged
) {
140 settings_
->InitPrefMembers();
141 base::MessageLoopForUI loop
;
142 // The proxy is disabled initially.
143 settings_
->enabled_by_user_
= false;
144 settings_
->SetProxyConfigs(false, false, false, false);
146 EXPECT_FALSE(settings_
->IsDataReductionProxyEnabled());
147 EXPECT_FALSE(settings_
->IsDataReductionProxyManaged());
149 CheckOnPrefChange(true, true, false);
150 EXPECT_TRUE(settings_
->IsDataReductionProxyEnabled());
151 EXPECT_FALSE(settings_
->IsDataReductionProxyManaged());
153 CheckOnPrefChange(true, true, true);
154 EXPECT_TRUE(settings_
->IsDataReductionProxyEnabled());
155 EXPECT_TRUE(settings_
->IsDataReductionProxyManaged());
157 base::MessageLoop::current()->RunUntilIdle();
160 TEST_F(DataReductionProxySettingsTest
, TestResetDataReductionStatistics
) {
161 int64 original_content_length
;
162 int64 received_content_length
;
163 int64 last_update_time
;
164 settings_
->ResetDataReductionStatistics();
165 settings_
->GetContentLengths(kNumDaysInHistory
,
166 &original_content_length
,
167 &received_content_length
,
169 EXPECT_EQ(0L, original_content_length
);
170 EXPECT_EQ(0L, received_content_length
);
171 EXPECT_EQ(last_update_time_
.ToInternalValue(), last_update_time
);
174 TEST_F(DataReductionProxySettingsTest
, TestContentLengths
) {
175 int64 original_content_length
;
176 int64 received_content_length
;
177 int64 last_update_time
;
179 // Request |kNumDaysInHistory| days.
180 settings_
->GetContentLengths(kNumDaysInHistory
,
181 &original_content_length
,
182 &received_content_length
,
184 const unsigned int days
= kNumDaysInHistory
;
185 // Received content length history values are 0 to |kNumDaysInHistory - 1|.
186 int64 expected_total_received_content_length
= (days
- 1L) * days
/ 2;
187 // Original content length history values are 0 to
188 // |2 * (kNumDaysInHistory - 1)|.
189 long expected_total_original_content_length
= (days
- 1L) * days
;
190 EXPECT_EQ(expected_total_original_content_length
, original_content_length
);
191 EXPECT_EQ(expected_total_received_content_length
, received_content_length
);
192 EXPECT_EQ(last_update_time_
.ToInternalValue(), last_update_time
);
194 // Request |kNumDaysInHistory - 1| days.
195 settings_
->GetContentLengths(kNumDaysInHistory
- 1,
196 &original_content_length
,
197 &received_content_length
,
199 expected_total_received_content_length
-= (days
- 1);
200 expected_total_original_content_length
-= 2 * (days
- 1);
201 EXPECT_EQ(expected_total_original_content_length
, original_content_length
);
202 EXPECT_EQ(expected_total_received_content_length
, received_content_length
);
205 settings_
->GetContentLengths(0,
206 &original_content_length
,
207 &received_content_length
,
209 expected_total_received_content_length
= 0;
210 expected_total_original_content_length
= 0;
211 EXPECT_EQ(expected_total_original_content_length
, original_content_length
);
212 EXPECT_EQ(expected_total_received_content_length
, received_content_length
);
214 // Request 1 day. First day had 0 bytes so should be same as 0 days.
215 settings_
->GetContentLengths(1,
216 &original_content_length
,
217 &received_content_length
,
219 EXPECT_EQ(expected_total_original_content_length
, original_content_length
);
220 EXPECT_EQ(expected_total_received_content_length
, received_content_length
);
223 // TODO(marq): Add a test to verify that MaybeActivateDataReductionProxy
224 // is called when the pref in |settings_| is enabled.
225 TEST_F(DataReductionProxySettingsTest
, TestMaybeActivateDataReductionProxy
) {
226 // Initialize the pref member in |settings_| without the usual callback
227 // so it won't trigger MaybeActivateDataReductionProxy when the pref value
229 settings_
->spdy_proxy_auth_enabled_
.Init(
230 prefs::kDataReductionProxyEnabled
,
231 settings_
->GetOriginalProfilePrefs());
232 settings_
->data_reduction_proxy_alternative_enabled_
.Init(
233 prefs::kDataReductionProxyAltEnabled
,
234 settings_
->GetOriginalProfilePrefs());
236 // TODO(bengr): Test enabling/disabling while a probe is outstanding.
237 base::MessageLoopForUI loop
;
238 // The proxy is enabled and unrestructed initially.
239 // Request succeeded but with bad response, expect proxy to be restricted.
241 kProbeURLWithBadResponse
,
242 kWarmupURLWithNoContentResponse
,
248 // Request succeeded with valid response, expect proxy to be unrestricted.
250 kProbeURLWithOKResponse
,
251 kWarmupURLWithNoContentResponse
,
257 // Request failed, expect proxy to be enabled but restricted.
259 kProbeURLWithNoResponse
,
260 kWarmupURLWithNoContentResponse
,
266 // The proxy is disabled initially. Probes should not be emitted to change
269 kProbeURLWithOKResponse
,
270 kWarmupURLWithNoContentResponse
,
278 TEST_F(DataReductionProxySettingsTest
, TestOnIPAddressChanged
) {
279 base::MessageLoopForUI loop
;
280 // The proxy is enabled initially.
281 pref_service_
.SetBoolean(prefs::kDataReductionProxyEnabled
, true);
282 settings_
->spdy_proxy_auth_enabled_
.Init(
283 prefs::kDataReductionProxyEnabled
,
284 settings_
->GetOriginalProfilePrefs());
285 settings_
->data_reduction_proxy_alternative_enabled_
.Init(
286 prefs::kDataReductionProxyAltEnabled
,
287 settings_
->GetOriginalProfilePrefs());
288 settings_
->enabled_by_user_
= true;
289 settings_
->restricted_by_carrier_
= false;
290 settings_
->SetProxyConfigs(true, false, false, true);
291 // IP address change triggers a probe that succeeds. Proxy remains
293 CheckProbeOnIPChange(kProbeURLWithOKResponse
,
294 kWarmupURLWithNoContentResponse
,
299 // IP address change triggers a probe that fails. Proxy is restricted.
300 CheckProbeOnIPChange(kProbeURLWithBadResponse
,
301 kWarmupURLWithNoContentResponse
,
306 // IP address change triggers a probe that fails. Proxy remains restricted.
307 CheckProbeOnIPChange(kProbeURLWithBadResponse
,
308 kWarmupURLWithNoContentResponse
,
313 // IP address change triggers a probe that succeeds. Proxy is unrestricted.
314 CheckProbeOnIPChange(kProbeURLWithOKResponse
,
315 kWarmupURLWithNoContentResponse
,
320 // Simulate a VPN connection. The proxy should be disabled.
321 MockSettings
* settings
= static_cast<MockSettings
*>(settings_
.get());
322 settings
->network_interfaces_
.reset(new net::NetworkInterfaceList());
323 settings
->network_interfaces_
->push_back(
324 net::NetworkInterface("tun0", /* network interface name */
325 "tun0", /* network interface friendly name */
326 0, /* interface index */
327 net::NetworkChangeNotifier::CONNECTION_WIFI
,
328 net::IPAddressNumber(), /* IP address */
329 0 /* network prefix */
331 settings_
->OnIPAddressChanged();
332 base::MessageLoop::current()->RunUntilIdle();
333 CheckProxyConfigs(false, false, false);
335 // Check that the proxy is re-enabled if a non-VPN connection is later used.
336 settings
->network_interfaces_
.reset(new net::NetworkInterfaceList());
337 settings
->network_interfaces_
->push_back(
338 net::NetworkInterface("eth0", /* network interface name */
339 "eth0", /* network interface friendly name */
340 0, /* interface index */
341 net::NetworkChangeNotifier::CONNECTION_WIFI
,
342 net::IPAddressNumber(),
343 0 /* network prefix */
345 CheckProbeOnIPChange(kProbeURLWithOKResponse
,
346 kWarmupURLWithNoContentResponse
,
353 TEST_F(DataReductionProxySettingsTest
, TestOnProxyEnabledPrefChange
) {
354 settings_
->InitPrefMembers();
355 base::MessageLoopForUI loop
;
356 // The proxy is enabled initially.
357 settings_
->enabled_by_user_
= true;
358 settings_
->SetProxyConfigs(true, false, false, true);
359 // The pref is disabled, so correspondingly should be the proxy.
360 CheckOnPrefChange(false, false, false);
361 // The pref is enabled, so correspondingly should be the proxy.
362 CheckOnPrefChange(true, true, false);
365 TEST_F(DataReductionProxySettingsTest
, TestInitDataReductionProxyOn
) {
366 MockSettings
* settings
= static_cast<MockSettings
*>(settings_
.get());
367 EXPECT_CALL(*settings
, RecordStartupState(PROXY_ENABLED
));
369 pref_service_
.SetBoolean(prefs::kDataReductionProxyEnabled
, true);
370 CheckInitDataReductionProxy(true);
373 TEST_F(DataReductionProxySettingsTest
, TestInitDataReductionProxyOff
) {
374 // InitDataReductionProxySettings with the preference off will directly call
376 MockSettings
* settings
= static_cast<MockSettings
*>(settings_
.get());
377 EXPECT_CALL(*settings
, RecordStartupState(PROXY_DISABLED
));
379 pref_service_
.SetBoolean(prefs::kDataReductionProxyEnabled
, false);
380 CheckInitDataReductionProxy(false);
383 TEST_F(DataReductionProxySettingsTest
, TestEnableProxyFromCommandLine
) {
384 MockSettings
* settings
= static_cast<MockSettings
*>(settings_
.get());
385 EXPECT_CALL(*settings
, RecordStartupState(PROXY_ENABLED
));
387 CommandLine::ForCurrentProcess()->AppendSwitch(
388 switches::kEnableDataReductionProxy
);
389 CheckInitDataReductionProxy(true);
392 TEST_F(DataReductionProxySettingsTest
, TestGetDailyContentLengths
) {
393 DataReductionProxySettings::ContentLengthList result
=
394 settings_
->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength
);
396 ASSERT_FALSE(result
.empty());
397 ASSERT_EQ(kNumDaysInHistory
, result
.size());
399 for (size_t i
= 0; i
< kNumDaysInHistory
; ++i
) {
400 long expected_length
=
401 static_cast<long>((kNumDaysInHistory
- 1 - i
) * 2);
402 ASSERT_EQ(expected_length
, result
[i
]);
406 TEST_F(DataReductionProxySettingsTest
, CheckInitMetricsWhenNotAllowed
) {
407 // No call to |AddProxyToCommandLine()| was made, so the proxy feature
408 // should be unavailable.
409 base::MessageLoopForUI loop
;
410 // Clear the command line. Setting flags can force the proxy to be allowed.
411 CommandLine::ForCurrentProcess()->InitFromArgv(0, NULL
);
413 ResetSettings(false, false, false, false, false);
414 MockSettings
* settings
= static_cast<MockSettings
*>(settings_
.get());
415 EXPECT_FALSE(settings
->params()->allowed());
416 EXPECT_CALL(*settings
, RecordStartupState(PROXY_NOT_AVAILABLE
));
418 scoped_ptr
<DataReductionProxyConfigurator
> configurator(
419 new TestDataReductionProxyConfig());
420 settings_
->SetProxyConfigurator(configurator
.Pass());
421 scoped_refptr
<net::TestURLRequestContextGetter
> request_context
=
422 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
423 settings_
->InitDataReductionProxySettings(
426 request_context
.get());
427 settings_
->SetOnDataReductionEnabledCallback(
428 base::Bind(&DataReductionProxySettingsTestBase::
429 RegisterSyntheticFieldTrialCallback
,
430 base::Unretained(this)));
432 base::MessageLoop::current()->RunUntilIdle();
435 } // namespace data_reduction_proxy