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 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 6 #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 7 8 #include <string> 9 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" 18 19 namespace net { 20 21 class EpollServer; 22 class IPEndPoint; 23 24 namespace tools { 25 namespace test { 26 27 static const QuicGuid kTestGuid = 42; 28 static const int kTestPort = 123; 29 30 // Simple random number generator used to compute random numbers suitable 31 // for pseudo-randomly dropping packets in tests. It works by computing 32 // the sha1 hash of the current seed, and using the first 64 bits as 33 // the next random number, and the next seed. 34 class SimpleRandom { 35 public: SimpleRandom()36 SimpleRandom() : seed_(0) {} 37 38 // Returns a random number in the range [0, kuint64max]. 39 uint64 RandUint64(); 40 set_seed(uint64 seed)41 void set_seed(uint64 seed) { seed_ = seed; } 42 43 private: 44 uint64 seed_; 45 }; 46 47 class MockConnection : public QuicConnection { 48 public: 49 // Uses a MockHelper, GUID of 42, and 127.0.0.1:123. 50 explicit MockConnection(bool is_server); 51 52 // Uses a MockHelper, GUID of 42. 53 MockConnection(IPEndPoint address, 54 bool is_server); 55 56 // Uses a MockHelper, and 127.0.0.1:123 57 MockConnection(QuicGuid guid, 58 bool is_server); 59 60 virtual ~MockConnection(); 61 62 // If the constructor that uses a MockHelper has been used then this method 63 // will advance the time of the MockClock. 64 void AdvanceTime(QuicTime::Delta delta); 65 66 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address, 67 const IPEndPoint& peer_address, 68 const QuicEncryptedPacket& packet)); 69 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error)); 70 MOCK_METHOD2(SendConnectionCloseWithDetails, void( 71 QuicErrorCode error, 72 const std::string& details)); 73 MOCK_METHOD2(SendRstStream, void(QuicStreamId id, 74 QuicRstStreamErrorCode error)); 75 MOCK_METHOD3(SendGoAway, void(QuicErrorCode error, 76 QuicStreamId last_good_stream_id, 77 const std::string& reason)); 78 MOCK_METHOD0(OnCanWrite, bool()); 79 ReallyProcessUdpPacket(const IPEndPoint & self_address,const IPEndPoint & peer_address,const QuicEncryptedPacket & packet)80 void ReallyProcessUdpPacket(const IPEndPoint& self_address, 81 const IPEndPoint& peer_address, 82 const QuicEncryptedPacket& packet) { 83 return QuicConnection::ProcessUdpPacket(self_address, peer_address, packet); 84 } 85 OnProtocolVersionMismatch(QuicVersion version)86 virtual bool OnProtocolVersionMismatch(QuicVersion version) { return false; } 87 88 private: 89 scoped_ptr<QuicPacketWriter> writer_; 90 scoped_ptr<QuicConnectionHelperInterface> helper_; 91 92 DISALLOW_COPY_AND_ASSIGN(MockConnection); 93 }; 94 95 class TestSession : public QuicSession { 96 public: 97 TestSession(QuicConnection* connection, const QuicConfig& config); 98 virtual ~TestSession(); 99 100 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); 101 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); 102 103 void SetCryptoStream(QuicCryptoStream* stream); 104 105 virtual QuicCryptoStream* GetCryptoStream(); 106 107 private: 108 QuicCryptoStream* crypto_stream_; 109 DISALLOW_COPY_AND_ASSIGN(TestSession); 110 }; 111 112 class MockPacketWriter : public QuicPacketWriter { 113 public: 114 MockPacketWriter(); 115 virtual ~MockPacketWriter(); 116 117 MOCK_METHOD5(WritePacket, 118 WriteResult(const char* buffer, 119 size_t buf_len, 120 const IPAddressNumber& self_address, 121 const IPEndPoint& peer_address, 122 QuicBlockedWriterInterface* blocked_writer)); 123 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool()); 124 }; 125 126 class MockQuicSessionOwner : public QuicSessionOwner { 127 public: 128 MockQuicSessionOwner(); 129 ~MockQuicSessionOwner(); 130 MOCK_METHOD2(OnConnectionClosed, void(QuicGuid guid, QuicErrorCode error)); 131 }; 132 133 class TestDecompressorVisitor : public QuicSpdyDecompressor::Visitor { 134 public: ~TestDecompressorVisitor()135 virtual ~TestDecompressorVisitor() {} 136 virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE; 137 virtual void OnDecompressionError() OVERRIDE; 138 data()139 std::string data() { return data_; } error()140 bool error() { return error_; } 141 142 private: 143 std::string data_; 144 bool error_; 145 }; 146 147 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface { 148 public: 149 MockAckNotifierDelegate(); 150 virtual ~MockAckNotifierDelegate(); 151 152 MOCK_METHOD0(OnAckNotification, void()); 153 }; 154 155 } // namespace test 156 } // namespace tools 157 } // namespace net 158 159 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 160