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_headers_block_parser.h"
      6 
      7 #include <string>
      8 
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/string_number_conversions.h"
     11 #include "base/sys_byteorder.h"
     12 #include "testing/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace net {
     16 
     17 using base::IntToString;
     18 using base::StringPiece;
     19 using std::string;
     20 using testing::ElementsAre;
     21 using testing::ElementsAreArray;
     22 
     23 // A mock the handler class to check that we parse out the correct headers
     24 // and call the callback methods when we should.
     25 class MockSpdyHeadersHandler : public SpdyHeadersHandlerInterface {
     26  public:
     27   MOCK_METHOD2(OnHeaderBlock, void(SpdyStreamId stream_id,
     28                                    uint32_t num_of_headers));
     29   MOCK_METHOD2(OnHeaderBlockEnd, void(SpdyStreamId stream_id, size_t bytes));
     30   MOCK_METHOD3(OnHeader, void(SpdyStreamId stream_id,
     31                               StringPiece,
     32                               StringPiece));
     33 };
     34 
     35 class SpdyHeadersBlockParserTest :
     36     public ::testing::TestWithParam<SpdyMajorVersion> {
     37  public:
     38   virtual ~SpdyHeadersBlockParserTest() {}
     39 
     40  protected:
     41   virtual void SetUp() {
     42     // Create a parser using the mock handler.
     43     spdy_version_ = GetParam();
     44     parser_.reset(new SpdyHeadersBlockParser(spdy_version_, &handler_));
     45     length_field_size_ =
     46         SpdyHeadersBlockParser::LengthFieldSizeForVersion(spdy_version_);
     47   }
     48 
     49   // Create a header block with a specified number of headers.
     50   string CreateHeaders(uint32_t num_headers, bool insert_nulls) {
     51     string headers;
     52 
     53     // First, write the number of headers in the header block.
     54     headers += EncodeLength(num_headers);
     55 
     56     // Second, write the key-value pairs.
     57     for (uint32_t i = 0; i < num_headers; i++) {
     58       // Build the key.
     59       string key;
     60       if (insert_nulls) {
     61         key = string(kBaseKey) + string("\0", 1) + IntToString(i);
     62       } else {
     63         key = string(kBaseKey) + IntToString(i);
     64       }
     65       // Encode the key as SPDY header.
     66       headers += EncodeLength(key.length());
     67       headers += key;
     68 
     69       // Build the value.
     70       string value;
     71       if (insert_nulls) {
     72         value = string(kBaseValue) + string("\0", 1) + IntToString(i);
     73       } else {
     74         value = string(kBaseValue) + IntToString(i);
     75       }
     76       // Encode the value as SPDY header.
     77       headers += EncodeLength(value.length());
     78       headers += value;
     79     }
     80     return headers;
     81   }
     82 
     83   string EncodeLength(uint32_t len) {
     84     char buffer[4];
     85     if (length_field_size_ == sizeof(uint32_t)) {
     86       uint32_t net_order_len = htonl(len);
     87       memcpy(buffer, &net_order_len, length_field_size_);
     88     } else if (length_field_size_ == sizeof(uint16_t)) {
     89       uint16_t net_order_len = htons(len);
     90       memcpy(buffer, &net_order_len, length_field_size_);
     91     } else {
     92       CHECK(false) << "Invalid length field size";
     93     }
     94     return string(buffer, length_field_size_);
     95   }
     96 
     97   size_t length_field_size_;
     98   SpdyMajorVersion spdy_version_;
     99 
    100   MockSpdyHeadersHandler handler_;
    101   scoped_ptr<SpdyHeadersBlockParser> parser_;
    102 
    103   static const char *const kBaseKey;
    104   static const char *const kBaseValue;
    105 
    106   // Number of headers and header blocks used in the tests.
    107   static const int kNumHeadersInBlock = 10;
    108   static const int kNumHeaderBlocks = 10;
    109 };
    110 
    111 const char *const SpdyHeadersBlockParserTest::kBaseKey = "test_key";
    112 const char *const SpdyHeadersBlockParserTest::kBaseValue = "test_value";
    113 
    114 // All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
    115 INSTANTIATE_TEST_CASE_P(SpdyHeadersBlockParserTests,
    116                         SpdyHeadersBlockParserTest,
    117                         ::testing::Values(SPDY2, SPDY3, SPDY4));
    118 
    119 TEST_P(SpdyHeadersBlockParserTest, BasicTest) {
    120   // Sanity test, verify that we parse out correctly a block with
    121   // a single key-value pair and that we notify when we start and finish
    122   // handling a headers block.
    123   string headers(CreateHeaders(1, false));
    124 
    125   EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
    126 
    127   std::string expect_key = kBaseKey + IntToString(0);
    128   std::string expect_value = kBaseValue + IntToString(0);
    129   EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
    130                                  StringPiece(expect_value))).Times(1);
    131   EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
    132 
    133   EXPECT_TRUE(parser_->
    134       HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
    135   EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
    136 }
    137 
    138 TEST_P(SpdyHeadersBlockParserTest, NullsSupportedTest) {
    139   // Sanity test, verify that we parse out correctly a block with
    140   // a single key-value pair when the key and value contain null charecters.
    141   string headers(CreateHeaders(1, true));
    142 
    143   EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
    144 
    145   std::string expect_key = kBaseKey + string("\0", 1) + IntToString(0);
    146   std::string expect_value = kBaseValue + string("\0", 1) + IntToString(0);
    147   EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
    148                                  StringPiece(expect_value))).Times(1);
    149   EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
    150 
    151   EXPECT_TRUE(parser_->
    152       HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
    153   EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
    154 }
    155 
    156 TEST_P(SpdyHeadersBlockParserTest, MultipleBlocksAndHeadersWithPartialData) {
    157   testing::InSequence s;
    158 
    159   // CreateHeaders is deterministic; we can call it once for the whole test.
    160   string headers(CreateHeaders(kNumHeadersInBlock, false));
    161 
    162   // The mock doesn't retain storage of arguments, so keep them in scope.
    163   std::vector<string> retained_arguments;
    164   for (int i = 0; i < kNumHeadersInBlock; i++) {
    165     retained_arguments.push_back(kBaseKey + IntToString(i));
    166     retained_arguments.push_back(kBaseValue + IntToString(i));
    167   }
    168   // For each block we expect to parse out the headers in order.
    169   for (int i = 0; i < kNumHeaderBlocks; i++) {
    170     EXPECT_CALL(handler_, OnHeaderBlock(i, kNumHeadersInBlock)).Times(1);
    171     for (int j = 0; j < kNumHeadersInBlock; j++) {
    172       EXPECT_CALL(handler_, OnHeader(
    173           i,
    174           StringPiece(retained_arguments[2 * j]),
    175           StringPiece(retained_arguments[2 * j + 1]))).Times(1);
    176     }
    177     EXPECT_CALL(handler_, OnHeaderBlockEnd(i, headers.length())).Times(1);
    178   }
    179   // Parse the header blocks, feeding the parser one byte at a time.
    180   for (int i = 0; i < kNumHeaderBlocks; i++) {
    181     for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
    182       if ((it + 1) == headers.end()) {
    183         // Last byte completes the block.
    184         EXPECT_TRUE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
    185         EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
    186       } else {
    187         EXPECT_FALSE(parser_->HandleControlFrameHeadersData(i, &(*it), 1));
    188         EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
    189       }
    190     }
    191   }
    192 }
    193 
    194 TEST_P(SpdyHeadersBlockParserTest, HandlesEmptyCallsTest) {
    195   EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
    196 
    197   string headers(CreateHeaders(1, false));
    198 
    199   string expect_key = kBaseKey + IntToString(0);
    200   string expect_value = kBaseValue + IntToString(0);
    201   EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
    202                                  StringPiece(expect_value))).Times(1);
    203   EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
    204 
    205   // Send a header in pieces with intermediate empty calls.
    206   for (string::iterator it = headers.begin(); it != headers.end(); ++it) {
    207     if ((it + 1) == headers.end()) {
    208       // Last byte completes the block.
    209       EXPECT_TRUE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
    210       EXPECT_EQ(SpdyHeadersBlockParser::OK, parser_->get_error());
    211     } else {
    212       EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, &(*it), 1));
    213       EXPECT_EQ(SpdyHeadersBlockParser::NEED_MORE_DATA, parser_->get_error());
    214       EXPECT_FALSE(parser_->HandleControlFrameHeadersData(1, NULL, 0));
    215     }
    216   }
    217 }
    218 
    219 TEST_P(SpdyHeadersBlockParserTest, LargeBlocksDiscardedTest) {
    220   // Header block with too many headers.
    221   {
    222     string headers = EncodeLength(
    223         parser_->MaxNumberOfHeadersForVersion(spdy_version_) + 1);
    224     EXPECT_FALSE(parser_->
    225         HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
    226     EXPECT_EQ(SpdyHeadersBlockParser::HEADER_BLOCK_TOO_LARGE,
    227               parser_->get_error());
    228   }
    229   parser_->Reset();
    230   // Header block with one header, which has a too-long key.
    231   {
    232     EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
    233 
    234     string headers = EncodeLength(1) + EncodeLength(
    235         SpdyHeadersBlockParser::kMaximumFieldLength + 1);
    236     EXPECT_FALSE(parser_->
    237         HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
    238     EXPECT_EQ(SpdyHeadersBlockParser::HEADER_FIELD_TOO_LARGE,
    239               parser_->get_error());
    240   }
    241 }
    242 
    243 TEST_P(SpdyHeadersBlockParserTest, ExtraDataTest) {
    244   string headers = CreateHeaders(1, false) + "foobar";
    245 
    246   EXPECT_CALL(handler_, OnHeaderBlock(1, 1)).Times(1);
    247   EXPECT_CALL(handler_, OnHeaderBlockEnd(1, headers.length())).Times(1);
    248 
    249   string expect_key = kBaseKey + IntToString(0);
    250   string expect_value = kBaseValue + IntToString(0);
    251   EXPECT_CALL(handler_, OnHeader(1, StringPiece(expect_key),
    252                                  StringPiece(expect_value))).Times(1);
    253 
    254   EXPECT_FALSE(parser_->
    255       HandleControlFrameHeadersData(1, headers.c_str(), headers.length()));
    256   EXPECT_EQ(SpdyHeadersBlockParser::TOO_MUCH_DATA, parser_->get_error());
    257 }
    258 
    259 }  // namespace net
    260