Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / prefs / proxy_policy_unittest.cc
blob650564644741c450adb389f4f511e42ba7c8e7c3
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.
5 #include "base/bind.h"
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/browser_process.h"
11 #include "chrome/browser/prefs/browser_prefs.h"
12 #include "chrome/browser/prefs/command_line_pref_store.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "components/policy/core/common/external_data_fetcher.h"
15 #include "components/policy/core/common/mock_configuration_policy_provider.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/core/common/policy_service_impl.h"
18 #include "components/policy/core/common/policy_types.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "components/proxy_config/proxy_config_dictionary.h"
21 #include "components/proxy_config/proxy_config_pref_names.h"
22 #include "components/syncable_prefs/pref_service_mock_factory.h"
23 #include "components/syncable_prefs/pref_service_syncable.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "policy/policy_constants.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using ::testing::Return;
29 using ::testing::_;
31 namespace policy {
33 namespace {
35 void assertProxyMode(const ProxyConfigDictionary& dict,
36 ProxyPrefs::ProxyMode expected_mode) {
37 ProxyPrefs::ProxyMode actual_mode;
38 ASSERT_TRUE(dict.GetMode(&actual_mode));
39 EXPECT_EQ(expected_mode, actual_mode);
42 void assertProxyServer(const ProxyConfigDictionary& dict,
43 const std::string& expected) {
44 std::string actual;
45 if (!expected.empty()) {
46 ASSERT_TRUE(dict.GetProxyServer(&actual));
47 EXPECT_EQ(expected, actual);
48 } else {
49 EXPECT_FALSE(dict.GetProxyServer(&actual));
53 void assertPacUrl(const ProxyConfigDictionary& dict,
54 const std::string& expected) {
55 std::string actual;
56 if (!expected.empty()) {
57 ASSERT_TRUE(dict.GetPacUrl(&actual));
58 EXPECT_EQ(expected, actual);
59 } else {
60 EXPECT_FALSE(dict.GetPacUrl(&actual));
64 void assertBypassList(const ProxyConfigDictionary& dict,
65 const std::string& expected) {
66 std::string actual;
67 if (!expected.empty()) {
68 ASSERT_TRUE(dict.GetBypassList(&actual));
69 EXPECT_EQ(expected, actual);
70 } else {
71 EXPECT_FALSE(dict.GetBypassList(&actual));
75 void assertProxyModeWithoutParams(const ProxyConfigDictionary& dict,
76 ProxyPrefs::ProxyMode proxy_mode) {
77 assertProxyMode(dict, proxy_mode);
78 assertProxyServer(dict, std::string());
79 assertPacUrl(dict, std::string());
80 assertBypassList(dict, std::string());
83 } // namespace
85 class ProxyPolicyTest : public testing::Test {
86 protected:
87 ProxyPolicyTest() : command_line_(base::CommandLine::NO_PROGRAM) {}
89 void SetUp() override {
90 EXPECT_CALL(provider_, IsInitializationComplete(_))
91 .WillRepeatedly(Return(true));
93 PolicyServiceImpl::Providers providers;
94 providers.push_back(&provider_);
95 policy_service_.reset(new PolicyServiceImpl(providers));
96 provider_.Init();
99 void TearDown() override { provider_.Shutdown(); }
101 scoped_ptr<PrefService> CreatePrefService(bool with_managed_policies) {
102 syncable_prefs::PrefServiceMockFactory factory;
103 factory.set_command_line_prefs(new CommandLinePrefStore(&command_line_));
104 if (with_managed_policies) {
105 factory.SetManagedPolicies(policy_service_.get(),
106 g_browser_process->browser_policy_connector());
109 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
110 new user_prefs::PrefRegistrySyncable);
111 scoped_ptr<syncable_prefs::PrefServiceSyncable> prefs =
112 factory.CreateSyncable(registry.get());
113 chrome::RegisterUserProfilePrefs(registry.get());
114 return prefs.Pass();
117 content::TestBrowserThreadBundle thread_bundle_;
118 base::CommandLine command_line_;
119 MockConfigurationPolicyProvider provider_;
120 scoped_ptr<PolicyServiceImpl> policy_service_;
123 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) {
124 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
125 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
126 base::Value* mode_name =
127 new base::StringValue(ProxyPrefs::kFixedServersProxyModeName);
128 PolicyMap policy;
129 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
130 POLICY_SOURCE_CLOUD, mode_name, nullptr);
131 policy.Set(key::kProxyBypassList,
132 POLICY_LEVEL_MANDATORY,
133 POLICY_SCOPE_USER,
134 POLICY_SOURCE_CLOUD,
135 new base::StringValue("abc"),
136 NULL);
137 policy.Set(key::kProxyServer,
138 POLICY_LEVEL_MANDATORY,
139 POLICY_SCOPE_USER,
140 POLICY_SOURCE_CLOUD,
141 new base::StringValue("ghi"),
142 NULL);
143 provider_.UpdateChromePolicy(policy);
145 // First verify that command-line options are set correctly when
146 // there is no policy in effect.
147 scoped_ptr<PrefService> prefs(CreatePrefService(false));
148 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
149 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
150 assertProxyServer(dict, "789");
151 assertPacUrl(dict, std::string());
152 assertBypassList(dict, "123");
154 // Try a second time time with the managed PrefStore in place, the
155 // manual proxy policy should have removed all traces of the command
156 // line and replaced them with the policy versions.
157 prefs = CreatePrefService(true);
158 ProxyConfigDictionary dict2(
159 prefs->GetDictionary(proxy_config::prefs::kProxy));
160 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
161 assertProxyServer(dict2, "ghi");
162 assertPacUrl(dict2, std::string());
163 assertBypassList(dict2, "abc");
166 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) {
167 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
168 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
169 base::Value* mode_name =
170 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
171 PolicyMap policy;
172 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
173 POLICY_SOURCE_CLOUD, mode_name, nullptr);
174 provider_.UpdateChromePolicy(policy);
176 // First verify that command-line options are set correctly when
177 // there is no policy in effect.
178 scoped_ptr<PrefService> prefs = CreatePrefService(false);
179 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
180 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
181 assertProxyServer(dict, "789");
182 assertPacUrl(dict, std::string());
183 assertBypassList(dict, "123");
185 // Try a second time time with the managed PrefStore in place, the
186 // no proxy policy should have removed all traces of the command
187 // line proxy settings, even though they were not the specific one
188 // set in policy.
189 prefs = CreatePrefService(true);
190 ProxyConfigDictionary dict2(
191 prefs->GetDictionary(proxy_config::prefs::kProxy));
192 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
195 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) {
196 command_line_.AppendSwitch(switches::kNoProxyServer);
197 base::Value* mode_name =
198 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
199 PolicyMap policy;
200 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
201 POLICY_SOURCE_CLOUD, mode_name, nullptr);
202 provider_.UpdateChromePolicy(policy);
204 // First verify that command-line options are set correctly when
205 // there is no policy in effect.
206 scoped_ptr<PrefService> prefs = CreatePrefService(false);
207 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
208 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
210 // Try a second time time with the managed PrefStore in place, the
211 // auto-detect should be overridden. The default pref store must be
212 // in place with the appropriate default value for this to work.
213 prefs = CreatePrefService(true);
214 ProxyConfigDictionary dict2(
215 prefs->GetDictionary(proxy_config::prefs::kProxy));
216 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
219 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) {
220 command_line_.AppendSwitch(switches::kProxyAutoDetect);
221 base::Value* mode_name =
222 new base::StringValue(ProxyPrefs::kDirectProxyModeName);
223 PolicyMap policy;
224 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
225 POLICY_SOURCE_CLOUD, mode_name, nullptr);
226 provider_.UpdateChromePolicy(policy);
228 // First verify that the auto-detect is set if there is no managed
229 // PrefStore.
230 scoped_ptr<PrefService> prefs = CreatePrefService(false);
231 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
232 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
234 // Try a second time time with the managed PrefStore in place, the
235 // auto-detect should be overridden. The default pref store must be
236 // in place with the appropriate default value for this to work.
237 prefs = CreatePrefService(true);
238 ProxyConfigDictionary dict2(
239 prefs->GetDictionary(proxy_config::prefs::kProxy));
240 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
243 } // namespace policy