OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/pref_proxy_config_service.h" | 5 #include "chrome/browser/net/pref_proxy_config_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "chrome/browser/net/chrome_url_request_context.h" | 9 #include "chrome/browser/net/chrome_url_request_context.h" |
10 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 10 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
11 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 11 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/testing_pref_service.h" | 14 #include "chrome/test/testing_pref_service.h" |
15 #include "net/proxy/proxy_config_service_common_unittest.h" | 15 #include "net/proxy/proxy_config_service_common_unittest.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using testing::_; | 19 using testing::_; |
20 using testing::Mock; | 20 using testing::Mock; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url"; | 24 const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url"; |
25 | 25 |
26 // Testing proxy config service that allows us to fire notifications at will. | 26 // Testing proxy config service that allows us to fire notifications at will. |
27 class TestProxyConfigService : public net::ProxyConfigService { | 27 class TestProxyConfigService : public net::ProxyConfigService { |
28 public: | 28 public: |
29 explicit TestProxyConfigService(const net::ProxyConfig& config) | 29 TestProxyConfigService(const net::ProxyConfig& config, |
30 : config_(config) { | 30 ConfigAvailability availability) |
31 } | 31 : config_(config), |
| 32 availability_(availability) {} |
32 | 33 |
33 void SetProxyConfig(const net::ProxyConfig config) { | 34 void SetProxyConfig(const net::ProxyConfig config, |
| 35 ConfigAvailability availability) { |
34 config_ = config; | 36 config_ = config; |
| 37 availability_ = availability; |
35 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, | 38 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, |
36 OnProxyConfigChanged(config_)); | 39 OnProxyConfigChanged(config, availability)); |
37 } | 40 } |
38 | 41 |
39 private: | 42 private: |
40 virtual void AddObserver(net::ProxyConfigService::Observer* observer) { | 43 virtual void AddObserver(net::ProxyConfigService::Observer* observer) { |
41 observers_.AddObserver(observer); | 44 observers_.AddObserver(observer); |
42 } | 45 } |
43 | 46 |
44 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) { | 47 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) { |
45 observers_.RemoveObserver(observer); | 48 observers_.RemoveObserver(observer); |
46 } | 49 } |
47 | 50 |
48 virtual bool GetLatestProxyConfig(net::ProxyConfig* config) { | 51 virtual net::ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 52 net::ProxyConfig* config) { |
49 *config = config_; | 53 *config = config_; |
50 return true; | 54 return availability_; |
51 } | 55 } |
52 | 56 |
53 net::ProxyConfig config_; | 57 net::ProxyConfig config_; |
| 58 ConfigAvailability availability_; |
54 ObserverList<net::ProxyConfigService::Observer, true> observers_; | 59 ObserverList<net::ProxyConfigService::Observer, true> observers_; |
55 }; | 60 }; |
56 | 61 |
57 // A mock observer for capturing callbacks. | 62 // A mock observer for capturing callbacks. |
58 class MockObserver : public net::ProxyConfigService::Observer { | 63 class MockObserver : public net::ProxyConfigService::Observer { |
59 public: | 64 public: |
60 MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&)); | 65 MOCK_METHOD2(OnProxyConfigChanged, |
| 66 void(const net::ProxyConfig&, |
| 67 net::ProxyConfigService::ConfigAvailability)); |
61 }; | 68 }; |
62 | 69 |
63 template<typename TESTBASE> | 70 template<typename TESTBASE> |
64 class PrefProxyConfigServiceTestBase : public TESTBASE { | 71 class PrefProxyConfigServiceTestBase : public TESTBASE { |
65 protected: | 72 protected: |
66 PrefProxyConfigServiceTestBase() | 73 PrefProxyConfigServiceTestBase() |
67 : ui_thread_(BrowserThread::UI, &loop_), | 74 : ui_thread_(BrowserThread::UI, &loop_), |
68 io_thread_(BrowserThread::IO, &loop_) {} | 75 io_thread_(BrowserThread::IO, &loop_) {} |
69 | 76 |
70 virtual void Init(PrefService* pref_service) { | 77 virtual void Init(PrefService* pref_service) { |
71 ASSERT_TRUE(pref_service); | 78 ASSERT_TRUE(pref_service); |
72 PrefProxyConfigService::RegisterPrefs(pref_service); | 79 PrefProxyConfigService::RegisterPrefs(pref_service); |
73 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); | 80 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); |
74 delegate_service_ = new TestProxyConfigService(fixed_config_); | 81 delegate_service_ = |
| 82 new TestProxyConfigService(fixed_config_, |
| 83 net::ProxyConfigService::CONFIG_VALID); |
75 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service); | 84 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service); |
76 proxy_config_service_.reset( | 85 proxy_config_service_.reset( |
77 new PrefProxyConfigService(proxy_config_tracker_.get(), | 86 new PrefProxyConfigService(proxy_config_tracker_.get(), |
78 delegate_service_)); | 87 delegate_service_)); |
79 } | 88 } |
80 | 89 |
81 virtual void TearDown() { | 90 virtual void TearDown() { |
82 proxy_config_tracker_->DetachFromPrefService(); | 91 proxy_config_tracker_->DetachFromPrefService(); |
83 loop_.RunAllPending(); | 92 loop_.RunAllPending(); |
84 proxy_config_service_.reset(); | 93 proxy_config_service_.reset(); |
(...skipping 16 matching lines...) Expand all Loading... |
101 virtual void SetUp() { | 110 virtual void SetUp() { |
102 pref_service_.reset(new TestingPrefService()); | 111 pref_service_.reset(new TestingPrefService()); |
103 Init(pref_service_.get()); | 112 Init(pref_service_.get()); |
104 } | 113 } |
105 | 114 |
106 scoped_ptr<TestingPrefService> pref_service_; | 115 scoped_ptr<TestingPrefService> pref_service_; |
107 }; | 116 }; |
108 | 117 |
109 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { | 118 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { |
110 net::ProxyConfig actual_config; | 119 net::ProxyConfig actual_config; |
111 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 120 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 121 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
112 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 122 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
113 } | 123 } |
114 | 124 |
115 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { | 125 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { |
116 pref_service_->SetManagedPref( | 126 pref_service_->SetManagedPref( |
117 prefs::kProxy, | 127 prefs::kProxy, |
118 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); | 128 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
119 loop_.RunAllPending(); | 129 loop_.RunAllPending(); |
120 | 130 |
121 net::ProxyConfig actual_config; | 131 net::ProxyConfig actual_config; |
122 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 132 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 133 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
123 EXPECT_FALSE(actual_config.auto_detect()); | 134 EXPECT_FALSE(actual_config.auto_detect()); |
124 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 135 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
125 actual_config.proxy_rules().type); | 136 actual_config.proxy_rules().type); |
126 EXPECT_EQ(actual_config.proxy_rules().single_proxy, | 137 EXPECT_EQ(actual_config.proxy_rules().single_proxy, |
127 net::ProxyServer::FromURI("http://example.com:3128", | 138 net::ProxyServer::FromURI("http://example.com:3128", |
128 net::ProxyServer::SCHEME_HTTP)); | 139 net::ProxyServer::SCHEME_HTTP)); |
129 | 140 |
130 pref_service_->SetManagedPref(prefs::kProxy, | 141 pref_service_->SetManagedPref(prefs::kProxy, |
131 ProxyConfigDictionary::CreateAutoDetect()); | 142 ProxyConfigDictionary::CreateAutoDetect()); |
132 loop_.RunAllPending(); | 143 loop_.RunAllPending(); |
133 | 144 |
134 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 145 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 146 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
135 EXPECT_TRUE(actual_config.auto_detect()); | 147 EXPECT_TRUE(actual_config.auto_detect()); |
136 } | 148 } |
137 | 149 |
138 // Compares proxy configurations, but allows different identifiers. | 150 // Compares proxy configurations, but allows different identifiers. |
139 MATCHER_P(ProxyConfigMatches, config, "") { | 151 MATCHER_P(ProxyConfigMatches, config, "") { |
140 net::ProxyConfig reference(config); | 152 net::ProxyConfig reference(config); |
141 reference.set_id(arg.id()); | 153 reference.set_id(arg.id()); |
142 return reference.Equals(arg); | 154 return reference.Equals(arg); |
143 } | 155 } |
144 | 156 |
145 TEST_F(PrefProxyConfigServiceTest, Observers) { | 157 TEST_F(PrefProxyConfigServiceTest, Observers) { |
| 158 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 159 net::ProxyConfigService::CONFIG_VALID; |
146 MockObserver observer; | 160 MockObserver observer; |
147 proxy_config_service_->AddObserver(&observer); | 161 proxy_config_service_->AddObserver(&observer); |
148 | 162 |
149 // Firing the observers in the delegate should trigger a notification. | 163 // Firing the observers in the delegate should trigger a notification. |
150 net::ProxyConfig config2; | 164 net::ProxyConfig config2; |
151 config2.set_auto_detect(true); | 165 config2.set_auto_detect(true); |
152 EXPECT_CALL(observer, | 166 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), |
153 OnProxyConfigChanged(ProxyConfigMatches(config2))).Times(1); | 167 CONFIG_VALID)).Times(1); |
154 delegate_service_->SetProxyConfig(config2); | 168 delegate_service_->SetProxyConfig(config2, CONFIG_VALID); |
155 loop_.RunAllPending(); | 169 loop_.RunAllPending(); |
156 Mock::VerifyAndClearExpectations(&observer); | 170 Mock::VerifyAndClearExpectations(&observer); |
157 | 171 |
158 // Override configuration, this should trigger a notification. | 172 // Override configuration, this should trigger a notification. |
159 net::ProxyConfig pref_config; | 173 net::ProxyConfig pref_config; |
160 pref_config.set_pac_url(GURL(kFixedPacUrl)); | 174 pref_config.set_pac_url(GURL(kFixedPacUrl)); |
161 | 175 |
162 EXPECT_CALL(observer, | 176 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config), |
163 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1); | 177 CONFIG_VALID)).Times(1); |
164 pref_service_->SetManagedPref( | 178 pref_service_->SetManagedPref( |
165 prefs::kProxy, | 179 prefs::kProxy, |
166 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); | 180 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); |
167 loop_.RunAllPending(); | 181 loop_.RunAllPending(); |
168 Mock::VerifyAndClearExpectations(&observer); | 182 Mock::VerifyAndClearExpectations(&observer); |
169 | 183 |
170 // Since there are pref overrides, delegate changes should be ignored. | 184 // Since there are pref overrides, delegate changes should be ignored. |
171 net::ProxyConfig config3; | 185 net::ProxyConfig config3; |
172 config3.proxy_rules().ParseFromString("http=config3:80"); | 186 config3.proxy_rules().ParseFromString("http=config3:80"); |
173 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0); | 187 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0); |
174 fixed_config_.set_auto_detect(true); | 188 fixed_config_.set_auto_detect(true); |
175 delegate_service_->SetProxyConfig(config3); | 189 delegate_service_->SetProxyConfig(config3, CONFIG_VALID); |
176 loop_.RunAllPending(); | 190 loop_.RunAllPending(); |
177 Mock::VerifyAndClearExpectations(&observer); | 191 Mock::VerifyAndClearExpectations(&observer); |
178 | 192 |
179 // Clear the override should switch back to the fixed configuration. | 193 // Clear the override should switch back to the fixed configuration. |
180 EXPECT_CALL(observer, | 194 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3), |
181 OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1); | 195 CONFIG_VALID)).Times(1); |
182 pref_service_->RemoveManagedPref(prefs::kProxy); | 196 pref_service_->RemoveManagedPref(prefs::kProxy); |
183 loop_.RunAllPending(); | 197 loop_.RunAllPending(); |
184 Mock::VerifyAndClearExpectations(&observer); | 198 Mock::VerifyAndClearExpectations(&observer); |
185 | 199 |
186 // Delegate service notifications should show up again. | 200 // Delegate service notifications should show up again. |
187 net::ProxyConfig config4; | 201 net::ProxyConfig config4; |
188 config4.proxy_rules().ParseFromString("socks:config4"); | 202 config4.proxy_rules().ParseFromString("socks:config4"); |
189 EXPECT_CALL(observer, | 203 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), |
190 OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1); | 204 CONFIG_VALID)).Times(1); |
191 delegate_service_->SetProxyConfig(config4); | 205 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); |
192 loop_.RunAllPending(); | 206 loop_.RunAllPending(); |
193 Mock::VerifyAndClearExpectations(&observer); | 207 Mock::VerifyAndClearExpectations(&observer); |
194 | 208 |
195 proxy_config_service_->RemoveObserver(&observer); | 209 proxy_config_service_->RemoveObserver(&observer); |
196 } | 210 } |
| 211 |
| 212 TEST_F(PrefProxyConfigServiceTest, Fallback) { |
| 213 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 214 net::ProxyConfigService::CONFIG_VALID; |
| 215 MockObserver observer; |
| 216 net::ProxyConfig actual_config; |
| 217 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), |
| 218 net::ProxyConfigService::CONFIG_UNSET); |
| 219 proxy_config_service_->AddObserver(&observer); |
| 220 |
| 221 // Prepare test data. |
| 222 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); |
| 223 net::ProxyConfig user_config = |
| 224 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl)); |
| 225 |
| 226 // Set a recommended pref. |
| 227 EXPECT_CALL(observer, |
| 228 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), |
| 229 CONFIG_VALID)).Times(1); |
| 230 pref_service_->SetRecommendedPref( |
| 231 prefs::kProxy, |
| 232 ProxyConfigDictionary::CreateAutoDetect()); |
| 233 loop_.RunAllPending(); |
| 234 Mock::VerifyAndClearExpectations(&observer); |
| 235 EXPECT_EQ(CONFIG_VALID, |
| 236 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 237 EXPECT_THAT(actual_config, ProxyConfigMatches(recommended_config)); |
| 238 |
| 239 // Override in user prefs. |
| 240 EXPECT_CALL(observer, |
| 241 OnProxyConfigChanged(ProxyConfigMatches(user_config), |
| 242 CONFIG_VALID)).Times(1); |
| 243 pref_service_->SetManagedPref( |
| 244 prefs::kProxy, |
| 245 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); |
| 246 loop_.RunAllPending(); |
| 247 Mock::VerifyAndClearExpectations(&observer); |
| 248 EXPECT_EQ(CONFIG_VALID, |
| 249 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 250 EXPECT_THAT(actual_config, ProxyConfigMatches(user_config)); |
| 251 |
| 252 // Go back to recommended pref. |
| 253 EXPECT_CALL(observer, |
| 254 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), |
| 255 CONFIG_VALID)).Times(1); |
| 256 pref_service_->RemoveManagedPref(prefs::kProxy); |
| 257 loop_.RunAllPending(); |
| 258 Mock::VerifyAndClearExpectations(&observer); |
| 259 EXPECT_EQ(CONFIG_VALID, |
| 260 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 261 EXPECT_THAT(actual_config, ProxyConfigMatches(recommended_config)); |
| 262 |
| 263 proxy_config_service_->RemoveObserver(&observer); |
| 264 } |
197 | 265 |
198 // Test parameter object for testing command line proxy configuration. | 266 // Test parameter object for testing command line proxy configuration. |
199 struct CommandLineTestParams { | 267 struct CommandLineTestParams { |
200 // Explicit assignment operator, so testing::TestWithParam works with MSVC. | 268 // Explicit assignment operator, so testing::TestWithParam works with MSVC. |
201 CommandLineTestParams& operator=(const CommandLineTestParams& other) { | 269 CommandLineTestParams& operator=(const CommandLineTestParams& other) { |
202 description = other.description; | 270 description = other.description; |
203 for (unsigned int i = 0; i < arraysize(switches); i++) | 271 for (unsigned int i = 0; i < arraysize(switches); i++) |
204 switches[i] = other.switches[i]; | 272 switches[i] = other.switches[i]; |
205 is_null = other.is_null; | 273 is_null = other.is_null; |
206 auto_detect = other.auto_detect; | 274 auto_detect = other.auto_detect; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 Init(pref_service_.get()); | 318 Init(pref_service_.get()); |
251 } | 319 } |
252 | 320 |
253 private: | 321 private: |
254 CommandLine command_line_; | 322 CommandLine command_line_; |
255 scoped_ptr<PrefService> pref_service_; | 323 scoped_ptr<PrefService> pref_service_; |
256 }; | 324 }; |
257 | 325 |
258 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { | 326 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { |
259 net::ProxyConfig config; | 327 net::ProxyConfig config; |
260 proxy_config_service_->GetLatestProxyConfig(&config); | 328 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 329 proxy_config_service_->GetLatestProxyConfig(&config)); |
261 | 330 |
262 if (GetParam().is_null) { | 331 if (GetParam().is_null) { |
263 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); | 332 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); |
264 } else { | 333 } else { |
265 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); | 334 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); |
266 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); | 335 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); |
267 EXPECT_EQ(GetParam().pac_url, config.pac_url()); | 336 EXPECT_EQ(GetParam().pac_url, config.pac_url()); |
268 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules())); | 337 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules())); |
269 } | 338 } |
270 } | 339 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 net::ProxyRulesExpectation::Empty(), | 447 net::ProxyRulesExpectation::Empty(), |
379 }, | 448 }, |
380 }; | 449 }; |
381 | 450 |
382 INSTANTIATE_TEST_CASE_P( | 451 INSTANTIATE_TEST_CASE_P( |
383 PrefProxyConfigServiceCommandLineTestInstance, | 452 PrefProxyConfigServiceCommandLineTestInstance, |
384 PrefProxyConfigServiceCommandLineTest, | 453 PrefProxyConfigServiceCommandLineTest, |
385 testing::ValuesIn(kCommandLineTestParams)); | 454 testing::ValuesIn(kCommandLineTestParams)); |
386 | 455 |
387 } // namespace | 456 } // namespace |
OLD | NEW |