1 // Copyright (c) 2013 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_test_utils.h"
7 #include "base/sys_byteorder.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/io_buffer.h"
12 const int kStunHeaderSize
= 20;
13 const uint16 kStunBindingRequest
= 0x0001;
14 const uint16 kStunBindingResponse
= 0x0102;
15 const uint16 kStunBindingError
= 0x0111;
16 const uint32 kStunMagicCookie
= 0x2112A442;
18 MockIPCSender::MockIPCSender() { }
19 MockIPCSender::~MockIPCSender() { }
21 FakeSocket::FakeSocket(std::string
* written_data
)
22 : read_pending_(false),
24 written_data_(written_data
),
26 write_pending_(false) {
29 FakeSocket::~FakeSocket() { }
31 void FakeSocket::AppendInputData(const char* data
, int data_size
) {
32 input_data_
.insert(input_data_
.end(), data
, data
+ data_size
);
33 // Complete pending read if any.
35 read_pending_
= false;
36 int result
= std::min(read_buffer_size_
,
37 static_cast<int>(input_data_
.size() - input_pos_
));
39 memcpy(read_buffer_
->data(), &input_data_
[0] + input_pos_
, result
);
42 net::CompletionCallback cb
= read_callback_
;
43 read_callback_
.Reset();
48 void FakeSocket::SetPeerAddress(const net::IPEndPoint
& peer_address
) {
49 peer_address_
= peer_address
;
52 void FakeSocket::SetLocalAddress(const net::IPEndPoint
& local_address
) {
53 local_address_
= local_address
;
56 int FakeSocket::Read(net::IOBuffer
* buf
, int buf_len
,
57 const net::CompletionCallback
& callback
) {
59 if (input_pos_
< static_cast<int>(input_data_
.size())){
60 int result
= std::min(buf_len
,
61 static_cast<int>(input_data_
.size()) - input_pos_
);
62 memcpy(buf
->data(), &(*input_data_
.begin()) + input_pos_
, result
);
68 read_buffer_size_
= buf_len
;
69 read_callback_
= callback
;
70 return net::ERR_IO_PENDING
;
74 int FakeSocket::Write(net::IOBuffer
* buf
, int buf_len
,
75 const net::CompletionCallback
& callback
) {
77 DCHECK(!write_pending_
);
81 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, base::Bind(
82 &FakeSocket::DoAsyncWrite
, base::Unretained(this),
83 scoped_refptr
<net::IOBuffer
>(buf
), buf_len
, callback
));
84 write_pending_
= true;
85 return net::ERR_IO_PENDING
;
89 written_data_
->insert(written_data_
->end(),
90 buf
->data(), buf
->data() + buf_len
);
95 void FakeSocket::DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
, int buf_len
,
96 const net::CompletionCallback
& callback
) {
97 write_pending_
= false;
100 written_data_
->insert(written_data_
->end(),
101 buf
->data(), buf
->data() + buf_len
);
103 callback
.Run(buf_len
);
106 int FakeSocket::SetReceiveBufferSize(int32 size
) {
108 return net::ERR_NOT_IMPLEMENTED
;
111 int FakeSocket::SetSendBufferSize(int32 size
) {
113 return net::ERR_NOT_IMPLEMENTED
;
116 int FakeSocket::Connect(const net::CompletionCallback
& callback
) {
120 void FakeSocket::Disconnect() {
124 bool FakeSocket::IsConnected() const {
128 bool FakeSocket::IsConnectedAndIdle() const {
132 int FakeSocket::GetPeerAddress(net::IPEndPoint
* address
) const {
133 *address
= peer_address_
;
137 int FakeSocket::GetLocalAddress(net::IPEndPoint
* address
) const {
138 *address
= local_address_
;
142 const net::BoundNetLog
& FakeSocket::NetLog() const {
147 void FakeSocket::SetSubresourceSpeculation() {
151 void FakeSocket::SetOmniboxSpeculation() {
155 bool FakeSocket::WasEverUsed() const {
159 bool FakeSocket::UsingTCPFastOpen() const {
163 bool FakeSocket::WasNpnNegotiated() const {
167 net::NextProto
FakeSocket::GetNegotiatedProtocol() const {
168 return net::kProtoUnknown
;
171 bool FakeSocket::GetSSLInfo(net::SSLInfo
* ssl_info
) {
175 void FakeSocket::GetConnectionAttempts(net::ConnectionAttempts
* out
) const {
179 void CreateRandomPacket(std::vector
<char>* packet
) {
180 size_t size
= kStunHeaderSize
+ rand() % 1000;
181 packet
->resize(size
);
182 for (size_t i
= 0; i
< size
; i
++) {
183 (*packet
)[i
] = rand() % 256;
185 // Always set the first bit to ensure that generated packet is not
186 // valid STUN packet.
187 (*packet
)[0] = (*packet
)[0] | 0x80;
190 static void CreateStunPacket(std::vector
<char>* packet
, uint16 type
) {
191 CreateRandomPacket(packet
);
192 *reinterpret_cast<uint16
*>(&*packet
->begin()) = base::HostToNet16(type
);
193 *reinterpret_cast<uint16
*>(&*packet
->begin() + 2) =
194 base::HostToNet16(packet
->size() - kStunHeaderSize
);
195 *reinterpret_cast<uint32
*>(&*packet
->begin() + 4) =
196 base::HostToNet32(kStunMagicCookie
);
199 void CreateStunRequest(std::vector
<char>* packet
) {
200 CreateStunPacket(packet
, kStunBindingRequest
);
203 void CreateStunResponse(std::vector
<char>* packet
) {
204 CreateStunPacket(packet
, kStunBindingResponse
);
207 void CreateStunError(std::vector
<char>* packet
) {
208 CreateStunPacket(packet
, kStunBindingError
);
211 net::IPEndPoint
ParseAddress(const std::string ip_str
, uint16 port
) {
212 net::IPAddressNumber ip
;
213 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_str
, &ip
));
214 return net::IPEndPoint(ip
, port
);