| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" | 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/md5.h" |
| 8 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 9 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| 10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 11 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h" | 15 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h" |
| 14 #include "chrome/browser/prefs/proxy_prefs.h" | 16 #include "chrome/browser/prefs/proxy_prefs.h" |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/metrics/variations/variations_util.h" | 19 #include "chrome/common/metrics/variations/variations_util.h" |
| 18 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 19 #include "components/variations/entropy_provider.h" | 21 #include "components/variations/entropy_provider.h" |
| 22 #include "net/base/auth.h" |
| 23 #include "net/base/host_port_pair.h" |
| 24 #include "net/http/http_auth.h" |
| 25 #include "net/http/http_auth_cache.h" |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 23 | 29 |
| 24 const char kDataReductionProxyOrigin[] = "https://foo:443/"; | 30 const char kDataReductionProxyOrigin[] = "https://foo.com:443/"; |
| 31 const char kDataReductionProxyFallback[] = "http://bar.com:80"; |
| 25 const char kDataReductionProxyAuth[] = "12345"; | 32 const char kDataReductionProxyAuth[] = "12345"; |
| 26 | 33 |
| 27 const char kProbeURLWithOKResponse[] = "http://ok.org/"; | 34 const char kProbeURLWithOKResponse[] = "http://ok.org/"; |
| 28 const char kProbeURLWithBadResponse[] = "http://bad.org/"; | 35 const char kProbeURLWithBadResponse[] = "http://bad.org/"; |
| 29 const char kProbeURLWithNoResponse[] = "http://no.org/"; | 36 const char kProbeURLWithNoResponse[] = "http://no.org/"; |
| 30 | 37 |
| 31 class TestDataReductionProxySettings | 38 class TestDataReductionProxySettings |
| 32 : public DataReductionProxySettings { | 39 : public DataReductionProxySettings { |
| 33 public: | 40 public: |
| 34 TestDataReductionProxySettings(PrefService* profile_prefs, | 41 TestDataReductionProxySettings(PrefService* profile_prefs, |
| 35 PrefService* local_state_prefs) | 42 PrefService* local_state_prefs) |
| 36 : DataReductionProxySettings(), | 43 : DataReductionProxySettings(), |
| 37 success_(false), | 44 success_(false), |
| 38 fake_fetcher_request_count_(0), | 45 fake_fetcher_request_count_(0), |
| 39 profile_prefs_(profile_prefs), | 46 profile_prefs_(profile_prefs), |
| 40 local_state_prefs_(local_state_prefs) { | 47 local_state_prefs_(local_state_prefs) { |
| 41 } | 48 } |
| 42 | 49 |
| 50 // TODO(marq): Replace virtual methods with MOCKs. Also mock LogProxyState. |
| 43 // DataReductionProxySettings implementation: | 51 // DataReductionProxySettings implementation: |
| 44 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { | 52 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { |
| 45 if (test_url_.empty()) | 53 if (test_url_.empty()) |
| 46 return NULL; | 54 return NULL; |
| 47 net::URLFetcher* fetcher = | 55 net::URLFetcher* fetcher = |
| 48 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); | 56 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); |
| 49 fake_fetcher_request_count_++; | 57 fake_fetcher_request_count_++; |
| 50 return fetcher; | 58 return fetcher; |
| 51 } | 59 } |
| 52 | 60 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 82 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() | 90 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() |
| 83 : testing::Test() { | 91 : testing::Test() { |
| 84 } | 92 } |
| 85 | 93 |
| 86 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} | 94 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} |
| 87 | 95 |
| 88 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { | 96 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { |
| 89 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 97 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 90 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 98 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 91 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 99 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 100 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); |
| 101 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 92 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); | 102 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); |
| 93 } | 103 } |
| 94 | 104 |
| 95 // testing::Test implementation: | 105 // testing::Test implementation: |
| 96 void DataReductionProxySettingsTestBase::SetUp() { | 106 void DataReductionProxySettingsTestBase::SetUp() { |
| 97 PrefRegistrySimple* registry = pref_service_.registry(); | 107 PrefRegistrySimple* registry = pref_service_.registry(); |
| 98 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | 108 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); |
| 99 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | 109 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); |
| 100 registry->RegisterInt64Pref( | 110 registry->RegisterInt64Pref( |
| 101 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); | 111 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 126 std::string server; | 136 std::string server; |
| 127 dict->GetString("mode", &mode); | 137 dict->GetString("mode", &mode); |
| 128 ASSERT_EQ(expected_mode, mode); | 138 ASSERT_EQ(expected_mode, mode); |
| 129 dict->GetString("server", &server); | 139 dict->GetString("server", &server); |
| 130 ASSERT_EQ(expected_servers, server); | 140 ASSERT_EQ(expected_servers, server); |
| 131 } | 141 } |
| 132 | 142 |
| 133 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 143 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
| 134 bool expected_enabled) { | 144 bool expected_enabled) { |
| 135 if (expected_enabled) { | 145 if (expected_enabled) { |
| 146 std::string main_proxy = kDataReductionProxyOrigin; |
| 147 std::string fallback_proxy = kDataReductionProxyFallback; |
| 136 std::string servers = | 148 std::string servers = |
| 137 "http=" + Settings()->GetDataReductionProxyOrigin() + ",direct://;"; | 149 "http=" + main_proxy + "," + fallback_proxy + ",direct://;"; |
| 138 CheckProxyPref(servers, | 150 CheckProxyPref(servers, |
| 139 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 151 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
| 140 } else { | 152 } else { |
| 141 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | 153 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); |
| 142 } | 154 } |
| 143 } | 155 } |
| 144 | 156 |
| 145 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, | 157 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, |
| 146 const std::string& probe_url, | 158 const std::string& probe_url, |
| 147 const std::string& response, | 159 const std::string& response, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 219 |
| 208 virtual void SetProbeResult(const std::string& test_url, | 220 virtual void SetProbeResult(const std::string& test_url, |
| 209 const std::string& response, | 221 const std::string& response, |
| 210 bool success) OVERRIDE { | 222 bool success) OVERRIDE { |
| 211 settings_->set_probe_result(test_url, response, success); | 223 settings_->set_probe_result(test_url, response, success); |
| 212 } | 224 } |
| 213 | 225 |
| 214 scoped_ptr<TestDataReductionProxySettings> settings_; | 226 scoped_ptr<TestDataReductionProxySettings> settings_; |
| 215 }; | 227 }; |
| 216 | 228 |
| 229 TEST_F(DataReductionProxySettingsTest, TestAuthenticationInit) { |
| 230 AddProxyToCommandLine(); |
| 231 net::HttpAuthCache cache; |
| 232 DataReductionProxySettings::InitDataReductionAuthentication(&cache); |
| 233 DataReductionProxySettings::DataReductionProxyList proxies = |
| 234 DataReductionProxySettings::GetDataReductionProxies(); |
| 235 for (DataReductionProxySettings::DataReductionProxyList::iterator it = |
| 236 proxies.begin(); it != proxies.end(); ++it) { |
| 237 //GURL server = (*it).GetOrigin(); |
| 238 net::HttpAuthCache::Entry* entry = |
| 239 cache.LookupByPath(*it, std::string()); |
| 240 EXPECT_TRUE(entry != NULL); |
| 241 EXPECT_EQ(net::HttpAuth::AUTH_SCHEME_SPDYPROXY, entry->scheme()); |
| 242 EXPECT_EQ("SpdyProxy", entry->auth_challenge().substr(0,9)); |
| 243 } |
| 244 GURL bad_server = GURL("https://bad.proxy.com/"); |
| 245 net::HttpAuthCache::Entry* entry = |
| 246 cache.LookupByPath(bad_server, std::string()); |
| 247 EXPECT_TRUE(entry == NULL); |
| 248 } |
| 249 |
| 217 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { | 250 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { |
| 218 AddProxyToCommandLine(); | 251 AddProxyToCommandLine(); |
| 219 // SetUp() adds the origin to the command line, which should be returned here. | 252 // SetUp() adds the origin to the command line, which should be returned here. |
| 220 std::string result = settings_->GetDataReductionProxyOrigin(); | 253 std::string result = |
| 254 DataReductionProxySettings::GetDataReductionProxyOrigin(); |
| 221 EXPECT_EQ(kDataReductionProxyOrigin, result); | 255 EXPECT_EQ(kDataReductionProxyOrigin, result); |
| 222 } | 256 } |
| 223 | 257 |
| 224 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyAuth) { | 258 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { |
| 225 AddProxyToCommandLine(); | 259 DataReductionProxySettings::DataReductionProxyList proxies = |
| 226 // SetUp() adds the auth string to the command line, which should be returned | 260 DataReductionProxySettings::GetDataReductionProxies(); |
| 227 // here. | 261 |
| 228 std::string result = settings_->GetDataReductionProxyAuth(); | 262 unsigned int expected_proxy_size = 0u; |
| 229 EXPECT_EQ(kDataReductionProxyAuth, result); | 263 #if defined(SPDY_PROXY_AUTH_ORIGIN) |
| 264 ++expected_proxy_size; |
| 265 #endif |
| 266 #if defined(DATA_REDUCTION_FALLBACK_HOST) |
| 267 ++expected_proxy_size; |
| 268 #endif |
| 269 |
| 270 EXPECT_EQ(expected_proxy_size, proxies.size()); |
| 271 |
| 272 // Adding just the fallback on the command line shouldn't add a proxy unless |
| 273 // there was already one compiled in. |
| 274 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 275 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); |
| 276 proxies = DataReductionProxySettings::GetDataReductionProxies(); |
| 277 |
| 278 // So: if there weren't any proxies before, there still won't be. |
| 279 // If there were one or two, there will be two now. |
| 280 expected_proxy_size = expected_proxy_size == 0u ? 0u : 2u; |
| 281 |
| 282 EXPECT_EQ(expected_proxy_size, proxies.size()); |
| 283 |
| 284 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 285 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 286 proxies = DataReductionProxySettings::GetDataReductionProxies(); |
| 287 EXPECT_EQ(2u, proxies.size()); |
| 288 |
| 289 // Command line proxies have precedence, so even if there were other values |
| 290 // compiled in, these should be the ones in the list. |
| 291 EXPECT_EQ("foo.com", proxies[0].host()); |
| 292 EXPECT_EQ(443 ,proxies[0].EffectiveIntPort()); |
| 293 EXPECT_EQ("bar.com", proxies[1].host()); |
| 294 EXPECT_EQ(80, proxies[1].EffectiveIntPort()); |
| 230 } | 295 } |
| 231 | 296 |
| 232 // Test that the auth value set by preprocessor directive is not returned | 297 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) { |
| 298 AddProxyToCommandLine(); |
| 299 std::string salt = "8675309"; // Jenny's number to test the hash generator. |
| 300 std::string salted_key = salt + kDataReductionProxyAuth + salt; |
| 301 base::string16 expected_hash = UTF8ToUTF16(base::MD5String(salted_key)); |
| 302 EXPECT_EQ(expected_hash, |
| 303 DataReductionProxySettings::AuthHashForSalt(8675309)); |
| 304 } |
| 305 |
| 306 // Test that the auth key set by preprocessor directive is not used |
| 233 // when an origin is set via a switch. This test only does anything useful in | 307 // when an origin is set via a switch. This test only does anything useful in |
| 234 // Chrome builds. | 308 // Chrome builds. |
| 235 TEST_F(DataReductionProxySettingsTest, | 309 TEST_F(DataReductionProxySettingsTest, |
| 236 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | 310 TestAuthHashGenerationWithOriginSetViaSwitch) { |
| 237 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 311 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 238 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 312 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 239 std::string result = settings_->GetDataReductionProxyAuth(); | 313 EXPECT_EQ(base::string16(), |
| 240 EXPECT_EQ("", result); | 314 DataReductionProxySettings::AuthHashForSalt(8675309)); |
| 241 } | 315 } |
| 242 | 316 |
| 317 |
| 243 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { | 318 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { |
| 244 settings_->InitPrefMembers(); | 319 settings_->InitPrefMembers(); |
| 245 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); | 320 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); |
| 246 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 321 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
| 247 | 322 |
| 248 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | 323 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); |
| 249 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 324 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 250 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 325 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
| 251 | 326 |
| 252 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | 327 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, |
| 253 base::Value::CreateBooleanValue(true)); | 328 base::Value::CreateBooleanValue(true)); |
| 254 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 329 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 255 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); | 330 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); |
| 256 } | 331 } |
| 257 | 332 |
| 333 |
| 334 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) { |
| 335 AddProxyToCommandLine(); |
| 336 typedef struct { |
| 337 std::string host; |
| 338 std::string realm; |
| 339 bool expected_to_succeed; |
| 340 } challenge_test; |
| 341 |
| 342 challenge_test tests[] = { |
| 343 {"foo.com:443", "", false}, // 0. No realm. |
| 344 {"foo.com:443", "xxx", false}, // 1. Wrong realm. |
| 345 {"foo.com:443", "spdyproxy", false}, // 2. Case matters. |
| 346 {"foo.com:443", "SpdyProxy", true}, // 3. OK. |
| 347 {"foo.com:443", "SpdyProxy1234567", true}, // 4. OK |
| 348 {"bar.com:80", "SpdyProxy1234567", true}, // 5. OK. |
| 349 {"foo.com:443", "SpdyProxyxxx", true}, // 6. OK |
| 350 {"", "SpdyProxy1234567", false}, // 7. No challenger. |
| 351 {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host. |
| 352 {"foo.com", "SpdyProxy1234567", false}, // 9. No port. |
| 353 {"foo.com:80", "SpdyProxy1234567", false}, // 10.Wrong port. |
| 354 {"bar.com:81", "SpdyProxy1234567", false}, // 11.Wrong port. |
| 355 }; |
| 356 |
| 357 for (int i = 0; i <= 11; ++i) { |
| 358 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); |
| 359 auth_info->challenger = net::HostPortPair::FromString(tests[i].host); |
| 360 auth_info->realm = tests[i].realm; |
| 361 EXPECT_EQ(tests[i].expected_to_succeed, |
| 362 settings_->IsAcceptableAuthChallenge(auth_info.get())); |
| 363 } |
| 364 } |
| 365 |
| 366 TEST_F(DataReductionProxySettingsTest, TestChallengeTokens) { |
| 367 AddProxyToCommandLine(); |
| 368 typedef struct { |
| 369 std::string realm; |
| 370 bool expected_empty_token; |
| 371 } token_test; |
| 372 |
| 373 token_test tests[] = { |
| 374 {"", true}, // 0. No realm. |
| 375 {"xxx", true}, // 1. realm too short. |
| 376 {"spdyproxy", true}, // 2. no salt. |
| 377 {"SpdyProxyxxx", true}, // 3. Salt not an int. |
| 378 {"SpdyProxy1234567", false}, // 4. OK |
| 379 }; |
| 380 |
| 381 for (int i = 0; i <= 4; ++i) { |
| 382 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); |
| 383 auth_info->challenger = |
| 384 net::HostPortPair::FromString(kDataReductionProxyOrigin); |
| 385 auth_info->realm = tests[i].realm; |
| 386 base::string16 token = settings_->GetTokenForAuthChallenge(auth_info.get()); |
| 387 EXPECT_EQ(tests[i].expected_empty_token, token.empty()); |
| 388 } |
| 389 } |
| 390 |
| 258 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { | 391 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { |
| 259 int64 original_content_length; | 392 int64 original_content_length; |
| 260 int64 received_content_length; | 393 int64 received_content_length; |
| 261 int64 last_update_time; | 394 int64 last_update_time; |
| 262 settings_->ResetDataReductionStatistics(); | 395 settings_->ResetDataReductionStatistics(); |
| 263 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, | 396 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, |
| 264 &original_content_length, | 397 &original_content_length, |
| 265 &received_content_length, | 398 &received_content_length, |
| 266 &last_update_time); | 399 &last_update_time); |
| 267 EXPECT_EQ(0L, original_content_length); | 400 EXPECT_EQ(0L, original_content_length); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 "http://*bat.com" | 541 "http://*bat.com" |
| 409 }; | 542 }; |
| 410 | 543 |
| 411 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); | 544 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); |
| 412 int i = 0; | 545 int i = 0; |
| 413 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | 546 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); |
| 414 it != settings_->bypass_rules_.end(); ++it) { | 547 it != settings_->bypass_rules_.end(); ++it) { |
| 415 EXPECT_EQ(expected[i++], *it); | 548 EXPECT_EQ(expected[i++], *it); |
| 416 } | 549 } |
| 417 } | 550 } |
| 418 | |
| OLD | NEW |