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/proto/cached_network_parameters.pb.h"
14 #include "net/quic/quic_connection.h"
15 #include "net/quic/quic_packet_writer.h"
16 #include "net/quic/quic_session.h"
17 #include "net/spdy/spdy_framer.h"
18 #include "net/tools/quic/quic_dispatcher.h"
19 #include "net/tools/quic/quic_per_connection_packet_writer.h"
20 #include "net/tools/quic/quic_server_session.h"
21 #include "testing/gmock/include/gmock/gmock.h"
31 static const QuicConnectionId kTestConnectionId
= 42;
32 static const uint16 kTestPort
= 123;
33 static const uint32 kInitialStreamFlowControlWindowForTest
=
35 static const uint32 kInitialSessionFlowControlWindowForTest
=
36 1536 * 1024; // 1.5 MB
38 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
39 // nack ranges of width 1 packet, starting from |least_unacked|.
40 QuicAckFrame
MakeAckFrameWithNackRanges(size_t num_nack_ranges
,
41 QuicPacketSequenceNumber least_unacked
);
43 class TestSession
: public QuicSession
{
45 TestSession(QuicConnection
* connection
, const QuicConfig
& config
);
46 virtual ~TestSession();
48 MOCK_METHOD1(CreateIncomingDataStream
, QuicDataStream
*(QuicStreamId id
));
49 MOCK_METHOD0(CreateOutgoingDataStream
, QuicDataStream
*());
51 void SetCryptoStream(QuicCryptoStream
* stream
);
53 virtual QuicCryptoStream
* GetCryptoStream() override
;
56 QuicCryptoStream
* crypto_stream_
;
58 DISALLOW_COPY_AND_ASSIGN(TestSession
);
61 class MockPacketWriter
: public QuicPacketWriter
{
64 virtual ~MockPacketWriter();
66 MOCK_METHOD4(WritePacket
,
67 WriteResult(const char* buffer
,
69 const IPAddressNumber
& self_address
,
70 const IPEndPoint
& peer_address
));
71 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered
, bool());
72 MOCK_CONST_METHOD0(IsWriteBlocked
, bool());
73 MOCK_METHOD0(SetWritable
, void());
76 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter
);
79 class MockQuicServerSessionVisitor
: public QuicServerSessionVisitor
{
81 MockQuicServerSessionVisitor();
82 virtual ~MockQuicServerSessionVisitor();
83 MOCK_METHOD2(OnConnectionClosed
, void(QuicConnectionId connection_id
,
84 QuicErrorCode error
));
85 MOCK_METHOD1(OnWriteBlocked
,
86 void(QuicBlockedWriterInterface
* blocked_writer
));
87 MOCK_METHOD1(OnConnectionAddedToTimeWaitList
,
88 void(QuicConnectionId connection_id
));
89 MOCK_METHOD1(OnConnectionRemovedFromTimeWaitList
,
90 void(QuicConnectionId connection_id
));
93 DISALLOW_COPY_AND_ASSIGN(MockQuicServerSessionVisitor
);
96 class MockAckNotifierDelegate
: public QuicAckNotifier::DelegateInterface
{
98 MockAckNotifierDelegate();
100 MOCK_METHOD3(OnAckNotification
,
101 void(int num_retransmitted_packets
,
102 int num_retransmitted_bytes
,
103 QuicTime::Delta delta_largest_observed
));
106 // Object is ref counted.
107 virtual ~MockAckNotifierDelegate();
110 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate
);
113 // Creates per-connection packet writers that register themselves with the
114 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
115 // be routed to the appropriate QuicConnection.
116 class TestWriterFactory
: public QuicDispatcher::PacketWriterFactory
{
119 ~TestWriterFactory() override
;
121 QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
122 QuicConnection
* connection
) override
;
124 // Calls OnPacketSent on the last QuicConnection to write through one of the
125 // packet writers created by this factory.
126 void OnPacketSent(WriteResult result
);
129 class PerConnectionPacketWriter
: public QuicPerConnectionPacketWriter
{
131 PerConnectionPacketWriter(TestWriterFactory
* factory
,
132 QuicPacketWriter
* writer
,
133 QuicConnection
* connection
);
134 ~PerConnectionPacketWriter() override
;
136 WriteResult
WritePacket(const char* buffer
,
138 const IPAddressNumber
& self_address
,
139 const IPEndPoint
& peer_address
) override
;
142 TestWriterFactory
* factory_
;
145 // If an asynchronous write is happening and |writer| gets deleted, this
146 // clears the pointer to it to prevent use-after-free.
147 void Unregister(PerConnectionPacketWriter
* writer
);
149 PerConnectionPacketWriter
* current_writer_
;
152 class MockTimeWaitListManager
: public QuicTimeWaitListManager
{
154 MockTimeWaitListManager(QuicPacketWriter
* writer
,
155 QuicServerSessionVisitor
* visitor
,
156 QuicConnectionHelperInterface
* helper
);
158 ~MockTimeWaitListManager() override
;
160 MOCK_METHOD3(AddConnectionIdToTimeWait
,
161 void(QuicConnectionId connection_id
,
163 QuicEncryptedPacket
* close_packet
));
165 void QuicTimeWaitListManager_AddConnectionIdToTimeWait(
166 QuicConnectionId connection_id
,
168 QuicEncryptedPacket
* close_packet
) {
169 QuicTimeWaitListManager::AddConnectionIdToTimeWait(connection_id
, version
,
173 MOCK_METHOD5(ProcessPacket
,
174 void(const IPEndPoint
& server_address
,
175 const IPEndPoint
& client_address
,
176 QuicConnectionId connection_id
,
177 QuicPacketSequenceNumber sequence_number
,
178 const QuicEncryptedPacket
& packet
));
185 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_