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/memory/ptr_util.h" | |
6 #include "base/rand_util.h" | |
7 #include "base/sys_byteorder.h" | |
8 #include "blimp/net/common.h" | |
9 #include "blimp/net/compressed_packet_reader.h" | |
10 #include "blimp/net/compressed_packet_writer.h" | |
11 #include "blimp/net/test_common.h" | |
12 #include "net/base/test_completion_callback.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 using testing::_; | |
17 using testing::DoAll; | |
18 using testing::InvokeArgument; | |
19 using testing::SaveArg; | |
20 | |
21 namespace blimp { | |
22 namespace { | |
23 | |
24 // Calls the CompletionCallback (indicated by the 0-based index cb_idx) | |
25 // with the value |result|. | |
26 ACTION_TEMPLATE(InvokeCompletionCallback, | |
27 HAS_1_TEMPLATE_PARAMS(int, cb_idx), | |
28 AND_1_VALUE_PARAMS(result)) { | |
29 testing::get<cb_idx>(args).Run(result); | |
30 } | |
31 | |
32 // Copies a DrainableIOBuffer to a GrowableIOBuffer. | |
33 // |dest_buf_idx|: The 0-based index of a GrowableIOBuffer. | |
34 // |src_buf|: A DrainableIOBuffer. | |
35 ACTION_TEMPLATE(CopyBuffer, | |
36 HAS_1_TEMPLATE_PARAMS(int, dest_buf_idx), | |
37 AND_1_VALUE_PARAMS(src_buf)) { | |
38 const auto& dest_buf = testing::get<dest_buf_idx>(args); | |
39 ASSERT_TRUE(dest_buf); | |
40 ASSERT_TRUE(src_buf); | |
41 ASSERT_EQ(0, dest_buf->offset()); | |
42 src_buf->SetOffset(0); | |
43 dest_buf->SetCapacity(src_buf->BytesRemaining()); | |
44 memcpy(dest_buf->data(), src_buf->data(), src_buf->BytesRemaining()); | |
45 } | |
46 | |
47 class CompressedPacketTest : public testing::Test { | |
48 public: | |
49 CompressedPacketTest() | |
50 : mock_reader_(new MockPacketReader), | |
51 mock_writer_(new MockPacketWriter), | |
52 compressed_reader_( | |
53 new CompressedPacketReader(base::WrapUnique(mock_reader_))), | |
54 compressed_writer_( | |
55 new CompressedPacketWriter(base::WrapUnique(mock_writer_))) {} | |
56 ~CompressedPacketTest() override {} | |
57 | |
58 protected: | |
59 // Returns the compressed result of |content|. | |
60 std::string Compress(const std::string& content) { | |
61 scoped_refptr<net::StringIOBuffer> content_str_buf( | |
62 new net::StringIOBuffer(content)); | |
63 scoped_refptr<net::DrainableIOBuffer> content_buf( | |
64 new net::DrainableIOBuffer(content_str_buf.get(), | |
65 content_str_buf->size())); | |
66 scoped_refptr<net::DrainableIOBuffer> compressed_buf; | |
67 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) | |
68 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), | |
69 InvokeCompletionCallback<1>(net::OK))); | |
70 net::TestCompletionCallback completion_cb_1; | |
71 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | |
72 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); | |
73 return std::string(compressed_buf->data(), | |
74 compressed_buf->BytesRemaining()); | |
75 } | |
76 | |
77 // Returns the decompressed result of |compressed|. | |
78 std::string Decompress(const std::string& compressed) { | |
79 scoped_refptr<net::StringIOBuffer> compressed_str_buf( | |
80 new net::StringIOBuffer(compressed)); | |
81 scoped_refptr<net::DrainableIOBuffer> compressed_buf( | |
82 new net::DrainableIOBuffer(compressed_str_buf.get(), | |
83 compressed_str_buf->size())); | |
84 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( | |
85 new net::GrowableIOBuffer); | |
86 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | |
87 .WillOnce(DoAll( | |
88 CopyBuffer<0>(compressed_buf), | |
89 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | |
90 net::TestCompletionCallback completion_cb_2; | |
91 compressed_reader_->ReadPacket(decompressed_buf, | |
92 completion_cb_2.callback()); | |
93 int size = completion_cb_2.WaitForResult(); | |
94 return std::string(decompressed_buf->data(), size); | |
95 } | |
96 | |
97 bool CheckRoundTrip(const std::string& content) { | |
98 return Decompress(Compress(content)) == content; | |
99 } | |
100 | |
101 MockPacketReader* mock_reader_; | |
102 MockPacketWriter* mock_writer_; | |
103 std::unique_ptr<CompressedPacketReader> compressed_reader_; | |
104 std::unique_ptr<CompressedPacketWriter> compressed_writer_; | |
105 testing::InSequence s; | |
106 }; | |
107 | |
108 TEST_F(CompressedPacketTest, Empty) { | |
109 EXPECT_TRUE(CheckRoundTrip("1234")); | |
110 EXPECT_TRUE(CheckRoundTrip("")); | |
111 EXPECT_TRUE(CheckRoundTrip("1234")); | |
112 } | |
113 | |
114 TEST_F(CompressedPacketTest, Simple) { | |
115 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA"; | |
116 std::string compressed = Compress(source); | |
117 std::string decompressed = Decompress(compressed); | |
118 EXPECT_GT(source.size(), compressed.size()); | |
119 EXPECT_EQ(source, decompressed); | |
120 } | |
121 | |
122 TEST_F(CompressedPacketTest, DisjointSequences) { | |
123 EXPECT_TRUE(CheckRoundTrip("1234")); | |
124 EXPECT_TRUE(CheckRoundTrip("5678")); | |
125 EXPECT_TRUE(CheckRoundTrip("ABCD")); | |
126 } | |
127 | |
128 TEST_F(CompressedPacketTest, AdditiveSequences) { | |
129 EXPECT_TRUE(CheckRoundTrip("12")); | |
130 EXPECT_TRUE(CheckRoundTrip("123")); | |
131 EXPECT_TRUE(CheckRoundTrip("1234")); | |
132 EXPECT_TRUE(CheckRoundTrip("12345")); | |
133 EXPECT_TRUE(CheckRoundTrip("123456")); | |
134 } | |
135 | |
136 TEST_F(CompressedPacketTest, ReversedSequences) { | |
137 EXPECT_TRUE(CheckRoundTrip("123456")); | |
138 EXPECT_TRUE(CheckRoundTrip("12345")); | |
139 EXPECT_TRUE(CheckRoundTrip("1234")); | |
140 EXPECT_TRUE(CheckRoundTrip("123")); | |
141 EXPECT_TRUE(CheckRoundTrip("12")); | |
142 } | |
143 | |
144 TEST_F(CompressedPacketTest, CompressionStateRetainedAcrossCalls) { | |
145 // Ensure that a character sequence is encoded in a more compact manner | |
146 // if it is compressed more than once. | |
147 int size_1 = Compress("1234").size(); | |
148 int size_2 = Compress("1234").size(); | |
149 EXPECT_GT(size_1, size_2); | |
150 } | |
151 | |
152 TEST_F(CompressedPacketTest, LargeInput) { | |
153 std::string big_str(kMaxPacketPayloadSizeBytes, 'A'); // 3MB of A's. | |
154 EXPECT_TRUE(CheckRoundTrip(big_str)); | |
155 } | |
156 | |
157 TEST_F(CompressedPacketTest, LowCompressionRatio) { | |
158 // This size (2338) was found "in the wild" to repro an issue with output | |
159 // buffer overflows. | |
160 const int data_size = 2338; | |
161 | |
162 EXPECT_TRUE(CheckRoundTrip(base::RandBytesAsString(data_size))); | |
163 EXPECT_TRUE(CheckRoundTrip(base::RandBytesAsString(data_size))); | |
164 } | |
165 | |
166 TEST_F(CompressedPacketTest, DecompressIllegallyLargePayload) { | |
167 // We can't use the compressor to compress an illegally sized payload, however | |
168 // we can concatenate the output of smaller payloads to form an uber-payload. | |
169 std::string huge_block = | |
170 Compress(std::string(kMaxPacketPayloadSizeBytes, 'A')) + | |
171 Compress("1337 payl0ad 0verfl0w 'spl0it"); | |
172 | |
173 scoped_refptr<net::StringIOBuffer> compressed_str_buf( | |
174 new net::StringIOBuffer(huge_block)); | |
175 scoped_refptr<net::DrainableIOBuffer> compressed_buf( | |
176 new net::DrainableIOBuffer(compressed_str_buf.get(), | |
177 compressed_str_buf->size())); | |
178 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( | |
179 new net::GrowableIOBuffer); | |
180 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | |
181 .WillOnce( | |
182 DoAll(CopyBuffer<0>(compressed_buf), | |
183 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | |
184 net::TestCompletionCallback completion_cb_2; | |
185 compressed_reader_->ReadPacket(decompressed_buf, completion_cb_2.callback()); | |
186 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_2.WaitForResult()); | |
187 } | |
188 | |
189 TEST_F(CompressedPacketTest, CompressIllegallyLargePayload) { | |
190 scoped_refptr<net::IOBuffer> big_buf( | |
191 new net::IOBuffer(kMaxPacketPayloadSizeBytes + 1)); | |
192 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( | |
193 big_buf.get(), kMaxPacketPayloadSizeBytes + 1)); | |
194 net::TestCompletionCallback completion_cb_1; | |
195 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | |
196 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_1.WaitForResult()); | |
197 } | |
198 | |
199 TEST_F(CompressedPacketTest, PayloadSmallerThanDeclared) { | |
200 scoped_refptr<net::StringIOBuffer> content_str_buf( | |
201 new net::StringIOBuffer("Just a small payload")); | |
202 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( | |
203 content_str_buf.get(), content_str_buf->size())); | |
204 scoped_refptr<net::DrainableIOBuffer> compressed_buf; | |
205 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) | |
206 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), | |
207 InvokeCompletionCallback<1>(net::OK))); | |
208 net::TestCompletionCallback completion_cb_1; | |
209 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | |
210 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); | |
211 | |
212 // Increase the payload size header significantly. | |
213 *(reinterpret_cast<uint32_t*>(compressed_buf->data())) = | |
214 base::HostToNet32(kMaxPacketPayloadSizeBytes - 1); | |
215 | |
216 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | |
217 .WillOnce( | |
218 DoAll(CopyBuffer<0>(compressed_buf), | |
219 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | |
220 net::TestCompletionCallback completion_cb_2; | |
221 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), | |
222 completion_cb_2.callback()); | |
223 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); | |
224 } | |
225 | |
226 } // namespace | |
227 } // namespace blimp | |
OLD | NEW |