Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: blimp/net/stream_packet_reader_unittest.cc

Issue 2632803002: Remove all blimp network code. (Closed)
Patch Set: merge from origin/master for good measure Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/net/stream_packet_reader.cc ('k') | blimp/net/stream_packet_writer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 <stddef.h>
6 #include <memory>
7 #include <string>
8
9 #include "base/at_exit.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/sys_byteorder.h"
13 #include "blimp/net/blimp_stats.h"
14 #include "blimp/net/common.h"
15 #include "blimp/net/stream_packet_reader.h"
16 #include "blimp/net/test_common.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/socket/socket.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using testing::_;
26 using testing::DoAll;
27 using testing::Mock;
28 using testing::NotNull;
29 using testing::Return;
30 using testing::SaveArg;
31 using testing::WithArg;
32
33 namespace blimp {
34 namespace {
35
36 class StreamPacketReaderTest : public testing::Test {
37 public:
38 StreamPacketReaderTest()
39 : buffer_(new net::GrowableIOBuffer),
40 test_msg_("U WOT M8"),
41 data_reader_(&socket_) {}
42
43 ~StreamPacketReaderTest() override {}
44
45 void SetUp() override {
46 ASSERT_EQ(0, BlimpStats::GetInstance()->Get(BlimpStats::BYTES_RECEIVED));
47 }
48
49 void ReadPacket() { data_reader_.ReadPacket(buffer_, callback_.callback()); }
50
51 protected:
52 base::ShadowingAtExitManager at_exit_manager;
53 base::MessageLoop message_loop_;
54 scoped_refptr<net::GrowableIOBuffer> buffer_;
55 std::string test_msg_;
56 net::TestCompletionCallback callback_;
57 testing::StrictMock<MockStreamSocket> socket_;
58 testing::InSequence sequence_;
59 StreamPacketReader data_reader_;
60 };
61
62 // Successful read with 1 async header read and 1 async payload read.
63 TEST_F(StreamPacketReaderTest, ReadAsyncHeaderAsyncPayload) {
64 net::CompletionCallback socket_cb;
65
66 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
67 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
68 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)));
69 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
70 .WillOnce(DoAll(FillBufferFromString<0>(test_msg_),
71 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)));
72
73 ReadPacket();
74 socket_cb.Run(kPacketHeaderSizeBytes);
75 socket_cb.Run(test_msg_.size());
76 ASSERT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
77 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg_.size(), test_msg_));
78 }
79
80 // Successful read with 1 async header read and 1 sync payload read.
81 TEST_F(StreamPacketReaderTest, ReadAsyncHeaderSyncPayload) {
82 net::CompletionCallback socket_cb;
83
84 // Asynchronous payload read expectation.
85 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
86 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
87 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)));
88
89 // Synchronous payload read expectation. Fills the buffer and returns
90 // immediately.
91 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
92 .WillOnce(
93 DoAll(FillBufferFromString<0>(test_msg_), Return(test_msg_.size())));
94
95 ReadPacket();
96 EXPECT_FALSE(callback_.have_result());
97
98 socket_cb.Run(kPacketHeaderSizeBytes);
99 ASSERT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
100 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg_.size(), test_msg_));
101 }
102
103 // Successful read with 1 sync header read and 1 async payload read.
104 TEST_F(StreamPacketReaderTest, ReadSyncHeaderAsyncPayload) {
105 net::CompletionCallback socket_cb;
106
107 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
108 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
109 Return(kPacketHeaderSizeBytes)));
110 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
111 .WillOnce(DoAll(FillBufferFromString<0>(test_msg_),
112 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)));
113
114 ReadPacket();
115 socket_cb.Run(test_msg_.size());
116 ASSERT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
117 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg_.size(), test_msg_));
118 }
119
120 // Successful read with 1 sync header read and 1 sync payload read.
121 TEST_F(StreamPacketReaderTest, ReadSyncHeaderSyncPayload) {
122 net::CompletionCallback socket_cb;
123
124 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
125 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
126 Return(kPacketHeaderSizeBytes)));
127 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
128 .WillOnce(
129 DoAll(FillBufferFromString<0>(test_msg_), Return(test_msg_.size())));
130
131 ReadPacket();
132 ASSERT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
133 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg_.size(), test_msg_));
134 }
135
136 // Successful read of 2 messages, header and payload reads all completing
137 // synchronously with no partial results.
138 TEST_F(StreamPacketReaderTest, ReadMultipleMessagesSync) {
139 net::CompletionCallback socket_cb;
140 std::string test_msg2 = test_msg_ + "SlightlyLongerString";
141
142 // Read the first message's header synchronously.
143 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
144 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
145 Return(kPacketHeaderSizeBytes)))
146 .RetiresOnSaturation();
147
148 // Read the first message's payload synchronously.
149 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
150 .WillOnce(
151 DoAll(FillBufferFromString<0>(test_msg_), Return(test_msg_.size())))
152 .RetiresOnSaturation();
153
154 // Read the second message's header synchronously.
155 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
156 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg2.size())),
157 Return(kPacketHeaderSizeBytes)))
158 .RetiresOnSaturation();
159
160 // Read the second message's payload synchronously.
161 EXPECT_CALL(socket_, Read(NotNull(), test_msg2.size(), _))
162 .WillOnce(
163 DoAll(FillBufferFromString<0>(test_msg2), Return(test_msg2.size())))
164 .RetiresOnSaturation();
165
166 ReadPacket();
167 ASSERT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
168 EXPECT_EQ(static_cast<int>(test_msg_.size()),
169 BlimpStats::GetInstance()->Get(BlimpStats::BYTES_RECEIVED));
170
171 ReadPacket();
172 ASSERT_EQ(static_cast<int>(test_msg2.size()), callback_.WaitForResult());
173 EXPECT_EQ(static_cast<int>(test_msg_.size() + test_msg2.size()),
174 BlimpStats::GetInstance()->Get(BlimpStats::BYTES_RECEIVED));
175
176 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg2.size(), test_msg2));
177 EXPECT_FALSE(callback_.have_result());
178 }
179
180 // Successful read of 2 messages, header and payload reads all completing
181 // asynchronously with no partial results.
182 TEST_F(StreamPacketReaderTest, ReadMultipleMessagesAsync) {
183 net::TestCompletionCallback read_cb1;
184 net::TestCompletionCallback read_cb2;
185 net::CompletionCallback socket_cb;
186
187 // Read a header asynchronously.
188 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
189 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
190 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)))
191 .RetiresOnSaturation();
192
193 // Read a payload asynchronously.
194 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
195 .WillOnce(DoAll(FillBufferFromString<0>(test_msg_),
196 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)))
197 .RetiresOnSaturation();
198
199 // Read a header asynchronously.
200 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
201 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
202 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)))
203 .RetiresOnSaturation();
204
205 // Read a payload asynchronously.
206 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
207 .WillOnce(DoAll(FillBufferFromString<0>(test_msg_),
208 SaveArg<2>(&socket_cb), Return(net::ERR_IO_PENDING)))
209 .RetiresOnSaturation();
210
211 data_reader_.ReadPacket(buffer_, read_cb1.callback());
212 socket_cb.Run(kPacketHeaderSizeBytes);
213 socket_cb.Run(test_msg_.size());
214 ASSERT_EQ(static_cast<int>(test_msg_.size()), read_cb1.WaitForResult());
215
216 data_reader_.ReadPacket(buffer_, read_cb2.callback());
217 socket_cb.Run(kPacketHeaderSizeBytes);
218 socket_cb.Run(test_msg_.size());
219 ASSERT_EQ(static_cast<int>(test_msg_.size()), read_cb2.WaitForResult());
220 EXPECT_TRUE(BufferStartsWith(buffer_.get(), test_msg_.size(), test_msg_));
221 }
222
223 // Verify that partial header reads are supported.
224 // Read #0: 1 header byte is read.
225 // Read #1: Remainder of header bytes read.
226 TEST_F(StreamPacketReaderTest, PartialHeaderReadAsync) {
227 net::CompletionCallback cb;
228 std::string header = EncodeHeader(test_msg_.size());
229
230 // The first byte is received (sliced via substr()).
231 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
232 .WillOnce(DoAll(FillBufferFromString<0>(header.substr(0, 1)),
233 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
234 // The remainder is received (sliced via substr()).
235 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes - 1, _))
236 .WillOnce(DoAll(
237 FillBufferFromString<0>(header.substr(1, kPacketHeaderSizeBytes - 1)),
238 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
239 // Verify that we start reading the body once the header has been fully read.
240 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
241 .WillOnce(Return(net::ERR_IO_PENDING));
242
243 ReadPacket();
244 cb.Run(1);
245 cb.Run(kPacketHeaderSizeBytes - 1);
246 }
247
248 // Verify that partial payload reads are supported.
249 // Read #0: Header is fully read synchronously.
250 // Read #1: First payload byte is read. (Um, it's an acoustic cup modem.)
251 // Read #2: Remainder of payload bytes are read.
252 TEST_F(StreamPacketReaderTest, PartialPayloadReadAsync) {
253 net::CompletionCallback cb;
254
255 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
256 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
257 Return(kPacketHeaderSizeBytes)));
258 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
259 .WillOnce(DoAll(FillBufferFromString<0>(test_msg_.substr(0, 1)),
260 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
261 ReadPacket();
262 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size() - 1, _))
263 .WillOnce(DoAll(
264 FillBufferFromString<0>(test_msg_.substr(1, test_msg_.size() - 1)),
265 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
266
267 cb.Run(1);
268 cb.Run(test_msg_.size() - 1);
269 EXPECT_EQ(static_cast<int>(test_msg_.size()), callback_.WaitForResult());
270 }
271
272 // Verify that synchronous header read errors are reported correctly.
273 TEST_F(StreamPacketReaderTest, ReadHeaderErrorSync) {
274 net::CompletionCallback cb;
275 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
276 .WillOnce(Return(net::ERR_FAILED));
277 ReadPacket();
278 EXPECT_EQ(net::ERR_FAILED, callback_.WaitForResult());
279 }
280
281 // Verify that synchronous payload read errors are reported correctly.
282 TEST_F(StreamPacketReaderTest, ReadPayloadErrorSync) {
283 net::CompletionCallback cb;
284
285 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
286 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
287 Return(kPacketHeaderSizeBytes)));
288 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
289 .WillOnce(Return(net::ERR_FAILED));
290
291 ReadPacket();
292 EXPECT_EQ(net::ERR_FAILED, callback_.WaitForResult());
293 }
294
295 // Verify EOF handling during synchronous header reads.
296 TEST_F(StreamPacketReaderTest, ReadHeaderEOFSync) {
297 net::CompletionCallback cb;
298 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
299 .WillOnce(Return(0));
300 ReadPacket();
301 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, callback_.WaitForResult());
302 }
303
304 // Verify EOF handling during synchronous payload reads.
305 TEST_F(StreamPacketReaderTest, ReadPayloadEOFSync) {
306 net::CompletionCallback cb;
307
308 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
309 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
310 Return(kPacketHeaderSizeBytes)));
311 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
312 .WillOnce(Return(0));
313
314 ReadPacket();
315 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, callback_.WaitForResult());
316 }
317
318 // Verify EOF handling during asynchronous header reads.
319 TEST_F(StreamPacketReaderTest, ReadHeaderEOFAsync) {
320 net::CompletionCallback cb;
321 net::TestCompletionCallback test_cb;
322
323 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
324 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
325 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
326
327 ReadPacket();
328 cb.Run(0);
329 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, callback_.WaitForResult());
330 }
331
332 // Verify EOF handling during asynchronous payload reads.
333 TEST_F(StreamPacketReaderTest, ReadPayloadEOFAsync) {
334 net::CompletionCallback cb;
335
336 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
337 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
338 Return(kPacketHeaderSizeBytes)));
339 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
340 .WillOnce(DoAll(SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
341
342 ReadPacket();
343 cb.Run(0);
344 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, callback_.WaitForResult());
345 }
346
347 // Verify that async header read errors are reported correctly.
348 TEST_F(StreamPacketReaderTest, ReadHeaderErrorAsync) {
349 net::CompletionCallback cb;
350 net::TestCompletionCallback test_cb;
351
352 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
353 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
354 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
355
356 ReadPacket();
357 cb.Run(net::ERR_FAILED);
358 EXPECT_EQ(net::ERR_FAILED, callback_.WaitForResult());
359 }
360
361 // Verify that asynchronous paylod read errors are reported correctly.
362 TEST_F(StreamPacketReaderTest, ReadPayloadErrorAsync) {
363 net::CompletionCallback cb;
364
365 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
366 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
367 Return(kPacketHeaderSizeBytes)));
368 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
369 .WillOnce(DoAll(SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
370
371 ReadPacket();
372 cb.Run(net::ERR_FAILED);
373 EXPECT_EQ(net::ERR_FAILED, callback_.WaitForResult());
374 }
375
376 // Verify that async header read completions don't break us if the
377 // StreamPacketReader object was destroyed.
378 TEST_F(StreamPacketReaderTest, ReaderDeletedDuringAsyncHeaderRead) {
379 net::CompletionCallback cb;
380 net::TestCompletionCallback test_cb;
381 std::unique_ptr<StreamPacketReader> reader(new StreamPacketReader(&socket_));
382
383 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
384 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
385 SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
386
387 reader->ReadPacket(buffer_, callback_.callback());
388 reader.reset(); // Delete the reader object.
389 cb.Run(kPacketHeaderSizeBytes); // Complete the socket operation.
390 }
391
392 // Verify that async payload read completions don't break us if the
393 // StreamPacketReader object was destroyed.
394 TEST_F(StreamPacketReaderTest, ReaderDeletedDuringAsyncPayloadRead) {
395 net::CompletionCallback cb;
396 std::unique_ptr<StreamPacketReader> reader(new StreamPacketReader(&socket_));
397
398 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
399 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(test_msg_.size())),
400 Return(kPacketHeaderSizeBytes)));
401 EXPECT_CALL(socket_, Read(NotNull(), test_msg_.size(), _))
402 .WillOnce(DoAll(SaveArg<2>(&cb), Return(net::ERR_IO_PENDING)));
403 reader->ReadPacket(buffer_, callback_.callback());
404
405 reader.reset(); // Delete the reader object.
406 cb.Run(net::ERR_FAILED); // Complete the socket operation.
407 }
408
409 // Verify that zero-length payload is reported as an erroneous input.
410 TEST_F(StreamPacketReaderTest, ReadWhatIsThisAPacketForAnts) {
411 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
412 .WillOnce(DoAll(FillBufferFromString<0>(EncodeHeader(0)),
413 Return(kPacketHeaderSizeBytes)))
414 .RetiresOnSaturation();
415
416 ReadPacket();
417 EXPECT_EQ(net::ERR_INVALID_RESPONSE, callback_.WaitForResult());
418 }
419
420 // Verify that an illegally large payloads is reported as an erroneous inputs.
421 TEST_F(StreamPacketReaderTest, ReadErrorIllegallyLargePayload) {
422 EXPECT_CALL(socket_, Read(NotNull(), kPacketHeaderSizeBytes, _))
423 .WillOnce(DoAll(
424 FillBufferFromString<0>(EncodeHeader(kMaxPacketPayloadSizeBytes + 1)),
425 Return(kPacketHeaderSizeBytes)));
426
427 ReadPacket();
428 EXPECT_EQ(net::ERR_INVALID_RESPONSE, callback_.WaitForResult());
429 }
430
431 } // namespace
432
433 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/stream_packet_reader.cc ('k') | blimp/net/stream_packet_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698