ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / prefs / proxy_policy_unittest.cc
blob3daee075bfd27150a4cbd883e044d06591e2505d
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/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/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/mock_configuration_policy_provider.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_service_impl.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_prefs.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "policy/policy_constants.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using ::testing::Return;
27 using ::testing::_;
29 namespace policy {
31 namespace {
33 void assertProxyMode(const ProxyConfigDictionary& dict,
34 ProxyPrefs::ProxyMode expected_mode) {
35 ProxyPrefs::ProxyMode actual_mode;
36 ASSERT_TRUE(dict.GetMode(&actual_mode));
37 EXPECT_EQ(expected_mode, actual_mode);
40 void assertProxyServer(const ProxyConfigDictionary& dict,
41 const std::string& expected) {
42 std::string actual;
43 if (!expected.empty()) {
44 ASSERT_TRUE(dict.GetProxyServer(&actual));
45 EXPECT_EQ(expected, actual);
46 } else {
47 EXPECT_FALSE(dict.GetProxyServer(&actual));
51 void assertPacUrl(const ProxyConfigDictionary& dict,
52 const std::string& expected) {
53 std::string actual;
54 if (!expected.empty()) {
55 ASSERT_TRUE(dict.GetPacUrl(&actual));
56 EXPECT_EQ(expected, actual);
57 } else {
58 EXPECT_FALSE(dict.GetPacUrl(&actual));
62 void assertBypassList(const ProxyConfigDictionary& dict,
63 const std::string& expected) {
64 std::string actual;
65 if (!expected.empty()) {
66 ASSERT_TRUE(dict.GetBypassList(&actual));
67 EXPECT_EQ(expected, actual);
68 } else {
69 EXPECT_FALSE(dict.GetBypassList(&actual));
73 void assertProxyModeWithoutParams(const ProxyConfigDictionary& dict,
74 ProxyPrefs::ProxyMode proxy_mode) {
75 assertProxyMode(dict, proxy_mode);
76 assertProxyServer(dict, std::string());
77 assertPacUrl(dict, std::string());
78 assertBypassList(dict, std::string());
81 } // namespace
83 class ProxyPolicyTest : public testing::Test {
84 protected:
85 ProxyPolicyTest() : command_line_(base::CommandLine::NO_PROGRAM) {}
87 void SetUp() override {
88 EXPECT_CALL(provider_, IsInitializationComplete(_))
89 .WillRepeatedly(Return(true));
91 PolicyServiceImpl::Providers providers;
92 providers.push_back(&provider_);
93 policy_service_.reset(new PolicyServiceImpl(providers));
94 provider_.Init();
97 void TearDown() override { provider_.Shutdown(); }
99 scoped_ptr<PrefService> CreatePrefService(bool with_managed_policies) {
100 PrefServiceMockFactory factory;
101 factory.SetCommandLine(&command_line_);
102 if (with_managed_policies)
103 factory.SetManagedPolicies(policy_service_.get());
104 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
105 new user_prefs::PrefRegistrySyncable);
106 scoped_ptr<PrefServiceSyncable> prefs =
107 factory.CreateSyncable(registry.get());
108 chrome::RegisterUserProfilePrefs(registry.get());
109 return prefs.Pass();
112 content::TestBrowserThreadBundle thread_bundle_;
113 base::CommandLine command_line_;
114 MockConfigurationPolicyProvider provider_;
115 scoped_ptr<PolicyServiceImpl> policy_service_;
118 TEST_F(ProxyPolicyTest, OverridesCommandLineOptions) {
119 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
120 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
121 base::Value* mode_name =
122 new base::StringValue(ProxyPrefs::kFixedServersProxyModeName);
123 PolicyMap policy;
124 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
125 mode_name, NULL);
126 policy.Set(key::kProxyBypassList,
127 POLICY_LEVEL_MANDATORY,
128 POLICY_SCOPE_USER,
129 new base::StringValue("abc"),
130 NULL);
131 policy.Set(key::kProxyServer,
132 POLICY_LEVEL_MANDATORY,
133 POLICY_SCOPE_USER,
134 new base::StringValue("ghi"),
135 NULL);
136 provider_.UpdateChromePolicy(policy);
138 // First verify that command-line options are set correctly when
139 // there is no policy in effect.
140 scoped_ptr<PrefService> prefs(CreatePrefService(false));
141 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
142 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
143 assertProxyServer(dict, "789");
144 assertPacUrl(dict, std::string());
145 assertBypassList(dict, "123");
147 // Try a second time time with the managed PrefStore in place, the
148 // manual proxy policy should have removed all traces of the command
149 // line and replaced them with the policy versions.
150 prefs = CreatePrefService(true);
151 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
152 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS);
153 assertProxyServer(dict2, "ghi");
154 assertPacUrl(dict2, std::string());
155 assertBypassList(dict2, "abc");
158 TEST_F(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) {
159 command_line_.AppendSwitchASCII(switches::kProxyBypassList, "123");
160 command_line_.AppendSwitchASCII(switches::kProxyServer, "789");
161 base::Value* mode_name =
162 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
163 PolicyMap policy;
164 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
165 mode_name, NULL);
166 provider_.UpdateChromePolicy(policy);
168 // First verify that command-line options are set correctly when
169 // there is no policy in effect.
170 scoped_ptr<PrefService> prefs = CreatePrefService(false);
171 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
172 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS);
173 assertProxyServer(dict, "789");
174 assertPacUrl(dict, std::string());
175 assertBypassList(dict, "123");
177 // Try a second time time with the managed PrefStore in place, the
178 // no proxy policy should have removed all traces of the command
179 // line proxy settings, even though they were not the specific one
180 // set in policy.
181 prefs = CreatePrefService(true);
182 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
183 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
186 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) {
187 command_line_.AppendSwitch(switches::kNoProxyServer);
188 base::Value* mode_name =
189 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
190 PolicyMap policy;
191 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
192 mode_name, NULL);
193 provider_.UpdateChromePolicy(policy);
195 // First verify that command-line options are set correctly when
196 // there is no policy in effect.
197 scoped_ptr<PrefService> prefs = CreatePrefService(false);
198 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
199 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
201 // Try a second time time with the managed PrefStore in place, the
202 // auto-detect should be overridden. The default pref store must be
203 // in place with the appropriate default value for this to work.
204 prefs = CreatePrefService(true);
205 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
206 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
209 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) {
210 command_line_.AppendSwitch(switches::kProxyAutoDetect);
211 base::Value* mode_name =
212 new base::StringValue(ProxyPrefs::kDirectProxyModeName);
213 PolicyMap policy;
214 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
215 mode_name, NULL);
216 provider_.UpdateChromePolicy(policy);
218 // First verify that the auto-detect is set if there is no managed
219 // PrefStore.
220 scoped_ptr<PrefService> prefs = CreatePrefService(false);
221 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy));
222 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
224 // Try a second time time with the managed PrefStore in place, the
225 // auto-detect should be overridden. The default pref store must be
226 // in place with the appropriate default value for this to work.
227 prefs = CreatePrefService(true);
228 ProxyConfigDictionary dict2(prefs->GetDictionary(prefs::kProxy));
229 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
232 } // namespace policy