|  | // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 
|  | // Use of this source code is governed by a BSD-style license that can be | 
|  | // found in the LICENSE file. | 
|  |  | 
|  | #include "quiche/quic/test_tools/simple_data_producer.h" | 
|  |  | 
|  | #include <memory> | 
|  | #include <string> | 
|  | #include <utility> | 
|  |  | 
|  | #include "absl/strings/string_view.h" | 
|  | #include "quiche/quic/core/quic_data_writer.h" | 
|  | #include "quiche/quic/platform/api/quic_bug_tracker.h" | 
|  | #include "quiche/quic/platform/api/quic_flags.h" | 
|  |  | 
|  | namespace quic { | 
|  |  | 
|  | namespace test { | 
|  |  | 
|  | SimpleDataProducer::SimpleDataProducer() {} | 
|  |  | 
|  | SimpleDataProducer::~SimpleDataProducer() {} | 
|  |  | 
|  | void SimpleDataProducer::SaveStreamData(QuicStreamId id, | 
|  | absl::string_view data) { | 
|  | if (data.empty()) { | 
|  | return; | 
|  | } | 
|  | if (!send_buffer_map_.contains(id)) { | 
|  | send_buffer_map_[id] = std::make_unique<QuicStreamSendBuffer>(&allocator_); | 
|  | } | 
|  | send_buffer_map_[id]->SaveStreamData(data); | 
|  | } | 
|  |  | 
|  | void SimpleDataProducer::SaveCryptoData(EncryptionLevel level, | 
|  | QuicStreamOffset offset, | 
|  | absl::string_view data) { | 
|  | auto key = std::make_pair(level, offset); | 
|  | crypto_buffer_map_[key] = std::string(data); | 
|  | } | 
|  |  | 
|  | WriteStreamDataResult SimpleDataProducer::WriteStreamData( | 
|  | QuicStreamId id, QuicStreamOffset offset, QuicByteCount data_length, | 
|  | QuicDataWriter* writer) { | 
|  | auto iter = send_buffer_map_.find(id); | 
|  | if (iter == send_buffer_map_.end()) { | 
|  | return STREAM_MISSING; | 
|  | } | 
|  | if (iter->second->WriteStreamData(offset, data_length, writer)) { | 
|  | return WRITE_SUCCESS; | 
|  | } | 
|  | return WRITE_FAILED; | 
|  | } | 
|  |  | 
|  | bool SimpleDataProducer::WriteCryptoData(EncryptionLevel level, | 
|  | QuicStreamOffset offset, | 
|  | QuicByteCount data_length, | 
|  | QuicDataWriter* writer) { | 
|  | auto it = crypto_buffer_map_.find(std::make_pair(level, offset)); | 
|  | if (it == crypto_buffer_map_.end() || it->second.length() < data_length) { | 
|  | // If we fail to find an exact match for the offset, try to find a | 
|  | // superset entry that covers the requested range. | 
|  | for (auto& entry : crypto_buffer_map_) { | 
|  | if (entry.first.first == level && entry.first.second <= offset && | 
|  | offset + data_length <= entry.first.second + entry.second.length()) { | 
|  | return writer->WriteStringPiece(absl::string_view( | 
|  | entry.second.data() + (offset - entry.first.second), data_length)); | 
|  | } | 
|  | } | 
|  | return false; | 
|  | } | 
|  | return writer->WriteStringPiece( | 
|  | absl::string_view(it->second.data(), data_length)); | 
|  | } | 
|  |  | 
|  | }  // namespace test | 
|  |  | 
|  | }  // namespace quic |