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
3 // found in the LICENSE file.
6 #include "base/callback.h"
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/prefs/browser_prefs.h"
11 #include "chrome/browser/prefs/pref_service_mock_factory.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/prefs/proxy_config_dictionary.h"
14 #include "chrome/browser/prefs/proxy_prefs.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/policy/core/common/external_data_fetcher.h"
18 #include "components/policy/core/common/mock_configuration_policy_provider.h"
19 #include "components/policy/core/common/policy_map.h"
20 #include "components/policy/core/common/policy_service_impl.h"
21 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "policy/policy_constants.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::Return
;
32 void assertProxyMode(const ProxyConfigDictionary
& dict
,
33 ProxyPrefs::ProxyMode expected_mode
) {
34 ProxyPrefs::ProxyMode actual_mode
;
35 ASSERT_TRUE(dict
.GetMode(&actual_mode
));
36 EXPECT_EQ(expected_mode
, actual_mode
);
39 void assertProxyServer(const ProxyConfigDictionary
& dict
,
40 const std::string
& expected
) {
42 if (!expected
.empty()) {
43 ASSERT_TRUE(dict
.GetProxyServer(&actual
));
44 EXPECT_EQ(expected
, actual
);
46 EXPECT_FALSE(dict
.GetProxyServer(&actual
));
50 void assertPacUrl(const ProxyConfigDictionary
& dict
,
51 const std::string
& expected
) {
53 if (!expected
.empty()) {
54 ASSERT_TRUE(dict
.GetPacUrl(&actual
));
55 EXPECT_EQ(expected
, actual
);
57 EXPECT_FALSE(dict
.GetPacUrl(&actual
));
61 void assertBypassList(const ProxyConfigDictionary
& dict
,
62 const std::string
& expected
) {
64 if (!expected
.empty()) {
65 ASSERT_TRUE(dict
.GetBypassList(&actual
));
66 EXPECT_EQ(expected
, actual
);
68 EXPECT_FALSE(dict
.GetBypassList(&actual
));
72 void assertProxyModeWithoutParams(const ProxyConfigDictionary
& dict
,
73 ProxyPrefs::ProxyMode proxy_mode
) {
74 assertProxyMode(dict
, proxy_mode
);
75 assertProxyServer(dict
, std::string());
76 assertPacUrl(dict
, std::string());
77 assertBypassList(dict
, std::string());
82 class ProxyPolicyTest
: public testing::Test
{
84 ProxyPolicyTest() : command_line_(base::CommandLine::NO_PROGRAM
) {}
86 void SetUp() override
{
87 EXPECT_CALL(provider_
, IsInitializationComplete(_
))
88 .WillRepeatedly(Return(true));
90 PolicyServiceImpl::Providers providers
;
91 providers
.push_back(&provider_
);
92 policy_service_
.reset(new PolicyServiceImpl(providers
));
96 void TearDown() override
{ provider_
.Shutdown(); }
98 scoped_ptr
<PrefService
> CreatePrefService(bool with_managed_policies
) {
99 PrefServiceMockFactory factory
;
100 factory
.SetCommandLine(&command_line_
);
101 if (with_managed_policies
)
102 factory
.SetManagedPolicies(policy_service_
.get());
103 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
104 new user_prefs::PrefRegistrySyncable
);
105 scoped_ptr
<PrefServiceSyncable
> prefs
=
106 factory
.CreateSyncable(registry
.get());
107 chrome::RegisterUserProfilePrefs(registry
.get());
111 base::MessageLoop loop_
;
112 base::CommandLine command_line_
;
113 MockConfigurationPolicyProvider provider_
;
114 scoped_ptr
<PolicyServiceImpl
> policy_service_
;
117 TEST_F(ProxyPolicyTest
, OverridesCommandLineOptions
) {
118 command_line_
.AppendSwitchASCII(switches::kProxyBypassList
, "123");
119 command_line_
.AppendSwitchASCII(switches::kProxyServer
, "789");
120 base::Value
* mode_name
=
121 new base::StringValue(ProxyPrefs::kFixedServersProxyModeName
);
123 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
125 policy
.Set(key::kProxyBypassList
,
126 POLICY_LEVEL_MANDATORY
,
128 new base::StringValue("abc"),
130 policy
.Set(key::kProxyServer
,
131 POLICY_LEVEL_MANDATORY
,
133 new base::StringValue("ghi"),
135 provider_
.UpdateChromePolicy(policy
);
137 // First verify that command-line options are set correctly when
138 // there is no policy in effect.
139 scoped_ptr
<PrefService
> prefs(CreatePrefService(false));
140 ProxyConfigDictionary
dict(prefs
->GetDictionary(prefs::kProxy
));
141 assertProxyMode(dict
, ProxyPrefs::MODE_FIXED_SERVERS
);
142 assertProxyServer(dict
, "789");
143 assertPacUrl(dict
, std::string());
144 assertBypassList(dict
, "123");
146 // Try a second time time with the managed PrefStore in place, the
147 // manual proxy policy should have removed all traces of the command
148 // line and replaced them with the policy versions.
149 prefs
= CreatePrefService(true);
150 ProxyConfigDictionary
dict2(prefs
->GetDictionary(prefs::kProxy
));
151 assertProxyMode(dict2
, ProxyPrefs::MODE_FIXED_SERVERS
);
152 assertProxyServer(dict2
, "ghi");
153 assertPacUrl(dict2
, std::string());
154 assertBypassList(dict2
, "abc");
157 TEST_F(ProxyPolicyTest
, OverridesUnrelatedCommandLineOptions
) {
158 command_line_
.AppendSwitchASCII(switches::kProxyBypassList
, "123");
159 command_line_
.AppendSwitchASCII(switches::kProxyServer
, "789");
160 base::Value
* mode_name
=
161 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
);
163 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
165 provider_
.UpdateChromePolicy(policy
);
167 // First verify that command-line options are set correctly when
168 // there is no policy in effect.
169 scoped_ptr
<PrefService
> prefs
= CreatePrefService(false);
170 ProxyConfigDictionary
dict(prefs
->GetDictionary(prefs::kProxy
));
171 assertProxyMode(dict
, ProxyPrefs::MODE_FIXED_SERVERS
);
172 assertProxyServer(dict
, "789");
173 assertPacUrl(dict
, std::string());
174 assertBypassList(dict
, "123");
176 // Try a second time time with the managed PrefStore in place, the
177 // no proxy policy should have removed all traces of the command
178 // line proxy settings, even though they were not the specific one
180 prefs
= CreatePrefService(true);
181 ProxyConfigDictionary
dict2(prefs
->GetDictionary(prefs::kProxy
));
182 assertProxyModeWithoutParams(dict2
, ProxyPrefs::MODE_AUTO_DETECT
);
185 TEST_F(ProxyPolicyTest
, OverridesCommandLineNoProxy
) {
186 command_line_
.AppendSwitch(switches::kNoProxyServer
);
187 base::Value
* mode_name
=
188 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
);
190 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
192 provider_
.UpdateChromePolicy(policy
);
194 // First verify that command-line options are set correctly when
195 // there is no policy in effect.
196 scoped_ptr
<PrefService
> prefs
= CreatePrefService(false);
197 ProxyConfigDictionary
dict(prefs
->GetDictionary(prefs::kProxy
));
198 assertProxyModeWithoutParams(dict
, ProxyPrefs::MODE_DIRECT
);
200 // Try a second time time with the managed PrefStore in place, the
201 // auto-detect should be overridden. The default pref store must be
202 // in place with the appropriate default value for this to work.
203 prefs
= CreatePrefService(true);
204 ProxyConfigDictionary
dict2(prefs
->GetDictionary(prefs::kProxy
));
205 assertProxyModeWithoutParams(dict2
, ProxyPrefs::MODE_AUTO_DETECT
);
208 TEST_F(ProxyPolicyTest
, OverridesCommandLineAutoDetect
) {
209 command_line_
.AppendSwitch(switches::kProxyAutoDetect
);
210 base::Value
* mode_name
=
211 new base::StringValue(ProxyPrefs::kDirectProxyModeName
);
213 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
215 provider_
.UpdateChromePolicy(policy
);
217 // First verify that the auto-detect is set if there is no managed
219 scoped_ptr
<PrefService
> prefs
= CreatePrefService(false);
220 ProxyConfigDictionary
dict(prefs
->GetDictionary(prefs::kProxy
));
221 assertProxyModeWithoutParams(dict
, ProxyPrefs::MODE_AUTO_DETECT
);
223 // Try a second time time with the managed PrefStore in place, the
224 // auto-detect should be overridden. The default pref store must be
225 // in place with the appropriate default value for this to work.
226 prefs
= CreatePrefService(true);
227 ProxyConfigDictionary
dict2(prefs
->GetDictionary(prefs::kProxy
));
228 assertProxyModeWithoutParams(dict2
, ProxyPrefs::MODE_DIRECT
);
231 } // namespace policy