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

Side by Side Diff: net/filter/brotli_filter_unittest.cc

Issue 1616863002: Fix division by zero. Test filter with empty output. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 | « net/filter/brotli_filter.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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/filter/brotli_filter.h" 9 #include "net/filter/brotli_filter.h"
10 #include "net/filter/mock_filter_context.h" 10 #include "net/filter/mock_filter_context.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 filter_->FlushStreamBuffer(encoded_len()); 150 filter_->FlushStreamBuffer(encoded_len());
151 151
152 ASSERT_GE(kDefaultBufferSize, source_len()); 152 ASSERT_GE(kDefaultBufferSize, source_len());
153 char decode_buffer[kDefaultBufferSize]; 153 char decode_buffer[kDefaultBufferSize];
154 int decode_size = kDefaultBufferSize; 154 int decode_size = kDefaultBufferSize;
155 filter_->ReadData(decode_buffer, &decode_size); 155 filter_->ReadData(decode_buffer, &decode_size);
156 156
157 // Compare the decoding result with source data 157 // Compare the decoding result with source data
158 EXPECT_EQ(source_len(), decode_size); 158 EXPECT_EQ(source_len(), decode_size);
159 EXPECT_EQ(memcmp(source_buffer(), decode_buffer, source_len()), 0); 159 EXPECT_EQ(memcmp(source_buffer(), decode_buffer, source_len()), 0);
160 filter_.reset(nullptr);
cbentzel 2016/01/21 10:42:59 Why is this needed? Shouldn't the scoped_ptr be go
eustas 2016/01/21 10:49:14 It is for making destruction an explicit part of t
160 } 161 }
161 162
162 // Tests we can call filter repeatedly to get all the data decoded. 163 // Tests we can call filter repeatedly to get all the data decoded.
163 // To do that, we create a filter with a small buffer that can not hold all 164 // To do that, we create a filter with a small buffer that can not hold all
164 // the input data. 165 // the input data.
165 TEST_F(BrotliUnitTest, DecodeWithSmallBuffer) { 166 TEST_F(BrotliUnitTest, DecodeWithSmallBuffer) {
166 InitFilterWithBufferSize(kSmallBufferSize); 167 InitFilterWithBufferSize(kSmallBufferSize);
167 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size()); 168 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size());
168 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), 169 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
169 encoded_buffer(), encoded_len(), 170 encoded_buffer(), encoded_len(),
170 kDefaultBufferSize); 171 kDefaultBufferSize);
172 filter_.reset(nullptr);
171 } 173 }
172 174
173 // Tests we can still decode with just 1 byte buffer in the filter. 175 // Tests we can still decode with just 1 byte buffer in the filter.
174 // The purpose of this test: sometimes the filter will consume input without 176 // The purpose of this test: sometimes the filter will consume input without
175 // generating output. Verify filter can handle it correctly. 177 // generating output. Verify filter can handle it correctly.
176 TEST_F(BrotliUnitTest, DecodeWithOneByteBuffer) { 178 TEST_F(BrotliUnitTest, DecodeWithOneByteBuffer) {
177 InitFilterWithBufferSize(1); 179 InitFilterWithBufferSize(1);
178 EXPECT_EQ(1, filter_->stream_buffer_size()); 180 EXPECT_EQ(1, filter_->stream_buffer_size());
179 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), 181 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
180 encoded_buffer(), encoded_len(), 182 encoded_buffer(), encoded_len(),
181 kDefaultBufferSize); 183 kDefaultBufferSize);
184 filter_.reset(nullptr);
182 } 185 }
183 186
184 // Tests we can decode when caller has small buffer to read out from filter. 187 // Tests we can decode when caller has small buffer to read out from filter.
185 TEST_F(BrotliUnitTest, DecodeWithSmallOutputBuffer) { 188 TEST_F(BrotliUnitTest, DecodeWithSmallOutputBuffer) {
186 InitFilter(); 189 InitFilter();
187 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), 190 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
188 encoded_buffer(), encoded_len(), kSmallBufferSize); 191 encoded_buffer(), encoded_len(), kSmallBufferSize);
189 } 192 }
190 193
191 // Tests we can decode when caller has small buffer and input is also broken 194 // Tests we can decode when caller has small buffer and input is also broken
192 // into small parts. This may uncover some corner cases that doesn't happen with 195 // into small parts. This may uncover some corner cases that doesn't happen with
193 // one-byte buffers. 196 // one-byte buffers.
194 TEST_F(BrotliUnitTest, DecodeWithSmallInputAndOutputBuffer) { 197 TEST_F(BrotliUnitTest, DecodeWithSmallInputAndOutputBuffer) {
195 InitFilterWithBufferSize(kSmallBufferSize); 198 InitFilterWithBufferSize(kSmallBufferSize);
196 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), 199 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
197 encoded_buffer(), encoded_len(), kSmallBufferSize); 200 encoded_buffer(), encoded_len(), kSmallBufferSize);
201 filter_.reset(nullptr);
198 } 202 }
199 203
200 // Tests we can still decode with just 1 byte buffer in the filter and just 1 204 // Tests we can still decode with just 1 byte buffer in the filter and just 1
201 // byte buffer in the caller. 205 // byte buffer in the caller.
202 TEST_F(BrotliUnitTest, DecodeWithOneByteInputAndOutputBuffer) { 206 TEST_F(BrotliUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
203 InitFilterWithBufferSize(1); 207 InitFilterWithBufferSize(1);
204 EXPECT_EQ(1, filter_->stream_buffer_size()); 208 EXPECT_EQ(1, filter_->stream_buffer_size());
205 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), 209 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
206 encoded_buffer(), encoded_len(), 1); 210 encoded_buffer(), encoded_len(), 1);
211 filter_.reset(nullptr);
207 } 212 }
208 213
209 // Decoding deflate stream with corrupted data. 214 // Decoding brotli stream with corrupted data.
210 TEST_F(BrotliUnitTest, DecodeCorruptedData) { 215 TEST_F(BrotliUnitTest, DecodeCorruptedData) {
211 char corrupt_data[kDefaultBufferSize]; 216 char corrupt_data[kDefaultBufferSize];
212 int corrupt_data_len = encoded_len(); 217 int corrupt_data_len = encoded_len();
213 memcpy(corrupt_data, encoded_buffer(), encoded_len()); 218 memcpy(corrupt_data, encoded_buffer(), encoded_len());
214 219
215 int pos = corrupt_data_len / 2; 220 int pos = corrupt_data_len / 2;
216 corrupt_data[pos] = !corrupt_data[pos]; 221 corrupt_data[pos] = !corrupt_data[pos];
217 222
218 // Decode the corrupted data with filter 223 // Decode the corrupted data with filter.
219 InitFilter(); 224 InitFilter();
220 char corrupt_decode_buffer[kDefaultBufferSize]; 225 char corrupt_decode_buffer[kDefaultBufferSize];
221 int corrupt_decode_size = kDefaultBufferSize; 226 int corrupt_decode_size = kDefaultBufferSize;
222 227
223 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len, 228 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
224 corrupt_decode_buffer, &corrupt_decode_size); 229 corrupt_decode_buffer, &corrupt_decode_size);
225 230
226 // Expect failures 231 // Expect failures.
227 EXPECT_EQ(Filter::FILTER_ERROR, code); 232 EXPECT_EQ(Filter::FILTER_ERROR, code);
233 filter_.reset(nullptr);
228 } 234 }
229 235
230 // Decoding deflate stream with missing data. 236 // Decoding brotli stream with missing data.
231 TEST_F(BrotliUnitTest, DecodeMissingData) { 237 TEST_F(BrotliUnitTest, DecodeMissingData) {
232 char corrupt_data[kDefaultBufferSize]; 238 char corrupt_data[kDefaultBufferSize];
233 int corrupt_data_len = encoded_len(); 239 int corrupt_data_len = encoded_len();
234 memcpy(corrupt_data, encoded_buffer(), encoded_len()); 240 memcpy(corrupt_data, encoded_buffer(), encoded_len());
235 241
236 int pos = corrupt_data_len / 2; 242 int pos = corrupt_data_len / 2;
237 int len = corrupt_data_len - pos - 1; 243 int len = corrupt_data_len - pos - 1;
238 memmove(&corrupt_data[pos], &corrupt_data[pos + 1], len); 244 memmove(&corrupt_data[pos], &corrupt_data[pos + 1], len);
239 --corrupt_data_len; 245 --corrupt_data_len;
240 246
241 // Decode the corrupted data with filter 247 // Decode the corrupted data with filter.
242 InitFilter(); 248 InitFilter();
243 char corrupt_decode_buffer[kDefaultBufferSize]; 249 char corrupt_decode_buffer[kDefaultBufferSize];
244 int corrupt_decode_size = kDefaultBufferSize; 250 int corrupt_decode_size = kDefaultBufferSize;
245 251
246 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len, 252 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
247 corrupt_decode_buffer, &corrupt_decode_size); 253 corrupt_decode_buffer, &corrupt_decode_size);
248 254
249 // Expect failures 255 // Expect failures.
250 EXPECT_EQ(Filter::FILTER_ERROR, code); 256 EXPECT_EQ(Filter::FILTER_ERROR, code);
257 filter_.reset(nullptr);
258 }
259
260 // Decoding brotli stream with empty output data.
261 TEST_F(BrotliUnitTest, DecodeEmptyData) {
262 char data[1] = {6}; // WBITS = 16, ISLAST = 1, ISLASTEMPTY = 1
263 int data_len = 1;
264
265 InitFilter();
266 char decode_buffer[kDefaultBufferSize];
267 int decode_size = kDefaultBufferSize;
268 int code = DecodeAllWithFilter(filter_.get(), data, data_len, decode_buffer,
269 &decode_size);
270
271 // Expect success / empty output.
272 EXPECT_EQ(Filter::FILTER_DONE, code);
273 EXPECT_EQ(0, decode_size);
274 filter_.reset(nullptr);
251 } 275 }
252 276
253 } // namespace net 277 } // namespace net
OLDNEW
« no previous file with comments | « net/filter/brotli_filter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698