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_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
8 #define NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
13 #include "base/basictypes.h"
14 #include "base/strings/string_piece.h"
15 #include "net/quic/congestion_control/loss_detection_interface.h"
16 #include "net/quic/congestion_control/send_algorithm_interface.h"
17 #include "net/quic/quic_ack_notifier.h"
18 #include "net/quic/quic_client_session_base.h"
19 #include "net/quic/quic_connection.h"
20 #include "net/quic/quic_framer.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_sent_packet_manager.h"
23 #include "net/quic/quic_session.h"
24 #include "net/quic/test_tools/mock_clock.h"
25 #include "net/quic/test_tools/mock_random.h"
26 #include "net/spdy/spdy_framer.h"
27 #include "net/tools/quic/quic_dispatcher.h"
28 #include "net/tools/quic/quic_per_connection_packet_writer.h"
29 #include "net/tools/quic/quic_server_session.h"
30 #include "testing/gmock/include/gmock/gmock.h"
36 static const QuicConnectionId kTestConnectionId
= 42;
37 static const uint16 kTestPort
= 123;
38 static const uint32 kInitialStreamFlowControlWindowForTest
=
40 static const uint32 kInitialSessionFlowControlWindowForTest
=
41 1536 * 1024; // 1.5 MB
42 // Data stream IDs start at 5: the crypto stream is 1, headers stream is 3.
43 static const QuicStreamId kClientDataStreamId1
= 5;
44 static const QuicStreamId kClientDataStreamId2
= 7;
45 static const QuicStreamId kClientDataStreamId3
= 9;
47 // Returns the test peer IP address.
48 IPAddressNumber
TestPeerIPAddress();
50 // Upper limit on versions we support.
51 QuicVersion
QuicVersionMax();
53 // Lower limit on versions we support.
54 QuicVersion
QuicVersionMin();
56 // Returns an address for 127.0.0.1.
57 IPAddressNumber
Loopback4();
59 // Returns an address for ::1.
60 IPAddressNumber
Loopback6();
62 // Returns an address for 0.0.0.0.
63 IPAddressNumber
Any4();
65 void GenerateBody(std::string
* body
, int length
);
67 // Create an encrypted packet for testing.
68 // If versions == nullptr, uses &QuicSupportedVersions().
69 // Note that the packet is encrypted with NullEncrypter, so to decrypt the
70 // constructed packet, the framer must be set to use NullDecrypter.
71 QuicEncryptedPacket
* ConstructEncryptedPacket(
72 QuicConnectionId connection_id
,
75 QuicPacketSequenceNumber sequence_number
,
76 const std::string
& data
,
77 QuicConnectionIdLength connection_id_length
,
78 QuicSequenceNumberLength sequence_number_length
,
79 QuicVersionVector
* versions
);
81 // This form assumes |versions| == nullptr.
82 QuicEncryptedPacket
* ConstructEncryptedPacket(
83 QuicConnectionId connection_id
,
86 QuicPacketSequenceNumber sequence_number
,
87 const std::string
& data
,
88 QuicConnectionIdLength connection_id_length
,
89 QuicSequenceNumberLength sequence_number_length
);
91 // This form assumes |connection_id_length| == PACKET_8BYTE_CONNECTION_ID,
92 // |sequence_number_length| == PACKET_6BYTE_SEQUENCE_NUMBER and
93 // |versions| == nullptr.
94 QuicEncryptedPacket
* ConstructEncryptedPacket(
95 QuicConnectionId connection_id
,
98 QuicPacketSequenceNumber sequence_number
,
99 const std::string
& data
);
101 // Create an encrypted packet for testing whose data portion erroneous.
102 // The specific way the data portion is erroneous is not specified, but
103 // it is an error that QuicFramer detects.
104 // Note that the packet is encrypted with NullEncrypter, so to decrypt the
105 // constructed packet, the framer must be set to use NullDecrypter.
106 QuicEncryptedPacket
* ConstructMisFramedEncryptedPacket(
107 QuicConnectionId connection_id
,
110 QuicPacketSequenceNumber sequence_number
,
111 const std::string
& data
,
112 QuicConnectionIdLength connection_id_length
,
113 QuicSequenceNumberLength sequence_number_length
,
114 QuicVersionVector
* versions
);
116 void CompareCharArraysWithHexError(const std::string
& description
,
118 const int actual_len
,
119 const char* expected
,
120 const int expected_len
);
122 bool DecodeHexString(const base::StringPiece
& hex
, std::string
* bytes
);
124 // Returns the length of a QuicPacket that is capable of holding either a
125 // stream frame or a minimal ack frame. Sets |*payload_length| to the number
126 // of bytes of stream data that will fit in such a packet.
127 size_t GetPacketLengthForOneStream(
129 bool include_version
,
130 QuicConnectionIdLength connection_id_length
,
131 QuicSequenceNumberLength sequence_number_length
,
132 InFecGroup is_in_fec_group
,
133 size_t* payload_length
);
135 // Returns QuicConfig set to default values.
136 QuicConfig
DefaultQuicConfig();
138 // Returns a QuicConfig set to default values that supports stateless rejects.
139 QuicConfig
DefaultQuicConfigStatelessRejects();
141 // Returns a version vector consisting of |version|.
142 QuicVersionVector
SupportedVersions(QuicVersion version
);
144 // Testing convenience method to construct a QuicAckFrame with entropy_hash set
145 // to 0 and largest_observed from peer set to |largest_observed|.
146 QuicAckFrame
MakeAckFrame(QuicPacketSequenceNumber largest_observed
);
148 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
149 // nack ranges of width 1 packet, starting from |least_unacked|.
150 QuicAckFrame
MakeAckFrameWithNackRanges(size_t num_nack_ranges
,
151 QuicPacketSequenceNumber least_unacked
);
153 // Returns a QuicPacket that is owned by the caller, and
154 // is populated with the fields in |header| and |frames|, or is nullptr if the
155 // packet could not be created.
156 QuicPacket
* BuildUnsizedDataPacket(QuicFramer
* framer
,
157 const QuicPacketHeader
& header
,
158 const QuicFrames
& frames
);
159 // Returns a QuicPacket that is owned by the caller, and of size |packet_size|.
160 QuicPacket
* BuildUnsizedDataPacket(QuicFramer
* framer
,
161 const QuicPacketHeader
& header
,
162 const QuicFrames
& frames
,
165 template<typename SaveType
>
168 ValueRestore(SaveType
* name
, SaveType value
)
181 DISALLOW_COPY_AND_ASSIGN(ValueRestore
);
184 // Simple random number generator used to compute random numbers suitable
185 // for pseudo-randomly dropping packets in tests. It works by computing
186 // the sha1 hash of the current seed, and using the first 64 bits as
187 // the next random number, and the next seed.
190 SimpleRandom() : seed_(0) {}
192 // Returns a random number in the range [0, kuint64max].
195 void set_seed(uint64 seed
) { seed_
= seed
; }
200 DISALLOW_COPY_AND_ASSIGN(SimpleRandom
);
203 class MockFramerVisitor
: public QuicFramerVisitorInterface
{
206 ~MockFramerVisitor() override
;
208 MOCK_METHOD1(OnError
, void(QuicFramer
* framer
));
209 // The constructor sets this up to return false by default.
210 MOCK_METHOD1(OnProtocolVersionMismatch
, bool(QuicVersion version
));
211 MOCK_METHOD0(OnPacket
, void());
212 MOCK_METHOD1(OnPublicResetPacket
, void(const QuicPublicResetPacket
& header
));
213 MOCK_METHOD1(OnVersionNegotiationPacket
,
214 void(const QuicVersionNegotiationPacket
& packet
));
215 MOCK_METHOD0(OnRevivedPacket
, void());
216 // The constructor sets this up to return true by default.
217 MOCK_METHOD1(OnUnauthenticatedHeader
, bool(const QuicPacketHeader
& header
));
218 // The constructor sets this up to return true by default.
219 MOCK_METHOD1(OnUnauthenticatedPublicHeader
, bool(
220 const QuicPacketPublicHeader
& header
));
221 MOCK_METHOD1(OnDecryptedPacket
, void(EncryptionLevel level
));
222 MOCK_METHOD1(OnPacketHeader
, bool(const QuicPacketHeader
& header
));
223 MOCK_METHOD1(OnFecProtectedPayload
, void(base::StringPiece payload
));
224 MOCK_METHOD1(OnStreamFrame
, bool(const QuicStreamFrame
& frame
));
225 MOCK_METHOD1(OnAckFrame
, bool(const QuicAckFrame
& frame
));
226 MOCK_METHOD1(OnStopWaitingFrame
, bool(const QuicStopWaitingFrame
& frame
));
227 MOCK_METHOD1(OnPingFrame
, bool(const QuicPingFrame
& frame
));
228 MOCK_METHOD1(OnFecData
, void(const QuicFecData
& fec
));
229 MOCK_METHOD1(OnRstStreamFrame
, bool(const QuicRstStreamFrame
& frame
));
230 MOCK_METHOD1(OnConnectionCloseFrame
,
231 bool(const QuicConnectionCloseFrame
& frame
));
232 MOCK_METHOD1(OnGoAwayFrame
, bool(const QuicGoAwayFrame
& frame
));
233 MOCK_METHOD1(OnWindowUpdateFrame
, bool(const QuicWindowUpdateFrame
& frame
));
234 MOCK_METHOD1(OnBlockedFrame
, bool(const QuicBlockedFrame
& frame
));
235 MOCK_METHOD0(OnPacketComplete
, void());
238 DISALLOW_COPY_AND_ASSIGN(MockFramerVisitor
);
241 class NoOpFramerVisitor
: public QuicFramerVisitorInterface
{
243 NoOpFramerVisitor() {}
245 void OnError(QuicFramer
* framer
) override
{}
246 void OnPacket() override
{}
247 void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) override
{}
248 void OnVersionNegotiationPacket(
249 const QuicVersionNegotiationPacket
& packet
) override
{}
250 void OnRevivedPacket() override
{}
251 bool OnProtocolVersionMismatch(QuicVersion version
) override
;
252 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
;
253 bool OnUnauthenticatedPublicHeader(
254 const QuicPacketPublicHeader
& header
) override
;
255 void OnDecryptedPacket(EncryptionLevel level
) override
{}
256 bool OnPacketHeader(const QuicPacketHeader
& header
) override
;
257 void OnFecProtectedPayload(base::StringPiece payload
) override
{}
258 bool OnStreamFrame(const QuicStreamFrame
& frame
) override
;
259 bool OnAckFrame(const QuicAckFrame
& frame
) override
;
260 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) override
;
261 bool OnPingFrame(const QuicPingFrame
& frame
) override
;
262 void OnFecData(const QuicFecData
& fec
) override
{}
263 bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) override
;
264 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame
& frame
) override
;
265 bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) override
;
266 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
;
267 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
;
268 void OnPacketComplete() override
{}
271 DISALLOW_COPY_AND_ASSIGN(NoOpFramerVisitor
);
274 class MockConnectionVisitor
: public QuicConnectionVisitorInterface
{
276 MockConnectionVisitor();
277 ~MockConnectionVisitor() override
;
279 MOCK_METHOD1(OnStreamFrame
, void(const QuicStreamFrame
& frame
));
280 MOCK_METHOD1(OnWindowUpdateFrame
, void(const QuicWindowUpdateFrame
& frame
));
281 MOCK_METHOD1(OnBlockedFrame
, void(const QuicBlockedFrame
& frame
));
282 MOCK_METHOD1(OnRstStream
, void(const QuicRstStreamFrame
& frame
));
283 MOCK_METHOD1(OnGoAway
, void(const QuicGoAwayFrame
& frame
));
284 MOCK_METHOD2(OnConnectionClosed
, void(QuicErrorCode error
, bool from_peer
));
285 MOCK_METHOD0(OnWriteBlocked
, void());
286 MOCK_METHOD0(OnCanWrite
, void());
287 MOCK_METHOD1(OnCongestionWindowChange
, void(QuicTime now
));
288 MOCK_CONST_METHOD0(WillingAndAbleToWrite
, bool());
289 MOCK_CONST_METHOD0(HasPendingHandshake
, bool());
290 MOCK_CONST_METHOD0(HasOpenDynamicStreams
, bool());
291 MOCK_METHOD1(OnSuccessfulVersionNegotiation
,
292 void(const QuicVersion
& version
));
293 MOCK_METHOD0(OnConfigNegotiated
, void());
296 DISALLOW_COPY_AND_ASSIGN(MockConnectionVisitor
);
299 class MockHelper
: public QuicConnectionHelperInterface
{
302 ~MockHelper() override
;
303 const QuicClock
* GetClock() const override
;
304 QuicRandom
* GetRandomGenerator() override
;
305 QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) override
;
306 void AdvanceTime(QuicTime::Delta delta
);
310 MockRandom random_generator_
;
312 DISALLOW_COPY_AND_ASSIGN(MockHelper
);
315 class NiceMockPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
317 NiceMockPacketWriterFactory() {}
318 ~NiceMockPacketWriterFactory() override
{}
320 QuicPacketWriter
* Create(QuicConnection
* /*connection*/) const override
;
323 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory
);
326 class MockConnection
: public QuicConnection
{
328 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
329 explicit MockConnection(Perspective perspective
);
331 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
332 MockConnection(Perspective perspective
, bool is_secure
);
334 // Uses a MockHelper, ConnectionId of 42.
335 MockConnection(IPEndPoint address
, Perspective perspective
);
337 // Uses a MockHelper, and 127.0.0.1:123.
338 MockConnection(QuicConnectionId connection_id
, Perspective perspective
);
340 // Uses a MockHelper, and 127.0.0.1:123.
341 MockConnection(QuicConnectionId connection_id
,
342 Perspective perspective
,
345 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123.
346 MockConnection(Perspective perspective
,
347 const QuicVersionVector
& supported_versions
);
349 MockConnection(QuicConnectionId connection_id
,
351 Perspective perspective
,
353 const QuicVersionVector
& supported_versions
);
355 ~MockConnection() override
;
357 // If the constructor that uses a MockHelper has been used then this method
358 // will advance the time of the MockClock.
359 void AdvanceTime(QuicTime::Delta delta
);
361 MOCK_METHOD3(ProcessUdpPacket
, void(const IPEndPoint
& self_address
,
362 const IPEndPoint
& peer_address
,
363 const QuicEncryptedPacket
& packet
));
364 MOCK_METHOD1(SendConnectionClose
, void(QuicErrorCode error
));
365 MOCK_METHOD2(SendConnectionCloseWithDetails
,
366 void(QuicErrorCode error
, const std::string
& details
));
367 MOCK_METHOD2(SendConnectionClosePacket
,
368 void(QuicErrorCode error
, const std::string
& details
));
369 MOCK_METHOD3(SendRstStream
, void(QuicStreamId id
,
370 QuicRstStreamErrorCode error
,
371 QuicStreamOffset bytes_written
));
372 MOCK_METHOD3(SendGoAway
,
373 void(QuicErrorCode error
,
374 QuicStreamId last_good_stream_id
,
375 const std::string
& reason
));
376 MOCK_METHOD1(SendBlocked
, void(QuicStreamId id
));
377 MOCK_METHOD2(SendWindowUpdate
, void(QuicStreamId id
,
378 QuicStreamOffset byte_offset
));
379 MOCK_METHOD0(OnCanWrite
, void());
381 MOCK_METHOD1(OnSendConnectionState
, void(const CachedNetworkParameters
&));
382 MOCK_METHOD2(ResumeConnectionState
,
383 void(const CachedNetworkParameters
&, bool));
385 MOCK_METHOD1(OnError
, void(QuicFramer
* framer
));
386 void QuicConnection_OnError(QuicFramer
* framer
) {
387 QuicConnection::OnError(framer
);
390 void ReallyProcessUdpPacket(const IPEndPoint
& self_address
,
391 const IPEndPoint
& peer_address
,
392 const QuicEncryptedPacket
& packet
) {
393 QuicConnection::ProcessUdpPacket(self_address
, peer_address
, packet
);
396 bool OnProtocolVersionMismatch(QuicVersion version
) override
{
401 scoped_ptr
<QuicConnectionHelperInterface
> helper_
;
403 DISALLOW_COPY_AND_ASSIGN(MockConnection
);
406 class PacketSavingConnection
: public MockConnection
{
408 explicit PacketSavingConnection(Perspective perspective
);
410 PacketSavingConnection(Perspective perspective
,
411 const QuicVersionVector
& supported_versions
);
413 ~PacketSavingConnection() override
;
415 void SendOrQueuePacket(QueuedPacket packet
) override
;
417 std::vector
<QuicEncryptedPacket
*> encrypted_packets_
;
420 DISALLOW_COPY_AND_ASSIGN(PacketSavingConnection
);
423 class MockQuicSpdySession
: public QuicSpdySession
{
425 explicit MockQuicSpdySession(QuicConnection
* connection
);
426 ~MockQuicSpdySession() override
;
428 QuicCryptoStream
* GetCryptoStream() override
{ return crypto_stream_
.get(); }
430 MOCK_METHOD2(OnConnectionClosed
, void(QuicErrorCode error
, bool from_peer
));
431 MOCK_METHOD1(CreateIncomingDynamicStream
, QuicDataStream
*(QuicStreamId id
));
432 MOCK_METHOD0(CreateOutgoingDynamicStream
, QuicDataStream
*());
433 MOCK_METHOD6(WritevData
,
434 QuicConsumedData(QuicStreamId id
,
435 const QuicIOVector
& data
,
436 QuicStreamOffset offset
,
438 FecProtection fec_protection
,
439 QuicAckNotifier::DelegateInterface
*));
440 MOCK_METHOD2(OnStreamHeaders
, void(QuicStreamId stream_id
,
441 base::StringPiece headers_data
));
442 MOCK_METHOD2(OnStreamHeadersPriority
, void(QuicStreamId stream_id
,
443 QuicPriority priority
));
444 MOCK_METHOD3(OnStreamHeadersComplete
, void(QuicStreamId stream_id
,
447 MOCK_METHOD3(SendRstStream
, void(QuicStreamId stream_id
,
448 QuicRstStreamErrorCode error
,
449 QuicStreamOffset bytes_written
));
450 MOCK_METHOD0(IsCryptoHandshakeConfirmed
, bool());
452 using QuicSession::ActivateStream
;
455 scoped_ptr
<QuicCryptoStream
> crypto_stream_
;
457 DISALLOW_COPY_AND_ASSIGN(MockQuicSpdySession
);
460 class TestQuicSpdyServerSession
: public QuicSpdySession
{
462 TestQuicSpdyServerSession(QuicConnection
* connection
,
463 const QuicConfig
& config
,
464 const QuicCryptoServerConfig
* crypto_config
);
465 ~TestQuicSpdyServerSession() override
;
467 MOCK_METHOD1(CreateIncomingDynamicStream
, QuicDataStream
*(QuicStreamId id
));
468 MOCK_METHOD0(CreateOutgoingDynamicStream
, QuicDataStream
*());
470 QuicCryptoServerStream
* GetCryptoStream() override
;
473 scoped_ptr
<QuicCryptoServerStream
> crypto_stream_
;
475 DISALLOW_COPY_AND_ASSIGN(TestQuicSpdyServerSession
);
478 class TestQuicSpdyClientSession
: public QuicClientSessionBase
{
480 TestQuicSpdyClientSession(QuicConnection
* connection
,
481 const QuicConfig
& config
,
482 const QuicServerId
& server_id
,
483 QuicCryptoClientConfig
* crypto_config
);
484 ~TestQuicSpdyClientSession() override
;
486 // QuicClientSessionBase
487 MOCK_METHOD1(OnProofValid
,
488 void(const QuicCryptoClientConfig::CachedState
& cached
));
489 MOCK_METHOD1(OnProofVerifyDetailsAvailable
,
490 void(const ProofVerifyDetails
& verify_details
));
492 // TestQuicSpdyClientSession
493 MOCK_METHOD1(CreateIncomingDynamicStream
, QuicDataStream
*(QuicStreamId id
));
494 MOCK_METHOD0(CreateOutgoingDynamicStream
, QuicDataStream
*());
496 QuicCryptoClientStream
* GetCryptoStream() override
;
499 scoped_ptr
<QuicCryptoClientStream
> crypto_stream_
;
501 DISALLOW_COPY_AND_ASSIGN(TestQuicSpdyClientSession
);
504 class MockPacketWriter
: public QuicPacketWriter
{
507 ~MockPacketWriter() override
;
509 MOCK_METHOD4(WritePacket
,
510 WriteResult(const char* buffer
,
512 const IPAddressNumber
& self_address
,
513 const IPEndPoint
& peer_address
));
514 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered
, bool());
515 MOCK_CONST_METHOD0(IsWriteBlocked
, bool());
516 MOCK_METHOD0(SetWritable
, void());
519 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter
);
522 class MockSendAlgorithm
: public SendAlgorithmInterface
{
525 ~MockSendAlgorithm() override
;
527 MOCK_METHOD2(SetFromConfig
,
528 void(const QuicConfig
& config
,
529 Perspective perspective
));
530 MOCK_METHOD1(SetNumEmulatedConnections
, void(int num_connections
));
531 MOCK_METHOD1(SetMaxCongestionWindow
,
532 void(QuicByteCount max_congestion_window
));
533 MOCK_METHOD4(OnCongestionEvent
, void(bool rtt_updated
,
534 QuicByteCount bytes_in_flight
,
535 const CongestionVector
& acked_packets
,
536 const CongestionVector
& lost_packets
));
537 MOCK_METHOD5(OnPacketSent
,
538 bool(QuicTime
, QuicByteCount
, QuicPacketSequenceNumber
,
539 QuicByteCount
, HasRetransmittableData
));
540 MOCK_METHOD1(OnRetransmissionTimeout
, void(bool));
541 MOCK_METHOD0(RevertRetransmissionTimeout
, void());
542 MOCK_CONST_METHOD3(TimeUntilSend
,
543 QuicTime::Delta(QuicTime now
,
544 QuicByteCount bytes_in_flight
,
545 HasRetransmittableData
));
546 MOCK_CONST_METHOD0(PacingRate
, QuicBandwidth(void));
547 MOCK_CONST_METHOD0(BandwidthEstimate
, QuicBandwidth(void));
548 MOCK_CONST_METHOD0(HasReliableBandwidthEstimate
, bool());
549 MOCK_METHOD1(OnRttUpdated
, void(QuicPacketSequenceNumber
));
550 MOCK_CONST_METHOD0(RetransmissionDelay
, QuicTime::Delta(void));
551 MOCK_CONST_METHOD0(GetCongestionWindow
, QuicByteCount());
552 MOCK_CONST_METHOD0(InSlowStart
, bool());
553 MOCK_CONST_METHOD0(InRecovery
, bool());
554 MOCK_CONST_METHOD0(GetSlowStartThreshold
, QuicByteCount());
555 MOCK_CONST_METHOD0(GetCongestionControlType
, CongestionControlType());
556 MOCK_METHOD2(ResumeConnectionState
,
557 void(const CachedNetworkParameters
&, bool));
560 DISALLOW_COPY_AND_ASSIGN(MockSendAlgorithm
);
563 class MockLossAlgorithm
: public LossDetectionInterface
{
566 ~MockLossAlgorithm() override
;
568 MOCK_CONST_METHOD0(GetLossDetectionType
, LossDetectionType());
569 MOCK_METHOD4(DetectLostPackets
,
570 SequenceNumberSet(const QuicUnackedPacketMap
& unacked_packets
,
571 const QuicTime
& time
,
572 QuicPacketSequenceNumber largest_observed
,
573 const RttStats
& rtt_stats
));
574 MOCK_CONST_METHOD0(GetLossTimeout
, QuicTime());
577 DISALLOW_COPY_AND_ASSIGN(MockLossAlgorithm
);
580 class TestEntropyCalculator
:
581 public QuicReceivedEntropyHashCalculatorInterface
{
583 TestEntropyCalculator();
584 ~TestEntropyCalculator() override
;
586 QuicPacketEntropyHash
EntropyHash(
587 QuicPacketSequenceNumber sequence_number
) const override
;
590 DISALLOW_COPY_AND_ASSIGN(TestEntropyCalculator
);
593 class MockEntropyCalculator
: public TestEntropyCalculator
{
595 MockEntropyCalculator();
596 ~MockEntropyCalculator() override
;
600 QuicPacketEntropyHash(QuicPacketSequenceNumber sequence_number
));
603 DISALLOW_COPY_AND_ASSIGN(MockEntropyCalculator
);
606 class MockAckNotifierDelegate
: public QuicAckNotifier::DelegateInterface
{
608 MockAckNotifierDelegate();
610 MOCK_METHOD3(OnAckNotification
,
611 void(int num_retransmitted_packets
,
612 int num_retransmitted_bytes
,
613 QuicTime::Delta delta_largest_observed
));
616 // Object is ref counted.
617 ~MockAckNotifierDelegate() override
;
620 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate
);
623 class MockNetworkChangeVisitor
:
624 public QuicSentPacketManager::NetworkChangeVisitor
{
626 MockNetworkChangeVisitor();
627 ~MockNetworkChangeVisitor() override
;
629 MOCK_METHOD0(OnCongestionWindowChange
, void());
630 MOCK_METHOD0(OnRttChange
, void());
633 DISALLOW_COPY_AND_ASSIGN(MockNetworkChangeVisitor
);
636 // Creates per-connection packet writers that register themselves with the
637 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
638 // be routed to the appropriate QuicConnection.
639 class TestWriterFactory
: public tools::QuicDispatcher::PacketWriterFactory
{
642 ~TestWriterFactory() override
;
644 QuicPacketWriter
* Create(QuicPacketWriter
* writer
,
645 QuicConnection
* connection
) override
;
647 // Calls OnPacketSent on the last QuicConnection to write through one of the
648 // packet writers created by this factory.
649 void OnPacketSent(WriteResult result
);
652 class PerConnectionPacketWriter
653 : public tools::QuicPerConnectionPacketWriter
{
655 PerConnectionPacketWriter(TestWriterFactory
* factory
,
656 QuicPacketWriter
* writer
,
657 QuicConnection
* connection
);
658 ~PerConnectionPacketWriter() override
;
660 WriteResult
WritePacket(const char* buffer
,
662 const IPAddressNumber
& self_address
,
663 const IPEndPoint
& peer_address
) override
;
666 TestWriterFactory
* factory_
;
669 // If an asynchronous write is happening and |writer| gets deleted, this
670 // clears the pointer to it to prevent use-after-free.
671 void Unregister(PerConnectionPacketWriter
* writer
);
673 PerConnectionPacketWriter
* current_writer_
;
676 class MockQuicConnectionDebugVisitor
: public QuicConnectionDebugVisitor
{
678 MockQuicConnectionDebugVisitor();
679 ~MockQuicConnectionDebugVisitor() override
;
681 MOCK_METHOD1(OnFrameAddedToPacket
, void(const QuicFrame
&));
683 MOCK_METHOD6(OnPacketSent
,
684 void(const SerializedPacket
&,
685 QuicPacketSequenceNumber
,
688 const QuicEncryptedPacket
&,
691 MOCK_METHOD3(OnPacketReceived
,
692 void(const IPEndPoint
&,
694 const QuicEncryptedPacket
&));
696 MOCK_METHOD1(OnIncorrectConnectionId
, void(QuicConnectionId
));
698 MOCK_METHOD1(OnProtocolVersionMismatch
, void(QuicVersion
));
700 MOCK_METHOD1(OnPacketHeader
, void(const QuicPacketHeader
& header
));
702 MOCK_METHOD1(OnStreamFrame
, void(const QuicStreamFrame
&));
704 MOCK_METHOD1(OnAckFrame
, void(const QuicAckFrame
& frame
));
706 MOCK_METHOD1(OnStopWaitingFrame
, void(const QuicStopWaitingFrame
&));
708 MOCK_METHOD1(OnRstStreamFrame
, void(const QuicRstStreamFrame
&));
710 MOCK_METHOD1(OnConnectionCloseFrame
, void(const QuicConnectionCloseFrame
&));
712 MOCK_METHOD1(OnPublicResetPacket
, void(const QuicPublicResetPacket
&));
714 MOCK_METHOD1(OnVersionNegotiationPacket
,
715 void(const QuicVersionNegotiationPacket
&));
717 MOCK_METHOD2(OnRevivedPacket
,
718 void(const QuicPacketHeader
&, StringPiece payload
));
721 // Creates a client session for testing.
723 // server_id: The server id associated with this stream.
724 // supports_stateless_rejects: Does this client support stateless rejects.
725 // connection_start_time: The time to set for the connection clock.
726 // Needed for strike-register nonce verification. The client
727 // connection_start_time should be synchronized witht the server
728 // start time, otherwise nonce verification will fail.
729 // crypto_client_config: Pointer to the crypto client config.
730 // client_connection: Pointer reference for newly created
731 // connection. This object will be owned by the
733 // client_session: Pointer reference for the newly created client
734 // session. The new object will be owned by the caller.
735 void CreateClientSessionForTest(QuicServerId server_id
,
736 bool supports_stateless_rejects
,
737 QuicTime::Delta connection_start_time
,
738 QuicCryptoClientConfig
* crypto_client_config
,
739 PacketSavingConnection
** client_connection
,
740 TestQuicSpdyClientSession
** client_session
);
742 // Creates a server session for testing.
744 // server_id: The server id associated with this stream.
745 // connection_start_time: The time to set for the connection clock.
746 // Needed for strike-register nonce verification. The server
747 // connection_start_time should be synchronized witht the client
748 // start time, otherwise nonce verification will fail.
749 // crypto_server_config: Pointer to the crypto server config.
750 // server_connection: Pointer reference for newly created
751 // connection. This object will be owned by the
753 // server_session: Pointer reference for the newly created server
754 // session. The new object will be owned by the caller.
755 void CreateServerSessionForTest(QuicServerId server_id
,
756 QuicTime::Delta connection_start_time
,
757 QuicCryptoServerConfig
* crypto_server_config
,
758 PacketSavingConnection
** server_connection
,
759 TestQuicSpdyServerSession
** server_session
);
764 #endif // NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_