xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/simple_data_producer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2017 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_SIMPLE_DATA_PRODUCER_H_
6 #define QUICHE_QUIC_TEST_TOOLS_SIMPLE_DATA_PRODUCER_H_
7 
8 #include "absl/container/flat_hash_map.h"
9 #include "absl/strings/string_view.h"
10 #include "quiche/quic/core/quic_stream_frame_data_producer.h"
11 #include "quiche/quic/core/quic_stream_send_buffer.h"
12 #include "quiche/common/simple_buffer_allocator.h"
13 
14 namespace quic {
15 
16 namespace test {
17 
18 // A simple data producer which copies stream data into a map from stream
19 // id to send buffer.
20 class SimpleDataProducer : public QuicStreamFrameDataProducer {
21  public:
22   SimpleDataProducer();
23   ~SimpleDataProducer() override;
24 
25   // Saves `data` to be provided when WriteStreamData() is called. Multiple
26   // calls to SaveStreamData() for the same stream ID append to the buffer for
27   // that stream.
28   void SaveStreamData(QuicStreamId id, absl::string_view data);
29 
30   void SaveCryptoData(EncryptionLevel level, QuicStreamOffset offset,
31                       absl::string_view data);
32 
33   // QuicStreamFrameDataProducer
34   WriteStreamDataResult WriteStreamData(QuicStreamId id,
35                                         QuicStreamOffset offset,
36                                         QuicByteCount data_length,
37                                         QuicDataWriter* writer) override;
38   bool WriteCryptoData(EncryptionLevel level, QuicStreamOffset offset,
39                        QuicByteCount data_length,
40                        QuicDataWriter* writer) override;
41 
42  private:
43   using SendBufferMap =
44       absl::flat_hash_map<QuicStreamId, std::unique_ptr<QuicStreamSendBuffer>>;
45 
46   using CryptoBufferMap =
47       absl::flat_hash_map<std::pair<EncryptionLevel, QuicStreamOffset>,
48                           std::string>;
49 
50   quiche::SimpleBufferAllocator allocator_;
51 
52   SendBufferMap send_buffer_map_;
53 
54   // |crypto_buffer_map_| stores data provided by SaveCryptoData to later write
55   // in WriteCryptoData. The level and data passed into SaveCryptoData are used
56   // as the key to identify the data when WriteCryptoData is called.
57   // WriteCryptoData will only succeed if there is data in the map for the
58   // provided level and offset, and the data in the map matches the data_length
59   // passed into WriteCryptoData.
60   //
61   // Unlike SaveStreamData/WriteStreamData which uses a map of
62   // QuicStreamSendBuffers (for each stream ID), this map provides data for
63   // specific offsets. Using a QuicStreamSendBuffer requires that all data
64   // before an offset exist, whereas this allows providing data that exists at
65   // arbitrary offsets for testing.
66   CryptoBufferMap crypto_buffer_map_;
67 };
68 
69 }  // namespace test
70 
71 }  // namespace quic
72 
73 #endif  // QUICHE_QUIC_TEST_TOOLS_SIMPLE_DATA_PRODUCER_H_
74