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/scoped_ptr.h" |
| 6 #include "base/string16.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/policy/configuration_policy_reader.h" |
| 9 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 10 #include "policy/policy_constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace policy { |
| 14 |
| 15 // Holds a set of test parameters, consisting of policy name and type. |
| 16 class TypeAndName { |
| 17 public: |
| 18 TypeAndName(ConfigurationPolicyType type, const char* policy_name) |
| 19 : type_(type), |
| 20 policy_name_(policy_name) {} |
| 21 |
| 22 ConfigurationPolicyType type() const { return type_; } |
| 23 string16 policy_name() const { return ASCIIToUTF16(policy_name_); } |
| 24 |
| 25 private: |
| 26 ConfigurationPolicyType type_; |
| 27 const char* policy_name_; |
| 28 }; |
| 29 |
| 30 template<typename TESTBASE> |
| 31 class ConfigurationPolicyReaderTestBase : public TESTBASE { |
| 32 protected: |
| 33 ConfigurationPolicyReaderTestBase() : provider_() { |
| 34 managed_reader_.reset(new ConfigurationPolicyReader(&provider_, |
| 35 PolicyStatusInfo::MANDATORY)); |
| 36 recommended_reader_.reset(new ConfigurationPolicyReader(&provider_, |
| 37 PolicyStatusInfo::RECOMMENDED)); |
| 38 status_ok_ = ASCIIToUTF16("ok"); |
| 39 } |
| 40 |
| 41 DictionaryValue* CreateDictionary(TypeAndName policy) { |
| 42 DictionaryValue* dict = new DictionaryValue(); |
| 43 dict->SetString("name", policy.policy_name()); |
| 44 dict->SetString("level", |
| 45 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| 46 dict->SetString("sourceType", |
| 47 PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER)); |
| 48 dict->SetBoolean("set", true); |
| 49 dict->SetString("status", status_ok_); |
| 50 |
| 51 return dict; |
| 52 } |
| 53 |
| 54 MockConfigurationPolicyProvider provider_; |
| 55 scoped_ptr<ConfigurationPolicyReader> managed_reader_; |
| 56 scoped_ptr<ConfigurationPolicyReader> recommended_reader_; |
| 57 string16 status_ok_; |
| 58 }; |
| 59 |
| 60 // Test cases for list-valued policy settings. |
| 61 class ConfigurationPolicyReaderListTest : |
| 62 public ConfigurationPolicyReaderTestBase< |
| 63 testing::TestWithParam<TypeAndName> > { |
| 64 }; |
| 65 |
| 66 TEST_P(ConfigurationPolicyReaderListTest, GetDefault) { |
| 67 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(GetParam().type())); |
| 68 } |
| 69 |
| 70 TEST_P(ConfigurationPolicyReaderListTest, SetValue) { |
| 71 ListValue* in_value = new ListValue(); |
| 72 in_value->Append(Value::CreateStringValue("test1")); |
| 73 in_value->Append(Value::CreateStringValue("test2")); |
| 74 provider_.AddPolicy(GetParam().type(), in_value); |
| 75 managed_reader_->OnUpdatePolicy(); |
| 76 |
| 77 scoped_ptr<DictionaryValue> dict(CreateDictionary(GetParam())); |
| 78 dict->Set("value", in_value->DeepCopy()); |
| 79 EXPECT_TRUE( |
| 80 dict->Equals(managed_reader_->GetPolicyStatus(GetParam().type()))); |
| 81 |
| 82 recommended_reader_->OnUpdatePolicy(); |
| 83 dict->SetString("level", |
| 84 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 85 EXPECT_TRUE(dict->Equals( |
| 86 recommended_reader_->GetPolicyStatus(GetParam().type()))); |
| 87 } |
| 88 |
| 89 INSTANTIATE_TEST_CASE_P( |
| 90 ConfigurationPolicyReaderListTestInstance, |
| 91 ConfigurationPolicyReaderListTest, |
| 92 testing::Values( |
| 93 TypeAndName(kPolicyRestoreOnStartupURLs, |
| 94 key::kRestoreOnStartupURLs), |
| 95 TypeAndName(kPolicyExtensionInstallWhitelist, |
| 96 key::kExtensionInstallWhitelist), |
| 97 TypeAndName(kPolicyExtensionInstallBlacklist, |
| 98 key::kExtensionInstallBlacklist), |
| 99 TypeAndName(kPolicyDisabledPlugins, |
| 100 key::kDisabledPlugins), |
| 101 TypeAndName(kPolicyDisabledPluginsExceptions, |
| 102 key::kDisabledPluginsExceptions), |
| 103 TypeAndName(kPolicyEnabledPlugins, |
| 104 key::kEnabledPlugins), |
| 105 TypeAndName(kPolicyDisabledSchemes, |
| 106 key::kDisabledSchemes))); |
| 107 |
| 108 // Test cases for string-valued policy settings. |
| 109 class ConfigurationPolicyReaderStringTest : |
| 110 public ConfigurationPolicyReaderTestBase< |
| 111 testing::TestWithParam<TypeAndName> > { |
| 112 }; |
| 113 |
| 114 TEST_P(ConfigurationPolicyReaderStringTest, GetDefault) { |
| 115 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(GetParam().type())); |
| 116 } |
| 117 |
| 118 TEST_P(ConfigurationPolicyReaderStringTest, SetValue) { |
| 119 provider_.AddPolicy(GetParam().type(), |
| 120 Value::CreateStringValue("http://chromium.org")); |
| 121 managed_reader_->OnUpdatePolicy(); |
| 122 scoped_ptr<DictionaryValue> dict(CreateDictionary(GetParam())); |
| 123 dict->Set("value", Value::CreateStringValue("http://chromium.org")); |
| 124 EXPECT_TRUE( |
| 125 dict->Equals(managed_reader_->GetPolicyStatus(GetParam().type()))); |
| 126 |
| 127 recommended_reader_->OnUpdatePolicy(); |
| 128 dict->SetString("level", |
| 129 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 130 EXPECT_TRUE(dict->Equals( |
| 131 recommended_reader_->GetPolicyStatus(GetParam().type()))); |
| 132 } |
| 133 |
| 134 INSTANTIATE_TEST_CASE_P( |
| 135 ConfigurationPolicyReaderStringTestInstance, |
| 136 ConfigurationPolicyReaderStringTest, |
| 137 testing::Values( |
| 138 TypeAndName(kPolicyHomepageLocation, |
| 139 key::kHomepageLocation), |
| 140 TypeAndName(kPolicyApplicationLocaleValue, |
| 141 key::kApplicationLocaleValue), |
| 142 TypeAndName(kPolicyAuthSchemes, |
| 143 key::kAuthSchemes), |
| 144 TypeAndName(kPolicyAuthServerWhitelist, |
| 145 key::kAuthServerWhitelist), |
| 146 TypeAndName(kPolicyAuthNegotiateDelegateWhitelist, |
| 147 key::kAuthNegotiateDelegateWhitelist), |
| 148 TypeAndName(kPolicyGSSAPILibraryName, |
| 149 key::kGSSAPILibraryName), |
| 150 TypeAndName(kPolicyDiskCacheDir, |
| 151 key::kDiskCacheDir))); |
| 152 |
| 153 #if !defined(OS_CHROMEOS) |
| 154 INSTANTIATE_TEST_CASE_P( |
| 155 ConfigurationPolicyReaderDownloadDirectoryInstance, |
| 156 ConfigurationPolicyReaderStringTest, |
| 157 testing::Values(TypeAndName(kPolicyDownloadDirectory, |
| 158 key::kDownloadDirectory))); |
| 159 #endif // !defined(OS_CHROMEOS) |
| 160 |
| 161 // Test cases for boolean-valued policy settings. |
| 162 class ConfigurationPolicyReaderBooleanTest : |
| 163 public ConfigurationPolicyReaderTestBase< |
| 164 testing::TestWithParam<TypeAndName> > { |
| 165 }; |
| 166 |
| 167 TEST_P(ConfigurationPolicyReaderBooleanTest, GetDefault) { |
| 168 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(GetParam().type())); |
| 169 } |
| 170 |
| 171 TEST_P(ConfigurationPolicyReaderBooleanTest, SetValue) { |
| 172 provider_.AddPolicy(GetParam().type(), Value::CreateBooleanValue(true)); |
| 173 managed_reader_->OnUpdatePolicy(); |
| 174 scoped_ptr<DictionaryValue> dict(CreateDictionary(GetParam())); |
| 175 dict->Set("value", Value::CreateBooleanValue(true)); |
| 176 EXPECT_TRUE(dict->Equals( |
| 177 managed_reader_->GetPolicyStatus(GetParam().type()))); |
| 178 |
| 179 recommended_reader_->OnUpdatePolicy(); |
| 180 dict->SetString("level", |
| 181 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 182 EXPECT_TRUE(dict->Equals( |
| 183 recommended_reader_->GetPolicyStatus(GetParam().type()))); |
| 184 |
| 185 provider_.AddPolicy(GetParam().type(), Value::CreateBooleanValue(false)); |
| 186 managed_reader_->OnUpdatePolicy(); |
| 187 dict->Set("value", Value::CreateBooleanValue(false)); |
| 188 dict->SetString("level", |
| 189 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); |
| 190 EXPECT_TRUE( |
| 191 dict->Equals(managed_reader_->GetPolicyStatus(GetParam().type()))); |
| 192 |
| 193 recommended_reader_->OnUpdatePolicy(); |
| 194 dict->SetString("level", |
| 195 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 196 EXPECT_TRUE(dict->Equals( |
| 197 recommended_reader_->GetPolicyStatus(GetParam().type()))); |
| 198 } |
| 199 |
| 200 INSTANTIATE_TEST_CASE_P( |
| 201 ConfigurationPolicyReaderBooleanTestInstance, |
| 202 ConfigurationPolicyReaderBooleanTest, |
| 203 testing::Values( |
| 204 TypeAndName(kPolicyHomepageIsNewTabPage, |
| 205 key::kHomepageIsNewTabPage), |
| 206 TypeAndName(kPolicyAlternateErrorPagesEnabled, |
| 207 key::kAlternateErrorPagesEnabled), |
| 208 TypeAndName(kPolicySearchSuggestEnabled, |
| 209 key::kSearchSuggestEnabled), |
| 210 TypeAndName(kPolicyDnsPrefetchingEnabled, |
| 211 key::kDnsPrefetchingEnabled), |
| 212 TypeAndName(kPolicyDisableSpdy, |
| 213 key::kDisableSpdy), |
| 214 TypeAndName(kPolicySafeBrowsingEnabled, |
| 215 key::kSafeBrowsingEnabled), |
| 216 TypeAndName(kPolicyMetricsReportingEnabled, |
| 217 key::kMetricsReportingEnabled), |
| 218 TypeAndName(kPolicyPasswordManagerEnabled, |
| 219 key::kPasswordManagerEnabled), |
| 220 TypeAndName(kPolicyPasswordManagerAllowShowPasswords, |
| 221 key::kPasswordManagerAllowShowPasswords), |
| 222 TypeAndName(kPolicyShowHomeButton, |
| 223 key::kShowHomeButton), |
| 224 TypeAndName(kPolicyPrintingEnabled, |
| 225 key::kPrintingEnabled), |
| 226 TypeAndName(kPolicyJavascriptEnabled, |
| 227 key::kJavascriptEnabled), |
| 228 TypeAndName(kPolicyRemoteAccessClientFirewallTraversal, |
| 229 key::kRemoteAccessClientFirewallTraversal), |
| 230 TypeAndName(kPolicyRemoteAccessHostFirewallTraversal, |
| 231 key::kRemoteAccessHostFirewallTraversal), |
| 232 TypeAndName(kPolicyCloudPrintProxyEnabled, |
| 233 key::kCloudPrintProxyEnabled), |
| 234 TypeAndName(kPolicySavingBrowserHistoryDisabled, |
| 235 key::kSavingBrowserHistoryDisabled), |
| 236 TypeAndName(kPolicySavingBrowserHistoryDisabled, |
| 237 key::kSavingBrowserHistoryDisabled), |
| 238 TypeAndName(kPolicyDisableAuthNegotiateCnameLookup, |
| 239 key::kDisableAuthNegotiateCnameLookup), |
| 240 TypeAndName(kPolicyEnableAuthNegotiatePort, |
| 241 key::kEnableAuthNegotiatePort), |
| 242 TypeAndName(kPolicyInstantEnabled, |
| 243 key::kInstantEnabled), |
| 244 TypeAndName(kPolicyDisablePluginFinder, |
| 245 key::kDisablePluginFinder), |
| 246 TypeAndName(kPolicyClearSiteDataOnExit, |
| 247 key::kClearSiteDataOnExit), |
| 248 TypeAndName(kPolicyDefaultBrowserSettingEnabled, |
| 249 key::kDefaultBrowserSettingEnabled), |
| 250 TypeAndName(kPolicyDisable3DAPIs, |
| 251 key::kDisable3DAPIs), |
| 252 TypeAndName(kPolicyTranslateEnabled, |
| 253 key::kTranslateEnabled), |
| 254 TypeAndName(kPolicyAllowOutdatedPlugins, |
| 255 key::kAllowOutdatedPlugins), |
| 256 TypeAndName(kPolicyAlwaysAuthorizePlugins, |
| 257 key::kAlwaysAuthorizePlugins), |
| 258 TypeAndName(kPolicyBookmarkBarEnabled, |
| 259 key::kBookmarkBarEnabled), |
| 260 TypeAndName(kPolicyEditBookmarksEnabled, |
| 261 key::kEditBookmarksEnabled), |
| 262 TypeAndName(kPolicyAllowFileSelectionDialogs, |
| 263 key::kAllowFileSelectionDialogs), |
| 264 TypeAndName(kPolicyAllowCrossOriginAuthPrompt, |
| 265 key::kAllowCrossOriginAuthPrompt))); |
| 266 |
| 267 #if defined(OS_CHROMEOS) |
| 268 INSTANTIATE_TEST_CASE_P( |
| 269 CrosConfigurationPolicyReaderBooleanTestInstance, |
| 270 ConfigurationPolicyReaderBooleanTest, |
| 271 testing::Values( |
| 272 TypeAndName(kPolicyChromeOsLockOnIdleSuspend, |
| 273 key::kEnableScreenLock))); |
| 274 #endif // defined(OS_CHROMEOS) |
| 275 |
| 276 // Test cases for integer-valued policy settings. |
| 277 class ConfigurationPolicyReaderIntegerTest : |
| 278 public ConfigurationPolicyReaderTestBase< |
| 279 testing::TestWithParam<TypeAndName> > { |
| 280 }; |
| 281 |
| 282 TEST_P(ConfigurationPolicyReaderIntegerTest, GetDefault) { |
| 283 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(GetParam().type())); |
| 284 } |
| 285 |
| 286 TEST_P(ConfigurationPolicyReaderIntegerTest, SetValue) { |
| 287 provider_.AddPolicy(GetParam().type(), Value::CreateIntegerValue(3)); |
| 288 managed_reader_->OnUpdatePolicy(); |
| 289 scoped_ptr<DictionaryValue> dict(CreateDictionary(GetParam())); |
| 290 dict->Set("value", Value::CreateIntegerValue(3)); |
| 291 |
| 292 EXPECT_TRUE(dict->Equals( |
| 293 managed_reader_->GetPolicyStatus(GetParam().type()))); |
| 294 |
| 295 recommended_reader_->OnUpdatePolicy(); |
| 296 dict->SetString("level", |
| 297 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); |
| 298 EXPECT_TRUE(dict->Equals( |
| 299 recommended_reader_->GetPolicyStatus(GetParam().type()))); |
| 300 } |
| 301 |
| 302 INSTANTIATE_TEST_CASE_P( |
| 303 ConfigurationPolicyReaderIntegerTestInstance, |
| 304 ConfigurationPolicyReaderIntegerTest, |
| 305 testing::Values( |
| 306 TypeAndName(kPolicyRestoreOnStartup, |
| 307 key::kRestoreOnStartup), |
| 308 TypeAndName(kPolicyPolicyRefreshRate, |
| 309 key::kPolicyRefreshRate), |
| 310 TypeAndName(kPolicyMaxConnectionsPerProxy, |
| 311 key::kMaxConnectionsPerProxy))); |
| 312 |
| 313 } // namespace policy |
OLD | NEW |