Home | History | Annotate | Download | only in policy
      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/file_path.h"
      6 #include "base/memory/ref_counted.h"
      7 #include "chrome/browser/policy/configuration_policy_pref_store.h"
      8 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
      9 #include "chrome/browser/prefs/proxy_config_dictionary.h"
     10 #include "chrome/common/pref_names.h"
     11 #include "chrome/common/pref_store_observer_mock.h"
     12 #include "content/common/notification_service.h"
     13 #include "testing/gmock/include/gmock/gmock.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 using testing::_;
     17 using testing::Mock;
     18 
     19 namespace policy {
     20 
     21 // Holds a set of test parameters, consisting of pref name and policy type.
     22 class TypeAndName {
     23  public:
     24   TypeAndName(ConfigurationPolicyType type, const char* pref_name)
     25       : type_(type),
     26         pref_name_(pref_name) {}
     27 
     28   ConfigurationPolicyType type() const { return type_; }
     29   const char* pref_name() const { return pref_name_; }
     30 
     31  private:
     32   ConfigurationPolicyType type_;
     33   const char* pref_name_;
     34 };
     35 
     36 template<typename TESTBASE>
     37 class ConfigurationPolicyPrefStoreTestBase : public TESTBASE {
     38  protected:
     39   ConfigurationPolicyPrefStoreTestBase()
     40       : provider_(),
     41         store_(new ConfigurationPolicyPrefStore(&provider_)) {}
     42 
     43   MockConfigurationPolicyProvider provider_;
     44   scoped_refptr<ConfigurationPolicyPrefStore> store_;
     45 };
     46 
     47 // Test cases for list-valued policy settings.
     48 class ConfigurationPolicyPrefStoreListTest
     49     : public ConfigurationPolicyPrefStoreTestBase<
     50                  testing::TestWithParam<TypeAndName> > {
     51 };
     52 
     53 TEST_P(ConfigurationPolicyPrefStoreListTest, GetDefault) {
     54   EXPECT_EQ(PrefStore::READ_NO_VALUE,
     55             store_->GetValue(GetParam().pref_name(), NULL));
     56 }
     57 
     58 TEST_P(ConfigurationPolicyPrefStoreListTest, SetValue) {
     59   ListValue* in_value = new ListValue();
     60   in_value->Append(Value::CreateStringValue("test1"));
     61   in_value->Append(Value::CreateStringValue("test2,"));
     62   provider_.AddPolicy(GetParam().type(), in_value);
     63   store_->OnUpdatePolicy();
     64   const Value* value;
     65   EXPECT_EQ(PrefStore::READ_OK,
     66             store_->GetValue(GetParam().pref_name(), &value));
     67   EXPECT_TRUE(in_value->Equals(value));
     68 }
     69 
     70 INSTANTIATE_TEST_CASE_P(
     71     ConfigurationPolicyPrefStoreListTestInstance,
     72     ConfigurationPolicyPrefStoreListTest,
     73     testing::Values(
     74         TypeAndName(kPolicyRestoreOnStartupURLs,
     75                     prefs::kURLsToRestoreOnStartup),
     76         TypeAndName(kPolicyExtensionInstallWhitelist,
     77                     prefs::kExtensionInstallAllowList),
     78         TypeAndName(kPolicyExtensionInstallBlacklist,
     79                     prefs::kExtensionInstallDenyList),
     80         TypeAndName(kPolicyDisabledPlugins,
     81                     prefs::kPluginsDisabledPlugins),
     82         TypeAndName(kPolicyDisabledPluginsExceptions,
     83                     prefs::kPluginsDisabledPluginsExceptions),
     84         TypeAndName(kPolicyEnabledPlugins,
     85                     prefs::kPluginsEnabledPlugins),
     86         TypeAndName(kPolicyDisabledSchemes,
     87                     prefs::kDisabledSchemes)));
     88 
     89 // Test cases for string-valued policy settings.
     90 class ConfigurationPolicyPrefStoreStringTest
     91     : public ConfigurationPolicyPrefStoreTestBase<
     92                  testing::TestWithParam<TypeAndName> > {
     93 };
     94 
     95 TEST_P(ConfigurationPolicyPrefStoreStringTest, GetDefault) {
     96   EXPECT_EQ(PrefStore::READ_NO_VALUE,
     97             store_->GetValue(GetParam().pref_name(), NULL));
     98 }
     99 
    100 TEST_P(ConfigurationPolicyPrefStoreStringTest, SetValue) {
    101   provider_.AddPolicy(GetParam().type(),
    102                       Value::CreateStringValue("http://chromium.org"));
    103   store_->OnUpdatePolicy();
    104   const Value* value;
    105   EXPECT_EQ(PrefStore::READ_OK,
    106             store_->GetValue(GetParam().pref_name(), &value));
    107   EXPECT_TRUE(StringValue("http://chromium.org").Equals(value));
    108 }
    109 
    110 INSTANTIATE_TEST_CASE_P(
    111     ConfigurationPolicyPrefStoreStringTestInstance,
    112     ConfigurationPolicyPrefStoreStringTest,
    113     testing::Values(
    114         TypeAndName(kPolicyHomepageLocation,
    115                     prefs::kHomePage),
    116         TypeAndName(kPolicyApplicationLocaleValue,
    117                     prefs::kApplicationLocale),
    118         TypeAndName(kPolicyApplicationLocaleValue,
    119                     prefs::kApplicationLocale),
    120         TypeAndName(kPolicyAuthSchemes,
    121                     prefs::kAuthSchemes),
    122         TypeAndName(kPolicyAuthServerWhitelist,
    123                     prefs::kAuthServerWhitelist),
    124         TypeAndName(kPolicyAuthNegotiateDelegateWhitelist,
    125                     prefs::kAuthNegotiateDelegateWhitelist),
    126         TypeAndName(kPolicyDownloadDirectory,
    127                     prefs::kDownloadDefaultDirectory),
    128         TypeAndName(kPolicyGSSAPILibraryName,
    129                     prefs::kGSSAPILibraryName)));
    130 
    131 // Test cases for boolean-valued policy settings.
    132 class ConfigurationPolicyPrefStoreBooleanTest
    133     : public ConfigurationPolicyPrefStoreTestBase<
    134                  testing::TestWithParam<TypeAndName> > {
    135 };
    136 
    137 TEST_P(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) {
    138   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    139             store_->GetValue(GetParam().pref_name(), NULL));
    140 }
    141 
    142 TEST_P(ConfigurationPolicyPrefStoreBooleanTest, SetValue) {
    143   provider_.AddPolicy(GetParam().type(), Value::CreateBooleanValue(false));
    144   store_->OnUpdatePolicy();
    145   const Value* value;
    146   bool result = true;
    147   EXPECT_EQ(PrefStore::READ_OK,
    148             store_->GetValue(GetParam().pref_name(), &value));
    149   EXPECT_TRUE(FundamentalValue(false).Equals(value));
    150 
    151   provider_.AddPolicy(GetParam().type(), Value::CreateBooleanValue(true));
    152   store_->OnUpdatePolicy();
    153   result = false;
    154   EXPECT_EQ(PrefStore::READ_OK,
    155             store_->GetValue(GetParam().pref_name(), &value));
    156   EXPECT_TRUE(FundamentalValue(true).Equals(value));
    157 }
    158 
    159 INSTANTIATE_TEST_CASE_P(
    160     ConfigurationPolicyPrefStoreBooleanTestInstance,
    161     ConfigurationPolicyPrefStoreBooleanTest,
    162     testing::Values(
    163         TypeAndName(kPolicyHomepageIsNewTabPage,
    164                     prefs::kHomePageIsNewTabPage),
    165         TypeAndName(kPolicyAlternateErrorPagesEnabled,
    166                     prefs::kAlternateErrorPagesEnabled),
    167         TypeAndName(kPolicySearchSuggestEnabled,
    168                     prefs::kSearchSuggestEnabled),
    169         TypeAndName(kPolicyDnsPrefetchingEnabled,
    170                     prefs::kNetworkPredictionEnabled),
    171         TypeAndName(kPolicyDisableSpdy,
    172                     prefs::kDisableSpdy),
    173         TypeAndName(kPolicySafeBrowsingEnabled,
    174                     prefs::kSafeBrowsingEnabled),
    175         TypeAndName(kPolicyMetricsReportingEnabled,
    176                     prefs::kMetricsReportingEnabled),
    177         TypeAndName(kPolicyPasswordManagerEnabled,
    178                     prefs::kPasswordManagerEnabled),
    179         TypeAndName(kPolicyPasswordManagerAllowShowPasswords,
    180                     prefs::kPasswordManagerAllowShowPasswords),
    181         TypeAndName(kPolicyShowHomeButton,
    182                     prefs::kShowHomeButton),
    183         TypeAndName(kPolicyPrintingEnabled,
    184                     prefs::kPrintingEnabled),
    185         TypeAndName(kPolicyJavascriptEnabled,
    186                     prefs::kWebKitJavascriptEnabled),
    187         TypeAndName(kPolicyIncognitoEnabled,
    188                     prefs::kIncognitoEnabled),
    189         TypeAndName(kPolicyCloudPrintProxyEnabled,
    190                     prefs::kCloudPrintProxyEnabled),
    191         TypeAndName(kPolicySavingBrowserHistoryDisabled,
    192                     prefs::kSavingBrowserHistoryDisabled),
    193         TypeAndName(kPolicySavingBrowserHistoryDisabled,
    194                     prefs::kSavingBrowserHistoryDisabled),
    195         TypeAndName(kPolicyDisableAuthNegotiateCnameLookup,
    196                     prefs::kDisableAuthNegotiateCnameLookup),
    197         TypeAndName(kPolicyEnableAuthNegotiatePort,
    198                     prefs::kEnableAuthNegotiatePort),
    199         TypeAndName(kPolicyInstantEnabled,
    200                     prefs::kInstantEnabled),
    201         TypeAndName(kPolicyDisablePluginFinder,
    202                     prefs::kDisablePluginFinder),
    203         TypeAndName(kPolicyClearSiteDataOnExit,
    204                     prefs::kClearSiteDataOnExit),
    205         TypeAndName(kPolicyDefaultBrowserSettingEnabled,
    206                     prefs::kDefaultBrowserSettingEnabled),
    207         TypeAndName(kPolicyDisable3DAPIs,
    208                     prefs::kDisable3DAPIs),
    209         TypeAndName(kPolicyTranslateEnabled,
    210                     prefs::kEnableTranslate),
    211         TypeAndName(kPolicyAllowOutdatedPlugins,
    212                     prefs::kPluginsAllowOutdated),
    213         TypeAndName(kPolicyBookmarkBarEnabled,
    214                     prefs::kEnableBookmarkBar),
    215         TypeAndName(kPolicyEditBookmarksEnabled,
    216                     prefs::kEditBookmarksEnabled),
    217         TypeAndName(kPolicyAllowFileSelectionDialogs,
    218                     prefs::kAllowFileSelectionDialogs)));
    219 
    220 #if defined(OS_CHROMEOS)
    221 INSTANTIATE_TEST_CASE_P(
    222     CrosConfigurationPolicyPrefStoreBooleanTestInstance,
    223     ConfigurationPolicyPrefStoreBooleanTest,
    224     testing::Values(
    225         TypeAndName(kPolicyChromeOsLockOnIdleSuspend,
    226                     prefs::kEnableScreenLock)));
    227 #endif  // defined(OS_CHROMEOS)
    228 
    229 // Test cases for integer-valued policy settings.
    230 class ConfigurationPolicyPrefStoreIntegerTest
    231     : public ConfigurationPolicyPrefStoreTestBase<
    232                  testing::TestWithParam<TypeAndName> > {
    233 };
    234 
    235 TEST_P(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) {
    236   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    237             store_->GetValue(GetParam().pref_name(), NULL));
    238 }
    239 
    240 TEST_P(ConfigurationPolicyPrefStoreIntegerTest, SetValue) {
    241   provider_.AddPolicy(GetParam().type(), Value::CreateIntegerValue(2));
    242   store_->OnUpdatePolicy();
    243   const Value* value = NULL;
    244   EXPECT_EQ(PrefStore::READ_OK,
    245             store_->GetValue(GetParam().pref_name(), &value));
    246   EXPECT_TRUE(FundamentalValue(2).Equals(value));
    247 }
    248 
    249 INSTANTIATE_TEST_CASE_P(
    250     ConfigurationPolicyPrefStoreIntegerTestInstance,
    251     ConfigurationPolicyPrefStoreIntegerTest,
    252     testing::Values(
    253         TypeAndName(kPolicyRestoreOnStartup,
    254                     prefs::kRestoreOnStartup),
    255         TypeAndName(kPolicyPolicyRefreshRate,
    256                     prefs::kPolicyRefreshRate)));
    257 
    258 // Test cases for the proxy policy settings.
    259 class ConfigurationPolicyPrefStoreProxyTest : public testing::Test {
    260  protected:
    261   // Verify that all the proxy prefs are set to the specified expected values.
    262   static void VerifyProxyPrefs(
    263       const ConfigurationPolicyPrefStore& store,
    264       const std::string& expected_proxy_server,
    265       const std::string& expected_proxy_pac_url,
    266       const std::string& expected_proxy_bypass_list,
    267       const ProxyPrefs::ProxyMode& expected_proxy_mode) {
    268     const Value* value = NULL;
    269     ASSERT_EQ(PrefStore::READ_OK,
    270               store.GetValue(prefs::kProxy, &value));
    271     ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType());
    272     ProxyConfigDictionary dict(static_cast<const DictionaryValue*>(value));
    273     std::string s;
    274     if (expected_proxy_server.empty()) {
    275       EXPECT_FALSE(dict.GetProxyServer(&s));
    276     } else {
    277       ASSERT_TRUE(dict.GetProxyServer(&s));
    278       EXPECT_EQ(expected_proxy_server, s);
    279     }
    280     if (expected_proxy_pac_url.empty()) {
    281       EXPECT_FALSE(dict.GetPacUrl(&s));
    282     } else {
    283       ASSERT_TRUE(dict.GetPacUrl(&s));
    284       EXPECT_EQ(expected_proxy_pac_url, s);
    285     }
    286     if (expected_proxy_bypass_list.empty()) {
    287       EXPECT_FALSE(dict.GetBypassList(&s));
    288     } else {
    289       ASSERT_TRUE(dict.GetBypassList(&s));
    290       EXPECT_EQ(expected_proxy_bypass_list, s);
    291     }
    292     ProxyPrefs::ProxyMode mode;
    293     ASSERT_TRUE(dict.GetMode(&mode));
    294     EXPECT_EQ(expected_proxy_mode, mode);
    295   }
    296 };
    297 
    298 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptions) {
    299   MockConfigurationPolicyProvider provider;
    300   provider.AddPolicy(kPolicyProxyBypassList,
    301                      Value::CreateStringValue("http://chromium.org/override"));
    302   provider.AddPolicy(kPolicyProxyServer,
    303                      Value::CreateStringValue("chromium.org"));
    304   provider.AddPolicy(kPolicyProxyServerMode,
    305                      Value::CreateIntegerValue(
    306                          kPolicyManuallyConfiguredProxyServerMode));
    307 
    308   scoped_refptr<ConfigurationPolicyPrefStore> store(
    309       new ConfigurationPolicyPrefStore(&provider));
    310   VerifyProxyPrefs(
    311       *store, "chromium.org", "", "http://chromium.org/override",
    312       ProxyPrefs::MODE_FIXED_SERVERS);
    313 }
    314 
    315 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptionsReversedApplyOrder) {
    316   MockConfigurationPolicyProvider provider;
    317   provider.AddPolicy(kPolicyProxyServerMode,
    318                      Value::CreateIntegerValue(
    319                          kPolicyManuallyConfiguredProxyServerMode));
    320   provider.AddPolicy(kPolicyProxyBypassList,
    321                      Value::CreateStringValue("http://chromium.org/override"));
    322   provider.AddPolicy(kPolicyProxyServer,
    323                      Value::CreateStringValue("chromium.org"));
    324   scoped_refptr<ConfigurationPolicyPrefStore> store(
    325       new ConfigurationPolicyPrefStore(&provider));
    326   VerifyProxyPrefs(
    327       *store, "chromium.org", "", "http://chromium.org/override",
    328       ProxyPrefs::MODE_FIXED_SERVERS);
    329 }
    330 
    331 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ManualOptionsInvalid) {
    332   MockConfigurationPolicyProvider provider;
    333   provider.AddPolicy(kPolicyProxyServerMode,
    334                      Value::CreateIntegerValue(
    335                          kPolicyManuallyConfiguredProxyServerMode));
    336 
    337   scoped_refptr<ConfigurationPolicyPrefStore> store(
    338       new ConfigurationPolicyPrefStore(&provider));
    339   const Value* value = NULL;
    340   EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(prefs::kProxy, &value));
    341 }
    342 
    343 
    344 TEST_F(ConfigurationPolicyPrefStoreProxyTest, NoProxyServerMode) {
    345   MockConfigurationPolicyProvider provider;
    346   provider.AddPolicy(kPolicyProxyServerMode,
    347                      Value::CreateIntegerValue(kPolicyNoProxyServerMode));
    348 
    349   scoped_refptr<ConfigurationPolicyPrefStore> store(
    350       new ConfigurationPolicyPrefStore(&provider));
    351   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_DIRECT);
    352 }
    353 
    354 TEST_F(ConfigurationPolicyPrefStoreProxyTest, NoProxyModeName) {
    355   MockConfigurationPolicyProvider provider;
    356   provider.AddPolicy(
    357       kPolicyProxyMode,
    358       Value::CreateStringValue(ProxyPrefs::kDirectProxyModeName));
    359 
    360   scoped_refptr<ConfigurationPolicyPrefStore> store(
    361       new ConfigurationPolicyPrefStore(&provider));
    362   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_DIRECT);
    363 }
    364 
    365 TEST_F(ConfigurationPolicyPrefStoreProxyTest, AutoDetectProxyServerMode) {
    366   MockConfigurationPolicyProvider provider;
    367   provider.AddPolicy(
    368       kPolicyProxyServerMode,
    369       Value::CreateIntegerValue(kPolicyAutoDetectProxyServerMode));
    370 
    371   scoped_refptr<ConfigurationPolicyPrefStore> store(
    372       new ConfigurationPolicyPrefStore(&provider));
    373   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_AUTO_DETECT);
    374 }
    375 
    376 TEST_F(ConfigurationPolicyPrefStoreProxyTest, AutoDetectProxyModeName) {
    377   MockConfigurationPolicyProvider provider;
    378   provider.AddPolicy(
    379       kPolicyProxyMode,
    380       Value::CreateStringValue(ProxyPrefs::kAutoDetectProxyModeName));
    381 
    382   scoped_refptr<ConfigurationPolicyPrefStore> store(
    383       new ConfigurationPolicyPrefStore(&provider));
    384   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_AUTO_DETECT);
    385 }
    386 
    387 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyMode) {
    388   MockConfigurationPolicyProvider provider;
    389   provider.AddPolicy(kPolicyProxyPacUrl,
    390                      Value::CreateStringValue("http://short.org/proxy.pac"));
    391   provider.AddPolicy(
    392       kPolicyProxyMode,
    393       Value::CreateStringValue(ProxyPrefs::kPacScriptProxyModeName));
    394 
    395   scoped_refptr<ConfigurationPolicyPrefStore> store(
    396       new ConfigurationPolicyPrefStore(&provider));
    397   VerifyProxyPrefs(*store, "", "http://short.org/proxy.pac", "",
    398                    ProxyPrefs::MODE_PAC_SCRIPT);
    399 }
    400 
    401 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyModeInvalid) {
    402   MockConfigurationPolicyProvider provider;
    403   provider.AddPolicy(
    404       kPolicyProxyMode,
    405       Value::CreateStringValue(ProxyPrefs::kPacScriptProxyModeName));
    406 
    407   scoped_refptr<ConfigurationPolicyPrefStore> store(
    408       new ConfigurationPolicyPrefStore(&provider));
    409   const Value* value = NULL;
    410   EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(prefs::kProxy, &value));
    411 }
    412 
    413 // Regression test for http://crbug.com/78016, CPanel returns empty strings
    414 // for unset properties.
    415 TEST_F(ConfigurationPolicyPrefStoreProxyTest, PacScriptProxyModeBug78016) {
    416   MockConfigurationPolicyProvider provider;
    417   provider.AddPolicy(kPolicyProxyServer,
    418                      Value::CreateStringValue(""));
    419   provider.AddPolicy(kPolicyProxyPacUrl,
    420                      Value::CreateStringValue("http://short.org/proxy.pac"));
    421   provider.AddPolicy(
    422       kPolicyProxyMode,
    423       Value::CreateStringValue(ProxyPrefs::kPacScriptProxyModeName));
    424 
    425   scoped_refptr<ConfigurationPolicyPrefStore> store(
    426       new ConfigurationPolicyPrefStore(&provider));
    427   VerifyProxyPrefs(*store, "", "http://short.org/proxy.pac", "",
    428                    ProxyPrefs::MODE_PAC_SCRIPT);
    429 }
    430 
    431 TEST_F(ConfigurationPolicyPrefStoreProxyTest, UseSystemProxyServerMode) {
    432   MockConfigurationPolicyProvider provider;
    433   provider.AddPolicy(
    434       kPolicyProxyServerMode,
    435       Value::CreateIntegerValue(kPolicyUseSystemProxyServerMode));
    436 
    437   scoped_refptr<ConfigurationPolicyPrefStore> store(
    438       new ConfigurationPolicyPrefStore(&provider));
    439   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_SYSTEM);
    440 }
    441 
    442 TEST_F(ConfigurationPolicyPrefStoreProxyTest, UseSystemProxyMode) {
    443   MockConfigurationPolicyProvider provider;
    444   provider.AddPolicy(
    445       kPolicyProxyMode,
    446       Value::CreateStringValue(ProxyPrefs::kSystemProxyModeName));
    447 
    448   scoped_refptr<ConfigurationPolicyPrefStore> store(
    449       new ConfigurationPolicyPrefStore(&provider));
    450   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_SYSTEM);
    451 }
    452 
    453 TEST_F(ConfigurationPolicyPrefStoreProxyTest,
    454        ProxyModeOverridesProxyServerMode) {
    455   MockConfigurationPolicyProvider provider;
    456   provider.AddPolicy(kPolicyProxyServerMode,
    457                      Value::CreateIntegerValue(kPolicyNoProxyServerMode));
    458   provider.AddPolicy(
    459       kPolicyProxyMode,
    460       Value::CreateStringValue(ProxyPrefs::kAutoDetectProxyModeName));
    461 
    462   scoped_refptr<ConfigurationPolicyPrefStore> store(
    463       new ConfigurationPolicyPrefStore(&provider));
    464   VerifyProxyPrefs(*store, "", "", "", ProxyPrefs::MODE_AUTO_DETECT);
    465 }
    466 
    467 TEST_F(ConfigurationPolicyPrefStoreProxyTest, ProxyInvalid) {
    468   for (int i = 0; i < MODE_COUNT; ++i) {
    469     MockConfigurationPolicyProvider provider;
    470     provider.AddPolicy(kPolicyProxyServerMode, Value::CreateIntegerValue(i));
    471     // No mode expects all three parameters being set.
    472     provider.AddPolicy(kPolicyProxyPacUrl,
    473                        Value::CreateStringValue("http://short.org/proxy.pac"));
    474     provider.AddPolicy(kPolicyProxyBypassList,
    475                        Value::CreateStringValue(
    476                            "http://chromium.org/override"));
    477     provider.AddPolicy(kPolicyProxyServer,
    478                        Value::CreateStringValue("chromium.org"));
    479 
    480     scoped_refptr<ConfigurationPolicyPrefStore> store(
    481         new ConfigurationPolicyPrefStore(&provider));
    482     const Value* value = NULL;
    483     EXPECT_EQ(PrefStore::READ_NO_VALUE,
    484               store->GetValue(prefs::kProxy, &value));
    485   }
    486 }
    487 
    488 class ConfigurationPolicyPrefStoreDefaultSearchTest : public testing::Test {
    489 };
    490 
    491 // Checks that if the policy for default search is valid, i.e. there's a
    492 // search URL, that all the elements have been given proper defaults.
    493 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MinimallyDefined) {
    494   const char* const search_url = "http://test.com/search?t={searchTerms}";
    495   MockConfigurationPolicyProvider provider;
    496   provider.AddPolicy(kPolicyDefaultSearchProviderEnabled,
    497                      Value::CreateBooleanValue(true));
    498   provider.AddPolicy(kPolicyDefaultSearchProviderSearchURL,
    499                      Value::CreateStringValue(search_url));
    500 
    501   scoped_refptr<ConfigurationPolicyPrefStore> store(
    502       new ConfigurationPolicyPrefStore(&provider));
    503 
    504   const Value* value = NULL;
    505   EXPECT_EQ(PrefStore::READ_OK,
    506             store->GetValue(prefs::kDefaultSearchProviderSearchURL, &value));
    507   EXPECT_TRUE(StringValue(search_url).Equals(value));
    508 
    509   EXPECT_EQ(PrefStore::READ_OK,
    510             store->GetValue(prefs::kDefaultSearchProviderName, &value));
    511   EXPECT_TRUE(StringValue("test.com").Equals(value));
    512 
    513   EXPECT_EQ(PrefStore::READ_OK,
    514             store->GetValue(prefs::kDefaultSearchProviderKeyword, &value));
    515   EXPECT_TRUE(StringValue(std::string()).Equals(value));
    516 
    517   EXPECT_EQ(PrefStore::READ_OK,
    518             store->GetValue(prefs::kDefaultSearchProviderSuggestURL, &value));
    519   EXPECT_TRUE(StringValue(std::string()).Equals(value));
    520 
    521   EXPECT_EQ(PrefStore::READ_OK,
    522             store->GetValue(prefs::kDefaultSearchProviderIconURL, &value));
    523   EXPECT_TRUE(StringValue(std::string()).Equals(value));
    524 
    525   EXPECT_EQ(PrefStore::READ_OK,
    526             store->GetValue(prefs::kDefaultSearchProviderEncodings, &value));
    527   EXPECT_TRUE(StringValue(std::string()).Equals(value));
    528 
    529   EXPECT_EQ(PrefStore::READ_OK,
    530             store->GetValue(prefs::kDefaultSearchProviderInstantURL, &value));
    531   EXPECT_TRUE(StringValue(std::string()).Equals(value));
    532 }
    533 
    534 // Checks that for a fully defined search policy, all elements have been
    535 // read properly.
    536 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, FullyDefined) {
    537   const char* const search_url = "http://test.com/search?t={searchTerms}";
    538   const char* const suggest_url = "http://test.com/sugg?={searchTerms}";
    539   const char* const icon_url = "http://test.com/icon.jpg";
    540   const char* const name = "MyName";
    541   const char* const keyword = "MyKeyword";
    542   const char* const encodings = "UTF-16;UTF-8";
    543   MockConfigurationPolicyProvider provider;
    544   provider.AddPolicy(kPolicyDefaultSearchProviderEnabled,
    545                      Value::CreateBooleanValue(true));
    546   provider.AddPolicy(kPolicyDefaultSearchProviderSearchURL,
    547                      Value::CreateStringValue(search_url));
    548   provider.AddPolicy(kPolicyDefaultSearchProviderName,
    549                      Value::CreateStringValue(name));
    550   provider.AddPolicy(kPolicyDefaultSearchProviderKeyword,
    551                      Value::CreateStringValue(keyword));
    552   provider.AddPolicy(kPolicyDefaultSearchProviderSuggestURL,
    553                      Value::CreateStringValue(suggest_url));
    554   provider.AddPolicy(kPolicyDefaultSearchProviderIconURL,
    555                      Value::CreateStringValue(icon_url));
    556   provider.AddPolicy(kPolicyDefaultSearchProviderEncodings,
    557                      Value::CreateStringValue(encodings));
    558 
    559   scoped_refptr<ConfigurationPolicyPrefStore> store(
    560       new ConfigurationPolicyPrefStore(&provider));
    561 
    562   const Value* value = NULL;
    563   EXPECT_EQ(PrefStore::READ_OK,
    564             store->GetValue(prefs::kDefaultSearchProviderSearchURL, &value));
    565   EXPECT_TRUE(StringValue(search_url).Equals(value));
    566 
    567   EXPECT_EQ(PrefStore::READ_OK,
    568             store->GetValue(prefs::kDefaultSearchProviderName, &value));
    569   EXPECT_TRUE(StringValue(name).Equals(value));
    570 
    571   EXPECT_EQ(PrefStore::READ_OK,
    572             store->GetValue(prefs::kDefaultSearchProviderKeyword, &value));
    573   EXPECT_TRUE(StringValue(keyword).Equals(value));
    574 
    575   EXPECT_EQ(PrefStore::READ_OK,
    576             store->GetValue(prefs::kDefaultSearchProviderSuggestURL, &value));
    577   EXPECT_TRUE(StringValue(suggest_url).Equals(value));
    578 
    579   EXPECT_EQ(PrefStore::READ_OK,
    580             store->GetValue(prefs::kDefaultSearchProviderIconURL, &value));
    581   EXPECT_TRUE(StringValue(icon_url).Equals(value));
    582 
    583   EXPECT_EQ(PrefStore::READ_OK,
    584             store->GetValue(prefs::kDefaultSearchProviderEncodings, &value));
    585   EXPECT_TRUE(StringValue(encodings).Equals(value));
    586 }
    587 
    588 // Checks that if the default search policy is missing, that no elements of the
    589 // default search policy will be present.
    590 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, MissingUrl) {
    591   const char* const suggest_url = "http://test.com/sugg?t={searchTerms}";
    592   const char* const icon_url = "http://test.com/icon.jpg";
    593   const char* const name = "MyName";
    594   const char* const keyword = "MyKeyword";
    595   const char* const encodings = "UTF-16;UTF-8";
    596   MockConfigurationPolicyProvider provider;
    597   provider.AddPolicy(kPolicyDefaultSearchProviderEnabled,
    598                      Value::CreateBooleanValue(true));
    599   provider.AddPolicy(kPolicyDefaultSearchProviderName,
    600                      Value::CreateStringValue(name));
    601   provider.AddPolicy(kPolicyDefaultSearchProviderKeyword,
    602                      Value::CreateStringValue(keyword));
    603   provider.AddPolicy(kPolicyDefaultSearchProviderSuggestURL,
    604                      Value::CreateStringValue(suggest_url));
    605   provider.AddPolicy(kPolicyDefaultSearchProviderIconURL,
    606                      Value::CreateStringValue(icon_url));
    607   provider.AddPolicy(kPolicyDefaultSearchProviderEncodings,
    608                      Value::CreateStringValue(encodings));
    609 
    610   scoped_refptr<ConfigurationPolicyPrefStore> store(
    611       new ConfigurationPolicyPrefStore(&provider));
    612 
    613   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    614             store->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL));
    615   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    616             store->GetValue(prefs::kDefaultSearchProviderName, NULL));
    617   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    618             store->GetValue(prefs::kDefaultSearchProviderKeyword, NULL));
    619   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    620             store->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL));
    621   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    622             store->GetValue(prefs::kDefaultSearchProviderIconURL, NULL));
    623   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    624             store->GetValue(prefs::kDefaultSearchProviderEncodings, NULL));
    625 }
    626 
    627 // Checks that if the default search policy is invalid, that no elements of the
    628 // default search policy will be present.
    629 TEST_F(ConfigurationPolicyPrefStoreDefaultSearchTest, Invalid) {
    630   const char* const bad_search_url = "http://test.com/noSearchTerms";
    631   const char* const suggest_url = "http://test.com/sugg?t={searchTerms}";
    632   const char* const icon_url = "http://test.com/icon.jpg";
    633   const char* const name = "MyName";
    634   const char* const keyword = "MyKeyword";
    635   const char* const encodings = "UTF-16;UTF-8";
    636   MockConfigurationPolicyProvider provider;
    637   provider.AddPolicy(kPolicyDefaultSearchProviderEnabled,
    638                      Value::CreateBooleanValue(true));
    639   provider.AddPolicy(kPolicyDefaultSearchProviderSearchURL,
    640                      Value::CreateStringValue(bad_search_url));
    641   provider.AddPolicy(kPolicyDefaultSearchProviderName,
    642                      Value::CreateStringValue(name));
    643   provider.AddPolicy(kPolicyDefaultSearchProviderKeyword,
    644                      Value::CreateStringValue(keyword));
    645   provider.AddPolicy(kPolicyDefaultSearchProviderSuggestURL,
    646                      Value::CreateStringValue(suggest_url));
    647   provider.AddPolicy(kPolicyDefaultSearchProviderIconURL,
    648                      Value::CreateStringValue(icon_url));
    649   provider.AddPolicy(kPolicyDefaultSearchProviderEncodings,
    650                      Value::CreateStringValue(encodings));
    651 
    652   scoped_refptr<ConfigurationPolicyPrefStore> store(
    653       new ConfigurationPolicyPrefStore(&provider));
    654 
    655   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    656             store->GetValue(prefs::kDefaultSearchProviderSearchURL, NULL));
    657   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    658             store->GetValue(prefs::kDefaultSearchProviderName, NULL));
    659   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    660             store->GetValue(prefs::kDefaultSearchProviderKeyword, NULL));
    661   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    662             store->GetValue(prefs::kDefaultSearchProviderSuggestURL, NULL));
    663   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    664             store->GetValue(prefs::kDefaultSearchProviderIconURL, NULL));
    665   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    666             store->GetValue(prefs::kDefaultSearchProviderEncodings, NULL));
    667 }
    668 
    669 // Test cases for the Sync policy setting.
    670 class ConfigurationPolicyPrefStoreSyncTest
    671     : public ConfigurationPolicyPrefStoreTestBase<testing::Test> {
    672 };
    673 
    674 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Default) {
    675   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    676             store_->GetValue(prefs::kSyncManaged, NULL));
    677 }
    678 
    679 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Enabled) {
    680   provider_.AddPolicy(kPolicySyncDisabled, Value::CreateBooleanValue(false));
    681   store_->OnUpdatePolicy();
    682   // Enabling Sync should not set the pref.
    683   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    684             store_->GetValue(prefs::kSyncManaged, NULL));
    685 }
    686 
    687 TEST_F(ConfigurationPolicyPrefStoreSyncTest, Disabled) {
    688   provider_.AddPolicy(kPolicySyncDisabled, Value::CreateBooleanValue(true));
    689   store_->OnUpdatePolicy();
    690   // Sync should be flagged as managed.
    691   const Value* value = NULL;
    692   EXPECT_EQ(PrefStore::READ_OK, store_->GetValue(prefs::kSyncManaged, &value));
    693   ASSERT_TRUE(value != NULL);
    694   EXPECT_TRUE(FundamentalValue(true).Equals(value));
    695 }
    696 
    697 // Test cases for the Autofill policy setting.
    698 class ConfigurationPolicyPrefStoreAutofillTest
    699     : public ConfigurationPolicyPrefStoreTestBase<testing::Test> {
    700 };
    701 
    702 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Default) {
    703   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    704             store_->GetValue(prefs::kSyncManaged, NULL));
    705 }
    706 
    707 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Enabled) {
    708   provider_.AddPolicy(kPolicyAutoFillEnabled, Value::CreateBooleanValue(true));
    709   store_->OnUpdatePolicy();
    710   // Enabling Autofill should not set the pref.
    711   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    712             store_->GetValue(prefs::kSyncManaged, NULL));
    713 }
    714 
    715 TEST_F(ConfigurationPolicyPrefStoreAutofillTest, Disabled) {
    716   provider_.AddPolicy(kPolicyAutoFillEnabled, Value::CreateBooleanValue(false));
    717   store_->OnUpdatePolicy();
    718   // Disabling Autofill should switch the pref to managed.
    719   const Value* value = NULL;
    720   EXPECT_EQ(PrefStore::READ_OK,
    721             store_->GetValue(prefs::kAutofillEnabled, &value));
    722   EXPECT_TRUE(FundamentalValue(false).Equals(value));
    723 }
    724 
    725 // Exercises the policy refresh mechanism.
    726 class ConfigurationPolicyPrefStoreRefreshTest
    727     : public ConfigurationPolicyPrefStoreTestBase<testing::Test> {
    728  protected:
    729   virtual void SetUp() {
    730     store_->AddObserver(&observer_);
    731   }
    732 
    733   virtual void TearDown() {
    734     store_->RemoveObserver(&observer_);
    735   }
    736 
    737   PrefStoreObserverMock observer_;
    738 };
    739 
    740 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) {
    741   const Value* value = NULL;
    742   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    743             store_->GetValue(prefs::kHomePage, NULL));
    744 
    745   EXPECT_CALL(observer_, OnPrefValueChanged(prefs::kHomePage)).Times(1);
    746   provider_.AddPolicy(kPolicyHomepageLocation,
    747                       Value::CreateStringValue("http://www.chromium.org"));
    748   store_->OnUpdatePolicy();
    749   Mock::VerifyAndClearExpectations(&observer_);
    750   EXPECT_EQ(PrefStore::READ_OK,
    751             store_->GetValue(prefs::kHomePage, &value));
    752   EXPECT_TRUE(StringValue("http://www.chromium.org").Equals(value));
    753 
    754   EXPECT_CALL(observer_, OnPrefValueChanged(_)).Times(0);
    755   store_->OnUpdatePolicy();
    756   Mock::VerifyAndClearExpectations(&observer_);
    757 
    758   EXPECT_CALL(observer_, OnPrefValueChanged(prefs::kHomePage)).Times(1);
    759   provider_.RemovePolicy(kPolicyHomepageLocation);
    760   store_->OnUpdatePolicy();
    761   Mock::VerifyAndClearExpectations(&observer_);
    762   EXPECT_EQ(PrefStore::READ_NO_VALUE,
    763             store_->GetValue(prefs::kHomePage, NULL));
    764 }
    765 
    766 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Initialization) {
    767   EXPECT_FALSE(store_->IsInitializationComplete());
    768 
    769   EXPECT_CALL(observer_, OnInitializationCompleted()).Times(1);
    770 
    771   provider_.SetInitializationComplete(true);
    772   EXPECT_FALSE(store_->IsInitializationComplete());
    773 
    774   store_->OnUpdatePolicy();
    775   Mock::VerifyAndClearExpectations(&observer_);
    776   EXPECT_TRUE(store_->IsInitializationComplete());
    777 }
    778 
    779 }  // namespace policy
    780