OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/spdy_buffer.h" | 5 #include "net/spdy/spdy_buffer.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | |
11 | 10 |
12 #include "base/bind.h" | 11 #include "base/bind.h" |
13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
14 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/spdy/platform/api/spdy_string.h" |
15 #include "net/spdy/spdy_protocol.h" | 15 #include "net/spdy/spdy_protocol.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const char kData[] = "hello!\0hi."; | 22 const char kData[] = "hello!\0hi."; |
23 const size_t kDataSize = arraysize(kData); | 23 const size_t kDataSize = arraysize(kData); |
24 | 24 |
25 class SpdyBufferTest : public ::testing::Test {}; | 25 class SpdyBufferTest : public ::testing::Test {}; |
26 | 26 |
27 // Make a string from the data remaining in |buffer|. | 27 // Make a string from the data remaining in |buffer|. |
28 std::string BufferToString(const SpdyBuffer& buffer) { | 28 SpdyString BufferToString(const SpdyBuffer& buffer) { |
29 return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize()); | 29 return SpdyString(buffer.GetRemainingData(), buffer.GetRemainingSize()); |
30 } | 30 } |
31 | 31 |
32 // Construct a SpdyBuffer from a SpdySerializedFrame and make sure its data | 32 // Construct a SpdyBuffer from a SpdySerializedFrame and make sure its data |
33 // points to the frame's underlying data. | 33 // points to the frame's underlying data. |
34 TEST_F(SpdyBufferTest, FrameConstructor) { | 34 TEST_F(SpdyBufferTest, FrameConstructor) { |
35 SpdyBuffer buffer( | 35 SpdyBuffer buffer( |
36 std::unique_ptr<SpdySerializedFrame>(new SpdySerializedFrame( | 36 std::unique_ptr<SpdySerializedFrame>(new SpdySerializedFrame( |
37 const_cast<char*>(kData), kDataSize, false /* owns_buffer */))); | 37 const_cast<char*>(kData), kDataSize, false /* owns_buffer */))); |
38 | 38 |
39 EXPECT_EQ(kData, buffer.GetRemainingData()); | 39 EXPECT_EQ(kData, buffer.GetRemainingData()); |
40 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); | 40 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
41 } | 41 } |
42 | 42 |
43 // Construct a SpdyBuffer from a const char*/size_t pair and make sure | 43 // Construct a SpdyBuffer from a const char*/size_t pair and make sure |
44 // it makes a copy of the data. | 44 // it makes a copy of the data. |
45 TEST_F(SpdyBufferTest, DataConstructor) { | 45 TEST_F(SpdyBufferTest, DataConstructor) { |
46 std::string data(kData, kDataSize); | 46 SpdyString data(kData, kDataSize); |
47 SpdyBuffer buffer(data.data(), data.size()); | 47 SpdyBuffer buffer(data.data(), data.size()); |
48 // This mutation shouldn't affect |buffer|'s data. | 48 // This mutation shouldn't affect |buffer|'s data. |
49 data[0] = 'H'; | 49 data[0] = 'H'; |
50 | 50 |
51 EXPECT_NE(kData, buffer.GetRemainingData()); | 51 EXPECT_NE(kData, buffer.GetRemainingData()); |
52 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); | 52 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
53 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); | 53 EXPECT_EQ(SpdyString(kData, kDataSize), BufferToString(buffer)); |
54 } | 54 } |
55 | 55 |
56 void IncrementBy(size_t* x, | 56 void IncrementBy(size_t* x, |
57 SpdyBuffer::ConsumeSource expected_consume_source, | 57 SpdyBuffer::ConsumeSource expected_consume_source, |
58 size_t delta, | 58 size_t delta, |
59 SpdyBuffer::ConsumeSource consume_source) { | 59 SpdyBuffer::ConsumeSource consume_source) { |
60 EXPECT_EQ(expected_consume_source, consume_source); | 60 EXPECT_EQ(expected_consume_source, consume_source); |
61 *x += delta; | 61 *x += delta; |
62 } | 62 } |
63 | 63 |
64 // Construct a SpdyBuffer and call Consume() on it, which should | 64 // Construct a SpdyBuffer and call Consume() on it, which should |
65 // update the remaining data pointer and size appropriately, as well | 65 // update the remaining data pointer and size appropriately, as well |
66 // as calling the consume callbacks. | 66 // as calling the consume callbacks. |
67 TEST_F(SpdyBufferTest, Consume) { | 67 TEST_F(SpdyBufferTest, Consume) { |
68 SpdyBuffer buffer(kData, kDataSize); | 68 SpdyBuffer buffer(kData, kDataSize); |
69 | 69 |
70 size_t x1 = 0; | 70 size_t x1 = 0; |
71 size_t x2 = 0; | 71 size_t x2 = 0; |
72 buffer.AddConsumeCallback( | 72 buffer.AddConsumeCallback( |
73 base::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME)); | 73 base::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME)); |
74 buffer.AddConsumeCallback( | 74 buffer.AddConsumeCallback( |
75 base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME)); | 75 base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME)); |
76 | 76 |
77 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); | 77 EXPECT_EQ(SpdyString(kData, kDataSize), BufferToString(buffer)); |
78 | 78 |
79 buffer.Consume(5); | 79 buffer.Consume(5); |
80 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); | 80 EXPECT_EQ(SpdyString(kData + 5, kDataSize - 5), BufferToString(buffer)); |
81 EXPECT_EQ(5u, x1); | 81 EXPECT_EQ(5u, x1); |
82 EXPECT_EQ(5u, x2); | 82 EXPECT_EQ(5u, x2); |
83 | 83 |
84 buffer.Consume(kDataSize - 5); | 84 buffer.Consume(kDataSize - 5); |
85 EXPECT_EQ(0u, buffer.GetRemainingSize()); | 85 EXPECT_EQ(0u, buffer.GetRemainingSize()); |
86 EXPECT_EQ(kDataSize, x1); | 86 EXPECT_EQ(kDataSize, x1); |
87 EXPECT_EQ(kDataSize, x2); | 87 EXPECT_EQ(kDataSize, x2); |
88 } | 88 } |
89 | 89 |
90 // Construct a SpdyBuffer and attach a ConsumeCallback to it. The | 90 // Construct a SpdyBuffer and attach a ConsumeCallback to it. The |
(...skipping 12 matching lines...) Expand all Loading... |
103 | 103 |
104 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() | 104 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() |
105 // points to the buffer's remaining data and isn't updated by | 105 // points to the buffer's remaining data and isn't updated by |
106 // Consume(). | 106 // Consume(). |
107 TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) { | 107 TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) { |
108 SpdyBuffer buffer(kData, kDataSize); | 108 SpdyBuffer buffer(kData, kDataSize); |
109 | 109 |
110 buffer.Consume(5); | 110 buffer.Consume(5); |
111 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData(); | 111 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData(); |
112 size_t io_buffer_size = buffer.GetRemainingSize(); | 112 size_t io_buffer_size = buffer.GetRemainingSize(); |
113 const std::string expectedData(kData + 5, kDataSize - 5); | 113 const SpdyString expectedData(kData + 5, kDataSize - 5); |
114 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); | 114 EXPECT_EQ(expectedData, SpdyString(io_buffer->data(), io_buffer_size)); |
115 | 115 |
116 buffer.Consume(kDataSize - 5); | 116 buffer.Consume(kDataSize - 5); |
117 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); | 117 EXPECT_EQ(expectedData, SpdyString(io_buffer->data(), io_buffer_size)); |
118 } | 118 } |
119 | 119 |
120 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() | 120 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() |
121 // outlives the buffer itself. | 121 // outlives the buffer itself. |
122 TEST_F(SpdyBufferTest, IOBufferForRemainingDataOutlivesBuffer) { | 122 TEST_F(SpdyBufferTest, IOBufferForRemainingDataOutlivesBuffer) { |
123 std::unique_ptr<SpdyBuffer> buffer(new SpdyBuffer(kData, kDataSize)); | 123 std::unique_ptr<SpdyBuffer> buffer(new SpdyBuffer(kData, kDataSize)); |
124 | 124 |
125 scoped_refptr<IOBuffer> io_buffer = buffer->GetIOBufferForRemainingData(); | 125 scoped_refptr<IOBuffer> io_buffer = buffer->GetIOBufferForRemainingData(); |
126 buffer.reset(); | 126 buffer.reset(); |
127 | 127 |
128 // This will cause a use-after-free error if |io_buffer| doesn't | 128 // This will cause a use-after-free error if |io_buffer| doesn't |
129 // outlive |buffer|. | 129 // outlive |buffer|. |
130 std::memcpy(io_buffer->data(), kData, kDataSize); | 130 std::memcpy(io_buffer->data(), kData, kDataSize); |
131 } | 131 } |
132 | 132 |
133 } // namespace | 133 } // namespace |
134 | 134 |
135 } // namespace net | 135 } // namespace net |
OLD | NEW |