OLD | NEW |
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> | 7 #include <string.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } | 57 } |
58 | 58 |
59 void ParseAddress(const std::string& ip_str, int port, IPEndPoint* address) { | 59 void ParseAddress(const std::string& ip_str, int port, IPEndPoint* address) { |
60 IPAddressNumber ip_number; | 60 IPAddressNumber ip_number; |
61 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | 61 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
62 if (!rv) | 62 if (!rv) |
63 return; | 63 return; |
64 *address = IPEndPoint(ip_number, port); | 64 *address = IPEndPoint(ip_number, port); |
65 } | 65 } |
66 | 66 |
| 67 void TestAcceptAsync() { |
| 68 TestCompletionCallback accept_callback; |
| 69 scoped_ptr<TCPSocket> accepted_socket; |
| 70 IPEndPoint accepted_address; |
| 71 ASSERT_EQ(ERR_IO_PENDING, |
| 72 socket_.Accept(&accepted_socket, &accepted_address, |
| 73 accept_callback.callback())); |
| 74 |
| 75 TestCompletionCallback connect_callback; |
| 76 TCPClientSocket connecting_socket(local_address_list(), |
| 77 NULL, NetLog::Source()); |
| 78 connecting_socket.Connect(connect_callback.callback()); |
| 79 |
| 80 EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 81 EXPECT_EQ(OK, accept_callback.WaitForResult()); |
| 82 |
| 83 EXPECT_TRUE(accepted_socket.get()); |
| 84 |
| 85 // Both sockets should be on the loopback network interface. |
| 86 EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 87 } |
| 88 |
67 AddressList local_address_list() const { | 89 AddressList local_address_list() const { |
68 return AddressList(local_address_); | 90 return AddressList(local_address_); |
69 } | 91 } |
70 | 92 |
71 TCPSocket socket_; | 93 TCPSocket socket_; |
72 IPEndPoint local_address_; | 94 IPEndPoint local_address_; |
73 }; | 95 }; |
74 | 96 |
75 // Test listening and accepting with a socket bound to an IPv4 address. | 97 // Test listening and accepting with a socket bound to an IPv4 address. |
76 TEST_F(TCPSocketTest, Accept) { | 98 TEST_F(TCPSocketTest, Accept) { |
(...skipping 19 matching lines...) Expand all Loading... |
96 | 118 |
97 // Both sockets should be on the loopback network interface. | 119 // Both sockets should be on the loopback network interface. |
98 EXPECT_EQ(accepted_address.address(), local_address_.address()); | 120 EXPECT_EQ(accepted_address.address(), local_address_.address()); |
99 | 121 |
100 EXPECT_EQ(OK, connect_callback.WaitForResult()); | 122 EXPECT_EQ(OK, connect_callback.WaitForResult()); |
101 } | 123 } |
102 | 124 |
103 // Test Accept() callback. | 125 // Test Accept() callback. |
104 TEST_F(TCPSocketTest, AcceptAsync) { | 126 TEST_F(TCPSocketTest, AcceptAsync) { |
105 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); | 127 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 128 TestAcceptAsync(); |
| 129 } |
106 | 130 |
107 TestCompletionCallback accept_callback; | 131 #if defined(OS_WIN) |
108 scoped_ptr<TCPSocket> accepted_socket; | 132 // Test Accept() for AdoptListenSocket. |
109 IPEndPoint accepted_address; | 133 TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) { |
110 ASSERT_EQ(ERR_IO_PENDING, | 134 // Create a socket to be used with AdoptListenSocket. |
111 socket_.Accept(&accepted_socket, &accepted_address, | 135 SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
112 accept_callback.callback())); | 136 ASSERT_EQ(OK, socket_.AdoptListenSocket(existing_socket)); |
113 | 137 |
114 TestCompletionCallback connect_callback; | 138 IPEndPoint address; |
115 TCPClientSocket connecting_socket(local_address_list(), | 139 ParseAddress("127.0.0.1", 0, &address); |
116 NULL, NetLog::Source()); | 140 SockaddrStorage storage; |
117 connecting_socket.Connect(connect_callback.callback()); | 141 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); |
| 142 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); |
118 | 143 |
119 EXPECT_EQ(OK, connect_callback.WaitForResult()); | 144 ASSERT_EQ(OK, socket_.Listen(kListenBacklog)); |
120 EXPECT_EQ(OK, accept_callback.WaitForResult()); | 145 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_)); |
121 | 146 |
122 EXPECT_TRUE(accepted_socket.get()); | 147 TestAcceptAsync(); |
123 | |
124 // Both sockets should be on the loopback network interface. | |
125 EXPECT_EQ(accepted_address.address(), local_address_.address()); | |
126 } | 148 } |
| 149 #endif |
127 | 150 |
128 // Accept two connections simultaneously. | 151 // Accept two connections simultaneously. |
129 TEST_F(TCPSocketTest, Accept2Connections) { | 152 TEST_F(TCPSocketTest, Accept2Connections) { |
130 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); | 153 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
131 | 154 |
132 TestCompletionCallback accept_callback; | 155 TestCompletionCallback accept_callback; |
133 scoped_ptr<TCPSocket> accepted_socket; | 156 scoped_ptr<TCPSocket> accepted_socket; |
134 IPEndPoint accepted_address; | 157 IPEndPoint accepted_address; |
135 | 158 |
136 ASSERT_EQ(ERR_IO_PENDING, | 159 ASSERT_EQ(ERR_IO_PENDING, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 memmove(&buffer[bytes_read], read_buffer->data(), read_result); | 277 memmove(&buffer[bytes_read], read_buffer->data(), read_result); |
255 bytes_read += read_result; | 278 bytes_read += read_result; |
256 } | 279 } |
257 | 280 |
258 std::string received_message(buffer.begin(), buffer.end()); | 281 std::string received_message(buffer.begin(), buffer.end()); |
259 ASSERT_EQ(message, received_message); | 282 ASSERT_EQ(message, received_message); |
260 } | 283 } |
261 | 284 |
262 } // namespace | 285 } // namespace |
263 } // namespace net | 286 } // namespace net |
OLD | NEW |