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, |
(...skipping 16 matching lines...) Expand all Loading... | |
52 } | 58 } |
53 | 59 |
54 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { | 60 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { |
55 return profile_prefs_; | 61 return profile_prefs_; |
56 } | 62 } |
57 | 63 |
58 virtual PrefService* GetLocalStatePrefs() OVERRIDE { | 64 virtual PrefService* GetLocalStatePrefs() OVERRIDE { |
59 return local_state_prefs_; | 65 return local_state_prefs_; |
60 } | 66 } |
61 | 67 |
68 virtual std::string GetDefaultProxyHost() OVERRIDE { return std::string(); } | |
69 virtual std::string GetDefaultFallbackProxyHost() OVERRIDE { | |
70 return std::string(); | |
71 } | |
72 | |
62 void set_probe_result(const std::string& test_url, | 73 void set_probe_result(const std::string& test_url, |
63 const std::string& response, | 74 const std::string& response, |
64 bool success) { | 75 bool success) { |
65 test_url_ = test_url; | 76 test_url_ = test_url; |
66 response_ = response; | 77 response_ = response; |
67 success_ = success; | 78 success_ = success; |
68 } | 79 } |
69 | 80 |
70 const int fake_fetcher_request_count() { | 81 const int fake_fetcher_request_count() { |
71 return fake_fetcher_request_count_; | 82 return fake_fetcher_request_count_; |
(...skipping 11 matching lines...) Expand all Loading... | |
83 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() | 94 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() |
84 : testing::Test() { | 95 : testing::Test() { |
85 } | 96 } |
86 | 97 |
87 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} | 98 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} |
88 | 99 |
89 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { | 100 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { |
90 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 101 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
91 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 102 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
92 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 103 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
104 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); | |
105 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
93 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); | 106 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); |
94 } | 107 } |
95 | 108 |
96 // testing::Test implementation: | 109 // testing::Test implementation: |
97 void DataReductionProxySettingsTestBase::SetUp() { | 110 void DataReductionProxySettingsTestBase::SetUp() { |
98 PrefRegistrySimple* registry = pref_service_.registry(); | 111 PrefRegistrySimple* registry = pref_service_.registry(); |
99 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | 112 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); |
100 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | 113 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); |
101 registry->RegisterInt64Pref( | 114 registry->RegisterInt64Pref( |
102 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); | 115 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); |
(...skipping 24 matching lines...) Expand all Loading... | |
127 std::string server; | 140 std::string server; |
128 dict->GetString("mode", &mode); | 141 dict->GetString("mode", &mode); |
129 ASSERT_EQ(expected_mode, mode); | 142 ASSERT_EQ(expected_mode, mode); |
130 dict->GetString("server", &server); | 143 dict->GetString("server", &server); |
131 ASSERT_EQ(expected_servers, server); | 144 ASSERT_EQ(expected_servers, server); |
132 } | 145 } |
133 | 146 |
134 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 147 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
135 bool expected_enabled) { | 148 bool expected_enabled) { |
136 if (expected_enabled) { | 149 if (expected_enabled) { |
150 std::string main_proxy = kDataReductionProxyOrigin; | |
151 std::string fallback_proxy = kDataReductionProxyFallback; | |
137 std::string servers = | 152 std::string servers = |
138 "http=" + Settings()->GetDataReductionProxyOrigin() + ",direct://;"; | 153 "http=" + main_proxy + "," + fallback_proxy + ",direct://;"; |
139 CheckProxyPref(servers, | 154 CheckProxyPref(servers, |
140 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 155 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
141 } else { | 156 } else { |
142 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | 157 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); |
143 } | 158 } |
144 } | 159 } |
145 | 160 |
146 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, | 161 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, |
147 const std::string& probe_url, | 162 const std::string& probe_url, |
148 const std::string& response, | 163 const std::string& response, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 | 223 |
209 virtual void SetProbeResult(const std::string& test_url, | 224 virtual void SetProbeResult(const std::string& test_url, |
210 const std::string& response, | 225 const std::string& response, |
211 bool success) OVERRIDE { | 226 bool success) OVERRIDE { |
212 settings_->set_probe_result(test_url, response, success); | 227 settings_->set_probe_result(test_url, response, success); |
213 } | 228 } |
214 | 229 |
215 scoped_ptr<TestDataReductionProxySettings> settings_; | 230 scoped_ptr<TestDataReductionProxySettings> settings_; |
216 }; | 231 }; |
217 | 232 |
233 TEST_F(DataReductionProxySettingsTest, TestAuthenticationInit) { | |
234 AddProxyToCommandLine(); | |
235 net::HttpAuthCache cache; | |
236 settings_->InitDataReductionAuthentication(&cache); | |
237 DataReductionProxySettings::DataReductionProxyList proxies = | |
238 settings_->GetDataReductionProxies(); | |
239 for (DataReductionProxySettings::DataReductionProxyList::iterator it = | |
240 proxies.begin(); | |
bengr
2013/10/22 21:44:22
indent +3, i think.
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
241 it != proxies.end(); ++it) { | |
242 //GURL server = (*it).GetOrigin(); | |
243 net::HttpAuthCache::Entry* entry = | |
244 cache.LookupByPath(*it, std::string()); | |
245 EXPECT_TRUE(entry != NULL); | |
246 EXPECT_EQ(net::HttpAuth::AUTH_SCHEME_SPDYPROXY, entry->scheme()); | |
247 EXPECT_EQ("SpdyProxy", entry->auth_challenge().substr(0,9)); | |
248 } | |
249 GURL bad_server = GURL("https://bad.proxy.com/"); | |
250 net::HttpAuthCache::Entry* entry = | |
251 cache.LookupByPath(bad_server, std::string()); | |
252 EXPECT_TRUE(entry == NULL); | |
253 | |
bengr
2013/10/22 21:44:22
Remove blank line.
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
254 } | |
255 | |
218 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { | 256 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { |
219 AddProxyToCommandLine(); | 257 AddProxyToCommandLine(); |
220 // SetUp() adds the origin to the command line, which should be returned here. | 258 // SetUp() adds the origin to the command line, which should be returned here. |
221 std::string result = settings_->GetDataReductionProxyOrigin(); | 259 std::string result = settings_->GetDataReductionProxyOrigin(); |
222 EXPECT_EQ(kDataReductionProxyOrigin, result); | 260 EXPECT_EQ(kDataReductionProxyOrigin, result); |
223 } | 261 } |
224 | 262 |
225 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyAuth) { | 263 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { |
226 AddProxyToCommandLine(); | 264 DataReductionProxySettings::DataReductionProxyList proxies = |
227 // SetUp() adds the auth string to the command line, which should be returned | 265 settings_->GetDataReductionProxies(); |
228 // here. | 266 EXPECT_TRUE(proxies.empty()); |
229 std::string result = settings_->GetDataReductionProxyAuth(); | 267 |
230 EXPECT_EQ(kDataReductionProxyAuth, result); | 268 // Adding just the fallback on the command line shouldn't add a proxy. |
269 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
270 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); | |
271 proxies = settings_->GetDataReductionProxies(); | |
272 EXPECT_TRUE(proxies.empty()); | |
273 | |
274 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
275 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | |
276 proxies = settings_->GetDataReductionProxies(); | |
277 EXPECT_EQ(2u, proxies.size()); | |
278 | |
279 EXPECT_EQ("foo.com",proxies[0].host()); | |
bengr
2013/10/22 21:44:22
Here and in the three lines below, add a space aft
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
280 EXPECT_EQ(443,proxies[0].EffectiveIntPort()); | |
281 EXPECT_EQ("bar.com",proxies[1].host()); | |
282 EXPECT_EQ(80,proxies[1].EffectiveIntPort()); | |
231 } | 283 } |
232 | 284 |
233 // Test that the auth value set by preprocessor directive is not returned | 285 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) { |
286 AddProxyToCommandLine(); | |
287 std::string salt = "8675309"; // Jenny's number to test the hash generator. | |
288 std::string salted_key = salt + kDataReductionProxyAuth + salt; | |
289 base::string16 expected_hash = UTF8ToUTF16(base::MD5String(salted_key)); | |
290 EXPECT_EQ(expected_hash, settings_->AuthHashForSalt(8675309)); | |
291 } | |
292 | |
293 // 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 | 294 // when an origin is set via a switch. This test only does anything useful in |
235 // Chrome builds. | 295 // Chrome builds. |
236 TEST_F(DataReductionProxySettingsTest, | 296 TEST_F(DataReductionProxySettingsTest, |
237 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | 297 TestAuthHashGenerationWithOriginSetViaSwitch) { |
238 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 298 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
239 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 299 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
240 std::string result = settings_->GetDataReductionProxyAuth(); | 300 EXPECT_EQ(base::string16(), settings_->AuthHashForSalt(8675309)); |
241 EXPECT_EQ("", result); | |
242 } | 301 } |
243 | 302 |
244 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { | 303 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { |
245 settings_->InitPrefMembers(); | 304 settings_->InitPrefMembers(); |
246 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); | 305 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); |
247 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 306 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
248 | 307 |
249 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | 308 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); |
250 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 309 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
251 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 310 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
252 | 311 |
253 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | 312 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, |
254 base::Value::CreateBooleanValue(true)); | 313 base::Value::CreateBooleanValue(true)); |
255 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 314 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
256 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); | 315 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); |
257 } | 316 } |
258 | 317 |
318 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) { | |
319 AddProxyToCommandLine(); | |
320 typedef struct { | |
321 std::string host; | |
322 std::string realm; | |
323 bool expected_to_succeed; | |
324 } challenge_test; | |
325 | |
326 challenge_test tests[] = { | |
327 {"foo.com:443", "", false}, // 0. No realm. | |
328 {"foo.com:443", "xxx", false}, // 1. Wrong realm. | |
329 {"foo.com:443", "spdyproxy", false}, // 2. Case matters. | |
330 {"foo.com:443", "SpdyProxy", true}, // 3. OK. | |
331 {"foo.com:443", "SpdyProxy1234567", true}, // 4. OK | |
332 {"bar.com:80", "SpdyProxy1234567", true}, // 5. OK. | |
333 {"foo.com:443", "SpdyProxyxxx", true}, // 6. OK | |
334 {"", "SpdyProxy1234567", false}, // 7. No challenger. | |
335 {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host. | |
336 {"foo.com", "SpdyProxy1234567", false}, // 9. No port. | |
337 {"foo.com:80", "SpdyProxy1234567", false}, // 10.Wrong port. | |
338 {"bar.com:81", "SpdyProxy1234567", false}, // 11.Wrong port. | |
339 }; | |
340 | |
341 for (int i = 0; i <= 11; i++) { | |
bengr
2013/10/22 21:44:22
++i
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
342 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); | |
343 auth_info->challenger = net::HostPortPair::FromString(tests[i].host); | |
344 auth_info->realm = tests[i].realm; | |
345 EXPECT_EQ(tests[i].expected_to_succeed, | |
346 settings_->IsAcceptableAuthChallenge(auth_info.get())); | |
347 } | |
348 } | |
349 | |
350 TEST_F(DataReductionProxySettingsTest, TestChallengeTokens) { | |
351 AddProxyToCommandLine(); | |
352 typedef struct { | |
353 std::string realm; | |
354 bool expected_empty_token; | |
355 } token_test; | |
356 | |
357 token_test tests[] = { | |
358 {"", true}, // 0. No realm. | |
359 {"xxx", true}, // 1. realm too short. | |
360 {"spdyproxy", true}, // 2. no salt. | |
361 {"SpdyProxyxxx", true}, // 3. Salt not an int. | |
362 {"SpdyProxy1234567", false},// 4. OK | |
bengr
2013/10/22 21:44:22
Shift the comments all +1
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
363 }; | |
364 | |
365 for (int i = 0; i <= 4; i++) { | |
bengr
2013/10/22 21:44:22
++i
marq (ping after 24h)
2013/10/23 12:23:20
Done.
| |
366 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); | |
367 auth_info->challenger = | |
368 net::HostPortPair::FromString(kDataReductionProxyOrigin); | |
369 auth_info->realm = tests[i].realm; | |
370 base::string16 token = settings_->GetTokenForAuthChallenge(auth_info.get()); | |
371 EXPECT_EQ(tests[i].expected_empty_token, token.empty()); | |
372 } | |
373 } | |
374 | |
259 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { | 375 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { |
260 int64 original_content_length; | 376 int64 original_content_length; |
261 int64 received_content_length; | 377 int64 received_content_length; |
262 int64 last_update_time; | 378 int64 last_update_time; |
263 settings_->ResetDataReductionStatistics(); | 379 settings_->ResetDataReductionStatistics(); |
264 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, | 380 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, |
265 &original_content_length, | 381 &original_content_length, |
266 &received_content_length, | 382 &received_content_length, |
267 &last_update_time); | 383 &last_update_time); |
268 EXPECT_EQ(0L, original_content_length); | 384 EXPECT_EQ(0L, original_content_length); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 "http://*bat.com" | 525 "http://*bat.com" |
410 }; | 526 }; |
411 | 527 |
412 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); | 528 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); |
413 int i = 0; | 529 int i = 0; |
414 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | 530 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); |
415 it != settings_->bypass_rules_.end(); ++it) { | 531 it != settings_->bypass_rules_.end(); ++it) { |
416 EXPECT_EQ(expected[i++], *it); | 532 EXPECT_EQ(expected[i++], *it); |
417 } | 533 } |
418 } | 534 } |
419 | |
OLD | NEW |