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

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: Fix compile issues 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 NOTREACHED(); 55 NOTREACHED();
55 return net::ERR_SOCKET_NOT_CONNECTED; 56 return net::ERR_SOCKET_NOT_CONNECTED;
56 } 57 }
57 58
58 int GetLocalAddress(net::IPEndPoint* address) const override { 59 int GetLocalAddress(net::IPEndPoint* address) const override {
59 *address = address_; 60 *address = address_;
60 return 0; 61 return 0;
61 } 62 }
62 63
63 int Listen(const net::IPEndPoint& address) override { 64 int Listen(const net::IPEndPoint& address) override {
65 if (address.port() != kTestPort1) {
66 for (auto used_port : *used_ports) {
67 if (used_port == address.port())
68 return -1;
69 }
70 used_ports->push_back(address.port());
71 }
72
64 address_ = address; 73 address_ = address;
65 return 0; 74 return 0;
66 } 75 }
67 76
68 int RecvFrom(net::IOBuffer* buf, 77 int RecvFrom(net::IOBuffer* buf,
69 int buf_len, 78 int buf_len,
70 net::IPEndPoint* address, 79 net::IPEndPoint* address,
71 const net::CompletionCallback& callback) override { 80 const net::CompletionCallback& callback) override {
72 CHECK(recv_callback_.is_null()); 81 CHECK(recv_callback_.is_null());
73 if (incoming_packets_.size() > 0) { 82 if (incoming_packets_.size() > 0) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 private: 165 private:
157 net::IPEndPoint address_; 166 net::IPEndPoint address_;
158 std::deque<UDPPacket>* sent_packets_; 167 std::deque<UDPPacket>* sent_packets_;
159 std::deque<UDPPacket> incoming_packets_; 168 std::deque<UDPPacket> incoming_packets_;
160 net::BoundNetLog net_log_; 169 net::BoundNetLog net_log_;
161 170
162 scoped_refptr<net::IOBuffer> recv_buffer_; 171 scoped_refptr<net::IOBuffer> recv_buffer_;
163 net::IPEndPoint* recv_address_; 172 net::IPEndPoint* recv_address_;
164 int recv_size_; 173 int recv_size_;
165 net::CompletionCallback recv_callback_; 174 net::CompletionCallback recv_callback_;
175 static std::vector<uint16_t>* used_ports;
166 }; 176 };
167 177
178 std::vector<uint16_t>* FakeDatagramServerSocket::used_ports =
179 new std::vector<uint16_t>;
tommi (sloooow) - chröme 2016/07/11 17:54:59 would be good to avoid global vars like this consi
Guido Urdaneta 2016/07/12 10:15:00 Done.
180
181 std::unique_ptr<net::DatagramServerSocket> FakeDatagramServerSocketFactory(
182 std::deque<FakeDatagramServerSocket::UDPPacket>* sent_packets) {
183 return base::WrapUnique(new FakeDatagramServerSocket(sent_packets));
184 }
185
168 } // namespace 186 } // namespace
169 187
170 namespace content { 188 namespace content {
171 189
172 class P2PSocketHostUdpTest : public testing::Test { 190 class P2PSocketHostUdpTest : public testing::Test {
173 protected: 191 protected:
174 void SetUp() override { 192 void SetUp() override {
175 EXPECT_CALL( 193 EXPECT_CALL(
176 sender_, 194 sender_,
177 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID)))) 195 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID))))
178 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 196 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
179 197
180 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); 198 socket_host_.reset(new P2PSocketHostUdp(
181 socket_ = new FakeDatagramServerSocket(&sent_packets_); 199 &sender_, 0, &throttler_,
182 socket_host_->socket_.reset(socket_); 200 base::Bind(&FakeDatagramServerSocketFactory, &sent_packets_)));
183 201
184 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); 202 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
185 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); 203 socket_host_->Init(local_address_, 0, 0, P2PHostAndIPEndPoint());
204 socket_ = GetSocketFromHost(socket_host_.get());
186 205
187 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); 206 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1);
188 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); 207 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2);
189 208
190 std::unique_ptr<rtc::Timing> timing(new FakeTiming()); 209 std::unique_ptr<rtc::Timing> timing(new FakeTiming());
191 throttler_.SetTiming(std::move(timing)); 210 throttler_.SetTiming(std::move(timing));
192 } 211 }
193 212
213 static FakeDatagramServerSocket* GetSocketFromHost(
214 P2PSocketHostUdp* socket_host) {
215 return static_cast<FakeDatagramServerSocket*>(socket_host->socket_.get());
216 }
217
194 P2PMessageThrottler throttler_; 218 P2PMessageThrottler throttler_;
195 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; 219 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_;
196 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. 220 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|.
197 std::unique_ptr<P2PSocketHostUdp> socket_host_; 221 std::unique_ptr<P2PSocketHostUdp> socket_host_;
198 MockIPCSender sender_; 222 MockIPCSender sender_;
199 223
200 net::IPEndPoint local_address_; 224 net::IPEndPoint local_address_;
201 225
202 net::IPEndPoint dest1_; 226 net::IPEndPoint dest1_;
203 net::IPEndPoint dest2_; 227 net::IPEndPoint dest2_;
204 }; 228 };
205 229
206 // Verify that we can send STUN messages before we receive anything 230 // 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. 398 // This packet will be dropped, as limit only for a single packet.
375 socket_host_->Send(dest3, packet1, options, 0); 399 socket_host_->Send(dest3, packet1, options, 0);
376 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); 400 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224);
377 // This packet should also be dropped. 401 // This packet should also be dropped.
378 socket_host_->Send(dest4, packet1, options, 0); 402 socket_host_->Send(dest4, packet1, options, 0);
379 // |dest1| is known, we can send as many packets to it. 403 // |dest1| is known, we can send as many packets to it.
380 socket_host_->Send(dest1_, packet1, options, 0); 404 socket_host_->Send(dest1_, packet1, options, 0);
381 ASSERT_EQ(sent_packets_.size(), 4U); 405 ASSERT_EQ(sent_packets_.size(), 4U);
382 } 406 }
383 407
408 // Verify that we can open UDP sockets listening in a given port range,
409 // and fail if all ports in the range are already in use.
410 TEST_F(P2PSocketHostUdpTest, PortRange) {
411 const uint16_t min_port = 10000;
412 const uint16_t max_port = 10001;
413 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets;
414 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory =
415 base::Bind(&FakeDatagramServerSocketFactory, &sent_packets);
416 P2PMessageThrottler throttler;
417 MockIPCSender sender;
418 EXPECT_CALL(
419 sender,
420 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSocketCreated::ID))))
421 .Times(max_port - min_port + 1);
422
423 for (unsigned port = min_port; port <= max_port; ++port) {
424 std::unique_ptr<P2PSocketHostUdp> socket_host(
425 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
426 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0);
427 bool rv = socket_host->Init(local_address, min_port, max_port,
428 P2PHostAndIPEndPoint());
429 EXPECT_TRUE(rv);
430
431 FakeDatagramServerSocket* socket = GetSocketFromHost(socket_host.get());
432 net::IPEndPoint bound_address;
433 socket->GetLocalAddress(&bound_address);
434 EXPECT_EQ(port, bound_address.port());
435 }
436
437 EXPECT_CALL(sender,
438 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnError::ID))));
439 std::unique_ptr<P2PSocketHostUdp> socket_host(
440 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory));
441 net::IPEndPoint local_address = ParseAddress(kTestLocalIpAddress, 0);
442 bool rv = socket_host->Init(local_address, min_port, max_port,
443 P2PHostAndIPEndPoint());
444 EXPECT_FALSE(rv);
445 }
446
384 } // namespace content 447 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698