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 "content/browser/renderer_host/p2p/socket_host_udp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <deque> | 8 #include <deque> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" |
13 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
14 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 15 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
15 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" | 16 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/udp/datagram_server_socket.h" | 20 #include "net/udp/datagram_server_socket.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "third_party/webrtc/base/timing.h" | 23 #include "third_party/webrtc/base/timing.h" |
(...skipping 12 matching lines...) Expand all Loading... |
35 void set_now(double now) { now_ = now; } | 36 void set_now(double now) { now_ = now; } |
36 | 37 |
37 private: | 38 private: |
38 double now_; | 39 double now_; |
39 }; | 40 }; |
40 | 41 |
41 class FakeDatagramServerSocket : public net::DatagramServerSocket { | 42 class FakeDatagramServerSocket : public net::DatagramServerSocket { |
42 public: | 43 public: |
43 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; | 44 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; |
44 | 45 |
45 // P2PSocketHostUdp destroyes a socket on errors so sent packets | 46 // P2PSocketHostUdp destroys a socket on errors so sent packets |
46 // need to be stored outside of this object. | 47 // need to be stored outside of this object. |
47 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) | 48 FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets, |
48 : sent_packets_(sent_packets) { | 49 std::vector<uint16_t>* used_ports) |
49 } | 50 : sent_packets_(sent_packets), |
| 51 recv_address_(nullptr), |
| 52 recv_size_(0), |
| 53 used_ports_(used_ports) {} |
50 | 54 |
51 void Close() override {} | 55 void Close() override {} |
52 | 56 |
53 int GetPeerAddress(net::IPEndPoint* address) const override { | 57 int GetPeerAddress(net::IPEndPoint* address) const override { |
54 NOTREACHED(); | 58 NOTREACHED(); |
55 return net::ERR_SOCKET_NOT_CONNECTED; | 59 return net::ERR_SOCKET_NOT_CONNECTED; |
56 } | 60 } |
57 | 61 |
58 int GetLocalAddress(net::IPEndPoint* address) const override { | 62 int GetLocalAddress(net::IPEndPoint* address) const override { |
59 *address = address_; | 63 *address = address_; |
60 return 0; | 64 return 0; |
61 } | 65 } |
62 | 66 |
63 int Listen(const net::IPEndPoint& address) override { | 67 int Listen(const net::IPEndPoint& address) override { |
| 68 if (used_ports_) { |
| 69 for (auto used_port : *used_ports_) { |
| 70 if (used_port == address.port()) |
| 71 return -1; |
| 72 } |
| 73 used_ports_->push_back(address.port()); |
| 74 } |
| 75 |
64 address_ = address; | 76 address_ = address; |
65 return 0; | 77 return 0; |
66 } | 78 } |
67 | 79 |
68 int RecvFrom(net::IOBuffer* buf, | 80 int RecvFrom(net::IOBuffer* buf, |
69 int buf_len, | 81 int buf_len, |
70 net::IPEndPoint* address, | 82 net::IPEndPoint* address, |
71 const net::CompletionCallback& callback) override { | 83 const net::CompletionCallback& callback) override { |
72 CHECK(recv_callback_.is_null()); | 84 CHECK(recv_callback_.is_null()); |
73 if (incoming_packets_.size() > 0) { | 85 if (incoming_packets_.size() > 0) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 private: | 168 private: |
157 net::IPEndPoint address_; | 169 net::IPEndPoint address_; |
158 std::deque<UDPPacket>* sent_packets_; | 170 std::deque<UDPPacket>* sent_packets_; |
159 std::deque<UDPPacket> incoming_packets_; | 171 std::deque<UDPPacket> incoming_packets_; |
160 net::BoundNetLog net_log_; | 172 net::BoundNetLog net_log_; |
161 | 173 |
162 scoped_refptr<net::IOBuffer> recv_buffer_; | 174 scoped_refptr<net::IOBuffer> recv_buffer_; |
163 net::IPEndPoint* recv_address_; | 175 net::IPEndPoint* recv_address_; |
164 int recv_size_; | 176 int recv_size_; |
165 net::CompletionCallback recv_callback_; | 177 net::CompletionCallback recv_callback_; |
| 178 std::vector<uint16_t>* used_ports_; |
166 }; | 179 }; |
167 | 180 |
| 181 std::unique_ptr<net::DatagramServerSocket> CreateFakeDatagramServerSocket( |
| 182 std::deque<FakeDatagramServerSocket::UDPPacket>* sent_packets, |
| 183 std::vector<uint16_t>* used_ports) { |
| 184 return base::WrapUnique( |
| 185 new FakeDatagramServerSocket(sent_packets, used_ports)); |
| 186 } |
| 187 |
168 } // namespace | 188 } // namespace |
169 | 189 |
170 namespace content { | 190 namespace content { |
171 | 191 |
172 class P2PSocketHostUdpTest : public testing::Test { | 192 class P2PSocketHostUdpTest : public testing::Test { |
173 protected: | 193 protected: |
174 void SetUp() override { | 194 void SetUp() override { |
175 EXPECT_CALL( | 195 EXPECT_CALL( |
176 sender_, | 196 sender_, |
177 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) | 197 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) |
178 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 198 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
179 | 199 |
180 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); | 200 socket_host_.reset(new P2PSocketHostUdp( |
181 socket_ = new FakeDatagramServerSocket(&sent_packets_); | 201 &sender_, 0, &throttler_, |
182 socket_host_->socket_.reset(socket_); | 202 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets_, nullptr))); |
183 | 203 |
184 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 204 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
185 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); | 205 socket_host_->Init(local_address_, 0, 0, P2PHostAndIPEndPoint()); |
| 206 socket_ = GetSocketFromHost(socket_host_.get()); |
186 | 207 |
187 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); | 208 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); |
188 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); | 209 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); |
189 | 210 |
190 std::unique_ptr<rtc::Timing> timing(new FakeTiming()); | 211 std::unique_ptr<rtc::Timing> timing(new FakeTiming()); |
191 throttler_.SetTiming(std::move(timing)); | 212 throttler_.SetTiming(std::move(timing)); |
192 } | 213 } |
193 | 214 |
| 215 static FakeDatagramServerSocket* GetSocketFromHost( |
| 216 P2PSocketHostUdp* socket_host) { |
| 217 return static_cast<FakeDatagramServerSocket*>(socket_host->socket_.get()); |
| 218 } |
| 219 |
194 P2PMessageThrottler throttler_; | 220 P2PMessageThrottler throttler_; |
195 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; | 221 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; |
196 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. | 222 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. |
197 std::unique_ptr<P2PSocketHostUdp> socket_host_; | 223 std::unique_ptr<P2PSocketHostUdp> socket_host_; |
198 MockIPCSender sender_; | 224 MockIPCSender sender_; |
199 | 225 |
200 net::IPEndPoint local_address_; | 226 net::IPEndPoint local_address_; |
201 | 227 |
202 net::IPEndPoint dest1_; | 228 net::IPEndPoint dest1_; |
203 net::IPEndPoint dest2_; | 229 net::IPEndPoint dest2_; |
204 }; | 230 }; |
205 | 231 |
206 // Verify that we can send STUN messages before we receive anything | 232 // Verify that we can send STUN messages before we receive anything |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 // This packet will be dropped, as limit only for a single packet. | 400 // This packet will be dropped, as limit only for a single packet. |
375 socket_host_->Send(dest3, packet1, options, 0); | 401 socket_host_->Send(dest3, packet1, options, 0); |
376 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); | 402 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); |
377 // This packet should also be dropped. | 403 // This packet should also be dropped. |
378 socket_host_->Send(dest4, packet1, options, 0); | 404 socket_host_->Send(dest4, packet1, options, 0); |
379 // |dest1| is known, we can send as many packets to it. | 405 // |dest1| is known, we can send as many packets to it. |
380 socket_host_->Send(dest1_, packet1, options, 0); | 406 socket_host_->Send(dest1_, packet1, options, 0); |
381 ASSERT_EQ(sent_packets_.size(), 4U); | 407 ASSERT_EQ(sent_packets_.size(), 4U); |
382 } | 408 } |
383 | 409 |
| 410 // Verify that we can open UDP sockets listening in a given port range, |
| 411 // and fail if all ports in the range are already in use. |
| 412 TEST_F(P2PSocketHostUdpTest, PortRangeImplicitPort) { |
| 413 const uint16_t min_port = 10000; |
| 414 const uint16_t max_port = 10001; |
| 415 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets; |
| 416 std::vector<uint16_t> used_ports; |
| 417 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory = |
| 418 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports); |
| 419 P2PMessageThrottler throttler; |
| 420 MockIPCSender sender; |
| 421 EXPECT_CALL( |
| 422 sender, |
| 423 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) |
| 424 .Times(max_port - min_port + 1) |
| 425 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 426 |
| 427 for (unsigned port = min_port; port <= max_port; ++port) { |
| 428 std::unique_ptr<P2PSocketHostUdp> socket_host( |
| 429 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); |
| 430 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0); |
| 431 bool rv = socket_host->Init(local_address, min_port, max_port, |
| 432 P2PHostAndIPEndPoint()); |
| 433 EXPECT_TRUE(rv); |
| 434 |
| 435 FakeDatagramServerSocket* socket = GetSocketFromHost(socket_host.get()); |
| 436 net::IPEndPoint bound_address; |
| 437 socket->GetLocalAddress(&bound_address); |
| 438 EXPECT_EQ(port, bound_address.port()); |
| 439 } |
| 440 |
| 441 EXPECT_CALL(sender, |
| 442 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnError::ID)))) |
| 443 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 444 std::unique_ptr<P2PSocketHostUdp> socket_host( |
| 445 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); |
| 446 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0); |
| 447 bool rv = socket_host->Init(local_address, min_port, max_port, |
| 448 P2PHostAndIPEndPoint()); |
| 449 EXPECT_FALSE(rv); |
| 450 } |
| 451 |
| 452 // Verify that we can open a UDP socket listening in a given port included in |
| 453 // a given valid range. |
| 454 TEST_F(P2PSocketHostUdpTest, PortRangeExplictValidPort) { |
| 455 const uint16_t min_port = 10000; |
| 456 const uint16_t max_port = 10001; |
| 457 const uint16_t valid_port = min_port; |
| 458 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets; |
| 459 std::vector<uint16_t> used_ports; |
| 460 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory = |
| 461 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports); |
| 462 P2PMessageThrottler throttler; |
| 463 MockIPCSender sender; |
| 464 EXPECT_CALL( |
| 465 sender, |
| 466 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) |
| 467 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 468 |
| 469 std::unique_ptr<P2PSocketHostUdp> socket_host( |
| 470 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); |
| 471 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, valid_port); |
| 472 bool rv = socket_host->Init(local_address, min_port, max_port, |
| 473 P2PHostAndIPEndPoint()); |
| 474 EXPECT_TRUE(rv); |
| 475 |
| 476 FakeDatagramServerSocket* socket = GetSocketFromHost(socket_host.get()); |
| 477 net::IPEndPoint bound_address; |
| 478 socket->GetLocalAddress(&bound_address); |
| 479 EXPECT_EQ(local_address.port(), bound_address.port()); |
| 480 } |
| 481 |
| 482 // Verify that we cannot open a UDP socket listening in a given port not |
| 483 // included in a given valid range. |
| 484 TEST_F(P2PSocketHostUdpTest, PortRangeExplictInvalidPort) { |
| 485 const uint16_t min_port = 10000; |
| 486 const uint16_t max_port = 10001; |
| 487 const uint16_t invalid_port = max_port + 1; |
| 488 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets; |
| 489 std::vector<uint16_t> used_ports; |
| 490 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory = |
| 491 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports); |
| 492 P2PMessageThrottler throttler; |
| 493 MockIPCSender sender; |
| 494 EXPECT_CALL(sender, |
| 495 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnError::ID)))) |
| 496 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 497 |
| 498 std::unique_ptr<P2PSocketHostUdp> socket_host( |
| 499 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); |
| 500 net::IPEndPoint local_address = |
| 501 ParseAddress(kTestLocalIpAddress, invalid_port); |
| 502 bool rv = socket_host->Init(local_address, min_port, max_port, |
| 503 P2PHostAndIPEndPoint()); |
| 504 EXPECT_FALSE(rv); |
| 505 } |
| 506 |
384 } // namespace content | 507 } // namespace content |
OLD | NEW |