| 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 "components/proxy_config/pref_proxy_config_tracker_impl.h" | 5 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/prefs/pref_registry_simple.h" | 14 #include "components/prefs/pref_registry_simple.h" |
| 14 #include "components/prefs/testing_pref_service.h" | 15 #include "components/prefs/testing_pref_service.h" |
| 15 #include "components/proxy_config/proxy_config_dictionary.h" | 16 #include "components/proxy_config/proxy_config_dictionary.h" |
| 16 #include "components/proxy_config/proxy_config_pref_names.h" | 17 #include "components/proxy_config/proxy_config_pref_names.h" |
| 17 #include "net/proxy/proxy_info.h" | 18 #include "net/proxy/proxy_info.h" |
| 18 #include "net/proxy/proxy_list.h" | 19 #include "net/proxy/proxy_list.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 new TestProxyConfigService(fixed_config_, | 82 new TestProxyConfigService(fixed_config_, |
| 82 net::ProxyConfigService::CONFIG_VALID); | 83 net::ProxyConfigService::CONFIG_VALID); |
| 83 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( | 84 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( |
| 84 pref_service_.get(), base::ThreadTaskRunnerHandle::Get())); | 85 pref_service_.get(), base::ThreadTaskRunnerHandle::Get())); |
| 85 proxy_config_service_ = | 86 proxy_config_service_ = |
| 86 proxy_config_tracker_->CreateTrackingProxyConfigService( | 87 proxy_config_tracker_->CreateTrackingProxyConfigService( |
| 87 std::unique_ptr<net::ProxyConfigService>(delegate_service_)); | 88 std::unique_ptr<net::ProxyConfigService>(delegate_service_)); |
| 88 // SetProxyConfigServiceImpl triggers update of initial prefs proxy | 89 // SetProxyConfigServiceImpl triggers update of initial prefs proxy |
| 89 // config by tracker to chrome proxy config service, so flush all pending | 90 // config by tracker to chrome proxy config service, so flush all pending |
| 90 // tasks so that tests start fresh. | 91 // tasks so that tests start fresh. |
| 91 loop_.RunUntilIdle(); | 92 base::RunLoop().RunUntilIdle(); |
| 92 } | 93 } |
| 93 | 94 |
| 94 ~PrefProxyConfigTrackerImplTest() override { | 95 ~PrefProxyConfigTrackerImplTest() override { |
| 95 proxy_config_tracker_->DetachFromPrefService(); | 96 proxy_config_tracker_->DetachFromPrefService(); |
| 96 loop_.RunUntilIdle(); | 97 base::RunLoop().RunUntilIdle(); |
| 97 proxy_config_tracker_.reset(); | 98 proxy_config_tracker_.reset(); |
| 98 proxy_config_service_.reset(); | 99 proxy_config_service_.reset(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 base::MessageLoop loop_; | 102 base::MessageLoop loop_; |
| 102 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 103 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 103 TestProxyConfigService* delegate_service_; // weak | 104 TestProxyConfigService* delegate_service_; // weak |
| 104 std::unique_ptr<net::ProxyConfigService> proxy_config_service_; | 105 std::unique_ptr<net::ProxyConfigService> proxy_config_service_; |
| 105 net::ProxyConfig fixed_config_; | 106 net::ProxyConfig fixed_config_; |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; | 109 std::unique_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { | 112 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { |
| 112 net::ProxyConfig actual_config; | 113 net::ProxyConfig actual_config; |
| 113 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 114 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 114 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 115 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 115 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 116 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
| 116 } | 117 } |
| 117 | 118 |
| 118 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) { | 119 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) { |
| 119 pref_service_->SetManagedPref(proxy_config::prefs::kProxy, | 120 pref_service_->SetManagedPref(proxy_config::prefs::kProxy, |
| 120 ProxyConfigDictionary::CreateFixedServers( | 121 ProxyConfigDictionary::CreateFixedServers( |
| 121 "http://example.com:3128", std::string())); | 122 "http://example.com:3128", std::string())); |
| 122 loop_.RunUntilIdle(); | 123 base::RunLoop().RunUntilIdle(); |
| 123 | 124 |
| 124 net::ProxyConfig actual_config; | 125 net::ProxyConfig actual_config; |
| 125 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 126 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 126 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 127 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 127 EXPECT_FALSE(actual_config.auto_detect()); | 128 EXPECT_FALSE(actual_config.auto_detect()); |
| 128 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 129 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 129 actual_config.proxy_rules().type); | 130 actual_config.proxy_rules().type); |
| 130 EXPECT_EQ(actual_config.proxy_rules().single_proxies.Get(), | 131 EXPECT_EQ(actual_config.proxy_rules().single_proxies.Get(), |
| 131 net::ProxyServer::FromURI("http://example.com:3128", | 132 net::ProxyServer::FromURI("http://example.com:3128", |
| 132 net::ProxyServer::SCHEME_HTTP)); | 133 net::ProxyServer::SCHEME_HTTP)); |
| 133 | 134 |
| 134 pref_service_->SetManagedPref(proxy_config::prefs::kProxy, | 135 pref_service_->SetManagedPref(proxy_config::prefs::kProxy, |
| 135 ProxyConfigDictionary::CreateAutoDetect()); | 136 ProxyConfigDictionary::CreateAutoDetect()); |
| 136 loop_.RunUntilIdle(); | 137 base::RunLoop().RunUntilIdle(); |
| 137 | 138 |
| 138 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 139 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 139 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 140 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 140 EXPECT_TRUE(actual_config.auto_detect()); | 141 EXPECT_TRUE(actual_config.auto_detect()); |
| 141 } | 142 } |
| 142 | 143 |
| 143 // Compares proxy configurations, but allows different identifiers. | 144 // Compares proxy configurations, but allows different identifiers. |
| 144 MATCHER_P(ProxyConfigMatches, config, "") { | 145 MATCHER_P(ProxyConfigMatches, config, "") { |
| 145 net::ProxyConfig reference(config); | 146 net::ProxyConfig reference(config); |
| 146 reference.set_id(arg.id()); | 147 reference.set_id(arg.id()); |
| 147 return reference.Equals(arg); | 148 return reference.Equals(arg); |
| 148 } | 149 } |
| 149 | 150 |
| 150 TEST_F(PrefProxyConfigTrackerImplTest, Observers) { | 151 TEST_F(PrefProxyConfigTrackerImplTest, Observers) { |
| 151 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 152 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 152 net::ProxyConfigService::CONFIG_VALID; | 153 net::ProxyConfigService::CONFIG_VALID; |
| 153 MockObserver observer; | 154 MockObserver observer; |
| 154 proxy_config_service_->AddObserver(&observer); | 155 proxy_config_service_->AddObserver(&observer); |
| 155 | 156 |
| 156 // Firing the observers in the delegate should trigger a notification. | 157 // Firing the observers in the delegate should trigger a notification. |
| 157 net::ProxyConfig config2; | 158 net::ProxyConfig config2; |
| 158 config2.set_auto_detect(true); | 159 config2.set_auto_detect(true); |
| 159 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), | 160 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), |
| 160 CONFIG_VALID)).Times(1); | 161 CONFIG_VALID)).Times(1); |
| 161 delegate_service_->SetProxyConfig(config2, CONFIG_VALID); | 162 delegate_service_->SetProxyConfig(config2, CONFIG_VALID); |
| 162 loop_.RunUntilIdle(); | 163 base::RunLoop().RunUntilIdle(); |
| 163 Mock::VerifyAndClearExpectations(&observer); | 164 Mock::VerifyAndClearExpectations(&observer); |
| 164 | 165 |
| 165 // Override configuration, this should trigger a notification. | 166 // Override configuration, this should trigger a notification. |
| 166 net::ProxyConfig pref_config; | 167 net::ProxyConfig pref_config; |
| 167 pref_config.set_pac_url(GURL(kFixedPacUrl)); | 168 pref_config.set_pac_url(GURL(kFixedPacUrl)); |
| 168 | 169 |
| 169 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config), | 170 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config), |
| 170 CONFIG_VALID)).Times(1); | 171 CONFIG_VALID)).Times(1); |
| 171 pref_service_->SetManagedPref( | 172 pref_service_->SetManagedPref( |
| 172 proxy_config::prefs::kProxy, | 173 proxy_config::prefs::kProxy, |
| 173 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); | 174 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); |
| 174 loop_.RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 175 Mock::VerifyAndClearExpectations(&observer); | 176 Mock::VerifyAndClearExpectations(&observer); |
| 176 | 177 |
| 177 // Since there are pref overrides, delegate changes should be ignored. | 178 // Since there are pref overrides, delegate changes should be ignored. |
| 178 net::ProxyConfig config3; | 179 net::ProxyConfig config3; |
| 179 config3.proxy_rules().ParseFromString("http=config3:80"); | 180 config3.proxy_rules().ParseFromString("http=config3:80"); |
| 180 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0); | 181 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0); |
| 181 fixed_config_.set_auto_detect(true); | 182 fixed_config_.set_auto_detect(true); |
| 182 delegate_service_->SetProxyConfig(config3, CONFIG_VALID); | 183 delegate_service_->SetProxyConfig(config3, CONFIG_VALID); |
| 183 loop_.RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
| 184 Mock::VerifyAndClearExpectations(&observer); | 185 Mock::VerifyAndClearExpectations(&observer); |
| 185 | 186 |
| 186 // Clear the override should switch back to the fixed configuration. | 187 // Clear the override should switch back to the fixed configuration. |
| 187 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3), | 188 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3), |
| 188 CONFIG_VALID)).Times(1); | 189 CONFIG_VALID)).Times(1); |
| 189 pref_service_->RemoveManagedPref(proxy_config::prefs::kProxy); | 190 pref_service_->RemoveManagedPref(proxy_config::prefs::kProxy); |
| 190 loop_.RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 191 Mock::VerifyAndClearExpectations(&observer); | 192 Mock::VerifyAndClearExpectations(&observer); |
| 192 | 193 |
| 193 // Delegate service notifications should show up again. | 194 // Delegate service notifications should show up again. |
| 194 net::ProxyConfig config4; | 195 net::ProxyConfig config4; |
| 195 config4.proxy_rules().ParseFromString("socks:config4"); | 196 config4.proxy_rules().ParseFromString("socks:config4"); |
| 196 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), | 197 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), |
| 197 CONFIG_VALID)).Times(1); | 198 CONFIG_VALID)).Times(1); |
| 198 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); | 199 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); |
| 199 loop_.RunUntilIdle(); | 200 base::RunLoop().RunUntilIdle(); |
| 200 Mock::VerifyAndClearExpectations(&observer); | 201 Mock::VerifyAndClearExpectations(&observer); |
| 201 | 202 |
| 202 proxy_config_service_->RemoveObserver(&observer); | 203 proxy_config_service_->RemoveObserver(&observer); |
| 203 } | 204 } |
| 204 | 205 |
| 205 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { | 206 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { |
| 206 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = | 207 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = |
| 207 net::ProxyConfigService::CONFIG_VALID; | 208 net::ProxyConfigService::CONFIG_VALID; |
| 208 MockObserver observer; | 209 MockObserver observer; |
| 209 net::ProxyConfig actual_config; | 210 net::ProxyConfig actual_config; |
| 210 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), | 211 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), |
| 211 net::ProxyConfigService::CONFIG_UNSET); | 212 net::ProxyConfigService::CONFIG_UNSET); |
| 212 proxy_config_service_->AddObserver(&observer); | 213 proxy_config_service_->AddObserver(&observer); |
| 213 | 214 |
| 214 // Prepare test data. | 215 // Prepare test data. |
| 215 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); | 216 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); |
| 216 net::ProxyConfig user_config = | 217 net::ProxyConfig user_config = |
| 217 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl)); | 218 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl)); |
| 218 | 219 |
| 219 // Set a recommended pref. | 220 // Set a recommended pref. |
| 220 EXPECT_CALL(observer, | 221 EXPECT_CALL(observer, |
| 221 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), | 222 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), |
| 222 CONFIG_VALID)).Times(1); | 223 CONFIG_VALID)).Times(1); |
| 223 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, | 224 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, |
| 224 ProxyConfigDictionary::CreateAutoDetect()); | 225 ProxyConfigDictionary::CreateAutoDetect()); |
| 225 loop_.RunUntilIdle(); | 226 base::RunLoop().RunUntilIdle(); |
| 226 Mock::VerifyAndClearExpectations(&observer); | 227 Mock::VerifyAndClearExpectations(&observer); |
| 227 EXPECT_EQ(CONFIG_VALID, | 228 EXPECT_EQ(CONFIG_VALID, |
| 228 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 229 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 229 EXPECT_TRUE(actual_config.Equals(recommended_config)); | 230 EXPECT_TRUE(actual_config.Equals(recommended_config)); |
| 230 | 231 |
| 231 // Override in user prefs. | 232 // Override in user prefs. |
| 232 EXPECT_CALL(observer, | 233 EXPECT_CALL(observer, |
| 233 OnProxyConfigChanged(ProxyConfigMatches(user_config), | 234 OnProxyConfigChanged(ProxyConfigMatches(user_config), |
| 234 CONFIG_VALID)).Times(1); | 235 CONFIG_VALID)).Times(1); |
| 235 pref_service_->SetManagedPref( | 236 pref_service_->SetManagedPref( |
| 236 proxy_config::prefs::kProxy, | 237 proxy_config::prefs::kProxy, |
| 237 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); | 238 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); |
| 238 loop_.RunUntilIdle(); | 239 base::RunLoop().RunUntilIdle(); |
| 239 Mock::VerifyAndClearExpectations(&observer); | 240 Mock::VerifyAndClearExpectations(&observer); |
| 240 EXPECT_EQ(CONFIG_VALID, | 241 EXPECT_EQ(CONFIG_VALID, |
| 241 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 242 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 242 EXPECT_TRUE(actual_config.Equals(user_config)); | 243 EXPECT_TRUE(actual_config.Equals(user_config)); |
| 243 | 244 |
| 244 // Go back to recommended pref. | 245 // Go back to recommended pref. |
| 245 EXPECT_CALL(observer, | 246 EXPECT_CALL(observer, |
| 246 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), | 247 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), |
| 247 CONFIG_VALID)).Times(1); | 248 CONFIG_VALID)).Times(1); |
| 248 pref_service_->RemoveManagedPref(proxy_config::prefs::kProxy); | 249 pref_service_->RemoveManagedPref(proxy_config::prefs::kProxy); |
| 249 loop_.RunUntilIdle(); | 250 base::RunLoop().RunUntilIdle(); |
| 250 Mock::VerifyAndClearExpectations(&observer); | 251 Mock::VerifyAndClearExpectations(&observer); |
| 251 EXPECT_EQ(CONFIG_VALID, | 252 EXPECT_EQ(CONFIG_VALID, |
| 252 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 253 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 253 EXPECT_TRUE(actual_config.Equals(recommended_config)); | 254 EXPECT_TRUE(actual_config.Equals(recommended_config)); |
| 254 | 255 |
| 255 proxy_config_service_->RemoveObserver(&observer); | 256 proxy_config_service_->RemoveObserver(&observer); |
| 256 } | 257 } |
| 257 | 258 |
| 258 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { | 259 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { |
| 259 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, | 260 pref_service_->SetRecommendedPref(proxy_config::prefs::kProxy, |
| 260 ProxyConfigDictionary::CreateAutoDetect()); | 261 ProxyConfigDictionary::CreateAutoDetect()); |
| 261 pref_service_->SetUserPref(proxy_config::prefs::kProxy, | 262 pref_service_->SetUserPref(proxy_config::prefs::kProxy, |
| 262 ProxyConfigDictionary::CreateSystem()); | 263 ProxyConfigDictionary::CreateSystem()); |
| 263 loop_.RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 264 | 265 |
| 265 // Test if we actually use the system setting, which is |kFixedPacUrl|. | 266 // Test if we actually use the system setting, which is |kFixedPacUrl|. |
| 266 net::ProxyConfig actual_config; | 267 net::ProxyConfig actual_config; |
| 267 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, | 268 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, |
| 268 proxy_config_service_->GetLatestProxyConfig(&actual_config)); | 269 proxy_config_service_->GetLatestProxyConfig(&actual_config)); |
| 269 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 270 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
| 270 } | 271 } |
| 271 | 272 |
| 272 } // namespace | 273 } // namespace |
| OLD | NEW |