OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/sys_byteorder.h" |
| 6 #include "blimp/net/common.h" |
| 7 #include "blimp/net/compressed_packet_reader.h" |
| 8 #include "blimp/net/compressed_packet_writer.h" |
| 9 #include "blimp/net/test_common.h" |
| 10 #include "net/base/test_completion_callback.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using testing::_; |
| 15 using testing::DoAll; |
| 16 using testing::InvokeArgument; |
| 17 using testing::SaveArg; |
| 18 |
| 19 namespace blimp { |
| 20 namespace { |
| 21 |
| 22 // Calls the CompletionCallback (indicated by the 0-based index cb_idx) |
| 23 // with the value |result|. |
| 24 ACTION_TEMPLATE(InvokeCompletionCallback, |
| 25 HAS_1_TEMPLATE_PARAMS(int, cb_idx), |
| 26 AND_1_VALUE_PARAMS(result)) { |
| 27 testing::get<cb_idx>(args).Run(result); |
| 28 } |
| 29 |
| 30 // Copies a DrainableIOBuffer to a GrowableIOBuffer. |
| 31 // |dest_buf_idx|: The 0-based index of a GrowableIOBuffer. |
| 32 // |src_buf|: A DrainableIOBuffer. |
| 33 ACTION_TEMPLATE(CopyBuffer, |
| 34 HAS_1_TEMPLATE_PARAMS(int, dest_buf_idx), |
| 35 AND_1_VALUE_PARAMS(src_buf)) { |
| 36 const auto& dest_buf = testing::get<dest_buf_idx>(args); |
| 37 DCHECK(dest_buf); |
| 38 DCHECK(src_buf); |
| 39 DCHECK_EQ(0, dest_buf->offset()); |
| 40 src_buf->SetOffset(0); |
| 41 dest_buf->SetCapacity(src_buf->BytesRemaining()); |
| 42 memcpy(dest_buf->data(), src_buf->data(), src_buf->BytesRemaining()); |
| 43 } |
| 44 |
| 45 class CompressedPacketTest : public testing::Test { |
| 46 public: |
| 47 CompressedPacketTest() |
| 48 : mock_reader_(new MockPacketReader), |
| 49 mock_writer_(new MockPacketWriter), |
| 50 compressed_reader_( |
| 51 new CompressedPacketReader(make_scoped_ptr(mock_reader_))), |
| 52 compressed_writer_( |
| 53 new CompressedPacketWriter(make_scoped_ptr(mock_writer_))) {} |
| 54 ~CompressedPacketTest() override {} |
| 55 |
| 56 protected: |
| 57 // Returns the compressed result of |content|. |
| 58 std::string Compress(const std::string& content) { |
| 59 scoped_refptr<net::StringIOBuffer> content_str_buf( |
| 60 new net::StringIOBuffer(content)); |
| 61 scoped_refptr<net::DrainableIOBuffer> content_buf( |
| 62 new net::DrainableIOBuffer(content_str_buf.get(), content.size())); |
| 63 scoped_refptr<net::DrainableIOBuffer> compressed_buf; |
| 64 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) |
| 65 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), |
| 66 InvokeCompletionCallback<1>(net::OK))); |
| 67 net::TestCompletionCallback completion_cb_1; |
| 68 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 69 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); |
| 70 return std::string(compressed_buf->data(), |
| 71 compressed_buf->BytesRemaining()); |
| 72 } |
| 73 |
| 74 // Returns the decompressed result of |compressed|. |
| 75 std::string Decompress(const std::string& compressed) { |
| 76 scoped_refptr<net::StringIOBuffer> compressed_str_buf( |
| 77 new net::StringIOBuffer(compressed)); |
| 78 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| 79 new net::DrainableIOBuffer(compressed_str_buf.get(), |
| 80 compressed.size())); |
| 81 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( |
| 82 new net::GrowableIOBuffer); |
| 83 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 84 .WillOnce(DoAll( |
| 85 CopyBuffer<0>(compressed_buf), |
| 86 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 87 net::TestCompletionCallback completion_cb_2; |
| 88 compressed_reader_->ReadPacket(decompressed_buf, |
| 89 completion_cb_2.callback()); |
| 90 int size = completion_cb_2.WaitForResult(); |
| 91 return std::string(decompressed_buf->data(), size); |
| 92 } |
| 93 |
| 94 bool CheckRoundTrip(const std::string& content) { |
| 95 std::string compressed = Compress(content); |
| 96 std::string decompressed = Decompress(compressed); |
| 97 return Decompress(Compress(content)) == content; |
| 98 } |
| 99 |
| 100 MockPacketReader* mock_reader_; |
| 101 MockPacketWriter* mock_writer_; |
| 102 scoped_ptr<CompressedPacketReader> compressed_reader_; |
| 103 scoped_ptr<CompressedPacketWriter> compressed_writer_; |
| 104 testing::InSequence s; |
| 105 }; |
| 106 |
| 107 TEST_F(CompressedPacketTest, Empty) { |
| 108 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 109 EXPECT_TRUE(CheckRoundTrip("")); |
| 110 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 111 } |
| 112 |
| 113 TEST_F(CompressedPacketTest, Simple) { |
| 114 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 115 std::string compressed = Compress(source); |
| 116 std::string decompressed = Decompress(compressed); |
| 117 EXPECT_GT(source.size(), compressed.size()); |
| 118 EXPECT_EQ(source, decompressed); |
| 119 } |
| 120 |
| 121 TEST_F(CompressedPacketTest, DisjointSequences) { |
| 122 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 123 EXPECT_TRUE(CheckRoundTrip("5678")); |
| 124 EXPECT_TRUE(CheckRoundTrip("ABCD")); |
| 125 } |
| 126 |
| 127 TEST_F(CompressedPacketTest, AdditiveSequences) { |
| 128 EXPECT_TRUE(CheckRoundTrip("12")); |
| 129 EXPECT_TRUE(CheckRoundTrip("123")); |
| 130 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 131 EXPECT_TRUE(CheckRoundTrip("12345")); |
| 132 EXPECT_TRUE(CheckRoundTrip("123456")); |
| 133 } |
| 134 |
| 135 TEST_F(CompressedPacketTest, ReversedSequences) { |
| 136 EXPECT_TRUE(CheckRoundTrip("123456")); |
| 137 EXPECT_TRUE(CheckRoundTrip("12345")); |
| 138 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 139 EXPECT_TRUE(CheckRoundTrip("123")); |
| 140 EXPECT_TRUE(CheckRoundTrip("12")); |
| 141 } |
| 142 |
| 143 TEST_F(CompressedPacketTest, CompressionStateRetainedAcrossCalls) { |
| 144 // Ensure that a character sequence is encoded in a more compact manner |
| 145 // if it is compressed more than once. |
| 146 int size_1 = Compress("1234").size(); |
| 147 int size_2 = Compress("1234").size(); |
| 148 EXPECT_GT(size_1, size_2); |
| 149 } |
| 150 |
| 151 TEST_F(CompressedPacketTest, LargeInput) { |
| 152 std::string big_str(kMaxPacketPayloadSizeBytes, 'A'); // 3MB of A's. |
| 153 EXPECT_TRUE(CheckRoundTrip(big_str)); |
| 154 } |
| 155 |
| 156 TEST_F(CompressedPacketTest, DecompressIllegallyLargePayload) { |
| 157 scoped_refptr<net::IOBuffer> garbage_buf(new net::IOBuffer(4)); |
| 158 *(reinterpret_cast<uint32_t*>(garbage_buf->data())) = |
| 159 base::HostToNet32(kMaxPacketPayloadSizeBytes + 1); |
| 160 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| 161 new net::DrainableIOBuffer(garbage_buf.get(), 4)); |
| 162 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( |
| 163 new net::GrowableIOBuffer); |
| 164 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 165 .WillOnce( |
| 166 DoAll(CopyBuffer<0>(compressed_buf), |
| 167 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 168 net::TestCompletionCallback completion_cb_2; |
| 169 compressed_reader_->ReadPacket(decompressed_buf, completion_cb_2.callback()); |
| 170 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_2.WaitForResult()); |
| 171 } |
| 172 |
| 173 TEST_F(CompressedPacketTest, CompressIllegallyLargePayload) { |
| 174 scoped_refptr<net::IOBuffer> big_buf( |
| 175 new net::IOBuffer(kMaxPacketPayloadSizeBytes + 1)); |
| 176 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( |
| 177 big_buf.get(), kMaxPacketPayloadSizeBytes + 1)); |
| 178 net::TestCompletionCallback completion_cb_1; |
| 179 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 180 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_1.WaitForResult()); |
| 181 } |
| 182 |
| 183 } // namespace |
| 184 } // namespace blimp |
OLD | NEW |