Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / socket / sequenced_socket_data_unittest.cc
blobe234b993377dbd599c325666c6adc2c8200aa27a
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.
5 #include <string>
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 //-----------------------------------------------------------------------------
22 namespace net {
24 namespace {
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 {
41 public:
42 ReentrantHelper(StreamSocket* socket)
43 : socket_(socket),
44 verify_read_(false),
45 first_read_data_(nullptr),
46 first_len_(-1),
47 second_read_(false),
48 second_write_data_(nullptr),
49 second_len_(-1) {}
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) {
54 verify_read_ = true;
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) {
62 verify_read_ = false;
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,
69 int read_len,
70 int second_rv,
71 CompletionCallback callback) {
72 second_read_ = true;
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,
82 int write_len,
83 int second_rv,
84 CompletionCallback callback) {
85 second_read_ = false;
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_; }
101 private:
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);
106 if (verify_read_) {
107 ASSERT_EQ(std::string(first_read_data_, first_len_),
108 std::string(first_read_buf_->data(), rv));
111 if (second_read_) {
112 ASSERT_EQ(second_rv_, socket_->Read(second_read_buf_.get(), second_len_,
113 second_callback_));
114 } else {
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_;
124 bool verify_read_;
125 scoped_refptr<IOBuffer> first_read_buf_;
126 const char* first_read_data_;
127 int first_len_;
129 CompletionCallback second_callback_;
130 bool second_read_;
131 int second_rv_;
132 scoped_refptr<IOBuffer> second_read_buf_;
133 const char* second_write_data_;
134 int second_len_;
136 DISALLOW_COPY_AND_ASSIGN(ReentrantHelper);
139 class SequencedSocketDataTest : public testing::Test {
140 public:
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
147 // value.
148 void ReentrantReadCallback(const char* data,
149 int len1,
150 int len2,
151 int expected_rv2,
152 int rv);
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,
162 // then
163 // attempts to write to the socket and verifies that that it returns the
164 // correct value.
165 void ReentrantWriteCallback(int expected_rv1,
166 const char* data,
167 int len,
168 int expected_rv2,
169 int rv);
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,
175 int len,
176 CompletionCallback callback,
177 int expected_rv,
178 int rv);
180 // Callback which adds a failure if it's ever called.
181 void FailingCompletionCallback(int rv);
183 protected:
184 void Initialize(MockRead* reads,
185 size_t reads_count,
186 MockWrite* writes,
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);
198 // When a given test completes, data_.at_eof() is expected to
199 // match the value specified here. Most test should consume all
200 // reads and writes, but some tests verify error handling behavior
201 // do not consume all data.
202 void set_expect_eof(bool expect_eof) { expect_eof_ = expect_eof; }
204 TestCompletionCallback read_callback_;
205 scoped_refptr<IOBuffer> read_buf_;
206 TestCompletionCallback write_callback_;
207 CompletionCallback failing_callback_;
208 StreamSocket* sock_;
210 private:
211 MockConnect connect_data_;
212 scoped_ptr<SequencedSocketData> data_;
214 const HostPortPair endpoint_;
215 scoped_refptr<TransportSocketParams> tcp_params_;
216 MockClientSocketFactory socket_factory_;
217 MockTransportClientSocketPool socket_pool_;
218 ClientSocketHandle connection_;
219 bool expect_eof_;
221 DISALLOW_COPY_AND_ASSIGN(SequencedSocketDataTest);
224 SequencedSocketDataTest::SequencedSocketDataTest()
225 : failing_callback_(
226 base::Bind(&SequencedSocketDataTest::FailingCompletionCallback,
227 base::Unretained(this))),
228 sock_(nullptr),
229 connect_data_(SYNCHRONOUS, OK),
230 endpoint_("www.google.com", 443),
231 tcp_params_(new TransportSocketParams(
232 endpoint_,
233 false,
234 false,
235 OnHostResolutionCallback(),
236 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
237 socket_pool_(10, 10, &socket_factory_),
238 expect_eof_(true) {
241 SequencedSocketDataTest::~SequencedSocketDataTest() {
242 // Make sure no unexpected pending tasks will cause a failure.
243 base::RunLoop().RunUntilIdle();
244 if (expect_eof_) {
245 EXPECT_EQ(expect_eof_, data_->at_read_eof());
246 EXPECT_EQ(expect_eof_, data_->at_write_eof());
250 void SequencedSocketDataTest::Initialize(MockRead* reads,
251 size_t reads_count,
252 MockWrite* writes,
253 size_t writes_count) {
254 data_.reset(
255 new SequencedSocketData(reads, reads_count, writes, writes_count));
256 data_->set_connect_data(connect_data_);
257 socket_factory_.AddSocketDataProvider(data_.get());
259 EXPECT_EQ(OK,
260 connection_.Init(
261 endpoint_.ToString(), tcp_params_, LOWEST, CompletionCallback(),
262 reinterpret_cast<TransportClientSocketPool*>(&socket_pool_),
263 BoundNetLog()));
264 sock_ = connection_.socket();
267 void SequencedSocketDataTest::AssertSyncReadEquals(const char* data, int len) {
268 // Issue the read, which will complete immediately.
269 AssertReadReturns(len, len);
270 AssertReadBufferEquals(data, len);
273 void SequencedSocketDataTest::AssertAsyncReadEquals(const char* data, int len) {
274 // Issue the read, which will be completed asynchronously.
275 AssertReadReturns(len, ERR_IO_PENDING);
277 EXPECT_TRUE(sock_->IsConnected());
279 // Now the read should complete.
280 ASSERT_EQ(len, read_callback_.WaitForResult());
281 AssertReadBufferEquals(data, len);
284 void SequencedSocketDataTest::AssertReadReturns(int len, int rv) {
285 read_buf_ = new IOBuffer(len);
286 if (rv == ERR_IO_PENDING) {
287 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback()));
288 ASSERT_FALSE(read_callback_.have_result());
289 } else {
290 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, failing_callback_));
294 void SequencedSocketDataTest::AssertReadBufferEquals(const char* data,
295 int len) {
296 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len));
299 void SequencedSocketDataTest::AssertSyncWriteEquals(const char* data, int len) {
300 // Issue the write, which should be complete immediately.
301 AssertWriteReturns(data, len, len);
302 ASSERT_FALSE(write_callback_.have_result());
305 void SequencedSocketDataTest::AssertAsyncWriteEquals(const char* data,
306 int len) {
307 // Issue the read, which should be completed asynchronously.
308 AssertWriteReturns(data, len, ERR_IO_PENDING);
310 EXPECT_FALSE(read_callback_.have_result());
311 EXPECT_TRUE(sock_->IsConnected());
313 ASSERT_EQ(len, write_callback_.WaitForResult());
316 void SequencedSocketDataTest::AssertWriteReturns(const char* data,
317 int len,
318 int rv) {
319 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
320 memcpy(buf->data(), data, len);
322 if (rv == ERR_IO_PENDING) {
323 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback()));
324 ASSERT_FALSE(write_callback_.have_result());
325 } else {
326 ASSERT_EQ(rv, sock_->Write(buf.get(), len, failing_callback_));
330 void SequencedSocketDataTest::ReentrantReadCallback(const char* data,
331 int len1,
332 int len2,
333 int expected_rv2,
334 int rv) {
335 ASSERT_EQ(len1, rv);
336 AssertReadBufferEquals(data, len1);
338 AssertReadReturns(len2, expected_rv2);
341 void SequencedSocketDataTest::ReentrantAsyncReadCallback(int expected_rv,
342 int len,
343 int rv) {
344 ASSERT_EQ(expected_rv, rv);
346 AssertReadReturns(len, ERR_IO_PENDING);
349 void SequencedSocketDataTest::ReentrantWriteCallback(int expected_rv1,
350 const char* data,
351 int len,
352 int expected_rv2,
353 int rv) {
354 ASSERT_EQ(expected_rv1, rv);
356 AssertWriteReturns(data, len, expected_rv2);
359 void SequencedSocketDataTest::ReentrantAsyncWriteCallback(
360 const char* data,
361 int len,
362 CompletionCallback callback,
363 int expected_rv,
364 int rv) {
365 EXPECT_EQ(expected_rv, rv);
366 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len));
367 memcpy(write_buf->data(), data, len);
368 EXPECT_EQ(ERR_IO_PENDING, sock_->Write(write_buf.get(), len, callback));
371 void SequencedSocketDataTest::FailingCompletionCallback(int rv) {
372 ADD_FAILURE() << "Callback should not have been invoked";
375 // ----------- Read
377 TEST_F(SequencedSocketDataTest, SingleSyncRead) {
378 MockRead reads[] = {
379 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
382 Initialize(reads, arraysize(reads), nullptr, 0);
383 AssertSyncReadEquals(kMsg1, kLen1);
386 TEST_F(SequencedSocketDataTest, MultipleSyncReads) {
387 MockRead reads[] = {
388 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
389 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1),
390 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2),
391 MockRead(SYNCHRONOUS, kMsg3, kLen3, 3),
392 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4),
393 MockRead(SYNCHRONOUS, kMsg3, kLen3, 5),
394 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6),
397 Initialize(reads, arraysize(reads), nullptr, 0);
399 AssertSyncReadEquals(kMsg1, kLen1);
400 AssertSyncReadEquals(kMsg2, kLen2);
401 AssertSyncReadEquals(kMsg3, kLen3);
402 AssertSyncReadEquals(kMsg3, kLen3);
403 AssertSyncReadEquals(kMsg2, kLen2);
404 AssertSyncReadEquals(kMsg3, kLen3);
405 AssertSyncReadEquals(kMsg1, kLen1);
408 TEST_F(SequencedSocketDataTest, SingleAsyncRead) {
409 MockRead reads[] = {
410 MockRead(ASYNC, kMsg1, kLen1, 0),
413 Initialize(reads, arraysize(reads), nullptr, 0);
415 AssertAsyncReadEquals(kMsg1, kLen1);
418 TEST_F(SequencedSocketDataTest, MultipleAsyncReads) {
419 MockRead reads[] = {
420 MockRead(ASYNC, kMsg1, kLen1, 0),
421 MockRead(ASYNC, kMsg2, kLen2, 1),
422 MockRead(ASYNC, kMsg3, kLen3, 2),
423 MockRead(ASYNC, kMsg3, kLen3, 3),
424 MockRead(ASYNC, kMsg2, kLen2, 4),
425 MockRead(ASYNC, kMsg3, kLen3, 5),
426 MockRead(ASYNC, kMsg1, kLen1, 6),
429 Initialize(reads, arraysize(reads), nullptr, 0);
431 AssertAsyncReadEquals(kMsg1, kLen1);
432 AssertAsyncReadEquals(kMsg2, kLen2);
433 AssertAsyncReadEquals(kMsg3, kLen3);
434 AssertAsyncReadEquals(kMsg3, kLen3);
435 AssertAsyncReadEquals(kMsg2, kLen2);
436 AssertAsyncReadEquals(kMsg3, kLen3);
437 AssertAsyncReadEquals(kMsg1, kLen1);
440 TEST_F(SequencedSocketDataTest, MixedReads) {
441 MockRead reads[] = {
442 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
443 MockRead(ASYNC, kMsg2, kLen2, 1),
444 MockRead(SYNCHRONOUS, kMsg3, kLen3, 2),
445 MockRead(ASYNC, kMsg3, kLen3, 3),
446 MockRead(SYNCHRONOUS, kMsg2, kLen2, 4),
447 MockRead(ASYNC, kMsg3, kLen3, 5),
448 MockRead(SYNCHRONOUS, kMsg1, kLen1, 6),
451 Initialize(reads, arraysize(reads), nullptr, 0);
453 AssertSyncReadEquals(kMsg1, kLen1);
454 AssertAsyncReadEquals(kMsg2, kLen2);
455 AssertSyncReadEquals(kMsg3, kLen3);
456 AssertAsyncReadEquals(kMsg3, kLen3);
457 AssertSyncReadEquals(kMsg2, kLen2);
458 AssertAsyncReadEquals(kMsg3, kLen3);
459 AssertSyncReadEquals(kMsg1, kLen1);
462 TEST_F(SequencedSocketDataTest, SyncReadFromCompletionCallback) {
463 MockRead reads[] = {
464 MockRead(ASYNC, kMsg1, kLen1, 0), MockRead(SYNCHRONOUS, kMsg2, kLen2, 1),
467 Initialize(reads, arraysize(reads), nullptr, 0);
469 read_buf_ = new IOBuffer(kLen1);
470 ASSERT_EQ(
471 ERR_IO_PENDING,
472 sock_->Read(
473 read_buf_.get(), kLen1,
474 base::Bind(&SequencedSocketDataTest::ReentrantReadCallback,
475 base::Unretained(this), kMsg1, kLen1, kLen2, kLen2)));
477 base::MessageLoop::current()->RunUntilIdle();
478 AssertReadBufferEquals(kMsg2, kLen2);
481 TEST_F(SequencedSocketDataTest, ManyReentrantReads) {
482 MockRead reads[] = {
483 MockRead(ASYNC, kMsg1, kLen1, 0),
484 MockRead(ASYNC, kMsg2, kLen2, 1),
485 MockRead(ASYNC, kMsg3, kLen3, 2),
486 MockRead(ASYNC, kMsg4, kLen4, 3),
489 Initialize(reads, arraysize(reads), nullptr, 0);
491 read_buf_ = new IOBuffer(kLen4);
493 ReentrantHelper helper3(sock_);
494 helper3.SetExpectedRead(kMsg3, kLen3);
495 helper3.SetInvokeRead(read_buf_, kLen4, ERR_IO_PENDING,
496 read_callback_.callback());
498 ReentrantHelper helper2(sock_);
499 helper2.SetExpectedRead(kMsg2, kLen2);
500 helper2.SetInvokeRead(helper3.read_buf(), kLen3, ERR_IO_PENDING,
501 helper3.callback());
503 ReentrantHelper helper(sock_);
504 helper.SetExpectedRead(kMsg1, kLen1);
505 helper.SetInvokeRead(helper2.read_buf(), kLen2, ERR_IO_PENDING,
506 helper2.callback());
508 sock_->Read(helper.read_buf().get(), kLen1, helper.callback());
510 ASSERT_EQ(kLen4, read_callback_.WaitForResult());
511 AssertReadBufferEquals(kMsg4, kLen4);
514 TEST_F(SequencedSocketDataTest, AsyncReadFromCompletionCallback) {
515 MockRead reads[] = {
516 MockRead(ASYNC, kMsg1, kLen1, 0), MockRead(ASYNC, kMsg2, kLen2, 1),
519 Initialize(reads, arraysize(reads), nullptr, 0);
521 read_buf_ = new IOBuffer(kLen1);
522 ASSERT_EQ(
523 ERR_IO_PENDING,
524 sock_->Read(read_buf_.get(), kLen1,
525 base::Bind(&SequencedSocketDataTest::ReentrantReadCallback,
526 base::Unretained(this), kMsg1, kLen1, kLen2,
527 ERR_IO_PENDING)));
529 ASSERT_FALSE(read_callback_.have_result());
530 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
531 AssertReadBufferEquals(kMsg2, kLen2);
534 TEST_F(SequencedSocketDataTest, SingleSyncReadTooEarly) {
535 MockRead reads[] = {
536 MockRead(SYNCHRONOUS, kMsg1, kLen1, 1),
539 MockWrite writes[] = {MockWrite(SYNCHRONOUS, 0, 0)};
541 Initialize(reads, arraysize(reads), writes, arraysize(writes));
543 EXPECT_NONFATAL_FAILURE(AssertReadReturns(kLen1, ERR_UNEXPECTED),
544 "Unable to perform synchronous IO while stopped");
545 set_expect_eof(false);
548 TEST_F(SequencedSocketDataTest, SingleSyncReadSmallBuffer) {
549 MockRead reads[] = {
550 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
553 Initialize(reads, arraysize(reads), nullptr, 0);
555 // Read the first chunk.
556 AssertReadReturns(kLen1 - 1, kLen1 - 1);
557 AssertReadBufferEquals(kMsg1, kLen1 - 1);
558 // Then read the second chunk.
559 AssertReadReturns(1, 1);
560 AssertReadBufferEquals(kMsg1 + kLen1 - 1, 1);
563 TEST_F(SequencedSocketDataTest, SingleSyncReadLargeBuffer) {
564 MockRead reads[] = {
565 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
568 Initialize(reads, arraysize(reads), nullptr, 0);
569 scoped_refptr<IOBuffer> read_buf(new IOBuffer(2 * kLen1));
570 ASSERT_EQ(kLen1, sock_->Read(read_buf.get(), 2 * kLen1, failing_callback_));
571 ASSERT_EQ(std::string(kMsg1, kLen1), std::string(read_buf->data(), kLen1));
574 TEST_F(SequencedSocketDataTest, SingleAsyncReadLargeBuffer) {
575 MockRead reads[] = {
576 MockRead(ASYNC, kMsg1, kLen1, 0),
579 Initialize(reads, arraysize(reads), nullptr, 0);
581 scoped_refptr<IOBuffer> read_buf(new IOBuffer(2 * kLen1));
582 ASSERT_EQ(ERR_IO_PENDING,
583 sock_->Read(read_buf.get(), 2 * kLen1, read_callback_.callback()));
584 ASSERT_EQ(kLen1, read_callback_.WaitForResult());
585 ASSERT_EQ(std::string(kMsg1, kLen1), std::string(read_buf->data(), kLen1));
588 // ----------- Write
590 TEST_F(SequencedSocketDataTest, SingleSyncWriteTooEarly) {
591 MockWrite writes[] = {
592 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1),
595 MockRead reads[] = {MockRead(SYNCHRONOUS, 0, 0)};
597 Initialize(reads, arraysize(reads), writes, arraysize(writes));
599 EXPECT_NONFATAL_FAILURE(AssertWriteReturns(kMsg1, kLen1, ERR_UNEXPECTED),
600 "Unable to perform synchronous IO while stopped");
602 set_expect_eof(false);
605 TEST_F(SequencedSocketDataTest, DISABLED_SingleSyncWriteTooSmall) {
606 MockWrite writes[] = {
607 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0),
610 Initialize(nullptr, 0, writes, arraysize(writes));
612 // Attempt to write all of the message, but only some will be written.
613 EXPECT_NONFATAL_FAILURE(AssertSyncWriteEquals(kMsg1, kLen1 - 1), "");
616 TEST_F(SequencedSocketDataTest, SingleSyncPartialWrite) {
617 MockWrite writes[] = {
618 MockWrite(SYNCHRONOUS, kMsg1, kLen1 - 1, 0),
619 MockWrite(SYNCHRONOUS, kMsg1 + kLen1 - 1, 1, 1),
622 Initialize(nullptr, 0, writes, arraysize(writes));
624 // Attempt to write all of the message, but only some will be written.
625 AssertSyncWriteEquals(kMsg1, kLen1 - 1);
626 // Write the rest of the message.
627 AssertSyncWriteEquals(kMsg1 + kLen1 - 1, 1);
630 TEST_F(SequencedSocketDataTest, SingleSyncWrite) {
631 MockWrite writes[] = {
632 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0),
635 Initialize(nullptr, 0, writes, arraysize(writes));
637 AssertSyncWriteEquals(kMsg1, kLen1);
640 TEST_F(SequencedSocketDataTest, MultipleSyncWrites) {
641 MockWrite writes[] = {
642 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0),
643 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1),
644 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2),
645 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 3),
646 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4),
647 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 5),
648 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6),
651 Initialize(nullptr, 0, writes, arraysize(writes));
653 AssertSyncWriteEquals(kMsg1, kLen1);
654 AssertSyncWriteEquals(kMsg2, kLen2);
655 AssertSyncWriteEquals(kMsg3, kLen3);
656 AssertSyncWriteEquals(kMsg3, kLen3);
657 AssertSyncWriteEquals(kMsg2, kLen2);
658 AssertSyncWriteEquals(kMsg3, kLen3);
659 AssertSyncWriteEquals(kMsg1, kLen1);
662 TEST_F(SequencedSocketDataTest, SingleAsyncWrite) {
663 MockWrite writes[] = {
664 MockWrite(ASYNC, kMsg1, kLen1, 0),
667 Initialize(nullptr, 0, writes, arraysize(writes));
669 AssertAsyncWriteEquals(kMsg1, kLen1);
672 TEST_F(SequencedSocketDataTest, MultipleAsyncWrites) {
673 MockWrite writes[] = {
674 MockWrite(ASYNC, kMsg1, kLen1, 0),
675 MockWrite(ASYNC, kMsg2, kLen2, 1),
676 MockWrite(ASYNC, kMsg3, kLen3, 2),
677 MockWrite(ASYNC, kMsg3, kLen3, 3),
678 MockWrite(ASYNC, kMsg2, kLen2, 4),
679 MockWrite(ASYNC, kMsg3, kLen3, 5),
680 MockWrite(ASYNC, kMsg1, kLen1, 6),
683 Initialize(nullptr, 0, writes, arraysize(writes));
685 AssertAsyncWriteEquals(kMsg1, kLen1);
686 AssertAsyncWriteEquals(kMsg2, kLen2);
687 AssertAsyncWriteEquals(kMsg3, kLen3);
688 AssertAsyncWriteEquals(kMsg3, kLen3);
689 AssertAsyncWriteEquals(kMsg2, kLen2);
690 AssertAsyncWriteEquals(kMsg3, kLen3);
691 AssertAsyncWriteEquals(kMsg1, kLen1);
694 TEST_F(SequencedSocketDataTest, MixedWrites) {
695 MockWrite writes[] = {
696 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0),
697 MockWrite(ASYNC, kMsg2, kLen2, 1),
698 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2),
699 MockWrite(ASYNC, kMsg3, kLen3, 3),
700 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 4),
701 MockWrite(ASYNC, kMsg3, kLen3, 5),
702 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 6),
705 Initialize(nullptr, 0, writes, arraysize(writes));
707 AssertSyncWriteEquals(kMsg1, kLen1);
708 AssertAsyncWriteEquals(kMsg2, kLen2);
709 AssertSyncWriteEquals(kMsg3, kLen3);
710 AssertAsyncWriteEquals(kMsg3, kLen3);
711 AssertSyncWriteEquals(kMsg2, kLen2);
712 AssertAsyncWriteEquals(kMsg3, kLen3);
713 AssertSyncWriteEquals(kMsg1, kLen1);
716 TEST_F(SequencedSocketDataTest, SyncWriteFromCompletionCallback) {
717 MockWrite writes[] = {
718 MockWrite(ASYNC, kMsg1, kLen1, 0),
719 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1),
722 Initialize(nullptr, 0, writes, arraysize(writes));
724 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
725 memcpy(write_buf->data(), kMsg1, kLen1);
726 ASSERT_EQ(
727 ERR_IO_PENDING,
728 sock_->Write(
729 write_buf.get(), kLen1,
730 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback,
731 base::Unretained(this), kLen1, kMsg2, kLen2, kLen2)));
733 base::MessageLoop::current()->RunUntilIdle();
736 TEST_F(SequencedSocketDataTest, AsyncWriteFromCompletionCallback) {
737 MockWrite writes[] = {
738 MockWrite(ASYNC, kMsg1, kLen1, 0), MockWrite(ASYNC, kMsg2, kLen2, 1),
741 Initialize(nullptr, 0, writes, arraysize(writes));
743 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
744 memcpy(write_buf->data(), kMsg1, kLen1);
745 ASSERT_EQ(
746 ERR_IO_PENDING,
747 sock_->Write(write_buf.get(), kLen1,
748 base::Bind(&SequencedSocketDataTest::ReentrantWriteCallback,
749 base::Unretained(this), kLen1, kMsg2, kLen2,
750 ERR_IO_PENDING)));
752 ASSERT_FALSE(write_callback_.have_result());
753 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
756 TEST_F(SequencedSocketDataTest, ManyReentrantWrites) {
757 MockWrite writes[] = {
758 MockWrite(ASYNC, kMsg1, kLen1, 0),
759 MockWrite(ASYNC, kMsg2, kLen2, 1),
760 MockWrite(ASYNC, kMsg3, kLen3, 2),
761 MockWrite(ASYNC, kMsg4, kLen4, 3),
764 Initialize(nullptr, 0, writes, arraysize(writes));
766 ReentrantHelper helper3(sock_);
767 helper3.SetExpectedWrite(kLen3);
768 helper3.SetInvokeWrite(kMsg4, kLen4, ERR_IO_PENDING,
769 write_callback_.callback());
771 ReentrantHelper helper2(sock_);
772 helper2.SetExpectedWrite(kLen2);
773 helper2.SetInvokeWrite(kMsg3, kLen3, ERR_IO_PENDING, helper3.callback());
775 ReentrantHelper helper(sock_);
776 helper.SetExpectedWrite(kLen1);
777 helper.SetInvokeWrite(kMsg2, kLen2, ERR_IO_PENDING, helper2.callback());
779 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
780 memcpy(write_buf->data(), kMsg1, kLen1);
781 sock_->Write(write_buf.get(), kLen1, helper.callback());
783 ASSERT_EQ(kLen4, write_callback_.WaitForResult());
786 // ----------- Mixed Reads and Writes
788 TEST_F(SequencedSocketDataTest, MixedSyncOperations) {
789 MockRead reads[] = {
790 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
791 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3),
794 MockWrite writes[] = {
795 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1),
796 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2),
799 Initialize(reads, arraysize(reads), writes, arraysize(writes));
801 AssertSyncReadEquals(kMsg1, kLen1);
802 AssertSyncWriteEquals(kMsg2, kLen2);
803 AssertSyncWriteEquals(kMsg3, kLen3);
804 AssertSyncReadEquals(kMsg2, kLen2);
807 TEST_F(SequencedSocketDataTest, MixedAsyncOperations) {
808 MockRead reads[] = {
809 MockRead(ASYNC, kMsg1, kLen1, 0), MockRead(ASYNC, kMsg2, kLen2, 3),
812 MockWrite writes[] = {
813 MockWrite(ASYNC, kMsg2, kLen2, 1), MockWrite(ASYNC, kMsg3, kLen3, 2),
816 Initialize(reads, arraysize(reads), writes, arraysize(writes));
818 AssertAsyncReadEquals(kMsg1, kLen1);
819 AssertAsyncWriteEquals(kMsg2, kLen2);
820 AssertAsyncWriteEquals(kMsg3, kLen3);
821 AssertAsyncReadEquals(kMsg2, kLen2);
824 TEST_F(SequencedSocketDataTest, InterleavedAsyncOperations) {
825 // Order of completion is read, write, write, read.
826 MockRead reads[] = {
827 MockRead(ASYNC, kMsg1, kLen1, 0), MockRead(ASYNC, kMsg2, kLen2, 3),
830 MockWrite writes[] = {
831 MockWrite(ASYNC, kMsg2, kLen2, 1), MockWrite(ASYNC, kMsg3, kLen3, 2),
834 Initialize(reads, arraysize(reads), writes, arraysize(writes));
836 // Issue the write, which will block until the read completes.
837 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
839 // Issue the read which will return first.
840 AssertReadReturns(kLen1, ERR_IO_PENDING);
842 ASSERT_EQ(kLen1, read_callback_.WaitForResult());
843 AssertReadBufferEquals(kMsg1, kLen1);
845 ASSERT_TRUE(write_callback_.have_result());
846 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
848 // Issue the read, which will block until the write completes.
849 AssertReadReturns(kLen2, ERR_IO_PENDING);
851 // Issue the writes which will return first.
852 AssertWriteReturns(kMsg3, kLen3, ERR_IO_PENDING);
853 ASSERT_EQ(kLen3, write_callback_.WaitForResult());
855 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
856 AssertReadBufferEquals(kMsg2, kLen2);
859 TEST_F(SequencedSocketDataTest, InterleavedMixedOperations) {
860 // Order of completion is read, write, write, read.
861 MockRead reads[] = {
862 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0),
863 MockRead(ASYNC, kMsg2, kLen2, 3),
864 MockRead(ASYNC, kMsg3, kLen3, 5),
867 MockWrite writes[] = {
868 MockWrite(ASYNC, kMsg2, kLen2, 1),
869 MockWrite(SYNCHRONOUS, kMsg3, kLen3, 2),
870 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 4),
873 Initialize(reads, arraysize(reads), writes, arraysize(writes));
875 // Issue the write, which will block until the read completes.
876 AssertWriteReturns(kMsg2, kLen2, ERR_IO_PENDING);
878 // Issue the writes which will complete immediately.
879 AssertSyncReadEquals(kMsg1, kLen1);
881 ASSERT_FALSE(write_callback_.have_result());
882 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
884 // Issue the read, which will block until the write completes.
885 AssertReadReturns(kLen2, ERR_IO_PENDING);
887 // Issue the writes which will complete immediately.
888 AssertSyncWriteEquals(kMsg3, kLen3);
890 ASSERT_FALSE(read_callback_.have_result());
891 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
892 AssertReadBufferEquals(kMsg2, kLen2);
894 // Issue the read, which will block until the write completes.
895 AssertReadReturns(kLen2, ERR_IO_PENDING);
897 // Issue the writes which will complete immediately.
898 AssertSyncWriteEquals(kMsg1, kLen1);
900 ASSERT_FALSE(read_callback_.have_result());
901 ASSERT_EQ(kLen3, read_callback_.WaitForResult());
902 AssertReadBufferEquals(kMsg3, kLen3);
905 TEST_F(SequencedSocketDataTest, AsyncReadFromWriteCompletionCallback) {
906 MockWrite writes[] = {
907 MockWrite(ASYNC, kMsg1, kLen1, 0),
910 MockRead reads[] = {
911 MockRead(ASYNC, kMsg2, kLen2, 1),
914 Initialize(reads, arraysize(reads), writes, arraysize(writes));
916 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
917 memcpy(write_buf->data(), kMsg1, kLen1);
918 ASSERT_EQ(ERR_IO_PENDING,
919 sock_->Write(
920 write_buf.get(), kLen1,
921 base::Bind(&SequencedSocketDataTest::ReentrantAsyncReadCallback,
922 base::Unretained(this), kLen1, kLen2)));
924 ASSERT_FALSE(read_callback_.have_result());
925 ASSERT_EQ(kLen2, read_callback_.WaitForResult());
926 AssertReadBufferEquals(kMsg2, kLen2);
929 TEST_F(SequencedSocketDataTest, AsyncWriteFromReadCompletionCallback) {
930 MockWrite writes[] = {
931 MockWrite(ASYNC, kMsg2, kLen2, 1),
934 MockRead reads[] = {
935 MockRead(ASYNC, kMsg1, kLen1, 0),
938 Initialize(reads, arraysize(reads), writes, arraysize(writes));
940 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1));
941 ASSERT_EQ(
942 ERR_IO_PENDING,
943 sock_->Read(
944 read_buf.get(), kLen1,
945 base::Bind(&SequencedSocketDataTest::ReentrantAsyncWriteCallback,
946 base::Unretained(this), kMsg2, kLen2,
947 write_callback_.callback(), kLen1)));
949 ASSERT_FALSE(write_callback_.have_result());
950 ASSERT_EQ(kLen2, write_callback_.WaitForResult());
953 TEST_F(SequencedSocketDataTest, MixedReentrantOperations) {
954 MockWrite writes[] = {
955 MockWrite(ASYNC, kMsg1, kLen1, 0), MockWrite(ASYNC, kMsg3, kLen3, 2),
958 MockRead reads[] = {
959 MockRead(ASYNC, kMsg2, kLen2, 1), MockRead(ASYNC, kMsg4, kLen4, 3),
962 Initialize(reads, arraysize(reads), writes, arraysize(writes));
964 read_buf_ = new IOBuffer(kLen4);
966 ReentrantHelper helper3(sock_);
967 helper3.SetExpectedWrite(kLen3);
968 helper3.SetInvokeRead(read_buf_, kLen4, ERR_IO_PENDING,
969 read_callback_.callback());
971 ReentrantHelper helper2(sock_);
972 helper2.SetExpectedRead(kMsg2, kLen2);
973 helper2.SetInvokeWrite(kMsg3, kLen3, ERR_IO_PENDING, helper3.callback());
975 ReentrantHelper helper(sock_);
976 helper.SetExpectedWrite(kLen1);
977 helper.SetInvokeRead(helper2.read_buf(), kLen2, ERR_IO_PENDING,
978 helper2.callback());
980 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
981 memcpy(write_buf->data(), kMsg1, kLen1);
982 sock_->Write(write_buf.get(), kLen1, helper.callback());
984 ASSERT_EQ(kLen4, read_callback_.WaitForResult());
987 TEST_F(SequencedSocketDataTest, MixedReentrantOperationsThenSynchronousRead) {
988 MockWrite writes[] = {
989 MockWrite(ASYNC, kMsg1, kLen1, 0), MockWrite(ASYNC, kMsg3, kLen3, 2),
992 MockRead reads[] = {
993 MockRead(ASYNC, kMsg2, kLen2, 1), MockRead(SYNCHRONOUS, kMsg4, kLen4, 3),
996 Initialize(reads, arraysize(reads), writes, arraysize(writes));
998 read_buf_ = new IOBuffer(kLen4);
1000 ReentrantHelper helper3(sock_);
1001 helper3.SetExpectedWrite(kLen3);
1002 helper3.SetInvokeRead(read_buf_, kLen4, kLen4, failing_callback_);
1004 ReentrantHelper helper2(sock_);
1005 helper2.SetExpectedRead(kMsg2, kLen2);
1006 helper2.SetInvokeWrite(kMsg3, kLen3, ERR_IO_PENDING, helper3.callback());
1008 ReentrantHelper helper(sock_);
1009 helper.SetExpectedWrite(kLen1);
1010 helper.SetInvokeRead(helper2.read_buf(), kLen2, ERR_IO_PENDING,
1011 helper2.callback());
1013 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1));
1014 memcpy(write_buf->data(), kMsg1, kLen1);
1015 ASSERT_EQ(ERR_IO_PENDING,
1016 sock_->Write(write_buf.get(), kLen1, helper.callback()));
1018 base::MessageLoop::current()->RunUntilIdle();
1019 AssertReadBufferEquals(kMsg4, kLen4);
1022 TEST_F(SequencedSocketDataTest, MixedReentrantOperationsThenSynchronousWrite) {
1023 MockWrite writes[] = {
1024 MockWrite(ASYNC, kMsg2, kLen2, 1),
1025 MockWrite(SYNCHRONOUS, kMsg4, kLen4, 3),
1028 MockRead reads[] = {
1029 MockRead(ASYNC, kMsg1, kLen1, 0), MockRead(ASYNC, kMsg3, kLen3, 2),
1032 Initialize(reads, arraysize(reads), writes, arraysize(writes));
1034 read_buf_ = new IOBuffer(kLen4);
1036 ReentrantHelper helper3(sock_);
1037 helper3.SetExpectedRead(kMsg3, kLen3);
1038 helper3.SetInvokeWrite(kMsg4, kLen4, kLen4, failing_callback_);
1040 ReentrantHelper helper2(sock_);
1041 helper2.SetExpectedWrite(kLen2);
1042 helper2.SetInvokeRead(helper3.read_buf(), kLen3, ERR_IO_PENDING,
1043 helper3.callback());
1045 ReentrantHelper helper(sock_);
1046 helper.SetExpectedRead(kMsg1, kLen1);
1047 helper.SetInvokeWrite(kMsg2, kLen2, ERR_IO_PENDING, helper2.callback());
1049 ASSERT_EQ(ERR_IO_PENDING,
1050 sock_->Read(helper.read_buf().get(), kLen1, helper.callback()));
1052 base::MessageLoop::current()->RunUntilIdle();
1055 } // namespace
1057 } // namespace net