| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 scoped_ptr<SpdyFrame> frame(framer.SerializePushPromise(push_promise)); | 184 scoped_ptr<SpdyFrame> frame(framer.SerializePushPromise(push_promise)); |
| 185 ResetBuffer(); | 185 ResetBuffer(); |
| 186 memcpy(buffer_.get(), frame->data(), framer.GetPushPromiseMinimumSize()); | 186 memcpy(buffer_.get(), frame->data(), framer.GetPushPromiseMinimumSize()); |
| 187 size_ += framer.GetPushPromiseMinimumSize(); | 187 size_ += framer.GetPushPromiseMinimumSize(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 virtual void OnContinuation(SpdyStreamId stream_id, bool end) OVERRIDE { | 190 virtual void OnContinuation(SpdyStreamId stream_id, bool end) OVERRIDE { |
| 191 LOG(FATAL); | 191 LOG(FATAL); |
| 192 } | 192 } |
| 193 | 193 |
| 194 virtual void OnPriority(SpdyStreamId stream_id, |
| 195 SpdyStreamId parent_stream_id, |
| 196 uint8 weight, |
| 197 bool exclusive) OVERRIDE { |
| 198 // Do nothing. |
| 199 } |
| 200 |
| 194 char* ReleaseBuffer() { | 201 char* ReleaseBuffer() { |
| 195 CHECK(finished_); | 202 CHECK(finished_); |
| 196 return buffer_.release(); | 203 return buffer_.release(); |
| 197 } | 204 } |
| 198 | 205 |
| 199 virtual void OnWindowUpdate(SpdyStreamId stream_id, | 206 virtual void OnWindowUpdate(SpdyStreamId stream_id, |
| 200 uint32 delta_window_size) OVERRIDE { | 207 uint32 delta_window_size) OVERRIDE { |
| 201 LOG(FATAL); | 208 LOG(FATAL); |
| 202 } | 209 } |
| 203 | 210 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 220 }; | 227 }; |
| 221 | 228 |
| 222 class TestSpdyVisitor : public SpdyFramerVisitorInterface, | 229 class TestSpdyVisitor : public SpdyFramerVisitorInterface, |
| 223 public SpdyFramerDebugVisitorInterface { | 230 public SpdyFramerDebugVisitorInterface { |
| 224 public: | 231 public: |
| 225 // This is larger than our max frame size because header blocks that | 232 // This is larger than our max frame size because header blocks that |
| 226 // are too long can spill over into CONTINUATION frames. | 233 // are too long can spill over into CONTINUATION frames. |
| 227 static const size_t kDefaultHeaderBufferSize = 16 * 1024 * 1024; | 234 static const size_t kDefaultHeaderBufferSize = 16 * 1024 * 1024; |
| 228 | 235 |
| 229 explicit TestSpdyVisitor(SpdyMajorVersion version) | 236 explicit TestSpdyVisitor(SpdyMajorVersion version) |
| 230 : framer_(version), | 237 : framer_(version), |
| 231 use_compression_(false), | 238 use_compression_(false), |
| 232 error_count_(0), | 239 error_count_(0), |
| 233 syn_frame_count_(0), | 240 syn_frame_count_(0), |
| 234 syn_reply_frame_count_(0), | 241 syn_reply_frame_count_(0), |
| 235 headers_frame_count_(0), | 242 headers_frame_count_(0), |
| 236 push_promise_frame_count_(0), | 243 push_promise_frame_count_(0), |
| 237 goaway_count_(0), | 244 goaway_count_(0), |
| 238 setting_count_(0), | 245 setting_count_(0), |
| 239 settings_ack_sent_(0), | 246 settings_ack_sent_(0), |
| 240 settings_ack_received_(0), | 247 settings_ack_received_(0), |
| 241 continuation_count_(0), | 248 continuation_count_(0), |
| 242 altsvc_count_(0), | 249 altsvc_count_(0), |
| 243 test_altsvc_ir_(0), | 250 priority_count_(0), |
| 244 last_window_update_stream_(0), | 251 test_altsvc_ir_(0), |
| 245 last_window_update_delta_(0), | 252 last_window_update_stream_(0), |
| 246 last_push_promise_stream_(0), | 253 last_window_update_delta_(0), |
| 247 last_push_promise_promised_stream_(0), | 254 last_push_promise_stream_(0), |
| 248 data_bytes_(0), | 255 last_push_promise_promised_stream_(0), |
| 249 fin_frame_count_(0), | 256 data_bytes_(0), |
| 250 fin_opaque_data_(), | 257 fin_frame_count_(0), |
| 251 fin_flag_count_(0), | 258 fin_opaque_data_(), |
| 252 zero_length_data_frame_count_(0), | 259 fin_flag_count_(0), |
| 253 control_frame_header_data_count_(0), | 260 zero_length_data_frame_count_(0), |
| 254 zero_length_control_frame_header_data_count_(0), | 261 control_frame_header_data_count_(0), |
| 255 data_frame_count_(0), | 262 zero_length_control_frame_header_data_count_(0), |
| 256 last_payload_len_(0), | 263 data_frame_count_(0), |
| 257 last_frame_len_(0), | 264 last_payload_len_(0), |
| 258 header_buffer_(new char[kDefaultHeaderBufferSize]), | 265 last_frame_len_(0), |
| 259 header_buffer_length_(0), | 266 header_buffer_(new char[kDefaultHeaderBufferSize]), |
| 260 header_buffer_size_(kDefaultHeaderBufferSize), | 267 header_buffer_length_(0), |
| 261 header_stream_id_(-1), | 268 header_buffer_size_(kDefaultHeaderBufferSize), |
| 262 header_control_type_(DATA), | 269 header_stream_id_(-1), |
| 263 header_buffer_valid_(false) { | 270 header_control_type_(DATA), |
| 264 } | 271 header_buffer_valid_(false) {} |
| 265 | 272 |
| 266 virtual void OnError(SpdyFramer* f) OVERRIDE { | 273 virtual void OnError(SpdyFramer* f) OVERRIDE { |
| 267 LOG(INFO) << "SpdyFramer Error: " | 274 LOG(INFO) << "SpdyFramer Error: " |
| 268 << SpdyFramer::ErrorCodeToString(f->error_code()); | 275 << SpdyFramer::ErrorCodeToString(f->error_code()); |
| 269 ++error_count_; | 276 ++error_count_; |
| 270 } | 277 } |
| 271 | 278 |
| 272 virtual void OnDataFrameHeader(SpdyStreamId stream_id, | 279 virtual void OnDataFrameHeader(SpdyStreamId stream_id, |
| 273 size_t length, | 280 size_t length, |
| 274 bool fin) OVERRIDE { | 281 bool fin) OVERRIDE { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 } | 439 } |
| 433 | 440 |
| 434 virtual void OnSendCompressedFrame(SpdyStreamId stream_id, | 441 virtual void OnSendCompressedFrame(SpdyStreamId stream_id, |
| 435 SpdyFrameType type, | 442 SpdyFrameType type, |
| 436 size_t payload_len, | 443 size_t payload_len, |
| 437 size_t frame_len) OVERRIDE { | 444 size_t frame_len) OVERRIDE { |
| 438 last_payload_len_ = payload_len; | 445 last_payload_len_ = payload_len; |
| 439 last_frame_len_ = frame_len; | 446 last_frame_len_ = frame_len; |
| 440 } | 447 } |
| 441 | 448 |
| 449 virtual void OnPriority(SpdyStreamId stream_id, |
| 450 SpdyStreamId parent_stream_id, |
| 451 uint8 weight, |
| 452 bool exclusive) OVERRIDE { |
| 453 ++priority_count_; |
| 454 } |
| 455 |
| 442 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id, | 456 virtual void OnReceiveCompressedFrame(SpdyStreamId stream_id, |
| 443 SpdyFrameType type, | 457 SpdyFrameType type, |
| 444 size_t frame_len) OVERRIDE { | 458 size_t frame_len) OVERRIDE { |
| 445 last_frame_len_ = frame_len; | 459 last_frame_len_ = frame_len; |
| 446 } | 460 } |
| 447 | 461 |
| 448 // Convenience function which runs a framer simulation with particular input. | 462 // Convenience function which runs a framer simulation with particular input. |
| 449 void SimulateInFramer(const unsigned char* input, size_t size) { | 463 void SimulateInFramer(const unsigned char* input, size_t size) { |
| 450 framer_.set_enable_compression(use_compression_); | 464 framer_.set_enable_compression(use_compression_); |
| 451 framer_.set_visitor(this); | 465 framer_.set_visitor(this); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 int syn_frame_count_; | 514 int syn_frame_count_; |
| 501 int syn_reply_frame_count_; | 515 int syn_reply_frame_count_; |
| 502 int headers_frame_count_; | 516 int headers_frame_count_; |
| 503 int push_promise_frame_count_; | 517 int push_promise_frame_count_; |
| 504 int goaway_count_; | 518 int goaway_count_; |
| 505 int setting_count_; | 519 int setting_count_; |
| 506 int settings_ack_sent_; | 520 int settings_ack_sent_; |
| 507 int settings_ack_received_; | 521 int settings_ack_received_; |
| 508 int continuation_count_; | 522 int continuation_count_; |
| 509 int altsvc_count_; | 523 int altsvc_count_; |
| 524 int priority_count_; |
| 510 SpdyAltSvcIR test_altsvc_ir_; | 525 SpdyAltSvcIR test_altsvc_ir_; |
| 511 SpdyStreamId last_window_update_stream_; | 526 SpdyStreamId last_window_update_stream_; |
| 512 uint32 last_window_update_delta_; | 527 uint32 last_window_update_delta_; |
| 513 SpdyStreamId last_push_promise_stream_; | 528 SpdyStreamId last_push_promise_stream_; |
| 514 SpdyStreamId last_push_promise_promised_stream_; | 529 SpdyStreamId last_push_promise_promised_stream_; |
| 515 int data_bytes_; | 530 int data_bytes_; |
| 516 int fin_frame_count_; // The count of RST_STREAM type frames received. | 531 int fin_frame_count_; // The count of RST_STREAM type frames received. |
| 517 std::string fin_opaque_data_; | 532 std::string fin_opaque_data_; |
| 518 int fin_flag_count_; // The count of frames with the FIN flag set. | 533 int fin_flag_count_; // The count of frames with the FIN flag set. |
| 519 int zero_length_data_frame_count_; // The count of zero-length data frames. | 534 int zero_length_data_frame_count_; // The count of zero-length data frames. |
| (...skipping 2496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3016 SpdyAltSvcIR altsvc_ir(3); | 3031 SpdyAltSvcIR altsvc_ir(3); |
| 3017 altsvc_ir.set_max_age(5); | 3032 altsvc_ir.set_max_age(5); |
| 3018 altsvc_ir.set_port(443); | 3033 altsvc_ir.set_port(443); |
| 3019 altsvc_ir.set_protocol_id("pid1"); | 3034 altsvc_ir.set_protocol_id("pid1"); |
| 3020 altsvc_ir.set_host("host"); | 3035 altsvc_ir.set_host("host"); |
| 3021 altsvc_ir.set_origin("origin"); | 3036 altsvc_ir.set_origin("origin"); |
| 3022 scoped_ptr<SpdySerializedFrame> frame(framer.SerializeFrame(altsvc_ir)); | 3037 scoped_ptr<SpdySerializedFrame> frame(framer.SerializeFrame(altsvc_ir)); |
| 3023 CompareFrame(kDescription, *frame, kFrameData, arraysize(kFrameData)); | 3038 CompareFrame(kDescription, *frame, kFrameData, arraysize(kFrameData)); |
| 3024 } | 3039 } |
| 3025 | 3040 |
| 3041 TEST_P(SpdyFramerTest, CreatePriority) { |
| 3042 if (spdy_version_ <= SPDY3) { |
| 3043 return; |
| 3044 } |
| 3045 |
| 3046 SpdyFramer framer(spdy_version_); |
| 3047 |
| 3048 const char kDescription[] = "PRIORITY frame"; |
| 3049 const char kType = static_cast<unsigned char>( |
| 3050 SpdyConstants::SerializeFrameType(spdy_version_, PRIORITY)); |
| 3051 const unsigned char kFrameData[] = { |
| 3052 0x00, 0x05, kType, 0x00, |
| 3053 0x00, 0x00, 0x00, 0x02, // Stream ID = 2 |
| 3054 0x80, 0x00, 0x00, 0x01, // Exclusive dependency, parent stream ID = 1 |
| 3055 0x10, // Weight = 16 |
| 3056 }; |
| 3057 SpdyPriorityIR priority_ir(2, 1, 16, true); |
| 3058 scoped_ptr<SpdySerializedFrame> frame(framer.SerializeFrame(priority_ir)); |
| 3059 CompareFrame(kDescription, *frame, kFrameData, arraysize(kFrameData)); |
| 3060 } |
| 3061 |
| 3026 TEST_P(SpdyFramerTest, ReadCompressedSynStreamHeaderBlock) { | 3062 TEST_P(SpdyFramerTest, ReadCompressedSynStreamHeaderBlock) { |
| 3027 SpdyFramer framer(spdy_version_); | 3063 SpdyFramer framer(spdy_version_); |
| 3028 SpdySynStreamIR syn_stream(1); | 3064 SpdySynStreamIR syn_stream(1); |
| 3029 syn_stream.set_priority(1); | 3065 syn_stream.set_priority(1); |
| 3030 syn_stream.SetHeader("aa", "vv"); | 3066 syn_stream.SetHeader("aa", "vv"); |
| 3031 syn_stream.SetHeader("bb", "ww"); | 3067 syn_stream.SetHeader("bb", "ww"); |
| 3032 SpdyHeaderBlock headers = syn_stream.name_value_block(); | 3068 SpdyHeaderBlock headers = syn_stream.name_value_block(); |
| 3033 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSynStream(syn_stream)); | 3069 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSynStream(syn_stream)); |
| 3034 EXPECT_TRUE(control_frame.get() != NULL); | 3070 EXPECT_TRUE(control_frame.get() != NULL); |
| 3035 TestSpdyVisitor visitor(spdy_version_); | 3071 TestSpdyVisitor visitor(spdy_version_); |
| (...skipping 2339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5375 EXPECT_EQ(443u, visitor.test_altsvc_ir_.port()); | 5411 EXPECT_EQ(443u, visitor.test_altsvc_ir_.port()); |
| 5376 EXPECT_EQ("protocolid", visitor.test_altsvc_ir_.protocol_id()); | 5412 EXPECT_EQ("protocolid", visitor.test_altsvc_ir_.protocol_id()); |
| 5377 EXPECT_EQ("hostname", visitor.test_altsvc_ir_.host()); | 5413 EXPECT_EQ("hostname", visitor.test_altsvc_ir_.host()); |
| 5378 } | 5414 } |
| 5379 | 5415 |
| 5380 // Tests handling of PRIORITY frames. | 5416 // Tests handling of PRIORITY frames. |
| 5381 TEST_P(SpdyFramerTest, ReadPriority) { | 5417 TEST_P(SpdyFramerTest, ReadPriority) { |
| 5382 if (spdy_version_ <= SPDY3) { | 5418 if (spdy_version_ <= SPDY3) { |
| 5383 return; | 5419 return; |
| 5384 } | 5420 } |
| 5421 SpdyFramer framer(spdy_version_); |
| 5422 SpdyPriorityIR priority(3, 1, 99, false); |
| 5423 scoped_ptr<SpdySerializedFrame> frame(framer.SerializePriority(priority)); |
| 5424 testing::StrictMock<test::MockSpdyFramerVisitor> visitor; |
| 5425 framer.set_visitor(&visitor); |
| 5426 EXPECT_CALL(visitor, OnPriority(3, 1, 99, false)); |
| 5427 framer.ProcessInput(frame->data(), frame->size()); |
| 5385 | 5428 |
| 5386 const unsigned char kFrameData[] = { | 5429 EXPECT_EQ(SpdyFramer::SPDY_RESET, framer.state()); |
| 5387 0x00, 0x05, 0x02, 0x00, // PRIORITY frame | 5430 EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, framer.error_code()) |
| 5388 0x00, 0x00, 0x00, 0x03, // stream ID 3 | 5431 << SpdyFramer::ErrorCodeToString(framer.error_code()); |
| 5389 0x00, 0x00, 0x00, 0x01, // dependent on stream id 1 | 5432 // TODO(mlavan): once we actually maintain a priority tree, |
| 5390 0x00 // weight 0 | 5433 // check that state is adjusted correctly. |
| 5391 }; | |
| 5392 | |
| 5393 TestSpdyVisitor visitor(spdy_version_); | |
| 5394 visitor.SimulateInFramer(kFrameData, sizeof(kFrameData)); | |
| 5395 | |
| 5396 EXPECT_EQ(SpdyFramer::SPDY_RESET, visitor.framer_.state()); | |
| 5397 EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, visitor.framer_.error_code()) | |
| 5398 << SpdyFramer::ErrorCodeToString(visitor.framer_.error_code()); | |
| 5399 } | 5434 } |
| 5400 | 5435 |
| 5401 // Tests handling of PRIORITY frame with incorrect size. | 5436 // Tests handling of PRIORITY frame with incorrect size. |
| 5402 TEST_P(SpdyFramerTest, ReadIncorrectlySizedPriority) { | 5437 TEST_P(SpdyFramerTest, ReadIncorrectlySizedPriority) { |
| 5403 if (spdy_version_ <= SPDY3) { | 5438 if (spdy_version_ <= SPDY3) { |
| 5404 return; | 5439 return; |
| 5405 } | 5440 } |
| 5406 | 5441 |
| 5407 // PRIORITY frame of size 4, which isn't correct. | 5442 // PRIORITY frame of size 4, which isn't correct. |
| 5408 const unsigned char kFrameData[] = { | 5443 const unsigned char kFrameData[] = { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5446 EXPECT_EQ(4u, framer.MapWeightToPriority(109)); | 5481 EXPECT_EQ(4u, framer.MapWeightToPriority(109)); |
| 5447 EXPECT_EQ(4u, framer.MapWeightToPriority(74)); | 5482 EXPECT_EQ(4u, framer.MapWeightToPriority(74)); |
| 5448 EXPECT_EQ(5u, framer.MapWeightToPriority(73)); | 5483 EXPECT_EQ(5u, framer.MapWeightToPriority(73)); |
| 5449 EXPECT_EQ(5u, framer.MapWeightToPriority(37)); | 5484 EXPECT_EQ(5u, framer.MapWeightToPriority(37)); |
| 5450 EXPECT_EQ(6u, framer.MapWeightToPriority(36)); | 5485 EXPECT_EQ(6u, framer.MapWeightToPriority(36)); |
| 5451 EXPECT_EQ(6u, framer.MapWeightToPriority(1)); | 5486 EXPECT_EQ(6u, framer.MapWeightToPriority(1)); |
| 5452 EXPECT_EQ(7u, framer.MapWeightToPriority(0)); | 5487 EXPECT_EQ(7u, framer.MapWeightToPriority(0)); |
| 5453 } | 5488 } |
| 5454 | 5489 |
| 5455 } // namespace net | 5490 } // namespace net |
| OLD | NEW |