OLD | NEW |
---|---|
(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 // Use filter to decode compressed data, and compare the decoded result with | |
48 // the orginal data. | |
49 // Parameters: |source| and |source_len| are original data and its size. | |
50 // |encoded_source| and |encoded_source_len| are compressed data and its size. | |
51 // |output_buffer_size| specifies the size of buffer to read out data from | |
52 // filter. | |
53 void DecodeAndCompareWithFilter(Filter* filter, | |
54 const char* source, | |
55 int source_len, | |
Ryan Sleevi
2015/12/17 01:04:40
https://www.chromium.org/developers/coding-style#T
eustas
2015/12/17 16:44:54
I also prefer to use size_t where appropriate, but
| |
56 const char* encoded_source, | |
57 int encoded_source_len, | |
58 int output_buffer_size) { | |
59 // Make sure we have enough space to hold the decoding output. | |
60 ASSERT_GE(kDefaultBufferSize, source_len); | |
61 ASSERT_GE(kDefaultBufferSize, output_buffer_size); | |
62 | |
63 char decode_buffer[kDefaultBufferSize]; | |
64 char* decode_next = decode_buffer; | |
65 int decode_avail_size = kDefaultBufferSize; | |
66 | |
67 const char* encode_next = encoded_source; | |
68 int encode_avail_size = encoded_source_len; | |
69 | |
70 int code = Filter::FILTER_OK; | |
71 while (code != Filter::FILTER_DONE) { | |
72 int encode_data_len = | |
73 std::min(encode_avail_size, filter->stream_buffer_size()); | |
74 memcpy(filter->stream_buffer()->data(), encode_next, encode_data_len); | |
75 filter->FlushStreamBuffer(encode_data_len); | |
76 encode_next += encode_data_len; | |
77 encode_avail_size -= encode_data_len; | |
78 | |
79 while (true) { | |
80 int decode_data_len = std::min(decode_avail_size, output_buffer_size); | |
81 | |
82 code = filter->ReadData(decode_next, &decode_data_len); | |
83 decode_next += decode_data_len; | |
84 decode_avail_size -= decode_data_len; | |
85 | |
86 ASSERT_NE(Filter::FILTER_ERROR, code); | |
87 | |
88 if (code == Filter::FILTER_NEED_MORE_DATA || | |
89 code == Filter::FILTER_DONE) { | |
90 break; | |
91 } | |
92 } | |
93 } | |
94 | |
95 // Compare the decoding result with source data | |
96 int decode_total_data_len = kDefaultBufferSize - decode_avail_size; | |
97 EXPECT_EQ(source_len, decode_total_data_len); | |
98 EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0); | |
99 } | |
100 | |
101 // Unsafe function to use filter to decode compressed data. | |
102 // Parameters: |source| and |source_len| are compressed data and its size. | |
103 // |dest| is the buffer for decoding results. Upon entry, |*dest_len| is the | |
104 // size of the output buffer. Upon exit, |*dest_len| is the number of chars | |
105 // written into the buffer. | |
106 int DecodeAllWithFilter(Filter* filter, | |
107 const char* source, | |
108 int source_len, | |
109 char* dest, | |
110 int* dest_len) { | |
111 memcpy(filter->stream_buffer()->data(), source, source_len); | |
112 filter->FlushStreamBuffer(source_len); | |
113 return filter->ReadData(dest, dest_len); | |
114 } | |
115 | |
116 void InitFilter() { | |
117 std::vector<Filter::FilterType> filter_types; | |
118 filter_types.push_back(Filter::FILTER_TYPE_BROTLI); | |
119 filter_.reset(Filter::Factory(filter_types, filter_context_)); | |
120 ASSERT_TRUE(filter_.get()); | |
121 ASSERT_LE(kDefaultBufferSize, filter_->stream_buffer_size()); | |
122 } | |
123 | |
124 void InitFilterWithBufferSize(int buffer_size) { | |
125 std::vector<Filter::FilterType> filter_types; | |
126 filter_types.push_back(Filter::FILTER_TYPE_BROTLI); | |
127 filter_.reset( | |
128 Filter::FactoryForTests(filter_types, filter_context_, buffer_size)); | |
129 ASSERT_TRUE(filter_.get()); | |
130 } | |
131 | |
132 const char* source_buffer() const { return source_buffer_.data(); } | |
133 int source_len() const { return static_cast<int>(source_buffer_.size()); } | |
134 | |
135 const char* encoded_buffer() const { return encoded_buffer_.data(); } | |
136 int encoded_len() const { return static_cast<int>(encoded_buffer_.size()); } | |
137 | |
138 scoped_ptr<Filter> filter_; | |
139 | |
140 private: | |
141 MockFilterContext filter_context_; | |
142 std::string source_buffer_; | |
143 std::string encoded_buffer_; | |
144 }; | |
145 | |
146 // Basic scenario: decoding brotli data with big enough buffer. | |
147 TEST_F(BrotliUnitTest, DecodeBrotli) { | |
148 InitFilter(); | |
149 memcpy(filter_->stream_buffer()->data(), encoded_buffer(), encoded_len()); | |
150 filter_->FlushStreamBuffer(encoded_len()); | |
151 | |
152 ASSERT_GE(kDefaultBufferSize, source_len()); | |
153 char decode_buffer[kDefaultBufferSize]; | |
154 int decode_size = kDefaultBufferSize; | |
155 filter_->ReadData(decode_buffer, &decode_size); | |
156 | |
157 // Compare the decoding result with source data | |
158 EXPECT_EQ(source_len(), decode_size); | |
159 EXPECT_EQ(memcmp(source_buffer(), decode_buffer, source_len()), 0); | |
160 } | |
161 | |
162 // 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 // the input data. | |
165 TEST_F(BrotliUnitTest, DecodeWithSmallBuffer) { | |
166 InitFilterWithBufferSize(kSmallBufferSize); | |
167 EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size()); | |
168 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), | |
169 encoded_buffer(), encoded_len(), | |
170 kDefaultBufferSize); | |
171 } | |
172 | |
173 // 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 | |
175 // generating output. Verify filter can handle it correctly. | |
176 TEST_F(BrotliUnitTest, DecodeWithOneByteBuffer) { | |
177 InitFilterWithBufferSize(1); | |
178 EXPECT_EQ(1, filter_->stream_buffer_size()); | |
179 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), | |
180 encoded_buffer(), encoded_len(), | |
181 kDefaultBufferSize); | |
182 } | |
183 | |
184 // Tests we can decode when caller has small buffer to read out from filter. | |
185 TEST_F(BrotliUnitTest, DecodeWithSmallOutputBuffer) { | |
186 InitFilter(); | |
187 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), | |
188 encoded_buffer(), encoded_len(), kSmallBufferSize); | |
189 } | |
190 | |
191 // 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 | |
193 // one-byte buffers. | |
194 TEST_F(BrotliUnitTest, DecodeWithSmallInputAndOutputBuffer) { | |
195 InitFilterWithBufferSize(kSmallBufferSize); | |
196 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), | |
197 encoded_buffer(), encoded_len(), kSmallBufferSize); | |
198 } | |
199 | |
200 // Tests we can still decode with just 1 byte buffer in the filter and just 1 | |
201 // byte buffer in the caller. | |
202 TEST_F(BrotliUnitTest, DecodeWithOneByteInputAndOutputBuffer) { | |
203 InitFilterWithBufferSize(1); | |
204 EXPECT_EQ(1, filter_->stream_buffer_size()); | |
205 DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(), | |
206 encoded_buffer(), encoded_len(), 1); | |
207 } | |
208 | |
209 // Decoding deflate stream with corrupted data. | |
210 TEST_F(BrotliUnitTest, DecodeCorruptedData) { | |
211 char corrupt_data[kDefaultBufferSize]; | |
212 int corrupt_data_len = encoded_len(); | |
213 memcpy(corrupt_data, encoded_buffer(), encoded_len()); | |
214 | |
215 int pos = corrupt_data_len / 2; | |
216 corrupt_data[pos] = !corrupt_data[pos]; | |
217 | |
218 // Decode the corrupted data with filter | |
219 InitFilter(); | |
220 char corrupt_decode_buffer[kDefaultBufferSize]; | |
221 int corrupt_decode_size = kDefaultBufferSize; | |
222 | |
223 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len, | |
224 corrupt_decode_buffer, &corrupt_decode_size); | |
225 | |
226 // Expect failures | |
227 EXPECT_EQ(Filter::FILTER_ERROR, code); | |
228 } | |
229 | |
230 // Decoding deflate stream with missing data. | |
231 TEST_F(BrotliUnitTest, DecodeMissingData) { | |
Ryan Sleevi
2015/12/17 01:04:40
Why don't any of these tests need to be conditiona
eustas
2015/12/17 16:44:54
The whole BrotliUnitTest is excluded when brotli s
| |
232 char corrupt_data[kDefaultBufferSize]; | |
233 int corrupt_data_len = encoded_len(); | |
234 memcpy(corrupt_data, encoded_buffer(), encoded_len()); | |
235 | |
236 int pos = corrupt_data_len / 2; | |
237 int len = corrupt_data_len - pos - 1; | |
238 memmove(&corrupt_data[pos], &corrupt_data[pos + 1], len); | |
239 --corrupt_data_len; | |
240 | |
241 // Decode the corrupted data with filter | |
242 InitFilter(); | |
243 char corrupt_decode_buffer[kDefaultBufferSize]; | |
244 int corrupt_decode_size = kDefaultBufferSize; | |
245 | |
246 int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len, | |
247 corrupt_decode_buffer, &corrupt_decode_size); | |
248 | |
249 // Expect failures | |
250 EXPECT_EQ(Filter::FILTER_ERROR, code); | |
251 } | |
252 | |
253 } // namespace net | |
OLD | NEW |