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 <gtest/gtest.h>
      6 
      7 #include "base/mac/scoped_cftyperef.h"
      8 #include "base/stl_util-inl.h"
      9 #include "base/sys_string_conversions.h"
     10 #include "chrome/browser/policy/configuration_policy_pref_store.h"
     11 #include "chrome/browser/policy/configuration_policy_provider_mac.h"
     12 #include "chrome/browser/policy/mock_configuration_policy_store.h"
     13 #include "chrome/browser/preferences_mock_mac.h"
     14 #include "policy/policy_constants.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace policy {
     18 
     19 // Holds parameters for the parametrized policy tests.
     20 class PolicyTestParams {
     21  public:
     22   // Takes ownership of |test_value|.
     23   PolicyTestParams(ConfigurationPolicyType type,
     24                    const char* policy_name,
     25                    Value* test_value)
     26       : type_(type),
     27         policy_name_(policy_name),
     28         test_value_(test_value) {}
     29 
     30   // testing::TestWithParam does copying, so provide copy constructor and
     31   // assignment operator.
     32   PolicyTestParams(const PolicyTestParams& other)
     33       : type_(other.type_),
     34         policy_name_(other.policy_name_),
     35         test_value_(other.test_value_->DeepCopy()) {}
     36 
     37   const PolicyTestParams& operator=(PolicyTestParams other) {
     38     swap(other);
     39     return *this;
     40   }
     41 
     42   void swap(PolicyTestParams& other) {
     43     std::swap(type_, other.type_);
     44     std::swap(policy_name_, other.policy_name_);
     45     test_value_.swap(other.test_value_);
     46   }
     47 
     48   ConfigurationPolicyType type() const { return type_; }
     49   const char* policy_name() const { return policy_name_; }
     50   const Value* test_value() const { return test_value_.get(); }
     51 
     52   // Get the test value in the appropriate CFPropertyListRef representation.
     53   CFPropertyListRef GetPropertyListValue() const {
     54     switch (test_value_->GetType()) {
     55       case Value::TYPE_BOOLEAN: {
     56         bool v;
     57         if (!test_value_->GetAsBoolean(&v))
     58           return NULL;
     59         return CFRetain(v ? kCFBooleanTrue : kCFBooleanFalse);
     60       }
     61       case Value::TYPE_INTEGER: {
     62         int v;
     63         if (!test_value_->GetAsInteger(&v))
     64           return NULL;
     65         return CFNumberCreate(NULL, kCFNumberIntType, &v);
     66       }
     67       case Value::TYPE_STRING: {
     68         std::string v;
     69         if (!test_value_->GetAsString(&v))
     70           return NULL;
     71         return base::SysUTF8ToCFStringRef(v);
     72       }
     73       case Value::TYPE_LIST: {
     74         const ListValue* list =
     75             static_cast<const ListValue*>(test_value_.get());
     76         base::mac::ScopedCFTypeRef<CFMutableArrayRef> array(
     77             CFArrayCreateMutable(NULL, list->GetSize(),
     78                                  &kCFTypeArrayCallBacks));
     79         for (ListValue::const_iterator element(list->begin());
     80              element != list->end(); ++element) {
     81           if (!(*element)->IsType(Value::TYPE_STRING))
     82             return NULL;
     83           std::string element_value;
     84           if (!(*element)->GetAsString(&element_value))
     85             return NULL;
     86           base::mac::ScopedCFTypeRef<CFStringRef> cf_element_value(
     87               base::SysUTF8ToCFStringRef(element_value));
     88           CFArrayAppendValue(array, cf_element_value.get());
     89         }
     90         return array.release();
     91       }
     92       default:
     93         return NULL;
     94     }
     95   }
     96 
     97   // Factory methods that create parameter objects for different value types.
     98   static PolicyTestParams ForStringPolicy(
     99       ConfigurationPolicyType type,
    100       const char* name) {
    101     return PolicyTestParams(type, name, Value::CreateStringValue("test"));
    102   }
    103   static PolicyTestParams ForBooleanPolicy(
    104       ConfigurationPolicyType type,
    105       const char* name) {
    106     return PolicyTestParams(type, name, Value::CreateBooleanValue(true));
    107   }
    108   static PolicyTestParams ForIntegerPolicy(
    109       ConfigurationPolicyType type,
    110       const char* name) {
    111     return PolicyTestParams(type, name, Value::CreateIntegerValue(42));
    112   }
    113   static PolicyTestParams ForListPolicy(
    114       ConfigurationPolicyType type,
    115       const char* name) {
    116     ListValue* value = new ListValue;
    117     value->Set(0U, Value::CreateStringValue("first"));
    118     value->Set(1U, Value::CreateStringValue("second"));
    119     return PolicyTestParams(type, name, value);
    120   }
    121 
    122  private:
    123   ConfigurationPolicyType type_;
    124   const char* policy_name_;
    125   scoped_ptr<Value> test_value_;
    126 };
    127 
    128 // Parametrized test class for testing whether ConfigurationPolicyProviderMac
    129 // can handle all policies correctly.
    130 class ConfigurationPolicyProviderMacTest
    131     : public testing::TestWithParam<PolicyTestParams> {
    132  public:
    133   virtual void SetUp() {
    134     prefs_ = new MockPreferences;
    135     store_.reset(new MockConfigurationPolicyStore);
    136   }
    137 
    138  protected:
    139   MockPreferences* prefs_;
    140   scoped_ptr<MockConfigurationPolicyStore> store_;
    141 };
    142 
    143 TEST_P(ConfigurationPolicyProviderMacTest, Default) {
    144   ConfigurationPolicyProviderMac provider(
    145       ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), prefs_);
    146   EXPECT_TRUE(provider.Provide(store_.get()));
    147   EXPECT_TRUE(store_->policy_map().empty());
    148 }
    149 
    150 TEST_P(ConfigurationPolicyProviderMacTest, Invalid) {
    151   base::mac::ScopedCFTypeRef<CFStringRef> name(
    152       base::SysUTF8ToCFStringRef(GetParam().policy_name()));
    153   base::mac::ScopedCFTypeRef<CFDataRef> invalid_data(
    154       CFDataCreate(NULL, NULL, 0));
    155   prefs_->AddTestItem(name, invalid_data.get(), true);
    156 
    157   // Create the provider and have it read |prefs_|.
    158   ConfigurationPolicyProviderMac provider(
    159       ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), prefs_);
    160   EXPECT_TRUE(provider.Provide(store_.get()));
    161   EXPECT_TRUE(store_->policy_map().empty());
    162 }
    163 
    164 TEST_P(ConfigurationPolicyProviderMacTest, TestNonForcedValue) {
    165   base::mac::ScopedCFTypeRef<CFStringRef> name(
    166       base::SysUTF8ToCFStringRef(GetParam().policy_name()));
    167   base::mac::ScopedCFTypeRef<CFPropertyListRef> test_value(
    168       GetParam().GetPropertyListValue());
    169   ASSERT_TRUE(test_value.get());
    170   prefs_->AddTestItem(name, test_value.get(), false);
    171 
    172   // Create the provider and have it read |prefs_|.
    173   ConfigurationPolicyProviderMac provider(
    174       ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), prefs_);
    175   EXPECT_TRUE(provider.Provide(store_.get()));
    176   EXPECT_TRUE(store_->policy_map().empty());
    177 }
    178 
    179 TEST_P(ConfigurationPolicyProviderMacTest, TestValue) {
    180   base::mac::ScopedCFTypeRef<CFStringRef> name(
    181       base::SysUTF8ToCFStringRef(GetParam().policy_name()));
    182   base::mac::ScopedCFTypeRef<CFPropertyListRef> test_value(
    183       GetParam().GetPropertyListValue());
    184   ASSERT_TRUE(test_value.get());
    185   prefs_->AddTestItem(name, test_value, true);
    186 
    187   // Create the provider and have it read |prefs_|.
    188   ConfigurationPolicyProviderMac provider(
    189       ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), prefs_);
    190   EXPECT_TRUE(provider.Provide(store_.get()));
    191   ASSERT_EQ(1U, store_->policy_map().size());
    192   const Value* value = store_->Get(GetParam().type());
    193   ASSERT_TRUE(value);
    194   EXPECT_TRUE(GetParam().test_value()->Equals(value));
    195 }
    196 
    197 // Instantiate the test case for all policies.
    198 INSTANTIATE_TEST_CASE_P(
    199     ConfigurationPolicyProviderMacTestInstance,
    200     ConfigurationPolicyProviderMacTest,
    201     testing::Values(
    202         PolicyTestParams::ForStringPolicy(
    203             kPolicyHomepageLocation,
    204             key::kHomepageLocation),
    205         PolicyTestParams::ForBooleanPolicy(
    206             kPolicyHomepageIsNewTabPage,
    207             key::kHomepageIsNewTabPage),
    208         PolicyTestParams::ForIntegerPolicy(
    209             kPolicyRestoreOnStartup,
    210             key::kRestoreOnStartup),
    211         PolicyTestParams::ForListPolicy(
    212             kPolicyRestoreOnStartupURLs,
    213             key::kRestoreOnStartupURLs),
    214         PolicyTestParams::ForBooleanPolicy(
    215             kPolicyDefaultSearchProviderEnabled,
    216             key::kDefaultSearchProviderEnabled),
    217         PolicyTestParams::ForStringPolicy(
    218             kPolicyDefaultSearchProviderName,
    219             key::kDefaultSearchProviderName),
    220         PolicyTestParams::ForStringPolicy(
    221             kPolicyDefaultSearchProviderKeyword,
    222             key::kDefaultSearchProviderKeyword),
    223         PolicyTestParams::ForStringPolicy(
    224             kPolicyDefaultSearchProviderSearchURL,
    225             key::kDefaultSearchProviderSearchURL),
    226         PolicyTestParams::ForStringPolicy(
    227             kPolicyDefaultSearchProviderSuggestURL,
    228             key::kDefaultSearchProviderSuggestURL),
    229         PolicyTestParams::ForStringPolicy(
    230             kPolicyDefaultSearchProviderInstantURL,
    231             key::kDefaultSearchProviderInstantURL),
    232         PolicyTestParams::ForStringPolicy(
    233             kPolicyDefaultSearchProviderIconURL,
    234             key::kDefaultSearchProviderIconURL),
    235         PolicyTestParams::ForStringPolicy(
    236             kPolicyDefaultSearchProviderEncodings,
    237             key::kDefaultSearchProviderEncodings),
    238         PolicyTestParams::ForStringPolicy(
    239             kPolicyProxyMode,
    240             key::kProxyMode),
    241         PolicyTestParams::ForIntegerPolicy(
    242             kPolicyProxyServerMode,
    243             key::kProxyServerMode),
    244         PolicyTestParams::ForStringPolicy(
    245             kPolicyProxyServer,
    246             key::kProxyServer),
    247         PolicyTestParams::ForStringPolicy(
    248             kPolicyProxyPacUrl,
    249             key::kProxyPacUrl),
    250         PolicyTestParams::ForStringPolicy(
    251             kPolicyProxyBypassList,
    252             key::kProxyBypassList),
    253         PolicyTestParams::ForBooleanPolicy(
    254             kPolicyAlternateErrorPagesEnabled,
    255             key::kAlternateErrorPagesEnabled),
    256         PolicyTestParams::ForBooleanPolicy(
    257             kPolicySearchSuggestEnabled,
    258             key::kSearchSuggestEnabled),
    259         PolicyTestParams::ForBooleanPolicy(
    260             kPolicyDnsPrefetchingEnabled,
    261             key::kDnsPrefetchingEnabled),
    262         PolicyTestParams::ForBooleanPolicy(
    263             kPolicySafeBrowsingEnabled,
    264             key::kSafeBrowsingEnabled),
    265         PolicyTestParams::ForBooleanPolicy(
    266             kPolicyMetricsReportingEnabled,
    267             key::kMetricsReportingEnabled),
    268         PolicyTestParams::ForBooleanPolicy(
    269             kPolicyPasswordManagerEnabled,
    270             key::kPasswordManagerEnabled),
    271         PolicyTestParams::ForBooleanPolicy(
    272             kPolicyPasswordManagerAllowShowPasswords,
    273             key::kPasswordManagerAllowShowPasswords),
    274         PolicyTestParams::ForListPolicy(
    275             kPolicyDisabledPlugins,
    276             key::kDisabledPlugins),
    277         PolicyTestParams::ForListPolicy(
    278             kPolicyDisabledPluginsExceptions,
    279             key::kDisabledPluginsExceptions),
    280         PolicyTestParams::ForListPolicy(
    281             kPolicyEnabledPlugins,
    282             key::kEnabledPlugins),
    283         PolicyTestParams::ForBooleanPolicy(
    284             kPolicyAutoFillEnabled,
    285             key::kAutoFillEnabled),
    286         PolicyTestParams::ForStringPolicy(
    287             kPolicyApplicationLocaleValue,
    288             key::kApplicationLocaleValue),
    289         PolicyTestParams::ForBooleanPolicy(
    290             kPolicySyncDisabled,
    291             key::kSyncDisabled),
    292         PolicyTestParams::ForListPolicy(
    293             kPolicyExtensionInstallWhitelist,
    294             key::kExtensionInstallWhitelist),
    295         PolicyTestParams::ForListPolicy(
    296             kPolicyExtensionInstallBlacklist,
    297             key::kExtensionInstallBlacklist),
    298         PolicyTestParams::ForBooleanPolicy(
    299             kPolicyShowHomeButton,
    300             key::kShowHomeButton),
    301         PolicyTestParams::ForBooleanPolicy(
    302             kPolicyPrintingEnabled,
    303             key::kPrintingEnabled),
    304         PolicyTestParams::ForBooleanPolicy(
    305             kPolicyInstantEnabled,
    306             key::kInstantEnabled),
    307         PolicyTestParams::ForIntegerPolicy(
    308             kPolicyPolicyRefreshRate,
    309             key::kPolicyRefreshRate),
    310         PolicyTestParams::ForBooleanPolicy(
    311             kPolicyDisablePluginFinder,
    312             key::kDisablePluginFinder),
    313         PolicyTestParams::ForBooleanPolicy(
    314             kPolicyClearSiteDataOnExit,
    315             key::kClearSiteDataOnExit),
    316         PolicyTestParams::ForStringPolicy(
    317             kPolicyDownloadDirectory,
    318             key::kDownloadDirectory),
    319         PolicyTestParams::ForBooleanPolicy(
    320             kPolicyDefaultBrowserSettingEnabled,
    321             key::kDefaultBrowserSettingEnabled),
    322         PolicyTestParams::ForBooleanPolicy(
    323             kPolicyCloudPrintProxyEnabled,
    324             key::kCloudPrintProxyEnabled),
    325         PolicyTestParams::ForBooleanPolicy(
    326             kPolicyTranslateEnabled,
    327             key::kTranslateEnabled),
    328         PolicyTestParams::ForBooleanPolicy(
    329             kPolicyAllowOutdatedPlugins,
    330             key::kAllowOutdatedPlugins),
    331         PolicyTestParams::ForBooleanPolicy(
    332             kPolicyBookmarkBarEnabled,
    333             key::kBookmarkBarEnabled),
    334         PolicyTestParams::ForBooleanPolicy(
    335             kPolicyEditBookmarksEnabled,
    336             key::kEditBookmarksEnabled),
    337         PolicyTestParams::ForBooleanPolicy(
    338             kPolicyAllowFileSelectionDialogs,
    339             key::kAllowFileSelectionDialogs),
    340         PolicyTestParams::ForListPolicy(
    341             kPolicyDisabledSchemes,
    342             key::kDisabledSchemes)));
    343 
    344 }  // namespace policy
    345