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
, SingleSyncWriteTooSmall
) {
651 MockWrite writes
[] = {
652 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
655 Initialize(nullptr, 0, writes
, arraysize(writes
));
657 // Expecting too small of a write triggers multiple expectation failures.
659 // The gtest infrastructure does not have a macro similar to
660 // EXPECT_NONFATAL_FAILURE which works when there is more than one
663 // However, tests can gather the TestPartResultArray and directly
664 // validate the test failures. That's what the rest of this test does.
666 ::testing::TestPartResultArray gtest_failures
;
669 ::testing::ScopedFakeTestPartResultReporter
gtest_reporter(
670 ::testing::ScopedFakeTestPartResultReporter::
671 INTERCEPT_ONLY_CURRENT_THREAD
,
673 AssertSyncWriteEquals(kMsg1
, kLen1
- 1);
676 static const char* kExpectedFailures
[] = {
677 "Expected: (data.length()) >= (expected_data.length())",
678 "Value of: actual_data",
679 "Value of: sock_->Write(buf.get(), len, failing_callback_)"};
680 ASSERT_EQ(arraysize(kExpectedFailures
),
681 static_cast<size_t>(gtest_failures
.size()));
683 for (int i
= 0; i
< gtest_failures
.size(); ++i
) {
684 const ::testing::TestPartResult
& result
=
685 gtest_failures
.GetTestPartResult(i
);
686 EXPECT_TRUE(strstr(result
.message(), kExpectedFailures
[i
]) != NULL
);
689 set_expect_eof(false);
692 TEST_F(SequencedSocketDataTest
, SingleSyncPartialWrite
) {
693 MockWrite writes
[] = {
694 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
- 1, 0),
695 MockWrite(SYNCHRONOUS
, kMsg1
+ kLen1
- 1, 1, 1),
698 Initialize(nullptr, 0, writes
, arraysize(writes
));
700 // Attempt to write all of the message, but only some will be written.
701 AssertSyncWriteEquals(kMsg1
, kLen1
- 1);
702 // Write the rest of the message.
703 AssertSyncWriteEquals(kMsg1
+ kLen1
- 1, 1);
706 TEST_F(SequencedSocketDataTest
, SingleSyncWrite
) {
707 MockWrite writes
[] = {
708 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
711 Initialize(nullptr, 0, writes
, arraysize(writes
));
713 AssertSyncWriteEquals(kMsg1
, kLen1
);
716 TEST_F(SequencedSocketDataTest
, MultipleSyncWrites
) {
717 MockWrite writes
[] = {
718 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
719 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
720 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
721 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 3),
722 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
723 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 5),
724 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
727 Initialize(nullptr, 0, writes
, arraysize(writes
));
729 AssertSyncWriteEquals(kMsg1
, kLen1
);
730 AssertSyncWriteEquals(kMsg2
, kLen2
);
731 AssertSyncWriteEquals(kMsg3
, kLen3
);
732 AssertSyncWriteEquals(kMsg3
, kLen3
);
733 AssertSyncWriteEquals(kMsg2
, kLen2
);
734 AssertSyncWriteEquals(kMsg3
, kLen3
);
735 AssertSyncWriteEquals(kMsg1
, kLen1
);
738 TEST_F(SequencedSocketDataTest
, SingleAsyncWrite
) {
739 MockWrite writes
[] = {
740 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
743 Initialize(nullptr, 0, writes
, arraysize(writes
));
745 AssertAsyncWriteEquals(kMsg1
, kLen1
);
748 TEST_F(SequencedSocketDataTest
, MultipleAsyncWrites
) {
749 MockWrite writes
[] = {
750 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
751 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
752 MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
753 MockWrite(ASYNC
, kMsg3
, kLen3
, 3),
754 MockWrite(ASYNC
, kMsg2
, kLen2
, 4),
755 MockWrite(ASYNC
, kMsg3
, kLen3
, 5),
756 MockWrite(ASYNC
, kMsg1
, kLen1
, 6),
759 Initialize(nullptr, 0, writes
, arraysize(writes
));
761 AssertAsyncWriteEquals(kMsg1
, kLen1
);
762 AssertAsyncWriteEquals(kMsg2
, kLen2
);
763 AssertAsyncWriteEquals(kMsg3
, kLen3
);
764 AssertAsyncWriteEquals(kMsg3
, kLen3
);
765 AssertAsyncWriteEquals(kMsg2
, kLen2
);
766 AssertAsyncWriteEquals(kMsg3
, kLen3
);
767 AssertAsyncWriteEquals(kMsg1
, kLen1
);
770 TEST_F(SequencedSocketDataTest
, MixedWrites
) {
771 MockWrite writes
[] = {
772 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
773 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
774 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
775 MockWrite(ASYNC
, kMsg3
, kLen3
, 3),
776 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4),
777 MockWrite(ASYNC
, kMsg3
, kLen3
, 5),
778 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6),
781 Initialize(nullptr, 0, writes
, arraysize(writes
));
783 AssertSyncWriteEquals(kMsg1
, kLen1
);
784 AssertAsyncWriteEquals(kMsg2
, kLen2
);
785 AssertSyncWriteEquals(kMsg3
, kLen3
);
786 AssertAsyncWriteEquals(kMsg3
, kLen3
);
787 AssertSyncWriteEquals(kMsg2
, kLen2
);
788 AssertAsyncWriteEquals(kMsg3
, kLen3
);
789 AssertSyncWriteEquals(kMsg1
, kLen1
);
792 TEST_F(SequencedSocketDataTest
, SyncWriteFromCompletionCallback
) {
793 MockWrite writes
[] = {
794 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
795 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
798 Initialize(nullptr, 0, writes
, arraysize(writes
));
800 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
801 memcpy(write_buf
->data(), kMsg1
, kLen1
);
805 write_buf
.get(), kLen1
,
806 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback
,
807 base::Unretained(this), kLen1
, kMsg2
, kLen2
, kLen2
)));
809 base::MessageLoop::current()->RunUntilIdle();
812 TEST_F(SequencedSocketDataTest
, AsyncWriteFromCompletionCallback
) {
813 MockWrite writes
[] = {
814 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
817 Initialize(nullptr, 0, writes
, arraysize(writes
));
819 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
820 memcpy(write_buf
->data(), kMsg1
, kLen1
);
823 sock_
->Write(write_buf
.get(), kLen1
,
824 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback
,
825 base::Unretained(this), kLen1
, kMsg2
, kLen2
,
828 ASSERT_FALSE(write_callback_
.have_result());
829 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
832 TEST_F(SequencedSocketDataTest
, ManyReentrantWrites
) {
833 MockWrite writes
[] = {
834 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
835 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
836 MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
837 MockWrite(ASYNC
, kMsg4
, kLen4
, 3),
840 Initialize(nullptr, 0, writes
, arraysize(writes
));
842 ReentrantHelper
helper3(sock_
);
843 helper3
.SetExpectedWrite(kLen3
);
844 helper3
.SetInvokeWrite(kMsg4
, kLen4
, ERR_IO_PENDING
,
845 write_callback_
.callback());
847 ReentrantHelper
helper2(sock_
);
848 helper2
.SetExpectedWrite(kLen2
);
849 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
851 ReentrantHelper
helper(sock_
);
852 helper
.SetExpectedWrite(kLen1
);
853 helper
.SetInvokeWrite(kMsg2
, kLen2
, ERR_IO_PENDING
, helper2
.callback());
855 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
856 memcpy(write_buf
->data(), kMsg1
, kLen1
);
857 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback());
859 ASSERT_EQ(kLen4
, write_callback_
.WaitForResult());
862 // ----------- Mixed Reads and Writes
864 TEST_F(SequencedSocketDataTest
, MixedSyncOperations
) {
866 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
867 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 3),
870 MockWrite writes
[] = {
871 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1),
872 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
875 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
877 AssertSyncReadEquals(kMsg1
, kLen1
);
878 AssertSyncWriteEquals(kMsg2
, kLen2
);
879 AssertSyncWriteEquals(kMsg3
, kLen3
);
880 AssertSyncReadEquals(kMsg2
, kLen2
);
883 TEST_F(SequencedSocketDataTest
, MixedAsyncOperations
) {
885 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg2
, kLen2
, 3),
888 MockWrite writes
[] = {
889 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
892 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
894 AssertAsyncReadEquals(kMsg1
, kLen1
);
895 AssertAsyncWriteEquals(kMsg2
, kLen2
);
896 AssertAsyncWriteEquals(kMsg3
, kLen3
);
897 AssertAsyncReadEquals(kMsg2
, kLen2
);
900 TEST_F(SequencedSocketDataTest
, InterleavedAsyncOperations
) {
901 // Order of completion is read, write, write, read.
903 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg2
, kLen2
, 3),
906 MockWrite writes
[] = {
907 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
910 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
912 // Issue the write, which will block until the read completes.
913 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
915 // Issue the read which will return first.
916 AssertReadReturns(kLen1
, ERR_IO_PENDING
);
918 ASSERT_EQ(kLen1
, read_callback_
.WaitForResult());
919 AssertReadBufferEquals(kMsg1
, kLen1
);
921 ASSERT_TRUE(write_callback_
.have_result());
922 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
924 // Issue the read, which will block until the write completes.
925 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
927 // Issue the writes which will return first.
928 AssertWriteReturns(kMsg3
, kLen3
, ERR_IO_PENDING
);
929 ASSERT_EQ(kLen3
, write_callback_
.WaitForResult());
931 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
932 AssertReadBufferEquals(kMsg2
, kLen2
);
935 TEST_F(SequencedSocketDataTest
, InterleavedMixedOperations
) {
936 // Order of completion is read, write, write, read.
938 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0),
939 MockRead(ASYNC
, kMsg2
, kLen2
, 3),
940 MockRead(ASYNC
, kMsg3
, kLen3
, 5),
943 MockWrite writes
[] = {
944 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
945 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2),
946 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 4),
949 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
951 // Issue the write, which will block until the read completes.
952 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
954 // Issue the writes which will complete immediately.
955 AssertSyncReadEquals(kMsg1
, kLen1
);
957 ASSERT_FALSE(write_callback_
.have_result());
958 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
960 // Issue the read, which will block until the write completes.
961 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
963 // Issue the writes which will complete immediately.
964 AssertSyncWriteEquals(kMsg3
, kLen3
);
966 ASSERT_FALSE(read_callback_
.have_result());
967 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
968 AssertReadBufferEquals(kMsg2
, kLen2
);
970 // Issue the read, which will block until the write completes.
971 AssertReadReturns(kLen2
, ERR_IO_PENDING
);
973 // Issue the writes which will complete immediately.
974 AssertSyncWriteEquals(kMsg1
, kLen1
);
976 ASSERT_FALSE(read_callback_
.have_result());
977 ASSERT_EQ(kLen3
, read_callback_
.WaitForResult());
978 AssertReadBufferEquals(kMsg3
, kLen3
);
981 TEST_F(SequencedSocketDataTest
, AsyncReadFromWriteCompletionCallback
) {
982 MockWrite writes
[] = {
983 MockWrite(ASYNC
, kMsg1
, kLen1
, 0),
987 MockRead(ASYNC
, kMsg2
, kLen2
, 1),
990 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
992 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
993 memcpy(write_buf
->data(), kMsg1
, kLen1
);
994 ASSERT_EQ(ERR_IO_PENDING
,
996 write_buf
.get(), kLen1
,
997 base::Bind(&SequencedSocketDataTest::ReentrantAsyncReadCallback
,
998 base::Unretained(this), kLen1
, kLen2
)));
1000 ASSERT_FALSE(read_callback_
.have_result());
1001 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
1002 AssertReadBufferEquals(kMsg2
, kLen2
);
1005 TEST_F(SequencedSocketDataTest
, AsyncWriteFromReadCompletionCallback
) {
1006 MockWrite writes
[] = {
1007 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
1010 MockRead reads
[] = {
1011 MockRead(ASYNC
, kMsg1
, kLen1
, 0),
1014 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1016 scoped_refptr
<IOBuffer
> read_buf(new IOBuffer(kLen1
));
1020 read_buf
.get(), kLen1
,
1021 base::Bind(&SequencedSocketDataTest::ReentrantAsyncWriteCallback
,
1022 base::Unretained(this), kMsg2
, kLen2
,
1023 write_callback_
.callback(), kLen1
)));
1025 ASSERT_FALSE(write_callback_
.have_result());
1026 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
1029 TEST_F(SequencedSocketDataTest
, MixedReentrantOperations
) {
1030 MockWrite writes
[] = {
1031 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
1034 MockRead reads
[] = {
1035 MockRead(ASYNC
, kMsg2
, kLen2
, 1), MockRead(ASYNC
, kMsg4
, kLen4
, 3),
1038 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1040 read_buf_
= new IOBuffer(kLen4
);
1042 ReentrantHelper
helper3(sock_
);
1043 helper3
.SetExpectedWrite(kLen3
);
1044 helper3
.SetInvokeRead(read_buf_
, kLen4
, ERR_IO_PENDING
,
1045 read_callback_
.callback());
1047 ReentrantHelper
helper2(sock_
);
1048 helper2
.SetExpectedRead(kMsg2
, kLen2
);
1049 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
1051 ReentrantHelper
helper(sock_
);
1052 helper
.SetExpectedWrite(kLen1
);
1053 helper
.SetInvokeRead(helper2
.read_buf(), kLen2
, ERR_IO_PENDING
,
1054 helper2
.callback());
1056 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
1057 memcpy(write_buf
->data(), kMsg1
, kLen1
);
1058 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback());
1060 ASSERT_EQ(kLen4
, read_callback_
.WaitForResult());
1063 TEST_F(SequencedSocketDataTest
, MixedReentrantOperationsThenSynchronousRead
) {
1064 MockWrite writes
[] = {
1065 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), MockWrite(ASYNC
, kMsg3
, kLen3
, 2),
1068 MockRead reads
[] = {
1069 MockRead(ASYNC
, kMsg2
, kLen2
, 1), MockRead(SYNCHRONOUS
, kMsg4
, kLen4
, 3),
1072 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1074 read_buf_
= new IOBuffer(kLen4
);
1076 ReentrantHelper
helper3(sock_
);
1077 helper3
.SetExpectedWrite(kLen3
);
1078 helper3
.SetInvokeRead(read_buf_
, kLen4
, kLen4
, failing_callback_
);
1080 ReentrantHelper
helper2(sock_
);
1081 helper2
.SetExpectedRead(kMsg2
, kLen2
);
1082 helper2
.SetInvokeWrite(kMsg3
, kLen3
, ERR_IO_PENDING
, helper3
.callback());
1084 ReentrantHelper
helper(sock_
);
1085 helper
.SetExpectedWrite(kLen1
);
1086 helper
.SetInvokeRead(helper2
.read_buf(), kLen2
, ERR_IO_PENDING
,
1087 helper2
.callback());
1089 scoped_refptr
<IOBuffer
> write_buf(new IOBuffer(kLen1
));
1090 memcpy(write_buf
->data(), kMsg1
, kLen1
);
1091 ASSERT_EQ(ERR_IO_PENDING
,
1092 sock_
->Write(write_buf
.get(), kLen1
, helper
.callback()));
1094 base::MessageLoop::current()->RunUntilIdle();
1095 AssertReadBufferEquals(kMsg4
, kLen4
);
1098 TEST_F(SequencedSocketDataTest
, MixedReentrantOperationsThenSynchronousWrite
) {
1099 MockWrite writes
[] = {
1100 MockWrite(ASYNC
, kMsg2
, kLen2
, 1),
1101 MockWrite(SYNCHRONOUS
, kMsg4
, kLen4
, 3),
1104 MockRead reads
[] = {
1105 MockRead(ASYNC
, kMsg1
, kLen1
, 0), MockRead(ASYNC
, kMsg3
, kLen3
, 2),
1108 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
1110 read_buf_
= new IOBuffer(kLen4
);
1112 ReentrantHelper
helper3(sock_
);
1113 helper3
.SetExpectedRead(kMsg3
, kLen3
);
1114 helper3
.SetInvokeWrite(kMsg4
, kLen4
, kLen4
, failing_callback_
);
1116 ReentrantHelper
helper2(sock_
);
1117 helper2
.SetExpectedWrite(kLen2
);
1118 helper2
.SetInvokeRead(helper3
.read_buf(), kLen3
, ERR_IO_PENDING
,
1119 helper3
.callback());
1121 ReentrantHelper
helper(sock_
);
1122 helper
.SetExpectedRead(kMsg1
, kLen1
);
1123 helper
.SetInvokeWrite(kMsg2
, kLen2
, ERR_IO_PENDING
, helper2
.callback());
1125 ASSERT_EQ(ERR_IO_PENDING
,
1126 sock_
->Read(helper
.read_buf().get(), kLen1
, helper
.callback()));
1128 base::MessageLoop::current()->RunUntilIdle();