| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 size_t writes_count); | 187 size_t writes_count); |
| 188 | 188 |
| 189 void AssertSyncReadEquals(const char* data, int len); | 189 void AssertSyncReadEquals(const char* data, int len); |
| 190 void AssertAsyncReadEquals(const char* data, int len); | 190 void AssertAsyncReadEquals(const char* data, int len); |
| 191 void AssertReadReturns(int len, int rv); | 191 void AssertReadReturns(int len, int rv); |
| 192 void AssertReadBufferEquals(const char* data, int len); | 192 void AssertReadBufferEquals(const char* data, int len); |
| 193 | 193 |
| 194 void AssertSyncWriteEquals(const char* data, int len); | 194 void AssertSyncWriteEquals(const char* data, int len); |
| 195 void AssertAsyncWriteEquals(const char* data, int len); | 195 void AssertAsyncWriteEquals(const char* data, int len); |
| 196 void AssertWriteReturns(const char* data, int len, int rv); | 196 void AssertWriteReturns(const char* data, int len, int rv); |
| 197 void CompleteRead(); |
| 197 | 198 |
| 198 // When a given test completes, data_.at_eof() is expected to | 199 // When a given test completes, data_.at_eof() is expected to |
| 199 // match the value specified here. Most test should consume all | 200 // match the value specified here. Most test should consume all |
| 200 // reads and writes, but some tests verify error handling behavior | 201 // reads and writes, but some tests verify error handling behavior |
| 201 // do not consume all data. | 202 // do not consume all data. |
| 202 void set_expect_eof(bool expect_eof) { expect_eof_ = expect_eof; } | 203 void set_expect_eof(bool expect_eof) { expect_eof_ = expect_eof; } |
| 203 | 204 |
| 204 TestCompletionCallback read_callback_; | 205 TestCompletionCallback read_callback_; |
| 205 scoped_refptr<IOBuffer> read_buf_; | 206 scoped_refptr<IOBuffer> read_buf_; |
| 206 TestCompletionCallback write_callback_; | 207 TestCompletionCallback write_callback_; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 int len) { | 307 int len) { |
| 307 // Issue the read, which should be completed asynchronously. | 308 // Issue the read, which should be completed asynchronously. |
| 308 AssertWriteReturns(data, len, ERR_IO_PENDING); | 309 AssertWriteReturns(data, len, ERR_IO_PENDING); |
| 309 | 310 |
| 310 EXPECT_FALSE(read_callback_.have_result()); | 311 EXPECT_FALSE(read_callback_.have_result()); |
| 311 EXPECT_TRUE(sock_->IsConnected()); | 312 EXPECT_TRUE(sock_->IsConnected()); |
| 312 | 313 |
| 313 ASSERT_EQ(len, write_callback_.WaitForResult()); | 314 ASSERT_EQ(len, write_callback_.WaitForResult()); |
| 314 } | 315 } |
| 315 | 316 |
| 317 void SequencedSocketDataTest::CompleteRead() { |
| 318 data_->CompleteRead(); |
| 319 } |
| 320 |
| 316 void SequencedSocketDataTest::AssertWriteReturns(const char* data, | 321 void SequencedSocketDataTest::AssertWriteReturns(const char* data, |
| 317 int len, | 322 int len, |
| 318 int rv) { | 323 int rv) { |
| 319 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 324 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
| 320 memcpy(buf->data(), data, len); | 325 memcpy(buf->data(), data, len); |
| 321 | 326 |
| 322 if (rv == ERR_IO_PENDING) { | 327 if (rv == ERR_IO_PENDING) { |
| 323 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); | 328 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); |
| 324 ASSERT_FALSE(write_callback_.have_result()); | 329 ASSERT_FALSE(write_callback_.have_result()); |
| 325 } else { | 330 } else { |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 ASSERT_EQ(ERR_IO_PENDING, | 601 ASSERT_EQ(ERR_IO_PENDING, |
| 597 sock_->Read(read_buf.get(), 1, read_callback_.callback())); | 602 sock_->Read(read_buf.get(), 1, read_callback_.callback())); |
| 598 ASSERT_FALSE(read_callback_.have_result()); | 603 ASSERT_FALSE(read_callback_.have_result()); |
| 599 | 604 |
| 600 // Even though the read is scheduled to complete at sequence number 0, | 605 // Even though the read is scheduled to complete at sequence number 0, |
| 601 // verify that the read callback in never called. | 606 // verify that the read callback in never called. |
| 602 base::MessageLoop::current()->RunUntilIdle(); | 607 base::MessageLoop::current()->RunUntilIdle(); |
| 603 ASSERT_FALSE(read_callback_.have_result()); | 608 ASSERT_FALSE(read_callback_.have_result()); |
| 604 } | 609 } |
| 605 | 610 |
| 611 TEST_F(SequencedSocketDataTest, CompleteRead) { |
| 612 MockRead reads[] = { |
| 613 MockRead(ASYNC, ERR_IO_PENDING, 0), MockRead(ASYNC, kMsg1, kLen1, 1), |
| 614 }; |
| 615 |
| 616 Initialize(reads, arraysize(reads), nullptr, 0); |
| 617 |
| 618 AssertReadReturns(kLen1, ERR_IO_PENDING); |
| 619 ASSERT_FALSE(read_callback_.have_result()); |
| 620 |
| 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()); |
| 625 |
| 626 CompleteRead(); |
| 627 |
| 628 ASSERT_TRUE(read_callback_.have_result()); |
| 629 ASSERT_EQ(kLen1, read_callback_.WaitForResult()); |
| 630 AssertReadBufferEquals(kMsg1, kLen1); |
| 631 } |
| 632 |
| 606 // ----------- Write | 633 // ----------- Write |
| 607 | 634 |
| 608 TEST_F(SequencedSocketDataTest, SingleSyncWriteTooEarly) { | 635 TEST_F(SequencedSocketDataTest, SingleSyncWriteTooEarly) { |
| 609 MockWrite writes[] = { | 636 MockWrite writes[] = { |
| 610 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), | 637 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), |
| 611 }; | 638 }; |
| 612 | 639 |
| 613 MockRead reads[] = {MockRead(SYNCHRONOUS, 0, 0)}; | 640 MockRead reads[] = {MockRead(SYNCHRONOUS, 0, 0)}; |
| 614 | 641 |
| 615 Initialize(reads, arraysize(reads), writes, arraysize(writes)); | 642 Initialize(reads, arraysize(reads), writes, arraysize(writes)); |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1066 | 1093 |
| 1067 ASSERT_EQ(ERR_IO_PENDING, | 1094 ASSERT_EQ(ERR_IO_PENDING, |
| 1068 sock_->Read(helper.read_buf().get(), kLen1, helper.callback())); | 1095 sock_->Read(helper.read_buf().get(), kLen1, helper.callback())); |
| 1069 | 1096 |
| 1070 base::MessageLoop::current()->RunUntilIdle(); | 1097 base::MessageLoop::current()->RunUntilIdle(); |
| 1071 } | 1098 } |
| 1072 | 1099 |
| 1073 } // namespace | 1100 } // namespace |
| 1074 | 1101 |
| 1075 } // namespace net | 1102 } // namespace net |
| OLD | NEW |