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