• 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_read_queue.h"
6 
7 #include <algorithm>
8 #include <cstddef>
9 #include <memory>
10 #include <string>
11 #include <utility>
12 
13 #include "base/functional/bind.h"
14 #include "base/functional/callback.h"
15 #include "net/spdy/spdy_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace net::test {
19 namespace {
20 
21 const char kData[] = "SPDY read queue test data.\0Some more data.";
22 const size_t kDataSize = std::size(kData);
23 
24 // Enqueues |data| onto |queue| in chunks of at most |max_buffer_size|
25 // bytes.
EnqueueString(const std::string & data,size_t max_buffer_size,SpdyReadQueue * queue)26 void EnqueueString(const std::string& data,
27                    size_t max_buffer_size,
28                    SpdyReadQueue* queue) {
29   ASSERT_GT(data.size(), 0u);
30   ASSERT_GT(max_buffer_size, 0u);
31   size_t old_total_size = queue->GetTotalSize();
32   for (size_t i = 0; i < data.size();) {
33     size_t buffer_size = std::min(data.size() - i, max_buffer_size);
34     queue->Enqueue(std::make_unique<SpdyBuffer>(data.data() + i, buffer_size));
35     i += buffer_size;
36     EXPECT_FALSE(queue->IsEmpty());
37     EXPECT_EQ(old_total_size + i, queue->GetTotalSize());
38   }
39 }
40 
41 // Dequeues all bytes in |queue| in chunks of at most
42 // |max_buffer_size| bytes and returns the data as a string.
DrainToString(size_t max_buffer_size,SpdyReadQueue * queue)43 std::string DrainToString(size_t max_buffer_size, SpdyReadQueue* queue) {
44   std::string data;
45 
46   // Pad the buffer so we can detect out-of-bound writes.
47   size_t padding = std::max(static_cast<size_t>(4096), queue->GetTotalSize());
48   size_t buffer_size_with_padding = padding + max_buffer_size + padding;
49   auto buffer = std::make_unique<char[]>(buffer_size_with_padding);
50   std::memset(buffer.get(), 0, buffer_size_with_padding);
51   char* buffer_data = buffer.get() + padding;
52 
53   while (!queue->IsEmpty()) {
54     size_t old_total_size = queue->GetTotalSize();
55     EXPECT_GT(old_total_size, 0u);
56     size_t dequeued_bytes = queue->Dequeue(buffer_data, max_buffer_size);
57 
58     // Make sure |queue| doesn't write past either end of its given
59     // boundaries.
60     for (int i = 1; i <= static_cast<int>(padding); ++i) {
61       EXPECT_EQ('\0', buffer_data[-i]) << -i;
62     }
63     for (size_t i = 0; i < padding; ++i) {
64       EXPECT_EQ('\0', buffer_data[max_buffer_size + i]) << i;
65     }
66 
67     data.append(buffer_data, dequeued_bytes);
68     EXPECT_EQ(dequeued_bytes, std::min(max_buffer_size, dequeued_bytes));
69     EXPECT_EQ(queue->GetTotalSize(), old_total_size - dequeued_bytes);
70   }
71   EXPECT_TRUE(queue->IsEmpty());
72   return data;
73 }
74 
75 // Enqueue a test string with the given enqueue/dequeue max buffer
76 // sizes.
RunEnqueueDequeueTest(size_t enqueue_max_buffer_size,size_t dequeue_max_buffer_size)77 void RunEnqueueDequeueTest(size_t enqueue_max_buffer_size,
78                            size_t dequeue_max_buffer_size) {
79   std::string data(kData, kDataSize);
80   SpdyReadQueue read_queue;
81   EnqueueString(data, enqueue_max_buffer_size, &read_queue);
82   const std::string& drained_data =
83       DrainToString(dequeue_max_buffer_size, &read_queue);
84   EXPECT_EQ(data, drained_data);
85 }
86 
OnBufferDiscarded(bool * discarded,size_t * discarded_bytes,size_t delta,SpdyBuffer::ConsumeSource consume_source)87 void OnBufferDiscarded(bool* discarded,
88                        size_t* discarded_bytes,
89                        size_t delta,
90                        SpdyBuffer::ConsumeSource consume_source) {
91   EXPECT_EQ(SpdyBuffer::DISCARD, consume_source);
92   *discarded = true;
93   *discarded_bytes = delta;
94 }
95 
96 }  // namespace
97 
98 class SpdyReadQueueTest : public ::testing::Test {};
99 
100 // Call RunEnqueueDequeueTest() with various buffer size combinatinos.
101 
TEST_F(SpdyReadQueueTest,LargeEnqueueAndDequeueBuffers)102 TEST_F(SpdyReadQueueTest, LargeEnqueueAndDequeueBuffers) {
103   RunEnqueueDequeueTest(2 * kDataSize, 2 * kDataSize);
104 }
105 
TEST_F(SpdyReadQueueTest,OneByteEnqueueAndDequeueBuffers)106 TEST_F(SpdyReadQueueTest, OneByteEnqueueAndDequeueBuffers) {
107   RunEnqueueDequeueTest(1, 1);
108 }
109 
TEST_F(SpdyReadQueueTest,CoprimeBufferSizes)110 TEST_F(SpdyReadQueueTest, CoprimeBufferSizes) {
111   RunEnqueueDequeueTest(2, 3);
112   RunEnqueueDequeueTest(3, 2);
113 }
114 
TEST_F(SpdyReadQueueTest,Clear)115 TEST_F(SpdyReadQueueTest, Clear) {
116   auto buffer = std::make_unique<SpdyBuffer>(kData, kDataSize);
117   bool discarded = false;
118   size_t discarded_bytes = 0;
119   buffer->AddConsumeCallback(
120       base::BindRepeating(&OnBufferDiscarded, &discarded, &discarded_bytes));
121 
122   SpdyReadQueue read_queue;
123   read_queue.Enqueue(std::move(buffer));
124 
125   EXPECT_FALSE(discarded);
126   EXPECT_EQ(0u, discarded_bytes);
127   EXPECT_FALSE(read_queue.IsEmpty());
128 
129   read_queue.Clear();
130 
131   EXPECT_TRUE(discarded);
132   EXPECT_EQ(kDataSize, discarded_bytes);
133   EXPECT_TRUE(read_queue.IsEmpty());
134 }
135 
136 }  // namespace net::test
137