Home | History | Annotate | Download | only in spdy
      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.
      4 
      5 #include "net/spdy/spdy_pinnable_buffer_piece.h"
      6 
      7 #include "net/spdy/spdy_prefixed_buffer_reader.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace net {
     11 
     12 namespace test {
     13 
     14 using base::StringPiece;
     15 
     16 class SpdyPinnableBufferPieceTest : public ::testing::Test {
     17  protected:
     18   SpdyPrefixedBufferReader Build(std::string prefix, std::string suffix) {
     19     prefix_ = prefix;
     20     suffix_ = suffix;
     21     return SpdyPrefixedBufferReader(prefix_.data(), prefix_.length(),
     22                                     suffix_.data(), suffix_.length());
     23   }
     24   std::string prefix_, suffix_;
     25 };
     26 
     27 TEST_F(SpdyPinnableBufferPieceTest, Pin) {
     28   SpdyPrefixedBufferReader reader = Build("foobar", "");
     29   SpdyPinnableBufferPiece piece;
     30   EXPECT_TRUE(reader.ReadN(6, &piece));
     31 
     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());
     36 
     37   piece.Pin();
     38 
     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());
     43 
     44   // Pinning again has no effect.
     45   const char* buffer = piece.buffer();
     46   piece.Pin();
     47   EXPECT_EQ(buffer, piece.buffer());
     48 }
     49 
     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));
     55 
     56   piece1.Pin();
     57 
     58   EXPECT_EQ(StringPiece("foob"), piece1);
     59   EXPECT_TRUE(piece1.IsPinned());
     60   EXPECT_EQ(StringPiece("ar"), piece2);
     61   EXPECT_FALSE(piece2.IsPinned());
     62 
     63   piece1.Swap(&piece2);
     64 
     65   EXPECT_EQ(StringPiece("ar"), piece1);
     66   EXPECT_FALSE(piece1.IsPinned());
     67   EXPECT_EQ(StringPiece("foob"), piece2);
     68   EXPECT_TRUE(piece2.IsPinned());
     69 
     70   SpdyPinnableBufferPiece empty;
     71   piece2.Swap(&empty);
     72 
     73   EXPECT_EQ(StringPiece(""), piece2);
     74   EXPECT_FALSE(piece2.IsPinned());
     75 }
     76 
     77 }  // namespace test
     78 
     79 }  // namespace net
     80