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

Side by Side Diff: components/subresource_filter/core/common/unindexed_ruleset_unittest.cc

Issue 2279803002: Fix UnindexedRulesetWriter finishing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add regression test. Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/subresource_filter/core/common/unindexed_ruleset.h" 5 #include "components/subresource_filter/core/common/unindexed_ruleset.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 DISALLOW_COPY_AND_ASSIGN(UnindexedRulesetTestBuilder); 111 DISALLOW_COPY_AND_ASSIGN(UnindexedRulesetTestBuilder);
112 }; 112 };
113 113
114 bool IsRulesetValid(const std::string& ruleset_contents, 114 bool IsRulesetValid(const std::string& ruleset_contents,
115 const std::vector<proto::UrlRule>& expected_url_rules) { 115 const std::vector<proto::UrlRule>& expected_url_rules) {
116 google::protobuf::io::ArrayInputStream array_input(ruleset_contents.data(), 116 google::protobuf::io::ArrayInputStream array_input(ruleset_contents.data(),
117 ruleset_contents.size()); 117 ruleset_contents.size());
118 UnindexedRulesetReader reader(&array_input); 118 UnindexedRulesetReader reader(&array_input);
119 proto::FilteringRules chunk; 119 proto::FilteringRules chunk;
120 std::vector<proto::UrlRule> read_rules; 120 std::vector<proto::UrlRule> read_rules;
121 while (reader.ReadNextChunk(&chunk)) { 121 while (reader.ReadNextChunk(&chunk)) {
engedy 2016/08/26 15:43:17 The test below only ensures that either of these s
pkalinnikov1 2016/08/26 16:29:47 Done.
122 read_rules.insert(read_rules.end(), chunk.url_rules().begin(), 122 read_rules.insert(read_rules.end(), chunk.url_rules().begin(),
123 chunk.url_rules().end()); 123 chunk.url_rules().end());
124 } 124 }
125 125
126 if (expected_url_rules.size() != read_rules.size()) 126 if (expected_url_rules.size() != read_rules.size())
127 return false; 127 return false;
128 for (size_t i = 0, size = read_rules.size(); i != size; ++i) { 128 for (size_t i = 0, size = read_rules.size(); i != size; ++i) {
129 if (!IsEqual(expected_url_rules[i], read_rules[i])) 129 if (!IsEqual(expected_url_rules[i], read_rules[i]))
130 return false; 130 return false;
131 } 131 }
132 return true; 132 return true;
133 } 133 }
134 134
135 } // namespace 135 } // namespace
136 136
137 TEST(UnindexedRulesetTest, EmptyRuleset) { 137 TEST(UnindexedRulesetTest, EmptyRuleset) {
138 UnindexedRulesetTestBuilder builder; 138 UnindexedRulesetTestBuilder builder;
139 EXPECT_TRUE(builder.Finish()); 139 EXPECT_TRUE(builder.Finish());
140 EXPECT_TRUE(IsRulesetValid(builder.ruleset_contents(), builder.url_rules())); 140 EXPECT_TRUE(IsRulesetValid(builder.ruleset_contents(), builder.url_rules()));
141 } 141 }
142 142
143 // The following test addresses a bug, that the UnindexedRulesetWriter didn't
engedy 2016/08/26 15:43:17 Let's just call a spade a spade + phrasing suggest
pkalinnikov1 2016/08/26 16:29:47 Done.
144 // trim the output coded stream on Finish, causing the output had some extra
145 // uninitialized bytes at the end.
146 TEST(UnindexedRulesetTest, EmptyRulesetRegression) {
147 size_t contents_size_after_finish = 0;
148 std::string contents;
149
150 {
151 google::protobuf::io::StringOutputStream output(&contents);
152 UnindexedRulesetWriter ruleset_writer(&output);
153 EXPECT_TRUE(ruleset_writer.Finish());
engedy 2016/08/26 15:43:17 Hang on, is this going to allocate a single byte o
pkalinnikov1 2016/08/26 16:29:47 No, the size of the contents will be 0. I could ad
154 contents_size_after_finish = contents.size();
155 }
156
157 // The |contents| shouldn't be resized after writer/stream destruction.
158 EXPECT_EQ(contents_size_after_finish, contents.size());
159 }
160
143 TEST(UnindexedRulesetTest, OneUrlRule) { 161 TEST(UnindexedRulesetTest, OneUrlRule) {
144 UnindexedRulesetTestBuilder builder; 162 UnindexedRulesetTestBuilder builder;
145 EXPECT_TRUE(builder.AddUrlRule(UrlPattern("example.com"), 163 EXPECT_TRUE(builder.AddUrlRule(UrlPattern("example.com"),
146 proto::SOURCE_TYPE_THIRD_PARTY, false)); 164 proto::SOURCE_TYPE_THIRD_PARTY, false));
147 EXPECT_TRUE(builder.Finish()); 165 EXPECT_TRUE(builder.Finish());
148 EXPECT_TRUE(IsRulesetValid(builder.ruleset_contents(), builder.url_rules())); 166 EXPECT_TRUE(IsRulesetValid(builder.ruleset_contents(), builder.url_rules()));
149 } 167 }
150 168
151 TEST(UnindexedRulesetTest, ManyUrlRules) { 169 TEST(UnindexedRulesetTest, ManyUrlRules) {
152 UnindexedRulesetTestBuilder builder; 170 UnindexedRulesetTestBuilder builder;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 207
190 { 208 {
191 std::string ruleset_contents = builder.ruleset_contents(); 209 std::string ruleset_contents = builder.ruleset_contents();
192 ASSERT_GT(ruleset_contents.size(), static_cast<size_t>(100)); 210 ASSERT_GT(ruleset_contents.size(), static_cast<size_t>(100));
193 ruleset_contents.resize(100); 211 ruleset_contents.resize(100);
194 EXPECT_FALSE(IsRulesetValid(ruleset_contents, builder.url_rules())); 212 EXPECT_FALSE(IsRulesetValid(ruleset_contents, builder.url_rules()));
195 } 213 }
196 } 214 }
197 215
198 } // namespace subresource_filter 216 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698