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()); | |
Wez
2016/03/29 00:45:40
Don't we use ASSERT in tests, rather than DCHECK?
Kevin M
2016/03/29 20:36:24
Done.
| |
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 std::string compressed = Compress(content); | |
97 std::string decompressed = Decompress(compressed); | |
98 return Decompress(Compress(content)) == content; | |
99 } | |
100 | |
101 MockPacketReader* mock_reader_; | |
102 MockPacketWriter* mock_writer_; | |
103 scoped_ptr<CompressedPacketReader> compressed_reader_; | |
104 scoped_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, DecompressIllegallyLargePayload) { | |
158 scoped_refptr<net::IOBuffer> garbage_buf(new net::IOBuffer(4)); | |
159 *(reinterpret_cast<uint32_t*>(garbage_buf->data())) = | |
160 base::HostToNet32(kMaxPacketPayloadSizeBytes + 1); | |
161 scoped_refptr<net::DrainableIOBuffer> compressed_buf( | |
162 new net::DrainableIOBuffer(garbage_buf.get(), 4)); | |
163 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( | |
164 new net::GrowableIOBuffer); | |
165 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | |
166 .WillOnce( | |
167 DoAll(CopyBuffer<0>(compressed_buf), | |
168 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | |
169 net::TestCompletionCallback completion_cb_2; | |
170 compressed_reader_->ReadPacket(decompressed_buf, completion_cb_2.callback()); | |
171 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_2.WaitForResult()); | |
172 } | |
173 | |
174 TEST_F(CompressedPacketTest, CompressIllegallyLargePayload) { | |
175 scoped_refptr<net::IOBuffer> big_buf( | |
176 new net::IOBuffer(kMaxPacketPayloadSizeBytes + 1)); | |
177 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( | |
178 big_buf.get(), kMaxPacketPayloadSizeBytes + 1)); | |
179 net::TestCompletionCallback completion_cb_1; | |
180 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | |
181 EXPECT_EQ(net::ERR_FILE_TOO_BIG, completion_cb_1.WaitForResult()); | |
182 } | |
183 | |
184 TEST_F(CompressedPacketTest, PayloadSmallerThanDeclared) { | |
185 scoped_refptr<net::StringIOBuffer> content_str_buf( | |
186 new net::StringIOBuffer("Just a small payload")); | |
187 scoped_refptr<net::DrainableIOBuffer> content_buf(new net::DrainableIOBuffer( | |
188 content_str_buf.get(), content_str_buf->size())); | |
189 scoped_refptr<net::DrainableIOBuffer> compressed_buf; | |
190 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) | |
191 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), | |
192 InvokeCompletionCallback<1>(net::OK))); | |
193 net::TestCompletionCallback completion_cb_1; | |
194 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | |
195 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); | |
196 | |
197 // Increase the payload size header significantly. | |
198 *(reinterpret_cast<uint32_t*>(compressed_buf->data())) = | |
199 base::HostToNet32(kMaxPacketPayloadSizeBytes - 1); | |
200 | |
201 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | |
202 .WillOnce( | |
203 DoAll(CopyBuffer<0>(compressed_buf), | |
204 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | |
205 net::TestCompletionCallback completion_cb_2; | |
206 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), | |
207 completion_cb_2.callback()); | |
208 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); | |
209 } | |
210 | |
211 } // namespace | |
212 } // namespace blimp | |
OLD | NEW |