Use the appropriate variant of IntToString in //remoting
[chromium-blink-merge.git] / remoting / protocol / fake_stream_socket.cc
blobb857ca1a1546c992a994a6972f430f651c9c60e4
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());
34 if (peer_socket_) {
35 task_runner_->PostTask(
36 FROM_HERE, base::Bind(&FakeStreamSocket::AppendReadError, peer_socket_,
37 net::ERR_CONNECTION_CLOSED));
41 void FakeStreamSocket::AppendInputData(const std::string& data) {
42 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
43 input_data_.insert(input_data_.end(), data.begin(), data.end());
44 // Complete pending read if any.
45 if (!read_callback_.is_null()) {
46 int result = std::min(read_buffer_size_,
47 static_cast<int>(input_data_.size() - input_pos_));
48 EXPECT_GT(result, 0);
49 memcpy(read_buffer_->data(),
50 &(*input_data_.begin()) + input_pos_, result);
51 input_pos_ += result;
52 read_buffer_ = nullptr;
54 base::ResetAndReturn(&read_callback_).Run(result);
58 void FakeStreamSocket::AppendReadError(int error) {
59 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
60 // Complete pending read if any.
61 if (!read_callback_.is_null()) {
62 base::ResetAndReturn(&read_callback_).Run(error);
63 } else {
64 next_read_error_ = error;
68 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) {
69 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
70 peer_socket_ = peer_socket->GetWeakPtr();
71 peer_socket->peer_socket_ = GetWeakPtr();
74 base::WeakPtr<FakeStreamSocket> FakeStreamSocket::GetWeakPtr() {
75 return weak_factory_.GetWeakPtr();
78 int FakeStreamSocket::Read(const scoped_refptr<net::IOBuffer>& buf,
79 int buf_len,
80 const net::CompletionCallback& callback) {
81 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
83 if (input_pos_ < static_cast<int>(input_data_.size())) {
84 int result = std::min(buf_len,
85 static_cast<int>(input_data_.size()) - input_pos_);
86 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result);
87 input_pos_ += result;
88 return result;
89 } else if (next_read_error_ != net::OK) {
90 int r = next_read_error_;
91 next_read_error_ = net::OK;
92 return r;
93 } else {
94 read_buffer_ = buf;
95 read_buffer_size_ = buf_len;
96 read_callback_ = callback;
97 return net::ERR_IO_PENDING;
101 int FakeStreamSocket::Write(const scoped_refptr<net::IOBuffer>& buf,
102 int buf_len,
103 const net::CompletionCallback& callback) {
104 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
105 EXPECT_FALSE(write_pending_);
107 if (write_limit_ > 0)
108 buf_len = std::min(write_limit_, buf_len);
110 if (async_write_) {
111 task_runner_->PostTask(FROM_HERE, base::Bind(
112 &FakeStreamSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(),
113 scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
114 write_pending_ = true;
115 return net::ERR_IO_PENDING;
116 } else {
117 if (next_write_error_ != net::OK) {
118 int r = next_write_error_;
119 next_write_error_ = net::OK;
120 return r;
123 DoWrite(buf, buf_len);
124 return buf_len;
128 void FakeStreamSocket::DoAsyncWrite(const scoped_refptr<net::IOBuffer>& buf,
129 int buf_len,
130 const net::CompletionCallback& callback) {
131 write_pending_ = false;
133 if (next_write_error_ != net::OK) {
134 int r = next_write_error_;
135 next_write_error_ = net::OK;
136 callback.Run(r);
137 return;
140 DoWrite(buf.get(), buf_len);
141 callback.Run(buf_len);
144 void FakeStreamSocket::DoWrite(const scoped_refptr<net::IOBuffer>& buf,
145 int buf_len) {
146 written_data_.insert(written_data_.end(),
147 buf->data(), buf->data() + buf_len);
149 if (peer_socket_) {
150 task_runner_->PostTask(
151 FROM_HERE,
152 base::Bind(&FakeStreamSocket::AppendInputData,
153 peer_socket_,
154 std::string(buf->data(), buf->data() + buf_len)));
158 FakeStreamChannelFactory::FakeStreamChannelFactory()
159 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
160 asynchronous_create_(false),
161 fail_create_(false),
162 weak_factory_(this) {
165 FakeStreamChannelFactory::~FakeStreamChannelFactory() {}
167 FakeStreamSocket* FakeStreamChannelFactory::GetFakeChannel(
168 const std::string& name) {
169 return channels_[name].get();
172 void FakeStreamChannelFactory::CreateChannel(
173 const std::string& name,
174 const ChannelCreatedCallback& callback) {
175 scoped_ptr<FakeStreamSocket> channel(new FakeStreamSocket());
176 channels_[name] = channel->GetWeakPtr();
178 if (fail_create_)
179 channel.reset();
181 if (asynchronous_create_) {
182 task_runner_->PostTask(FROM_HERE, base::Bind(
183 &FakeStreamChannelFactory::NotifyChannelCreated,
184 weak_factory_.GetWeakPtr(), base::Passed(&channel), name, callback));
185 } else {
186 NotifyChannelCreated(channel.Pass(), name, callback);
190 void FakeStreamChannelFactory::NotifyChannelCreated(
191 scoped_ptr<FakeStreamSocket> owned_channel,
192 const std::string& name,
193 const ChannelCreatedCallback& callback) {
194 if (channels_.find(name) != channels_.end())
195 callback.Run(owned_channel.Pass());
198 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) {
199 channels_.erase(name);
202 } // namespace protocol
203 } // namespace remoting