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

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

Issue 693943003: Update from https://crrev.com/302630 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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/filter.cc ('k') | net/http/disk_cache_based_quic_server_info.cc » ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "net/base/io_buffer.h"
5 #include "net/filter/filter.h" 6 #include "net/filter/filter.h"
6 #include "net/filter/mock_filter_context.h" 7 #include "net/filter/mock_filter_context.h"
7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
8 9
9 namespace net { 10 namespace net {
10 11
12 namespace {
13
14 class PassThroughFilter : public Filter {
15 public:
16 PassThroughFilter() {}
17
18 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override {
19 return CopyOut(dest_buffer, dest_len);
20 }
21
22 DISALLOW_COPY_AND_ASSIGN(PassThroughFilter);
23 };
24
25 } // namespace
26
11 class FilterTest : public testing::Test { 27 class FilterTest : public testing::Test {
12 }; 28 };
13 29
14 TEST(FilterTest, ContentTypeId) { 30 TEST(FilterTest, ContentTypeId) {
15 // Check for basic translation of Content-Encoding, including case variations. 31 // Check for basic translation of Content-Encoding, including case variations.
16 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE, 32 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
17 Filter::ConvertEncodingToType("deflate")); 33 Filter::ConvertEncodingToType("deflate"));
18 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE, 34 EXPECT_EQ(Filter::FILTER_TYPE_DEFLATE,
19 Filter::ConvertEncodingToType("deflAte")); 35 Filter::ConvertEncodingToType("deflAte"));
20 EXPECT_EQ(Filter::FILTER_TYPE_GZIP, 36 EXPECT_EQ(Filter::FILTER_TYPE_GZIP,
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 // Except on downloads, where they just get saved. 373 // Except on downloads, where they just get saved.
358 encoding_types.clear(); 374 encoding_types.clear();
359 encoding_types.push_back(Filter::FILTER_TYPE_GZIP); 375 encoding_types.push_back(Filter::FILTER_TYPE_GZIP);
360 filter_context.SetDownload(true); 376 filter_context.SetDownload(true);
361 filter_context.SetMimeType(kHtmlMime); 377 filter_context.SetMimeType(kHtmlMime);
362 filter_context.SetURL(GURL(kGzUrl)); 378 filter_context.SetURL(GURL(kGzUrl));
363 Filter::FixupEncodingTypes(filter_context, &encoding_types); 379 Filter::FixupEncodingTypes(filter_context, &encoding_types);
364 EXPECT_TRUE(encoding_types.empty()); 380 EXPECT_TRUE(encoding_types.empty());
365 } 381 }
366 382
367 } // namespace net 383 // Make sure a series of three pass-through filters copies the data cleanly.
384 // Regression test for http://crbug.com/418975.
385 TEST(FilterTest, ThreeFilterChain) {
386 scoped_ptr<PassThroughFilter> filter1(new PassThroughFilter);
387 scoped_ptr<PassThroughFilter> filter2(new PassThroughFilter);
388 scoped_ptr<PassThroughFilter> filter3(new PassThroughFilter);
389
390 filter1->InitBuffer(32 * 1024);
391 filter2->InitBuffer(32 * 1024);
392 filter3->InitBuffer(32 * 1024);
393
394 filter2->next_filter_ = filter3.Pass();
395 filter1->next_filter_ = filter2.Pass();
396
397 // Initialize the input array with a varying byte sequence.
398 const size_t input_array_size = 64 * 1024;
399 char input_array[input_array_size];
400 size_t read_array_index = 0;
401 for (size_t i = 0; i < input_array_size; i++) {
402 input_array[i] = i % 113;
403 }
404
405 const size_t output_array_size = 4 * 1024;
406 char output_array[output_array_size];
407
408 size_t compare_array_index = 0;
409
410 do {
411 // Read data from the filter chain.
412 int amount_read = output_array_size;
413 Filter::FilterStatus status = filter1->ReadData(output_array, &amount_read);
414 EXPECT_NE(Filter::FILTER_ERROR, status);
415 EXPECT_EQ(0, memcmp(output_array, input_array + compare_array_index,
416 amount_read));
417 compare_array_index += amount_read;
418
419 // Detect the various indications that data transfer along the chain is
420 // complete.
421 if (Filter::FILTER_DONE == status || Filter::FILTER_ERROR == status ||
422 (Filter::FILTER_OK == status && amount_read == 0) ||
423 (Filter::FILTER_NEED_MORE_DATA == status &&
424 read_array_index == input_array_size))
425 break;
426
427 if (Filter::FILTER_OK == status)
428 continue;
429
430 // Write needed data into the filter chain.
431 ASSERT_EQ(Filter::FILTER_NEED_MORE_DATA, status);
432 ASSERT_NE(0, filter1->stream_buffer_size());
433 size_t amount_to_copy = std::min(
434 static_cast<size_t>(filter1->stream_buffer_size()),
435 input_array_size - read_array_index);
436 memcpy(filter1->stream_buffer()->data(),
437 input_array + read_array_index,
438 amount_to_copy);
439 filter1->FlushStreamBuffer(amount_to_copy);
440 read_array_index += amount_to_copy;
441 } while (true);
442
443 EXPECT_EQ(read_array_index, input_array_size);
444 EXPECT_EQ(compare_array_index, input_array_size);
445 }
446
447 } // Namespace net
OLDNEW
« no previous file with comments | « net/filter/filter.cc ('k') | net/http/disk_cache_based_quic_server_info.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698