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 ASSERT_TRUE(dest_buf); |
| 38 ASSERT_TRUE(src_buf); |
| 39 ASSERT_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(), |
| 63 content_str_buf->size())); |
| 64 scoped_refptr<net::DrainableIOBuffer> compressed_buf; |
| 65 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) |
| 66 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), |
| 67 InvokeCompletionCallback<1>(net::OK))); |
| 68 net::TestCompletionCallback completion_cb_1; |
| 69 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 70 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); |
| 71 return std::string(compressed_buf->data(), |
| 72 compressed_buf->BytesRemaining()); |
| 73 } |
| 74 |
| 75 // Returns the decompressed result of |compressed|. |
| 76 std::string Decompress(const std::string& compressed) { |
| 77 scoped_refptr<net::StringIOBuffer> compressed_str_buf( |
| 78 new net::StringIOBuffer(compressed)); |
| 79 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| 80 new net::DrainableIOBuffer(compressed_str_buf.get(), |
| 81 compressed_str_buf->size())); |
| 82 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( |
| 83 new net::GrowableIOBuffer); |
| 84 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 85 .WillOnce(DoAll( |
| 86 CopyBuffer<0>(compressed_buf), |
| 87 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 88 net::TestCompletionCallback completion_cb_2; |
| 89 compressed_reader_->ReadPacket(decompressed_buf, |
| 90 completion_cb_2.callback()); |
| 91 int size = completion_cb_2.WaitForResult(); |
| 92 return std::string(decompressed_buf->data(), size); |
| 93 } |
| 94 |
| 95 bool CheckRoundTrip(const std::string& content) { |
| 96 return Decompress(Compress(content)) == content; |
| 97 } |
| 98 |
| 99 MockPacketReader* mock_reader_; |
| 100 MockPacketWriter* mock_writer_; |
| 101 scoped_ptr<CompressedPacketReader> compressed_reader_; |
| 102 scoped_ptr<CompressedPacketWriter> compressed_writer_; |
| 103 testing::InSequence s; |
| 104 }; |
| 105 |
| 106 TEST_F(CompressedPacketTest, Empty) { |
| 107 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 108 EXPECT_TRUE(CheckRoundTrip("")); |
| 109 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 110 } |
| 111 |
| 112 TEST_F(CompressedPacketTest, Simple) { |
| 113 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 114 std::string compressed = Compress(source); |
| 115 std::string decompressed = Decompress(compressed); |
| 116 EXPECT_GT(source.size(), compressed.size()); |
| 117 EXPECT_EQ(source, decompressed); |
| 118 } |
| 119 |
| 120 TEST_F(CompressedPacketTest, DisjointSequences) { |
| 121 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 122 EXPECT_TRUE(CheckRoundTrip("5678")); |
| 123 EXPECT_TRUE(CheckRoundTrip("ABCD")); |
| 124 } |
| 125 |
| 126 TEST_F(CompressedPacketTest, AdditiveSequences) { |
| 127 EXPECT_TRUE(CheckRoundTrip("12")); |
| 128 EXPECT_TRUE(CheckRoundTrip("123")); |
| 129 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 130 EXPECT_TRUE(CheckRoundTrip("12345")); |
| 131 EXPECT_TRUE(CheckRoundTrip("123456")); |
| 132 } |
| 133 |
| 134 TEST_F(CompressedPacketTest, ReversedSequences) { |
| 135 EXPECT_TRUE(CheckRoundTrip("123456")); |
| 136 EXPECT_TRUE(CheckRoundTrip("12345")); |
| 137 EXPECT_TRUE(CheckRoundTrip("1234")); |
| 138 EXPECT_TRUE(CheckRoundTrip("123")); |
| 139 EXPECT_TRUE(CheckRoundTrip("12")); |
| 140 } |
| 141 |
| 142 TEST_F(CompressedPacketTest, CompressionStateRetainedAcrossCalls) { |
| 143 // Ensure that a character sequence is encoded in a more compact manner |
| 144 // if it is compressed more than once. |
| 145 int size_1 = Compress("1234").size(); |
| 146 int size_2 = Compress("1234").size(); |
| 147 EXPECT_GT(size_1, size_2); |
| 148 } |
| 149 |
| 150 TEST_F(CompressedPacketTest, LargeInput) { |
| 151 std::string big_str(kMaxPacketPayloadSizeBytes, 'A'); // 3MB of A's. |
| 152 EXPECT_TRUE(CheckRoundTrip(big_str)); |
| 153 } |
| 154 |
| 155 TEST_F(CompressedPacketTest, DecompressIllegallyLargePayload) { |
| 156 // We can't use the compressor to compress an illegally sized payload, however |
| 157 // we can concatenate the output of smaller payloads to form an uber-payload. |
| 158 std::string huge_block = |
| 159 Compress(std::string(kMaxPacketPayloadSizeBytes, 'A')) + |
| 160 Compress("1337 payl0ad 0verfl0w 'spl0it"); |
| 161 |
| 162 scoped_refptr<net::StringIOBuffer> compressed_str_buf( |
| 163 new net::StringIOBuffer(huge_block)); |
| 164 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| 165 new net::DrainableIOBuffer(compressed_str_buf.get(), |
| 166 compressed_str_buf->size())); |
| 167 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( |
| 168 new net::GrowableIOBuffer); |
| 169 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 170 .WillOnce( |
| 171 DoAll(CopyBuffer<0>(compressed_buf), |
| 172 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 173 net::TestCompletionCallback completion_cb_2; |
| 174 compressed_reader_->ReadPacket(decompressed_buf, completion_cb_2.callback()); |
| 175 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_2.WaitForResult()); |
| 176 } |
| 177 |
| 178 TEST_F(CompressedPacketTest, CompressIllegallyLargePayload) { |
| 179 scoped_refptr<net::IOBuffer> big_buf( |
| 180 new net::IOBuffer(kMaxPacketPayloadSizeBytes + 1)); |
| 181 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( |
| 182 big_buf.get(), kMaxPacketPayloadSizeBytes + 1)); |
| 183 net::TestCompletionCallback completion_cb_1; |
| 184 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 185 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_1.WaitForResult()); |
| 186 } |
| 187 |
| 188 TEST_F(CompressedPacketTest, PayloadSmallerThanDeclared) { |
| 189 scoped_refptr<net::StringIOBuffer> content_str_buf( |
| 190 new net::StringIOBuffer("Just a small payload")); |
| 191 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( |
| 192 content_str_buf.get(), content_str_buf->size())); |
| 193 scoped_refptr<net::DrainableIOBuffer> compressed_buf; |
| 194 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) |
| 195 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), |
| 196 InvokeCompletionCallback<1>(net::OK))); |
| 197 net::TestCompletionCallback completion_cb_1; |
| 198 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 199 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); |
| 200 |
| 201 // Increase the payload size header significantly. |
| 202 *(reinterpret_cast<uint32_t*>(compressed_buf->data())) = |
| 203 base::HostToNet32(kMaxPacketPayloadSizeBytes - 1); |
| 204 |
| 205 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 206 .WillOnce( |
| 207 DoAll(CopyBuffer<0>(compressed_buf), |
| 208 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 209 net::TestCompletionCallback completion_cb_2; |
| 210 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), |
| 211 completion_cb_2.callback()); |
| 212 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); |
| 213 } |
| 214 |
| 215 } // namespace |
| 216 } // namespace blimp |
OLD | NEW |