| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/spdy_headers_block_parser.h" | 5 #include "net/spdy/spdy_headers_block_parser.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 using base::IntToString; | 16 using base::IntToString; |
| 17 using base::StringPiece; | 17 using base::StringPiece; |
| 18 using std::string; | 18 using std::string; |
| 19 using testing::ElementsAre; | 19 using testing::ElementsAre; |
| 20 using testing::ElementsAreArray; | 20 using testing::ElementsAreArray; |
| 21 | 21 |
| 22 class MockKeyValueHandler : public KeyValueHandler { | 22 // A mock the handler class to check that we parse out the correct headers |
| 23 // and call the callback methods when we should. |
| 24 class MockSpdyHeadersHandler : public SpdyHeadersHandlerInterface { |
| 23 public: | 25 public: |
| 24 // A mock the handler method to make sure we parse out (and call it with) | 26 MOCK_METHOD1(OnHeaderBlock, void(uint32_t num_of_headers)); |
| 25 // the correct parameters. | 27 MOCK_METHOD0(OnHeaderBlockEnd, void()); |
| 26 MOCK_METHOD2(OnKeyValuePair, void(const StringPiece&, const StringPiece&)); | 28 MOCK_METHOD2(OnKeyValuePair, void(const StringPiece&, const StringPiece&)); |
| 27 }; | 29 }; |
| 28 | 30 |
| 29 class SpdyHeadersBlockParserTest : public testing::Test { | 31 class SpdyHeadersBlockParserTest : public testing::Test { |
| 30 public: | 32 public: |
| 31 virtual ~SpdyHeadersBlockParserTest() {} | 33 virtual ~SpdyHeadersBlockParserTest() {} |
| 32 | 34 |
| 33 protected: | 35 protected: |
| 34 virtual void SetUp() { | 36 virtual void SetUp() { |
| 35 // Create a parser using the mock handler. | 37 // Create a parser using the mock handler. |
| 36 parser_.reset(new SpdyHeadersBlockParser(&handler_)); | 38 parser_.reset(new SpdyHeadersBlockParser(&handler_)); |
| 37 } | 39 } |
| 38 | 40 |
| 39 MockKeyValueHandler handler_; | 41 MockSpdyHeadersHandler handler_; |
| 40 scoped_ptr<SpdyHeadersBlockParser> parser_; | 42 scoped_ptr<SpdyHeadersBlockParser> parser_; |
| 41 | 43 |
| 42 // Create a header block with a specified number of headers. | 44 // Create a header block with a specified number of headers. |
| 43 static string CreateHeaders(uint32_t num_headers, bool insert_nulls) { | 45 static string CreateHeaders(uint32_t num_headers, bool insert_nulls) { |
| 44 string headers; | 46 string headers; |
| 45 char length_field[sizeof(uint32_t)]; | 47 char length_field[sizeof(uint32_t)]; |
| 46 | 48 |
| 47 // First, write the number of headers in the header block. | 49 // First, write the number of headers in the header block. |
| 48 StoreInt(num_headers, length_field); | 50 StoreInt(num_headers, length_field); |
| 49 headers += string(length_field, sizeof(length_field)); | 51 headers += string(length_field, sizeof(length_field)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 uint32_t net_order_len = base::HostToNet32(num); | 92 uint32_t net_order_len = base::HostToNet32(num); |
| 91 memcpy(buf, &net_order_len, sizeof(num)); | 93 memcpy(buf, &net_order_len, sizeof(num)); |
| 92 } | 94 } |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 const char* SpdyHeadersBlockParserTest::base_key = "test_key"; | 97 const char* SpdyHeadersBlockParserTest::base_key = "test_key"; |
| 96 const char* SpdyHeadersBlockParserTest::base_value = "test_value"; | 98 const char* SpdyHeadersBlockParserTest::base_value = "test_value"; |
| 97 | 99 |
| 98 TEST_F(SpdyHeadersBlockParserTest, Basic) { | 100 TEST_F(SpdyHeadersBlockParserTest, Basic) { |
| 99 // Sanity test, verify that we parse out correctly a block with | 101 // Sanity test, verify that we parse out correctly a block with |
| 100 // a single key-value pair. | 102 // a single key-value pair and that we notify when we start and finish |
| 103 // handling a headers block. |
| 104 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1); |
| 105 |
| 101 std::string expect_key = base_key + IntToString(0); | 106 std::string expect_key = base_key + IntToString(0); |
| 102 std::string expect_value = base_value + IntToString(0); | 107 std::string expect_value = base_value + IntToString(0); |
| 103 EXPECT_CALL(handler_, OnKeyValuePair(StringPiece(expect_key), | 108 EXPECT_CALL(handler_, OnKeyValuePair(StringPiece(expect_key), |
| 104 StringPiece(expect_value))).Times(1); | 109 StringPiece(expect_value))).Times(1); |
| 110 EXPECT_CALL(handler_, OnHeaderBlockEnd()).Times(1); |
| 111 |
| 105 string headers(CreateHeaders(1, false)); | 112 string headers(CreateHeaders(1, false)); |
| 106 parser_->HandleControlFrameHeadersData(headers.c_str(), headers.length()); | 113 parser_->HandleControlFrameHeadersData(headers.c_str(), headers.length()); |
| 107 } | 114 } |
| 108 | 115 |
| 109 TEST_F(SpdyHeadersBlockParserTest, NullsSupported) { | 116 TEST_F(SpdyHeadersBlockParserTest, NullsSupported) { |
| 110 // Sanity test, verify that we parse out correctly a block with | 117 // Sanity test, verify that we parse out correctly a block with |
| 111 // a single key-value pair when the key and value contain null charecters. | 118 // a single key-value pair when the key and value contain null charecters. |
| 119 EXPECT_CALL(handler_, OnHeaderBlock(1)).Times(1); |
| 120 |
| 112 std::string expect_key = base_key + string("\0", 1) + IntToString(0); | 121 std::string expect_key = base_key + string("\0", 1) + IntToString(0); |
| 113 std::string expect_value = base_value + string("\0", 1) + IntToString(0); | 122 std::string expect_value = base_value + string("\0", 1) + IntToString(0); |
| 114 EXPECT_CALL(handler_, OnKeyValuePair(StringPiece(expect_key), | 123 EXPECT_CALL(handler_, OnKeyValuePair(StringPiece(expect_key), |
| 115 StringPiece(expect_value))).Times(1); | 124 StringPiece(expect_value))).Times(1); |
| 125 EXPECT_CALL(handler_, OnHeaderBlockEnd()).Times(1); |
| 126 |
| 116 string headers(CreateHeaders(1, true)); | 127 string headers(CreateHeaders(1, true)); |
| 117 parser_->HandleControlFrameHeadersData(headers.c_str(), headers.length()); | 128 parser_->HandleControlFrameHeadersData(headers.c_str(), headers.length()); |
| 118 } | 129 } |
| 119 | 130 |
| 120 TEST_F(SpdyHeadersBlockParserTest, MultipleBlocksMultipleHeadersPerBlock) { | 131 TEST_F(SpdyHeadersBlockParserTest, MultipleBlocksMultipleHeadersPerBlock) { |
| 121 testing::InSequence s; | 132 testing::InSequence s; |
| 122 | 133 |
| 123 // The mock doesn't retain storage of arguments, so keep them in scope. | 134 // The mock doesn't retain storage of arguments, so keep them in scope. |
| 124 std::vector<string> retained_arguments; | 135 std::vector<string> retained_arguments; |
| 125 for (int i = 0; i < kNumHeadersInBlock; i++) { | 136 for (int i = 0; i < kNumHeadersInBlock; i++) { |
| 126 retained_arguments.push_back(base_key + IntToString(i)); | 137 retained_arguments.push_back(base_key + IntToString(i)); |
| 127 retained_arguments.push_back(base_value + IntToString(i)); | 138 retained_arguments.push_back(base_value + IntToString(i)); |
| 128 } | 139 } |
| 129 // For each block we expect to parse out the headers in order. | 140 // For each block we expect to parse out the headers in order. |
| 130 for (int i = 0; i < kNumHeaderBlocks; i++) { | 141 for (int i = 0; i < kNumHeaderBlocks; i++) { |
| 142 EXPECT_CALL(handler_, OnHeaderBlock(kNumHeadersInBlock)).Times(1); |
| 131 for (int j = 0; j < kNumHeadersInBlock; j++) { | 143 for (int j = 0; j < kNumHeadersInBlock; j++) { |
| 132 EXPECT_CALL(handler_, OnKeyValuePair( | 144 EXPECT_CALL(handler_, OnKeyValuePair( |
| 133 StringPiece(retained_arguments[2 * j]), | 145 StringPiece(retained_arguments[2 * j]), |
| 134 StringPiece(retained_arguments[2 * j + 1]))).Times(1); | 146 StringPiece(retained_arguments[2 * j + 1]))).Times(1); |
| 135 } | 147 } |
| 148 EXPECT_CALL(handler_, OnHeaderBlockEnd()).Times(1); |
| 136 } | 149 } |
| 137 // Parse the blocks, breaking them into multiple reads at various points. | 150 // Parse the blocks, breaking them into multiple reads at various points. |
| 138 for (int i = 0; i < kNumHeaderBlocks; i++) { | 151 for (int i = 0; i < kNumHeaderBlocks; i++) { |
| 139 unsigned break_index = 4 * i; | 152 unsigned break_index = 4 * i; |
| 140 string headers(CreateHeaders(kNumHeadersInBlock, false)); | 153 string headers(CreateHeaders(kNumHeadersInBlock, false)); |
| 141 parser_->HandleControlFrameHeadersData( | 154 parser_->HandleControlFrameHeadersData( |
| 142 headers.c_str(), headers.length() - break_index); | 155 headers.c_str(), headers.length() - break_index); |
| 143 parser_->HandleControlFrameHeadersData( | 156 parser_->HandleControlFrameHeadersData( |
| 144 headers.c_str() + headers.length() - break_index, break_index); | 157 headers.c_str() + headers.length() - break_index, break_index); |
| 145 } | 158 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 EXPECT_EQ(3u, reader.Available()); | 237 EXPECT_EQ(3u, reader.Available()); |
| 225 EXPECT_FALSE(reader.ReadN(4, buffer)); | 238 EXPECT_FALSE(reader.ReadN(4, buffer)); |
| 226 EXPECT_TRUE(reader.ReadN(3, buffer)); | 239 EXPECT_TRUE(reader.ReadN(3, buffer)); |
| 227 EXPECT_THAT(buffer, ElementsAreArray("ijkdefgh")); | 240 EXPECT_THAT(buffer, ElementsAreArray("ijkdefgh")); |
| 228 | 241 |
| 229 EXPECT_EQ(0u, reader.Available()); | 242 EXPECT_EQ(0u, reader.Available()); |
| 230 EXPECT_THAT(reader.Remainder(), ElementsAre()); | 243 EXPECT_THAT(reader.Remainder(), ElementsAre()); |
| 231 } | 244 } |
| 232 | 245 |
| 233 } // namespace net | 246 } // namespace net |
| OLD | NEW |