• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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/spdy/spdy_buffer.h"
6 
7 #include <cstddef>
8 #include <cstring>
9 #include <string>
10 #include <utility>
11 
12 #include "base/functional/bind.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "net/base/io_buffer.h"
16 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace net {
20 
21 namespace {
22 
23 const char kData[] = "hello!\0hi.";
24 const size_t kDataSize = std::size(kData);
25 
26 class SpdyBufferTest : public ::testing::Test {};
27 
28 // Make a string from the data remaining in |buffer|.
BufferToString(const SpdyBuffer & buffer)29 std::string BufferToString(const SpdyBuffer& buffer) {
30   return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize());
31 }
32 
33 // Construct a SpdyBuffer from a spdy::SpdySerializedFrame and make sure its
34 // data points to the frame's underlying data.
TEST_F(SpdyBufferTest,FrameConstructor)35 TEST_F(SpdyBufferTest, FrameConstructor) {
36   SpdyBuffer buffer(std::make_unique<spdy::SpdySerializedFrame>(
37       const_cast<char*>(kData), kDataSize, false /* owns_buffer */));
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.
TEST_F(SpdyBufferTest,DataConstructor)45 TEST_F(SpdyBufferTest, DataConstructor) {
46   std::string data(kData, kDataSize);
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());
53   EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
54 }
55 
IncrementBy(size_t * x,SpdyBuffer::ConsumeSource expected_consume_source,size_t delta,SpdyBuffer::ConsumeSource consume_source)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);
61   *x += delta;
62 }
63 
64 // Construct a SpdyBuffer and call Consume() on it, which should
65 // update the remaining data pointer and size appropriately, as well
66 // as calling the consume callbacks.
TEST_F(SpdyBufferTest,Consume)67 TEST_F(SpdyBufferTest, Consume) {
68   SpdyBuffer buffer(kData, kDataSize);
69 
70   size_t x1 = 0;
71   size_t x2 = 0;
72   buffer.AddConsumeCallback(
73       base::BindRepeating(&IncrementBy, &x1, SpdyBuffer::CONSUME));
74   buffer.AddConsumeCallback(
75       base::BindRepeating(&IncrementBy, &x2, SpdyBuffer::CONSUME));
76 
77   EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
78 
79   buffer.Consume(5);
80   EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer));
81   EXPECT_EQ(5u, x1);
82   EXPECT_EQ(5u, x2);
83 
84   buffer.Consume(kDataSize - 5);
85   EXPECT_EQ(0u, buffer.GetRemainingSize());
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.
TEST_F(SpdyBufferTest,ConsumeOnDestruction)92 TEST_F(SpdyBufferTest, ConsumeOnDestruction) {
93   size_t x = 0;
94 
95   {
96     SpdyBuffer buffer(kData, kDataSize);
97     buffer.AddConsumeCallback(
98         base::BindRepeating(&IncrementBy, &x, SpdyBuffer::DISCARD));
99   }
100 
101   EXPECT_EQ(kDataSize, x);
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().
TEST_F(SpdyBufferTest,GetIOBufferForRemainingData)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();
113   const std::string expectedData(kData + 5, kDataSize - 5);
114   EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
115 
116   buffer.Consume(kDataSize - 5);
117   EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
118 }
119 
120 // Make sure the IOBuffer returned by GetIOBufferForRemainingData()
121 // outlives the buffer itself.
TEST_F(SpdyBufferTest,IOBufferForRemainingDataOutlivesBuffer)122 TEST_F(SpdyBufferTest, IOBufferForRemainingDataOutlivesBuffer) {
123   auto buffer = std::make_unique<SpdyBuffer>(kData, kDataSize);
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 
132 }  // namespace
133 
134 }  // namespace net
135