OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 123 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
124 | 124 |
125 // Now the read should complete | 125 // Now the read should complete |
126 ASSERT_EQ(len, read_callback_.WaitForResult()); | 126 ASSERT_EQ(len, read_callback_.WaitForResult()); |
127 AssertReadBufferEquals(data, len); | 127 AssertReadBufferEquals(data, len); |
128 } | 128 } |
129 | 129 |
130 void DeterministicSocketDataTest::AssertReadReturns(const char* data, | 130 void DeterministicSocketDataTest::AssertReadReturns(const char* data, |
131 int len, int rv) { | 131 int len, int rv) { |
132 read_buf_ = new IOBuffer(len); | 132 read_buf_ = new IOBuffer(len); |
133 ASSERT_EQ(rv, sock_->Read(read_buf_, len, read_callback_.callback())); | 133 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback())); |
134 } | 134 } |
135 | 135 |
136 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, | 136 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, |
137 int len) { | 137 int len) { |
138 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); | 138 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); |
139 } | 139 } |
140 | 140 |
141 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, | 141 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, |
142 int len) { | 142 int len) { |
143 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 143 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
144 memcpy(buf->data(), data, len); | 144 memcpy(buf->data(), data, len); |
145 | 145 |
146 // Issue the write, which will complete immediately | 146 // Issue the write, which will complete immediately |
147 ASSERT_EQ(len, sock_->Write(buf, len, write_callback_.callback())); | 147 ASSERT_EQ(len, sock_->Write(buf.get(), len, write_callback_.callback())); |
148 } | 148 } |
149 | 149 |
150 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, | 150 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, |
151 int len) { | 151 int len) { |
152 // Issue the read, which will be completed asynchronously | 152 // Issue the read, which will be completed asynchronously |
153 AssertWriteReturns(data, len, ERR_IO_PENDING); | 153 AssertWriteReturns(data, len, ERR_IO_PENDING); |
154 | 154 |
155 EXPECT_FALSE(read_callback_.have_result()); | 155 EXPECT_FALSE(read_callback_.have_result()); |
156 EXPECT_TRUE(sock_->IsConnected()); | 156 EXPECT_TRUE(sock_->IsConnected()); |
157 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 157 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
158 | 158 |
159 ASSERT_EQ(len, write_callback_.WaitForResult()); | 159 ASSERT_EQ(len, write_callback_.WaitForResult()); |
160 } | 160 } |
161 | 161 |
162 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, | 162 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, |
163 int len, int rv) { | 163 int len, int rv) { |
164 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 164 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
165 memcpy(buf->data(), data, len); | 165 memcpy(buf->data(), data, len); |
166 | 166 |
167 // Issue the read, which will complete asynchronously | 167 // Issue the read, which will complete asynchronously |
168 ASSERT_EQ(rv, sock_->Write(buf, len, write_callback_.callback())); | 168 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); |
169 } | 169 } |
170 | 170 |
171 void DeterministicSocketDataTest::ReentrantReadCallback(int len, int rv) { | 171 void DeterministicSocketDataTest::ReentrantReadCallback(int len, int rv) { |
172 scoped_refptr<IOBuffer> read_buf(new IOBuffer(len)); | 172 scoped_refptr<IOBuffer> read_buf(new IOBuffer(len)); |
173 EXPECT_EQ(len, sock_->Read(read_buf, len, | 173 EXPECT_EQ(len, |
174 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback, | 174 sock_->Read( |
175 base::Unretained(this), len))); | 175 read_buf.get(), |
| 176 len, |
| 177 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback, |
| 178 base::Unretained(this), |
| 179 len))); |
176 } | 180 } |
177 | 181 |
178 void DeterministicSocketDataTest::ReentrantWriteCallback( | 182 void DeterministicSocketDataTest::ReentrantWriteCallback( |
179 const char* data, int len, int rv) { | 183 const char* data, int len, int rv) { |
180 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len)); | 184 scoped_refptr<IOBuffer> write_buf(new IOBuffer(len)); |
181 memcpy(write_buf->data(), data, len); | 185 memcpy(write_buf->data(), data, len); |
182 EXPECT_EQ(len, sock_->Write(write_buf, len, | 186 EXPECT_EQ(len, |
183 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback, | 187 sock_->Write( |
184 base::Unretained(this), data, len))); | 188 write_buf.get(), |
| 189 len, |
| 190 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback, |
| 191 base::Unretained(this), |
| 192 data, |
| 193 len))); |
185 } | 194 } |
186 | 195 |
187 // ----------- Read | 196 // ----------- Read |
188 | 197 |
189 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { | 198 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { |
190 MockRead reads[] = { | 199 MockRead reads[] = { |
191 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read | 200 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read |
192 MockRead(SYNCHRONOUS, 0, 1), // EOF | 201 MockRead(SYNCHRONOUS, 0, 1), // EOF |
193 }; | 202 }; |
194 | 203 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 MockRead reads[] = { | 326 MockRead reads[] = { |
318 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read | 327 MockRead(ASYNC, kMsg1, kLen1, 0), // Async Read |
319 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read | 328 MockRead(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Read |
320 }; | 329 }; |
321 | 330 |
322 Initialize(reads, arraysize(reads), NULL, 0); | 331 Initialize(reads, arraysize(reads), NULL, 0); |
323 | 332 |
324 data_->StopAfter(2); | 333 data_->StopAfter(2); |
325 | 334 |
326 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1)); | 335 scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1)); |
327 ASSERT_EQ(ERR_IO_PENDING, sock_->Read(read_buf, kLen1, | 336 ASSERT_EQ(ERR_IO_PENDING, |
328 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback, | 337 sock_->Read( |
329 base::Unretained(this), kLen2))); | 338 read_buf.get(), |
| 339 kLen1, |
| 340 base::Bind(&DeterministicSocketDataTest::ReentrantReadCallback, |
| 341 base::Unretained(this), |
| 342 kLen2))); |
330 data_->Run(); | 343 data_->Run(); |
331 } | 344 } |
332 | 345 |
333 // ----------- Write | 346 // ----------- Write |
334 | 347 |
335 TEST_F(DeterministicSocketDataTest, SingleSyncWriteWhileStopped) { | 348 TEST_F(DeterministicSocketDataTest, SingleSyncWriteWhileStopped) { |
336 MockWrite writes[] = { | 349 MockWrite writes[] = { |
337 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read | 350 MockWrite(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read |
338 }; | 351 }; |
339 | 352 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write | 471 MockWrite(ASYNC, kMsg1, kLen1, 0), // Async Write |
459 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write | 472 MockWrite(SYNCHRONOUS, kMsg2, kLen2, 1), // Sync Write |
460 }; | 473 }; |
461 | 474 |
462 Initialize(NULL, 0, writes, arraysize(writes)); | 475 Initialize(NULL, 0, writes, arraysize(writes)); |
463 | 476 |
464 data_->StopAfter(2); | 477 data_->StopAfter(2); |
465 | 478 |
466 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1)); | 479 scoped_refptr<IOBuffer> write_buf(new IOBuffer(kLen1)); |
467 memcpy(write_buf->data(), kMsg1, kLen1); | 480 memcpy(write_buf->data(), kMsg1, kLen1); |
468 ASSERT_EQ(ERR_IO_PENDING, sock_->Write(write_buf, kLen1, | 481 ASSERT_EQ(ERR_IO_PENDING, |
469 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback, | 482 sock_->Write( |
470 base::Unretained(this), kMsg2, kLen2))); | 483 write_buf.get(), |
| 484 kLen1, |
| 485 base::Bind(&DeterministicSocketDataTest::ReentrantWriteCallback, |
| 486 base::Unretained(this), |
| 487 kMsg2, |
| 488 kLen2))); |
471 data_->Run(); | 489 data_->Run(); |
472 } | 490 } |
473 | 491 |
474 // ----------- Mixed Reads and Writes | 492 // ----------- Mixed Reads and Writes |
475 | 493 |
476 TEST_F(DeterministicSocketDataTest, MixedSyncOperations) { | 494 TEST_F(DeterministicSocketDataTest, MixedSyncOperations) { |
477 MockRead reads[] = { | 495 MockRead reads[] = { |
478 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read | 496 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read |
479 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3), // Sync Read | 497 MockRead(SYNCHRONOUS, kMsg2, kLen2, 3), // Sync Read |
480 MockRead(SYNCHRONOUS, 0, 4), // EOF | 498 MockRead(SYNCHRONOUS, 0, 4), // EOF |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 data_->StopAfter(1); | 613 data_->StopAfter(1); |
596 AssertSyncWriteEquals(kMsg3, kLen3); | 614 AssertSyncWriteEquals(kMsg3, kLen3); |
597 | 615 |
598 data_->RunFor(1); | 616 data_->RunFor(1); |
599 ASSERT_TRUE(read_callback_.have_result()); | 617 ASSERT_TRUE(read_callback_.have_result()); |
600 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); | 618 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); |
601 AssertReadBufferEquals(kMsg2, kLen2); | 619 AssertReadBufferEquals(kMsg2, kLen2); |
602 } | 620 } |
603 | 621 |
604 } // namespace net | 622 } // namespace net |
OLD | NEW |