OLD | NEW |
| (Empty) |
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 | |
5 #include "net/spdy/spdy_buffer.h" | |
6 | |
7 #include <cstddef> | |
8 #include <cstring> | |
9 #include <memory> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "net/base/io_buffer.h" | |
14 #include "net/spdy/platform/api/spdy_string.h" | |
15 #include "net/spdy/spdy_protocol.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace net { | |
19 | |
20 namespace { | |
21 | |
22 const char kData[] = "hello!\0hi."; | |
23 const size_t kDataSize = arraysize(kData); | |
24 | |
25 class SpdyBufferTest : public ::testing::Test {}; | |
26 | |
27 // Make a string from the data remaining in |buffer|. | |
28 SpdyString BufferToString(const SpdyBuffer& buffer) { | |
29 return SpdyString(buffer.GetRemainingData(), buffer.GetRemainingSize()); | |
30 } | |
31 | |
32 // Construct a SpdyBuffer from a SpdySerializedFrame and make sure its data | |
33 // points to the frame's underlying data. | |
34 TEST_F(SpdyBufferTest, FrameConstructor) { | |
35 SpdyBuffer buffer( | |
36 std::unique_ptr<SpdySerializedFrame>(new 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. | |
45 TEST_F(SpdyBufferTest, DataConstructor) { | |
46 SpdyString 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(SpdyString(kData, kDataSize), BufferToString(buffer)); | |
54 } | |
55 | |
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. | |
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::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME)); | |
74 buffer.AddConsumeCallback( | |
75 base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME)); | |
76 | |
77 EXPECT_EQ(SpdyString(kData, kDataSize), BufferToString(buffer)); | |
78 | |
79 buffer.Consume(5); | |
80 EXPECT_EQ(SpdyString(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. | |
92 TEST_F(SpdyBufferTest, ConsumeOnDestruction) { | |
93 size_t x = 0; | |
94 | |
95 { | |
96 SpdyBuffer buffer(kData, kDataSize); | |
97 buffer.AddConsumeCallback( | |
98 base::Bind(&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(). | |
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 SpdyString expectedData(kData + 5, kDataSize - 5); | |
114 EXPECT_EQ(expectedData, SpdyString(io_buffer->data(), io_buffer_size)); | |
115 | |
116 buffer.Consume(kDataSize - 5); | |
117 EXPECT_EQ(expectedData, SpdyString(io_buffer->data(), io_buffer_size)); | |
118 } | |
119 | |
120 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() | |
121 // outlives the buffer itself. | |
122 TEST_F(SpdyBufferTest, IOBufferForRemainingDataOutlivesBuffer) { | |
123 std::unique_ptr<SpdyBuffer> buffer(new SpdyBuffer(kData, kDataSize)); | |
124 | |
125 scoped_refptr<IOBuffer> io_buffer = buffer->GetIOBufferForRemainingData(); | |
126 buffer.reset(); | |
127 | |
128 // This will cause a use-after-free error if |io_buffer| doesn't | |
129 // outlive |buffer|. | |
130 std::memcpy(io_buffer->data(), kData, kDataSize); | |
131 } | |
132 | |
133 } // namespace | |
134 | |
135 } // namespace net | |
OLD | NEW |