OLD | NEW |
---|---|
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 // This file contains some tests for TCPClientSocket. | 5 // This file contains some tests for TCPClientSocket. |
6 // transport_client_socket_unittest.cc contans some other tests that | 6 // transport_client_socket_unittest.cc contans some other tests that |
7 // are common for TCP and other types of sockets. | 7 // are common for TCP and other types of sockets. |
8 | 8 |
9 #include "net/socket/tcp_client_socket.h" | 9 #include "net/socket/tcp_client_socket.h" |
10 | 10 |
11 #include <string> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/bind.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/run_loop.h" | |
18 #include "net/base/address_list.h" | |
19 #include "net/base/io_buffer.h" | |
11 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
12 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
13 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
14 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
24 #include "net/dns/mock_host_resolver.h" | |
25 #include "net/log/net_log.h" | |
26 #include "net/log/net_log_unittest.h" | |
27 #include "net/socket/client_socket_factory.h" | |
15 #include "net/socket/tcp_server_socket.h" | 28 #include "net/socket/tcp_server_socket.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "testing/platform_test.h" | |
17 | 31 |
18 namespace net { | 32 namespace net { |
19 | 33 |
20 namespace { | 34 namespace { |
21 | 35 |
36 const char kServerReply[] = "HTTP/1.1 404 Not Found"; | |
37 | |
38 enum ClientSocketTestTypes { TCP, SCTP }; | |
39 | |
40 } // namespace net | |
41 | |
42 class TCPClientSocketTest | |
43 : public ::testing::TestWithParam<ClientSocketTestTypes> { | |
44 public: | |
45 TCPClientSocketTest() | |
46 : listen_port_(0), | |
47 socket_factory_(ClientSocketFactory::GetDefaultFactory()), | |
48 close_server_socket_on_next_send_(false) {} | |
49 | |
50 virtual ~TCPClientSocketTest() {} | |
51 | |
52 // Testcase hooks | |
53 void SetUp() override; | |
54 | |
55 void CloseServerSocket() { | |
56 // delete the connected_sock_, which will close it. | |
57 connected_sock_.reset(); | |
58 } | |
59 | |
60 void AcceptCallback(int res) { | |
61 ASSERT_EQ(OK, res); | |
62 connect_loop_.Quit(); | |
63 } | |
64 | |
65 int DrainClientSocket(IOBuffer* buf, | |
66 uint32 buf_len, | |
67 uint32 bytes_to_read, | |
68 TestCompletionCallback* callback); | |
69 | |
70 // Establishes a connection to the server. | |
71 void EstablishConnection(TestCompletionCallback* callback); | |
72 | |
73 // Sends a request from the client to the server socket. Makes the server read | |
74 // the request and send a response. | |
75 void SendRequestAndResponse(); | |
76 | |
77 // Makes |connected_sock_| to read |expected_bytes_read| bytes. Returns the | |
78 // the data read as a string. | |
79 std::string ReadServerData(int expected_bytes_read); | |
80 | |
81 // Sends server response. | |
82 void SendServerResponse(); | |
83 | |
84 void set_close_server_socket_on_next_send(bool close) { | |
85 close_server_socket_on_next_send_ = close; | |
86 } | |
87 | |
88 protected: | |
89 base::RunLoop connect_loop_; | |
90 uint16 listen_port_; | |
91 TestNetLog net_log_; | |
92 ClientSocketFactory* const socket_factory_; | |
93 scoped_ptr<StreamSocket> sock_; | |
94 scoped_ptr<StreamSocket> connected_sock_; | |
95 | |
96 private: | |
97 scoped_ptr<TCPServerSocket> listen_sock_; | |
98 bool close_server_socket_on_next_send_; | |
99 }; | |
100 | |
101 void TCPClientSocketTest::SetUp() { | |
102 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); | |
103 | |
104 // Open a server socket on an ephemeral port. | |
105 listen_sock_.reset(new TCPServerSocket(NULL, NetLog::Source())); | |
106 IPAddressNumber address; | |
107 ParseIPLiteralToNumber("127.0.0.1", &address); | |
108 IPEndPoint local_address(address, 0); | |
109 ASSERT_EQ(OK, listen_sock_->Listen(local_address, 1)); | |
110 // Get the server's address (including the actual port number). | |
111 ASSERT_EQ(OK, listen_sock_->GetLocalAddress(&local_address)); | |
112 listen_port_ = local_address.port(); | |
113 listen_sock_->Accept( | |
114 &connected_sock_, | |
115 base::Bind(&TCPClientSocketTest::AcceptCallback, base::Unretained(this))); | |
116 | |
117 AddressList addr; | |
118 // MockHostResolver resolves everything to 127.0.0.1. | |
119 scoped_ptr<HostResolver> resolver(new MockHostResolver()); | |
120 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); | |
121 TestCompletionCallback callback; | |
122 int rv = resolver->Resolve(info, DEFAULT_PRIORITY, &addr, callback.callback(), | |
123 NULL, BoundNetLog()); | |
124 CHECK_EQ(ERR_IO_PENDING, rv); | |
125 rv = callback.WaitForResult(); | |
126 CHECK_EQ(rv, OK); | |
127 sock_ = socket_factory_->CreateTransportClientSocket(addr, &net_log_, | |
128 NetLog::Source()); | |
129 } | |
130 | |
131 int TCPClientSocketTest::DrainClientSocket(IOBuffer* buf, | |
132 uint32 buf_len, | |
133 uint32 bytes_to_read, | |
134 TestCompletionCallback* callback) { | |
135 int rv = OK; | |
136 uint32 bytes_read = 0; | |
137 | |
138 while (bytes_read < bytes_to_read) { | |
139 rv = sock_->Read(buf, buf_len, callback->callback()); | |
140 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
141 rv = callback->GetResult(rv); | |
142 EXPECT_GT(rv, 0); | |
143 bytes_read += rv; | |
144 } | |
145 | |
146 return static_cast<int>(bytes_read); | |
147 } | |
148 | |
149 void TCPClientSocketTest::EstablishConnection( | |
150 TestCompletionCallback* callback) { | |
151 int rv = sock_->Connect(callback->callback()); | |
152 // Wait for |listen_sock_| to accept a connection. | |
153 connect_loop_.Run(); | |
154 // Now wait for the client socket to accept the connection. | |
155 EXPECT_EQ(OK, callback->GetResult(rv)); | |
156 } | |
157 | |
158 void TCPClientSocketTest::SendRequestAndResponse() { | |
159 // Send client request. | |
160 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | |
161 int request_len = strlen(request_text); | |
162 scoped_refptr<DrainableIOBuffer> request_buffer( | |
163 new DrainableIOBuffer(new IOBuffer(request_len), request_len)); | |
164 memcpy(request_buffer->data(), request_text, request_len); | |
165 | |
166 int bytes_written = 0; | |
167 while (request_buffer->BytesRemaining() > 0) { | |
168 TestCompletionCallback write_callback; | |
169 int write_result = | |
170 sock_->Write(request_buffer.get(), request_buffer->BytesRemaining(), | |
171 write_callback.callback()); | |
172 write_result = write_callback.GetResult(write_result); | |
173 ASSERT_GT(write_result, 0); | |
174 ASSERT_LE(bytes_written + write_result, request_len); | |
175 request_buffer->DidConsume(write_result); | |
176 bytes_written += write_result; | |
177 } | |
178 ASSERT_EQ(request_len, bytes_written); | |
179 | |
180 // Confirm that the server receives what client sent. | |
181 std::string data_received = ReadServerData(bytes_written); | |
182 ASSERT_TRUE(connected_sock_->IsConnectedAndIdle()); | |
183 ASSERT_EQ(request_text, data_received); | |
184 | |
185 // Write server response. | |
186 SendServerResponse(); | |
187 } | |
188 | |
189 void TCPClientSocketTest::SendServerResponse() { | |
190 // TODO(dkegel): this might not be long enough to tickle some bugs. | |
191 int reply_len = strlen(kServerReply); | |
192 scoped_refptr<DrainableIOBuffer> write_buffer( | |
193 new DrainableIOBuffer(new IOBuffer(reply_len), reply_len)); | |
194 memcpy(write_buffer->data(), kServerReply, reply_len); | |
195 int bytes_written = 0; | |
196 while (write_buffer->BytesRemaining() > 0) { | |
197 TestCompletionCallback write_callback; | |
198 int write_result = connected_sock_->Write(write_buffer.get(), | |
199 write_buffer->BytesRemaining(), | |
200 write_callback.callback()); | |
201 write_result = write_callback.GetResult(write_result); | |
202 ASSERT_GE(write_result, 0); | |
203 ASSERT_LE(bytes_written + write_result, reply_len); | |
204 write_buffer->DidConsume(write_result); | |
205 bytes_written += write_result; | |
206 } | |
207 if (close_server_socket_on_next_send_) | |
208 CloseServerSocket(); | |
209 } | |
210 | |
211 std::string TCPClientSocketTest::ReadServerData(int expected_bytes_read) { | |
212 int bytes_read = 0; | |
213 scoped_refptr<IOBufferWithSize> read_buffer( | |
214 new IOBufferWithSize(expected_bytes_read)); | |
215 while (bytes_read < expected_bytes_read) { | |
216 TestCompletionCallback read_callback; | |
217 int rv = connected_sock_->Read(read_buffer.get(), | |
218 expected_bytes_read - bytes_read, | |
219 read_callback.callback()); | |
220 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
221 rv = read_callback.GetResult(rv); | |
222 EXPECT_GE(rv, 0); | |
223 bytes_read += rv; | |
224 } | |
225 EXPECT_EQ(expected_bytes_read, bytes_read); | |
226 return std::string(read_buffer->data(), bytes_read); | |
227 } | |
228 | |
229 // TODO(leighton): Add SCTP to this list when it is ready. | |
230 INSTANTIATE_TEST_CASE_P(StreamSocket, | |
231 TCPClientSocketTest, | |
232 ::testing::Values(TCP)); | |
mmenke
2015/04/24 20:45:49
This TODO seems to indicate this is intended to te
xunjieli
2015/04/24 20:50:15
Sounds good! will do.
| |
233 | |
234 TEST_P(TCPClientSocketTest, Connect) { | |
235 TestCompletionCallback callback; | |
236 EXPECT_FALSE(sock_->IsConnected()); | |
237 | |
238 int rv = sock_->Connect(callback.callback()); | |
239 // Wait for |listen_sock_| to accept a connection. | |
240 connect_loop_.Run(); | |
241 | |
242 TestNetLog::CapturedEntryList net_log_entries; | |
243 net_log_.GetEntries(&net_log_entries); | |
244 EXPECT_TRUE( | |
245 LogContainsBeginEvent(net_log_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | |
246 EXPECT_TRUE( | |
247 LogContainsBeginEvent(net_log_entries, 1, NetLog::TYPE_TCP_CONNECT)); | |
248 // Now wait for the client socket to accept the connection. | |
249 if (rv != OK) { | |
250 ASSERT_EQ(rv, ERR_IO_PENDING); | |
251 rv = callback.WaitForResult(); | |
252 EXPECT_EQ(rv, OK); | |
253 } | |
254 | |
255 EXPECT_TRUE(sock_->IsConnected()); | |
256 net_log_.GetEntries(&net_log_entries); | |
257 EXPECT_TRUE( | |
258 LogContainsEndEvent(net_log_entries, -1, NetLog::TYPE_TCP_CONNECT)); | |
259 | |
260 sock_->Disconnect(); | |
261 EXPECT_FALSE(sock_->IsConnected()); | |
262 } | |
263 | |
264 TEST_P(TCPClientSocketTest, IsConnected) { | |
265 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | |
266 TestCompletionCallback callback; | |
267 uint32 bytes_read; | |
268 | |
269 EXPECT_FALSE(sock_->IsConnected()); | |
270 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | |
271 | |
272 EstablishConnection(&callback); | |
273 | |
274 EXPECT_TRUE(sock_->IsConnected()); | |
275 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | |
276 | |
277 // Send the request and wait for the server to respond. | |
278 SendRequestAndResponse(); | |
279 | |
280 // Drain a single byte so we know we've received some data. | |
281 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); | |
282 ASSERT_EQ(bytes_read, 1u); | |
283 | |
284 // Socket should be considered connected, but not idle, due to | |
285 // pending data. | |
286 EXPECT_TRUE(sock_->IsConnected()); | |
287 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | |
288 | |
289 bytes_read = | |
290 DrainClientSocket(buf.get(), 4096, strlen(kServerReply) - 1, &callback); | |
291 ASSERT_EQ(bytes_read, strlen(kServerReply) - 1); | |
292 | |
293 // After draining the data, the socket should be back to connected | |
294 // and idle. | |
295 EXPECT_TRUE(sock_->IsConnected()); | |
296 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | |
297 | |
298 // This time close the server socket immediately after the server response. | |
299 set_close_server_socket_on_next_send(true); | |
300 SendRequestAndResponse(); | |
301 | |
302 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); | |
303 ASSERT_EQ(bytes_read, 1u); | |
304 | |
305 // As above because of data. | |
306 EXPECT_TRUE(sock_->IsConnected()); | |
307 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | |
308 | |
309 bytes_read = | |
310 DrainClientSocket(buf.get(), 4096, strlen(kServerReply) - 1, &callback); | |
311 ASSERT_EQ(bytes_read, strlen(kServerReply) - 1); | |
312 | |
313 // Once the data is drained, the socket should now be seen as not | |
314 // connected. | |
315 if (sock_->IsConnected()) { | |
316 // In the unlikely event that the server's connection closure is not | |
317 // processed in time, wait for the connection to be closed. | |
318 int rv = sock_->Read(buf.get(), 4096, callback.callback()); | |
319 EXPECT_EQ(0, callback.GetResult(rv)); | |
320 EXPECT_FALSE(sock_->IsConnected()); | |
321 } | |
322 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | |
323 } | |
324 | |
325 TEST_P(TCPClientSocketTest, Read) { | |
326 TestCompletionCallback callback; | |
327 EstablishConnection(&callback); | |
328 | |
329 SendRequestAndResponse(); | |
330 | |
331 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | |
332 uint32 bytes_read = | |
333 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); | |
334 ASSERT_EQ(bytes_read, strlen(kServerReply)); | |
335 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); | |
336 | |
337 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | |
338 // then close the server socket, and note the close. | |
339 | |
340 int rv = sock_->Read(buf.get(), 4096, callback.callback()); | |
341 ASSERT_EQ(ERR_IO_PENDING, rv); | |
342 CloseServerSocket(); | |
343 EXPECT_EQ(0, callback.WaitForResult()); | |
344 } | |
345 | |
346 TEST_P(TCPClientSocketTest, Read_SmallChunks) { | |
347 TestCompletionCallback callback; | |
348 EstablishConnection(&callback); | |
349 | |
350 SendRequestAndResponse(); | |
351 | |
352 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | |
353 uint32 bytes_read = 0; | |
354 while (bytes_read < strlen(kServerReply)) { | |
355 int rv = sock_->Read(buf.get(), 1, callback.callback()); | |
356 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
357 | |
358 rv = callback.GetResult(rv); | |
359 | |
360 ASSERT_EQ(1, rv); | |
361 bytes_read += rv; | |
362 } | |
363 | |
364 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | |
365 // then close the server socket, and note the close. | |
366 | |
367 int rv = sock_->Read(buf.get(), 1, callback.callback()); | |
368 ASSERT_EQ(ERR_IO_PENDING, rv); | |
369 CloseServerSocket(); | |
370 EXPECT_EQ(0, callback.WaitForResult()); | |
371 } | |
372 | |
373 TEST_P(TCPClientSocketTest, Read_Interrupted) { | |
374 TestCompletionCallback callback; | |
375 EstablishConnection(&callback); | |
376 | |
377 SendRequestAndResponse(); | |
378 | |
379 // Do a partial read and then exit. This test should not crash! | |
380 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); | |
381 int rv = sock_->Read(buf.get(), 16, callback.callback()); | |
382 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
383 | |
384 rv = callback.GetResult(rv); | |
385 | |
386 EXPECT_NE(0, rv); | |
387 } | |
388 | |
389 TEST_P(TCPClientSocketTest, FullDuplex_ReadFirst) { | |
390 TestCompletionCallback callback; | |
391 EstablishConnection(&callback); | |
392 | |
393 // Read first. There's no data, so it should return ERR_IO_PENDING. | |
394 const int kBufLen = 4096; | |
395 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | |
396 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); | |
397 EXPECT_EQ(ERR_IO_PENDING, rv); | |
398 | |
399 const int kWriteBufLen = 64 * 1024; | |
400 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | |
401 char* request_data = request_buffer->data(); | |
402 memset(request_data, 'A', kWriteBufLen); | |
403 TestCompletionCallback write_callback; | |
404 | |
405 int bytes_written = 0; | |
406 while (true) { | |
407 rv = sock_->Write(request_buffer.get(), kWriteBufLen, | |
408 write_callback.callback()); | |
409 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
410 if (rv == ERR_IO_PENDING) { | |
411 ReadServerData(bytes_written); | |
412 ASSERT_TRUE(connected_sock_->IsConnectedAndIdle()); | |
413 SendServerResponse(); | |
414 rv = write_callback.WaitForResult(); | |
415 break; | |
416 } | |
417 bytes_written += rv; | |
418 } | |
419 | |
420 // At this point, both read and write have returned ERR_IO_PENDING, and the | |
421 // write callback has executed. We wait for the read callback to run now to | |
422 // make sure that the socket can handle full duplex communications. | |
423 | |
424 rv = callback.WaitForResult(); | |
425 EXPECT_GE(rv, 0); | |
426 } | |
427 | |
428 TEST_P(TCPClientSocketTest, FullDuplex_WriteFirst) { | |
429 TestCompletionCallback callback; | |
430 EstablishConnection(&callback); | |
431 | |
432 const int kWriteBufLen = 64 * 1024; | |
433 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | |
434 char* request_data = request_buffer->data(); | |
435 memset(request_data, 'A', kWriteBufLen); | |
436 TestCompletionCallback write_callback; | |
437 | |
438 int bytes_written = 0; | |
439 while (true) { | |
440 int rv = sock_->Write(request_buffer.get(), kWriteBufLen, | |
441 write_callback.callback()); | |
442 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
443 | |
444 if (rv == ERR_IO_PENDING) | |
445 break; | |
446 bytes_written += rv; | |
447 } | |
448 | |
449 // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the | |
450 // Read() to block on ERR_IO_PENDING too. | |
451 | |
452 const int kBufLen = 4096; | |
453 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | |
454 while (true) { | |
455 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); | |
456 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | |
457 if (rv == ERR_IO_PENDING) | |
458 break; | |
459 } | |
460 | |
461 // At this point, both read and write have returned ERR_IO_PENDING. Now we | |
462 // run the write and read callbacks to make sure they can handle full duplex | |
463 // communications. | |
464 | |
465 ReadServerData(bytes_written); | |
466 ASSERT_TRUE(connected_sock_->IsConnectedAndIdle()); | |
xunjieli
2015/04/24 20:50:15
Matt, I should remove line 466 and 412's ASSERT_TR
mmenke
2015/04/24 20:55:40
Yes, you should remove just remove them.
They may
| |
467 SendServerResponse(); | |
468 int rv = write_callback.WaitForResult(); | |
469 EXPECT_GE(rv, 0); | |
470 | |
471 // It's possible the read is blocked because it's already read all the data. | |
472 // Close the server socket, so there will at least be a 0-byte read. | |
473 CloseServerSocket(); | |
474 | |
475 rv = callback.WaitForResult(); | |
476 EXPECT_GE(rv, 0); | |
477 } | |
478 | |
22 // Try binding a socket to loopback interface and verify that we can | 479 // Try binding a socket to loopback interface and verify that we can |
23 // still connect to a server on the same interface. | 480 // still connect to a server on the same interface. |
24 TEST(TCPClientSocketTest, BindLoopbackToLoopback) { | 481 TEST(TCPClientSocketLoopbackTest, BindLoopbackToLoopback) { |
25 IPAddressNumber lo_address; | 482 IPAddressNumber lo_address; |
26 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); | 483 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); |
27 | 484 |
28 TCPServerSocket server(NULL, NetLog::Source()); | 485 TCPServerSocket server(NULL, NetLog::Source()); |
29 ASSERT_EQ(OK, server.Listen(IPEndPoint(lo_address, 0), 1)); | 486 ASSERT_EQ(OK, server.Listen(IPEndPoint(lo_address, 0), 1)); |
30 IPEndPoint server_address; | 487 IPEndPoint server_address; |
31 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); | 488 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); |
32 | 489 |
33 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); | 490 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); |
34 | 491 |
(...skipping 17 matching lines...) Expand all Loading... | |
52 | 509 |
53 EXPECT_TRUE(socket.IsConnected()); | 510 EXPECT_TRUE(socket.IsConnected()); |
54 socket.Disconnect(); | 511 socket.Disconnect(); |
55 EXPECT_FALSE(socket.IsConnected()); | 512 EXPECT_FALSE(socket.IsConnected()); |
56 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, | 513 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, |
57 socket.GetLocalAddress(&local_address_result)); | 514 socket.GetLocalAddress(&local_address_result)); |
58 } | 515 } |
59 | 516 |
60 // Try to bind socket to the loopback interface and connect to an | 517 // Try to bind socket to the loopback interface and connect to an |
61 // external address, verify that connection fails. | 518 // external address, verify that connection fails. |
62 TEST(TCPClientSocketTest, BindLoopbackToExternal) { | 519 TEST(TCPClientSocketLoopbackTest, BindLoopbackToExternal) { |
63 IPAddressNumber external_ip; | 520 IPAddressNumber external_ip; |
64 ASSERT_TRUE(ParseIPLiteralToNumber("72.14.213.105", &external_ip)); | 521 ASSERT_TRUE(ParseIPLiteralToNumber("72.14.213.105", &external_ip)); |
65 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), | 522 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), |
66 NULL, NetLog::Source()); | 523 NULL, NetLog::Source()); |
67 | 524 |
68 IPAddressNumber lo_address; | 525 IPAddressNumber lo_address; |
69 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); | 526 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); |
70 EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); | 527 EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); |
71 | 528 |
72 TestCompletionCallback connect_callback; | 529 TestCompletionCallback connect_callback; |
73 int result = socket.Connect(connect_callback.callback()); | 530 int result = socket.Connect(connect_callback.callback()); |
74 if (result == ERR_IO_PENDING) | 531 if (result == ERR_IO_PENDING) |
75 result = connect_callback.WaitForResult(); | 532 result = connect_callback.WaitForResult(); |
76 | 533 |
77 // We may get different errors here on different system, but | 534 // We may get different errors here on different system, but |
78 // connect() is not expected to succeed. | 535 // connect() is not expected to succeed. |
79 EXPECT_NE(OK, result); | 536 EXPECT_NE(OK, result); |
80 } | 537 } |
81 | 538 |
82 // Bind a socket to the IPv4 loopback interface and try to connect to | 539 // Bind a socket to the IPv4 loopback interface and try to connect to |
83 // the IPv6 loopback interface, verify that connection fails. | 540 // the IPv6 loopback interface, verify that connection fails. |
84 TEST(TCPClientSocketTest, BindLoopbackToIPv6) { | 541 TEST(TCPClientSocketLoopbackTest, BindLoopbackToIPv6) { |
85 IPAddressNumber ipv6_lo_ip; | 542 IPAddressNumber ipv6_lo_ip; |
86 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &ipv6_lo_ip)); | 543 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &ipv6_lo_ip)); |
87 TCPServerSocket server(NULL, NetLog::Source()); | 544 TCPServerSocket server(NULL, NetLog::Source()); |
88 int listen_result = server.Listen(IPEndPoint(ipv6_lo_ip, 0), 1); | 545 int listen_result = server.Listen(IPEndPoint(ipv6_lo_ip, 0), 1); |
89 if (listen_result != OK) { | 546 if (listen_result != OK) { |
90 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." | 547 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." |
91 " Skipping the test"; | 548 " Skipping the test"; |
92 return; | 549 return; |
93 } | 550 } |
94 | 551 |
95 IPEndPoint server_address; | 552 IPEndPoint server_address; |
96 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); | 553 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); |
97 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); | 554 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); |
98 | 555 |
99 IPAddressNumber ipv4_lo_ip; | 556 IPAddressNumber ipv4_lo_ip; |
100 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ipv4_lo_ip)); | 557 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ipv4_lo_ip)); |
101 EXPECT_EQ(OK, socket.Bind(IPEndPoint(ipv4_lo_ip, 0))); | 558 EXPECT_EQ(OK, socket.Bind(IPEndPoint(ipv4_lo_ip, 0))); |
102 | 559 |
103 TestCompletionCallback connect_callback; | 560 TestCompletionCallback connect_callback; |
104 int result = socket.Connect(connect_callback.callback()); | 561 int result = socket.Connect(connect_callback.callback()); |
105 if (result == ERR_IO_PENDING) | 562 if (result == ERR_IO_PENDING) |
106 result = connect_callback.WaitForResult(); | 563 result = connect_callback.WaitForResult(); |
107 | 564 |
108 EXPECT_NE(OK, result); | 565 EXPECT_NE(OK, result); |
109 } | 566 } |
110 | 567 |
111 } // namespace | |
112 | |
113 } // namespace net | 568 } // namespace net |
OLD | NEW |