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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 121 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
122 | 122 |
123 // Now the read should complete | 123 // Now the read should complete |
124 ASSERT_EQ(len, read_callback_.WaitForResult()); | 124 ASSERT_EQ(len, read_callback_.WaitForResult()); |
125 AssertReadBufferEquals(data, len); | 125 AssertReadBufferEquals(data, len); |
126 } | 126 } |
127 | 127 |
128 void DeterministicSocketDataTest::AssertReadReturns(const char* data, | 128 void DeterministicSocketDataTest::AssertReadReturns(const char* data, |
129 int len, int rv) { | 129 int len, int rv) { |
130 read_buf_ = new IOBuffer(len); | 130 read_buf_ = new IOBuffer(len); |
131 ASSERT_EQ(rv, sock_->Read(read_buf_, len, read_callback_.callback())); | 131 ASSERT_EQ(rv, sock_->Read(read_buf_.get(), len, read_callback_.callback())); |
132 } | 132 } |
133 | 133 |
134 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, | 134 void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, |
135 int len) { | 135 int len) { |
136 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); | 136 ASSERT_EQ(std::string(data, len), std::string(read_buf_->data(), len)); |
137 } | 137 } |
138 | 138 |
139 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, | 139 void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, |
140 int len) { | 140 int len) { |
141 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 141 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
142 memcpy(buf->data(), data, len); | 142 memcpy(buf->data(), data, len); |
143 | 143 |
144 // Issue the write, which will complete immediately | 144 // Issue the write, which will complete immediately |
145 ASSERT_EQ(len, sock_->Write(buf, len, write_callback_.callback())); | 145 ASSERT_EQ(len, sock_->Write(buf.get(), len, write_callback_.callback())); |
146 } | 146 } |
147 | 147 |
148 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, | 148 void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, |
149 int len) { | 149 int len) { |
150 // Issue the read, which will be completed asynchronously | 150 // Issue the read, which will be completed asynchronously |
151 AssertWriteReturns(data, len, ERR_IO_PENDING); | 151 AssertWriteReturns(data, len, ERR_IO_PENDING); |
152 | 152 |
153 EXPECT_FALSE(read_callback_.have_result()); | 153 EXPECT_FALSE(read_callback_.have_result()); |
154 EXPECT_TRUE(sock_->IsConnected()); | 154 EXPECT_TRUE(sock_->IsConnected()); |
155 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked | 155 data_->RunFor(1); // Runs 1 step, to cause the callbacks to be invoked |
156 | 156 |
157 ASSERT_EQ(len, write_callback_.WaitForResult()); | 157 ASSERT_EQ(len, write_callback_.WaitForResult()); |
158 } | 158 } |
159 | 159 |
160 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, | 160 void DeterministicSocketDataTest::AssertWriteReturns(const char* data, |
161 int len, int rv) { | 161 int len, int rv) { |
162 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 162 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
163 memcpy(buf->data(), data, len); | 163 memcpy(buf->data(), data, len); |
164 | 164 |
165 // Issue the read, which will complete asynchronously | 165 // Issue the read, which will complete asynchronously |
166 ASSERT_EQ(rv, sock_->Write(buf, len, write_callback_.callback())); | 166 ASSERT_EQ(rv, sock_->Write(buf.get(), len, write_callback_.callback())); |
167 } | 167 } |
168 | 168 |
169 // ----------- Read | 169 // ----------- Read |
170 | 170 |
171 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { | 171 TEST_F(DeterministicSocketDataTest, SingleSyncReadWhileStopped) { |
172 MockRead reads[] = { | 172 MockRead reads[] = { |
173 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read | 173 MockRead(SYNCHRONOUS, kMsg1, kLen1, 0), // Sync Read |
174 MockRead(SYNCHRONOUS, 0, 1), // EOF | 174 MockRead(SYNCHRONOUS, 0, 1), // EOF |
175 }; | 175 }; |
176 | 176 |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 // Issue the writes which will complete immediately | 536 // Issue the writes which will complete immediately |
537 data_->StopAfter(1); | 537 data_->StopAfter(1); |
538 AssertSyncWriteEquals(kMsg3, kLen3); | 538 AssertSyncWriteEquals(kMsg3, kLen3); |
539 | 539 |
540 data_->RunFor(1); | 540 data_->RunFor(1); |
541 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); | 541 ASSERT_EQ(kLen2, read_callback_.WaitForResult()); |
542 AssertReadBufferEquals(kMsg2, kLen2); | 542 AssertReadBufferEquals(kMsg2, kLen2); |
543 } | 543 } |
544 | 544 |
545 } // namespace net | 545 } // namespace net |
OLD | NEW |