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 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
6 #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
10 #include "base/strings/string_piece.h"
11 #include "net/quic/quic_connection.h"
12 #include "net/quic/quic_packet_writer.h"
13 #include "net/quic/quic_session.h"
14 #include "net/quic/quic_spdy_decompressor.h"
15 #include "net/spdy/spdy_framer.h"
16 #include "net/tools/quic/quic_server_session.h"
17 #include "testing/gmock/include/gmock/gmock.h"
27 // Upper limit on versions we support.
28 QuicVersion QuicVersionMax();
30 // Lower limit on versions we support.
31 QuicVersion QuicVersionMin();
33 // Simple random number generator used to compute random numbers suitable
34 // for pseudo-randomly dropping packets in tests. It works by computing
35 // the sha1 hash of the current seed, and using the first 64 bits as
36 // the next random number, and the next seed.
39 SimpleRandom() : seed_(0) {}
41 // Returns a random number in the range [0, kuint64max].
44 void set_seed(uint64 seed) { seed_ = seed; }
50 class MockConnection : public QuicConnection {
52 // Uses a QuicConnectionHelper created with fd and eps.
53 MockConnection(QuicGuid guid,
59 MockConnection(QuicGuid guid, IPEndPoint address, bool is_server);
60 MockConnection(QuicGuid guid,
62 QuicConnectionHelperInterface* helper,
63 QuicPacketWriter* writer,
65 virtual ~MockConnection();
67 // If the constructor that uses a MockHelper has been used then this method
68 // will advance the time of the MockClock.
69 void AdvanceTime(QuicTime::Delta delta);
71 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address,
72 const IPEndPoint& peer_address,
73 const QuicEncryptedPacket& packet));
74 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error));
75 MOCK_METHOD2(SendConnectionCloseWithDetails, void(
77 const std::string& details));
78 MOCK_METHOD2(SendRstStream, void(QuicStreamId id,
79 QuicRstStreamErrorCode error));
80 MOCK_METHOD3(SendGoAway, void(QuicErrorCode error,
81 QuicStreamId last_good_stream_id,
82 const std::string& reason));
83 MOCK_METHOD0(OnCanWrite, bool());
85 void ReallyProcessUdpPacket(const IPEndPoint& self_address,
86 const IPEndPoint& peer_address,
87 const QuicEncryptedPacket& packet) {
88 return QuicConnection::ProcessUdpPacket(self_address, peer_address, packet);
91 virtual bool OnProtocolVersionMismatch(QuicVersion version) { return false; }
94 const bool has_mock_helper_;
95 scoped_ptr<QuicPacketWriter> writer_;
96 scoped_ptr<QuicConnectionHelperInterface> helper_;
98 DISALLOW_COPY_AND_ASSIGN(MockConnection);
101 class TestSession : public QuicSession {
103 TestSession(QuicConnection* connection,
104 const QuicConfig& config,
106 virtual ~TestSession();
108 MOCK_METHOD1(CreateIncomingReliableStream,
109 ReliableQuicStream*(QuicStreamId id));
110 MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*());
112 void SetCryptoStream(QuicCryptoStream* stream);
114 virtual QuicCryptoStream* GetCryptoStream();
117 QuicCryptoStream* crypto_stream_;
118 DISALLOW_COPY_AND_ASSIGN(TestSession);
121 class MockPacketWriter : public QuicPacketWriter {
124 virtual ~MockPacketWriter();
126 MOCK_METHOD5(WritePacket,
127 WriteResult(const char* buffer,
129 const IPAddressNumber& self_address,
130 const IPEndPoint& peer_address,
131 QuicBlockedWriterInterface* blocked_writer));
132 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool());
135 class MockQuicSessionOwner : public QuicSessionOwner {
137 MockQuicSessionOwner();
138 ~MockQuicSessionOwner();
139 MOCK_METHOD2(OnConnectionClosed, void(QuicGuid guid, QuicErrorCode error));
142 class TestDecompressorVisitor : public QuicSpdyDecompressor::Visitor {
144 virtual ~TestDecompressorVisitor() {}
145 virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE;
146 virtual void OnDecompressionError() OVERRIDE;
148 std::string data() { return data_; }
149 bool error() { return error_; }
156 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface {
158 MockAckNotifierDelegate();
159 virtual ~MockAckNotifierDelegate();
161 MOCK_METHOD0(OnAckNotification, void());
168 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_