OLD | NEW |
| (Empty) |
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/crypto/cached_network_parameters.h" | |
14 #include "net/quic/quic_connection.h" | |
15 #include "net/quic/quic_packet_writer.h" | |
16 #include "net/quic/quic_session.h" | |
17 #include "net/spdy/spdy_framer.h" | |
18 #include "net/tools/quic/quic_dispatcher.h" | |
19 #include "net/tools/quic/quic_per_connection_packet_writer.h" | |
20 #include "net/tools/quic/quic_server_session.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 | |
23 namespace net { | |
24 | |
25 class EpollServer; | |
26 class IPEndPoint; | |
27 | |
28 namespace tools { | |
29 namespace test { | |
30 | |
31 static const QuicConnectionId kTestConnectionId = 42; | |
32 static const uint16 kTestPort = 123; | |
33 static const uint32 kInitialStreamFlowControlWindowForTest = | |
34 32 * 1024; // 32 KB | |
35 static const uint32 kInitialSessionFlowControlWindowForTest = | |
36 64 * 1024; // 64 KB | |
37 | |
38 // Testing convenience method to construct a QuicAckFrame with |num_nack_ranges| | |
39 // nack ranges of width 1 packet, starting from |least_unacked|. | |
40 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges, | |
41 QuicPacketSequenceNumber least_unacked); | |
42 | |
43 class NiceMockPacketWriterFactory : public QuicConnection::PacketWriterFactory { | |
44 public: | |
45 NiceMockPacketWriterFactory() {} | |
46 ~NiceMockPacketWriterFactory() override {} | |
47 | |
48 QuicPacketWriter* Create(QuicConnection* /*connection*/) const override; | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(NiceMockPacketWriterFactory); | |
52 }; | |
53 | |
54 class MockConnection : public QuicConnection { | |
55 public: | |
56 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123. | |
57 explicit MockConnection(bool is_server); | |
58 | |
59 // Uses a MockHelper, ConnectionId of 42, and 127.0.0.1:123. | |
60 MockConnection(bool is_server, bool is_secure); | |
61 | |
62 // Uses a MockHelper, ConnectionId of 42. | |
63 MockConnection(IPEndPoint address, bool is_server); | |
64 | |
65 // Uses a MockHelper, and 127.0.0.1:123 | |
66 MockConnection(QuicConnectionId connection_id, bool is_server); | |
67 | |
68 // Uses a Mock helper, ConnectionId of 42, and 127.0.0.1:123. | |
69 MockConnection(bool is_server, const QuicVersionVector& supported_versions); | |
70 | |
71 virtual ~MockConnection(); | |
72 | |
73 // If the constructor that uses a MockHelper has been used then this method | |
74 // will advance the time of the MockClock. | |
75 void AdvanceTime(QuicTime::Delta delta); | |
76 | |
77 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address, | |
78 const IPEndPoint& peer_address, | |
79 const QuicEncryptedPacket& packet)); | |
80 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error)); | |
81 MOCK_METHOD2(SendConnectionCloseWithDetails, | |
82 void(QuicErrorCode error, const std::string& details)); | |
83 MOCK_METHOD2(SendConnectionClosePacket, | |
84 void(QuicErrorCode error, const std::string& details)); | |
85 MOCK_METHOD3(SendRstStream, void(QuicStreamId id, | |
86 QuicRstStreamErrorCode error, | |
87 QuicStreamOffset bytes_written)); | |
88 MOCK_METHOD3(SendGoAway, | |
89 void(QuicErrorCode error, | |
90 QuicStreamId last_good_stream_id, | |
91 const std::string& reason)); | |
92 MOCK_METHOD1(SendBlocked, void(QuicStreamId id)); | |
93 MOCK_METHOD2(SendWindowUpdate, void(QuicStreamId id, | |
94 QuicStreamOffset byte_offset)); | |
95 MOCK_METHOD0(OnCanWrite, void()); | |
96 MOCK_CONST_METHOD0(HasPendingWrites, bool()); | |
97 | |
98 MOCK_METHOD1(ResumeConnectionState, bool(const CachedNetworkParameters&)); | |
99 | |
100 void ReallyProcessUdpPacket(const IPEndPoint& self_address, | |
101 const IPEndPoint& peer_address, | |
102 const QuicEncryptedPacket& packet) { | |
103 QuicConnection::ProcessUdpPacket(self_address, peer_address, packet); | |
104 } | |
105 | |
106 virtual bool OnProtocolVersionMismatch(QuicVersion version) override { | |
107 return false; | |
108 } | |
109 | |
110 private: | |
111 scoped_ptr<QuicConnectionHelperInterface> helper_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(MockConnection); | |
114 }; | |
115 | |
116 class TestSession : public QuicSession { | |
117 public: | |
118 TestSession(QuicConnection* connection, const QuicConfig& config); | |
119 virtual ~TestSession(); | |
120 | |
121 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); | |
122 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); | |
123 | |
124 void SetCryptoStream(QuicCryptoStream* stream); | |
125 | |
126 virtual QuicCryptoStream* GetCryptoStream() override; | |
127 | |
128 private: | |
129 QuicCryptoStream* crypto_stream_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(TestSession); | |
132 }; | |
133 | |
134 class MockPacketWriter : public QuicPacketWriter { | |
135 public: | |
136 MockPacketWriter(); | |
137 virtual ~MockPacketWriter(); | |
138 | |
139 MOCK_METHOD4(WritePacket, | |
140 WriteResult(const char* buffer, | |
141 size_t buf_len, | |
142 const IPAddressNumber& self_address, | |
143 const IPEndPoint& peer_address)); | |
144 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool()); | |
145 MOCK_CONST_METHOD0(IsWriteBlocked, bool()); | |
146 MOCK_METHOD0(SetWritable, void()); | |
147 | |
148 private: | |
149 DISALLOW_COPY_AND_ASSIGN(MockPacketWriter); | |
150 }; | |
151 | |
152 class MockQuicServerSessionVisitor : public QuicServerSessionVisitor { | |
153 public: | |
154 MockQuicServerSessionVisitor(); | |
155 virtual ~MockQuicServerSessionVisitor(); | |
156 MOCK_METHOD2(OnConnectionClosed, void(QuicConnectionId connection_id, | |
157 QuicErrorCode error)); | |
158 MOCK_METHOD1(OnWriteBlocked, | |
159 void(QuicBlockedWriterInterface* blocked_writer)); | |
160 MOCK_METHOD1(OnConnectionAddedToTimeWaitList, | |
161 void(QuicConnectionId connection_id)); | |
162 MOCK_METHOD1(OnConnectionRemovedFromTimeWaitList, | |
163 void(QuicConnectionId connection_id)); | |
164 | |
165 private: | |
166 DISALLOW_COPY_AND_ASSIGN(MockQuicServerSessionVisitor); | |
167 }; | |
168 | |
169 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface { | |
170 public: | |
171 MockAckNotifierDelegate(); | |
172 | |
173 MOCK_METHOD3(OnAckNotification, | |
174 void(int num_retransmitted_packets, | |
175 int num_retransmitted_bytes, | |
176 QuicTime::Delta delta_largest_observed)); | |
177 | |
178 protected: | |
179 // Object is ref counted. | |
180 virtual ~MockAckNotifierDelegate(); | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(MockAckNotifierDelegate); | |
183 }; | |
184 | |
185 // Creates per-connection packet writers that register themselves with the | |
186 // TestWriterFactory on each write so that TestWriterFactory::OnPacketSent can | |
187 // be routed to the appropriate QuicConnection. | |
188 class TestWriterFactory : public QuicDispatcher::PacketWriterFactory { | |
189 public: | |
190 TestWriterFactory(); | |
191 ~TestWriterFactory() override; | |
192 | |
193 QuicPacketWriter* Create(QuicPacketWriter* writer, | |
194 QuicConnection* connection) override; | |
195 | |
196 // Calls OnPacketSent on the last QuicConnection to write through one of the | |
197 // packet writers created by this factory. | |
198 void OnPacketSent(WriteResult result); | |
199 | |
200 private: | |
201 class PerConnectionPacketWriter : public QuicPerConnectionPacketWriter { | |
202 public: | |
203 PerConnectionPacketWriter(TestWriterFactory* factory, | |
204 QuicPacketWriter* writer, | |
205 QuicConnection* connection); | |
206 ~PerConnectionPacketWriter() override; | |
207 | |
208 WriteResult WritePacket(const char* buffer, | |
209 size_t buf_len, | |
210 const IPAddressNumber& self_address, | |
211 const IPEndPoint& peer_address) override; | |
212 | |
213 private: | |
214 TestWriterFactory* factory_; | |
215 }; | |
216 | |
217 // If an asynchronous write is happening and |writer| gets deleted, this | |
218 // clears the pointer to it to prevent use-after-free. | |
219 void Unregister(PerConnectionPacketWriter* writer); | |
220 | |
221 PerConnectionPacketWriter* current_writer_; | |
222 }; | |
223 | |
224 } // namespace test | |
225 } // namespace tools | |
226 } // namespace net | |
227 | |
228 #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ | |
OLD | NEW |