Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / test_tools / quic_test_utils.h
blobe2b7c6243f2353e98faa018ce292f39b16e003a9
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.
4 //
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_
10 #include <string>
11 #include <vector>
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"
32 namespace net {
34 namespace test {
36 static const QuicConnectionId kTestConnectionId = 42;
37 static const uint16 kTestPort = 123;
38 static const uint32 kInitialStreamFlowControlWindowForTest =
39 1024 * 1024; // 1 MB
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,
73 bool version_flag,
74 bool reset_flag,
75 QuicPacketNumber packet_number,
76 const std::string& data,
77 QuicConnectionIdLength connection_id_length,
78 QuicPacketNumberLength packet_number_length,
79 QuicVersionVector* versions);
81 // This form assumes |versions| == nullptr.
82 QuicEncryptedPacket* ConstructEncryptedPacket(
83 QuicConnectionId connection_id,
84 bool version_flag,
85 bool reset_flag,
86 QuicPacketNumber packet_number,
87 const std::string& data,
88 QuicConnectionIdLength connection_id_length,
89 QuicPacketNumberLength packet_number_length);
91 // This form assumes |connection_id_length| == PACKET_8BYTE_CONNECTION_ID,
92 // |packet_number_length| == PACKET_6BYTE_PACKET_NUMBER and
93 // |versions| == nullptr.
94 QuicEncryptedPacket* ConstructEncryptedPacket(QuicConnectionId connection_id,
95 bool version_flag,
96 bool reset_flag,
97 QuicPacketNumber packet_number,
98 const std::string& data);
100 // Create an encrypted packet for testing whose data portion erroneous.
101 // The specific way the data portion is erroneous is not specified, but
102 // it is an error that QuicFramer detects.
103 // Note that the packet is encrypted with NullEncrypter, so to decrypt the
104 // constructed packet, the framer must be set to use NullDecrypter.
105 QuicEncryptedPacket* ConstructMisFramedEncryptedPacket(
106 QuicConnectionId connection_id,
107 bool version_flag,
108 bool reset_flag,
109 QuicPacketNumber packet_number,
110 const std::string& data,
111 QuicConnectionIdLength connection_id_length,
112 QuicPacketNumberLength packet_number_length,
113 QuicVersionVector* versions);
115 void CompareCharArraysWithHexError(const std::string& description,
116 const char* actual,
117 const int actual_len,
118 const char* expected,
119 const int expected_len);
121 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes);
123 // Returns the length of a QuicPacket that is capable of holding either a
124 // stream frame or a minimal ack frame. Sets |*payload_length| to the number
125 // of bytes of stream data that will fit in such a packet.
126 size_t GetPacketLengthForOneStream(QuicVersion version,
127 bool include_version,
128 QuicConnectionIdLength connection_id_length,
129 QuicPacketNumberLength packet_number_length,
130 InFecGroup is_in_fec_group,
131 size_t* payload_length);
133 // Returns QuicConfig set to default values.
134 QuicConfig DefaultQuicConfig();
136 // Returns a QuicConfig set to default values that supports stateless rejects.
137 QuicConfig DefaultQuicConfigStatelessRejects();
139 // Returns a version vector consisting of |version|.
140 QuicVersionVector SupportedVersions(QuicVersion version);
142 // Testing convenience method to construct a QuicAckFrame with entropy_hash set
143 // to 0 and largest_observed from peer set to |largest_observed|.
144 QuicAckFrame MakeAckFrame(QuicPacketNumber largest_observed);
146 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
147 // nack ranges of width 1 packet, starting from |least_unacked|.
148 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges,
149 QuicPacketNumber least_unacked);
151 // Returns a QuicPacket that is owned by the caller, and
152 // is populated with the fields in |header| and |frames|, or is nullptr if the
153 // packet could not be created.
154 QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
155 const QuicPacketHeader& header,
156 const QuicFrames& frames);
157 // Returns a QuicPacket that is owned by the caller, and of size |packet_size|.
158 QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
159 const QuicPacketHeader& header,
160 const QuicFrames& frames,
161 size_t packet_size);
163 template<typename SaveType>
164 class ValueRestore {
165 public:
166 ValueRestore(SaveType* name, SaveType value)
167 : name_(name),
168 value_(*name) {
169 *name_ = value;
171 ~ValueRestore() {
172 *name_ = value_;
175 private:
176 SaveType* name_;
177 SaveType value_;
179 DISALLOW_COPY_AND_ASSIGN(ValueRestore);
182 // Simple random number generator used to compute random numbers suitable
183 // for pseudo-randomly dropping packets in tests. It works by computing
184 // the sha1 hash of the current seed, and using the first 64 bits as
185 // the next random number, and the next seed.
186 class SimpleRandom {
187 public:
188 SimpleRandom() : seed_(0) {}
190 // Returns a random number in the range [0, kuint64max].
191 uint64 RandUint64();
193 void set_seed(uint64 seed) { seed_ = seed; }
195 private:
196 uint64 seed_;
198 DISALLOW_COPY_AND_ASSIGN(SimpleRandom);
201 class MockFramerVisitor : public QuicFramerVisitorInterface {
202 public:
203 MockFramerVisitor();
204 ~MockFramerVisitor() override;
206 MOCK_METHOD1(OnError, void(QuicFramer* framer));
207 // The constructor sets this up to return false by default.
208 MOCK_METHOD1(OnProtocolVersionMismatch, bool(QuicVersion version));
209 MOCK_METHOD0(OnPacket, void());
210 MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket& header));
211 MOCK_METHOD1(OnVersionNegotiationPacket,
212 void(const QuicVersionNegotiationPacket& packet));
213 MOCK_METHOD0(OnRevivedPacket, void());
214 // The constructor sets this up to return true by default.
215 MOCK_METHOD1(OnUnauthenticatedHeader, bool(const QuicPacketHeader& header));
216 // The constructor sets this up to return true by default.
217 MOCK_METHOD1(OnUnauthenticatedPublicHeader, bool(
218 const QuicPacketPublicHeader& header));
219 MOCK_METHOD1(OnDecryptedPacket, void(EncryptionLevel level));
220 MOCK_METHOD1(OnPacketHeader, bool(const QuicPacketHeader& header));
221 MOCK_METHOD1(OnFecProtectedPayload, void(base::StringPiece payload));
222 MOCK_METHOD1(OnStreamFrame, bool(const QuicStreamFrame& frame));
223 MOCK_METHOD1(OnAckFrame, bool(const QuicAckFrame& frame));
224 MOCK_METHOD1(OnStopWaitingFrame, bool(const QuicStopWaitingFrame& frame));
225 MOCK_METHOD1(OnPingFrame, bool(const QuicPingFrame& frame));
226 MOCK_METHOD1(OnFecData, void(const QuicFecData& fec));
227 MOCK_METHOD1(OnRstStreamFrame, bool(const QuicRstStreamFrame& frame));
228 MOCK_METHOD1(OnConnectionCloseFrame,
229 bool(const QuicConnectionCloseFrame& frame));
230 MOCK_METHOD1(OnGoAwayFrame, bool(const QuicGoAwayFrame& frame));
231 MOCK_METHOD1(OnWindowUpdateFrame, bool(const QuicWindowUpdateFrame& frame));
232 MOCK_METHOD1(OnBlockedFrame, bool(const QuicBlockedFrame& frame));
233 MOCK_METHOD0(OnPacketComplete, void());
235 private:
236 DISALLOW_COPY_AND_ASSIGN(MockFramerVisitor);
239 class NoOpFramerVisitor : public QuicFramerVisitorInterface {
240 public:
241 NoOpFramerVisitor() {}
243 void OnError(QuicFramer* framer) override {}
244 void OnPacket() override {}
245 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {}
246 void OnVersionNegotiationPacket(
247 const QuicVersionNegotiationPacket& packet) override {}
248 void OnRevivedPacket() override {}
249 bool OnProtocolVersionMismatch(QuicVersion version) override;
250 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override;
251 bool OnUnauthenticatedPublicHeader(
252 const QuicPacketPublicHeader& header) override;
253 void OnDecryptedPacket(EncryptionLevel level) override {}
254 bool OnPacketHeader(const QuicPacketHeader& header) override;
255 void OnFecProtectedPayload(base::StringPiece payload) override {}
256 bool OnStreamFrame(const QuicStreamFrame& frame) override;
257 bool OnAckFrame(const QuicAckFrame& frame) override;
258 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override;
259 bool OnPingFrame(const QuicPingFrame& frame) override;
260 void OnFecData(const QuicFecData& fec) override {}
261 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override;
262 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override;
263 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override;
264 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
265 bool OnBlockedFrame(const QuicBlockedFrame& frame) override;
266 void OnPacketComplete() override {}
268 private:
269 DISALLOW_COPY_AND_ASSIGN(NoOpFramerVisitor);
272 class MockConnectionVisitor : public QuicConnectionVisitorInterface {
273 public:
274 MockConnectionVisitor();
275 ~MockConnectionVisitor() override;
277 MOCK_METHOD1(OnStreamFrame, void(const QuicStreamFrame& frame));
278 MOCK_METHOD1(OnWindowUpdateFrame, void(const QuicWindowUpdateFrame& frame));
279 MOCK_METHOD1(OnBlockedFrame, void(const QuicBlockedFrame& frame));
280 MOCK_METHOD1(OnRstStream, void(const QuicRstStreamFrame& frame));
281 MOCK_METHOD1(OnGoAway, void(const QuicGoAwayFrame& frame));
282 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
283 MOCK_METHOD0(OnWriteBlocked, void());
284 MOCK_METHOD0(OnCanWrite, void());
285 MOCK_METHOD1(OnCongestionWindowChange, void(QuicTime now));
286 MOCK_METHOD0(OnConnectionMigration, void());
287 MOCK_CONST_METHOD0(WillingAndAbleToWrite, bool());
288 MOCK_CONST_METHOD0(HasPendingHandshake, bool());
289 MOCK_CONST_METHOD0(HasOpenDynamicStreams, bool());
290 MOCK_METHOD1(OnSuccessfulVersionNegotiation,
291 void(const QuicVersion& version));
292 MOCK_METHOD0(OnConfigNegotiated, void());
294 private:
295 DISALLOW_COPY_AND_ASSIGN(MockConnectionVisitor);
298 class MockHelper : public QuicConnectionHelperInterface {
299 public:
300 MockHelper();
301 ~MockHelper() override;
302 const QuicClock* GetClock() const override;
303 QuicRandom* GetRandomGenerator() override;
304 QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override;
305 void AdvanceTime(QuicTime::Delta delta);
307 private:
308 MockClock clock_;
309 MockRandom random_generator_;
311 DISALLOW_COPY_AND_ASSIGN(MockHelper);
314 class NiceMockPacketWriterFactory : public QuicConnection::PacketWriterFactory {
315 public:
316 NiceMockPacketWriterFactory() {}
317 ~NiceMockPacketWriterFactory() override {}
319 QuicPacketWriter* Create(QuicConnection* /*connection*/) const override;
321 private:
322 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory);
325 class MockConnection : public QuicConnection {
326 public:
327 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
328 explicit MockConnection(Perspective perspective);
330 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
331 MockConnection(Perspective perspective, bool is_secure);
333 // Uses a MockHelper, ConnectionId of 42.
334 MockConnection(IPEndPoint address, Perspective perspective);
336 // Uses a MockHelper, and 127.0.0.1:123.
337 MockConnection(QuicConnectionId connection_id, Perspective perspective);
339 // Uses a MockHelper, and 127.0.0.1:123.
340 MockConnection(QuicConnectionId connection_id,
341 Perspective perspective,
342 bool is_secure);
344 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123.
345 MockConnection(Perspective perspective,
346 const QuicVersionVector& supported_versions);
348 MockConnection(QuicConnectionId connection_id,
349 IPEndPoint address,
350 Perspective perspective,
351 bool is_secure,
352 const QuicVersionVector& supported_versions);
354 ~MockConnection() override;
356 // If the constructor that uses a MockHelper has been used then this method
357 // will advance the time of the MockClock.
358 void AdvanceTime(QuicTime::Delta delta);
360 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address,
361 const IPEndPoint& peer_address,
362 const QuicEncryptedPacket& packet));
363 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error));
364 MOCK_METHOD2(SendConnectionCloseWithDetails,
365 void(QuicErrorCode error, const std::string& details));
366 MOCK_METHOD2(SendConnectionClosePacket,
367 void(QuicErrorCode error, const std::string& details));
368 MOCK_METHOD3(SendRstStream, void(QuicStreamId id,
369 QuicRstStreamErrorCode error,
370 QuicStreamOffset bytes_written));
371 MOCK_METHOD3(SendGoAway,
372 void(QuicErrorCode error,
373 QuicStreamId last_good_stream_id,
374 const std::string& reason));
375 MOCK_METHOD1(SendBlocked, void(QuicStreamId id));
376 MOCK_METHOD2(SendWindowUpdate, void(QuicStreamId id,
377 QuicStreamOffset byte_offset));
378 MOCK_METHOD0(OnCanWrite, void());
380 MOCK_METHOD1(OnSendConnectionState, void(const CachedNetworkParameters&));
381 MOCK_METHOD2(ResumeConnectionState,
382 void(const CachedNetworkParameters&, bool));
384 MOCK_METHOD1(OnError, void(QuicFramer* framer));
385 void QuicConnection_OnError(QuicFramer* framer) {
386 QuicConnection::OnError(framer);
389 void ReallyProcessUdpPacket(const IPEndPoint& self_address,
390 const IPEndPoint& peer_address,
391 const QuicEncryptedPacket& packet) {
392 QuicConnection::ProcessUdpPacket(self_address, peer_address, packet);
395 bool OnProtocolVersionMismatch(QuicVersion version) override {
396 return false;
399 void ReallySendGoAway(QuicErrorCode error,
400 QuicStreamId last_good_stream_id,
401 const std::string& reason) {
402 QuicConnection::SendGoAway(error, last_good_stream_id, reason);
405 private:
406 scoped_ptr<QuicConnectionHelperInterface> helper_;
408 DISALLOW_COPY_AND_ASSIGN(MockConnection);
411 class PacketSavingConnection : public MockConnection {
412 public:
413 explicit PacketSavingConnection(Perspective perspective);
415 PacketSavingConnection(Perspective perspective,
416 const QuicVersionVector& supported_versions);
418 ~PacketSavingConnection() override;
420 void SendOrQueuePacket(QueuedPacket packet) override;
422 std::vector<QuicEncryptedPacket*> encrypted_packets_;
424 private:
425 DISALLOW_COPY_AND_ASSIGN(PacketSavingConnection);
428 class MockQuicSpdySession : public QuicSpdySession {
429 public:
430 explicit MockQuicSpdySession(QuicConnection* connection);
431 ~MockQuicSpdySession() override;
433 QuicCryptoStream* GetCryptoStream() override { return crypto_stream_.get(); }
435 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
436 MOCK_METHOD1(CreateIncomingDynamicStream, QuicDataStream*(QuicStreamId id));
437 MOCK_METHOD0(CreateOutgoingDynamicStream, QuicDataStream*());
438 MOCK_METHOD6(WritevData,
439 QuicConsumedData(QuicStreamId id,
440 const QuicIOVector& data,
441 QuicStreamOffset offset,
442 bool fin,
443 FecProtection fec_protection,
444 QuicAckNotifier::DelegateInterface*));
445 MOCK_METHOD2(OnStreamHeaders, void(QuicStreamId stream_id,
446 base::StringPiece headers_data));
447 MOCK_METHOD2(OnStreamHeadersPriority, void(QuicStreamId stream_id,
448 QuicPriority priority));
449 MOCK_METHOD3(OnStreamHeadersComplete, void(QuicStreamId stream_id,
450 bool fin,
451 size_t frame_len));
452 MOCK_METHOD3(SendRstStream, void(QuicStreamId stream_id,
453 QuicRstStreamErrorCode error,
454 QuicStreamOffset bytes_written));
455 MOCK_METHOD0(IsCryptoHandshakeConfirmed, bool());
457 using QuicSession::ActivateStream;
459 private:
460 scoped_ptr<QuicCryptoStream> crypto_stream_;
462 DISALLOW_COPY_AND_ASSIGN(MockQuicSpdySession);
465 class TestQuicSpdyServerSession : public QuicSpdySession {
466 public:
467 TestQuicSpdyServerSession(QuicConnection* connection,
468 const QuicConfig& config,
469 const QuicCryptoServerConfig* crypto_config);
470 ~TestQuicSpdyServerSession() override;
472 MOCK_METHOD1(CreateIncomingDynamicStream, QuicDataStream*(QuicStreamId id));
473 MOCK_METHOD0(CreateOutgoingDynamicStream, QuicDataStream*());
475 QuicCryptoServerStream* GetCryptoStream() override;
477 private:
478 scoped_ptr<QuicCryptoServerStream> crypto_stream_;
480 DISALLOW_COPY_AND_ASSIGN(TestQuicSpdyServerSession);
483 class TestQuicSpdyClientSession : public QuicClientSessionBase {
484 public:
485 TestQuicSpdyClientSession(QuicConnection* connection,
486 const QuicConfig& config,
487 const QuicServerId& server_id,
488 QuicCryptoClientConfig* crypto_config);
489 ~TestQuicSpdyClientSession() override;
491 // QuicClientSessionBase
492 MOCK_METHOD1(OnProofValid,
493 void(const QuicCryptoClientConfig::CachedState& cached));
494 MOCK_METHOD1(OnProofVerifyDetailsAvailable,
495 void(const ProofVerifyDetails& verify_details));
497 // TestQuicSpdyClientSession
498 MOCK_METHOD1(CreateIncomingDynamicStream, QuicDataStream*(QuicStreamId id));
499 MOCK_METHOD0(CreateOutgoingDynamicStream, QuicDataStream*());
501 QuicCryptoClientStream* GetCryptoStream() override;
503 private:
504 scoped_ptr<QuicCryptoClientStream> crypto_stream_;
506 DISALLOW_COPY_AND_ASSIGN(TestQuicSpdyClientSession);
509 class MockPacketWriter : public QuicPacketWriter {
510 public:
511 MockPacketWriter();
512 ~MockPacketWriter() override;
514 MOCK_METHOD4(WritePacket,
515 WriteResult(const char* buffer,
516 size_t buf_len,
517 const IPAddressNumber& self_address,
518 const IPEndPoint& peer_address));
519 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool());
520 MOCK_CONST_METHOD0(IsWriteBlocked, bool());
521 MOCK_METHOD0(SetWritable, void());
523 private:
524 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter);
527 class MockSendAlgorithm : public SendAlgorithmInterface {
528 public:
529 MockSendAlgorithm();
530 ~MockSendAlgorithm() override;
532 MOCK_METHOD2(SetFromConfig,
533 void(const QuicConfig& config,
534 Perspective perspective));
535 MOCK_METHOD1(SetNumEmulatedConnections, void(int num_connections));
536 MOCK_METHOD1(SetMaxCongestionWindow,
537 void(QuicByteCount max_congestion_window));
538 MOCK_METHOD4(OnCongestionEvent, void(bool rtt_updated,
539 QuicByteCount bytes_in_flight,
540 const CongestionVector& acked_packets,
541 const CongestionVector& lost_packets));
542 MOCK_METHOD5(OnPacketSent,
543 bool(QuicTime,
544 QuicByteCount,
545 QuicPacketNumber,
546 QuicByteCount,
547 HasRetransmittableData));
548 MOCK_METHOD1(OnRetransmissionTimeout, void(bool));
549 MOCK_METHOD0(RevertRetransmissionTimeout, void());
550 MOCK_CONST_METHOD3(TimeUntilSend,
551 QuicTime::Delta(QuicTime now,
552 QuicByteCount bytes_in_flight,
553 HasRetransmittableData));
554 MOCK_CONST_METHOD0(PacingRate, QuicBandwidth(void));
555 MOCK_CONST_METHOD0(BandwidthEstimate, QuicBandwidth(void));
556 MOCK_CONST_METHOD0(HasReliableBandwidthEstimate, bool());
557 MOCK_METHOD1(OnRttUpdated, void(QuicPacketNumber));
558 MOCK_CONST_METHOD0(RetransmissionDelay, QuicTime::Delta(void));
559 MOCK_CONST_METHOD0(GetCongestionWindow, QuicByteCount());
560 MOCK_CONST_METHOD0(InSlowStart, bool());
561 MOCK_CONST_METHOD0(InRecovery, bool());
562 MOCK_CONST_METHOD0(GetSlowStartThreshold, QuicByteCount());
563 MOCK_CONST_METHOD0(GetCongestionControlType, CongestionControlType());
564 MOCK_METHOD2(ResumeConnectionState,
565 void(const CachedNetworkParameters&, bool));
567 private:
568 DISALLOW_COPY_AND_ASSIGN(MockSendAlgorithm);
571 class MockLossAlgorithm : public LossDetectionInterface {
572 public:
573 MockLossAlgorithm();
574 ~MockLossAlgorithm() override;
576 MOCK_CONST_METHOD0(GetLossDetectionType, LossDetectionType());
577 MOCK_METHOD4(DetectLostPackets,
578 PacketNumberSet(const QuicUnackedPacketMap& unacked_packets,
579 const QuicTime& time,
580 QuicPacketNumber largest_observed,
581 const RttStats& rtt_stats));
582 MOCK_CONST_METHOD0(GetLossTimeout, QuicTime());
584 private:
585 DISALLOW_COPY_AND_ASSIGN(MockLossAlgorithm);
588 class TestEntropyCalculator :
589 public QuicReceivedEntropyHashCalculatorInterface {
590 public:
591 TestEntropyCalculator();
592 ~TestEntropyCalculator() override;
594 QuicPacketEntropyHash EntropyHash(
595 QuicPacketNumber packet_number) const override;
597 private:
598 DISALLOW_COPY_AND_ASSIGN(TestEntropyCalculator);
601 class MockEntropyCalculator : public TestEntropyCalculator {
602 public:
603 MockEntropyCalculator();
604 ~MockEntropyCalculator() override;
606 MOCK_CONST_METHOD1(EntropyHash,
607 QuicPacketEntropyHash(QuicPacketNumber packet_number));
609 private:
610 DISALLOW_COPY_AND_ASSIGN(MockEntropyCalculator);
613 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface {
614 public:
615 MockAckNotifierDelegate();
617 MOCK_METHOD3(OnAckNotification,
618 void(int num_retransmitted_packets,
619 int num_retransmitted_bytes,
620 QuicTime::Delta delta_largest_observed));
622 protected:
623 // Object is ref counted.
624 ~MockAckNotifierDelegate() override;
626 private:
627 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate);
630 class MockNetworkChangeVisitor :
631 public QuicSentPacketManager::NetworkChangeVisitor {
632 public:
633 MockNetworkChangeVisitor();
634 ~MockNetworkChangeVisitor() override;
636 MOCK_METHOD0(OnCongestionWindowChange, void());
637 MOCK_METHOD0(OnRttChange, void());
639 private:
640 DISALLOW_COPY_AND_ASSIGN(MockNetworkChangeVisitor);
643 // Creates per-connection packet writers that register themselves with the
644 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
645 // be routed to the appropriate QuicConnection.
646 class TestWriterFactory : public tools::QuicDispatcher::PacketWriterFactory {
647 public:
648 TestWriterFactory();
649 ~TestWriterFactory() override;
651 QuicPacketWriter* Create(QuicPacketWriter* writer,
652 QuicConnection* connection) override;
654 // Calls OnPacketSent on the last QuicConnection to write through one of the
655 // packet writers created by this factory.
656 void OnPacketSent(WriteResult result);
658 private:
659 class PerConnectionPacketWriter
660 : public tools::QuicPerConnectionPacketWriter {
661 public:
662 PerConnectionPacketWriter(TestWriterFactory* factory,
663 QuicPacketWriter* writer,
664 QuicConnection* connection);
665 ~PerConnectionPacketWriter() override;
667 WriteResult WritePacket(const char* buffer,
668 size_t buf_len,
669 const IPAddressNumber& self_address,
670 const IPEndPoint& peer_address) override;
672 private:
673 TestWriterFactory* factory_;
676 // If an asynchronous write is happening and |writer| gets deleted, this
677 // clears the pointer to it to prevent use-after-free.
678 void Unregister(PerConnectionPacketWriter* writer);
680 PerConnectionPacketWriter* current_writer_;
683 class MockQuicConnectionDebugVisitor : public QuicConnectionDebugVisitor {
684 public:
685 MockQuicConnectionDebugVisitor();
686 ~MockQuicConnectionDebugVisitor() override;
688 MOCK_METHOD1(OnFrameAddedToPacket, void(const QuicFrame&));
690 MOCK_METHOD6(OnPacketSent,
691 void(const SerializedPacket&,
692 QuicPacketNumber,
693 EncryptionLevel,
694 TransmissionType,
695 const QuicEncryptedPacket&,
696 QuicTime));
698 MOCK_METHOD3(OnPacketReceived,
699 void(const IPEndPoint&,
700 const IPEndPoint&,
701 const QuicEncryptedPacket&));
703 MOCK_METHOD1(OnIncorrectConnectionId, void(QuicConnectionId));
705 MOCK_METHOD1(OnProtocolVersionMismatch, void(QuicVersion));
707 MOCK_METHOD1(OnPacketHeader, void(const QuicPacketHeader& header));
709 MOCK_METHOD1(OnStreamFrame, void(const QuicStreamFrame&));
711 MOCK_METHOD1(OnAckFrame, void(const QuicAckFrame& frame));
713 MOCK_METHOD1(OnStopWaitingFrame, void(const QuicStopWaitingFrame&));
715 MOCK_METHOD1(OnRstStreamFrame, void(const QuicRstStreamFrame&));
717 MOCK_METHOD1(OnConnectionCloseFrame, void(const QuicConnectionCloseFrame&));
719 MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket&));
721 MOCK_METHOD1(OnVersionNegotiationPacket,
722 void(const QuicVersionNegotiationPacket&));
724 MOCK_METHOD2(OnRevivedPacket,
725 void(const QuicPacketHeader&, StringPiece payload));
728 // Creates a client session for testing.
730 // server_id: The server id associated with this stream.
731 // supports_stateless_rejects: Does this client support stateless rejects.
732 // connection_start_time: The time to set for the connection clock.
733 // Needed for strike-register nonce verification. The client
734 // connection_start_time should be synchronized witht the server
735 // start time, otherwise nonce verification will fail.
736 // crypto_client_config: Pointer to the crypto client config.
737 // client_connection: Pointer reference for newly created
738 // connection. This object will be owned by the
739 // client_session.
740 // client_session: Pointer reference for the newly created client
741 // session. The new object will be owned by the caller.
742 void CreateClientSessionForTest(QuicServerId server_id,
743 bool supports_stateless_rejects,
744 QuicTime::Delta connection_start_time,
745 QuicCryptoClientConfig* crypto_client_config,
746 PacketSavingConnection** client_connection,
747 TestQuicSpdyClientSession** client_session);
749 // Creates a server session for testing.
751 // server_id: The server id associated with this stream.
752 // connection_start_time: The time to set for the connection clock.
753 // Needed for strike-register nonce verification. The server
754 // connection_start_time should be synchronized witht the client
755 // start time, otherwise nonce verification will fail.
756 // crypto_server_config: Pointer to the crypto server config.
757 // server_connection: Pointer reference for newly created
758 // connection. This object will be owned by the
759 // server_session.
760 // server_session: Pointer reference for the newly created server
761 // session. The new object will be owned by the caller.
762 void CreateServerSessionForTest(QuicServerId server_id,
763 QuicTime::Delta connection_start_time,
764 QuicCryptoServerConfig* crypto_server_config,
765 PacketSavingConnection** server_connection,
766 TestQuicSpdyServerSession** server_session);
768 } // namespace test
769 } // namespace net
771 #endif // NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_