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. 4 5 #include <string> 6 7 #include "base/callback.h" 8 #include "base/files/file_path.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/prefs/pref_store_observer_mock.h" 12 #include "base/run_loop.h" 13 #include "chrome/browser/policy/configuration_policy_handler.h" 14 #include "chrome/browser/policy/configuration_policy_handler_list.h" 15 #include "chrome/browser/policy/configuration_policy_pref_store.h" 16 #include "chrome/browser/policy/external_data_fetcher.h" 17 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 18 #include "chrome/browser/policy/policy_map.h" 19 #include "chrome/browser/policy/policy_service_impl.h" 20 #include "chrome/browser/prefs/incognito_mode_prefs.h" 21 #include "chrome/browser/prefs/proxy_config_dictionary.h" 22 #include "chrome/common/content_settings.h" 23 #include "chrome/common/pref_names.h" 24 #include "policy/policy_constants.h" 25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gtest/include/gtest/gtest.h" 27 28 using testing::Mock; 29 using testing::Return; 30 using testing::_; 31 32 namespace policy { 33 34 // Holds a set of test parameters, consisting of pref name and policy name. 35 class PolicyAndPref { 36 public: 37 PolicyAndPref(const char* policy_name, const char* pref_name) 38 : policy_name_(policy_name), 39 pref_name_(pref_name) {} 40 41 const char* policy_name() const { return policy_name_; } 42 const char* pref_name() const { return pref_name_; } 43 44 private: 45 const char* policy_name_; 46 const char* pref_name_; 47 }; 48 49 class ConfigurationPolicyPrefStoreTest : public testing::Test { 50 protected: 51 ConfigurationPolicyPrefStoreTest() { 52 EXPECT_CALL(provider_, IsInitializationComplete(_)) 53 .WillRepeatedly(Return(false)); 54 provider_.Init(); 55 PolicyServiceImpl::Providers providers; 56 providers.push_back(&provider_); 57 policy_service_.reset(new PolicyServiceImpl(providers)); 58 store_ = new ConfigurationPolicyPrefStore(policy_service_.get(), 59 &handler_list_, 60 POLICY_LEVEL_MANDATORY); 61 } 62 63 virtual void TearDown() OVERRIDE { 64 provider_.Shutdown(); 65 } 66 67 void UpdateProviderPolicy(const PolicyMap& policy) { 68 provider_.UpdateChromePolicy(policy); 69 base::RunLoop loop; 70 loop.RunUntilIdle(); 71 } 72 73 ConfigurationPolicyHandlerList handler_list_; 74 MockConfigurationPolicyProvider provider_; 75 scoped_ptr<PolicyServiceImpl> policy_service_; 76 scoped_refptr<ConfigurationPolicyPrefStore> store_; 77 base::MessageLoop loop_; 78 }; 79 80 // Test cases for list-valued policy settings. 81 class ConfigurationPolicyPrefStoreListTest 82 : public ConfigurationPolicyPrefStoreTest, 83 public testing::WithParamInterface<PolicyAndPref> {}; 84 85 TEST_P(ConfigurationPolicyPrefStoreListTest, GetDefault) { 86 EXPECT_FALSE(store_->GetValue(GetParam().pref_name(), NULL)); 87 } 88 89 TEST_P(ConfigurationPolicyPrefStoreListTest, SetValue) { 90 base::ListValue* in_value = new base::ListValue(); 91 in_value->Append(base::Value::CreateStringValue("test1")); 92 in_value->Append(base::Value::CreateStringValue("test2,")); 93 PolicyMap policy; 94 policy.Set(GetParam().policy_name(), POLICY_LEVEL_MANDATORY, 95 POLICY_SCOPE_USER, in_value, NULL); 96 UpdateProviderPolicy(policy); 97 const base::Value* value = NULL; 98 EXPECT_TRUE(store_->GetValue(GetParam().pref_name(), &value)); 99 ASSERT_TRUE(value); 100 EXPECT_TRUE(in_value->Equals(value)); 101 } 102 103 INSTANTIATE_TEST_CASE_P( 104 ConfigurationPolicyPrefStoreListTestInstance, 105 ConfigurationPolicyPrefStoreListTest, 106 testing::Values( 107 PolicyAndPref(key::kRestoreOnStartupURLs, 108 prefs::kURLsToRestoreOnStartup), 109 PolicyAndPref(key::kDisabledPlugins, 110 prefs::kPluginsDisabledPlugins), 111 PolicyAndPref(key::kDisabledPluginsExceptions, 112 prefs::kPluginsDisabledPluginsExceptions), 113 PolicyAndPref(key::kEnabledPlugins, 114 prefs::kPluginsEnabledPlugins), 115 PolicyAndPref(key::kAutoSelectCertificateForUrls, 116 prefs::kManagedAutoSelectCertificateForUrls), 117 PolicyAndPref(key::kURLBlacklist, 118 prefs::kUrlBlacklist), 119 PolicyAndPref(key::kURLWhitelist, 120 prefs::kUrlWhitelist))); 121 122 // Test cases for string-valued policy settings. 123 class ConfigurationPolicyPrefStoreStringTest 124 : public ConfigurationPolicyPrefStoreTest, 125 public testing::WithParamInterface<PolicyAndPref> {}; 126 127 TEST_P(ConfigurationPolicyPrefStoreStringTest, GetDefault) { 128 EXPECT_FALSE(store_->GetValue(GetParam().pref_name(), NULL)); 129 } 130 131 TEST_P(ConfigurationPolicyPrefStoreStringTest, SetValue) { 132 PolicyMap policy; 133 policy.Set(GetParam().policy_name(), POLICY_LEVEL_MANDATORY, 134 POLICY_SCOPE_USER, 135 base::Value::CreateStringValue("http://chromium.org"), NULL); 136 UpdateProviderPolicy(policy); 137 const base::Value* value = NULL; 138 EXPECT_TRUE(store_->GetValue(GetParam().pref_name(), &value)); 139 ASSERT_TRUE(value); 140 EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value)); 141 } 142 143 INSTANTIATE_TEST_CASE_P( 144 ConfigurationPolicyPrefStoreStringTestInstance, 145 ConfigurationPolicyPrefStoreStringTest, 146 testing::Values( 147 PolicyAndPref(key::kRestrictSigninToPattern, 148 prefs::kGoogleServicesUsernamePattern), 149 PolicyAndPref(key::kHomepageLocation, 150 prefs::kHomePage), 151 PolicyAndPref(key::kApplicationLocaleValue, 152 prefs::kApplicationLocale), 153 PolicyAndPref(key::kAuthSchemes, 154 prefs::kAuthSchemes), 155 PolicyAndPref(key::kAuthServerWhitelist, 156 prefs::kAuthServerWhitelist), 157 PolicyAndPref(key::kAuthNegotiateDelegateWhitelist, 158 prefs::kAuthNegotiateDelegateWhitelist), 159 PolicyAndPref(key::kGSSAPILibraryName, 160 prefs::kGSSAPILibraryName), 161 PolicyAndPref(key::kVariationsRestrictParameter, 162 prefs::kVariationsRestrictParameter))); 163 164 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 165 INSTANTIATE_TEST_CASE_P( 166 ConfigurationPolicyPrefStoreDownloadDirectoryInstance, 167 ConfigurationPolicyPrefStoreStringTest, 168 testing::Values( 169 PolicyAndPref(key::kDiskCacheDir, 170 prefs::kDiskCacheDir), 171 PolicyAndPref(key::kDownloadDirectory, 172 prefs::kDownloadDefaultDirectory))); 173 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 174 175 // Test cases for boolean-valued policy settings. 176 class ConfigurationPolicyPrefStoreBooleanTest 177 : public ConfigurationPolicyPrefStoreTest, 178 public testing::WithParamInterface<PolicyAndPref> {}; 179 180 TEST_P(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) { 181 EXPECT_FALSE(store_->GetValue(GetParam().pref_name(), NULL)); 182 } 183 184 TEST_P(ConfigurationPolicyPrefStoreBooleanTest, SetValue) { 185 PolicyMap policy; 186 policy.Set(GetParam().policy_name(), POLICY_LEVEL_MANDATORY, 187 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); 188 UpdateProviderPolicy(policy); 189 const base::Value* value = NULL; 190 EXPECT_TRUE(store_->GetValue(GetParam().pref_name(), &value)); 191 ASSERT_TRUE(value); 192 bool boolean_value = true; 193 bool result = value->GetAsBoolean(&boolean_value); 194 ASSERT_TRUE(result); 195 EXPECT_FALSE(boolean_value); 196 197 policy.Set(GetParam().policy_name(), POLICY_LEVEL_MANDATORY, 198 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); 199 UpdateProviderPolicy(policy); 200 value = NULL; 201 EXPECT_TRUE(store_->GetValue(GetParam().pref_name(), &value)); 202 boolean_value = false; 203 result = value->GetAsBoolean(&boolean_value); 204 ASSERT_TRUE(result); 205 EXPECT_TRUE(boolean_value); 206 } 207 208 INSTANTIATE_TEST_CASE_P( 209 ConfigurationPolicyPrefStoreBooleanTestInstance, 210 ConfigurationPolicyPrefStoreBooleanTest, 211 testing::Values( 212 PolicyAndPref(key::kHomepageIsNewTabPage, 213 prefs::kHomePageIsNewTabPage), 214 PolicyAndPref(key::kAlternateErrorPagesEnabled, 215 prefs::kAlternateErrorPagesEnabled), 216 PolicyAndPref(key::kSearchSuggestEnabled, 217 prefs::kSearchSuggestEnabled), 218 PolicyAndPref(key::kDnsPrefetchingEnabled, 219 prefs::kNetworkPredictionEnabled), 220 PolicyAndPref(key::kBuiltInDnsClientEnabled, 221 prefs::kBuiltInDnsClientEnabled), 222 PolicyAndPref(key::kDisableSpdy, 223 prefs::kDisableSpdy), 224 PolicyAndPref(key::kSafeBrowsingEnabled, 225 prefs::kSafeBrowsingEnabled), 226 PolicyAndPref(key::kForceSafeSearch, 227 prefs::kForceSafeSearch), 228 PolicyAndPref(key::kMetricsReportingEnabled, 229 prefs::kMetricsReportingEnabled), 230 PolicyAndPref(key::kPasswordManagerEnabled, 231 prefs::kPasswordManagerEnabled), 232 PolicyAndPref(key::kPasswordManagerAllowShowPasswords, 233 prefs::kPasswordManagerAllowShowPasswords), 234 PolicyAndPref(key::kShowHomeButton, 235 prefs::kShowHomeButton), 236 PolicyAndPref(key::kPrintingEnabled, 237 prefs::kPrintingEnabled), 238 PolicyAndPref(key::kRemoteAccessHostFirewallTraversal, 239 prefs::kRemoteAccessHostFirewallTraversal), 240 PolicyAndPref(key::kCloudPrintProxyEnabled, 241 prefs::kCloudPrintProxyEnabled), 242 PolicyAndPref(key::kCloudPrintSubmitEnabled, 243 prefs::kCloudPrintSubmitEnabled), 244 PolicyAndPref(key::kSavingBrowserHistoryDisabled, 245 prefs::kSavingBrowserHistoryDisabled), 246 PolicyAndPref(key::kEnableOriginBoundCerts, 247 prefs::kEnableOriginBoundCerts), 248 PolicyAndPref(key::kDisableSSLRecordSplitting, 249 prefs::kDisableSSLRecordSplitting), 250 PolicyAndPref(key::kEnableOnlineRevocationChecks, 251 prefs::kCertRevocationCheckingEnabled), 252 PolicyAndPref(key::kRequireOnlineRevocationChecksForLocalAnchors, 253 prefs::kCertRevocationCheckingRequiredLocalAnchors), 254 PolicyAndPref(key::kDisableAuthNegotiateCnameLookup, 255 prefs::kDisableAuthNegotiateCnameLookup), 256 PolicyAndPref(key::kEnableAuthNegotiatePort, 257 prefs::kEnableAuthNegotiatePort), 258 PolicyAndPref(key::kDisablePluginFinder, 259 prefs::kDisablePluginFinder), 260 PolicyAndPref(key::kDefaultBrowserSettingEnabled, 261 prefs::kDefaultBrowserSettingEnabled), 262 PolicyAndPref(key::kDisable3DAPIs, 263 prefs::kDisable3DAPIs), 264 PolicyAndPref(key::kTranslateEnabled, 265 prefs::kEnableTranslate), 266 PolicyAndPref(key::kAllowOutdatedPlugins, 267 prefs::kPluginsAllowOutdated), 268 PolicyAndPref(key::kAlwaysAuthorizePlugins, 269 prefs::kPluginsAlwaysAuthorize), 270 PolicyAndPref(key::kBookmarkBarEnabled, 271 prefs::kShowBookmarkBar), 272 PolicyAndPref(key::kEditBookmarksEnabled, 273 prefs::kEditBookmarksEnabled), 274 PolicyAndPref(key::kAllowFileSelectionDialogs, 275 prefs::kAllowFileSelectionDialogs), 276 PolicyAndPref(key::kAllowCrossOriginAuthPrompt, 277 prefs::kAllowCrossOriginAuthPrompt), 278 PolicyAndPref(key::kImportBookmarks, 279 prefs::kImportBookmarks), 280 PolicyAndPref(key::kImportHistory, 281 prefs::kImportHistory), 282 PolicyAndPref(key::kImportHomepage, 283 prefs::kImportHomepage), 284 PolicyAndPref(key::kImportSearchEngine, 285 prefs::kImportSearchEngine), 286 PolicyAndPref(key::kImportSavedPasswords, 287 prefs::kImportSavedPasswords), 288 PolicyAndPref(key::kEnableMemoryInfo, 289 prefs::kEnableMemoryInfo), 290 PolicyAndPref(key::kDisablePrintPreview, 291 prefs::kPrintPreviewDisabled), 292 PolicyAndPref(key::kDeveloperToolsDisabled, 293 prefs::kDevToolsDisabled), 294 PolicyAndPref(key::kHideWebStoreIcon, 295 prefs::kHideWebStoreIcon))); 296 297 #if defined(OS_CHROMEOS) 298 INSTANTIATE_TEST_CASE_P( 299 CrosConfigurationPolicyPrefStoreBooleanTestInstance, 300 ConfigurationPolicyPrefStoreBooleanTest, 301 testing::Values( 302 PolicyAndPref(key::kChromeOsLockOnIdleSuspend, 303 prefs::kEnableScreenLock), 304 PolicyAndPref(key::kDriveDisabled, 305 prefs::kDisableDrive), 306 PolicyAndPref(key::kDriveDisabledOverCellular, 307 prefs::kDisableDriveOverCellular), 308 PolicyAndPref(key::kExternalStorageDisabled, 309 prefs::kExternalStorageDisabled), 310 PolicyAndPref(key::kShowAccessibilityOptionsInSystemTrayMenu, 311 prefs::kShouldAlwaysShowAccessibilityMenu), 312 PolicyAndPref(key::kLargeCursorEnabled, 313 prefs::kLargeCursorEnabled), 314 PolicyAndPref(key::kSpokenFeedbackEnabled, 315 prefs::kSpokenFeedbackEnabled), 316 PolicyAndPref(key::kHighContrastEnabled, 317 prefs::kHighContrastEnabled), 318 PolicyAndPref(key::kAudioOutputAllowed, 319 prefs::kAudioOutputAllowed), 320 PolicyAndPref(key::kAudioCaptureAllowed, 321 prefs::kAudioCaptureAllowed), 322 PolicyAndPref(key::kAttestationEnabledForUser, 323 prefs::kAttestationEnabled))); 324 #endif // defined(OS_CHROMEOS) 325 326 // Test cases for integer-valued policy settings. 327 class ConfigurationPolicyPrefStoreIntegerTest 328 : public ConfigurationPolicyPrefStoreTest, 329 public testing::WithParamInterface<PolicyAndPref> {}; 330 331 TEST_P(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) { 332 EXPECT_FALSE(store_->GetValue(GetParam().pref_name(), NULL)); 333 } 334 335 TEST_P(ConfigurationPolicyPrefStoreIntegerTest, SetValue) { 336 PolicyMap policy; 337 policy.Set(GetParam().policy_name(), POLICY_LEVEL_MANDATORY, 338 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(2), NULL); 339 UpdateProviderPolicy(policy); 340 const base::Value* value = NULL; 341 EXPECT_TRUE(store_->GetValue(GetParam().pref_name(), &value)); 342 EXPECT_TRUE(base::FundamentalValue(2).Equals(value)); 343 } 344 345 INSTANTIATE_TEST_CASE_P( 346 ConfigurationPolicyPrefStoreIntegerTestInstance, 347 ConfigurationPolicyPrefStoreIntegerTest, 348 testing::Values( 349 PolicyAndPref(key::kDefaultCookiesSetting, 350 prefs::kManagedDefaultCookiesSetting), 351 PolicyAndPref(key::kDefaultImagesSetting, 352 prefs::kManagedDefaultImagesSetting), 353 PolicyAndPref(key::kDefaultPluginsSetting, 354 prefs::kManagedDefaultPluginsSetting), 355 PolicyAndPref(key::kDefaultPopupsSetting, 356 prefs::kManagedDefaultPopupsSetting), 357 PolicyAndPref(key::kDefaultNotificationsSetting, 358 prefs::kManagedDefaultNotificationsSetting), 359 PolicyAndPref(key::kDefaultGeolocationSetting, 360 prefs::kManagedDefaultGeolocationSetting), 361 PolicyAndPref(key::kRestoreOnStartup, 362 prefs::kRestoreOnStartup), 363 PolicyAndPref(key::kDiskCacheSize, 364 prefs::kDiskCacheSize), 365 PolicyAndPref(key::kMediaCacheSize, 366 prefs::kMediaCacheSize), 367 PolicyAndPref(key::kPolicyRefreshRate, 368 prefs::kUserPolicyRefreshRate), 369 PolicyAndPref(key::kMaxConnectionsPerProxy, 370 prefs::kMaxConnectionsPerProxy))); 371 372 // Test cases for the proxy policy settings. 373 class ConfigurationPolicyPrefStoreProxyTest 374 : public ConfigurationPolicyPrefStoreTest { 375 protected: 376 // Verify that all the proxy prefs are set to the specified expected values. 377 void VerifyProxyPrefs( 378 const std::string& expected_proxy_server, 379 const std::string& expected_proxy_pac_url, 380 const std::string& expected_proxy_bypass_list, 381 const ProxyPrefs::ProxyMode& expected_proxy_mode) { 382 const base::Value* value = NULL; 383 ASSERT_TRUE(store_->GetValue(prefs::kProxy, &value)); 384 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType()); 385 ProxyConfigDictionary dict( 386 static_cast<const base::DictionaryValue*>(value)); 387 std::string s; 388 if (expected_proxy_server.empty()) { 389 EXPECT_FALSE(dict.GetProxyServer(&s)); 390 } else { 391 ASSERT_TRUE(dict.GetProxyServer(&s)); 392 EXPECT_EQ(expected_proxy_server, s); 393 } 394 if (expected_proxy_pac_url.empty()) { 395 EXPECT_FALSE(dict.GetPacUrl(&s)); 396 } else { 397 ASSERT_TRUE(dict.GetPacUrl(&s)); 398 EXPECT_EQ(expected_proxy_pac_url, s); 399 } 400 if (expected_proxy_bypass_list.empty()) { 401 EXPECT_FALSE(dict.GetBypassList(&s)); 402 } else { 403 ASSERT_TRUE(dict.GetBypassList(&s)); 404 EXPECT_EQ(expected_proxy_bypass_list, s); 405 } 406 ProxyPrefs::ProxyMode mode; 407 ASSERT_TRUE(dict.GetMode(&mode)); 408 EXPECT_EQ(expected_proxy_mode, mode); 409 } 410 }; 411 412 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptions) { 413 PolicyMap policy; 414 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 415 base::Value::CreateStringValue("http://chromium.org/override"), 416 NULL); 417 policy.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 418 base::Value::CreateStringValue("chromium.org"), NULL); 419 policy.Set( 420 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 421 base::Value::CreateIntegerValue( 422 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE), 423 NULL); 424 UpdateProviderPolicy(policy); 425 426 VerifyProxyPrefs("chromium.org", 427 std::string(), 428 "http://chromium.org/override", 429 ProxyPrefs::MODE_FIXED_SERVERS); 430 } 431 432 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptionsReversedApplyOrder) { 433 PolicyMap policy; 434 policy.Set( 435 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 436 base::Value::CreateIntegerValue( 437 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE), 438 NULL); 439 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 440 base::Value::CreateStringValue("http://chromium.org/override"), 441 NULL); 442 policy.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 443 base::Value::CreateStringValue("chromium.org"), NULL); 444 UpdateProviderPolicy(policy); 445 446 VerifyProxyPrefs("chromium.org", 447 std::string(), 448 "http://chromium.org/override", 449 ProxyPrefs::MODE_FIXED_SERVERS); 450 } 451 452 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptionsInvalid) { 453 PolicyMap policy; 454 policy.Set( 455 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 456 base::Value::CreateIntegerValue( 457 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE), 458 NULL); 459 UpdateProviderPolicy(policy); 460 461 const base::Value* value = NULL; 462 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value)); 463 } 464 465 466 TEST_F(ConfigurationPolicyPrefStoreProxyTest, NoProxyServerMode) { 467 PolicyMap policy; 468 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 469 base::Value::CreateIntegerValue( 470 ProxyPolicyHandler::PROXY_SERVER_MODE), 471 NULL); 472 UpdateProviderPolicy(policy); 473 VerifyProxyPrefs( 474 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT); 475 } 476 477 TEST_F(ConfigurationPolicyPrefStoreProxyTest, NoProxyModeName) { 478 PolicyMap policy; 479 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 480 base::Value::CreateStringValue(ProxyPrefs::kDirectProxyModeName), 481 NULL); 482 UpdateProviderPolicy(policy); 483 VerifyProxyPrefs( 484 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT); 485 } 486 487 TEST_F(ConfigurationPolicyPrefStoreProxyTest, AutoDetectProxyServerMode) { 488 PolicyMap policy; 489 policy.Set( 490 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 491 base::Value::CreateIntegerValue( 492 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE), 493 NULL); 494 UpdateProviderPolicy(policy); 495 VerifyProxyPrefs(std::string(), 496 std::string(), 497 std::string(), 498 ProxyPrefs::MODE_AUTO_DETECT); 499 } 500 501 TEST_F(ConfigurationPolicyPrefStoreProxyTest, AutoDetectProxyModeName) { 502 PolicyMap policy; 503 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 504 base::Value::CreateStringValue( 505 ProxyPrefs::kAutoDetectProxyModeName), 506 NULL); 507 UpdateProviderPolicy(policy); 508 VerifyProxyPrefs(std::string(), 509 std::string(), 510 std::string(), 511 ProxyPrefs::MODE_AUTO_DETECT); 512 } 513 514 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyMode) { 515 PolicyMap policy; 516 policy.Set(key::kProxyPacUrl, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 517 base::Value::CreateStringValue("http://short.org/proxy.pac"), 518 NULL); 519 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 520 base::Value::CreateStringValue( 521 ProxyPrefs::kPacScriptProxyModeName), 522 NULL); 523 UpdateProviderPolicy(policy); 524 VerifyProxyPrefs(std::string(), 525 "http://short.org/proxy.pac", 526 std::string(), 527 ProxyPrefs::MODE_PAC_SCRIPT); 528 } 529 530 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyModeInvalid) { 531 PolicyMap policy; 532 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 533 base::Value::CreateStringValue( 534 ProxyPrefs::kPacScriptProxyModeName), 535 NULL); 536 UpdateProviderPolicy(policy); 537 const base::Value* value = NULL; 538 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value)); 539 } 540 541 // Regression test for http://crbug.com/78016, CPanel returns empty strings 542 // for unset properties. 543 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyModeBug78016) { 544 PolicyMap policy; 545 policy.Set(key::kProxyServer, 546 POLICY_LEVEL_MANDATORY, 547 POLICY_SCOPE_USER, 548 base::Value::CreateStringValue(std::string()), 549 NULL); 550 policy.Set(key::kProxyPacUrl, 551 POLICY_LEVEL_MANDATORY, 552 POLICY_SCOPE_USER, 553 base::Value::CreateStringValue("http://short.org/proxy.pac"), 554 NULL); 555 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 556 base::Value::CreateStringValue( 557 ProxyPrefs::kPacScriptProxyModeName), 558 NULL); 559 UpdateProviderPolicy(policy); 560 VerifyProxyPrefs(std::string(), 561 "http://short.org/proxy.pac", 562 std::string(), 563 ProxyPrefs::MODE_PAC_SCRIPT); 564 } 565 566 TEST_F(ConfigurationPolicyPrefStoreProxyTest, UseSystemProxyServerMode) { 567 PolicyMap policy; 568 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 569 base::Value::CreateIntegerValue( 570 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE), 571 NULL); 572 UpdateProviderPolicy(policy); 573 VerifyProxyPrefs( 574 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM); 575 } 576 577 TEST_F(ConfigurationPolicyPrefStoreProxyTest, UseSystemProxyMode) { 578 PolicyMap policy; 579 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 580 base::Value::CreateStringValue(ProxyPrefs::kSystemProxyModeName), 581 NULL); 582 UpdateProviderPolicy(policy); 583 VerifyProxyPrefs( 584 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM); 585 } 586 587 TEST_F(ConfigurationPolicyPrefStoreProxyTest, 588 ProxyModeOverridesProxyServerMode) { 589 PolicyMap policy; 590 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 591 base::Value::CreateIntegerValue( 592 ProxyPolicyHandler::PROXY_SERVER_MODE), 593 NULL); 594 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 595 base::Value::CreateStringValue( 596 ProxyPrefs::kAutoDetectProxyModeName), 597 NULL); 598 UpdateProviderPolicy(policy); 599 VerifyProxyPrefs(std::string(), 600 std::string(), 601 std::string(), 602 ProxyPrefs::MODE_AUTO_DETECT); 603 } 604 605 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ProxyInvalid) { 606 // No mode expects all three parameters being set. 607 PolicyMap policy; 608 policy.Set(key::kProxyPacUrl, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 609 base::Value::CreateStringValue("http://short.org/proxy.pac"), 610 NULL); 611 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 612 base::Value::CreateStringValue("http://chromium.org/override"), 613 NULL); 614 policy.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 615 base::Value::CreateStringValue("chromium.org"), NULL); 616 for (int i = 0; i < ProxyPolicyHandler::MODE_COUNT; ++i) { 617 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, 618 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(i), NULL); 619 UpdateProviderPolicy(policy); 620 const base::Value* value = NULL; 621 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value)); 622 } 623 } 624 625 class ConfigurationPolicyPrefStoreDefaultSearchTest 626 : public ConfigurationPolicyPrefStoreTest { 627 public: 628 ConfigurationPolicyPrefStoreDefaultSearchTest() { 629 default_alternate_urls_.AppendString( 630 "http://www.google.com/#q={searchTerms}"); 631 default_alternate_urls_.AppendString( 632 "http://www.google.com/search#q={searchTerms}"); 633 } 634 635 protected: 636 static const char* const kSearchURL; 637 static const char* const kSuggestURL; 638 static const char* const kIconURL; 639 static const char* const kName; 640 static const char* const kKeyword; 641 static const char* const kReplacementKey; 642 static const char* const kImageURL; 643 static const char* const kImageParams; 644 645 // Build a default search policy by setting search-related keys in |policy| to 646 // reasonable values. You can update any of the keys after calling this 647 // method. 648 void BuildDefaultSearchPolicy(PolicyMap* policy); 649 650 base::ListValue default_alternate_urls_; 651 }; 652 653 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kSearchURL = 654 "http://test.com/search?t={searchTerms}"; 655 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kSuggestURL = 656 "http://test.com/sugg?={searchTerms}"; 657 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kIconURL = 658 "http://test.com/icon.jpg"; 659 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kName = 660 "MyName"; 661 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kKeyword = 662 "MyKeyword"; 663 const char* const 664 ConfigurationPolicyPrefStoreDefaultSearchTest::kReplacementKey = "espv"; 665 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kImageURL = 666 "http://test.com/searchbyimage/upload"; 667 const char* const ConfigurationPolicyPrefStoreDefaultSearchTest::kImageParams = 668 "image_content=content,image_url=http://test.com/test.png"; 669 670 void ConfigurationPolicyPrefStoreDefaultSearchTest:: 671 BuildDefaultSearchPolicy(PolicyMap* policy) { 672 base::ListValue* encodings = new base::ListValue(); 673 encodings->AppendString("UTF-16"); 674 encodings->AppendString("UTF-8"); 675 policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, 676 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); 677 policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, 678 POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), 679 NULL); 680 policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY, 681 POLICY_SCOPE_USER, base::Value::CreateStringValue(kName), NULL); 682 policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY, 683 POLICY_SCOPE_USER, base::Value::CreateStringValue(kKeyword), 684 NULL); 685 policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY, 686 POLICY_SCOPE_USER, base::Value::CreateStringValue(kSuggestURL), 687 NULL); 688 policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY, 689 POLICY_SCOPE_USER, base::Value::CreateStringValue(kIconURL), 690 NULL); 691 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, 692 POLICY_SCOPE_USER, encodings, NULL); 693 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, 694 POLICY_SCOPE_USER, default_alternate_urls_.DeepCopy(), NULL); 695 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, 696 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 697 base::Value::CreateStringValue(kReplacementKey), NULL); 698 policy->Set(key::kDefaultSearchProviderImageURL, 699 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 700 base::Value::CreateStringValue(kImageURL), NULL); 701 policy->Set(key::kDefaultSearchProviderImageURLPostParams, 702 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 703 base::Value::CreateStringValue(kImageParams), NULL); 704 } 705 706 // Checks that if the policy for default search is valid, i.e. there's a 707 // search URL, that all the elements have been given proper defaults. 708 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MinimallyDefined) { 709 PolicyMap policy; 710 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, 711 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); 712 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, 713 POLICY_SCOPE_USER, base::Value::CreateStringValue(kSearchURL), 714 NULL); 715 UpdateProviderPolicy(policy); 716 717 const base::Value* value = NULL; 718 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); 719 EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); 720 721 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); 722 EXPECT_TRUE(base::StringValue("test.com").Equals(value)); 723 724 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); 725 EXPECT_TRUE(base::StringValue("test.com").Equals(value)); 726 727 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, 728 &value)); 729 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 730 731 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); 732 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 733 734 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); 735 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 736 737 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderInstantURL, 738 &value)); 739 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 740 741 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, 742 &value)); 743 EXPECT_TRUE(base::ListValue().Equals(value)); 744 745 EXPECT_TRUE( 746 store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, 747 &value)); 748 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 749 750 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); 751 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 752 753 EXPECT_TRUE(store_->GetValue( 754 prefs::kDefaultSearchProviderSearchURLPostParams, &value)); 755 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 756 757 EXPECT_TRUE(store_->GetValue( 758 prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); 759 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 760 761 EXPECT_TRUE(store_->GetValue( 762 prefs::kDefaultSearchProviderInstantURLPostParams, &value)); 763 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 764 765 EXPECT_TRUE(store_->GetValue( 766 prefs::kDefaultSearchProviderImageURLPostParams, &value)); 767 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 768 } 769 770 // Checks that for a fully defined search policy, all elements have been 771 // read properly. 772 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, FullyDefined) { 773 PolicyMap policy; 774 BuildDefaultSearchPolicy(&policy); 775 UpdateProviderPolicy(policy); 776 777 const base::Value* value = NULL; 778 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); 779 EXPECT_TRUE(base::StringValue(kSearchURL).Equals(value)); 780 781 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderName, &value)); 782 EXPECT_TRUE(base::StringValue(kName).Equals(value)); 783 784 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, &value)); 785 EXPECT_TRUE(base::StringValue(kKeyword).Equals(value)); 786 787 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, 788 &value)); 789 EXPECT_TRUE(base::StringValue(kSuggestURL).Equals(value)); 790 791 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, &value)); 792 EXPECT_TRUE(base::StringValue(kIconURL).Equals(value)); 793 794 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, &value)); 795 EXPECT_TRUE(base::StringValue("UTF-16;UTF-8").Equals(value)); 796 797 EXPECT_TRUE(store_->GetValue( 798 prefs::kDefaultSearchProviderAlternateURLs, &value)); 799 EXPECT_TRUE(default_alternate_urls_.Equals(value)); 800 801 EXPECT_TRUE( 802 store_->GetValue(prefs::kDefaultSearchProviderSearchTermsReplacementKey, 803 &value)); 804 EXPECT_TRUE(base::StringValue(kReplacementKey).Equals(value)); 805 806 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, &value)); 807 EXPECT_TRUE(base::StringValue(std::string(kImageURL)).Equals(value)); 808 809 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderImageURLPostParams, 810 &value)); 811 EXPECT_TRUE(base::StringValue(std::string(kImageParams)).Equals(value)); 812 813 EXPECT_TRUE(store_->GetValue( 814 prefs::kDefaultSearchProviderSearchURLPostParams, &value)); 815 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 816 817 EXPECT_TRUE(store_->GetValue( 818 prefs::kDefaultSearchProviderSuggestURLPostParams, &value)); 819 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 820 821 EXPECT_TRUE(store_->GetValue( 822 prefs::kDefaultSearchProviderInstantURLPostParams, &value)); 823 EXPECT_TRUE(base::StringValue(std::string()).Equals(value)); 824 } 825 826 // Checks that if the default search policy is missing, that no elements of the 827 // default search policy will be present. 828 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MissingUrl) { 829 PolicyMap policy; 830 BuildDefaultSearchPolicy(&policy); 831 policy.Erase(key::kDefaultSearchProviderSearchURL); 832 UpdateProviderPolicy(policy); 833 834 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); 835 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); 836 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); 837 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); 838 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); 839 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); 840 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, 841 NULL)); 842 EXPECT_FALSE(store_->GetValue( 843 prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); 844 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); 845 EXPECT_FALSE(store_->GetValue( 846 prefs::kDefaultSearchProviderImageURLPostParams, NULL)); 847 } 848 849 // Checks that if the default search policy is invalid, that no elements of the 850 // default search policy will be present. 851 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, Invalid) { 852 PolicyMap policy; 853 BuildDefaultSearchPolicy(&policy); 854 const char* const bad_search_url = "http://test.com/noSearchTerms"; 855 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, 856 POLICY_SCOPE_USER, 857 base::Value::CreateStringValue(bad_search_url), NULL); 858 UpdateProviderPolicy(policy); 859 860 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL)); 861 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderName, NULL)); 862 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderKeyword, NULL)); 863 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL)); 864 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderIconURL, NULL)); 865 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderEncodings, NULL)); 866 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderAlternateURLs, 867 NULL)); 868 EXPECT_FALSE(store_->GetValue( 869 prefs::kDefaultSearchProviderSearchTermsReplacementKey, NULL)); 870 EXPECT_FALSE(store_->GetValue(prefs::kDefaultSearchProviderImageURL, NULL)); 871 EXPECT_FALSE(store_->GetValue( 872 prefs::kDefaultSearchProviderImageURLPostParams, NULL)); 873 } 874 875 // Checks that if the default search policy is invalid, that no elements of the 876 // default search policy will be present. 877 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, Disabled) { 878 PolicyMap policy; 879 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, 880 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); 881 UpdateProviderPolicy(policy); 882 883 const base::Value* value = NULL; 884 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderEnabled, &value)); 885 base::FundamentalValue expected_enabled(false); 886 EXPECT_TRUE(base::Value::Equals(&expected_enabled, value)); 887 EXPECT_TRUE(store_->GetValue(prefs::kDefaultSearchProviderSearchURL, &value)); 888 base::StringValue expected_search_url((std::string())); 889 EXPECT_TRUE(base::Value::Equals(&expected_search_url, value)); 890 } 891 892 // Tests Incognito mode availability preference setting. 893 class ConfigurationPolicyPrefStoreIncognitoModeTest 894 : public ConfigurationPolicyPrefStoreTest { 895 protected: 896 static const int kIncognitoModeAvailabilityNotSet = -1; 897 898 enum ObsoleteIncognitoEnabledValue { 899 INCOGNITO_ENABLED_UNKNOWN, 900 INCOGNITO_ENABLED_TRUE, 901 INCOGNITO_ENABLED_FALSE 902 }; 903 904 void SetPolicies(ObsoleteIncognitoEnabledValue incognito_enabled, 905 int availability) { 906 PolicyMap policy; 907 if (incognito_enabled != INCOGNITO_ENABLED_UNKNOWN) { 908 policy.Set(key::kIncognitoEnabled, POLICY_LEVEL_MANDATORY, 909 POLICY_SCOPE_USER, 910 base::Value::CreateBooleanValue( 911 incognito_enabled == INCOGNITO_ENABLED_TRUE), 912 NULL); 913 } 914 if (availability >= 0) { 915 policy.Set(key::kIncognitoModeAvailability, POLICY_LEVEL_MANDATORY, 916 POLICY_SCOPE_USER, 917 base::Value::CreateIntegerValue(availability), 918 NULL); 919 } 920 UpdateProviderPolicy(policy); 921 } 922 923 void VerifyValues(IncognitoModePrefs::Availability availability) { 924 const base::Value* value = NULL; 925 EXPECT_TRUE(store_->GetValue(prefs::kIncognitoModeAvailability, &value)); 926 EXPECT_TRUE(base::FundamentalValue(availability).Equals(value)); 927 } 928 }; 929 930 // The following testcases verify that if the obsolete IncognitoEnabled 931 // policy is not set, the IncognitoModeAvailability values should be copied 932 // from IncognitoModeAvailability policy to pref "as is". 933 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 934 NoObsoletePolicyAndIncognitoEnabled) { 935 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::ENABLED); 936 VerifyValues(IncognitoModePrefs::ENABLED); 937 } 938 939 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 940 NoObsoletePolicyAndIncognitoDisabled) { 941 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::DISABLED); 942 VerifyValues(IncognitoModePrefs::DISABLED); 943 } 944 945 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 946 NoObsoletePolicyAndIncognitoForced) { 947 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::FORCED); 948 VerifyValues(IncognitoModePrefs::FORCED); 949 } 950 951 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 952 NoObsoletePolicyAndNoIncognitoAvailability) { 953 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, kIncognitoModeAvailabilityNotSet); 954 const base::Value* value = NULL; 955 EXPECT_FALSE(store_->GetValue(prefs::kIncognitoModeAvailability, &value)); 956 } 957 958 // Checks that if the obsolete IncognitoEnabled policy is set, if sets 959 // the IncognitoModeAvailability preference only in case 960 // the IncognitoModeAvailability policy is not specified. 961 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 962 ObsoletePolicyDoesNotAffectAvailabilityEnabled) { 963 SetPolicies(INCOGNITO_ENABLED_FALSE, IncognitoModePrefs::ENABLED); 964 VerifyValues(IncognitoModePrefs::ENABLED); 965 } 966 967 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 968 ObsoletePolicyDoesNotAffectAvailabilityForced) { 969 SetPolicies(INCOGNITO_ENABLED_TRUE, IncognitoModePrefs::FORCED); 970 VerifyValues(IncognitoModePrefs::FORCED); 971 } 972 973 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 974 ObsoletePolicySetsPreferenceToEnabled) { 975 SetPolicies(INCOGNITO_ENABLED_TRUE, kIncognitoModeAvailabilityNotSet); 976 VerifyValues(IncognitoModePrefs::ENABLED); 977 } 978 979 TEST_F(ConfigurationPolicyPrefStoreIncognitoModeTest, 980 ObsoletePolicySetsPreferenceToDisabled) { 981 SetPolicies(INCOGNITO_ENABLED_FALSE, kIncognitoModeAvailabilityNotSet); 982 VerifyValues(IncognitoModePrefs::DISABLED); 983 } 984 985 // Test cases for the Sync policy setting. 986 class ConfigurationPolicyPrefStoreSyncTest 987 : public ConfigurationPolicyPrefStoreTest {}; 988 989 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Default) { 990 EXPECT_FALSE(store_->GetValue(prefs::kSyncManaged, NULL)); 991 } 992 993 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Enabled) { 994 PolicyMap policy; 995 policy.Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 996 base::Value::CreateBooleanValue(false), NULL); 997 UpdateProviderPolicy(policy); 998 // Enabling Sync should not set the pref. 999 EXPECT_FALSE(store_->GetValue(prefs::kSyncManaged, NULL)); 1000 } 1001 1002 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Disabled) { 1003 PolicyMap policy; 1004 policy.Set(key::kSyncDisabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 1005 base::Value::CreateBooleanValue(true), NULL); 1006 UpdateProviderPolicy(policy); 1007 // Sync should be flagged as managed. 1008 const base::Value* value = NULL; 1009 EXPECT_TRUE(store_->GetValue(prefs::kSyncManaged, &value)); 1010 ASSERT_TRUE(value); 1011 bool sync_managed = false; 1012 bool result = value->GetAsBoolean(&sync_managed); 1013 ASSERT_TRUE(result); 1014 EXPECT_TRUE(sync_managed); 1015 } 1016 1017 // Test cases for how the DownloadDirectory and AllowFileSelectionDialogs policy 1018 // influence the PromptForDownload preference. 1019 class ConfigurationPolicyPrefStorePromptDownloadTest 1020 : public ConfigurationPolicyPrefStoreTest {}; 1021 1022 TEST_F(ConfigurationPolicyPrefStorePromptDownloadTest, Default) { 1023 EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); 1024 } 1025 1026 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 1027 TEST_F(ConfigurationPolicyPrefStorePromptDownloadTest, SetDownloadDirectory) { 1028 PolicyMap policy; 1029 EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); 1030 policy.Set(key::kDownloadDirectory, 1031 POLICY_LEVEL_MANDATORY, 1032 POLICY_SCOPE_USER, 1033 base::Value::CreateStringValue(std::string()), 1034 NULL); 1035 UpdateProviderPolicy(policy); 1036 1037 // Setting a DownloadDirectory should disable the PromptForDownload pref. 1038 const base::Value* value = NULL; 1039 EXPECT_TRUE(store_->GetValue(prefs::kPromptForDownload, 1040 &value)); 1041 ASSERT_TRUE(value); 1042 bool prompt_for_download = true; 1043 bool result = value->GetAsBoolean(&prompt_for_download); 1044 ASSERT_TRUE(result); 1045 EXPECT_FALSE(prompt_for_download); 1046 } 1047 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 1048 1049 TEST_F(ConfigurationPolicyPrefStorePromptDownloadTest, 1050 EnableFileSelectionDialogs) { 1051 PolicyMap policy; 1052 EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); 1053 policy.Set(key::kAllowFileSelectionDialogs, POLICY_LEVEL_MANDATORY, 1054 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); 1055 UpdateProviderPolicy(policy); 1056 1057 // Allowing file-selection dialogs should not influence the PromptForDownload 1058 // pref. 1059 EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); 1060 } 1061 1062 TEST_F(ConfigurationPolicyPrefStorePromptDownloadTest, 1063 DisableFileSelectionDialogs) { 1064 PolicyMap policy; 1065 EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); 1066 policy.Set(key::kAllowFileSelectionDialogs, POLICY_LEVEL_MANDATORY, 1067 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); 1068 UpdateProviderPolicy(policy); 1069 1070 // Disabling file-selection dialogs should disable the PromptForDownload pref. 1071 const base::Value* value = NULL; 1072 EXPECT_TRUE(store_->GetValue(prefs::kPromptForDownload, 1073 &value)); 1074 ASSERT_TRUE(value); 1075 bool prompt_for_download = true; 1076 bool result = value->GetAsBoolean(&prompt_for_download); 1077 ASSERT_TRUE(result); 1078 EXPECT_FALSE(prompt_for_download); 1079 } 1080 1081 // Test cases for the Autofill policy setting. 1082 class ConfigurationPolicyPrefStoreAutofillTest 1083 : public ConfigurationPolicyPrefStoreTest {}; 1084 1085 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Default) { 1086 EXPECT_FALSE(store_->GetValue(autofill::prefs::kAutofillEnabled, NULL)); 1087 } 1088 1089 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Enabled) { 1090 PolicyMap policy; 1091 policy.Set(key::kAutoFillEnabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 1092 base::Value::CreateBooleanValue(true), NULL); 1093 UpdateProviderPolicy(policy); 1094 // Enabling Autofill should not set the pref. 1095 EXPECT_FALSE(store_->GetValue(autofill::prefs::kAutofillEnabled, NULL)); 1096 } 1097 1098 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Disabled) { 1099 PolicyMap policy; 1100 policy.Set(key::kAutoFillEnabled, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 1101 base::Value::CreateBooleanValue(false), NULL); 1102 UpdateProviderPolicy(policy); 1103 // Disabling Autofill should switch the pref to managed. 1104 const base::Value* value = NULL; 1105 EXPECT_TRUE(store_->GetValue(autofill::prefs::kAutofillEnabled, &value)); 1106 ASSERT_TRUE(value); 1107 bool autofill_enabled = true; 1108 bool result = value->GetAsBoolean(&autofill_enabled); 1109 ASSERT_TRUE(result); 1110 EXPECT_FALSE(autofill_enabled); 1111 } 1112 1113 #if defined(OS_CHROMEOS) 1114 // Test cases for the screen magnifier type policy setting. 1115 class ConfigurationPolicyPrefStoreScreenMagnifierTypeTest 1116 : public ConfigurationPolicyPrefStoreTest {}; 1117 1118 TEST_F(ConfigurationPolicyPrefStoreScreenMagnifierTypeTest, Default) { 1119 EXPECT_FALSE(store_->GetValue(prefs::kScreenMagnifierEnabled, NULL)); 1120 EXPECT_FALSE(store_->GetValue(prefs::kScreenMagnifierType, NULL)); 1121 } 1122 1123 TEST_F(ConfigurationPolicyPrefStoreScreenMagnifierTypeTest, Disabled) { 1124 PolicyMap policy; 1125 policy.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY, 1126 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL); 1127 UpdateProviderPolicy(policy); 1128 const base::Value* enabled = NULL; 1129 EXPECT_TRUE(store_->GetValue(prefs::kScreenMagnifierEnabled, &enabled)); 1130 ASSERT_TRUE(enabled); 1131 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled)); 1132 const base::Value* type = NULL; 1133 EXPECT_TRUE(store_->GetValue(prefs::kScreenMagnifierType, &type)); 1134 ASSERT_TRUE(type); 1135 EXPECT_TRUE(base::FundamentalValue(0).Equals(type)); 1136 } 1137 1138 TEST_F(ConfigurationPolicyPrefStoreScreenMagnifierTypeTest, Enabled) { 1139 PolicyMap policy; 1140 policy.Set(key::kScreenMagnifierType, POLICY_LEVEL_MANDATORY, 1141 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(1), NULL); 1142 UpdateProviderPolicy(policy); 1143 const base::Value* enabled = NULL; 1144 EXPECT_TRUE(store_->GetValue(prefs::kScreenMagnifierEnabled, &enabled)); 1145 ASSERT_TRUE(enabled); 1146 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled)); 1147 const base::Value* type = NULL; 1148 EXPECT_TRUE(store_->GetValue(prefs::kScreenMagnifierType, &type)); 1149 ASSERT_TRUE(type); 1150 EXPECT_TRUE(base::FundamentalValue(1).Equals(type)); 1151 } 1152 #endif // defined(OS_CHROMEOS) 1153 1154 // Exercises the policy refresh mechanism. 1155 class ConfigurationPolicyPrefStoreRefreshTest 1156 : public ConfigurationPolicyPrefStoreTest { 1157 protected: 1158 virtual void SetUp() { 1159 ConfigurationPolicyPrefStoreTest::SetUp(); 1160 store_->AddObserver(&observer_); 1161 } 1162 1163 virtual void TearDown() { 1164 store_->RemoveObserver(&observer_); 1165 ConfigurationPolicyPrefStoreTest::TearDown(); 1166 } 1167 1168 PrefStoreObserverMock observer_; 1169 }; 1170 1171 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) { 1172 const base::Value* value = NULL; 1173 EXPECT_FALSE(store_->GetValue(prefs::kHomePage, NULL)); 1174 1175 EXPECT_CALL(observer_, OnPrefValueChanged(prefs::kHomePage)).Times(1); 1176 PolicyMap policy; 1177 policy.Set(key::kHomepageLocation, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 1178 base::Value::CreateStringValue("http://www.chromium.org"), NULL); 1179 UpdateProviderPolicy(policy); 1180 Mock::VerifyAndClearExpectations(&observer_); 1181 EXPECT_TRUE(store_->GetValue(prefs::kHomePage, &value)); 1182 EXPECT_TRUE(base::StringValue("http://www.chromium.org").Equals(value)); 1183 1184 EXPECT_CALL(observer_, OnPrefValueChanged(_)).Times(0); 1185 UpdateProviderPolicy(policy); 1186 Mock::VerifyAndClearExpectations(&observer_); 1187 1188 EXPECT_CALL(observer_, OnPrefValueChanged(prefs::kHomePage)).Times(1); 1189 policy.Erase(key::kHomepageLocation); 1190 UpdateProviderPolicy(policy); 1191 Mock::VerifyAndClearExpectations(&observer_); 1192 EXPECT_FALSE(store_->GetValue(prefs::kHomePage, NULL)); 1193 } 1194 1195 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Initialization) { 1196 EXPECT_FALSE(store_->IsInitializationComplete()); 1197 EXPECT_CALL(provider_, IsInitializationComplete(POLICY_DOMAIN_CHROME)) 1198 .WillRepeatedly(Return(true)); 1199 EXPECT_CALL(observer_, OnInitializationCompleted(true)).Times(1); 1200 PolicyMap policy; 1201 UpdateProviderPolicy(policy); 1202 Mock::VerifyAndClearExpectations(&observer_); 1203 EXPECT_TRUE(store_->IsInitializationComplete()); 1204 } 1205 1206 // Tests for policies that don't quite fit the previous patterns. 1207 class ConfigurationPolicyPrefStoreOthersTest 1208 : public ConfigurationPolicyPrefStoreTest {}; 1209 1210 TEST_F(ConfigurationPolicyPrefStoreOthersTest, JavascriptEnabled) { 1211 // This is a boolean policy, but affects an integer preference. 1212 EXPECT_FALSE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, NULL)); 1213 PolicyMap policy; 1214 policy.Set(key::kJavascriptEnabled, POLICY_LEVEL_MANDATORY, 1215 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL); 1216 UpdateProviderPolicy(policy); 1217 EXPECT_FALSE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, NULL)); 1218 policy.Set(key::kJavascriptEnabled, POLICY_LEVEL_MANDATORY, 1219 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); 1220 UpdateProviderPolicy(policy); 1221 const base::Value* value = NULL; 1222 EXPECT_TRUE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, 1223 &value)); 1224 EXPECT_TRUE(base::FundamentalValue(CONTENT_SETTING_BLOCK).Equals(value)); 1225 } 1226 1227 TEST_F(ConfigurationPolicyPrefStoreOthersTest, JavascriptEnabledOverridden) { 1228 EXPECT_FALSE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, NULL)); 1229 PolicyMap policy; 1230 policy.Set(key::kJavascriptEnabled, POLICY_LEVEL_MANDATORY, 1231 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL); 1232 UpdateProviderPolicy(policy); 1233 const base::Value* value = NULL; 1234 EXPECT_TRUE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, 1235 &value)); 1236 EXPECT_TRUE(base::FundamentalValue(CONTENT_SETTING_BLOCK).Equals(value)); 1237 // DefaultJavaScriptSetting overrides JavascriptEnabled. 1238 policy.Set(key::kDefaultJavaScriptSetting, POLICY_LEVEL_MANDATORY, 1239 POLICY_SCOPE_USER, 1240 base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW), 1241 NULL); 1242 UpdateProviderPolicy(policy); 1243 EXPECT_TRUE(store_->GetValue(prefs::kManagedDefaultJavaScriptSetting, 1244 &value)); 1245 EXPECT_TRUE(base::FundamentalValue(CONTENT_SETTING_ALLOW).Equals(value)); 1246 } 1247 1248 } // namespace policy 1249