QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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_QUARTC_TEST_QUARTC_PEER_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_TEST_QUARTC_PEER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_alarm_factory.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_bandwidth.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_time.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 18 | #include "net/third_party/quiche/src/quic/quartc/quartc_endpoint.h" |
| 19 | #include "net/third_party/quiche/src/quic/quartc/quartc_session.h" |
| 20 | #include "net/third_party/quiche/src/quic/quartc/quartc_stream.h" |
| 21 | #include "net/third_party/quiche/src/quic/quartc/test/quartc_data_source.h" |
| 22 | |
| 23 | namespace quic { |
| 24 | namespace test { |
| 25 | |
QUICHE team | 500e0aa | 2019-04-16 14:30:01 -0700 | [diff] [blame] | 26 | // Map of source id to sequence number. |
| 27 | using IdToSequenceNumberMap = std::map<int32_t, int64_t>; |
| 28 | |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 29 | // ParsedQuartcDataFrame with a receive_time. |
| 30 | struct ReceivedMessage { |
| 31 | ParsedQuartcDataFrame frame; |
| 32 | QuicTime receive_time = QuicTime::Zero(); |
| 33 | }; |
| 34 | |
| 35 | // Test utility that adapts QuartcDataSources to a QuartcSession. |
| 36 | // The utility creates and manages a set of QuartcDataSources. It sends the |
| 37 | // data produced by those sources as QUIC datagram frames. It reconfigures the |
| 38 | // maximum frame size of each source in order to fit test frames into QUIC |
| 39 | // datagram frames. It also adjusts the bitrate of each source to fit within |
| 40 | // the bandwidth available to the session. |
| 41 | class QuartcPeer : public QuartcEndpoint::Delegate, |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 42 | public QuartcDataSource::Delegate { |
| 43 | public: |
| 44 | // Creates a QuartcPeer that sends data from a set of sources described by |
| 45 | // |configs|. Note that the max frame size of each config may be adjusted in |
| 46 | // order to fit within the constraints of the QUIC session. |
| 47 | QuartcPeer(const QuicClock* clock, |
| 48 | QuicAlarmFactory* alarm_factory, |
| 49 | QuicRandom* random, |
QUICHE team | 7eef071 | 2019-05-09 09:42:38 -0700 | [diff] [blame] | 50 | QuicBufferAllocator* buffer_allocator, |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 51 | const std::vector<QuartcDataSource::Config>& configs); |
| 52 | QuartcPeer(QuartcPeer&) = delete; |
| 53 | QuartcPeer& operator=(QuartcPeer&) = delete; |
| 54 | |
| 55 | ~QuartcPeer(); |
| 56 | |
QUICHE team | bd74746 | 2019-04-16 14:29:38 -0700 | [diff] [blame] | 57 | // Enable or disable this peer. Disabling a peer causes it to stop sending |
| 58 | // messages (which may be useful for flushing data during tests). |
| 59 | // A peer begins disabled. It automatically enables itself as soon as its |
| 60 | // session becomes writable, and disables itself when its session closes. |
| 61 | bool Enabled() const { return enabled_; } |
| 62 | void SetEnabled(bool value); |
| 63 | |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 64 | // Messages received from the peer, in the order they were received. |
| 65 | const std::vector<ReceivedMessage>& received_messages() const { |
| 66 | return received_messages_; |
| 67 | } |
| 68 | |
QUICHE team | bd74746 | 2019-04-16 14:29:38 -0700 | [diff] [blame] | 69 | // Returns a map of source id to the sequence number of the last frame |
| 70 | // produced by that source. |
QUICHE team | 500e0aa | 2019-04-16 14:30:01 -0700 | [diff] [blame] | 71 | IdToSequenceNumberMap GetLastSequenceNumbers() const; |
QUICHE team | bd74746 | 2019-04-16 14:29:38 -0700 | [diff] [blame] | 72 | |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 73 | // QuartcEndpoint::Delegate overrides. |
| 74 | void OnSessionCreated(QuartcSession* session) override; |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 75 | |
| 76 | // QuartcSession::Delegate overrides. |
| 77 | void OnCryptoHandshakeComplete() override; |
| 78 | void OnConnectionWritable() override; |
| 79 | void OnIncomingStream(QuartcStream* stream) override; |
| 80 | void OnCongestionControlChange(QuicBandwidth bandwidth_estimate, |
| 81 | QuicBandwidth pacing_rate, |
| 82 | QuicTime::Delta latest_rtt) override; |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 83 | void OnConnectionClosed(const QuicConnectionCloseFrame& frame, |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 84 | ConnectionCloseSource source) override; |
| 85 | void OnMessageReceived(QuicStringPiece message) override; |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 86 | void OnMessageSent(int64_t /*datagram_id*/) override {} |
| 87 | void OnMessageAcked(int64_t /*datagram_id*/, |
| 88 | QuicTime /*receive_timestamp*/) override {} |
| 89 | void OnMessageLost(int64_t /*datagram_id*/) override {} |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 90 | |
| 91 | // QuartcDataSource::Delegate overrides. |
| 92 | void OnDataProduced(const char* data, size_t length) override; |
| 93 | |
| 94 | private: |
| 95 | const QuicClock* clock_; |
| 96 | QuicAlarmFactory* alarm_factory_; |
| 97 | QuicRandom* random_; |
QUICHE team | 7eef071 | 2019-05-09 09:42:38 -0700 | [diff] [blame] | 98 | QuicBufferAllocator* buffer_allocator_; |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 99 | |
QUICHE team | bd74746 | 2019-04-16 14:29:38 -0700 | [diff] [blame] | 100 | // Whether the peer is currently sending. |
| 101 | bool enabled_; |
| 102 | |
QUICHE team | 1ece526 | 2019-04-16 14:25:51 -0700 | [diff] [blame] | 103 | // Session used for sending and receiving data. Not owned. Created by an |
| 104 | // external QuartcEndpoint and set in the |OnSessionCreated| callback. |
| 105 | QuartcSession* session_; |
| 106 | |
| 107 | // Saved copy of the configs for data sources. These configs may be modified |
| 108 | // before |data_sources_| are initialized (for example, to set appropriate |
| 109 | // max frame sizes). |
| 110 | std::vector<QuartcDataSource::Config> configs_; |
| 111 | |
| 112 | // Data sources are initialized once the session is created and enabled once |
| 113 | // the session is able to send. |
| 114 | std::vector<std::unique_ptr<QuartcDataSource>> data_sources_; |
| 115 | |
| 116 | // Messages received by this peer from the remote peer. Stored in the order |
| 117 | // they are received. |
| 118 | std::vector<ReceivedMessage> received_messages_; |
| 119 | }; |
| 120 | |
| 121 | } // namespace test |
| 122 | } // namespace quic |
| 123 | |
| 124 | #endif // QUICHE_QUIC_QUARTC_FAKE_QUARTC_PEER_H_ |