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

Unified Diff: net/socket/tcp_socket_unittest.cc

Issue 2815993002: Adds a method to TCPServerSocket to adopt a socket. (Closed)
Patch Set: Rebased to fix merge conflict Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: net/socket/tcp_socket_unittest.cc
diff --git a/net/socket/tcp_socket_unittest.cc b/net/socket/tcp_socket_unittest.cc
index d9da13c660cc5f7910de744af5932ff324f575d0..9a9418745a2136e4de241359101d24197ceb1eba 100644
--- a/net/socket/tcp_socket_unittest.cc
+++ b/net/socket/tcp_socket_unittest.cc
@@ -11,6 +11,7 @@
#include <string>
#include <vector>
+#include "base/files/file_util.h"
mmenke 2017/04/21 19:20:38 I don't think this is used?
Raul Vera 2017/05/01 05:35:25 It was, but isn't now, so I've removed it.
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "net/base/address_list.h"
@@ -21,6 +22,9 @@
#include "net/base/test_completion_callback.h"
#include "net/log/net_log_source.h"
#include "net/socket/socket_performance_watcher.h"
+#if defined(OS_POSIX)
+#include "net/socket/socket_posix.h"
+#endif
mmenke 2017/04/21 19:20:38 No longer needed, I believe.
Raul Vera 2017/05/01 05:35:25 Good catch. I missed that. Thanks.
#include "net/socket/tcp_client_socket.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -226,24 +230,41 @@ TEST_F(TCPSocketTest, AcceptAsync) {
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_THAT(socket_.AdoptListenSocket(existing_socket), IsOk());
+// Test AdoptConnectedSocket()
+TEST_F(TCPSocketTest, AdoptConnectedSocket) {
+ SocketDescriptor socket_fd =
+ CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ ASSERT_GE(socket_fd, 0);
+ ASSERT_TRUE(base::SetNonBlocking(socket_fd));
+ IPEndPoint address(IPAddress::IPv4Localhost(), 0);
+ SockaddrStorage storage;
+ ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
+ ASSERT_GE(bind(socket_fd, storage.addr, storage.addr_len), 0);
+ ASSERT_GE(listen(socket_fd, kListenBacklog), 0);
mmenke 2017/04/21 19:20:38 This isn't a connected socket, it's a listening so
Raul Vera 2017/05/01 05:35:25 Indeed. This turned out to require exposing GetSoc
+
+ ASSERT_THAT(socket_.AdoptConnectedSocket(socket_fd, IPEndPoint()), IsOk());
+ ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
+
+ TestAcceptAsync();
+}
+
+// Test Accept() for AdoptUnconnectedSocket.
+TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) {
+ // Create a socket to be used with AdoptUnconnectedSocket.
+ SocketDescriptor existing_socket =
+ CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk());
IPEndPoint address(IPAddress::IPv4Localhost(), 0);
SockaddrStorage storage;
ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
- ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len));
+ ASSERT_EQ(bind(existing_socket, storage.addr, storage.addr_len), 0);
mmenke 2017/04/21 19:20:38 Expected should go before actual, for ASSERT_EQ (Y
Raul Vera 2017/05/01 05:35:25 That code is gone now, but thanks.
ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
TestAcceptAsync();
}
-#endif
// Accept two connections simultaneously.
TEST_F(TCPSocketTest, Accept2Connections) {

Powered by Google App Engine
This is Rietveld 408576698