fake_stream_socket.h 2.5 KB

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