Commit f97a51c3 authored by Charlie Harrison's avatar Charlie Harrison Committed by Commit Bot

Add ruleset_converter test utils

This is a pre-req to a refactoring effort splitting off logic
from main.cc and adding new tests.

This CL also changes rule_stream_test.cc -> rule_stream_unittest.cc

Bug: 833419
Change-Id: I9c73a336c2bae2fdbdbe7db68346c8c9a64be325
Reviewed-on: https://chromium-review.googlesource.com/1040045
Commit-Queue: Charlie Harrison <csharrison@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555486}
parent 5419f892
......@@ -24,7 +24,9 @@ source_set("support") {
source_set("unit_tests") {
testonly = true
sources = [
"rule_stream_test.cc",
"rule_stream_unittest.cc",
"ruleset_test_util.cc",
"ruleset_test_util.h",
]
deps = [
":support",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/subresource_filter/tools/ruleset_converter/ruleset_test_util.h"
#include <fstream>
#include <ios>
#include <istream>
#include "base/files/file_util.h"
#include "components/subresource_filter/tools/rule_parser/rule_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace subresource_filter {
TestRulesetContents::TestRulesetContents() = default;
TestRulesetContents::~TestRulesetContents() = default;
TestRulesetContents::TestRulesetContents(const TestRulesetContents& other)
: url_rules(other.url_rules), css_rules(other.css_rules) {}
void TestRulesetContents::AppendRules(
const std::vector<std::string>& text_rules,
bool allow_errors) {
RuleParser parser;
for (const std::string& text_rule : text_rules) {
url_pattern_index::proto::RuleType rule_type = parser.Parse(text_rule);
switch (rule_type) {
case url_pattern_index::proto::RULE_TYPE_URL:
url_rules.push_back(parser.url_rule().ToProtobuf());
break;
case url_pattern_index::proto::RULE_TYPE_CSS:
css_rules.push_back(parser.css_rule().ToProtobuf());
break;
case url_pattern_index::proto::RULE_TYPE_UNSPECIFIED:
ASSERT_TRUE(allow_errors);
break;
default:
ASSERT_TRUE(false);
}
}
}
void TestRulesetContents::AppendParsedRules(const TestRulesetContents& other) {
url_rules.insert(url_rules.end(), other.url_rules.begin(),
other.url_rules.end());
css_rules.insert(css_rules.end(), other.css_rules.begin(),
other.css_rules.end());
}
bool TestRulesetContents::operator==(const TestRulesetContents& other) const {
if (url_rules.size() != other.url_rules.size() ||
css_rules.size() != other.css_rules.size()) {
return false;
}
for (size_t i = 0; i < url_rules.size(); ++i) {
if (!AreUrlRulesEqual(url_rules[i], other.url_rules[i]))
return false;
}
for (size_t i = 0; i < css_rules.size(); ++i) {
if (!AreCssRulesEqual(css_rules[i], other.css_rules[i]))
return false;
}
return true;
}
std::ostream& operator<<(std::ostream& out,
const TestRulesetContents& contents) {
for (const auto& rule : contents.url_rules) {
out << ToString(rule) << "\n";
}
for (const auto& rule : contents.css_rules) {
out << ToString(rule) << "\n";
}
return out;
}
ScopedTempRulesetFile::ScopedTempRulesetFile(RulesetFormat format)
: format_(format) {
// Cannot ASSERT due to returning in constructor.
CHECK(scoped_dir_.CreateUniqueTempDir());
CHECK(base::CreateTemporaryFileInDir(scoped_dir_.GetPath(), &ruleset_path_));
}
ScopedTempRulesetFile::~ScopedTempRulesetFile() = default;
std::unique_ptr<RuleOutputStream> ScopedTempRulesetFile::OpenForOutput() const {
return RuleOutputStream::Create(
std::make_unique<std::ofstream>(ruleset_path().MaybeAsASCII(),
std::ios::binary | std::ios::out),
format());
}
// Opens the |ruleset_file| with already existing ruleset and returns the
// corresponding input stream, or nullptr if it failed to be created.
std::unique_ptr<RuleInputStream> ScopedTempRulesetFile::OpenForInput() const {
return RuleInputStream::Create(
std::make_unique<std::ifstream>(ruleset_path().MaybeAsASCII(),
std::ios::binary | std::ios::in),
format());
}
void ScopedTempRulesetFile::WriteRuleset(
const TestRulesetContents& contents) const {
std::unique_ptr<RuleOutputStream> output = OpenForOutput();
ASSERT_NE(nullptr, output);
for (const auto& rule : contents.url_rules)
EXPECT_TRUE(output->PutUrlRule(rule));
for (const auto& rule : contents.css_rules)
EXPECT_TRUE(output->PutCssRule(rule));
EXPECT_TRUE(output->Finish());
}
TestRulesetContents ScopedTempRulesetFile::ReadContents() const {
std::unique_ptr<RuleInputStream> input = OpenForInput();
TestRulesetContents contents;
url_pattern_index::proto::RuleType rule_type =
url_pattern_index::proto::RULE_TYPE_UNSPECIFIED;
while ((rule_type = input->FetchNextRule()) !=
url_pattern_index::proto::RULE_TYPE_UNSPECIFIED) {
if (rule_type == url_pattern_index::proto::RULE_TYPE_URL) {
contents.url_rules.push_back(input->GetUrlRule());
} else {
CHECK_EQ(url_pattern_index::proto::RULE_TYPE_CSS, rule_type);
contents.css_rules.push_back(input->GetCssRule());
}
}
return contents;
}
bool AreUrlRulesEqual(const url_pattern_index::proto::UrlRule& first,
const url_pattern_index::proto::UrlRule& second) {
return first.SerializeAsString() == second.SerializeAsString();
}
bool AreCssRulesEqual(const url_pattern_index::proto::CssRule& first,
const url_pattern_index::proto::CssRule& second) {
return first.SerializeAsString() == second.SerializeAsString();
}
} // namespace subresource_filter
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SUBRESOURCE_FILTER_TOOLS_RULESET_CONVERTER_RULESET_TEST_UTIL_H_
#define COMPONENTS_SUBRESOURCE_FILTER_TOOLS_RULESET_CONVERTER_RULESET_TEST_UTIL_H_
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "components/subresource_filter/tools/ruleset_converter/rule_stream.h"
#include "components/subresource_filter/tools/ruleset_converter/ruleset_format.h"
#include "components/url_pattern_index/proto/rules.pb.h"
namespace subresource_filter {
// Stores lists of the rules that a test ruleset consists of.
struct TestRulesetContents {
TestRulesetContents();
TestRulesetContents(const TestRulesetContents& other);
~TestRulesetContents();
std::vector<url_pattern_index::proto::UrlRule> url_rules;
std::vector<url_pattern_index::proto::CssRule> css_rules;
// Parses |text_rules| and appends them to the |ruleset|.
void AppendRules(const std::vector<std::string>& text_rules,
bool allow_errors = false);
// Extends this TestRulesetContents rules with rules from |other|.
void AppendParsedRules(const TestRulesetContents& other);
bool operator==(const TestRulesetContents& other) const;
};
std::ostream& operator<<(std::ostream& out,
const TestRulesetContents& contents);
// Stores identification information about a temporary File with a ruleset.
// Deletes the file automatically on destruction.
class ScopedTempRulesetFile {
public:
// Creates a temporary file of the specified |format|.
explicit ScopedTempRulesetFile(RulesetFormat format);
~ScopedTempRulesetFile();
// Opens the |ruleset_file| and creates an empty rule output stream to this
// file. Returns the stream or nullptr if it failed to be created.
std::unique_ptr<RuleOutputStream> OpenForOutput() const;
// Opens the |ruleset_file| and returns the corresponding input stream, or
// nullptr if it failed to be created.
std::unique_ptr<RuleInputStream> OpenForInput() const;
// Opens the |ruleset_file|, and writes the test ruleset |contents| to it in
// the corresponding format.
void WriteRuleset(const TestRulesetContents& contents) const;
TestRulesetContents ReadContents() const;
const base::FilePath& ruleset_path() const { return ruleset_path_; }
RulesetFormat format() const { return format_; }
private:
base::ScopedTempDir scoped_dir_;
base::FilePath ruleset_path_;
const RulesetFormat format_; // The format of the |file|.
DISALLOW_COPY_AND_ASSIGN(ScopedTempRulesetFile);
};
bool AreUrlRulesEqual(const url_pattern_index::proto::UrlRule& first,
const url_pattern_index::proto::UrlRule& second);
bool AreCssRulesEqual(const url_pattern_index::proto::CssRule& first,
const url_pattern_index::proto::CssRule& second);
} // namespace subresource_filter
#endif // COMPONENTS_SUBRESOURCE_FILTER_TOOLS_RULESET_CONVERTER_RULESET_TEST_UTIL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment