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

Side by Side Diff: content/browser/renderer_host/p2p/socket_host_udp_unittest.cc

Issue 2140693002: Support port range for IPC P2P UDP sockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address tommi's comments Created 4 years, 5 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 unified diff | Download patch
OLDNEW
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
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.
48 FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets,
49 std::vector<uint16_t>* used_ports)
50 : sent_packets_(sent_packets),
51 recv_address_(nullptr),
52 recv_size_(0),
53 used_ports_(used_ports) {}
54
47 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) 55 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets)
48 : sent_packets_(sent_packets) { 56 : FakeDatagramServerSocket(sent_packets, nullptr) {}
49 }
50 57
51 void Close() override {} 58 void Close() override {}
52 59
53 int GetPeerAddress(net::IPEndPoint* address) const override { 60 int GetPeerAddress(net::IPEndPoint* address) const override {
54 NOTREACHED(); 61 NOTREACHED();
55 return net::ERR_SOCKET_NOT_CONNECTED; 62 return net::ERR_SOCKET_NOT_CONNECTED;
56 } 63 }
57 64
58 int GetLocalAddress(net::IPEndPoint* address) const override { 65 int GetLocalAddress(net::IPEndPoint* address) const override {
59 *address = address_; 66 *address = address_;
60 return 0; 67 return 0;
61 } 68 }
62 69
63 int Listen(const net::IPEndPoint& address) override { 70 int Listen(const net::IPEndPoint& address) override {
71 if (used_ports_) {
72 for (auto used_port : *used_ports_) {
73 if (used_port == address.port())
74 return -1;
75 }
76 used_ports_->push_back(address.port());
77 }
78
64 address_ = address; 79 address_ = address;
65 return 0; 80 return 0;
66 } 81 }
67 82
68 int RecvFrom(net::IOBuffer* buf, 83 int RecvFrom(net::IOBuffer* buf,
69 int buf_len, 84 int buf_len,
70 net::IPEndPoint* address, 85 net::IPEndPoint* address,
71 const net::CompletionCallback& callback) override { 86 const net::CompletionCallback& callback) override {
72 CHECK(recv_callback_.is_null()); 87 CHECK(recv_callback_.is_null());
73 if (incoming_packets_.size() > 0) { 88 if (incoming_packets_.size() > 0) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 private: 171 private:
157 net::IPEndPoint address_; 172 net::IPEndPoint address_;
158 std::deque<UDPPacket>* sent_packets_; 173 std::deque<UDPPacket>* sent_packets_;
159 std::deque<UDPPacket> incoming_packets_; 174 std::deque<UDPPacket> incoming_packets_;
160 net::BoundNetLog net_log_; 175 net::BoundNetLog net_log_;
161 176
162 scoped_refptr<net::IOBuffer> recv_buffer_; 177 scoped_refptr<net::IOBuffer> recv_buffer_;
163 net::IPEndPoint* recv_address_; 178 net::IPEndPoint* recv_address_;
164 int recv_size_; 179 int recv_size_;
165 net::CompletionCallback recv_callback_; 180 net::CompletionCallback recv_callback_;
181 std::vector<uint16_t>* used_ports_;
166 }; 182 };
167 183
184 std::unique_ptr<net::DatagramServerSocket> CreateFakeDatagramServerSocket(
185 std::deque<FakeDatagramServerSocket::UDPPacket>* sent_packets,
186 std::vector<uint16_t>* used_ports) {
187 return base::WrapUnique(
188 new FakeDatagramServerSocket(sent_packets, used_ports));
189 }
190
168 } // namespace 191 } // namespace
169 192
170 namespace content { 193 namespace content {
171 194
172 class P2PSocketHostUdpTest : public testing::Test { 195 class P2PSocketHostUdpTest : public testing::Test {
173 protected: 196 protected:
174 void SetUp() override { 197 void SetUp() override {
175 EXPECT_CALL( 198 EXPECT_CALL(
176 sender_, 199 sender_,
177 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) 200 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID))))
178 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 201 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
179 202
180 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); 203 socket_host_.reset(new P2PSocketHostUdp(
181 socket_ = new FakeDatagramServerSocket(&sent_packets_); 204 &sender_, 0, &throttler_,
182 socket_host_->socket_.reset(socket_); 205 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets_, nullptr)));
183 206
184 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); 207 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
185 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); 208 socket_host_->Init(local_address_, 0, 0, P2PHostAndIPEndPoint());
209 socket_ = GetSocketFromHost(socket_host_.get());
186 210
187 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); 211 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1);
188 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); 212 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2);
189 213
190 std::unique_ptr<rtc::Timing> timing(new FakeTiming()); 214 std::unique_ptr<rtc::Timing> timing(new FakeTiming());
191 throttler_.SetTiming(std::move(timing)); 215 throttler_.SetTiming(std::move(timing));
192 } 216 }
193 217
218 static FakeDatagramServerSocket* GetSocketFromHost(
219 P2PSocketHostUdp* socket_host) {
220 return static_cast<FakeDatagramServerSocket*>(socket_host->socket_.get());
221 }
222
194 P2PMessageThrottler throttler_; 223 P2PMessageThrottler throttler_;
195 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; 224 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_;
196 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. 225 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|.
197 std::unique_ptr<P2PSocketHostUdp> socket_host_; 226 std::unique_ptr<P2PSocketHostUdp> socket_host_;
198 MockIPCSender sender_; 227 MockIPCSender sender_;
199 228
200 net::IPEndPoint local_address_; 229 net::IPEndPoint local_address_;
201 230
202 net::IPEndPoint dest1_; 231 net::IPEndPoint dest1_;
203 net::IPEndPoint dest2_; 232 net::IPEndPoint dest2_;
204 }; 233 };
205 234
206 // Verify that we can send STUN messages before we receive anything 235 // Verify that we can send STUN messages before we receive anything
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 // This packet will be dropped, as limit only for a single packet. 403 // This packet will be dropped, as limit only for a single packet.
375 socket_host_->Send(dest3, packet1, options, 0); 404 socket_host_->Send(dest3, packet1, options, 0);
376 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); 405 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224);
377 // This packet should also be dropped. 406 // This packet should also be dropped.
378 socket_host_->Send(dest4, packet1, options, 0); 407 socket_host_->Send(dest4, packet1, options, 0);
379 // |dest1| is known, we can send as many packets to it. 408 // |dest1| is known, we can send as many packets to it.
380 socket_host_->Send(dest1_, packet1, options, 0); 409 socket_host_->Send(dest1_, packet1, options, 0);
381 ASSERT_EQ(sent_packets_.size(), 4U); 410 ASSERT_EQ(sent_packets_.size(), 4U);
382 } 411 }
383 412
413 // Verify that we can open UDP sockets listening in a given port range,
414 // and fail if all ports in the range are already in use.
415 TEST_F(P2PSocketHostUdpTest, PortRangeImplicitPort) {
416 const uint16_t min_port = 10000;
417 const uint16_t max_port = 10001;
418 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets;
419 std::vector<uint16_t> used_ports;
420 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory =
421 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports);
422 P2PMessageThrottler throttler;
423 MockIPCSender sender;
424 EXPECT_CALL(
425 sender,
426 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID))))
427 .Times(max_port - min_port + 1)
428 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
429
430 for (unsigned port = min_port; port <= max_port; ++port) {
431 std::unique_ptr<P2PSocketHostUdp> socket_host(
432 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
433 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0);
434 bool rv = socket_host->Init(local_address, min_port, max_port,
435 P2PHostAndIPEndPoint());
436 EXPECT_TRUE(rv);
437
438 FakeDatagramServerSocket* socket = GetSocketFromHost(socket_host.get());
439 net::IPEndPoint bound_address;
440 socket->GetLocalAddress(&bound_address);
441 EXPECT_EQ(port, bound_address.port());
442 }
443
444 EXPECT_CALL(sender,
445 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnError::ID))))
446 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
447 std::unique_ptr<P2PSocketHostUdp> socket_host(
448 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
449 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0);
450 bool rv = socket_host->Init(local_address, min_port, max_port,
451 P2PHostAndIPEndPoint());
452 EXPECT_FALSE(rv);
453 }
454
455 // Verify that we can open a UDP socket listening in a given port included in
456 // a given valid range.
457 TEST_F(P2PSocketHostUdpTest, PortRangeExplictValidPort) {
458 const uint16_t min_port = 10000;
459 const uint16_t max_port = 10001;
460 const uint16_t valid_port = min_port;
461 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets;
462 std::vector<uint16_t> used_ports;
463 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory =
464 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports);
465 P2PMessageThrottler throttler;
466 MockIPCSender sender;
467 EXPECT_CALL(
468 sender,
469 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID))))
470 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
471
472 std::unique_ptr<P2PSocketHostUdp> socket_host(
473 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
474 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, valid_port);
475 bool rv = socket_host->Init(local_address, min_port, max_port,
476 P2PHostAndIPEndPoint());
477 EXPECT_TRUE(rv);
478
479 FakeDatagramServerSocket* socket = GetSocketFromHost(socket_host.get());
480 net::IPEndPoint bound_address;
481 socket->GetLocalAddress(&bound_address);
482 EXPECT_EQ(local_address.port(), bound_address.port());
483 }
484
485 // Verify that we cannot open a UDP socket listening in a given port not
486 // included in a given valid range.
487 TEST_F(P2PSocketHostUdpTest, PortRangeExplictInvalidPort) {
488 const uint16_t min_port = 10000;
489 const uint16_t max_port = 10001;
490 const uint16_t invalid_port = max_port + 1;
491 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets;
492 std::vector<uint16_t> used_ports;
493 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory =
494 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports);
495 P2PMessageThrottler throttler;
496 MockIPCSender sender;
497 EXPECT_CALL(sender,
498 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnError::ID))))
499 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
500
501 std::unique_ptr<P2PSocketHostUdp> socket_host(
502 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
503 net::IPEndPoint local_address =
504 ParseAddress(kTestLocalIpAddress, invalid_port);
505 bool rv = socket_host->Init(local_address, min_port, max_port,
506 P2PHostAndIPEndPoint());
507 EXPECT_FALSE(rv);
508 }
509
384 } // namespace content 510 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698