Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / test_tools / quic_test_utils.h
blob0f084df4125b71f86e2347555c9df99e7e07851e
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 "testing/gmock/include/gmock/gmock.h"
31 namespace net {
33 namespace test {
35 static const QuicConnectionId kTestConnectionId = 42;
36 static const uint16 kTestPort = 123;
37 static const uint32 kInitialStreamFlowControlWindowForTest =
38 1024 * 1024; // 1 MB
39 static const uint32 kInitialSessionFlowControlWindowForTest =
40 1536 * 1024; // 1.5 MB
41 // Data stream IDs start at 5: the crypto stream is 1, headers stream is 3.
42 static const QuicStreamId kClientDataStreamId1 = 5;
43 static const QuicStreamId kClientDataStreamId2 = 7;
44 static const QuicStreamId kClientDataStreamId3 = 9;
46 // Returns the test peer IP address.
47 IPAddressNumber TestPeerIPAddress();
49 // Upper limit on versions we support.
50 QuicVersion QuicVersionMax();
52 // Lower limit on versions we support.
53 QuicVersion QuicVersionMin();
55 // Returns an address for 127.0.0.1.
56 IPAddressNumber Loopback4();
58 // Returns an address for ::1.
59 IPAddressNumber Loopback6();
61 void GenerateBody(std::string* body, int length);
63 // Create an encrypted packet for testing.
64 // If versions == nullptr, uses &QuicSupportedVersions().
65 QuicEncryptedPacket* ConstructEncryptedPacket(
66 QuicConnectionId connection_id,
67 bool version_flag,
68 bool reset_flag,
69 QuicPacketSequenceNumber sequence_number,
70 const std::string& data,
71 QuicConnectionIdLength connection_id_length,
72 QuicSequenceNumberLength sequence_number_length,
73 QuicVersionVector* versions);
75 // This form assumes |versions| == nullptr.
76 QuicEncryptedPacket* ConstructEncryptedPacket(
77 QuicConnectionId connection_id,
78 bool version_flag,
79 bool reset_flag,
80 QuicPacketSequenceNumber sequence_number,
81 const std::string& data,
82 QuicConnectionIdLength connection_id_length,
83 QuicSequenceNumberLength sequence_number_length);
85 // This form assumes |connection_id_length| == PACKET_8BYTE_CONNECTION_ID,
86 // |sequence_number_length| == PACKET_6BYTE_SEQUENCE_NUMBER and
87 // |versions| == nullptr.
88 QuicEncryptedPacket* ConstructEncryptedPacket(
89 QuicConnectionId connection_id,
90 bool version_flag,
91 bool reset_flag,
92 QuicPacketSequenceNumber sequence_number,
93 const std::string& data);
95 // Create an encrypted packet for testing whose data portion contains
96 // a framing error.
97 QuicEncryptedPacket* ConstructMisFramedEncryptedPacket(
98 QuicConnectionId connection_id,
99 bool version_flag,
100 bool reset_flag,
101 QuicPacketSequenceNumber sequence_number,
102 const std::string& data,
103 QuicConnectionIdLength connection_id_length,
104 QuicSequenceNumberLength sequence_number_length,
105 QuicVersionVector* versions);
107 void CompareCharArraysWithHexError(const std::string& description,
108 const char* actual,
109 const int actual_len,
110 const char* expected,
111 const int expected_len);
113 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes);
115 // Returns the length of a QuicPacket that is capable of holding either a
116 // stream frame or a minimal ack frame. Sets |*payload_length| to the number
117 // of bytes of stream data that will fit in such a packet.
118 size_t GetPacketLengthForOneStream(
119 QuicVersion version,
120 bool include_version,
121 QuicConnectionIdLength connection_id_length,
122 QuicSequenceNumberLength sequence_number_length,
123 InFecGroup is_in_fec_group,
124 size_t* payload_length);
126 // Returns QuicConfig set to default values.
127 QuicConfig DefaultQuicConfig();
129 // Returns a version vector consisting of |version|.
130 QuicVersionVector SupportedVersions(QuicVersion version);
132 // Testing convenience method to construct a QuicAckFrame with entropy_hash set
133 // to 0 and largest_observed from peer set to |largest_observed|.
134 QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed);
136 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
137 // nack ranges of width 1 packet, starting from |least_unacked|.
138 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges,
139 QuicPacketSequenceNumber least_unacked);
141 // Returns a QuicPacket that is owned by the caller, and
142 // is populated with the fields in |header| and |frames|, or is nullptr if the
143 // packet could not be created.
144 QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
145 const QuicPacketHeader& header,
146 const QuicFrames& frames);
147 // Returns a QuicPacket that is owned by the caller, and of size |packet_size|.
148 QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
149 const QuicPacketHeader& header,
150 const QuicFrames& frames,
151 size_t packet_size);
153 template<typename SaveType>
154 class ValueRestore {
155 public:
156 ValueRestore(SaveType* name, SaveType value)
157 : name_(name),
158 value_(*name) {
159 *name_ = value;
161 ~ValueRestore() {
162 *name_ = value_;
165 private:
166 SaveType* name_;
167 SaveType value_;
169 DISALLOW_COPY_AND_ASSIGN(ValueRestore);
172 // Simple random number generator used to compute random numbers suitable
173 // for pseudo-randomly dropping packets in tests. It works by computing
174 // the sha1 hash of the current seed, and using the first 64 bits as
175 // the next random number, and the next seed.
176 class SimpleRandom {
177 public:
178 SimpleRandom() : seed_(0) {}
180 // Returns a random number in the range [0, kuint64max].
181 uint64 RandUint64();
183 void set_seed(uint64 seed) { seed_ = seed; }
185 private:
186 uint64 seed_;
188 DISALLOW_COPY_AND_ASSIGN(SimpleRandom);
191 class MockFramerVisitor : public QuicFramerVisitorInterface {
192 public:
193 MockFramerVisitor();
194 ~MockFramerVisitor() override;
196 MOCK_METHOD1(OnError, void(QuicFramer* framer));
197 // The constructor sets this up to return false by default.
198 MOCK_METHOD1(OnProtocolVersionMismatch, bool(QuicVersion version));
199 MOCK_METHOD0(OnPacket, void());
200 MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket& header));
201 MOCK_METHOD1(OnVersionNegotiationPacket,
202 void(const QuicVersionNegotiationPacket& packet));
203 MOCK_METHOD0(OnRevivedPacket, void());
204 // The constructor sets this up to return true by default.
205 MOCK_METHOD1(OnUnauthenticatedHeader, bool(const QuicPacketHeader& header));
206 // The constructor sets this up to return true by default.
207 MOCK_METHOD1(OnUnauthenticatedPublicHeader, bool(
208 const QuicPacketPublicHeader& header));
209 MOCK_METHOD1(OnDecryptedPacket, void(EncryptionLevel level));
210 MOCK_METHOD1(OnPacketHeader, bool(const QuicPacketHeader& header));
211 MOCK_METHOD1(OnFecProtectedPayload, void(base::StringPiece payload));
212 MOCK_METHOD1(OnStreamFrame, bool(const QuicStreamFrame& frame));
213 MOCK_METHOD1(OnAckFrame, bool(const QuicAckFrame& frame));
214 MOCK_METHOD1(OnStopWaitingFrame, bool(const QuicStopWaitingFrame& frame));
215 MOCK_METHOD1(OnPingFrame, bool(const QuicPingFrame& frame));
216 MOCK_METHOD1(OnFecData, void(const QuicFecData& fec));
217 MOCK_METHOD1(OnRstStreamFrame, bool(const QuicRstStreamFrame& frame));
218 MOCK_METHOD1(OnConnectionCloseFrame,
219 bool(const QuicConnectionCloseFrame& frame));
220 MOCK_METHOD1(OnGoAwayFrame, bool(const QuicGoAwayFrame& frame));
221 MOCK_METHOD1(OnWindowUpdateFrame, bool(const QuicWindowUpdateFrame& frame));
222 MOCK_METHOD1(OnBlockedFrame, bool(const QuicBlockedFrame& frame));
223 MOCK_METHOD0(OnPacketComplete, void());
225 private:
226 DISALLOW_COPY_AND_ASSIGN(MockFramerVisitor);
229 class NoOpFramerVisitor : public QuicFramerVisitorInterface {
230 public:
231 NoOpFramerVisitor() {}
233 void OnError(QuicFramer* framer) override {}
234 void OnPacket() override {}
235 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {}
236 void OnVersionNegotiationPacket(
237 const QuicVersionNegotiationPacket& packet) override {}
238 void OnRevivedPacket() override {}
239 bool OnProtocolVersionMismatch(QuicVersion version) override;
240 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override;
241 bool OnUnauthenticatedPublicHeader(
242 const QuicPacketPublicHeader& header) override;
243 void OnDecryptedPacket(EncryptionLevel level) override {}
244 bool OnPacketHeader(const QuicPacketHeader& header) override;
245 void OnFecProtectedPayload(base::StringPiece payload) override {}
246 bool OnStreamFrame(const QuicStreamFrame& frame) override;
247 bool OnAckFrame(const QuicAckFrame& frame) override;
248 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override;
249 bool OnPingFrame(const QuicPingFrame& frame) override;
250 void OnFecData(const QuicFecData& fec) override {}
251 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override;
252 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override;
253 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override;
254 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
255 bool OnBlockedFrame(const QuicBlockedFrame& frame) override;
256 void OnPacketComplete() override {}
258 private:
259 DISALLOW_COPY_AND_ASSIGN(NoOpFramerVisitor);
262 class MockConnectionVisitor : public QuicConnectionVisitorInterface {
263 public:
264 MockConnectionVisitor();
265 ~MockConnectionVisitor() override;
267 MOCK_METHOD1(OnStreamFrames, void(const std::vector<QuicStreamFrame>& frame));
268 MOCK_METHOD1(OnWindowUpdateFrames,
269 void(const std::vector<QuicWindowUpdateFrame>& frame));
270 MOCK_METHOD1(OnBlockedFrames,
271 void(const std::vector<QuicBlockedFrame>& frame));
272 MOCK_METHOD1(OnRstStream, void(const QuicRstStreamFrame& frame));
273 MOCK_METHOD1(OnGoAway, void(const QuicGoAwayFrame& frame));
274 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
275 MOCK_METHOD0(OnWriteBlocked, void());
276 MOCK_METHOD0(OnCanWrite, void());
277 MOCK_METHOD1(OnCongestionWindowChange, void(QuicTime now));
278 MOCK_CONST_METHOD0(WillingAndAbleToWrite, bool());
279 MOCK_CONST_METHOD0(HasPendingHandshake, bool());
280 MOCK_CONST_METHOD0(HasOpenDataStreams, bool());
281 MOCK_METHOD1(OnSuccessfulVersionNegotiation,
282 void(const QuicVersion& version));
283 MOCK_METHOD0(OnConfigNegotiated, void());
285 private:
286 DISALLOW_COPY_AND_ASSIGN(MockConnectionVisitor);
289 class MockHelper : public QuicConnectionHelperInterface {
290 public:
291 MockHelper();
292 ~MockHelper() override;
293 const QuicClock* GetClock() const override;
294 QuicRandom* GetRandomGenerator() override;
295 QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override;
296 void AdvanceTime(QuicTime::Delta delta);
298 private:
299 MockClock clock_;
300 MockRandom random_generator_;
302 DISALLOW_COPY_AND_ASSIGN(MockHelper);
305 class NiceMockPacketWriterFactory : public QuicConnection::PacketWriterFactory {
306 public:
307 NiceMockPacketWriterFactory() {}
308 ~NiceMockPacketWriterFactory() override {}
310 QuicPacketWriter* Create(QuicConnection* /*connection*/) const override;
312 private:
313 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory);
316 class MockConnection : public QuicConnection {
317 public:
318 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
319 explicit MockConnection(Perspective perspective);
321 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
322 MockConnection(Perspective perspective, bool is_secure);
324 // Uses a MockHelper, ConnectionId of 42.
325 MockConnection(IPEndPoint address, Perspective perspective);
327 // Uses a MockHelper, and 127.0.0.1:123.
328 MockConnection(QuicConnectionId connection_id, Perspective perspective);
330 // Uses a MockHelper, and 127.0.0.1:123.
331 MockConnection(QuicConnectionId connection_id,
332 Perspective perspective,
333 bool is_secure);
335 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123.
336 MockConnection(Perspective perspective,
337 const QuicVersionVector& supported_versions);
339 MockConnection(QuicConnectionId connection_id,
340 IPEndPoint address,
341 Perspective perspective,
342 bool is_secure,
343 const QuicVersionVector& supported_versions);
345 ~MockConnection() override;
347 // If the constructor that uses a MockHelper has been used then this method
348 // will advance the time of the MockClock.
349 void AdvanceTime(QuicTime::Delta delta);
351 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address,
352 const IPEndPoint& peer_address,
353 const QuicEncryptedPacket& packet));
354 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error));
355 MOCK_METHOD2(SendConnectionCloseWithDetails,
356 void(QuicErrorCode error, const std::string& details));
357 MOCK_METHOD2(SendConnectionClosePacket,
358 void(QuicErrorCode error, const std::string& details));
359 MOCK_METHOD3(SendRstStream, void(QuicStreamId id,
360 QuicRstStreamErrorCode error,
361 QuicStreamOffset bytes_written));
362 MOCK_METHOD3(SendGoAway,
363 void(QuicErrorCode error,
364 QuicStreamId last_good_stream_id,
365 const std::string& reason));
366 MOCK_METHOD1(SendBlocked, void(QuicStreamId id));
367 MOCK_METHOD2(SendWindowUpdate, void(QuicStreamId id,
368 QuicStreamOffset byte_offset));
369 MOCK_METHOD0(OnCanWrite, void());
371 MOCK_METHOD1(OnSendConnectionState, void(const CachedNetworkParameters&));
372 MOCK_METHOD2(ResumeConnectionState,
373 bool(const CachedNetworkParameters&, bool));
375 MOCK_METHOD1(OnError, void(QuicFramer* framer));
376 void QuicConnection_OnError(QuicFramer* framer) {
377 QuicConnection::OnError(framer);
380 void ReallyProcessUdpPacket(const IPEndPoint& self_address,
381 const IPEndPoint& peer_address,
382 const QuicEncryptedPacket& packet) {
383 QuicConnection::ProcessUdpPacket(self_address, peer_address, packet);
386 bool OnProtocolVersionMismatch(QuicVersion version) override {
387 return false;
390 private:
391 scoped_ptr<QuicConnectionHelperInterface> helper_;
393 DISALLOW_COPY_AND_ASSIGN(MockConnection);
396 class PacketSavingConnection : public MockConnection {
397 public:
398 explicit PacketSavingConnection(Perspective perspective);
400 PacketSavingConnection(Perspective perspective,
401 const QuicVersionVector& supported_versions);
403 ~PacketSavingConnection() override;
405 void SendOrQueuePacket(QueuedPacket packet) override;
407 std::vector<QuicEncryptedPacket*> encrypted_packets_;
409 private:
410 DISALLOW_COPY_AND_ASSIGN(PacketSavingConnection);
413 class MockSession : public QuicSession {
414 public:
415 explicit MockSession(QuicConnection* connection);
416 ~MockSession() override;
417 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
418 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
419 MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*());
420 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
421 MOCK_METHOD6(WritevData,
422 QuicConsumedData(QuicStreamId id,
423 const IOVector& data,
424 QuicStreamOffset offset,
425 bool fin,
426 FecProtection fec_protection,
427 QuicAckNotifier::DelegateInterface*));
428 MOCK_METHOD2(OnStreamHeaders, void(QuicStreamId stream_id,
429 base::StringPiece headers_data));
430 MOCK_METHOD2(OnStreamHeadersPriority, void(QuicStreamId stream_id,
431 QuicPriority priority));
432 MOCK_METHOD3(OnStreamHeadersComplete, void(QuicStreamId stream_id,
433 bool fin,
434 size_t frame_len));
435 MOCK_METHOD3(SendRstStream, void(QuicStreamId stream_id,
436 QuicRstStreamErrorCode error,
437 QuicStreamOffset bytes_written));
438 MOCK_METHOD0(IsCryptoHandshakeConfirmed, bool());
440 using QuicSession::ActivateStream;
442 private:
443 DISALLOW_COPY_AND_ASSIGN(MockSession);
446 class TestSession : public QuicSession {
447 public:
448 TestSession(QuicConnection* connection, const QuicConfig& config);
449 ~TestSession() override;
451 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
452 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
454 void SetCryptoStream(QuicCryptoStream* stream);
456 QuicCryptoStream* GetCryptoStream() override;
458 private:
459 QuicCryptoStream* crypto_stream_;
461 DISALLOW_COPY_AND_ASSIGN(TestSession);
464 class TestClientSession : public QuicClientSessionBase {
465 public:
466 TestClientSession(QuicConnection* connection, const QuicConfig& config);
467 ~TestClientSession() override;
469 // QuicClientSessionBase
470 MOCK_METHOD1(OnProofValid,
471 void(const QuicCryptoClientConfig::CachedState& cached));
472 MOCK_METHOD1(OnProofVerifyDetailsAvailable,
473 void(const ProofVerifyDetails& verify_details));
475 // TestClientSession
476 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
477 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
479 void SetCryptoStream(QuicCryptoStream* stream);
481 QuicCryptoStream* GetCryptoStream() override;
483 private:
484 QuicCryptoStream* crypto_stream_;
486 DISALLOW_COPY_AND_ASSIGN(TestClientSession);
489 class MockPacketWriter : public QuicPacketWriter {
490 public:
491 MockPacketWriter();
492 ~MockPacketWriter() override;
494 MOCK_METHOD4(WritePacket,
495 WriteResult(const char* buffer,
496 size_t buf_len,
497 const IPAddressNumber& self_address,
498 const IPEndPoint& peer_address));
499 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool());
500 MOCK_CONST_METHOD0(IsWriteBlocked, bool());
501 MOCK_METHOD0(SetWritable, void());
503 private:
504 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter);
507 class MockSendAlgorithm : public SendAlgorithmInterface {
508 public:
509 MockSendAlgorithm();
510 ~MockSendAlgorithm() override;
512 MOCK_METHOD2(SetFromConfig,
513 void(const QuicConfig& config,
514 Perspective perspective));
515 MOCK_METHOD1(SetNumEmulatedConnections, void(int num_connections));
516 MOCK_METHOD1(SetMaxCongestionWindow,
517 void(QuicByteCount max_congestion_window));
518 MOCK_METHOD4(OnCongestionEvent, void(bool rtt_updated,
519 QuicByteCount bytes_in_flight,
520 const CongestionVector& acked_packets,
521 const CongestionVector& lost_packets));
522 MOCK_METHOD5(OnPacketSent,
523 bool(QuicTime, QuicByteCount, QuicPacketSequenceNumber,
524 QuicByteCount, HasRetransmittableData));
525 MOCK_METHOD1(OnRetransmissionTimeout, void(bool));
526 MOCK_METHOD0(RevertRetransmissionTimeout, void());
527 MOCK_CONST_METHOD3(TimeUntilSend,
528 QuicTime::Delta(QuicTime now,
529 QuicByteCount bytes_in_flight,
530 HasRetransmittableData));
531 MOCK_CONST_METHOD0(PacingRate, QuicBandwidth(void));
532 MOCK_CONST_METHOD0(BandwidthEstimate, QuicBandwidth(void));
533 MOCK_CONST_METHOD0(HasReliableBandwidthEstimate, bool());
534 MOCK_METHOD1(OnRttUpdated, void(QuicPacketSequenceNumber));
535 MOCK_CONST_METHOD0(RetransmissionDelay, QuicTime::Delta(void));
536 MOCK_CONST_METHOD0(GetCongestionWindow, QuicByteCount());
537 MOCK_CONST_METHOD0(InSlowStart, bool());
538 MOCK_CONST_METHOD0(InRecovery, bool());
539 MOCK_CONST_METHOD0(GetSlowStartThreshold, QuicByteCount());
540 MOCK_CONST_METHOD0(GetCongestionControlType, CongestionControlType());
541 MOCK_METHOD2(ResumeConnectionState,
542 bool(const CachedNetworkParameters&, bool));
544 private:
545 DISALLOW_COPY_AND_ASSIGN(MockSendAlgorithm);
548 class MockLossAlgorithm : public LossDetectionInterface {
549 public:
550 MockLossAlgorithm();
551 ~MockLossAlgorithm() override;
553 MOCK_CONST_METHOD0(GetLossDetectionType, LossDetectionType());
554 MOCK_METHOD4(DetectLostPackets,
555 SequenceNumberSet(const QuicUnackedPacketMap& unacked_packets,
556 const QuicTime& time,
557 QuicPacketSequenceNumber largest_observed,
558 const RttStats& rtt_stats));
559 MOCK_CONST_METHOD0(GetLossTimeout, QuicTime());
561 private:
562 DISALLOW_COPY_AND_ASSIGN(MockLossAlgorithm);
565 class TestEntropyCalculator :
566 public QuicReceivedEntropyHashCalculatorInterface {
567 public:
568 TestEntropyCalculator();
569 ~TestEntropyCalculator() override;
571 QuicPacketEntropyHash EntropyHash(
572 QuicPacketSequenceNumber sequence_number) const override;
574 private:
575 DISALLOW_COPY_AND_ASSIGN(TestEntropyCalculator);
578 class MockEntropyCalculator : public TestEntropyCalculator {
579 public:
580 MockEntropyCalculator();
581 ~MockEntropyCalculator() override;
583 MOCK_CONST_METHOD1(
584 EntropyHash,
585 QuicPacketEntropyHash(QuicPacketSequenceNumber sequence_number));
587 private:
588 DISALLOW_COPY_AND_ASSIGN(MockEntropyCalculator);
591 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface {
592 public:
593 MockAckNotifierDelegate();
595 MOCK_METHOD3(OnAckNotification,
596 void(int num_retransmitted_packets,
597 int num_retransmitted_bytes,
598 QuicTime::Delta delta_largest_observed));
600 protected:
601 // Object is ref counted.
602 ~MockAckNotifierDelegate() override;
604 private:
605 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate);
608 class MockNetworkChangeVisitor :
609 public QuicSentPacketManager::NetworkChangeVisitor {
610 public:
611 MockNetworkChangeVisitor();
612 ~MockNetworkChangeVisitor() override;
614 MOCK_METHOD0(OnCongestionWindowChange, void());
615 MOCK_METHOD0(OnRttChange, void());
617 private:
618 DISALLOW_COPY_AND_ASSIGN(MockNetworkChangeVisitor);
621 // Creates per-connection packet writers that register themselves with the
622 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
623 // be routed to the appropriate QuicConnection.
624 class TestWriterFactory : public tools::QuicDispatcher::PacketWriterFactory {
625 public:
626 TestWriterFactory();
627 ~TestWriterFactory() override;
629 QuicPacketWriter* Create(QuicPacketWriter* writer,
630 QuicConnection* connection) override;
632 // Calls OnPacketSent on the last QuicConnection to write through one of the
633 // packet writers created by this factory.
634 void OnPacketSent(WriteResult result);
636 private:
637 class PerConnectionPacketWriter
638 : public tools::QuicPerConnectionPacketWriter {
639 public:
640 PerConnectionPacketWriter(TestWriterFactory* factory,
641 QuicPacketWriter* writer,
642 QuicConnection* connection);
643 ~PerConnectionPacketWriter() override;
645 WriteResult WritePacket(const char* buffer,
646 size_t buf_len,
647 const IPAddressNumber& self_address,
648 const IPEndPoint& peer_address) override;
650 private:
651 TestWriterFactory* factory_;
654 // If an asynchronous write is happening and |writer| gets deleted, this
655 // clears the pointer to it to prevent use-after-free.
656 void Unregister(PerConnectionPacketWriter* writer);
658 PerConnectionPacketWriter* current_writer_;
661 class MockQuicConnectionDebugVisitor : public QuicConnectionDebugVisitor {
662 public:
663 MockQuicConnectionDebugVisitor();
664 ~MockQuicConnectionDebugVisitor();
666 MOCK_METHOD1(OnFrameAddedToPacket, void(const QuicFrame&));
668 MOCK_METHOD6(OnPacketSent,
669 void(const SerializedPacket&,
670 QuicPacketSequenceNumber,
671 EncryptionLevel,
672 TransmissionType,
673 const QuicEncryptedPacket&,
674 QuicTime));
676 MOCK_METHOD3(OnPacketReceived,
677 void(const IPEndPoint&,
678 const IPEndPoint&,
679 const QuicEncryptedPacket&));
681 MOCK_METHOD1(OnIncorrectConnectionId, void(QuicConnectionId));
683 MOCK_METHOD1(OnProtocolVersionMismatch, void(QuicVersion));
685 MOCK_METHOD1(OnPacketHeader, void(const QuicPacketHeader& header));
687 MOCK_METHOD1(OnStreamFrame, void(const QuicStreamFrame&));
689 MOCK_METHOD1(OnAckFrame, void(const QuicAckFrame& frame));
691 MOCK_METHOD1(OnStopWaitingFrame, void(const QuicStopWaitingFrame&));
693 MOCK_METHOD1(OnRstStreamFrame, void(const QuicRstStreamFrame&));
695 MOCK_METHOD1(OnConnectionCloseFrame, void(const QuicConnectionCloseFrame&));
697 MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket&));
699 MOCK_METHOD1(OnVersionNegotiationPacket,
700 void(const QuicVersionNegotiationPacket&));
702 MOCK_METHOD2(OnRevivedPacket,
703 void(const QuicPacketHeader&, StringPiece payload));
706 } // namespace test
707 } // namespace net
709 #endif // NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_