• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_session.h"
6 
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "net/base/address_list.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/net_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace remoting {
16 namespace protocol {
17 
18 const char kTestJid[] = "host1@gmail.com/chromoting123";
19 
FakeSocket()20 FakeSocket::FakeSocket()
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_pending_(false),
27       read_buffer_size_(0),
28       input_pos_(0),
29       message_loop_(base::MessageLoop::current()),
30       weak_factory_(this) {
31 }
32 
~FakeSocket()33 FakeSocket::~FakeSocket() {
34   EXPECT_EQ(message_loop_, base::MessageLoop::current());
35 }
36 
AppendInputData(const std::vector<char> & data)37 void FakeSocket::AppendInputData(const std::vector<char>& data) {
38   EXPECT_EQ(message_loop_, base::MessageLoop::current());
39   input_data_.insert(input_data_.end(), data.begin(), data.end());
40   // Complete pending read if any.
41   if (read_pending_) {
42     read_pending_ = false;
43     int result = std::min(read_buffer_size_,
44                           static_cast<int>(input_data_.size() - input_pos_));
45     CHECK(result > 0);
46     memcpy(read_buffer_->data(),
47            &(*input_data_.begin()) + input_pos_, result);
48     input_pos_ += result;
49     read_buffer_ = NULL;
50     read_callback_.Run(result);
51   }
52 }
53 
PairWith(FakeSocket * peer_socket)54 void FakeSocket::PairWith(FakeSocket* peer_socket) {
55   EXPECT_EQ(message_loop_, base::MessageLoop::current());
56   peer_socket_ = peer_socket->weak_factory_.GetWeakPtr();
57   peer_socket->peer_socket_ = weak_factory_.GetWeakPtr();
58 }
59 
Read(net::IOBuffer * buf,int buf_len,const net::CompletionCallback & callback)60 int FakeSocket::Read(net::IOBuffer* buf, int buf_len,
61                      const net::CompletionCallback& callback) {
62   EXPECT_EQ(message_loop_, base::MessageLoop::current());
63 
64   if (next_read_error_ != net::OK) {
65     int r = next_read_error_;
66     next_read_error_ = net::OK;
67     return r;
68   }
69 
70   if (input_pos_ < static_cast<int>(input_data_.size())) {
71     int result = std::min(buf_len,
72                           static_cast<int>(input_data_.size()) - input_pos_);
73     memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result);
74     input_pos_ += result;
75     return result;
76   } else {
77     read_pending_ = true;
78     read_buffer_ = buf;
79     read_buffer_size_ = buf_len;
80     read_callback_ = callback;
81     return net::ERR_IO_PENDING;
82   }
83 }
84 
Write(net::IOBuffer * buf,int buf_len,const net::CompletionCallback & callback)85 int FakeSocket::Write(net::IOBuffer* buf, int buf_len,
86                       const net::CompletionCallback& callback) {
87   EXPECT_EQ(message_loop_, base::MessageLoop::current());
88   EXPECT_FALSE(write_pending_);
89 
90   if (write_limit_ > 0)
91     buf_len = std::min(write_limit_, buf_len);
92 
93   if (async_write_) {
94     message_loop_->PostTask(FROM_HERE, base::Bind(
95         &FakeSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(),
96         scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
97     write_pending_ = true;
98     return net::ERR_IO_PENDING;
99   } else {
100     if (next_write_error_ != net::OK) {
101       int r = next_write_error_;
102       next_write_error_ = net::OK;
103       return r;
104     }
105 
106     DoWrite(buf, buf_len);
107     return buf_len;
108   }
109 }
110 
DoAsyncWrite(scoped_refptr<net::IOBuffer> buf,int buf_len,const net::CompletionCallback & callback)111 void FakeSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, 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 
DoWrite(net::IOBuffer * buf,int buf_len)126 void FakeSocket::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     message_loop_->PostTask(
132         FROM_HERE,
133         base::Bind(&FakeSocket::AppendInputData,
134                    peer_socket_,
135                    std::vector<char>(buf->data(), buf->data() + buf_len)));
136   }
137 }
138 
SetReceiveBufferSize(int32 size)139 int FakeSocket::SetReceiveBufferSize(int32 size) {
140   NOTIMPLEMENTED();
141   return net::ERR_NOT_IMPLEMENTED;
142 }
143 
SetSendBufferSize(int32 size)144 int FakeSocket::SetSendBufferSize(int32 size) {
145   NOTIMPLEMENTED();
146   return net::ERR_NOT_IMPLEMENTED;
147 }
148 
Connect(const net::CompletionCallback & callback)149 int FakeSocket::Connect(const net::CompletionCallback& callback) {
150   EXPECT_EQ(message_loop_, base::MessageLoop::current());
151   return net::OK;
152 }
153 
Disconnect()154 void FakeSocket::Disconnect() {
155   peer_socket_.reset();
156 }
157 
IsConnected() const158 bool FakeSocket::IsConnected() const {
159   EXPECT_EQ(message_loop_, base::MessageLoop::current());
160   return true;
161 }
162 
IsConnectedAndIdle() const163 bool FakeSocket::IsConnectedAndIdle() const {
164   NOTIMPLEMENTED();
165   return false;
166 }
167 
GetPeerAddress(net::IPEndPoint * address) const168 int FakeSocket::GetPeerAddress(net::IPEndPoint* address) const {
169   net::IPAddressNumber ip(net::kIPv4AddressSize);
170   *address = net::IPEndPoint(ip, 0);
171   return net::OK;
172 }
173 
GetLocalAddress(net::IPEndPoint * address) const174 int FakeSocket::GetLocalAddress(net::IPEndPoint* address) const {
175   NOTIMPLEMENTED();
176   return net::ERR_NOT_IMPLEMENTED;
177 }
178 
NetLog() const179 const net::BoundNetLog& FakeSocket::NetLog() const {
180   EXPECT_EQ(message_loop_, base::MessageLoop::current());
181   return net_log_;
182 }
183 
SetSubresourceSpeculation()184 void FakeSocket::SetSubresourceSpeculation() {
185   NOTIMPLEMENTED();
186 }
187 
SetOmniboxSpeculation()188 void FakeSocket::SetOmniboxSpeculation() {
189   NOTIMPLEMENTED();
190 }
191 
WasEverUsed() const192 bool FakeSocket::WasEverUsed() const {
193   NOTIMPLEMENTED();
194   return true;
195 }
196 
UsingTCPFastOpen() const197 bool FakeSocket::UsingTCPFastOpen() const {
198   NOTIMPLEMENTED();
199   return true;
200 }
201 
WasNpnNegotiated() const202 bool FakeSocket::WasNpnNegotiated() const {
203   return false;
204 }
205 
GetNegotiatedProtocol() const206 net::NextProto FakeSocket::GetNegotiatedProtocol() const {
207   NOTIMPLEMENTED();
208   return net::kProtoUnknown;
209 }
210 
GetSSLInfo(net::SSLInfo * ssl_info)211 bool FakeSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
212   return false;
213 }
214 
FakeUdpSocket()215 FakeUdpSocket::FakeUdpSocket()
216     : read_pending_(false),
217       input_pos_(0),
218       message_loop_(base::MessageLoop::current()) {
219 }
220 
~FakeUdpSocket()221 FakeUdpSocket::~FakeUdpSocket() {
222   EXPECT_EQ(message_loop_, base::MessageLoop::current());
223 }
224 
AppendInputPacket(const char * data,int data_size)225 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) {
226   EXPECT_EQ(message_loop_, base::MessageLoop::current());
227   input_packets_.push_back(std::string());
228   input_packets_.back().assign(data, data + data_size);
229 
230   // Complete pending read if any.
231   if (read_pending_) {
232     read_pending_ = false;
233     int result = std::min(data_size, read_buffer_size_);
234     memcpy(read_buffer_->data(), data, result);
235     input_pos_ = input_packets_.size();
236     read_callback_.Run(result);
237     read_buffer_ = NULL;
238   }
239 }
240 
Read(net::IOBuffer * buf,int buf_len,const net::CompletionCallback & callback)241 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len,
242                         const net::CompletionCallback& callback) {
243   EXPECT_EQ(message_loop_, base::MessageLoop::current());
244   if (input_pos_ < static_cast<int>(input_packets_.size())) {
245     int result = std::min(
246         buf_len, static_cast<int>(input_packets_[input_pos_].size()));
247     memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result);
248     ++input_pos_;
249     return result;
250   } else {
251     read_pending_ = true;
252     read_buffer_ = buf;
253     read_buffer_size_ = buf_len;
254     read_callback_ = callback;
255     return net::ERR_IO_PENDING;
256   }
257 }
258 
Write(net::IOBuffer * buf,int buf_len,const net::CompletionCallback & callback)259 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len,
260                          const net::CompletionCallback& callback) {
261   EXPECT_EQ(message_loop_, base::MessageLoop::current());
262   written_packets_.push_back(std::string());
263   written_packets_.back().assign(buf->data(), buf->data() + buf_len);
264   return buf_len;
265 }
266 
SetReceiveBufferSize(int32 size)267 int FakeUdpSocket::SetReceiveBufferSize(int32 size) {
268   NOTIMPLEMENTED();
269   return net::ERR_NOT_IMPLEMENTED;
270 }
271 
SetSendBufferSize(int32 size)272 int FakeUdpSocket::SetSendBufferSize(int32 size) {
273   NOTIMPLEMENTED();
274   return net::ERR_NOT_IMPLEMENTED;
275 }
276 
FakeSession()277 FakeSession::FakeSession()
278     : event_handler_(NULL),
279       candidate_config_(CandidateSessionConfig::CreateDefault()),
280       config_(SessionConfig::ForTest()),
281       message_loop_(base::MessageLoop::current()),
282       async_creation_(false),
283       jid_(kTestJid),
284       error_(OK),
285       closed_(false),
286       weak_factory_(this) {
287 }
288 
~FakeSession()289 FakeSession::~FakeSession() { }
290 
GetStreamChannel(const std::string & name)291 FakeSocket* FakeSession::GetStreamChannel(const std::string& name) {
292   return stream_channels_[name];
293 }
294 
GetDatagramChannel(const std::string & name)295 FakeUdpSocket* FakeSession::GetDatagramChannel(const std::string& name) {
296   return datagram_channels_[name];
297 }
298 
SetEventHandler(EventHandler * event_handler)299 void FakeSession::SetEventHandler(EventHandler* event_handler) {
300   event_handler_ = event_handler;
301 }
302 
error()303 ErrorCode FakeSession::error() {
304   return error_;
305 }
306 
jid()307 const std::string& FakeSession::jid() {
308   return jid_;
309 }
310 
candidate_config()311 const CandidateSessionConfig* FakeSession::candidate_config() {
312   return candidate_config_.get();
313 }
314 
config()315 const SessionConfig& FakeSession::config() {
316   return config_;
317 }
318 
set_config(const SessionConfig & config)319 void FakeSession::set_config(const SessionConfig& config) {
320   config_ = config;
321 }
322 
GetTransportChannelFactory()323 ChannelFactory* FakeSession::GetTransportChannelFactory() {
324   return this;
325 }
326 
GetMultiplexedChannelFactory()327 ChannelFactory* FakeSession::GetMultiplexedChannelFactory() {
328   return this;
329 }
330 
Close()331 void FakeSession::Close() {
332   closed_ = true;
333 }
334 
CreateStreamChannel(const std::string & name,const StreamChannelCallback & callback)335 void FakeSession::CreateStreamChannel(
336     const std::string& name,
337     const StreamChannelCallback& callback) {
338   scoped_ptr<FakeSocket> channel;
339   // If we are in the error state then we put NULL in the channels list, so that
340   // NotifyStreamChannelCallback() still calls the callback.
341   if (error_ == OK)
342     channel.reset(new FakeSocket());
343   stream_channels_[name] = channel.release();
344 
345   if (async_creation_) {
346     message_loop_->PostTask(FROM_HERE, base::Bind(
347         &FakeSession::NotifyStreamChannelCallback, weak_factory_.GetWeakPtr(),
348         name, callback));
349   } else {
350     NotifyStreamChannelCallback(name, callback);
351   }
352 }
353 
NotifyStreamChannelCallback(const std::string & name,const StreamChannelCallback & callback)354 void FakeSession::NotifyStreamChannelCallback(
355     const std::string& name,
356     const StreamChannelCallback& callback) {
357   if (stream_channels_.find(name) != stream_channels_.end())
358     callback.Run(scoped_ptr<net::StreamSocket>(stream_channels_[name]));
359 }
360 
CreateDatagramChannel(const std::string & name,const DatagramChannelCallback & callback)361 void FakeSession::CreateDatagramChannel(
362     const std::string& name,
363     const DatagramChannelCallback& callback) {
364   scoped_ptr<FakeUdpSocket> channel;
365   // If we are in the error state then we put NULL in the channels list, so that
366   // NotifyStreamChannelCallback() still calls the callback.
367   if (error_ == OK)
368     channel.reset(new FakeUdpSocket());
369   datagram_channels_[name] = channel.release();
370 
371   if (async_creation_) {
372     message_loop_->PostTask(FROM_HERE, base::Bind(
373         &FakeSession::NotifyDatagramChannelCallback, weak_factory_.GetWeakPtr(),
374         name, callback));
375   } else {
376     NotifyDatagramChannelCallback(name, callback);
377   }
378 }
379 
NotifyDatagramChannelCallback(const std::string & name,const DatagramChannelCallback & callback)380 void FakeSession::NotifyDatagramChannelCallback(
381     const std::string& name,
382     const DatagramChannelCallback& callback) {
383   if (datagram_channels_.find(name) != datagram_channels_.end())
384     callback.Run(scoped_ptr<net::Socket>(datagram_channels_[name]));
385 }
386 
CancelChannelCreation(const std::string & name)387 void FakeSession::CancelChannelCreation(const std::string& name) {
388   stream_channels_.erase(name);
389   datagram_channels_.erase(name);
390 }
391 
392 }  // namespace protocol
393 }  // namespace remoting
394