Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: net/socket/tcp_socket_unittest.cc

Issue 23881002: Windows only: Move client socket functionality from TCPClientSocket into TCPSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/tcp_socket.h" 5 #include "net/socket/tcp_socket.h"
6 6
7 #include <string.h>
8
7 #include <string> 9 #include <string>
10 #include <vector>
8 11
12 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "build/build_config.h"
10 #include "net/base/address_list.h" 15 #include "net/base/address_list.h"
16 #include "net/base/io_buffer.h"
11 #include "net/base/ip_endpoint.h" 17 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h" 19 #include "net/base/test_completion_callback.h"
14 #include "net/socket/tcp_client_socket.h" 20 #include "net/socket/tcp_client_socket.h"
15 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h" 22 #include "testing/platform_test.h"
17 23
18 namespace net { 24 namespace net {
19 25
20 namespace { 26 namespace {
21 const int kListenBacklog = 5; 27 const int kListenBacklog = 5;
22 28
23 class TCPSocketTest : public PlatformTest { 29 class TCPSocketTest : public PlatformTest {
24 protected: 30 protected:
25 TCPSocketTest() : socket_(NULL, NetLog::Source()) { 31 TCPSocketTest() : socket_(NULL, NetLog::Source()) {
26 } 32 }
27 33
28 void SetUpListenIPv4() { 34 void SetUpListenIPv4() {
29 IPEndPoint address; 35 IPEndPoint address;
30 ParseAddress("127.0.0.1", 0, &address); 36 ParseAddress("127.0.0.1", 0, &address);
31 37
32 ASSERT_EQ(OK, socket_.Create(ADDRESS_FAMILY_IPV4)); 38 ASSERT_EQ(OK, socket_.Open(ADDRESS_FAMILY_IPV4));
33 ASSERT_EQ(OK, socket_.Bind(address)); 39 ASSERT_EQ(OK, socket_.Bind(address));
34 ASSERT_EQ(OK, socket_.Listen(kListenBacklog)); 40 ASSERT_EQ(OK, socket_.Listen(kListenBacklog));
35 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_)); 41 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
36 } 42 }
37 43
38 void SetUpListenIPv6(bool* success) { 44 void SetUpListenIPv6(bool* success) {
39 *success = false; 45 *success = false;
40 IPEndPoint address; 46 IPEndPoint address;
41 ParseAddress("::1", 0, &address); 47 ParseAddress("::1", 0, &address);
42 48
43 if (socket_.Create(ADDRESS_FAMILY_IPV6) != OK || 49 if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK ||
44 socket_.Bind(address) != OK || 50 socket_.Bind(address) != OK ||
45 socket_.Listen(kListenBacklog) != OK) { 51 socket_.Listen(kListenBacklog) != OK) {
46 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is " 52 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is "
47 "disabled. Skipping the test"; 53 "disabled. Skipping the test";
48 return; 54 return;
49 } 55 }
50 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_)); 56 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
51 *success = true; 57 *success = true;
52 } 58 }
53 59
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 ASSERT_EQ(OK, result); 193 ASSERT_EQ(OK, result);
188 194
189 EXPECT_TRUE(accepted_socket.get()); 195 EXPECT_TRUE(accepted_socket.get());
190 196
191 // Both sockets should be on the loopback network interface. 197 // Both sockets should be on the loopback network interface.
192 EXPECT_EQ(accepted_address.address(), local_address_.address()); 198 EXPECT_EQ(accepted_address.address(), local_address_.address());
193 199
194 EXPECT_EQ(OK, connect_callback.WaitForResult()); 200 EXPECT_EQ(OK, connect_callback.WaitForResult());
195 } 201 }
196 202
203 // TODO(yzshen): Enable it for other platforms once TCPSocketLibevent supports
204 // client socket operations.
205 #if defined(OS_WIN)
206
207 TEST_F(TCPSocketTest, ReadWrite) {
208 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
209
210 TestCompletionCallback connect_callback;
211 TCPSocket connecting_socket(NULL, NetLog::Source());
212 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
213 ASSERT_EQ(OK, result);
214 connecting_socket.Connect(local_address_, connect_callback.callback());
215
216 TestCompletionCallback accept_callback;
217 scoped_ptr<TCPSocket> accepted_socket;
218 IPEndPoint accepted_address;
219 result = socket_.Accept(&accepted_socket, &accepted_address,
220 accept_callback.callback());
221 ASSERT_EQ(OK, accept_callback.GetResult(result));
222
223 ASSERT_TRUE(accepted_socket.get());
224
225 // Both sockets should be on the loopback network interface.
226 EXPECT_EQ(accepted_address.address(), local_address_.address());
227
228 EXPECT_EQ(OK, connect_callback.WaitForResult());
229
230 const std::string message("test message");
231 std::vector<char> buffer(message.size());
232
233 size_t bytes_written = 0;
234 while (bytes_written < message.size()) {
235 scoped_refptr<IOBufferWithSize> write_buffer(
236 new IOBufferWithSize(message.size() - bytes_written));
237 memmove(write_buffer->data(), message.data() + bytes_written,
238 message.size() - bytes_written);
239
240 TestCompletionCallback write_callback;
241 int write_result = accepted_socket->Write(
242 write_buffer.get(), write_buffer->size(), write_callback.callback());
243 write_result = write_callback.GetResult(write_result);
244 ASSERT_TRUE(write_result >= 0);
245 bytes_written += write_result;
246 ASSERT_TRUE(bytes_written <= message.size());
247 }
248
249 size_t bytes_read = 0;
250 while (bytes_read < message.size()) {
251 scoped_refptr<IOBufferWithSize> read_buffer(
252 new IOBufferWithSize(message.size() - bytes_read));
253 TestCompletionCallback read_callback;
254 int read_result = connecting_socket.Read(
255 read_buffer.get(), read_buffer->size(), read_callback.callback());
256 read_result = read_callback.GetResult(read_result);
257 ASSERT_TRUE(read_result >= 0);
258 ASSERT_TRUE(bytes_read + read_result <= message.size());
259 memmove(&buffer[bytes_read], read_buffer->data(), read_result);
260 bytes_read += read_result;
261 }
262
263 std::string received_message(buffer.begin(), buffer.end());
264 ASSERT_EQ(message, received_message);
265 }
266
267 #endif
268
197 } // namespace 269 } // namespace
198 } // namespace net 270 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698