QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | #include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 12 | |
| 13 | namespace quic { |
| 14 | |
| 15 | namespace test { |
| 16 | |
| 17 | SimpleDataProducer::SimpleDataProducer() {} |
| 18 | |
| 19 | SimpleDataProducer::~SimpleDataProducer() {} |
| 20 | |
| 21 | void SimpleDataProducer::SaveStreamData(QuicStreamId id, |
| 22 | const struct iovec* iov, |
| 23 | int iov_count, |
| 24 | size_t iov_offset, |
| 25 | QuicByteCount data_length) { |
| 26 | if (data_length == 0) { |
| 27 | return; |
| 28 | } |
| 29 | if (!QuicContainsKey(send_buffer_map_, id)) { |
| 30 | send_buffer_map_[id] = QuicMakeUnique<QuicStreamSendBuffer>(&allocator_); |
| 31 | } |
| 32 | send_buffer_map_[id]->SaveStreamData(iov, iov_count, iov_offset, data_length); |
| 33 | } |
| 34 | |
| 35 | void SimpleDataProducer::SaveCryptoData(EncryptionLevel level, |
| 36 | QuicStreamOffset offset, |
| 37 | QuicStringPiece data) { |
| 38 | auto key = std::make_pair(level, offset); |
| 39 | crypto_buffer_map_[key] = data; |
| 40 | } |
| 41 | |
| 42 | WriteStreamDataResult SimpleDataProducer::WriteStreamData( |
| 43 | QuicStreamId id, |
| 44 | QuicStreamOffset offset, |
| 45 | QuicByteCount data_length, |
| 46 | QuicDataWriter* writer) { |
| 47 | auto iter = send_buffer_map_.find(id); |
| 48 | if (iter == send_buffer_map_.end()) { |
| 49 | return STREAM_MISSING; |
| 50 | } |
| 51 | if (iter->second->WriteStreamData(offset, data_length, writer)) { |
| 52 | return WRITE_SUCCESS; |
| 53 | } |
| 54 | return WRITE_FAILED; |
| 55 | } |
| 56 | |
| 57 | bool SimpleDataProducer::WriteCryptoData(EncryptionLevel level, |
| 58 | QuicStreamOffset offset, |
| 59 | QuicByteCount data_length, |
| 60 | QuicDataWriter* writer) { |
| 61 | auto it = crypto_buffer_map_.find(std::make_pair(level, offset)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 62 | if (it == crypto_buffer_map_.end() || it->second.length() < data_length) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | return false; |
| 64 | } |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 65 | return writer->WriteStringPiece( |
| 66 | QuicStringPiece(it->second.data(), data_length)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace test |
| 70 | |
| 71 | } // namespace quic |