| Index: components/subresource_filter/core/common/test_ruleset_creator.h
|
| diff --git a/components/subresource_filter/core/common/test_ruleset_creator.h b/components/subresource_filter/core/common/test_ruleset_creator.h
|
| index 6bc69de9a366341bff68a696fa7b54d0b24a5f3f..bb8851cee72ff34e5a54d01a3e795dadfe951919 100644
|
| --- a/components/subresource_filter/core/common/test_ruleset_creator.h
|
| +++ b/components/subresource_filter/core/common/test_ruleset_creator.h
|
| @@ -10,6 +10,7 @@
|
| #include <vector>
|
|
|
| #include "base/files/file.h"
|
| +#include "base/files/file_path.h"
|
| #include "base/files/scoped_temp_dir.h"
|
| #include "base/macros.h"
|
| #include "base/strings/string_piece.h"
|
| @@ -17,36 +18,55 @@
|
| namespace subresource_filter {
|
| namespace testing {
|
|
|
| +// Encapsulates a testing subresource filtering ruleset serialized either in
|
| +// indexed or unindexed format. The ruleset |contents| can be accessed directly
|
| +// as a byte buffer, as well as through the file |path| pointing to a temporary
|
| +// file that is cleaned up when the TestRulesetCreator is destroyed.
|
| +struct TestRuleset {
|
| + TestRuleset();
|
| + ~TestRuleset();
|
| +
|
| + // Convenience function to open a read-only file handle to |ruleset|.
|
| + static base::File Open(const TestRuleset& ruleset);
|
| +
|
| + std::vector<uint8_t> contents;
|
| + base::FilePath path;
|
| +};
|
| +
|
| +// Encapsulates the same ruleset in both indexed and unindexed formats.
|
| +struct TestRulesetPair {
|
| + TestRulesetPair();
|
| + ~TestRulesetPair();
|
| +
|
| + TestRuleset unindexed;
|
| + TestRuleset indexed;
|
| +};
|
| +
|
| // Helper class to create subresource filtering rulesets for testing.
|
| +//
|
| +// All temporary files corresponding to TestRulesets are cleaned up when the
|
| +// instance goes out of scope, but file handles already open can still be used
|
| +// and read even after this.
|
| class TestRulesetCreator {
|
| public:
|
| TestRulesetCreator();
|
| ~TestRulesetCreator();
|
|
|
| - // Creates a testing ruleset to disallow subresource loads from URL paths
|
| - // having the given |suffix|, and replaces the |buffer| with the ruleset data.
|
| - //
|
| - // Enclose the call to this method in ASSERT_NO_FATAL_FAILURE to detect
|
| - // errors.
|
| - static void CreateRulesetToDisallowURLsWithPathSuffix(
|
| + // Creates a testing ruleset comprised of a single filtering rule that
|
| + // disallows subresource loads from URL paths having the given |suffix|.
|
| + // Enclose call in ASSERT_NO_FATAL_FAILURE to detect errors.
|
| + void CreateRulesetToDisallowURLsWithPathSuffix(
|
| base::StringPiece suffix,
|
| - std::vector<uint8_t>* buffer);
|
| -
|
| - // Same as above, but puts the ruleset into a file and returns its read-only
|
| - // file handle in |ruleset_file|.
|
| - //
|
| - // The underlying temporary file will be deleted when the TestRulesetCreator
|
| - // instance goes out of scope, but the |ruleset_file| handle may outlive and
|
| - // can still be used even after the destruction.
|
| - //
|
| - // Enclose the call to this method in ASSERT_NO_FATAL_FAILURE to detect
|
| - // errors.
|
| - void CreateRulesetFileToDisallowURLsWithPathSuffix(base::StringPiece suffix,
|
| - base::File* ruleset_file);
|
| + TestRulesetPair* test_ruleset_pair);
|
|
|
| private:
|
| + // Writes the |ruleset_contents| to a temporary file, and initializes
|
| + // |ruleset| to have the same |contents|, and the |path| to this file.
|
| + void CreateTestRulesetFromContents(std::vector<uint8_t> ruleset_contents,
|
| + TestRuleset* ruleset);
|
| +
|
| base::ScopedTempDir scoped_temp_dir_;
|
| - int next_ruleset_version_ = 1;
|
| + int next_unique_file_suffix = 1;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(TestRulesetCreator);
|
| };
|
|
|