1 // Copyright (c) 2017 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 "quiche/quic/core/quic_control_frame_manager.h"
6
7 #include <utility>
8
9 #include "quiche/quic/core/crypto/null_encrypter.h"
10 #include "quiche/quic/core/frames/quic_ack_frequency_frame.h"
11 #include "quiche/quic/core/frames/quic_retire_connection_id_frame.h"
12 #include "quiche/quic/core/quic_types.h"
13 #include "quiche/quic/platform/api/quic_expect_bug.h"
14 #include "quiche/quic/platform/api/quic_flags.h"
15 #include "quiche/quic/platform/api/quic_test.h"
16 #include "quiche/quic/test_tools/quic_test_utils.h"
17
18 using testing::_;
19 using testing::InSequence;
20 using testing::Invoke;
21 using testing::Return;
22 using testing::StrictMock;
23
24 namespace quic {
25 namespace test {
26
27 class QuicControlFrameManagerPeer {
28 public:
QueueSize(QuicControlFrameManager * manager)29 static size_t QueueSize(QuicControlFrameManager* manager) {
30 return manager->control_frames_.size();
31 }
32 };
33
34 namespace {
35
36 const QuicStreamId kTestStreamId = 5;
37 const QuicRstStreamErrorCode kTestStopSendingCode =
38 QUIC_STREAM_ENCODER_STREAM_ERROR;
39
40 class QuicControlFrameManagerTest : public QuicTest {
41 public:
QuicControlFrameManagerTest()42 QuicControlFrameManagerTest()
43 : connection_(new MockQuicConnection(&helper_, &alarm_factory_,
44 Perspective::IS_SERVER)),
45 session_(std::make_unique<StrictMock<MockQuicSession>>(connection_)),
46 manager_(std::make_unique<QuicControlFrameManager>(session_.get())) {
47 connection_->SetEncrypter(
48 ENCRYPTION_FORWARD_SECURE,
49 std::make_unique<NullEncrypter>(connection_->perspective()));
50 }
51
52 protected:
53 MockQuicConnectionHelper helper_;
54 MockAlarmFactory alarm_factory_;
55 MockQuicConnection* connection_;
56 std::unique_ptr<StrictMock<MockQuicSession>> session_;
57 std::unique_ptr<QuicControlFrameManager> manager_;
58 };
59
TEST_F(QuicControlFrameManagerTest,InitialState)60 TEST_F(QuicControlFrameManagerTest, InitialState) {
61 EXPECT_EQ(0u, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
62 EXPECT_FALSE(manager_->HasPendingRetransmission());
63 EXPECT_FALSE(manager_->WillingToWrite());
64 }
65
TEST_F(QuicControlFrameManagerTest,WriteOrBufferRstStream)66 TEST_F(QuicControlFrameManagerTest, WriteOrBufferRstStream) {
67 QuicRstStreamFrame rst_stream = {1, kTestStreamId, QUIC_STREAM_CANCELLED, 0};
68 EXPECT_CALL(*session_, WriteControlFrame(_, _))
69 .WillOnce(Invoke(
70 [&rst_stream](const QuicFrame& frame, TransmissionType /*type*/) {
71 EXPECT_EQ(RST_STREAM_FRAME, frame.type);
72 EXPECT_EQ(rst_stream, *frame.rst_stream_frame);
73 ClearControlFrame(frame);
74 return true;
75 }));
76 manager_->WriteOrBufferRstStream(
77 rst_stream.stream_id,
78 QuicResetStreamError::FromInternal(rst_stream.error_code),
79 rst_stream.byte_offset);
80 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
81 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&rst_stream)));
82 EXPECT_FALSE(manager_->WillingToWrite());
83 }
84
TEST_F(QuicControlFrameManagerTest,WriteOrBufferGoAway)85 TEST_F(QuicControlFrameManagerTest, WriteOrBufferGoAway) {
86 QuicGoAwayFrame goaway = {1, QUIC_PEER_GOING_AWAY, kTestStreamId,
87 "Going away."};
88 EXPECT_CALL(*session_, WriteControlFrame(_, _))
89 .WillOnce(
90 Invoke([&goaway](const QuicFrame& frame, TransmissionType /*type*/) {
91 EXPECT_EQ(GOAWAY_FRAME, frame.type);
92 EXPECT_EQ(goaway, *frame.goaway_frame);
93 ClearControlFrame(frame);
94 return true;
95 }));
96 manager_->WriteOrBufferGoAway(goaway.error_code, goaway.last_good_stream_id,
97 goaway.reason_phrase);
98 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
99 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&goaway)));
100 EXPECT_FALSE(manager_->WillingToWrite());
101 }
102
TEST_F(QuicControlFrameManagerTest,WriteOrBufferWindowUpdate)103 TEST_F(QuicControlFrameManagerTest, WriteOrBufferWindowUpdate) {
104 QuicWindowUpdateFrame window_update = {1, kTestStreamId, 100};
105 EXPECT_CALL(*session_, WriteControlFrame(_, _))
106 .WillOnce(Invoke(
107 [&window_update](const QuicFrame& frame, TransmissionType /*type*/) {
108 EXPECT_EQ(WINDOW_UPDATE_FRAME, frame.type);
109 EXPECT_EQ(window_update, frame.window_update_frame);
110 ClearControlFrame(frame);
111 return true;
112 }));
113 manager_->WriteOrBufferWindowUpdate(window_update.stream_id,
114 window_update.max_data);
115 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
116 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(window_update)));
117 EXPECT_FALSE(manager_->WillingToWrite());
118 }
119
TEST_F(QuicControlFrameManagerTest,WriteOrBufferBlocked)120 TEST_F(QuicControlFrameManagerTest, WriteOrBufferBlocked) {
121 QuicBlockedFrame blocked = {1, kTestStreamId, 10};
122 EXPECT_CALL(*session_, WriteControlFrame(_, _))
123 .WillOnce(
124 Invoke([&blocked](const QuicFrame& frame, TransmissionType /*type*/) {
125 EXPECT_EQ(BLOCKED_FRAME, frame.type);
126 EXPECT_EQ(blocked, frame.blocked_frame);
127 ClearControlFrame(frame);
128 return true;
129 }));
130 manager_->WriteOrBufferBlocked(blocked.stream_id, blocked.offset);
131 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
132 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(blocked)));
133 EXPECT_FALSE(manager_->WillingToWrite());
134 }
135
TEST_F(QuicControlFrameManagerTest,WriteOrBufferStopSending)136 TEST_F(QuicControlFrameManagerTest, WriteOrBufferStopSending) {
137 QuicStopSendingFrame stop_sending = {1, kTestStreamId, kTestStopSendingCode};
138 EXPECT_CALL(*session_, WriteControlFrame(_, _))
139 .WillOnce(Invoke(
140 [&stop_sending](const QuicFrame& frame, TransmissionType /*type*/) {
141 EXPECT_EQ(STOP_SENDING_FRAME, frame.type);
142 EXPECT_EQ(stop_sending, frame.stop_sending_frame);
143 ClearControlFrame(frame);
144 return true;
145 }));
146 manager_->WriteOrBufferStopSending(
147 QuicResetStreamError::FromInternal(stop_sending.error_code),
148 stop_sending.stream_id);
149 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
150 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(stop_sending)));
151 EXPECT_FALSE(manager_->WillingToWrite());
152 }
153
TEST_F(QuicControlFrameManagerTest,BufferWhenWriteControlFrameReturnsFalse)154 TEST_F(QuicControlFrameManagerTest, BufferWhenWriteControlFrameReturnsFalse) {
155 QuicBlockedFrame blocked = {1, kTestStreamId, 0};
156
157 // Attempt write a control frame, but since WriteControlFrame returns false,
158 // the frame will be buffered.
159 EXPECT_CALL(*session_, WriteControlFrame(_, _)).WillOnce(Return(false));
160 manager_->WriteOrBufferBlocked(blocked.stream_id, blocked.offset);
161 EXPECT_TRUE(manager_->WillingToWrite());
162 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(blocked)));
163
164 // OnCanWrite will send the frame.
165 EXPECT_CALL(*session_, WriteControlFrame(_, _))
166 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
167 manager_->OnCanWrite();
168 EXPECT_FALSE(manager_->WillingToWrite());
169 }
170
TEST_F(QuicControlFrameManagerTest,BufferThenSendThenBuffer)171 TEST_F(QuicControlFrameManagerTest, BufferThenSendThenBuffer) {
172 InSequence s;
173 QuicBlockedFrame frame1 = {1, kTestStreamId, 0};
174 QuicBlockedFrame frame2 = {2, kTestStreamId + 1, 1};
175
176 // Attempt write a control frame, but since WriteControlFrame returns false,
177 // the frame will be buffered.
178 EXPECT_CALL(*session_, WriteControlFrame(_, _)).WillOnce(Return(false));
179 manager_->WriteOrBufferBlocked(frame1.stream_id, frame1.offset);
180 manager_->WriteOrBufferBlocked(frame2.stream_id, frame2.offset);
181 EXPECT_TRUE(manager_->WillingToWrite());
182 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame1)));
183 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame2)));
184
185 // OnCanWrite will send the first frame, but WriteControlFrame will return
186 // false and the second frame will remain buffered.
187 EXPECT_CALL(*session_, WriteControlFrame(_, _))
188 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
189 EXPECT_CALL(*session_, WriteControlFrame(_, _)).WillOnce(Return(false));
190 manager_->OnCanWrite();
191 EXPECT_TRUE(manager_->WillingToWrite());
192
193 // Now the second frame will finally be sent.
194 EXPECT_CALL(*session_, WriteControlFrame(_, _))
195 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
196 manager_->OnCanWrite();
197 EXPECT_FALSE(manager_->WillingToWrite());
198 }
199
TEST_F(QuicControlFrameManagerTest,OnControlFrameAcked)200 TEST_F(QuicControlFrameManagerTest, OnControlFrameAcked) {
201 QuicRstStreamFrame frame1 = {1, kTestStreamId, QUIC_STREAM_CANCELLED, 0};
202 QuicGoAwayFrame frame2 = {2, QUIC_PEER_GOING_AWAY, kTestStreamId,
203 "Going away."};
204 QuicWindowUpdateFrame frame3 = {3, kTestStreamId, 100};
205 QuicBlockedFrame frame4 = {4, kTestStreamId, 0};
206 QuicStopSendingFrame frame5 = {5, kTestStreamId, kTestStopSendingCode};
207
208 // Write 5 all frames.
209 InSequence s;
210 EXPECT_CALL(*session_, WriteControlFrame(_, _))
211 .Times(5)
212 .WillRepeatedly(Invoke(&ClearControlFrameWithTransmissionType));
213 manager_->WriteOrBufferRstStream(
214 frame1.stream_id, QuicResetStreamError::FromInternal(frame1.error_code),
215 frame1.byte_offset);
216 manager_->WriteOrBufferGoAway(frame2.error_code, frame2.last_good_stream_id,
217 frame2.reason_phrase);
218 manager_->WriteOrBufferWindowUpdate(frame3.stream_id, frame3.max_data);
219 manager_->WriteOrBufferBlocked(frame4.stream_id, frame4.offset);
220 manager_->WriteOrBufferStopSending(
221 QuicResetStreamError::FromInternal(frame5.error_code), frame5.stream_id);
222
223 // Verify all 5 are still outstanding.
224 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&frame1)));
225 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&frame2)));
226 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame3)));
227 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame4)));
228 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame5)));
229 EXPECT_FALSE(manager_->HasPendingRetransmission());
230
231 // Ack the third frame, but since the first is still in the queue, the size
232 // will not shrink.
233 EXPECT_TRUE(manager_->OnControlFrameAcked(QuicFrame(frame3)));
234 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(frame3)));
235 EXPECT_EQ(5, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
236
237 // Ack the second frame, but since the first is still in the queue, the size
238 // will not shrink.
239 EXPECT_TRUE(manager_->OnControlFrameAcked(QuicFrame(&frame2)));
240 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(&frame2)));
241 EXPECT_EQ(5, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
242
243 // Only after the first frame in the queue is acked do the frames get
244 // removed ... now see that the length has been reduced by 3.
245 EXPECT_TRUE(manager_->OnControlFrameAcked(QuicFrame(&frame1)));
246 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(&frame1)));
247 EXPECT_EQ(2, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
248
249 // Duplicate ack should change nothing.
250 EXPECT_FALSE(manager_->OnControlFrameAcked(QuicFrame(&frame2)));
251 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(&frame1)));
252 EXPECT_EQ(2, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
253
254 // Ack the fourth frame which will shrink the queue.
255 EXPECT_TRUE(manager_->OnControlFrameAcked(QuicFrame(frame4)));
256 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(frame4)));
257 EXPECT_EQ(1, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
258
259 // Ack the fourth frame which will empty the queue.
260 EXPECT_TRUE(manager_->OnControlFrameAcked(QuicFrame(frame5)));
261 EXPECT_FALSE(manager_->IsControlFrameOutstanding(QuicFrame(frame5)));
262 EXPECT_EQ(0, QuicControlFrameManagerPeer::QueueSize(manager_.get()));
263 }
264
TEST_F(QuicControlFrameManagerTest,OnControlFrameLost)265 TEST_F(QuicControlFrameManagerTest, OnControlFrameLost) {
266 QuicRstStreamFrame frame1 = {1, kTestStreamId, QUIC_STREAM_CANCELLED, 0};
267 QuicGoAwayFrame frame2 = {2, QUIC_PEER_GOING_AWAY, kTestStreamId,
268 "Going away."};
269 QuicWindowUpdateFrame frame3 = {3, kTestStreamId, 100};
270 QuicBlockedFrame frame4 = {4, kTestStreamId, 0};
271 QuicStopSendingFrame frame5 = {5, kTestStreamId, kTestStopSendingCode};
272
273 // Write the first 3 frames, but leave the second two buffered.
274 InSequence s;
275 EXPECT_CALL(*session_, WriteControlFrame(_, _))
276 .Times(3)
277 .WillRepeatedly(Invoke(&ClearControlFrameWithTransmissionType));
278 manager_->WriteOrBufferRstStream(
279 frame1.stream_id, QuicResetStreamError::FromInternal(frame1.error_code),
280 frame1.byte_offset);
281 manager_->WriteOrBufferGoAway(frame2.error_code, frame2.last_good_stream_id,
282 frame2.reason_phrase);
283 manager_->WriteOrBufferWindowUpdate(frame3.stream_id, frame3.max_data);
284 EXPECT_CALL(*session_, WriteControlFrame(_, _)).WillOnce(Return(false));
285 manager_->WriteOrBufferBlocked(frame4.stream_id, frame4.offset);
286 manager_->WriteOrBufferStopSending(
287 QuicResetStreamError::FromInternal(frame5.error_code), frame5.stream_id);
288
289 // Lose frames 1, 2, 3.
290 manager_->OnControlFrameLost(QuicFrame(&frame1));
291 manager_->OnControlFrameLost(QuicFrame(&frame2));
292 manager_->OnControlFrameLost(QuicFrame(frame3));
293 EXPECT_TRUE(manager_->HasPendingRetransmission());
294 // Verify that the lost frames are still outstanding.
295 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&frame1)));
296 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(&frame2)));
297 EXPECT_TRUE(manager_->IsControlFrameOutstanding(QuicFrame(frame3)));
298
299 // Ack control frame 2.
300 manager_->OnControlFrameAcked(QuicFrame(&frame2));
301
302 // OnCanWrite will retransmit the lost frames, but will not sent the
303 // not-yet-sent frames.
304 EXPECT_CALL(*session_, WriteControlFrame(_, _))
305 .WillOnce(
306 Invoke([&frame1](const QuicFrame& frame, TransmissionType /*type*/) {
307 EXPECT_EQ(RST_STREAM_FRAME, frame.type);
308 EXPECT_EQ(frame1, *frame.rst_stream_frame);
309 ClearControlFrame(frame);
310 return true;
311 }));
312 EXPECT_CALL(*session_, WriteControlFrame(_, _))
313 .WillOnce(
314 Invoke([&frame3](const QuicFrame& frame, TransmissionType /*type*/) {
315 EXPECT_EQ(WINDOW_UPDATE_FRAME, frame.type);
316 EXPECT_EQ(frame3, frame.window_update_frame);
317 ClearControlFrame(frame);
318 return true;
319 }));
320 manager_->OnCanWrite();
321 EXPECT_FALSE(manager_->HasPendingRetransmission());
322 EXPECT_TRUE(manager_->WillingToWrite());
323
324 // Send control frames 4, and 5.
325 EXPECT_CALL(*session_, WriteControlFrame(_, _))
326 .WillOnce(
327 Invoke([&frame4](const QuicFrame& frame, TransmissionType /*type*/) {
328 EXPECT_EQ(BLOCKED_FRAME, frame.type);
329 EXPECT_EQ(frame4, frame.blocked_frame);
330 ClearControlFrame(frame);
331 return true;
332 }));
333 EXPECT_CALL(*session_, WriteControlFrame(_, _))
334 .WillOnce(
335 Invoke([&frame5](const QuicFrame& frame, TransmissionType /*type*/) {
336 EXPECT_EQ(STOP_SENDING_FRAME, frame.type);
337 EXPECT_EQ(frame5, frame.stop_sending_frame);
338 ClearControlFrame(frame);
339 return true;
340 }));
341 manager_->OnCanWrite();
342 EXPECT_FALSE(manager_->WillingToWrite());
343 }
344
TEST_F(QuicControlFrameManagerTest,RetransmitControlFrame)345 TEST_F(QuicControlFrameManagerTest, RetransmitControlFrame) {
346 QuicRstStreamFrame frame1 = {1, kTestStreamId, QUIC_STREAM_CANCELLED, 0};
347 QuicGoAwayFrame frame2 = {2, QUIC_PEER_GOING_AWAY, kTestStreamId,
348 "Going away."};
349 QuicWindowUpdateFrame frame3 = {3, kTestStreamId, 100};
350 QuicBlockedFrame frame4 = {4, kTestStreamId, 0};
351
352 // Send all 4 frames.
353 InSequence s;
354 EXPECT_CALL(*session_, WriteControlFrame(_, _))
355 .Times(4)
356 .WillRepeatedly(Invoke(&ClearControlFrameWithTransmissionType));
357 manager_->WriteOrBufferRstStream(
358 frame1.stream_id, QuicResetStreamError::FromInternal(frame1.error_code),
359 frame1.byte_offset);
360 manager_->WriteOrBufferGoAway(frame2.error_code, frame2.last_good_stream_id,
361 frame2.reason_phrase);
362 manager_->WriteOrBufferWindowUpdate(frame3.stream_id, frame3.max_data);
363 manager_->WriteOrBufferBlocked(frame4.stream_id, frame4.offset);
364
365 // Ack control frame 2.
366 manager_->OnControlFrameAcked(QuicFrame(&frame2));
367 // Do not retransmit an acked frame
368 EXPECT_CALL(*session_, WriteControlFrame(_, _)).Times(0);
369 EXPECT_TRUE(
370 manager_->RetransmitControlFrame(QuicFrame(&frame2), PTO_RETRANSMISSION));
371
372 // Retransmit frame 3.
373 EXPECT_CALL(*session_, WriteControlFrame(_, _))
374 .WillOnce(
375 Invoke([&frame3](const QuicFrame& frame, TransmissionType /*type*/) {
376 EXPECT_EQ(WINDOW_UPDATE_FRAME, frame.type);
377 EXPECT_EQ(frame3, frame.window_update_frame);
378 ClearControlFrame(frame);
379 return true;
380 }));
381 EXPECT_TRUE(
382 manager_->RetransmitControlFrame(QuicFrame(frame3), PTO_RETRANSMISSION));
383
384 // Retransmit frame 4, but since WriteControlFrame returned false the
385 // frame will still need retransmission.
386 EXPECT_CALL(*session_, WriteControlFrame(_, _))
387 .WillOnce(
388 Invoke([&frame4](const QuicFrame& frame, TransmissionType /*type*/) {
389 EXPECT_EQ(BLOCKED_FRAME, frame.type);
390 EXPECT_EQ(frame4, frame.blocked_frame);
391 return false;
392 }));
393 EXPECT_FALSE(
394 manager_->RetransmitControlFrame(QuicFrame(frame4), PTO_RETRANSMISSION));
395 }
396
TEST_F(QuicControlFrameManagerTest,SendAndAckAckFrequencyFrame)397 TEST_F(QuicControlFrameManagerTest, SendAndAckAckFrequencyFrame) {
398 // Send AckFrequencyFrame
399 QuicAckFrequencyFrame frame_to_send;
400 frame_to_send.packet_tolerance = 10;
401 frame_to_send.max_ack_delay = QuicTime::Delta::FromMilliseconds(24);
402 EXPECT_CALL(*session_, WriteControlFrame(_, _))
403 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
404 manager_->WriteOrBufferAckFrequency(frame_to_send);
405
406 // Ack AckFrequencyFrame.
407 QuicAckFrequencyFrame expected_ack_frequency = frame_to_send;
408 expected_ack_frequency.control_frame_id = 1;
409 expected_ack_frequency.sequence_number = 1;
410 EXPECT_TRUE(
411 manager_->OnControlFrameAcked(QuicFrame(&expected_ack_frequency)));
412 }
413
TEST_F(QuicControlFrameManagerTest,NewAndRetireConnectionIdFrames)414 TEST_F(QuicControlFrameManagerTest, NewAndRetireConnectionIdFrames) {
415 // Send NewConnectionIdFrame
416 EXPECT_CALL(*session_, WriteControlFrame(_, _))
417 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
418 QuicNewConnectionIdFrame new_connection_id_frame(
419 1, TestConnectionId(3), /*sequence_number=*/1,
420 /*stateless_reset_token=*/
421 {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /*retire_prior_to=*/1);
422 manager_->WriteOrBufferNewConnectionId(
423 new_connection_id_frame.connection_id,
424 new_connection_id_frame.sequence_number,
425 new_connection_id_frame.retire_prior_to,
426 new_connection_id_frame.stateless_reset_token);
427
428 // Send RetireConnectionIdFrame
429 EXPECT_CALL(*session_, WriteControlFrame(_, _))
430 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
431 QuicRetireConnectionIdFrame retire_connection_id_frame(2,
432 /*sequence_number=*/0);
433 manager_->WriteOrBufferRetireConnectionId(
434 retire_connection_id_frame.sequence_number);
435
436 // Ack both frames.
437 EXPECT_TRUE(
438 manager_->OnControlFrameAcked(QuicFrame(&new_connection_id_frame)));
439 EXPECT_TRUE(
440 manager_->OnControlFrameAcked(QuicFrame(&retire_connection_id_frame)));
441 }
442
TEST_F(QuicControlFrameManagerTest,DonotRetransmitOldWindowUpdates)443 TEST_F(QuicControlFrameManagerTest, DonotRetransmitOldWindowUpdates) {
444 // Send two window updates for the same stream.
445 QuicWindowUpdateFrame window_update1(1, kTestStreamId, 200);
446 EXPECT_CALL(*session_, WriteControlFrame(_, _))
447 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
448 manager_->WriteOrBufferWindowUpdate(window_update1.stream_id,
449 window_update1.max_data);
450
451 QuicWindowUpdateFrame window_update2(2, kTestStreamId, 300);
452 EXPECT_CALL(*session_, WriteControlFrame(_, _))
453 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
454 manager_->WriteOrBufferWindowUpdate(window_update2.stream_id,
455 window_update2.max_data);
456
457 // Mark both window updates as lost.
458 manager_->OnControlFrameLost(QuicFrame(window_update1));
459 manager_->OnControlFrameLost(QuicFrame(window_update2));
460 EXPECT_TRUE(manager_->HasPendingRetransmission());
461 EXPECT_TRUE(manager_->WillingToWrite());
462
463 // Verify only the latest window update gets retransmitted.
464 EXPECT_CALL(*session_, WriteControlFrame(_, _))
465 .WillOnce(Invoke(
466 [&window_update2](const QuicFrame& frame, TransmissionType /*type*/) {
467 EXPECT_EQ(WINDOW_UPDATE_FRAME, frame.type);
468 EXPECT_EQ(window_update2, frame.window_update_frame);
469 ClearControlFrame(frame);
470 return true;
471 }));
472 manager_->OnCanWrite();
473 EXPECT_FALSE(manager_->HasPendingRetransmission());
474 EXPECT_FALSE(manager_->WillingToWrite());
475 }
476
TEST_F(QuicControlFrameManagerTest,RetransmitWindowUpdateOfDifferentStreams)477 TEST_F(QuicControlFrameManagerTest, RetransmitWindowUpdateOfDifferentStreams) {
478 // Send two window updates for different streams.
479 QuicWindowUpdateFrame window_update1(1, kTestStreamId + 2, 200);
480 EXPECT_CALL(*session_, WriteControlFrame(_, _))
481 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
482 manager_->WriteOrBufferWindowUpdate(window_update1.stream_id,
483 window_update1.max_data);
484
485 QuicWindowUpdateFrame window_update2(2, kTestStreamId + 4, 300);
486 EXPECT_CALL(*session_, WriteControlFrame(_, _))
487 .WillOnce(Invoke(&ClearControlFrameWithTransmissionType));
488 manager_->WriteOrBufferWindowUpdate(window_update2.stream_id,
489 window_update2.max_data);
490
491 // Mark both window updates as lost.
492 manager_->OnControlFrameLost(QuicFrame(window_update1));
493 manager_->OnControlFrameLost(QuicFrame(window_update2));
494 EXPECT_TRUE(manager_->HasPendingRetransmission());
495 EXPECT_TRUE(manager_->WillingToWrite());
496
497 // Verify both window updates get retransmitted.
498 EXPECT_CALL(*session_, WriteControlFrame(_, _))
499 .Times(2)
500 .WillRepeatedly(Invoke(&ClearControlFrameWithTransmissionType));
501 manager_->OnCanWrite();
502 EXPECT_FALSE(manager_->HasPendingRetransmission());
503 EXPECT_FALSE(manager_->WillingToWrite());
504 }
505
TEST_F(QuicControlFrameManagerTest,TooManyBufferedControlFrames)506 TEST_F(QuicControlFrameManagerTest, TooManyBufferedControlFrames) {
507 // Write 1000 control frames.
508 EXPECT_CALL(*session_, WriteControlFrame(_, _)).WillOnce(Return(false));
509 for (size_t i = 0; i < 1000; ++i) {
510 manager_->WriteOrBufferRstStream(
511 kTestStreamId,
512 QuicResetStreamError::FromInternal(QUIC_STREAM_CANCELLED), 0);
513 }
514 // Verify that writing one more control frame causes connection close.
515 EXPECT_CALL(
516 *connection_,
517 CloseConnection(QUIC_TOO_MANY_BUFFERED_CONTROL_FRAMES, _,
518 ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET));
519 manager_->WriteOrBufferRstStream(
520 kTestStreamId, QuicResetStreamError::FromInternal(QUIC_STREAM_CANCELLED),
521 0);
522 }
523
TEST_F(QuicControlFrameManagerTest,NumBufferedMaxStreams)524 TEST_F(QuicControlFrameManagerTest, NumBufferedMaxStreams) {
525 std::vector<QuicMaxStreamsFrame> max_streams_frames;
526 size_t expected_buffered_frames = 0;
527 for (int i = 0; i < 5; ++i) {
528 // Save the frame so it can be ACK'd later.
529 EXPECT_CALL(*session_, WriteControlFrame(_, _))
530 .WillOnce(Invoke([&max_streams_frames](const QuicFrame& frame,
531 TransmissionType /*type*/) {
532 max_streams_frames.push_back(frame.max_streams_frame);
533 ClearControlFrame(frame);
534 return true;
535 }));
536
537 // The contents of the frame don't matter for this test.
538 manager_->WriteOrBufferMaxStreams(0, false);
539 EXPECT_EQ(++expected_buffered_frames, manager_->NumBufferedMaxStreams());
540 }
541
542 for (const QuicMaxStreamsFrame& frame : max_streams_frames) {
543 manager_->OnControlFrameAcked(QuicFrame(frame));
544 EXPECT_EQ(--expected_buffered_frames, manager_->NumBufferedMaxStreams());
545 }
546 EXPECT_EQ(0, manager_->NumBufferedMaxStreams());
547 }
548
549 } // namespace
550 } // namespace test
551 } // namespace quic
552