Chromium Code Reviews| 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 #include "net/socket/tcp_server_socket.h" | 5 #include "net/socket/tcp_server_socket.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.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_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
| 18 #include "net/socket/tcp_client_socket.h" | 18 #include "net/socket/tcp_client_socket.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 const int kListenBacklog = 5; | 25 const int kListenBacklog = 5; |
| 26 const int kListenPort = 31373; | |
| 26 | 27 |
| 27 class TCPServerSocketTest : public PlatformTest { | 28 class TCPServerSocketTest : public PlatformTest { |
| 28 protected: | 29 protected: |
| 29 TCPServerSocketTest() | 30 TCPServerSocketTest() |
| 30 : socket_(NULL, NetLog::Source()) { | 31 : socket_(NULL, NetLog::Source()) { |
| 31 } | 32 } |
| 32 | 33 |
| 33 void SetUpIPv4() { | 34 void SetUpIPv4() { |
| 34 IPEndPoint address; | 35 IPEndPoint address; |
| 35 ParseAddress("127.0.0.1", 0, &address); | 36 ParseAddress("127.0.0.1", 0, &address); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 ASSERT_TRUE(read_result >= 0); | 240 ASSERT_TRUE(read_result >= 0); |
| 240 ASSERT_TRUE(bytes_read + read_result <= message.size()); | 241 ASSERT_TRUE(bytes_read + read_result <= message.size()); |
| 241 memmove(&buffer[bytes_read], read_buffer->data(), read_result); | 242 memmove(&buffer[bytes_read], read_buffer->data(), read_result); |
| 242 bytes_read += read_result; | 243 bytes_read += read_result; |
| 243 } | 244 } |
| 244 | 245 |
| 245 std::string received_message(buffer.begin(), buffer.end()); | 246 std::string received_message(buffer.begin(), buffer.end()); |
| 246 ASSERT_EQ(message, received_message); | 247 ASSERT_EQ(message, received_message); |
| 247 } | 248 } |
| 248 | 249 |
| 250 TEST_F(TCPServerSocketTest, Rebind) { | |
| 251 IPEndPoint address; | |
| 252 ParseAddress("127.0.0.1", kListenPort, &address); | |
|
wtc
2013/07/25 18:03:56
Using a constant listen port may prevent another p
Bei Zhang
2013/07/26 05:10:40
Done. I think no solution is perfect here. I canno
| |
| 253 | |
| 254 scoped_ptr<TCPServerSocket> socket( | |
| 255 new TCPServerSocket(NULL, NetLog::Source())); | |
| 256 ASSERT_EQ(OK, socket->Listen(address, kListenBacklog)); | |
| 257 | |
| 258 scoped_ptr<TCPServerSocket> conflict_socket( | |
| 259 new TCPServerSocket(NULL, NetLog::Source())); | |
| 260 ASSERT_NE(OK, conflict_socket->Listen(address, kListenBacklog)); | |
| 261 | |
| 262 // Unbind | |
| 263 socket.reset(NULL); | |
| 264 | |
| 265 ASSERT_EQ(OK, conflict_socket->Listen(address, kListenBacklog)); | |
| 266 } | |
| 267 | |
| 249 } // namespace | 268 } // namespace |
| 250 | 269 |
| 251 } // namespace net | 270 } // namespace net |
| OLD | NEW |