OLD | NEW |
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 "components/policy/core/common/policy_loader_win.h" | 5 #include "components/policy/core/common/policy_loader_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <userenv.h> | 10 #include <userenv.h> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // are stored as strings), and can only be retrieved if a corresponding schema | 65 // are stored as strings), and can only be retrieved if a corresponding schema |
66 // is written. | 66 // is written. |
67 bool InstallValue(const base::Value& value, | 67 bool InstallValue(const base::Value& value, |
68 HKEY hive, | 68 HKEY hive, |
69 const base::string16& path, | 69 const base::string16& path, |
70 const base::string16& name) { | 70 const base::string16& name) { |
71 // KEY_ALL_ACCESS causes the ctor to create the key if it does not exist yet. | 71 // KEY_ALL_ACCESS causes the ctor to create the key if it does not exist yet. |
72 RegKey key(hive, path.c_str(), KEY_ALL_ACCESS); | 72 RegKey key(hive, path.c_str(), KEY_ALL_ACCESS); |
73 EXPECT_TRUE(key.Valid()); | 73 EXPECT_TRUE(key.Valid()); |
74 switch (value.GetType()) { | 74 switch (value.GetType()) { |
75 case base::Value::TYPE_NULL: | 75 case base::Value::Type::NONE: |
76 return key.WriteValue(name.c_str(), L"") == ERROR_SUCCESS; | 76 return key.WriteValue(name.c_str(), L"") == ERROR_SUCCESS; |
77 | 77 |
78 case base::Value::TYPE_BOOLEAN: { | 78 case base::Value::Type::BOOLEAN: { |
79 bool bool_value; | 79 bool bool_value; |
80 if (!value.GetAsBoolean(&bool_value)) | 80 if (!value.GetAsBoolean(&bool_value)) |
81 return false; | 81 return false; |
82 return key.WriteValue(name.c_str(), bool_value ? 1 : 0) == ERROR_SUCCESS; | 82 return key.WriteValue(name.c_str(), bool_value ? 1 : 0) == ERROR_SUCCESS; |
83 } | 83 } |
84 | 84 |
85 case base::Value::TYPE_INTEGER: { | 85 case base::Value::Type::INTEGER: { |
86 int int_value; | 86 int int_value; |
87 if (!value.GetAsInteger(&int_value)) | 87 if (!value.GetAsInteger(&int_value)) |
88 return false; | 88 return false; |
89 return key.WriteValue(name.c_str(), int_value) == ERROR_SUCCESS; | 89 return key.WriteValue(name.c_str(), int_value) == ERROR_SUCCESS; |
90 } | 90 } |
91 | 91 |
92 case base::Value::TYPE_DOUBLE: { | 92 case base::Value::Type::DOUBLE: { |
93 double double_value; | 93 double double_value; |
94 if (!value.GetAsDouble(&double_value)) | 94 if (!value.GetAsDouble(&double_value)) |
95 return false; | 95 return false; |
96 base::string16 str_value = | 96 base::string16 str_value = |
97 UTF8ToUTF16(base::DoubleToString(double_value)); | 97 UTF8ToUTF16(base::DoubleToString(double_value)); |
98 return key.WriteValue(name.c_str(), str_value.c_str()) == ERROR_SUCCESS; | 98 return key.WriteValue(name.c_str(), str_value.c_str()) == ERROR_SUCCESS; |
99 } | 99 } |
100 | 100 |
101 case base::Value::TYPE_STRING: { | 101 case base::Value::Type::STRING: { |
102 base::string16 str_value; | 102 base::string16 str_value; |
103 if (!value.GetAsString(&str_value)) | 103 if (!value.GetAsString(&str_value)) |
104 return false; | 104 return false; |
105 return key.WriteValue(name.c_str(), str_value.c_str()) == ERROR_SUCCESS; | 105 return key.WriteValue(name.c_str(), str_value.c_str()) == ERROR_SUCCESS; |
106 } | 106 } |
107 | 107 |
108 case base::Value::TYPE_DICTIONARY: { | 108 case base::Value::Type::DICTIONARY: { |
109 const base::DictionaryValue* sub_dict = NULL; | 109 const base::DictionaryValue* sub_dict = NULL; |
110 if (!value.GetAsDictionary(&sub_dict)) | 110 if (!value.GetAsDictionary(&sub_dict)) |
111 return false; | 111 return false; |
112 for (base::DictionaryValue::Iterator it(*sub_dict); | 112 for (base::DictionaryValue::Iterator it(*sub_dict); |
113 !it.IsAtEnd(); it.Advance()) { | 113 !it.IsAtEnd(); it.Advance()) { |
114 if (!InstallValue(it.value(), hive, path + kPathSep + name, | 114 if (!InstallValue(it.value(), hive, path + kPathSep + name, |
115 UTF8ToUTF16(it.key()))) { | 115 UTF8ToUTF16(it.key()))) { |
116 return false; | 116 return false; |
117 } | 117 } |
118 } | 118 } |
119 return true; | 119 return true; |
120 } | 120 } |
121 | 121 |
122 case base::Value::TYPE_LIST: { | 122 case base::Value::Type::LIST: { |
123 const base::ListValue* list = NULL; | 123 const base::ListValue* list = NULL; |
124 if (!value.GetAsList(&list)) | 124 if (!value.GetAsList(&list)) |
125 return false; | 125 return false; |
126 for (size_t i = 0; i < list->GetSize(); ++i) { | 126 for (size_t i = 0; i < list->GetSize(); ++i) { |
127 const base::Value* item; | 127 const base::Value* item; |
128 if (!list->Get(i, &item)) | 128 if (!list->Get(i, &item)) |
129 return false; | 129 return false; |
130 if (!InstallValue(*item, hive, path + kPathSep + name, | 130 if (!InstallValue(*item, hive, path + kPathSep + name, |
131 base::UintToString16(i + 1))) { | 131 base::UintToString16(i + 1))) { |
132 return false; | 132 return false; |
133 } | 133 } |
134 } | 134 } |
135 return true; | 135 return true; |
136 } | 136 } |
137 | 137 |
138 case base::Value::TYPE_BINARY: | 138 case base::Value::Type::BINARY: |
139 return false; | 139 return false; |
140 } | 140 } |
141 NOTREACHED(); | 141 NOTREACHED(); |
142 return false; | 142 return false; |
143 } | 143 } |
144 | 144 |
145 // This class provides sandboxing and mocking for the parts of the Windows | 145 // This class provides sandboxing and mocking for the parts of the Windows |
146 // Registry implementing Group Policy. It prepares two temporary sandbox keys, | 146 // Registry implementing Group Policy. It prepares two temporary sandbox keys, |
147 // one for HKLM and one for HKCU. A test's calls to the registry are redirected | 147 // one for HKLM and one for HKCU. A test's calls to the registry are redirected |
148 // by Windows to these sandboxes, allowing the tests to manipulate and access | 148 // by Windows to these sandboxes, allowing the tests to manipulate and access |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 data.push_back(base::ByteSwapToLE16(L'\0')); | 617 data.push_back(base::ByteSwapToLE16(L'\0')); |
618 | 618 |
619 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(base::char16), | 619 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(base::char16), |
620 reinterpret_cast<uint8_t*>(data.data())); | 620 reinterpret_cast<uint8_t*>(data.data())); |
621 } | 621 } |
622 | 622 |
623 void PRegTestHarness::AppendPolicyToPRegFile(const base::string16& path, | 623 void PRegTestHarness::AppendPolicyToPRegFile(const base::string16& path, |
624 const std::string& key, | 624 const std::string& key, |
625 const base::Value* value) { | 625 const base::Value* value) { |
626 switch (value->GetType()) { | 626 switch (value->GetType()) { |
627 case base::Value::TYPE_BOOLEAN: { | 627 case base::Value::Type::BOOLEAN: { |
628 bool boolean_value = false; | 628 bool boolean_value = false; |
629 ASSERT_TRUE(value->GetAsBoolean(&boolean_value)); | 629 ASSERT_TRUE(value->GetAsBoolean(&boolean_value)); |
630 AppendDWORDToPRegFile(path, key, boolean_value); | 630 AppendDWORDToPRegFile(path, key, boolean_value); |
631 break; | 631 break; |
632 } | 632 } |
633 case base::Value::TYPE_INTEGER: { | 633 case base::Value::Type::INTEGER: { |
634 int int_value = 0; | 634 int int_value = 0; |
635 ASSERT_TRUE(value->GetAsInteger(&int_value)); | 635 ASSERT_TRUE(value->GetAsInteger(&int_value)); |
636 AppendDWORDToPRegFile(path, key, int_value); | 636 AppendDWORDToPRegFile(path, key, int_value); |
637 break; | 637 break; |
638 } | 638 } |
639 case base::Value::TYPE_DOUBLE: { | 639 case base::Value::Type::DOUBLE: { |
640 double double_value = 0; | 640 double double_value = 0; |
641 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 641 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
642 AppendStringToPRegFile(path, key, base::DoubleToString(double_value)); | 642 AppendStringToPRegFile(path, key, base::DoubleToString(double_value)); |
643 break; | 643 break; |
644 } | 644 } |
645 case base::Value::TYPE_STRING: { | 645 case base::Value::Type::STRING: { |
646 std::string string_value; | 646 std::string string_value; |
647 ASSERT_TRUE(value->GetAsString(&string_value)); | 647 ASSERT_TRUE(value->GetAsString(&string_value)); |
648 AppendStringToPRegFile(path, key, string_value); | 648 AppendStringToPRegFile(path, key, string_value); |
649 break; | 649 break; |
650 } | 650 } |
651 case base::Value::TYPE_DICTIONARY: { | 651 case base::Value::Type::DICTIONARY: { |
652 base::string16 subpath = path + kPathSep + UTF8ToUTF16(key); | 652 base::string16 subpath = path + kPathSep + UTF8ToUTF16(key); |
653 const base::DictionaryValue* dict = NULL; | 653 const base::DictionaryValue* dict = NULL; |
654 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 654 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
655 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); | 655 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); |
656 entry.Advance()) { | 656 entry.Advance()) { |
657 AppendPolicyToPRegFile(subpath, entry.key(), &entry.value()); | 657 AppendPolicyToPRegFile(subpath, entry.key(), &entry.value()); |
658 } | 658 } |
659 break; | 659 break; |
660 } | 660 } |
661 case base::Value::TYPE_LIST: { | 661 case base::Value::Type::LIST: { |
662 base::string16 subpath = path + kPathSep + UTF8ToUTF16(key); | 662 base::string16 subpath = path + kPathSep + UTF8ToUTF16(key); |
663 const base::ListValue* list = NULL; | 663 const base::ListValue* list = NULL; |
664 ASSERT_TRUE(value->GetAsList(&list)); | 664 ASSERT_TRUE(value->GetAsList(&list)); |
665 for (size_t i = 0; i < list->GetSize(); ++i) { | 665 for (size_t i = 0; i < list->GetSize(); ++i) { |
666 const base::Value* entry = NULL; | 666 const base::Value* entry = NULL; |
667 ASSERT_TRUE(list->Get(i, &entry)); | 667 ASSERT_TRUE(list->Get(i, &entry)); |
668 AppendPolicyToPRegFile(subpath, base::IntToString(i + 1), entry); | 668 AppendPolicyToPRegFile(subpath, base::IntToString(i + 1), entry); |
669 } | 669 } |
670 break; | 670 break; |
671 } | 671 } |
672 case base::Value::TYPE_BINARY: | 672 case base::Value::Type::BINARY: |
673 case base::Value::TYPE_NULL: { | 673 case base::Value::Type::NONE: { |
674 ADD_FAILURE(); | 674 ADD_FAILURE(); |
675 break; | 675 break; |
676 } | 676 } |
677 } | 677 } |
678 } | 678 } |
679 | 679 |
680 } // namespace | 680 } // namespace |
681 | 681 |
682 // Instantiate abstract test case for basic policy reading tests. | 682 // Instantiate abstract test case for basic policy reading tests. |
683 INSTANTIATE_TEST_CASE_P( | 683 INSTANTIATE_TEST_CASE_P( |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, | 1185 expected.Get(ns_a).LoadFrom(&expected_a, POLICY_LEVEL_MANDATORY, |
1186 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1186 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1187 base::DictionaryValue expected_b; | 1187 base::DictionaryValue expected_b; |
1188 expected_b.SetInteger("policy 1", 2); | 1188 expected_b.SetInteger("policy 1", 2); |
1189 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, | 1189 expected.Get(ns_b).LoadFrom(&expected_b, POLICY_LEVEL_MANDATORY, |
1190 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); | 1190 POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM); |
1191 EXPECT_TRUE(Matches(expected)); | 1191 EXPECT_TRUE(Matches(expected)); |
1192 } | 1192 } |
1193 | 1193 |
1194 } // namespace policy | 1194 } // namespace policy |
OLD | NEW |