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.
7 #include "base/macros.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/socket/client_socket_handle.h"
14 #include "net/socket/socket_test_util.h"
15 #include "net/socket/transport_client_socket_pool.h"
16 #include "testing/gtest/include/gtest/gtest-spi.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
20 //-----------------------------------------------------------------------------
26 const char kMsg1
[] = "\0hello!\xff";
27 const int kLen1
= arraysize(kMsg1
);
28 const char kMsg2
[] = "\0a2345678\0";
29 const int kLen2
= arraysize(kMsg2
);
30 const char kMsg3
[] = "bye!";
31 const int kLen3
= arraysize(kMsg3
);
32 const char kMsg4
[] = "supercalifragilisticexpialidocious";
33 const int kLen4
= arraysize(kMsg4
);
35 // Helper class for starting the next operation operation reentrantly after the
36 // previous operation completed asynchronously. When OnIOComplete is called,
37 // it will first verify that the previous operation behaved as expected. This is
38 // specified by either SetExpectedRead or SetExpectedWrite. It will then invoke
39 // a read or write operation specified by SetInvokeRead or SetInvokeWrite.
40 class ReentrantHelper
{
42 ReentrantHelper(StreamSocket
* socket
)
45 first_read_data_(nullptr),
48 second_write_data_(nullptr),
51 // Expect that the previous operation will return |first_len| and will fill
52 // |first_read_data_| with |first_read_data|.
53 void SetExpectedRead(const char* first_read_data
, int first_len
) {
55 first_read_buf_
= new IOBuffer(first_len
);
56 first_read_data_
= first_read_data
;
57 first_len_
= first_len
;
60 // Expect that the previous operation will return |first_len|.
61 void SetExpectedWrite(int first_len
) {
63 first_len_
= first_len
;
66 // After verifying expectations, invoke a read of |read_len| bytes into
67 // |read_buf|, notifying |callback| when complete.
68 void SetInvokeRead(scoped_refptr
<IOBuffer
> read_buf
,
71 CompletionCallback callback
) {
73 second_read_buf_
= read_buf
;
74 second_rv_
= second_rv
;
75 second_callback_
= callback
;
76 second_len_
= read_len
;
79 // After verifying expectations, invoke a write of |write_len| bytes from
80 // |write_data|, notifying |callback| when complete.
81 void SetInvokeWrite(const char* write_data
,
84 CompletionCallback callback
) {
86 second_rv_
= second_rv
;
87 second_write_data_
= write_data
;
88 second_callback_
= callback
;
89 second_len_
= write_len
;
92 // Returns the OnIOComplete callback for this helper.
93 CompletionCallback
callback() {
94 return base::Bind(&ReentrantHelper::OnIOComplete
, base::Unretained(this));
97 // Retuns the buffer where data is expected to have been written,
98 // when checked by SetExpectRead()
99 scoped_refptr
<IOBuffer
> read_buf() { return first_read_buf_
; }
102 void OnIOComplete(int rv
) {
103 CHECK_NE(-1, first_len_
) << "Expectation not set.";
104 CHECK_NE(-1, second_len_
) << "Invocation not set.";
105 ASSERT_EQ(first_len_
, rv
);
107 ASSERT_EQ(std::string(first_read_data_
, first_len_
),
108 std::string(first_read_buf_
->data(), rv
));
112 ASSERT_EQ(second_rv_
, socket_
->Read(second_read_buf_
.get(), second_len_
,
115 scoped_refptr
<IOBuffer
> write_buf
= new IOBuffer(second_len_
);
116 memcpy(write_buf
->data(), second_write_data_
, second_len_
);
117 ASSERT_EQ(second_rv_
,
118 socket_
->Write(write_buf
.get(), second_len_
, second_callback_
));
122 StreamSocket
* socket_
;
125 scoped_refptr
<IOBuffer
> first_read_buf_
;
126 const char* first_read_data_
;
129 CompletionCallback second_callback_
;
132 scoped_refptr
<IOBuffer
> second_read_buf_
;
133 const char* second_write_data_
;
136 DISALLOW_COPY_AND_ASSIGN(ReentrantHelper
);
139 class SequencedSocketDataTest
: public testing::Test
{
141 SequencedSocketDataTest();
142 ~SequencedSocketDataTest() override
;
144 // This method is used as the completion callback for an async read
145 // operation and when invoked, it verifies that the correct data was read,
146 // then reads from the socket and verifies that that it returns the correct
148 void ReentrantReadCallback(const char* data
,
154 // This method is used at the completion callback for an async operation.
155 // When executed, verifies that |rv| equals |expected_rv| and then
156 // attempts an aync read from the socket into |read_buf_| (initialized
157 // to |read_buf_len|) using |callback|.
158 void ReentrantAsyncReadCallback(int len1
, int len2
, int rv
);
160 // This method is used as the completion callback for an async write
161 // operation and when invoked, it verifies that the write returned correctly,
163 // attempts to write to the socket and verifies that that it returns the
165 void ReentrantWriteCallback(int expected_rv1
,
171 // This method is used at the completion callback for an async operation.
172 // When executed, verifies that |rv| equals |expected_rv| and then
173 // attempts an aync write of |data| with |callback|
174 void ReentrantAsyncWriteCallback(const char* data
,
176 CompletionCallback callback
,
180 // Callback which adds a failure if it's ever called.
181 void FailingCompletionCallback(int rv
);
184 void Initialize(MockRead
* reads
,
187 size_t writes_count
);
189 void AssertSyncReadEquals(const char* data
, int len
);
190 void AssertAsyncReadEquals(const char* data
, int len
);
191 void AssertReadReturns(int len
, int rv
);
192 void AssertReadBufferEquals(const char* data
, int len
);
194 void AssertSyncWriteEquals(const char* data
, int len
);
195 void AssertAsyncWriteEquals(const char* data
, int len
);
196 void AssertWriteReturns(const char* data
, int len
, int rv
);
199 // When a given test completes, data_.at_eof() is expected to
200 // match the value specified here. Most test should consume all
201 // reads and writes, but some tests verify error handling behavior
202 // do not consume all data.
203 void set_expect_eof(bool expect_eof
) { expect_eof_
= expect_eof
; }
205 TestCompletionCallback read_callback_
;
206 scoped_refptr
<IOBuffer
> read_buf_
;
207 TestCompletionCallback write_callback_
;
208 CompletionCallback failing_callback_
;
212 MockConnect connect_data_
;
213 scoped_ptr
<SequencedSocketData
> data_
;
215 const HostPortPair endpoint_
;
216 scoped_refptr
<TransportSocketParams
> tcp_params_
;
217 MockClientSocketFactory socket_factory_
;
218 MockTransportClientSocketPool socket_pool_
;
219 ClientSocketHandle connection_
;
222 DISALLOW_COPY_AND_ASSIGN(SequencedSocketDataTest
);
225 SequencedSocketDataTest::SequencedSocketDataTest()
227 base::Bind(&SequencedSocketDataTest::FailingCompletionCallback
,
228 base::Unretained(this))),
230 connect_data_(SYNCHRONOUS
, OK
),
231 endpoint_("www.google.com", 443),
232 tcp_params_(new TransportSocketParams(
236 OnHostResolutionCallback(),
237 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
)),
238 socket_pool_(10, 10, &socket_factory_
),
242 SequencedSocketDataTest::~SequencedSocketDataTest() {
243 // Make sure no unexpected pending tasks will cause a failure.
244 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(expect_eof_
, data_
->AllReadDataConsumed());
247 EXPECT_EQ(expect_eof_
, data_
->AllWriteDataConsumed());
251 void SequencedSocketDataTest::Initialize(MockRead
* reads
,
254 size_t writes_count
) {
256 new SequencedSocketData(reads
, reads_count
, writes
, writes_count
));
257 data_
->set_connect_data(connect_data_
);
258 socket_factory_
.AddSocketDataProvider(data_
.get());
262 endpoint_
.ToString(), tcp_params_
, LOWEST
, CompletionCallback(),
263 reinterpret_cast<TransportClientSocketPool
*>(&socket_pool_
),
265 sock_
= connection_
.socket();
268 void SequencedSocketDataTest::AssertSyncReadEquals(const char* data
, int len
) {
269 // Issue the read, which will complete immediately.
270 AssertReadReturns(len
, len
);
271 AssertReadBufferEquals(data
, len
);
274 void SequencedSocketDataTest::AssertAsyncReadEquals(const char* data
, int len
) {
275 // Issue the read, which will be completed asynchronously.
276 AssertReadReturns(len
, ERR_IO_PENDING
);
278 EXPECT_TRUE(sock_
->IsConnected());
280 // Now the read should complete.
281 ASSERT_EQ(len
, read_callback_
.WaitForResult());
282 AssertReadBufferEquals(data
, len
);
285 void SequencedSocketDataTest::AssertReadReturns(int len
, int rv
) {
286 read_buf_
= new IOBuffer(len
);
287 if (rv
== ERR_IO_PENDING
) {
288 ASSERT_EQ(rv
, sock_
->Read(read_buf_
.get(), len
, read_callback_
.callback()));
289 ASSERT_FALSE(read_callback_
.have_result());
291 ASSERT_EQ(rv
, sock_
->Read(read_buf_
.get(), len
, failing_callback_
));
295 void SequencedSocketDataTest::AssertReadBufferEquals(const char* data
,
297 ASSERT_EQ(std::string(data
, len
), std::string(read_buf_
->data(), len
));
300 void SequencedSocketDataTest::AssertSyncWriteEquals(const char* data
, int len
) {
301 // Issue the write, which should be complete immediately.
302 AssertWriteReturns(data
, len
, len
);
303 ASSERT_FALSE(write_callback_
.have_result());
306 void SequencedSocketDataTest::AssertAsyncWriteEquals(const char* data
,
308 // Issue the read, which should be completed asynchronously.
309 AssertWriteReturns(data
, len
, ERR_IO_PENDING
);
311 EXPECT_FALSE(read_callback_
.have_result());
312 EXPECT_TRUE(sock_
->IsConnected());
314 ASSERT_EQ(len
, write_callback_
.WaitForResult());
317 void SequencedSocketDataTest::CompleteRead() {
318 data_
->CompleteRead();
321 void SequencedSocketDataTest::AssertWriteReturns(const char* data
,
324 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
325 memcpy(buf
->data(), data
, len
);
327 if (rv
== ERR_IO_PENDING
) {
328 ASSERT_EQ(rv
, sock_
->Write(buf
.get(), len
, write_callback_
.callback()));
329 ASSERT_FALSE(write_callback_
.have_result());
331 ASSERT_EQ(rv
, sock_
->Write(buf
.get(), len
, failing_callback_
));
335 void SequencedSocketDataTest::ReentrantReadCallback(const char* data
,
341 AssertReadBufferEquals(data
, len1
);
343 AssertReadReturns(len2
, expected_rv2
);
346 void SequencedSocketDataTest::ReentrantAsyncReadCallback(int expected_rv
,
349 ASSERT_EQ(expected_rv
, rv
);
351 AssertReadReturns(len
, ERR_IO_PENDING
);
354 void SequencedSocketDataTest::ReentrantWriteCallback(int expected_rv1
,
359 ASSERT_EQ(expected_rv1
, rv
);
361 AssertWriteReturns(data
, len
, expected_rv2
);
364 void SequencedSocketDataTest::ReentrantAsyncWriteCallback(
367 CompletionCallback callback
,
370 EXPECT_EQ(expected_rv
, rv
);
371 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(len
));
372 memcpy(write_buf
->data(), data
, len
);
373 EXPECT_EQ(ERR_IO_PENDING
, sock_
->Write(write_buf
.get(), len
, callback
));
376 void SequencedSocketDataTest::FailingCompletionCallback(int rv
) {
377 ADD_FAILURE() << "Callback should not have been invoked";
382 TEST_F(SequencedSocketDataTest
, SingleSyncRead
) {
384 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
387 Initialize(reads
, arraysize(reads
), nullptr, 0);
388 AssertSyncReadEquals(kMsg1
, kLen1
);
391 TEST_F(SequencedSocketDataTest
, MultipleSyncReads
) {
393 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
394 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
395 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
396 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 3),
397 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
398 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 5),
399 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
402 Initialize(reads
, arraysize(reads
), nullptr, 0);
404 AssertSyncReadEquals(kMsg1
, kLen1
);
405 AssertSyncReadEquals(kMsg2
, kLen2
);
406 AssertSyncReadEquals(kMsg3
, kLen3
);
407 AssertSyncReadEquals(kMsg3
, kLen3
);
408 AssertSyncReadEquals(kMsg2
, kLen2
);
409 AssertSyncReadEquals(kMsg3
, kLen3
);
410 AssertSyncReadEquals(kMsg1
, kLen1
);
413 TEST_F(SequencedSocketDataTest
, SingleAsyncRead
) {
415 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
418 Initialize(reads
, arraysize(reads
), nullptr, 0);
420 AssertAsyncReadEquals(kMsg1
, kLen1
);
423 TEST_F(SequencedSocketDataTest
, MultipleAsyncReads
) {
425 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
426 MockRead(ASYNC
, kMsg2
, kLen2
, 1),
427 MockRead(ASYNC
, kMsg3
, kLen3
, 2),
428 MockRead(ASYNC
, kMsg3
, kLen3
, 3),
429 MockRead(ASYNC
, kMsg2
, kLen2
, 4),
430 MockRead(ASYNC
, kMsg3
, kLen3
, 5),
431 MockRead(ASYNC
, kMsg1
, kLen1
, 6),
434 Initialize(reads
, arraysize(reads
), nullptr, 0);
436 AssertAsyncReadEquals(kMsg1
, kLen1
);
437 AssertAsyncReadEquals(kMsg2
, kLen2
);
438 AssertAsyncReadEquals(kMsg3
, kLen3
);
439 AssertAsyncReadEquals(kMsg3
, kLen3
);
440 AssertAsyncReadEquals(kMsg2
, kLen2
);
441 AssertAsyncReadEquals(kMsg3
, kLen3
);
442 AssertAsyncReadEquals(kMsg1
, kLen1
);
445 TEST_F(SequencedSocketDataTest
, MixedReads
) {
447 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
448 MockRead(ASYNC
, kMsg2
, kLen2
, 1),
449 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
450 MockRead(ASYNC
, kMsg3
, kLen3
, 3),
451 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
452 MockRead(ASYNC
, kMsg3
, kLen3
, 5),
453 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
456 Initialize(reads
, arraysize(reads
), nullptr, 0);
458 AssertSyncReadEquals(kMsg1
, kLen1
);
459 AssertAsyncReadEquals(kMsg2
, kLen2
);
460 AssertSyncReadEquals(kMsg3
, kLen3
);
461 AssertAsyncReadEquals(kMsg3
, kLen3
);
462 AssertSyncReadEquals(kMsg2
, kLen2
);
463 AssertAsyncReadEquals(kMsg3
, kLen3
);
464 AssertSyncReadEquals(kMsg1
, kLen1
);
467 TEST_F(SequencedSocketDataTest
, SyncReadFromCompletionCallback
) {
469 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
472 Initialize(reads
, arraysize(reads
), nullptr, 0);
474 read_buf_
= new IOBuffer(kLen1
);
478 read_buf_
.get(), kLen1
,
479 base::Bind(&SequencedSocketDataTest::ReentrantReadCallback
,
480 base::Unretained(this), kMsg1
, kLen1
, kLen2
, kLen2
)));
482 base::MessageLoop::current()->RunUntilIdle();
483 AssertReadBufferEquals(kMsg2
, kLen2
);
486 TEST_F(SequencedSocketDataTest
, ManyReentrantReads
) {
488 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
489 MockRead(ASYNC
, kMsg2
, kLen2
, 1),
490 MockRead(ASYNC
, kMsg3
, kLen3
, 2),
491 MockRead(ASYNC
, kMsg4
, kLen4
, 3),
494 Initialize(reads
, arraysize(reads
), nullptr, 0);
496 read_buf_
= new IOBuffer(kLen4
);
498 ReentrantHelper
helper3(sock_
);
499 helper3
.SetExpectedRead(kMsg3
, kLen3
);
500 helper3
.SetInvokeRead(read_buf_
, kLen4
, ERR_IO_PENDING
,
501 read_callback_
.callback());
503 ReentrantHelper
helper2(sock_
);
504 helper2
.SetExpectedRead(kMsg2
, kLen2
);
505 helper2
.SetInvokeRead(helper3
.read_buf(), kLen3
, ERR_IO_PENDING
,
508 ReentrantHelper
helper(sock_
);
509 helper
.SetExpectedRead(kMsg1
, kLen1
);
510 helper
.SetInvokeRead(helper2
.read_buf(), kLen2
, ERR_IO_PENDING
,
513 sock_
->Read(helper
.read_buf().get(), kLen1
, helper
.callback());
515 ASSERT_EQ(kLen4
, read_callback_
.WaitForResult());
516 AssertReadBufferEquals(kMsg4
, kLen4
);
519 TEST_F(SequencedSocketDataTest
, AsyncReadFromCompletionCallback
) {
521 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg2
, kLen2
, 1),
524 Initialize(reads
, arraysize(reads
), nullptr, 0);
526 read_buf_
= new IOBuffer(kLen1
);
529 sock_
->Read(read_buf_
.get(), kLen1
,
530 base::Bind(&SequencedSocketDataTest::ReentrantReadCallback
,
531 base::Unretained(this), kMsg1
, kLen1
, kLen2
,
534 ASSERT_FALSE(read_callback_
.have_result());
535 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
536 AssertReadBufferEquals(kMsg2
, kLen2
);
539 TEST_F(SequencedSocketDataTest
, SingleSyncReadTooEarly
) {
541 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 1),
544 MockWrite writes
[] = {MockWrite(SYNCHRONOUS
, 0, 0)};
546 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
548 EXPECT_NONFATAL_FAILURE(AssertReadReturns(kLen1
, ERR_UNEXPECTED
),
549 "Unable to perform synchronous IO while stopped");
550 set_expect_eof(false);
553 TEST_F(SequencedSocketDataTest
, SingleSyncReadSmallBuffer
) {
555 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
558 Initialize(reads
, arraysize(reads
), nullptr, 0);
560 // Read the first chunk.
561 AssertReadReturns(kLen1
- 1, kLen1
- 1);
562 AssertReadBufferEquals(kMsg1
, kLen1
- 1);
563 // Then read the second chunk.
564 AssertReadReturns(1, 1);
565 AssertReadBufferEquals(kMsg1
+ kLen1
- 1, 1);
568 TEST_F(SequencedSocketDataTest
, SingleSyncReadLargeBuffer
) {
570 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
573 Initialize(reads
, arraysize(reads
), nullptr, 0);
574 scoped_refptr
<IOBuffer
> read_buf(new IOBuffer(2 * kLen1
));
575 ASSERT_EQ(kLen1
, sock_
->Read(read_buf
.get(), 2 * kLen1
, failing_callback_
));
576 ASSERT_EQ(std::string(kMsg1
, kLen1
), std::string(read_buf
->data(), kLen1
));
579 TEST_F(SequencedSocketDataTest
, SingleAsyncReadLargeBuffer
) {
581 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
584 Initialize(reads
, arraysize(reads
), nullptr, 0);
586 scoped_refptr
<IOBuffer
> read_buf(new IOBuffer(2 * kLen1
));
587 ASSERT_EQ(ERR_IO_PENDING
,
588 sock_
->Read(read_buf
.get(), 2 * kLen1
, read_callback_
.callback()));
589 ASSERT_EQ(kLen1
, read_callback_
.WaitForResult());
590 ASSERT_EQ(std::string(kMsg1
, kLen1
), std::string(read_buf
->data(), kLen1
));
593 TEST_F(SequencedSocketDataTest
, HangingRead
) {
595 MockRead(ASYNC
, ERR_IO_PENDING
, 0),
598 Initialize(reads
, arraysize(reads
), nullptr, 0);
600 scoped_refptr
<IOBuffer
> read_buf(new IOBuffer(1));
601 ASSERT_EQ(ERR_IO_PENDING
,
602 sock_
->Read(read_buf
.get(), 1, read_callback_
.callback()));
603 ASSERT_FALSE(read_callback_
.have_result());
605 // Even though the read is scheduled to complete at sequence number 0,
606 // verify that the read callback in never called.
607 base::MessageLoop::current()->RunUntilIdle();
608 ASSERT_FALSE(read_callback_
.have_result());
611 TEST_F(SequencedSocketDataTest
, CompleteRead
) {
613 MockRead(ASYNC
, ERR_IO_PENDING
, 0), MockRead(ASYNC
, kMsg1
, kLen1
, 1),
616 Initialize(reads
, arraysize(reads
), nullptr, 0);
618 AssertReadReturns(kLen1
, ERR_IO_PENDING
);
619 ASSERT_FALSE(read_callback_
.have_result());
621 // Even though the read is scheduled to complete at sequence number 0,
622 // verify that the read callback in not called, until CompleteRead() is.
623 base::MessageLoop::current()->RunUntilIdle();
624 ASSERT_FALSE(read_callback_
.have_result());
628 ASSERT_TRUE(read_callback_
.have_result());
629 ASSERT_EQ(kLen1
, read_callback_
.WaitForResult());
630 AssertReadBufferEquals(kMsg1
, kLen1
);
635 TEST_F(SequencedSocketDataTest
, SingleSyncWriteTooEarly
) {
636 MockWrite writes
[] = {
637 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 1),
640 MockRead reads
[] = {MockRead(SYNCHRONOUS
, 0, 0)};
642 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
644 EXPECT_NONFATAL_FAILURE(AssertWriteReturns(kMsg1
, kLen1
, ERR_UNEXPECTED
),
645 "Unable to perform synchronous IO while stopped");
647 set_expect_eof(false);
650 TEST_F(SequencedSocketDataTest
, DISABLED_SingleSyncWriteTooSmall
) {
651 MockWrite writes
[] = {
652 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
655 Initialize(nullptr, 0, writes
, arraysize(writes
));
657 // Attempt to write all of the message, but only some will be written.
658 EXPECT_NONFATAL_FAILURE(AssertSyncWriteEquals(kMsg1
, kLen1
- 1), "");
661 TEST_F(SequencedSocketDataTest
, SingleSyncPartialWrite
) {
662 MockWrite writes
[] = {
663 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
- 1, 0),
664 MockWrite(SYNCHRONOUS
, kMsg1
+ kLen1
- 1, 1, 1),
667 Initialize(nullptr, 0, writes
, arraysize(writes
));
669 // Attempt to write all of the message, but only some will be written.
670 AssertSyncWriteEquals(kMsg1
, kLen1
- 1);
671 // Write the rest of the message.
672 AssertSyncWriteEquals(kMsg1
+ kLen1
- 1, 1);
675 TEST_F(SequencedSocketDataTest
, SingleSyncWrite
) {
676 MockWrite writes
[] = {
677 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
680 Initialize(nullptr, 0, writes
, arraysize(writes
));
682 AssertSyncWriteEquals(kMsg1
, kLen1
);
685 TEST_F(SequencedSocketDataTest
, MultipleSyncWrites
) {
686 MockWrite writes
[] = {
687 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
688 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
689 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
690 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 3),
691 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
692 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 5),
693 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
696 Initialize(nullptr, 0, writes
, arraysize(writes
));
698 AssertSyncWriteEquals(kMsg1
, kLen1
);
699 AssertSyncWriteEquals(kMsg2
, kLen2
);
700 AssertSyncWriteEquals(kMsg3
, kLen3
);
701 AssertSyncWriteEquals(kMsg3
, kLen3
);
702 AssertSyncWriteEquals(kMsg2
, kLen2
);
703 AssertSyncWriteEquals(kMsg3
, kLen3
);
704 AssertSyncWriteEquals(kMsg1
, kLen1
);
707 TEST_F(SequencedSocketDataTest
, SingleAsyncWrite
) {
708 MockWrite writes
[] = {
709 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
712 Initialize(nullptr, 0, writes
, arraysize(writes
));
714 AssertAsyncWriteEquals(kMsg1
, kLen1
);
717 TEST_F(SequencedSocketDataTest
, MultipleAsyncWrites
) {
718 MockWrite writes
[] = {
719 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
720 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
721 MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
722 MockWrite(ASYNC
, kMsg3
, kLen3
, 3),
723 MockWrite(ASYNC
, kMsg2
, kLen2
, 4),
724 MockWrite(ASYNC
, kMsg3
, kLen3
, 5),
725 MockWrite(ASYNC
, kMsg1
, kLen1
, 6),
728 Initialize(nullptr, 0, writes
, arraysize(writes
));
730 AssertAsyncWriteEquals(kMsg1
, kLen1
);
731 AssertAsyncWriteEquals(kMsg2
, kLen2
);
732 AssertAsyncWriteEquals(kMsg3
, kLen3
);
733 AssertAsyncWriteEquals(kMsg3
, kLen3
);
734 AssertAsyncWriteEquals(kMsg2
, kLen2
);
735 AssertAsyncWriteEquals(kMsg3
, kLen3
);
736 AssertAsyncWriteEquals(kMsg1
, kLen1
);
739 TEST_F(SequencedSocketDataTest
, MixedWrites
) {
740 MockWrite writes
[] = {
741 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
742 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
743 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
744 MockWrite(ASYNC
, kMsg3
, kLen3
, 3),
745 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
746 MockWrite(ASYNC
, kMsg3
, kLen3
, 5),
747 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
750 Initialize(nullptr, 0, writes
, arraysize(writes
));
752 AssertSyncWriteEquals(kMsg1
, kLen1
);
753 AssertAsyncWriteEquals(kMsg2
, kLen2
);
754 AssertSyncWriteEquals(kMsg3
, kLen3
);
755 AssertAsyncWriteEquals(kMsg3
, kLen3
);
756 AssertSyncWriteEquals(kMsg2
, kLen2
);
757 AssertAsyncWriteEquals(kMsg3
, kLen3
);
758 AssertSyncWriteEquals(kMsg1
, kLen1
);
761 TEST_F(SequencedSocketDataTest
, SyncWriteFromCompletionCallback
) {
762 MockWrite writes
[] = {
763 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
764 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
767 Initialize(nullptr, 0, writes
, arraysize(writes
));
769 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
770 memcpy(write_buf
->data(), kMsg1
, kLen1
);
774 write_buf
.get(), kLen1
,
775 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback
,
776 base::Unretained(this), kLen1
, kMsg2
, kLen2
, kLen2
)));
778 base::MessageLoop::current()->RunUntilIdle();
781 TEST_F(SequencedSocketDataTest
, AsyncWriteFromCompletionCallback
) {
782 MockWrite writes
[] = {
783 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
786 Initialize(nullptr, 0, writes
, arraysize(writes
));
788 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
789 memcpy(write_buf
->data(), kMsg1
, kLen1
);
792 sock_
->Write(write_buf
.get(), kLen1
,
793 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback
,
794 base::Unretained(this), kLen1
, kMsg2
, kLen2
,
797 ASSERT_FALSE(write_callback_
.have_result());
798 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
801 TEST_F(SequencedSocketDataTest
, ManyReentrantWrites
) {
802 MockWrite writes
[] = {
803 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
804 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
805 MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
806 MockWrite(ASYNC
, kMsg4
, kLen4
, 3),
809 Initialize(nullptr, 0, writes
, arraysize(writes
));
811 ReentrantHelper
helper3(sock_
);
812 helper3
.SetExpectedWrite(kLen3
);
813 helper3
.SetInvokeWrite(kMsg4
, kLen4
, ERR_IO_PENDING
,
814 write_callback_
.callback());
816 ReentrantHelper
helper2(sock_
);
817 helper2
.SetExpectedWrite(kLen2
);
818 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
820 ReentrantHelper
helper(sock_
);
821 helper
.SetExpectedWrite(kLen1
);
822 helper
.SetInvokeWrite(kMsg2
, kLen2
, ERR_IO_PENDING
, helper2
.callback());
824 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
825 memcpy(write_buf
->data(), kMsg1
, kLen1
);
826 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback());
828 ASSERT_EQ(kLen4
, write_callback_
.WaitForResult());
831 // ----------- Mixed Reads and Writes
833 TEST_F(SequencedSocketDataTest
, MixedSyncOperations
) {
835 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
836 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 3),
839 MockWrite writes
[] = {
840 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
841 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
844 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
846 AssertSyncReadEquals(kMsg1
, kLen1
);
847 AssertSyncWriteEquals(kMsg2
, kLen2
);
848 AssertSyncWriteEquals(kMsg3
, kLen3
);
849 AssertSyncReadEquals(kMsg2
, kLen2
);
852 TEST_F(SequencedSocketDataTest
, MixedAsyncOperations
) {
854 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg2
, kLen2
, 3),
857 MockWrite writes
[] = {
858 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
861 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
863 AssertAsyncReadEquals(kMsg1
, kLen1
);
864 AssertAsyncWriteEquals(kMsg2
, kLen2
);
865 AssertAsyncWriteEquals(kMsg3
, kLen3
);
866 AssertAsyncReadEquals(kMsg2
, kLen2
);
869 TEST_F(SequencedSocketDataTest
, InterleavedAsyncOperations
) {
870 // Order of completion is read, write, write, read.
872 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg2
, kLen2
, 3),
875 MockWrite writes
[] = {
876 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
879 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
881 // Issue the write, which will block until the read completes.
882 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
884 // Issue the read which will return first.
885 AssertReadReturns(kLen1
, ERR_IO_PENDING
);
887 ASSERT_EQ(kLen1
, read_callback_
.WaitForResult());
888 AssertReadBufferEquals(kMsg1
, kLen1
);
890 ASSERT_TRUE(write_callback_
.have_result());
891 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
893 // Issue the read, which will block until the write completes.
894 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
896 // Issue the writes which will return first.
897 AssertWriteReturns(kMsg3
, kLen3
, ERR_IO_PENDING
);
898 ASSERT_EQ(kLen3
, write_callback_
.WaitForResult());
900 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
901 AssertReadBufferEquals(kMsg2
, kLen2
);
904 TEST_F(SequencedSocketDataTest
, InterleavedMixedOperations
) {
905 // Order of completion is read, write, write, read.
907 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
908 MockRead(ASYNC
, kMsg2
, kLen2
, 3),
909 MockRead(ASYNC
, kMsg3
, kLen3
, 5),
912 MockWrite writes
[] = {
913 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
914 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
915 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 4),
918 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
920 // Issue the write, which will block until the read completes.
921 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
923 // Issue the writes which will complete immediately.
924 AssertSyncReadEquals(kMsg1
, kLen1
);
926 ASSERT_FALSE(write_callback_
.have_result());
927 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
929 // Issue the read, which will block until the write completes.
930 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
932 // Issue the writes which will complete immediately.
933 AssertSyncWriteEquals(kMsg3
, kLen3
);
935 ASSERT_FALSE(read_callback_
.have_result());
936 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
937 AssertReadBufferEquals(kMsg2
, kLen2
);
939 // Issue the read, which will block until the write completes.
940 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
942 // Issue the writes which will complete immediately.
943 AssertSyncWriteEquals(kMsg1
, kLen1
);
945 ASSERT_FALSE(read_callback_
.have_result());
946 ASSERT_EQ(kLen3
, read_callback_
.WaitForResult());
947 AssertReadBufferEquals(kMsg3
, kLen3
);
950 TEST_F(SequencedSocketDataTest
, AsyncReadFromWriteCompletionCallback
) {
951 MockWrite writes
[] = {
952 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
956 MockRead(ASYNC
, kMsg2
, kLen2
, 1),
959 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
961 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
962 memcpy(write_buf
->data(), kMsg1
, kLen1
);
963 ASSERT_EQ(ERR_IO_PENDING
,
965 write_buf
.get(), kLen1
,
966 base::Bind(&SequencedSocketDataTest::ReentrantAsyncReadCallback
,
967 base::Unretained(this), kLen1
, kLen2
)));
969 ASSERT_FALSE(read_callback_
.have_result());
970 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
971 AssertReadBufferEquals(kMsg2
, kLen2
);
974 TEST_F(SequencedSocketDataTest
, AsyncWriteFromReadCompletionCallback
) {
975 MockWrite writes
[] = {
976 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
980 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
983 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
985 scoped_refptr
<IOBuffer
> read_buf(new IOBuffer(kLen1
));
989 read_buf
.get(), kLen1
,
990 base::Bind(&SequencedSocketDataTest::ReentrantAsyncWriteCallback
,
991 base::Unretained(this), kMsg2
, kLen2
,
992 write_callback_
.callback(), kLen1
)));
994 ASSERT_FALSE(write_callback_
.have_result());
995 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
998 TEST_F(SequencedSocketDataTest
, MixedReentrantOperations
) {
999 MockWrite writes
[] = {
1000 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
1003 MockRead reads
[] = {
1004 MockRead(ASYNC
, kMsg2
, kLen2
, 1), MockRead(ASYNC
, kMsg4
, kLen4
, 3),
1007 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1009 read_buf_
= new IOBuffer(kLen4
);
1011 ReentrantHelper
helper3(sock_
);
1012 helper3
.SetExpectedWrite(kLen3
);
1013 helper3
.SetInvokeRead(read_buf_
, kLen4
, ERR_IO_PENDING
,
1014 read_callback_
.callback());
1016 ReentrantHelper
helper2(sock_
);
1017 helper2
.SetExpectedRead(kMsg2
, kLen2
);
1018 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
1020 ReentrantHelper
helper(sock_
);
1021 helper
.SetExpectedWrite(kLen1
);
1022 helper
.SetInvokeRead(helper2
.read_buf(), kLen2
, ERR_IO_PENDING
,
1023 helper2
.callback());
1025 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
1026 memcpy(write_buf
->data(), kMsg1
, kLen1
);
1027 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback());
1029 ASSERT_EQ(kLen4
, read_callback_
.WaitForResult());
1032 TEST_F(SequencedSocketDataTest
, MixedReentrantOperationsThenSynchronousRead
) {
1033 MockWrite writes
[] = {
1034 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
1037 MockRead reads
[] = {
1038 MockRead(ASYNC
, kMsg2
, kLen2
, 1), MockRead(SYNCHRONOUS
, kMsg4
, kLen4
, 3),
1041 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1043 read_buf_
= new IOBuffer(kLen4
);
1045 ReentrantHelper
helper3(sock_
);
1046 helper3
.SetExpectedWrite(kLen3
);
1047 helper3
.SetInvokeRead(read_buf_
, kLen4
, kLen4
, failing_callback_
);
1049 ReentrantHelper
helper2(sock_
);
1050 helper2
.SetExpectedRead(kMsg2
, kLen2
);
1051 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
1053 ReentrantHelper
helper(sock_
);
1054 helper
.SetExpectedWrite(kLen1
);
1055 helper
.SetInvokeRead(helper2
.read_buf(), kLen2
, ERR_IO_PENDING
,
1056 helper2
.callback());
1058 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
1059 memcpy(write_buf
->data(), kMsg1
, kLen1
);
1060 ASSERT_EQ(ERR_IO_PENDING
,
1061 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback()));
1063 base::MessageLoop::current()->RunUntilIdle();
1064 AssertReadBufferEquals(kMsg4
, kLen4
);
1067 TEST_F(SequencedSocketDataTest
, MixedReentrantOperationsThenSynchronousWrite
) {
1068 MockWrite writes
[] = {
1069 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
1070 MockWrite(SYNCHRONOUS
, kMsg4
, kLen4
, 3),
1073 MockRead reads
[] = {
1074 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg3
, kLen3
, 2),
1077 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1079 read_buf_
= new IOBuffer(kLen4
);
1081 ReentrantHelper
helper3(sock_
);
1082 helper3
.SetExpectedRead(kMsg3
, kLen3
);
1083 helper3
.SetInvokeWrite(kMsg4
, kLen4
, kLen4
, failing_callback_
);
1085 ReentrantHelper
helper2(sock_
);
1086 helper2
.SetExpectedWrite(kLen2
);
1087 helper2
.SetInvokeRead(helper3
.read_buf(), kLen3
, ERR_IO_PENDING
,
1088 helper3
.callback());
1090 ReentrantHelper
helper(sock_
);
1091 helper
.SetExpectedRead(kMsg1
, kLen1
);
1092 helper
.SetInvokeWrite(kMsg2
, kLen2
, ERR_IO_PENDING
, helper2
.callback());
1094 ASSERT_EQ(ERR_IO_PENDING
,
1095 sock_
->Read(helper
.read_buf().get(), kLen1
, helper
.callback()));
1097 base::MessageLoop::current()->RunUntilIdle();