| 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/bind.h" |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "components/policy/core/common/policy_load_status.h" | 18 #include "components/policy/core/common/policy_load_status.h" |
| 18 #include "components/policy/core/common/registry_dict.h" | 19 #include "components/policy/core/common/registry_dict.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace policy { | 22 namespace policy { |
| 22 namespace preg_parser { | 23 namespace preg_parser { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Preg files are relative to |kRegistryPolBaseDir|. | 26 // Preg files are relative to |kRegistryPolBaseDir|. |
| 26 const char kRegistryPolBaseDir[] = "chrome/test/data/policy/gpo"; | 27 const char kRegistryPolBaseDir[] = "chrome/test/data/policy/gpo"; |
| 27 const char kRegistryPolFile[] = "parser_test/registry.pol"; | 28 const char kRegistryPolFile[] = "parser_test/registry.pol"; |
| 28 const char kInvalidEncodingRegistryPolFile[] = "invalid_encoding/registry.pol"; | 29 const char kInvalidEncodingRegistryPolFile[] = "invalid_encoding/registry.pol"; |
| 30 const char kNonExistingRegistryPolFile[] = "does_not_exist.pol"; |
| 29 | 31 |
| 30 const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium"; | 32 const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium"; |
| 31 | 33 |
| 32 // Check whether two RegistryDicts equal each other. | 34 // Check whether two RegistryDicts equal each other. |
| 33 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, | 35 testing::AssertionResult RegistryDictEquals(const RegistryDict& a, |
| 34 const RegistryDict& b) { | 36 const RegistryDict& b) { |
| 35 auto iter_key_a = a.keys().begin(); | 37 auto iter_key_a = a.keys().begin(); |
| 36 auto iter_key_b = b.keys().begin(); | 38 auto iter_key_b = b.keys().begin(); |
| 37 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); | 39 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); |
| 38 ++iter_key_a, ++iter_key_b) { | 40 ++iter_key_a, ++iter_key_b) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 test_data_dir_.AppendASCII(kInvalidEncodingRegistryPolFile)); | 168 test_data_dir_.AppendASCII(kInvalidEncodingRegistryPolFile)); |
| 167 PolicyLoadStatusSample status; | 169 PolicyLoadStatusSample status; |
| 168 RegistryDict dict; | 170 RegistryDict dict; |
| 169 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), | 171 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), |
| 170 &dict, &status)); | 172 &dict, &status)); |
| 171 | 173 |
| 172 RegistryDict empty; | 174 RegistryDict empty; |
| 173 EXPECT_TRUE(RegistryDictEquals(dict, empty)); | 175 EXPECT_TRUE(RegistryDictEquals(dict, empty)); |
| 174 } | 176 } |
| 175 | 177 |
| 178 TEST_F(PRegParserTest, OverrideReportCallback) { |
| 179 // Tests overriding the PolicyLoadStatusSample's ReportCallback. |
| 180 std::bitset<POLICY_LOAD_STATUS_SIZE> status_bits; |
| 181 PolicyLoadStatusSample::ReportCallback report_callback = |
| 182 base::Bind([](std::bitset<POLICY_LOAD_STATUS_SIZE>* status_bits, |
| 183 PolicyLoadStatus status) { (*status_bits)[status] = true; }, |
| 184 &status_bits); |
| 185 |
| 186 { |
| 187 // Needs to be scoped because reporting happens in the destructor of |
| 188 // PolicyLoadStatusSample. |
| 189 PolicyLoadStatusSample status(report_callback); |
| 190 RegistryDict dict; |
| 191 base::FilePath test_file( |
| 192 test_data_dir_.AppendASCII(kNonExistingRegistryPolFile)); |
| 193 ASSERT_FALSE(preg_parser::ReadFile( |
| 194 test_file, base::ASCIIToUTF16(kRegistryKey), &dict, &status)); |
| 195 } |
| 196 |
| 197 std::bitset<POLICY_LOAD_STATUS_SIZE> expected_status_bits; |
| 198 expected_status_bits[POLICY_LOAD_STATUS_STARTED] = true; |
| 199 expected_status_bits[POLICY_LOAD_STATUS_READ_ERROR] = true; |
| 200 EXPECT_EQ(expected_status_bits, status_bits); |
| 201 } |
| 202 |
| 176 } // namespace | 203 } // namespace |
| 177 } // namespace preg_parser | 204 } // namespace preg_parser |
| 178 } // namespace policy | 205 } // namespace policy |
| OLD | NEW |