[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 5 | #include "net/spdy/spdy_buffer.h" |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 6 | |
| 7 | #include <cstddef> |
[email protected] | 08881f9a | 2013-07-12 19:30:31 | [diff] [blame] | 8 | #include <cstring> |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 9 | #include <string> |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 10 | #include <utility> |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 11 | |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 12 | #include "base/bind.h" |
Peter Kasting | 383640f5 | 2018-02-13 06:22:40 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 15 | #include "net/base/io_buffer.h" |
Ryan Hamilton | ea4fa19 | 2022-04-12 18:30:49 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h" |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace net { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | const char kData[] = "hello!\0hi."; |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 24 | const size_t kDataSize = std::size(kData); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 25 | |
| 26 | class SpdyBufferTest : public ::testing::Test {}; |
| 27 | |
| 28 | // Make a string from the data remaining in |buffer|. |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 29 | std::string BufferToString(const SpdyBuffer& buffer) { |
| 30 | return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize()); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 31 | } |
| 32 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 33 | // Construct a SpdyBuffer from a spdy::SpdySerializedFrame and make sure its |
| 34 | // data points to the frame's underlying data. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 35 | TEST_F(SpdyBufferTest, FrameConstructor) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 36 | SpdyBuffer buffer(std::make_unique<spdy::SpdySerializedFrame>( |
bnc | 3f6a855 | 2017-05-17 13:40:34 | [diff] [blame] | 37 | const_cast<char*>(kData), kDataSize, false /* owns_buffer */)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 38 | |
| 39 | EXPECT_EQ(kData, buffer.GetRemainingData()); |
| 40 | EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
| 41 | } |
| 42 | |
| 43 | // Construct a SpdyBuffer from a const char*/size_t pair and make sure |
| 44 | // it makes a copy of the data. |
| 45 | TEST_F(SpdyBufferTest, DataConstructor) { |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 46 | std::string data(kData, kDataSize); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 47 | SpdyBuffer buffer(data.data(), data.size()); |
| 48 | // This mutation shouldn't affect |buffer|'s data. |
| 49 | data[0] = 'H'; |
| 50 | |
| 51 | EXPECT_NE(kData, buffer.GetRemainingData()); |
| 52 | EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 53 | EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 8a938fed | 2013-04-18 08:31:58 | [diff] [blame] | 56 | void IncrementBy(size_t* x, |
| 57 | SpdyBuffer::ConsumeSource expected_consume_source, |
| 58 | size_t delta, |
| 59 | SpdyBuffer::ConsumeSource consume_source) { |
| 60 | EXPECT_EQ(expected_consume_source, consume_source); |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 61 | *x += delta; |
| 62 | } |
| 63 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 64 | // Construct a SpdyBuffer and call Consume() on it, which should |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 65 | // update the remaining data pointer and size appropriately, as well |
| 66 | // as calling the consume callbacks. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 67 | TEST_F(SpdyBufferTest, Consume) { |
| 68 | SpdyBuffer buffer(kData, kDataSize); |
| 69 | |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 70 | size_t x1 = 0; |
| 71 | size_t x2 = 0; |
[email protected] | 8a938fed | 2013-04-18 08:31:58 | [diff] [blame] | 72 | buffer.AddConsumeCallback( |
Anna Malova | fd90c3e | 2020-03-05 17:34:28 | [diff] [blame] | 73 | base::BindRepeating(&IncrementBy, &x1, SpdyBuffer::CONSUME)); |
[email protected] | 8a938fed | 2013-04-18 08:31:58 | [diff] [blame] | 74 | buffer.AddConsumeCallback( |
Anna Malova | fd90c3e | 2020-03-05 17:34:28 | [diff] [blame] | 75 | base::BindRepeating(&IncrementBy, &x2, SpdyBuffer::CONSUME)); |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 76 | |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 77 | EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 78 | |
| 79 | buffer.Consume(5); |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 80 | EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 81 | EXPECT_EQ(5u, x1); |
| 82 | EXPECT_EQ(5u, x2); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 83 | |
| 84 | buffer.Consume(kDataSize - 5); |
| 85 | EXPECT_EQ(0u, buffer.GetRemainingSize()); |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 86 | EXPECT_EQ(kDataSize, x1); |
| 87 | EXPECT_EQ(kDataSize, x2); |
| 88 | } |
| 89 | |
| 90 | // Construct a SpdyBuffer and attach a ConsumeCallback to it. The |
| 91 | // callback should be called when the SpdyBuffer is destroyed. |
| 92 | TEST_F(SpdyBufferTest, ConsumeOnDestruction) { |
| 93 | size_t x = 0; |
| 94 | |
| 95 | { |
| 96 | SpdyBuffer buffer(kData, kDataSize); |
[email protected] | 8a938fed | 2013-04-18 08:31:58 | [diff] [blame] | 97 | buffer.AddConsumeCallback( |
Anna Malova | fd90c3e | 2020-03-05 17:34:28 | [diff] [blame] | 98 | base::BindRepeating(&IncrementBy, &x, SpdyBuffer::DISCARD)); |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | EXPECT_EQ(kDataSize, x); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Make sure the IOBuffer returned by GetIOBufferForRemainingData() |
| 105 | // points to the buffer's remaining data and isn't updated by |
| 106 | // Consume(). |
| 107 | TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) { |
| 108 | SpdyBuffer buffer(kData, kDataSize); |
| 109 | |
| 110 | buffer.Consume(5); |
| 111 | scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData(); |
| 112 | size_t io_buffer_size = buffer.GetRemainingSize(); |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 113 | const std::string expectedData(kData + 5, kDataSize - 5); |
| 114 | EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 115 | |
| 116 | buffer.Consume(kDataSize - 5); |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 117 | EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 08881f9a | 2013-07-12 19:30:31 | [diff] [blame] | 120 | // Make sure the IOBuffer returned by GetIOBufferForRemainingData() |
| 121 | // outlives the buffer itself. |
| 122 | TEST_F(SpdyBufferTest, IOBufferForRemainingDataOutlivesBuffer) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 123 | auto buffer = std::make_unique<SpdyBuffer>(kData, kDataSize); |
[email protected] | 08881f9a | 2013-07-12 19:30:31 | [diff] [blame] | 124 | scoped_refptr<IOBuffer> io_buffer = buffer->GetIOBufferForRemainingData(); |
| 125 | buffer.reset(); |
| 126 | |
| 127 | // This will cause a use-after-free error if |io_buffer| doesn't |
| 128 | // outlive |buffer|. |
| 129 | std::memcpy(io_buffer->data(), kData, kDataSize); |
| 130 | } |
| 131 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 132 | } // namespace |
| 133 | |
| 134 | } // namespace net |