Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / prefs / proxy_policy_unittest.cc
blobe7963abce6c057239cbcde3c436872d997884170
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 "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/pref_registry/pref_registry_syncable.h"
19 #include "components/proxy_config/proxy_config_dictionary.h"
20 #include "components/proxy_config/proxy_config_pref_names.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "policy/policy_constants.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::Return;
26 using ::testing::_;
28 namespace policy {
30 namespace {
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) {
41 std::string actual;
42 if (!expected.empty()) {
43 ASSERT_TRUE(dict.GetProxyServer(&actual));
44 EXPECT_EQ(expected, actual);
45 } else {
46 EXPECT_FALSE(dict.GetProxyServer(&actual));
50 void assertPacUrl(const ProxyConfigDictionary& dict,
51 const std::string& expected) {
52 std::string actual;
53 if (!expected.empty()) {
54 ASSERT_TRUE(dict.GetPacUrl(&actual));
55 EXPECT_EQ(expected, actual);
56 } else {
57 EXPECT_FALSE(dict.GetPacUrl(&actual));
61 void assertBypassList(const ProxyConfigDictionary& dict,
62 const std::string& expected) {
63 std::string actual;
64 if (!expected.empty()) {
65 ASSERT_TRUE(dict.GetBypassList(&actual));
66 EXPECT_EQ(expected, actual);
67 } else {
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());
80 } // namespace
82 class ProxyPolicyTest : public testing::Test {
83 protected:
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));
93 provider_.Init();
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());
108 return prefs.Pass();
111 content::TestBrowserThreadBundle thread_bundle_;
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);
122 PolicyMap policy;
123 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
124 mode_name, NULL);
125 policy.Set(key::kProxyBypassList,
126 POLICY_LEVEL_MANDATORY,
127 POLICY_SCOPE_USER,
128 new base::StringValue("abc"),
129 NULL);
130 policy.Set(key::kProxyServer,
131 POLICY_LEVEL_MANDATORY,
132 POLICY_SCOPE_USER,
133 new base::StringValue("ghi"),
134 NULL);
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(proxy_config::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(
151 prefs->GetDictionary(proxy_config::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(proxy_config::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(
183 prefs->GetDictionary(proxy_config::prefs::kProxy));
184 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
187 TEST_F(ProxyPolicyTest, OverridesCommandLineNoProxy) {
188 command_line_.AppendSwitch(switches::kNoProxyServer);
189 base::Value* mode_name =
190 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName);
191 PolicyMap policy;
192 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
193 mode_name, NULL);
194 provider_.UpdateChromePolicy(policy);
196 // First verify that command-line options are set correctly when
197 // there is no policy in effect.
198 scoped_ptr<PrefService> prefs = CreatePrefService(false);
199 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
200 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT);
202 // Try a second time time with the managed PrefStore in place, the
203 // auto-detect should be overridden. The default pref store must be
204 // in place with the appropriate default value for this to work.
205 prefs = CreatePrefService(true);
206 ProxyConfigDictionary dict2(
207 prefs->GetDictionary(proxy_config::prefs::kProxy));
208 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT);
211 TEST_F(ProxyPolicyTest, OverridesCommandLineAutoDetect) {
212 command_line_.AppendSwitch(switches::kProxyAutoDetect);
213 base::Value* mode_name =
214 new base::StringValue(ProxyPrefs::kDirectProxyModeName);
215 PolicyMap policy;
216 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
217 mode_name, NULL);
218 provider_.UpdateChromePolicy(policy);
220 // First verify that the auto-detect is set if there is no managed
221 // PrefStore.
222 scoped_ptr<PrefService> prefs = CreatePrefService(false);
223 ProxyConfigDictionary dict(prefs->GetDictionary(proxy_config::prefs::kProxy));
224 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT);
226 // Try a second time time with the managed PrefStore in place, the
227 // auto-detect should be overridden. The default pref store must be
228 // in place with the appropriate default value for this to work.
229 prefs = CreatePrefService(true);
230 ProxyConfigDictionary dict2(
231 prefs->GetDictionary(proxy_config::prefs::kProxy));
232 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT);
235 } // namespace policy