Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: chrome/browser/policy/policy_loader_win_unittest.cc

Issue 10696034: Share JSON schema constants. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/policy/policy_loader_win.cc ('k') | chrome/chrome_common.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/policy_loader_win.h" 5 #include "chrome/browser/policy/policy_loader_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/string16.h" 10 #include "base/string16.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "base/win/registry.h" 14 #include "base/win/registry.h"
15 #include "chrome/browser/policy/async_policy_provider.h" 15 #include "chrome/browser/policy/async_policy_provider.h"
16 #include "chrome/browser/policy/configuration_policy_provider_test.h" 16 #include "chrome/browser/policy/configuration_policy_provider_test.h"
17 #include "chrome/browser/policy/policy_bundle.h" 17 #include "chrome/browser/policy/policy_bundle.h"
18 #include "chrome/browser/policy/policy_map.h" 18 #include "chrome/browser/policy/policy_map.h"
19 #include "chrome/common/json_schema_constants.h"
19 #include "policy/policy_constants.h" 20 #include "policy/policy_constants.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
23 namespace schema = json_schema_constants;
24
22 using base::win::RegKey; 25 using base::win::RegKey;
23 using namespace policy::registry_constants; 26 using namespace policy::registry_constants;
24 27
25 namespace policy { 28 namespace policy {
26 29
27 namespace { 30 namespace {
28 31
29 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; 32 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests";
30 const wchar_t kUnitTestMachineOverrideSubKey[] = 33 const wchar_t kUnitTestMachineOverrideSubKey[] =
31 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; 34 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override";
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 NOTREACHED(); 115 NOTREACHED();
113 return false; 116 return false;
114 } 117 }
115 118
116 // Builds a JSON schema that represents the types contained in |value|. 119 // Builds a JSON schema that represents the types contained in |value|.
117 // Ownership is transferred to the caller. 120 // Ownership is transferred to the caller.
118 base::DictionaryValue* BuildSchema(const base::Value& value) { 121 base::DictionaryValue* BuildSchema(const base::Value& value) {
119 base::DictionaryValue* schema = new base::DictionaryValue(); 122 base::DictionaryValue* schema = new base::DictionaryValue();
120 switch (value.GetType()) { 123 switch (value.GetType()) {
121 case base::Value::TYPE_NULL: 124 case base::Value::TYPE_NULL:
122 schema->SetString(kType, "null"); 125 schema->SetString(schema::kType, "null");
123 break; 126 break;
124 case base::Value::TYPE_BOOLEAN: 127 case base::Value::TYPE_BOOLEAN:
125 schema->SetString(kType, "boolean"); 128 schema->SetString(schema::kType, "boolean");
126 break; 129 break;
127 case base::Value::TYPE_INTEGER: 130 case base::Value::TYPE_INTEGER:
128 schema->SetString(kType, "integer"); 131 schema->SetString(schema::kType, "integer");
129 break; 132 break;
130 case base::Value::TYPE_DOUBLE: 133 case base::Value::TYPE_DOUBLE:
131 schema->SetString(kType, "number"); 134 schema->SetString(schema::kType, "number");
132 break; 135 break;
133 case base::Value::TYPE_STRING: 136 case base::Value::TYPE_STRING:
134 schema->SetString(kType, "string"); 137 schema->SetString(schema::kType, "string");
135 break; 138 break;
136 139
137 case base::Value::TYPE_LIST: { 140 case base::Value::TYPE_LIST: {
138 // Assumes every list element has the same type. 141 // Assumes every list element has the same type.
139 const base::ListValue* list = NULL; 142 const base::ListValue* list = NULL;
140 if (value.GetAsList(&list) && !list->empty()) { 143 if (value.GetAsList(&list) && !list->empty()) {
141 schema->SetString(kType, "array"); 144 schema->SetString(schema::kType, "array");
142 schema->Set(kItems, BuildSchema(**list->begin())); 145 schema->Set(schema::kItems, BuildSchema(**list->begin()));
143 } 146 }
144 break; 147 break;
145 } 148 }
146 149
147 case base::Value::TYPE_DICTIONARY: { 150 case base::Value::TYPE_DICTIONARY: {
148 const base::DictionaryValue* dict = NULL; 151 const base::DictionaryValue* dict = NULL;
149 if (value.GetAsDictionary(&dict)) { 152 if (value.GetAsDictionary(&dict)) {
150 base::DictionaryValue* properties = new base::DictionaryValue(); 153 base::DictionaryValue* properties = new base::DictionaryValue();
151 for (base::DictionaryValue::Iterator it(*dict); 154 for (base::DictionaryValue::Iterator it(*dict);
152 it.HasNext(); it.Advance()) { 155 it.HasNext(); it.Advance()) {
153 properties->Set(it.key(), BuildSchema(it.value())); 156 properties->Set(it.key(), BuildSchema(it.value()));
154 } 157 }
155 schema->SetString(kType, "object"); 158 schema->SetString(schema::kType, "object");
156 schema->Set(kProperties, properties); 159 schema->Set(schema::kProperties, properties);
157 } 160 }
158 break; 161 break;
159 } 162 }
160 163
161 case base::Value::TYPE_BINARY: 164 case base::Value::TYPE_BINARY:
162 break; 165 break;
163 } 166 }
164 return schema; 167 return schema;
165 } 168 }
166 169
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 572
570 PolicyBundle expected; 573 PolicyBundle expected;
571 expected.Get(POLICY_DOMAIN_EXTENSIONS, "int") 574 expected.Get(POLICY_DOMAIN_EXTENSIONS, "int")
572 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 575 .LoadFrom(&policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
573 EXPECT_TRUE(Matches(expected)); 576 EXPECT_TRUE(Matches(expected));
574 } 577 }
575 578
576 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) { 579 TEST_F(PolicyLoaderWinTest, DefaultPropertySchemaType) {
577 // Build a schema for an "object" with a default schema for its properties. 580 // Build a schema for an "object" with a default schema for its properties.
578 base::DictionaryValue default_schema; 581 base::DictionaryValue default_schema;
579 default_schema.SetString(kType, "number"); 582 default_schema.SetString(schema::kType, "number");
580 base::DictionaryValue integer_schema; 583 base::DictionaryValue integer_schema;
581 integer_schema.SetString(kType, "integer"); 584 integer_schema.SetString(schema::kType, "integer");
582 base::DictionaryValue properties; 585 base::DictionaryValue properties;
583 properties.Set("special-int1", integer_schema.DeepCopy()); 586 properties.Set("special-int1", integer_schema.DeepCopy());
584 properties.Set("special-int2", integer_schema.DeepCopy()); 587 properties.Set("special-int2", integer_schema.DeepCopy());
585 base::DictionaryValue schema; 588 base::DictionaryValue schema;
586 schema.SetString(kType, "object"); 589 schema.SetString(schema::kType, "object");
587 schema.Set(kProperties, properties.DeepCopy()); 590 schema.Set(schema::kProperties, properties.DeepCopy());
588 schema.Set(kAdditionalProperties, default_schema.DeepCopy()); 591 schema.Set(schema::kAdditionalProperties, default_schema.DeepCopy());
589 592
590 const string16 kPathSuffix = 593 const string16 kPathSuffix =
591 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\test"); 594 kRegistryMandatorySubKey + ASCIIToUTF16("\\3rdparty\\extensions\\test");
592 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema)); 595 EXPECT_TRUE(WriteSchema(schema, HKEY_CURRENT_USER, kPathSuffix, kSchema));
593 596
594 // Write some test values. 597 // Write some test values.
595 base::DictionaryValue policy; 598 base::DictionaryValue policy;
596 // These special values have a specific schema for them. 599 // These special values have a specific schema for them.
597 policy.SetInteger("special-int1", 123); 600 policy.SetInteger("special-int1", 123);
598 policy.SetString("special-int2", "-456"); 601 policy.SetString("special-int2", "-456");
599 // Other values default to be loaded as doubles. 602 // Other values default to be loaded as doubles.
600 policy.SetInteger("double1", 789.0); 603 policy.SetInteger("double1", 789.0);
601 policy.SetString("double2", "123.456e7"); 604 policy.SetString("double2", "123.456e7");
602 policy.SetString("invalid", "omg"); 605 policy.SetString("invalid", "omg");
603 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory)); 606 EXPECT_TRUE(InstallValue(policy, HKEY_CURRENT_USER, kPathSuffix, kMandatory));
604 607
605 base::DictionaryValue expected_policy; 608 base::DictionaryValue expected_policy;
606 expected_policy.SetInteger("special-int1", 123); 609 expected_policy.SetInteger("special-int1", 123);
607 expected_policy.SetInteger("special-int2", -456); 610 expected_policy.SetInteger("special-int2", -456);
608 expected_policy.SetDouble("double1", 789.0); 611 expected_policy.SetDouble("double1", 789.0);
609 expected_policy.SetDouble("double2", 123.456e7); 612 expected_policy.SetDouble("double2", 123.456e7);
610 PolicyBundle expected; 613 PolicyBundle expected;
611 expected.Get(POLICY_DOMAIN_EXTENSIONS, "test") 614 expected.Get(POLICY_DOMAIN_EXTENSIONS, "test")
612 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 615 .LoadFrom(&expected_policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
613 EXPECT_TRUE(Matches(expected)); 616 EXPECT_TRUE(Matches(expected));
614 } 617 }
615 618
616 } // namespace policy 619 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/policy_loader_win.cc ('k') | chrome/chrome_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698