Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / socket / tcp_socket_unittest.cc
blob374621e8020f32388fdf053abfc307805cd159fb
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
3 // found in the LICENSE file.
5 #include "net/socket/tcp_socket.h"
7 #include <string.h>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/socket/tcp_client_socket.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/platform_test.h"
25 namespace net {
27 namespace {
28 const int kListenBacklog = 5;
30 class TCPSocketTest : public PlatformTest {
31 protected:
32 TCPSocketTest() : socket_(NULL, NetLog::Source()) {
35 void SetUpListenIPv4() {
36 IPEndPoint address;
37 ParseAddress("127.0.0.1", 0, &address);
39 ASSERT_EQ(OK, socket_.Open(ADDRESS_FAMILY_IPV4));
40 ASSERT_EQ(OK, socket_.Bind(address));
41 ASSERT_EQ(OK, socket_.Listen(kListenBacklog));
42 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
45 void SetUpListenIPv6(bool* success) {
46 *success = false;
47 IPEndPoint address;
48 ParseAddress("::1", 0, &address);
50 if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK ||
51 socket_.Bind(address) != OK ||
52 socket_.Listen(kListenBacklog) != OK) {
53 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is "
54 "disabled. Skipping the test";
55 return;
57 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
58 *success = true;
61 void ParseAddress(const std::string& ip_str,
62 uint16 port,
63 IPEndPoint* address) {
64 IPAddressNumber ip_number;
65 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number);
66 if (!rv)
67 return;
68 *address = IPEndPoint(ip_number, port);
71 void TestAcceptAsync() {
72 TestCompletionCallback accept_callback;
73 scoped_ptr<TCPSocket> accepted_socket;
74 IPEndPoint accepted_address;
75 ASSERT_EQ(ERR_IO_PENDING,
76 socket_.Accept(&accepted_socket, &accepted_address,
77 accept_callback.callback()));
79 TestCompletionCallback connect_callback;
80 TCPClientSocket connecting_socket(local_address_list(),
81 NULL, NetLog::Source());
82 connecting_socket.Connect(connect_callback.callback());
84 EXPECT_EQ(OK, connect_callback.WaitForResult());
85 EXPECT_EQ(OK, accept_callback.WaitForResult());
87 EXPECT_TRUE(accepted_socket.get());
89 // Both sockets should be on the loopback network interface.
90 EXPECT_EQ(accepted_address.address(), local_address_.address());
93 AddressList local_address_list() const {
94 return AddressList(local_address_);
97 TCPSocket socket_;
98 IPEndPoint local_address_;
101 // Test listening and accepting with a socket bound to an IPv4 address.
102 TEST_F(TCPSocketTest, Accept) {
103 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
105 TestCompletionCallback connect_callback;
106 // TODO(yzshen): Switch to use TCPSocket when it supports client socket
107 // operations.
108 TCPClientSocket connecting_socket(local_address_list(),
109 NULL, NetLog::Source());
110 connecting_socket.Connect(connect_callback.callback());
112 TestCompletionCallback accept_callback;
113 scoped_ptr<TCPSocket> accepted_socket;
114 IPEndPoint accepted_address;
115 int result = socket_.Accept(&accepted_socket, &accepted_address,
116 accept_callback.callback());
117 if (result == ERR_IO_PENDING)
118 result = accept_callback.WaitForResult();
119 ASSERT_EQ(OK, result);
121 EXPECT_TRUE(accepted_socket.get());
123 // Both sockets should be on the loopback network interface.
124 EXPECT_EQ(accepted_address.address(), local_address_.address());
126 EXPECT_EQ(OK, connect_callback.WaitForResult());
129 // Test Accept() callback.
130 TEST_F(TCPSocketTest, AcceptAsync) {
131 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
132 TestAcceptAsync();
135 #if defined(OS_WIN)
136 // Test Accept() for AdoptListenSocket.
137 TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) {
138 // Create a socket to be used with AdoptListenSocket.
139 SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
140 ASSERT_EQ(OK, socket_.AdoptListenSocket(existing_socket));
142 IPEndPoint address;
143 ParseAddress("127.0.0.1", 0, &address);
144 SockaddrStorage storage;
145 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
146 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len));
148 ASSERT_EQ(OK, socket_.Listen(kListenBacklog));
149 ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_));
151 TestAcceptAsync();
153 #endif
155 // Accept two connections simultaneously.
156 TEST_F(TCPSocketTest, Accept2Connections) {
157 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
159 TestCompletionCallback accept_callback;
160 scoped_ptr<TCPSocket> accepted_socket;
161 IPEndPoint accepted_address;
163 ASSERT_EQ(ERR_IO_PENDING,
164 socket_.Accept(&accepted_socket, &accepted_address,
165 accept_callback.callback()));
167 TestCompletionCallback connect_callback;
168 TCPClientSocket connecting_socket(local_address_list(),
169 NULL, NetLog::Source());
170 connecting_socket.Connect(connect_callback.callback());
172 TestCompletionCallback connect_callback2;
173 TCPClientSocket connecting_socket2(local_address_list(),
174 NULL, NetLog::Source());
175 connecting_socket2.Connect(connect_callback2.callback());
177 EXPECT_EQ(OK, accept_callback.WaitForResult());
179 TestCompletionCallback accept_callback2;
180 scoped_ptr<TCPSocket> accepted_socket2;
181 IPEndPoint accepted_address2;
183 int result = socket_.Accept(&accepted_socket2, &accepted_address2,
184 accept_callback2.callback());
185 if (result == ERR_IO_PENDING)
186 result = accept_callback2.WaitForResult();
187 ASSERT_EQ(OK, result);
189 EXPECT_EQ(OK, connect_callback.WaitForResult());
190 EXPECT_EQ(OK, connect_callback2.WaitForResult());
192 EXPECT_TRUE(accepted_socket.get());
193 EXPECT_TRUE(accepted_socket2.get());
194 EXPECT_NE(accepted_socket.get(), accepted_socket2.get());
196 EXPECT_EQ(accepted_address.address(), local_address_.address());
197 EXPECT_EQ(accepted_address2.address(), local_address_.address());
200 // Test listening and accepting with a socket bound to an IPv6 address.
201 TEST_F(TCPSocketTest, AcceptIPv6) {
202 bool initialized = false;
203 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized));
204 if (!initialized)
205 return;
207 TestCompletionCallback connect_callback;
208 TCPClientSocket connecting_socket(local_address_list(),
209 NULL, NetLog::Source());
210 connecting_socket.Connect(connect_callback.callback());
212 TestCompletionCallback accept_callback;
213 scoped_ptr<TCPSocket> accepted_socket;
214 IPEndPoint accepted_address;
215 int result = socket_.Accept(&accepted_socket, &accepted_address,
216 accept_callback.callback());
217 if (result == ERR_IO_PENDING)
218 result = accept_callback.WaitForResult();
219 ASSERT_EQ(OK, result);
221 EXPECT_TRUE(accepted_socket.get());
223 // Both sockets should be on the loopback network interface.
224 EXPECT_EQ(accepted_address.address(), local_address_.address());
226 EXPECT_EQ(OK, connect_callback.WaitForResult());
229 TEST_F(TCPSocketTest, ReadWrite) {
230 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
232 TestCompletionCallback connect_callback;
233 TCPSocket connecting_socket(NULL, NetLog::Source());
234 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
235 ASSERT_EQ(OK, result);
236 connecting_socket.Connect(local_address_, connect_callback.callback());
238 TestCompletionCallback accept_callback;
239 scoped_ptr<TCPSocket> accepted_socket;
240 IPEndPoint accepted_address;
241 result = socket_.Accept(&accepted_socket, &accepted_address,
242 accept_callback.callback());
243 ASSERT_EQ(OK, accept_callback.GetResult(result));
245 ASSERT_TRUE(accepted_socket.get());
247 // Both sockets should be on the loopback network interface.
248 EXPECT_EQ(accepted_address.address(), local_address_.address());
250 EXPECT_EQ(OK, connect_callback.WaitForResult());
252 const std::string message("test message");
253 std::vector<char> buffer(message.size());
255 size_t bytes_written = 0;
256 while (bytes_written < message.size()) {
257 scoped_refptr<IOBufferWithSize> write_buffer(
258 new IOBufferWithSize(message.size() - bytes_written));
259 memmove(write_buffer->data(), message.data() + bytes_written,
260 message.size() - bytes_written);
262 TestCompletionCallback write_callback;
263 int write_result = accepted_socket->Write(
264 write_buffer.get(), write_buffer->size(), write_callback.callback());
265 write_result = write_callback.GetResult(write_result);
266 ASSERT_TRUE(write_result >= 0);
267 bytes_written += write_result;
268 ASSERT_TRUE(bytes_written <= message.size());
271 size_t bytes_read = 0;
272 while (bytes_read < message.size()) {
273 scoped_refptr<IOBufferWithSize> read_buffer(
274 new IOBufferWithSize(message.size() - bytes_read));
275 TestCompletionCallback read_callback;
276 int read_result = connecting_socket.Read(
277 read_buffer.get(), read_buffer->size(), read_callback.callback());
278 read_result = read_callback.GetResult(read_result);
279 ASSERT_TRUE(read_result >= 0);
280 ASSERT_TRUE(bytes_read + read_result <= message.size());
281 memmove(&buffer[bytes_read], read_buffer->data(), read_result);
282 bytes_read += read_result;
285 std::string received_message(buffer.begin(), buffer.end());
286 ASSERT_EQ(message, received_message);
289 } // namespace
290 } // namespace net