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 // Common utilities for Quic tests
7 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
8 #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
12 #include "base/strings/string_piece.h"
13 #include "net/quic/quic_connection.h"
14 #include "net/quic/quic_packet_writer.h"
15 #include "net/quic/quic_session.h"
16 #include "net/spdy/spdy_framer.h"
17 #include "net/tools/quic/quic_dispatcher.h"
18 #include "net/tools/quic/quic_per_connection_packet_writer.h"
19 #include "net/tools/quic/quic_server_session.h"
20 #include "testing/gmock/include/gmock/gmock.h"
30 static const QuicConnectionId kTestConnectionId
= 42;
31 static const int kTestPort
= 123;
32 static const uint32 kInitialStreamFlowControlWindowForTest
=
34 static const uint32 kInitialSessionFlowControlWindowForTest
=
37 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
38 // nack ranges of width 1 packet, starting from |least_unacked|.
39 QuicAckFrame
MakeAckFrameWithNackRanges(size_t num_nack_ranges
,
40 QuicPacketSequenceNumber least_unacked
);
42 class MockConnection
: public QuicConnection
{
44 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
45 explicit MockConnection(bool is_server
);
47 // Uses a MockHelper, ConnectionId of 42.
48 MockConnection(IPEndPoint address
, bool is_server
);
50 // Uses a MockHelper, and 127.0.0.1:123
51 MockConnection(QuicConnectionId connection_id
, bool is_server
);
53 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123.
54 MockConnection(bool is_server
, const QuicVersionVector
& supported_versions
);
56 virtual ~MockConnection();
58 // If the constructor that uses a MockHelper has been used then this method
59 // will advance the time of the MockClock.
60 void AdvanceTime(QuicTime::Delta delta
);
62 MOCK_METHOD3(ProcessUdpPacket
, void(const IPEndPoint
& self_address
,
63 const IPEndPoint
& peer_address
,
64 const QuicEncryptedPacket
& packet
));
65 MOCK_METHOD1(SendConnectionClose
, void(QuicErrorCode error
));
66 MOCK_METHOD2(SendConnectionCloseWithDetails
, void(
68 const std::string
& details
));
69 MOCK_METHOD2(SendConnectionClosePacket
, void(QuicErrorCode error
,
70 const std::string
& details
));
71 MOCK_METHOD3(SendRstStream
, void(QuicStreamId id
,
72 QuicRstStreamErrorCode error
,
73 QuicStreamOffset bytes_written
));
74 MOCK_METHOD3(SendGoAway
, void(QuicErrorCode error
,
75 QuicStreamId last_good_stream_id
,
76 const std::string
& reason
));
77 MOCK_METHOD1(SendBlocked
, void(QuicStreamId id
));
78 MOCK_METHOD2(SendWindowUpdate
, void(QuicStreamId id
,
79 QuicStreamOffset byte_offset
));
80 MOCK_METHOD0(OnCanWrite
, void());
81 MOCK_CONST_METHOD0(HasPendingWrites
, bool());
83 void ReallyProcessUdpPacket(const IPEndPoint
& self_address
,
84 const IPEndPoint
& peer_address
,
85 const QuicEncryptedPacket
& packet
) {
86 return QuicConnection::ProcessUdpPacket(self_address
, peer_address
, packet
);
89 virtual bool OnProtocolVersionMismatch(QuicVersion version
) { return false; }
92 scoped_ptr
<QuicConnectionHelperInterface
> helper_
;
94 DISALLOW_COPY_AND_ASSIGN(MockConnection
);
97 class TestSession
: public QuicSession
{
99 TestSession(QuicConnection
* connection
, const QuicConfig
& config
);
100 virtual ~TestSession();
102 MOCK_METHOD1(CreateIncomingDataStream
, QuicDataStream
*(QuicStreamId id
));
103 MOCK_METHOD0(CreateOutgoingDataStream
, QuicDataStream
*());
105 void SetCryptoStream(QuicCryptoStream
* stream
);
107 virtual QuicCryptoStream
* GetCryptoStream() OVERRIDE
;
110 QuicCryptoStream
* crypto_stream_
;
112 DISALLOW_COPY_AND_ASSIGN(TestSession
);
115 class MockPacketWriter
: public QuicPacketWriter
{
118 virtual ~MockPacketWriter();
120 MOCK_METHOD4(WritePacket
,
121 WriteResult(const char* buffer
,
123 const IPAddressNumber
& self_address
,
124 const IPEndPoint
& peer_address
));
125 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered
, bool());
126 MOCK_CONST_METHOD0(IsWriteBlocked
, bool());
127 MOCK_METHOD0(SetWritable
, void());
130 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter
);
133 class MockQuicServerSessionVisitor
: public QuicServerSessionVisitor
{
135 MockQuicServerSessionVisitor();
136 virtual ~MockQuicServerSessionVisitor();
137 MOCK_METHOD2(OnConnectionClosed
, void(QuicConnectionId connection_id
,
138 QuicErrorCode error
));
139 MOCK_METHOD1(OnWriteBlocked
,
140 void(QuicBlockedWriterInterface
* blocked_writer
));
143 DISALLOW_COPY_AND_ASSIGN(MockQuicServerSessionVisitor
);
146 class MockAckNotifierDelegate
: public QuicAckNotifier::DelegateInterface
{
148 MockAckNotifierDelegate();
150 MOCK_METHOD5(OnAckNotification
, void(int num_original_packets
,
151 int num_original_bytes
,
152 int num_retransmitted_packets
,
153 int num_retransmitted_bytes
,
154 QuicTime::Delta delta_largest_observed
));
157 // Object is ref counted.
158 virtual ~MockAckNotifierDelegate();
160 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate
);
163 // Creates per-connection packet writers that register themselves with the
164 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
165 // be routed to the appropriate QuicConnection.
166 class TestWriterFactory
: public QuicDispatcher::PacketWriterFactory
{
169 virtual ~TestWriterFactory();
171 virtual QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
172 QuicConnection
* connection
) override
;
174 // Calls OnPacketSent on the last QuicConnection to write through one of the
175 // packet writers created by this factory.
176 void OnPacketSent(WriteResult result
);
179 class PerConnectionPacketWriter
: public QuicPerConnectionPacketWriter
{
181 PerConnectionPacketWriter(TestWriterFactory
* factory
,
182 QuicPacketWriter
* writer
,
183 QuicConnection
* connection
);
184 virtual ~PerConnectionPacketWriter();
186 virtual WriteResult
WritePacket(
189 const IPAddressNumber
& self_address
,
190 const IPEndPoint
& peer_address
) OVERRIDE
;
193 TestWriterFactory
* factory_
;
196 // If an asynchronous write is happening and |writer| gets deleted, this
197 // clears the pointer to it to prevent use-after-free.
198 void Unregister(PerConnectionPacketWriter
* writer
);
200 PerConnectionPacketWriter
* current_writer_
;
207 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_