| 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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "net/spdy/spdy_framer.h" | 11 #include "net/spdy/spdy_framer.h" |
| 11 #include "net/spdy/spdy_protocol.h" | 12 #include "net/spdy/spdy_protocol.h" |
| 12 #include "net/spdy/spdy_frame_builder.h" | 13 #include "net/spdy/spdy_frame_builder.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 15 | 16 |
| 16 using std::string; | 17 using std::string; |
| 17 using std::max; | 18 using std::max; |
| 18 using std::min; | 19 using std::min; |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (identical) return; | 301 if (identical) return; |
| 301 ADD_FAILURE() | 302 ADD_FAILURE() |
| 302 << "Description:\n" | 303 << "Description:\n" |
| 303 << description | 304 << description |
| 304 << "\n\nExpected:\n" | 305 << "\n\nExpected:\n" |
| 305 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) | 306 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len) |
| 306 << "\nActual:\n" | 307 << "\nActual:\n" |
| 307 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); | 308 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len); |
| 308 } | 309 } |
| 309 | 310 |
| 310 class TestSpdyVisitor : public SpdyFramerVisitorInterface { | 311 class TestSpdyVisitor : public SpdyFramerVisitorInterface, |
| 312 public SpdyFramerDebugVisitorInterface { |
| 311 public: | 313 public: |
| 312 static const size_t kDefaultHeaderBufferSize = 16 * 1024; | 314 static const size_t kDefaultHeaderBufferSize = 16 * 1024; |
| 313 static const size_t kDefaultCredentialBufferSize = 16 * 1024; | 315 static const size_t kDefaultCredentialBufferSize = 16 * 1024; |
| 314 | 316 |
| 315 explicit TestSpdyVisitor(int version) | 317 explicit TestSpdyVisitor(int version) |
| 316 : framer_(version), | 318 : framer_(version), |
| 317 use_compression_(false), | 319 use_compression_(false), |
| 318 error_count_(0), | 320 error_count_(0), |
| 319 syn_frame_count_(0), | 321 syn_frame_count_(0), |
| 320 syn_reply_frame_count_(0), | 322 syn_reply_frame_count_(0), |
| 321 headers_frame_count_(0), | 323 headers_frame_count_(0), |
| 322 goaway_count_(0), | 324 goaway_count_(0), |
| 323 setting_count_(0), | 325 setting_count_(0), |
| 324 data_bytes_(0), | 326 data_bytes_(0), |
| 325 fin_frame_count_(0), | 327 fin_frame_count_(0), |
| 326 fin_flag_count_(0), | 328 fin_flag_count_(0), |
| 327 zero_length_data_frame_count_(0), | 329 zero_length_data_frame_count_(0), |
| 328 header_blocks_count_(0), | 330 header_blocks_count_(0), |
| 329 control_frame_header_data_count_(0), | 331 control_frame_header_data_count_(0), |
| 330 zero_length_control_frame_header_data_count_(0), | 332 zero_length_control_frame_header_data_count_(0), |
| 331 data_frame_count_(0), | 333 data_frame_count_(0), |
| 334 last_decompressed_size_(0), |
| 335 last_compressed_size_(0), |
| 332 header_buffer_(new char[kDefaultHeaderBufferSize]), | 336 header_buffer_(new char[kDefaultHeaderBufferSize]), |
| 333 header_buffer_length_(0), | 337 header_buffer_length_(0), |
| 334 header_buffer_size_(kDefaultHeaderBufferSize), | 338 header_buffer_size_(kDefaultHeaderBufferSize), |
| 335 header_stream_id_(-1), | 339 header_stream_id_(-1), |
| 336 header_control_type_(NUM_CONTROL_FRAME_TYPES), | 340 header_control_type_(NUM_CONTROL_FRAME_TYPES), |
| 337 header_buffer_valid_(false), | 341 header_buffer_valid_(false), |
| 338 credential_buffer_(new char[kDefaultCredentialBufferSize]), | 342 credential_buffer_(new char[kDefaultCredentialBufferSize]), |
| 339 credential_buffer_length_(0), | 343 credential_buffer_length_(0), |
| 340 credential_buffer_size_(kDefaultCredentialBufferSize) { | 344 credential_buffer_size_(kDefaultCredentialBufferSize) { |
| 341 } | 345 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 credential_buffer_size_ - credential_buffer_length_; | 471 credential_buffer_size_ - credential_buffer_length_; |
| 468 if (len > available) { | 472 if (len > available) { |
| 469 return false; | 473 return false; |
| 470 } | 474 } |
| 471 memcpy(credential_buffer_.get() + credential_buffer_length_, | 475 memcpy(credential_buffer_.get() + credential_buffer_length_, |
| 472 credential_data, len); | 476 credential_data, len); |
| 473 credential_buffer_length_ += len; | 477 credential_buffer_length_ += len; |
| 474 return true; | 478 return true; |
| 475 } | 479 } |
| 476 | 480 |
| 481 virtual void OnCompressedHeaderBlock(size_t decompressed_size, |
| 482 size_t compressed_size) OVERRIDE { |
| 483 last_decompressed_size_ = decompressed_size; |
| 484 last_compressed_size_ = compressed_size; |
| 485 } |
| 486 |
| 477 // Convenience function which runs a framer simulation with particular input. | 487 // Convenience function which runs a framer simulation with particular input. |
| 478 void SimulateInFramer(const unsigned char* input, size_t size) { | 488 void SimulateInFramer(const unsigned char* input, size_t size) { |
| 479 framer_.set_enable_compression(use_compression_); | 489 framer_.set_enable_compression(use_compression_); |
| 480 framer_.set_visitor(this); | 490 framer_.set_visitor(this); |
| 481 size_t input_remaining = size; | 491 size_t input_remaining = size; |
| 482 const char* input_ptr = reinterpret_cast<const char*>(input); | 492 const char* input_ptr = reinterpret_cast<const char*>(input); |
| 483 while (input_remaining > 0 && | 493 while (input_remaining > 0 && |
| 484 framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) { | 494 framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) { |
| 485 // To make the tests more interesting, we feed random (amd small) chunks | 495 // To make the tests more interesting, we feed random (amd small) chunks |
| 486 // into the framer. This simulates getting strange-sized reads from | 496 // into the framer. This simulates getting strange-sized reads from |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 int last_window_update_delta_; | 544 int last_window_update_delta_; |
| 535 int data_bytes_; | 545 int data_bytes_; |
| 536 int fin_frame_count_; // The count of RST_STREAM type frames received. | 546 int fin_frame_count_; // The count of RST_STREAM type frames received. |
| 537 int fin_flag_count_; // The count of frames with the FIN flag set. | 547 int fin_flag_count_; // The count of frames with the FIN flag set. |
| 538 int zero_length_data_frame_count_; // The count of zero-length data frames. | 548 int zero_length_data_frame_count_; // The count of zero-length data frames. |
| 539 int header_blocks_count_; | 549 int header_blocks_count_; |
| 540 int control_frame_header_data_count_; // The count of chunks received. | 550 int control_frame_header_data_count_; // The count of chunks received. |
| 541 // The count of zero-length control frame header data chunks received. | 551 // The count of zero-length control frame header data chunks received. |
| 542 int zero_length_control_frame_header_data_count_; | 552 int zero_length_control_frame_header_data_count_; |
| 543 int data_frame_count_; | 553 int data_frame_count_; |
| 554 size_t last_decompressed_size_; |
| 555 size_t last_compressed_size_; |
| 544 | 556 |
| 545 // Header block streaming state: | 557 // Header block streaming state: |
| 546 scoped_array<char> header_buffer_; | 558 scoped_array<char> header_buffer_; |
| 547 size_t header_buffer_length_; | 559 size_t header_buffer_length_; |
| 548 size_t header_buffer_size_; | 560 size_t header_buffer_size_; |
| 549 SpdyStreamId header_stream_id_; | 561 SpdyStreamId header_stream_id_; |
| 550 SpdyControlType header_control_type_; | 562 SpdyControlType header_control_type_; |
| 551 bool header_buffer_valid_; | 563 bool header_buffer_valid_; |
| 552 SpdyHeaderBlock headers_; | 564 SpdyHeaderBlock headers_; |
| 553 | 565 |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 | 907 |
| 896 TEST_P(SpdyFramerTest, BasicCompression) { | 908 TEST_P(SpdyFramerTest, BasicCompression) { |
| 897 SpdyHeaderBlock headers; | 909 SpdyHeaderBlock headers; |
| 898 headers["server"] = "SpdyServer 1.0"; | 910 headers["server"] = "SpdyServer 1.0"; |
| 899 headers["date"] = "Mon 12 Jan 2009 12:12:12 PST"; | 911 headers["date"] = "Mon 12 Jan 2009 12:12:12 PST"; |
| 900 headers["status"] = "200"; | 912 headers["status"] = "200"; |
| 901 headers["version"] = "HTTP/1.1"; | 913 headers["version"] = "HTTP/1.1"; |
| 902 headers["content-type"] = "text/html"; | 914 headers["content-type"] = "text/html"; |
| 903 headers["content-length"] = "12"; | 915 headers["content-length"] = "12"; |
| 904 | 916 |
| 917 TestSpdyVisitor visitor(spdy_version_); |
| 905 SpdyFramer framer(spdy_version_); | 918 SpdyFramer framer(spdy_version_); |
| 919 framer.set_debug_visitor(&visitor); |
| 906 framer.set_enable_compression(true); | 920 framer.set_enable_compression(true); |
| 907 scoped_ptr<SpdySynStreamControlFrame> frame1( | 921 scoped_ptr<SpdySynStreamControlFrame> frame1( |
| 908 framer.CreateSynStream(1, // stream id | 922 framer.CreateSynStream(1, // stream id |
| 909 0, // associated stream id | 923 0, // associated stream id |
| 910 1, // priority | 924 1, // priority |
| 911 0, // credential slot | 925 0, // credential slot |
| 912 CONTROL_FLAG_NONE, | 926 CONTROL_FLAG_NONE, |
| 913 true, // compress | 927 true, // compress |
| 914 &headers)); | 928 &headers)); |
| 929 if (IsSpdy2()) { |
| 930 EXPECT_EQ(139u, visitor.last_decompressed_size_); |
| 931 #if defined(USE_SYSTEM_ZLIB) |
| 932 EXPECT_EQ(93u, visitor.last_compressed_size_); |
| 933 #else // !defined(USE_SYSTEM_ZLIB) |
| 934 EXPECT_EQ(135u, visitor.last_compressed_size_); |
| 935 #endif // !defined(USE_SYSTEM_ZLIB) |
| 936 } else { |
| 937 EXPECT_EQ(165u, visitor.last_decompressed_size_); |
| 938 #if defined(USE_SYSTEM_ZLIB) |
| 939 EXPECT_EQ(72u, visitor.last_compressed_size_); |
| 940 #else // !defined(USE_SYSTEM_ZLIB) |
| 941 EXPECT_EQ(117u, visitor.last_compressed_size_); |
| 942 #endif // !defined(USE_SYSTEM_ZLIB) |
| 943 } |
| 915 scoped_ptr<SpdySynStreamControlFrame> frame2( | 944 scoped_ptr<SpdySynStreamControlFrame> frame2( |
| 916 framer.CreateSynStream(1, // stream id | 945 framer.CreateSynStream(1, // stream id |
| 917 0, // associated stream id | 946 0, // associated stream id |
| 918 1, // priority | 947 1, // priority |
| 919 0, // credential slot | 948 0, // credential slot |
| 920 CONTROL_FLAG_NONE, | 949 CONTROL_FLAG_NONE, |
| 921 true, // compress | 950 true, // compress |
| 922 &headers)); | 951 &headers)); |
| 923 | 952 |
| 924 // Expect the second frame to be more compact than the first. | 953 // Expect the second frame to be more compact than the first. |
| (...skipping 2287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3212 const uint32 kWireFormat = htonl(IsSpdy2() ? 0x04030201 : 0x01020304); | 3241 const uint32 kWireFormat = htonl(IsSpdy2() ? 0x04030201 : 0x01020304); |
| 3213 | 3242 |
| 3214 SettingsFlagsAndId id_and_flags = | 3243 SettingsFlagsAndId id_and_flags = |
| 3215 SettingsFlagsAndId::FromWireFormat(spdy_version_, kWireFormat); | 3244 SettingsFlagsAndId::FromWireFormat(spdy_version_, kWireFormat); |
| 3216 EXPECT_EQ(kId, id_and_flags.id()); | 3245 EXPECT_EQ(kId, id_and_flags.id()); |
| 3217 EXPECT_EQ(kFlags, id_and_flags.flags()); | 3246 EXPECT_EQ(kFlags, id_and_flags.flags()); |
| 3218 EXPECT_EQ(kWireFormat, id_and_flags.GetWireFormat(spdy_version_)); | 3247 EXPECT_EQ(kWireFormat, id_and_flags.GetWireFormat(spdy_version_)); |
| 3219 } | 3248 } |
| 3220 | 3249 |
| 3221 } // namespace net | 3250 } // namespace net |
| OLD | NEW |