| 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/spdy/spdy_deframer_visitor.h" | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <limits> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/rand_util.h" | |
| 15 #include "net/spdy/hpack/hpack_constants.h" | |
| 16 #include "net/spdy/mock_spdy_framer_visitor.h" | |
| 17 #include "net/spdy/spdy_frame_builder.h" | |
| 18 #include "net/spdy/spdy_frame_reader.h" | |
| 19 #include "net/spdy/spdy_protocol.h" | |
| 20 #include "net/spdy/spdy_protocol_test_utils.h" | |
| 21 #include "net/spdy/spdy_test_utils.h" | |
| 22 | |
| 23 using ::base::MakeUnique; | |
| 24 | |
| 25 namespace net { | |
| 26 namespace test { | |
| 27 namespace { | |
| 28 | |
| 29 class SpdyDeframerVisitorTest : public ::testing::Test { | |
| 30 protected: | |
| 31 SpdyDeframerVisitorTest() | |
| 32 : encoder_(SpdyFramer::ENABLE_COMPRESSION), | |
| 33 decoder_(SpdyFramer::ENABLE_COMPRESSION) { | |
| 34 decoder_.set_process_single_input_frame(true); | |
| 35 auto collector = MakeUnique<DeframerCallbackCollector>(&collected_frames_); | |
| 36 auto log_and_collect = | |
| 37 SpdyDeframerVisitorInterface::LogBeforeVisiting(std::move(collector)); | |
| 38 deframer_ = SpdyTestDeframer::CreateConverter(std::move(log_and_collect)); | |
| 39 decoder_.set_visitor(deframer_.get()); | |
| 40 } | |
| 41 | |
| 42 bool DeframeInput(const char* input, size_t size) { | |
| 43 size_t input_remaining = size; | |
| 44 while (input_remaining > 0 && | |
| 45 decoder_.spdy_framer_error() == SpdyFramer::SPDY_NO_ERROR) { | |
| 46 // To make the tests more interesting, we feed random (and small) chunks | |
| 47 // into the framer. This simulates getting strange-sized reads from | |
| 48 // the socket. | |
| 49 const size_t kMaxReadSize = 32; | |
| 50 size_t bytes_read = | |
| 51 (base::RandGenerator(std::min(input_remaining, kMaxReadSize))) + 1; | |
| 52 size_t bytes_processed = decoder_.ProcessInput(input, bytes_read); | |
| 53 input_remaining -= bytes_processed; | |
| 54 input += bytes_processed; | |
| 55 if (decoder_.state() == SpdyFramer::SPDY_READY_FOR_FRAME) { | |
| 56 deframer_->AtFrameEnd(); | |
| 57 } | |
| 58 } | |
| 59 return (input_remaining == 0 && | |
| 60 decoder_.spdy_framer_error() == SpdyFramer::SPDY_NO_ERROR); | |
| 61 } | |
| 62 | |
| 63 SpdySerializedFrame SerializeFrame(const SpdyFrameIR& frame) { | |
| 64 return encoder_.SerializeFrame(frame); | |
| 65 } | |
| 66 | |
| 67 SpdyString SerializeFrames( | |
| 68 const std::vector<std::unique_ptr<SpdyFrameIR>>& frames) { | |
| 69 SpdyString result; | |
| 70 for (const auto& frame_ptr : frames) { | |
| 71 auto sf = SerializeFrame(*frame_ptr); | |
| 72 result.append(sf.data(), sf.size()); | |
| 73 } | |
| 74 return result; | |
| 75 } | |
| 76 | |
| 77 // bool | |
| 78 | |
| 79 SpdyFramer encoder_; | |
| 80 SpdyFramer decoder_; | |
| 81 std::vector<CollectedFrame> collected_frames_; | |
| 82 std::unique_ptr<SpdyTestDeframer> deframer_; | |
| 83 }; | |
| 84 | |
| 85 TEST_F(SpdyDeframerVisitorTest, DataFrame) { | |
| 86 const char kFrameData[] = { | |
| 87 0x00, 0x00, 0x0d, // Length = 13. | |
| 88 0x00, // DATA | |
| 89 0x08, // PADDED | |
| 90 0x00, 0x00, 0x00, 0x01, // Stream 1 | |
| 91 0x07, // Pad length field. | |
| 92 'h', 'e', 'l', 'l', // Data | |
| 93 'o', // More Data | |
| 94 0x00, 0x00, 0x00, 0x00, // Padding | |
| 95 0x00, 0x00, 0x00 // More Padding | |
| 96 }; | |
| 97 | |
| 98 EXPECT_TRUE(DeframeInput(kFrameData, sizeof kFrameData)); | |
| 99 ASSERT_EQ(1u, collected_frames_.size()); | |
| 100 const CollectedFrame& cf0 = collected_frames_[0]; | |
| 101 ASSERT_NE(cf0.frame_ir, nullptr); | |
| 102 | |
| 103 SpdyDataIR expected_ir(1, "hello"); | |
| 104 expected_ir.set_padding_len(8); | |
| 105 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 106 } | |
| 107 | |
| 108 TEST_F(SpdyDeframerVisitorTest, HeaderFrameWithContinuation) { | |
| 109 const char kFrameData[] = { | |
| 110 0x00, 0x00, 0x05, // Payload Length: 5 | |
| 111 0x01, // Type: HEADERS | |
| 112 0x09, // Flags: PADDED | END_STREAM | |
| 113 0x00, 0x00, 0x00, 0x01, // Stream: 1 | |
| 114 0x04, // Padding Length: 4 | |
| 115 0x00, 0x00, 0x00, 0x00, // Padding | |
| 116 /* Second Frame */ | |
| 117 0x00, 0x00, 0x12, // Payload Length: 18 | |
| 118 0x09, // Type: CONTINUATION | |
| 119 0x04, // Flags: END_HEADERS | |
| 120 0x00, 0x00, 0x00, 0x01, // Stream: 1 | |
| 121 0x00, // Unindexed, literal name & value | |
| 122 0x03, 0x62, 0x61, 0x72, // Name len and name (3, "bar") | |
| 123 0x03, 0x66, 0x6f, 0x6f, // Value len and value (3, "foo") | |
| 124 0x00, // Unindexed, literal name & value | |
| 125 0x03, 0x66, 0x6f, 0x6f, // Name len and name (3, "foo") | |
| 126 0x03, 0x62, 0x61, 0x72, // Value len and value (3, "bar") | |
| 127 }; | |
| 128 | |
| 129 EXPECT_TRUE(DeframeInput(kFrameData, sizeof kFrameData)); | |
| 130 ASSERT_EQ(1u, collected_frames_.size()); | |
| 131 const CollectedFrame& cf0 = collected_frames_[0]; | |
| 132 | |
| 133 StringPairVector headers; | |
| 134 headers.push_back({"bar", "foo"}); | |
| 135 headers.push_back({"foo", "bar"}); | |
| 136 | |
| 137 EXPECT_TRUE(cf0.VerifyHasHeaders(headers)); | |
| 138 | |
| 139 SpdyHeadersIR expected_ir(1); | |
| 140 // Yet again SpdyFramerVisitorInterface is lossy: it doesn't call OnPadding | |
| 141 // for HEADERS, just for DATA. Sigh. | |
| 142 // expected_ir.set_padding_len(5); | |
| 143 expected_ir.set_fin(true); | |
| 144 for (const auto& nv : headers) { | |
| 145 expected_ir.SetHeader(nv.first, nv.second); | |
| 146 } | |
| 147 | |
| 148 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 149 | |
| 150 // Confirm that mismatches are also detected. | |
| 151 headers.push_back({"baz", "bing"}); | |
| 152 EXPECT_FALSE(cf0.VerifyHasHeaders(headers)); | |
| 153 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 154 | |
| 155 headers.pop_back(); | |
| 156 EXPECT_TRUE(cf0.VerifyHasHeaders(headers)); | |
| 157 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 158 | |
| 159 expected_ir.SetHeader("baz", "bing"); | |
| 160 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 161 EXPECT_TRUE(cf0.VerifyHasHeaders(headers)); | |
| 162 } | |
| 163 | |
| 164 TEST_F(SpdyDeframerVisitorTest, PriorityFrame) { | |
| 165 const char kFrameData[] = { | |
| 166 0x00, 0x00, 0x05, // Length: 5 | |
| 167 0x02, // Type: PRIORITY | |
| 168 0x00, // Flags: none | |
| 169 0x00, 0x00, 0x00, 0x65, // Stream: 101 | |
| 170 0x80u, 0x00, 0x00, 0x01, // Parent: 1 (Exclusive) | |
| 171 0x10, // Weight: 17 | |
| 172 }; | |
| 173 | |
| 174 EXPECT_TRUE(DeframeInput(kFrameData, sizeof kFrameData)); | |
| 175 ASSERT_EQ(1u, collected_frames_.size()); | |
| 176 const CollectedFrame& cf0 = collected_frames_[0]; | |
| 177 | |
| 178 SpdyPriorityIR expected_ir(101, 1, 17, true); | |
| 179 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 180 | |
| 181 // Confirm that mismatches are also detected. | |
| 182 expected_ir.set_weight(16); | |
| 183 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 184 expected_ir.set_weight(17); | |
| 185 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 186 | |
| 187 expected_ir.set_parent_stream_id(50); | |
| 188 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 189 expected_ir.set_parent_stream_id(1); | |
| 190 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 191 | |
| 192 expected_ir.set_stream_id(201); | |
| 193 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 194 expected_ir.set_stream_id(101); | |
| 195 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 196 | |
| 197 expected_ir.set_exclusive(false); | |
| 198 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 199 expected_ir.set_exclusive(true); | |
| 200 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 201 } | |
| 202 | |
| 203 TEST_F(SpdyDeframerVisitorTest, DISABLED_RstStreamFrame) { | |
| 204 // TODO(jamessynge): Please implement. | |
| 205 } | |
| 206 | |
| 207 TEST_F(SpdyDeframerVisitorTest, SettingsFrame) { | |
| 208 // Settings frame with two entries for the same parameter but with different | |
| 209 // values. The last one will be in the decoded SpdySettingsIR, but the vector | |
| 210 // of settings will have both, in the same order. | |
| 211 const char kFrameData[] = { | |
| 212 0x00, 0x00, 0x0c, // Length | |
| 213 0x04, // Type (SETTINGS) | |
| 214 0x00, // Flags | |
| 215 0x00, 0x00, 0x00, 0x00, // Stream id (must be zero) | |
| 216 0x00, 0x04, // Setting id (SETTINGS_INITIAL_WINDOW_SIZE) | |
| 217 0x0a, 0x0b, 0x0c, 0x0d, // Setting value | |
| 218 0x00, 0x04, // Setting id (SETTINGS_INITIAL_WINDOW_SIZE) | |
| 219 0x00, 0x00, 0x00, 0xffu // Setting value | |
| 220 }; | |
| 221 | |
| 222 EXPECT_TRUE(DeframeInput(kFrameData, sizeof kFrameData)); | |
| 223 ASSERT_EQ(1u, collected_frames_.size()); | |
| 224 const CollectedFrame& cf0 = collected_frames_[0]; | |
| 225 ASSERT_NE(cf0.frame_ir, nullptr); | |
| 226 | |
| 227 SpdySettingsIR expected_ir; | |
| 228 expected_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, 255); | |
| 229 EXPECT_TRUE(cf0.VerifyHasFrame(expected_ir)); | |
| 230 | |
| 231 SettingVector expected_settings; | |
| 232 expected_settings.push_back({SETTINGS_INITIAL_WINDOW_SIZE, 0x0a0b0c0d}); | |
| 233 expected_settings.push_back({SETTINGS_INITIAL_WINDOW_SIZE, 255}); | |
| 234 | |
| 235 EXPECT_TRUE(cf0.VerifyHasSettings(expected_settings)); | |
| 236 | |
| 237 // Confirm that mismatches are also detected. | |
| 238 expected_settings.push_back({SETTINGS_INITIAL_WINDOW_SIZE, 65536}); | |
| 239 EXPECT_FALSE(cf0.VerifyHasSettings(expected_settings)); | |
| 240 | |
| 241 expected_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, 65536); | |
| 242 EXPECT_FALSE(cf0.VerifyHasFrame(expected_ir)); | |
| 243 | |
| 244 SpdySettingsIR unexpected_ir; | |
| 245 unexpected_ir.set_is_ack(true); | |
| 246 EXPECT_FALSE(cf0.VerifyHasFrame(unexpected_ir)); | |
| 247 } | |
| 248 | |
| 249 TEST_F(SpdyDeframerVisitorTest, DISABLED_PushPromiseFrame) { | |
| 250 // TODO(jamessynge): Please implement. | |
| 251 } | |
| 252 | |
| 253 TEST_F(SpdyDeframerVisitorTest, DISABLED_PingFrame) { | |
| 254 // TODO(jamessynge): Please implement. | |
| 255 } | |
| 256 | |
| 257 TEST_F(SpdyDeframerVisitorTest, DISABLED_GoAwayFrame) { | |
| 258 // TODO(jamessynge): Please implement. | |
| 259 } | |
| 260 | |
| 261 TEST_F(SpdyDeframerVisitorTest, DISABLED_WindowUpdateFrame) { | |
| 262 // TODO(jamessynge): Please implement. | |
| 263 } | |
| 264 | |
| 265 TEST_F(SpdyDeframerVisitorTest, DISABLED_AltSvcFrame) { | |
| 266 // TODO(jamessynge): Please implement. | |
| 267 } | |
| 268 | |
| 269 } // namespace | |
| 270 } // namespace test | |
| 271 } // namespace net | |
| OLD | NEW |