Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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/prefs/pref_store_observer_mock.h"
     10 #include "base/run_loop.h"
     11 #include "components/policy/core/browser/configuration_policy_handler.h"
     12 #include "components/policy/core/browser/configuration_policy_pref_store.h"
     13 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
     14 #include "components/policy/core/common/external_data_fetcher.h"
     15 #include "components/policy/core/common/policy_details.h"
     16 #include "components/policy/core/common/policy_map.h"
     17 #include "components/policy/core/common/policy_pref_names.h"
     18 #include "components/policy/core/common/policy_service_impl.h"
     19 #include "testing/gmock/include/gmock/gmock.h"
     20 
     21 // Note: this file should move to components/policy/core/browser, but the
     22 // components_unittests runner does not load the ResourceBundle as
     23 // ChromeTestSuite::Initialize does, which leads to failures using
     24 // PolicyErrorMap.
     25 
     26 using testing::Mock;
     27 using testing::Return;
     28 using testing::_;
     29 
     30 namespace {
     31 
     32 const char kTestPolicy[] = "test.policy";
     33 const char kTestPref[] = "test.pref";
     34 
     35 }  // namespace
     36 
     37 namespace policy {
     38 
     39 // Test cases for list-valued policy settings.
     40 class ConfigurationPolicyPrefStoreListTest
     41     : public ConfigurationPolicyPrefStoreTest {
     42   virtual void SetUp() OVERRIDE {
     43     handler_list_.AddHandler(
     44         make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
     45             kTestPolicy, kTestPref, base::Value::TYPE_LIST)));
     46   }
     47 };
     48 
     49 TEST_F(ConfigurationPolicyPrefStoreListTest, GetDefault) {
     50   EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
     51 }
     52 
     53 TEST_F(ConfigurationPolicyPrefStoreListTest, SetValue) {
     54   base::ListValue* in_value = new base::ListValue();
     55   in_value->Append(new base::StringValue("test1"));
     56   in_value->Append(new base::StringValue("test2,"));
     57   PolicyMap policy;
     58   policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     59              POLICY_SCOPE_USER, in_value, NULL);
     60   UpdateProviderPolicy(policy);
     61   const base::Value* value = NULL;
     62   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
     63   ASSERT_TRUE(value);
     64   EXPECT_TRUE(in_value->Equals(value));
     65 }
     66 
     67 // Test cases for string-valued policy settings.
     68 class ConfigurationPolicyPrefStoreStringTest
     69     : public ConfigurationPolicyPrefStoreTest {
     70   virtual void SetUp() OVERRIDE {
     71     handler_list_.AddHandler(
     72         make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
     73             kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
     74   }
     75 };
     76 
     77 TEST_F(ConfigurationPolicyPrefStoreStringTest, GetDefault) {
     78   EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
     79 }
     80 
     81 TEST_F(ConfigurationPolicyPrefStoreStringTest, SetValue) {
     82   PolicyMap policy;
     83   policy.Set(kTestPolicy,
     84              POLICY_LEVEL_MANDATORY,
     85              POLICY_SCOPE_USER,
     86              new base::StringValue("http://chromium.org"),
     87              NULL);
     88   UpdateProviderPolicy(policy);
     89   const base::Value* value = NULL;
     90   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
     91   ASSERT_TRUE(value);
     92   EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value));
     93 }
     94 
     95 // Test cases for boolean-valued policy settings.
     96 class ConfigurationPolicyPrefStoreBooleanTest
     97     : public ConfigurationPolicyPrefStoreTest {
     98   virtual void SetUp() OVERRIDE {
     99     handler_list_.AddHandler(
    100         make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
    101             kTestPolicy, kTestPref, base::Value::TYPE_BOOLEAN)));
    102   }
    103 };
    104 
    105 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) {
    106   EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
    107 }
    108 
    109 TEST_F(ConfigurationPolicyPrefStoreBooleanTest, SetValue) {
    110   PolicyMap policy;
    111   policy.Set(kTestPolicy,
    112              POLICY_LEVEL_MANDATORY,
    113              POLICY_SCOPE_USER,
    114              new base::FundamentalValue(false),
    115              NULL);
    116   UpdateProviderPolicy(policy);
    117   const base::Value* value = NULL;
    118   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
    119   ASSERT_TRUE(value);
    120   bool boolean_value = true;
    121   bool result = value->GetAsBoolean(&boolean_value);
    122   ASSERT_TRUE(result);
    123   EXPECT_FALSE(boolean_value);
    124 
    125   policy.Set(kTestPolicy,
    126              POLICY_LEVEL_MANDATORY,
    127              POLICY_SCOPE_USER,
    128              new base::FundamentalValue(true),
    129              NULL);
    130   UpdateProviderPolicy(policy);
    131   value = NULL;
    132   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
    133   boolean_value = false;
    134   result = value->GetAsBoolean(&boolean_value);
    135   ASSERT_TRUE(result);
    136   EXPECT_TRUE(boolean_value);
    137 }
    138 
    139 // Test cases for integer-valued policy settings.
    140 class ConfigurationPolicyPrefStoreIntegerTest
    141     : public ConfigurationPolicyPrefStoreTest {
    142   virtual void SetUp() OVERRIDE {
    143     handler_list_.AddHandler(
    144         make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
    145             kTestPolicy, kTestPref, base::Value::TYPE_INTEGER)));
    146   }
    147 };
    148 
    149 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) {
    150   EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
    151 }
    152 
    153 TEST_F(ConfigurationPolicyPrefStoreIntegerTest, SetValue) {
    154   PolicyMap policy;
    155   policy.Set(kTestPolicy,
    156              POLICY_LEVEL_MANDATORY,
    157              POLICY_SCOPE_USER,
    158              new base::FundamentalValue(2),
    159              NULL);
    160   UpdateProviderPolicy(policy);
    161   const base::Value* value = NULL;
    162   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
    163   EXPECT_TRUE(base::FundamentalValue(2).Equals(value));
    164 }
    165 
    166 // Exercises the policy refresh mechanism.
    167 class ConfigurationPolicyPrefStoreRefreshTest
    168     : public ConfigurationPolicyPrefStoreTest {
    169  protected:
    170   virtual void SetUp() OVERRIDE {
    171     ConfigurationPolicyPrefStoreTest::SetUp();
    172     store_->AddObserver(&observer_);
    173     handler_list_.AddHandler(
    174         make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
    175             kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
    176   }
    177 
    178   virtual void TearDown() OVERRIDE {
    179     store_->RemoveObserver(&observer_);
    180     ConfigurationPolicyPrefStoreTest::TearDown();
    181   }
    182 
    183   PrefStoreObserverMock observer_;
    184 };
    185 
    186 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) {
    187   const base::Value* value = NULL;
    188   EXPECT_FALSE(store_->GetValue(kTestPolicy, NULL));
    189 
    190   PolicyMap policy;
    191   policy.Set(kTestPolicy,
    192              POLICY_LEVEL_MANDATORY,
    193              POLICY_SCOPE_USER,
    194              new base::StringValue("http://www.chromium.org"),
    195              NULL);
    196   UpdateProviderPolicy(policy);
    197   observer_.VerifyAndResetChangedKey(kTestPref);
    198   EXPECT_TRUE(store_->GetValue(kTestPref, &value));
    199   EXPECT_TRUE(base::StringValue("http://www.chromium.org").Equals(value));
    200 
    201   UpdateProviderPolicy(policy);
    202   EXPECT_TRUE(observer_.changed_keys.empty());
    203 
    204   policy.Erase(kTestPolicy);
    205   UpdateProviderPolicy(policy);
    206   observer_.VerifyAndResetChangedKey(kTestPref);
    207   EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
    208 }
    209 
    210 TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Initialization) {
    211   EXPECT_FALSE(store_->IsInitializationComplete());
    212   EXPECT_CALL(provider_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
    213       .WillRepeatedly(Return(true));
    214   PolicyMap policy;
    215   UpdateProviderPolicy(policy);
    216   EXPECT_TRUE(observer_.initialized);
    217   EXPECT_TRUE(observer_.initialization_success);
    218   Mock::VerifyAndClearExpectations(&observer_);
    219   EXPECT_TRUE(store_->IsInitializationComplete());
    220 }
    221 
    222 }  // namespace policy
    223