| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "testing/platform_test.h" | 7 #include "testing/platform_test.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 //----------------------------------------------------------------------------- | 10 //----------------------------------------------------------------------------- |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 void AssertSyncReadEquals(const char* data, int len); | 35 void AssertSyncReadEquals(const char* data, int len); |
| 36 void AssertAsyncReadEquals(const char* data, int len); | 36 void AssertAsyncReadEquals(const char* data, int len); |
| 37 void AssertReadReturns(const char* data, int len, int rv); | 37 void AssertReadReturns(const char* data, int len, int rv); |
| 38 void AssertReadBufferEquals(const char* data, int len); | 38 void AssertReadBufferEquals(const char* data, int len); |
| 39 | 39 |
| 40 void AssertSyncWriteEquals(const char* data, int len); | 40 void AssertSyncWriteEquals(const char* data, int len); |
| 41 void AssertAsyncWriteEquals(const char* data, int len); | 41 void AssertAsyncWriteEquals(const char* data, int len); |
| 42 void AssertWriteReturns(const char* data, int len, int rv); | 42 void AssertWriteReturns(const char* data, int len, int rv); |
| 43 | 43 |
| 44 TestOldCompletionCallback read_callback_; | 44 TestCompletionCallback read_callback_; |
| 45 TestOldCompletionCallback write_callback_; | 45 TestCompletionCallback write_callback_; |
| 46 StreamSocket* sock_; | 46 StreamSocket* sock_; |
| 47 scoped_refptr<DeterministicSocketData> data_; | 47 scoped_refptr<DeterministicSocketData> data_; |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 scoped_refptr<IOBuffer> read_buf_; | 50 scoped_refptr<IOBuffer> read_buf_; |
| 51 MockConnect connect_data_; | 51 MockConnect connect_data_; |
| 52 | 52 |
| 53 HostPortPair endpoint_; | 53 HostPortPair endpoint_; |
| 54 scoped_refptr<TransportSocketParams> tcp_params_; | 54 scoped_refptr<TransportSocketParams> tcp_params_; |
| 55 ClientSocketPoolHistograms histograms_; | 55 ClientSocketPoolHistograms histograms_; |
| 56 DeterministicMockClientSocketFactory socket_factory_; | 56 DeterministicMockClientSocketFactory socket_factory_; |
| 57 MockTransportClientSocketPool socket_pool_; | 57 MockTransportClientSocketPool socket_pool_; |
| 58 ClientSocketHandle connection_; | 58 ClientSocketHandle connection_; |
| 59 | 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(DeterministicSocketDataTest); | 60 DISALLOW_COPY_AND_ASSIGN(DeterministicSocketDataTest); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 DeterministicSocketDataTest::DeterministicSocketDataTest() | 63 DeterministicSocketDataTest::DeterministicSocketDataTest() |
| 64 : read_callback_(), | 64 : sock_(NULL), |
| 65 write_callback_(), | |
| 66 sock_(NULL), | |
| 67 data_(NULL), | 65 data_(NULL), |
| 68 read_buf_(NULL), | 66 read_buf_(NULL), |
| 69 connect_data_(false, OK), | 67 connect_data_(false, OK), |
| 70 endpoint_("www.google.com", 443), | 68 endpoint_("www.google.com", 443), |
| 71 tcp_params_(new TransportSocketParams(endpoint_, | 69 tcp_params_(new TransportSocketParams(endpoint_, |
| 72 LOWEST, | 70 LOWEST, |
| 73 false, | 71 false, |
| 74 false)), | 72 false)), |
| 75 histograms_(""), | 73 histograms_(""), |
| 76 socket_pool_(10, 10, &histograms_, &socket_factory_) { | 74 socket_pool_(10, 10, &histograms_, &socket_factory_) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 116 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
| 119 | 117 |
| 120 // Now the read should complete | 118 // Now the read should complete |
| 121 ASSERT_EQ(len, read_callback_.WaitForResult()); | 119 ASSERT_EQ(len, read_callback_.WaitForResult()); |
| 122 AssertReadBufferEquals(data, len); | 120 AssertReadBufferEquals(data, len); |
| 123 } | 121 } |
| 124 | 122 |
| 125 void DeterministicSocketDataTest::AssertReadReturns(const char* data, | 123 void DeterministicSocketDataTest::AssertReadReturns(const char* data, |
| 126 int len, int rv) { | 124 int len, int rv) { |
| 127 read_buf_ = new IOBuffer(len); | 125 read_buf_ = new IOBuffer(len); |
| 128 ASSERT_EQ(rv, sock_->Read(read_buf_, len, &read_callback_)); | 126 ASSERT_EQ(rv, sock_->Read(read_buf_, len, read_callback_.callback())); |
| 129 } | 127 } |
| 130 | 128 |
| 131 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, | 129 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, |
| 132 int len) { | 130 int len) { |
| 133 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); | 131 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); |
| 134 } | 132 } |
| 135 | 133 |
| 136 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, | 134 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, |
| 137 int len) { | 135 int len) { |
| 138 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 136 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
| 139 memcpy(buf->data(), data, len); | 137 memcpy(buf->data(), data, len); |
| 140 | 138 |
| 141 // Issue the write, which will complete immediately | 139 // Issue the write, which will complete immediately |
| 142 ASSERT_EQ(len, sock_->Write(buf, len, &write_callback_)); | 140 ASSERT_EQ(len, sock_->Write(buf, len, write_callback_.callback())); |
| 143 } | 141 } |
| 144 | 142 |
| 145 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, | 143 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, |
| 146 int len) { | 144 int len) { |
| 147 // Issue the read, which will be completed asynchronously | 145 // Issue the read, which will be completed asynchronously |
| 148 AssertWriteReturns(data, len, ERR_IO_PENDING); | 146 AssertWriteReturns(data, len, ERR_IO_PENDING); |
| 149 | 147 |
| 150 EXPECT_FALSE(read_callback_.have_result()); | 148 EXPECT_FALSE(read_callback_.have_result()); |
| 151 EXPECT_TRUE(sock_->IsConnected()); | 149 EXPECT_TRUE(sock_->IsConnected()); |
| 152 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 150 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
| 153 | 151 |
| 154 ASSERT_EQ(len, write_callback_.WaitForResult()); | 152 ASSERT_EQ(len, write_callback_.WaitForResult()); |
| 155 } | 153 } |
| 156 | 154 |
| 157 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, | 155 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, |
| 158 int len, int rv) { | 156 int len, int rv) { |
| 159 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 157 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
| 160 memcpy(buf->data(), data, len); | 158 memcpy(buf->data(), data, len); |
| 161 | 159 |
| 162 // Issue the read, which will complete asynchronously | 160 // Issue the read, which will complete asynchronously |
| 163 ASSERT_EQ(rv, sock_->Write(buf, len, &write_callback_)); | 161 ASSERT_EQ(rv, sock_->Write(buf, len, write_callback_.callback())); |
| 164 } | 162 } |
| 165 | 163 |
| 166 // ----------- Read | 164 // ----------- Read |
| 167 | 165 |
| 168 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { | 166 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { |
| 169 MockRead reads[] = { | 167 MockRead reads[] = { |
| 170 MockRead(false, kMsg1, kLen1, 0), // Sync Read | 168 MockRead(false, kMsg1, kLen1, 0), // Sync Read |
| 171 MockRead(false, 0, 1), // EOF | 169 MockRead(false, 0, 1), // EOF |
| 172 }; | 170 }; |
| 173 | 171 |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 // Issue the writes which will complete immediately | 523 // Issue the writes which will complete immediately |
| 526 data_->StopAfter(1); | 524 data_->StopAfter(1); |
| 527 AssertSyncWriteEquals(kMsg3, kLen3); | 525 AssertSyncWriteEquals(kMsg3, kLen3); |
| 528 | 526 |
| 529 data_->RunFor(1); | 527 data_->RunFor(1); |
| 530 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); | 528 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); |
| 531 AssertReadBufferEquals(kMsg2, kLen2); | 529 AssertReadBufferEquals(kMsg2, kLen2); |
| 532 } | 530 } |
| 533 | 531 |
| 534 } // namespace net | 532 } // namespace net |
| OLD | NEW |