| 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"; | 25 // Preg files are relative to |kRegistryPolBaseDir|. |
| 26 const char kRegistryPolBaseDir[] = "chrome/test/data/policy/gpo"; |
| 27 const char kRegistryPolFile[] = "parser_test/registry.pol"; |
| 28 const char kInvalidEncodingRegistryPolFile[] = "invalid_encoding/registry.pol"; |
| 29 |
| 26 const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium"; | 30 const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium"; |
| 27 | 31 |
| 28 // Check whether two RegistryDicts equal each other. | 32 // Check whether two RegistryDicts equal each other. |
| 29 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, | 33 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, |
| 30 const RegistryDict& b) { | 34 const RegistryDict& b) { |
| 31 auto iter_key_a = a.keys().begin(); | 35 auto iter_key_a = a.keys().begin(); |
| 32 auto iter_key_b = b.keys().begin(); | 36 auto iter_key_b = b.keys().begin(); |
| 33 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); | 37 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); |
| 34 ++iter_key_a, ++iter_key_b) { | 38 ++iter_key_a, ++iter_key_b) { |
| 35 if (iter_key_a->first != iter_key_b->first) { | 39 if (iter_key_a->first != iter_key_b->first) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 void SetInteger(RegistryDict* dict, const std::string& name, int value) { | 80 void SetInteger(RegistryDict* dict, const std::string& name, int value) { |
| 77 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); | 81 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void SetString(RegistryDict* dict, | 84 void SetString(RegistryDict* dict, |
| 81 const std::string& name, | 85 const std::string& name, |
| 82 const std::string& value) { | 86 const std::string& value) { |
| 83 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); | 87 dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value))); |
| 84 } | 88 } |
| 85 | 89 |
| 86 TEST(PRegParserTest, TestParseFile) { | 90 class PRegParserTest : public testing::Test { |
| 87 base::FilePath test_data_dir; | 91 protected: |
| 88 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | 92 void SetUp() override { |
| 93 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_)); |
| 94 test_data_dir_ = test_data_dir_.AppendASCII(kRegistryPolBaseDir); |
| 95 } |
| 89 | 96 |
| 97 base::FilePath test_data_dir_; |
| 98 }; |
| 99 |
| 100 TEST_F(PRegParserTest, TestParseFile) { |
| 90 // Prepare the test dictionary with some data so the test can check that the | 101 // Prepare the test dictionary with some data so the test can check that the |
| 91 // PReg action triggers work, i.e. remove these items. | 102 // PReg action triggers work, i.e. remove these items. |
| 92 RegistryDict dict; | 103 RegistryDict dict; |
| 93 SetInteger(&dict, "DeleteValuesTest1", 1); | 104 SetInteger(&dict, "DeleteValuesTest1", 1); |
| 94 SetString(&dict, "DeleteValuesTest2", "2"); | 105 SetString(&dict, "DeleteValuesTest2", "2"); |
| 95 dict.SetKey("DeleteKeysTest1", base::MakeUnique<RegistryDict>()); | 106 dict.SetKey("DeleteKeysTest1", base::MakeUnique<RegistryDict>()); |
| 96 std::unique_ptr<RegistryDict> delete_keys_test(new RegistryDict()); | 107 std::unique_ptr<RegistryDict> delete_keys_test(new RegistryDict()); |
| 97 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); | 108 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); |
| 98 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); | 109 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); |
| 99 SetInteger(&dict, "DelTest", 1); | 110 SetInteger(&dict, "DelTest", 1); |
| 100 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 111 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
| 101 SetInteger(subdict.get(), "DelValsTest1", 1); | 112 SetInteger(subdict.get(), "DelValsTest1", 1); |
| 102 SetString(subdict.get(), "DelValsTest2", "2"); | 113 SetString(subdict.get(), "DelValsTest2", "2"); |
| 103 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | 114 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); |
| 104 dict.SetKey("DelValsTest", std::move(subdict)); | 115 dict.SetKey("DelValsTest", std::move(subdict)); |
| 105 | 116 |
| 106 // Run the parser. | 117 // Run the parser. |
| 107 base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile)); | 118 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile)); |
| 108 PolicyLoadStatusSample status; | 119 PolicyLoadStatusSample status; |
| 109 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), | 120 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 110 &dict, &status)); | 121 &dict, &status)); |
| 111 | 122 |
| 112 // Build the expected output dictionary. | 123 // Build the expected output dictionary. |
| 113 RegistryDict expected; | 124 RegistryDict expected; |
| 114 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); | 125 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); |
| 115 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); | 126 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); |
| 116 expected.SetKey("DelValsTest", std::move(del_vals_dict)); | 127 expected.SetKey("DelValsTest", std::move(del_vals_dict)); |
| 117 SetInteger(&expected, "HomepageIsNewTabPage", 1); | 128 SetInteger(&expected, "HomepageIsNewTabPage", 1); |
| 118 SetString(&expected, "HomepageLocation", "http://www.example.com"); | 129 SetString(&expected, "HomepageLocation", "http://www.example.com"); |
| 119 SetInteger(&expected, "RestoreOnStartup", 4); | 130 SetInteger(&expected, "RestoreOnStartup", 4); |
| 120 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); | 131 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); |
| 121 SetString(startup_urls.get(), "1", "http://www.chromium.org"); | 132 SetString(startup_urls.get(), "1", "http://www.chromium.org"); |
| 122 SetString(startup_urls.get(), "2", "http://www.example.com"); | 133 SetString(startup_urls.get(), "2", "http://www.example.com"); |
| 123 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); | 134 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); |
| 124 SetInteger(&expected, "ShowHomeButton", 1); | 135 SetInteger(&expected, "ShowHomeButton", 1); |
| 125 SetString(&expected, "Snowman", "\xE2\x98\x83"); | 136 SetString(&expected, "Snowman", "\xE2\x98\x83"); |
| 126 SetString(&expected, "Empty", ""); | 137 SetString(&expected, "Empty", ""); |
| 127 | 138 |
| 128 EXPECT_TRUE(RegistryDictEquals(dict, expected)); | 139 EXPECT_TRUE(RegistryDictEquals(dict, expected)); |
| 129 } | 140 } |
| 130 | 141 |
| 131 TEST(PRegParserTest, SubstringRootInvalid) { | 142 TEST_F(PRegParserTest, SubstringRootInvalid) { |
| 132 // A root of "Aa/Bb/Cc" should not be considered a valid root for a | 143 // A root of "Aa/Bb/Cc" should not be considered a valid root for a |
| 133 // key like "Aa/Bb/C". | 144 // key like "Aa/Bb/C". |
| 134 base::FilePath test_data_dir; | 145 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile)); |
| 135 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 136 | |
| 137 base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile)); | |
| 138 RegistryDict empty; | 146 RegistryDict empty; |
| 139 PolicyLoadStatusSample status; | 147 PolicyLoadStatusSample status; |
| 140 | 148 |
| 141 // No data should be loaded for partial roots ("Aa/Bb/C"). | 149 // No data should be loaded for partial roots ("Aa/Bb/C"). |
| 142 RegistryDict dict1; | 150 RegistryDict dict1; |
| 143 ASSERT_TRUE(preg_parser::ReadFile( | 151 ASSERT_TRUE(preg_parser::ReadFile( |
| 144 test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chro"), &dict1, | 152 test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chro"), &dict1, |
| 145 &status)); | 153 &status)); |
| 146 EXPECT_TRUE(RegistryDictEquals(dict1, empty)); | 154 EXPECT_TRUE(RegistryDictEquals(dict1, empty)); |
| 147 | 155 |
| 148 // Safety check with kRegistryKey (dict should not be empty). | 156 // Safety check with kRegistryKey (dict should not be empty). |
| 149 RegistryDict dict2; | 157 RegistryDict dict2; |
| 150 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), | 158 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 151 &dict2, &status)); | 159 &dict2, &status)); |
| 152 EXPECT_FALSE(RegistryDictEquals(dict2, empty)); | 160 EXPECT_FALSE(RegistryDictEquals(dict2, empty)); |
| 153 } | 161 } |
| 154 | 162 |
| 163 TEST_F(PRegParserTest, RejectInvalidStrings) { |
| 164 // Tests whether strings with invalid characters are rejected. |
| 165 base::FilePath test_file( |
| 166 test_data_dir_.AppendASCII(kInvalidEncodingRegistryPolFile)); |
| 167 PolicyLoadStatusSample status; |
| 168 RegistryDict dict; |
| 169 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 170 &dict, &status)); |
| 171 |
| 172 RegistryDict empty; |
| 173 EXPECT_TRUE(RegistryDictEquals(dict, empty)); |
| 174 } |
| 175 |
| 155 } // namespace | 176 } // namespace |
| 156 } // namespace preg_parser | 177 } // namespace preg_parser |
| 157 } // namespace policy | 178 } // namespace policy |
| OLD | NEW |