summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/managed_mode_policy_provider_unittest.cc
blob: 4d98b6e9e27fb21c87e5d0a13618c25bfdf64615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/prefs/testing_pref_store.h"
#include "chrome/browser/policy/configuration_policy_provider_test.h"
#include "chrome/browser/policy/managed_mode_policy_provider.h"
#include "chrome/browser/policy/policy_bundle.h"
#include "chrome/browser/policy/policy_map.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

namespace {

class TestHarness : public PolicyProviderTestHarness {
 public:
  TestHarness();
  virtual ~TestHarness();

  static PolicyProviderTestHarness* Create();

  virtual void SetUp() OVERRIDE;

  // PolicyProviderTestHarness implementation:
  virtual ConfigurationPolicyProvider* CreateProvider(
      const PolicyDefinitionList* policy_definition_list) OVERRIDE;

  virtual void InstallEmptyPolicy() OVERRIDE;
  virtual void InstallStringPolicy(const std::string& policy_name,
                                   const std::string& policy_value) OVERRIDE;
  virtual void InstallIntegerPolicy(const std::string& policy_name,
                                    int policy_value) OVERRIDE;
  virtual void InstallBooleanPolicy(const std::string& policy_name,
                                    bool policy_value) OVERRIDE;
  virtual void InstallStringListPolicy(
      const std::string& policy_name,
      const base::ListValue* policy_value) OVERRIDE;
  virtual void InstallDictionaryPolicy(
      const std::string& policy_name,
      const base::DictionaryValue* policy_value) OVERRIDE;

 private:
  void InstallPolicy(const std::string& policy_name, base::Value* policy_value);

  scoped_refptr<TestingPrefStore> pref_store_;

  DISALLOW_COPY_AND_ASSIGN(TestHarness);
};

TestHarness::TestHarness()
    : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER),
      pref_store_(new TestingPrefStore) {
  pref_store_->SetInitializationCompleted();
}

TestHarness::~TestHarness() {}

// static
PolicyProviderTestHarness* TestHarness::Create() {
  return new TestHarness();
}

void TestHarness::SetUp() {
}

ConfigurationPolicyProvider* TestHarness::CreateProvider(
    const PolicyDefinitionList* policy_definition_list) {
  return new ManagedModePolicyProvider(pref_store_);
}

void TestHarness::InstallEmptyPolicy() {}

void TestHarness::InstallStringPolicy(const std::string& policy_name,
                                      const std::string& policy_value) {
  InstallPolicy(policy_name, base::Value::CreateStringValue(policy_value));
}

void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
                                       int policy_value) {
  InstallPolicy(policy_name, base::Value::CreateIntegerValue(policy_value));
}

void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
                                       bool policy_value) {
  InstallPolicy(policy_name, base::Value::CreateBooleanValue(policy_value));
}

void TestHarness::InstallStringListPolicy(const std::string& policy_name,
                                          const base::ListValue* policy_value) {
  InstallPolicy(policy_name, policy_value->DeepCopy());
}

void TestHarness::InstallDictionaryPolicy(
    const std::string& policy_name,
    const base::DictionaryValue* policy_value) {
  InstallPolicy(policy_name, policy_value->DeepCopy());
}

void TestHarness::InstallPolicy(const std::string& policy_name,
                                base::Value* policy_value) {
  base::DictionaryValue* cached_policy = NULL;
  base::Value* value = NULL;
  if (pref_store_->GetMutableValue(ManagedModePolicyProvider::kPolicies,
                                   &value)) {
    ASSERT_TRUE(value->GetAsDictionary(&cached_policy));
  } else {
    cached_policy = new base::DictionaryValue;
    pref_store_->SetValue(ManagedModePolicyProvider::kPolicies,
                          cached_policy);
  }

  cached_policy->SetWithoutPathExpansion(policy_name, policy_value);
}

}  // namespace

// Instantiate abstract test case for basic policy reading tests.
INSTANTIATE_TEST_CASE_P(
    ManagedModePolicyProviderTest,
    ConfigurationPolicyProviderTest,
    testing::Values(TestHarness::Create));

class ManagedModePolicyProviderAPITest : public PolicyTestBase {
 protected:
  ManagedModePolicyProviderAPITest()
      : pref_store_(new TestingPrefStore),
        provider_(pref_store_) {
    pref_store_->SetInitializationCompleted();
  }
  virtual ~ManagedModePolicyProviderAPITest() {}

  virtual void SetUp() OVERRIDE {
    PolicyTestBase::SetUp();
    provider_.Init();
  }

  virtual void TearDown() OVERRIDE {
    provider_.Shutdown();
    PolicyTestBase::TearDown();
  }

  scoped_refptr<TestingPrefStore> pref_store_;
  ManagedModePolicyProvider provider_;
};

const char kPolicyKey[] = "TestingPolicy";

TEST_F(ManagedModePolicyProviderAPITest, Empty) {
  EXPECT_FALSE(provider_.GetPolicy(kPolicyKey));

  const PolicyBundle kEmptyBundle;
  EXPECT_TRUE(provider_.policies().Equals(kEmptyBundle));
}

TEST_F(ManagedModePolicyProviderAPITest, SetPolicy) {
  base::StringValue policy_value("PolicyValue");
  provider_.SetPolicy(kPolicyKey, &policy_value);

  EXPECT_TRUE(base::Value::Equals(&policy_value,
                                  provider_.GetPolicy(kPolicyKey)));

  PolicyBundle expected_bundle;
  PolicyMap* policy_map =
      &expected_bundle.Get(POLICY_DOMAIN_CHROME, std::string());
  policy_map->Set(kPolicyKey, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
                  policy_value.DeepCopy());
  EXPECT_TRUE(provider_.policies().Equals(expected_bundle));

  // A newly-created provider should have the same policies.
  ManagedModePolicyProvider new_provider(pref_store_);
  new_provider.Init();
  EXPECT_TRUE(new_provider.policies().Equals(expected_bundle));

  // Cleanup.
  new_provider.Shutdown();
}

}  // namespace policy