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/callback_helpers.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/address_list.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 FakeStreamSocket::FakeStreamSocket()
21 : async_write_(false),
22 write_pending_(false),
24 next_write_error_(net::OK
),
25 next_read_error_(net::OK
),
28 task_runner_(base::ThreadTaskRunnerHandle::Get()),
32 FakeStreamSocket::~FakeStreamSocket() {
33 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
36 void FakeStreamSocket::AppendInputData(const std::string
& data
) {
37 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
38 input_data_
.insert(input_data_
.end(), data
.begin(), data
.end());
39 // Complete pending read if any.
40 if (!read_callback_
.is_null()) {
41 int result
= std::min(read_buffer_size_
,
42 static_cast<int>(input_data_
.size() - input_pos_
));
44 memcpy(read_buffer_
->data(),
45 &(*input_data_
.begin()) + input_pos_
, result
);
47 read_buffer_
= nullptr;
49 base::ResetAndReturn(&read_callback_
).Run(result
);
53 void FakeStreamSocket::AppendReadError(int error
) {
54 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
55 // Complete pending read if any.
56 if (!read_callback_
.is_null()) {
57 base::ResetAndReturn(&read_callback_
).Run(error
);
59 next_read_error_
= error
;
63 void FakeStreamSocket::PairWith(FakeStreamSocket
* peer_socket
) {
64 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
65 peer_socket_
= peer_socket
->GetWeakPtr();
66 peer_socket
->peer_socket_
= GetWeakPtr();
69 base::WeakPtr
<FakeStreamSocket
> FakeStreamSocket::GetWeakPtr() {
70 return weak_factory_
.GetWeakPtr();
73 int FakeStreamSocket::Read(net::IOBuffer
* buf
, int buf_len
,
74 const net::CompletionCallback
& callback
) {
75 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
77 if (input_pos_
< static_cast<int>(input_data_
.size())) {
78 int result
= std::min(buf_len
,
79 static_cast<int>(input_data_
.size()) - input_pos_
);
80 memcpy(buf
->data(), &(*input_data_
.begin()) + input_pos_
, result
);
83 } else if (next_read_error_
!= net::OK
) {
84 int r
= next_read_error_
;
85 next_read_error_
= net::OK
;
89 read_buffer_size_
= buf_len
;
90 read_callback_
= callback
;
91 return net::ERR_IO_PENDING
;
95 int FakeStreamSocket::Write(net::IOBuffer
* buf
, int buf_len
,
96 const net::CompletionCallback
& callback
) {
97 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
98 EXPECT_FALSE(write_pending_
);
100 if (write_limit_
> 0)
101 buf_len
= std::min(write_limit_
, buf_len
);
104 task_runner_
->PostTask(FROM_HERE
, base::Bind(
105 &FakeStreamSocket::DoAsyncWrite
, weak_factory_
.GetWeakPtr(),
106 scoped_refptr
<net::IOBuffer
>(buf
), buf_len
, callback
));
107 write_pending_
= true;
108 return net::ERR_IO_PENDING
;
110 if (next_write_error_
!= net::OK
) {
111 int r
= next_write_error_
;
112 next_write_error_
= net::OK
;
116 DoWrite(buf
, buf_len
);
121 void FakeStreamSocket::DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
,
123 const net::CompletionCallback
& callback
) {
124 write_pending_
= false;
126 if (next_write_error_
!= net::OK
) {
127 int r
= next_write_error_
;
128 next_write_error_
= net::OK
;
133 DoWrite(buf
.get(), buf_len
);
134 callback
.Run(buf_len
);
137 void FakeStreamSocket::DoWrite(net::IOBuffer
* buf
, int buf_len
) {
138 written_data_
.insert(written_data_
.end(),
139 buf
->data(), buf
->data() + buf_len
);
141 if (peer_socket_
.get()) {
142 task_runner_
->PostTask(
144 base::Bind(&FakeStreamSocket::AppendInputData
,
146 std::string(buf
->data(), buf
->data() + buf_len
)));
150 int FakeStreamSocket::SetReceiveBufferSize(int32 size
) {
151 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
153 return net::ERR_NOT_IMPLEMENTED
;
156 int FakeStreamSocket::SetSendBufferSize(int32 size
) {
157 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
159 return net::ERR_NOT_IMPLEMENTED
;
162 int FakeStreamSocket::Connect(const net::CompletionCallback
& callback
) {
163 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
167 void FakeStreamSocket::Disconnect() {
168 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
170 if (peer_socket_
.get()) {
171 task_runner_
->PostTask(
173 base::Bind(&FakeStreamSocket::AppendReadError
,
175 net::ERR_CONNECTION_CLOSED
));
177 peer_socket_
.reset();
180 bool FakeStreamSocket::IsConnected() const {
181 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
185 bool FakeStreamSocket::IsConnectedAndIdle() const {
186 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
191 int FakeStreamSocket::GetPeerAddress(net::IPEndPoint
* address
) const {
192 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
193 net::IPAddressNumber
ip(net::kIPv4AddressSize
);
194 *address
= net::IPEndPoint(ip
, 0);
198 int FakeStreamSocket::GetLocalAddress(net::IPEndPoint
* address
) const {
199 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
201 return net::ERR_NOT_IMPLEMENTED
;
204 const net::BoundNetLog
& FakeStreamSocket::NetLog() const {
205 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
209 void FakeStreamSocket::SetSubresourceSpeculation() {
210 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
214 void FakeStreamSocket::SetOmniboxSpeculation() {
215 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
219 bool FakeStreamSocket::WasEverUsed() const {
220 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
225 bool FakeStreamSocket::UsingTCPFastOpen() const {
226 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
231 bool FakeStreamSocket::WasNpnNegotiated() const {
232 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
236 net::NextProto
FakeStreamSocket::GetNegotiatedProtocol() const {
237 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
239 return net::kProtoUnknown
;
242 bool FakeStreamSocket::GetSSLInfo(net::SSLInfo
* ssl_info
) {
243 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
247 void FakeStreamSocket::GetConnectionAttempts(
248 net::ConnectionAttempts
* out
) const {
249 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
253 FakeStreamChannelFactory::FakeStreamChannelFactory()
254 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
255 asynchronous_create_(false),
257 weak_factory_(this) {
260 FakeStreamChannelFactory::~FakeStreamChannelFactory() {}
262 FakeStreamSocket
* FakeStreamChannelFactory::GetFakeChannel(
263 const std::string
& name
) {
264 return channels_
[name
].get();
267 void FakeStreamChannelFactory::CreateChannel(
268 const std::string
& name
,
269 const ChannelCreatedCallback
& callback
) {
270 scoped_ptr
<FakeStreamSocket
> channel(new FakeStreamSocket());
271 channels_
[name
] = channel
->GetWeakPtr();
276 if (asynchronous_create_
) {
277 task_runner_
->PostTask(FROM_HERE
, base::Bind(
278 &FakeStreamChannelFactory::NotifyChannelCreated
,
279 weak_factory_
.GetWeakPtr(), base::Passed(&channel
), name
, callback
));
281 NotifyChannelCreated(channel
.Pass(), name
, callback
);
285 void FakeStreamChannelFactory::NotifyChannelCreated(
286 scoped_ptr
<FakeStreamSocket
> owned_channel
,
287 const std::string
& name
,
288 const ChannelCreatedCallback
& callback
) {
289 if (channels_
.find(name
) != channels_
.end())
290 callback
.Run(owned_channel
.Pass());
293 void FakeStreamChannelFactory::CancelChannelCreation(const std::string
& name
) {
294 channels_
.erase(name
);
297 } // namespace protocol
298 } // namespace remoting