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 <string> | 9 #include <string> |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 class SpdyBufferTest : public ::testing::Test {}; | 26 class SpdyBufferTest : public ::testing::Test {}; |
27 | 27 |
28 // Make a string from the data remaining in |buffer|. | 28 // Make a string from the data remaining in |buffer|. |
29 std::string BufferToString(const SpdyBuffer& buffer) { | 29 std::string BufferToString(const SpdyBuffer& buffer) { |
30 return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize()); | 30 return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize()); |
31 } | 31 } |
32 | 32 |
33 // Construct a SpdyBuffer from a SpdyFrame and make sure its data | 33 // Construct a SpdyBuffer from a SpdyFrame and make sure its data |
34 // points to the frame's underlying data. | 34 // points to the frame's underlying data. |
35 TEST_F(SpdyBufferTest, FrameConstructor) { | 35 TEST_F(SpdyBufferTest, FrameConstructor) { |
36 SpdyBuffer buffer( | 36 SpdyBuffer buffer(scoped_ptr<SpdyFrame>(new SpdyFrame( |
37 scoped_ptr<SpdyFrame>( | 37 const_cast<char*>(kData), kDataSize, false /* owns_buffer */))); |
38 new SpdyFrame(const_cast<char*>(kData), kDataSize, | |
39 false /* owns_buffer */))); | |
40 | 38 |
41 EXPECT_EQ(kData, buffer.GetRemainingData()); | 39 EXPECT_EQ(kData, buffer.GetRemainingData()); |
42 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); | 40 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
43 } | 41 } |
44 | 42 |
45 // 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 |
46 // it makes a copy of the data. | 44 // it makes a copy of the data. |
47 TEST_F(SpdyBufferTest, DataConstructor) { | 45 TEST_F(SpdyBufferTest, DataConstructor) { |
48 std::string data(kData, kDataSize); | 46 std::string data(kData, kDataSize); |
49 SpdyBuffer buffer(data.data(), data.size()); | 47 SpdyBuffer buffer(data.data(), data.size()); |
(...skipping 14 matching lines...) Expand all Loading... |
64 } | 62 } |
65 | 63 |
66 // Construct a SpdyBuffer and call Consume() on it, which should | 64 // Construct a SpdyBuffer and call Consume() on it, which should |
67 // update the remaining data pointer and size appropriately, as well | 65 // update the remaining data pointer and size appropriately, as well |
68 // as calling the consume callbacks. | 66 // as calling the consume callbacks. |
69 TEST_F(SpdyBufferTest, Consume) { | 67 TEST_F(SpdyBufferTest, Consume) { |
70 SpdyBuffer buffer(kData, kDataSize); | 68 SpdyBuffer buffer(kData, kDataSize); |
71 | 69 |
72 size_t x1 = 0; | 70 size_t x1 = 0; |
73 size_t x2 = 0; | 71 size_t x2 = 0; |
74 buffer.AddConsumeCallback( | 72 buffer.AddConsumeCallback(base::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME)); |
75 base::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME)); | 73 buffer.AddConsumeCallback(base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME)); |
76 buffer.AddConsumeCallback( | |
77 base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME)); | |
78 | 74 |
79 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); | 75 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); |
80 | 76 |
81 buffer.Consume(5); | 77 buffer.Consume(5); |
82 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); | 78 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); |
83 EXPECT_EQ(5u, x1); | 79 EXPECT_EQ(5u, x1); |
84 EXPECT_EQ(5u, x2); | 80 EXPECT_EQ(5u, x2); |
85 | 81 |
86 buffer.Consume(kDataSize - 5); | 82 buffer.Consume(kDataSize - 5); |
87 EXPECT_EQ(0u, buffer.GetRemainingSize()); | 83 EXPECT_EQ(0u, buffer.GetRemainingSize()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 buffer.reset(); | 124 buffer.reset(); |
129 | 125 |
130 // This will cause a use-after-free error if |io_buffer| doesn't | 126 // This will cause a use-after-free error if |io_buffer| doesn't |
131 // outlive |buffer|. | 127 // outlive |buffer|. |
132 std::memcpy(io_buffer->data(), kData, kDataSize); | 128 std::memcpy(io_buffer->data(), kData, kDataSize); |
133 } | 129 } |
134 | 130 |
135 } // namespace | 131 } // namespace |
136 | 132 |
137 } // namespace net | 133 } // namespace net |
OLD | NEW |