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

Unified Diff: components/policy/core/common/preg_parser_unittest.cc

Issue 2791193005: Add fuzzer test for preg_parser (Closed)
Patch Set: Can't char string concat base::FilePath::value()s on Windows since file paths are UTF16 there. Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: components/policy/core/common/preg_parser_unittest.cc
diff --git a/components/policy/core/common/preg_parser_unittest.cc b/components/policy/core/common/preg_parser_unittest.cc
index 757ac70e62ef13b65ba266688a3042098cb3651f..2f05fb09914570a47fa1de3528e6bd4adb9d5a70 100644
--- a/components/policy/core/common/preg_parser_unittest.cc
+++ b/components/policy/core/common/preg_parser_unittest.cc
@@ -22,6 +22,10 @@ namespace policy {
namespace preg_parser {
namespace {
+const char kRegistryPolFile[] = "chrome/test/data/policy/registry.pol";
+const char kRegistryKey[] = "SOFTWARE\\Policies\\Chromium";
+const char kPartialRegistryKey[] = "SOFTWARE\\Policies\\Chro";
+
// Check whether two RegistryDicts equal each other.
testing::AssertionResult RegistryDictEquals(const RegistryDict& a,
const RegistryDict& b) {
@@ -30,15 +34,20 @@ testing::AssertionResult RegistryDictEquals(const RegistryDict& a,
for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end();
++iter_key_a, ++iter_key_b) {
if (iter_key_a->first != iter_key_b->first) {
- return testing::AssertionFailure()
- << "Key mismatch " << iter_key_a->first
- << " vs. " << iter_key_b->first;
+ return testing::AssertionFailure() << "Key mismatch " << iter_key_a->first
+ << " vs. " << iter_key_b->first;
}
- testing::AssertionResult result = RegistryDictEquals(*iter_key_a->second,
- *iter_key_b->second);
+ testing::AssertionResult result =
+ RegistryDictEquals(*iter_key_a->second, *iter_key_b->second);
if (!result)
return result;
}
+ if (iter_key_a != a.keys().end())
+ return testing::AssertionFailure()
+ << "key mismatch, a has extra key " << iter_key_a->first;
+ if (iter_key_b != b.keys().end())
+ return testing::AssertionFailure()
+ << "key mismatch, b has extra key " << iter_key_b->first;
auto iter_value_a = a.values().begin();
auto iter_value_b = b.values().begin();
@@ -53,19 +62,25 @@ testing::AssertionResult RegistryDictEquals(const RegistryDict& a,
<< "=" << *iter_value_b->second.get();
}
}
+ if (iter_value_a != a.values().end())
+ return testing::AssertionFailure()
+ << "Value mismatch, a has extra value " << iter_value_a->first << "="
+ << *iter_value_a->second.get();
+ if (iter_value_b != b.values().end())
+ return testing::AssertionFailure()
+ << "Value mismatch, b has extra value " << iter_value_b->first << "="
+ << *iter_value_b->second.get();
return testing::AssertionSuccess();
}
-void SetInteger(RegistryDict* dict,
- const std::string& name,
- int value) {
+void SetInteger(RegistryDict* dict, const std::string& name, int value) {
dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value)));
}
void SetString(RegistryDict* dict,
const std::string& name,
- const std::string& value) {
+ const std::string& value) {
dict->SetValue(name, base::WrapUnique<base::Value>(new base::Value(value)));
}
@@ -90,12 +105,10 @@ TEST(PRegParserTest, TestParseFile) {
dict.SetKey("DelValsTest", std::move(subdict));
// Run the parser.
- base::FilePath test_file(
- test_data_dir.AppendASCII("chrome/test/data/policy/registry.pol"));
+ base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile));
PolicyLoadStatusSample status;
- ASSERT_TRUE(preg_parser::ReadFile(
- test_file, base::ASCIIToUTF16("SOFTWARE\\Policies\\Chromium"),
- &dict, &status));
+ ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey),
+ &dict, &status));
// Build the expected output dictionary.
RegistryDict expected;
@@ -116,6 +129,29 @@ TEST(PRegParserTest, TestParseFile) {
EXPECT_TRUE(RegistryDictEquals(dict, expected));
}
+TEST(PRegParserTest, SubstringRootInvalid) {
+ // A root of "alpha/beta/gamma" should not be considered a valid root for a
+ // key like "alpha/beta/gammadelta".
+ base::FilePath test_data_dir;
+ ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir));
+
+ base::FilePath test_file(test_data_dir.AppendASCII(kRegistryPolFile));
+ RegistryDict empty;
+ PolicyLoadStatusSample status;
+
+ // No data should be loaded for key = kPartialRegistryKey.
+ RegistryDict dict1;
+ ASSERT_TRUE(preg_parser::ReadFile(
+ test_file, base::ASCIIToUTF16(kPartialRegistryKey), &dict1, &status));
+ EXPECT_TRUE(RegistryDictEquals(dict1, empty));
+
+ // Safety check with kRegistryKey (dict should not be empty).
+ RegistryDict dict2;
+ ASSERT_TRUE(preg_parser::ReadFile(test_file, base::ASCIIToUTF16(kRegistryKey),
+ &dict2, &status));
+ EXPECT_FALSE(RegistryDictEquals(dict2, empty));
+}
+
} // namespace
} // namespace preg_parser
} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698