QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 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/platform/api/quic_mem_slice_storage.h" |
QUICHE team | ea19735 | 2019-07-16 16:54:52 -0700 | [diff] [blame] | 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
QUICHE team | ea19735 | 2019-07-16 16:54:52 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_test_mem_slice_vector.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
| 11 | namespace quic { |
| 12 | namespace test { |
| 13 | namespace { |
| 14 | |
| 15 | class QuicMemSliceStorageImplTest : public QuicTest { |
| 16 | public: |
| 17 | QuicMemSliceStorageImplTest() = default; |
| 18 | }; |
| 19 | |
| 20 | TEST_F(QuicMemSliceStorageImplTest, EmptyIov) { |
| 21 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 1024); |
| 22 | EXPECT_TRUE(storage.ToSpan().empty()); |
| 23 | } |
| 24 | |
| 25 | TEST_F(QuicMemSliceStorageImplTest, SingleIov) { |
| 26 | SimpleBufferAllocator allocator; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 27 | std::string body(3, 'c'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | struct iovec iov = {const_cast<char*>(body.data()), body.length()}; |
| 29 | QuicMemSliceStorage storage(&iov, 1, &allocator, 1024); |
| 30 | auto span = storage.ToSpan(); |
| 31 | EXPECT_EQ("ccc", span.GetData(0)); |
| 32 | EXPECT_NE(static_cast<const void*>(span.GetData(0).data()), body.data()); |
| 33 | } |
| 34 | |
| 35 | TEST_F(QuicMemSliceStorageImplTest, MultipleIovInSingleSlice) { |
| 36 | SimpleBufferAllocator allocator; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 37 | std::string body1(3, 'a'); |
| 38 | std::string body2(4, 'b'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 39 | struct iovec iov[] = {{const_cast<char*>(body1.data()), body1.length()}, |
| 40 | {const_cast<char*>(body2.data()), body2.length()}}; |
| 41 | |
| 42 | QuicMemSliceStorage storage(iov, 2, &allocator, 1024); |
| 43 | auto span = storage.ToSpan(); |
| 44 | EXPECT_EQ("aaabbbb", span.GetData(0)); |
| 45 | } |
| 46 | |
| 47 | TEST_F(QuicMemSliceStorageImplTest, MultipleIovInMultipleSlice) { |
| 48 | SimpleBufferAllocator allocator; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 49 | std::string body1(4, 'a'); |
| 50 | std::string body2(4, 'b'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | struct iovec iov[] = {{const_cast<char*>(body1.data()), body1.length()}, |
| 52 | {const_cast<char*>(body2.data()), body2.length()}}; |
| 53 | |
| 54 | QuicMemSliceStorage storage(iov, 2, &allocator, 4); |
| 55 | auto span = storage.ToSpan(); |
| 56 | EXPECT_EQ("aaaa", span.GetData(0)); |
| 57 | EXPECT_EQ("bbbb", span.GetData(1)); |
| 58 | } |
| 59 | |
QUICHE team | ea19735 | 2019-07-16 16:54:52 -0700 | [diff] [blame] | 60 | TEST_F(QuicMemSliceStorageImplTest, AppendMemSlices) { |
| 61 | std::string body1(3, 'a'); |
| 62 | std::string body2(4, 'b'); |
| 63 | std::vector<std::pair<char*, size_t>> buffers; |
| 64 | buffers.push_back( |
| 65 | std::make_pair(const_cast<char*>(body1.data()), body1.length())); |
| 66 | buffers.push_back( |
| 67 | std::make_pair(const_cast<char*>(body2.data()), body2.length())); |
| 68 | QuicTestMemSliceVector mem_slices(buffers); |
| 69 | |
| 70 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 71 | mem_slices.span().ConsumeAll( |
| 72 | [&storage](QuicMemSlice slice) { storage.Append(std::move(slice)); }); |
| 73 | |
| 74 | EXPECT_EQ("aaa", storage.ToSpan().GetData(0)); |
| 75 | EXPECT_EQ("bbbb", storage.ToSpan().GetData(1)); |
| 76 | } |
| 77 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | } // namespace |
| 79 | } // namespace test |
| 80 | } // namespace quic |