Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_udp_unittest.cc
blob3ccacfc288985413d4b40a34bdc569a3fd426886
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
7 #include <deque>
8 #include <vector>
10 #include "base/logging.h"
11 #include "base/sys_byteorder.h"
12 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h"
13 #include "content/browser/renderer_host/p2p/socket_host_throttler.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/net_errors.h"
17 #include "net/udp/datagram_server_socket.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/libjingle/source/talk/base/timing.h"
22 using ::testing::_;
23 using ::testing::DeleteArg;
24 using ::testing::DoAll;
25 using ::testing::Return;
27 namespace {
29 class FakeTiming : public talk_base::Timing {
30 public:
31 FakeTiming() : now_(0.0) {}
32 virtual double TimerNow() OVERRIDE { return now_; }
33 void set_now(double now) { now_ = now; }
35 private:
36 double now_;
39 class FakeDatagramServerSocket : public net::DatagramServerSocket {
40 public:
41 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket;
43 // P2PSocketHostUdp destroyes a socket on errors so sent packets
44 // need to be stored outside of this object.
45 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets)
46 : sent_packets_(sent_packets) {
49 virtual void Close() OVERRIDE {
52 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE {
53 NOTREACHED();
54 return net::ERR_SOCKET_NOT_CONNECTED;
57 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE {
58 *address = address_;
59 return 0;
62 virtual int Listen(const net::IPEndPoint& address) OVERRIDE {
63 address_ = address;
64 return 0;
67 virtual int RecvFrom(net::IOBuffer* buf, int buf_len,
68 net::IPEndPoint* address,
69 const net::CompletionCallback& callback) OVERRIDE {
70 CHECK(recv_callback_.is_null());
71 if (incoming_packets_.size() > 0) {
72 scoped_refptr<net::IOBuffer> buffer(buf);
73 int size = std::min(
74 static_cast<int>(incoming_packets_.front().second.size()), buf_len);
75 memcpy(buffer->data(), &*incoming_packets_.front().second.begin(), size);
76 *address = incoming_packets_.front().first;
77 incoming_packets_.pop_front();
78 return size;
79 } else {
80 recv_callback_ = callback;
81 recv_buffer_ = buf;
82 recv_size_ = buf_len;
83 recv_address_ = address;
84 return net::ERR_IO_PENDING;
88 virtual int SendTo(net::IOBuffer* buf, int buf_len,
89 const net::IPEndPoint& address,
90 const net::CompletionCallback& callback) OVERRIDE {
91 scoped_refptr<net::IOBuffer> buffer(buf);
92 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len);
93 sent_packets_->push_back(UDPPacket(address, data_vector));
94 return buf_len;
97 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
98 return true;
101 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
102 return true;
105 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) {
106 if (!recv_callback_.is_null()) {
107 int size = std::min(recv_size_, static_cast<int>(data.size()));
108 memcpy(recv_buffer_->data(), &*data.begin(), size);
109 *recv_address_ = address;
110 net::CompletionCallback cb = recv_callback_;
111 recv_callback_.Reset();
112 recv_buffer_ = NULL;
113 cb.Run(size);
114 } else {
115 incoming_packets_.push_back(UDPPacket(address, data));
119 virtual const net::BoundNetLog& NetLog() const OVERRIDE {
120 return net_log_;
123 virtual void AllowAddressReuse() OVERRIDE {
124 NOTIMPLEMENTED();
127 virtual void AllowBroadcast() OVERRIDE {
128 NOTIMPLEMENTED();
131 virtual int JoinGroup(
132 const net::IPAddressNumber& group_address) const OVERRIDE {
133 NOTIMPLEMENTED();
134 return net::ERR_NOT_IMPLEMENTED;
137 virtual int LeaveGroup(
138 const net::IPAddressNumber& group_address) const OVERRIDE {
139 NOTIMPLEMENTED();
140 return net::ERR_NOT_IMPLEMENTED;
143 virtual int SetMulticastInterface(uint32 interface_index) OVERRIDE {
144 NOTIMPLEMENTED();
145 return net::ERR_NOT_IMPLEMENTED;
148 virtual int SetMulticastTimeToLive(int time_to_live) OVERRIDE {
149 NOTIMPLEMENTED();
150 return net::ERR_NOT_IMPLEMENTED;
153 virtual int SetMulticastLoopbackMode(bool loopback) OVERRIDE {
154 NOTIMPLEMENTED();
155 return net::ERR_NOT_IMPLEMENTED;
158 virtual int SetDiffServCodePoint(net::DiffServCodePoint dscp) OVERRIDE {
159 NOTIMPLEMENTED();
160 return net::ERR_NOT_IMPLEMENTED;
163 private:
164 net::IPEndPoint address_;
165 std::deque<UDPPacket>* sent_packets_;
166 std::deque<UDPPacket> incoming_packets_;
167 net::BoundNetLog net_log_;
169 scoped_refptr<net::IOBuffer> recv_buffer_;
170 net::IPEndPoint* recv_address_;
171 int recv_size_;
172 net::CompletionCallback recv_callback_;
175 } // namespace
177 namespace content {
179 class P2PSocketHostUdpTest : public testing::Test {
180 protected:
181 virtual void SetUp() OVERRIDE {
182 EXPECT_CALL(sender_, Send(
183 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID))))
184 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
186 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_));
187 socket_ = new FakeDatagramServerSocket(&sent_packets_);
188 socket_host_->socket_.reset(socket_);
190 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
191 socket_host_->Init(local_address_, net::IPEndPoint());
193 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1);
194 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2);
196 scoped_ptr<talk_base::Timing> timing(new FakeTiming());
197 throttler_.SetTiming(timing.Pass());
200 P2PMessageThrottler throttler_;
201 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_;
202 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|.
203 scoped_ptr<P2PSocketHostUdp> socket_host_;
204 MockIPCSender sender_;
206 net::IPEndPoint local_address_;
208 net::IPEndPoint dest1_;
209 net::IPEndPoint dest2_;
212 // Verify that we can send STUN messages before we receive anything
213 // from the other side.
214 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) {
215 EXPECT_CALL(sender_, Send(
216 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
217 .Times(3)
218 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
220 std::vector<char> packet1;
221 CreateStunRequest(&packet1);
222 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0);
224 std::vector<char> packet2;
225 CreateStunResponse(&packet2);
226 socket_host_->Send(dest1_, packet2, net::DSCP_NO_CHANGE, 0);
228 std::vector<char> packet3;
229 CreateStunError(&packet3);
230 socket_host_->Send(dest1_, packet3, net::DSCP_NO_CHANGE, 0);
232 ASSERT_EQ(sent_packets_.size(), 3U);
233 ASSERT_EQ(sent_packets_[0].second, packet1);
234 ASSERT_EQ(sent_packets_[1].second, packet2);
235 ASSERT_EQ(sent_packets_[2].second, packet3);
238 // Verify that no data packets can be sent before STUN binding has
239 // finished.
240 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) {
241 EXPECT_CALL(sender_, Send(
242 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
243 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
245 std::vector<char> packet;
246 CreateRandomPacket(&packet);
247 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0);
249 ASSERT_EQ(sent_packets_.size(), 0U);
252 // Verify that we can send data after we've received STUN request
253 // from the other side.
254 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) {
255 // Receive packet from |dest1_|.
256 std::vector<char> request_packet;
257 CreateStunRequest(&request_packet);
259 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
260 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
261 socket_->ReceivePacket(dest1_, request_packet);
263 // Now we should be able to send any data to |dest1_|.
264 EXPECT_CALL(sender_, Send(
265 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
266 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
267 std::vector<char> packet;
268 CreateRandomPacket(&packet);
269 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0);
271 ASSERT_EQ(1U, sent_packets_.size());
272 ASSERT_EQ(dest1_, sent_packets_[0].first);
275 // Verify that we can send data after we've received STUN response
276 // from the other side.
277 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) {
278 // Receive packet from |dest1_|.
279 std::vector<char> request_packet;
280 CreateStunRequest(&request_packet);
282 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
283 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
284 socket_->ReceivePacket(dest1_, request_packet);
286 // Now we should be able to send any data to |dest1_|.
287 EXPECT_CALL(sender_, Send(
288 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
289 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
290 std::vector<char> packet;
291 CreateRandomPacket(&packet);
292 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0);
294 ASSERT_EQ(1U, sent_packets_.size());
295 ASSERT_EQ(dest1_, sent_packets_[0].first);
298 // Verify messages still cannot be sent to an unathorized host after
299 // successful binding with different host.
300 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) {
301 // Receive packet from |dest1_|.
302 std::vector<char> request_packet;
303 CreateStunRequest(&request_packet);
305 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
306 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
307 socket_->ReceivePacket(dest1_, request_packet);
309 // Should fail when trying to send the same packet to |dest2_|.
310 std::vector<char> packet;
311 CreateRandomPacket(&packet);
312 EXPECT_CALL(sender_, Send(
313 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
314 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
315 socket_host_->Send(dest2_, packet, net::DSCP_NO_CHANGE, 0);
318 // Verify throttler not allowing unlimited sending of ICE messages to
319 // any destination.
320 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) {
321 EXPECT_CALL(sender_, Send(
322 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
323 .Times(2)
324 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
326 std::vector<char> packet1;
327 CreateStunRequest(&packet1);
328 throttler_.SetSendIceBandwidth(packet1.size() * 2);
329 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0);
330 socket_host_->Send(dest2_, packet1, net::DSCP_NO_CHANGE, 0);
332 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222);
333 // This packet must be dropped by the throttler.
334 socket_host_->Send(dest3, packet1, net::DSCP_NO_CHANGE, 0);
335 ASSERT_EQ(sent_packets_.size(), 2U);
338 // Verify we can send packets to a known destination when ICE throttling is
339 // active.
340 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) {
341 // Receive packet from |dest1_|.
342 std::vector<char> request_packet;
343 CreateStunRequest(&request_packet);
345 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
346 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
347 socket_->ReceivePacket(dest1_, request_packet);
349 EXPECT_CALL(sender_, Send(
350 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
351 .Times(4)
352 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
354 std::vector<char> packet1;
355 CreateStunRequest(&packet1);
356 throttler_.SetSendIceBandwidth(packet1.size());
357 // |dest1_| is known address, throttling will not be applied.
358 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0);
359 // Trying to send the packet to dest1_ in the same window. It should go.
360 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0);
362 // Throttler should allow this packet to go through.
363 socket_host_->Send(dest2_, packet1, net::DSCP_NO_CHANGE, 0);
365 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2223);
366 // This packet will be dropped, as limit only for a single packet.
367 socket_host_->Send(dest3, packet1, net::DSCP_NO_CHANGE, 0);
368 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224);
369 // This packet should also be dropped.
370 socket_host_->Send(dest4, packet1, net::DSCP_NO_CHANGE, 0);
371 // |dest1| is known, we can send as many packets to it.
372 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0);
373 ASSERT_EQ(sent_packets_.size(), 4U);
376 } // namespace content