1 // Copyright 2014 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_pinnable_buffer_piece.h"
7 #include "net/spdy/spdy_prefixed_buffer_reader.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 using base::StringPiece
;
16 class SpdyPinnableBufferPieceTest
: public ::testing::Test
{
18 SpdyPrefixedBufferReader
Build(std::string prefix
, std::string suffix
) {
21 return SpdyPrefixedBufferReader(prefix_
.data(), prefix_
.length(),
22 suffix_
.data(), suffix_
.length());
24 std::string prefix_
, suffix_
;
27 TEST_F(SpdyPinnableBufferPieceTest
, Pin
) {
28 SpdyPrefixedBufferReader reader
= Build("foobar", "");
29 SpdyPinnableBufferPiece piece
;
30 EXPECT_TRUE(reader
.ReadN(6, &piece
));
32 // Piece points to underlying prefix storage.
33 EXPECT_EQ(StringPiece("foobar"), piece
);
34 EXPECT_FALSE(piece
.IsPinned());
35 EXPECT_EQ(prefix_
.data(), piece
.buffer());
39 // Piece now points to allocated storage.
40 EXPECT_EQ(StringPiece("foobar"), piece
);
41 EXPECT_TRUE(piece
.IsPinned());
42 EXPECT_NE(prefix_
.data(), piece
.buffer());
44 // Pinning again has no effect.
45 const char* buffer
= piece
.buffer();
47 EXPECT_EQ(buffer
, piece
.buffer());
50 TEST_F(SpdyPinnableBufferPieceTest
, Swap
) {
51 SpdyPrefixedBufferReader reader
= Build("foobar", "");
52 SpdyPinnableBufferPiece piece1
, piece2
;
53 EXPECT_TRUE(reader
.ReadN(4, &piece1
));
54 EXPECT_TRUE(reader
.ReadN(2, &piece2
));
58 EXPECT_EQ(StringPiece("foob"), piece1
);
59 EXPECT_TRUE(piece1
.IsPinned());
60 EXPECT_EQ(StringPiece("ar"), piece2
);
61 EXPECT_FALSE(piece2
.IsPinned());
65 EXPECT_EQ(StringPiece("ar"), piece1
);
66 EXPECT_FALSE(piece1
.IsPinned());
67 EXPECT_EQ(StringPiece("foob"), piece2
);
68 EXPECT_TRUE(piece2
.IsPinned());
70 SpdyPinnableBufferPiece empty
;
73 EXPECT_EQ(StringPiece(""), piece2
);
74 EXPECT_FALSE(piece2
.IsPinned());