| Index: net/filter/gzip_source_stream_unittest.cc
|
| diff --git a/net/filter/gzip_source_stream_unittest.cc b/net/filter/gzip_source_stream_unittest.cc
|
| index f9416237744786e4259b796b62420d7bd0c3bd03..f0cb1802a47bed364d203112db579392871ac84d 100644
|
| --- a/net/filter/gzip_source_stream_unittest.cc
|
| +++ b/net/filter/gzip_source_stream_unittest.cc
|
| @@ -2,12 +2,14 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +#include <memory>
|
| #include <string>
|
| #include <utility>
|
|
|
| #include "base/bind.h"
|
| #include "base/bit_cast.h"
|
| #include "base/callback.h"
|
| +#include "base/memory/ptr_util.h"
|
| #include "net/base/completion_callback.h"
|
| #include "net/base/io_buffer.h"
|
| #include "net/base/test_completion_callback.h"
|
| @@ -21,7 +23,7 @@ namespace net {
|
|
|
| namespace {
|
|
|
| -const int kBufferSize = 4096;
|
| +const int kBigBufferSize = 4096;
|
| const int kSmallBufferSize = 1;
|
|
|
| // How many bytes to leave unused at the end of |source_data_|. This margin is
|
| @@ -29,42 +31,40 @@ const int kSmallBufferSize = 1;
|
| // out of room in the output buffer.
|
| const size_t kEOFMargin = 64;
|
|
|
| +struct GzipTestParam {
|
| + GzipTestParam(int buf_size, MockSourceStream::Mode read_mode)
|
| + : buffer_size(buf_size), mode(read_mode) {}
|
| +
|
| + const int buffer_size;
|
| + const MockSourceStream::Mode mode;
|
| +};
|
| +
|
| } // namespace
|
|
|
| -class GzipSourceStreamTest
|
| - : public ::testing::TestWithParam<MockSourceStream::Mode> {
|
| +class GzipSourceStreamTest : public ::testing::TestWithParam<GzipTestParam> {
|
| protected:
|
| - GzipSourceStreamTest() : output_buffer_size_(kBufferSize) {}
|
| - // If |allow_gzip_fallback| is true, will use
|
| - // GZIP_SOURCE_STREAM_GZIP_WITH_FALLBACK when constructing |gzip_stream_|.
|
| - void Init(bool allow_gzip_fallback) {
|
| - source_data_len_ = kBufferSize - kEOFMargin;
|
| + GzipSourceStreamTest() : output_buffer_size_(GetParam().buffer_size) {}
|
| +
|
| + // Helpful function to initialize the test fixture.|type| specifies which type
|
| + // of GzipSourceStream to create. It must be one of TYPE_GZIP,
|
| + // TYPE_GZIP_FALLBACK and TYPE_DEFLATE.
|
| + void Init(SourceStream::SourceType type) {
|
| + EXPECT_TRUE(SourceStream::TYPE_GZIP == type ||
|
| + SourceStream::TYPE_GZIP_FALLBACK == type ||
|
| + SourceStream::TYPE_DEFLATE == type);
|
| + source_data_len_ = kBigBufferSize - kEOFMargin;
|
|
|
| for (size_t i = 0; i < source_data_len_; i++)
|
| source_data_[i] = i % 256;
|
|
|
| - deflated_data_len_ = kBufferSize;
|
| - CompressGzip(source_data_, source_data_len_, deflated_data_,
|
| - &deflated_data_len_, false);
|
| -
|
| - gzipped_data_len_ = kBufferSize;
|
| - CompressGzip(source_data_, source_data_len_, gzipped_data_,
|
| - &gzipped_data_len_, true);
|
| + encoded_data_len_ = kBigBufferSize;
|
| + CompressGzip(source_data_, source_data_len_, encoded_data_,
|
| + &encoded_data_len_, type != SourceStream::TYPE_DEFLATE);
|
|
|
| output_buffer_ = new IOBuffer(output_buffer_size_);
|
| - std::unique_ptr<MockSourceStream> deflate_source(new MockSourceStream);
|
| - deflate_source_ = deflate_source.get();
|
| - deflate_stream_ = GzipSourceStream::Create(std::move(deflate_source),
|
| - SourceStream::TYPE_DEFLATE);
|
| - std::unique_ptr<MockSourceStream> gzip_source(new MockSourceStream);
|
| - gzip_source_ = gzip_source.get();
|
| - if (allow_gzip_fallback) {
|
| - gzip_stream_ = GzipSourceStream::Create(std::move(gzip_source),
|
| - SourceStream::TYPE_GZIP_FALLBACK);
|
| - } else {
|
| - gzip_stream_ = GzipSourceStream::Create(std::move(gzip_source),
|
| - SourceStream::TYPE_GZIP);
|
| - }
|
| + std::unique_ptr<MockSourceStream> source(new MockSourceStream());
|
| + source_ = source.get();
|
| + stream_ = GzipSourceStream::Create(std::move(source), type);
|
| }
|
|
|
| // If MockSourceStream::Mode is ASYNC, completes 1 read from |mock_stream| and
|
| @@ -73,7 +73,7 @@ class GzipSourceStreamTest
|
| int CompleteReadIfAsync(int previous_result,
|
| TestCompletionCallback* callback,
|
| MockSourceStream* mock_stream) {
|
| - if (GetParam() == MockSourceStream::ASYNC) {
|
| + if (GetParam().mode == MockSourceStream::ASYNC) {
|
| EXPECT_EQ(ERR_IO_PENDING, previous_result);
|
| mock_stream->CompleteNextRead();
|
| return callback->WaitForResult();
|
| @@ -81,187 +81,175 @@ class GzipSourceStreamTest
|
| return previous_result;
|
| }
|
|
|
| - void set_output_buffer_size(int output_buffer_size) {
|
| - output_buffer_size_ = output_buffer_size;
|
| - }
|
| -
|
| char* source_data() { return source_data_; }
|
| size_t source_data_len() { return source_data_len_; }
|
|
|
| - char* deflated_data() { return deflated_data_; }
|
| - size_t deflated_data_len() { return deflated_data_len_; }
|
| -
|
| - char* gzipped_data() { return gzipped_data_; }
|
| - size_t gzipped_data_len() { return gzipped_data_len_; }
|
| + char* encoded_data() { return encoded_data_; }
|
| + size_t encoded_data_len() { return encoded_data_len_; }
|
|
|
| IOBuffer* output_buffer() { return output_buffer_.get(); }
|
| char* output_data() { return output_buffer_->data(); }
|
| size_t output_buffer_size() { return output_buffer_size_; }
|
|
|
| - MockSourceStream* deflate_source() { return deflate_source_; }
|
| - GzipSourceStream* deflate_stream() { return deflate_stream_.get(); }
|
| - MockSourceStream* gzip_source() { return gzip_source_; }
|
| - GzipSourceStream* gzip_stream() { return gzip_stream_.get(); }
|
| -
|
| - void AddTrailingDeflatedData(const char* data, size_t data_len) {
|
| - DCHECK_LE(data_len, kBufferSize - deflated_data_len_);
|
| - memcpy(deflated_data_ + deflated_data_len_, data, data_len);
|
| - deflated_data_len_ += data_len;
|
| - }
|
| -
|
| - int ReadStream(GzipSourceStream* stream, const CompletionCallback& callback) {
|
| - return stream->Read(output_buffer(), output_buffer_size(), callback);
|
| - }
|
| -
|
| - int ReadDeflateStream(const CompletionCallback& callback) {
|
| - return ReadStream(deflate_stream_.get(), callback);
|
| - }
|
| -
|
| - int ReadGzipStream(const CompletionCallback& callback) {
|
| - return ReadStream(gzip_stream_.get(), callback);
|
| + MockSourceStream* source() { return source_; }
|
| + GzipSourceStream* stream() { return stream_.get(); }
|
| +
|
| + // Reads from |stream_| until an error occurs or the EOF is reached.
|
| + // When an error occurs, returns the net error code. When an EOF is reached,
|
| + // returns the number of bytes read and appends data read to |output|.
|
| + int ReadStream(std::string* output) {
|
| + int bytes_read = 0;
|
| + while (true) {
|
| + TestCompletionCallback callback;
|
| + int rv = stream_->Read(output_buffer(), output_buffer_size(),
|
| + callback.callback());
|
| + if (rv == ERR_IO_PENDING)
|
| + rv = CompleteReadIfAsync(rv, &callback, source());
|
| + if (rv == OK)
|
| + break;
|
| + if (rv < OK)
|
| + return rv;
|
| + EXPECT_GT(rv, OK);
|
| + bytes_read += rv;
|
| + output->append(output_data(), rv);
|
| + }
|
| + return bytes_read;
|
| }
|
|
|
| private:
|
| - char source_data_[kBufferSize];
|
| + char source_data_[kBigBufferSize];
|
| size_t source_data_len_;
|
|
|
| - char deflated_data_[kBufferSize];
|
| - size_t deflated_data_len_;
|
| -
|
| - char gzipped_data_[kBufferSize];
|
| - size_t gzipped_data_len_;
|
| + char encoded_data_[kBigBufferSize];
|
| + size_t encoded_data_len_;
|
|
|
| scoped_refptr<IOBuffer> output_buffer_;
|
| - int output_buffer_size_;
|
| + const int output_buffer_size_;
|
|
|
| - MockSourceStream* deflate_source_;
|
| - std::unique_ptr<GzipSourceStream> deflate_stream_;
|
| - MockSourceStream* gzip_source_;
|
| - std::unique_ptr<GzipSourceStream> gzip_stream_;
|
| + MockSourceStream* source_;
|
| + std::unique_ptr<GzipSourceStream> stream_;
|
| };
|
|
|
| -INSTANTIATE_TEST_CASE_P(GzipSourceStreamTests,
|
| - GzipSourceStreamTest,
|
| - ::testing::Values(MockSourceStream::SYNC,
|
| - MockSourceStream::ASYNC));
|
| +INSTANTIATE_TEST_CASE_P(
|
| + GzipSourceStreamTests,
|
| + GzipSourceStreamTest,
|
| + ::testing::Values(GzipTestParam(kBigBufferSize, MockSourceStream::SYNC),
|
| + GzipTestParam(kSmallBufferSize, MockSourceStream::SYNC),
|
| + GzipTestParam(kBigBufferSize, MockSourceStream::ASYNC),
|
| + GzipTestParam(kSmallBufferSize,
|
| + MockSourceStream::ASYNC)));
|
|
|
| TEST_P(GzipSourceStreamTest, EmptyStream) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - deflate_source()->AddReadResult("", 0, OK, GetParam());
|
| + Init(SourceStream::TYPE_DEFLATE);
|
| + source()->AddReadResult("", 0, OK, GetParam().mode);
|
| TestCompletionCallback callback;
|
| - int result = ReadDeflateStream(callback.callback());
|
| - result = CompleteReadIfAsync(result, &callback, deflate_source());
|
| + std::string actual_output;
|
| + int result = ReadStream(&actual_output);
|
| EXPECT_EQ(OK, result);
|
| - EXPECT_EQ("DEFLATE", deflate_stream()->Description());
|
| + EXPECT_EQ("DEFLATE", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, DeflateOneBlock) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - deflate_source()->AddReadResult(deflated_data(), deflated_data_len(), OK,
|
| - GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadDeflateStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, deflate_source());
|
| + Init(SourceStream::TYPE_DEFLATE);
|
| + source()->AddReadResult(encoded_data(), encoded_data_len(), OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(encoded_data(), 0, OK, GetParam().mode);
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(static_cast<int>(source_data_len()), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), source_data(), source_data_len()));
|
| - EXPECT_EQ("DEFLATE", deflate_stream()->Description());
|
| + EXPECT_EQ(std::string(source_data(), source_data_len()), actual_output);
|
| + EXPECT_EQ("DEFLATE", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, GzipOneBloc) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - gzip_source()->AddReadResult(gzipped_data(), gzipped_data_len(), OK,
|
| - GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| + Init(SourceStream::TYPE_GZIP);
|
| + source()->AddReadResult(encoded_data(), encoded_data_len(), OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(encoded_data(), 0, OK, GetParam().mode);
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(static_cast<int>(source_data_len()), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), source_data(), source_data_len()));
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| + EXPECT_EQ(std::string(source_data(), source_data_len()), actual_output);
|
| + EXPECT_EQ("GZIP", stream()->Description());
|
| }
|
|
|
| -TEST_P(GzipSourceStreamTest, DeflateTwoBlocks) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - deflate_source()->AddReadResult(deflated_data(), 10, OK, GetParam());
|
| - deflate_source()->AddReadResult(deflated_data() + 10,
|
| - deflated_data_len() - 10, OK, GetParam());
|
| - deflate_source()->AddReadResult(deflated_data() + deflated_data_len(), 0, OK,
|
| - GetParam());
|
| +TEST_P(GzipSourceStreamTest, DeflateTwoReads) {
|
| + Init(SourceStream::TYPE_DEFLATE);
|
| + source()->AddReadResult(encoded_data(), 10, OK, GetParam().mode);
|
| + source()->AddReadResult(encoded_data() + 10, encoded_data_len() - 10, OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(encoded_data() + encoded_data_len(), 0, OK,
|
| + GetParam().mode);
|
| std::string actual_output;
|
| - while (true) {
|
| - TestCompletionCallback callback;
|
| - int rv = ReadDeflateStream(callback.callback());
|
| - if (rv == ERR_IO_PENDING)
|
| - rv = CompleteReadIfAsync(rv, &callback, deflate_source());
|
| - if (rv == OK)
|
| - break;
|
| - ASSERT_GT(rv, OK);
|
| - actual_output.append(output_data(), rv);
|
| - }
|
| - EXPECT_EQ(source_data_len(), actual_output.size());
|
| + int rv = ReadStream(&actual_output);
|
| + EXPECT_EQ(static_cast<int>(source_data_len()), rv);
|
| EXPECT_EQ(std::string(source_data(), source_data_len()), actual_output);
|
| - EXPECT_EQ("DEFLATE", deflate_stream()->Description());
|
| + EXPECT_EQ("DEFLATE", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, PassThroughAfterEOF) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| + Init(SourceStream::TYPE_DEFLATE);
|
| char test_data[] = "Hello, World!";
|
| - AddTrailingDeflatedData(test_data, sizeof(test_data));
|
| - deflate_source()->AddReadResult(deflated_data(), deflated_data_len(), OK,
|
| - GetParam());
|
| + std::string encoded_data_with_trailing_data(encoded_data(),
|
| + encoded_data_len());
|
| + encoded_data_with_trailing_data.append(test_data, sizeof(test_data));
|
| + source()->AddReadResult(encoded_data_with_trailing_data.c_str(),
|
| + encoded_data_len() + sizeof(test_data), OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(encoded_data(), 0, OK, GetParam().mode);
|
| // Compressed and uncompressed data get returned as separate Read() results,
|
| // so this test has to call Read twice.
|
| - TestCompletionCallback callback;
|
| - int rv = ReadDeflateStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, deflate_source());
|
| - EXPECT_EQ(static_cast<int>(source_data_len() + sizeof(test_data)), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), source_data(), source_data_len()));
|
| - EXPECT_EQ(0, memcmp(output_data() + source_data_len(), test_data,
|
| - sizeof(test_data)));
|
| - EXPECT_EQ("DEFLATE", deflate_stream()->Description());
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| + std::string expected_output(source_data(), source_data_len());
|
| + expected_output.append(test_data, sizeof(test_data));
|
| + EXPECT_EQ(static_cast<int>(expected_output.size()), rv);
|
| + EXPECT_EQ(expected_output, actual_output);
|
| + EXPECT_EQ("DEFLATE", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, MissingZlibHeader) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| + Init(SourceStream::TYPE_DEFLATE);
|
| const size_t kZlibHeaderLen = 2;
|
| - deflate_source()->AddReadResult(deflated_data() + kZlibHeaderLen,
|
| - deflated_data_len() - kZlibHeaderLen, OK,
|
| - GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadDeflateStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, deflate_source());
|
| + source()->AddReadResult(encoded_data() + kZlibHeaderLen,
|
| + encoded_data_len() - kZlibHeaderLen, OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(encoded_data(), 0, OK, GetParam().mode);
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(static_cast<int>(source_data_len()), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), source_data(), source_data_len()));
|
| - EXPECT_EQ("DEFLATE", deflate_stream()->Description());
|
| + EXPECT_EQ(std::string(source_data(), source_data_len()), actual_output);
|
| + EXPECT_EQ("DEFLATE", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, CorruptGzipHeader) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - gzipped_data()[0] = 0;
|
| - gzip_source()->AddReadResult(gzipped_data(), gzipped_data_len(), OK,
|
| - GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| + Init(SourceStream::TYPE_GZIP);
|
| + encoded_data()[0] = 0;
|
| + source()->AddReadResult(encoded_data(), encoded_data_len(), OK,
|
| + GetParam().mode);
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(ERR_CONTENT_DECODING_FAILED, rv);
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| + EXPECT_EQ("GZIP", stream()->Description());
|
| }
|
|
|
| TEST_P(GzipSourceStreamTest, GzipFallback) {
|
| - Init(/*allow_gzip_fallback=*/true);
|
| - gzip_source()->AddReadResult(source_data(), source_data_len(), OK,
|
| - GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| + Init(SourceStream::TYPE_GZIP_FALLBACK);
|
| + source()->AddReadResult(source_data(), source_data_len(), OK,
|
| + GetParam().mode);
|
| + source()->AddReadResult(source_data(), 0, OK, GetParam().mode);
|
| +
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(static_cast<int>(source_data_len()), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), source_data(), source_data_len()));
|
| - EXPECT_EQ("GZIP_FALLBACK", gzip_stream()->Description());
|
| + EXPECT_EQ(std::string(source_data(), source_data_len()), actual_output);
|
| + EXPECT_EQ("GZIP_FALLBACK", stream()->Description());
|
| }
|
|
|
| // This test checks that the gzip stream source works correctly on 'golden' data
|
| // as produced by gzip(1).
|
| TEST_P(GzipSourceStreamTest, GzipCorrectness) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| + Init(SourceStream::TYPE_GZIP);
|
| char plain_data[] = "Hello, World!";
|
| unsigned char gzip_data[] = {
|
| // From:
|
| @@ -270,51 +258,22 @@ TEST_P(GzipSourceStreamTest, GzipCorrectness) {
|
| 0x1f, 0x8b, 0x08, 0x00, 0x2b, 0x02, 0x84, 0x55, 0x00, 0x03, 0xf3,
|
| 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x08, 0xcf, 0x2f, 0xca, 0x49,
|
| 0x51, 0x04, 0x00, 0xd0, 0xc3, 0x4a, 0xec, 0x0d, 0x00, 0x00, 0x00};
|
| - gzip_source()->AddReadResult(reinterpret_cast<char*>(gzip_data),
|
| - sizeof(gzip_data), OK, GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| - EXPECT_EQ(static_cast<int>(strlen(plain_data)), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), plain_data, strlen(plain_data)));
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| -}
|
| -
|
| -TEST_P(GzipSourceStreamTest, GzipCorrectnessWithSmallOutputBuffer) {
|
| - set_output_buffer_size(kSmallBufferSize);
|
| - Init(/*allow_gzip_fallback=*/false);
|
| - char plain_data[] = "Hello, World!";
|
| - unsigned char gzip_data[] = {
|
| - // From:
|
| - // echo -n 'Hello, World!' | gzip | xxd -i | sed -e 's/^/ /'
|
| - // The footer is the last 8 bytes.
|
| - 0x1f, 0x8b, 0x08, 0x00, 0x2b, 0x02, 0x84, 0x55, 0x00, 0x03, 0xf3,
|
| - 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x08, 0xcf, 0x2f, 0xca, 0x49,
|
| - 0x51, 0x04, 0x00, 0xd0, 0xc3, 0x4a, 0xec, 0x0d, 0x00, 0x00, 0x00};
|
| - gzip_source()->AddReadResult(reinterpret_cast<char*>(gzip_data),
|
| - sizeof(gzip_data), OK, GetParam());
|
| - gzip_source()->AddReadResult(
|
| + source()->AddReadResult(reinterpret_cast<char*>(gzip_data), sizeof(gzip_data),
|
| + OK, GetParam().mode);
|
| + source()->AddReadResult(
|
| reinterpret_cast<char*>(gzip_data) + sizeof(gzip_data), 0, OK,
|
| - GetParam());
|
| + GetParam().mode);
|
| std::string actual_output;
|
| - while (true) {
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - if (rv == ERR_IO_PENDING)
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| - if (rv == OK)
|
| - break;
|
| - ASSERT_GT(rv, OK);
|
| - actual_output.append(output_data(), rv);
|
| - }
|
| + int rv = ReadStream(&actual_output);
|
| + EXPECT_EQ(static_cast<int>(strlen(plain_data)), rv);
|
| EXPECT_EQ(plain_data, actual_output);
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| + EXPECT_EQ("GZIP", stream()->Description());
|
| }
|
|
|
| // Only test synchronous read because it's not straightforward to know how many
|
| // MockSourceStream reads to complete in order for GzipSourceStream to return.
|
| TEST_P(GzipSourceStreamTest, GzipCorrectnessWithSmallInputBuffer) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| + Init(SourceStream::TYPE_GZIP);
|
| char plain_data[] = "Hello, World!";
|
| unsigned char gzip_data[] = {
|
| // From:
|
| @@ -326,30 +285,22 @@ TEST_P(GzipSourceStreamTest, GzipCorrectnessWithSmallInputBuffer) {
|
| size_t gzip_data_len = sizeof(gzip_data);
|
| // Add a sequence of small reads.
|
| for (size_t i = 0; i < gzip_data_len; i++) {
|
| - gzip_source()->AddReadResult(reinterpret_cast<char*>(gzip_data) + i, 1, OK,
|
| - MockSourceStream::SYNC);
|
| + source()->AddReadResult(reinterpret_cast<char*>(gzip_data) + i, 1, OK,
|
| + MockSourceStream::SYNC);
|
| }
|
| - gzip_source()->AddReadResult(
|
| - reinterpret_cast<char*>(gzip_data) + gzip_data_len, 0, OK,
|
| - MockSourceStream::SYNC);
|
| - TestCompletionCallback callback;
|
| + source()->AddReadResult(reinterpret_cast<char*>(gzip_data) + gzip_data_len, 0,
|
| + OK, MockSourceStream::SYNC);
|
| std::string actual_output;
|
| - while (true) {
|
| - int rv = ReadGzipStream(callback.callback());
|
| - if (rv == OK)
|
| - break;
|
| - ASSERT_GT(rv, OK);
|
| - actual_output.append(output_data(), rv);
|
| - }
|
| - EXPECT_EQ(strlen(plain_data), actual_output.size());
|
| + int rv = ReadStream(&actual_output);
|
| + EXPECT_EQ(static_cast<int>(strlen(plain_data)), rv);
|
| EXPECT_EQ(plain_data, actual_output);
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| + EXPECT_EQ("GZIP", stream()->Description());
|
| }
|
|
|
| // Same as GzipCorrectness except that last 8 bytes are removed to test that the
|
| // implementation can handle missing footer.
|
| TEST_P(GzipSourceStreamTest, GzipCorrectnessWithoutFooter) {
|
| - Init(/*allow_gzip_fallback=*/false);
|
| + Init(SourceStream::TYPE_GZIP);
|
| char plain_data[] = "Hello, World!";
|
| unsigned char gzip_data[] = {
|
| // From:
|
| @@ -358,14 +309,15 @@ TEST_P(GzipSourceStreamTest, GzipCorrectnessWithoutFooter) {
|
| 0x1f, 0x8b, 0x08, 0x00, 0x2b, 0x02, 0x84, 0x55, 0x00,
|
| 0x03, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x08,
|
| 0xcf, 0x2f, 0xca, 0x49, 0x51, 0x04, 0x00};
|
| - gzip_source()->AddReadResult(reinterpret_cast<char*>(gzip_data),
|
| - sizeof(gzip_data), OK, GetParam());
|
| - TestCompletionCallback callback;
|
| - int rv = ReadGzipStream(callback.callback());
|
| - rv = CompleteReadIfAsync(rv, &callback, gzip_source());
|
| + source()->AddReadResult(reinterpret_cast<char*>(gzip_data), sizeof(gzip_data),
|
| + OK, GetParam().mode);
|
| + source()->AddReadResult(reinterpret_cast<char*>(gzip_data), 0, OK,
|
| + GetParam().mode);
|
| + std::string actual_output;
|
| + int rv = ReadStream(&actual_output);
|
| EXPECT_EQ(static_cast<int>(strlen(plain_data)), rv);
|
| - EXPECT_EQ(0, memcmp(output_data(), plain_data, strlen(plain_data)));
|
| - EXPECT_EQ("GZIP", gzip_stream()->Description());
|
| + EXPECT_EQ(plain_data, actual_output);
|
| + EXPECT_EQ("GZIP", stream()->Description());
|
| }
|
|
|
| } // namespace net
|
|
|