OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/renderer_host/p2p/socket_host_tcp_server.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 11 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
12 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 12 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 using ::testing::DeleteArg; | 18 using ::testing::DeleteArg; |
19 using ::testing::DoAll; | 19 using ::testing::DoAll; |
20 using ::testing::Return; | 20 using ::testing::Return; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class FakeServerSocket : public net::ServerSocket { | 24 class FakeServerSocket : public net::ServerSocket { |
25 public: | 25 public: |
26 FakeServerSocket() | 26 FakeServerSocket() : listening_(false), accept_socket_(nullptr) {} |
27 : listening_(false), | |
28 accept_socket_(NULL) { | |
29 } | |
30 | 27 |
31 ~FakeServerSocket() override {} | 28 ~FakeServerSocket() override {} |
32 | 29 |
33 bool listening() { return listening_; } | 30 bool listening() { return listening_; } |
34 | 31 |
35 void AddIncoming(net::StreamSocket* socket) { | 32 void AddIncoming(net::StreamSocket* socket) { |
36 if (!accept_callback_.is_null()) { | 33 if (!accept_callback_.is_null()) { |
37 DCHECK(incoming_sockets_.empty()); | 34 DCHECK(incoming_sockets_.empty()); |
38 accept_socket_->reset(socket); | 35 accept_socket_->reset(socket); |
39 accept_socket_ = NULL; | 36 accept_socket_ = nullptr; |
40 | 37 |
41 // This copy is necessary because this implementation of ServerSocket | 38 // This copy is necessary because this implementation of ServerSocket |
42 // bases logic on the null-ness of |accept_callback_| in the bound | 39 // bases logic on the null-ness of |accept_callback_| in the bound |
43 // callback. | 40 // callback. |
44 net::CompletionCallback cb = accept_callback_; | 41 net::CompletionCallback cb = accept_callback_; |
45 accept_callback_.Reset(); | 42 accept_callback_.Reset(); |
46 cb.Run(net::OK); | 43 cb.Run(net::OK); |
47 } else { | 44 } else { |
48 incoming_sockets_.push_back(socket); | 45 incoming_sockets_.push_back(socket); |
49 } | 46 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 return host->socket_.get(); | 112 return host->socket_.get(); |
116 } | 113 } |
117 | 114 |
118 MockIPCSender sender_; | 115 MockIPCSender sender_; |
119 FakeServerSocket* socket_; // Owned by |socket_host_|. | 116 FakeServerSocket* socket_; // Owned by |socket_host_|. |
120 std::unique_ptr<P2PSocketHostTcpServer> socket_host_; | 117 std::unique_ptr<P2PSocketHostTcpServer> socket_host_; |
121 }; | 118 }; |
122 | 119 |
123 // Accept incoming connection. | 120 // Accept incoming connection. |
124 TEST_F(P2PSocketHostTcpServerTest, Accept) { | 121 TEST_F(P2PSocketHostTcpServerTest, Accept) { |
125 FakeSocket* incoming = new FakeSocket(NULL); | 122 FakeSocket* incoming = new FakeSocket(nullptr); |
126 incoming->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 123 incoming->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
127 net::IPEndPoint addr = ParseAddress(kTestIpAddress1, kTestPort1); | 124 net::IPEndPoint addr = ParseAddress(kTestIpAddress1, kTestPort1); |
128 incoming->SetPeerAddress(addr); | 125 incoming->SetPeerAddress(addr); |
129 | 126 |
130 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr))) | 127 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr))) |
131 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 128 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
132 socket_->AddIncoming(incoming); | 129 socket_->AddIncoming(incoming); |
133 | 130 |
134 const int kAcceptedSocketId = 1; | 131 const int kAcceptedSocketId = 1; |
135 | 132 |
136 std::unique_ptr<P2PSocketHost> new_host( | 133 std::unique_ptr<P2PSocketHost> new_host( |
137 socket_host_->AcceptIncomingTcpConnection(addr, kAcceptedSocketId)); | 134 socket_host_->AcceptIncomingTcpConnection(addr, kAcceptedSocketId)); |
138 ASSERT_TRUE(new_host.get() != NULL); | 135 ASSERT_TRUE(new_host.get() != nullptr); |
139 EXPECT_EQ(incoming, GetSocketFormTcpSocketHost( | 136 EXPECT_EQ(incoming, GetSocketFormTcpSocketHost( |
140 reinterpret_cast<P2PSocketHostTcp*>(new_host.get()))); | 137 reinterpret_cast<P2PSocketHostTcp*>(new_host.get()))); |
141 } | 138 } |
142 | 139 |
143 // Accept 2 simultaneous connections. | 140 // Accept 2 simultaneous connections. |
144 TEST_F(P2PSocketHostTcpServerTest, Accept2) { | 141 TEST_F(P2PSocketHostTcpServerTest, Accept2) { |
145 FakeSocket* incoming1 = new FakeSocket(NULL); | 142 FakeSocket* incoming1 = new FakeSocket(nullptr); |
146 incoming1->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 143 incoming1->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
147 net::IPEndPoint addr1 = ParseAddress(kTestIpAddress1, kTestPort1); | 144 net::IPEndPoint addr1 = ParseAddress(kTestIpAddress1, kTestPort1); |
148 incoming1->SetPeerAddress(addr1); | 145 incoming1->SetPeerAddress(addr1); |
149 FakeSocket* incoming2 = new FakeSocket(NULL); | 146 FakeSocket* incoming2 = new FakeSocket(nullptr); |
150 incoming2->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 147 incoming2->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
151 net::IPEndPoint addr2 = ParseAddress(kTestIpAddress2, kTestPort2); | 148 net::IPEndPoint addr2 = ParseAddress(kTestIpAddress2, kTestPort2); |
152 incoming2->SetPeerAddress(addr2); | 149 incoming2->SetPeerAddress(addr2); |
153 | 150 |
154 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr1))) | 151 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr1))) |
155 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 152 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
156 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr2))) | 153 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr2))) |
157 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 154 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
158 socket_->AddIncoming(incoming1); | 155 socket_->AddIncoming(incoming1); |
159 socket_->AddIncoming(incoming2); | 156 socket_->AddIncoming(incoming2); |
160 | 157 |
161 const int kAcceptedSocketId1 = 1; | 158 const int kAcceptedSocketId1 = 1; |
162 const int kAcceptedSocketId2 = 2; | 159 const int kAcceptedSocketId2 = 2; |
163 | 160 |
164 std::unique_ptr<P2PSocketHost> new_host1( | 161 std::unique_ptr<P2PSocketHost> new_host1( |
165 socket_host_->AcceptIncomingTcpConnection(addr1, kAcceptedSocketId1)); | 162 socket_host_->AcceptIncomingTcpConnection(addr1, kAcceptedSocketId1)); |
166 ASSERT_TRUE(new_host1.get() != NULL); | 163 ASSERT_TRUE(new_host1.get() != nullptr); |
167 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( | 164 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( |
168 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); | 165 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); |
169 std::unique_ptr<P2PSocketHost> new_host2( | 166 std::unique_ptr<P2PSocketHost> new_host2( |
170 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); | 167 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); |
171 ASSERT_TRUE(new_host2.get() != NULL); | 168 ASSERT_TRUE(new_host2.get() != nullptr); |
172 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( | 169 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( |
173 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); | 170 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); |
174 } | 171 } |
175 | 172 |
176 } // namespace content | 173 } // namespace content |
OLD | NEW |