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 CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_
10 #include "content/common/p2p_messages.h"
11 #include "ipc/ipc_sender.h"
12 #include "net/base/net_errors.h"
13 #include "net/socket/stream_socket.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTestLocalIpAddress
[] = "123.44.22.4";
18 const char kTestIpAddress1
[] = "123.44.22.31";
19 const uint16 kTestPort1
= 234;
20 const char kTestIpAddress2
[] = "133.11.22.33";
21 const uint16 kTestPort2
= 543;
23 class MockIPCSender
: public IPC::Sender
{
26 virtual ~MockIPCSender();
28 MOCK_METHOD1(Send
, bool(IPC::Message
* msg
));
31 class FakeSocket
: public net::StreamSocket
{
33 FakeSocket(std::string
* written_data
);
34 ~FakeSocket() override
;
36 void set_async_write(bool async_write
) { async_write_
= async_write
; }
37 void AppendInputData(const char* data
, int data_size
);
38 int input_pos() const { return input_pos_
; }
39 bool read_pending() const { return read_pending_
; }
40 void SetPeerAddress(const net::IPEndPoint
& peer_address
);
41 void SetLocalAddress(const net::IPEndPoint
& local_address
);
43 // net::Socket implementation.
44 int Read(net::IOBuffer
* buf
,
46 const net::CompletionCallback
& callback
) override
;
47 int Write(net::IOBuffer
* buf
,
49 const net::CompletionCallback
& callback
) override
;
50 int SetReceiveBufferSize(int32 size
) override
;
51 int SetSendBufferSize(int32 size
) override
;
52 int Connect(const net::CompletionCallback
& callback
) override
;
53 void Disconnect() override
;
54 bool IsConnected() const override
;
55 bool IsConnectedAndIdle() const override
;
56 int GetPeerAddress(net::IPEndPoint
* address
) const override
;
57 int GetLocalAddress(net::IPEndPoint
* address
) const override
;
58 const net::BoundNetLog
& NetLog() const override
;
59 void SetSubresourceSpeculation() override
;
60 void SetOmniboxSpeculation() override
;
61 bool WasEverUsed() const override
;
62 bool UsingTCPFastOpen() const override
;
63 bool WasNpnNegotiated() const override
;
64 net::NextProto
GetNegotiatedProtocol() const override
;
65 bool GetSSLInfo(net::SSLInfo
* ssl_info
) override
;
66 void GetConnectionAttempts(net::ConnectionAttempts
* out
) const override
;
67 void ClearConnectionAttempts() override
{}
68 void AddConnectionAttempts(const net::ConnectionAttempts
& attempts
) override
{
72 void DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
, int buf_len
,
73 const net::CompletionCallback
& callback
);
76 scoped_refptr
<net::IOBuffer
> read_buffer_
;
77 int read_buffer_size_
;
78 net::CompletionCallback read_callback_
;
80 std::string input_data_
;
83 std::string
* written_data_
;
87 net::IPEndPoint peer_address_
;
88 net::IPEndPoint local_address_
;
90 net::BoundNetLog net_log_
;
93 void CreateRandomPacket(std::vector
<char>* packet
);
94 void CreateStunRequest(std::vector
<char>* packet
);
95 void CreateStunResponse(std::vector
<char>* packet
);
96 void CreateStunError(std::vector
<char>* packet
);
98 net::IPEndPoint
ParseAddress(const std::string ip_str
, uint16 port
);
100 MATCHER_P(MatchMessage
, type
, "") {
101 return arg
->type() == type
;
104 MATCHER_P(MatchPacketMessage
, packet_content
, "") {
105 if (arg
->type() != P2PMsg_OnDataReceived::ID
)
107 P2PMsg_OnDataReceived::Param params
;
108 P2PMsg_OnDataReceived::Read(arg
, ¶ms
);
109 return base::get
<2>(params
) == packet_content
;
112 MATCHER_P(MatchIncomingSocketMessage
, address
, "") {
113 if (arg
->type() != P2PMsg_OnIncomingTcpConnection::ID
)
115 P2PMsg_OnIncomingTcpConnection::Param params
;
116 P2PMsg_OnIncomingTcpConnection::Read(
118 return base::get
<1>(params
) == address
;
121 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_