Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: remoting/protocol/fake_stream_socket.cc

Issue 580243003: Cleanup Fake* classes in remoting/protocol (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sctp
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "remoting/protocol/fake_stream_socket.h"
6
7 #include "base/bind.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"
15
16 namespace remoting {
17 namespace protocol {
18
19 FakeStreamSocket::FakeStreamSocket()
20 : async_write_(false),
21 write_pending_(false),
22 write_limit_(0),
23 next_write_error_(net::OK),
24 next_read_error_(net::OK),
25 read_pending_(false),
26 read_buffer_size_(0),
27 input_pos_(0),
28 task_runner_(base::ThreadTaskRunnerHandle::Get()),
29 weak_factory_(this) {
30 }
31
32 FakeStreamSocket::~FakeStreamSocket() {
33 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
34 }
35
36 void FakeStreamSocket::AppendInputData(const std::string& data) {
37 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
Wez 2014/09/20 00:25:10 Should these be ASSERT_TRUE, since bad things will
Sergey Ulanov 2014/09/22 19:22:11 ASSERT_TRUE() only works in functions that return
38 input_data_.insert(input_data_.end(), data.begin(), data.end());
39 // Complete pending read if any.
40 if (read_pending_) {
41 read_pending_ = false;
42 int result = std::min(read_buffer_size_,
43 static_cast<int>(input_data_.size() - input_pos_));
44 CHECK(result > 0);
Wez 2014/09/20 00:25:10 EXPECT_GT or ASSERT_GT?
Sergey Ulanov 2014/09/22 19:22:12 Done.
45 memcpy(read_buffer_->data(),
46 &(*input_data_.begin()) + input_pos_, result);
47 input_pos_ += result;
48 read_buffer_ = NULL;
49 read_callback_.Run(result);
50 }
51 }
52
53 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) {
54 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
55 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr();
56 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr();
57 }
58
59 int FakeStreamSocket::Read(net::IOBuffer* buf, int buf_len,
60 const net::CompletionCallback& callback) {
61 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
62
63 if (next_read_error_ != net::OK) {
64 int r = next_read_error_;
65 next_read_error_ = net::OK;
66 return r;
67 }
68
69 if (input_pos_ < static_cast<int>(input_data_.size())) {
Wez 2014/09/20 00:25:10 Looks like this fake provides no way to return EOF
Sergey Ulanov 2014/09/22 19:22:12 Yes. We've never needed it - that feature can be a
70 int result = std::min(buf_len,
71 static_cast<int>(input_data_.size()) - input_pos_);
72 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result);
73 input_pos_ += result;
74 return result;
75 } else {
76 read_pending_ = true;
77 read_buffer_ = buf;
78 read_buffer_size_ = buf_len;
79 read_callback_ = callback;
80 return net::ERR_IO_PENDING;
81 }
82 }
83
84 int FakeStreamSocket::Write(net::IOBuffer* buf, int buf_len,
85 const net::CompletionCallback& callback) {
86 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
87 EXPECT_FALSE(write_pending_);
88
89 if (write_limit_ > 0)
90 buf_len = std::min(write_limit_, buf_len);
91
92 if (async_write_) {
93 task_runner_->PostTask(FROM_HERE, base::Bind(
94 &FakeStreamSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(),
95 scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
96 write_pending_ = true;
97 return net::ERR_IO_PENDING;
98 } else {
99 if (next_write_error_ != net::OK) {
100 int r = next_write_error_;
101 next_write_error_ = net::OK;
102 return r;
103 }
104
105 DoWrite(buf, buf_len);
106 return buf_len;
107 }
108 }
109
110 void FakeStreamSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf,
111 int buf_len,
112 const net::CompletionCallback& callback) {
113 write_pending_ = false;
114
115 if (next_write_error_ != net::OK) {
116 int r = next_write_error_;
117 next_write_error_ = net::OK;
118 callback.Run(r);
119 return;
120 }
121
122 DoWrite(buf.get(), buf_len);
123 callback.Run(buf_len);
124 }
125
126 void FakeStreamSocket::DoWrite(net::IOBuffer* buf, int buf_len) {
127 written_data_.insert(written_data_.end(),
128 buf->data(), buf->data() + buf_len);
129
130 if (peer_socket_.get()) {
131 task_runner_->PostTask(
132 FROM_HERE,
133 base::Bind(&FakeStreamSocket::AppendInputData,
134 peer_socket_,
135 std::string(buf->data(), buf->data() + buf_len)));
136 }
137 }
138
139 int FakeStreamSocket::SetReceiveBufferSize(int32 size) {
140 NOTIMPLEMENTED();
141 return net::ERR_NOT_IMPLEMENTED;
142 }
143
144 int FakeStreamSocket::SetSendBufferSize(int32 size) {
145 NOTIMPLEMENTED();
146 return net::ERR_NOT_IMPLEMENTED;
147 }
148
149 int FakeStreamSocket::Connect(const net::CompletionCallback& callback) {
150 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
151 return net::OK;
152 }
153
154 void FakeStreamSocket::Disconnect() {
155 peer_socket_.reset();
156 }
157
158 bool FakeStreamSocket::IsConnected() const {
159 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
Wez 2014/09/20 00:25:10 Looks like several calls e.g. ::Disconnect(), ::Se
Sergey Ulanov 2014/09/22 19:22:12 Done.
160 return true;
161 }
162
163 bool FakeStreamSocket::IsConnectedAndIdle() const {
164 NOTIMPLEMENTED();
165 return false;
166 }
167
168 int FakeStreamSocket::GetPeerAddress(net::IPEndPoint* address) const {
169 net::IPAddressNumber ip(net::kIPv4AddressSize);
170 *address = net::IPEndPoint(ip, 0);
171 return net::OK;
172 }
173
174 int FakeStreamSocket::GetLocalAddress(net::IPEndPoint* address) const {
175 NOTIMPLEMENTED();
176 return net::ERR_NOT_IMPLEMENTED;
177 }
178
179 const net::BoundNetLog& FakeStreamSocket::NetLog() const {
180 EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
181 return net_log_;
182 }
183
184 void FakeStreamSocket::SetSubresourceSpeculation() {
185 NOTIMPLEMENTED();
186 }
187
188 void FakeStreamSocket::SetOmniboxSpeculation() {
189 NOTIMPLEMENTED();
190 }
191
192 bool FakeStreamSocket::WasEverUsed() const {
193 NOTIMPLEMENTED();
194 return true;
195 }
196
197 bool FakeStreamSocket::UsingTCPFastOpen() const {
198 NOTIMPLEMENTED();
199 return true;
200 }
201
202 bool FakeStreamSocket::WasNpnNegotiated() const {
203 return false;
204 }
205
206 net::NextProto FakeStreamSocket::GetNegotiatedProtocol() const {
207 NOTIMPLEMENTED();
208 return net::kProtoUnknown;
209 }
210
211 bool FakeStreamSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
212 return false;
213 }
214
215 FakeStreamChannelFactory::FakeStreamChannelFactory()
216 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
217 async_creation_(false),
218 fail_(false),
219 weak_factory_(this) {
220 }
221
222 FakeStreamChannelFactory::~FakeStreamChannelFactory() {}
223
224 FakeStreamSocket* FakeStreamChannelFactory::GetChannel(
225 const std::string& name) {
226 return channels_[name];
227 }
228
229 void FakeStreamChannelFactory::CreateChannel(
230 const std::string& name,
231 const ChannelCreatedCallback& callback) {
232 scoped_ptr<FakeStreamSocket> channel;
Wez 2014/09/20 00:25:10 See comments on datagram equivalent.
Sergey Ulanov 2014/09/22 19:22:12 Done.
233 // If we are in the error state then we put NULL in the channels list, so that
234 // NotifyChannelCreated() still calls the callback.
235 if (!fail_)
236 channel.reset(new FakeStreamSocket());
237 channels_[name] = channel.release();
238
239 if (async_creation_) {
240 task_runner_->PostTask(FROM_HERE, base::Bind(
241 &FakeStreamChannelFactory::NotifyChannelCreated,
242 weak_factory_.GetWeakPtr(),
243 name, callback));
244 } else {
245 NotifyChannelCreated(name, callback);
246 }
247 }
248
249 void FakeStreamChannelFactory::NotifyChannelCreated(
250 const std::string& name,
251 const ChannelCreatedCallback& callback) {
252 if (channels_.find(name) != channels_.end())
253 callback.Run(scoped_ptr<net::StreamSocket>(channels_[name]));
Wez 2014/09/20 00:25:10 Set comments on datagram equivalent.
Sergey Ulanov 2014/09/22 19:22:12 Done.
254 }
255
256 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) {
257 channels_.erase(name);
258 }
259
260 } // namespace protocol
261 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698