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
3 // found in the LICENSE file.
5 #include "net/socket/socket_test_util.h"
7 #include "testing/platform_test.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 //-----------------------------------------------------------------------------
14 static const char kMsg1
[] = "\0hello!\xff";
15 static const int kLen1
= arraysize(kMsg1
);
16 static const char kMsg2
[] = "\012345678\0";
17 static const int kLen2
= arraysize(kMsg2
);
18 static const char kMsg3
[] = "bye!";
19 static const int kLen3
= arraysize(kMsg3
);
21 } // anonymous namespace
25 class DeterministicSocketDataTest
: public PlatformTest
{
27 DeterministicSocketDataTest();
29 virtual void TearDown();
32 void Initialize(MockRead
* reads
, size_t reads_count
, MockWrite
* writes
,
35 void AssertSyncReadEquals(const char* data
, int len
);
36 void AssertAsyncReadEquals(const char* data
, int len
);
37 void AssertReadReturns(const char* data
, int len
, int rv
);
38 void AssertReadBufferEquals(const char* data
, int len
);
40 void AssertSyncWriteEquals(const char* data
, int len
);
41 void AssertAsyncWriteEquals(const char* data
, int len
);
42 void AssertWriteReturns(const char* data
, int len
, int rv
);
44 TestCompletionCallback read_callback_
;
45 TestCompletionCallback write_callback_
;
47 scoped_refptr
<DeterministicSocketData
> data_
;
50 scoped_refptr
<IOBuffer
> read_buf_
;
51 MockConnect connect_data_
;
53 HostPortPair endpoint_
;
54 scoped_refptr
<TransportSocketParams
> tcp_params_
;
55 ClientSocketPoolHistograms histograms_
;
56 DeterministicMockClientSocketFactory socket_factory_
;
57 MockTransportClientSocketPool socket_pool_
;
58 ClientSocketHandle connection_
;
60 DISALLOW_COPY_AND_ASSIGN(DeterministicSocketDataTest
);
63 DeterministicSocketDataTest::DeterministicSocketDataTest()
67 connect_data_(SYNCHRONOUS
, OK
),
68 endpoint_("www.google.com", 443),
69 tcp_params_(new TransportSocketParams(endpoint_
,
73 OnHostResolutionCallback())),
75 socket_pool_(10, 10, &histograms_
, &socket_factory_
) {
78 void DeterministicSocketDataTest::TearDown() {
79 // Empty the current queue.
80 MessageLoop::current()->RunAllPending();
81 PlatformTest::TearDown();
84 void DeterministicSocketDataTest::Initialize(MockRead
* reads
,
87 size_t writes_count
) {
88 data_
= new DeterministicSocketData(reads
, reads_count
, writes
, writes_count
);
89 data_
->set_connect_data(connect_data_
);
90 socket_factory_
.AddSocketDataProvider(data_
.get());
92 // Perform the TCP connect
94 connection_
.Init(endpoint_
.ToString(),
98 reinterpret_cast<TransportClientSocketPool
*>(&socket_pool_
),
100 sock_
= connection_
.socket();
103 void DeterministicSocketDataTest::AssertSyncReadEquals(const char* data
,
105 // Issue the read, which will complete immediately
106 AssertReadReturns(data
, len
, len
);
107 AssertReadBufferEquals(data
, len
);
110 void DeterministicSocketDataTest::AssertAsyncReadEquals(const char* data
,
112 // Issue the read, which will be completed asynchronously
113 AssertReadReturns(data
, len
, ERR_IO_PENDING
);
115 EXPECT_FALSE(read_callback_
.have_result());
116 EXPECT_TRUE(sock_
->IsConnected());
117 data_
->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
119 // Now the read should complete
120 ASSERT_EQ(len
, read_callback_
.WaitForResult());
121 AssertReadBufferEquals(data
, len
);
124 void DeterministicSocketDataTest::AssertReadReturns(const char* data
,
126 read_buf_
= new IOBuffer(len
);
127 ASSERT_EQ(rv
, sock_
->Read(read_buf_
, len
, read_callback_
.callback()));
130 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data
,
132 ASSERT_EQ(std::string(data
, len
), std::string(read_buf_
->data(), len
));
135 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data
,
137 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
138 memcpy(buf
->data(), data
, len
);
140 // Issue the write, which will complete immediately
141 ASSERT_EQ(len
, sock_
->Write(buf
, len
, write_callback_
.callback()));
144 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data
,
146 // Issue the read, which will be completed asynchronously
147 AssertWriteReturns(data
, len
, ERR_IO_PENDING
);
149 EXPECT_FALSE(read_callback_
.have_result());
150 EXPECT_TRUE(sock_
->IsConnected());
151 data_
->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked
153 ASSERT_EQ(len
, write_callback_
.WaitForResult());
156 void DeterministicSocketDataTest::AssertWriteReturns(const char* data
,
158 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
159 memcpy(buf
->data(), data
, len
);
161 // Issue the read, which will complete asynchronously
162 ASSERT_EQ(rv
, sock_
->Write(buf
, len
, write_callback_
.callback()));
167 TEST_F(DeterministicSocketDataTest
, SingleSyncReadWhileStopped
) {
169 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
170 MockRead(SYNCHRONOUS
, 0, 1), // EOF
173 Initialize(reads
, arraysize(reads
), NULL
, 0);
175 data_
->SetStopped(true);
176 AssertReadReturns(kMsg1
, kLen1
, ERR_UNEXPECTED
);
179 TEST_F(DeterministicSocketDataTest
, SingleSyncReadTooEarly
) {
181 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 1), // Sync Read
182 MockRead(SYNCHRONOUS
, 0, 2), // EOF
185 Initialize(reads
, arraysize(reads
), NULL
, 0);
188 ASSERT_FALSE(data_
->stopped());
189 AssertReadReturns(kMsg1
, kLen1
, ERR_UNEXPECTED
);
192 TEST_F(DeterministicSocketDataTest
, SingleSyncRead
) {
194 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
195 MockRead(SYNCHRONOUS
, 0, 1), // EOF
198 Initialize(reads
, arraysize(reads
), NULL
, 0);
199 // Make sure we don't stop before we've read all the data
201 AssertSyncReadEquals(kMsg1
, kLen1
);
204 TEST_F(DeterministicSocketDataTest
, MultipleSyncReads
) {
206 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
207 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 1), // Sync Read
208 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Read
209 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 3), // Sync Read
210 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 4), // Sync Read
211 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 5), // Sync Read
212 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 6), // Sync Read
213 MockRead(SYNCHRONOUS
, 0, 7), // EOF
216 Initialize(reads
, arraysize(reads
), NULL
, 0);
218 // Make sure we don't stop before we've read all the data
219 data_
->StopAfter(10);
220 AssertSyncReadEquals(kMsg1
, kLen1
);
221 AssertSyncReadEquals(kMsg2
, kLen2
);
222 AssertSyncReadEquals(kMsg3
, kLen3
);
223 AssertSyncReadEquals(kMsg3
, kLen3
);
224 AssertSyncReadEquals(kMsg2
, kLen2
);
225 AssertSyncReadEquals(kMsg3
, kLen3
);
226 AssertSyncReadEquals(kMsg1
, kLen1
);
229 TEST_F(DeterministicSocketDataTest
, SingleAsyncRead
) {
231 MockRead(ASYNC
, kMsg1
, kLen1
, 0), // Async Read
232 MockRead(SYNCHRONOUS
, 0, 1), // EOF
235 Initialize(reads
, arraysize(reads
), NULL
, 0);
237 AssertAsyncReadEquals(kMsg1
, kLen1
);
240 TEST_F(DeterministicSocketDataTest
, MultipleAsyncReads
) {
242 MockRead(ASYNC
, kMsg1
, kLen1
, 0), // Async Read
243 MockRead(ASYNC
, kMsg2
, kLen2
, 1), // Async Read
244 MockRead(ASYNC
, kMsg3
, kLen3
, 2), // Async Read
245 MockRead(ASYNC
, kMsg3
, kLen3
, 3), // Async Read
246 MockRead(ASYNC
, kMsg2
, kLen2
, 4), // Async Read
247 MockRead(ASYNC
, kMsg3
, kLen3
, 5), // Async Read
248 MockRead(ASYNC
, kMsg1
, kLen1
, 6), // Async Read
249 MockRead(SYNCHRONOUS
, 0, 7), // EOF
252 Initialize(reads
, arraysize(reads
), NULL
, 0);
254 AssertAsyncReadEquals(kMsg1
, kLen1
);
255 AssertAsyncReadEquals(kMsg2
, kLen2
);
256 AssertAsyncReadEquals(kMsg3
, kLen3
);
257 AssertAsyncReadEquals(kMsg3
, kLen3
);
258 AssertAsyncReadEquals(kMsg2
, kLen2
);
259 AssertAsyncReadEquals(kMsg3
, kLen3
);
260 AssertAsyncReadEquals(kMsg1
, kLen1
);
263 TEST_F(DeterministicSocketDataTest
, MixedReads
) {
265 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
266 MockRead(ASYNC
, kMsg2
, kLen2
, 1), // Async Read
267 MockRead(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Read
268 MockRead(ASYNC
, kMsg3
, kLen3
, 3), // Async Read
269 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 4), // Sync Read
270 MockRead(ASYNC
, kMsg3
, kLen3
, 5), // Async Read
271 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 6), // Sync Read
272 MockRead(SYNCHRONOUS
, 0, 7), // EOF
275 Initialize(reads
, arraysize(reads
), NULL
, 0);
278 AssertSyncReadEquals(kMsg1
, kLen1
);
279 AssertAsyncReadEquals(kMsg2
, kLen2
);
281 AssertSyncReadEquals(kMsg3
, kLen3
);
282 AssertAsyncReadEquals(kMsg3
, kLen3
);
284 AssertSyncReadEquals(kMsg2
, kLen2
);
285 AssertAsyncReadEquals(kMsg3
, kLen3
);
287 AssertSyncReadEquals(kMsg1
, kLen1
);
292 TEST_F(DeterministicSocketDataTest
, SingleSyncWriteWhileStopped
) {
293 MockWrite writes
[] = {
294 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
297 Initialize(NULL
, 0, writes
, arraysize(writes
));
299 data_
->SetStopped(true);
300 AssertWriteReturns(kMsg1
, kLen1
, ERR_UNEXPECTED
);
303 TEST_F(DeterministicSocketDataTest
, SingleSyncWriteTooEarly
) {
304 MockWrite writes
[] = {
305 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 1), // Sync Write
308 Initialize(NULL
, 0, writes
, arraysize(writes
));
311 ASSERT_FALSE(data_
->stopped());
312 AssertWriteReturns(kMsg1
, kLen1
, ERR_UNEXPECTED
);
315 TEST_F(DeterministicSocketDataTest
, SingleSyncWrite
) {
316 MockWrite writes
[] = {
317 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Write
320 Initialize(NULL
, 0, writes
, arraysize(writes
));
322 // Make sure we don't stop before we've read all the data
324 AssertSyncWriteEquals(kMsg1
, kLen1
);
327 TEST_F(DeterministicSocketDataTest
, MultipleSyncWrites
) {
328 MockWrite writes
[] = {
329 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Write
330 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1), // Sync Write
331 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Write
332 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 3), // Sync Write
333 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4), // Sync Write
334 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 5), // Sync Write
335 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6), // Sync Write
338 Initialize(NULL
, 0, writes
, arraysize(writes
));
340 // Make sure we don't stop before we've read all the data
341 data_
->StopAfter(10);
342 AssertSyncWriteEquals(kMsg1
, kLen1
);
343 AssertSyncWriteEquals(kMsg2
, kLen2
);
344 AssertSyncWriteEquals(kMsg3
, kLen3
);
345 AssertSyncWriteEquals(kMsg3
, kLen3
);
346 AssertSyncWriteEquals(kMsg2
, kLen2
);
347 AssertSyncWriteEquals(kMsg3
, kLen3
);
348 AssertSyncWriteEquals(kMsg1
, kLen1
);
351 TEST_F(DeterministicSocketDataTest
, SingleAsyncWrite
) {
352 MockWrite writes
[] = {
353 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), // Async Write
356 Initialize(NULL
, 0, writes
, arraysize(writes
));
358 AssertAsyncWriteEquals(kMsg1
, kLen1
);
361 TEST_F(DeterministicSocketDataTest
, MultipleAsyncWrites
) {
362 MockWrite writes
[] = {
363 MockWrite(ASYNC
, kMsg1
, kLen1
, 0), // Async Write
364 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), // Async Write
365 MockWrite(ASYNC
, kMsg3
, kLen3
, 2), // Async Write
366 MockWrite(ASYNC
, kMsg3
, kLen3
, 3), // Async Write
367 MockWrite(ASYNC
, kMsg2
, kLen2
, 4), // Async Write
368 MockWrite(ASYNC
, kMsg3
, kLen3
, 5), // Async Write
369 MockWrite(ASYNC
, kMsg1
, kLen1
, 6), // Async Write
372 Initialize(NULL
, 0, writes
, arraysize(writes
));
374 AssertAsyncWriteEquals(kMsg1
, kLen1
);
375 AssertAsyncWriteEquals(kMsg2
, kLen2
);
376 AssertAsyncWriteEquals(kMsg3
, kLen3
);
377 AssertAsyncWriteEquals(kMsg3
, kLen3
);
378 AssertAsyncWriteEquals(kMsg2
, kLen2
);
379 AssertAsyncWriteEquals(kMsg3
, kLen3
);
380 AssertAsyncWriteEquals(kMsg1
, kLen1
);
383 TEST_F(DeterministicSocketDataTest
, MixedWrites
) {
384 MockWrite writes
[] = {
385 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Write
386 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), // Async Write
387 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Write
388 MockWrite(ASYNC
, kMsg3
, kLen3
, 3), // Async Write
389 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 4), // Sync Write
390 MockWrite(ASYNC
, kMsg3
, kLen3
, 5), // Async Write
391 MockWrite(SYNCHRONOUS
, kMsg1
, kLen1
, 6), // Sync Write
394 Initialize(NULL
, 0, writes
, arraysize(writes
));
397 AssertSyncWriteEquals(kMsg1
, kLen1
);
398 AssertAsyncWriteEquals(kMsg2
, kLen2
);
400 AssertSyncWriteEquals(kMsg3
, kLen3
);
401 AssertAsyncWriteEquals(kMsg3
, kLen3
);
403 AssertSyncWriteEquals(kMsg2
, kLen2
);
404 AssertAsyncWriteEquals(kMsg3
, kLen3
);
406 AssertSyncWriteEquals(kMsg1
, kLen1
);
409 // ----------- Mixed Reads and Writes
411 TEST_F(DeterministicSocketDataTest
, MixedSyncOperations
) {
413 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
414 MockRead(SYNCHRONOUS
, kMsg2
, kLen2
, 3), // Sync Read
415 MockRead(SYNCHRONOUS
, 0, 4), // EOF
418 MockWrite writes
[] = {
419 MockWrite(SYNCHRONOUS
, kMsg2
, kLen2
, 1), // Sync Write
420 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Write
423 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
425 // Make sure we don't stop before we've read/written everything
426 data_
->StopAfter(10);
427 AssertSyncReadEquals(kMsg1
, kLen1
);
428 AssertSyncWriteEquals(kMsg2
, kLen2
);
429 AssertSyncWriteEquals(kMsg3
, kLen3
);
430 AssertSyncReadEquals(kMsg2
, kLen2
);
433 TEST_F(DeterministicSocketDataTest
, MixedAsyncOperations
) {
435 MockRead(ASYNC
, kMsg1
, kLen1
, 0), // Sync Read
436 MockRead(ASYNC
, kMsg2
, kLen2
, 3), // Sync Read
437 MockRead(ASYNC
, 0, 4), // EOF
440 MockWrite writes
[] = {
441 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), // Sync Write
442 MockWrite(ASYNC
, kMsg3
, kLen3
, 2), // Sync Write
445 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
447 AssertAsyncReadEquals(kMsg1
, kLen1
);
448 AssertAsyncWriteEquals(kMsg2
, kLen2
);
449 AssertAsyncWriteEquals(kMsg3
, kLen3
);
450 AssertAsyncReadEquals(kMsg2
, kLen2
);
453 TEST_F(DeterministicSocketDataTest
, InterleavedAsyncOperations
) {
454 // Order of completion is read, write, write, read
456 MockRead(ASYNC
, kMsg1
, kLen1
, 0), // Async Read
457 MockRead(ASYNC
, kMsg2
, kLen2
, 3), // Async Read
458 MockRead(ASYNC
, 0, 4), // EOF
461 MockWrite writes
[] = {
462 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), // Async Write
463 MockWrite(ASYNC
, kMsg3
, kLen3
, 2), // Async Write
466 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
468 // Issue the write, which will block until the read completes
469 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
471 // Issue the read which will return first
472 AssertReadReturns(kMsg1
, kLen1
, ERR_IO_PENDING
);
475 ASSERT_EQ(kLen1
, read_callback_
.WaitForResult());
476 AssertReadBufferEquals(kMsg1
, kLen1
);
479 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
482 // Issue the read, which will block until the write completes
483 AssertReadReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
485 // Issue the writes which will return first
486 AssertWriteReturns(kMsg3
, kLen3
, ERR_IO_PENDING
);
489 ASSERT_EQ(kLen3
, write_callback_
.WaitForResult());
492 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
493 AssertReadBufferEquals(kMsg2
, kLen2
);
496 TEST_F(DeterministicSocketDataTest
, InterleavedMixedOperations
) {
497 // Order of completion is read, write, write, read
499 MockRead(SYNCHRONOUS
, kMsg1
, kLen1
, 0), // Sync Read
500 MockRead(ASYNC
, kMsg2
, kLen2
, 3), // Async Read
501 MockRead(SYNCHRONOUS
, 0, 4), // EOF
504 MockWrite writes
[] = {
505 MockWrite(ASYNC
, kMsg2
, kLen2
, 1), // Async Write
506 MockWrite(SYNCHRONOUS
, kMsg3
, kLen3
, 2), // Sync Write
509 Initialize(reads
, arraysize(reads
), writes
, arraysize(writes
));
511 // Issue the write, which will block until the read completes
512 AssertWriteReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
514 // Issue the writes which will complete immediately
516 AssertSyncReadEquals(kMsg1
, kLen1
);
519 ASSERT_EQ(kLen2
, write_callback_
.WaitForResult());
521 // Issue the read, which will block until the write completes
522 AssertReadReturns(kMsg2
, kLen2
, ERR_IO_PENDING
);
524 // Issue the writes which will complete immediately
526 AssertSyncWriteEquals(kMsg3
, kLen3
);
529 ASSERT_EQ(kLen2
, read_callback_
.WaitForResult());
530 AssertReadBufferEquals(kMsg2
, kLen2
);