NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / net / spdyproxy / data_reduction_proxy_settings_unittest.cc
blob17841fb97b7ba58a3fcb2409dc803f443cfb4851
1 // Copyright 2013 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 "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h"
7 #include "base/command_line.h"
8 #include "base/md5.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/prefs/testing_pref_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
20 #include "chrome/browser/prefs/proxy_prefs.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/metrics/variations/variations_util.h"
23 #include "chrome/common/pref_names.h"
24 #include "components/variations/entropy_provider.h"
25 #include "net/base/auth.h"
26 #include "net/base/host_port_pair.h"
27 #include "net/http/http_auth.h"
28 #include "net/http/http_auth_cache.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_status_code.h"
31 #include "net/url_request/test_url_fetcher_factory.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/gurl.h"
36 using testing::_;
37 using testing::AnyNumber;
38 using testing::Return;
40 const char kDataReductionProxyOrigin[] = "https://foo.com:443/";
41 const char kDataReductionProxyDevHost[] = "http://foo-dev.com:80";
42 const char kDataReductionProxyFallback[] = "http://bar.com:80";
43 const char kDataReductionProxyAuth[] = "12345";
45 const char kProbeURLWithOKResponse[] = "http://ok.org/";
46 const char kProbeURLWithBadResponse[] = "http://bad.org/";
47 const char kProbeURLWithNoResponse[] = "http://no.org/";
49 // Transform "normal"-looking headers (\n-separated) to the appropriate
50 // input format for ParseRawHeaders (\0-separated).
51 void HeadersToRaw(std::string* headers) {
52 std::replace(headers->begin(), headers->end(), '\n', '\0');
53 if (!headers->empty())
54 *headers += '\0';
57 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
58 if (enabled) {
59 if (success)
60 return spdyproxy::SUCCEEDED_PROXY_ALREADY_ENABLED;
61 else
62 return spdyproxy::FAILED_PROXY_DISABLED;
63 } else {
64 if (success)
65 return spdyproxy::SUCCEEDED_PROXY_ENABLED;
66 else
67 return spdyproxy::FAILED_PROXY_ALREADY_DISABLED;
71 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
72 : testing::Test() {
75 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
77 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() {
78 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
79 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
80 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
81 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback);
82 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
83 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth);
86 // testing::Test implementation:
87 void DataReductionProxySettingsTestBase::SetUp() {
88 PrefRegistrySimple* registry = pref_service_.registry();
89 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
90 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
91 registry->RegisterInt64Pref(
92 prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
93 registry->RegisterDictionaryPref(prefs::kProxy);
94 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthEnabled, false);
95 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthWasEnabledBefore, false);
96 ResetSettings();
98 ListPrefUpdate original_update(&pref_service_,
99 prefs::kDailyHttpOriginalContentLength);
100 ListPrefUpdate received_update(&pref_service_,
101 prefs::kDailyHttpReceivedContentLength);
102 for (int64 i = 0; i < spdyproxy::kNumDaysInHistory; i++) {
103 original_update->Insert(0,
104 new base::StringValue(base::Int64ToString(2 * i)));
105 received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));
107 last_update_time_ = base::Time::Now().LocalMidnight();
108 pref_service_.SetInt64(
109 prefs::kDailyHttpContentLengthLastUpdateDate,
110 last_update_time_.ToInternalValue());
113 template <class C>
114 void DataReductionProxySettingsTestBase::ResetSettings() {
115 MockDataReductionProxySettings<C>* settings =
116 new MockDataReductionProxySettings<C>;
117 EXPECT_CALL(*settings, GetOriginalProfilePrefs())
118 .Times(AnyNumber())
119 .WillRepeatedly(Return(&pref_service_));
120 EXPECT_CALL(*settings, GetLocalStatePrefs())
121 .Times(AnyNumber())
122 .WillRepeatedly(Return(&pref_service_));
123 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
124 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0);
125 settings_.reset(settings);
128 // Explicitly generate required instantiations.
129 template void
130 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>();
131 template void
132 DataReductionProxySettingsTestBase::ResetSettings<
133 DataReductionProxySettingsAndroid>();
135 template <class C>
136 void DataReductionProxySettingsTestBase::SetProbeResult(
137 const std::string& test_url,
138 const std::string& response,
139 ProbeURLFetchResult result,
140 bool success,
141 int expected_calls) {
142 MockDataReductionProxySettings<C>* settings =
143 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
144 if (0 == expected_calls) {
145 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
146 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0);
147 } else {
148 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
149 EXPECT_CALL(*settings, GetURLFetcher())
150 .Times(expected_calls)
151 .WillRepeatedly(Return(new net::FakeURLFetcher(
152 GURL(test_url),
153 settings,
154 response,
155 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
156 success ? net::URLRequestStatus::SUCCESS :
157 net::URLRequestStatus::FAILED)));
161 // Explicitly generate required instantiations.
162 template void
163 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
164 const std::string& test_url,
165 const std::string& response,
166 ProbeURLFetchResult result,
167 bool success,
168 int expected_calls);
169 template void
170 DataReductionProxySettingsTestBase::SetProbeResult<
171 DataReductionProxySettingsAndroid>(const std::string& test_url,
172 const std::string& response,
173 ProbeURLFetchResult result,
174 bool success,
175 int expected_calls);
177 void DataReductionProxySettingsTestBase::CheckProxyPref(
178 const std::string& expected_servers,
179 const std::string& expected_mode) {
180 const base::DictionaryValue* dict =
181 pref_service_.GetDictionary(prefs::kProxy);
182 std::string mode;
183 std::string server;
184 dict->GetString("mode", &mode);
185 ASSERT_EQ(expected_mode, mode);
186 dict->GetString("server", &server);
187 ASSERT_EQ(expected_servers, server);
190 void DataReductionProxySettingsTestBase::CheckProxyConfigs(
191 bool expected_enabled, bool expected_restricted) {
192 if (expected_enabled) {
193 std::string main_proxy = kDataReductionProxyOrigin;
194 std::string fallback_proxy = kDataReductionProxyFallback;
195 std::string servers = expected_restricted ?
196 "http=" + fallback_proxy + ",direct://;" :
197 "http=" + main_proxy + "," + fallback_proxy + ",direct://;";
198 CheckProxyPref(servers,
199 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
200 } else {
201 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
205 void DataReductionProxySettingsTestBase::CheckProbe(
206 bool initially_enabled,
207 const std::string& probe_url,
208 const std::string& response,
209 bool request_succeeded,
210 bool expected_enabled,
211 bool expected_restricted) {
212 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
213 if (initially_enabled)
214 settings_->enabled_by_user_ = true;
215 settings_->restricted_by_carrier_ = false;
216 SetProbeResult(probe_url,
217 response,
218 FetchResult(initially_enabled,
219 request_succeeded && (response == "OK")),
220 request_succeeded,
221 initially_enabled ? 1 : 0);
222 settings_->MaybeActivateDataReductionProxy(false);
223 base::MessageLoop::current()->RunUntilIdle();
224 CheckProxyConfigs(expected_enabled, expected_restricted);
227 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange(
228 const std::string& probe_url,
229 const std::string& response,
230 bool request_succeeded,
231 bool expected_restricted) {
232 SetProbeResult(probe_url,
233 response,
234 FetchResult(!settings_->restricted_by_carrier_,
235 request_succeeded && (response == "OK")),
236 request_succeeded,
238 settings_->OnIPAddressChanged();
239 base::MessageLoop::current()->RunUntilIdle();
240 CheckProxyConfigs(true, expected_restricted);
243 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
244 bool enabled,
245 bool expected_enabled) {
246 // Always have a sucessful probe for pref change tests.
247 SetProbeResult(kProbeURLWithOKResponse,
248 "OK",
249 FetchResult(enabled, true),
250 true,
251 expected_enabled ? 1 : 0);
252 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled);
253 base::MessageLoop::current()->RunUntilIdle();
254 // Never expect the proxy to be restricted for pref change tests.
255 CheckProxyConfigs(expected_enabled, false);
258 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
259 bool enabled_at_startup) {
260 AddProxyToCommandLine();
261 base::MessageLoopForUI loop;
262 SetProbeResult(kProbeURLWithOKResponse,
263 "OK",
264 FetchResult(enabled_at_startup, true),
265 true,
266 enabled_at_startup ? 1 : 0);
267 settings_->InitDataReductionProxySettings();
268 base::MessageLoop::current()->RunUntilIdle();
269 if (enabled_at_startup) {
270 CheckProxyConfigs(enabled_at_startup, false);
271 } else {
272 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
277 class DataReductionProxySettingsTest
278 : public ConcreteDataReductionProxySettingsTest<
279 DataReductionProxySettings> {
283 TEST_F(DataReductionProxySettingsTest, TestAuthenticationInit) {
284 AddProxyToCommandLine();
285 net::HttpAuthCache cache;
286 DataReductionProxySettings::InitDataReductionAuthentication(&cache);
287 DataReductionProxySettings::DataReductionProxyList proxies =
288 DataReductionProxySettings::GetDataReductionProxies();
289 for (DataReductionProxySettings::DataReductionProxyList::iterator it =
290 proxies.begin(); it != proxies.end(); ++it) {
291 net::HttpAuthCache::Entry* entry = cache.LookupByPath(*it,
292 std::string("/"));
293 EXPECT_TRUE(entry != NULL);
294 EXPECT_EQ(net::HttpAuth::AUTH_SCHEME_SPDYPROXY, entry->scheme());
295 EXPECT_EQ("SpdyProxy", entry->auth_challenge().substr(0,9));
297 GURL bad_server = GURL("https://bad.proxy.com/");
298 net::HttpAuthCache::Entry* entry =
299 cache.LookupByPath(bad_server, std::string());
300 EXPECT_TRUE(entry == NULL);
303 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) {
304 AddProxyToCommandLine();
305 // SetUp() adds the origin to the command line, which should be returned here.
306 std::string result =
307 DataReductionProxySettings::GetDataReductionProxyOrigin();
308 EXPECT_EQ(kDataReductionProxyOrigin, result);
311 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyDevOrigin) {
312 AddProxyToCommandLine();
313 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
314 switches::kSpdyProxyDevAuthOrigin, kDataReductionProxyDevHost);
315 std::string result =
316 DataReductionProxySettings::GetDataReductionProxyOrigin();
317 EXPECT_EQ(kDataReductionProxyDevHost, result);
320 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) {
321 DataReductionProxySettings::DataReductionProxyList proxies =
322 DataReductionProxySettings::GetDataReductionProxies();
324 unsigned int expected_proxy_size = 0u;
325 #if defined(SPDY_PROXY_AUTH_ORIGIN)
326 ++expected_proxy_size;
327 #endif
328 #if defined(DATA_REDUCTION_FALLBACK_HOST)
329 ++expected_proxy_size;
330 #endif
332 EXPECT_EQ(expected_proxy_size, proxies.size());
334 // Adding just the fallback on the command line shouldn't add a proxy unless
335 // there was already one compiled in.
336 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
337 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback);
338 proxies = DataReductionProxySettings::GetDataReductionProxies();
340 // So: if there weren't any proxies before, there still won't be.
341 // If there were one or two, there will be two now.
342 expected_proxy_size = expected_proxy_size == 0u ? 0u : 2u;
344 EXPECT_EQ(expected_proxy_size, proxies.size());
346 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
347 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
348 proxies = DataReductionProxySettings::GetDataReductionProxies();
349 EXPECT_EQ(2u, proxies.size());
351 // Command line proxies have precedence, so even if there were other values
352 // compiled in, these should be the ones in the list.
353 EXPECT_EQ("foo.com", proxies[0].host());
354 EXPECT_EQ(443 ,proxies[0].EffectiveIntPort());
355 EXPECT_EQ("bar.com", proxies[1].host());
356 EXPECT_EQ(80, proxies[1].EffectiveIntPort());
359 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) {
360 AddProxyToCommandLine();
361 std::string salt = "8675309"; // Jenny's number to test the hash generator.
362 std::string salted_key = salt + kDataReductionProxyAuth + salt;
363 base::string16 expected_hash = base::UTF8ToUTF16(base::MD5String(salted_key));
364 EXPECT_EQ(expected_hash,
365 DataReductionProxySettings::AuthHashForSalt(8675309));
368 // Test that the auth key set by preprocessor directive is not used
369 // when an origin is set via a switch. This test only does anything useful in
370 // Chrome builds.
371 TEST_F(DataReductionProxySettingsTest,
372 TestAuthHashGenerationWithOriginSetViaSwitch) {
373 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
374 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
375 EXPECT_EQ(base::string16(),
376 DataReductionProxySettings::AuthHashForSalt(8675309));
380 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) {
381 settings_->InitPrefMembers();
382 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled());
383 EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
385 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
386 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
387 EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
389 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled,
390 base::Value::CreateBooleanValue(true));
391 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
392 EXPECT_TRUE(settings_->IsDataReductionProxyManaged());
396 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) {
397 AddProxyToCommandLine();
398 typedef struct {
399 std::string host;
400 std::string realm;
401 bool expected_to_succeed;
402 } challenge_test;
404 challenge_test tests[] = {
405 {"foo.com:443", "", false}, // 0. No realm.
406 {"foo.com:443", "xxx", false}, // 1. Wrong realm.
407 {"foo.com:443", "spdyproxy", false}, // 2. Case matters.
408 {"foo.com:443", "SpdyProxy", true}, // 3. OK.
409 {"foo.com:443", "SpdyProxy1234567", true}, // 4. OK
410 {"bar.com:80", "SpdyProxy1234567", true}, // 5. OK.
411 {"foo.com:443", "SpdyProxyxxx", true}, // 6. OK
412 {"", "SpdyProxy1234567", false}, // 7. No challenger.
413 {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host.
414 {"foo.com", "SpdyProxy1234567", false}, // 9. No port.
415 {"foo.com:80", "SpdyProxy1234567", false}, // 10.Wrong port.
416 {"bar.com:81", "SpdyProxy1234567", false}, // 11.Wrong port.
419 for (int i = 0; i <= 11; ++i) {
420 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
421 auth_info->challenger = net::HostPortPair::FromString(tests[i].host);
422 auth_info->realm = tests[i].realm;
423 EXPECT_EQ(tests[i].expected_to_succeed,
424 settings_->IsAcceptableAuthChallenge(auth_info.get()));
428 TEST_F(DataReductionProxySettingsTest, TestChallengeTokens) {
429 AddProxyToCommandLine();
430 typedef struct {
431 std::string realm;
432 bool expected_empty_token;
433 } token_test;
435 token_test tests[] = {
436 {"", true}, // 0. No realm.
437 {"xxx", true}, // 1. realm too short.
438 {"spdyproxy", true}, // 2. no salt.
439 {"SpdyProxyxxx", true}, // 3. Salt not an int.
440 {"SpdyProxy1234567", false}, // 4. OK
443 for (int i = 0; i <= 4; ++i) {
444 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
445 auth_info->challenger =
446 net::HostPortPair::FromString(kDataReductionProxyOrigin);
447 auth_info->realm = tests[i].realm;
448 base::string16 token = settings_->GetTokenForAuthChallenge(auth_info.get());
449 EXPECT_EQ(tests[i].expected_empty_token, token.empty());
453 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) {
454 int64 original_content_length;
455 int64 received_content_length;
456 int64 last_update_time;
457 settings_->ResetDataReductionStatistics();
458 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory,
459 &original_content_length,
460 &received_content_length,
461 &last_update_time);
462 EXPECT_EQ(0L, original_content_length);
463 EXPECT_EQ(0L, received_content_length);
464 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
467 TEST_F(DataReductionProxySettingsTest, TestContentLengths) {
468 int64 original_content_length;
469 int64 received_content_length;
470 int64 last_update_time;
472 // Request |kNumDaysInHistory| days.
473 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory,
474 &original_content_length,
475 &received_content_length,
476 &last_update_time);
477 const unsigned int days = spdyproxy::kNumDaysInHistory;
478 // Received content length history values are 0 to |kNumDaysInHistory - 1|.
479 int64 expected_total_received_content_length = (days - 1L) * days / 2;
480 // Original content length history values are 0 to
481 // |2 * (kNumDaysInHistory - 1)|.
482 long expected_total_original_content_length = (days - 1L) * days;
483 EXPECT_EQ(expected_total_original_content_length, original_content_length);
484 EXPECT_EQ(expected_total_received_content_length, received_content_length);
485 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
487 // Request |kNumDaysInHistory - 1| days.
488 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory - 1,
489 &original_content_length,
490 &received_content_length,
491 &last_update_time);
492 expected_total_received_content_length -= (days - 1);
493 expected_total_original_content_length -= 2 * (days - 1);
494 EXPECT_EQ(expected_total_original_content_length, original_content_length);
495 EXPECT_EQ(expected_total_received_content_length, received_content_length);
497 // Request 0 days.
498 settings_->GetContentLengths(0,
499 &original_content_length,
500 &received_content_length,
501 &last_update_time);
502 expected_total_received_content_length = 0;
503 expected_total_original_content_length = 0;
504 EXPECT_EQ(expected_total_original_content_length, original_content_length);
505 EXPECT_EQ(expected_total_received_content_length, received_content_length);
507 // Request 1 day. First day had 0 bytes so should be same as 0 days.
508 settings_->GetContentLengths(1,
509 &original_content_length,
510 &received_content_length,
511 &last_update_time);
512 EXPECT_EQ(expected_total_original_content_length, original_content_length);
513 EXPECT_EQ(expected_total_received_content_length, received_content_length);
516 // TODO(marq): Add a test to verify that MaybeActivateDataReductionProxy
517 // is called when the pref in |settings_| is enabled.
518 TEST_F(DataReductionProxySettingsTest, TestMaybeActivateDataReductionProxy) {
519 AddProxyToCommandLine();
521 // Initialize the pref member in |settings_| without the usual callback
522 // so it won't trigger MaybeActivateDataReductionProxy when the pref value
523 // is set.
524 settings_->spdy_proxy_auth_enabled_.Init(
525 prefs::kSpdyProxyAuthEnabled,
526 settings_->GetOriginalProfilePrefs());
528 // TODO(bengr): Test enabling/disabling while a probe is outstanding.
529 base::MessageLoopForUI loop;
530 // The proxy is enabled and unrestructed initially.
531 // Request succeeded but with bad response, expect proxy to be restricted.
532 CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, true, true);
533 // Request succeeded with valid response, expect proxy to be unrestricted.
534 CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true, false);
535 // Request failed, expect proxy to be enabled but restricted.
536 CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true);
537 // The proxy is disabled initially. Probes should not be emitted to change
538 // state.
539 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false);
542 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) {
543 AddProxyToCommandLine();
544 base::MessageLoopForUI loop;
545 // The proxy is enabled initially.
546 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
547 settings_->spdy_proxy_auth_enabled_.Init(
548 prefs::kSpdyProxyAuthEnabled,
549 settings_->GetOriginalProfilePrefs());
550 settings_->enabled_by_user_ = true;
551 settings_->restricted_by_carrier_ = false;
552 settings_->SetProxyConfigs(true, false, true);
553 // IP address change triggers a probe that succeeds. Proxy remains
554 // unrestricted.
555 CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false);
556 // IP address change triggers a probe that fails. Proxy is restricted.
557 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
558 // IP address change triggers a probe that fails. Proxy remains restricted.
559 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
560 // IP address change triggers a probe that succeed. Proxy is unrestricted.
561 CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false);
564 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) {
565 AddProxyToCommandLine();
566 settings_->InitPrefMembers();
567 base::MessageLoopForUI loop;
568 // The proxy is enabled initially.
569 settings_->enabled_by_user_ = true;
570 settings_->SetProxyConfigs(true, false, true);
571 // The pref is disabled, so correspondingly should be the proxy.
572 CheckOnPrefChange(false, false);
573 // The pref is enabled, so correspondingly should be the proxy.
574 CheckOnPrefChange(true, true);
577 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) {
578 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
579 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_ENABLED));
581 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
582 CheckInitDataReductionProxy(true);
585 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) {
586 // InitDataReductionProxySettings with the preference off will directly call
587 // LogProxyState.
588 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
589 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_DISABLED));
591 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, false);
592 CheckInitDataReductionProxy(false);
595 TEST_F(DataReductionProxySettingsTest, TestSetProxyFromCommandLine) {
596 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
597 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_ENABLED));
599 CommandLine::ForCurrentProcess()->AppendSwitch(
600 switches::kEnableSpdyProxyAuth);
601 CheckInitDataReductionProxy(true);
604 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) {
605 DataReductionProxySettings::ContentLengthList result =
606 settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
608 ASSERT_FALSE(result.empty());
609 ASSERT_EQ(spdyproxy::kNumDaysInHistory, result.size());
611 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
612 long expected_length =
613 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2);
614 ASSERT_EQ(expected_length, result[i]);
618 TEST_F(DataReductionProxySettingsTest, TestBypassList) {
619 settings_->AddHostPatternToBypass("http://www.google.com");
620 settings_->AddHostPatternToBypass("fefe:13::abc/33");
621 settings_->AddURLPatternToBypass("foo.org/images/*");
622 settings_->AddURLPatternToBypass("http://foo.com/*");
623 settings_->AddURLPatternToBypass("http://baz.com:22/bar/*");
624 settings_->AddURLPatternToBypass("http://*bat.com/bar/*");
626 std::string expected[] = {
627 "http://www.google.com",
628 "fefe:13::abc/33",
629 "foo.org",
630 "http://foo.com",
631 "http://baz.com:22",
632 "http://*bat.com"
635 ASSERT_EQ(settings_->bypass_rules_.size(), 6u);
636 int i = 0;
637 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin();
638 it != settings_->bypass_rules_.end(); ++it) {
639 EXPECT_EQ(expected[i++], *it);
643 TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) {
644 // No call to |AddProxyToCommandLine()| was made, so the proxy feature
645 // should be unavailable.
646 EXPECT_FALSE(DataReductionProxySettings::IsDataReductionProxyAllowed());
647 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
648 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_NOT_AVAILABLE));
649 settings_->InitDataReductionProxySettings();