| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/preg_parser.h" | 5 #include "components/policy/core/common/preg_parser.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "components/policy/core/common/policy_load_status.h" | 17 #include "components/policy/core/common/policy_load_status.h" |
| 18 #include "components/policy/core/common/registry_dict.h" | 18 #include "components/policy/core/common/registry_dict.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace policy { | 21 namespace policy { |
| 22 namespace preg_parser { | 22 namespace preg_parser { |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kRegistryPolFile[] = "chrome/test/data/policy/registry.pol"; |
| 26 const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium"; |
| 27 const char kPartialRegistryKey[] = "SOFTWARE\\Policies\\Chro"; |
| 28 |
| 25 // Check whether two RegistryDicts equal each other. | 29 // Check whether two RegistryDicts equal each other. |
| 26 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, | 30 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, |
| 27 const RegistryDict& b) { | 31 const RegistryDict& b) { |
| 28 auto iter_key_a = a.keys().begin(); | 32 auto iter_key_a = a.keys().begin(); |
| 29 auto iter_key_b = b.keys().begin(); | 33 auto iter_key_b = b.keys().begin(); |
| 30 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); | 34 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); |
| 31 ++iter_key_a, ++iter_key_b) { | 35 ++iter_key_a, ++iter_key_b) { |
| 32 if (iter_key_a->first != iter_key_b->first) { | 36 if (iter_key_a->first != iter_key_b->first) { |
| 33 return testing::AssertionFailure() | 37 return testing::AssertionFailure() << "Key mismatch " << iter_key_a->first |
| 34 << "Key mismatch " << iter_key_a->first | 38 << " vs. " << iter_key_b->first; |
| 35 << " vs. " << iter_key_b->first; | |
| 36 } | 39 } |
| 37 testing::AssertionResult result = RegistryDictEquals(*iter_key_a->second, | 40 testing::AssertionResult result = |
| 38 *iter_key_b->second); | 41 RegistryDictEquals(*iter_key_a->second, *iter_key_b->second); |
| 39 if (!result) | 42 if (!result) |
| 40 return result; | 43 return result; |
| 41 } | 44 } |
| 45 if (iter_key_a != a.keys().end()) |
| 46 return testing::AssertionFailure() |
| 47 << "key mismatch, a has extra key " << iter_key_a->first; |
| 48 if (iter_key_b != b.keys().end()) |
| 49 return testing::AssertionFailure() |
| 50 << "key mismatch, b has extra key " << iter_key_b->first; |
| 42 | 51 |
| 43 auto iter_value_a = a.values().begin(); | 52 auto iter_value_a = a.values().begin(); |
| 44 auto iter_value_b = b.values().begin(); | 53 auto iter_value_b = b.values().begin(); |
| 45 for (; iter_value_a != a.values().end() && iter_value_b != b.values().end(); | 54 for (; iter_value_a != a.values().end() && iter_value_b != b.values().end(); |
| 46 ++iter_value_a, ++iter_value_b) { | 55 ++iter_value_a, ++iter_value_b) { |
| 47 if (iter_value_a->first != iter_value_b->first || | 56 if (iter_value_a->first != iter_value_b->first || |
| 48 !base::Value::Equals(iter_value_a->second.get(), | 57 !base::Value::Equals(iter_value_a->second.get(), |
| 49 iter_value_b->second.get())) { | 58 iter_value_b->second.get())) { |
| 50 return testing::AssertionFailure() | 59 return testing::AssertionFailure() |
| 51 << "Value mismatch " << iter_value_a->first << "=" | 60 << "Value mismatch " << iter_value_a->first << "=" |
| 52 << *iter_value_a->second.get() << " vs. " << iter_value_b->first | 61 << *iter_value_a->second.get() << " vs. " << iter_value_b->first |
| 53 << "=" << *iter_value_b->second.get(); | 62 << "=" << *iter_value_b->second.get(); |
| 54 } | 63 } |
| 55 } | 64 } |
| 65 if (iter_value_a != a.values().end()) |
| 66 return testing::AssertionFailure() |
| 67 << "Value mismatch, a has extra value " << iter_value_a->first << "=" |
| 68 << *iter_value_a->second.get(); |
| 69 if (iter_value_b != b.values().end()) |
| 70 return testing::AssertionFailure() |
| 71 << "Value mismatch, b has extra value " << iter_value_b->first << "=" |
| 72 << *iter_value_b->second.get(); |
| 56 | 73 |
| 57 return testing::AssertionSuccess(); | 74 return testing::AssertionSuccess(); |
| 58 } | 75 } |
| 59 | 76 |
| 60 void SetInteger(RegistryDict* dict, | 77 void SetInteger(RegistryDict* dict, const std::string& name, int value) { |
| 61 const std::string& name, | |
| 62 int value) { | |
| 63 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); | 78 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); |
| 64 } | 79 } |
| 65 | 80 |
| 66 void SetString(RegistryDict* dict, | 81 void SetString(RegistryDict* dict, |
| 67 const std::string& name, | 82 const std::string& name, |
| 68 const std::string& value) { | 83 const std::string& value) { |
| 69 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); | 84 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); |
| 70 } | 85 } |
| 71 | 86 |
| 72 TEST(PRegParserTest, TestParseFile) { | 87 TEST(PRegParserTest, TestParseFile) { |
| 73 base::FilePath test_data_dir; | 88 base::FilePath test_data_dir; |
| 74 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | 89 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); |
| 75 | 90 |
| 76 // Prepare the test dictionary with some data so the test can check that the | 91 // Prepare the test dictionary with some data so the test can check that the |
| 77 // PReg action triggers work, i.e. remove these items. | 92 // PReg action triggers work, i.e. remove these items. |
| 78 RegistryDict dict; | 93 RegistryDict dict; |
| 79 SetInteger(&dict, "DeleteValuesTest1", 1); | 94 SetInteger(&dict, "DeleteValuesTest1", 1); |
| 80 SetString(&dict, "DeleteValuesTest2", "2"); | 95 SetString(&dict, "DeleteValuesTest2", "2"); |
| 81 dict.SetKey("DeleteKeysTest1", base::MakeUnique<RegistryDict>()); | 96 dict.SetKey("DeleteKeysTest1", base::MakeUnique<RegistryDict>()); |
| 82 std::unique_ptr<RegistryDict> delete_keys_test(new RegistryDict()); | 97 std::unique_ptr<RegistryDict> delete_keys_test(new RegistryDict()); |
| 83 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); | 98 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); |
| 84 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); | 99 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); |
| 85 SetInteger(&dict, "DelTest", 1); | 100 SetInteger(&dict, "DelTest", 1); |
| 86 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 101 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
| 87 SetInteger(subdict.get(), "DelValsTest1", 1); | 102 SetInteger(subdict.get(), "DelValsTest1", 1); |
| 88 SetString(subdict.get(), "DelValsTest2", "2"); | 103 SetString(subdict.get(), "DelValsTest2", "2"); |
| 89 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | 104 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); |
| 90 dict.SetKey("DelValsTest", std::move(subdict)); | 105 dict.SetKey("DelValsTest", std::move(subdict)); |
| 91 | 106 |
| 92 // Run the parser. | 107 // Run the parser. |
| 93 base::FilePath test_file( | 108 base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile)); |
| 94 test_data_dir.AppendASCII("chrome/test/data/policy/registry.pol")); | |
| 95 PolicyLoadStatusSample status; | 109 PolicyLoadStatusSample status; |
| 96 ASSERT_TRUE(preg_parser::ReadFile( | 110 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 97 test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chromium"), | 111 &dict, &status)); |
| 98 &dict, &status)); | |
| 99 | 112 |
| 100 // Build the expected output dictionary. | 113 // Build the expected output dictionary. |
| 101 RegistryDict expected; | 114 RegistryDict expected; |
| 102 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); | 115 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); |
| 103 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | 116 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); |
| 104 expected.SetKey("DelValsTest", std::move(del_vals_dict)); | 117 expected.SetKey("DelValsTest", std::move(del_vals_dict)); |
| 105 SetInteger(&expected, "HomepageIsNewTabPage", 1); | 118 SetInteger(&expected, "HomepageIsNewTabPage", 1); |
| 106 SetString(&expected, "HomepageLocation", "http://www.example.com"); | 119 SetString(&expected, "HomepageLocation", "http://www.example.com"); |
| 107 SetInteger(&expected, "RestoreOnStartup", 4); | 120 SetInteger(&expected, "RestoreOnStartup", 4); |
| 108 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); | 121 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); |
| 109 SetString(startup_urls.get(), "1", "http://www.chromium.org"); | 122 SetString(startup_urls.get(), "1", "http://www.chromium.org"); |
| 110 SetString(startup_urls.get(), "2", "http://www.example.com"); | 123 SetString(startup_urls.get(), "2", "http://www.example.com"); |
| 111 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); | 124 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); |
| 112 SetInteger(&expected, "ShowHomeButton", 1); | 125 SetInteger(&expected, "ShowHomeButton", 1); |
| 113 SetString(&expected, "Snowman", "\xE2\x98\x83"); | 126 SetString(&expected, "Snowman", "\xE2\x98\x83"); |
| 114 SetString(&expected, "Empty", ""); | 127 SetString(&expected, "Empty", ""); |
| 115 | 128 |
| 116 EXPECT_TRUE(RegistryDictEquals(dict, expected)); | 129 EXPECT_TRUE(RegistryDictEquals(dict, expected)); |
| 117 } | 130 } |
| 118 | 131 |
| 132 TEST(PRegParserTest, SubstringRootInvalid) { |
| 133 // A root of "alpha/beta/gamma" should not be considered a valid root for a |
| 134 // key like "alpha/beta/gammadelta". |
| 135 base::FilePath test_data_dir; |
| 136 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); |
| 137 |
| 138 base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile)); |
| 139 RegistryDict empty; |
| 140 PolicyLoadStatusSample status; |
| 141 |
| 142 // No data should be loaded for key = kPartialRegistryKey. |
| 143 RegistryDict dict1; |
| 144 ASSERT_TRUE(preg_parser::ReadFile( |
| 145 test_file, base::ASCIIToUTF16(kPartialRegistryKey), &dict1, &status)); |
| 146 EXPECT_TRUE(RegistryDictEquals(dict1, empty)); |
| 147 |
| 148 // Safety check with kRegistryKey (dict should not be empty). |
| 149 RegistryDict dict2; |
| 150 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 151 &dict2, &status)); |
| 152 EXPECT_FALSE(RegistryDictEquals(dict2, empty)); |
| 153 } |
| 154 |
| 119 } // namespace | 155 } // namespace |
| 120 } // namespace preg_parser | 156 } // namespace preg_parser |
| 121 } // namespace policy | 157 } // namespace policy |
| OLD | NEW |