Home | History | Annotate | Download | only in test_tools
      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_session.h"
     13 #include "net/quic/quic_spdy_decompressor.h"
     14 #include "net/spdy/spdy_framer.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 
     17 namespace net {
     18 
     19 class EpollServer;
     20 class IPEndPoint;
     21 
     22 namespace tools {
     23 namespace test {
     24 
     25 std::string SerializeUncompressedHeaders(const SpdyHeaderBlock& headers);
     26 
     27 class MockConnection : public QuicConnection {
     28  public:
     29   // Uses a QuicConnectionHelper created with fd and eps.
     30   MockConnection(QuicGuid guid,
     31                  IPEndPoint address,
     32                  int fd,
     33                  EpollServer* eps,
     34                  bool is_server);
     35   // Uses a MockHelper.
     36   MockConnection(QuicGuid guid, IPEndPoint address, bool is_server);
     37   MockConnection(QuicGuid guid,
     38                  IPEndPoint address,
     39                  QuicConnectionHelperInterface* helper, bool is_server);
     40   virtual ~MockConnection();
     41 
     42   // If the constructor that uses a MockHelper has been used then this method
     43   // will advance the time of the MockClock.
     44   void AdvanceTime(QuicTime::Delta delta);
     45 
     46   MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address,
     47                                       const IPEndPoint& peer_address,
     48                                       const QuicEncryptedPacket& packet));
     49   MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error));
     50   MOCK_METHOD2(SendConnectionCloseWithDetails, void(
     51       QuicErrorCode error,
     52       const std::string& details));
     53   MOCK_METHOD2(SendRstStream, void(QuicStreamId id,
     54                                    QuicRstStreamErrorCode error));
     55   MOCK_METHOD3(SendGoAway, void(QuicErrorCode error,
     56                                 QuicStreamId last_good_stream_id,
     57                                 const std::string& reason));
     58   MOCK_METHOD0(OnCanWrite, bool());
     59 
     60   void ReallyProcessUdpPacket(const IPEndPoint& self_address,
     61                               const IPEndPoint& peer_address,
     62                               const QuicEncryptedPacket& packet) {
     63     return QuicConnection::ProcessUdpPacket(self_address, peer_address, packet);
     64   }
     65 
     66   virtual bool OnProtocolVersionMismatch(QuicVersion version) { return false; }
     67 
     68  private:
     69   const bool has_mock_helper_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(MockConnection);
     72 };
     73 
     74 class TestDecompressorVisitor : public QuicSpdyDecompressor::Visitor {
     75  public:
     76   virtual ~TestDecompressorVisitor() {}
     77   virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE;
     78   virtual void OnDecompressionError() OVERRIDE;
     79 
     80   std::string data() { return data_; }
     81   bool error() { return error_; }
     82 
     83  private:
     84   std::string data_;
     85   bool error_;
     86 };
     87 
     88 class TestSession : public QuicSession {
     89  public:
     90   TestSession(QuicConnection* connection,
     91               const QuicConfig& config,
     92               bool is_server);
     93   virtual ~TestSession();
     94 
     95   MOCK_METHOD1(CreateIncomingReliableStream,
     96                ReliableQuicStream*(QuicStreamId id));
     97   MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*());
     98 
     99   void SetCryptoStream(QuicCryptoStream* stream);
    100 
    101   virtual QuicCryptoStream* GetCryptoStream();
    102 
    103  private:
    104   QuicCryptoStream* crypto_stream_;
    105   DISALLOW_COPY_AND_ASSIGN(TestSession);
    106 };
    107 
    108 }  // namespace test
    109 }  // namespace tools
    110 }  // namespace net
    111 
    112 #endif  // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_
    113