blob: 32b29be2a4f79f545bd49c6c28fa633dad8ea532 [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
26// ParsedQuartcDataFrame with a receive_time.
27struct ReceivedMessage {
28 ParsedQuartcDataFrame frame;
29 QuicTime receive_time = QuicTime::Zero();
30};
31
32// Test utility that adapts QuartcDataSources to a QuartcSession.
33// The utility creates and manages a set of QuartcDataSources. It sends the
34// data produced by those sources as QUIC datagram frames. It reconfigures the
35// maximum frame size of each source in order to fit test frames into QUIC
36// datagram frames. It also adjusts the bitrate of each source to fit within
37// the bandwidth available to the session.
38class QuartcPeer : public QuartcEndpoint::Delegate,
39 public QuartcSession::Delegate,
40 public QuartcDataSource::Delegate {
41 public:
42 // Creates a QuartcPeer that sends data from a set of sources described by
43 // |configs|. Note that the max frame size of each config may be adjusted in
44 // order to fit within the constraints of the QUIC session.
45 QuartcPeer(const QuicClock* clock,
46 QuicAlarmFactory* alarm_factory,
47 QuicRandom* random,
48 const std::vector<QuartcDataSource::Config>& configs);
49 QuartcPeer(QuartcPeer&) = delete;
50 QuartcPeer& operator=(QuartcPeer&) = delete;
51
52 ~QuartcPeer();
53
QUICHE teambd747462019-04-16 14:29:38 -070054 // Enable or disable this peer. Disabling a peer causes it to stop sending
55 // messages (which may be useful for flushing data during tests).
56 // A peer begins disabled. It automatically enables itself as soon as its
57 // session becomes writable, and disables itself when its session closes.
58 bool Enabled() const { return enabled_; }
59 void SetEnabled(bool value);
60
QUICHE team1ece5262019-04-16 14:25:51 -070061 // Messages received from the peer, in the order they were received.
62 const std::vector<ReceivedMessage>& received_messages() const {
63 return received_messages_;
64 }
65
QUICHE teambd747462019-04-16 14:29:38 -070066 // Returns a map of source id to the sequence number of the last frame
67 // produced by that source.
68 std::map<int32_t, int64_t> GetLastSequenceNumbers() const;
69
QUICHE team1ece5262019-04-16 14:25:51 -070070 // QuartcEndpoint::Delegate overrides.
71 void OnSessionCreated(QuartcSession* session) override;
72 void OnConnectError(QuicErrorCode error,
73 const std::string& error_details) override;
74
75 // QuartcSession::Delegate overrides.
76 void OnCryptoHandshakeComplete() override;
77 void OnConnectionWritable() override;
78 void OnIncomingStream(QuartcStream* stream) override;
79 void OnCongestionControlChange(QuicBandwidth bandwidth_estimate,
80 QuicBandwidth pacing_rate,
81 QuicTime::Delta latest_rtt) override;
82 void OnConnectionClosed(QuicErrorCode error_code,
83 const std::string& error_details,
84 ConnectionCloseSource source) override;
85 void OnMessageReceived(QuicStringPiece message) override;
86
87 // QuartcDataSource::Delegate overrides.
88 void OnDataProduced(const char* data, size_t length) override;
89
90 private:
91 const QuicClock* clock_;
92 QuicAlarmFactory* alarm_factory_;
93 QuicRandom* random_;
94
QUICHE teambd747462019-04-16 14:29:38 -070095 // Whether the peer is currently sending.
96 bool enabled_;
97
QUICHE team1ece5262019-04-16 14:25:51 -070098 // Session used for sending and receiving data. Not owned. Created by an
99 // external QuartcEndpoint and set in the |OnSessionCreated| callback.
100 QuartcSession* session_;
101
102 // Saved copy of the configs for data sources. These configs may be modified
103 // before |data_sources_| are initialized (for example, to set appropriate
104 // max frame sizes).
105 std::vector<QuartcDataSource::Config> configs_;
106
107 // Data sources are initialized once the session is created and enabled once
108 // the session is able to send.
109 std::vector<std::unique_ptr<QuartcDataSource>> data_sources_;
110
111 // Messages received by this peer from the remote peer. Stored in the order
112 // they are received.
113 std::vector<ReceivedMessage> received_messages_;
114};
115
116} // namespace test
117} // namespace quic
118
119#endif // QUICHE_QUIC_QUARTC_FAKE_QUARTC_PEER_H_