Fix CryptAuth enrollment logic.
[chromium-blink-merge.git] / remoting / protocol / fake_stream_socket.cc
blobfc7f31e52dfecaf6f35266a9992f4e6159c2b7dc
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"
7 #include "base/bind.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"
17 namespace remoting {
18 namespace protocol {
20 FakeStreamSocket::FakeStreamSocket()
21 : async_write_(false),
22 write_pending_(false),
23 write_limit_(0),
24 next_write_error_(net::OK),
25 next_read_error_(net::OK),
26 read_buffer_size_(0),
27 input_pos_(0),
28 task_runner_(base::ThreadTaskRunnerHandle::Get()),
29 weak_factory_(this) {
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_));
43 EXPECT_GT(result, 0);
44 memcpy(read_buffer_->data(),
45 &(*input_data_.begin()) + input_pos_, result);
46 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);
58 } else {
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);
81 input_pos_ += result;
82 return result;
83 } else if (next_read_error_ != net::OK) {
84 int r = next_read_error_;
85 next_read_error_ = net::OK;
86 return r;
87 } else {
88 read_buffer_ = buf;
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);
103 if (async_write_) {
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;
109 } else {
110 if (next_write_error_ != net::OK) {
111 int r = next_write_error_;
112 next_write_error_ = net::OK;
113 return r;
116 DoWrite(buf, buf_len);
117 return buf_len;
121 void FakeStreamSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf,
122 int buf_len,
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;
129 callback.Run(r);
130 return;
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(
143 FROM_HERE,
144 base::Bind(&FakeStreamSocket::AppendInputData,
145 peer_socket_,
146 std::string(buf->data(), buf->data() + buf_len)));
150 int FakeStreamSocket::SetReceiveBufferSize(int32 size) {
151 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
152 NOTIMPLEMENTED();
153 return net::ERR_NOT_IMPLEMENTED;
156 int FakeStreamSocket::SetSendBufferSize(int32 size) {
157 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
158 NOTIMPLEMENTED();
159 return net::ERR_NOT_IMPLEMENTED;
162 int FakeStreamSocket::Connect(const net::CompletionCallback& callback) {
163 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
164 return net::OK;
167 void FakeStreamSocket::Disconnect() {
168 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
170 if (peer_socket_.get()) {
171 task_runner_->PostTask(
172 FROM_HERE,
173 base::Bind(&FakeStreamSocket::AppendReadError,
174 peer_socket_,
175 net::ERR_CONNECTION_CLOSED));
177 peer_socket_.reset();
180 bool FakeStreamSocket::IsConnected() const {
181 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
182 return true;
185 bool FakeStreamSocket::IsConnectedAndIdle() const {
186 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
187 NOTIMPLEMENTED();
188 return false;
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);
195 return net::OK;
198 int FakeStreamSocket::GetLocalAddress(net::IPEndPoint* address) const {
199 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
200 NOTIMPLEMENTED();
201 return net::ERR_NOT_IMPLEMENTED;
204 const net::BoundNetLog& FakeStreamSocket::NetLog() const {
205 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
206 return net_log_;
209 void FakeStreamSocket::SetSubresourceSpeculation() {
210 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
211 NOTIMPLEMENTED();
214 void FakeStreamSocket::SetOmniboxSpeculation() {
215 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
216 NOTIMPLEMENTED();
219 bool FakeStreamSocket::WasEverUsed() const {
220 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
221 NOTIMPLEMENTED();
222 return true;
225 bool FakeStreamSocket::UsingTCPFastOpen() const {
226 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
227 NOTIMPLEMENTED();
228 return true;
231 bool FakeStreamSocket::WasNpnNegotiated() const {
232 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
233 return false;
236 net::NextProto FakeStreamSocket::GetNegotiatedProtocol() const {
237 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
238 NOTIMPLEMENTED();
239 return net::kProtoUnknown;
242 bool FakeStreamSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
243 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
244 return false;
247 void FakeStreamSocket::GetConnectionAttempts(
248 net::ConnectionAttempts* out) const {
249 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
250 out->clear();
253 FakeStreamChannelFactory::FakeStreamChannelFactory()
254 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
255 asynchronous_create_(false),
256 fail_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();
273 if (fail_create_)
274 channel.reset();
276 if (asynchronous_create_) {
277 task_runner_->PostTask(FROM_HERE, base::Bind(
278 &FakeStreamChannelFactory::NotifyChannelCreated,
279 weak_factory_.GetWeakPtr(), base::Passed(&channel), name, callback));
280 } else {
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