| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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. | |
| 4 | |
| 5 #include "components/policy/core/common/preg_parser_win.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/base_paths.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/values.h" | |
| 16 #include "components/policy/core/common/policy_load_status.h" | |
| 17 #include "components/policy/core/common/registry_dict_win.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace policy { | |
| 21 namespace preg_parser { | |
| 22 namespace { | |
| 23 | |
| 24 // Check whether two RegistryDicts equal each other. | |
| 25 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, | |
| 26 const RegistryDict& b) { | |
| 27 auto iter_key_a = a.keys().begin(); | |
| 28 auto iter_key_b = b.keys().begin(); | |
| 29 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); | |
| 30 ++iter_key_a, ++iter_key_b) { | |
| 31 if (iter_key_a->first != iter_key_b->first) { | |
| 32 return testing::AssertionFailure() | |
| 33 << "Key mismatch " << iter_key_a->first | |
| 34 << " vs. " << iter_key_b->first; | |
| 35 } | |
| 36 testing::AssertionResult result = RegistryDictEquals(*iter_key_a->second, | |
| 37 *iter_key_b->second); | |
| 38 if (!result) | |
| 39 return result; | |
| 40 } | |
| 41 | |
| 42 auto iter_value_a = a.values().begin(); | |
| 43 auto iter_value_b = b.values().begin(); | |
| 44 for (; iter_value_a != a.values().end() && iter_value_b != b.values().end(); | |
| 45 ++iter_value_a, ++iter_value_b) { | |
| 46 if (iter_value_a->first != iter_value_b->first || | |
| 47 !base::Value::Equals(iter_value_a->second.get(), | |
| 48 iter_value_b->second.get())) { | |
| 49 return testing::AssertionFailure() | |
| 50 << "Value mismatch " << iter_value_a->first << "=" | |
| 51 << *iter_value_a->second.get() << " vs. " << iter_value_b->first | |
| 52 << "=" << *iter_value_b->second.get(); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 return testing::AssertionSuccess(); | |
| 57 } | |
| 58 | |
| 59 void SetInteger(RegistryDict* dict, | |
| 60 const std::string& name, | |
| 61 int value) { | |
| 62 dict->SetValue( | |
| 63 name, base::WrapUnique<base::Value>(new base::FundamentalValue(value))); | |
| 64 } | |
| 65 | |
| 66 void SetString(RegistryDict* dict, | |
| 67 const std::string& name, | |
| 68 const std::string& value) { | |
| 69 dict->SetValue(name, | |
| 70 base::WrapUnique<base::Value>(new base::StringValue(value))); | |
| 71 } | |
| 72 | |
| 73 TEST(PRegParserWinTest, TestParseFile) { | |
| 74 base::FilePath test_data_dir; | |
| 75 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 76 | |
| 77 // Prepare the test dictionary with some data so the test can check that the | |
| 78 // PReg action triggers work, i.e. remove these items. | |
| 79 RegistryDict dict; | |
| 80 SetInteger(&dict, "DeleteValuesTest1", 1); | |
| 81 SetString(&dict, "DeleteValuesTest2", "2"); | |
| 82 dict.SetKey("DeleteKeysTest1", base::MakeUnique<RegistryDict>()); | |
| 83 std::unique_ptr<RegistryDict> delete_keys_test(new RegistryDict()); | |
| 84 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); | |
| 85 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); | |
| 86 SetInteger(&dict, "DelTest", 1); | |
| 87 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | |
| 88 SetInteger(subdict.get(), "DelValsTest1", 1); | |
| 89 SetString(subdict.get(), "DelValsTest2", "2"); | |
| 90 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | |
| 91 dict.SetKey("DelValsTest", std::move(subdict)); | |
| 92 | |
| 93 // Run the parser. | |
| 94 base::FilePath test_file( | |
| 95 test_data_dir.AppendASCII("chrome/test/data/policy/registry.pol")); | |
| 96 PolicyLoadStatusSample status; | |
| 97 ASSERT_TRUE(preg_parser::ReadFile( | |
| 98 test_file, L"SOFTWARE\\Policies\\Chromium", &dict, &status)); | |
| 99 | |
| 100 // Build the expected output dictionary. | |
| 101 RegistryDict expected; | |
| 102 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); | |
| 103 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | |
| 104 expected.SetKey("DelValsTest", std::move(del_vals_dict)); | |
| 105 SetInteger(&expected, "HomepageIsNewTabPage", 1); | |
| 106 SetString(&expected, "HomepageLocation", "http://www.example.com"); | |
| 107 SetInteger(&expected, "RestoreOnStartup", 4); | |
| 108 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); | |
| 109 SetString(startup_urls.get(), "1", "http://www.chromium.org"); | |
| 110 SetString(startup_urls.get(), "2", "http://www.example.com"); | |
| 111 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); | |
| 112 SetInteger(&expected, "ShowHomeButton", 1); | |
| 113 SetString(&expected, "Snowman", "\xE2\x98\x83"); | |
| 114 SetString(&expected, "Empty", ""); | |
| 115 | |
| 116 EXPECT_TRUE(RegistryDictEquals(dict, expected)); | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| 120 } // namespace preg_parser | |
| 121 } // namespace policy | |
| OLD | NEW |