Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: net/http2/hpack/decoder/hpack_whole_entry_buffer_test.cc

Issue 2615383002: Implement StringPieceHasSubstrMatcher. (Closed)
Patch Set: Rebase. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/test/gtest_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/http2/hpack/decoder/hpack_whole_entry_buffer.h" 5 #include "net/http2/hpack/decoder/hpack_whole_entry_buffer.h"
6 6
7 // Tests of HpackWholeEntryBuffer: does it buffer correctly, and does it 7 // Tests of HpackWholeEntryBuffer: does it buffer correctly, and does it
8 // detect Huffman decoding errors and oversize string errors? 8 // detect Huffman decoding errors and oversize string errors?
9 9
10 #include "net/test/gtest_util.h"
11 #include "testing/gmock/include/gmock/gmock-matchers.h"
10 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 14
13 using base::StringPiece; 15 using base::StringPiece;
14 using ::testing::AllOf; 16 using ::testing::AllOf;
15 using ::testing::HasSubstr; 17 using ::testing::HasSubstr;
16 using ::testing::InSequence; 18 using ::testing::InSequence;
17 using ::testing::Property; 19 using ::testing::Property;
18 using ::testing::SaveArg;
19 using ::testing::StrictMock; 20 using ::testing::StrictMock;
20 using ::testing::_; 21 using ::testing::_;
21 22
22 namespace net { 23 namespace net {
23 namespace test { 24 namespace test {
24 namespace { 25 namespace {
25 26
26 constexpr size_t kMaxStringSize = 20; 27 constexpr size_t kMaxStringSize = 20;
27 28
29 // Define HasSubstr() for base::StringPiece arguments.
30 // This shadows ::testing::HasSubstr(), which only works on argument types
31 // that can be implicilty converted to a std::string.
32 inline ::testing::PolymorphicMatcher<StringPieceHasSubstrMatcher> HasSubstr(
33 const std::string& substring) {
34 return ::testing::MakePolymorphicMatcher(
35 StringPieceHasSubstrMatcher(substring));
36 }
37
28 class MockHpackWholeEntryListener : public HpackWholeEntryListener { 38 class MockHpackWholeEntryListener : public HpackWholeEntryListener {
29 public: 39 public:
30 ~MockHpackWholeEntryListener() override {} 40 ~MockHpackWholeEntryListener() override {}
31 41
32 MOCK_METHOD1(OnIndexedHeader, void(size_t index)); 42 MOCK_METHOD1(OnIndexedHeader, void(size_t index));
33 MOCK_METHOD3(OnNameIndexAndLiteralValue, 43 MOCK_METHOD3(OnNameIndexAndLiteralValue,
34 void(HpackEntryType entry_type, 44 void(HpackEntryType entry_type,
35 size_t name_index, 45 size_t name_index,
36 HpackDecoderStringBuffer* value_buffer)); 46 HpackDecoderStringBuffer* value_buffer));
37 MOCK_METHOD3(OnLiteralNameAndValue, 47 MOCK_METHOD3(OnLiteralNameAndValue,
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 Property(&HpackDecoderStringBuffer::BufferedLength, 9)), 160 Property(&HpackDecoderStringBuffer::BufferedLength, 9)),
151 AllOf(Property(&HpackDecoderStringBuffer::str, "Header Value"), 161 AllOf(Property(&HpackDecoderStringBuffer::str, "Header Value"),
152 Property(&HpackDecoderStringBuffer::BufferedLength, 0)))); 162 Property(&HpackDecoderStringBuffer::BufferedLength, 0))));
153 163
154 entry_buffer_.OnValueEnd(); 164 entry_buffer_.OnValueEnd();
155 } 165 }
156 166
157 // Verify that a name longer than the allowed size generates an error. 167 // Verify that a name longer than the allowed size generates an error.
158 TEST_F(HpackWholeEntryBufferTest, NameTooLong) { 168 TEST_F(HpackWholeEntryBufferTest, NameTooLong) {
159 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 0); 169 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 0);
160 StringPiece error_message; 170 EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry name")));
161 EXPECT_CALL(listener_, OnHpackDecodeError(_))
162 .WillOnce(SaveArg<0>(&error_message));
163 entry_buffer_.OnNameStart(false, kMaxStringSize + 1); 171 entry_buffer_.OnNameStart(false, kMaxStringSize + 1);
164 EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry name"));
165 } 172 }
166 173
167 // Verify that a name longer than the allowed size generates an error. 174 // Verify that a name longer than the allowed size generates an error.
168 TEST_F(HpackWholeEntryBufferTest, ValueTooLong) { 175 TEST_F(HpackWholeEntryBufferTest, ValueTooLong) {
169 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 1); 176 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kIndexedLiteralHeader, 1);
170 StringPiece error_message; 177 EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry value")));
171 EXPECT_CALL(listener_, OnHpackDecodeError(_))
172 .WillOnce(SaveArg<0>(&error_message));
173 entry_buffer_.OnValueStart(false, kMaxStringSize + 1); 178 entry_buffer_.OnValueStart(false, kMaxStringSize + 1);
174 EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry value"));
175 } 179 }
176 180
177 // Verify that a Huffman encoded name with an explicit EOS generates an error 181 // Verify that a Huffman encoded name with an explicit EOS generates an error
178 // for an explicit EOS. 182 // for an explicit EOS.
179 TEST_F(HpackWholeEntryBufferTest, NameHuffmanError) { 183 TEST_F(HpackWholeEntryBufferTest, NameHuffmanError) {
180 const char data[] = "\xff\xff\xff"; 184 const char data[] = "\xff\xff\xff";
181 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kUnindexedLiteralHeader, 185 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kUnindexedLiteralHeader,
182 0); 186 0);
183 entry_buffer_.OnNameStart(true, 4); 187 entry_buffer_.OnNameStart(true, 4);
184 entry_buffer_.OnNameData(data, 3); 188 entry_buffer_.OnNameData(data, 3);
185 189
186 StringPiece error_message; 190 EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry name")));
187 EXPECT_CALL(listener_, OnHpackDecodeError(_))
188 .WillOnce(SaveArg<0>(&error_message));
189 191
190 entry_buffer_.OnNameData(data, 1); 192 entry_buffer_.OnNameData(data, 1);
191 EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry name"));
192 193
193 // After an error is reported, the listener is not called again. 194 // After an error is reported, the listener is not called again.
194 EXPECT_CALL(listener_, OnDynamicTableSizeUpdate(8096)).Times(0); 195 EXPECT_CALL(listener_, OnDynamicTableSizeUpdate(8096)).Times(0);
195 entry_buffer_.OnDynamicTableSizeUpdate(8096); 196 entry_buffer_.OnDynamicTableSizeUpdate(8096);
196 } 197 }
197 198
198 // Verify that a Huffman encoded value that isn't properly terminated with 199 // Verify that a Huffman encoded value that isn't properly terminated with
199 // a partial EOS symbol generates an error. 200 // a partial EOS symbol generates an error.
200 TEST_F(HpackWholeEntryBufferTest, ValueeHuffmanError) { 201 TEST_F(HpackWholeEntryBufferTest, ValueeHuffmanError) {
201 const char data[] = "\x00\x00\x00"; 202 const char data[] = "\x00\x00\x00";
202 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kNeverIndexedLiteralHeader, 203 entry_buffer_.OnStartLiteralHeader(HpackEntryType::kNeverIndexedLiteralHeader,
203 61); 204 61);
204 entry_buffer_.OnValueStart(true, 3); 205 entry_buffer_.OnValueStart(true, 3);
205 entry_buffer_.OnValueData(data, 3); 206 entry_buffer_.OnValueData(data, 3);
206 207
207 StringPiece error_message; 208 EXPECT_CALL(listener_, OnHpackDecodeError(HasSubstr("HPACK entry value")));
208 EXPECT_CALL(listener_, OnHpackDecodeError(_))
209 .WillOnce(SaveArg<0>(&error_message));
210 209
211 entry_buffer_.OnValueEnd(); 210 entry_buffer_.OnValueEnd();
212 EXPECT_THAT(error_message.as_string(), HasSubstr("HPACK entry value"));
213 211
214 // After an error is reported, the listener is not called again. 212 // After an error is reported, the listener is not called again.
215 EXPECT_CALL(listener_, OnIndexedHeader(17)).Times(0); 213 EXPECT_CALL(listener_, OnIndexedHeader(17)).Times(0);
216 entry_buffer_.OnIndexedHeader(17); 214 entry_buffer_.OnIndexedHeader(17);
217 } 215 }
218 216
219 } // namespace 217 } // namespace
220 } // namespace test 218 } // namespace test
221 } // namespace net 219 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/test/gtest_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698