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