Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: components/policy/core/common/preg_parser_unittest.cc

Issue 2860973002: Allow PolicyLoadStatusSample to override reporting method (Closed)
Patch Set: Cleanups and class rename. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/policy/core/common/preg_parser_fuzzer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test)); 111 dict.SetKey("DeleteKeysTest2", std::move(delete_keys_test));
110 SetInteger(&dict, "DelTest", 1); 112 SetInteger(&dict, "DelTest", 1);
111 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); 113 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
112 SetInteger(subdict.get(), "DelValsTest1", 1); 114 SetInteger(subdict.get(), "DelValsTest1", 1);
113 SetString(subdict.get(), "DelValsTest2", "2"); 115 SetString(subdict.get(), "DelValsTest2", "2");
114 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); 116 subdict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>());
115 dict.SetKey("DelValsTest", std::move(subdict)); 117 dict.SetKey("DelValsTest", std::move(subdict));
116 118
117 // Run the parser. 119 // Run the parser.
118 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile)); 120 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile));
119 PolicyLoadStatusSample status; 121 PolicyLoadStatusUmaReporter status;
120 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), 122 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey),
121 &dict, &status)); 123 &dict, &status));
122 124
123 // Build the expected output dictionary. 125 // Build the expected output dictionary.
124 RegistryDict expected; 126 RegistryDict expected;
125 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict()); 127 std::unique_ptr<RegistryDict> del_vals_dict(new RegistryDict());
126 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>()); 128 del_vals_dict->SetKey("DelValsTest3", base::MakeUnique<RegistryDict>());
127 expected.SetKey("DelValsTest", std::move(del_vals_dict)); 129 expected.SetKey("DelValsTest", std::move(del_vals_dict));
128 SetInteger(&expected, "HomepageIsNewTabPage", 1); 130 SetInteger(&expected, "HomepageIsNewTabPage", 1);
129 SetString(&expected, "HomepageLocation", "http://www.example.com"); 131 SetString(&expected, "HomepageLocation", "http://www.example.com");
130 SetInteger(&expected, "RestoreOnStartup", 4); 132 SetInteger(&expected, "RestoreOnStartup", 4);
131 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict()); 133 std::unique_ptr<RegistryDict> startup_urls(new RegistryDict());
132 SetString(startup_urls.get(), "1", "http://www.chromium.org"); 134 SetString(startup_urls.get(), "1", "http://www.chromium.org");
133 SetString(startup_urls.get(), "2", "http://www.example.com"); 135 SetString(startup_urls.get(), "2", "http://www.example.com");
134 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls)); 136 expected.SetKey("RestoreOnStartupURLs", std::move(startup_urls));
135 SetInteger(&expected, "ShowHomeButton", 1); 137 SetInteger(&expected, "ShowHomeButton", 1);
136 SetString(&expected, "Snowman", "\xE2\x98\x83"); 138 SetString(&expected, "Snowman", "\xE2\x98\x83");
137 SetString(&expected, "Empty", ""); 139 SetString(&expected, "Empty", "");
138 140
139 EXPECT_TRUE(RegistryDictEquals(dict, expected)); 141 EXPECT_TRUE(RegistryDictEquals(dict, expected));
140 } 142 }
141 143
142 TEST_F(PRegParserTest, SubstringRootInvalid) { 144 TEST_F(PRegParserTest, SubstringRootInvalid) {
143 // A root of "Aa/Bb/Cc" should not be considered a valid root for a 145 // A root of "Aa/Bb/Cc" should not be considered a valid root for a
144 // key like "Aa/Bb/C". 146 // key like "Aa/Bb/C".
145 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile)); 147 base::FilePath test_file(test_data_dir_.AppendASCII(kRegistryPolFile));
146 RegistryDict empty; 148 RegistryDict empty;
147 PolicyLoadStatusSample status; 149 PolicyLoadStatusUmaReporter status;
148 150
149 // No data should be loaded for partial roots ("Aa/Bb/C"). 151 // No data should be loaded for partial roots ("Aa/Bb/C").
150 RegistryDict dict1; 152 RegistryDict dict1;
151 ASSERT_TRUE(preg_parser::ReadFile( 153 ASSERT_TRUE(preg_parser::ReadFile(
152 test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chro"), &dict1, 154 test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chro"), &dict1,
153 &status)); 155 &status));
154 EXPECT_TRUE(RegistryDictEquals(dict1, empty)); 156 EXPECT_TRUE(RegistryDictEquals(dict1, empty));
155 157
156 // Safety check with kRegistryKey (dict should not be empty). 158 // Safety check with kRegistryKey (dict should not be empty).
157 RegistryDict dict2; 159 RegistryDict dict2;
158 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey), 160 ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey),
159 &dict2, &status)); 161 &dict2, &status));
160 EXPECT_FALSE(RegistryDictEquals(dict2, empty)); 162 EXPECT_FALSE(RegistryDictEquals(dict2, empty));
161 } 163 }
162 164
163 TEST_F(PRegParserTest, RejectInvalidStrings) { 165 TEST_F(PRegParserTest, RejectInvalidStrings) {
164 // Tests whether strings with invalid characters are rejected. 166 // Tests whether strings with invalid characters are rejected.
165 base::FilePath test_file( 167 base::FilePath test_file(
166 test_data_dir_.AppendASCII(kInvalidEncodingRegistryPolFile)); 168 test_data_dir_.AppendASCII(kInvalidEncodingRegistryPolFile));
167 PolicyLoadStatusSample status; 169 PolicyLoadStatusUmaReporter 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, LoadStatusSampling) {
179 // Tests load status sampling.
180 PolicyLoadStatusUmaReporter status;
181 RegistryDict dict;
182 base::FilePath test_file(
183 test_data_dir_.AppendASCII(kNonExistingRegistryPolFile));
184 ASSERT_FALSE(preg_parser::ReadFile(
185 test_file, base::ASCIIToUTF16(kRegistryKey), &dict, &status));
186
187 PolicyLoadStatusSampler::StatusSet expected_status_set;
188 expected_status_set[POLICY_LOAD_STATUS_STARTED] = true;
189 expected_status_set[POLICY_LOAD_STATUS_READ_ERROR] = true;
190 EXPECT_EQ(expected_status_set, status.GetStatusSet());
191 }
192
176 } // namespace 193 } // namespace
177 } // namespace preg_parser 194 } // namespace preg_parser
178 } // namespace policy 195 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/preg_parser_fuzzer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698