Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_udp_unittest.cc
blob5d67a24fc3327563c21773cc973ae8ba7084e7b0
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/webrtc/base/timing.h"
22 using ::testing::_;
23 using ::testing::DeleteArg;
24 using ::testing::DoAll;
25 using ::testing::Return;
27 namespace {
29 class FakeTiming : public rtc::Timing {
30 public:
31 FakeTiming() : now_(0.0) {}
32 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 void Close() override {}
51 int GetPeerAddress(net::IPEndPoint* address) const override {
52 NOTREACHED();
53 return net::ERR_SOCKET_NOT_CONNECTED;
56 int GetLocalAddress(net::IPEndPoint* address) const override {
57 *address = address_;
58 return 0;
61 int Listen(const net::IPEndPoint& address) override {
62 address_ = address;
63 return 0;
66 int RecvFrom(net::IOBuffer* buf,
67 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 int SendTo(net::IOBuffer* buf,
89 int buf_len,
90 const net::IPEndPoint& address,
91 const net::CompletionCallback& callback) override {
92 scoped_refptr<net::IOBuffer> buffer(buf);
93 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len);
94 sent_packets_->push_back(UDPPacket(address, data_vector));
95 return buf_len;
98 int SetReceiveBufferSize(int32 size) override { return net::OK; }
100 int SetSendBufferSize(int32 size) override { return net::OK; }
102 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) {
103 if (!recv_callback_.is_null()) {
104 int size = std::min(recv_size_, static_cast<int>(data.size()));
105 memcpy(recv_buffer_->data(), &*data.begin(), size);
106 *recv_address_ = address;
107 net::CompletionCallback cb = recv_callback_;
108 recv_callback_.Reset();
109 recv_buffer_ = NULL;
110 cb.Run(size);
111 } else {
112 incoming_packets_.push_back(UDPPacket(address, data));
116 const net::BoundNetLog& NetLog() const override { return net_log_; }
118 void AllowAddressReuse() override { NOTIMPLEMENTED(); }
120 void AllowBroadcast() override { NOTIMPLEMENTED(); }
122 int JoinGroup(const net::IPAddressNumber& group_address) const override {
123 NOTIMPLEMENTED();
124 return net::ERR_NOT_IMPLEMENTED;
127 int LeaveGroup(const net::IPAddressNumber& group_address) const override {
128 NOTIMPLEMENTED();
129 return net::ERR_NOT_IMPLEMENTED;
132 int SetMulticastInterface(uint32 interface_index) override {
133 NOTIMPLEMENTED();
134 return net::ERR_NOT_IMPLEMENTED;
137 int SetMulticastTimeToLive(int time_to_live) override {
138 NOTIMPLEMENTED();
139 return net::ERR_NOT_IMPLEMENTED;
142 int SetMulticastLoopbackMode(bool loopback) override {
143 NOTIMPLEMENTED();
144 return net::ERR_NOT_IMPLEMENTED;
147 int SetDiffServCodePoint(net::DiffServCodePoint dscp) override {
148 NOTIMPLEMENTED();
149 return net::ERR_NOT_IMPLEMENTED;
152 void DetachFromThread() override { NOTIMPLEMENTED(); }
154 private:
155 net::IPEndPoint address_;
156 std::deque<UDPPacket>* sent_packets_;
157 std::deque<UDPPacket> incoming_packets_;
158 net::BoundNetLog net_log_;
160 scoped_refptr<net::IOBuffer> recv_buffer_;
161 net::IPEndPoint* recv_address_;
162 int recv_size_;
163 net::CompletionCallback recv_callback_;
166 } // namespace
168 namespace content {
170 class P2PSocketHostUdpTest : public testing::Test {
171 protected:
172 void SetUp() override {
173 EXPECT_CALL(sender_, Send(
174 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID))))
175 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
177 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_));
178 socket_ = new FakeDatagramServerSocket(&sent_packets_);
179 socket_host_->socket_.reset(socket_);
181 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
182 socket_host_->Init(local_address_, P2PHostAndIPEndPoint());
184 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1);
185 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2);
187 scoped_ptr<rtc::Timing> timing(new FakeTiming());
188 throttler_.SetTiming(timing.Pass());
191 P2PMessageThrottler throttler_;
192 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_;
193 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|.
194 scoped_ptr<P2PSocketHostUdp> socket_host_;
195 MockIPCSender sender_;
197 net::IPEndPoint local_address_;
199 net::IPEndPoint dest1_;
200 net::IPEndPoint dest2_;
203 // Verify that we can send STUN messages before we receive anything
204 // from the other side.
205 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) {
206 EXPECT_CALL(sender_, Send(
207 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
208 .Times(3)
209 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
211 rtc::PacketOptions options;
212 std::vector<char> packet1;
213 CreateStunRequest(&packet1);
214 socket_host_->Send(dest1_, packet1, options, 0);
216 std::vector<char> packet2;
217 CreateStunResponse(&packet2);
218 socket_host_->Send(dest1_, packet2, options, 0);
220 std::vector<char> packet3;
221 CreateStunError(&packet3);
222 socket_host_->Send(dest1_, packet3, options, 0);
224 ASSERT_EQ(sent_packets_.size(), 3U);
225 ASSERT_EQ(sent_packets_[0].second, packet1);
226 ASSERT_EQ(sent_packets_[1].second, packet2);
227 ASSERT_EQ(sent_packets_[2].second, packet3);
230 // Verify that no data packets can be sent before STUN binding has
231 // finished.
232 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) {
233 EXPECT_CALL(sender_, Send(
234 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
235 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
237 rtc::PacketOptions options;
238 std::vector<char> packet;
239 CreateRandomPacket(&packet);
240 socket_host_->Send(dest1_, packet, options, 0);
242 ASSERT_EQ(sent_packets_.size(), 0U);
245 // Verify that we can send data after we've received STUN request
246 // from the other side.
247 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) {
248 // Receive packet from |dest1_|.
249 std::vector<char> request_packet;
250 CreateStunRequest(&request_packet);
252 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
253 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
254 socket_->ReceivePacket(dest1_, request_packet);
256 // Now we should be able to send any data to |dest1_|.
257 EXPECT_CALL(sender_, Send(
258 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
259 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
261 rtc::PacketOptions options;
262 std::vector<char> packet;
263 CreateRandomPacket(&packet);
264 socket_host_->Send(dest1_, packet, options, 0);
266 ASSERT_EQ(1U, sent_packets_.size());
267 ASSERT_EQ(dest1_, sent_packets_[0].first);
270 // Verify that we can send data after we've received STUN response
271 // from the other side.
272 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) {
273 // Receive packet from |dest1_|.
274 std::vector<char> request_packet;
275 CreateStunRequest(&request_packet);
277 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
278 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
279 socket_->ReceivePacket(dest1_, request_packet);
281 // Now we should be able to send any data to |dest1_|.
282 EXPECT_CALL(sender_, Send(
283 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
284 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
286 rtc::PacketOptions options;
287 std::vector<char> packet;
288 CreateRandomPacket(&packet);
289 socket_host_->Send(dest1_, packet, options, 0);
291 ASSERT_EQ(1U, sent_packets_.size());
292 ASSERT_EQ(dest1_, sent_packets_[0].first);
295 // Verify messages still cannot be sent to an unathorized host after
296 // successful binding with different host.
297 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) {
298 // Receive packet from |dest1_|.
299 std::vector<char> request_packet;
300 CreateStunRequest(&request_packet);
302 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
303 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
304 socket_->ReceivePacket(dest1_, request_packet);
306 // Should fail when trying to send the same packet to |dest2_|.
307 rtc::PacketOptions options;
308 std::vector<char> packet;
309 CreateRandomPacket(&packet);
310 EXPECT_CALL(sender_, Send(
311 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
312 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
313 socket_host_->Send(dest2_, packet, options, 0);
316 // Verify throttler not allowing unlimited sending of ICE messages to
317 // any destination.
318 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) {
319 EXPECT_CALL(sender_, Send(
320 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
321 .Times(2)
322 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
324 rtc::PacketOptions options;
325 std::vector<char> packet1;
326 CreateStunRequest(&packet1);
327 throttler_.SetSendIceBandwidth(packet1.size() * 2);
328 socket_host_->Send(dest1_, packet1, options, 0);
329 socket_host_->Send(dest2_, packet1, options, 0);
331 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222);
332 // This packet must be dropped by the throttler.
333 socket_host_->Send(dest3, packet1, options, 0);
334 ASSERT_EQ(sent_packets_.size(), 2U);
337 // Verify we can send packets to a known destination when ICE throttling is
338 // active.
339 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) {
340 // Receive packet from |dest1_|.
341 std::vector<char> request_packet;
342 CreateStunRequest(&request_packet);
344 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
345 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
346 socket_->ReceivePacket(dest1_, request_packet);
348 EXPECT_CALL(sender_, Send(
349 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
350 .Times(4)
351 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
353 rtc::PacketOptions options;
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, options, 0);
359 // Trying to send the packet to dest1_ in the same window. It should go.
360 socket_host_->Send(dest1_, packet1, options, 0);
362 // Throttler should allow this packet to go through.
363 socket_host_->Send(dest2_, packet1, options, 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, options, 0);
368 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224);
369 // This packet should also be dropped.
370 socket_host_->Send(dest4, packet1, options, 0);
371 // |dest1| is known, we can send as many packets to it.
372 socket_host_->Send(dest1_, packet1, options, 0);
373 ASSERT_EQ(sent_packets_.size(), 4U);
376 } // namespace content