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.
5 #ifndef REMOTING_PROTOCOL_FAKE_SESSION_H_
6 #define REMOTING_PROTOCOL_FAKE_SESSION_H_
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"
27 extern const char kTestJid
[];
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.
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.
39 class FakeSocket
: public net::StreamSocket
{
42 virtual ~FakeSocket();
44 const std::string
& written_data() const { return written_data_
; }
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_
; }
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
;
61 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
62 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
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
;
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
);
88 int next_write_error_
;
92 scoped_refptr
<net::IOBuffer
> read_buffer_
;
93 int read_buffer_size_
;
94 net::CompletionCallback read_callback_
;
95 base::WeakPtr
<FakeSocket
> peer_socket_
;
97 std::string written_data_
;
98 std::string input_data_
;
101 net::BoundNetLog net_log_
;
103 base::MessageLoop
* message_loop_
;
104 base::WeakPtrFactory
<FakeSocket
> weak_factory_
;
106 DISALLOW_COPY_AND_ASSIGN(FakeSocket
);
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().
112 class FakeUdpSocket
: public net::Socket
{
115 virtual ~FakeUdpSocket();
117 const std::vector
<std::string
>& written_packets() const {
118 return written_packets_
;
121 void AppendInputPacket(const char* data
, int data_size
);
122 int input_pos() const { return input_pos_
; }
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
;
130 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
131 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
135 scoped_refptr
<net::IOBuffer
> read_buffer_
;
136 int read_buffer_size_
;
137 net::CompletionCallback read_callback_
;
139 std::vector
<std::string
> written_packets_
;
140 std::vector
<std::string
> input_packets_
;
143 base::MessageLoop
* message_loop_
;
145 DISALLOW_COPY_AND_ASSIGN(FakeUdpSocket
);
148 // FakeSession is a dummy protocol::Session that uses FakeSocket for all
150 class FakeSession
: public Session
,
151 public ChannelFactory
{
154 virtual ~FakeSession();
156 EventHandler
* event_handler() { return event_handler_
; }
158 void set_async_creation(bool async_creation
) {
159 async_creation_
= async_creation
;
162 void set_error(ErrorCode error
) { error_
= error
; }
164 bool is_closed() const { return closed_
; }
166 FakeSocket
* GetStreamChannel(const std::string
& name
);
167 FakeUdpSocket
* GetDatagramChannel(const std::string
& name
);
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
;
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
;
190 void NotifyStreamChannelCallback(
191 const std::string
& name
,
192 const StreamChannelCallback
& callback
);
193 void NotifyDatagramChannelCallback(
194 const std::string
& name
,
195 const DatagramChannelCallback
& callback
);
197 EventHandler
* event_handler_
;
198 scoped_ptr
<const CandidateSessionConfig
> candidate_config_
;
199 SessionConfig config_
;
200 base::MessageLoop
* message_loop_
;
202 bool async_creation_
;
204 std::map
<std::string
, FakeSocket
*> stream_channels_
;
205 std::map
<std::string
, FakeUdpSocket
*> datagram_channels_
;
212 base::WeakPtrFactory
<FakeSession
> weak_factory_
;
214 DISALLOW_COPY_AND_ASSIGN(FakeSession
);
217 } // namespace protocol
218 } // namespace remoting
220 #endif // REMOTING_PROTOCOL_FAKE_SESSION_H_