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