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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 6 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
7 #include "chrome/browser/policy/policy_map.h" | 7 #include "chrome/browser/policy/policy_map.h" |
8 #include "chrome/browser/policy/policy_service_impl.h" | 8 #include "chrome/browser/policy/policy_service_impl.h" |
9 #include "chrome/browser/prefs/browser_prefs.h" | 9 #include "chrome/browser/prefs/browser_prefs.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 class ProxyPolicyTest : public testing::Test { | 76 class ProxyPolicyTest : public testing::Test { |
77 protected: | 77 protected: |
78 ProxyPolicyTest() | 78 ProxyPolicyTest() |
79 : command_line_(CommandLine::NO_PROGRAM) {} | 79 : command_line_(CommandLine::NO_PROGRAM) {} |
80 | 80 |
81 virtual void SetUp() OVERRIDE { | 81 virtual void SetUp() OVERRIDE { |
82 EXPECT_CALL(provider_, ProvideInternal(_)) | |
83 .WillRepeatedly(CopyPolicyMap(&policy_)); | |
84 EXPECT_CALL(provider_, IsInitializationComplete()) | 82 EXPECT_CALL(provider_, IsInitializationComplete()) |
85 .WillRepeatedly(Return(true)); | 83 .WillRepeatedly(Return(true)); |
86 | 84 |
87 PolicyServiceImpl::Providers providers; | 85 PolicyServiceImpl::Providers providers; |
88 providers.push_back(&provider_); | 86 providers.push_back(&provider_); |
89 policy_service_.reset(new PolicyServiceImpl(providers)); | 87 policy_service_.reset(new PolicyServiceImpl(providers)); |
90 } | 88 } |
91 | 89 |
92 PrefService* CreatePrefService(bool with_managed_policies) { | 90 PrefService* CreatePrefService(bool with_managed_policies) { |
93 PrefServiceMockBuilder builder; | 91 PrefServiceMockBuilder builder; |
94 builder.WithCommandLine(&command_line_); | 92 builder.WithCommandLine(&command_line_); |
95 if (with_managed_policies) | 93 if (with_managed_policies) |
96 builder.WithManagedPolicies(policy_service_.get()); | 94 builder.WithManagedPolicies(policy_service_.get()); |
97 PrefService* prefs = builder.Create(); | 95 PrefService* prefs = builder.Create(); |
98 browser::RegisterUserPrefs(prefs); | 96 browser::RegisterUserPrefs(prefs); |
99 return prefs; | 97 return prefs; |
100 } | 98 } |
101 | 99 |
102 CommandLine command_line_; | 100 CommandLine command_line_; |
103 PolicyMap policy_; | |
104 MockConfigurationPolicyProvider provider_; | 101 MockConfigurationPolicyProvider provider_; |
105 scoped_ptr<PolicyServiceImpl> policy_service_; | 102 scoped_ptr<PolicyServiceImpl> policy_service_; |
106 }; | 103 }; |
107 | 104 |
108 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) { | 105 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) { |
109 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 106 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
110 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); | 107 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); |
111 Value* mode_name = Value::CreateStringValue( | 108 Value* mode_name = Value::CreateStringValue( |
112 ProxyPrefs::kFixedServersProxyModeName); | 109 ProxyPrefs::kFixedServersProxyModeName); |
113 policy_.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 110 PolicyMap policy; |
114 mode_name); | 111 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
115 policy_.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 112 mode_name); |
116 Value::CreateStringValue("abc")); | 113 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
117 policy_.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 114 Value::CreateStringValue("abc")); |
118 Value::CreateStringValue("ghi")); | 115 policy.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
119 provider_.NotifyPolicyUpdated(); | 116 Value::CreateStringValue("ghi")); |
| 117 provider_.UpdateChromePolicy(policy); |
120 | 118 |
121 // First verify that command-line options are set correctly when | 119 // First verify that command-line options are set correctly when |
122 // there is no policy in effect. | 120 // there is no policy in effect. |
123 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 121 scoped_ptr<PrefService> prefs(CreatePrefService(false)); |
124 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 122 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
125 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); | 123 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
126 assertProxyServer(dict, "789"); | 124 assertProxyServer(dict, "789"); |
127 assertPacUrl(dict, ""); | 125 assertPacUrl(dict, ""); |
128 assertBypassList(dict, "123"); | 126 assertBypassList(dict, "123"); |
129 | 127 |
130 // Try a second time time with the managed PrefStore in place, the | 128 // Try a second time time with the managed PrefStore in place, the |
131 // manual proxy policy should have removed all traces of the command | 129 // manual proxy policy should have removed all traces of the command |
132 // line and replaced them with the policy versions. | 130 // line and replaced them with the policy versions. |
133 prefs.reset(CreatePrefService(true)); | 131 prefs.reset(CreatePrefService(true)); |
134 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 132 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
135 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS); | 133 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS); |
136 assertProxyServer(dict2, "ghi"); | 134 assertProxyServer(dict2, "ghi"); |
137 assertPacUrl(dict2, ""); | 135 assertPacUrl(dict2, ""); |
138 assertBypassList(dict2, "abc"); | 136 assertBypassList(dict2, "abc"); |
139 } | 137 } |
140 | 138 |
141 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) { | 139 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) { |
142 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); | 140 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
143 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); | 141 command_line_.AppendSwitchASCII(switches::kProxyServer, "789"); |
144 Value* mode_name = Value::CreateStringValue( | 142 Value* mode_name = Value::CreateStringValue( |
145 ProxyPrefs::kAutoDetectProxyModeName); | 143 ProxyPrefs::kAutoDetectProxyModeName); |
146 policy_.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 144 PolicyMap policy; |
147 mode_name); | 145 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
148 provider_.NotifyPolicyUpdated(); | 146 mode_name); |
| 147 provider_.UpdateChromePolicy(policy); |
149 | 148 |
150 // First verify that command-line options are set correctly when | 149 // First verify that command-line options are set correctly when |
151 // there is no policy in effect. | 150 // there is no policy in effect. |
152 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 151 scoped_ptr<PrefService> prefs(CreatePrefService(false)); |
153 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 152 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
154 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); | 153 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
155 assertProxyServer(dict, "789"); | 154 assertProxyServer(dict, "789"); |
156 assertPacUrl(dict, ""); | 155 assertPacUrl(dict, ""); |
157 assertBypassList(dict, "123"); | 156 assertBypassList(dict, "123"); |
158 | 157 |
159 // Try a second time time with the managed PrefStore in place, the | 158 // Try a second time time with the managed PrefStore in place, the |
160 // no proxy policy should have removed all traces of the command | 159 // no proxy policy should have removed all traces of the command |
161 // line proxy settings, even though they were not the specific one | 160 // line proxy settings, even though they were not the specific one |
162 // set in policy. | 161 // set in policy. |
163 prefs.reset(CreatePrefService(true)); | 162 prefs.reset(CreatePrefService(true)); |
164 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 163 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
165 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); | 164 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
166 } | 165 } |
167 | 166 |
168 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) { | 167 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) { |
169 command_line_.AppendSwitch(switches::kNoProxyServer); | 168 command_line_.AppendSwitch(switches::kNoProxyServer); |
170 Value* mode_name = Value::CreateStringValue( | 169 Value* mode_name = Value::CreateStringValue( |
171 ProxyPrefs::kAutoDetectProxyModeName); | 170 ProxyPrefs::kAutoDetectProxyModeName); |
172 policy_.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 171 PolicyMap policy; |
173 mode_name); | 172 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
174 provider_.NotifyPolicyUpdated(); | 173 mode_name); |
| 174 provider_.UpdateChromePolicy(policy); |
175 | 175 |
176 // First verify that command-line options are set correctly when | 176 // First verify that command-line options are set correctly when |
177 // there is no policy in effect. | 177 // there is no policy in effect. |
178 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 178 scoped_ptr<PrefService> prefs(CreatePrefService(false)); |
179 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 179 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
180 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT); | 180 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT); |
181 | 181 |
182 // Try a second time time with the managed PrefStore in place, the | 182 // Try a second time time with the managed PrefStore in place, the |
183 // auto-detect should be overridden. The default pref store must be | 183 // auto-detect should be overridden. The default pref store must be |
184 // in place with the appropriate default value for this to work. | 184 // in place with the appropriate default value for this to work. |
185 prefs.reset(CreatePrefService(true)); | 185 prefs.reset(CreatePrefService(true)); |
186 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 186 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
187 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); | 187 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
188 } | 188 } |
189 | 189 |
190 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) { | 190 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) { |
191 command_line_.AppendSwitch(switches::kProxyAutoDetect); | 191 command_line_.AppendSwitch(switches::kProxyAutoDetect); |
192 Value* mode_name = Value::CreateStringValue( | 192 Value* mode_name = Value::CreateStringValue( |
193 ProxyPrefs::kDirectProxyModeName); | 193 ProxyPrefs::kDirectProxyModeName); |
194 policy_.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 194 PolicyMap policy; |
195 mode_name); | 195 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
196 provider_.NotifyPolicyUpdated(); | 196 mode_name); |
| 197 provider_.UpdateChromePolicy(policy); |
197 | 198 |
198 // First verify that the auto-detect is set if there is no managed | 199 // First verify that the auto-detect is set if there is no managed |
199 // PrefStore. | 200 // PrefStore. |
200 scoped_ptr<PrefService> prefs(CreatePrefService(false)); | 201 scoped_ptr<PrefService> prefs(CreatePrefService(false)); |
201 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); | 202 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
202 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT); | 203 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT); |
203 | 204 |
204 // Try a second time time with the managed PrefStore in place, the | 205 // Try a second time time with the managed PrefStore in place, the |
205 // auto-detect should be overridden. The default pref store must be | 206 // auto-detect should be overridden. The default pref store must be |
206 // in place with the appropriate default value for this to work. | 207 // in place with the appropriate default value for this to work. |
207 prefs.reset(CreatePrefService(true)); | 208 prefs.reset(CreatePrefService(true)); |
208 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); | 209 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy)); |
209 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT); | 210 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT); |
210 } | 211 } |
211 | 212 |
212 } // namespace policy | 213 } // namespace policy |
OLD | NEW |