1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/tcp_socket.h"
#include <string.h>
#include <string>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/tcp_client_socket.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace net {
namespace {
const int kListenBacklog = 5;
class TCPSocketTest : public PlatformTest {
protected:
TCPSocketTest() : socket_(NULL, NetLog::Source()) {
}
void SetUpListenIPv4() {
IPEndPoint address;
ParseAddress("127.0.0.1", 0, &address);
ASSERT_EQ(OK, socket_.Open(ADDRESS_FAMILY_IPV4));
ASSERT_EQ(OK, socket_.Bind(address));
ASSERT_EQ(OK, socket_.Listen(kListenBacklog));
ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
}
void SetUpListenIPv6(bool* success) {
*success = false;
IPEndPoint address;
ParseAddress("::1", 0, &address);
if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK ||
socket_.Bind(address) != OK ||
socket_.Listen(kListenBacklog) != OK) {
LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is "
"disabled. Skipping the test";
return;
}
ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
*success = true;
}
void ParseAddress(const std::string& ip_str, int port, IPEndPoint* address) {
IPAddressNumber ip_number;
bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
if (!rv)
return;
*address = IPEndPoint(ip_number, port);
}
void TestAcceptAsync() {
TestCompletionCallback accept_callback;
scoped_ptr<TCPSocket> accepted_socket;
IPEndPoint accepted_address;
ASSERT_EQ(ERR_IO_PENDING,
socket_.Accept(&accepted_socket, &accepted_address,
accept_callback.callback()));
TestCompletionCallback connect_callback;
TCPClientSocket connecting_socket(local_address_list(),
NULL, NetLog::Source());
connecting_socket.Connect(connect_callback.callback());
EXPECT_EQ(OK, connect_callback.WaitForResult());
EXPECT_EQ(OK, accept_callback.WaitForResult());
EXPECT_TRUE(accepted_socket.get());
// Both sockets should be on the loopback network interface.
EXPECT_EQ(accepted_address.address(), local_address_.address());
}
AddressList local_address_list() const {
return AddressList(local_address_);
}
TCPSocket socket_;
IPEndPoint local_address_;
};
// Test listening and accepting with a socket bound to an IPv4 address.
TEST_F(TCPSocketTest, Accept) {
ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
TestCompletionCallback connect_callback;
// TODO(yzshen): Switch to use TCPSocket when it supports client socket
// operations.
TCPClientSocket connecting_socket(local_address_list(),
NULL, NetLog::Source());
connecting_socket.Connect(connect_callback.callback());
TestCompletionCallback accept_callback;
scoped_ptr<TCPSocket> accepted_socket;
IPEndPoint accepted_address;
int result = socket_.Accept(&accepted_socket, &accepted_address,
accept_callback.callback());
if (result == ERR_IO_PENDING)
result = accept_callback.WaitForResult();
ASSERT_EQ(OK, result);
EXPECT_TRUE(accepted_socket.get());
// Both sockets should be on the loopback network interface.
EXPECT_EQ(accepted_address.address(), local_address_.address());
EXPECT_EQ(OK, connect_callback.WaitForResult());
}
// Test Accept() callback.
TEST_F(TCPSocketTest, AcceptAsync) {
ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
TestAcceptAsync();
}
#if defined(OS_WIN)
// Test Accept() for AdoptListenSocket.
TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) {
// Create a socket to be used with AdoptListenSocket.
SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ASSERT_EQ(OK, socket_.AdoptListenSocket(existing_socket));
IPEndPoint address;
ParseAddress("127.0.0.1", 0, &address);
SockaddrStorage storage;
ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len));
ASSERT_EQ(OK, socket_.Listen(kListenBacklog));
ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
TestAcceptAsync();
}
#endif
// Accept two connections simultaneously.
TEST_F(TCPSocketTest, Accept2Connections) {
ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
TestCompletionCallback accept_callback;
scoped_ptr<TCPSocket> accepted_socket;
IPEndPoint accepted_address;
ASSERT_EQ(ERR_IO_PENDING,
socket_.Accept(&accepted_socket, &accepted_address,
accept_callback.callback()));
TestCompletionCallback connect_callback;
TCPClientSocket connecting_socket(local_address_list(),
NULL, NetLog::Source());
connecting_socket.Connect(connect_callback.callback());
TestCompletionCallback connect_callback2;
TCPClientSocket connecting_socket2(local_address_list(),
NULL, NetLog::Source());
connecting_socket2.Connect(connect_callback2.callback());
EXPECT_EQ(OK, accept_callback.WaitForResult());
TestCompletionCallback accept_callback2;
scoped_ptr<TCPSocket> accepted_socket2;
IPEndPoint accepted_address2;
int result = socket_.Accept(&accepted_socket2, &accepted_address2,
accept_callback2.callback());
if (result == ERR_IO_PENDING)
result = accept_callback2.WaitForResult();
ASSERT_EQ(OK, result);
EXPECT_EQ(OK, connect_callback.WaitForResult());
EXPECT_EQ(OK, connect_callback2.WaitForResult());
EXPECT_TRUE(accepted_socket.get());
EXPECT_TRUE(accepted_socket2.get());
EXPECT_NE(accepted_socket.get(), accepted_socket2.get());
EXPECT_EQ(accepted_address.address(), local_address_.address());
EXPECT_EQ(accepted_address2.address(), local_address_.address());
}
// Test listening and accepting with a socket bound to an IPv6 address.
TEST_F(TCPSocketTest, AcceptIPv6) {
bool initialized = false;
ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized));
if (!initialized)
return;
TestCompletionCallback connect_callback;
TCPClientSocket connecting_socket(local_address_list(),
NULL, NetLog::Source());
connecting_socket.Connect(connect_callback.callback());
TestCompletionCallback accept_callback;
scoped_ptr<TCPSocket> accepted_socket;
IPEndPoint accepted_address;
int result = socket_.Accept(&accepted_socket, &accepted_address,
accept_callback.callback());
if (result == ERR_IO_PENDING)
result = accept_callback.WaitForResult();
ASSERT_EQ(OK, result);
EXPECT_TRUE(accepted_socket.get());
// Both sockets should be on the loopback network interface.
EXPECT_EQ(accepted_address.address(), local_address_.address());
EXPECT_EQ(OK, connect_callback.WaitForResult());
}
TEST_F(TCPSocketTest, ReadWrite) {
ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
TestCompletionCallback connect_callback;
TCPSocket connecting_socket(NULL, NetLog::Source());
int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
ASSERT_EQ(OK, result);
connecting_socket.Connect(local_address_, connect_callback.callback());
TestCompletionCallback accept_callback;
scoped_ptr<TCPSocket> accepted_socket;
IPEndPoint accepted_address;
result = socket_.Accept(&accepted_socket, &accepted_address,
accept_callback.callback());
ASSERT_EQ(OK, accept_callback.GetResult(result));
ASSERT_TRUE(accepted_socket.get());
// Both sockets should be on the loopback network interface.
EXPECT_EQ(accepted_address.address(), local_address_.address());
EXPECT_EQ(OK, connect_callback.WaitForResult());
const std::string message("test message");
std::vector<char> buffer(message.size());
size_t bytes_written = 0;
while (bytes_written < message.size()) {
scoped_refptr<IOBufferWithSize> write_buffer(
new IOBufferWithSize(message.size() - bytes_written));
memmove(write_buffer->data(), message.data() + bytes_written,
message.size() - bytes_written);
TestCompletionCallback write_callback;
int write_result = accepted_socket->Write(
write_buffer.get(), write_buffer->size(), write_callback.callback());
write_result = write_callback.GetResult(write_result);
ASSERT_TRUE(write_result >= 0);
bytes_written += write_result;
ASSERT_TRUE(bytes_written <= message.size());
}
size_t bytes_read = 0;
while (bytes_read < message.size()) {
scoped_refptr<IOBufferWithSize> read_buffer(
new IOBufferWithSize(message.size() - bytes_read));
TestCompletionCallback read_callback;
int read_result = connecting_socket.Read(
read_buffer.get(), read_buffer->size(), read_callback.callback());
read_result = read_callback.GetResult(read_result);
ASSERT_TRUE(read_result >= 0);
ASSERT_TRUE(bytes_read + read_result <= message.size());
memmove(&buffer[bytes_read], read_buffer->data(), read_result);
bytes_read += read_result;
}
std::string received_message(buffer.begin(), buffer.end());
ASSERT_EQ(message, received_message);
}
} // namespace
} // namespace net
|