Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / browser / configuration_policy_handler_unittest.cc
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 "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"
12
13 namespace policy {
14
15 namespace {
16
17 class SimpleSchemaValidatingPolicyHandler
18     : public SchemaValidatingPolicyHandler {
19  public:
20   SimpleSchemaValidatingPolicyHandler(const Schema& schema,
21                                       SchemaOnErrorStrategy strategy)
22       : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {}
23   virtual ~SimpleSchemaValidatingPolicyHandler() {}
24
25   virtual void ApplyPolicySettings(const policy::PolicyMap&,
26                                    PrefValueMap*) OVERRIDE {
27   }
28
29   bool CheckAndGetValueForTest(const PolicyMap& policies,
30                                scoped_ptr<base::Value>* value) {
31     return SchemaValidatingPolicyHandler::CheckAndGetValue(
32         policies, NULL, value);
33   }
34 };
35
36 }  // namespace
37
38 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
39   std::string error;
40   static const char kSchemaJson[] =
41       "{"
42       "  \"type\": \"object\","
43       "  \"properties\": {"
44       "    \"OneToThree\": {"
45       "      \"type\": \"integer\","
46       "      \"minimum\": 1,"
47       "      \"maximum\": 3"
48       "    },"
49       "    \"Colors\": {"
50       "      \"type\": \"string\","
51       "      \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
52       "    }"
53       "  }"
54       "}";
55   Schema schema = Schema::Parse(kSchemaJson, &error);
56   ASSERT_TRUE(schema.valid()) << error;
57
58   static const char kPolicyMapJson[] =
59       "{"
60       "  \"PolicyForTesting\": {"
61       "    \"OneToThree\": 2,"
62       "    \"Colors\": \"White\""
63       "  }"
64       "}";
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;
68
69   const base::DictionaryValue* policy_map_dict = NULL;
70   ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
71
72   PolicyMap policy_map;
73   policy_map.LoadFrom(
74       policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
75
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);
80
81   base::DictionaryValue* dict = NULL;
82   ASSERT_TRUE(output_value->GetAsDictionary(&dict));
83
84   // Test that CheckAndGetValue() actually dropped invalid properties.
85   int int_value = -1;
86   EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value));
87   EXPECT_EQ(2, int_value);
88   EXPECT_FALSE(dict->HasKey("Colors"));
89 }
90
91 }  // namespace policy