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.com:443/"; |
25 const char kDataReductionProxyOriginHostPort[] = "foo:443"; | 29 const char kDataReductionProxyFallback[] = "http://bar.com: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, |
36 PrefService* local_state_prefs) | 40 PrefService* local_state_prefs) |
37 : DataReductionProxySettings(), | 41 : DataReductionProxySettings(), |
38 success_(false), | 42 success_(false), |
39 fake_fetcher_request_count_(0), | 43 fake_fetcher_request_count_(0), |
40 profile_prefs_(profile_prefs), | 44 profile_prefs_(profile_prefs), |
41 local_state_prefs_(local_state_prefs) { | 45 local_state_prefs_(local_state_prefs) { |
42 } | 46 } |
43 | 47 |
44 // DataReductionProxySettings implementation: | 48 // DataReductionProxySettings implementation: |
45 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { | 49 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { |
46 if (test_url_.empty()) | 50 if (test_url_.empty()) |
47 return NULL; | 51 return NULL; |
48 net::URLFetcher* fetcher = | 52 net::URLFetcher* fetcher = |
49 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); | 53 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); |
50 fake_fetcher_request_count_++; | 54 fake_fetcher_request_count_++; |
51 return fetcher; | 55 return fetcher; |
52 } | 56 } |
53 | 57 |
54 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { | 58 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { |
bengr
2013/10/22 17:49:30
Can you use MOCK_METHOD0 for all of these override
marq (ping after 24h)
2013/10/22 21:18:01
I was not previously aware of that, and that will
bengr
2013/10/22 21:44:21
Please add a TODO here and in all locations where
| |
55 return profile_prefs_; | 59 return profile_prefs_; |
56 } | 60 } |
57 | 61 |
58 virtual PrefService* GetLocalStatePrefs() OVERRIDE { | 62 virtual PrefService* GetLocalStatePrefs() OVERRIDE { |
59 return local_state_prefs_; | 63 return local_state_prefs_; |
60 } | 64 } |
61 | 65 |
66 virtual std::string GetDefaultProxyHost() OVERRIDE { return std::string(); } | |
67 virtual std::string GetDefaultFallbackProxyHost() OVERRIDE { | |
68 return std::string(); | |
69 } | |
70 | |
62 void set_probe_result(const std::string& test_url, | 71 void set_probe_result(const std::string& test_url, |
63 const std::string& response, | 72 const std::string& response, |
64 bool success) { | 73 bool success) { |
65 test_url_ = test_url; | 74 test_url_ = test_url; |
66 response_ = response; | 75 response_ = response; |
67 success_ = success; | 76 success_ = success; |
68 } | 77 } |
69 | 78 |
70 const int fake_fetcher_request_count() { | 79 const int fake_fetcher_request_count() { |
71 return fake_fetcher_request_count_; | 80 return fake_fetcher_request_count_; |
(...skipping 11 matching lines...) Expand all Loading... | |
83 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() | 92 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() |
84 : testing::Test() { | 93 : testing::Test() { |
85 } | 94 } |
86 | 95 |
87 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} | 96 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} |
88 | 97 |
89 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { | 98 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { |
90 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 99 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
91 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 100 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
92 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 101 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
102 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); | |
103 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
93 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); | 104 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth); |
94 } | 105 } |
95 | 106 |
96 // testing::Test implementation: | 107 // testing::Test implementation: |
97 void DataReductionProxySettingsTestBase::SetUp() { | 108 void DataReductionProxySettingsTestBase::SetUp() { |
98 PrefRegistrySimple* registry = pref_service_.registry(); | 109 PrefRegistrySimple* registry = pref_service_.registry(); |
99 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | 110 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); |
100 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | 111 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); |
101 registry->RegisterInt64Pref( | 112 registry->RegisterInt64Pref( |
102 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); | 113 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); |
(...skipping 24 matching lines...) Expand all Loading... | |
127 std::string server; | 138 std::string server; |
128 dict->GetString("mode", &mode); | 139 dict->GetString("mode", &mode); |
129 ASSERT_EQ(expected_mode, mode); | 140 ASSERT_EQ(expected_mode, mode); |
130 dict->GetString("server", &server); | 141 dict->GetString("server", &server); |
131 ASSERT_EQ(expected_servers, server); | 142 ASSERT_EQ(expected_servers, server); |
132 } | 143 } |
133 | 144 |
134 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 145 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
135 bool expected_enabled) { | 146 bool expected_enabled) { |
136 if (expected_enabled) { | 147 if (expected_enabled) { |
148 std::string main_proxy = kDataReductionProxyOrigin; | |
149 std::string fallback_proxy = kDataReductionProxyFallback; | |
137 std::string servers = | 150 std::string servers = |
138 "http=" + Settings()->GetDataReductionProxyOrigin() + ",direct://;"; | 151 "http=" + main_proxy + "," + fallback_proxy + ",direct://;"; |
139 CheckProxyPref(servers, | 152 CheckProxyPref(servers, |
140 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); | 153 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); |
141 } else { | 154 } else { |
142 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | 155 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); |
143 } | 156 } |
144 } | 157 } |
145 | 158 |
146 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, | 159 void DataReductionProxySettingsTestBase::CheckProbe(bool initially_enabled, |
147 const std::string& probe_url, | 160 const std::string& probe_url, |
148 const std::string& response, | 161 const std::string& response, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 scoped_ptr<TestDataReductionProxySettings> settings_; | 228 scoped_ptr<TestDataReductionProxySettings> settings_; |
216 }; | 229 }; |
217 | 230 |
218 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { | 231 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) { |
219 AddProxyToCommandLine(); | 232 AddProxyToCommandLine(); |
220 // SetUp() adds the origin to the command line, which should be returned here. | 233 // SetUp() adds the origin to the command line, which should be returned here. |
221 std::string result = settings_->GetDataReductionProxyOrigin(); | 234 std::string result = settings_->GetDataReductionProxyOrigin(); |
222 EXPECT_EQ(kDataReductionProxyOrigin, result); | 235 EXPECT_EQ(kDataReductionProxyOrigin, result); |
223 } | 236 } |
224 | 237 |
225 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyAuth) { | 238 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) { |
226 AddProxyToCommandLine(); | 239 DataReductionProxySettings::DataReductionProxyList proxies = |
227 // SetUp() adds the auth string to the command line, which should be returned | 240 settings_->GetDataReductionProxies(); |
228 // here. | 241 EXPECT_TRUE(proxies.empty()); |
229 std::string result = settings_->GetDataReductionProxyAuth(); | 242 |
230 EXPECT_EQ(kDataReductionProxyAuth, result); | 243 // Adding just the fallback on the command line shouldn't add a proxy. |
244 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
245 switches::kSpdyProxyAuthFallback, kDataReductionProxyFallback); | |
246 proxies = settings_->GetDataReductionProxies(); | |
247 dump_proxies(proxies); | |
248 EXPECT_EQ(0u, proxies.size()); | |
249 EXPECT_TRUE(proxies.empty()); | |
250 | |
251 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
252 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | |
253 proxies = settings_->GetDataReductionProxies(); | |
254 EXPECT_EQ(2u, proxies.size()); | |
255 | |
256 EXPECT_EQ("foo.com",proxies[0].host()); | |
257 EXPECT_EQ(443u,proxies[0].port()); | |
258 EXPECT_EQ("bar.com",proxies[1].host()); | |
259 EXPECT_EQ(80u,proxies[1].port()); | |
231 } | 260 } |
232 | 261 |
233 // Test that the auth value set by preprocessor directive is not returned | 262 TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) { |
263 AddProxyToCommandLine(); | |
264 std::string salt = "8675309"; // Jenny's number to test the hash generator. | |
bengr
2013/10/22 17:49:30
:)
marq (ping after 24h)
2013/10/22 21:18:01
Ack :-)
| |
265 std::string salted_key = salt + kDataReductionProxyAuth + salt; | |
266 base::string16 expected_hash = UTF8ToUTF16(base::MD5String(salted_key)); | |
267 EXPECT_EQ(expected_hash, settings_->AuthHashForSalt(8675309)); | |
268 } | |
269 | |
270 // 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 | 271 // when an origin is set via a switch. This test only does anything useful in |
235 // Chrome builds. | 272 // Chrome builds. |
236 TEST_F(DataReductionProxySettingsTest, | 273 TEST_F(DataReductionProxySettingsTest, |
237 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | 274 TestAuthHashGenerationWithOriginSetViaSwitch) { |
238 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 275 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
239 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | 276 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); |
240 std::string result = settings_->GetDataReductionProxyAuth(); | 277 EXPECT_EQ(base::string16(), settings_->AuthHashForSalt(8675309)); |
241 EXPECT_EQ("", result); | |
242 } | 278 } |
243 | 279 |
244 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { | 280 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { |
245 settings_->InitPrefMembers(); | 281 settings_->InitPrefMembers(); |
246 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); | 282 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled()); |
247 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 283 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
248 | 284 |
249 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | 285 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); |
250 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 286 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
251 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); | 287 EXPECT_FALSE(settings_->IsDataReductionProxyManaged()); |
252 | 288 |
253 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | 289 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, |
254 base::Value::CreateBooleanValue(true)); | 290 base::Value::CreateBooleanValue(true)); |
255 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); | 291 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled()); |
256 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); | 292 EXPECT_TRUE(settings_->IsDataReductionProxyManaged()); |
257 } | 293 } |
258 | 294 |
295 TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) { | |
296 AddProxyToCommandLine(); | |
297 typedef struct { | |
298 std::string host; | |
299 std::string realm; | |
300 bool expected_to_succeed; | |
301 } challenge_test; | |
302 | |
303 challenge_test tests[] = { | |
304 {"foo.com:443", "", false}, // 0. No realm. | |
305 {"foo.com:443", "xxx", false}, // 1. Wrong realm. | |
306 {"foo.com:443", "spdyproxy", false}, // 2. Case matters. | |
307 {"foo.com:443", "SpdyProxy", true}, // 3. OK. | |
308 {"foo.com:443", "SpdyProxy1234567", true}, // 4. OK | |
309 {"bar.com:80", "SpdyProxy1234567", true}, // 5. OK. | |
310 {"foo.com:443", "SpdyProxyxxx", true}, // 6. OK | |
311 {"", "SpdyProxy1234567", false}, // 7. No challenger. | |
bengr
2013/10/22 17:49:30
align
marq (ping after 24h)
2013/10/22 21:18:01
Done.
| |
312 {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host. | |
313 {"foo.com", "SpdyProxy1234567", false}, // 9. No port. | |
314 {"foo.com:80", "SpdyProxy1234567", false}, // 10.Wrong port. | |
315 {"bar.com:81", "SpdyProxy1234567", false}, // 11.Wrong port. | |
316 }; | |
317 | |
318 for (int i = 0; i <= 11; i++) { | |
319 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); | |
320 auth_info->challenger = net::HostPortPair::FromString(tests[i].host); | |
321 auth_info->realm = tests[i].realm; | |
322 EXPECT_EQ(tests[i].expected_to_succeed, | |
323 settings_->IsAcceptableAuthChallenge(auth_info.get())); | |
324 } | |
325 } | |
326 | |
327 // More tests: | |
bengr
2013/10/22 17:49:30
Why is this comment here?
marq (ping after 24h)
2013/10/22 21:18:01
To remind me to write more tests. It's gone now an
| |
328 // -> test auth cache is populated and responds to challenges | |
329 // -> test GetTokenForAuthChellenge | |
330 | |
331 | |
259 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { | 332 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { |
260 int64 original_content_length; | 333 int64 original_content_length; |
261 int64 received_content_length; | 334 int64 received_content_length; |
262 int64 last_update_time; | 335 int64 last_update_time; |
263 settings_->ResetDataReductionStatistics(); | 336 settings_->ResetDataReductionStatistics(); |
264 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, | 337 settings_->GetContentLengths(spdyproxy::kNumDaysInHistory, |
265 &original_content_length, | 338 &original_content_length, |
266 &received_content_length, | 339 &received_content_length, |
267 &last_update_time); | 340 &last_update_time); |
268 EXPECT_EQ(0L, original_content_length); | 341 EXPECT_EQ(0L, original_content_length); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 }; | 483 }; |
411 | 484 |
412 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); | 485 ASSERT_EQ(settings_->bypass_rules_.size(), 6u); |
413 int i = 0; | 486 int i = 0; |
414 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | 487 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); |
415 it != settings_->bypass_rules_.end(); ++it) { | 488 it != settings_->bypass_rules_.end(); ++it) { |
416 EXPECT_EQ(expected[i++], *it); | 489 EXPECT_EQ(expected[i++], *it); |
417 } | 490 } |
418 } | 491 } |
419 | 492 |
493 TEST_F(DataReductionProxySettingsTest, TestAcceptableAuthTokens) { | |
494 AddProxyToCommandLine(); | |
bengr
2013/10/22 17:49:30
?
marq (ping after 24h)
2013/10/22 21:18:01
Now gone.
| |
495 } | |
496 | |
OLD | NEW |