• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6 
7 #ifndef NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
8 #define NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/strings/string_piece.h"
14 #include "net/quic/congestion_control/loss_detection_interface.h"
15 #include "net/quic/congestion_control/send_algorithm_interface.h"
16 #include "net/quic/quic_ack_notifier.h"
17 #include "net/quic/quic_client_session_base.h"
18 #include "net/quic/quic_connection.h"
19 #include "net/quic/quic_dispatcher.h"
20 #include "net/quic/quic_framer.h"
21 #include "net/quic/quic_per_connection_packet_writer.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 "testing/gmock/include/gmock/gmock.h"
28 
29 namespace net {
30 
31 namespace test {
32 
33 static const QuicConnectionId kTestConnectionId = 42;
34 static const int kTestPort = 123;
35 static const uint32 kInitialStreamFlowControlWindowForTest =
36     32 * 1024;  // 32 KB
37 static const uint32 kInitialSessionFlowControlWindowForTest =
38     64 * 1024;  // 64 KB
39 
40 // Data stream IDs start at 5: the crypto stream is 1, headers stream is 3.
41 static const QuicStreamId kClientDataStreamId1 = 5;
42 static const QuicStreamId kClientDataStreamId2 = 7;
43 static const QuicStreamId kClientDataStreamId3 = 9;
44 static const QuicStreamId kClientDataStreamId4 = 11;
45 
46 // Returns the test peer IP address.
47 IPAddressNumber TestPeerIPAddress();
48 
49 // Upper limit on versions we support.
50 QuicVersion QuicVersionMax();
51 
52 // Lower limit on versions we support.
53 QuicVersion QuicVersionMin();
54 
55 // Returns an address for 127.0.0.1.
56 IPAddressNumber Loopback4();
57 
58 // Returns an address for ::1.
59 IPAddressNumber Loopback6();
60 
61 void GenerateBody(std::string* body, int length);
62 
63 // Create an encrypted packet for testing.
64 QuicEncryptedPacket* ConstructEncryptedPacket(
65     QuicConnectionId connection_id,
66     bool version_flag,
67     bool reset_flag,
68     QuicPacketSequenceNumber sequence_number,
69     const std::string& data);
70 
71 void CompareCharArraysWithHexError(const std::string& description,
72                                    const char* actual,
73                                    const int actual_len,
74                                    const char* expected,
75                                    const int expected_len);
76 
77 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes);
78 
79 // Returns the length of a QuicPacket that is capable of holding either a
80 // stream frame or a minimal ack frame.  Sets |*payload_length| to the number
81 // of bytes of stream data that will fit in such a packet.
82 size_t GetPacketLengthForOneStream(
83     QuicVersion version,
84     bool include_version,
85     QuicSequenceNumberLength sequence_number_length,
86     InFecGroup is_in_fec_group,
87     size_t* payload_length);
88 
89 // Returns QuicConfig set to default values.
90 QuicConfig DefaultQuicConfig();
91 
92 // Returns a version vector consisting of |version|.
93 QuicVersionVector SupportedVersions(QuicVersion version);
94 
95 // Testing convenience method to construct a QuicAckFrame with entropy_hash set
96 // to 0 and largest_observed from peer set to |largest_observed|.
97 QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed);
98 
99 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges|
100 // nack ranges of width 1 packet, starting from |least_unacked|.
101 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges,
102                                         QuicPacketSequenceNumber least_unacked);
103 
104 // Returns a SerializedPacket whose |packet| member is owned by the caller, and
105 // is populated with the fields in |header| and |frames|, or is NULL if the
106 // packet could not be created.
107 SerializedPacket BuildUnsizedDataPacket(QuicFramer* framer,
108                                         const QuicPacketHeader& header,
109                                         const QuicFrames& frames);
110 
111 template<typename SaveType>
112 class ValueRestore {
113  public:
ValueRestore(SaveType * name,SaveType value)114   ValueRestore(SaveType* name, SaveType value)
115       : name_(name),
116         value_(*name) {
117     *name_ = value;
118   }
~ValueRestore()119   ~ValueRestore() {
120     *name_ = value_;
121   }
122 
123  private:
124   SaveType* name_;
125   SaveType value_;
126 
127   DISALLOW_COPY_AND_ASSIGN(ValueRestore);
128 };
129 
130 // Simple random number generator used to compute random numbers suitable
131 // for pseudo-randomly dropping packets in tests.  It works by computing
132 // the sha1 hash of the current seed, and using the first 64 bits as
133 // the next random number, and the next seed.
134 class SimpleRandom {
135  public:
SimpleRandom()136   SimpleRandom() : seed_(0) {}
137 
138   // Returns a random number in the range [0, kuint64max].
139   uint64 RandUint64();
140 
set_seed(uint64 seed)141   void set_seed(uint64 seed) { seed_ = seed; }
142 
143  private:
144   uint64 seed_;
145 
146   DISALLOW_COPY_AND_ASSIGN(SimpleRandom);
147 };
148 
149 class MockFramerVisitor : public QuicFramerVisitorInterface {
150  public:
151   MockFramerVisitor();
152   virtual ~MockFramerVisitor();
153 
154   MOCK_METHOD1(OnError, void(QuicFramer* framer));
155   // The constructor sets this up to return false by default.
156   MOCK_METHOD1(OnProtocolVersionMismatch, bool(QuicVersion version));
157   MOCK_METHOD0(OnPacket, void());
158   MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket& header));
159   MOCK_METHOD1(OnVersionNegotiationPacket,
160                void(const QuicVersionNegotiationPacket& packet));
161   MOCK_METHOD0(OnRevivedPacket, void());
162   // The constructor sets this up to return true by default.
163   MOCK_METHOD1(OnUnauthenticatedHeader, bool(const QuicPacketHeader& header));
164   // The constructor sets this up to return true by default.
165   MOCK_METHOD1(OnUnauthenticatedPublicHeader, bool(
166       const QuicPacketPublicHeader& header));
167   MOCK_METHOD1(OnDecryptedPacket, void(EncryptionLevel level));
168   MOCK_METHOD1(OnPacketHeader, bool(const QuicPacketHeader& header));
169   MOCK_METHOD1(OnFecProtectedPayload, void(base::StringPiece payload));
170   MOCK_METHOD1(OnStreamFrame, bool(const QuicStreamFrame& frame));
171   MOCK_METHOD1(OnAckFrame, bool(const QuicAckFrame& frame));
172   MOCK_METHOD1(OnCongestionFeedbackFrame,
173                bool(const QuicCongestionFeedbackFrame& frame));
174   MOCK_METHOD1(OnStopWaitingFrame, bool(const QuicStopWaitingFrame& frame));
175   MOCK_METHOD1(OnPingFrame, bool(const QuicPingFrame& frame));
176   MOCK_METHOD1(OnFecData, void(const QuicFecData& fec));
177   MOCK_METHOD1(OnRstStreamFrame, bool(const QuicRstStreamFrame& frame));
178   MOCK_METHOD1(OnConnectionCloseFrame,
179                bool(const QuicConnectionCloseFrame& frame));
180   MOCK_METHOD1(OnGoAwayFrame, bool(const QuicGoAwayFrame& frame));
181   MOCK_METHOD1(OnWindowUpdateFrame, bool(const QuicWindowUpdateFrame& frame));
182   MOCK_METHOD1(OnBlockedFrame, bool(const QuicBlockedFrame& frame));
183   MOCK_METHOD0(OnPacketComplete, void());
184 
185  private:
186   DISALLOW_COPY_AND_ASSIGN(MockFramerVisitor);
187 };
188 
189 class NoOpFramerVisitor : public QuicFramerVisitorInterface {
190  public:
NoOpFramerVisitor()191   NoOpFramerVisitor() {}
192 
OnError(QuicFramer * framer)193   virtual void OnError(QuicFramer* framer) OVERRIDE {}
OnPacket()194   virtual void OnPacket() OVERRIDE {}
OnPublicResetPacket(const QuicPublicResetPacket & packet)195   virtual void OnPublicResetPacket(
196       const QuicPublicResetPacket& packet) OVERRIDE {}
OnVersionNegotiationPacket(const QuicVersionNegotiationPacket & packet)197   virtual void OnVersionNegotiationPacket(
198       const QuicVersionNegotiationPacket& packet) OVERRIDE {}
OnRevivedPacket()199   virtual void OnRevivedPacket() OVERRIDE {}
200   virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE;
201   virtual bool OnUnauthenticatedHeader(const QuicPacketHeader& header) OVERRIDE;
202   virtual bool OnUnauthenticatedPublicHeader(
203       const QuicPacketPublicHeader& header) OVERRIDE;
OnDecryptedPacket(EncryptionLevel level)204   virtual void OnDecryptedPacket(EncryptionLevel level) OVERRIDE {}
205   virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE;
OnFecProtectedPayload(base::StringPiece payload)206   virtual void OnFecProtectedPayload(base::StringPiece payload) OVERRIDE {}
207   virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE;
208   virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE;
209   virtual bool OnCongestionFeedbackFrame(
210       const QuicCongestionFeedbackFrame& frame) OVERRIDE;
211   virtual bool OnStopWaitingFrame(
212       const QuicStopWaitingFrame& frame) OVERRIDE;
213   virtual bool OnPingFrame(const QuicPingFrame& frame) OVERRIDE;
OnFecData(const QuicFecData & fec)214   virtual void OnFecData(const QuicFecData& fec) OVERRIDE {}
215   virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE;
216   virtual bool OnConnectionCloseFrame(
217       const QuicConnectionCloseFrame& frame) OVERRIDE;
218   virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE;
219   virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) OVERRIDE;
220   virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE;
OnPacketComplete()221   virtual void OnPacketComplete() OVERRIDE {}
222 
223  private:
224   DISALLOW_COPY_AND_ASSIGN(NoOpFramerVisitor);
225 };
226 
227 class MockConnectionVisitor : public QuicConnectionVisitorInterface {
228  public:
229   MockConnectionVisitor();
230   virtual ~MockConnectionVisitor();
231 
232   MOCK_METHOD1(OnStreamFrames, void(const std::vector<QuicStreamFrame>& frame));
233   MOCK_METHOD1(OnWindowUpdateFrames,
234                void(const std::vector<QuicWindowUpdateFrame>& frame));
235   MOCK_METHOD1(OnBlockedFrames,
236                void(const std::vector<QuicBlockedFrame>& frame));
237   MOCK_METHOD1(OnRstStream, void(const QuicRstStreamFrame& frame));
238   MOCK_METHOD1(OnGoAway, void(const QuicGoAwayFrame& frame));
239   MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
240   MOCK_METHOD0(OnWriteBlocked, void());
241   MOCK_METHOD0(OnCanWrite, void());
242   MOCK_METHOD1(OnCongestionWindowChange, void(QuicTime now));
243   MOCK_CONST_METHOD0(WillingAndAbleToWrite, bool());
244   MOCK_CONST_METHOD0(HasPendingHandshake, bool());
245   MOCK_CONST_METHOD0(HasOpenDataStreams, bool());
246   MOCK_METHOD1(OnSuccessfulVersionNegotiation,
247                void(const QuicVersion& version));
248   MOCK_METHOD0(OnConfigNegotiated, void());
249 
250  private:
251   DISALLOW_COPY_AND_ASSIGN(MockConnectionVisitor);
252 };
253 
254 class MockHelper : public QuicConnectionHelperInterface {
255  public:
256   MockHelper();
257   virtual ~MockHelper();
258   virtual const QuicClock* GetClock() const OVERRIDE;
259   virtual QuicRandom* GetRandomGenerator() OVERRIDE;
260   virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE;
261   void AdvanceTime(QuicTime::Delta delta);
262 
263  private:
264   MockClock clock_;
265   MockRandom random_generator_;
266 
267   DISALLOW_COPY_AND_ASSIGN(MockHelper);
268 };
269 
270 class MockConnection : public QuicConnection {
271  public:
272   // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123.
273   explicit MockConnection(bool is_server);
274 
275   // Uses a MockHelper, ConnectionId of 42.
276   MockConnection(IPEndPoint address, bool is_server);
277 
278   // Uses a MockHelper, and 127.0.0.1:123
279   MockConnection(QuicConnectionId connection_id, bool is_server);
280 
281   // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123.
282   MockConnection(bool is_server, const QuicVersionVector& supported_versions);
283 
284   virtual ~MockConnection();
285 
286   // If the constructor that uses a MockHelper has been used then this method
287   // will advance the time of the MockClock.
288   void AdvanceTime(QuicTime::Delta delta);
289 
290   MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address,
291                                       const IPEndPoint& peer_address,
292                                       const QuicEncryptedPacket& packet));
293   MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error));
294   MOCK_METHOD2(SendConnectionCloseWithDetails, void(QuicErrorCode error,
295                                                     const string& details));
296   MOCK_METHOD2(SendConnectionClosePacket, void(QuicErrorCode error,
297                                                const string& details));
298   MOCK_METHOD3(SendRstStream, void(QuicStreamId id,
299                                    QuicRstStreamErrorCode error,
300                                    QuicStreamOffset bytes_written));
301   MOCK_METHOD3(SendGoAway, void(QuicErrorCode error,
302                                 QuicStreamId last_good_stream_id,
303                                 const string& reason));
304   MOCK_METHOD1(SendBlocked, void(QuicStreamId id));
305   MOCK_METHOD2(SendWindowUpdate, void(QuicStreamId id,
306                                       QuicStreamOffset byte_offset));
307   MOCK_METHOD0(OnCanWrite, void());
308 
ProcessUdpPacketInternal(const IPEndPoint & self_address,const IPEndPoint & peer_address,const QuicEncryptedPacket & packet)309   void ProcessUdpPacketInternal(const IPEndPoint& self_address,
310                                 const IPEndPoint& peer_address,
311                                 const QuicEncryptedPacket& packet) {
312     QuicConnection::ProcessUdpPacket(self_address, peer_address, packet);
313   }
314 
OnProtocolVersionMismatch(QuicVersion version)315   virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
316     return false;
317   }
318 
319  private:
320   scoped_ptr<QuicConnectionHelperInterface> helper_;
321 
322   DISALLOW_COPY_AND_ASSIGN(MockConnection);
323 };
324 
325 class PacketSavingConnection : public MockConnection {
326  public:
327   explicit PacketSavingConnection(bool is_server);
328 
329   PacketSavingConnection(bool is_server,
330                          const QuicVersionVector& supported_versions);
331 
332   virtual ~PacketSavingConnection();
333 
334   virtual void SendOrQueuePacket(QueuedPacket packet) OVERRIDE;
335 
336   std::vector<QuicPacket*> packets_;
337   std::vector<QuicEncryptedPacket*> encrypted_packets_;
338 
339  private:
340   DISALLOW_COPY_AND_ASSIGN(PacketSavingConnection);
341 };
342 
343 class MockSession : public QuicSession {
344  public:
345   explicit MockSession(QuicConnection* connection);
346   virtual ~MockSession();
347   MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer));
348   MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
349   MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*());
350   MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
351   MOCK_METHOD6(WritevData,
352                QuicConsumedData(QuicStreamId id,
353                                 const IOVector& data,
354                                 QuicStreamOffset offset,
355                                 bool fin,
356                                 FecProtection fec_protection,
357                                 QuicAckNotifier::DelegateInterface*));
358   MOCK_METHOD2(OnStreamHeaders, void(QuicStreamId stream_id,
359                                      base::StringPiece headers_data));
360   MOCK_METHOD2(OnStreamHeadersPriority, void(QuicStreamId stream_id,
361                                              QuicPriority priority));
362   MOCK_METHOD3(OnStreamHeadersComplete, void(QuicStreamId stream_id,
363                                              bool fin,
364                                              size_t frame_len));
365   MOCK_METHOD3(SendRstStream, void(QuicStreamId stream_id,
366                                    QuicRstStreamErrorCode error,
367                                    QuicStreamOffset bytes_written));
368   MOCK_METHOD0(IsCryptoHandshakeConfirmed, bool());
369 
370   using QuicSession::ActivateStream;
371 
372  private:
373   DISALLOW_COPY_AND_ASSIGN(MockSession);
374 };
375 
376 class TestSession : public QuicSession {
377  public:
378   TestSession(QuicConnection* connection, const QuicConfig& config);
379   virtual ~TestSession();
380 
381   MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
382   MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
383 
384   void SetCryptoStream(QuicCryptoStream* stream);
385 
386   virtual QuicCryptoStream* GetCryptoStream() OVERRIDE;
387 
388  private:
389   QuicCryptoStream* crypto_stream_;
390 
391   DISALLOW_COPY_AND_ASSIGN(TestSession);
392 };
393 
394 class TestClientSession : public QuicClientSessionBase {
395  public:
396   TestClientSession(QuicConnection* connection, const QuicConfig& config);
397   virtual ~TestClientSession();
398 
399   // QuicClientSessionBase
400   MOCK_METHOD1(OnProofValid,
401                void(const QuicCryptoClientConfig::CachedState& cached));
402   MOCK_METHOD1(OnProofVerifyDetailsAvailable,
403                void(const ProofVerifyDetails& verify_details));
404 
405   // TestClientSession
406   MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id));
407   MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*());
408 
409   void SetCryptoStream(QuicCryptoStream* stream);
410 
411   virtual QuicCryptoStream* GetCryptoStream() OVERRIDE;
412 
413  private:
414   QuicCryptoStream* crypto_stream_;
415 
416   DISALLOW_COPY_AND_ASSIGN(TestClientSession);
417 };
418 
419 class MockPacketWriter : public QuicPacketWriter {
420  public:
421   MockPacketWriter();
422   virtual ~MockPacketWriter();
423 
424   MOCK_METHOD4(WritePacket,
425                WriteResult(const char* buffer,
426                            size_t buf_len,
427                            const IPAddressNumber& self_address,
428                            const IPEndPoint& peer_address));
429   MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool());
430   MOCK_CONST_METHOD0(IsWriteBlocked, bool());
431   MOCK_METHOD0(SetWritable, void());
432 
433  private:
434   DISALLOW_COPY_AND_ASSIGN(MockPacketWriter);
435 };
436 
437 class MockSendAlgorithm : public SendAlgorithmInterface {
438  public:
439   MockSendAlgorithm();
440   virtual ~MockSendAlgorithm();
441 
442   MOCK_METHOD2(SetFromConfig, void(const QuicConfig& config, bool is_server));
443   MOCK_METHOD1(SetMaxPacketSize, void(QuicByteCount max_packet_size));
444   MOCK_METHOD2(OnIncomingQuicCongestionFeedbackFrame,
445                void(const QuicCongestionFeedbackFrame&,
446                     QuicTime feedback_receive_time));
447   MOCK_METHOD4(OnCongestionEvent, void(bool rtt_updated,
448                                        QuicByteCount bytes_in_flight,
449                                        const CongestionVector& acked_packets,
450                                        const CongestionVector& lost_packets));
451   MOCK_METHOD5(OnPacketSent,
452                bool(QuicTime, QuicByteCount, QuicPacketSequenceNumber,
453                     QuicByteCount, HasRetransmittableData));
454   MOCK_METHOD1(OnRetransmissionTimeout, void(bool));
455   MOCK_METHOD0(RevertRetransmissionTimeout, void());
456   MOCK_CONST_METHOD3(TimeUntilSend,
457                      QuicTime::Delta(QuicTime now,
458                                      QuicByteCount bytes_in_flight,
459                                      HasRetransmittableData));
460   MOCK_CONST_METHOD0(BandwidthEstimate, QuicBandwidth(void));
461   MOCK_CONST_METHOD0(HasReliableBandwidthEstimate, bool());
462   MOCK_METHOD1(OnRttUpdated, void(QuicPacketSequenceNumber));
463   MOCK_CONST_METHOD0(RetransmissionDelay, QuicTime::Delta(void));
464   MOCK_CONST_METHOD0(GetCongestionWindow, QuicByteCount());
465   MOCK_CONST_METHOD0(InSlowStart, bool());
466   MOCK_CONST_METHOD0(InRecovery, bool());
467   MOCK_CONST_METHOD0(GetSlowStartThreshold, QuicByteCount());
468   MOCK_CONST_METHOD0(GetCongestionControlType, CongestionControlType());
469 
470  private:
471   DISALLOW_COPY_AND_ASSIGN(MockSendAlgorithm);
472 };
473 
474 class MockLossAlgorithm : public LossDetectionInterface {
475  public:
476   MockLossAlgorithm();
477   virtual ~MockLossAlgorithm();
478 
479   MOCK_CONST_METHOD0(GetLossDetectionType, LossDetectionType());
480   MOCK_METHOD4(DetectLostPackets,
481                SequenceNumberSet(const QuicUnackedPacketMap& unacked_packets,
482                                  const QuicTime& time,
483                                  QuicPacketSequenceNumber largest_observed,
484                                  const RttStats& rtt_stats));
485   MOCK_CONST_METHOD0(GetLossTimeout, QuicTime());
486 
487  private:
488   DISALLOW_COPY_AND_ASSIGN(MockLossAlgorithm);
489 };
490 
491 class TestEntropyCalculator :
492       public QuicReceivedEntropyHashCalculatorInterface {
493  public:
494   TestEntropyCalculator();
495   virtual ~TestEntropyCalculator();
496 
497   virtual QuicPacketEntropyHash EntropyHash(
498       QuicPacketSequenceNumber sequence_number) const OVERRIDE;
499 
500  private:
501   DISALLOW_COPY_AND_ASSIGN(TestEntropyCalculator);
502 };
503 
504 class MockEntropyCalculator : public TestEntropyCalculator {
505  public:
506   MockEntropyCalculator();
507   virtual ~MockEntropyCalculator();
508 
509   MOCK_CONST_METHOD1(
510       EntropyHash,
511       QuicPacketEntropyHash(QuicPacketSequenceNumber sequence_number));
512 
513  private:
514   DISALLOW_COPY_AND_ASSIGN(MockEntropyCalculator);
515 };
516 
517 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface {
518  public:
519   MockAckNotifierDelegate();
520 
521   MOCK_METHOD5(OnAckNotification, void(int num_original_packets,
522                                        int num_original_bytes,
523                                        int num_retransmitted_packets,
524                                        int num_retransmitted_bytes,
525                                        QuicTime::Delta delta_largest_observed));
526 
527  protected:
528   // Object is ref counted.
529   virtual ~MockAckNotifierDelegate();
530 
531  private:
532   DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate);
533 };
534 
535 class MockNetworkChangeVisitor :
536       public QuicSentPacketManager::NetworkChangeVisitor {
537  public:
538   MockNetworkChangeVisitor();
539   virtual ~MockNetworkChangeVisitor();
540 
541   MOCK_METHOD1(OnCongestionWindowChange, void(QuicByteCount));
542 
543  private:
544   DISALLOW_COPY_AND_ASSIGN(MockNetworkChangeVisitor);
545 };
546 
547 // Creates per-connection packet writers that register themselves with the
548 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can
549 // be routed to the appropriate QuicConnection.
550 class TestWriterFactory : public QuicDispatcher::PacketWriterFactory {
551  public:
552   TestWriterFactory();
553   virtual ~TestWriterFactory();
554 
555   virtual QuicPacketWriter* Create(QuicServerPacketWriter* writer,
556                                    QuicConnection* connection) OVERRIDE;
557 
558   // Calls OnPacketSent on the last QuicConnection to write through one of the
559   // packet writers created by this factory.
560   void OnPacketSent(WriteResult result);
561 
562  private:
563   class PerConnectionPacketWriter : public QuicPerConnectionPacketWriter {
564    public:
565     PerConnectionPacketWriter(TestWriterFactory* factory,
566                               QuicServerPacketWriter* writer,
567                               QuicConnection* connection);
568     virtual ~PerConnectionPacketWriter();
569 
570     virtual WriteResult WritePacket(
571         const char* buffer,
572         size_t buf_len,
573         const IPAddressNumber& self_address,
574         const IPEndPoint& peer_address) OVERRIDE;
575 
576    private:
577     TestWriterFactory* factory_;
578   };
579 
580   // If an asynchronous write is happening and |writer| gets deleted, this
581   // clears the pointer to it to prevent use-after-free.
582   void Unregister(PerConnectionPacketWriter* writer);
583 
584   PerConnectionPacketWriter* current_writer_;
585 };
586 
587 }  // namespace test
588 }  // namespace net
589 
590 #endif  // NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
591