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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/spdy/spdy_protocol.h" | 15 #include "net/spdy/spdy_protocol.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
(...skipping 25 matching lines...) Expand all Loading... |
46 std::string data(kData, kDataSize); | 47 std::string data(kData, kDataSize); |
47 SpdyBuffer buffer(data.data(), data.size()); | 48 SpdyBuffer buffer(data.data(), data.size()); |
48 // This mutation shouldn't affect |buffer|'s data. | 49 // This mutation shouldn't affect |buffer|'s data. |
49 data[0] = 'H'; | 50 data[0] = 'H'; |
50 | 51 |
51 EXPECT_NE(kData, buffer.GetRemainingData()); | 52 EXPECT_NE(kData, buffer.GetRemainingData()); |
52 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); | 53 EXPECT_EQ(kDataSize, buffer.GetRemainingSize()); |
53 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); | 54 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); |
54 } | 55 } |
55 | 56 |
| 57 void IncrementBy(size_t* x, size_t delta) { |
| 58 *x += delta; |
| 59 } |
| 60 |
56 // Construct a SpdyBuffer and call Consume() on it, which should | 61 // Construct a SpdyBuffer and call Consume() on it, which should |
57 // update the remaining data pointer and size appropriately. | 62 // update the remaining data pointer and size appropriately, as well |
| 63 // as calling the consume callbacks. |
58 TEST_F(SpdyBufferTest, Consume) { | 64 TEST_F(SpdyBufferTest, Consume) { |
59 SpdyBuffer buffer(kData, kDataSize); | 65 SpdyBuffer buffer(kData, kDataSize); |
60 | 66 |
| 67 size_t x1 = 0; |
| 68 size_t x2 = 0; |
| 69 buffer.AddConsumeCallback(base::Bind(&IncrementBy, &x1)); |
| 70 buffer.AddConsumeCallback(base::Bind(&IncrementBy, &x2)); |
| 71 |
61 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); | 72 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer)); |
62 | 73 |
63 buffer.Consume(5); | 74 buffer.Consume(5); |
64 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); | 75 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer)); |
| 76 EXPECT_EQ(5u, x1); |
| 77 EXPECT_EQ(5u, x2); |
65 | 78 |
66 buffer.Consume(kDataSize - 5); | 79 buffer.Consume(kDataSize - 5); |
67 EXPECT_EQ(0u, buffer.GetRemainingSize()); | 80 EXPECT_EQ(0u, buffer.GetRemainingSize()); |
| 81 EXPECT_EQ(kDataSize, x1); |
| 82 EXPECT_EQ(kDataSize, x2); |
| 83 } |
| 84 |
| 85 // Construct a SpdyBuffer and attach a ConsumeCallback to it. The |
| 86 // callback should be called when the SpdyBuffer is destroyed. |
| 87 TEST_F(SpdyBufferTest, ConsumeOnDestruction) { |
| 88 size_t x = 0; |
| 89 |
| 90 { |
| 91 SpdyBuffer buffer(kData, kDataSize); |
| 92 buffer.AddConsumeCallback(base::Bind(&IncrementBy, &x)); |
| 93 } |
| 94 |
| 95 EXPECT_EQ(kDataSize, x); |
68 } | 96 } |
69 | 97 |
70 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() | 98 // Make sure the IOBuffer returned by GetIOBufferForRemainingData() |
71 // points to the buffer's remaining data and isn't updated by | 99 // points to the buffer's remaining data and isn't updated by |
72 // Consume(). | 100 // Consume(). |
73 TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) { | 101 TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) { |
74 SpdyBuffer buffer(kData, kDataSize); | 102 SpdyBuffer buffer(kData, kDataSize); |
75 | 103 |
76 buffer.Consume(5); | 104 buffer.Consume(5); |
77 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData(); | 105 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData(); |
78 size_t io_buffer_size = buffer.GetRemainingSize(); | 106 size_t io_buffer_size = buffer.GetRemainingSize(); |
79 const std::string expectedData(kData + 5, kDataSize - 5); | 107 const std::string expectedData(kData + 5, kDataSize - 5); |
80 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); | 108 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); |
81 | 109 |
82 buffer.Consume(kDataSize - 5); | 110 buffer.Consume(kDataSize - 5); |
83 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); | 111 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size)); |
84 } | 112 } |
85 | 113 |
86 } // namespace | 114 } // namespace |
87 | 115 |
88 } // namespace net | 116 } // namespace net |
OLD | NEW |