1 // Copyright 2014 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 "remoting/protocol/fake_stream_socket.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/base/address_list.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/net_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 FakeStreamSocket::FakeStreamSocket()
20 : async_write_(false),
21 write_pending_(false),
23 next_write_error_(net::OK
),
24 next_read_error_(net::OK
),
27 task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 FakeStreamSocket::~FakeStreamSocket() {
32 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
35 void FakeStreamSocket::AppendInputData(const std::string
& data
) {
36 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
37 input_data_
.insert(input_data_
.end(), data
.begin(), data
.end());
38 // Complete pending read if any.
39 if (!read_callback_
.is_null()) {
40 int result
= std::min(read_buffer_size_
,
41 static_cast<int>(input_data_
.size() - input_pos_
));
43 memcpy(read_buffer_
->data(),
44 &(*input_data_
.begin()) + input_pos_
, result
);
46 read_buffer_
= nullptr;
48 net::CompletionCallback callback
= read_callback_
;
49 read_callback_
.Reset();
54 void FakeStreamSocket::PairWith(FakeStreamSocket
* peer_socket
) {
55 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
56 peer_socket_
= peer_socket
->GetWeakPtr();
57 peer_socket
->peer_socket_
= GetWeakPtr();
60 base::WeakPtr
<FakeStreamSocket
> FakeStreamSocket::GetWeakPtr() {
61 return weak_factory_
.GetWeakPtr();
64 int FakeStreamSocket::Read(net::IOBuffer
* buf
, int buf_len
,
65 const net::CompletionCallback
& callback
) {
66 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
68 if (next_read_error_
!= net::OK
) {
69 int r
= next_read_error_
;
70 next_read_error_
= net::OK
;
74 if (input_pos_
< static_cast<int>(input_data_
.size())) {
75 int result
= std::min(buf_len
,
76 static_cast<int>(input_data_
.size()) - input_pos_
);
77 memcpy(buf
->data(), &(*input_data_
.begin()) + input_pos_
, result
);
82 read_buffer_size_
= buf_len
;
83 read_callback_
= callback
;
84 return net::ERR_IO_PENDING
;
88 int FakeStreamSocket::Write(net::IOBuffer
* buf
, int buf_len
,
89 const net::CompletionCallback
& callback
) {
90 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
91 EXPECT_FALSE(write_pending_
);
94 buf_len
= std::min(write_limit_
, buf_len
);
97 task_runner_
->PostTask(FROM_HERE
, base::Bind(
98 &FakeStreamSocket::DoAsyncWrite
, weak_factory_
.GetWeakPtr(),
99 scoped_refptr
<net::IOBuffer
>(buf
), buf_len
, callback
));
100 write_pending_
= true;
101 return net::ERR_IO_PENDING
;
103 if (next_write_error_
!= net::OK
) {
104 int r
= next_write_error_
;
105 next_write_error_
= net::OK
;
109 DoWrite(buf
, buf_len
);
114 void FakeStreamSocket::DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
,
116 const net::CompletionCallback
& callback
) {
117 write_pending_
= false;
119 if (next_write_error_
!= net::OK
) {
120 int r
= next_write_error_
;
121 next_write_error_
= net::OK
;
126 DoWrite(buf
.get(), buf_len
);
127 callback
.Run(buf_len
);
130 void FakeStreamSocket::DoWrite(net::IOBuffer
* buf
, int buf_len
) {
131 written_data_
.insert(written_data_
.end(),
132 buf
->data(), buf
->data() + buf_len
);
134 if (peer_socket_
.get()) {
135 task_runner_
->PostTask(
137 base::Bind(&FakeStreamSocket::AppendInputData
,
139 std::string(buf
->data(), buf
->data() + buf_len
)));
143 int FakeStreamSocket::SetReceiveBufferSize(int32 size
) {
144 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
146 return net::ERR_NOT_IMPLEMENTED
;
149 int FakeStreamSocket::SetSendBufferSize(int32 size
) {
150 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
152 return net::ERR_NOT_IMPLEMENTED
;
155 int FakeStreamSocket::Connect(const net::CompletionCallback
& callback
) {
156 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
160 void FakeStreamSocket::Disconnect() {
161 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
162 peer_socket_
.reset();
165 bool FakeStreamSocket::IsConnected() const {
166 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
170 bool FakeStreamSocket::IsConnectedAndIdle() const {
171 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
176 int FakeStreamSocket::GetPeerAddress(net::IPEndPoint
* address
) const {
177 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
178 net::IPAddressNumber
ip(net::kIPv4AddressSize
);
179 *address
= net::IPEndPoint(ip
, 0);
183 int FakeStreamSocket::GetLocalAddress(net::IPEndPoint
* address
) const {
184 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
186 return net::ERR_NOT_IMPLEMENTED
;
189 const net::BoundNetLog
& FakeStreamSocket::NetLog() const {
190 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
194 void FakeStreamSocket::SetSubresourceSpeculation() {
195 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
199 void FakeStreamSocket::SetOmniboxSpeculation() {
200 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
204 bool FakeStreamSocket::WasEverUsed() const {
205 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
210 bool FakeStreamSocket::UsingTCPFastOpen() const {
211 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
216 bool FakeStreamSocket::WasNpnNegotiated() const {
217 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
221 net::NextProto
FakeStreamSocket::GetNegotiatedProtocol() const {
222 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
224 return net::kProtoUnknown
;
227 bool FakeStreamSocket::GetSSLInfo(net::SSLInfo
* ssl_info
) {
228 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
232 FakeStreamChannelFactory::FakeStreamChannelFactory()
233 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
234 asynchronous_create_(false),
236 weak_factory_(this) {
239 FakeStreamChannelFactory::~FakeStreamChannelFactory() {}
241 FakeStreamSocket
* FakeStreamChannelFactory::GetFakeChannel(
242 const std::string
& name
) {
243 return channels_
[name
].get();
246 void FakeStreamChannelFactory::CreateChannel(
247 const std::string
& name
,
248 const ChannelCreatedCallback
& callback
) {
249 scoped_ptr
<FakeStreamSocket
> channel(new FakeStreamSocket());
250 channels_
[name
] = channel
->GetWeakPtr();
255 if (asynchronous_create_
) {
256 task_runner_
->PostTask(FROM_HERE
, base::Bind(
257 &FakeStreamChannelFactory::NotifyChannelCreated
,
258 weak_factory_
.GetWeakPtr(), base::Passed(&channel
), name
, callback
));
260 NotifyChannelCreated(channel
.Pass(), name
, callback
);
264 void FakeStreamChannelFactory::NotifyChannelCreated(
265 scoped_ptr
<FakeStreamSocket
> owned_channel
,
266 const std::string
& name
,
267 const ChannelCreatedCallback
& callback
) {
268 if (channels_
.find(name
) != channels_
.end())
269 callback
.Run(owned_channel
.Pass());
272 void FakeStreamChannelFactory::CancelChannelCreation(const std::string
& name
) {
273 channels_
.erase(name
);
276 } // namespace protocol
277 } // namespace remoting