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#ifndef REMOTING_PROTOCOL_FAKE_SESSION_H_
6#define REMOTING_PROTOCOL_FAKE_SESSION_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "net/base/completion_callback.h"
15#include "net/socket/socket.h"
16#include "net/socket/stream_socket.h"
17#include "remoting/protocol/channel_factory.h"
18#include "remoting/protocol/session.h"
19
20namespace base {
21class MessageLoop;
22}
23
24namespace remoting {
25namespace protocol {
26
27extern const char kTestJid[];
28
29// FakeSocket implement net::Socket interface for FakeConnection. All data
30// written to FakeSocket is stored in a buffer returned by written_data().
31// Read() reads data from another buffer that can be set with AppendInputData().
32// Pending reads are supported, so if there is a pending read AppendInputData()
33// calls the read callback.
34//
35// Two fake sockets can be connected to each other using the
36// PairWith() method, e.g.: a->PairWith(b). After this all data
37// written to |a| can be read from |b| and vica versa. Two connected
38// sockets |a| and |b| must be created and used on the same thread.
39class FakeSocket : public net::StreamSocket {
40 public:
41  FakeSocket();
42  virtual ~FakeSocket();
43
44  const std::string& written_data() const { return written_data_; }
45
46  void set_write_limit(int write_limit) { write_limit_ = write_limit; }
47  void set_async_write(bool async_write) { async_write_ = async_write; }
48  void set_next_write_error(int error) { next_write_error_ = error; }
49  void set_next_read_error(int error) { next_read_error_ = error; }
50  void AppendInputData(const std::vector<char>& data);
51  void PairWith(FakeSocket* peer_socket);
52  int input_pos() const { return input_pos_; }
53  bool read_pending() const { return read_pending_; }
54
55  // net::Socket implementation.
56  virtual int Read(net::IOBuffer* buf, int buf_len,
57                   const net::CompletionCallback& callback) OVERRIDE;
58  virtual int Write(net::IOBuffer* buf, int buf_len,
59                    const net::CompletionCallback& callback) OVERRIDE;
60
61  virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
62  virtual int SetSendBufferSize(int32 size) OVERRIDE;
63
64  // net::StreamSocket interface.
65  virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
66  virtual void Disconnect() OVERRIDE;
67  virtual bool IsConnected() const OVERRIDE;
68  virtual bool IsConnectedAndIdle() const OVERRIDE;
69  virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE;
70  virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE;
71  virtual const net::BoundNetLog& NetLog() const OVERRIDE;
72  virtual void SetSubresourceSpeculation() OVERRIDE;
73  virtual void SetOmniboxSpeculation() OVERRIDE;
74  virtual bool WasEverUsed() const OVERRIDE;
75  virtual bool UsingTCPFastOpen() const OVERRIDE;
76  virtual bool WasNpnNegotiated() const OVERRIDE;
77  virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE;
78  virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE;
79
80 private:
81  void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len,
82                    const net::CompletionCallback& callback);
83  void DoWrite(net::IOBuffer* buf, int buf_len);
84
85  bool async_write_;
86  bool write_pending_;
87  int write_limit_;
88  int next_write_error_;
89
90  int next_read_error_;
91  bool read_pending_;
92  scoped_refptr<net::IOBuffer> read_buffer_;
93  int read_buffer_size_;
94  net::CompletionCallback read_callback_;
95  base::WeakPtr<FakeSocket> peer_socket_;
96
97  std::string written_data_;
98  std::string input_data_;
99  int input_pos_;
100
101  net::BoundNetLog net_log_;
102
103  base::MessageLoop* message_loop_;
104  base::WeakPtrFactory<FakeSocket> weak_factory_;
105
106  DISALLOW_COPY_AND_ASSIGN(FakeSocket);
107};
108
109// FakeUdpSocket is similar to FakeSocket but behaves as UDP socket. All written
110// packets are stored separetely in written_packets(). AppendInputPacket() adds
111// one packet that will be returned by Read().
112class FakeUdpSocket : public net::Socket {
113 public:
114  FakeUdpSocket();
115  virtual ~FakeUdpSocket();
116
117  const std::vector<std::string>& written_packets() const {
118    return written_packets_;
119  }
120
121  void AppendInputPacket(const char* data, int data_size);
122  int input_pos() const { return input_pos_; }
123
124  // net::Socket implementation.
125  virtual int Read(net::IOBuffer* buf, int buf_len,
126                   const net::CompletionCallback& callback) OVERRIDE;
127  virtual int Write(net::IOBuffer* buf, int buf_len,
128                    const net::CompletionCallback& callback) OVERRIDE;
129
130  virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
131  virtual int SetSendBufferSize(int32 size) OVERRIDE;
132
133 private:
134  bool read_pending_;
135  scoped_refptr<net::IOBuffer> read_buffer_;
136  int read_buffer_size_;
137  net::CompletionCallback read_callback_;
138
139  std::vector<std::string> written_packets_;
140  std::vector<std::string> input_packets_;
141  int input_pos_;
142
143  base::MessageLoop* message_loop_;
144
145  DISALLOW_COPY_AND_ASSIGN(FakeUdpSocket);
146};
147
148// FakeSession is a dummy protocol::Session that uses FakeSocket for all
149// channels.
150class FakeSession : public Session,
151                    public ChannelFactory {
152 public:
153  FakeSession();
154  virtual ~FakeSession();
155
156  EventHandler* event_handler() { return event_handler_; }
157
158  void set_async_creation(bool async_creation) {
159    async_creation_ = async_creation;
160  }
161
162  void set_error(ErrorCode error) { error_ = error; }
163
164  bool is_closed() const { return closed_; }
165
166  FakeSocket* GetStreamChannel(const std::string& name);
167  FakeUdpSocket* GetDatagramChannel(const std::string& name);
168
169  // Session interface.
170  virtual void SetEventHandler(EventHandler* event_handler) OVERRIDE;
171  virtual ErrorCode error() OVERRIDE;
172  virtual const std::string& jid() OVERRIDE;
173  virtual const CandidateSessionConfig* candidate_config() OVERRIDE;
174  virtual const SessionConfig& config() OVERRIDE;
175  virtual void set_config(const SessionConfig& config) OVERRIDE;
176  virtual ChannelFactory* GetTransportChannelFactory() OVERRIDE;
177  virtual ChannelFactory* GetMultiplexedChannelFactory() OVERRIDE;
178  virtual void Close() OVERRIDE;
179
180  // ChannelFactory interface.
181  virtual void CreateStreamChannel(
182      const std::string& name,
183      const StreamChannelCallback& callback) OVERRIDE;
184  virtual void CreateDatagramChannel(
185      const std::string& name,
186      const DatagramChannelCallback& callback) OVERRIDE;
187  virtual void CancelChannelCreation(const std::string& name) OVERRIDE;
188
189 public:
190  void NotifyStreamChannelCallback(
191      const std::string& name,
192      const StreamChannelCallback& callback);
193  void NotifyDatagramChannelCallback(
194      const std::string& name,
195      const DatagramChannelCallback& callback);
196
197  EventHandler* event_handler_;
198  scoped_ptr<const CandidateSessionConfig> candidate_config_;
199  SessionConfig config_;
200  base::MessageLoop* message_loop_;
201
202  bool async_creation_;
203
204  std::map<std::string, FakeSocket*> stream_channels_;
205  std::map<std::string, FakeUdpSocket*> datagram_channels_;
206
207  std::string jid_;
208
209  ErrorCode error_;
210  bool closed_;
211
212  base::WeakPtrFactory<FakeSession> weak_factory_;
213
214  DISALLOW_COPY_AND_ASSIGN(FakeSession);
215};
216
217}  // namespace protocol
218}  // namespace remoting
219
220#endif  // REMOTING_PROTOCOL_FAKE_SESSION_H_
221