OLD | NEW |
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_string_decoder.h" | 5 #include "net/http2/hpack/decoder/hpack_string_decoder.h" |
6 | 6 |
7 // Tests of HpackStringDecoder. | 7 // Tests of HpackStringDecoder. |
8 | 8 |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
12 #include "net/http2/hpack/decoder/hpack_string_collector.h" | 10 #include "net/http2/hpack/decoder/hpack_string_collector.h" |
13 #include "net/http2/hpack/decoder/hpack_string_decoder_listener.h" | 11 #include "net/http2/hpack/decoder/hpack_string_decoder_listener.h" |
14 #include "net/http2/hpack/tools/hpack_block_builder.h" | 12 #include "net/http2/hpack/tools/hpack_block_builder.h" |
15 #include "net/http2/tools/failure.h" | 13 #include "net/http2/tools/failure.h" |
16 #include "net/http2/tools/http2_random.h" | 14 #include "net/http2/tools/http2_random.h" |
17 #include "net/http2/tools/random_decoder_test.h" | 15 #include "net/http2/tools/random_decoder_test.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
19 | 17 |
20 using ::testing::AssertionResult; | 18 using ::testing::AssertionResult; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 VLOG(1) << decoder_.DebugString(); | 65 VLOG(1) << decoder_.DebugString(); |
68 VLOG(2) << collector_; | 66 VLOG(2) << collector_; |
69 return decoder_.Resume(b, &listener_); | 67 return decoder_.Resume(b, &listener_); |
70 } | 68 } |
71 | 69 |
72 AssertionResult Collected(StringPiece s, bool huffman_encoded) { | 70 AssertionResult Collected(StringPiece s, bool huffman_encoded) { |
73 VLOG(1) << collector_; | 71 VLOG(1) << collector_; |
74 return collector_.Collected(s, huffman_encoded); | 72 return collector_.Collected(s, huffman_encoded); |
75 } | 73 } |
76 | 74 |
77 // Note that base::Bind() makes a copy of |expected_str| even though it is | 75 // expected_str is a string rather than a const string& or StringPiece so that |
78 // taken as a constant reference, so even if MakeValidator is called with a | 76 // the lambda makes a copy of the string, and thus the string to be passed to |
79 // C-style string that is cast to a temporary std::string that gets destroyed | 77 // Collected outlives the call to MakeValidator. |
80 // after the call to MakeValidator, |expected_str| is still valid later when | |
81 // the Validator is run. | |
82 AssertionResult StringValidator(const string& expected_str, | |
83 bool expected_huffman, | |
84 const DecodeBuffer& input, | |
85 DecodeStatus status) { | |
86 AssertionResult result = Collected(expected_str, expected_huffman); | |
87 if (result) { | |
88 VERIFY_EQ(collector_, | |
89 HpackStringCollector(expected_str, expected_huffman)); | |
90 } else { | |
91 VERIFY_NE(collector_, | |
92 HpackStringCollector(expected_str, expected_huffman)); | |
93 } | |
94 VLOG(2) << collector_.ToString(); | |
95 collector_.Clear(); | |
96 VLOG(2) << collector_; | |
97 return result; | |
98 } | |
99 | 78 |
100 Validator MakeValidator(const string& expected_str, bool expected_huffman) { | 79 Validator MakeValidator(const string& expected_str, bool expected_huffman) { |
101 return base::Bind(&HpackStringDecoderTest::StringValidator, | 80 return |
102 base::Unretained(this), expected_str, expected_huffman); | 81 [expected_str, expected_huffman, this]( |
| 82 const DecodeBuffer& input, DecodeStatus status) -> AssertionResult { |
| 83 AssertionResult result = Collected(expected_str, expected_huffman); |
| 84 if (result) { |
| 85 VERIFY_EQ(collector_, |
| 86 HpackStringCollector(expected_str, expected_huffman)); |
| 87 } else { |
| 88 VERIFY_NE(collector_, |
| 89 HpackStringCollector(expected_str, expected_huffman)); |
| 90 } |
| 91 VLOG(2) << collector_.ToString(); |
| 92 collector_.Clear(); |
| 93 VLOG(2) << collector_; |
| 94 return result; |
| 95 }; |
103 } | 96 } |
104 | 97 |
105 const StartMethod start_method_; | 98 const StartMethod start_method_; |
106 HpackStringDecoder decoder_; | 99 HpackStringDecoder decoder_; |
107 HpackStringCollector collector_; | 100 HpackStringCollector collector_; |
108 HpackStringDecoderVLoggingListener listener_; | 101 HpackStringDecoderVLoggingListener listener_; |
109 size_t start_decoding_calls_ = 0; | 102 size_t start_decoding_calls_ = 0; |
110 }; | 103 }; |
111 | 104 |
112 TEST_P(HpackStringDecoderTest, DecodeEmptyString) { | 105 TEST_P(HpackStringDecoderTest, DecodeEmptyString) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 INSTANTIATE_TEST_CASE_P(AllStartMethods, | 178 INSTANTIATE_TEST_CASE_P(AllStartMethods, |
186 HpackStringDecoderTest, | 179 HpackStringDecoderTest, |
187 ::testing::Values(kStart, | 180 ::testing::Values(kStart, |
188 kStartOnly, | 181 kStartOnly, |
189 kStartAndDecodeLength, | 182 kStartAndDecodeLength, |
190 kStartSpecialCaseShort)); | 183 kStartSpecialCaseShort)); |
191 | 184 |
192 } // namespace | 185 } // namespace |
193 } // namespace test | 186 } // namespace test |
194 } // namespace net | 187 } // namespace net |
OLD | NEW |