| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 7 |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "net/spdy/spdy_framer.h" | 9 #include "net/spdy/spdy_framer.h" |
| 10 #include "net/spdy/spdy_protocol.h" | 10 #include "net/spdy/spdy_protocol.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 headers["alpha"] = "beta"; | 141 headers["alpha"] = "beta"; |
| 142 headers["gamma"] = "charlie"; | 142 headers["gamma"] = "charlie"; |
| 143 SpdyFramer framer; | 143 SpdyFramer framer; |
| 144 | 144 |
| 145 // Encode the header block into a SynStream frame. | 145 // Encode the header block into a SynStream frame. |
| 146 scoped_ptr<SpdySynStreamControlFrame> frame( | 146 scoped_ptr<SpdySynStreamControlFrame> frame( |
| 147 framer.CreateSynStream(1, 0, 1, CONTROL_FLAG_NONE, true, &headers)); | 147 framer.CreateSynStream(1, 0, 1, CONTROL_FLAG_NONE, true, &headers)); |
| 148 EXPECT_TRUE(frame.get() != NULL); | 148 EXPECT_TRUE(frame.get() != NULL); |
| 149 | 149 |
| 150 SpdyHeaderBlock new_headers; | 150 SpdyHeaderBlock new_headers; |
| 151 framer.ParseHeaderBlock(frame.get(), &new_headers); | 151 EXPECT_TRUE(framer.ParseHeaderBlock(frame.get(), &new_headers)); |
| 152 | 152 |
| 153 EXPECT_EQ(headers.size(), new_headers.size()); | 153 EXPECT_EQ(headers.size(), new_headers.size()); |
| 154 EXPECT_EQ(headers["alpha"], new_headers["alpha"]); | 154 EXPECT_EQ(headers["alpha"], new_headers["alpha"]); |
| 155 EXPECT_EQ(headers["gamma"], new_headers["gamma"]); | 155 EXPECT_EQ(headers["gamma"], new_headers["gamma"]); |
| 156 } | 156 } |
| 157 | 157 |
| 158 TEST_F(SpdyFramerTest, OutOfOrderHeaders) { | 158 TEST_F(SpdyFramerTest, OutOfOrderHeaders) { |
| 159 SpdyFrameBuilder frame; | 159 SpdyFrameBuilder frame; |
| 160 | 160 |
| 161 frame.WriteUInt16(kControlFlagMask | 1); | 161 frame.WriteUInt16(kControlFlagMask | 1); |
| 162 frame.WriteUInt16(SYN_STREAM); | 162 frame.WriteUInt16(SYN_STREAM); |
| 163 frame.WriteUInt32(0); // Placeholder for the length. | 163 frame.WriteUInt32(0); // Placeholder for the length. |
| 164 frame.WriteUInt32(3); // stream_id | 164 frame.WriteUInt32(3); // stream_id |
| 165 frame.WriteUInt32(0); // associated stream id |
| 165 frame.WriteUInt16(0); // Priority. | 166 frame.WriteUInt16(0); // Priority. |
| 166 | 167 |
| 167 frame.WriteUInt16(2); // Number of headers. | 168 frame.WriteUInt16(2); // Number of headers. |
| 168 SpdyHeaderBlock::iterator it; | 169 SpdyHeaderBlock::iterator it; |
| 169 frame.WriteString("gamma"); | 170 frame.WriteString("gamma"); |
| 170 frame.WriteString("gamma"); | 171 frame.WriteString("gamma"); |
| 171 frame.WriteString("alpha"); | 172 frame.WriteString("alpha"); |
| 172 frame.WriteString("alpha"); | 173 frame.WriteString("alpha"); |
| 173 // write the length | 174 // write the length |
| 174 frame.WriteUInt32ToOffset(4, frame.length() - SpdyFrame::size()); | 175 frame.WriteUInt32ToOffset(4, frame.length() - SpdyFrame::size()); |
| 175 | 176 |
| 176 SpdyHeaderBlock new_headers; | 177 SpdyHeaderBlock new_headers; |
| 177 scoped_ptr<SpdyFrame> control_frame(frame.take()); | 178 scoped_ptr<SpdyFrame> control_frame(frame.take()); |
| 178 SpdyFramer framer; | 179 SpdyFramer framer; |
| 179 FramerSetEnableCompressionHelper(&framer, false); | 180 FramerSetEnableCompressionHelper(&framer, false); |
| 180 EXPECT_TRUE(framer.ParseHeaderBlock(control_frame.get(), &new_headers)); | 181 EXPECT_TRUE(framer.ParseHeaderBlock(control_frame.get(), &new_headers)); |
| 181 } | 182 } |
| 182 | 183 |
| 184 TEST_F(SpdyFramerTest, WrongNumberOfHeaders) { |
| 185 SpdyFrameBuilder frame1; |
| 186 SpdyFrameBuilder frame2; |
| 187 |
| 188 // a frame with smaller number of actual headers |
| 189 frame1.WriteUInt16(kControlFlagMask | 1); |
| 190 frame1.WriteUInt16(SYN_STREAM); |
| 191 frame1.WriteUInt32(0); // Placeholder for the length. |
| 192 frame1.WriteUInt32(3); // stream_id |
| 193 frame1.WriteUInt32(0); // associated stream id |
| 194 frame1.WriteUInt16(0); // Priority. |
| 195 |
| 196 frame1.WriteUInt16(1); // Wrong number of headers (underflow) |
| 197 frame1.WriteString("gamma"); |
| 198 frame1.WriteString("gamma"); |
| 199 frame1.WriteString("alpha"); |
| 200 frame1.WriteString("alpha"); |
| 201 // write the length |
| 202 frame1.WriteUInt32ToOffset(4, frame1.length() - SpdyFrame::size()); |
| 203 |
| 204 // a frame with larger number of actual headers |
| 205 frame2.WriteUInt16(kControlFlagMask | 1); |
| 206 frame2.WriteUInt16(SYN_STREAM); |
| 207 frame2.WriteUInt32(0); // Placeholder for the length. |
| 208 frame2.WriteUInt32(3); // stream_id |
| 209 frame2.WriteUInt32(0); // associated stream id |
| 210 frame2.WriteUInt16(0); // Priority. |
| 211 |
| 212 frame2.WriteUInt16(100); // Wrong number of headers (overflow) |
| 213 frame2.WriteString("gamma"); |
| 214 frame2.WriteString("gamma"); |
| 215 frame2.WriteString("alpha"); |
| 216 frame2.WriteString("alpha"); |
| 217 // write the length |
| 218 frame2.WriteUInt32ToOffset(4, frame2.length() - SpdyFrame::size()); |
| 219 |
| 220 SpdyHeaderBlock new_headers; |
| 221 scoped_ptr<SpdyFrame> syn_frame1(frame1.take()); |
| 222 scoped_ptr<SpdyFrame> syn_frame2(frame2.take()); |
| 223 SpdyFramer framer; |
| 224 FramerSetEnableCompressionHelper(&framer, false); |
| 225 EXPECT_FALSE(framer.ParseHeaderBlock(syn_frame1.get(), &new_headers)); |
| 226 EXPECT_FALSE(framer.ParseHeaderBlock(syn_frame2.get(), &new_headers)); |
| 227 } |
| 228 |
| 183 TEST_F(SpdyFramerTest, DuplicateHeader) { | 229 TEST_F(SpdyFramerTest, DuplicateHeader) { |
| 184 SpdyFrameBuilder frame; | 230 SpdyFrameBuilder frame; |
| 185 | 231 |
| 186 frame.WriteUInt16(kControlFlagMask | 1); | 232 frame.WriteUInt16(kControlFlagMask | 1); |
| 187 frame.WriteUInt16(SYN_STREAM); | 233 frame.WriteUInt16(SYN_STREAM); |
| 188 frame.WriteUInt32(0); // Placeholder for the length. | 234 frame.WriteUInt32(0); // Placeholder for the length. |
| 189 frame.WriteUInt32(3); // stream_id | 235 frame.WriteUInt32(3); // stream_id |
| 190 frame.WriteUInt32(0); // associated stream id | 236 frame.WriteUInt32(0); // associated stream id |
| 191 frame.WriteUInt16(0); // Priority. | 237 frame.WriteUInt16(0); // Priority. |
| 192 | 238 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 210 TEST_F(SpdyFramerTest, MultiValueHeader) { | 256 TEST_F(SpdyFramerTest, MultiValueHeader) { |
| 211 SpdyFrameBuilder frame; | 257 SpdyFrameBuilder frame; |
| 212 | 258 |
| 213 frame.WriteUInt16(kControlFlagMask | 1); | 259 frame.WriteUInt16(kControlFlagMask | 1); |
| 214 frame.WriteUInt16(SYN_STREAM); | 260 frame.WriteUInt16(SYN_STREAM); |
| 215 frame.WriteUInt32(0); // Placeholder for the length. | 261 frame.WriteUInt32(0); // Placeholder for the length. |
| 216 frame.WriteUInt32(3); // stream_id | 262 frame.WriteUInt32(3); // stream_id |
| 217 frame.WriteUInt32(0); // associated stream id | 263 frame.WriteUInt32(0); // associated stream id |
| 218 frame.WriteUInt16(0); // Priority. | 264 frame.WriteUInt16(0); // Priority. |
| 219 | 265 |
| 220 frame.WriteUInt16(2); // Number of headers. | 266 frame.WriteUInt16(1); // Number of headers. |
| 221 SpdyHeaderBlock::iterator it; | 267 SpdyHeaderBlock::iterator it; |
| 222 frame.WriteString("name"); | 268 frame.WriteString("name"); |
| 223 std::string value("value1\0value2"); | 269 std::string value("value1\0value2"); |
| 224 frame.WriteString(value); | 270 frame.WriteString(value); |
| 225 // write the length | 271 // write the length |
| 226 frame.WriteUInt32ToOffset(4, frame.length() - SpdyFrame::size()); | 272 frame.WriteUInt32ToOffset(4, frame.length() - SpdyFrame::size()); |
| 227 | 273 |
| 228 SpdyHeaderBlock new_headers; | 274 SpdyHeaderBlock new_headers; |
| 229 scoped_ptr<SpdyFrame> control_frame(frame.take()); | 275 scoped_ptr<SpdyFrame> control_frame(frame.take()); |
| 230 SpdyFramer framer; | 276 SpdyFramer framer; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 const unsigned char input[] = { | 465 const unsigned char input[] = { |
| 420 0x80, 0x01, 0x00, 0x01, // SYN Stream #1 | 466 0x80, 0x01, 0x00, 0x01, // SYN Stream #1 |
| 421 0x00, 0x00, 0x00, 0x14, | 467 0x00, 0x00, 0x00, 0x14, |
| 422 0x00, 0x00, 0x00, 0x01, | 468 0x00, 0x00, 0x00, 0x01, |
| 423 0x00, 0x00, 0x00, 0x00, | 469 0x00, 0x00, 0x00, 0x00, |
| 424 0x00, 0x00, 0x00, 0x01, | 470 0x00, 0x00, 0x00, 0x01, |
| 425 0x00, 0x02, 'h', 'h', | 471 0x00, 0x02, 'h', 'h', |
| 426 0x00, 0x02, 'v', 'v', | 472 0x00, 0x02, 'v', 'v', |
| 427 | 473 |
| 428 0x80, 0x01, 0x00, 0x02, // SYN REPLY Stream #1 | 474 0x80, 0x01, 0x00, 0x02, // SYN REPLY Stream #1 |
| 429 0x01, 0x00, 0x00, 0x14, | 475 0x01, 0x00, 0x00, 0x10, |
| 430 0x00, 0x00, 0x00, 0x01, | 476 0x00, 0x00, 0x00, 0x01, |
| 431 0x00, 0x00, 0x00, 0x00, | |
| 432 0x00, 0x00, 0x00, 0x01, | 477 0x00, 0x00, 0x00, 0x01, |
| 433 0x00, 0x02, 'a', 'a', | 478 0x00, 0x02, 'a', 'a', |
| 434 0x00, 0x02, 'b', 'b', | 479 0x00, 0x02, 'b', 'b', |
| 435 }; | 480 }; |
| 436 | 481 |
| 437 TestSpdyVisitor visitor; | 482 TestSpdyVisitor visitor; |
| 438 visitor.SimulateInFramer(input, sizeof(input)); | 483 visitor.SimulateInFramer(input, sizeof(input)); |
| 439 | 484 |
| 440 EXPECT_EQ(0, visitor.error_count_); | 485 EXPECT_EQ(0, visitor.error_count_); |
| 441 EXPECT_EQ(1, visitor.syn_frame_count_); | 486 EXPECT_EQ(1, visitor.syn_frame_count_); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 EXPECT_EQ(2, send_framer.num_stream_compressors()); | 585 EXPECT_EQ(2, send_framer.num_stream_compressors()); |
| 541 EXPECT_EQ(0, send_framer.num_stream_decompressors()); | 586 EXPECT_EQ(0, send_framer.num_stream_decompressors()); |
| 542 EXPECT_EQ(0, recv_framer.num_stream_compressors()); | 587 EXPECT_EQ(0, recv_framer.num_stream_compressors()); |
| 543 EXPECT_EQ(2, recv_framer.num_stream_decompressors()); | 588 EXPECT_EQ(2, recv_framer.num_stream_decompressors()); |
| 544 } | 589 } |
| 545 | 590 |
| 546 // Verify we don't leak when we leave streams unclosed | 591 // Verify we don't leak when we leave streams unclosed |
| 547 TEST_F(SpdyFramerTest, UnclosedStreamDataCompressors) { | 592 TEST_F(SpdyFramerTest, UnclosedStreamDataCompressors) { |
| 548 SpdyFramer send_framer; | 593 SpdyFramer send_framer; |
| 549 | 594 |
| 550 FramerSetEnableCompressionHelper(&send_framer, true); | 595 FramerSetEnableCompressionHelper(&send_framer, false); |
| 551 | 596 |
| 552 const char kHeader1[] = "header1"; | 597 const char kHeader1[] = "header1"; |
| 553 const char kHeader2[] = "header2"; | 598 const char kHeader2[] = "header2"; |
| 554 const char kValue1[] = "value1"; | 599 const char kValue1[] = "value1"; |
| 555 const char kValue2[] = "value2"; | 600 const char kValue2[] = "value2"; |
| 556 | 601 |
| 557 SpdyHeaderBlock block; | 602 SpdyHeaderBlock block; |
| 558 block[kHeader1] = kValue1; | 603 block[kHeader1] = kValue1; |
| 559 block[kHeader2] = kValue2; | 604 block[kHeader2] = kValue2; |
| 560 SpdyControlFlags flags(CONTROL_FLAG_NONE); | 605 SpdyControlFlags flags(CONTROL_FLAG_NONE); |
| 561 scoped_ptr<spdy::SpdyFrame> syn_frame( | 606 scoped_ptr<spdy::SpdyFrame> syn_frame( |
| 562 send_framer.CreateSynStream(1, 0, 0, flags, true, &block)); | 607 send_framer.CreateSynStream(1, 0, 0, flags, true, &block)); |
| 563 EXPECT_TRUE(syn_frame.get() != NULL); | 608 EXPECT_TRUE(syn_frame.get() != NULL); |
| 564 | 609 |
| 565 const char bytes[] = "this is a test test test test test!"; | 610 const char bytes[] = "this is a test test test test test!"; |
| 566 scoped_ptr<SpdyFrame> send_frame( | 611 scoped_ptr<SpdyFrame> send_frame( |
| 567 send_framer.CreateDataFrame(1, bytes, arraysize(bytes), | 612 send_framer.CreateDataFrame(1, bytes, arraysize(bytes), |
| 568 DATA_FLAG_FIN | DATA_FLAG_COMPRESSED)); | 613 DATA_FLAG_FIN)); |
| 569 EXPECT_TRUE(send_frame.get() != NULL); | 614 EXPECT_TRUE(send_frame.get() != NULL); |
| 570 | 615 |
| 571 // Run the inputs through the framer. | 616 // Run the inputs through the framer. |
| 572 TestSpdyVisitor visitor; | 617 TestSpdyVisitor visitor; |
| 573 const unsigned char* data; | 618 const unsigned char* data; |
| 574 data = reinterpret_cast<const unsigned char*>(syn_frame->data()); | 619 data = reinterpret_cast<const unsigned char*>(syn_frame->data()); |
| 575 visitor.SimulateInFramer(data, syn_frame->length() + SpdyFrame::size()); | 620 visitor.SimulateInFramer(data, syn_frame->length() + SpdyFrame::size()); |
| 576 data = reinterpret_cast<const unsigned char*>(send_frame->data()); | 621 data = reinterpret_cast<const unsigned char*>(send_frame->data()); |
| 577 visitor.SimulateInFramer(data, send_frame->length() + SpdyFrame::size()); | 622 visitor.SimulateInFramer(data, send_frame->length() + SpdyFrame::size()); |
| 578 | 623 |
| 579 EXPECT_EQ(0, visitor.error_count_); | 624 EXPECT_EQ(0, visitor.error_count_); |
| 580 EXPECT_EQ(1, visitor.syn_frame_count_); | 625 EXPECT_EQ(1, visitor.syn_frame_count_); |
| 581 EXPECT_EQ(0, visitor.syn_reply_frame_count_); | 626 EXPECT_EQ(0, visitor.syn_reply_frame_count_); |
| 582 EXPECT_EQ(arraysize(bytes), static_cast<unsigned>(visitor.data_bytes_)); | 627 EXPECT_EQ(arraysize(bytes), static_cast<unsigned>(visitor.data_bytes_)); |
| 583 EXPECT_EQ(0, visitor.fin_frame_count_); | 628 EXPECT_EQ(0, visitor.fin_frame_count_); |
| 584 EXPECT_EQ(0, visitor.fin_flag_count_); | 629 EXPECT_EQ(0, visitor.fin_flag_count_); |
| 585 EXPECT_EQ(1, visitor.zero_length_data_frame_count_); | 630 EXPECT_EQ(1, visitor.zero_length_data_frame_count_); |
| 586 | 631 |
| 587 // We closed the streams, so all compressors should be down. | 632 // We closed the streams, so all compressors should be down. |
| 588 EXPECT_EQ(0, visitor.framer_.num_stream_compressors()); | 633 EXPECT_EQ(0, visitor.framer_.num_stream_compressors()); |
| 589 EXPECT_EQ(0, visitor.framer_.num_stream_decompressors()); | 634 EXPECT_EQ(0, visitor.framer_.num_stream_decompressors()); |
| 590 EXPECT_EQ(0, send_framer.num_stream_compressors()); | 635 EXPECT_EQ(0, send_framer.num_stream_compressors()); |
| 591 EXPECT_EQ(0, send_framer.num_stream_decompressors()); | 636 EXPECT_EQ(0, send_framer.num_stream_decompressors()); |
| 592 } | 637 } |
| 593 | 638 |
| 594 } // namespace | 639 } // namespace |
| 595 | 640 |
| OLD | NEW |