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 int kTestPort1
= 234;
20 const char kTestIpAddress2
[] = "133.11.22.33";
21 const int 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 virtual ~FakeSocket();
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 virtual int Read(net::IOBuffer
* buf
, int buf_len
,
45 const net::CompletionCallback
& callback
) OVERRIDE
;
46 virtual int Write(net::IOBuffer
* buf
, int buf_len
,
47 const net::CompletionCallback
& callback
) OVERRIDE
;
48 virtual int SetReceiveBufferSize(int32 size
) OVERRIDE
;
49 virtual int SetSendBufferSize(int32 size
) OVERRIDE
;
50 virtual int Connect(const net::CompletionCallback
& callback
) OVERRIDE
;
51 virtual void Disconnect() OVERRIDE
;
52 virtual bool IsConnected() const OVERRIDE
;
53 virtual bool IsConnectedAndIdle() const OVERRIDE
;
54 virtual int GetPeerAddress(net::IPEndPoint
* address
) const OVERRIDE
;
55 virtual int GetLocalAddress(net::IPEndPoint
* address
) const OVERRIDE
;
56 virtual const net::BoundNetLog
& NetLog() const OVERRIDE
;
57 virtual void SetSubresourceSpeculation() OVERRIDE
;
58 virtual void SetOmniboxSpeculation() OVERRIDE
;
59 virtual bool WasEverUsed() const OVERRIDE
;
60 virtual bool UsingTCPFastOpen() const OVERRIDE
;
61 virtual bool WasNpnNegotiated() const OVERRIDE
;
62 virtual net::NextProto
GetNegotiatedProtocol() const OVERRIDE
;
63 virtual bool GetSSLInfo(net::SSLInfo
* ssl_info
) OVERRIDE
;
66 void DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
, int buf_len
,
67 const net::CompletionCallback
& callback
);
70 scoped_refptr
<net::IOBuffer
> read_buffer_
;
71 int read_buffer_size_
;
72 net::CompletionCallback read_callback_
;
74 std::string input_data_
;
77 std::string
* written_data_
;
81 net::IPEndPoint peer_address_
;
82 net::IPEndPoint local_address_
;
84 net::BoundNetLog net_log_
;
87 void CreateRandomPacket(std::vector
<char>* packet
);
88 void CreateStunRequest(std::vector
<char>* packet
);
89 void CreateStunResponse(std::vector
<char>* packet
);
90 void CreateStunError(std::vector
<char>* packet
);
92 net::IPEndPoint
ParseAddress(const std::string ip_str
, int port
);
94 MATCHER_P(MatchMessage
, type
, "") {
95 return arg
->type() == type
;
98 MATCHER_P(MatchPacketMessage
, packet_content
, "") {
99 if (arg
->type() != P2PMsg_OnDataReceived::ID
)
101 P2PMsg_OnDataReceived::Param params
;
102 P2PMsg_OnDataReceived::Read(arg
, ¶ms
);
103 return params
.c
== packet_content
;
106 MATCHER_P(MatchIncomingSocketMessage
, address
, "") {
107 if (arg
->type() != P2PMsg_OnIncomingTcpConnection::ID
)
109 P2PMsg_OnIncomingTcpConnection::Param params
;
110 P2PMsg_OnIncomingTcpConnection::Read(
112 return params
.b
== address
;
115 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_