OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/http2/decoder/payload_decoders/payload_decoder_base_test_util.h" | |
6 | |
7 #include "net/http2/decoder/frame_decoder_state_test_util.h" | |
8 #include "net/http2/http2_structures_test_util.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace net { | |
12 namespace test { | |
13 PayloadDecoderBaseTest::PayloadDecoderBaseTest() { | |
14 // If the test adds more data after the frame payload, | |
15 // stop as soon as the payload is decoded. | |
16 stop_decode_on_done_ = true; | |
17 frame_header_is_set_ = false; | |
18 Randomize(&frame_header_, RandomPtr()); | |
19 } | |
20 | |
21 DecodeStatus PayloadDecoderBaseTest::StartDecoding(DecodeBuffer* db) { | |
22 DVLOG(2) << "StartDecoding, db->Remaining=" << db->Remaining(); | |
23 // Make sure sub-class has set frame_header_ so that we can inject it | |
24 // into the payload decoder below. | |
25 if (!frame_header_is_set_) { | |
26 ADD_FAILURE() << "frame_header_ is not set"; | |
27 return DecodeStatus::kDecodeError; | |
28 } | |
29 // The contract with the payload decoders is that they won't receive a | |
30 // decode buffer that extends beyond the end of the frame. | |
31 if (db->Remaining() > frame_header_.payload_length) { | |
32 ADD_FAILURE() << "DecodeBuffer has too much data: " << db->Remaining() | |
33 << " > " << frame_header_.payload_length; | |
34 return DecodeStatus::kDecodeError; | |
35 } | |
36 | |
37 // Prepare the payload decoder. | |
38 PreparePayloadDecoder(); | |
39 | |
40 // Reconstruct the FrameDecoderState, prepare the listener, and add it to | |
41 // the FrameDecoderState. | |
42 frame_decoder_state_.~FrameDecoderState(); | |
43 new (&frame_decoder_state_) FrameDecoderState; | |
44 frame_decoder_state_.set_listener(PrepareListener()); | |
45 | |
46 // Make sure that a listener was provided. | |
47 if (frame_decoder_state_.listener() == nullptr) { | |
48 ADD_FAILURE() << "PrepareListener must return a listener."; | |
49 return DecodeStatus::kDecodeError; | |
50 } | |
51 | |
52 // Now that nothing in the payload decoder should be valid, inject the | |
53 // Http2FrameHeader whose payload we're about to decode. That header is the | |
54 // only state that a payload decoder should expect is valid when its Start | |
55 // method is called. | |
56 FrameDecoderStatePeer::set_frame_header(frame_header_, &frame_decoder_state_); | |
57 DecodeStatus status = StartDecodingPayload(db); | |
58 if (status != DecodeStatus::kDecodeInProgress) { | |
59 // Keep track of this so that a concrete test can verify that both fast | |
60 // and slow decoding paths have been tested. | |
61 ++fast_decode_count_; | |
62 } | |
63 return status; | |
64 } | |
65 | |
66 DecodeStatus PayloadDecoderBaseTest::ResumeDecoding(DecodeBuffer* db) { | |
67 DVLOG(2) << "ResumeDecoding, db->Remaining=" << db->Remaining(); | |
68 DecodeStatus status = ResumeDecodingPayload(db); | |
69 if (status != DecodeStatus::kDecodeInProgress) { | |
70 // Keep track of this so that a concrete test can verify that both fast | |
71 // and slow decoding paths have been tested. | |
72 ++slow_decode_count_; | |
73 } | |
74 return status; | |
75 } | |
76 | |
77 ::testing::AssertionResult | |
78 PayloadDecoderBaseTest::DecodePayloadAndValidateSeveralWays( | |
79 base::StringPiece payload, | |
80 Validator validator) { | |
81 VERIFY_TRUE(frame_header_is_set_); | |
82 // Cap the payload to be decoded at the declared payload length. This is | |
83 // required by the decoders' preconditions; they are designed on the | |
84 // assumption that they're never passed more than they're permitted to | |
85 // consume. | |
86 // Note that it is OK if the payload is too short; the validator may be | |
87 // designed to check for that. | |
88 if (payload.size() > frame_header_.payload_length) { | |
89 payload = base::StringPiece(payload.data(), frame_header_.payload_length); | |
90 } | |
91 DecodeBuffer db(payload); | |
92 ResetDecodeSpeedCounters(); | |
93 const bool kMayReturnZeroOnFirst = false; | |
94 return DecodeAndValidateSeveralWays(&db, kMayReturnZeroOnFirst, validator); | |
95 } | |
96 | |
97 } // namespace test | |
98 } // namespace net | |
OLD | NEW |