blob: d51ec69be701e1df61f8c971d721a4c1dd6a3b82 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h"
9#include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h"
10#include "net/third_party/quiche/src/quic/core/quic_stream_send_buffer.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
12
13namespace quic {
14
15namespace test {
16
17// A simple data producer which copies stream data into a map from stream
18// id to send buffer.
19class SimpleDataProducer : public QuicStreamFrameDataProducer {
20 public:
21 SimpleDataProducer();
22 ~SimpleDataProducer() override;
23
24 // Saves data to be provided when WriteStreamData is called. Data of length
25 // |data_length| is buffered to be provided for stream |id|. Multiple calls to
26 // SaveStreamData for the same stream ID append to the buffer for that stream.
27 // The data to be buffered is provided in |iov_count| iovec structs, with
28 // |iov| pointing to the first, and |iov_offset| indicating how many bytes
29 // into the iovec structs the data starts.
30 void SaveStreamData(QuicStreamId id,
31 const struct iovec* iov,
32 int iov_count,
33 size_t iov_offset,
34 QuicByteCount data_length);
35
36 void SaveCryptoData(EncryptionLevel level,
37 QuicStreamOffset offset,
38 QuicStringPiece data);
39
40 // QuicStreamFrameDataProducer
41 WriteStreamDataResult WriteStreamData(QuicStreamId id,
42 QuicStreamOffset offset,
43 QuicByteCount data_length,
44 QuicDataWriter* writer) override;
45 bool WriteCryptoData(EncryptionLevel level,
46 QuicStreamOffset offset,
47 QuicByteCount data_length,
48 QuicDataWriter* writer) override;
49
50 // TODO(wub): Allow QuicDefaultHasher to accept a pair. Then remove this.
51 class PairHash {
52 public:
53 template <class T1, class T2>
54 size_t operator()(const std::pair<T1, T2>& pair) const {
55 return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
56 }
57 };
58
59 private:
60 using SendBufferMap =
61 QuicUnorderedMap<QuicStreamId, std::unique_ptr<QuicStreamSendBuffer>>;
62
63 using CryptoBufferMap =
64 QuicUnorderedMap<std::pair<EncryptionLevel, QuicStreamOffset>,
65 QuicStringPiece,
66 PairHash>;
67
68 SimpleBufferAllocator allocator_;
69
70 SendBufferMap send_buffer_map_;
71
72 // |crypto_buffer_map_| stores data provided by SaveCryptoData to later write
73 // in WriteCryptoData. The level and data passed into SaveCryptoData are used
74 // as the key to identify the data when WriteCryptoData is called.
75 // WriteCryptoData will only succeed if there is data in the map for the
76 // provided level and offset, and the data in the map matches the data_length
77 // passed into WriteCryptoData.
78 //
79 // Unlike SaveStreamData/WriteStreamData which uses a map of
80 // QuicStreamSendBuffers (for each stream ID), this map provides data for
81 // specific offsets. Using a QuicStreamSendBuffer requires that all data
82 // before an offset exist, whereas this allows providing data that exists at
83 // arbitrary offsets for testing.
84 CryptoBufferMap crypto_buffer_map_;
85};
86
87} // namespace test
88
89} // namespace quic
90
91#endif // QUICHE_QUIC_TEST_TOOLS_SIMPLE_DATA_PRODUCER_H_