| 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 "net/quic/core/quic_stream_sequencer.h" | 5 #include "net/quic/core/quic_stream_sequencer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstdint> | 8 #include <cstdint> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 protected: | 80 protected: |
| 81 QuicStreamSequencerTest() | 81 QuicStreamSequencerTest() |
| 82 : connection_(new MockQuicConnection(&helper_, | 82 : connection_(new MockQuicConnection(&helper_, |
| 83 &alarm_factory_, | 83 &alarm_factory_, |
| 84 Perspective::IS_CLIENT)), | 84 Perspective::IS_CLIENT)), |
| 85 session_(connection_), | 85 session_(connection_), |
| 86 stream_(&session_, 1), | 86 stream_(&session_, 1), |
| 87 sequencer_(new QuicStreamSequencer(&stream_, &clock_)) {} | 87 sequencer_(new QuicStreamSequencer(&stream_, &clock_)) {} |
| 88 | 88 |
| 89 // Verify that the data in first region match with the expected[0]. | 89 // Verify that the data in first region match with the expected[0]. |
| 90 bool VerifyReadableRegion(const vector<string>& expected) { | 90 bool VerifyReadableRegion(const std::vector<string>& expected) { |
| 91 iovec iovecs[1]; | 91 iovec iovecs[1]; |
| 92 if (sequencer_->GetReadableRegions(iovecs, 1)) { | 92 if (sequencer_->GetReadableRegions(iovecs, 1)) { |
| 93 return (VerifyIovecs(iovecs, 1, vector<string>{expected[0]})); | 93 return (VerifyIovecs(iovecs, 1, std::vector<string>{expected[0]})); |
| 94 } | 94 } |
| 95 return false; | 95 return false; |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Verify that the data in each of currently readable regions match with each | 98 // Verify that the data in each of currently readable regions match with each |
| 99 // item given in |expected|. | 99 // item given in |expected|. |
| 100 bool VerifyReadableRegions(const vector<string>& expected) { | 100 bool VerifyReadableRegions(const std::vector<string>& expected) { |
| 101 iovec iovecs[5]; | 101 iovec iovecs[5]; |
| 102 size_t num_iovecs = | 102 size_t num_iovecs = |
| 103 sequencer_->GetReadableRegions(iovecs, arraysize(iovecs)); | 103 sequencer_->GetReadableRegions(iovecs, arraysize(iovecs)); |
| 104 return VerifyReadableRegion(expected) && | 104 return VerifyReadableRegion(expected) && |
| 105 VerifyIovecs(iovecs, num_iovecs, expected); | 105 VerifyIovecs(iovecs, num_iovecs, expected); |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool VerifyIovecs(iovec* iovecs, | 108 bool VerifyIovecs(iovec* iovecs, |
| 109 size_t num_iovecs, | 109 size_t num_iovecs, |
| 110 const vector<string>& expected) { | 110 const std::vector<string>& expected) { |
| 111 int start_position = 0; | 111 int start_position = 0; |
| 112 for (size_t i = 0; i < num_iovecs; ++i) { | 112 for (size_t i = 0; i < num_iovecs; ++i) { |
| 113 if (!VerifyIovec(iovecs[i], | 113 if (!VerifyIovec(iovecs[i], |
| 114 expected[0].substr(start_position, iovecs[i].iov_len))) { | 114 expected[0].substr(start_position, iovecs[i].iov_len))) { |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 start_position += iovecs[i].iov_len; | 117 start_position += iovecs[i].iov_len; |
| 118 } | 118 } |
| 119 return true; | 119 return true; |
| 120 } | 120 } |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 EXPECT_CALL(stream_, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS)); | 383 EXPECT_CALL(stream_, Reset(QUIC_MULTIPLE_TERMINATION_OFFSETS)); |
| 384 OnFinFrame(1, ""); | 384 OnFinFrame(1, ""); |
| 385 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get())); | 385 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get())); |
| 386 | 386 |
| 387 OnFinFrame(3, ""); | 387 OnFinFrame(3, ""); |
| 388 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get())); | 388 EXPECT_EQ(3u, QuicStreamSequencerPeer::GetCloseOffset(sequencer_.get())); |
| 389 } | 389 } |
| 390 | 390 |
| 391 class QuicSequencerRandomTest : public QuicStreamSequencerTest { | 391 class QuicSequencerRandomTest : public QuicStreamSequencerTest { |
| 392 public: | 392 public: |
| 393 typedef pair<int, string> Frame; | 393 typedef std::pair<int, string> Frame; |
| 394 typedef vector<Frame> FrameList; | 394 typedef std::vector<Frame> FrameList; |
| 395 | 395 |
| 396 void CreateFrames() { | 396 void CreateFrames() { |
| 397 int payload_size = arraysize(kPayload) - 1; | 397 int payload_size = arraysize(kPayload) - 1; |
| 398 int remaining_payload = payload_size; | 398 int remaining_payload = payload_size; |
| 399 while (remaining_payload != 0) { | 399 while (remaining_payload != 0) { |
| 400 int size = min(OneToN(6), remaining_payload); | 400 int size = min(OneToN(6), remaining_payload); |
| 401 int index = payload_size - remaining_payload; | 401 int index = payload_size - remaining_payload; |
| 402 list_.push_back(std::make_pair(index, string(kPayload + index, size))); | 402 list_.push_back(std::make_pair(index, string(kPayload + index, size))); |
| 403 remaining_payload -= size; | 403 remaining_payload -= size; |
| 404 } | 404 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 EXPECT_CALL(stream_, OnDataAvailable()); | 497 EXPECT_CALL(stream_, OnDataAvailable()); |
| 498 | 498 |
| 499 OnFrame(0, "abc"); | 499 OnFrame(0, "abc"); |
| 500 OnFrame(3, "def"); | 500 OnFrame(3, "def"); |
| 501 OnFrame(6, "ghi"); | 501 OnFrame(6, "ghi"); |
| 502 | 502 |
| 503 // abcdefghi buffered. | 503 // abcdefghi buffered. |
| 504 EXPECT_EQ(9u, sequencer_->NumBytesBuffered()); | 504 EXPECT_EQ(9u, sequencer_->NumBytesBuffered()); |
| 505 | 505 |
| 506 // Peek into the data. | 506 // Peek into the data. |
| 507 vector<string> expected = {"abcdefghi"}; | 507 std::vector<string> expected = {"abcdefghi"}; |
| 508 ASSERT_TRUE(VerifyReadableRegions(expected)); | 508 ASSERT_TRUE(VerifyReadableRegions(expected)); |
| 509 | 509 |
| 510 // Consume 1 byte. | 510 // Consume 1 byte. |
| 511 sequencer_->MarkConsumed(1); | 511 sequencer_->MarkConsumed(1); |
| 512 EXPECT_EQ(1u, stream_.flow_controller()->bytes_consumed()); | 512 EXPECT_EQ(1u, stream_.flow_controller()->bytes_consumed()); |
| 513 // Verify data. | 513 // Verify data. |
| 514 vector<string> expected2 = {"bcdefghi"}; | 514 std::vector<string> expected2 = {"bcdefghi"}; |
| 515 ASSERT_TRUE(VerifyReadableRegions(expected2)); | 515 ASSERT_TRUE(VerifyReadableRegions(expected2)); |
| 516 EXPECT_EQ(8u, sequencer_->NumBytesBuffered()); | 516 EXPECT_EQ(8u, sequencer_->NumBytesBuffered()); |
| 517 | 517 |
| 518 // Consume 2 bytes. | 518 // Consume 2 bytes. |
| 519 sequencer_->MarkConsumed(2); | 519 sequencer_->MarkConsumed(2); |
| 520 EXPECT_EQ(3u, stream_.flow_controller()->bytes_consumed()); | 520 EXPECT_EQ(3u, stream_.flow_controller()->bytes_consumed()); |
| 521 // Verify data. | 521 // Verify data. |
| 522 vector<string> expected3 = {"defghi"}; | 522 std::vector<string> expected3 = {"defghi"}; |
| 523 ASSERT_TRUE(VerifyReadableRegions(expected3)); | 523 ASSERT_TRUE(VerifyReadableRegions(expected3)); |
| 524 EXPECT_EQ(6u, sequencer_->NumBytesBuffered()); | 524 EXPECT_EQ(6u, sequencer_->NumBytesBuffered()); |
| 525 | 525 |
| 526 // Consume 5 bytes. | 526 // Consume 5 bytes. |
| 527 sequencer_->MarkConsumed(5); | 527 sequencer_->MarkConsumed(5); |
| 528 EXPECT_EQ(8u, stream_.flow_controller()->bytes_consumed()); | 528 EXPECT_EQ(8u, stream_.flow_controller()->bytes_consumed()); |
| 529 // Verify data. | 529 // Verify data. |
| 530 vector<string> expected4{"i"}; | 530 std::vector<string> expected4{"i"}; |
| 531 ASSERT_TRUE(VerifyReadableRegions(expected4)); | 531 ASSERT_TRUE(VerifyReadableRegions(expected4)); |
| 532 EXPECT_EQ(1u, sequencer_->NumBytesBuffered()); | 532 EXPECT_EQ(1u, sequencer_->NumBytesBuffered()); |
| 533 } | 533 } |
| 534 | 534 |
| 535 TEST_F(QuicStreamSequencerTest, MarkConsumedError) { | 535 TEST_F(QuicStreamSequencerTest, MarkConsumedError) { |
| 536 EXPECT_CALL(stream_, OnDataAvailable()); | 536 EXPECT_CALL(stream_, OnDataAvailable()); |
| 537 | 537 |
| 538 OnFrame(0, "abc"); | 538 OnFrame(0, "abc"); |
| 539 OnFrame(9, "jklmnopqrstuvwxyz"); | 539 OnFrame(9, "jklmnopqrstuvwxyz"); |
| 540 | 540 |
| 541 // Peek into the data. Only the first chunk should be readable because of the | 541 // Peek into the data. Only the first chunk should be readable because of the |
| 542 // missing data. | 542 // missing data. |
| 543 vector<string> expected{"abc"}; | 543 std::vector<string> expected{"abc"}; |
| 544 ASSERT_TRUE(VerifyReadableRegions(expected)); | 544 ASSERT_TRUE(VerifyReadableRegions(expected)); |
| 545 | 545 |
| 546 // Now, attempt to mark consumed more data than was readable and expect the | 546 // Now, attempt to mark consumed more data than was readable and expect the |
| 547 // stream to be closed. | 547 // stream to be closed. |
| 548 EXPECT_CALL(stream_, Reset(QUIC_ERROR_PROCESSING_STREAM)); | 548 EXPECT_CALL(stream_, Reset(QUIC_ERROR_PROCESSING_STREAM)); |
| 549 EXPECT_QUIC_BUG(sequencer_->MarkConsumed(4), | 549 EXPECT_QUIC_BUG(sequencer_->MarkConsumed(4), |
| 550 "Invalid argument to MarkConsumed." | 550 "Invalid argument to MarkConsumed." |
| 551 " expect to consume: 4, but not enough bytes available."); | 551 " expect to consume: 4, but not enough bytes available."); |
| 552 } | 552 } |
| 553 | 553 |
| 554 TEST_F(QuicStreamSequencerTest, MarkConsumedWithMissingPacket) { | 554 TEST_F(QuicStreamSequencerTest, MarkConsumedWithMissingPacket) { |
| 555 InSequence s; | 555 InSequence s; |
| 556 EXPECT_CALL(stream_, OnDataAvailable()); | 556 EXPECT_CALL(stream_, OnDataAvailable()); |
| 557 | 557 |
| 558 OnFrame(0, "abc"); | 558 OnFrame(0, "abc"); |
| 559 OnFrame(3, "def"); | 559 OnFrame(3, "def"); |
| 560 // Missing packet: 6, ghi. | 560 // Missing packet: 6, ghi. |
| 561 OnFrame(9, "jkl"); | 561 OnFrame(9, "jkl"); |
| 562 | 562 |
| 563 vector<string> expected = {"abcdef"}; | 563 std::vector<string> expected = {"abcdef"}; |
| 564 ASSERT_TRUE(VerifyReadableRegions(expected)); | 564 ASSERT_TRUE(VerifyReadableRegions(expected)); |
| 565 | 565 |
| 566 sequencer_->MarkConsumed(6); | 566 sequencer_->MarkConsumed(6); |
| 567 } | 567 } |
| 568 | 568 |
| 569 TEST_F(QuicStreamSequencerTest, DontAcceptOverlappingFrames) { | 569 TEST_F(QuicStreamSequencerTest, DontAcceptOverlappingFrames) { |
| 570 // The peer should never send us non-identical stream frames which contain | 570 // The peer should never send us non-identical stream frames which contain |
| 571 // overlapping byte ranges - if they do, we close the connection. | 571 // overlapping byte ranges - if they do, we close the connection. |
| 572 | 572 |
| 573 QuicStreamFrame frame1(kClientDataStreamId1, false, 1, StringPiece("hello")); | 573 QuicStreamFrame frame1(kClientDataStreamId1, false, 1, StringPiece("hello")); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 // Pass in a null iovec, expect to tear down connection. | 684 // Pass in a null iovec, expect to tear down connection. |
| 685 EXPECT_CALL(stream_, CloseConnectionWithDetails( | 685 EXPECT_CALL(stream_, CloseConnectionWithDetails( |
| 686 QUIC_STREAM_SEQUENCER_INVALID_STATE, _)); | 686 QUIC_STREAM_SEQUENCER_INVALID_STATE, _)); |
| 687 iovec iov{nullptr, 512}; | 687 iovec iov{nullptr, 512}; |
| 688 sequencer_->Readv(&iov, 1u); | 688 sequencer_->Readv(&iov, 1u); |
| 689 } | 689 } |
| 690 | 690 |
| 691 } // namespace | 691 } // namespace |
| 692 } // namespace test | 692 } // namespace test |
| 693 } // namespace net | 693 } // namespace net |
| OLD | NEW |