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 QUICHE_QUIC_TEST_TOOLS_SIMULATOR_QUIC_ENDPOINT_H_ 6 #define QUICHE_QUIC_TEST_TOOLS_SIMULATOR_QUIC_ENDPOINT_H_ 7 8 #include "absl/strings/string_view.h" 9 #include "quiche/quic/core/crypto/null_decrypter.h" 10 #include "quiche/quic/core/crypto/null_encrypter.h" 11 #include "quiche/quic/core/quic_connection.h" 12 #include "quiche/quic/core/quic_packet_writer.h" 13 #include "quiche/quic/core/quic_packets.h" 14 #include "quiche/quic/core/quic_stream_frame_data_producer.h" 15 #include "quiche/quic/core/quic_trace_visitor.h" 16 #include "quiche/quic/test_tools/simple_session_notifier.h" 17 #include "quiche/quic/test_tools/simulator/link.h" 18 #include "quiche/quic/test_tools/simulator/queue.h" 19 #include "quiche/quic/test_tools/simulator/quic_endpoint_base.h" 20 21 namespace quic { 22 namespace simulator { 23 24 // A QUIC connection endpoint. Wraps around QuicConnection. In order to 25 // initiate a transfer, the caller has to call AddBytesToTransfer(). The data 26 // transferred is always the same and is always transferred on a single stream. 27 // The endpoint receives all packets addressed to it, and verifies that the data 28 // received is what it's supposed to be. 29 class QuicEndpoint : public QuicEndpointBase, 30 public QuicConnectionVisitorInterface, 31 public SessionNotifierInterface { 32 public: 33 QuicEndpoint(Simulator* simulator, std::string name, std::string peer_name, 34 Perspective perspective, QuicConnectionId connection_id); 35 36 QuicByteCount bytes_to_transfer() const; 37 QuicByteCount bytes_transferred() const; 38 QuicByteCount bytes_received() const; wrong_data_received()39 bool wrong_data_received() const { return wrong_data_received_; } 40 41 // Send |bytes| bytes. Initiates the transfer if one is not already in 42 // progress. 43 void AddBytesToTransfer(QuicByteCount bytes); 44 45 // Begin QuicConnectionVisitorInterface implementation. 46 void OnStreamFrame(const QuicStreamFrame& frame) override; 47 void OnCryptoFrame(const QuicCryptoFrame& frame) override; 48 void OnCanWrite() override; 49 bool WillingAndAbleToWrite() const override; 50 bool ShouldKeepConnectionAlive() const override; 51 GetStreamsInfoForLogging()52 std::string GetStreamsInfoForLogging() const override { return ""; } OnWindowUpdateFrame(const QuicWindowUpdateFrame &)53 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& /*frame*/) override {} OnBlockedFrame(const QuicBlockedFrame &)54 void OnBlockedFrame(const QuicBlockedFrame& /*frame*/) override {} OnRstStream(const QuicRstStreamFrame &)55 void OnRstStream(const QuicRstStreamFrame& /*frame*/) override {} OnGoAway(const QuicGoAwayFrame &)56 void OnGoAway(const QuicGoAwayFrame& /*frame*/) override {} OnMessageReceived(absl::string_view)57 void OnMessageReceived(absl::string_view /*message*/) override {} OnHandshakeDoneReceived()58 void OnHandshakeDoneReceived() override {} OnNewTokenReceived(absl::string_view)59 void OnNewTokenReceived(absl::string_view /*token*/) override {} OnConnectionClosed(const QuicConnectionCloseFrame &,ConnectionCloseSource)60 void OnConnectionClosed(const QuicConnectionCloseFrame& /*frame*/, 61 ConnectionCloseSource /*source*/) override {} OnWriteBlocked()62 void OnWriteBlocked() override {} OnSuccessfulVersionNegotiation(const ParsedQuicVersion &)63 void OnSuccessfulVersionNegotiation( 64 const ParsedQuicVersion& /*version*/) override {} OnPacketReceived(const QuicSocketAddress &,const QuicSocketAddress &,bool)65 void OnPacketReceived(const QuicSocketAddress& /*self_address*/, 66 const QuicSocketAddress& /*peer_address*/, 67 bool /*is_connectivity_probe*/) override {} OnCongestionWindowChange(QuicTime)68 void OnCongestionWindowChange(QuicTime /*now*/) override {} OnConnectionMigration(AddressChangeType)69 void OnConnectionMigration(AddressChangeType /*type*/) override {} OnPathDegrading()70 void OnPathDegrading() override {} OnForwardProgressMadeAfterPathDegrading()71 void OnForwardProgressMadeAfterPathDegrading() override {} OnAckNeedsRetransmittableFrame()72 void OnAckNeedsRetransmittableFrame() override {} SendAckFrequency(const QuicAckFrequencyFrame &)73 void SendAckFrequency(const QuicAckFrequencyFrame& /*frame*/) override {} SendNewConnectionId(const QuicNewConnectionIdFrame &)74 void SendNewConnectionId(const QuicNewConnectionIdFrame& /*frame*/) override { 75 } SendRetireConnectionId(uint64_t)76 void SendRetireConnectionId(uint64_t /*sequence_number*/) override {} MaybeReserveConnectionId(const QuicConnectionId &)77 bool MaybeReserveConnectionId( 78 const QuicConnectionId& /*server_connection_id*/) override { 79 return true; 80 } OnServerConnectionIdRetired(const QuicConnectionId &)81 void OnServerConnectionIdRetired( 82 const QuicConnectionId& /*server_connection_id*/) override {} 83 bool AllowSelfAddressChange() const override; 84 HandshakeState GetHandshakeState() const override; OnMaxStreamsFrame(const QuicMaxStreamsFrame &)85 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& /*frame*/) override { 86 return true; 87 } OnStreamsBlockedFrame(const QuicStreamsBlockedFrame &)88 bool OnStreamsBlockedFrame( 89 const QuicStreamsBlockedFrame& /*frame*/) override { 90 return true; 91 } OnStopSendingFrame(const QuicStopSendingFrame &)92 void OnStopSendingFrame(const QuicStopSendingFrame& /*frame*/) override {} OnPacketDecrypted(EncryptionLevel)93 void OnPacketDecrypted(EncryptionLevel /*level*/) override {} OnOneRttPacketAcknowledged()94 void OnOneRttPacketAcknowledged() override {} OnHandshakePacketSent()95 void OnHandshakePacketSent() override {} OnKeyUpdate(KeyUpdateReason)96 void OnKeyUpdate(KeyUpdateReason /*reason*/) override {} AdvanceKeysAndCreateCurrentOneRttDecrypter()97 std::unique_ptr<QuicDecrypter> AdvanceKeysAndCreateCurrentOneRttDecrypter() 98 override { 99 return nullptr; 100 } CreateCurrentOneRttEncrypter()101 std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter() override { 102 return nullptr; 103 } BeforeConnectionCloseSent()104 void BeforeConnectionCloseSent() override {} ValidateToken(absl::string_view)105 bool ValidateToken(absl::string_view /*token*/) override { return true; } MaybeSendAddressToken()106 bool MaybeSendAddressToken() override { return false; } CreateContextForMultiPortPath(std::unique_ptr<MultiPortPathContextObserver>)107 void CreateContextForMultiPortPath( 108 std::unique_ptr<MultiPortPathContextObserver> /*context_observer*/) 109 override {} MigrateToMultiPortPath(std::unique_ptr<QuicPathValidationContext>)110 void MigrateToMultiPortPath( 111 std::unique_ptr<QuicPathValidationContext> /*context*/) override {} OnServerPreferredAddressAvailable(const QuicSocketAddress &)112 void OnServerPreferredAddressAvailable( 113 const QuicSocketAddress& /*server_preferred_address*/) override {} MaybeBundleOpportunistically()114 void MaybeBundleOpportunistically() override {} GetFlowControlSendWindowSize(QuicStreamId)115 QuicByteCount GetFlowControlSendWindowSize(QuicStreamId /*id*/) override { 116 return std::numeric_limits<QuicByteCount>::max(); 117 } 118 119 // End QuicConnectionVisitorInterface implementation. 120 121 // Begin SessionNotifierInterface methods: 122 bool OnFrameAcked(const QuicFrame& frame, QuicTime::Delta ack_delay_time, 123 QuicTime receive_timestamp) override; OnStreamFrameRetransmitted(const QuicStreamFrame &)124 void OnStreamFrameRetransmitted(const QuicStreamFrame& /*frame*/) override {} 125 void OnFrameLost(const QuicFrame& frame) override; 126 bool RetransmitFrames(const QuicFrames& frames, 127 TransmissionType type) override; 128 bool IsFrameOutstanding(const QuicFrame& frame) const override; 129 bool HasUnackedCryptoData() const override; 130 bool HasUnackedStreamData() const override; 131 // End SessionNotifierInterface implementation. 132 133 private: 134 // The producer outputs the repetition of the same byte. That sequence is 135 // verified by the receiver. 136 class DataProducer : public QuicStreamFrameDataProducer { 137 public: 138 WriteStreamDataResult WriteStreamData(QuicStreamId id, 139 QuicStreamOffset offset, 140 QuicByteCount data_length, 141 QuicDataWriter* writer) override; 142 bool WriteCryptoData(EncryptionLevel level, QuicStreamOffset offset, 143 QuicByteCount data_length, 144 QuicDataWriter* writer) override; 145 }; 146 147 std::unique_ptr<QuicConnection> CreateConnection( 148 Simulator* simulator, std::string name, std::string peer_name, 149 Perspective perspective, QuicConnectionId connection_id); 150 151 // Write stream data until |bytes_to_transfer_| is zero or the connection is 152 // write-blocked. 153 void WriteStreamData(); 154 155 DataProducer producer_; 156 157 QuicByteCount bytes_to_transfer_; 158 QuicByteCount bytes_transferred_; 159 160 // Set to true if the endpoint receives stream data different from what it 161 // expects. 162 bool wrong_data_received_; 163 164 // Record of received offsets in the data stream. 165 QuicIntervalSet<QuicStreamOffset> offsets_received_; 166 167 std::unique_ptr<test::SimpleSessionNotifier> notifier_; 168 }; 169 170 } // namespace simulator 171 } // namespace quic 172 173 #endif // QUICHE_QUIC_TEST_TOOLS_SIMULATOR_QUIC_ENDPOINT_H_ 174