| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/md5.h" | 8 #include "base/md5.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.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" | 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" | 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
" | 14 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
| 15 #include "net/http/http_auth.h" | 15 #include "net/http/http_auth.h" |
| 16 #include "net/http/http_auth_cache.h" | 16 #include "net/http/http_auth_cache.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kDataReductionProxy[] = "https://foo.com:443/"; | |
| 24 const char kDataReductionProxyDev[] = "http://foo-dev.com:80"; | |
| 25 const char kDataReductionProxyFallback[] = "http://bar.com:80"; | |
| 26 const char kDataReductionProxyKey[] = "12345"; | |
| 27 const char kDataReductionProxyAlt[] = "https://alt.com:443/"; | |
| 28 const char kDataReductionProxyAltFallback[] = "http://alt2.com:80"; | |
| 29 const char kDataReductionProxySSL[] = "http://ssl.com:80"; | |
| 30 | |
| 31 const char kProbeURLWithOKResponse[] = "http://ok.org/"; | 23 const char kProbeURLWithOKResponse[] = "http://ok.org/"; |
| 32 const char kProbeURLWithBadResponse[] = "http://bad.org/"; | 24 const char kProbeURLWithBadResponse[] = "http://bad.org/"; |
| 33 const char kProbeURLWithNoResponse[] = "http://no.org/"; | 25 const char kProbeURLWithNoResponse[] = "http://no.org/"; |
| 34 const char kWarmupURLWithNoContentResponse[] = "http://warm.org/"; | 26 const char kWarmupURLWithNoContentResponse[] = "http://warm.org/"; |
| 35 | 27 |
| 36 } // namespace | 28 } // namespace |
| 37 | 29 |
| 38 namespace data_reduction_proxy { | 30 namespace data_reduction_proxy { |
| 39 | 31 |
| 40 class DataReductionProxySettingsTest | 32 class DataReductionProxySettingsTest |
| 41 : public ConcreteDataReductionProxySettingsTest< | 33 : public ConcreteDataReductionProxySettingsTest< |
| 42 DataReductionProxySettings> { | 34 DataReductionProxySettings> { |
| 43 }; | 35 }; |
| 44 | 36 |
| 45 | |
| 46 TEST_F(DataReductionProxySettingsTest, TestAuthenticationInit) { | |
| 47 net::HttpAuthCache cache; | |
| 48 DataReductionProxyParams drp_params( | |
| 49 DataReductionProxyParams::kAllowed | | |
| 50 DataReductionProxyParams::kFallbackAllowed | | |
| 51 DataReductionProxyParams::kPromoAllowed); | |
| 52 drp_params.set_key(kDataReductionProxyKey); | |
| 53 DataReductionProxySettings::InitDataReductionAuthentication( | |
| 54 &cache, &drp_params); | |
| 55 DataReductionProxyParams::DataReductionProxyList proxies = | |
| 56 drp_params.GetAllowedProxies(); | |
| 57 for (DataReductionProxyParams::DataReductionProxyList::iterator it = | |
| 58 proxies.begin(); it != proxies.end(); ++it) { | |
| 59 net::HttpAuthCache::Entry* entry = cache.LookupByPath(*it, | |
| 60 std::string("/")); | |
| 61 EXPECT_TRUE(entry != NULL); | |
| 62 EXPECT_EQ(net::HttpAuth::AUTH_SCHEME_SPDYPROXY, entry->scheme()); | |
| 63 EXPECT_EQ("SpdyProxy", entry->auth_challenge().substr(0,9)); | |
| 64 } | |
| 65 GURL bad_server = GURL("https://bad.proxy.com/"); | |
| 66 net::HttpAuthCache::Entry* entry = | |
| 67 cache.LookupByPath(bad_server, std::string()); | |
| 68 EXPECT_TRUE(entry == NULL); | |
| 69 } | |
| 70 | |
| 71 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { | 37 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { |
| 72 // SetUp() adds the origin to the command line, which should be returned here. | 38 // SetUp() adds the origin to the command line, which should be returned here. |
| 73 std::string result = | 39 std::string result = |
| 74 settings_->params()->origin().spec(); | 40 settings_->params()->origin().spec(); |
| 75 EXPECT_EQ(GURL(kDataReductionProxy), GURL(result)); | 41 EXPECT_EQ(GURL(expected_params_->DefaultOrigin()), GURL(result)); |
| 76 } | 42 } |
| 77 | 43 |
| 78 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyDevOrigin) { | 44 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyDevOrigin) { |
| 79 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 45 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 80 switches::kDataReductionProxyDev, kDataReductionProxyDev); | 46 switches::kDataReductionProxyDev, expected_params_->DefaultDevOrigin()); |
| 81 ResetSettings(true, true, false, true); | 47 ResetSettings(true, true, false, true); |
| 82 std::string result = | 48 std::string result = |
| 83 settings_->params()->origin().spec(); | 49 settings_->params()->origin().spec(); |
| 84 EXPECT_EQ(GURL(kDataReductionProxyDev), GURL(result)); | 50 EXPECT_EQ(GURL(expected_params_->DefaultDevOrigin()), GURL(result)); |
| 85 } | 51 } |
| 86 | 52 |
| 87 | 53 |
| 88 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { | 54 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { |
| 89 DataReductionProxyParams drp_params( | |
| 90 DataReductionProxyParams::kAllowed | | |
| 91 DataReductionProxyParams::kFallbackAllowed | | |
| 92 DataReductionProxyParams::kPromoAllowed); | |
| 93 DataReductionProxyParams::DataReductionProxyList proxies = | 55 DataReductionProxyParams::DataReductionProxyList proxies = |
| 94 drp_params.GetAllowedProxies(); | 56 expected_params_->GetAllowedProxies(); |
| 95 | 57 |
| 96 unsigned int expected_proxy_size = 2u; | 58 unsigned int expected_proxy_size = 2u; |
| 97 EXPECT_EQ(expected_proxy_size, proxies.size()); | 59 EXPECT_EQ(expected_proxy_size, proxies.size()); |
| 98 | 60 |
| 99 // Command line proxies have precedence, so even if there were other values | 61 net::HostPortPair expected_origin = |
| 100 // compiled in, these should be the ones in the list. | 62 net::HostPortPair::FromURL(GURL(expected_params_->DefaultOrigin())); |
| 101 EXPECT_EQ("foo.com", proxies[0].host()); | 63 net::HostPortPair expected_fallback_origin = |
| 102 EXPECT_EQ(443 ,proxies[0].EffectiveIntPort()); | 64 net::HostPortPair::FromURL( |
| 103 EXPECT_EQ("bar.com", proxies[1].host()); | 65 GURL(expected_params_->DefaultFallbackOrigin())); |
| 104 EXPECT_EQ(80, proxies[1].EffectiveIntPort()); | 66 EXPECT_EQ(expected_origin.host(), proxies[0].host()); |
| 105 } | 67 EXPECT_EQ(expected_origin.port() ,proxies[0].EffectiveIntPort()); |
| 106 | 68 EXPECT_EQ(expected_fallback_origin.host(), proxies[1].host()); |
| 107 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) { | 69 EXPECT_EQ(expected_fallback_origin.port(), proxies[1].EffectiveIntPort()); |
| 108 std::string salt = "8675309"; // Jenny's number to test the hash generator. | |
| 109 std::string salted_key = salt + kDataReductionProxyKey + salt; | |
| 110 base::string16 expected_hash = base::UTF8ToUTF16(base::MD5String(salted_key)); | |
| 111 EXPECT_EQ(expected_hash, | |
| 112 DataReductionProxySettings::AuthHashForSalt( | |
| 113 8675309, kDataReductionProxyKey)); | |
| 114 } | 70 } |
| 115 | 71 |
| 116 TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigs) { | 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); |
| 117 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 79 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 118 switches::kDataReductionProxyAlt, kDataReductionProxyAlt); | 80 switches::kDataReductionProxyAlt, drp_params.DefaultAltOrigin()); |
| 119 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 81 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 120 switches::kDataReductionProxyAltFallback, kDataReductionProxyAltFallback); | 82 switches::kDataReductionProxyAltFallback, |
| 83 drp_params.DefaultAltFallbackOrigin()); |
| 121 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 84 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 122 switches::kDataReductionSSLProxy, kDataReductionProxySSL); | 85 switches::kDataReductionSSLProxy, drp_params.DefaultSSLOrigin()); |
| 123 ResetSettings(true, true, true, true); | 86 ResetSettings(true, true, true, true); |
| 124 TestDataReductionProxyConfig* config = | 87 TestDataReductionProxyConfig* config = |
| 125 static_cast<TestDataReductionProxyConfig*>( | 88 static_cast<TestDataReductionProxyConfig*>( |
| 126 settings_->configurator()); | 89 settings_->configurator()); |
| 127 | 90 |
| 128 settings_->SetProxyConfigs(true, true, false, false); | 91 settings_->SetProxyConfigs(true, true, false, false); |
| 129 EXPECT_TRUE(config->enabled_); | 92 EXPECT_TRUE(config->enabled_); |
| 130 EXPECT_TRUE(net::HostPortPair::FromString(kDataReductionProxyAlt).Equals( | 93 EXPECT_TRUE(net::HostPortPair::FromString( |
| 131 net::HostPortPair::FromString(config->origin_))); | 94 expected_params_->DefaultAltOrigin()).Equals( |
| 132 EXPECT_TRUE( | 95 net::HostPortPair::FromString(config->origin_))); |
| 133 net::HostPortPair::FromString(kDataReductionProxyAltFallback).Equals( | 96 EXPECT_TRUE(net::HostPortPair::FromString( |
| 97 expected_params_->DefaultAltFallbackOrigin()).Equals( |
| 134 net::HostPortPair::FromString(config->fallback_origin_))); | 98 net::HostPortPair::FromString(config->fallback_origin_))); |
| 135 EXPECT_TRUE(net::HostPortPair::FromString(kDataReductionProxySSL).Equals( | 99 EXPECT_TRUE(net::HostPortPair::FromString( |
| 136 net::HostPortPair::FromString(config->ssl_origin_))); | 100 expected_params_->DefaultSSLOrigin()).Equals( |
| 101 net::HostPortPair::FromString(config->ssl_origin_))); |
| 137 | 102 |
| 138 settings_->SetProxyConfigs(true, false, false, false); | 103 settings_->SetProxyConfigs(true, false, false, false); |
| 139 EXPECT_TRUE(config->enabled_); | 104 EXPECT_TRUE(config->enabled_); |
| 140 EXPECT_TRUE(net::HostPortPair::FromString(kDataReductionProxy).Equals( | 105 EXPECT_TRUE(net::HostPortPair::FromString(drp_params.DefaultOrigin()).Equals( |
| 141 net::HostPortPair::FromString(config->origin_))); | 106 net::HostPortPair::FromString(config->origin_))); |
| 142 EXPECT_TRUE(net::HostPortPair::FromString(kDataReductionProxyFallback).Equals( | 107 EXPECT_TRUE(net::HostPortPair::FromString( |
| 143 net::HostPortPair::FromString(config->fallback_origin_))); | 108 drp_params.DefaultFallbackOrigin()).Equals( |
| 109 net::HostPortPair::FromString(config->fallback_origin_))); |
| 144 EXPECT_EQ("", config->ssl_origin_); | 110 EXPECT_EQ("", config->ssl_origin_); |
| 145 | 111 |
| 146 settings_->SetProxyConfigs(false, true, false, false); | 112 settings_->SetProxyConfigs(false, true, false, false); |
| 147 EXPECT_FALSE(config->enabled_); | 113 EXPECT_FALSE(config->enabled_); |
| 148 EXPECT_EQ("", config->origin_); | 114 EXPECT_EQ("", config->origin_); |
| 149 EXPECT_EQ("", config->fallback_origin_); | 115 EXPECT_EQ("", config->fallback_origin_); |
| 150 EXPECT_EQ("", config->ssl_origin_); | 116 EXPECT_EQ("", config->ssl_origin_); |
| 151 | 117 |
| 152 settings_->SetProxyConfigs(false, false, false, false); | 118 settings_->SetProxyConfigs(false, false, false, false); |
| 153 EXPECT_FALSE(config->enabled_); | 119 EXPECT_FALSE(config->enabled_); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 170 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 136 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 171 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 137 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
| 172 | 138 |
| 173 CheckOnPrefChange(true, true, true); | 139 CheckOnPrefChange(true, true, true); |
| 174 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 140 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 175 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); | 141 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); |
| 176 | 142 |
| 177 base::MessageLoop::current()->RunUntilIdle(); | 143 base::MessageLoop::current()->RunUntilIdle(); |
| 178 } | 144 } |
| 179 | 145 |
| 180 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) { | |
| 181 typedef struct { | |
| 182 std::string host; | |
| 183 std::string realm; | |
| 184 bool expected_to_succeed; | |
| 185 } challenge_test; | |
| 186 | |
| 187 challenge_test tests[] = { | |
| 188 {"foo.com:443", "", false}, // 0. No realm. | |
| 189 {"foo.com:443", "xxx", false}, // 1. Wrong realm. | |
| 190 {"foo.com:443", "spdyproxy", false}, // 2. Case matters. | |
| 191 {"foo.com:443", "SpdyProxy", true}, // 3. OK. | |
| 192 {"foo.com:443", "SpdyProxy1234567", true}, // 4. OK | |
| 193 {"bar.com:80", "SpdyProxy1234567", true}, // 5. OK. | |
| 194 {"foo.com:443", "SpdyProxyxxx", true}, // 6. OK | |
| 195 {"", "SpdyProxy1234567", false}, // 7. No challenger. | |
| 196 {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host. | |
| 197 {"foo.com", "SpdyProxy1234567", false}, // 9. No port. | |
| 198 {"foo.com:80", "SpdyProxy1234567", false}, // 10.Wrong port. | |
| 199 {"bar.com:81", "SpdyProxy1234567", false}, // 11.Wrong port. | |
| 200 }; | |
| 201 | |
| 202 for (int i = 0; i <= 11; ++i) { | |
| 203 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); | |
| 204 auth_info->challenger = net::HostPortPair::FromString(tests[i].host); | |
| 205 auth_info->realm = tests[i].realm; | |
| 206 EXPECT_EQ(tests[i].expected_to_succeed, | |
| 207 settings_->IsAcceptableAuthChallenge(auth_info.get())); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 TEST_F(DataReductionProxySettingsTest, TestChallengeTokens) { | |
| 212 typedef struct { | |
| 213 std::string realm; | |
| 214 bool expected_empty_token; | |
| 215 } token_test; | |
| 216 | |
| 217 token_test tests[] = { | |
| 218 {"", true}, // 0. No realm. | |
| 219 {"xxx", true}, // 1. realm too short. | |
| 220 {"spdyproxy", true}, // 2. no salt. | |
| 221 {"SpdyProxyxxx", true}, // 3. Salt not an int. | |
| 222 {"SpdyProxy1234567", false}, // 4. OK | |
| 223 }; | |
| 224 | |
| 225 for (int i = 0; i <= 4; ++i) { | |
| 226 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); | |
| 227 auth_info->challenger = | |
| 228 net::HostPortPair::FromString(kDataReductionProxy); | |
| 229 auth_info->realm = tests[i].realm; | |
| 230 base::string16 token = settings_->GetTokenForAuthChallenge(auth_info.get()); | |
| 231 EXPECT_EQ(tests[i].expected_empty_token, token.empty()); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { | 146 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { |
| 236 int64 original_content_length; | 147 int64 original_content_length; |
| 237 int64 received_content_length; | 148 int64 received_content_length; |
| 238 int64 last_update_time; | 149 int64 last_update_time; |
| 239 settings_->ResetDataReductionStatistics(); | 150 settings_->ResetDataReductionStatistics(); |
| 240 settings_->GetContentLengths(kNumDaysInHistory, | 151 settings_->GetContentLengths(kNumDaysInHistory, |
| 241 &original_content_length, | 152 &original_content_length, |
| 242 &received_content_length, | 153 &received_content_length, |
| 243 &last_update_time); | 154 &last_update_time); |
| 244 EXPECT_EQ(0L, original_content_length); | 155 EXPECT_EQ(0L, original_content_length); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) { | 328 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) { |
| 418 // InitDataReductionProxySettings with the preference off will directly call | 329 // InitDataReductionProxySettings with the preference off will directly call |
| 419 // LogProxyState. | 330 // LogProxyState. |
| 420 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); | 331 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); |
| 421 EXPECT_CALL(*settings, RecordStartupState(PROXY_DISABLED)); | 332 EXPECT_CALL(*settings, RecordStartupState(PROXY_DISABLED)); |
| 422 | 333 |
| 423 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, false); | 334 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, false); |
| 424 CheckInitDataReductionProxy(false); | 335 CheckInitDataReductionProxy(false); |
| 425 } | 336 } |
| 426 | 337 |
| 427 TEST_F(DataReductionProxySettingsTest, TestSetProxyFromCommandLine) { | 338 TEST_F(DataReductionProxySettingsTest, TestEnableProxyFromCommandLine) { |
| 428 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); | 339 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); |
| 429 EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED)); | 340 EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED)); |
| 430 | 341 |
| 431 CommandLine::ForCurrentProcess()->AppendSwitch( | 342 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 432 switches::kEnableDataReductionProxy); | 343 switches::kEnableDataReductionProxy); |
| 433 CheckInitDataReductionProxy(true); | 344 CheckInitDataReductionProxy(true); |
| 434 } | 345 } |
| 435 | 346 |
| 436 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { | 347 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { |
| 437 DataReductionProxySettings::ContentLengthList result = | 348 DataReductionProxySettings::ContentLengthList result = |
| (...skipping 27 matching lines...) Expand all Loading... |
| 465 scoped_refptr<net::TestURLRequestContextGetter> request_context = | 376 scoped_refptr<net::TestURLRequestContextGetter> request_context = |
| 466 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 377 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); |
| 467 settings_->InitDataReductionProxySettings(&pref_service_, | 378 settings_->InitDataReductionProxySettings(&pref_service_, |
| 468 &pref_service_, | 379 &pref_service_, |
| 469 request_context.get()); | 380 request_context.get()); |
| 470 | 381 |
| 471 base::MessageLoop::current()->RunUntilIdle(); | 382 base::MessageLoop::current()->RunUntilIdle(); |
| 472 } | 383 } |
| 473 | 384 |
| 474 } // namespace data_reduction_proxy | 385 } // namespace data_reduction_proxy |
| OLD | NEW |