blob: 53712fd5395f9e457a47a6b25afaaf040434d4fe [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2018 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/test_tools/simple_session_notifier.h"
6
7#include "net/third_party/quiche/src/quic/core/quic_utils.h"
8#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
9#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
10#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
11
12using testing::_;
13using testing::InSequence;
14using testing::Return;
15using testing::StrictMock;
16
17namespace quic {
18namespace test {
19namespace {
20
21class MockQuicConnectionWithSendStreamData : public MockQuicConnection {
22 public:
23 MockQuicConnectionWithSendStreamData(MockQuicConnectionHelper* helper,
24 MockAlarmFactory* alarm_factory,
25 Perspective perspective)
26 : MockQuicConnection(helper, alarm_factory, perspective) {}
27
28 MOCK_METHOD4(SendStreamData,
29 QuicConsumedData(QuicStreamId id,
30 size_t write_length,
31 QuicStreamOffset offset,
32 StreamSendingState state));
33};
34
35class SimpleSessionNotifierTest : public QuicTest {
36 public:
37 SimpleSessionNotifierTest()
38 : connection_(&helper_, &alarm_factory_, Perspective::IS_CLIENT),
39 notifier_(&connection_) {
40 connection_.set_visitor(&visitor_);
41 QuicConnectionPeer::SetSessionDecidesWhatToWrite(&connection_);
42 EXPECT_FALSE(notifier_.WillingToWrite());
43 EXPECT_EQ(0u, notifier_.StreamBytesSent());
44 EXPECT_FALSE(notifier_.HasBufferedStreamData());
45 }
46
47 bool ControlFrameConsumed(const QuicFrame& frame) {
48 DeleteFrame(&const_cast<QuicFrame&>(frame));
49 return true;
50 }
51
52 MockQuicConnectionHelper helper_;
53 MockAlarmFactory alarm_factory_;
54 MockQuicConnectionVisitor visitor_;
55 StrictMock<MockQuicConnectionWithSendStreamData> connection_;
56 SimpleSessionNotifier notifier_;
57};
58
59TEST_F(SimpleSessionNotifierTest, WriteOrBufferData) {
60 InSequence s;
61 EXPECT_CALL(connection_, SendStreamData(3, 1024, 0, NO_FIN))
62 .WillOnce(Return(QuicConsumedData(1024, false)));
63 notifier_.WriteOrBufferData(3, 1024, NO_FIN);
64 EXPECT_EQ(0u, notifier_.StreamBytesToSend());
65 EXPECT_CALL(connection_, SendStreamData(5, 512, 0, NO_FIN))
66 .WillOnce(Return(QuicConsumedData(512, false)));
67 notifier_.WriteOrBufferData(5, 512, NO_FIN);
68 EXPECT_FALSE(notifier_.WillingToWrite());
69 // Connection is blocked.
70 EXPECT_CALL(connection_, SendStreamData(5, 512, 512, FIN))
71 .WillOnce(Return(QuicConsumedData(256, false)));
72 notifier_.WriteOrBufferData(5, 512, FIN);
73 EXPECT_TRUE(notifier_.WillingToWrite());
74 EXPECT_EQ(1792u, notifier_.StreamBytesSent());
75 EXPECT_EQ(256u, notifier_.StreamBytesToSend());
76 EXPECT_TRUE(notifier_.HasBufferedStreamData());
77
78 // New data cannot be sent as connection is blocked.
79 EXPECT_CALL(connection_, SendStreamData(7, 1024, 0, FIN)).Times(0);
80 notifier_.WriteOrBufferData(7, 1024, FIN);
81 EXPECT_EQ(1792u, notifier_.StreamBytesSent());
82}
83
84TEST_F(SimpleSessionNotifierTest, WriteOrBufferRstStream) {
85 InSequence s;
86 EXPECT_CALL(connection_, SendStreamData(5, 1024, 0, FIN))
87 .WillOnce(Return(QuicConsumedData(1024, true)));
88 notifier_.WriteOrBufferData(5, 1024, FIN);
89
90 // Reset stream 5 with no error.
91 EXPECT_CALL(connection_, SendControlFrame(_))
92 .WillRepeatedly(
93 Invoke(this, &SimpleSessionNotifierTest::ControlFrameConsumed));
94 notifier_.WriteOrBufferRstStream(5, QUIC_STREAM_NO_ERROR, 1024);
95 // Verify stream 5 is waiting for acks.
96 EXPECT_TRUE(notifier_.StreamIsWaitingForAcks(5));
97
98 // Reset stream 5 with error.
99 notifier_.WriteOrBufferRstStream(5, QUIC_ERROR_PROCESSING_STREAM, 1024);
100 EXPECT_FALSE(notifier_.StreamIsWaitingForAcks(5));
101}
102
zhongyifbb25772019-04-10 16:54:08 -0700103TEST_F(SimpleSessionNotifierTest, WriteOrBufferPing) {
104 InSequence s;
105 // Write ping when connection is not write blocked.
106 EXPECT_CALL(connection_, SendControlFrame(_))
107 .WillRepeatedly(
108 Invoke(this, &SimpleSessionNotifierTest::ControlFrameConsumed));
109 notifier_.WriteOrBufferPing();
110 EXPECT_EQ(0u, notifier_.StreamBytesToSend());
111 EXPECT_FALSE(notifier_.WillingToWrite());
112
113 // Write stream data and cause the connection to be write blocked.
114 EXPECT_CALL(connection_, SendStreamData(3, 1024, 0, NO_FIN))
115 .WillOnce(Return(QuicConsumedData(1024, false)));
116 notifier_.WriteOrBufferData(3, 1024, NO_FIN);
117 EXPECT_EQ(0u, notifier_.StreamBytesToSend());
118 EXPECT_CALL(connection_, SendStreamData(5, 512, 0, NO_FIN))
119 .WillOnce(Return(QuicConsumedData(256, false)));
120 notifier_.WriteOrBufferData(5, 512, NO_FIN);
121 EXPECT_TRUE(notifier_.WillingToWrite());
122
123 // Connection is blocked.
124 EXPECT_CALL(connection_, SendControlFrame(_)).Times(0);
125 notifier_.WriteOrBufferPing();
126}
127
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128TEST_F(SimpleSessionNotifierTest, NeuterUnencryptedData) {
129 InSequence s;
QUICHE team6987b4a2019-03-15 16:23:04 -0700130 // Send crypto data [0, 1024) in ENCRYPTION_INITIAL.
131 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500132 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
133 connection_.transport_version()),
134 1024, 0, NO_FIN))
135 .WillOnce(Return(QuicConsumedData(1024, false)));
136 notifier_.WriteOrBufferData(
137 QuicUtils::GetCryptoStreamId(connection_.transport_version()), 1024,
138 NO_FIN);
139 // Send crypto data [1024, 2048) in ENCRYPTION_ZERO_RTT.
140 connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT);
141 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
142 connection_.transport_version()),
143 1024, 1024, NO_FIN))
144 .WillOnce(Return(QuicConsumedData(1024, false)));
145 notifier_.WriteOrBufferData(
146 QuicUtils::GetCryptoStreamId(connection_.transport_version()), 1024,
147 NO_FIN);
148 // Ack [1024, 2048).
149 QuicStreamFrame stream_frame(
150 QuicUtils::GetCryptoStreamId(connection_.transport_version()), false,
151 1024, 1024);
152 notifier_.OnFrameAcked(QuicFrame(stream_frame), QuicTime::Delta::Zero());
153 EXPECT_TRUE(notifier_.StreamIsWaitingForAcks(
154 QuicUtils::GetCryptoStreamId(connection_.transport_version())));
155 // Neuters unencrypted data.
156 notifier_.NeuterUnencryptedData();
157 EXPECT_FALSE(notifier_.StreamIsWaitingForAcks(
158 QuicUtils::GetCryptoStreamId(connection_.transport_version())));
159}
160
161TEST_F(SimpleSessionNotifierTest, OnCanWrite) {
162 InSequence s;
QUICHE team6987b4a2019-03-15 16:23:04 -0700163 // Send crypto data [0, 1024) in ENCRYPTION_INITIAL.
164 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
166 connection_.transport_version()),
167 1024, 0, NO_FIN))
168 .WillOnce(Return(QuicConsumedData(1024, false)));
169 notifier_.WriteOrBufferData(
170 QuicUtils::GetCryptoStreamId(connection_.transport_version()), 1024,
171 NO_FIN);
172 // Send crypto data [1024, 2048) in ENCRYPTION_ZERO_RTT.
173 connection_.SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT);
174 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
175 connection_.transport_version()),
176 1024, 1024, NO_FIN))
177 .WillOnce(Return(QuicConsumedData(1024, false)));
178 notifier_.WriteOrBufferData(
179 QuicUtils::GetCryptoStreamId(connection_.transport_version()), 1024,
180 NO_FIN);
181 // Send stream 3 [0, 1024) and connection is blocked.
182 EXPECT_CALL(connection_, SendStreamData(3, 1024, 0, FIN))
183 .WillOnce(Return(QuicConsumedData(512, false)));
184 notifier_.WriteOrBufferData(3, 1024, FIN);
185 // Send stream 5 [0, 1024).
186 EXPECT_CALL(connection_, SendStreamData(5, _, _, _)).Times(0);
187 notifier_.WriteOrBufferData(5, 1024, NO_FIN);
188 // Reset stream 5 with error.
189 EXPECT_CALL(connection_, SendControlFrame(_)).Times(0);
190 notifier_.WriteOrBufferRstStream(5, QUIC_ERROR_PROCESSING_STREAM, 1024);
191
192 // Lost crypto data [500, 1500) and stream 3 [0, 512).
193 QuicStreamFrame frame1(
194 QuicUtils::GetCryptoStreamId(connection_.transport_version()), false, 500,
195 1000);
196 QuicStreamFrame frame2(3, false, 0, 512);
197 notifier_.OnFrameLost(QuicFrame(frame1));
198 notifier_.OnFrameLost(QuicFrame(frame2));
199
200 // Connection becomes writable.
201 // Lost crypto data gets retransmitted as [500, 1024) and [1024, 1500), as
202 // they are in different encryption levels.
203 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
204 connection_.transport_version()),
205 524, 500, NO_FIN))
206 .WillOnce(Return(QuicConsumedData(524, false)));
207 EXPECT_CALL(connection_, SendStreamData(QuicUtils::GetCryptoStreamId(
208 connection_.transport_version()),
209 476, 1024, NO_FIN))
210 .WillOnce(Return(QuicConsumedData(476, false)));
211 // Lost stream 3 data gets retransmitted.
212 EXPECT_CALL(connection_, SendStreamData(3, 512, 0, NO_FIN))
213 .WillOnce(Return(QuicConsumedData(512, false)));
214 // Buffered control frames get sent.
215 EXPECT_CALL(connection_, SendControlFrame(_))
216 .WillOnce(Invoke(this, &SimpleSessionNotifierTest::ControlFrameConsumed));
217 // Buffered stream 3 data [512, 1024) gets sent.
218 EXPECT_CALL(connection_, SendStreamData(3, 512, 512, FIN))
219 .WillOnce(Return(QuicConsumedData(512, true)));
220 notifier_.OnCanWrite();
221 EXPECT_FALSE(notifier_.WillingToWrite());
222}
223
224TEST_F(SimpleSessionNotifierTest, RetransmitFrames) {
225 InSequence s;
226 // Send stream 3 data [0, 10) and fin.
227 EXPECT_CALL(connection_, SendStreamData(3, 10, 0, FIN))
228 .WillOnce(Return(QuicConsumedData(10, true)));
229 notifier_.WriteOrBufferData(3, 10, FIN);
230 QuicStreamFrame frame1(3, true, 0, 10);
231 // Send stream 5 [0, 10) and fin.
232 EXPECT_CALL(connection_, SendStreamData(5, 10, 0, FIN))
233 .WillOnce(Return(QuicConsumedData(10, true)));
234 notifier_.WriteOrBufferData(5, 10, FIN);
235 QuicStreamFrame frame2(5, true, 0, 10);
236 // Reset stream 5 with no error.
237 EXPECT_CALL(connection_, SendControlFrame(_))
238 .WillOnce(Invoke(this, &SimpleSessionNotifierTest::ControlFrameConsumed));
239 notifier_.WriteOrBufferRstStream(5, QUIC_STREAM_NO_ERROR, 10);
240
241 // Ack stream 3 [3, 7), and stream 5 [8, 10).
242 QuicStreamFrame ack_frame1(3, false, 3, 4);
243 QuicStreamFrame ack_frame2(5, false, 8, 2);
244 notifier_.OnFrameAcked(QuicFrame(ack_frame1), QuicTime::Delta::Zero());
245 notifier_.OnFrameAcked(QuicFrame(ack_frame2), QuicTime::Delta::Zero());
246 EXPECT_FALSE(notifier_.WillingToWrite());
247
248 // Force to send.
249 QuicRstStreamFrame rst_stream(1, 5, QUIC_STREAM_NO_ERROR, 10);
250 QuicFrames frames;
251 frames.push_back(QuicFrame(frame2));
252 frames.push_back(QuicFrame(&rst_stream));
253 frames.push_back(QuicFrame(frame1));
254 // stream 5 data [0, 8), fin only are retransmitted.
255 EXPECT_CALL(connection_, SendStreamData(5, 8, 0, NO_FIN))
256 .WillOnce(Return(QuicConsumedData(8, false)));
257 EXPECT_CALL(connection_, SendStreamData(5, 0, 10, FIN))
258 .WillOnce(Return(QuicConsumedData(0, true)));
259 // rst_stream is retransmitted.
260 EXPECT_CALL(connection_, SendControlFrame(_))
261 .WillOnce(Invoke(this, &SimpleSessionNotifierTest::ControlFrameConsumed));
262 // stream 3 data [0, 3) is retransmitted and connection is blocked.
263 EXPECT_CALL(connection_, SendStreamData(3, 3, 0, NO_FIN))
264 .WillOnce(Return(QuicConsumedData(2, false)));
265 notifier_.RetransmitFrames(frames, RTO_RETRANSMISSION);
266 EXPECT_FALSE(notifier_.WillingToWrite());
267}
268
269} // namespace
270} // namespace test
271} // namespace quic