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

Side by Side Diff: net/base/gzip_filter_unittest.cc

Issue 19004: Change URLRequest to use a ref-counted buffer for actual IO.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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 | Annotate | Revision Log
« no previous file with comments | « net/base/filter.cc ('k') | net/base/io_buffer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <fstream> 5 #include <fstream>
6 #include <iostream> 6 #include <iostream>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 int decode_avail_size = kDefaultBufferSize; 167 int decode_avail_size = kDefaultBufferSize;
168 168
169 const char* encode_next = encoded_source; 169 const char* encode_next = encoded_source;
170 int encode_avail_size = encoded_source_len; 170 int encode_avail_size = encoded_source_len;
171 171
172 int code = Filter::FILTER_OK; 172 int code = Filter::FILTER_OK;
173 while (code != Filter::FILTER_DONE) { 173 while (code != Filter::FILTER_DONE) {
174 int encode_data_len; 174 int encode_data_len;
175 encode_data_len = std::min(encode_avail_size, 175 encode_data_len = std::min(encode_avail_size,
176 filter->stream_buffer_size()); 176 filter->stream_buffer_size());
177 memcpy(filter->stream_buffer(), encode_next, encode_data_len); 177 memcpy(filter->stream_buffer()->data(), encode_next, encode_data_len);
178 filter->FlushStreamBuffer(encode_data_len); 178 filter->FlushStreamBuffer(encode_data_len);
179 encode_next += encode_data_len; 179 encode_next += encode_data_len;
180 encode_avail_size -= encode_data_len; 180 encode_avail_size -= encode_data_len;
181 181
182 while (1) { 182 while (1) {
183 int decode_data_len = std::min(decode_avail_size, output_buffer_size); 183 int decode_data_len = std::min(decode_avail_size, output_buffer_size);
184 184
185 code = filter->ReadData(decode_next, &decode_data_len); 185 code = filter->ReadData(decode_next, &decode_data_len);
186 decode_next += decode_data_len; 186 decode_next += decode_data_len;
187 decode_avail_size -= decode_data_len; 187 decode_avail_size -= decode_data_len;
(...skipping 13 matching lines...) Expand all
201 EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0); 201 EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0);
202 } 202 }
203 203
204 // Unsafe function to use filter to decode compressed data. 204 // Unsafe function to use filter to decode compressed data.
205 // Parameters: Source and source_len are compressed data and its size. 205 // Parameters: Source and source_len are compressed data and its size.
206 // Dest is the buffer for decoding results. Upon entry, *dest_len is the size 206 // Dest is the buffer for decoding results. Upon entry, *dest_len is the size
207 // of the dest buffer. Upon exit, *dest_len is the number of chars written 207 // of the dest buffer. Upon exit, *dest_len is the number of chars written
208 // into the buffer. 208 // into the buffer.
209 int DecodeAllWithFilter(Filter* filter, const char* source, int source_len, 209 int DecodeAllWithFilter(Filter* filter, const char* source, int source_len,
210 char* dest, int* dest_len) { 210 char* dest, int* dest_len) {
211 memcpy(filter->stream_buffer(), source, source_len); 211 memcpy(filter->stream_buffer()->data(), source, source_len);
212 filter->FlushStreamBuffer(source_len); 212 filter->FlushStreamBuffer(source_len);
213 return filter->ReadData(dest, dest_len); 213 return filter->ReadData(dest, dest_len);
214 } 214 }
215 215
216 const char* source_buffer() const { return source_buffer_.data(); } 216 const char* source_buffer() const { return source_buffer_.data(); }
217 int source_len() const { return static_cast<int>(source_buffer_.size()); } 217 int source_len() const { return static_cast<int>(source_buffer_.size()); }
218 218
219 std::string source_buffer_; 219 std::string source_buffer_;
220 220
221 char* deflate_encode_buffer_; 221 char* deflate_encode_buffer_;
222 int deflate_encode_len_; 222 int deflate_encode_len_;
223 223
224 char* gzip_encode_buffer_; 224 char* gzip_encode_buffer_;
225 int gzip_encode_len_; 225 int gzip_encode_len_;
226 }; 226 };
227 227
228 // Basic scenario: decoding deflate data with big enough buffer. 228 // Basic scenario: decoding deflate data with big enough buffer.
229 TEST_F(GZipUnitTest, DecodeDeflate) { 229 TEST_F(GZipUnitTest, DecodeDeflate) {
230 // Decode the compressed data with filter 230 // Decode the compressed data with filter
231 std::vector<Filter::FilterType> filter_types; 231 std::vector<Filter::FilterType> filter_types;
232 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE); 232 filter_types.push_back(Filter::FILTER_TYPE_DEFLATE);
233 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); 233 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize));
234 ASSERT_TRUE(filter.get()); 234 ASSERT_TRUE(filter.get());
235 memcpy(filter->stream_buffer(), deflate_encode_buffer_, deflate_encode_len_); 235 memcpy(filter->stream_buffer()->data(), deflate_encode_buffer_,
236 deflate_encode_len_);
236 filter->FlushStreamBuffer(deflate_encode_len_); 237 filter->FlushStreamBuffer(deflate_encode_len_);
237 238
238 char deflate_decode_buffer[kDefaultBufferSize]; 239 char deflate_decode_buffer[kDefaultBufferSize];
239 int deflate_decode_size = kDefaultBufferSize; 240 int deflate_decode_size = kDefaultBufferSize;
240 filter->ReadData(deflate_decode_buffer, &deflate_decode_size); 241 filter->ReadData(deflate_decode_buffer, &deflate_decode_size);
241 242
242 // Compare the decoding result with source data 243 // Compare the decoding result with source data
243 EXPECT_TRUE(deflate_decode_size == source_len()); 244 EXPECT_TRUE(deflate_decode_size == source_len());
244 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0); 245 EXPECT_EQ(memcmp(source_buffer(), deflate_decode_buffer, source_len()), 0);
245 } 246 }
246 247
247 // Basic scenario: decoding gzip data with big enough buffer. 248 // Basic scenario: decoding gzip data with big enough buffer.
248 TEST_F(GZipUnitTest, DecodeGZip) { 249 TEST_F(GZipUnitTest, DecodeGZip) {
249 // Decode the compressed data with filter 250 // Decode the compressed data with filter
250 std::vector<Filter::FilterType> filter_types; 251 std::vector<Filter::FilterType> filter_types;
251 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 252 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
252 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); 253 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize));
253 ASSERT_TRUE(filter.get()); 254 ASSERT_TRUE(filter.get());
254 memcpy(filter->stream_buffer(), gzip_encode_buffer_, gzip_encode_len_); 255 memcpy(filter->stream_buffer()->data(), gzip_encode_buffer_,
256 gzip_encode_len_);
255 filter->FlushStreamBuffer(gzip_encode_len_); 257 filter->FlushStreamBuffer(gzip_encode_len_);
256 258
257 char gzip_decode_buffer[kDefaultBufferSize]; 259 char gzip_decode_buffer[kDefaultBufferSize];
258 int gzip_decode_size = kDefaultBufferSize; 260 int gzip_decode_size = kDefaultBufferSize;
259 filter->ReadData(gzip_decode_buffer, &gzip_decode_size); 261 filter->ReadData(gzip_decode_buffer, &gzip_decode_size);
260 262
261 // Compare the decoding result with source data 263 // Compare the decoding result with source data
262 EXPECT_TRUE(gzip_decode_size == source_len()); 264 EXPECT_TRUE(gzip_decode_size == source_len());
263 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0); 265 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
264 } 266 }
265 267
266 // SDCH scenario: decoding gzip data when content type says sdch,gzip. 268 // SDCH scenario: decoding gzip data when content type says sdch,gzip.
267 // This tests that sdch will degrade to pass through, and is what allows robust 269 // This tests that sdch will degrade to pass through, and is what allows robust
268 // handling when the response *might* be sdch,gzip by simply adding in the 270 // handling when the response *might* be sdch,gzip by simply adding in the
269 // tentative sdch decode. 271 // tentative sdch decode.
270 // All test code is otherwise modeled after the "basic" scenario above. 272 // All test code is otherwise modeled after the "basic" scenario above.
271 TEST_F(GZipUnitTest, DecodeGZipWithMistakenSdch) { 273 TEST_F(GZipUnitTest, DecodeGZipWithMistakenSdch) {
272 // Decode the compressed data with filter 274 // Decode the compressed data with filter
273 std::vector<Filter::FilterType> filter_types; 275 std::vector<Filter::FilterType> filter_types;
274 filter_types.push_back(Filter::FILTER_TYPE_SDCH); 276 filter_types.push_back(Filter::FILTER_TYPE_SDCH);
275 filter_types.push_back(Filter::FILTER_TYPE_GZIP); 277 filter_types.push_back(Filter::FILTER_TYPE_GZIP);
276 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize)); 278 scoped_ptr<Filter> filter(Filter::Factory(filter_types, kDefaultBufferSize));
277 ASSERT_TRUE(filter.get()); 279 ASSERT_TRUE(filter.get());
278 memcpy(filter->stream_buffer(), gzip_encode_buffer_, gzip_encode_len_); 280 memcpy(filter->stream_buffer()->data(), gzip_encode_buffer_,
281 gzip_encode_len_);
279 filter->FlushStreamBuffer(gzip_encode_len_); 282 filter->FlushStreamBuffer(gzip_encode_len_);
280 283
281 char gzip_decode_buffer[kDefaultBufferSize]; 284 char gzip_decode_buffer[kDefaultBufferSize];
282 int gzip_decode_size = kDefaultBufferSize; 285 int gzip_decode_size = kDefaultBufferSize;
283 filter->ReadData(gzip_decode_buffer, &gzip_decode_size); 286 filter->ReadData(gzip_decode_buffer, &gzip_decode_size);
284 287
285 // Compare the decoding result with source data 288 // Compare the decoding result with source data
286 EXPECT_TRUE(gzip_decode_size == source_len()); 289 EXPECT_TRUE(gzip_decode_size == source_len());
287 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0); 290 EXPECT_EQ(memcmp(source_buffer(), gzip_decode_buffer, source_len()), 0);
288 } 291 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 int corrupt_decode_size = kDefaultBufferSize; 406 int corrupt_decode_size = kDefaultBufferSize;
404 407
405 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len, 408 int code = DecodeAllWithFilter(filter.get(), corrupt_data, corrupt_data_len,
406 corrupt_decode_buffer, &corrupt_decode_size); 409 corrupt_decode_buffer, &corrupt_decode_size);
407 410
408 // Expect failures 411 // Expect failures
409 EXPECT_TRUE(code == Filter::FILTER_ERROR); 412 EXPECT_TRUE(code == Filter::FILTER_ERROR);
410 } 413 }
411 414
412 } // namespace 415 } // namespace
OLDNEW
« no previous file with comments | « net/base/filter.cc ('k') | net/base/io_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698