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