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_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 8 #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 9 10 #include <string> 11 12 #include "base/strings/string_piece.h" 13 #include "net/quic/quic_connection.h" 14 #include "net/quic/quic_packet_writer.h" 15 #include "net/quic/quic_session.h" 16 #include "net/spdy/spdy_framer.h" 17 #include "net/tools/quic/quic_server_session.h" 18 #include "testing/gmock/include/gmock/gmock.h" 19 20 namespace net { 21 22 class EpollServer; 23 class IPEndPoint; 24 25 namespace tools { 26 namespace test { 27 28 static const QuicConnectionId kTestConnectionId = 42; 29 static const int kTestPort = 123; 30 static const uint32 kInitialStreamFlowControlWindowForTest = 31 32 * 1024; // 32 KB 32 static const uint32 kInitialSessionFlowControlWindowForTest = 33 64 * 1024; // 64 KB 34 35 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges| 36 // nack ranges of width 1 packet, starting from |least_unacked|. 37 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges, 38 QuicPacketSequenceNumber least_unacked); 39 40 class MockConnection : public QuicConnection { 41 public: 42 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123. 43 explicit MockConnection(bool is_server); 44 45 // Uses a MockHelper, ConnectionId of 42. 46 MockConnection(IPEndPoint address, bool is_server); 47 48 // Uses a MockHelper, and 127.0.0.1:123 49 MockConnection(QuicConnectionId connection_id, bool is_server); 50 51 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123. 52 MockConnection(bool is_server, const QuicVersionVector& supported_versions); 53 54 virtual ~MockConnection(); 55 56 // If the constructor that uses a MockHelper has been used then this method 57 // will advance the time of the MockClock. 58 void AdvanceTime(QuicTime::Delta delta); 59 60 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address, 61 const IPEndPoint& peer_address, 62 const QuicEncryptedPacket& packet)); 63 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error)); 64 MOCK_METHOD2(SendConnectionCloseWithDetails, void( 65 QuicErrorCode error, 66 const std::string& details)); 67 MOCK_METHOD2(SendConnectionClosePacket, void(QuicErrorCode error, 68 const std::string& details)); 69 MOCK_METHOD3(SendRstStream, void(QuicStreamId id, 70 QuicRstStreamErrorCode error, 71 QuicStreamOffset bytes_written)); 72 MOCK_METHOD3(SendGoAway, void(QuicErrorCode error, 73 QuicStreamId last_good_stream_id, 74 const std::string& reason)); 75 MOCK_METHOD1(SendBlocked, void(QuicStreamId id)); 76 MOCK_METHOD2(SendWindowUpdate, void(QuicStreamId id, 77 QuicStreamOffset byte_offset)); 78 MOCK_METHOD0(OnCanWrite, void()); 79 MOCK_CONST_METHOD0(HasPendingWrites, bool()); 80 ReallyProcessUdpPacket(const IPEndPoint & self_address,const IPEndPoint & peer_address,const QuicEncryptedPacket & packet)81 void ReallyProcessUdpPacket(const IPEndPoint& self_address, 82 const IPEndPoint& peer_address, 83 const QuicEncryptedPacket& packet) { 84 return QuicConnection::ProcessUdpPacket(self_address, peer_address, packet); 85 } 86 OnProtocolVersionMismatch(QuicVersion version)87 virtual bool OnProtocolVersionMismatch(QuicVersion version) { return false; } 88 89 private: 90 scoped_ptr<QuicPacketWriter> writer_; 91 scoped_ptr<QuicConnectionHelperInterface> helper_; 92 93 DISALLOW_COPY_AND_ASSIGN(MockConnection); 94 }; 95 96 class TestSession : public QuicSession { 97 public: 98 TestSession(QuicConnection* connection, const QuicConfig& config); 99 virtual ~TestSession(); 100 101 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); 102 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); 103 104 void SetCryptoStream(QuicCryptoStream* stream); 105 106 virtual QuicCryptoStream* GetCryptoStream() OVERRIDE; 107 108 private: 109 QuicCryptoStream* crypto_stream_; 110 111 DISALLOW_COPY_AND_ASSIGN(TestSession); 112 }; 113 114 class MockPacketWriter : public QuicPacketWriter { 115 public: 116 MockPacketWriter(); 117 virtual ~MockPacketWriter(); 118 119 MOCK_METHOD4(WritePacket, 120 WriteResult(const char* buffer, 121 size_t buf_len, 122 const IPAddressNumber& self_address, 123 const IPEndPoint& peer_address)); 124 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool()); 125 MOCK_CONST_METHOD0(IsWriteBlocked, bool()); 126 MOCK_METHOD0(SetWritable, void()); 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter); 130 }; 131 132 class MockQuicServerSessionVisitor : public QuicServerSessionVisitor { 133 public: 134 MockQuicServerSessionVisitor(); 135 virtual ~MockQuicServerSessionVisitor(); 136 MOCK_METHOD2(OnConnectionClosed, void(QuicConnectionId connection_id, 137 QuicErrorCode error)); 138 MOCK_METHOD1(OnWriteBlocked, void(QuicBlockedWriterInterface* writer)); 139 140 private: 141 DISALLOW_COPY_AND_ASSIGN(MockQuicServerSessionVisitor); 142 }; 143 144 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface { 145 public: 146 MockAckNotifierDelegate(); 147 148 MOCK_METHOD5(OnAckNotification, void(int num_original_packets, 149 int num_original_bytes, 150 int num_retransmitted_packets, 151 int num_retransmitted_bytes, 152 QuicTime::Delta delta_largest_observed)); 153 154 protected: 155 // Object is ref counted. 156 virtual ~MockAckNotifierDelegate(); 157 158 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate); 159 }; 160 161 } // namespace test 162 } // namespace tools 163 } // namespace net 164 165 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 166