- add sources.
[platform/framework/web/crosswalk.git] / src / net / tools / quic / test_tools / quic_test_utils.h
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 // Upper limit on versions we support.
28 QuicVersion QuicVersionMax();
29
30 // Lower limit on versions we support.
31 QuicVersion QuicVersionMin();
32
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.
37 class SimpleRandom {
38  public:
39   SimpleRandom() : seed_(0) {}
40
41   // Returns a random number in the range [0, kuint64max].
42   uint64 RandUint64();
43
44   void set_seed(uint64 seed) { seed_ = seed; }
45
46  private:
47   uint64 seed_;
48 };
49
50 class MockConnection : public QuicConnection {
51  public:
52   // Uses a QuicConnectionHelper created with fd and eps.
53   MockConnection(QuicGuid guid,
54                  IPEndPoint address,
55                  int fd,
56                  EpollServer* eps,
57                  bool is_server);
58   // Uses a MockHelper.
59   MockConnection(QuicGuid guid, IPEndPoint address, bool is_server);
60   MockConnection(QuicGuid guid,
61                  IPEndPoint address,
62                  QuicConnectionHelperInterface* helper,
63                  QuicPacketWriter* writer,
64                  bool is_server);
65   virtual ~MockConnection();
66
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);
70
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(
76       QuicErrorCode error,
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());
84
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);
89   }
90
91   virtual bool OnProtocolVersionMismatch(QuicVersion version) { return false; }
92
93  private:
94   const bool has_mock_helper_;
95   scoped_ptr<QuicPacketWriter> writer_;
96   scoped_ptr<QuicConnectionHelperInterface> helper_;
97
98   DISALLOW_COPY_AND_ASSIGN(MockConnection);
99 };
100
101 class TestSession : public QuicSession {
102  public:
103   TestSession(QuicConnection* connection,
104               const QuicConfig& config,
105               bool is_server);
106   virtual ~TestSession();
107
108   MOCK_METHOD1(CreateIncomingReliableStream,
109                ReliableQuicStream*(QuicStreamId id));
110   MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*());
111
112   void SetCryptoStream(QuicCryptoStream* stream);
113
114   virtual QuicCryptoStream* GetCryptoStream();
115
116  private:
117   QuicCryptoStream* crypto_stream_;
118   DISALLOW_COPY_AND_ASSIGN(TestSession);
119 };
120
121 class MockPacketWriter : public QuicPacketWriter {
122  public:
123   MockPacketWriter();
124   virtual ~MockPacketWriter();
125
126   MOCK_METHOD5(WritePacket,
127                WriteResult(const char* buffer,
128                            size_t buf_len,
129                            const IPAddressNumber& self_address,
130                            const IPEndPoint& peer_address,
131                            QuicBlockedWriterInterface* blocked_writer));
132   MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool());
133 };
134
135 class MockQuicSessionOwner : public QuicSessionOwner {
136  public:
137   MockQuicSessionOwner();
138   ~MockQuicSessionOwner();
139   MOCK_METHOD2(OnConnectionClosed, void(QuicGuid guid, QuicErrorCode error));
140 };
141
142 class TestDecompressorVisitor : public QuicSpdyDecompressor::Visitor {
143  public:
144   virtual ~TestDecompressorVisitor() {}
145   virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE;
146   virtual void OnDecompressionError() OVERRIDE;
147
148   std::string data() { return data_; }
149   bool error() { return error_; }
150
151  private:
152   std::string data_;
153   bool error_;
154 };
155
156 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface {
157  public:
158   MockAckNotifierDelegate();
159   virtual ~MockAckNotifierDelegate();
160
161   MOCK_METHOD0(OnAckNotification, void());
162 };
163
164 }  // namespace test
165 }  // namespace tools
166 }  // namespace net
167
168 #endif  // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_