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 <deque> | 7 #include <deque> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using ::testing::_; | 22 using ::testing::_; |
23 using ::testing::DeleteArg; | 23 using ::testing::DeleteArg; |
24 using ::testing::DoAll; | 24 using ::testing::DoAll; |
25 using ::testing::Return; | 25 using ::testing::Return; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 class FakeTiming : public rtc::Timing { | 29 class FakeTiming : public rtc::Timing { |
30 public: | 30 public: |
31 FakeTiming() : now_(0.0) {} | 31 FakeTiming() : now_(0.0) {} |
32 virtual double TimerNow() OVERRIDE { return now_; } | 32 virtual double TimerNow() override { return now_; } |
33 void set_now(double now) { now_ = now; } | 33 void set_now(double now) { now_ = now; } |
34 | 34 |
35 private: | 35 private: |
36 double now_; | 36 double now_; |
37 }; | 37 }; |
38 | 38 |
39 class FakeDatagramServerSocket : public net::DatagramServerSocket { | 39 class FakeDatagramServerSocket : public net::DatagramServerSocket { |
40 public: | 40 public: |
41 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; | 41 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; |
42 | 42 |
43 // P2PSocketHostUdp destroyes a socket on errors so sent packets | 43 // P2PSocketHostUdp destroyes a socket on errors so sent packets |
44 // need to be stored outside of this object. | 44 // need to be stored outside of this object. |
45 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) | 45 explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) |
46 : sent_packets_(sent_packets) { | 46 : sent_packets_(sent_packets) { |
47 } | 47 } |
48 | 48 |
49 virtual void Close() OVERRIDE { | 49 virtual void Close() override { |
50 } | 50 } |
51 | 51 |
52 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE { | 52 virtual int GetPeerAddress(net::IPEndPoint* address) const override { |
53 NOTREACHED(); | 53 NOTREACHED(); |
54 return net::ERR_SOCKET_NOT_CONNECTED; | 54 return net::ERR_SOCKET_NOT_CONNECTED; |
55 } | 55 } |
56 | 56 |
57 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { | 57 virtual int GetLocalAddress(net::IPEndPoint* address) const override { |
58 *address = address_; | 58 *address = address_; |
59 return 0; | 59 return 0; |
60 } | 60 } |
61 | 61 |
62 virtual int Listen(const net::IPEndPoint& address) OVERRIDE { | 62 virtual int Listen(const net::IPEndPoint& address) override { |
63 address_ = address; | 63 address_ = address; |
64 return 0; | 64 return 0; |
65 } | 65 } |
66 | 66 |
67 virtual int RecvFrom(net::IOBuffer* buf, int buf_len, | 67 virtual int RecvFrom(net::IOBuffer* buf, int buf_len, |
68 net::IPEndPoint* address, | 68 net::IPEndPoint* address, |
69 const net::CompletionCallback& callback) OVERRIDE { | 69 const net::CompletionCallback& callback) override { |
70 CHECK(recv_callback_.is_null()); | 70 CHECK(recv_callback_.is_null()); |
71 if (incoming_packets_.size() > 0) { | 71 if (incoming_packets_.size() > 0) { |
72 scoped_refptr<net::IOBuffer> buffer(buf); | 72 scoped_refptr<net::IOBuffer> buffer(buf); |
73 int size = std::min( | 73 int size = std::min( |
74 static_cast<int>(incoming_packets_.front().second.size()), buf_len); | 74 static_cast<int>(incoming_packets_.front().second.size()), buf_len); |
75 memcpy(buffer->data(), &*incoming_packets_.front().second.begin(), size); | 75 memcpy(buffer->data(), &*incoming_packets_.front().second.begin(), size); |
76 *address = incoming_packets_.front().first; | 76 *address = incoming_packets_.front().first; |
77 incoming_packets_.pop_front(); | 77 incoming_packets_.pop_front(); |
78 return size; | 78 return size; |
79 } else { | 79 } else { |
80 recv_callback_ = callback; | 80 recv_callback_ = callback; |
81 recv_buffer_ = buf; | 81 recv_buffer_ = buf; |
82 recv_size_ = buf_len; | 82 recv_size_ = buf_len; |
83 recv_address_ = address; | 83 recv_address_ = address; |
84 return net::ERR_IO_PENDING; | 84 return net::ERR_IO_PENDING; |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 virtual int SendTo(net::IOBuffer* buf, int buf_len, | 88 virtual int SendTo(net::IOBuffer* buf, int buf_len, |
89 const net::IPEndPoint& address, | 89 const net::IPEndPoint& address, |
90 const net::CompletionCallback& callback) OVERRIDE { | 90 const net::CompletionCallback& callback) override { |
91 scoped_refptr<net::IOBuffer> buffer(buf); | 91 scoped_refptr<net::IOBuffer> buffer(buf); |
92 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len); | 92 std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len); |
93 sent_packets_->push_back(UDPPacket(address, data_vector)); | 93 sent_packets_->push_back(UDPPacket(address, data_vector)); |
94 return buf_len; | 94 return buf_len; |
95 } | 95 } |
96 | 96 |
97 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { | 97 virtual int SetReceiveBufferSize(int32 size) override { |
98 return net::OK; | 98 return net::OK; |
99 } | 99 } |
100 | 100 |
101 virtual int SetSendBufferSize(int32 size) OVERRIDE { | 101 virtual int SetSendBufferSize(int32 size) override { |
102 return net::OK; | 102 return net::OK; |
103 } | 103 } |
104 | 104 |
105 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) { | 105 void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) { |
106 if (!recv_callback_.is_null()) { | 106 if (!recv_callback_.is_null()) { |
107 int size = std::min(recv_size_, static_cast<int>(data.size())); | 107 int size = std::min(recv_size_, static_cast<int>(data.size())); |
108 memcpy(recv_buffer_->data(), &*data.begin(), size); | 108 memcpy(recv_buffer_->data(), &*data.begin(), size); |
109 *recv_address_ = address; | 109 *recv_address_ = address; |
110 net::CompletionCallback cb = recv_callback_; | 110 net::CompletionCallback cb = recv_callback_; |
111 recv_callback_.Reset(); | 111 recv_callback_.Reset(); |
112 recv_buffer_ = NULL; | 112 recv_buffer_ = NULL; |
113 cb.Run(size); | 113 cb.Run(size); |
114 } else { | 114 } else { |
115 incoming_packets_.push_back(UDPPacket(address, data)); | 115 incoming_packets_.push_back(UDPPacket(address, data)); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 virtual const net::BoundNetLog& NetLog() const OVERRIDE { | 119 virtual const net::BoundNetLog& NetLog() const override { |
120 return net_log_; | 120 return net_log_; |
121 } | 121 } |
122 | 122 |
123 virtual void AllowAddressReuse() OVERRIDE { | 123 virtual void AllowAddressReuse() override { |
124 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
125 } | 125 } |
126 | 126 |
127 virtual void AllowBroadcast() OVERRIDE { | 127 virtual void AllowBroadcast() override { |
128 NOTIMPLEMENTED(); | 128 NOTIMPLEMENTED(); |
129 } | 129 } |
130 | 130 |
131 virtual int JoinGroup( | 131 virtual int JoinGroup( |
132 const net::IPAddressNumber& group_address) const OVERRIDE { | 132 const net::IPAddressNumber& group_address) const override { |
133 NOTIMPLEMENTED(); | 133 NOTIMPLEMENTED(); |
134 return net::ERR_NOT_IMPLEMENTED; | 134 return net::ERR_NOT_IMPLEMENTED; |
135 } | 135 } |
136 | 136 |
137 virtual int LeaveGroup( | 137 virtual int LeaveGroup( |
138 const net::IPAddressNumber& group_address) const OVERRIDE { | 138 const net::IPAddressNumber& group_address) const override { |
139 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
140 return net::ERR_NOT_IMPLEMENTED; | 140 return net::ERR_NOT_IMPLEMENTED; |
141 } | 141 } |
142 | 142 |
143 virtual int SetMulticastInterface(uint32 interface_index) OVERRIDE { | 143 virtual int SetMulticastInterface(uint32 interface_index) override { |
144 NOTIMPLEMENTED(); | 144 NOTIMPLEMENTED(); |
145 return net::ERR_NOT_IMPLEMENTED; | 145 return net::ERR_NOT_IMPLEMENTED; |
146 } | 146 } |
147 | 147 |
148 virtual int SetMulticastTimeToLive(int time_to_live) OVERRIDE { | 148 virtual int SetMulticastTimeToLive(int time_to_live) override { |
149 NOTIMPLEMENTED(); | 149 NOTIMPLEMENTED(); |
150 return net::ERR_NOT_IMPLEMENTED; | 150 return net::ERR_NOT_IMPLEMENTED; |
151 } | 151 } |
152 | 152 |
153 virtual int SetMulticastLoopbackMode(bool loopback) OVERRIDE { | 153 virtual int SetMulticastLoopbackMode(bool loopback) override { |
154 NOTIMPLEMENTED(); | 154 NOTIMPLEMENTED(); |
155 return net::ERR_NOT_IMPLEMENTED; | 155 return net::ERR_NOT_IMPLEMENTED; |
156 } | 156 } |
157 | 157 |
158 virtual int SetDiffServCodePoint(net::DiffServCodePoint dscp) OVERRIDE { | 158 virtual int SetDiffServCodePoint(net::DiffServCodePoint dscp) override { |
159 NOTIMPLEMENTED(); | 159 NOTIMPLEMENTED(); |
160 return net::ERR_NOT_IMPLEMENTED; | 160 return net::ERR_NOT_IMPLEMENTED; |
161 } | 161 } |
162 | 162 |
163 virtual void DetachFromThread() OVERRIDE { | 163 virtual void DetachFromThread() override { |
164 NOTIMPLEMENTED(); | 164 NOTIMPLEMENTED(); |
165 } | 165 } |
166 | 166 |
167 private: | 167 private: |
168 net::IPEndPoint address_; | 168 net::IPEndPoint address_; |
169 std::deque<UDPPacket>* sent_packets_; | 169 std::deque<UDPPacket>* sent_packets_; |
170 std::deque<UDPPacket> incoming_packets_; | 170 std::deque<UDPPacket> incoming_packets_; |
171 net::BoundNetLog net_log_; | 171 net::BoundNetLog net_log_; |
172 | 172 |
173 scoped_refptr<net::IOBuffer> recv_buffer_; | 173 scoped_refptr<net::IOBuffer> recv_buffer_; |
174 net::IPEndPoint* recv_address_; | 174 net::IPEndPoint* recv_address_; |
175 int recv_size_; | 175 int recv_size_; |
176 net::CompletionCallback recv_callback_; | 176 net::CompletionCallback recv_callback_; |
177 }; | 177 }; |
178 | 178 |
179 } // namespace | 179 } // namespace |
180 | 180 |
181 namespace content { | 181 namespace content { |
182 | 182 |
183 class P2PSocketHostUdpTest : public testing::Test { | 183 class P2PSocketHostUdpTest : public testing::Test { |
184 protected: | 184 protected: |
185 virtual void SetUp() OVERRIDE { | 185 virtual void SetUp() override { |
186 EXPECT_CALL(sender_, Send( | 186 EXPECT_CALL(sender_, Send( |
187 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | 187 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) |
188 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 188 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
189 | 189 |
190 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); | 190 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); |
191 socket_ = new FakeDatagramServerSocket(&sent_packets_); | 191 socket_ = new FakeDatagramServerSocket(&sent_packets_); |
192 socket_host_->socket_.reset(socket_); | 192 socket_host_->socket_.reset(socket_); |
193 | 193 |
194 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 194 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
195 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); | 195 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 socket_host_->Send(dest3, packet1, options, 0); | 380 socket_host_->Send(dest3, packet1, options, 0); |
381 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); | 381 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); |
382 // This packet should also be dropped. | 382 // This packet should also be dropped. |
383 socket_host_->Send(dest4, packet1, options, 0); | 383 socket_host_->Send(dest4, packet1, options, 0); |
384 // |dest1| is known, we can send as many packets to it. | 384 // |dest1| is known, we can send as many packets to it. |
385 socket_host_->Send(dest1_, packet1, options, 0); | 385 socket_host_->Send(dest1_, packet1, options, 0); |
386 ASSERT_EQ(sent_packets_.size(), 4U); | 386 ASSERT_EQ(sent_packets_.size(), 4U); |
387 } | 387 } |
388 | 388 |
389 } // namespace content | 389 } // namespace content |
OLD | NEW |