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_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 8 #define NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 9 10 #include <string> 11 #include <vector> 12 13 #include "base/strings/string_piece.h" 14 #include "net/quic/congestion_control/send_algorithm_interface.h" 15 #include "net/quic/quic_ack_notifier.h" 16 #include "net/quic/quic_connection.h" 17 #include "net/quic/quic_framer.h" 18 #include "net/quic/quic_session.h" 19 #include "net/quic/quic_spdy_decompressor.h" 20 #include "net/quic/test_tools/mock_clock.h" 21 #include "net/quic/test_tools/mock_random.h" 22 #include "net/spdy/spdy_framer.h" 23 #include "testing/gmock/include/gmock/gmock.h" 24 25 namespace net { 26 27 namespace test { 28 29 static const QuicGuid kTestGuid = 42; 30 static const int kTestPort = 123; 31 32 // Upper limit on versions we support. 33 QuicVersion QuicVersionMax(); 34 35 // Lower limit on versions we support. 36 QuicVersion QuicVersionMin(); 37 38 // Returns an address for 127.0.0.1. 39 IPAddressNumber Loopback4(); 40 41 void CompareCharArraysWithHexError(const std::string& description, 42 const char* actual, 43 const int actual_len, 44 const char* expected, 45 const int expected_len); 46 47 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes); 48 49 // Returns the length of a QuicPacket that is capable of holding either a 50 // stream frame or a minimal ack frame. Sets |*payload_length| to the number 51 // of bytes of stream data that will fit in such a packet. 52 size_t GetPacketLengthForOneStream( 53 QuicVersion version, 54 bool include_version, 55 QuicSequenceNumberLength sequence_number_length, 56 InFecGroup is_in_fec_group, 57 size_t* payload_length); 58 59 // Size in bytes of the stream frame fields for an arbitrary StreamID and 60 // offset and the last frame in a packet. 61 size_t GetMinStreamFrameSize(QuicVersion version); 62 63 // Returns QuicConfig set to default values. 64 QuicConfig DefaultQuicConfig(); 65 66 template<typename SaveType> 67 class ValueRestore { 68 public: 69 ValueRestore(SaveType* name, SaveType value) 70 : name_(name), 71 value_(*name) { 72 *name_ = value; 73 } 74 ~ValueRestore() { 75 *name_ = value_; 76 } 77 78 private: 79 SaveType* name_; 80 SaveType value_; 81 82 DISALLOW_COPY_AND_ASSIGN(ValueRestore); 83 }; 84 85 class MockFramerVisitor : public QuicFramerVisitorInterface { 86 public: 87 MockFramerVisitor(); 88 ~MockFramerVisitor(); 89 90 MOCK_METHOD1(OnError, void(QuicFramer* framer)); 91 // The constructor sets this up to return false by default. 92 MOCK_METHOD1(OnProtocolVersionMismatch, bool(QuicVersion version)); 93 MOCK_METHOD0(OnPacket, void()); 94 MOCK_METHOD1(OnPublicResetPacket, void(const QuicPublicResetPacket& header)); 95 MOCK_METHOD1(OnVersionNegotiationPacket, 96 void(const QuicVersionNegotiationPacket& packet)); 97 MOCK_METHOD0(OnRevivedPacket, void()); 98 // The constructor sets this up to return true by default. 99 MOCK_METHOD1(OnUnauthenticatedHeader, bool(const QuicPacketHeader& header)); 100 MOCK_METHOD1(OnPacketHeader, bool(const QuicPacketHeader& header)); 101 MOCK_METHOD1(OnFecProtectedPayload, void(base::StringPiece payload)); 102 MOCK_METHOD1(OnStreamFrame, bool(const QuicStreamFrame& frame)); 103 MOCK_METHOD1(OnAckFrame, bool(const QuicAckFrame& frame)); 104 MOCK_METHOD1(OnCongestionFeedbackFrame, 105 bool(const QuicCongestionFeedbackFrame& frame)); 106 MOCK_METHOD1(OnFecData, void(const QuicFecData& fec)); 107 MOCK_METHOD1(OnRstStreamFrame, bool(const QuicRstStreamFrame& frame)); 108 MOCK_METHOD1(OnConnectionCloseFrame, 109 bool(const QuicConnectionCloseFrame& frame)); 110 MOCK_METHOD1(OnGoAwayFrame, bool(const QuicGoAwayFrame& frame)); 111 MOCK_METHOD0(OnPacketComplete, void()); 112 113 private: 114 DISALLOW_COPY_AND_ASSIGN(MockFramerVisitor); 115 }; 116 117 class NoOpFramerVisitor : public QuicFramerVisitorInterface { 118 public: 119 NoOpFramerVisitor() {} 120 121 virtual void OnError(QuicFramer* framer) OVERRIDE {} 122 virtual void OnPacket() OVERRIDE {} 123 virtual void OnPublicResetPacket( 124 const QuicPublicResetPacket& packet) OVERRIDE {} 125 virtual void OnVersionNegotiationPacket( 126 const QuicVersionNegotiationPacket& packet) OVERRIDE {} 127 virtual void OnRevivedPacket() OVERRIDE {} 128 virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE; 129 virtual bool OnUnauthenticatedHeader(const QuicPacketHeader& header) OVERRIDE; 130 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE; 131 virtual void OnFecProtectedPayload(base::StringPiece payload) OVERRIDE {} 132 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE; 133 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE; 134 virtual bool OnCongestionFeedbackFrame( 135 const QuicCongestionFeedbackFrame& frame) OVERRIDE; 136 virtual void OnFecData(const QuicFecData& fec) OVERRIDE {} 137 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE; 138 virtual bool OnConnectionCloseFrame( 139 const QuicConnectionCloseFrame& frame) OVERRIDE; 140 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE; 141 virtual void OnPacketComplete() OVERRIDE {} 142 143 private: 144 DISALLOW_COPY_AND_ASSIGN(NoOpFramerVisitor); 145 }; 146 147 class FramerVisitorCapturingPublicReset : public NoOpFramerVisitor { 148 public: 149 FramerVisitorCapturingPublicReset(); 150 virtual ~FramerVisitorCapturingPublicReset(); 151 152 virtual void OnPublicResetPacket( 153 const QuicPublicResetPacket& packet) OVERRIDE; 154 155 const QuicPublicResetPacket public_reset_packet() { 156 return public_reset_packet_; 157 } 158 159 private: 160 QuicPublicResetPacket public_reset_packet_; 161 }; 162 163 class FramerVisitorCapturingFrames : public NoOpFramerVisitor { 164 public: 165 FramerVisitorCapturingFrames(); 166 virtual ~FramerVisitorCapturingFrames(); 167 168 // Reset the visitor to it's initial state. 169 void Reset(); 170 171 // NoOpFramerVisitor 172 virtual void OnVersionNegotiationPacket( 173 const QuicVersionNegotiationPacket& packet) OVERRIDE; 174 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE; 175 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE; 176 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE; 177 virtual bool OnCongestionFeedbackFrame( 178 const QuicCongestionFeedbackFrame& frame) OVERRIDE; 179 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE; 180 virtual bool OnConnectionCloseFrame( 181 const QuicConnectionCloseFrame& frame) OVERRIDE; 182 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE; 183 184 size_t frame_count() const { return frame_count_; } 185 QuicPacketHeader* header() { return &header_; } 186 const std::vector<QuicStreamFrame>* stream_frames() const { 187 return &stream_frames_; 188 } 189 const std::vector<string*>& stream_data() const { 190 return stream_data_; 191 } 192 QuicAckFrame* ack() { return ack_.get(); } 193 QuicCongestionFeedbackFrame* feedback() { return feedback_.get(); } 194 QuicRstStreamFrame* rst() { return rst_.get(); } 195 QuicConnectionCloseFrame* close() { return close_.get(); } 196 QuicGoAwayFrame* goaway() { return goaway_.get(); } 197 QuicVersionNegotiationPacket* version_negotiation_packet() { 198 return version_negotiation_packet_.get(); 199 } 200 201 private: 202 size_t frame_count_; 203 QuicPacketHeader header_; 204 std::vector<QuicStreamFrame> stream_frames_; 205 std::vector<std::string*> stream_data_; 206 scoped_ptr<QuicAckFrame> ack_; 207 scoped_ptr<QuicCongestionFeedbackFrame> feedback_; 208 scoped_ptr<QuicRstStreamFrame> rst_; 209 scoped_ptr<QuicConnectionCloseFrame> close_; 210 scoped_ptr<QuicGoAwayFrame> goaway_; 211 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_; 212 213 DISALLOW_COPY_AND_ASSIGN(FramerVisitorCapturingFrames); 214 }; 215 216 class MockConnectionVisitor : public QuicConnectionVisitorInterface { 217 public: 218 MockConnectionVisitor(); 219 virtual ~MockConnectionVisitor(); 220 221 MOCK_METHOD1(OnStreamFrames, bool(const std::vector<QuicStreamFrame>& frame)); 222 MOCK_METHOD1(OnRstStream, void(const QuicRstStreamFrame& frame)); 223 MOCK_METHOD1(OnGoAway, void(const QuicGoAwayFrame& frame)); 224 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer)); 225 MOCK_METHOD0(OnCanWrite, bool()); 226 MOCK_CONST_METHOD0(HasPendingHandshake, bool()); 227 MOCK_METHOD1(OnSuccessfulVersionNegotiation, 228 void(const QuicVersion& version)); 229 MOCK_METHOD0(OnConfigNegotiated, void()); 230 231 private: 232 DISALLOW_COPY_AND_ASSIGN(MockConnectionVisitor); 233 }; 234 235 class MockHelper : public QuicConnectionHelperInterface { 236 public: 237 MockHelper(); 238 virtual ~MockHelper(); 239 240 MOCK_METHOD1(SetConnection, void(QuicConnection* connection)); 241 const QuicClock* GetClock() const; 242 QuicRandom* GetRandomGenerator(); 243 void AdvanceTime(QuicTime::Delta delta); 244 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate); 245 246 private: 247 MockClock clock_; 248 MockRandom random_generator_; 249 }; 250 251 class MockConnection : public QuicConnection { 252 public: 253 // Uses a MockHelper, GUID of 42, and 127.0.0.1:123. 254 explicit MockConnection(bool is_server); 255 256 // Uses a MockHelper, GUID of 42. 257 MockConnection(IPEndPoint address, 258 bool is_server); 259 260 // Uses a MockHelper, and 127.0.0.1:123 261 MockConnection(QuicGuid guid, 262 bool is_server); 263 264 virtual ~MockConnection(); 265 266 // If the constructor that uses a MockHelper has been used then this method 267 // will advance the time of the MockClock. 268 void AdvanceTime(QuicTime::Delta delta); 269 270 MOCK_METHOD3(ProcessUdpPacket, void(const IPEndPoint& self_address, 271 const IPEndPoint& peer_address, 272 const QuicEncryptedPacket& packet)); 273 MOCK_METHOD1(SendConnectionClose, void(QuicErrorCode error)); 274 MOCK_METHOD2(SendConnectionCloseWithDetails, void(QuicErrorCode error, 275 const string& details)); 276 MOCK_METHOD2(SendRstStream, void(QuicStreamId id, 277 QuicRstStreamErrorCode error)); 278 MOCK_METHOD3(SendGoAway, void(QuicErrorCode error, 279 QuicStreamId last_good_stream_id, 280 const string& reason)); 281 MOCK_METHOD0(OnCanWrite, bool()); 282 MOCK_CONST_METHOD0(HasPendingHandshake, bool()); 283 284 void ProcessUdpPacketInternal(const IPEndPoint& self_address, 285 const IPEndPoint& peer_address, 286 const QuicEncryptedPacket& packet) { 287 QuicConnection::ProcessUdpPacket(self_address, peer_address, packet); 288 } 289 290 virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE { 291 return false; 292 } 293 294 private: 295 scoped_ptr<QuicPacketWriter> writer_; 296 scoped_ptr<QuicConnectionHelperInterface> helper_; 297 298 DISALLOW_COPY_AND_ASSIGN(MockConnection); 299 }; 300 301 class PacketSavingConnection : public MockConnection { 302 public: 303 explicit PacketSavingConnection(bool is_server); 304 virtual ~PacketSavingConnection(); 305 306 virtual bool SendOrQueuePacket(EncryptionLevel level, 307 const SerializedPacket& packet, 308 TransmissionType transmission_type) OVERRIDE; 309 310 std::vector<QuicPacket*> packets_; 311 std::vector<QuicEncryptedPacket*> encrypted_packets_; 312 313 private: 314 DISALLOW_COPY_AND_ASSIGN(PacketSavingConnection); 315 }; 316 317 class MockSession : public QuicSession { 318 public: 319 explicit MockSession(QuicConnection* connection); 320 virtual ~MockSession(); 321 322 MOCK_METHOD4(OnPacket, bool(const IPEndPoint& self_address, 323 const IPEndPoint& peer_address, 324 const QuicPacketHeader& header, 325 const std::vector<QuicStreamFrame>& frame)); 326 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer)); 327 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); 328 MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*()); 329 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); 330 MOCK_METHOD6(WritevData, 331 QuicConsumedData(QuicStreamId id, 332 const struct iovec* iov, 333 int count, 334 QuicStreamOffset offset, 335 bool fin, 336 QuicAckNotifier::DelegateInterface*)); 337 MOCK_METHOD0(IsHandshakeComplete, bool()); 338 MOCK_METHOD0(IsCryptoHandshakeConfirmed, bool()); 339 340 private: 341 DISALLOW_COPY_AND_ASSIGN(MockSession); 342 }; 343 344 class TestSession : public QuicSession { 345 public: 346 TestSession(QuicConnection* connection, const QuicConfig& config); 347 virtual ~TestSession(); 348 349 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); 350 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); 351 352 void SetCryptoStream(QuicCryptoStream* stream); 353 354 virtual QuicCryptoStream* GetCryptoStream(); 355 356 private: 357 QuicCryptoStream* crypto_stream_; 358 DISALLOW_COPY_AND_ASSIGN(TestSession); 359 }; 360 361 class MockPacketWriter : public QuicPacketWriter { 362 public: 363 MockPacketWriter(); 364 virtual ~MockPacketWriter(); 365 366 MOCK_METHOD5(WritePacket, 367 WriteResult(const char* buffer, 368 size_t buf_len, 369 const IPAddressNumber& self_address, 370 const IPEndPoint& peer_address, 371 QuicBlockedWriterInterface* blocked_writer)); 372 MOCK_CONST_METHOD0(IsWriteBlockedDataBuffered, bool()); 373 }; 374 375 class MockSendAlgorithm : public SendAlgorithmInterface { 376 public: 377 MockSendAlgorithm(); 378 virtual ~MockSendAlgorithm(); 379 380 MOCK_METHOD2(SetFromConfig, void(const QuicConfig& config, bool is_server)); 381 MOCK_METHOD1(SetMaxPacketSize, void(QuicByteCount max_packet_size)); 382 MOCK_METHOD3(OnIncomingQuicCongestionFeedbackFrame, 383 void(const QuicCongestionFeedbackFrame&, 384 QuicTime feedback_receive_time, 385 const SentPacketsMap&)); 386 MOCK_METHOD3(OnPacketAcked, 387 void(QuicPacketSequenceNumber, QuicByteCount, QuicTime::Delta)); 388 MOCK_METHOD2(OnPacketLost, void(QuicPacketSequenceNumber, QuicTime)); 389 MOCK_METHOD5(OnPacketSent, 390 bool(QuicTime sent_time, QuicPacketSequenceNumber, QuicByteCount, 391 TransmissionType, HasRetransmittableData)); 392 MOCK_METHOD0(OnRetransmissionTimeout, void()); 393 MOCK_METHOD2(OnPacketAbandoned, void(QuicPacketSequenceNumber sequence_number, 394 QuicByteCount abandoned_bytes)); 395 MOCK_METHOD4(TimeUntilSend, QuicTime::Delta(QuicTime now, TransmissionType, 396 HasRetransmittableData, 397 IsHandshake)); 398 MOCK_CONST_METHOD0(BandwidthEstimate, QuicBandwidth(void)); 399 MOCK_CONST_METHOD0(SmoothedRtt, QuicTime::Delta(void)); 400 MOCK_CONST_METHOD0(RetransmissionDelay, QuicTime::Delta(void)); 401 MOCK_CONST_METHOD0(GetCongestionWindow, QuicByteCount()); 402 403 private: 404 DISALLOW_COPY_AND_ASSIGN(MockSendAlgorithm); 405 }; 406 407 class TestEntropyCalculator : 408 public QuicReceivedEntropyHashCalculatorInterface { 409 public: 410 TestEntropyCalculator(); 411 virtual ~TestEntropyCalculator(); 412 413 virtual QuicPacketEntropyHash EntropyHash( 414 QuicPacketSequenceNumber sequence_number) const OVERRIDE; 415 }; 416 417 class MockEntropyCalculator : public TestEntropyCalculator { 418 public: 419 MockEntropyCalculator(); 420 virtual ~MockEntropyCalculator(); 421 422 MOCK_CONST_METHOD1( 423 EntropyHash, 424 QuicPacketEntropyHash(QuicPacketSequenceNumber sequence_number)); 425 }; 426 427 class TestDecompressorVisitor : public QuicSpdyDecompressor::Visitor { 428 public: 429 virtual ~TestDecompressorVisitor() {} 430 virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE; 431 virtual void OnDecompressionError() OVERRIDE; 432 433 string data() { return data_; } 434 bool error() { return error_; } 435 436 private: 437 string data_; 438 bool error_; 439 }; 440 441 class MockAckNotifierDelegate : public QuicAckNotifier::DelegateInterface { 442 public: 443 MockAckNotifierDelegate(); 444 virtual ~MockAckNotifierDelegate(); 445 446 MOCK_METHOD0(OnAckNotification, void()); 447 }; 448 449 } // namespace test 450 } // namespace net 451 452 #endif // NET_QUIC_TEST_TOOLS_QUIC_TEST_UTILS_H_ 453