OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/callback.h" | 5 #include "base/callback.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "chrome/browser/policy/external_data_fetcher.h" | 9 #include "chrome/browser/policy/external_data_fetcher.h" |
10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
11 #include "chrome/browser/policy/policy_map.h" | 11 #include "chrome/browser/policy/policy_map.h" |
12 #include "chrome/browser/policy/policy_service_impl.h" | 12 #include "chrome/browser/policy/policy_service_impl.h" |
13 #include "chrome/browser/prefs/browser_prefs.h" | 13 #include "chrome/browser/prefs/browser_prefs.h" |
14 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 14 #include "chrome/browser/prefs/pref_service_mock_factory.h" |
15 #include "chrome/browser/prefs/pref_service_syncable.h" | 15 #include "chrome/browser/prefs/pref_service_syncable.h" |
16 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 16 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
17 #include "chrome/browser/prefs/proxy_prefs.h" | 17 #include "chrome/browser/prefs/proxy_prefs.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "components/user_prefs/pref_registry_syncable.h" | 20 #include "components/user_prefs/pref_registry_syncable.h" |
21 #include "policy/policy_constants.h" | 21 #include "policy/policy_constants.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using ::testing::Return; | 24 using ::testing::Return; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 PolicyServiceImpl::Providers providers; | 90 PolicyServiceImpl::Providers providers; |
91 providers.push_back(&provider_); | 91 providers.push_back(&provider_); |
92 policy_service_.reset(new PolicyServiceImpl(providers)); | 92 policy_service_.reset(new PolicyServiceImpl(providers)); |
93 provider_.Init(); | 93 provider_.Init(); |
94 } | 94 } |
95 | 95 |
96 virtual void TearDown() OVERRIDE { | 96 virtual void TearDown() OVERRIDE { |
97 provider_.Shutdown(); | 97 provider_.Shutdown(); |
98 } | 98 } |
99 | 99 |
100 PrefService* CreatePrefService(bool with_managed_policies) { | 100 scoped_ptr<PrefService> CreatePrefService(bool with_managed_policies) { |
101 PrefServiceMockBuilder builder; | 101 PrefServiceMockFactory factory; |
102 builder.WithCommandLine(&command_line_); | 102 factory.SetCommandLine(&command_line_); |
103 if (with_managed_policies) | 103 if (with_managed_policies) |
104 builder.WithManagedPolicies(policy_service_.get()); | 104 factory.SetManagedPolicies(policy_service_.get()); |
105 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( | 105 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( |
106 new user_prefs::PrefRegistrySyncable); | 106 new user_prefs::PrefRegistrySyncable); |
107 PrefServiceSyncable* prefs = builder.CreateSyncable(registry.get()); | 107 scoped_ptr<PrefServiceSyncable> prefs = |
| 108 factory.CreateSyncable(registry.get()); |
108 chrome::RegisterUserProfilePrefs(registry.get()); | 109 chrome::RegisterUserProfilePrefs(registry.get()); |
109 return prefs; | 110 return prefs.PassAs<PrefService>(); |
110 } | 111 } |
111 | 112 |
112 base::MessageLoop loop_; | 113 base::MessageLoop loop_; |
113 CommandLine command_line_; | 114 CommandLine command_line_; |
114 MockConfigurationPolicyProvider provider_; | 115 MockConfigurationPolicyProvider provider_; |
115 scoped_ptr<PolicyServiceImpl> policy_service_; | 116 scoped_ptr<PolicyServiceImpl> policy_service_; |
116 }; | 117 }; |
117 | 118 |
118 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) { | 119 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) { |
119 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 120 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
(...skipping 14 matching lines...) Expand all Loading... |
134 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 135 scoped_ptr<PrefService> prefs(CreatePrefService(false)); |
135 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 136 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
136 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); | 137 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
137 assertProxyServer(dict, "789"); | 138 assertProxyServer(dict, "789"); |
138 assertPacUrl(dict, std::string()); | 139 assertPacUrl(dict, std::string()); |
139 assertBypassList(dict, "123"); | 140 assertBypassList(dict, "123"); |
140 | 141 |
141 // Try a second time time with the managed PrefStore in place, the | 142 // Try a second time time with the managed PrefStore in place, the |
142 // manual proxy policy should have removed all traces of the command | 143 // manual proxy policy should have removed all traces of the command |
143 // line and replaced them with the policy versions. | 144 // line and replaced them with the policy versions. |
144 prefs.reset(CreatePrefService(true)); | 145 prefs = CreatePrefService(true); |
145 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 146 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
146 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS); | 147 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS); |
147 assertProxyServer(dict2, "ghi"); | 148 assertProxyServer(dict2, "ghi"); |
148 assertPacUrl(dict2, std::string()); | 149 assertPacUrl(dict2, std::string()); |
149 assertBypassList(dict2, "abc"); | 150 assertBypassList(dict2, "abc"); |
150 } | 151 } |
151 | 152 |
152 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) { | 153 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) { |
153 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 154 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
154 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); | 155 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); |
155 Value* mode_name = Value::CreateStringValue( | 156 Value* mode_name = Value::CreateStringValue( |
156 ProxyPrefs::kAutoDetectProxyModeName); | 157 ProxyPrefs::kAutoDetectProxyModeName); |
157 PolicyMap policy; | 158 PolicyMap policy; |
158 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 159 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
159 mode_name, NULL); | 160 mode_name, NULL); |
160 provider_.UpdateChromePolicy(policy); | 161 provider_.UpdateChromePolicy(policy); |
161 | 162 |
162 // First verify that command-line options are set correctly when | 163 // First verify that command-line options are set correctly when |
163 // there is no policy in effect. | 164 // there is no policy in effect. |
164 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 165 scoped_ptr<PrefService> prefs = CreatePrefService(false); |
165 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 166 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
166 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); | 167 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
167 assertProxyServer(dict, "789"); | 168 assertProxyServer(dict, "789"); |
168 assertPacUrl(dict, std::string()); | 169 assertPacUrl(dict, std::string()); |
169 assertBypassList(dict, "123"); | 170 assertBypassList(dict, "123"); |
170 | 171 |
171 // Try a second time time with the managed PrefStore in place, the | 172 // Try a second time time with the managed PrefStore in place, the |
172 // no proxy policy should have removed all traces of the command | 173 // no proxy policy should have removed all traces of the command |
173 // line proxy settings, even though they were not the specific one | 174 // line proxy settings, even though they were not the specific one |
174 // set in policy. | 175 // set in policy. |
175 prefs.reset(CreatePrefService(true)); | 176 prefs = CreatePrefService(true); |
176 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 177 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
177 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); | 178 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
178 } | 179 } |
179 | 180 |
180 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) { | 181 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) { |
181 command_line_.AppendSwitch(switches::kNoProxyServer); | 182 command_line_.AppendSwitch(switches::kNoProxyServer); |
182 Value* mode_name = Value::CreateStringValue( | 183 Value* mode_name = Value::CreateStringValue( |
183 ProxyPrefs::kAutoDetectProxyModeName); | 184 ProxyPrefs::kAutoDetectProxyModeName); |
184 PolicyMap policy; | 185 PolicyMap policy; |
185 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 186 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
186 mode_name, NULL); | 187 mode_name, NULL); |
187 provider_.UpdateChromePolicy(policy); | 188 provider_.UpdateChromePolicy(policy); |
188 | 189 |
189 // First verify that command-line options are set correctly when | 190 // First verify that command-line options are set correctly when |
190 // there is no policy in effect. | 191 // there is no policy in effect. |
191 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 192 scoped_ptr<PrefService> prefs = CreatePrefService(false); |
192 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 193 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
193 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT); | 194 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT); |
194 | 195 |
195 // Try a second time time with the managed PrefStore in place, the | 196 // Try a second time time with the managed PrefStore in place, the |
196 // auto-detect should be overridden. The default pref store must be | 197 // auto-detect should be overridden. The default pref store must be |
197 // in place with the appropriate default value for this to work. | 198 // in place with the appropriate default value for this to work. |
198 prefs.reset(CreatePrefService(true)); | 199 prefs = CreatePrefService(true); |
199 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 200 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
200 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); | 201 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
201 } | 202 } |
202 | 203 |
203 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) { | 204 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) { |
204 command_line_.AppendSwitch(switches::kProxyAutoDetect); | 205 command_line_.AppendSwitch(switches::kProxyAutoDetect); |
205 Value* mode_name = Value::CreateStringValue( | 206 Value* mode_name = Value::CreateStringValue( |
206 ProxyPrefs::kDirectProxyModeName); | 207 ProxyPrefs::kDirectProxyModeName); |
207 PolicyMap policy; | 208 PolicyMap policy; |
208 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 209 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
209 mode_name, NULL); | 210 mode_name, NULL); |
210 provider_.UpdateChromePolicy(policy); | 211 provider_.UpdateChromePolicy(policy); |
211 | 212 |
212 // First verify that the auto-detect is set if there is no managed | 213 // First verify that the auto-detect is set if there is no managed |
213 // PrefStore. | 214 // PrefStore. |
214 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 215 scoped_ptr<PrefService> prefs = CreatePrefService(false); |
215 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 216 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
216 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT); | 217 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT); |
217 | 218 |
218 // Try a second time time with the managed PrefStore in place, the | 219 // Try a second time time with the managed PrefStore in place, the |
219 // auto-detect should be overridden. The default pref store must be | 220 // auto-detect should be overridden. The default pref store must be |
220 // in place with the appropriate default value for this to work. | 221 // in place with the appropriate default value for this to work. |
221 prefs.reset(CreatePrefService(true)); | 222 prefs = CreatePrefService(true); |
222 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 223 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
223 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT); | 224 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT); |
224 } | 225 } |
225 | 226 |
226 } // namespace policy | 227 } // namespace policy |
OLD | NEW |