1// Copyright 2013 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#ifndef COMPONENTS_POLICY_CORE_COMMON_CONFIGURATION_POLICY_PROVIDER_TEST_H_
6#define COMPONENTS_POLICY_CORE_COMMON_CONFIGURATION_POLICY_PROVIDER_TEST_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback_forward.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/message_loop/message_loop.h"
15#include "components/policy/core/common/policy_types.h"
16#include "components/policy/core/common/schema.h"
17#include "components/policy/core/common/schema_registry.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace base {
21class DictionaryValue;
22class ListValue;
23class SequencedTaskRunner;
24class Value;
25}
26
27namespace policy {
28
29class ConfigurationPolicyProvider;
30
31namespace test_keys {
32
33extern const char kKeyString[];
34extern const char kKeyBoolean[];
35extern const char kKeyInteger[];
36extern const char kKeyStringList[];
37extern const char kKeyDictionary[];
38
39}  // namespace test_keys
40
41class PolicyTestBase : public testing::Test {
42 public:
43  PolicyTestBase();
44  virtual ~PolicyTestBase();
45
46  // testing::Test:
47  virtual void SetUp() OVERRIDE;
48  virtual void TearDown() OVERRIDE;
49
50 protected:
51  bool RegisterSchema(const PolicyNamespace& ns,
52                      const std::string& schema);
53
54  SchemaRegistry schema_registry_;
55
56  // Create an actual IO loop (needed by FilePathWatcher).
57  base::MessageLoopForIO loop_;
58
59 private:
60  DISALLOW_COPY_AND_ASSIGN(PolicyTestBase);
61};
62
63// An interface for creating a test policy provider and creating a policy
64// provider instance for testing. Used as the parameter to the abstract
65// ConfigurationPolicyProviderTest below.
66class PolicyProviderTestHarness {
67 public:
68  // |level| and |scope| are the level and scope of the policies returned by
69  // the providers from CreateProvider().
70  PolicyProviderTestHarness(PolicyLevel level, PolicyScope scope);
71  virtual ~PolicyProviderTestHarness();
72
73  // Actions to run at gtest SetUp() time.
74  virtual void SetUp() = 0;
75
76  // Create a new policy provider.
77  virtual ConfigurationPolicyProvider* CreateProvider(
78      SchemaRegistry* registry,
79      scoped_refptr<base::SequencedTaskRunner> task_runner) = 0;
80
81  // Returns the policy level and scope set by the policy provider.
82  PolicyLevel policy_level() const;
83  PolicyScope policy_scope() const;
84
85  // Helpers to configure the environment the policy provider reads from.
86  virtual void InstallEmptyPolicy() = 0;
87  virtual void InstallStringPolicy(const std::string& policy_name,
88                                   const std::string& policy_value) = 0;
89  virtual void InstallIntegerPolicy(const std::string& policy_name,
90                                    int policy_value) = 0;
91  virtual void InstallBooleanPolicy(const std::string& policy_name,
92                                    bool policy_value) = 0;
93  virtual void InstallStringListPolicy(const std::string& policy_name,
94                                       const base::ListValue* policy_value) = 0;
95  virtual void InstallDictionaryPolicy(
96      const std::string& policy_name,
97      const base::DictionaryValue* policy_value) = 0;
98
99  // Not every provider supports installing 3rd party policy. Those who do
100  // should override this method; the default just makes the test fail.
101  virtual void Install3rdPartyPolicy(const base::DictionaryValue* policies);
102
103 private:
104  PolicyLevel level_;
105  PolicyScope scope_;
106
107  DISALLOW_COPY_AND_ASSIGN(PolicyProviderTestHarness);
108};
109
110// A factory method for creating a test harness.
111typedef PolicyProviderTestHarness* (*CreatePolicyProviderTestHarness)();
112
113// Abstract policy provider test. This is meant to be instantiated for each
114// policy provider implementation, passing in a suitable harness factory
115// function as the test parameter.
116class ConfigurationPolicyProviderTest
117    : public PolicyTestBase,
118      public testing::WithParamInterface<CreatePolicyProviderTestHarness> {
119 protected:
120  ConfigurationPolicyProviderTest();
121  virtual ~ConfigurationPolicyProviderTest();
122
123  virtual void SetUp() OVERRIDE;
124  virtual void TearDown() OVERRIDE;
125
126  // Installs a valid policy and checks whether the provider returns the
127  // |expected_value|.
128  void CheckValue(const char* policy_name,
129                  const base::Value& expected_value,
130                  base::Closure install_value);
131
132  scoped_ptr<PolicyProviderTestHarness> test_harness_;
133  scoped_ptr<ConfigurationPolicyProvider> provider_;
134
135 private:
136  DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyProviderTest);
137};
138
139// An extension of ConfigurationPolicyProviderTest that also tests loading of
140// 3rd party policy. Policy provider implementations that support loading of
141// 3rd party policy should also instantiate these tests.
142class Configuration3rdPartyPolicyProviderTest
143    : public ConfigurationPolicyProviderTest {
144 protected:
145  Configuration3rdPartyPolicyProviderTest();
146  virtual ~Configuration3rdPartyPolicyProviderTest();
147
148 private:
149  DISALLOW_COPY_AND_ASSIGN(Configuration3rdPartyPolicyProviderTest);
150};
151
152}  // namespace policy
153
154#endif  // COMPONENTS_POLICY_CORE_COMMON_CONFIGURATION_POLICY_PROVIDER_TEST_H_
155