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

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

Issue 2333923004: Extracting NetLog inner classes into their own classes. (Closed)
Patch Set: Some nit fixes and better, impl-agnostic naming of net_log_parameters_callback_typedef.h -> net/log… Created 4 years, 2 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
OLDNEW
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/tcp_server_socket.h" 5 #include "net/socket/tcp_server_socket.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "net/base/address_list.h" 13 #include "net/base/address_list.h"
14 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
15 #include "net/base/ip_address.h" 15 #include "net/base/ip_address.h"
16 #include "net/base/ip_endpoint.h" 16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/base/test_completion_callback.h" 18 #include "net/base/test_completion_callback.h"
19 #include "net/log/net_log_source.h"
19 #include "net/socket/tcp_client_socket.h" 20 #include "net/socket/tcp_client_socket.h"
20 #include "net/test/gtest_util.h" 21 #include "net/test/gtest_util.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/platform_test.h" 24 #include "testing/platform_test.h"
24 25
25 using net::test::IsOk; 26 using net::test::IsOk;
26 27
27 namespace net { 28 namespace net {
28 29
29 namespace { 30 namespace {
30 const int kListenBacklog = 5; 31 const int kListenBacklog = 5;
31 32
32 class TCPServerSocketTest : public PlatformTest { 33 class TCPServerSocketTest : public PlatformTest {
33 protected: 34 protected:
34 TCPServerSocketTest() 35 TCPServerSocketTest() : socket_(NULL, NetLogSource()) {}
35 : socket_(NULL, NetLog::Source()) {
36 }
37 36
38 void SetUpIPv4() { 37 void SetUpIPv4() {
39 IPEndPoint address(IPAddress::IPv4Localhost(), 0); 38 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
40 ASSERT_THAT(socket_.Listen(address, kListenBacklog), IsOk()); 39 ASSERT_THAT(socket_.Listen(address, kListenBacklog), IsOk());
41 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); 40 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
42 } 41 }
43 42
44 void SetUpIPv6(bool* success) { 43 void SetUpIPv6(bool* success) {
45 *success = false; 44 *success = false;
46 IPEndPoint address(IPAddress::IPv6Localhost(), 0); 45 IPEndPoint address(IPAddress::IPv6Localhost(), 0);
(...skipping 18 matching lines...) Expand all
65 64
66 TCPServerSocket socket_; 65 TCPServerSocket socket_;
67 IPEndPoint local_address_; 66 IPEndPoint local_address_;
68 }; 67 };
69 68
70 TEST_F(TCPServerSocketTest, Accept) { 69 TEST_F(TCPServerSocketTest, Accept) {
71 ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); 70 ASSERT_NO_FATAL_FAILURE(SetUpIPv4());
72 71
73 TestCompletionCallback connect_callback; 72 TestCompletionCallback connect_callback;
74 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 73 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
75 NetLog::Source()); 74 NetLogSource());
76 connecting_socket.Connect(connect_callback.callback()); 75 connecting_socket.Connect(connect_callback.callback());
77 76
78 TestCompletionCallback accept_callback; 77 TestCompletionCallback accept_callback;
79 std::unique_ptr<StreamSocket> accepted_socket; 78 std::unique_ptr<StreamSocket> accepted_socket;
80 int result = socket_.Accept(&accepted_socket, accept_callback.callback()); 79 int result = socket_.Accept(&accepted_socket, accept_callback.callback());
81 if (result == ERR_IO_PENDING) 80 if (result == ERR_IO_PENDING)
82 result = accept_callback.WaitForResult(); 81 result = accept_callback.WaitForResult();
83 ASSERT_THAT(result, IsOk()); 82 ASSERT_THAT(result, IsOk());
84 83
85 ASSERT_TRUE(accepted_socket.get() != NULL); 84 ASSERT_TRUE(accepted_socket.get() != NULL);
(...skipping 10 matching lines...) Expand all
96 ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); 95 ASSERT_NO_FATAL_FAILURE(SetUpIPv4());
97 96
98 TestCompletionCallback accept_callback; 97 TestCompletionCallback accept_callback;
99 std::unique_ptr<StreamSocket> accepted_socket; 98 std::unique_ptr<StreamSocket> accepted_socket;
100 99
101 ASSERT_EQ(ERR_IO_PENDING, 100 ASSERT_EQ(ERR_IO_PENDING,
102 socket_.Accept(&accepted_socket, accept_callback.callback())); 101 socket_.Accept(&accepted_socket, accept_callback.callback()));
103 102
104 TestCompletionCallback connect_callback; 103 TestCompletionCallback connect_callback;
105 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 104 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
106 NetLog::Source()); 105 NetLogSource());
107 connecting_socket.Connect(connect_callback.callback()); 106 connecting_socket.Connect(connect_callback.callback());
108 107
109 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 108 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
110 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); 109 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
111 110
112 EXPECT_TRUE(accepted_socket != NULL); 111 EXPECT_TRUE(accepted_socket != NULL);
113 112
114 // Both sockets should be on the loopback network interface. 113 // Both sockets should be on the loopback network interface.
115 EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), 114 EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(),
116 local_address_.address()); 115 local_address_.address());
117 } 116 }
118 117
119 // Accept two connections simultaneously. 118 // Accept two connections simultaneously.
120 TEST_F(TCPServerSocketTest, Accept2Connections) { 119 TEST_F(TCPServerSocketTest, Accept2Connections) {
121 ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); 120 ASSERT_NO_FATAL_FAILURE(SetUpIPv4());
122 121
123 TestCompletionCallback accept_callback; 122 TestCompletionCallback accept_callback;
124 std::unique_ptr<StreamSocket> accepted_socket; 123 std::unique_ptr<StreamSocket> accepted_socket;
125 124
126 ASSERT_EQ(ERR_IO_PENDING, 125 ASSERT_EQ(ERR_IO_PENDING,
127 socket_.Accept(&accepted_socket, accept_callback.callback())); 126 socket_.Accept(&accepted_socket, accept_callback.callback()));
128 127
129 TestCompletionCallback connect_callback; 128 TestCompletionCallback connect_callback;
130 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 129 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
131 NetLog::Source()); 130 NetLogSource());
132 connecting_socket.Connect(connect_callback.callback()); 131 connecting_socket.Connect(connect_callback.callback());
133 132
134 TestCompletionCallback connect_callback2; 133 TestCompletionCallback connect_callback2;
135 TCPClientSocket connecting_socket2(local_address_list(), NULL, NULL, 134 TCPClientSocket connecting_socket2(local_address_list(), NULL, NULL,
136 NetLog::Source()); 135 NetLogSource());
137 connecting_socket2.Connect(connect_callback2.callback()); 136 connecting_socket2.Connect(connect_callback2.callback());
138 137
139 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); 138 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
140 139
141 TestCompletionCallback accept_callback2; 140 TestCompletionCallback accept_callback2;
142 std::unique_ptr<StreamSocket> accepted_socket2; 141 std::unique_ptr<StreamSocket> accepted_socket2;
143 int result = socket_.Accept(&accepted_socket2, accept_callback2.callback()); 142 int result = socket_.Accept(&accepted_socket2, accept_callback2.callback());
144 if (result == ERR_IO_PENDING) 143 if (result == ERR_IO_PENDING)
145 result = accept_callback2.WaitForResult(); 144 result = accept_callback2.WaitForResult();
146 ASSERT_THAT(result, IsOk()); 145 ASSERT_THAT(result, IsOk());
(...skipping 11 matching lines...) Expand all
158 } 157 }
159 158
160 TEST_F(TCPServerSocketTest, AcceptIPv6) { 159 TEST_F(TCPServerSocketTest, AcceptIPv6) {
161 bool initialized = false; 160 bool initialized = false;
162 ASSERT_NO_FATAL_FAILURE(SetUpIPv6(&initialized)); 161 ASSERT_NO_FATAL_FAILURE(SetUpIPv6(&initialized));
163 if (!initialized) 162 if (!initialized)
164 return; 163 return;
165 164
166 TestCompletionCallback connect_callback; 165 TestCompletionCallback connect_callback;
167 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 166 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
168 NetLog::Source()); 167 NetLogSource());
169 connecting_socket.Connect(connect_callback.callback()); 168 connecting_socket.Connect(connect_callback.callback());
170 169
171 TestCompletionCallback accept_callback; 170 TestCompletionCallback accept_callback;
172 std::unique_ptr<StreamSocket> accepted_socket; 171 std::unique_ptr<StreamSocket> accepted_socket;
173 int result = socket_.Accept(&accepted_socket, accept_callback.callback()); 172 int result = socket_.Accept(&accepted_socket, accept_callback.callback());
174 if (result == ERR_IO_PENDING) 173 if (result == ERR_IO_PENDING)
175 result = accept_callback.WaitForResult(); 174 result = accept_callback.WaitForResult();
176 ASSERT_THAT(result, IsOk()); 175 ASSERT_THAT(result, IsOk());
177 176
178 ASSERT_TRUE(accepted_socket.get() != NULL); 177 ASSERT_TRUE(accepted_socket.get() != NULL);
179 178
180 // Both sockets should be on the loopback network interface. 179 // Both sockets should be on the loopback network interface.
181 EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), 180 EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(),
182 local_address_.address()); 181 local_address_.address());
183 182
184 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 183 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
185 } 184 }
186 185
187 TEST_F(TCPServerSocketTest, AcceptIO) { 186 TEST_F(TCPServerSocketTest, AcceptIO) {
188 ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); 187 ASSERT_NO_FATAL_FAILURE(SetUpIPv4());
189 188
190 TestCompletionCallback connect_callback; 189 TestCompletionCallback connect_callback;
191 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 190 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
192 NetLog::Source()); 191 NetLogSource());
193 connecting_socket.Connect(connect_callback.callback()); 192 connecting_socket.Connect(connect_callback.callback());
194 193
195 TestCompletionCallback accept_callback; 194 TestCompletionCallback accept_callback;
196 std::unique_ptr<StreamSocket> accepted_socket; 195 std::unique_ptr<StreamSocket> accepted_socket;
197 int result = socket_.Accept(&accepted_socket, accept_callback.callback()); 196 int result = socket_.Accept(&accepted_socket, accept_callback.callback());
198 ASSERT_THAT(accept_callback.GetResult(result), IsOk()); 197 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
199 198
200 ASSERT_TRUE(accepted_socket.get() != NULL); 199 ASSERT_TRUE(accepted_socket.get() != NULL);
201 200
202 // Both sockets should be on the loopback network interface. 201 // Both sockets should be on the loopback network interface.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 bytes_read += read_result; 236 bytes_read += read_result;
238 } 237 }
239 238
240 std::string received_message(buffer.begin(), buffer.end()); 239 std::string received_message(buffer.begin(), buffer.end());
241 ASSERT_EQ(message, received_message); 240 ASSERT_EQ(message, received_message);
242 } 241 }
243 242
244 } // namespace 243 } // namespace
245 244
246 } // namespace net 245 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698