Pass CreateDirectory errors up to IndexedDB.
[chromium-blink-merge.git] / net / spdy / spdy_buffer_unittest.cc
blob5a8bfc8339e55e565065f32630e45623c5a6ff2f
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.
5 #include "net/spdy/spdy_buffer.h"
7 #include <cstddef>
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/io_buffer.h"
15 #include "net/spdy/spdy_protocol.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace net {
20 namespace {
22 const char kData[] = "hello!\0hi.";
23 const size_t kDataSize = arraysize(kData);
25 class SpdyBufferTest : public ::testing::Test {};
27 // Make a string from the data remaining in |buffer|.
28 std::string BufferToString(const SpdyBuffer& buffer) {
29 return std::string(buffer.GetRemainingData(), buffer.GetRemainingSize());
32 // Construct a SpdyBuffer from a SpdyFrame and make sure its data
33 // points to the frame's underlying data.
34 TEST_F(SpdyBufferTest, FrameConstructor) {
35 SpdyBuffer buffer(
36 scoped_ptr<SpdyFrame>(
37 new SpdyFrame(const_cast<char*>(kData), kDataSize,
38 false /* owns_buffer */)));
40 EXPECT_EQ(kData, buffer.GetRemainingData());
41 EXPECT_EQ(kDataSize, buffer.GetRemainingSize());
44 // Construct a SpdyBuffer from a const char*/size_t pair and make sure
45 // it makes a copy of the data.
46 TEST_F(SpdyBufferTest, DataConstructor) {
47 std::string data(kData, kDataSize);
48 SpdyBuffer buffer(data.data(), data.size());
49 // This mutation shouldn't affect |buffer|'s data.
50 data[0] = 'H';
52 EXPECT_NE(kData, buffer.GetRemainingData());
53 EXPECT_EQ(kDataSize, buffer.GetRemainingSize());
54 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
57 void IncrementBy(size_t* x,
58 SpdyBuffer::ConsumeSource expected_consume_source,
59 size_t delta,
60 SpdyBuffer::ConsumeSource consume_source) {
61 EXPECT_EQ(expected_consume_source, consume_source);
62 *x += delta;
65 // Construct a SpdyBuffer and call Consume() on it, which should
66 // update the remaining data pointer and size appropriately, as well
67 // as calling the consume callbacks.
68 TEST_F(SpdyBufferTest, Consume) {
69 SpdyBuffer buffer(kData, kDataSize);
71 size_t x1 = 0;
72 size_t x2 = 0;
73 buffer.AddConsumeCallback(
74 base::Bind(&IncrementBy, &x1, SpdyBuffer::CONSUME));
75 buffer.AddConsumeCallback(
76 base::Bind(&IncrementBy, &x2, SpdyBuffer::CONSUME));
78 EXPECT_EQ(std::string(kData, kDataSize), BufferToString(buffer));
80 buffer.Consume(5);
81 EXPECT_EQ(std::string(kData + 5, kDataSize - 5), BufferToString(buffer));
82 EXPECT_EQ(5u, x1);
83 EXPECT_EQ(5u, x2);
85 buffer.Consume(kDataSize - 5);
86 EXPECT_EQ(0u, buffer.GetRemainingSize());
87 EXPECT_EQ(kDataSize, x1);
88 EXPECT_EQ(kDataSize, x2);
91 // Construct a SpdyBuffer and attach a ConsumeCallback to it. The
92 // callback should be called when the SpdyBuffer is destroyed.
93 TEST_F(SpdyBufferTest, ConsumeOnDestruction) {
94 size_t x = 0;
97 SpdyBuffer buffer(kData, kDataSize);
98 buffer.AddConsumeCallback(
99 base::Bind(&IncrementBy, &x, SpdyBuffer::DISCARD));
102 EXPECT_EQ(kDataSize, x);
105 // Make sure the IOBuffer returned by GetIOBufferForRemainingData()
106 // points to the buffer's remaining data and isn't updated by
107 // Consume().
108 TEST_F(SpdyBufferTest, GetIOBufferForRemainingData) {
109 SpdyBuffer buffer(kData, kDataSize);
111 buffer.Consume(5);
112 scoped_refptr<IOBuffer> io_buffer = buffer.GetIOBufferForRemainingData();
113 size_t io_buffer_size = buffer.GetRemainingSize();
114 const std::string expectedData(kData + 5, kDataSize - 5);
115 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
117 buffer.Consume(kDataSize - 5);
118 EXPECT_EQ(expectedData, std::string(io_buffer->data(), io_buffer_size));
121 } // namespace
123 } // namespace net