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.
5 #include "base/json/json_reader.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/values.h"
8 #include "components/policy/core/browser/configuration_policy_handler.h"
9 #include "components/policy/core/common/policy_map.h"
10 #include "components/policy/core/common/schema.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 class SimpleSchemaValidatingPolicyHandler
18 : public SchemaValidatingPolicyHandler {
20 SimpleSchemaValidatingPolicyHandler(const Schema& schema,
21 SchemaOnErrorStrategy strategy)
22 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {}
23 virtual ~SimpleSchemaValidatingPolicyHandler() {}
25 virtual void ApplyPolicySettings(const policy::PolicyMap&,
26 PrefValueMap*) OVERRIDE {
29 bool CheckAndGetValueForTest(const PolicyMap& policies,
30 scoped_ptr<base::Value>* value) {
31 return SchemaValidatingPolicyHandler::CheckAndGetValue(
32 policies, NULL, value);
38 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
40 static const char kSchemaJson[] =
42 " \"type\": \"object\","
45 " \"type\": \"integer\","
50 " \"type\": \"string\","
51 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
55 Schema schema = Schema::Parse(kSchemaJson, &error);
56 ASSERT_TRUE(schema.valid()) << error;
58 static const char kPolicyMapJson[] =
60 " \"PolicyForTesting\": {"
62 " \"Colors\": \"White\""
65 scoped_ptr<base::Value> policy_map_value(base::JSONReader::ReadAndReturnError(
66 kPolicyMapJson, base::JSON_PARSE_RFC, NULL, &error));
67 ASSERT_TRUE(policy_map_value) << error;
69 const base::DictionaryValue* policy_map_dict = NULL;
70 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
74 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
76 SimpleSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID);
77 scoped_ptr<base::Value> output_value;
78 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value));
79 ASSERT_TRUE(output_value);
81 base::DictionaryValue* dict = NULL;
82 ASSERT_TRUE(output_value->GetAsDictionary(&dict));
84 // Test that CheckAndGetValue() actually dropped invalid properties.
86 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value));
87 EXPECT_EQ(2, int_value);
88 EXPECT_FALSE(dict->HasKey("Colors"));