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.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "build/build_config.h"
11 #include "components/policy/core/common/policy_details.h"
12 #include "components/policy/core/common/schema.h"
13 #include "policy/policy_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 // This unittest tests the code generated by
17 // chrome/tools/build/generate_policy_source.py.
23 // Checks if two schemas are the same or not. Note that this function doesn't
24 // consider restrictions on integers and strings nor pattern properties.
25 bool IsSameSchema(Schema a
, Schema b
) {
26 if (a
.valid() != b
.valid())
30 if (a
.type() != b
.type())
32 if (a
.type() == base::Value::TYPE_LIST
)
33 return IsSameSchema(a
.GetItems(), b
.GetItems());
34 if (a
.type() != base::Value::TYPE_DICTIONARY
)
36 Schema::Iterator a_it
= a
.GetPropertiesIterator();
37 Schema::Iterator b_it
= b
.GetPropertiesIterator();
38 while (!a_it
.IsAtEnd()) {
41 if (strcmp(a_it
.key(), b_it
.key()) != 0)
43 if (!IsSameSchema(a_it
.schema(), b_it
.schema()))
50 return IsSameSchema(a
.GetAdditionalProperties(), b
.GetAdditionalProperties());
55 TEST(GeneratePolicySource
, ChromeSchemaData
) {
56 Schema schema
= Schema::Wrap(GetChromeSchemaData());
57 ASSERT_TRUE(schema
.valid());
58 EXPECT_EQ(base::Value::TYPE_DICTIONARY
, schema
.type());
60 Schema subschema
= schema
.GetAdditionalProperties();
61 EXPECT_FALSE(subschema
.valid());
63 subschema
= schema
.GetProperty("no such policy exists");
64 EXPECT_FALSE(subschema
.valid());
66 subschema
= schema
.GetProperty(key::kSearchSuggestEnabled
);
67 ASSERT_TRUE(subschema
.valid());
68 EXPECT_EQ(base::Value::TYPE_BOOLEAN
, subschema
.type());
70 subschema
= schema
.GetProperty(key::kDefaultCookiesSetting
);
71 ASSERT_TRUE(subschema
.valid());
72 EXPECT_EQ(base::Value::TYPE_INTEGER
, subschema
.type());
74 subschema
= schema
.GetProperty(key::kProxyMode
);
75 ASSERT_TRUE(subschema
.valid());
76 EXPECT_EQ(base::Value::TYPE_STRING
, subschema
.type());
78 subschema
= schema
.GetProperty(key::kURLBlacklist
);
79 ASSERT_TRUE(subschema
.valid());
80 EXPECT_EQ(base::Value::TYPE_LIST
, subschema
.type());
81 ASSERT_TRUE(subschema
.GetItems().valid());
82 EXPECT_EQ(base::Value::TYPE_STRING
, subschema
.GetItems().type());
84 subschema
= schema
.GetProperty(key::kProxySettings
);
85 ASSERT_TRUE(subschema
.valid());
86 EXPECT_EQ(base::Value::TYPE_DICTIONARY
, subschema
.type());
87 EXPECT_FALSE(subschema
.GetAdditionalProperties().valid());
88 EXPECT_FALSE(subschema
.GetProperty("no such proxy key exists").valid());
89 ASSERT_TRUE(subschema
.GetProperty(key::kProxyMode
).valid());
90 ASSERT_TRUE(subschema
.GetProperty(key::kProxyServer
).valid());
91 ASSERT_TRUE(subschema
.GetProperty(key::kProxyServerMode
).valid());
92 ASSERT_TRUE(subschema
.GetProperty(key::kProxyPacUrl
).valid());
93 ASSERT_TRUE(subschema
.GetProperty(key::kProxyBypassList
).valid());
95 // Verify that all the Chrome policies are there.
96 for (Schema::Iterator it
= schema
.GetPropertiesIterator();
97 !it
.IsAtEnd(); it
.Advance()) {
98 EXPECT_TRUE(it
.key());
99 EXPECT_FALSE(std::string(it
.key()).empty());
100 EXPECT_TRUE(GetChromePolicyDetails(it
.key()));
103 // The properties are iterated in order.
104 const char* kExpectedProperties
[] = {
105 key::kProxyBypassList
,
109 key::kProxyServerMode
,
112 const char** next
= kExpectedProperties
;
113 for (Schema::Iterator
it(subschema
.GetPropertiesIterator());
114 !it
.IsAtEnd(); it
.Advance(), ++next
) {
115 ASSERT_TRUE(*next
!= NULL
);
116 EXPECT_STREQ(*next
, it
.key());
117 ASSERT_TRUE(it
.schema().valid());
118 EXPECT_EQ(base::Value::TYPE_STRING
, it
.schema().type());
120 EXPECT_TRUE(*next
== NULL
);
122 #if defined(OS_CHROMEOS)
123 subschema
= schema
.GetKnownProperty(key::kPowerManagementIdleSettings
);
124 ASSERT_TRUE(subschema
.valid());
126 EXPECT_TRUE(IsSameSchema(subschema
.GetKnownProperty("AC"),
127 subschema
.GetKnownProperty("Battery")));
129 subschema
= schema
.GetKnownProperty(key::kDeviceLoginScreenPowerManagement
);
130 ASSERT_TRUE(subschema
.valid());
132 EXPECT_TRUE(IsSameSchema(subschema
.GetKnownProperty("AC"),
133 subschema
.GetKnownProperty("Battery")));
137 TEST(GeneratePolicySource
, PolicyDetails
) {
138 EXPECT_FALSE(GetChromePolicyDetails(""));
139 EXPECT_FALSE(GetChromePolicyDetails("no such policy"));
140 EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable"));
141 EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled"));
142 EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled"));
144 const PolicyDetails
* details
=
145 GetChromePolicyDetails(key::kSearchSuggestEnabled
);
146 ASSERT_TRUE(details
);
147 EXPECT_FALSE(details
->is_deprecated
);
148 EXPECT_FALSE(details
->is_device_policy
);
149 EXPECT_EQ(6, details
->id
);
150 EXPECT_EQ(0u, details
->max_external_data_size
);
153 details
= GetChromePolicyDetails(key::kJavascriptEnabled
);
154 ASSERT_TRUE(details
);
155 EXPECT_TRUE(details
->is_deprecated
);
156 EXPECT_FALSE(details
->is_device_policy
);
157 EXPECT_EQ(9, details
->id
);
158 EXPECT_EQ(0u, details
->max_external_data_size
);
161 #if defined(OS_CHROMEOS)
162 details
= GetChromePolicyDetails(key::kDevicePolicyRefreshRate
);
163 ASSERT_TRUE(details
);
164 EXPECT_FALSE(details
->is_deprecated
);
165 EXPECT_TRUE(details
->is_device_policy
);
166 EXPECT_EQ(90, details
->id
);
167 EXPECT_EQ(0u, details
->max_external_data_size
);
170 // TODO(bartfab): add a test that verifies a max_external_data_size larger
171 // than 0, once a type 'external' policy is added.
174 } // namespace policy