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

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

Issue 1431723002: Add brotli content-encoding filter. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nit Created 5 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/files/file_util.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/path_service.h"
8 #include "net/base/io_buffer.h"
9 #include "net/filter/brotli_filter.h"
10 #include "net/filter/mock_filter_context.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h"
13
14 namespace {
15 const int kDefaultBufferSize = 4096;
16 const int kSmallBufferSize = 128;
17 } // namespace
18
19 namespace net {
20
21 // These tests use the path service, which uses autoreleased objects on the
22 // Mac, so this needs to be a PlatformTest.
23 class BrotliUnitTest : public PlatformTest {
24 protected:
25 void SetUp() override {
26 PlatformTest::SetUp();
27
28 // Get the path of data directory.
29 base::FilePath data_dir;
30 PathService::Get(base::DIR_SOURCE_ROOT, &data_dir);
31 data_dir = data_dir.AppendASCII("net");
32 data_dir = data_dir.AppendASCII("data");
33 data_dir = data_dir.AppendASCII("filter_unittests");
34
35 // Read data from the original file into buffer.
36 base::FilePath file_path;
37 file_path = data_dir.AppendASCII("google.txt");
38 ASSERT_TRUE(base::ReadFileToString(file_path, &source_buffer_));
39
40 // Read data from the encoded file into buffer.
41 base::FilePath encoded_file_path;
42 encoded_file_path = data_dir.AppendASCII("google.br");
43 ASSERT_TRUE(base::ReadFileToString(encoded_file_path, &encoded_buffer_));
44 ASSERT_GE(kDefaultBufferSize, static_cast<int>(encoded_buffer_.size()));
45 }
46
47 void TearDown() override { PlatformTest::TearDown(); }
Randy Smith (Not in Mondays) 2015/12/07 22:27:28 nit, suggestion: I don't personally feel like putt
eustas 2015/12/08 11:31:31 Ooops, didn't notice this (copied from zip filter
48
49 // Use filter to decode compressed data, and compare the decoded result with
50 // the orginal data.
51 // Parameters: |source| and |source_len| are original data and its size.
52 // |encoded_source| and |encoded_source_len| are compressed data and its size.
53 // |output_buffer_size| specifies the size of buffer to read out data from
54 // filter.
55 void DecodeAndCompareWithFilter(Filter* filter,
56 const char* source,
57 int source_len,
58 const char* encoded_source,
59 int encoded_source_len,
60 int output_buffer_size) {
61 // Make sure we have enough space to hold the decoding output.
62 ASSERT_GE(kDefaultBufferSize, source_len);
63 ASSERT_GE(kDefaultBufferSize, output_buffer_size);
64
65 char decode_buffer[kDefaultBufferSize];
66 char* decode_next = decode_buffer;
67 int decode_avail_size = kDefaultBufferSize;
68
69 const char* encode_next = encoded_source;
70 int encode_avail_size = encoded_source_len;
71
72 int code = Filter::FILTER_OK;
73 while (code != Filter::FILTER_DONE) {
74 int encode_data_len =
75 std::min(encode_avail_size, filter->stream_buffer_size());
76 memcpy(filter->stream_buffer()->data(), encode_next, encode_data_len);
77 filter->FlushStreamBuffer(encode_data_len);
78 encode_next += encode_data_len;
79 encode_avail_size -= encode_data_len;
80
81 while (true) {
82 int decode_data_len = std::min(decode_avail_size, output_buffer_size);
83
84 code = filter->ReadData(decode_next, &decode_data_len);
85 decode_next += decode_data_len;
86 decode_avail_size -= decode_data_len;
87
88 ASSERT_NE(Filter::FILTER_ERROR, code);
89
90 if (code == Filter::FILTER_NEED_MORE_DATA ||
91 code == Filter::FILTER_DONE) {
92 break;
93 }
94 }
95 }
96
97 // Compare the decoding result with source data
98 int decode_total_data_len = kDefaultBufferSize - decode_avail_size;
99 EXPECT_EQ(source_len, decode_total_data_len);
100 EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0);
101 }
102
103 // Unsafe function to use filter to decode compressed data.
104 // Parameters: |source| and |source_len| are compressed data and its size.
105 // |dest| is the buffer for decoding results. Upon entry, |*dest_len| is the
106 // size of the output buffer. Upon exit, |*dest_len| is the number of chars
107 // written into the buffer.
108 int DecodeAllWithFilter(Filter* filter,
109 const char* source,
110 int source_len,
111 char* dest,
112 int* dest_len) {
113 memcpy(filter->stream_buffer()->data(), source, source_len);
114 filter->FlushStreamBuffer(source_len);
115 return filter->ReadData(dest, dest_len);
116 }
117
118 void InitFilter() {
119 std::vector<Filter::FilterType> filter_types;
120 filter_types.push_back(Filter::FILTER_TYPE_BROTLI);
121 filter_.reset(Filter::Factory(filter_types, filter_context_));
122 ASSERT_TRUE(filter_.get());
123 ASSERT_LE(kDefaultBufferSize, filter_->stream_buffer_size());
124 }
125
126 void InitFilterWithBufferSize(int buffer_size) {
127 std::vector<Filter::FilterType> filter_types;
128 filter_types.push_back(Filter::FILTER_TYPE_BROTLI);
129 filter_.reset(
130 Filter::FactoryForTests(filter_types, filter_context_, buffer_size));
131 ASSERT_TRUE(filter_.get());
132 }
133
134 const char* source_buffer() const { return source_buffer_.data(); }
135 int source_len() const { return static_cast<int>(source_buffer_.size()); }
136
137 const char* encoded_buffer() const { return encoded_buffer_.data(); }
138 int encoded_len() const { return static_cast<int>(encoded_buffer_.size()); }
139
140 scoped_ptr<Filter> filter_;
141
142 private:
143 MockFilterContext filter_context_;
144 std::string source_buffer_;
145 std::string encoded_buffer_;
146 };
147
148 // Basic scenario: decoding gzip data with big enough buffer.
Randy Smith (Not in Mondays) 2015/12/07 22:27:28 nit: Shouldn't this be "decoding *brotli* data ...
eustas 2015/12/08 11:31:31 Done.
149 TEST_F(BrotliUnitTest, DecodeBrotli) {
150 InitFilter();
151 memcpy(filter_->stream_buffer()->data(), encoded_buffer(), encoded_len());
152 filter_->FlushStreamBuffer(encoded_len());
153
154 char decode_buffer[kDefaultBufferSize];
Randy Smith (Not in Mondays) 2015/12/07 22:27:28 Suggestion: Put a DCHECK or ASSERT here that kDefa
eustas 2015/12/08 11:31:31 Done.
155 int decode_size = kDefaultBufferSize;
156 filter_->ReadData(decode_buffer, &decode_size);
157
158 // Compare the decoding result with source data
159 EXPECT_EQ(source_len(), decode_size);
160 EXPECT_EQ(memcmp(source_buffer(), decode_buffer, source_len()), 0);
161 }
162
163 // Tests we can call filter repeatedly to get all the data decoded.
164 // To do that, we create a filter with a small buffer that can not hold all
165 // the input data.
166 TEST_F(BrotliUnitTest, DecodeWithSmallBuffer) {
167 InitFilterWithBufferSize(kSmallBufferSize);
168 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size());
169 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
170 encoded_buffer(), encoded_len(),
171 kDefaultBufferSize);
172 }
173
174 // Tests we can still decode with just 1 byte buffer in the filter.
175 // The purpose of this test: sometimes the filter will consume input without
176 // generating output. Verify filter can handle it correctly.
177 TEST_F(BrotliUnitTest, DecodeWithOneByteBuffer) {
178 InitFilterWithBufferSize(1);
179 EXPECT_EQ(1, filter_->stream_buffer_size());
180 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
181 encoded_buffer(), encoded_len(),
182 kDefaultBufferSize);
183 }
184
185 // Tests we can decode when caller has small buffer to read out from filter.
Randy Smith (Not in Mondays) 2015/12/07 22:27:28 DecodeWithSmallBuffer tests small filter buffer +
eustas 2015/12/08 11:31:31 The more tests the better. Added small/small test.
186 TEST_F(BrotliUnitTest, DecodeWithSmallOutputBuffer) {
187 InitFilter();
188 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
189 encoded_buffer(), encoded_len(), kSmallBufferSize);
190 }
191
192 // Tests we can still decode with just 1 byte buffer in the filter and just 1
193 // byte buffer in the caller.
194 TEST_F(BrotliUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
195 InitFilterWithBufferSize(1);
196 EXPECT_EQ(1, filter_->stream_buffer_size());
197 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
198 encoded_buffer(), encoded_len(), 1);
199 }
200
201 // Decoding deflate stream with corrupted data.
202 TEST_F(BrotliUnitTest, DecodeCorruptedData) {
203 char corrupt_data[kDefaultBufferSize];
204 int corrupt_data_len = encoded_len();
205 memcpy(corrupt_data, encoded_buffer(), encoded_len());
206
207 int pos = corrupt_data_len / 2;
208 corrupt_data[pos] = !corrupt_data[pos];
209
210 // Decode the corrupted data with filter
211 InitFilter();
212 char corrupt_decode_buffer[kDefaultBufferSize];
213 int corrupt_decode_size = kDefaultBufferSize;
214
215 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
216 corrupt_decode_buffer, &corrupt_decode_size);
217
218 // Expect failures
219 EXPECT_EQ(Filter::FILTER_ERROR, code);
220 }
221
222 // Decoding deflate stream with missing data.
223 TEST_F(BrotliUnitTest, DecodeMissingData) {
224 char corrupt_data[kDefaultBufferSize];
225 int corrupt_data_len = encoded_len();
226 memcpy(corrupt_data, encoded_buffer(), encoded_len());
227
228 int pos = corrupt_data_len / 2;
229 int len = corrupt_data_len - pos - 1;
230 memmove(&corrupt_data[pos], &corrupt_data[pos + 1], len);
231 --corrupt_data_len;
232
233 // Decode the corrupted data with filter
234 InitFilter();
235 char corrupt_decode_buffer[kDefaultBufferSize];
236 int corrupt_decode_size = kDefaultBufferSize;
237
238 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
239 corrupt_decode_buffer, &corrupt_decode_size);
240
241 // Expect failures
242 EXPECT_EQ(Filter::FILTER_ERROR, code);
243 }
244
245 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698