blob: b30a7c0d9164ed197d94bee6403275589ab54603 [file] [log] [blame]
QUICHE team82dee2f2019-01-18 12:35:12 -05001// 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.
4
5#include "net/third_party/quiche/src/spdy/core/spdy_pinnable_buffer_piece.h"
6
bnc44712912019-08-15 18:58:14 -07007#include <string>
8
QUICHE teamf3c80c92020-02-12 09:47:55 -08009#include "net/third_party/quiche/src/common/platform/api/quiche_test.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050010#include "net/third_party/quiche/src/spdy/core/spdy_prefixed_buffer_reader.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050011
12namespace spdy {
13
14namespace test {
15
QUICHE teamf3c80c92020-02-12 09:47:55 -080016class SpdyPinnableBufferPieceTest : public QuicheTest {
QUICHE team82dee2f2019-01-18 12:35:12 -050017 protected:
bnc44712912019-08-15 18:58:14 -070018 SpdyPrefixedBufferReader Build(const std::string& prefix,
19 const std::string& suffix) {
QUICHE team82dee2f2019-01-18 12:35:12 -050020 prefix_ = prefix;
21 suffix_ = suffix;
22 return SpdyPrefixedBufferReader(prefix_.data(), prefix_.length(),
23 suffix_.data(), suffix_.length());
24 }
bnc44712912019-08-15 18:58:14 -070025 std::string prefix_, suffix_;
QUICHE team82dee2f2019-01-18 12:35:12 -050026};
27
28TEST_F(SpdyPinnableBufferPieceTest, Pin) {
29 SpdyPrefixedBufferReader reader = Build("foobar", "");
30 SpdyPinnableBufferPiece piece;
31 EXPECT_TRUE(reader.ReadN(6, &piece));
32
33 // Piece points to underlying prefix storage.
bnc7f82d042020-01-03 12:18:53 -080034 EXPECT_EQ(quiche::QuicheStringPiece("foobar"),
35 quiche::QuicheStringPiece(piece));
QUICHE team82dee2f2019-01-18 12:35:12 -050036 EXPECT_FALSE(piece.IsPinned());
37 EXPECT_EQ(prefix_.data(), piece.buffer());
38
39 piece.Pin();
40
41 // Piece now points to allocated storage.
bnc7f82d042020-01-03 12:18:53 -080042 EXPECT_EQ(quiche::QuicheStringPiece("foobar"),
43 quiche::QuicheStringPiece(piece));
QUICHE team82dee2f2019-01-18 12:35:12 -050044 EXPECT_TRUE(piece.IsPinned());
45 EXPECT_NE(prefix_.data(), piece.buffer());
46
47 // Pinning again has no effect.
48 const char* buffer = piece.buffer();
49 piece.Pin();
50 EXPECT_EQ(buffer, piece.buffer());
51}
52
53TEST_F(SpdyPinnableBufferPieceTest, Swap) {
54 SpdyPrefixedBufferReader reader = Build("foobar", "");
55 SpdyPinnableBufferPiece piece1, piece2;
56 EXPECT_TRUE(reader.ReadN(4, &piece1));
57 EXPECT_TRUE(reader.ReadN(2, &piece2));
58
59 piece1.Pin();
60
bnc7f82d042020-01-03 12:18:53 -080061 EXPECT_EQ(quiche::QuicheStringPiece("foob"),
62 quiche::QuicheStringPiece(piece1));
QUICHE team82dee2f2019-01-18 12:35:12 -050063 EXPECT_TRUE(piece1.IsPinned());
bnc7f82d042020-01-03 12:18:53 -080064 EXPECT_EQ(quiche::QuicheStringPiece("ar"), quiche::QuicheStringPiece(piece2));
QUICHE team82dee2f2019-01-18 12:35:12 -050065 EXPECT_FALSE(piece2.IsPinned());
66
67 piece1.Swap(&piece2);
68
bnc7f82d042020-01-03 12:18:53 -080069 EXPECT_EQ(quiche::QuicheStringPiece("ar"), quiche::QuicheStringPiece(piece1));
QUICHE team82dee2f2019-01-18 12:35:12 -050070 EXPECT_FALSE(piece1.IsPinned());
bnc7f82d042020-01-03 12:18:53 -080071 EXPECT_EQ(quiche::QuicheStringPiece("foob"),
72 quiche::QuicheStringPiece(piece2));
QUICHE team82dee2f2019-01-18 12:35:12 -050073 EXPECT_TRUE(piece2.IsPinned());
74
75 SpdyPinnableBufferPiece empty;
76 piece2.Swap(&empty);
77
bnc7f82d042020-01-03 12:18:53 -080078 EXPECT_EQ(quiche::QuicheStringPiece(""), quiche::QuicheStringPiece(piece2));
QUICHE team82dee2f2019-01-18 12:35:12 -050079 EXPECT_FALSE(piece2.IsPinned());
80}
81
82} // namespace test
83
84} // namespace spdy