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> |
| 9 #include <stdint.h> |
8 #include <userenv.h> | 10 #include <userenv.h> |
9 | 11 |
10 #include <algorithm> | 12 #include <algorithm> |
11 #include <cstring> | 13 #include <cstring> |
12 #include <functional> | 14 #include <functional> |
13 #include <iterator> | 15 #include <iterator> |
14 #include <vector> | 16 #include <vector> |
15 | 17 |
16 #include "base/base_paths.h" | 18 #include "base/base_paths.h" |
17 #include "base/callback.h" | 19 #include "base/callback.h" |
18 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
19 #include "base/files/file_util.h" | 21 #include "base/files/file_util.h" |
20 #include "base/files/scoped_temp_dir.h" | 22 #include "base/files/scoped_temp_dir.h" |
21 #include "base/json/json_writer.h" | 23 #include "base/json/json_writer.h" |
| 24 #include "base/macros.h" |
22 #include "base/path_service.h" | 25 #include "base/path_service.h" |
23 #include "base/process/process_handle.h" | 26 #include "base/process/process_handle.h" |
24 #include "base/strings/string16.h" | 27 #include "base/strings/string16.h" |
25 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
26 #include "base/strings/string_util.h" | 29 #include "base/strings/string_util.h" |
27 #include "base/strings/stringprintf.h" | 30 #include "base/strings/stringprintf.h" |
28 #include "base/strings/utf_string_conversions.h" | 31 #include "base/strings/utf_string_conversions.h" |
29 #include "base/sys_byteorder.h" | 32 #include "base/sys_byteorder.h" |
30 #include "base/win/registry.h" | 33 #include "base/win/registry.h" |
31 #include "base/win/win_util.h" | 34 #include "base/win/win_util.h" |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 LPCTSTR machine_name, | 251 LPCTSTR machine_name, |
249 PSID sid_user, | 252 PSID sid_user, |
250 GUID* extension_guid, | 253 GUID* extension_guid, |
251 PGROUP_POLICY_OBJECT* gpo_list) override; | 254 PGROUP_POLICY_OBJECT* gpo_list) override; |
252 BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) override; | 255 BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) override; |
253 | 256 |
254 // Creates a harness instance. | 257 // Creates a harness instance. |
255 static PolicyProviderTestHarness* Create(); | 258 static PolicyProviderTestHarness* Create(); |
256 | 259 |
257 private: | 260 private: |
258 // Helper to append a base::string16 to an uint8 buffer. | 261 // Helper to append a base::string16 to an uint8_t buffer. |
259 static void AppendChars(std::vector<uint8>* buffer, | 262 static void AppendChars(std::vector<uint8_t>* buffer, |
260 const base::string16& chars); | 263 const base::string16& chars); |
261 | 264 |
262 // Appends a record with the given fields to the PReg file. | 265 // Appends a record with the given fields to the PReg file. |
263 void AppendRecordToPRegFile(const base::string16& path, | 266 void AppendRecordToPRegFile(const base::string16& path, |
264 const std::string& key, | 267 const std::string& key, |
265 DWORD type, | 268 DWORD type, |
266 DWORD size, | 269 DWORD size, |
267 uint8* data); | 270 uint8_t* data); |
268 | 271 |
269 // Appends the given DWORD |value| for |path| + |key| to the PReg file. | 272 // Appends the given DWORD |value| for |path| + |key| to the PReg file. |
270 void AppendDWORDToPRegFile(const base::string16& path, | 273 void AppendDWORDToPRegFile(const base::string16& path, |
271 const std::string& key, | 274 const std::string& key, |
272 DWORD value); | 275 DWORD value); |
273 | 276 |
274 // Appends the given string |value| for |path| + |key| to the PReg file. | 277 // Appends the given string |value| for |path| + |key| to the PReg file. |
275 void AppendStringToPRegFile(const base::string16& path, | 278 void AppendStringToPRegFile(const base::string16& path, |
276 const std::string& key, | 279 const std::string& key, |
277 const std::string& value); | 280 const std::string& value); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 BOOL PRegTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) { | 555 BOOL PRegTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) { |
553 return TRUE; | 556 return TRUE; |
554 } | 557 } |
555 | 558 |
556 // static | 559 // static |
557 PolicyProviderTestHarness* PRegTestHarness::Create() { | 560 PolicyProviderTestHarness* PRegTestHarness::Create() { |
558 return new PRegTestHarness(); | 561 return new PRegTestHarness(); |
559 } | 562 } |
560 | 563 |
561 // static | 564 // static |
562 void PRegTestHarness::AppendChars(std::vector<uint8>* buffer, | 565 void PRegTestHarness::AppendChars(std::vector<uint8_t>* buffer, |
563 const base::string16& chars) { | 566 const base::string16& chars) { |
564 for (base::string16::const_iterator c(chars.begin()); c != chars.end(); ++c) { | 567 for (base::string16::const_iterator c(chars.begin()); c != chars.end(); ++c) { |
565 buffer->push_back(*c & 0xff); | 568 buffer->push_back(*c & 0xff); |
566 buffer->push_back((*c >> 8) & 0xff); | 569 buffer->push_back((*c >> 8) & 0xff); |
567 } | 570 } |
568 } | 571 } |
569 | 572 |
570 void PRegTestHarness::AppendRecordToPRegFile(const base::string16& path, | 573 void PRegTestHarness::AppendRecordToPRegFile(const base::string16& path, |
571 const std::string& key, | 574 const std::string& key, |
572 DWORD type, | 575 DWORD type, |
573 DWORD size, | 576 DWORD size, |
574 uint8* data) { | 577 uint8_t* data) { |
575 std::vector<uint8> buffer; | 578 std::vector<uint8_t> buffer; |
576 AppendChars(&buffer, L"["); | 579 AppendChars(&buffer, L"["); |
577 AppendChars(&buffer, path); | 580 AppendChars(&buffer, path); |
578 AppendChars(&buffer, base::string16(L"\0;", 2)); | 581 AppendChars(&buffer, base::string16(L"\0;", 2)); |
579 AppendChars(&buffer, UTF8ToUTF16(key)); | 582 AppendChars(&buffer, UTF8ToUTF16(key)); |
580 AppendChars(&buffer, base::string16(L"\0;", 2)); | 583 AppendChars(&buffer, base::string16(L"\0;", 2)); |
581 type = base::ByteSwapToLE32(type); | 584 type = base::ByteSwapToLE32(type); |
582 uint8* type_data = reinterpret_cast<uint8*>(&type); | 585 uint8_t* type_data = reinterpret_cast<uint8_t*>(&type); |
583 buffer.insert(buffer.end(), type_data, type_data + sizeof(DWORD)); | 586 buffer.insert(buffer.end(), type_data, type_data + sizeof(DWORD)); |
584 AppendChars(&buffer, L";"); | 587 AppendChars(&buffer, L";"); |
585 size = base::ByteSwapToLE32(size); | 588 size = base::ByteSwapToLE32(size); |
586 uint8* size_data = reinterpret_cast<uint8*>(&size); | 589 uint8_t* size_data = reinterpret_cast<uint8_t*>(&size); |
587 buffer.insert(buffer.end(), size_data, size_data + sizeof(DWORD)); | 590 buffer.insert(buffer.end(), size_data, size_data + sizeof(DWORD)); |
588 AppendChars(&buffer, L";"); | 591 AppendChars(&buffer, L";"); |
589 buffer.insert(buffer.end(), data, data + size); | 592 buffer.insert(buffer.end(), data, data + size); |
590 AppendChars(&buffer, L"]"); | 593 AppendChars(&buffer, L"]"); |
591 | 594 |
592 ASSERT_TRUE(base::AppendToFile(preg_file_path_, | 595 ASSERT_TRUE(base::AppendToFile(preg_file_path_, |
593 reinterpret_cast<const char*>(buffer.data()), | 596 reinterpret_cast<const char*>(buffer.data()), |
594 buffer.size())); | 597 buffer.size())); |
595 } | 598 } |
596 | 599 |
597 void PRegTestHarness::AppendDWORDToPRegFile(const base::string16& path, | 600 void PRegTestHarness::AppendDWORDToPRegFile(const base::string16& path, |
598 const std::string& key, | 601 const std::string& key, |
599 DWORD value) { | 602 DWORD value) { |
600 value = base::ByteSwapToLE32(value); | 603 value = base::ByteSwapToLE32(value); |
601 AppendRecordToPRegFile(path, key, REG_DWORD, sizeof(DWORD), | 604 AppendRecordToPRegFile(path, key, REG_DWORD, sizeof(DWORD), |
602 reinterpret_cast<uint8*>(&value)); | 605 reinterpret_cast<uint8_t*>(&value)); |
603 } | 606 } |
604 | 607 |
605 void PRegTestHarness::AppendStringToPRegFile(const base::string16& path, | 608 void PRegTestHarness::AppendStringToPRegFile(const base::string16& path, |
606 const std::string& key, | 609 const std::string& key, |
607 const std::string& value) { | 610 const std::string& value) { |
608 base::string16 string16_value(UTF8ToUTF16(value)); | 611 base::string16 string16_value(UTF8ToUTF16(value)); |
609 std::vector<base::char16> data; | 612 std::vector<base::char16> data; |
610 std::transform(string16_value.begin(), string16_value.end(), | 613 std::transform(string16_value.begin(), string16_value.end(), |
611 std::back_inserter(data), std::ptr_fun(base::ByteSwapToLE16)); | 614 std::back_inserter(data), std::ptr_fun(base::ByteSwapToLE16)); |
612 data.push_back(base::ByteSwapToLE16(L'\0')); | 615 data.push_back(base::ByteSwapToLE16(L'\0')); |
613 | 616 |
614 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(base::char16), | 617 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(base::char16), |
615 reinterpret_cast<uint8*>(data.data())); | 618 reinterpret_cast<uint8_t*>(data.data())); |
616 } | 619 } |
617 | 620 |
618 void PRegTestHarness::AppendPolicyToPRegFile(const base::string16& path, | 621 void PRegTestHarness::AppendPolicyToPRegFile(const base::string16& path, |
619 const std::string& key, | 622 const std::string& key, |
620 const base::Value* value) { | 623 const base::Value* value) { |
621 switch (value->GetType()) { | 624 switch (value->GetType()) { |
622 case base::Value::TYPE_BOOLEAN: { | 625 case base::Value::TYPE_BOOLEAN: { |
623 bool boolean_value = false; | 626 bool boolean_value = false; |
624 ASSERT_TRUE(value->GetAsBoolean(&boolean_value)); | 627 ASSERT_TRUE(value->GetAsBoolean(&boolean_value)); |
625 AppendDWORDToPRegFile(path, key, boolean_value); | 628 AppendDWORDToPRegFile(path, key, boolean_value); |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1233 expected_policy.Set("alternative_browser_path", | 1236 expected_policy.Set("alternative_browser_path", |
1234 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 1237 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
1235 POLICY_SOURCE_PLATFORM, | 1238 POLICY_SOURCE_PLATFORM, |
1236 new base::StringValue("c:\\legacy\\browser.exe"), NULL); | 1239 new base::StringValue("c:\\legacy\\browser.exe"), NULL); |
1237 expected_policy.Set("url_list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 1240 expected_policy.Set("url_list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
1238 POLICY_SOURCE_PLATFORM, list.DeepCopy(), nullptr); | 1241 POLICY_SOURCE_PLATFORM, list.DeepCopy(), nullptr); |
1239 EXPECT_TRUE(Matches(expected)); | 1242 EXPECT_TRUE(Matches(expected)); |
1240 } | 1243 } |
1241 | 1244 |
1242 } // namespace policy | 1245 } // namespace policy |
OLD | NEW |