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(); | |
198 | 197 |
199 // When a given test completes, data_.at_eof() is expected to | 198 // When a given test completes, data_.at_eof() is expected to |
200 // match the value specified here. Most test should consume all | 199 // match the value specified here. Most test should consume all |
201 // reads and writes, but some tests verify error handling behavior | 200 // reads and writes, but some tests verify error handling behavior |
202 // do not consume all data. | 201 // do not consume all data. |
203 void set_expect_eof(bool expect_eof) { expect_eof_ = expect_eof; } | 202 void set_expect_eof(bool expect_eof) { expect_eof_ = expect_eof; } |
204 | 203 |
205 TestCompletionCallback read_callback_; | 204 TestCompletionCallback read_callback_; |
206 scoped_refptr<IOBuffer> read_buf_; | 205 scoped_refptr<IOBuffer> read_buf_; |
207 TestCompletionCallback write_callback_; | 206 TestCompletionCallback write_callback_; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 int len) { | 306 int len) { |
308 // Issue the read, which should be completed asynchronously. | 307 // Issue the read, which should be completed asynchronously. |
309 AssertWriteReturns(data, len, ERR_IO_PENDING); | 308 AssertWriteReturns(data, len, ERR_IO_PENDING); |
310 | 309 |
311 EXPECT_FALSE(read_callback_.have_result()); | 310 EXPECT_FALSE(read_callback_.have_result()); |
312 EXPECT_TRUE(sock_->IsConnected()); | 311 EXPECT_TRUE(sock_->IsConnected()); |
313 | 312 |
314 ASSERT_EQ(len, write_callback_.WaitForResult()); | 313 ASSERT_EQ(len, write_callback_.WaitForResult()); |
315 } | 314 } |
316 | 315 |
317 void SequencedSocketDataTest::CompleteRead() { | |
318 data_->CompleteRead(); | |
319 } | |
320 | |
321 void SequencedSocketDataTest::AssertWriteReturns(const char* data, | 316 void SequencedSocketDataTest::AssertWriteReturns(const char* data, |
322 int len, | 317 int len, |
323 int rv) { | 318 int rv) { |
324 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 319 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
325 memcpy(buf->data(), data, len); | 320 memcpy(buf->data(), data, len); |
326 | 321 |
327 if (rv == ERR_IO_PENDING) { | 322 if (rv == ERR_IO_PENDING) { |
328 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); | 323 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); |
329 ASSERT_FALSE(write_callback_.have_result()); | 324 ASSERT_FALSE(write_callback_.have_result()); |
330 } else { | 325 } else { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 ASSERT_EQ(ERR_IO_PENDING, | 596 ASSERT_EQ(ERR_IO_PENDING, |
602 sock_->Read(read_buf.get(), 1, read_callback_.callback())); | 597 sock_->Read(read_buf.get(), 1, read_callback_.callback())); |
603 ASSERT_FALSE(read_callback_.have_result()); | 598 ASSERT_FALSE(read_callback_.have_result()); |
604 | 599 |
605 // Even though the read is scheduled to complete at sequence number 0, | 600 // Even though the read is scheduled to complete at sequence number 0, |
606 // verify that the read callback in never called. | 601 // verify that the read callback in never called. |
607 base::MessageLoop::current()->RunUntilIdle(); | 602 base::MessageLoop::current()->RunUntilIdle(); |
608 ASSERT_FALSE(read_callback_.have_result()); | 603 ASSERT_FALSE(read_callback_.have_result()); |
609 } | 604 } |
610 | 605 |
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 | |
633 // ----------- Write | 606 // ----------- Write |
634 | 607 |
635 TEST_F(SequencedSocketDataTest, SingleSyncWriteTooEarly) { | 608 TEST_F(SequencedSocketDataTest, SingleSyncWriteTooEarly) { |
636 MockWrite writes[] = { | 609 MockWrite writes[] = { |
637 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), | 610 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 1), |
638 }; | 611 }; |
639 | 612 |
640 MockRead reads[] = {MockRead(SYNCHRONOUS, 0, 0)}; | 613 MockRead reads[] = {MockRead(SYNCHRONOUS, 0, 0)}; |
641 | 614 |
642 Initialize(reads, arraysize(reads), writes, arraysize(writes)); | 615 Initialize(reads, arraysize(reads), writes, arraysize(writes)); |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1093 | 1066 |
1094 ASSERT_EQ(ERR_IO_PENDING, | 1067 ASSERT_EQ(ERR_IO_PENDING, |
1095 sock_->Read(helper.read_buf().get(), kLen1, helper.callback())); | 1068 sock_->Read(helper.read_buf().get(), kLen1, helper.callback())); |
1096 | 1069 |
1097 base::MessageLoop::current()->RunUntilIdle(); | 1070 base::MessageLoop::current()->RunUntilIdle(); |
1098 } | 1071 } |
1099 | 1072 |
1100 } // namespace | 1073 } // namespace |
1101 | 1074 |
1102 } // namespace net | 1075 } // namespace net |
OLD | NEW |