1 // Copyright 2016 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 CHROMECAST_NET_FAKE_STREAM_SOCKET_H_
6 #define CHROMECAST_NET_FAKE_STREAM_SOCKET_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/macros.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/log/net_log_with_source.h"
15 #include "net/socket/stream_socket.h"
16 #include "net/traffic_annotation/network_traffic_annotation.h"
17 
18 namespace chromecast {
19 class SocketBuffer;
20 
21 // Fake StreamSocket that communicates with another instance in memory.
22 class FakeStreamSocket : public net::StreamSocket {
23  public:
24   FakeStreamSocket();
25   explicit FakeStreamSocket(const net::IPEndPoint& local_address);
26   ~FakeStreamSocket() override;
27 
28   // Sets the peer for this socket.
29   void SetPeer(FakeStreamSocket* peer);
30 
31   // Enables/disables "bad sender mode", where Write() will always try to send
32   // less than the full buffer. Disabled by default.
33   void SetBadSenderMode(bool bad_sender);
34 
35   // net::StreamSocket implementation:
36   int Read(net::IOBuffer* buf,
37            int buf_len,
38            net::CompletionOnceCallback callback) override;
39   int Write(
40       net::IOBuffer* buf,
41       int buf_len,
42       net::CompletionOnceCallback callback,
43       const net::NetworkTrafficAnnotationTag& traffic_annotation) override;
44   int SetReceiveBufferSize(int32_t size) override;
45   int SetSendBufferSize(int32_t size) override;
46   int Connect(net::CompletionOnceCallback callback) override;
47   void Disconnect() override;
48   bool IsConnected() const override;
49   bool IsConnectedAndIdle() const override;
50   int GetPeerAddress(net::IPEndPoint* address) const override;
51   int GetLocalAddress(net::IPEndPoint* address) const override;
52   const net::NetLogWithSource& NetLog() const override;
53   bool WasEverUsed() const override;
54   bool WasAlpnNegotiated() const override;
55   net::NextProto GetNegotiatedProtocol() const override;
56   bool GetSSLInfo(net::SSLInfo* ssl_info) override;
57   void GetConnectionAttempts(net::ConnectionAttempts* out) const override;
58   void ClearConnectionAttempts() override;
59   void AddConnectionAttempts(const net::ConnectionAttempts& attempts) override;
60   int64_t GetTotalReceivedBytes() const override;
61   void ApplySocketTag(const net::SocketTag& tag) override;
62 
63  private:
64   void RemoteDisconnected();
65 
66   const net::IPEndPoint local_address_;
67   const std::unique_ptr<SocketBuffer> buffer_;
68   FakeStreamSocket* peer_;
69   net::NetLogWithSource net_log_;
70   bool bad_sender_mode_ = false;
71 
72   DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket);
73 };
74 
75 }  // namespace chromecast
76 
77 #endif  // CHROMECAST_NET_FAKE_STREAM_SOCKET_H_
78