| 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" |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 23 | 27 |
| 24 const char kDataReductionProxyOrigin[] = "https://foo:443/"; | 28 const char kDataReductionProxyOrigin[] = "https://foo:443/"; |
| 25 const char kDataReductionProxyOriginHostPort[] = "foo:443"; | 29 const char kDataReductionProxyFallback[] = "http://bar:80"; |
| 26 const char kDataReductionProxyAuth[] = "12345"; | 30 const char kDataReductionProxyAuth[] = "12345"; |
| 27 | 31 |
| 28 const char kProbeURLWithOKResponse[] = "http://ok.org/"; | 32 const char kProbeURLWithOKResponse[] = "http://ok.org/"; |
| 29 const char kProbeURLWithBadResponse[] = "http://bad.org/"; | 33 const char kProbeURLWithBadResponse[] = "http://bad.org/"; |
| 30 const char kProbeURLWithNoResponse[] = "http://no.org/"; | 34 const char kProbeURLWithNoResponse[] = "http://no.org/"; |
| 31 | 35 |
| 32 class TestDataReductionProxySettings | 36 class TestDataReductionProxySettings |
| 33 : public DataReductionProxySettings { | 37 : public DataReductionProxySettings { |
| 34 public: | 38 public: |
| 35 TestDataReductionProxySettings(PrefService* profile_prefs, | 39 TestDataReductionProxySettings(PrefService* profile_prefs, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() | 87 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() |
| 84 : testing::Test() { | 88 : testing::Test() { |
| 85 } | 89 } |
| 86 | 90 |
| 87 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} | 91 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} |
| 88 | 92 |
| 89 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { | 93 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { |
| 90 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 94 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 91 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 95 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 92 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 96 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 97 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); |
| 98 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 93 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); | 99 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); |
| 94 } | 100 } |
| 95 | 101 |
| 96 // testing::Test implementation: | 102 // testing::Test implementation: |
| 97 void DataReductionProxySettingsTestBase::SetUp() { | 103 void DataReductionProxySettingsTestBase::SetUp() { |
| 98 PrefRegistrySimple* registry = pref_service_.registry(); | 104 PrefRegistrySimple* registry = pref_service_.registry(); |
| 99 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | 105 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); |
| 100 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | 106 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); |
| 101 registry->RegisterInt64Pref( | 107 registry->RegisterInt64Pref( |
| 102 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); | 108 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 127 std::string server; | 133 std::string server; |
| 128 dict->GetString("mode", &mode); | 134 dict->GetString("mode", &mode); |
| 129 ASSERT_EQ(expected_mode, mode); | 135 ASSERT_EQ(expected_mode, mode); |
| 130 dict->GetString("server", &server); | 136 dict->GetString("server", &server); |
| 131 ASSERT_EQ(expected_servers, server); | 137 ASSERT_EQ(expected_servers, server); |
| 132 } | 138 } |
| 133 | 139 |
| 134 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 140 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
| 135 bool expected_enabled) { | 141 bool expected_enabled) { |
| 136 if (expected_enabled) { | 142 if (expected_enabled) { |
| 143 std::string main_proxy = kDataReductionProxyOrigin; |
| 144 std::string fallback_proxy = kDataReductionProxyFallback; |
| 137 std::string servers = | 145 std::string servers = |
| 138 "http=" + Settings()->GetDataReductionProxyOrigin() + ",direct://;"; | 146 "http=" + main_proxy + "," + fallback_proxy + ",direct://;"; |
| 139 CheckProxyPref(servers, | 147 CheckProxyPref(servers, |
| 140 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 148 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
| 141 } else { | 149 } else { |
| 142 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | 150 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 | 153 |
| 146 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, | 154 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, |
| 147 const std::string& probe_url, | 155 const std::string& probe_url, |
| 148 const std::string& response, | 156 const std::string& response, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 scoped_ptr<TestDataReductionProxySettings> settings_; | 223 scoped_ptr<TestDataReductionProxySettings> settings_; |
| 216 }; | 224 }; |
| 217 | 225 |
| 218 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { | 226 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { |
| 219 AddProxyToCommandLine(); | 227 AddProxyToCommandLine(); |
| 220 // SetUp() adds the origin to the command line, which should be returned here. | 228 // SetUp() adds the origin to the command line, which should be returned here. |
| 221 std::string result = settings_->GetDataReductionProxyOrigin(); | 229 std::string result = settings_->GetDataReductionProxyOrigin(); |
| 222 EXPECT_EQ(kDataReductionProxyOrigin, result); | 230 EXPECT_EQ(kDataReductionProxyOrigin, result); |
| 223 } | 231 } |
| 224 | 232 |
| 225 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyAuth) { | 233 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { |
| 226 AddProxyToCommandLine(); | 234 DataReductionProxySettings::DataReductionProxyList proxies = |
| 227 // SetUp() adds the auth string to the command line, which should be returned | 235 settings_->GetDataReductionProxies(); |
| 228 // here. | 236 EXPECT_TRUE(proxies.empty()); |
| 229 std::string result = settings_->GetDataReductionProxyAuth(); | 237 |
| 230 EXPECT_EQ(kDataReductionProxyAuth, result); | 238 // Adding just the fallback on the command line shouldn't add a proxy. |
| 239 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 240 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); |
| 241 proxies = settings_->GetDataReductionProxies(); |
| 242 EXPECT_TRUE(proxies.empty()); |
| 243 |
| 244 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 245 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 246 proxies = settings_->GetDataReductionProxies(); |
| 247 EXPECT_EQ(2u, proxies.size()); |
| 248 |
| 249 EXPECT_EQ("foo",proxies[0].host()); |
| 250 EXPECT_EQ(443u,proxies[0].port()); |
| 251 EXPECT_EQ("bar",proxies[1].host()); |
| 252 EXPECT_EQ(80u,proxies[1].port()); |
| 231 } | 253 } |
| 232 | 254 |
| 233 // Test that the auth value set by preprocessor directive is not returned | 255 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) { |
| 256 AddProxyToCommandLine(); |
| 257 std::string salt = "8675309"; // Jenny's number to test the hash generator. |
| 258 std::string salted_key = salt + kDataReductionProxyAuth + salt; |
| 259 base::string16 expected_hash = UTF8ToUTF16(base::MD5String(salted_key)); |
| 260 EXPECT_EQ(expected_hash, settings_->AuthHashForSalt(8675309)); |
| 261 } |
| 262 |
| 263 // Test that the auth key set by preprocessor directive is not used |
| 234 // when an origin is set via a switch. This test only does anything useful in | 264 // when an origin is set via a switch. This test only does anything useful in |
| 235 // Chrome builds. | 265 // Chrome builds. |
| 236 TEST_F(DataReductionProxySettingsTest, | 266 TEST_F(DataReductionProxySettingsTest, |
| 237 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | 267 TestAuthHashGenerationWithOriginSetViaSwitch) { |
| 238 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 268 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 239 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 269 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
| 240 std::string result = settings_->GetDataReductionProxyAuth(); | 270 EXPECT_EQ(base::string16(), settings_->AuthHashForSalt(8675309)); |
| 241 EXPECT_EQ("", result); | |
| 242 } | 271 } |
| 243 | 272 |
| 244 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { | 273 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { |
| 245 settings_->InitPrefMembers(); | 274 settings_->InitPrefMembers(); |
| 246 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); | 275 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); |
| 247 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 276 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
| 248 | 277 |
| 249 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | 278 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); |
| 250 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 279 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 251 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 280 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
| 252 | 281 |
| 253 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | 282 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, |
| 254 base::Value::CreateBooleanValue(true)); | 283 base::Value::CreateBooleanValue(true)); |
| 255 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 284 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
| 256 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); | 285 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); |
| 257 } | 286 } |
| 258 | 287 |
| 288 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) { |
| 289 typedef struct { |
| 290 std::string host; |
| 291 std::string realm; |
| 292 bool expected_to_succeed; |
| 293 } challenge_test; |
| 294 |
| 295 challenge_test tests[] = { |
| 296 {"foo:443", "", false}, // 0. No realm. |
| 297 {"foo:443", "xxx", false}, // 1. Wrong realm. |
| 298 {"foo:443", "spdyproxy", false}, // 2. Case matters. |
| 299 {"foo:443", "SpdyProxy", true}, // 3. OK. |
| 300 {"foo:443", "SpdyProxy1234567", true}, // 4. OK |
| 301 {"bar:80", "SpdyProxy1234567", true}, // 5. OK. |
| 302 {"foo:443", "SpdyProxyxxx", true}, // 6. OK |
| 303 {"", "SpdyProxy1234567", false}, // 7. No challenger. |
| 304 {"xxx:443", "SpdyProxy1234567", false}, // 8. Wrong host. |
| 305 {"foo", "SpdyProxy1234567", false}, // 9. No port. |
| 306 {"foo:80", "SpdyProxy1234567", false}, // 10.Wrong port. |
| 307 {"bar:81", "SpdyProxy1234567", false}, // 11.Wrong port. |
| 308 }; |
| 309 |
| 310 |
| 311 for (int i = 0; i <= 11; i++) { |
| 312 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); |
| 313 auth_info->challenger = net::HostPortPair::FromURL(GURL(tests[i].host)); |
| 314 auth_info->realm = tests[i].realm; |
| 315 EXPECT_EQ(tests[i].expected_to_succeed, |
| 316 settings_->IsAcceptableAuthChallenge(auth_info.get())); |
| 317 } |
| 318 } |
| 319 |
| 259 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { | 320 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { |
| 260 int64 original_content_length; | 321 int64 original_content_length; |
| 261 int64 received_content_length; | 322 int64 received_content_length; |
| 262 int64 last_update_time; | 323 int64 last_update_time; |
| 263 settings_->ResetDataReductionStatistics(); | 324 settings_->ResetDataReductionStatistics(); |
| 264 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, | 325 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, |
| 265 &original_content_length, | 326 &original_content_length, |
| 266 &received_content_length, | 327 &received_content_length, |
| 267 &last_update_time); | 328 &last_update_time); |
| 268 EXPECT_EQ(0L, original_content_length); | 329 EXPECT_EQ(0L, original_content_length); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 }; | 471 }; |
| 411 | 472 |
| 412 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); | 473 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); |
| 413 int i = 0; | 474 int i = 0; |
| 414 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | 475 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); |
| 415 it != settings_->bypass_rules_.end(); ++it) { | 476 it != settings_->bypass_rules_.end(); ++it) { |
| 416 EXPECT_EQ(expected[i++], *it); | 477 EXPECT_EQ(expected[i++], *it); |
| 417 } | 478 } |
| 418 } | 479 } |
| 419 | 480 |
| 481 TEST_F(DataReductionProxySettingsTest, TestAcceptableAuthTokens) { |
| 482 AddProxyToCommandLine(); |
| 483 } |
| 484 |
| OLD | NEW |