blob: 828b1e20db49d8d41e064a241550d5f5c3a98285 [file] [log] [blame]
QUICHE team1ece5262019-04-16 14:25:51 -07001// 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
23namespace quic {
24namespace test {
25
QUICHE team500e0aa2019-04-16 14:30:01 -070026// Map of source id to sequence number.
27using IdToSequenceNumberMap = std::map<int32_t, int64_t>;
28
QUICHE team1ece5262019-04-16 14:25:51 -070029// ParsedQuartcDataFrame with a receive_time.
30struct 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.
41class QuartcPeer : public QuartcEndpoint::Delegate,
QUICHE team1ece5262019-04-16 14:25:51 -070042 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 team7eef0712019-05-09 09:42:38 -070050 QuicBufferAllocator* buffer_allocator,
QUICHE team1ece5262019-04-16 14:25:51 -070051 const std::vector<QuartcDataSource::Config>& configs);
52 QuartcPeer(QuartcPeer&) = delete;
53 QuartcPeer& operator=(QuartcPeer&) = delete;
54
55 ~QuartcPeer();
56
QUICHE teambd747462019-04-16 14:29:38 -070057 // 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 team1ece5262019-04-16 14:25:51 -070064 // 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 teambd747462019-04-16 14:29:38 -070069 // Returns a map of source id to the sequence number of the last frame
70 // produced by that source.
QUICHE team500e0aa2019-04-16 14:30:01 -070071 IdToSequenceNumberMap GetLastSequenceNumbers() const;
QUICHE teambd747462019-04-16 14:29:38 -070072
QUICHE team1ece5262019-04-16 14:25:51 -070073 // QuartcEndpoint::Delegate overrides.
74 void OnSessionCreated(QuartcSession* session) override;
QUICHE team1ece5262019-04-16 14:25:51 -070075
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;
fkastenholz5d880a92019-06-21 09:01:56 -070083 void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
QUICHE team1ece5262019-04-16 14:25:51 -070084 ConnectionCloseSource source) override;
85 void OnMessageReceived(QuicStringPiece message) override;
dschinazi17d42422019-06-18 16:35:07 -070086 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 team1ece5262019-04-16 14:25:51 -070090
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 team7eef0712019-05-09 09:42:38 -070098 QuicBufferAllocator* buffer_allocator_;
QUICHE team1ece5262019-04-16 14:25:51 -070099
QUICHE teambd747462019-04-16 14:29:38 -0700100 // Whether the peer is currently sending.
101 bool enabled_;
102
QUICHE team1ece5262019-04-16 14:25:51 -0700103 // 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_