OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 7 #include "chrome/browser/prefs/browser_prefs.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 10 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
| 11 #include "chrome/browser/prefs/proxy_prefs.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 void assertProxyMode(const ProxyConfigDictionary& dict, |
| 17 ProxyPrefs::ProxyMode expected_mode) { |
| 18 ProxyPrefs::ProxyMode actual_mode; |
| 19 ASSERT_TRUE(dict.GetMode(&actual_mode)); |
| 20 EXPECT_EQ(expected_mode, actual_mode); |
| 21 } |
| 22 |
| 23 void assertProxyServer(const ProxyConfigDictionary& dict, |
| 24 const std::string& expected) { |
| 25 std::string actual; |
| 26 if (!expected.empty()) { |
| 27 ASSERT_TRUE(dict.GetProxyServer(&actual)); |
| 28 EXPECT_EQ(expected, actual); |
| 29 } else { |
| 30 EXPECT_FALSE(dict.GetProxyServer(&actual)); |
| 31 } |
| 32 } |
| 33 |
| 34 void assertPacUrl(const ProxyConfigDictionary& dict, |
| 35 const std::string& expected) { |
| 36 std::string actual; |
| 37 if (!expected.empty()) { |
| 38 ASSERT_TRUE(dict.GetPacUrl(&actual)); |
| 39 EXPECT_EQ(expected, actual); |
| 40 } else { |
| 41 EXPECT_FALSE(dict.GetPacUrl(&actual)); |
| 42 } |
| 43 } |
| 44 |
| 45 void assertBypassList(const ProxyConfigDictionary& dict, |
| 46 const std::string& expected) { |
| 47 std::string actual; |
| 48 if (!expected.empty()) { |
| 49 ASSERT_TRUE(dict.GetBypassList(&actual)); |
| 50 EXPECT_EQ(expected, actual); |
| 51 } else { |
| 52 EXPECT_FALSE(dict.GetBypassList(&actual)); |
| 53 } |
| 54 } |
| 55 |
| 56 void assertProxyModeWithoutParams(const ProxyConfigDictionary& dict, |
| 57 ProxyPrefs::ProxyMode proxy_mode) { |
| 58 assertProxyMode(dict, proxy_mode); |
| 59 assertProxyServer(dict, ""); |
| 60 assertPacUrl(dict, ""); |
| 61 assertBypassList(dict, ""); |
| 62 } |
| 63 |
| 64 TEST(ProxyPolicyTest, OverridesCommandLineOptions) { |
| 65 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 66 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
| 67 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
| 68 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 69 new policy::MockConfigurationPolicyProvider()); |
| 70 Value* mode_name = Value::CreateStringValue( |
| 71 ProxyPrefs::kFixedServersProxyModeName); |
| 72 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); |
| 73 provider->AddPolicy(policy::kPolicyProxyBypassList, |
| 74 Value::CreateStringValue("abc")); |
| 75 provider->AddPolicy(policy::kPolicyProxyServer, |
| 76 Value::CreateStringValue("ghi")); |
| 77 |
| 78 // First verify that command-line options are set correctly when |
| 79 // there is no policy in effect. |
| 80 PrefServiceMockBuilder builder; |
| 81 builder.WithCommandLine(&command_line); |
| 82 scoped_ptr<PrefService> prefs(builder.Create()); |
| 83 browser::RegisterUserPrefs(prefs.get()); |
| 84 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
| 85 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
| 86 assertProxyServer(dict, "789"); |
| 87 assertPacUrl(dict, ""); |
| 88 assertBypassList(dict, "123"); |
| 89 |
| 90 // Try a second time time with the managed PrefStore in place, the |
| 91 // manual proxy policy should have removed all traces of the command |
| 92 // line and replaced them with the policy versions. |
| 93 builder.WithCommandLine(&command_line); |
| 94 builder.WithManagedPlatformProvider(provider.get()); |
| 95 scoped_ptr<PrefService> prefs2(builder.Create()); |
| 96 browser::RegisterUserPrefs(prefs2.get()); |
| 97 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy)); |
| 98 assertProxyMode(dict2, ProxyPrefs::MODE_FIXED_SERVERS); |
| 99 assertProxyServer(dict2, "ghi"); |
| 100 assertPacUrl(dict2, ""); |
| 101 assertBypassList(dict2, "abc"); |
| 102 } |
| 103 |
| 104 TEST(ProxyPolicyTest, OverridesUnrelatedCommandLineOptions) { |
| 105 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 106 command_line.AppendSwitchASCII(switches::kProxyBypassList, "123"); |
| 107 command_line.AppendSwitchASCII(switches::kProxyServer, "789"); |
| 108 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 109 new policy::MockConfigurationPolicyProvider()); |
| 110 Value* mode_name = Value::CreateStringValue( |
| 111 ProxyPrefs::kAutoDetectProxyModeName); |
| 112 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); |
| 113 |
| 114 // First verify that command-line options are set correctly when |
| 115 // there is no policy in effect. |
| 116 PrefServiceMockBuilder builder; |
| 117 builder.WithCommandLine(&command_line); |
| 118 scoped_ptr<PrefService> prefs(builder.Create()); |
| 119 browser::RegisterUserPrefs(prefs.get()); |
| 120 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
| 121 assertProxyMode(dict, ProxyPrefs::MODE_FIXED_SERVERS); |
| 122 assertProxyServer(dict, "789"); |
| 123 assertPacUrl(dict, ""); |
| 124 assertBypassList(dict, "123"); |
| 125 |
| 126 // Try a second time time with the managed PrefStore in place, the |
| 127 // no proxy policy should have removed all traces of the command |
| 128 // line proxy settings, even though they were not the specific one |
| 129 // set in policy. |
| 130 builder.WithCommandLine(&command_line); |
| 131 builder.WithManagedPlatformProvider(provider.get()); |
| 132 scoped_ptr<PrefService> prefs2(builder.Create()); |
| 133 browser::RegisterUserPrefs(prefs2.get()); |
| 134 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy)); |
| 135 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
| 136 } |
| 137 |
| 138 TEST(ProxyPolicyTest, OverridesCommandLineNoProxy) { |
| 139 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 140 command_line.AppendSwitch(switches::kNoProxyServer); |
| 141 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 142 new policy::MockConfigurationPolicyProvider()); |
| 143 Value* mode_name = Value::CreateStringValue( |
| 144 ProxyPrefs::kAutoDetectProxyModeName); |
| 145 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); |
| 146 |
| 147 // First verify that command-line options are set correctly when |
| 148 // there is no policy in effect. |
| 149 PrefServiceMockBuilder builder; |
| 150 builder.WithCommandLine(&command_line); |
| 151 scoped_ptr<PrefService> prefs(builder.Create()); |
| 152 browser::RegisterUserPrefs(prefs.get()); |
| 153 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
| 154 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_DIRECT); |
| 155 |
| 156 // Try a second time time with the managed PrefStore in place, the |
| 157 // auto-detect should be overridden. The default pref store must be |
| 158 // in place with the appropriate default value for this to work. |
| 159 builder.WithCommandLine(&command_line); |
| 160 builder.WithManagedPlatformProvider(provider.get()); |
| 161 scoped_ptr<PrefService> prefs2(builder.Create()); |
| 162 browser::RegisterUserPrefs(prefs2.get()); |
| 163 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy)); |
| 164 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_AUTO_DETECT); |
| 165 } |
| 166 |
| 167 TEST(ProxyPolicyTest, OverridesCommandLineAutoDetect) { |
| 168 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 169 command_line.AppendSwitch(switches::kProxyAutoDetect); |
| 170 scoped_ptr<policy::MockConfigurationPolicyProvider> provider( |
| 171 new policy::MockConfigurationPolicyProvider()); |
| 172 Value* mode_name = Value::CreateStringValue( |
| 173 ProxyPrefs::kDirectProxyModeName); |
| 174 provider->AddPolicy(policy::kPolicyProxyMode, mode_name); |
| 175 |
| 176 // First verify that the auto-detect is set if there is no managed |
| 177 // PrefStore. |
| 178 PrefServiceMockBuilder builder; |
| 179 builder.WithCommandLine(&command_line); |
| 180 scoped_ptr<PrefService> prefs(builder.Create()); |
| 181 browser::RegisterUserPrefs(prefs.get()); |
| 182 ProxyConfigDictionary dict(prefs->GetDictionary(prefs::kProxy)); |
| 183 assertProxyModeWithoutParams(dict, ProxyPrefs::MODE_AUTO_DETECT); |
| 184 |
| 185 // Try a second time time with the managed PrefStore in place, the |
| 186 // auto-detect should be overridden. The default pref store must be |
| 187 // in place with the appropriate default value for this to work. |
| 188 builder.WithCommandLine(&command_line); |
| 189 builder.WithManagedPlatformProvider(provider.get()); |
| 190 scoped_ptr<PrefService> prefs2(builder.Create()); |
| 191 browser::RegisterUserPrefs(prefs2.get()); |
| 192 ProxyConfigDictionary dict2(prefs2->GetDictionary(prefs::kProxy)); |
| 193 assertProxyModeWithoutParams(dict2, ProxyPrefs::MODE_DIRECT); |
| 194 } |
OLD | NEW |