9e77071709a43c8f135a36e33ec4bac241f4bf5c
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_settings_unittest.cc
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.
4
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
6
7 #include "base/command_line.h"
8 #include "base/md5.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"
19 #include "url/gurl.h"
20
21 namespace {
22
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/";
27
28 }  // namespace
29
30 namespace data_reduction_proxy {
31
32 class DataReductionProxySettingsTest
33     : public ConcreteDataReductionProxySettingsTest<
34           DataReductionProxySettings> {
35 };
36
37 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) {
38   // SetUp() adds the origin to the command line, which should be returned here.
39   std::string result =
40       settings_->params()->origin().spec();
41   EXPECT_EQ(GURL(expected_params_->DefaultOrigin()), GURL(result));
42 }
43
44 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyDevOrigin) {
45   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
46       switches::kDataReductionProxyDev, expected_params_->DefaultDevOrigin());
47   ResetSettings(true, true, false, true, false);
48   std::string result =
49       settings_->params()->origin().spec();
50   EXPECT_EQ(GURL(expected_params_->DefaultDevOrigin()), GURL(result));
51 }
52
53
54 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) {
55   DataReductionProxyParams::DataReductionProxyList proxies =
56       expected_params_->GetAllowedProxies();
57
58   unsigned int expected_proxy_size = 2u;
59   EXPECT_EQ(expected_proxy_size, proxies.size());
60
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());
70 }
71
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());
90
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_)));
102
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_);
111
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_);
117
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_);
123 }
124
125 TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigsHoldback) {
126   ResetSettings(true, true, true, true, true);
127   TestDataReductionProxyConfig* config =
128       static_cast<TestDataReductionProxyConfig*>(
129           settings_->configurator());
130
131    // Holdback.
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_);
137 }
138
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);
145
146   EXPECT_FALSE(settings_->IsDataReductionProxyEnabled());
147   EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
148
149   CheckOnPrefChange(true, true, false);
150   EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
151   EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
152
153   CheckOnPrefChange(true, true, true);
154   EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
155   EXPECT_TRUE(settings_->IsDataReductionProxyManaged());
156
157   base::MessageLoop::current()->RunUntilIdle();
158 }
159
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,
168                                &last_update_time);
169   EXPECT_EQ(0L, original_content_length);
170   EXPECT_EQ(0L, received_content_length);
171   EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
172 }
173
174 TEST_F(DataReductionProxySettingsTest, TestContentLengths) {
175   int64 original_content_length;
176   int64 received_content_length;
177   int64 last_update_time;
178
179   // Request |kNumDaysInHistory| days.
180   settings_->GetContentLengths(kNumDaysInHistory,
181                                &original_content_length,
182                                &received_content_length,
183                                &last_update_time);
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);
193
194   // Request |kNumDaysInHistory - 1| days.
195   settings_->GetContentLengths(kNumDaysInHistory - 1,
196                                &original_content_length,
197                                &received_content_length,
198                                &last_update_time);
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);
203
204   // Request 0 days.
205   settings_->GetContentLengths(0,
206                                &original_content_length,
207                                &received_content_length,
208                                &last_update_time);
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);
213
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,
218                                &last_update_time);
219   EXPECT_EQ(expected_total_original_content_length, original_content_length);
220   EXPECT_EQ(expected_total_received_content_length, received_content_length);
221 }
222
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
228   // is set.
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());
235
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.
240   CheckProbe(true,
241              kProbeURLWithBadResponse,
242              kWarmupURLWithNoContentResponse,
243              "Bad",
244              true,
245              true,
246              true,
247              false);
248   // Request succeeded with valid response, expect proxy to be unrestricted.
249   CheckProbe(true,
250              kProbeURLWithOKResponse,
251              kWarmupURLWithNoContentResponse,
252              "OK",
253              true,
254              true,
255              false,
256              false);
257   // Request failed, expect proxy to be enabled but restricted.
258   CheckProbe(true,
259              kProbeURLWithNoResponse,
260              kWarmupURLWithNoContentResponse,
261              "",
262              false,
263              true,
264              true,
265              false);
266   // The proxy is disabled initially. Probes should not be emitted to change
267   // state.
268   CheckProbe(false,
269              kProbeURLWithOKResponse,
270              kWarmupURLWithNoContentResponse,
271              "OK",
272              true,
273              false,
274              false,
275              false);
276 }
277
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
292   // unrestricted.
293   CheckProbeOnIPChange(kProbeURLWithOKResponse,
294                        kWarmupURLWithNoContentResponse,
295                        "OK",
296                        true,
297                        false,
298                        false);
299   // IP address change triggers a probe that fails. Proxy is restricted.
300   CheckProbeOnIPChange(kProbeURLWithBadResponse,
301                        kWarmupURLWithNoContentResponse,
302                        "Bad",
303                        true,
304                        true,
305                        false);
306   // IP address change triggers a probe that fails. Proxy remains restricted.
307   CheckProbeOnIPChange(kProbeURLWithBadResponse,
308                        kWarmupURLWithNoContentResponse,
309                        "Bad",
310                        true,
311                        true,
312                        false);
313   // IP address change triggers a probe that succeeds. Proxy is unrestricted.
314   CheckProbeOnIPChange(kProbeURLWithOKResponse,
315                        kWarmupURLWithNoContentResponse,
316                        "OK",
317                        true,
318                        false,
319                        false);
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 */
330                             ));
331   settings_->OnIPAddressChanged();
332   base::MessageLoop::current()->RunUntilIdle();
333   CheckProxyConfigs(false, false, false);
334
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 */
344                             ));
345   CheckProbeOnIPChange(kProbeURLWithOKResponse,
346                        kWarmupURLWithNoContentResponse,
347                        "OK",
348                        true,
349                        false,
350                        false);
351 }
352
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);
363 }
364
365 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) {
366   MockSettings* settings = static_cast<MockSettings*>(settings_.get());
367   EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED));
368
369   pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, true);
370   CheckInitDataReductionProxy(true);
371 }
372
373 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) {
374   // InitDataReductionProxySettings with the preference off will directly call
375   // LogProxyState.
376   MockSettings* settings = static_cast<MockSettings*>(settings_.get());
377   EXPECT_CALL(*settings, RecordStartupState(PROXY_DISABLED));
378
379   pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, false);
380   CheckInitDataReductionProxy(false);
381 }
382
383 TEST_F(DataReductionProxySettingsTest, TestEnableProxyFromCommandLine) {
384   MockSettings* settings = static_cast<MockSettings*>(settings_.get());
385   EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED));
386
387   CommandLine::ForCurrentProcess()->AppendSwitch(
388       switches::kEnableDataReductionProxy);
389   CheckInitDataReductionProxy(true);
390 }
391
392 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) {
393   DataReductionProxySettings::ContentLengthList result =
394       settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
395
396   ASSERT_FALSE(result.empty());
397   ASSERT_EQ(kNumDaysInHistory, result.size());
398
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]);
403   }
404 }
405
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);
412
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));
417
418   scoped_ptr<DataReductionProxyConfigurator> configurator(
419       new TestDataReductionProxyConfig());
420   settings_->SetProxyConfigurator(configurator.get());
421   scoped_refptr<net::TestURLRequestContextGetter> request_context =
422       new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
423   settings_->InitDataReductionProxySettings(
424       &pref_service_,
425       &pref_service_,
426       request_context.get());
427   settings_->SetOnDataReductionEnabledCallback(
428       base::Bind(&DataReductionProxySettingsTestBase::
429                  RegisterSyntheticFieldTrialCallback,
430                  base::Unretained(this)));
431
432   base::MessageLoop::current()->RunUntilIdle();
433 }
434
435 }  // namespace data_reduction_proxy