vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/quic_transport/quic_transport_stream.h" |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 6 | |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
| 9 | #include "net/third_party/quiche/src/quic/core/frames/quic_window_update_frame.h" |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 13 | #include "net/third_party/quiche/src/quic/quic_transport/quic_transport_session_interface.h" |
| 14 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
| 15 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/test_tools/quic_transport_test_tools.h" |
dmcardle | 1ec1119 | 2019-12-12 10:36:42 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | namespace test { |
| 21 | namespace { |
| 22 | |
vasilvv | 54deda7 | 2020-01-07 11:19:36 -0800 | [diff] [blame] | 23 | using testing::_; |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 24 | using testing::Return; |
| 25 | |
| 26 | ParsedQuicVersionVector GetVersions() { |
vasilvv | 7e5e609 | 2020-03-02 15:52:33 -0800 | [diff] [blame] | 27 | return {DefaultVersionForQuicTransport()}; |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | class MockQuicTransportSessionInterface : public QuicTransportSessionInterface { |
| 31 | public: |
wub | 713afae | 2020-04-27 07:48:31 -0700 | [diff] [blame] | 32 | MOCK_METHOD(bool, IsSessionReady, (), (const, override)); |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 35 | class QuicTransportStreamTest : public QuicTest { |
| 36 | public: |
| 37 | QuicTransportStreamTest() |
| 38 | : connection_(new MockQuicConnection(&helper_, |
| 39 | &alarm_factory_, |
| 40 | Perspective::IS_CLIENT, |
| 41 | GetVersions())), |
| 42 | session_(connection_) { |
vasilvv | 7e5e609 | 2020-03-02 15:52:33 -0800 | [diff] [blame] | 43 | QuicEnableVersion(DefaultVersionForQuicTransport()); |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 44 | session_.Initialize(); |
| 45 | |
| 46 | stream_ = new QuicTransportStream(0, &session_, &interface_); |
| 47 | session_.ActivateStream(QuicWrapUnique(stream_)); |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 48 | |
| 49 | auto visitor = std::make_unique<MockStreamVisitor>(); |
| 50 | visitor_ = visitor.get(); |
| 51 | stream_->set_visitor(std::move(visitor)); |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
dmcardle | 1ec1119 | 2019-12-12 10:36:42 -0800 | [diff] [blame] | 54 | void ReceiveStreamData(quiche::QuicheStringPiece data, |
| 55 | QuicStreamOffset offset) { |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 56 | QuicStreamFrame frame(0, false, offset, data); |
| 57 | stream_->OnStreamFrame(frame); |
| 58 | } |
| 59 | |
| 60 | protected: |
| 61 | MockAlarmFactory alarm_factory_; |
| 62 | MockQuicConnectionHelper helper_; |
| 63 | |
| 64 | MockQuicConnection* connection_; // Owned by |session_|. |
| 65 | MockQuicSession session_; |
| 66 | MockQuicTransportSessionInterface interface_; |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 67 | QuicTransportStream* stream_; // Owned by |session_|. |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 68 | MockStreamVisitor* visitor_; // Owned by |stream_|. |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | TEST_F(QuicTransportStreamTest, NotReady) { |
| 72 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(false)); |
| 73 | ReceiveStreamData("test", 0); |
| 74 | EXPECT_EQ(stream_->ReadableBytes(), 0u); |
| 75 | EXPECT_FALSE(stream_->CanWrite()); |
| 76 | } |
| 77 | |
| 78 | TEST_F(QuicTransportStreamTest, ReadWhenNotReady) { |
| 79 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(false)); |
| 80 | ReceiveStreamData("test", 0); |
| 81 | char buffer[4]; |
| 82 | QuicByteCount bytes_read = stream_->Read(buffer, sizeof(buffer)); |
| 83 | EXPECT_EQ(bytes_read, 0u); |
| 84 | } |
| 85 | |
| 86 | TEST_F(QuicTransportStreamTest, WriteWhenNotReady) { |
| 87 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(false)); |
| 88 | EXPECT_FALSE(stream_->Write("test")); |
| 89 | } |
| 90 | |
| 91 | TEST_F(QuicTransportStreamTest, Ready) { |
| 92 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
| 93 | ReceiveStreamData("test", 0); |
| 94 | EXPECT_EQ(stream_->ReadableBytes(), 4u); |
| 95 | EXPECT_TRUE(stream_->CanWrite()); |
| 96 | EXPECT_TRUE(stream_->Write("test")); |
| 97 | } |
| 98 | |
| 99 | TEST_F(QuicTransportStreamTest, ReceiveData) { |
| 100 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 101 | EXPECT_CALL(*visitor_, OnCanRead()); |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 102 | ReceiveStreamData("test", 0); |
| 103 | } |
| 104 | |
vasilvv | d88f162 | 2019-11-04 13:50:53 -0800 | [diff] [blame] | 105 | TEST_F(QuicTransportStreamTest, FinReadWithNoDataPending) { |
| 106 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
| 107 | EXPECT_CALL(*visitor_, OnFinRead()); |
| 108 | QuicStreamFrame frame(0, true, 0, ""); |
| 109 | stream_->OnStreamFrame(frame); |
| 110 | } |
| 111 | |
| 112 | TEST_F(QuicTransportStreamTest, FinReadWithDataPending) { |
| 113 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
| 114 | |
| 115 | EXPECT_CALL(*visitor_, OnCanRead()); |
| 116 | EXPECT_CALL(*visitor_, OnFinRead()).Times(0); |
| 117 | QuicStreamFrame frame(0, true, 0, "test"); |
| 118 | stream_->OnStreamFrame(frame); |
| 119 | |
| 120 | EXPECT_CALL(*visitor_, OnFinRead()).Times(1); |
| 121 | std::string buffer; |
| 122 | ASSERT_EQ(stream_->Read(&buffer), 4u); |
| 123 | } |
| 124 | |
vasilvv | 4e6ab33 | 2020-01-07 07:54:07 -0800 | [diff] [blame] | 125 | TEST_F(QuicTransportStreamTest, WritingTooMuchData) { |
| 126 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
| 127 | ASSERT_TRUE(stream_->CanWrite()); |
| 128 | |
| 129 | std::string a_little_bit_of_data(128, 'A'); |
| 130 | std::string a_lot_of_data(GetQuicFlag(FLAGS_quic_buffered_data_threshold) * 2, |
| 131 | 'a'); |
| 132 | |
| 133 | EXPECT_TRUE(stream_->Write(a_little_bit_of_data)); |
| 134 | EXPECT_TRUE(stream_->Write(a_little_bit_of_data)); |
| 135 | EXPECT_TRUE(stream_->Write(a_little_bit_of_data)); |
| 136 | |
| 137 | EXPECT_TRUE(stream_->Write(a_lot_of_data)); |
| 138 | EXPECT_FALSE(stream_->Write(a_lot_of_data)); |
| 139 | } |
| 140 | |
vasilvv | 54deda7 | 2020-01-07 11:19:36 -0800 | [diff] [blame] | 141 | TEST_F(QuicTransportStreamTest, CannotSendFinTwice) { |
| 142 | EXPECT_CALL(interface_, IsSessionReady()).WillRepeatedly(Return(true)); |
| 143 | ASSERT_TRUE(stream_->CanWrite()); |
| 144 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 145 | EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)) |
vasilvv | 54deda7 | 2020-01-07 11:19:36 -0800 | [diff] [blame] | 146 | .WillOnce(Return(QuicConsumedData(0, /*fin_consumed=*/true))); |
| 147 | EXPECT_TRUE(stream_->SendFin()); |
| 148 | EXPECT_FALSE(stream_->CanWrite()); |
| 149 | } |
| 150 | |
vasilvv | 312e3a5 | 2019-10-18 15:06:14 -0700 | [diff] [blame] | 151 | } // namespace |
| 152 | } // namespace test |
| 153 | } // namespace quic |