OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/quic_connection.h" | |
6 | |
7 #include <errno.h> | |
8 #include <memory> | |
9 #include <ostream> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/macros.h" | |
14 #include "base/stl_util.h" | |
15 #include "net/base/ip_address.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/quic/congestion_control/loss_detection_interface.h" | |
18 #include "net/quic/congestion_control/send_algorithm_interface.h" | |
19 #include "net/quic/crypto/null_encrypter.h" | |
20 #include "net/quic/crypto/quic_decrypter.h" | |
21 #include "net/quic/crypto/quic_encrypter.h" | |
22 #include "net/quic/quic_flags.h" | |
23 #include "net/quic/quic_protocol.h" | |
24 #include "net/quic/quic_simple_buffer_allocator.h" | |
25 #include "net/quic/quic_utils.h" | |
26 #include "net/quic/test_tools/mock_clock.h" | |
27 #include "net/quic/test_tools/mock_random.h" | |
28 #include "net/quic/test_tools/quic_config_peer.h" | |
29 #include "net/quic/test_tools/quic_connection_peer.h" | |
30 #include "net/quic/test_tools/quic_framer_peer.h" | |
31 #include "net/quic/test_tools/quic_packet_creator_peer.h" | |
32 #include "net/quic/test_tools/quic_packet_generator_peer.h" | |
33 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h" | |
34 #include "net/quic/test_tools/quic_test_utils.h" | |
35 #include "net/quic/test_tools/simple_quic_framer.h" | |
36 #include "net/test/gtest_util.h" | |
37 #include "testing/gmock/include/gmock/gmock.h" | |
38 #include "testing/gtest/include/gtest/gtest.h" | |
39 | |
40 using base::StringPiece; | |
41 using std::map; | |
42 using std::ostream; | |
43 using std::string; | |
44 using std::vector; | |
45 using testing::AnyNumber; | |
46 using testing::AtLeast; | |
47 using testing::Contains; | |
48 using testing::DoAll; | |
49 using testing::InSequence; | |
50 using testing::InvokeWithoutArgs; | |
51 using testing::NiceMock; | |
52 using testing::Ref; | |
53 using testing::Return; | |
54 using testing::SaveArg; | |
55 using testing::SetArgPointee; | |
56 using testing::StrictMock; | |
57 using testing::_; | |
58 | |
59 namespace net { | |
60 namespace test { | |
61 namespace { | |
62 | |
63 const char data1[] = "foo"; | |
64 const char data2[] = "bar"; | |
65 | |
66 const bool kFin = true; | |
67 const bool kEntropyFlag = true; | |
68 const bool kHasStopWaiting = true; | |
69 | |
70 const QuicPacketEntropyHash kTestEntropyHash = 76; | |
71 | |
72 const int kDefaultRetransmissionTimeMs = 500; | |
73 | |
74 const IPEndPoint kPeerAddress = IPEndPoint(Loopback6(), /*port=*/12345); | |
75 const IPEndPoint kSelfAddress = IPEndPoint(Loopback6(), /*port=*/443); | |
76 | |
77 Perspective InvertPerspective(Perspective perspective) { | |
78 return perspective == Perspective::IS_CLIENT ? Perspective::IS_SERVER | |
79 : Perspective::IS_CLIENT; | |
80 } | |
81 | |
82 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. | |
83 class TaggingEncrypter : public QuicEncrypter { | |
84 public: | |
85 explicit TaggingEncrypter(uint8_t tag) : tag_(tag) {} | |
86 | |
87 ~TaggingEncrypter() override {} | |
88 | |
89 // QuicEncrypter interface. | |
90 bool SetKey(StringPiece key) override { return true; } | |
91 | |
92 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } | |
93 | |
94 bool EncryptPacket(QuicPathId path_id, | |
95 QuicPacketNumber packet_number, | |
96 StringPiece associated_data, | |
97 StringPiece plaintext, | |
98 char* output, | |
99 size_t* output_length, | |
100 size_t max_output_length) override { | |
101 const size_t len = plaintext.size() + kTagSize; | |
102 if (max_output_length < len) { | |
103 return false; | |
104 } | |
105 // Memmove is safe for inplace encryption. | |
106 memmove(output, plaintext.data(), plaintext.size()); | |
107 output += plaintext.size(); | |
108 memset(output, tag_, kTagSize); | |
109 *output_length = len; | |
110 return true; | |
111 } | |
112 | |
113 size_t GetKeySize() const override { return 0; } | |
114 size_t GetNoncePrefixSize() const override { return 0; } | |
115 | |
116 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { | |
117 return ciphertext_size - kTagSize; | |
118 } | |
119 | |
120 size_t GetCiphertextSize(size_t plaintext_size) const override { | |
121 return plaintext_size + kTagSize; | |
122 } | |
123 | |
124 StringPiece GetKey() const override { return StringPiece(); } | |
125 | |
126 StringPiece GetNoncePrefix() const override { return StringPiece(); } | |
127 | |
128 private: | |
129 enum { | |
130 kTagSize = 12, | |
131 }; | |
132 | |
133 const uint8_t tag_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter); | |
136 }; | |
137 | |
138 // TaggingDecrypter ensures that the final kTagSize bytes of the message all | |
139 // have the same value and then removes them. | |
140 class TaggingDecrypter : public QuicDecrypter { | |
141 public: | |
142 ~TaggingDecrypter() override {} | |
143 | |
144 // QuicDecrypter interface | |
145 bool SetKey(StringPiece key) override { return true; } | |
146 | |
147 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } | |
148 | |
149 bool SetPreliminaryKey(StringPiece key) override { | |
150 QUIC_BUG << "should not be called"; | |
151 return false; | |
152 } | |
153 | |
154 bool SetDiversificationNonce(DiversificationNonce key) override { | |
155 return true; | |
156 } | |
157 | |
158 bool DecryptPacket(QuicPathId path_id, | |
159 QuicPacketNumber packet_number, | |
160 StringPiece associated_data, | |
161 StringPiece ciphertext, | |
162 char* output, | |
163 size_t* output_length, | |
164 size_t max_output_length) override { | |
165 if (ciphertext.size() < kTagSize) { | |
166 return false; | |
167 } | |
168 if (!CheckTag(ciphertext, GetTag(ciphertext))) { | |
169 return false; | |
170 } | |
171 *output_length = ciphertext.size() - kTagSize; | |
172 memcpy(output, ciphertext.data(), *output_length); | |
173 return true; | |
174 } | |
175 | |
176 StringPiece GetKey() const override { return StringPiece(); } | |
177 StringPiece GetNoncePrefix() const override { return StringPiece(); } | |
178 const char* cipher_name() const override { return "Tagging"; } | |
179 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. | |
180 uint32_t cipher_id() const override { return 0xFFFFFFF0; } | |
181 | |
182 protected: | |
183 virtual uint8_t GetTag(StringPiece ciphertext) { | |
184 return ciphertext.data()[ciphertext.size() - 1]; | |
185 } | |
186 | |
187 private: | |
188 enum { | |
189 kTagSize = 12, | |
190 }; | |
191 | |
192 bool CheckTag(StringPiece ciphertext, uint8_t tag) { | |
193 for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { | |
194 if (ciphertext.data()[i] != tag) { | |
195 return false; | |
196 } | |
197 } | |
198 | |
199 return true; | |
200 } | |
201 }; | |
202 | |
203 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message | |
204 // match the expected value. | |
205 class StrictTaggingDecrypter : public TaggingDecrypter { | |
206 public: | |
207 explicit StrictTaggingDecrypter(uint8_t tag) : tag_(tag) {} | |
208 ~StrictTaggingDecrypter() override {} | |
209 | |
210 // TaggingQuicDecrypter | |
211 uint8_t GetTag(StringPiece ciphertext) override { return tag_; } | |
212 | |
213 const char* cipher_name() const override { return "StrictTagging"; } | |
214 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. | |
215 uint32_t cipher_id() const override { return 0xFFFFFFF1; } | |
216 | |
217 private: | |
218 const uint8_t tag_; | |
219 }; | |
220 | |
221 class TestConnectionHelper : public QuicConnectionHelperInterface { | |
222 public: | |
223 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) | |
224 : clock_(clock), random_generator_(random_generator) { | |
225 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
226 } | |
227 | |
228 // QuicConnectionHelperInterface | |
229 const QuicClock* GetClock() const override { return clock_; } | |
230 | |
231 QuicRandom* GetRandomGenerator() override { return random_generator_; } | |
232 | |
233 QuicBufferAllocator* GetBufferAllocator() override { | |
234 return &buffer_allocator_; | |
235 } | |
236 | |
237 private: | |
238 MockClock* clock_; | |
239 MockRandom* random_generator_; | |
240 SimpleBufferAllocator buffer_allocator_; | |
241 | |
242 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); | |
243 }; | |
244 | |
245 class TestAlarmFactory : public QuicAlarmFactory { | |
246 public: | |
247 class TestAlarm : public QuicAlarm { | |
248 public: | |
249 explicit TestAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) | |
250 : QuicAlarm(std::move(delegate)) {} | |
251 | |
252 void SetImpl() override {} | |
253 void CancelImpl() override {} | |
254 using QuicAlarm::Fire; | |
255 }; | |
256 | |
257 TestAlarmFactory() {} | |
258 | |
259 QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override { | |
260 return new TestAlarm(QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); | |
261 } | |
262 | |
263 QuicArenaScopedPtr<QuicAlarm> CreateAlarm( | |
264 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, | |
265 QuicConnectionArena* arena) override { | |
266 return arena->New<TestAlarm>(std::move(delegate)); | |
267 } | |
268 | |
269 private: | |
270 DISALLOW_COPY_AND_ASSIGN(TestAlarmFactory); | |
271 }; | |
272 | |
273 class TestPacketWriter : public QuicPacketWriter { | |
274 public: | |
275 TestPacketWriter(QuicVersion version, MockClock* clock) | |
276 : version_(version), | |
277 framer_(SupportedVersions(version_)), | |
278 last_packet_size_(0), | |
279 write_blocked_(false), | |
280 write_should_fail_(false), | |
281 block_on_next_write_(false), | |
282 is_write_blocked_data_buffered_(false), | |
283 final_bytes_of_last_packet_(0), | |
284 final_bytes_of_previous_packet_(0), | |
285 use_tagging_decrypter_(false), | |
286 packets_write_attempts_(0), | |
287 clock_(clock), | |
288 write_pause_time_delta_(QuicTime::Delta::Zero()), | |
289 max_packet_size_(kMaxPacketSize) {} | |
290 | |
291 // QuicPacketWriter interface | |
292 WriteResult WritePacket(const char* buffer, | |
293 size_t buf_len, | |
294 const IPAddress& self_address, | |
295 const IPEndPoint& peer_address, | |
296 PerPacketOptions* options) override { | |
297 QuicEncryptedPacket packet(buffer, buf_len); | |
298 ++packets_write_attempts_; | |
299 | |
300 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { | |
301 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; | |
302 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, | |
303 sizeof(final_bytes_of_last_packet_)); | |
304 } | |
305 | |
306 if (use_tagging_decrypter_) { | |
307 framer_.framer()->SetDecrypter(ENCRYPTION_NONE, new TaggingDecrypter); | |
308 } | |
309 EXPECT_TRUE(framer_.ProcessPacket(packet)); | |
310 if (block_on_next_write_) { | |
311 write_blocked_ = true; | |
312 block_on_next_write_ = false; | |
313 } | |
314 if (IsWriteBlocked()) { | |
315 return WriteResult(WRITE_STATUS_BLOCKED, -1); | |
316 } | |
317 | |
318 if (ShouldWriteFail()) { | |
319 return WriteResult(WRITE_STATUS_ERROR, 0); | |
320 } | |
321 | |
322 last_packet_size_ = packet.length(); | |
323 | |
324 if (!write_pause_time_delta_.IsZero()) { | |
325 clock_->AdvanceTime(write_pause_time_delta_); | |
326 } | |
327 return WriteResult(WRITE_STATUS_OK, last_packet_size_); | |
328 } | |
329 | |
330 bool IsWriteBlockedDataBuffered() const override { | |
331 return is_write_blocked_data_buffered_; | |
332 } | |
333 | |
334 bool ShouldWriteFail() { return write_should_fail_; } | |
335 | |
336 bool IsWriteBlocked() const override { return write_blocked_; } | |
337 | |
338 void SetWritable() override { write_blocked_ = false; } | |
339 | |
340 void SetShouldWriteFail() { write_should_fail_ = true; } | |
341 | |
342 QuicByteCount GetMaxPacketSize( | |
343 const IPEndPoint& /*peer_address*/) const override { | |
344 return max_packet_size_; | |
345 } | |
346 | |
347 void BlockOnNextWrite() { block_on_next_write_ = true; } | |
348 | |
349 // Sets the amount of time that the writer should before the actual write. | |
350 void SetWritePauseTimeDelta(QuicTime::Delta delta) { | |
351 write_pause_time_delta_ = delta; | |
352 } | |
353 | |
354 const QuicPacketHeader& header() { return framer_.header(); } | |
355 | |
356 size_t frame_count() const { return framer_.num_frames(); } | |
357 | |
358 const vector<QuicAckFrame>& ack_frames() const { | |
359 return framer_.ack_frames(); | |
360 } | |
361 | |
362 const vector<QuicStopWaitingFrame>& stop_waiting_frames() const { | |
363 return framer_.stop_waiting_frames(); | |
364 } | |
365 | |
366 const vector<QuicConnectionCloseFrame>& connection_close_frames() const { | |
367 return framer_.connection_close_frames(); | |
368 } | |
369 | |
370 const vector<QuicRstStreamFrame>& rst_stream_frames() const { | |
371 return framer_.rst_stream_frames(); | |
372 } | |
373 | |
374 const vector<QuicStreamFrame*>& stream_frames() const { | |
375 return framer_.stream_frames(); | |
376 } | |
377 | |
378 const vector<QuicPingFrame>& ping_frames() const { | |
379 return framer_.ping_frames(); | |
380 } | |
381 | |
382 size_t last_packet_size() { return last_packet_size_; } | |
383 | |
384 const QuicVersionNegotiationPacket* version_negotiation_packet() { | |
385 return framer_.version_negotiation_packet(); | |
386 } | |
387 | |
388 void set_is_write_blocked_data_buffered(bool buffered) { | |
389 is_write_blocked_data_buffered_ = buffered; | |
390 } | |
391 | |
392 void set_perspective(Perspective perspective) { | |
393 // We invert perspective here, because the framer needs to parse packets | |
394 // we send. | |
395 QuicFramerPeer::SetPerspective(framer_.framer(), | |
396 InvertPerspective(perspective)); | |
397 } | |
398 | |
399 // final_bytes_of_last_packet_ returns the last four bytes of the previous | |
400 // packet as a little-endian, uint32_t. This is intended to be used with a | |
401 // TaggingEncrypter so that tests can determine which encrypter was used for | |
402 // a given packet. | |
403 uint32_t final_bytes_of_last_packet() { return final_bytes_of_last_packet_; } | |
404 | |
405 // Returns the final bytes of the second to last packet. | |
406 uint32_t final_bytes_of_previous_packet() { | |
407 return final_bytes_of_previous_packet_; | |
408 } | |
409 | |
410 void use_tagging_decrypter() { use_tagging_decrypter_ = true; } | |
411 | |
412 uint32_t packets_write_attempts() { return packets_write_attempts_; } | |
413 | |
414 void Reset() { framer_.Reset(); } | |
415 | |
416 void SetSupportedVersions(const QuicVersionVector& versions) { | |
417 framer_.SetSupportedVersions(versions); | |
418 } | |
419 | |
420 void set_max_packet_size(QuicByteCount max_packet_size) { | |
421 max_packet_size_ = max_packet_size; | |
422 } | |
423 | |
424 private: | |
425 QuicVersion version_; | |
426 SimpleQuicFramer framer_; | |
427 size_t last_packet_size_; | |
428 bool write_blocked_; | |
429 bool write_should_fail_; | |
430 bool block_on_next_write_; | |
431 bool is_write_blocked_data_buffered_; | |
432 uint32_t final_bytes_of_last_packet_; | |
433 uint32_t final_bytes_of_previous_packet_; | |
434 bool use_tagging_decrypter_; | |
435 uint32_t packets_write_attempts_; | |
436 MockClock* clock_; | |
437 // If non-zero, the clock will pause during WritePacket for this amount of | |
438 // time. | |
439 QuicTime::Delta write_pause_time_delta_; | |
440 QuicByteCount max_packet_size_; | |
441 | |
442 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter); | |
443 }; | |
444 | |
445 class TestConnection : public QuicConnection { | |
446 public: | |
447 TestConnection(QuicConnectionId connection_id, | |
448 IPEndPoint address, | |
449 TestConnectionHelper* helper, | |
450 TestAlarmFactory* alarm_factory, | |
451 TestPacketWriter* writer, | |
452 Perspective perspective, | |
453 QuicVersion version) | |
454 : QuicConnection(connection_id, | |
455 address, | |
456 helper, | |
457 alarm_factory, | |
458 writer, | |
459 /* owns_writer= */ false, | |
460 perspective, | |
461 SupportedVersions(version)) { | |
462 writer->set_perspective(perspective); | |
463 } | |
464 | |
465 void SendAck() { QuicConnectionPeer::SendAck(this); } | |
466 | |
467 void SetSendAlgorithm(QuicPathId path_id, | |
468 SendAlgorithmInterface* send_algorithm) { | |
469 QuicConnectionPeer::SetSendAlgorithm(this, path_id, send_algorithm); | |
470 } | |
471 | |
472 void SetLossAlgorithm(QuicPathId path_id, | |
473 LossDetectionInterface* loss_algorithm) { | |
474 QuicConnectionPeer::SetLossAlgorithm(this, path_id, loss_algorithm); | |
475 } | |
476 | |
477 void SendPacket(EncryptionLevel level, | |
478 QuicPathId path_id, | |
479 QuicPacketNumber packet_number, | |
480 QuicPacket* packet, | |
481 QuicPacketEntropyHash entropy_hash, | |
482 HasRetransmittableData retransmittable, | |
483 bool has_ack, | |
484 bool has_pending_frames) { | |
485 char buffer[kMaxPacketSize]; | |
486 size_t encrypted_length = | |
487 QuicConnectionPeer::GetFramer(this)->EncryptPayload( | |
488 ENCRYPTION_NONE, path_id, packet_number, *packet, buffer, | |
489 kMaxPacketSize); | |
490 delete packet; | |
491 SerializedPacket serialized_packet( | |
492 kDefaultPathId, packet_number, PACKET_6BYTE_PACKET_NUMBER, buffer, | |
493 encrypted_length, entropy_hash, has_ack, has_pending_frames); | |
494 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { | |
495 serialized_packet.retransmittable_frames.push_back( | |
496 QuicFrame(new QuicStreamFrame())); | |
497 } | |
498 OnSerializedPacket(&serialized_packet); | |
499 } | |
500 | |
501 QuicConsumedData SendStreamDataWithString( | |
502 QuicStreamId id, | |
503 StringPiece data, | |
504 QuicStreamOffset offset, | |
505 bool fin, | |
506 QuicAckListenerInterface* listener) { | |
507 struct iovec iov; | |
508 QuicIOVector data_iov(MakeIOVector(data, &iov)); | |
509 return QuicConnection::SendStreamData(id, data_iov, offset, fin, listener); | |
510 } | |
511 | |
512 QuicConsumedData SendStreamData3() { | |
513 return SendStreamDataWithString(kClientDataStreamId1, "food", 0, !kFin, | |
514 nullptr); | |
515 } | |
516 | |
517 QuicConsumedData SendStreamData5() { | |
518 return SendStreamDataWithString(kClientDataStreamId2, "food2", 0, !kFin, | |
519 nullptr); | |
520 } | |
521 | |
522 // Ensures the connection can write stream data before writing. | |
523 QuicConsumedData EnsureWritableAndSendStreamData5() { | |
524 EXPECT_TRUE(CanWriteStreamData()); | |
525 return SendStreamData5(); | |
526 } | |
527 | |
528 // The crypto stream has special semantics so that it is not blocked by a | |
529 // congestion window limitation, and also so that it gets put into a separate | |
530 // packet (so that it is easier to reason about a crypto frame not being | |
531 // split needlessly across packet boundaries). As a result, we have separate | |
532 // tests for some cases for this stream. | |
533 QuicConsumedData SendCryptoStreamData() { | |
534 return SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin, nullptr); | |
535 } | |
536 | |
537 void set_version(QuicVersion version) { | |
538 QuicConnectionPeer::GetFramer(this)->set_version(version); | |
539 } | |
540 | |
541 void SetSupportedVersions(const QuicVersionVector& versions) { | |
542 QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions); | |
543 writer()->SetSupportedVersions(versions); | |
544 } | |
545 | |
546 void set_perspective(Perspective perspective) { | |
547 writer()->set_perspective(perspective); | |
548 QuicConnectionPeer::SetPerspective(this, perspective); | |
549 } | |
550 | |
551 // Enable path MTU discovery. Assumes that the test is performed from the | |
552 // client perspective and the higher value of MTU target is used. | |
553 void EnablePathMtuDiscovery(MockSendAlgorithm* send_algorithm) { | |
554 ASSERT_EQ(Perspective::IS_CLIENT, perspective()); | |
555 | |
556 QuicConfig config; | |
557 QuicTagVector connection_options; | |
558 connection_options.push_back(kMTUH); | |
559 config.SetConnectionOptionsToSend(connection_options); | |
560 EXPECT_CALL(*send_algorithm, SetFromConfig(_, _)); | |
561 SetFromConfig(config); | |
562 | |
563 // Normally, the pacing would be disabled in the test, but calling | |
564 // SetFromConfig enables it. Set nearly-infinite bandwidth to make the | |
565 // pacing algorithm work. | |
566 EXPECT_CALL(*send_algorithm, PacingRate(_)) | |
567 .WillRepeatedly(Return(QuicBandwidth::FromKBytesPerSecond(10000))); | |
568 } | |
569 | |
570 TestAlarmFactory::TestAlarm* GetAckAlarm() { | |
571 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
572 QuicConnectionPeer::GetAckAlarm(this)); | |
573 } | |
574 | |
575 TestAlarmFactory::TestAlarm* GetPingAlarm() { | |
576 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
577 QuicConnectionPeer::GetPingAlarm(this)); | |
578 } | |
579 | |
580 TestAlarmFactory::TestAlarm* GetResumeWritesAlarm() { | |
581 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
582 QuicConnectionPeer::GetResumeWritesAlarm(this)); | |
583 } | |
584 | |
585 TestAlarmFactory::TestAlarm* GetRetransmissionAlarm() { | |
586 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
587 QuicConnectionPeer::GetRetransmissionAlarm(this)); | |
588 } | |
589 | |
590 TestAlarmFactory::TestAlarm* GetSendAlarm() { | |
591 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
592 QuicConnectionPeer::GetSendAlarm(this)); | |
593 } | |
594 | |
595 TestAlarmFactory::TestAlarm* GetTimeoutAlarm() { | |
596 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
597 QuicConnectionPeer::GetTimeoutAlarm(this)); | |
598 } | |
599 | |
600 TestAlarmFactory::TestAlarm* GetMtuDiscoveryAlarm() { | |
601 return reinterpret_cast<TestAlarmFactory::TestAlarm*>( | |
602 QuicConnectionPeer::GetMtuDiscoveryAlarm(this)); | |
603 } | |
604 | |
605 void SetMaxTailLossProbes(QuicPathId path_id, size_t max_tail_loss_probes) { | |
606 QuicSentPacketManagerPeer::SetMaxTailLossProbes( | |
607 QuicConnectionPeer::GetSentPacketManager(this, path_id), | |
608 max_tail_loss_probes); | |
609 } | |
610 | |
611 QuicByteCount GetBytesInFlight(QuicPathId path_id) { | |
612 return QuicSentPacketManagerPeer::GetBytesInFlight( | |
613 QuicConnectionPeer::GetSentPacketManager(this, path_id)); | |
614 } | |
615 | |
616 using QuicConnection::SelectMutualVersion; | |
617 using QuicConnection::set_defer_send_in_response_to_packets; | |
618 | |
619 private: | |
620 TestPacketWriter* writer() { | |
621 return static_cast<TestPacketWriter*>(QuicConnection::writer()); | |
622 } | |
623 | |
624 DISALLOW_COPY_AND_ASSIGN(TestConnection); | |
625 }; | |
626 | |
627 enum class AckResponse { kDefer, kImmediate }; | |
628 | |
629 // Run tests with combinations of {QuicVersion, AckResponse}. | |
630 struct TestParams { | |
631 TestParams(QuicVersion version, AckResponse ack_response) | |
632 : version(version), ack_response(ack_response) {} | |
633 | |
634 friend ostream& operator<<(ostream& os, const TestParams& p) { | |
635 os << "{ client_version: " << QuicVersionToString(p.version) | |
636 << " ack_response: " | |
637 << (p.ack_response == AckResponse::kDefer ? "defer" : "immediate") | |
638 << " }"; | |
639 return os; | |
640 } | |
641 | |
642 QuicVersion version; | |
643 AckResponse ack_response; | |
644 }; | |
645 | |
646 // Constructs various test permutations. | |
647 vector<TestParams> GetTestParams() { | |
648 vector<TestParams> params; | |
649 QuicVersionVector all_supported_versions = QuicSupportedVersions(); | |
650 for (size_t i = 0; i < all_supported_versions.size(); ++i) { | |
651 for (AckResponse ack_response : | |
652 {AckResponse::kDefer, AckResponse::kImmediate}) { | |
653 params.push_back(TestParams(all_supported_versions[i], ack_response)); | |
654 } | |
655 } | |
656 return params; | |
657 } | |
658 | |
659 class QuicConnectionTest : public ::testing::TestWithParam<TestParams> { | |
660 protected: | |
661 QuicConnectionTest() | |
662 : connection_id_(42), | |
663 framer_(SupportedVersions(version()), | |
664 QuicTime::Zero(), | |
665 Perspective::IS_CLIENT), | |
666 send_algorithm_(new StrictMock<MockSendAlgorithm>), | |
667 loss_algorithm_(new MockLossAlgorithm()), | |
668 helper_(new TestConnectionHelper(&clock_, &random_generator_)), | |
669 alarm_factory_(new TestAlarmFactory()), | |
670 peer_framer_(SupportedVersions(version()), | |
671 QuicTime::Zero(), | |
672 Perspective::IS_SERVER), | |
673 peer_creator_(connection_id_, | |
674 &peer_framer_, | |
675 &random_generator_, | |
676 &buffer_allocator_, | |
677 /*delegate=*/nullptr), | |
678 writer_(new TestPacketWriter(version(), &clock_)), | |
679 connection_(connection_id_, | |
680 kPeerAddress, | |
681 helper_.get(), | |
682 alarm_factory_.get(), | |
683 writer_.get(), | |
684 Perspective::IS_CLIENT, | |
685 version()), | |
686 creator_(QuicConnectionPeer::GetPacketCreator(&connection_)), | |
687 generator_(QuicConnectionPeer::GetPacketGenerator(&connection_)), | |
688 manager_(QuicConnectionPeer::GetSentPacketManager(&connection_, | |
689 kDefaultPathId)), | |
690 frame1_(1, false, 0, StringPiece(data1)), | |
691 frame2_(1, false, 3, StringPiece(data2)), | |
692 packet_number_length_(PACKET_6BYTE_PACKET_NUMBER), | |
693 connection_id_length_(PACKET_8BYTE_CONNECTION_ID) { | |
694 connection_.set_defer_send_in_response_to_packets(GetParam().ack_response == | |
695 AckResponse::kDefer); | |
696 FLAGS_quic_always_log_bugs_for_tests = true; | |
697 connection_.set_visitor(&visitor_); | |
698 connection_.SetSendAlgorithm(kDefaultPathId, send_algorithm_); | |
699 connection_.SetLossAlgorithm(kDefaultPathId, loss_algorithm_); | |
700 framer_.set_received_entropy_calculator(&entropy_calculator_); | |
701 peer_framer_.set_received_entropy_calculator(&peer_entropy_calculator_); | |
702 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
703 .WillRepeatedly(Return(QuicTime::Delta::Zero())); | |
704 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
705 .Times(AnyNumber()); | |
706 EXPECT_CALL(*send_algorithm_, RetransmissionDelay()) | |
707 .WillRepeatedly(Return(QuicTime::Delta::Zero())); | |
708 EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) | |
709 .WillRepeatedly(Return(kDefaultTCPMSS)); | |
710 EXPECT_CALL(*send_algorithm_, PacingRate(_)) | |
711 .WillRepeatedly(Return(QuicBandwidth::Zero())); | |
712 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
713 .WillByDefault(Return(true)); | |
714 EXPECT_CALL(*send_algorithm_, HasReliableBandwidthEstimate()) | |
715 .Times(AnyNumber()); | |
716 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) | |
717 .Times(AnyNumber()) | |
718 .WillRepeatedly(Return(QuicBandwidth::Zero())); | |
719 EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber()); | |
720 EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber()); | |
721 EXPECT_CALL(visitor_, WillingAndAbleToWrite()).Times(AnyNumber()); | |
722 EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber()); | |
723 EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber()); | |
724 EXPECT_CALL(visitor_, PostProcessAfterData()).Times(AnyNumber()); | |
725 EXPECT_CALL(visitor_, HasOpenDynamicStreams()) | |
726 .WillRepeatedly(Return(false)); | |
727 EXPECT_CALL(visitor_, OnCongestionWindowChange(_)).Times(AnyNumber()); | |
728 | |
729 EXPECT_CALL(*loss_algorithm_, GetLossTimeout()) | |
730 .WillRepeatedly(Return(QuicTime::Zero())); | |
731 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
732 .Times(AnyNumber()); | |
733 // TODO(ianswett): Fix QuicConnectionTests so they don't attempt to write | |
734 // non-crypto stream data at ENCRYPTION_NONE. | |
735 FLAGS_quic_never_write_unencrypted_data = false; | |
736 } | |
737 | |
738 QuicVersion version() { return GetParam().version; } | |
739 | |
740 QuicAckFrame* outgoing_ack() { | |
741 QuicFrame ack_frame = QuicConnectionPeer::GetUpdatedAckFrame(&connection_); | |
742 ack_ = *ack_frame.ack_frame; | |
743 return &ack_; | |
744 } | |
745 | |
746 QuicStopWaitingFrame* stop_waiting() { | |
747 QuicConnectionPeer::PopulateStopWaitingFrame(&connection_, &stop_waiting_); | |
748 return &stop_waiting_; | |
749 } | |
750 | |
751 QuicPacketNumber least_unacked() { | |
752 if (writer_->stop_waiting_frames().empty()) { | |
753 return 0; | |
754 } | |
755 return writer_->stop_waiting_frames()[0].least_unacked; | |
756 } | |
757 | |
758 void use_tagging_decrypter() { writer_->use_tagging_decrypter(); } | |
759 | |
760 void ProcessPacket(QuicPathId path_id, QuicPacketNumber number) { | |
761 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
762 ProcessDataPacket(path_id, number, !kEntropyFlag); | |
763 if (connection_.GetSendAlarm()->IsSet()) { | |
764 connection_.GetSendAlarm()->Fire(); | |
765 } | |
766 } | |
767 | |
768 QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) { | |
769 return ProcessFramePacketWithAddresses(frame, kSelfAddress, kPeerAddress); | |
770 } | |
771 | |
772 QuicPacketEntropyHash ProcessFramePacketWithAddresses( | |
773 QuicFrame frame, | |
774 IPEndPoint self_address, | |
775 IPEndPoint peer_address) { | |
776 QuicFrames frames; | |
777 frames.push_back(QuicFrame(frame)); | |
778 QuicPacketCreatorPeer::SetSendVersionInPacket( | |
779 &peer_creator_, connection_.perspective() == Perspective::IS_SERVER); | |
780 | |
781 char buffer[kMaxPacketSize]; | |
782 SerializedPacket serialized_packet = | |
783 QuicPacketCreatorPeer::SerializeAllFrames(&peer_creator_, frames, | |
784 buffer, kMaxPacketSize); | |
785 connection_.ProcessUdpPacket( | |
786 self_address, peer_address, | |
787 QuicReceivedPacket(serialized_packet.encrypted_buffer, | |
788 serialized_packet.encrypted_length, clock_.Now())); | |
789 if (connection_.GetSendAlarm()->IsSet()) { | |
790 connection_.GetSendAlarm()->Fire(); | |
791 } | |
792 return serialized_packet.entropy_hash; | |
793 } | |
794 | |
795 QuicPacketEntropyHash ProcessFramePacketAtLevel(QuicPathId path_id, | |
796 QuicPacketNumber number, | |
797 QuicFrame frame, | |
798 EncryptionLevel level) { | |
799 QuicPacketHeader header; | |
800 header.public_header.connection_id = connection_id_; | |
801 header.public_header.packet_number_length = packet_number_length_; | |
802 header.public_header.connection_id_length = connection_id_length_; | |
803 header.public_header.multipath_flag = path_id != kDefaultPathId; | |
804 header.path_id = path_id; | |
805 header.packet_number = number; | |
806 QuicFrames frames; | |
807 frames.push_back(frame); | |
808 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
809 | |
810 char buffer[kMaxPacketSize]; | |
811 size_t encrypted_length = framer_.EncryptPayload( | |
812 level, path_id, number, *packet, buffer, kMaxPacketSize); | |
813 connection_.ProcessUdpPacket( | |
814 kSelfAddress, kPeerAddress, | |
815 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
816 return base::checked_cast<QuicPacketEntropyHash>(encrypted_length); | |
817 } | |
818 | |
819 size_t ProcessDataPacket(QuicPathId path_id, | |
820 QuicPacketNumber number, | |
821 bool entropy_flag) { | |
822 return ProcessDataPacketAtLevel(path_id, number, entropy_flag, false, | |
823 ENCRYPTION_NONE); | |
824 } | |
825 | |
826 size_t ProcessDataPacketAtLevel(QuicPathId path_id, | |
827 QuicPacketNumber number, | |
828 bool entropy_flag, | |
829 bool has_stop_waiting, | |
830 EncryptionLevel level) { | |
831 std::unique_ptr<QuicPacket> packet( | |
832 ConstructDataPacket(path_id, number, entropy_flag, has_stop_waiting)); | |
833 char buffer[kMaxPacketSize]; | |
834 size_t encrypted_length = framer_.EncryptPayload( | |
835 level, path_id, number, *packet, buffer, kMaxPacketSize); | |
836 connection_.ProcessUdpPacket( | |
837 kSelfAddress, kPeerAddress, | |
838 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
839 if (connection_.GetSendAlarm()->IsSet()) { | |
840 connection_.GetSendAlarm()->Fire(); | |
841 } | |
842 return encrypted_length; | |
843 } | |
844 | |
845 void ProcessClosePacket(QuicPathId path_id, QuicPacketNumber number) { | |
846 std::unique_ptr<QuicPacket> packet(ConstructClosePacket(number)); | |
847 char buffer[kMaxPacketSize]; | |
848 size_t encrypted_length = framer_.EncryptPayload( | |
849 ENCRYPTION_NONE, path_id, number, *packet, buffer, kMaxPacketSize); | |
850 connection_.ProcessUdpPacket( | |
851 kSelfAddress, kPeerAddress, | |
852 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
853 } | |
854 | |
855 QuicByteCount SendStreamDataToPeer(QuicStreamId id, | |
856 StringPiece data, | |
857 QuicStreamOffset offset, | |
858 bool fin, | |
859 QuicPacketNumber* last_packet) { | |
860 QuicByteCount packet_size; | |
861 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
862 .WillOnce(DoAll(SaveArg<3>(&packet_size), Return(true))); | |
863 connection_.SendStreamDataWithString(id, data, offset, fin, nullptr); | |
864 if (last_packet != nullptr) { | |
865 *last_packet = creator_->packet_number(); | |
866 } | |
867 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
868 .Times(AnyNumber()); | |
869 return packet_size; | |
870 } | |
871 | |
872 void SendAckPacketToPeer() { | |
873 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
874 connection_.SendAck(); | |
875 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
876 .Times(AnyNumber()); | |
877 } | |
878 | |
879 void ProcessAckPacket(QuicPacketNumber packet_number, QuicAckFrame* frame) { | |
880 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, packet_number - 1); | |
881 ProcessFramePacket(QuicFrame(frame)); | |
882 } | |
883 | |
884 QuicPacketEntropyHash ProcessAckPacket(QuicAckFrame* frame) { | |
885 return ProcessFramePacket(QuicFrame(frame)); | |
886 } | |
887 | |
888 QuicPacketEntropyHash ProcessStopWaitingPacket(QuicStopWaitingFrame* frame) { | |
889 return ProcessFramePacket(QuicFrame(frame)); | |
890 } | |
891 | |
892 QuicPacketEntropyHash ProcessStopWaitingPacketAtLevel( | |
893 QuicPathId path_id, | |
894 QuicPacketNumber number, | |
895 QuicStopWaitingFrame* frame, | |
896 EncryptionLevel level) { | |
897 return ProcessFramePacketAtLevel(path_id, number, QuicFrame(frame), | |
898 ENCRYPTION_INITIAL); | |
899 } | |
900 | |
901 QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) { | |
902 return ProcessFramePacket(QuicFrame(frame)); | |
903 } | |
904 | |
905 QuicPacketEntropyHash ProcessPathClosePacket(QuicPathCloseFrame* frame) { | |
906 return ProcessFramePacket(QuicFrame(frame)); | |
907 } | |
908 | |
909 bool IsMissing(QuicPacketNumber number) { | |
910 return IsAwaitingPacket(*outgoing_ack(), number, 0); | |
911 } | |
912 | |
913 QuicPacket* ConstructPacket(QuicPacketHeader header, QuicFrames frames) { | |
914 QuicPacket* packet = BuildUnsizedDataPacket(&peer_framer_, header, frames); | |
915 EXPECT_NE(nullptr, packet); | |
916 return packet; | |
917 } | |
918 | |
919 QuicPacket* ConstructDataPacket(QuicPathId path_id, | |
920 QuicPacketNumber number, | |
921 bool entropy_flag, | |
922 bool has_stop_waiting) { | |
923 QuicPacketHeader header; | |
924 header.public_header.connection_id = connection_id_; | |
925 header.public_header.packet_number_length = packet_number_length_; | |
926 header.public_header.connection_id_length = connection_id_length_; | |
927 header.public_header.multipath_flag = path_id != kDefaultPathId; | |
928 header.entropy_flag = entropy_flag; | |
929 header.path_id = path_id; | |
930 header.packet_number = number; | |
931 | |
932 QuicFrames frames; | |
933 frames.push_back(QuicFrame(&frame1_)); | |
934 if (has_stop_waiting) { | |
935 frames.push_back(QuicFrame(&stop_waiting_)); | |
936 } | |
937 return ConstructPacket(header, frames); | |
938 } | |
939 | |
940 QuicPacket* ConstructClosePacket(QuicPacketNumber number) { | |
941 QuicPacketHeader header; | |
942 header.public_header.connection_id = connection_id_; | |
943 header.packet_number = number; | |
944 | |
945 QuicConnectionCloseFrame qccf; | |
946 qccf.error_code = QUIC_PEER_GOING_AWAY; | |
947 | |
948 QuicFrames frames; | |
949 frames.push_back(QuicFrame(&qccf)); | |
950 return ConstructPacket(header, frames); | |
951 } | |
952 | |
953 QuicTime::Delta DefaultRetransmissionTime() { | |
954 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); | |
955 } | |
956 | |
957 QuicTime::Delta DefaultDelayedAckTime() { | |
958 return QuicTime::Delta::FromMilliseconds(kMaxDelayedAckTimeMs); | |
959 } | |
960 | |
961 // Initialize a frame acknowledging all packets up to largest_observed. | |
962 const QuicAckFrame InitAckFrame(QuicPacketNumber largest_observed) { | |
963 QuicAckFrame frame(MakeAckFrame(largest_observed)); | |
964 if (GetParam().version <= QUIC_VERSION_33) { | |
965 if (largest_observed > 0) { | |
966 frame.entropy_hash = QuicConnectionPeer::GetSentEntropyHash( | |
967 &connection_, largest_observed); | |
968 } | |
969 } else { | |
970 frame.missing = false; | |
971 if (largest_observed > 0) { | |
972 frame.packets.Add(1, largest_observed + 1); | |
973 } | |
974 } | |
975 return frame; | |
976 } | |
977 | |
978 const QuicStopWaitingFrame InitStopWaitingFrame( | |
979 QuicPacketNumber least_unacked) { | |
980 QuicStopWaitingFrame frame; | |
981 frame.least_unacked = least_unacked; | |
982 return frame; | |
983 } | |
984 | |
985 // Explicitly nack a packet. | |
986 void NackPacket(QuicPacketNumber missing, QuicAckFrame* frame) { | |
987 if (frame->missing) { | |
988 frame->packets.Add(missing); | |
989 frame->entropy_hash ^= | |
990 QuicConnectionPeer::PacketEntropy(&connection_, missing); | |
991 } else { | |
992 frame->packets.Remove(missing); | |
993 } | |
994 } | |
995 | |
996 // Undo nacking a packet within the frame. | |
997 void AckPacket(QuicPacketNumber arrived, QuicAckFrame* frame) { | |
998 if (frame->missing) { | |
999 EXPECT_TRUE(frame->packets.Contains(arrived)); | |
1000 frame->packets.Remove(arrived); | |
1001 frame->entropy_hash ^= | |
1002 QuicConnectionPeer::PacketEntropy(&connection_, arrived); | |
1003 } else { | |
1004 EXPECT_FALSE(frame->packets.Contains(arrived)); | |
1005 frame->packets.Add(arrived); | |
1006 } | |
1007 } | |
1008 | |
1009 void TriggerConnectionClose() { | |
1010 // Send an erroneous packet to close the connection. | |
1011 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, _, | |
1012 ConnectionCloseSource::FROM_SELF)); | |
1013 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a | |
1014 // packet call to the visitor. | |
1015 ProcessDataPacket(kDefaultPathId, 6000, !kEntropyFlag); | |
1016 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == | |
1017 nullptr); | |
1018 } | |
1019 | |
1020 void BlockOnNextWrite() { | |
1021 writer_->BlockOnNextWrite(); | |
1022 EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1)); | |
1023 } | |
1024 | |
1025 void SetWritePauseTimeDelta(QuicTime::Delta delta) { | |
1026 writer_->SetWritePauseTimeDelta(delta); | |
1027 } | |
1028 | |
1029 void CongestionBlockWrites() { | |
1030 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
1031 .WillRepeatedly(testing::Return(QuicTime::Delta::FromSeconds(1))); | |
1032 } | |
1033 | |
1034 void CongestionUnblockWrites() { | |
1035 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
1036 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); | |
1037 } | |
1038 | |
1039 void set_perspective(Perspective perspective) { | |
1040 connection_.set_perspective(perspective); | |
1041 QuicFramerPeer::SetPerspective(&peer_framer_, | |
1042 InvertPerspective(perspective)); | |
1043 } | |
1044 | |
1045 QuicConnectionId connection_id_; | |
1046 QuicFramer framer_; | |
1047 MockEntropyCalculator entropy_calculator_; | |
1048 MockEntropyCalculator peer_entropy_calculator_; | |
1049 | |
1050 MockSendAlgorithm* send_algorithm_; | |
1051 MockLossAlgorithm* loss_algorithm_; | |
1052 MockClock clock_; | |
1053 MockRandom random_generator_; | |
1054 SimpleBufferAllocator buffer_allocator_; | |
1055 std::unique_ptr<TestConnectionHelper> helper_; | |
1056 std::unique_ptr<TestAlarmFactory> alarm_factory_; | |
1057 QuicFramer peer_framer_; | |
1058 QuicPacketCreator peer_creator_; | |
1059 std::unique_ptr<TestPacketWriter> writer_; | |
1060 TestConnection connection_; | |
1061 QuicPacketCreator* creator_; | |
1062 QuicPacketGenerator* generator_; | |
1063 QuicSentPacketManagerInterface* manager_; | |
1064 StrictMock<MockQuicConnectionVisitor> visitor_; | |
1065 | |
1066 QuicStreamFrame frame1_; | |
1067 QuicStreamFrame frame2_; | |
1068 QuicAckFrame ack_; | |
1069 QuicStopWaitingFrame stop_waiting_; | |
1070 QuicPacketNumberLength packet_number_length_; | |
1071 QuicConnectionIdLength connection_id_length_; | |
1072 | |
1073 private: | |
1074 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest); | |
1075 }; | |
1076 | |
1077 // Run all end to end tests with all supported versions. | |
1078 INSTANTIATE_TEST_CASE_P(SupportedVersion, | |
1079 QuicConnectionTest, | |
1080 ::testing::ValuesIn(GetTestParams())); | |
1081 | |
1082 TEST_P(QuicConnectionTest, SelfAddressChangeAtClient) { | |
1083 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1084 | |
1085 EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); | |
1086 EXPECT_TRUE(connection_.connected()); | |
1087 | |
1088 QuicStreamFrame stream_frame(1u, false, 0u, StringPiece()); | |
1089 EXPECT_CALL(visitor_, OnStreamFrame(_)); | |
1090 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), kSelfAddress, | |
1091 kPeerAddress); | |
1092 // Cause change in self_address. | |
1093 IPEndPoint self_address(IPAddress(1, 1, 1, 1), 123); | |
1094 EXPECT_CALL(visitor_, OnStreamFrame(_)); | |
1095 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), self_address, | |
1096 kPeerAddress); | |
1097 EXPECT_TRUE(connection_.connected()); | |
1098 } | |
1099 | |
1100 TEST_P(QuicConnectionTest, SelfAddressChangeAtServer) { | |
1101 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1102 | |
1103 set_perspective(Perspective::IS_SERVER); | |
1104 QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); | |
1105 | |
1106 EXPECT_EQ(Perspective::IS_SERVER, connection_.perspective()); | |
1107 EXPECT_TRUE(connection_.connected()); | |
1108 | |
1109 QuicStreamFrame stream_frame(1u, false, 0u, StringPiece()); | |
1110 EXPECT_CALL(visitor_, OnStreamFrame(_)); | |
1111 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), kSelfAddress, | |
1112 kPeerAddress); | |
1113 // Cause change in self_address. | |
1114 IPEndPoint self_address(IPAddress(1, 1, 1, 1), 123); | |
1115 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_ERROR_MIGRATING_ADDRESS, _, _)); | |
1116 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), self_address, | |
1117 kPeerAddress); | |
1118 EXPECT_FALSE(connection_.connected()); | |
1119 } | |
1120 | |
1121 TEST_P(QuicConnectionTest, ClientAddressChangeAndPacketReordered) { | |
1122 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1123 set_perspective(Perspective::IS_SERVER); | |
1124 QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); | |
1125 // Clear peer address. | |
1126 QuicConnectionPeer::SetPeerAddress(&connection_, IPEndPoint()); | |
1127 | |
1128 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); | |
1129 QuicStreamFrame stream_frame(1u, false, 0u, StringPiece()); | |
1130 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AnyNumber()); | |
1131 const IPEndPoint kNewPeerAddress = IPEndPoint(Loopback6(), | |
1132 /*port=*/23456); | |
1133 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), kSelfAddress, | |
1134 kNewPeerAddress); | |
1135 | |
1136 // Decrease packet number to simulate out-of-order packets. | |
1137 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 4); | |
1138 if (FLAGS_quic_do_not_migrate_on_old_packet) { | |
1139 // This is an old packet, do not migrate. | |
1140 EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)).Times(0); | |
1141 } else { | |
1142 // A connection migration is observed. | |
1143 EXPECT_CALL(visitor_, OnConnectionMigration(PORT_CHANGE)); | |
1144 } | |
1145 ProcessFramePacketWithAddresses(QuicFrame(&stream_frame), kSelfAddress, | |
1146 kPeerAddress); | |
1147 } | |
1148 | |
1149 TEST_P(QuicConnectionTest, MaxPacketSize) { | |
1150 EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective()); | |
1151 EXPECT_EQ(1350u, connection_.max_packet_length()); | |
1152 } | |
1153 | |
1154 TEST_P(QuicConnectionTest, SmallerServerMaxPacketSize) { | |
1155 QuicConnectionId connection_id = 42; | |
1156 TestConnection connection(connection_id, kPeerAddress, helper_.get(), | |
1157 alarm_factory_.get(), writer_.get(), | |
1158 Perspective::IS_SERVER, version()); | |
1159 EXPECT_EQ(Perspective::IS_SERVER, connection.perspective()); | |
1160 EXPECT_EQ(1000u, connection.max_packet_length()); | |
1161 } | |
1162 | |
1163 TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSize) { | |
1164 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1165 | |
1166 set_perspective(Perspective::IS_SERVER); | |
1167 connection_.SetMaxPacketLength(1000); | |
1168 | |
1169 QuicPacketHeader header; | |
1170 header.public_header.connection_id = connection_id_; | |
1171 header.public_header.version_flag = true; | |
1172 header.path_id = kDefaultPathId; | |
1173 header.packet_number = 1; | |
1174 | |
1175 QuicFrames frames; | |
1176 QuicPaddingFrame padding; | |
1177 frames.push_back(QuicFrame(&frame1_)); | |
1178 frames.push_back(QuicFrame(padding)); | |
1179 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
1180 char buffer[kMaxPacketSize]; | |
1181 size_t encrypted_length = framer_.EncryptPayload( | |
1182 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
1183 EXPECT_EQ(kMaxPacketSize, encrypted_length); | |
1184 | |
1185 framer_.set_version(version()); | |
1186 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
1187 connection_.ProcessUdpPacket( | |
1188 kSelfAddress, kPeerAddress, | |
1189 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
1190 | |
1191 EXPECT_EQ(kMaxPacketSize, connection_.max_packet_length()); | |
1192 } | |
1193 | |
1194 TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSizeWhileWriterLimited) { | |
1195 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1196 | |
1197 const QuicByteCount lower_max_packet_size = 1240; | |
1198 writer_->set_max_packet_size(lower_max_packet_size); | |
1199 set_perspective(Perspective::IS_SERVER); | |
1200 connection_.SetMaxPacketLength(1000); | |
1201 EXPECT_EQ(1000u, connection_.max_packet_length()); | |
1202 | |
1203 QuicPacketHeader header; | |
1204 header.public_header.connection_id = connection_id_; | |
1205 header.public_header.version_flag = true; | |
1206 header.path_id = kDefaultPathId; | |
1207 header.packet_number = 1; | |
1208 | |
1209 QuicFrames frames; | |
1210 QuicPaddingFrame padding; | |
1211 frames.push_back(QuicFrame(&frame1_)); | |
1212 frames.push_back(QuicFrame(padding)); | |
1213 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
1214 char buffer[kMaxPacketSize]; | |
1215 size_t encrypted_length = framer_.EncryptPayload( | |
1216 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
1217 EXPECT_EQ(kMaxPacketSize, encrypted_length); | |
1218 | |
1219 framer_.set_version(version()); | |
1220 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
1221 connection_.ProcessUdpPacket( | |
1222 kSelfAddress, kPeerAddress, | |
1223 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
1224 | |
1225 // Here, the limit imposed by the writer is lower than the size of the packet | |
1226 // received, so the writer max packet size is used. | |
1227 EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); | |
1228 } | |
1229 | |
1230 TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriter) { | |
1231 const QuicByteCount lower_max_packet_size = 1240; | |
1232 writer_->set_max_packet_size(lower_max_packet_size); | |
1233 | |
1234 static_assert(lower_max_packet_size < kDefaultMaxPacketSize, | |
1235 "Default maximum packet size is too low"); | |
1236 connection_.SetMaxPacketLength(kDefaultMaxPacketSize); | |
1237 | |
1238 EXPECT_EQ(lower_max_packet_size, connection_.max_packet_length()); | |
1239 } | |
1240 | |
1241 TEST_P(QuicConnectionTest, LimitMaxPacketSizeByWriterForNewConnection) { | |
1242 const QuicConnectionId connection_id = 17; | |
1243 const QuicByteCount lower_max_packet_size = 1240; | |
1244 writer_->set_max_packet_size(lower_max_packet_size); | |
1245 TestConnection connection(connection_id, kPeerAddress, helper_.get(), | |
1246 alarm_factory_.get(), writer_.get(), | |
1247 Perspective::IS_CLIENT, version()); | |
1248 EXPECT_EQ(Perspective::IS_CLIENT, connection.perspective()); | |
1249 EXPECT_EQ(lower_max_packet_size, connection.max_packet_length()); | |
1250 } | |
1251 | |
1252 TEST_P(QuicConnectionTest, PacketsInOrder) { | |
1253 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1254 | |
1255 ProcessPacket(kDefaultPathId, 1); | |
1256 EXPECT_EQ(1u, outgoing_ack()->largest_observed); | |
1257 if (outgoing_ack()->missing) { | |
1258 EXPECT_TRUE(outgoing_ack()->packets.Empty()); | |
1259 } else { | |
1260 EXPECT_EQ(1u, outgoing_ack()->packets.NumIntervals()); | |
1261 } | |
1262 | |
1263 ProcessPacket(kDefaultPathId, 2); | |
1264 EXPECT_EQ(2u, outgoing_ack()->largest_observed); | |
1265 if (outgoing_ack()->missing) { | |
1266 EXPECT_TRUE(outgoing_ack()->packets.Empty()); | |
1267 } else { | |
1268 EXPECT_EQ(1u, outgoing_ack()->packets.NumIntervals()); | |
1269 } | |
1270 | |
1271 ProcessPacket(kDefaultPathId, 3); | |
1272 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1273 if (outgoing_ack()->missing) { | |
1274 EXPECT_TRUE(outgoing_ack()->packets.Empty()); | |
1275 } else { | |
1276 EXPECT_EQ(1u, outgoing_ack()->packets.NumIntervals()); | |
1277 } | |
1278 } | |
1279 | |
1280 TEST_P(QuicConnectionTest, PacketsOutOfOrder) { | |
1281 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1282 | |
1283 ProcessPacket(kDefaultPathId, 3); | |
1284 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1285 EXPECT_TRUE(IsMissing(2)); | |
1286 EXPECT_TRUE(IsMissing(1)); | |
1287 | |
1288 ProcessPacket(kDefaultPathId, 2); | |
1289 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1290 EXPECT_FALSE(IsMissing(2)); | |
1291 EXPECT_TRUE(IsMissing(1)); | |
1292 | |
1293 ProcessPacket(kDefaultPathId, 1); | |
1294 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1295 EXPECT_FALSE(IsMissing(2)); | |
1296 EXPECT_FALSE(IsMissing(1)); | |
1297 } | |
1298 | |
1299 TEST_P(QuicConnectionTest, DuplicatePacket) { | |
1300 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1301 | |
1302 ProcessPacket(kDefaultPathId, 3); | |
1303 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1304 EXPECT_TRUE(IsMissing(2)); | |
1305 EXPECT_TRUE(IsMissing(1)); | |
1306 | |
1307 // Send packet 3 again, but do not set the expectation that | |
1308 // the visitor OnStreamFrame() will be called. | |
1309 ProcessDataPacket(kDefaultPathId, 3, !kEntropyFlag); | |
1310 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1311 EXPECT_TRUE(IsMissing(2)); | |
1312 EXPECT_TRUE(IsMissing(1)); | |
1313 } | |
1314 | |
1315 TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) { | |
1316 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1317 | |
1318 ProcessPacket(kDefaultPathId, 3); | |
1319 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1320 EXPECT_TRUE(IsMissing(2)); | |
1321 EXPECT_TRUE(IsMissing(1)); | |
1322 | |
1323 ProcessPacket(kDefaultPathId, 2); | |
1324 EXPECT_EQ(3u, outgoing_ack()->largest_observed); | |
1325 EXPECT_TRUE(IsMissing(1)); | |
1326 | |
1327 ProcessPacket(kDefaultPathId, 5); | |
1328 EXPECT_EQ(5u, outgoing_ack()->largest_observed); | |
1329 EXPECT_TRUE(IsMissing(1)); | |
1330 EXPECT_TRUE(IsMissing(4)); | |
1331 | |
1332 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a | |
1333 // packet the peer will not retransmit. It indicates this by sending 'least | |
1334 // awaiting' is 4. The connection should then realize 1 will not be | |
1335 // retransmitted, and will remove it from the missing list. | |
1336 QuicAckFrame frame = InitAckFrame(1); | |
1337 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _)); | |
1338 ProcessAckPacket(6, &frame); | |
1339 | |
1340 // Force an ack to be sent. | |
1341 SendAckPacketToPeer(); | |
1342 EXPECT_TRUE(IsMissing(4)); | |
1343 } | |
1344 | |
1345 TEST_P(QuicConnectionTest, RejectPacketTooFarOut) { | |
1346 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, _, | |
1347 ConnectionCloseSource::FROM_SELF)); | |
1348 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a | |
1349 // packet call to the visitor. | |
1350 ProcessDataPacket(kDefaultPathId, 6000, !kEntropyFlag); | |
1351 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == | |
1352 nullptr); | |
1353 } | |
1354 | |
1355 TEST_P(QuicConnectionTest, RejectUnencryptedStreamData) { | |
1356 // Process an unencrypted packet from the non-crypto stream. | |
1357 frame1_.stream_id = 3; | |
1358 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1359 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA, _, | |
1360 ConnectionCloseSource::FROM_SELF)); | |
1361 EXPECT_DFATAL(ProcessDataPacket(kDefaultPathId, 1, !kEntropyFlag), ""); | |
1362 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) == | |
1363 nullptr); | |
1364 const vector<QuicConnectionCloseFrame>& connection_close_frames = | |
1365 writer_->connection_close_frames(); | |
1366 EXPECT_EQ(1u, connection_close_frames.size()); | |
1367 EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA, | |
1368 connection_close_frames[0].error_code); | |
1369 } | |
1370 | |
1371 TEST_P(QuicConnectionTest, TruncatedAck) { | |
1372 if (GetParam().version > QUIC_VERSION_33) { | |
1373 return; | |
1374 } | |
1375 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1376 QuicPacketNumber num_packets = 256 * 2 + 1; | |
1377 for (QuicPacketNumber i = 0; i < num_packets; ++i) { | |
1378 SendStreamDataToPeer(3, "foo", i * 3, !kFin, nullptr); | |
1379 } | |
1380 | |
1381 QuicAckFrame frame = InitAckFrame(num_packets); | |
1382 // Create an ack with 256 nacks, none adjacent to one another. | |
1383 for (QuicPacketNumber i = 1; i <= 256; ++i) { | |
1384 NackPacket(i * 2, &frame); | |
1385 } | |
1386 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
1387 EXPECT_CALL(peer_entropy_calculator_, EntropyHash(511)) | |
1388 .WillOnce(Return(static_cast<QuicPacketEntropyHash>(0))); | |
1389 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1390 ProcessAckPacket(&frame); | |
1391 | |
1392 // A truncated ack will not have the true largest observed. | |
1393 EXPECT_GT(num_packets, manager_->GetLargestObserved(frame.path_id)); | |
1394 | |
1395 AckPacket(192, &frame); | |
1396 | |
1397 // Removing one missing packet allows us to ack 192 and one more range, but | |
1398 // 192 has already been declared lost, so it doesn't register as an ack. | |
1399 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
1400 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1401 ProcessAckPacket(&frame); | |
1402 EXPECT_EQ(num_packets, manager_->GetLargestObserved(frame.path_id)); | |
1403 } | |
1404 | |
1405 TEST_P(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) { | |
1406 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1407 | |
1408 ProcessPacket(kDefaultPathId, 1); | |
1409 // Delay sending, then queue up an ack. | |
1410 QuicConnectionPeer::SendAck(&connection_); | |
1411 | |
1412 // Process an ack with a least unacked of the received ack. | |
1413 // This causes an ack to be sent when TimeUntilSend returns 0. | |
1414 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
1415 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); | |
1416 // Skip a packet and then record an ack. | |
1417 QuicAckFrame frame = InitAckFrame(0); | |
1418 ProcessAckPacket(3, &frame); | |
1419 } | |
1420 | |
1421 TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) { | |
1422 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1423 | |
1424 ProcessPacket(kDefaultPathId, 3); | |
1425 // Should ack immediately since we have missing packets. | |
1426 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
1427 | |
1428 ProcessPacket(kDefaultPathId, 2); | |
1429 // Should ack immediately since we have missing packets. | |
1430 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
1431 | |
1432 ProcessPacket(kDefaultPathId, 1); | |
1433 // Should ack immediately, since this fills the last hole. | |
1434 EXPECT_EQ(3u, writer_->packets_write_attempts()); | |
1435 | |
1436 ProcessPacket(kDefaultPathId, 4); | |
1437 // Should not cause an ack. | |
1438 EXPECT_EQ(3u, writer_->packets_write_attempts()); | |
1439 } | |
1440 | |
1441 TEST_P(QuicConnectionTest, OutOfOrderAckReceiptCausesNoAck) { | |
1442 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1443 | |
1444 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); | |
1445 SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr); | |
1446 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
1447 | |
1448 QuicAckFrame ack1 = InitAckFrame(1); | |
1449 QuicAckFrame ack2 = InitAckFrame(2); | |
1450 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1451 ProcessAckPacket(2, &ack2); | |
1452 // Should ack immediately since we have missing packets. | |
1453 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
1454 | |
1455 ProcessAckPacket(1, &ack1); | |
1456 // Should not ack an ack filling a missing packet. | |
1457 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
1458 } | |
1459 | |
1460 TEST_P(QuicConnectionTest, AckReceiptCausesAckSend) { | |
1461 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1462 | |
1463 QuicPacketNumber original; | |
1464 QuicByteCount packet_size; | |
1465 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
1466 .WillOnce( | |
1467 DoAll(SaveArg<2>(&original), SaveArg<3>(&packet_size), Return(true))); | |
1468 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
1469 QuicAckFrame frame = InitAckFrame(original); | |
1470 NackPacket(original, &frame); | |
1471 // First nack triggers early retransmit. | |
1472 SendAlgorithmInterface::CongestionVector lost_packets; | |
1473 lost_packets.push_back(std::make_pair(1, kMaxPacketSize)); | |
1474 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
1475 .WillOnce(SetArgPointee<4>(lost_packets)); | |
1476 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1477 QuicPacketNumber retransmission; | |
1478 EXPECT_CALL(*send_algorithm_, | |
1479 OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _)) | |
1480 .WillOnce(DoAll(SaveArg<2>(&retransmission), Return(true))); | |
1481 | |
1482 ProcessAckPacket(&frame); | |
1483 | |
1484 QuicAckFrame frame2 = InitAckFrame(retransmission); | |
1485 NackPacket(original, &frame2); | |
1486 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1487 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
1488 ProcessAckPacket(&frame2); | |
1489 | |
1490 // Now if the peer sends an ack which still reports the retransmitted packet | |
1491 // as missing, that will bundle an ack with data after two acks in a row | |
1492 // indicate the high water mark needs to be raised. | |
1493 EXPECT_CALL(*send_algorithm_, | |
1494 OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); | |
1495 connection_.SendStreamDataWithString(3, "foo", 3, !kFin, nullptr); | |
1496 // No ack sent. | |
1497 EXPECT_EQ(1u, writer_->frame_count()); | |
1498 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1499 | |
1500 // No more packet loss for the rest of the test. | |
1501 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)).Times(AnyNumber()); | |
1502 ProcessAckPacket(&frame2); | |
1503 EXPECT_CALL(*send_algorithm_, | |
1504 OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)); | |
1505 connection_.SendStreamDataWithString(3, "foo", 3, !kFin, nullptr); | |
1506 // Ack bundled. | |
1507 EXPECT_EQ(3u, writer_->frame_count()); | |
1508 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1509 EXPECT_FALSE(writer_->ack_frames().empty()); | |
1510 | |
1511 // But an ack with no missing packets will not send an ack. | |
1512 AckPacket(original, &frame2); | |
1513 ProcessAckPacket(&frame2); | |
1514 ProcessAckPacket(&frame2); | |
1515 } | |
1516 | |
1517 TEST_P(QuicConnectionTest, 20AcksCausesAckSend) { | |
1518 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1519 | |
1520 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); | |
1521 | |
1522 QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_); | |
1523 // But an ack with no missing packets will not send an ack. | |
1524 QuicAckFrame frame = InitAckFrame(1); | |
1525 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1526 for (int i = 0; i < 19; ++i) { | |
1527 ProcessAckPacket(&frame); | |
1528 EXPECT_FALSE(ack_alarm->IsSet()); | |
1529 } | |
1530 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
1531 // The 20th ack packet will cause an ack to be sent. | |
1532 ProcessAckPacket(&frame); | |
1533 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
1534 } | |
1535 | |
1536 TEST_P(QuicConnectionTest, LeastUnackedLower) { | |
1537 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1538 | |
1539 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); | |
1540 SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr); | |
1541 SendStreamDataToPeer(1, "eep", 6, !kFin, nullptr); | |
1542 | |
1543 // Start out saying the least unacked is 2. | |
1544 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); | |
1545 QuicStopWaitingFrame frame = InitStopWaitingFrame(2); | |
1546 ProcessStopWaitingPacket(&frame); | |
1547 | |
1548 // Change it to 1, but lower the packet number to fake out-of-order packets. | |
1549 // This should be fine. | |
1550 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 1); | |
1551 // The scheduler will not process out of order acks, but all packet processing | |
1552 // causes the connection to try to write. | |
1553 EXPECT_CALL(visitor_, OnCanWrite()); | |
1554 QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1); | |
1555 ProcessStopWaitingPacket(&frame2); | |
1556 | |
1557 // Now claim it's one, but set the ordering so it was sent "after" the first | |
1558 // one. This should cause a connection error. | |
1559 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
1560 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 7); | |
1561 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA, _, | |
1562 ConnectionCloseSource::FROM_SELF)); | |
1563 QuicStopWaitingFrame frame3 = InitStopWaitingFrame(1); | |
1564 ProcessStopWaitingPacket(&frame3); | |
1565 } | |
1566 | |
1567 TEST_P(QuicConnectionTest, TooManySentPackets) { | |
1568 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1569 | |
1570 const int num_packets = kMaxTrackedPackets + 100; | |
1571 for (int i = 0; i < num_packets; ++i) { | |
1572 SendStreamDataToPeer(1, "foo", 3 * i, !kFin, nullptr); | |
1573 } | |
1574 | |
1575 // Ack packet 1, which leaves more than the limit outstanding. | |
1576 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1577 if (GetParam().version <= QUIC_VERSION_33) { | |
1578 EXPECT_CALL(visitor_, | |
1579 OnConnectionClosed(QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS, _, | |
1580 ConnectionCloseSource::FROM_SELF)); | |
1581 // We're receive buffer limited, so the connection won't try to write more. | |
1582 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); | |
1583 } | |
1584 | |
1585 // Nack the first packet and ack the rest, leaving a huge gap. | |
1586 QuicAckFrame frame1 = InitAckFrame(num_packets); | |
1587 NackPacket(1, &frame1); | |
1588 ProcessAckPacket(&frame1); | |
1589 } | |
1590 | |
1591 TEST_P(QuicConnectionTest, TooManyReceivedPackets) { | |
1592 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1593 if (GetParam().version <= QUIC_VERSION_33) { | |
1594 EXPECT_CALL(visitor_, | |
1595 OnConnectionClosed(QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS, | |
1596 _, ConnectionCloseSource::FROM_SELF)); | |
1597 } | |
1598 // Miss 99 of every 100 packets for 5500 packets. | |
1599 for (QuicPacketNumber i = 1; i < kMaxTrackedPackets + 500; i += 100) { | |
1600 ProcessPacket(kDefaultPathId, i); | |
1601 if (!connection_.connected()) { | |
1602 break; | |
1603 } | |
1604 } | |
1605 } | |
1606 | |
1607 TEST_P(QuicConnectionTest, LargestObservedLower) { | |
1608 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1609 | |
1610 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); | |
1611 SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr); | |
1612 SendStreamDataToPeer(1, "eep", 6, !kFin, nullptr); | |
1613 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1614 | |
1615 // Start out saying the largest observed is 2. | |
1616 QuicAckFrame frame1 = InitAckFrame(1); | |
1617 QuicAckFrame frame2 = InitAckFrame(2); | |
1618 ProcessAckPacket(&frame2); | |
1619 | |
1620 // Now change it to 1, and it should cause a connection error. | |
1621 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, _, | |
1622 ConnectionCloseSource::FROM_SELF)); | |
1623 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); | |
1624 ProcessAckPacket(&frame1); | |
1625 } | |
1626 | |
1627 TEST_P(QuicConnectionTest, AckUnsentData) { | |
1628 // Ack a packet which has not been sent. | |
1629 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, _, | |
1630 ConnectionCloseSource::FROM_SELF)); | |
1631 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1632 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
1633 QuicAckFrame frame(MakeAckFrame(1)); | |
1634 EXPECT_CALL(visitor_, OnCanWrite()).Times(0); | |
1635 ProcessAckPacket(&frame); | |
1636 } | |
1637 | |
1638 TEST_P(QuicConnectionTest, AckAll) { | |
1639 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1640 ProcessPacket(kDefaultPathId, 1); | |
1641 | |
1642 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 1); | |
1643 QuicAckFrame frame1 = InitAckFrame(0); | |
1644 ProcessAckPacket(&frame1); | |
1645 } | |
1646 | |
1647 TEST_P(QuicConnectionTest, BasicSending) { | |
1648 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1649 QuicPacketNumber last_packet; | |
1650 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 | |
1651 EXPECT_EQ(1u, last_packet); | |
1652 SendAckPacketToPeer(); // Packet 2 | |
1653 | |
1654 EXPECT_EQ(1u, least_unacked()); | |
1655 | |
1656 SendAckPacketToPeer(); // Packet 3 | |
1657 EXPECT_EQ(1u, least_unacked()); | |
1658 | |
1659 SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet); // Packet 4 | |
1660 EXPECT_EQ(4u, last_packet); | |
1661 SendAckPacketToPeer(); // Packet 5 | |
1662 EXPECT_EQ(1u, least_unacked()); | |
1663 | |
1664 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1665 | |
1666 // Peer acks up to packet 3. | |
1667 QuicAckFrame frame = InitAckFrame(3); | |
1668 ProcessAckPacket(&frame); | |
1669 SendAckPacketToPeer(); // Packet 6 | |
1670 | |
1671 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of | |
1672 // ack for 4. | |
1673 EXPECT_EQ(4u, least_unacked()); | |
1674 | |
1675 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1676 | |
1677 // Peer acks up to packet 4, the last packet. | |
1678 QuicAckFrame frame2 = InitAckFrame(6); | |
1679 ProcessAckPacket(&frame2); // Acks don't instigate acks. | |
1680 | |
1681 // Verify that we did not send an ack. | |
1682 EXPECT_EQ(6u, writer_->header().packet_number); | |
1683 | |
1684 // So the last ack has not changed. | |
1685 EXPECT_EQ(4u, least_unacked()); | |
1686 | |
1687 // If we force an ack, we shouldn't change our retransmit state. | |
1688 SendAckPacketToPeer(); // Packet 7 | |
1689 EXPECT_EQ(7u, least_unacked()); | |
1690 | |
1691 // But if we send more data it should. | |
1692 SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8 | |
1693 EXPECT_EQ(8u, last_packet); | |
1694 SendAckPacketToPeer(); // Packet 9 | |
1695 EXPECT_EQ(7u, least_unacked()); | |
1696 } | |
1697 | |
1698 // QuicConnection should record the the packet sent-time prior to sending the | |
1699 // packet. | |
1700 TEST_P(QuicConnectionTest, RecordSentTimeBeforePacketSent) { | |
1701 // We're using a MockClock for the tests, so we have complete control over the | |
1702 // time. | |
1703 // Our recorded timestamp for the last packet sent time will be passed in to | |
1704 // the send_algorithm. Make sure that it is set to the correct value. | |
1705 QuicTime actual_recorded_send_time = QuicTime::Zero(); | |
1706 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
1707 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); | |
1708 | |
1709 // First send without any pause and check the result. | |
1710 QuicTime expected_recorded_send_time = clock_.Now(); | |
1711 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
1712 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) | |
1713 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() | |
1714 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); | |
1715 | |
1716 // Now pause during the write, and check the results. | |
1717 actual_recorded_send_time = QuicTime::Zero(); | |
1718 const QuicTime::Delta write_pause_time_delta = | |
1719 QuicTime::Delta::FromMilliseconds(5000); | |
1720 SetWritePauseTimeDelta(write_pause_time_delta); | |
1721 expected_recorded_send_time = clock_.Now(); | |
1722 | |
1723 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
1724 .WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true))); | |
1725 connection_.SendStreamDataWithString(2, "baz", 0, !kFin, nullptr); | |
1726 EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time) | |
1727 << "Expected time = " << expected_recorded_send_time.ToDebuggingValue() | |
1728 << ". Actual time = " << actual_recorded_send_time.ToDebuggingValue(); | |
1729 } | |
1730 | |
1731 TEST_P(QuicConnectionTest, FramePacking) { | |
1732 // Send an ack and two stream frames in 1 packet by queueing them. | |
1733 { | |
1734 QuicConnection::ScopedPacketBundler bundler(&connection_, | |
1735 QuicConnection::SEND_ACK); | |
1736 connection_.SendStreamData3(); | |
1737 connection_.SendStreamData5(); | |
1738 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
1739 } | |
1740 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1741 EXPECT_FALSE(connection_.HasQueuedData()); | |
1742 | |
1743 // Parse the last packet and ensure it's an ack and two stream frames from | |
1744 // two different streams. | |
1745 EXPECT_EQ(4u, writer_->frame_count()); | |
1746 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
1747 EXPECT_FALSE(writer_->ack_frames().empty()); | |
1748 ASSERT_EQ(2u, writer_->stream_frames().size()); | |
1749 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); | |
1750 EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1]->stream_id); | |
1751 } | |
1752 | |
1753 TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) { | |
1754 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2 | |
1755 // packets by queueing them. | |
1756 { | |
1757 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
1758 QuicConnection::ScopedPacketBundler bundler(&connection_, | |
1759 QuicConnection::SEND_ACK); | |
1760 connection_.SendStreamData3(); | |
1761 connection_.SendCryptoStreamData(); | |
1762 } | |
1763 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1764 EXPECT_FALSE(connection_.HasQueuedData()); | |
1765 | |
1766 // Parse the last packet and ensure it's the crypto stream frame. | |
1767 EXPECT_EQ(1u, writer_->frame_count()); | |
1768 ASSERT_EQ(1u, writer_->stream_frames().size()); | |
1769 EXPECT_EQ(kCryptoStreamId, writer_->stream_frames()[0]->stream_id); | |
1770 } | |
1771 | |
1772 TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) { | |
1773 // Send an ack and two stream frames (one crypto, then one non-crypto) in 2 | |
1774 // packets by queueing them. | |
1775 { | |
1776 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
1777 QuicConnection::ScopedPacketBundler bundler(&connection_, | |
1778 QuicConnection::SEND_ACK); | |
1779 connection_.SendCryptoStreamData(); | |
1780 connection_.SendStreamData3(); | |
1781 } | |
1782 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1783 EXPECT_FALSE(connection_.HasQueuedData()); | |
1784 | |
1785 // Parse the last packet and ensure it's the stream frame from stream 3. | |
1786 EXPECT_EQ(1u, writer_->frame_count()); | |
1787 ASSERT_EQ(1u, writer_->stream_frames().size()); | |
1788 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); | |
1789 } | |
1790 | |
1791 TEST_P(QuicConnectionTest, FramePackingAckResponse) { | |
1792 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1793 // Process a data packet to queue up a pending ack. | |
1794 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
1795 ProcessDataPacket(kDefaultPathId, 1, kEntropyFlag); | |
1796 | |
1797 EXPECT_CALL(visitor_, OnCanWrite()) | |
1798 .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( | |
1799 &connection_, &TestConnection::SendStreamData3)), | |
1800 IgnoreResult(InvokeWithoutArgs( | |
1801 &connection_, &TestConnection::SendStreamData5)))); | |
1802 | |
1803 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
1804 | |
1805 // Process an ack to cause the visitor's OnCanWrite to be invoked. | |
1806 QuicAckFrame ack_one = InitAckFrame(0); | |
1807 ProcessAckPacket(3, &ack_one); | |
1808 | |
1809 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1810 EXPECT_FALSE(connection_.HasQueuedData()); | |
1811 | |
1812 // Parse the last packet and ensure it's an ack and two stream frames from | |
1813 // two different streams. | |
1814 EXPECT_EQ(4u, writer_->frame_count()); | |
1815 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
1816 EXPECT_FALSE(writer_->ack_frames().empty()); | |
1817 ASSERT_EQ(2u, writer_->stream_frames().size()); | |
1818 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); | |
1819 EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1]->stream_id); | |
1820 } | |
1821 | |
1822 TEST_P(QuicConnectionTest, FramePackingSendv) { | |
1823 // Send data in 1 packet by writing multiple blocks in a single iovector | |
1824 // using writev. | |
1825 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
1826 | |
1827 char data[] = "ABCD"; | |
1828 struct iovec iov[2]; | |
1829 iov[0].iov_base = data; | |
1830 iov[0].iov_len = 2; | |
1831 iov[1].iov_base = data + 2; | |
1832 iov[1].iov_len = 2; | |
1833 connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, !kFin, nullptr); | |
1834 | |
1835 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1836 EXPECT_FALSE(connection_.HasQueuedData()); | |
1837 | |
1838 // Parse the last packet and ensure multiple iovector blocks have | |
1839 // been packed into a single stream frame from one stream. | |
1840 EXPECT_EQ(1u, writer_->frame_count()); | |
1841 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1842 QuicStreamFrame* frame = writer_->stream_frames()[0]; | |
1843 EXPECT_EQ(1u, frame->stream_id); | |
1844 EXPECT_EQ("ABCD", StringPiece(frame->data_buffer, frame->data_length)); | |
1845 } | |
1846 | |
1847 TEST_P(QuicConnectionTest, FramePackingSendvQueued) { | |
1848 // Try to send two stream frames in 1 packet by using writev. | |
1849 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
1850 | |
1851 BlockOnNextWrite(); | |
1852 char data[] = "ABCD"; | |
1853 struct iovec iov[2]; | |
1854 iov[0].iov_base = data; | |
1855 iov[0].iov_len = 2; | |
1856 iov[1].iov_base = data + 2; | |
1857 iov[1].iov_len = 2; | |
1858 connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, !kFin, nullptr); | |
1859 | |
1860 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
1861 EXPECT_TRUE(connection_.HasQueuedData()); | |
1862 | |
1863 // Unblock the writes and actually send. | |
1864 writer_->SetWritable(); | |
1865 connection_.OnCanWrite(); | |
1866 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1867 | |
1868 // Parse the last packet and ensure it's one stream frame from one stream. | |
1869 EXPECT_EQ(1u, writer_->frame_count()); | |
1870 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1871 EXPECT_EQ(1u, writer_->stream_frames()[0]->stream_id); | |
1872 } | |
1873 | |
1874 TEST_P(QuicConnectionTest, SendingZeroBytes) { | |
1875 // Send a zero byte write with a fin using writev. | |
1876 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
1877 QuicIOVector empty_iov(nullptr, 0, 0); | |
1878 connection_.SendStreamData(kHeadersStreamId, empty_iov, 0, kFin, nullptr); | |
1879 | |
1880 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1881 EXPECT_FALSE(connection_.HasQueuedData()); | |
1882 | |
1883 // Parse the last packet and ensure it's one stream frame from one stream. | |
1884 EXPECT_EQ(1u, writer_->frame_count()); | |
1885 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1886 EXPECT_EQ(kHeadersStreamId, writer_->stream_frames()[0]->stream_id); | |
1887 EXPECT_TRUE(writer_->stream_frames()[0]->fin); | |
1888 } | |
1889 | |
1890 TEST_P(QuicConnectionTest, LargeSendWithPendingAck) { | |
1891 // Set the ack alarm by processing a ping frame. | |
1892 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1893 | |
1894 // Processs a PING frame. | |
1895 ProcessFramePacket(QuicFrame(QuicPingFrame())); | |
1896 // Ensure that this has caused the ACK alarm to be set. | |
1897 QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_); | |
1898 EXPECT_TRUE(ack_alarm->IsSet()); | |
1899 | |
1900 // Send data and ensure the ack is bundled. | |
1901 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(8); | |
1902 size_t len = 10000; | |
1903 std::unique_ptr<char[]> data_array(new char[len]); | |
1904 memset(data_array.get(), '?', len); | |
1905 struct iovec iov; | |
1906 iov.iov_base = data_array.get(); | |
1907 iov.iov_len = len; | |
1908 QuicIOVector iovector(&iov, 1, len); | |
1909 QuicConsumedData consumed = | |
1910 connection_.SendStreamData(kHeadersStreamId, iovector, 0, true, nullptr); | |
1911 EXPECT_EQ(len, consumed.bytes_consumed); | |
1912 EXPECT_TRUE(consumed.fin_consumed); | |
1913 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
1914 EXPECT_FALSE(connection_.HasQueuedData()); | |
1915 | |
1916 // Parse the last packet and ensure it's one stream frame with a fin. | |
1917 EXPECT_EQ(1u, writer_->frame_count()); | |
1918 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
1919 EXPECT_EQ(kHeadersStreamId, writer_->stream_frames()[0]->stream_id); | |
1920 EXPECT_TRUE(writer_->stream_frames()[0]->fin); | |
1921 // Ensure the ack alarm was cancelled when the ack was sent. | |
1922 EXPECT_FALSE(ack_alarm->IsSet()); | |
1923 } | |
1924 | |
1925 TEST_P(QuicConnectionTest, OnCanWrite) { | |
1926 // Visitor's OnCanWrite will send data, but will have more pending writes. | |
1927 EXPECT_CALL(visitor_, OnCanWrite()) | |
1928 .WillOnce(DoAll(IgnoreResult(InvokeWithoutArgs( | |
1929 &connection_, &TestConnection::SendStreamData3)), | |
1930 IgnoreResult(InvokeWithoutArgs( | |
1931 &connection_, &TestConnection::SendStreamData5)))); | |
1932 EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillOnce(Return(true)); | |
1933 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
1934 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); | |
1935 | |
1936 connection_.OnCanWrite(); | |
1937 | |
1938 // Parse the last packet and ensure it's the two stream frames from | |
1939 // two different streams. | |
1940 EXPECT_EQ(2u, writer_->frame_count()); | |
1941 EXPECT_EQ(2u, writer_->stream_frames().size()); | |
1942 EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0]->stream_id); | |
1943 EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1]->stream_id); | |
1944 } | |
1945 | |
1946 TEST_P(QuicConnectionTest, RetransmitOnNack) { | |
1947 QuicPacketNumber last_packet; | |
1948 QuicByteCount second_packet_size; | |
1949 SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet); // Packet 1 | |
1950 second_packet_size = | |
1951 SendStreamDataToPeer(3, "foos", 3, !kFin, &last_packet); // Packet 2 | |
1952 SendStreamDataToPeer(3, "fooos", 7, !kFin, &last_packet); // Packet 3 | |
1953 | |
1954 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
1955 | |
1956 // Don't lose a packet on an ack, and nothing is retransmitted. | |
1957 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1958 QuicAckFrame ack_one = InitAckFrame(1); | |
1959 ProcessAckPacket(&ack_one); | |
1960 | |
1961 // Lose a packet and ensure it triggers retransmission. | |
1962 QuicAckFrame nack_two = InitAckFrame(3); | |
1963 NackPacket(2, &nack_two); | |
1964 SendAlgorithmInterface::CongestionVector lost_packets; | |
1965 lost_packets.push_back(std::make_pair(2, kMaxPacketSize)); | |
1966 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
1967 .WillOnce(SetArgPointee<4>(lost_packets)); | |
1968 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
1969 EXPECT_CALL(*send_algorithm_, | |
1970 OnPacketSent(_, _, _, second_packet_size - kQuicVersionSize, _)) | |
1971 .Times(1); | |
1972 ProcessAckPacket(&nack_two); | |
1973 } | |
1974 | |
1975 TEST_P(QuicConnectionTest, DoNotSendQueuedPacketForResetStream) { | |
1976 // Block the connection to queue the packet. | |
1977 BlockOnNextWrite(); | |
1978 | |
1979 QuicStreamId stream_id = 2; | |
1980 connection_.SendStreamDataWithString(stream_id, "foo", 0, !kFin, nullptr); | |
1981 | |
1982 // Now that there is a queued packet, reset the stream. | |
1983 connection_.SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 14); | |
1984 | |
1985 // Unblock the connection and verify that only the RST_STREAM is sent. | |
1986 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
1987 writer_->SetWritable(); | |
1988 connection_.OnCanWrite(); | |
1989 EXPECT_EQ(1u, writer_->frame_count()); | |
1990 EXPECT_EQ(1u, writer_->rst_stream_frames().size()); | |
1991 } | |
1992 | |
1993 TEST_P(QuicConnectionTest, SendQueuedPacketForQuicRstStreamNoError) { | |
1994 // Block the connection to queue the packet. | |
1995 BlockOnNextWrite(); | |
1996 | |
1997 QuicStreamId stream_id = 2; | |
1998 connection_.SendStreamDataWithString(stream_id, "foo", 0, !kFin, nullptr); | |
1999 | |
2000 // Now that there is a queued packet, reset the stream. | |
2001 connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14); | |
2002 | |
2003 // Unblock the connection and verify that the RST_STREAM is sent and the data | |
2004 // packet is sent. | |
2005 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(2)); | |
2006 writer_->SetWritable(); | |
2007 connection_.OnCanWrite(); | |
2008 EXPECT_EQ(1u, writer_->frame_count()); | |
2009 EXPECT_EQ(1u, writer_->rst_stream_frames().size()); | |
2010 } | |
2011 | |
2012 TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnNack) { | |
2013 QuicStreamId stream_id = 2; | |
2014 QuicPacketNumber last_packet; | |
2015 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2016 SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet); | |
2017 SendStreamDataToPeer(stream_id, "fooos", 7, !kFin, &last_packet); | |
2018 | |
2019 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2020 connection_.SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 14); | |
2021 | |
2022 // Lose a packet and ensure it does not trigger retransmission. | |
2023 QuicAckFrame nack_two = InitAckFrame(last_packet); | |
2024 NackPacket(last_packet - 1, &nack_two); | |
2025 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2026 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
2027 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2028 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
2029 ProcessAckPacket(&nack_two); | |
2030 } | |
2031 | |
2032 TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnNack) { | |
2033 QuicStreamId stream_id = 2; | |
2034 QuicPacketNumber last_packet; | |
2035 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2036 SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet); | |
2037 SendStreamDataToPeer(stream_id, "fooos", 7, !kFin, &last_packet); | |
2038 | |
2039 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2040 connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14); | |
2041 | |
2042 // Lose a packet, ensure it triggers retransmission. | |
2043 QuicAckFrame nack_two = InitAckFrame(last_packet); | |
2044 NackPacket(last_packet - 1, &nack_two); | |
2045 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2046 SendAlgorithmInterface::CongestionVector lost_packets; | |
2047 lost_packets.push_back(std::make_pair(last_packet - 1, kMaxPacketSize)); | |
2048 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
2049 .WillOnce(SetArgPointee<4>(lost_packets)); | |
2050 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2051 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(1)); | |
2052 ProcessAckPacket(&nack_two); | |
2053 } | |
2054 | |
2055 TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnRTO) { | |
2056 QuicStreamId stream_id = 2; | |
2057 QuicPacketNumber last_packet; | |
2058 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2059 | |
2060 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2061 connection_.SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 14); | |
2062 | |
2063 // Fire the RTO and verify that the RST_STREAM is resent, not stream data. | |
2064 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2065 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2066 connection_.GetRetransmissionAlarm()->Fire(); | |
2067 EXPECT_EQ(1u, writer_->frame_count()); | |
2068 EXPECT_EQ(1u, writer_->rst_stream_frames().size()); | |
2069 EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); | |
2070 } | |
2071 | |
2072 TEST_P(QuicConnectionTest, RetransmitForQuicRstStreamNoErrorOnRTO) { | |
2073 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2074 | |
2075 QuicStreamId stream_id = 2; | |
2076 QuicPacketNumber last_packet; | |
2077 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2078 | |
2079 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2080 connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14); | |
2081 | |
2082 // Fire the RTO and verify that the RST_STREAM is resent, the stream data | |
2083 // is sent. | |
2084 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(2)); | |
2085 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2086 connection_.GetRetransmissionAlarm()->Fire(); | |
2087 EXPECT_EQ(1u, writer_->frame_count()); | |
2088 ASSERT_EQ(1u, writer_->rst_stream_frames().size()); | |
2089 EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); | |
2090 } | |
2091 | |
2092 TEST_P(QuicConnectionTest, DoNotSendPendingRetransmissionForResetStream) { | |
2093 QuicStreamId stream_id = 2; | |
2094 QuicPacketNumber last_packet; | |
2095 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2096 SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet); | |
2097 BlockOnNextWrite(); | |
2098 connection_.SendStreamDataWithString(stream_id, "fooos", 7, !kFin, nullptr); | |
2099 | |
2100 // Lose a packet which will trigger a pending retransmission. | |
2101 QuicAckFrame ack = InitAckFrame(last_packet); | |
2102 NackPacket(last_packet - 1, &ack); | |
2103 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2104 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
2105 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2106 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
2107 ProcessAckPacket(&ack); | |
2108 | |
2109 connection_.SendRstStream(stream_id, QUIC_ERROR_PROCESSING_STREAM, 14); | |
2110 | |
2111 // Unblock the connection and verify that the RST_STREAM is sent but not the | |
2112 // second data packet nor a retransmit. | |
2113 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2114 writer_->SetWritable(); | |
2115 connection_.OnCanWrite(); | |
2116 EXPECT_EQ(1u, writer_->frame_count()); | |
2117 EXPECT_EQ(1u, writer_->rst_stream_frames().size()); | |
2118 EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id); | |
2119 } | |
2120 | |
2121 TEST_P(QuicConnectionTest, SendPendingRetransmissionForQuicRstStreamNoError) { | |
2122 QuicStreamId stream_id = 2; | |
2123 QuicPacketNumber last_packet; | |
2124 SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet); | |
2125 SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet); | |
2126 BlockOnNextWrite(); | |
2127 connection_.SendStreamDataWithString(stream_id, "fooos", 7, !kFin, nullptr); | |
2128 | |
2129 // Lose a packet which will trigger a pending retransmission. | |
2130 QuicAckFrame ack = InitAckFrame(last_packet); | |
2131 NackPacket(last_packet - 1, &ack); | |
2132 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2133 SendAlgorithmInterface::CongestionVector lost_packets; | |
2134 lost_packets.push_back(std::make_pair(last_packet - 1, kMaxPacketSize)); | |
2135 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
2136 .WillOnce(SetArgPointee<4>(lost_packets)); | |
2137 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2138 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
2139 ProcessAckPacket(&ack); | |
2140 | |
2141 connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14); | |
2142 | |
2143 // Unblock the connection and verify that the RST_STREAM is sent and the | |
2144 // second data packet or a retransmit is sent. | |
2145 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AtLeast(2)); | |
2146 writer_->SetWritable(); | |
2147 connection_.OnCanWrite(); | |
2148 EXPECT_EQ(1u, writer_->frame_count()); | |
2149 EXPECT_EQ(0u, writer_->rst_stream_frames().size()); | |
2150 } | |
2151 | |
2152 TEST_P(QuicConnectionTest, RetransmitAckedPacket) { | |
2153 QuicPacketNumber last_packet; | |
2154 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 | |
2155 SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet); // Packet 2 | |
2156 SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet); // Packet 3 | |
2157 | |
2158 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2159 | |
2160 // Instigate a loss with an ack. | |
2161 QuicAckFrame nack_two = InitAckFrame(3); | |
2162 NackPacket(2, &nack_two); | |
2163 // The first nack should trigger a fast retransmission, but we'll be | |
2164 // write blocked, so the packet will be queued. | |
2165 BlockOnNextWrite(); | |
2166 | |
2167 SendAlgorithmInterface::CongestionVector lost_packets; | |
2168 lost_packets.push_back(std::make_pair(2, kMaxPacketSize)); | |
2169 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
2170 .WillOnce(SetArgPointee<4>(lost_packets)); | |
2171 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2172 ProcessAckPacket(&nack_two); | |
2173 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
2174 | |
2175 // Now, ack the previous transmission. | |
2176 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
2177 QuicAckFrame ack_all = InitAckFrame(3); | |
2178 ProcessAckPacket(&ack_all); | |
2179 | |
2180 // Unblock the socket and attempt to send the queued packets. We will always | |
2181 // send the retransmission. | |
2182 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 4, _, _)).Times(1); | |
2183 | |
2184 writer_->SetWritable(); | |
2185 connection_.OnCanWrite(); | |
2186 | |
2187 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
2188 // We do not store retransmittable frames of this retransmission. | |
2189 EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, | |
2190 kDefaultPathId, 4)); | |
2191 } | |
2192 | |
2193 TEST_P(QuicConnectionTest, RetransmitNackedLargestObserved) { | |
2194 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2195 QuicPacketNumber largest_observed; | |
2196 QuicByteCount packet_size; | |
2197 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2198 .WillOnce(DoAll(SaveArg<2>(&largest_observed), SaveArg<3>(&packet_size), | |
2199 Return(true))); | |
2200 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
2201 | |
2202 QuicAckFrame frame = InitAckFrame(1); | |
2203 NackPacket(largest_observed, &frame); | |
2204 // The first nack should retransmit the largest observed packet. | |
2205 SendAlgorithmInterface::CongestionVector lost_packets; | |
2206 lost_packets.push_back(std::make_pair(1, kMaxPacketSize)); | |
2207 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
2208 .WillOnce(SetArgPointee<4>(lost_packets)); | |
2209 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2210 EXPECT_CALL(*send_algorithm_, | |
2211 OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _)); | |
2212 ProcessAckPacket(&frame); | |
2213 } | |
2214 | |
2215 TEST_P(QuicConnectionTest, QueueAfterTwoRTOs) { | |
2216 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2217 | |
2218 for (int i = 0; i < 10; ++i) { | |
2219 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2220 connection_.SendStreamDataWithString(3, "foo", i * 3, !kFin, nullptr); | |
2221 } | |
2222 | |
2223 // Block the writer and ensure they're queued. | |
2224 BlockOnNextWrite(); | |
2225 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2226 // Only one packet should be retransmitted. | |
2227 connection_.GetRetransmissionAlarm()->Fire(); | |
2228 EXPECT_TRUE(connection_.HasQueuedData()); | |
2229 | |
2230 // Unblock the writer. | |
2231 writer_->SetWritable(); | |
2232 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds( | |
2233 2 * DefaultRetransmissionTime().ToMicroseconds())); | |
2234 // Retransmit already retransmitted packets event though the packet number | |
2235 // greater than the largest observed. | |
2236 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
2237 connection_.GetRetransmissionAlarm()->Fire(); | |
2238 connection_.OnCanWrite(); | |
2239 } | |
2240 | |
2241 TEST_P(QuicConnectionTest, WriteBlockedBufferedThenSent) { | |
2242 BlockOnNextWrite(); | |
2243 writer_->set_is_write_blocked_data_buffered(true); | |
2244 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2245 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2246 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2247 | |
2248 writer_->SetWritable(); | |
2249 connection_.OnCanWrite(); | |
2250 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2251 } | |
2252 | |
2253 TEST_P(QuicConnectionTest, WriteBlockedThenSent) { | |
2254 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
2255 BlockOnNextWrite(); | |
2256 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2257 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2258 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
2259 | |
2260 // The second packet should also be queued, in order to ensure packets are | |
2261 // never sent out of order. | |
2262 writer_->SetWritable(); | |
2263 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2264 EXPECT_EQ(2u, connection_.NumQueuedPackets()); | |
2265 | |
2266 // Now both are sent in order when we unblock. | |
2267 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
2268 connection_.OnCanWrite(); | |
2269 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2270 } | |
2271 | |
2272 TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) { | |
2273 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2274 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
2275 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2276 | |
2277 BlockOnNextWrite(); | |
2278 writer_->set_is_write_blocked_data_buffered(true); | |
2279 // Simulate the retransmission alarm firing. | |
2280 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2281 connection_.GetRetransmissionAlarm()->Fire(); | |
2282 | |
2283 // Ack the sent packet before the callback returns, which happens in | |
2284 // rare circumstances with write blocked sockets. | |
2285 QuicAckFrame ack = InitAckFrame(1); | |
2286 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2287 ProcessAckPacket(&ack); | |
2288 | |
2289 writer_->SetWritable(); | |
2290 connection_.OnCanWrite(); | |
2291 // There is now a pending packet, but with no retransmittable frames. | |
2292 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2293 EXPECT_FALSE(QuicConnectionPeer::HasRetransmittableFrames(&connection_, | |
2294 ack.path_id, 2)); | |
2295 } | |
2296 | |
2297 TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) { | |
2298 // Block the connection. | |
2299 BlockOnNextWrite(); | |
2300 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
2301 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
2302 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
2303 | |
2304 // Set the send and resumption alarms. Fire the alarms and ensure they don't | |
2305 // attempt to write. | |
2306 connection_.GetResumeWritesAlarm()->Set(clock_.ApproximateNow()); | |
2307 connection_.GetSendAlarm()->Set(clock_.ApproximateNow()); | |
2308 connection_.GetResumeWritesAlarm()->Fire(); | |
2309 connection_.GetSendAlarm()->Fire(); | |
2310 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
2311 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
2312 } | |
2313 | |
2314 TEST_P(QuicConnectionTest, NoLimitPacketsPerNack) { | |
2315 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2316 int offset = 0; | |
2317 // Send packets 1 to 15. | |
2318 for (int i = 0; i < 15; ++i) { | |
2319 SendStreamDataToPeer(1, "foo", offset, !kFin, nullptr); | |
2320 offset += 3; | |
2321 } | |
2322 | |
2323 // Ack 15, nack 1-14. | |
2324 | |
2325 QuicAckFrame nack = InitAckFrame(15); | |
2326 for (int i = 1; i < 15; ++i) { | |
2327 NackPacket(i, &nack); | |
2328 } | |
2329 | |
2330 // 14 packets have been NACK'd and lost. | |
2331 SendAlgorithmInterface::CongestionVector lost_packets; | |
2332 for (int i = 1; i < 15; ++i) { | |
2333 lost_packets.push_back(std::make_pair(i, kMaxPacketSize)); | |
2334 } | |
2335 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
2336 .WillOnce(SetArgPointee<4>(lost_packets)); | |
2337 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2338 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(14); | |
2339 ProcessAckPacket(&nack); | |
2340 } | |
2341 | |
2342 // Test sending multiple acks from the connection to the session. | |
2343 TEST_P(QuicConnectionTest, MultipleAcks) { | |
2344 QuicPacketNumber last_packet; | |
2345 SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1 | |
2346 EXPECT_EQ(1u, last_packet); | |
2347 SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet); // Packet 2 | |
2348 EXPECT_EQ(2u, last_packet); | |
2349 SendAckPacketToPeer(); // Packet 3 | |
2350 SendStreamDataToPeer(5, "foo", 0, !kFin, &last_packet); // Packet 4 | |
2351 EXPECT_EQ(4u, last_packet); | |
2352 SendStreamDataToPeer(1, "foo", 3, !kFin, &last_packet); // Packet 5 | |
2353 EXPECT_EQ(5u, last_packet); | |
2354 SendStreamDataToPeer(3, "foo", 3, !kFin, &last_packet); // Packet 6 | |
2355 EXPECT_EQ(6u, last_packet); | |
2356 | |
2357 // Client will ack packets 1, 2, [!3], 4, 5. | |
2358 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2359 QuicAckFrame frame1 = InitAckFrame(5); | |
2360 NackPacket(3, &frame1); | |
2361 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2362 ProcessAckPacket(&frame1); | |
2363 | |
2364 // Now the client implicitly acks 3, and explicitly acks 6. | |
2365 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2366 QuicAckFrame frame2 = InitAckFrame(6); | |
2367 ProcessAckPacket(&frame2); | |
2368 } | |
2369 | |
2370 TEST_P(QuicConnectionTest, DontLatchUnackedPacket) { | |
2371 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); // Packet 1; | |
2372 // From now on, we send acks, so the send algorithm won't mark them pending. | |
2373 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2374 .WillByDefault(Return(false)); | |
2375 SendAckPacketToPeer(); // Packet 2 | |
2376 | |
2377 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2378 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2379 QuicAckFrame frame = InitAckFrame(1); | |
2380 ProcessAckPacket(&frame); | |
2381 | |
2382 // Verify that our internal state has least-unacked as 2, because we're still | |
2383 // waiting for a potential ack for 2. | |
2384 | |
2385 EXPECT_EQ(2u, stop_waiting()->least_unacked); | |
2386 | |
2387 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2388 frame = InitAckFrame(2); | |
2389 ProcessAckPacket(&frame); | |
2390 EXPECT_EQ(3u, stop_waiting()->least_unacked); | |
2391 | |
2392 // When we send an ack, we make sure our least-unacked makes sense. In this | |
2393 // case since we're not waiting on an ack for 2 and all packets are acked, we | |
2394 // set it to 3. | |
2395 SendAckPacketToPeer(); // Packet 3 | |
2396 // Least_unacked remains at 3 until another ack is received. | |
2397 EXPECT_EQ(3u, stop_waiting()->least_unacked); | |
2398 // Check that the outgoing ack had its packet number as least_unacked. | |
2399 EXPECT_EQ(3u, least_unacked()); | |
2400 | |
2401 // Ack the ack, which updates the rtt and raises the least unacked. | |
2402 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2403 frame = InitAckFrame(3); | |
2404 ProcessAckPacket(&frame); | |
2405 | |
2406 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2407 .WillByDefault(Return(true)); | |
2408 SendStreamDataToPeer(1, "bar", 3, false, nullptr); // Packet 4 | |
2409 EXPECT_EQ(4u, stop_waiting()->least_unacked); | |
2410 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2411 .WillByDefault(Return(false)); | |
2412 SendAckPacketToPeer(); // Packet 5 | |
2413 EXPECT_EQ(4u, least_unacked()); | |
2414 | |
2415 // Send two data packets at the end, and ensure if the last one is acked, | |
2416 // the least unacked is raised above the ack packets. | |
2417 ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2418 .WillByDefault(Return(true)); | |
2419 SendStreamDataToPeer(1, "bar", 6, false, nullptr); // Packet 6 | |
2420 SendStreamDataToPeer(1, "bar", 9, false, nullptr); // Packet 7 | |
2421 | |
2422 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2423 frame = InitAckFrame(7); | |
2424 NackPacket(5, &frame); | |
2425 NackPacket(6, &frame); | |
2426 ProcessAckPacket(&frame); | |
2427 | |
2428 EXPECT_EQ(6u, stop_waiting()->least_unacked); | |
2429 } | |
2430 | |
2431 TEST_P(QuicConnectionTest, TLP) { | |
2432 connection_.SetMaxTailLossProbes(kDefaultPathId, 1); | |
2433 | |
2434 SendStreamDataToPeer(3, "foo", 0, !kFin, nullptr); | |
2435 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
2436 QuicTime retransmission_time = | |
2437 connection_.GetRetransmissionAlarm()->deadline(); | |
2438 EXPECT_NE(QuicTime::Zero(), retransmission_time); | |
2439 | |
2440 EXPECT_EQ(1u, writer_->header().packet_number); | |
2441 // Simulate the retransmission alarm firing and sending a tlp, | |
2442 // so send algorithm's OnRetransmissionTimeout is not called. | |
2443 clock_.AdvanceTime(retransmission_time - clock_.Now()); | |
2444 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2u, _, _)); | |
2445 connection_.GetRetransmissionAlarm()->Fire(); | |
2446 EXPECT_EQ(2u, writer_->header().packet_number); | |
2447 // We do not raise the high water mark yet. | |
2448 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
2449 } | |
2450 | |
2451 TEST_P(QuicConnectionTest, RTO) { | |
2452 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2453 | |
2454 QuicTime default_retransmission_time = | |
2455 clock_.ApproximateNow() + DefaultRetransmissionTime(); | |
2456 SendStreamDataToPeer(3, "foo", 0, !kFin, nullptr); | |
2457 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
2458 | |
2459 EXPECT_EQ(1u, writer_->header().packet_number); | |
2460 EXPECT_EQ(default_retransmission_time, | |
2461 connection_.GetRetransmissionAlarm()->deadline()); | |
2462 // Simulate the retransmission alarm firing. | |
2463 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2464 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2u, _, _)); | |
2465 connection_.GetRetransmissionAlarm()->Fire(); | |
2466 EXPECT_EQ(2u, writer_->header().packet_number); | |
2467 // We do not raise the high water mark yet. | |
2468 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
2469 } | |
2470 | |
2471 TEST_P(QuicConnectionTest, RTOWithSameEncryptionLevel) { | |
2472 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2473 | |
2474 QuicTime default_retransmission_time = | |
2475 clock_.ApproximateNow() + DefaultRetransmissionTime(); | |
2476 use_tagging_decrypter(); | |
2477 | |
2478 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at | |
2479 // the end of the packet. We can test this to check which encrypter was used. | |
2480 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); | |
2481 SendStreamDataToPeer(3, "foo", 0, !kFin, nullptr); | |
2482 EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); | |
2483 | |
2484 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); | |
2485 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2486 SendStreamDataToPeer(3, "foo", 0, !kFin, nullptr); | |
2487 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2488 | |
2489 EXPECT_EQ(default_retransmission_time, | |
2490 connection_.GetRetransmissionAlarm()->deadline()); | |
2491 { | |
2492 InSequence s; | |
2493 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 3, _, _)); | |
2494 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 4, _, _)); | |
2495 } | |
2496 | |
2497 // Simulate the retransmission alarm firing. | |
2498 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2499 connection_.GetRetransmissionAlarm()->Fire(); | |
2500 | |
2501 // Packet should have been sent with ENCRYPTION_NONE. | |
2502 EXPECT_EQ(0x01010101u, writer_->final_bytes_of_previous_packet()); | |
2503 | |
2504 // Packet should have been sent with ENCRYPTION_INITIAL. | |
2505 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2506 } | |
2507 | |
2508 TEST_P(QuicConnectionTest, SendHandshakeMessages) { | |
2509 use_tagging_decrypter(); | |
2510 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at | |
2511 // the end of the packet. We can test this to check which encrypter was used. | |
2512 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); | |
2513 | |
2514 // Attempt to send a handshake message and have the socket block. | |
2515 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
2516 .WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); | |
2517 BlockOnNextWrite(); | |
2518 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2519 // The packet should be serialized, but not queued. | |
2520 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
2521 | |
2522 // Switch to the new encrypter. | |
2523 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); | |
2524 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2525 | |
2526 // Now become writeable and flush the packets. | |
2527 writer_->SetWritable(); | |
2528 EXPECT_CALL(visitor_, OnCanWrite()); | |
2529 connection_.OnCanWrite(); | |
2530 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
2531 | |
2532 // Verify that the handshake packet went out at the null encryption. | |
2533 EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet()); | |
2534 } | |
2535 | |
2536 TEST_P(QuicConnectionTest, | |
2537 DropRetransmitsForNullEncryptedPacketAfterForwardSecure) { | |
2538 use_tagging_decrypter(); | |
2539 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); | |
2540 QuicPacketNumber packet_number; | |
2541 SendStreamDataToPeer(3, "foo", 0, !kFin, &packet_number); | |
2542 | |
2543 // Simulate the retransmission alarm firing and the socket blocking. | |
2544 BlockOnNextWrite(); | |
2545 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2546 connection_.GetRetransmissionAlarm()->Fire(); | |
2547 | |
2548 // Go forward secure. | |
2549 connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, | |
2550 new TaggingEncrypter(0x02)); | |
2551 connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); | |
2552 connection_.NeuterUnencryptedPackets(); | |
2553 | |
2554 EXPECT_EQ(QuicTime::Zero(), connection_.GetRetransmissionAlarm()->deadline()); | |
2555 // Unblock the socket and ensure that no packets are sent. | |
2556 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
2557 writer_->SetWritable(); | |
2558 connection_.OnCanWrite(); | |
2559 } | |
2560 | |
2561 TEST_P(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) { | |
2562 use_tagging_decrypter(); | |
2563 connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01)); | |
2564 connection_.SetDefaultEncryptionLevel(ENCRYPTION_NONE); | |
2565 | |
2566 SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr); | |
2567 | |
2568 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); | |
2569 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2570 | |
2571 SendStreamDataToPeer(2, "bar", 0, !kFin, nullptr); | |
2572 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2573 | |
2574 connection_.RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION); | |
2575 } | |
2576 | |
2577 TEST_P(QuicConnectionTest, DelayForwardSecureEncryptionUntilClientIsReady) { | |
2578 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at | |
2579 // the end of the packet. We can test this to check which encrypter was used. | |
2580 use_tagging_decrypter(); | |
2581 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); | |
2582 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2583 SendAckPacketToPeer(); | |
2584 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2585 | |
2586 // Set a forward-secure encrypter but do not make it the default, and verify | |
2587 // that it is not yet used. | |
2588 connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, | |
2589 new TaggingEncrypter(0x03)); | |
2590 SendAckPacketToPeer(); | |
2591 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2592 | |
2593 // Now simulate receipt of a forward-secure packet and verify that the | |
2594 // forward-secure encrypter is now used. | |
2595 connection_.OnDecryptedPacket(ENCRYPTION_FORWARD_SECURE); | |
2596 SendAckPacketToPeer(); | |
2597 EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); | |
2598 } | |
2599 | |
2600 TEST_P(QuicConnectionTest, DelayForwardSecureEncryptionUntilManyPacketSent) { | |
2601 // Set a congestion window of 10 packets. | |
2602 QuicPacketCount congestion_window = 10; | |
2603 EXPECT_CALL(*send_algorithm_, GetCongestionWindow()) | |
2604 .WillRepeatedly(Return(congestion_window * kDefaultMaxPacketSize)); | |
2605 | |
2606 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at | |
2607 // the end of the packet. We can test this to check which encrypter was used. | |
2608 use_tagging_decrypter(); | |
2609 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02)); | |
2610 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2611 SendAckPacketToPeer(); | |
2612 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2613 | |
2614 // Set a forward-secure encrypter but do not make it the default, and | |
2615 // verify that it is not yet used. | |
2616 connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, | |
2617 new TaggingEncrypter(0x03)); | |
2618 SendAckPacketToPeer(); | |
2619 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2620 | |
2621 // Now send a packet "Far enough" after the encrypter was set and verify that | |
2622 // the forward-secure encrypter is now used. | |
2623 for (uint64_t i = 0; i < 3 * congestion_window - 1; ++i) { | |
2624 EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet()); | |
2625 SendAckPacketToPeer(); | |
2626 } | |
2627 EXPECT_EQ(0x03030303u, writer_->final_bytes_of_last_packet()); | |
2628 } | |
2629 | |
2630 TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) { | |
2631 // SetFromConfig is always called after construction from InitializeSession. | |
2632 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
2633 QuicConfig config; | |
2634 connection_.SetFromConfig(config); | |
2635 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2636 use_tagging_decrypter(); | |
2637 | |
2638 const uint8_t tag = 0x07; | |
2639 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
2640 | |
2641 // Process an encrypted packet which can not yet be decrypted which should | |
2642 // result in the packet being buffered. | |
2643 ProcessDataPacketAtLevel(kDefaultPathId, 1, kEntropyFlag, !kHasStopWaiting, | |
2644 ENCRYPTION_INITIAL); | |
2645 | |
2646 // Transition to the new encryption state and process another encrypted packet | |
2647 // which should result in the original packet being processed. | |
2648 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
2649 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2650 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
2651 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(2); | |
2652 ProcessDataPacketAtLevel(kDefaultPathId, 2, kEntropyFlag, !kHasStopWaiting, | |
2653 ENCRYPTION_INITIAL); | |
2654 | |
2655 // Finally, process a third packet and note that we do not reprocess the | |
2656 // buffered packet. | |
2657 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
2658 ProcessDataPacketAtLevel(kDefaultPathId, 3, kEntropyFlag, !kHasStopWaiting, | |
2659 ENCRYPTION_INITIAL); | |
2660 } | |
2661 | |
2662 TEST_P(QuicConnectionTest, Buffer100NonDecryptablePackets) { | |
2663 // SetFromConfig is always called after construction from InitializeSession. | |
2664 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
2665 QuicConfig config; | |
2666 config.set_max_undecryptable_packets(100); | |
2667 connection_.SetFromConfig(config); | |
2668 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2669 use_tagging_decrypter(); | |
2670 | |
2671 const uint8_t tag = 0x07; | |
2672 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
2673 | |
2674 // Process an encrypted packet which can not yet be decrypted which should | |
2675 // result in the packet being buffered. | |
2676 for (QuicPacketNumber i = 1; i <= 100; ++i) { | |
2677 ProcessDataPacketAtLevel(kDefaultPathId, i, kEntropyFlag, !kHasStopWaiting, | |
2678 ENCRYPTION_INITIAL); | |
2679 } | |
2680 | |
2681 // Transition to the new encryption state and process another encrypted packet | |
2682 // which should result in the original packets being processed. | |
2683 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
2684 connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); | |
2685 connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
2686 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(101); | |
2687 ProcessDataPacketAtLevel(kDefaultPathId, 101, kEntropyFlag, !kHasStopWaiting, | |
2688 ENCRYPTION_INITIAL); | |
2689 | |
2690 // Finally, process a third packet and note that we do not reprocess the | |
2691 // buffered packet. | |
2692 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
2693 ProcessDataPacketAtLevel(kDefaultPathId, 102, kEntropyFlag, !kHasStopWaiting, | |
2694 ENCRYPTION_INITIAL); | |
2695 } | |
2696 | |
2697 TEST_P(QuicConnectionTest, TestRetransmitOrder) { | |
2698 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2699 | |
2700 QuicByteCount first_packet_size; | |
2701 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2702 .WillOnce(DoAll(SaveArg<3>(&first_packet_size), Return(true))); | |
2703 | |
2704 connection_.SendStreamDataWithString(3, "first_packet", 0, !kFin, nullptr); | |
2705 QuicByteCount second_packet_size; | |
2706 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2707 .WillOnce(DoAll(SaveArg<3>(&second_packet_size), Return(true))); | |
2708 connection_.SendStreamDataWithString(3, "second_packet", 12, !kFin, nullptr); | |
2709 EXPECT_NE(first_packet_size, second_packet_size); | |
2710 // Advance the clock by huge time to make sure packets will be retransmitted. | |
2711 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); | |
2712 { | |
2713 InSequence s; | |
2714 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, first_packet_size, _)); | |
2715 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, second_packet_size, _)); | |
2716 } | |
2717 connection_.GetRetransmissionAlarm()->Fire(); | |
2718 | |
2719 // Advance again and expect the packets to be sent again in the same order. | |
2720 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(20)); | |
2721 { | |
2722 InSequence s; | |
2723 EXPECT_CALL(visitor_, OnPathDegrading()); | |
2724 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, first_packet_size, _)); | |
2725 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, second_packet_size, _)); | |
2726 } | |
2727 connection_.GetRetransmissionAlarm()->Fire(); | |
2728 } | |
2729 | |
2730 TEST_P(QuicConnectionTest, SetRTOAfterWritingToSocket) { | |
2731 BlockOnNextWrite(); | |
2732 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2733 // Make sure that RTO is not started when the packet is queued. | |
2734 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2735 | |
2736 // Test that RTO is started once we write to the socket. | |
2737 writer_->SetWritable(); | |
2738 connection_.OnCanWrite(); | |
2739 EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2740 } | |
2741 | |
2742 TEST_P(QuicConnectionTest, DelayRTOWithAckReceipt) { | |
2743 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2744 | |
2745 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2746 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
2747 connection_.SendStreamDataWithString(2, "foo", 0, !kFin, nullptr); | |
2748 connection_.SendStreamDataWithString(3, "bar", 0, !kFin, nullptr); | |
2749 QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm(); | |
2750 EXPECT_TRUE(retransmission_alarm->IsSet()); | |
2751 EXPECT_EQ(clock_.Now() + DefaultRetransmissionTime(), | |
2752 retransmission_alarm->deadline()); | |
2753 | |
2754 // Advance the time right before the RTO, then receive an ack for the first | |
2755 // packet to delay the RTO. | |
2756 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
2757 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2758 QuicAckFrame ack = InitAckFrame(1); | |
2759 ProcessAckPacket(&ack); | |
2760 EXPECT_TRUE(retransmission_alarm->IsSet()); | |
2761 EXPECT_GT(retransmission_alarm->deadline(), clock_.Now()); | |
2762 | |
2763 // Move forward past the original RTO and ensure the RTO is still pending. | |
2764 clock_.AdvanceTime(2 * DefaultRetransmissionTime()); | |
2765 | |
2766 // Ensure the second packet gets retransmitted when it finally fires. | |
2767 EXPECT_TRUE(retransmission_alarm->IsSet()); | |
2768 EXPECT_LT(retransmission_alarm->deadline(), clock_.ApproximateNow()); | |
2769 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
2770 // Manually cancel the alarm to simulate a real test. | |
2771 connection_.GetRetransmissionAlarm()->Fire(); | |
2772 | |
2773 // The new retransmitted packet number should set the RTO to a larger value | |
2774 // than previously. | |
2775 EXPECT_TRUE(retransmission_alarm->IsSet()); | |
2776 QuicTime next_rto_time = retransmission_alarm->deadline(); | |
2777 QuicTime expected_rto_time = | |
2778 connection_.sent_packet_manager().GetRetransmissionTime(); | |
2779 EXPECT_EQ(next_rto_time, expected_rto_time); | |
2780 } | |
2781 | |
2782 TEST_P(QuicConnectionTest, TestQueued) { | |
2783 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
2784 | |
2785 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
2786 BlockOnNextWrite(); | |
2787 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
2788 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
2789 | |
2790 // Unblock the writes and actually send. | |
2791 writer_->SetWritable(); | |
2792 connection_.OnCanWrite(); | |
2793 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
2794 } | |
2795 | |
2796 TEST_P(QuicConnectionTest, InitialTimeout) { | |
2797 EXPECT_TRUE(connection_.connected()); | |
2798 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); | |
2799 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
2800 | |
2801 // SetFromConfig sets the initial timeouts before negotiation. | |
2802 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
2803 QuicConfig config; | |
2804 connection_.SetFromConfig(config); | |
2805 // Subtract a second from the idle timeout on the client side. | |
2806 QuicTime default_timeout = | |
2807 clock_.ApproximateNow() + | |
2808 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); | |
2809 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
2810 | |
2811 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NETWORK_IDLE_TIMEOUT, _, | |
2812 ConnectionCloseSource::FROM_SELF)); | |
2813 // Simulate the timeout alarm firing. | |
2814 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1)); | |
2815 connection_.GetTimeoutAlarm()->Fire(); | |
2816 | |
2817 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
2818 EXPECT_FALSE(connection_.connected()); | |
2819 | |
2820 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
2821 EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); | |
2822 EXPECT_FALSE(connection_.GetResumeWritesAlarm()->IsSet()); | |
2823 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2824 EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); | |
2825 EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
2826 } | |
2827 | |
2828 TEST_P(QuicConnectionTest, HandshakeTimeout) { | |
2829 // Use a shorter handshake timeout than idle timeout for this test. | |
2830 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(5); | |
2831 connection_.SetNetworkTimeouts(timeout, timeout); | |
2832 EXPECT_TRUE(connection_.connected()); | |
2833 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); | |
2834 | |
2835 QuicTime handshake_timeout = | |
2836 clock_.ApproximateNow() + timeout - QuicTime::Delta::FromSeconds(1); | |
2837 EXPECT_EQ(handshake_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
2838 EXPECT_TRUE(connection_.connected()); | |
2839 | |
2840 // Send and ack new data 3 seconds later to lengthen the idle timeout. | |
2841 SendStreamDataToPeer(kHeadersStreamId, "GET /", 0, kFin, nullptr); | |
2842 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(3)); | |
2843 QuicAckFrame frame = InitAckFrame(1); | |
2844 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2845 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2846 ProcessAckPacket(&frame); | |
2847 | |
2848 // Fire early to verify it wouldn't timeout yet. | |
2849 connection_.GetTimeoutAlarm()->Fire(); | |
2850 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
2851 EXPECT_TRUE(connection_.connected()); | |
2852 | |
2853 clock_.AdvanceTime(timeout - QuicTime::Delta::FromSeconds(2)); | |
2854 | |
2855 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_HANDSHAKE_TIMEOUT, _, | |
2856 ConnectionCloseSource::FROM_SELF)); | |
2857 // Simulate the timeout alarm firing. | |
2858 connection_.GetTimeoutAlarm()->Fire(); | |
2859 | |
2860 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
2861 EXPECT_FALSE(connection_.connected()); | |
2862 | |
2863 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
2864 EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); | |
2865 EXPECT_FALSE(connection_.GetResumeWritesAlarm()->IsSet()); | |
2866 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2867 EXPECT_FALSE(connection_.GetSendAlarm()->IsSet()); | |
2868 } | |
2869 | |
2870 TEST_P(QuicConnectionTest, PingAfterSend) { | |
2871 EXPECT_TRUE(connection_.connected()); | |
2872 EXPECT_CALL(visitor_, HasOpenDynamicStreams()).WillRepeatedly(Return(true)); | |
2873 EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); | |
2874 | |
2875 // Advance to 5ms, and send a packet to the peer, which will set | |
2876 // the ping alarm. | |
2877 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
2878 EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet()); | |
2879 SendStreamDataToPeer(kHeadersStreamId, "GET /", 0, kFin, nullptr); | |
2880 EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); | |
2881 EXPECT_EQ(clock_.ApproximateNow() + QuicTime::Delta::FromSeconds(15), | |
2882 connection_.GetPingAlarm()->deadline()); | |
2883 | |
2884 // Now recevie and ACK of the previous packet, which will move the | |
2885 // ping alarm forward. | |
2886 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
2887 QuicAckFrame frame = InitAckFrame(1); | |
2888 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2889 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2890 ProcessAckPacket(&frame); | |
2891 EXPECT_TRUE(connection_.GetPingAlarm()->IsSet()); | |
2892 // The ping timer is set slightly less than 15 seconds in the future, because | |
2893 // of the 1s ping timer alarm granularity. | |
2894 EXPECT_EQ(clock_.ApproximateNow() + QuicTime::Delta::FromSeconds(15) - | |
2895 QuicTime::Delta::FromMilliseconds(5), | |
2896 connection_.GetPingAlarm()->deadline()); | |
2897 | |
2898 writer_->Reset(); | |
2899 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(15)); | |
2900 connection_.GetPingAlarm()->Fire(); | |
2901 EXPECT_EQ(1u, writer_->frame_count()); | |
2902 ASSERT_EQ(1u, writer_->ping_frames().size()); | |
2903 writer_->Reset(); | |
2904 | |
2905 EXPECT_CALL(visitor_, HasOpenDynamicStreams()).WillRepeatedly(Return(false)); | |
2906 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); | |
2907 SendAckPacketToPeer(); | |
2908 | |
2909 EXPECT_FALSE(connection_.GetPingAlarm()->IsSet()); | |
2910 } | |
2911 | |
2912 // Tests whether sending an MTU discovery packet to peer successfully causes the | |
2913 // maximum packet size to increase. | |
2914 TEST_P(QuicConnectionTest, SendMtuDiscoveryPacket) { | |
2915 EXPECT_TRUE(connection_.connected()); | |
2916 | |
2917 // Send an MTU probe. | |
2918 const size_t new_mtu = kDefaultMaxPacketSize + 100; | |
2919 QuicByteCount mtu_probe_size; | |
2920 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2921 .WillOnce(DoAll(SaveArg<3>(&mtu_probe_size), Return(true))); | |
2922 connection_.SendMtuDiscoveryPacket(new_mtu); | |
2923 EXPECT_EQ(new_mtu, mtu_probe_size); | |
2924 EXPECT_EQ(1u, creator_->packet_number()); | |
2925 | |
2926 // Send more than MTU worth of data. No acknowledgement was received so far, | |
2927 // so the MTU should be at its old value. | |
2928 const string data(kDefaultMaxPacketSize + 1, '.'); | |
2929 QuicByteCount size_before_mtu_change; | |
2930 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2931 .WillOnce(DoAll(SaveArg<3>(&size_before_mtu_change), Return(true))) | |
2932 .WillOnce(Return(true)); | |
2933 connection_.SendStreamDataWithString(3, data, 0, kFin, nullptr); | |
2934 EXPECT_EQ(3u, creator_->packet_number()); | |
2935 EXPECT_EQ(kDefaultMaxPacketSize, size_before_mtu_change); | |
2936 | |
2937 // Acknowledge all packets so far. | |
2938 QuicAckFrame probe_ack = InitAckFrame(3); | |
2939 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2940 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2941 ProcessAckPacket(&probe_ack); | |
2942 EXPECT_EQ(new_mtu, connection_.max_packet_length()); | |
2943 | |
2944 // Send the same data again. Check that it fits into a single packet now. | |
2945 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
2946 connection_.SendStreamDataWithString(3, data, 0, kFin, nullptr); | |
2947 EXPECT_EQ(4u, creator_->packet_number()); | |
2948 } | |
2949 | |
2950 // Tests whether MTU discovery does not happen when it is not explicitly enabled | |
2951 // by the connection options. | |
2952 TEST_P(QuicConnectionTest, MtuDiscoveryDisabled) { | |
2953 EXPECT_TRUE(connection_.connected()); | |
2954 | |
2955 const QuicPacketCount number_of_packets = kPacketsBetweenMtuProbesBase * 2; | |
2956 for (QuicPacketCount i = 0; i < number_of_packets; i++) { | |
2957 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
2958 EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
2959 EXPECT_EQ(0u, connection_.mtu_probe_count()); | |
2960 } | |
2961 } | |
2962 | |
2963 // Tests whether MTU discovery works when the probe gets acknowledged on the | |
2964 // first try. | |
2965 TEST_P(QuicConnectionTest, MtuDiscoveryEnabled) { | |
2966 EXPECT_TRUE(connection_.connected()); | |
2967 | |
2968 connection_.EnablePathMtuDiscovery(send_algorithm_); | |
2969 | |
2970 // Send enough packets so that the next one triggers path MTU discovery. | |
2971 for (QuicPacketCount i = 0; i < kPacketsBetweenMtuProbesBase - 1; i++) { | |
2972 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
2973 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
2974 } | |
2975 | |
2976 // Trigger the probe. | |
2977 SendStreamDataToPeer(3, "!", kPacketsBetweenMtuProbesBase, | |
2978 /*fin=*/false, nullptr); | |
2979 ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
2980 QuicByteCount probe_size; | |
2981 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
2982 .WillOnce(DoAll(SaveArg<3>(&probe_size), Return(true))); | |
2983 connection_.GetMtuDiscoveryAlarm()->Fire(); | |
2984 EXPECT_EQ(kMtuDiscoveryTargetPacketSizeHigh, probe_size); | |
2985 | |
2986 const QuicPacketCount probe_packet_number = kPacketsBetweenMtuProbesBase + 1; | |
2987 ASSERT_EQ(probe_packet_number, creator_->packet_number()); | |
2988 | |
2989 // Acknowledge all packets sent so far. | |
2990 QuicAckFrame probe_ack = InitAckFrame(probe_packet_number); | |
2991 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
2992 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
2993 ProcessAckPacket(&probe_ack); | |
2994 EXPECT_EQ(kMtuDiscoveryTargetPacketSizeHigh, connection_.max_packet_length()); | |
2995 EXPECT_EQ(0u, connection_.GetBytesInFlight(kDefaultPathId)); | |
2996 | |
2997 // Send more packets, and ensure that none of them sets the alarm. | |
2998 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { | |
2999 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
3000 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3001 } | |
3002 | |
3003 EXPECT_EQ(1u, connection_.mtu_probe_count()); | |
3004 } | |
3005 | |
3006 // Tests whether MTU discovery works correctly when the probes never get | |
3007 // acknowledged. | |
3008 TEST_P(QuicConnectionTest, MtuDiscoveryFailed) { | |
3009 EXPECT_TRUE(connection_.connected()); | |
3010 | |
3011 connection_.EnablePathMtuDiscovery(send_algorithm_); | |
3012 | |
3013 const QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(100); | |
3014 | |
3015 EXPECT_EQ(kPacketsBetweenMtuProbesBase, | |
3016 QuicConnectionPeer::GetPacketsBetweenMtuProbes(&connection_)); | |
3017 // Lower the number of probes between packets in order to make the test go | |
3018 // much faster. | |
3019 const QuicPacketCount packets_between_probes_base = 10; | |
3020 QuicConnectionPeer::SetPacketsBetweenMtuProbes(&connection_, | |
3021 packets_between_probes_base); | |
3022 QuicConnectionPeer::SetNextMtuProbeAt(&connection_, | |
3023 packets_between_probes_base); | |
3024 | |
3025 // This tests sends more packets than strictly necessary to make sure that if | |
3026 // the connection was to send more discovery packets than needed, those would | |
3027 // get caught as well. | |
3028 const QuicPacketCount number_of_packets = | |
3029 packets_between_probes_base * (1 << (kMtuDiscoveryAttempts + 1)); | |
3030 vector<QuicPacketNumber> mtu_discovery_packets; | |
3031 // Called by the first ack. | |
3032 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3033 // Called on many acks. | |
3034 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)) | |
3035 .Times(AnyNumber()); | |
3036 for (QuicPacketCount i = 0; i < number_of_packets; i++) { | |
3037 SendStreamDataToPeer(3, "!", i, /*fin=*/false, nullptr); | |
3038 clock_.AdvanceTime(rtt); | |
3039 | |
3040 // Receive an ACK, which marks all data packets as received, and all MTU | |
3041 // discovery packets as missing. | |
3042 QuicAckFrame ack = InitAckFrame(creator_->packet_number()); | |
3043 for (QuicPacketNumber& packet : mtu_discovery_packets) { | |
3044 NackPacket(packet, &ack); | |
3045 } | |
3046 ProcessAckPacket(&ack); | |
3047 | |
3048 // Trigger MTU probe if it would be scheduled now. | |
3049 if (!connection_.GetMtuDiscoveryAlarm()->IsSet()) { | |
3050 continue; | |
3051 } | |
3052 | |
3053 // Fire the alarm. The alarm should cause a packet to be sent. | |
3054 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
3055 .WillOnce(Return(true)); | |
3056 connection_.GetMtuDiscoveryAlarm()->Fire(); | |
3057 // Record the packet number of the MTU discovery packet in order to | |
3058 // mark it as NACK'd. | |
3059 mtu_discovery_packets.push_back(creator_->packet_number()); | |
3060 } | |
3061 | |
3062 // Ensure the number of packets between probes grows exponentially by checking | |
3063 // it against the closed-form expression for the packet number. | |
3064 ASSERT_EQ(kMtuDiscoveryAttempts, mtu_discovery_packets.size()); | |
3065 for (QuicPacketNumber i = 0; i < kMtuDiscoveryAttempts; i++) { | |
3066 // 2^0 + 2^1 + 2^2 + ... + 2^n = 2^(n + 1) - 1 | |
3067 const QuicPacketCount packets_between_probes = | |
3068 packets_between_probes_base * ((1 << (i + 1)) - 1); | |
3069 EXPECT_EQ(packets_between_probes + (i + 1), mtu_discovery_packets[i]); | |
3070 } | |
3071 | |
3072 EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3073 EXPECT_EQ(kDefaultMaxPacketSize, connection_.max_packet_length()); | |
3074 EXPECT_EQ(kMtuDiscoveryAttempts, connection_.mtu_probe_count()); | |
3075 } | |
3076 | |
3077 // Tests whether MTU discovery works when the writer has a limit on how large a | |
3078 // packet can be. | |
3079 TEST_P(QuicConnectionTest, MtuDiscoveryWriterLimited) { | |
3080 EXPECT_TRUE(connection_.connected()); | |
3081 | |
3082 const QuicByteCount mtu_limit = kMtuDiscoveryTargetPacketSizeHigh - 1; | |
3083 writer_->set_max_packet_size(mtu_limit); | |
3084 connection_.EnablePathMtuDiscovery(send_algorithm_); | |
3085 | |
3086 // Send enough packets so that the next one triggers path MTU discovery. | |
3087 for (QuicPacketCount i = 0; i < kPacketsBetweenMtuProbesBase - 1; i++) { | |
3088 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
3089 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3090 } | |
3091 | |
3092 // Trigger the probe. | |
3093 SendStreamDataToPeer(3, "!", kPacketsBetweenMtuProbesBase, | |
3094 /*fin=*/false, nullptr); | |
3095 ASSERT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3096 QuicByteCount probe_size; | |
3097 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
3098 .WillOnce(DoAll(SaveArg<3>(&probe_size), Return(true))); | |
3099 connection_.GetMtuDiscoveryAlarm()->Fire(); | |
3100 EXPECT_EQ(mtu_limit, probe_size); | |
3101 | |
3102 const QuicPacketCount probe_sequence_number = | |
3103 kPacketsBetweenMtuProbesBase + 1; | |
3104 ASSERT_EQ(probe_sequence_number, creator_->packet_number()); | |
3105 | |
3106 // Acknowledge all packets sent so far. | |
3107 QuicAckFrame probe_ack = InitAckFrame(probe_sequence_number); | |
3108 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3109 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
3110 ProcessAckPacket(&probe_ack); | |
3111 EXPECT_EQ(mtu_limit, connection_.max_packet_length()); | |
3112 EXPECT_EQ(0u, connection_.GetBytesInFlight(kDefaultPathId)); | |
3113 | |
3114 // Send more packets, and ensure that none of them sets the alarm. | |
3115 for (QuicPacketCount i = 0; i < 4 * kPacketsBetweenMtuProbesBase; i++) { | |
3116 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
3117 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3118 } | |
3119 | |
3120 EXPECT_EQ(1u, connection_.mtu_probe_count()); | |
3121 } | |
3122 | |
3123 TEST_P(QuicConnectionTest, NoMtuDiscoveryAfterConnectionClosed) { | |
3124 EXPECT_TRUE(connection_.connected()); | |
3125 | |
3126 connection_.EnablePathMtuDiscovery(send_algorithm_); | |
3127 | |
3128 // Send enough packets so that the next one triggers path MTU discovery. | |
3129 for (QuicPacketCount i = 0; i < kPacketsBetweenMtuProbesBase - 1; i++) { | |
3130 SendStreamDataToPeer(3, ".", i, /*fin=*/false, nullptr); | |
3131 ASSERT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3132 } | |
3133 | |
3134 SendStreamDataToPeer(3, "!", kPacketsBetweenMtuProbesBase, | |
3135 /*fin=*/false, nullptr); | |
3136 EXPECT_TRUE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3137 | |
3138 EXPECT_CALL(visitor_, OnConnectionClosed(_, _, _)); | |
3139 connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", | |
3140 ConnectionCloseBehavior::SILENT_CLOSE); | |
3141 EXPECT_FALSE(connection_.GetMtuDiscoveryAlarm()->IsSet()); | |
3142 } | |
3143 | |
3144 TEST_P(QuicConnectionTest, TimeoutAfterSend) { | |
3145 EXPECT_TRUE(connection_.connected()); | |
3146 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
3147 QuicConfig config; | |
3148 connection_.SetFromConfig(config); | |
3149 EXPECT_FALSE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_)); | |
3150 | |
3151 const QuicTime::Delta initial_idle_timeout = | |
3152 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); | |
3153 const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); | |
3154 QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; | |
3155 | |
3156 // When we send a packet, the timeout will change to 5ms + | |
3157 // kInitialIdleTimeoutSecs. | |
3158 clock_.AdvanceTime(five_ms); | |
3159 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, kFin, nullptr); | |
3160 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3161 | |
3162 // Now send more data. This will not move the timeout becase | |
3163 // no data has been recieved since the previous write. | |
3164 clock_.AdvanceTime(five_ms); | |
3165 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, kFin, nullptr); | |
3166 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3167 | |
3168 // The original alarm will fire. We should not time out because we had a | |
3169 // network event at t=5ms. The alarm will reregister. | |
3170 clock_.AdvanceTime(initial_idle_timeout - five_ms - five_ms); | |
3171 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | |
3172 connection_.GetTimeoutAlarm()->Fire(); | |
3173 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
3174 EXPECT_TRUE(connection_.connected()); | |
3175 EXPECT_EQ(default_timeout + five_ms + five_ms, | |
3176 connection_.GetTimeoutAlarm()->deadline()); | |
3177 | |
3178 // This time, we should time out. | |
3179 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NETWORK_IDLE_TIMEOUT, _, | |
3180 ConnectionCloseSource::FROM_SELF)); | |
3181 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3182 clock_.AdvanceTime(five_ms); | |
3183 EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); | |
3184 connection_.GetTimeoutAlarm()->Fire(); | |
3185 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
3186 EXPECT_FALSE(connection_.connected()); | |
3187 } | |
3188 | |
3189 TEST_P(QuicConnectionTest, NewTimeoutAfterSendSilentClose) { | |
3190 // Same test as above, but complete a handshake which enables silent close, | |
3191 // causing no connection close packet to be sent. | |
3192 EXPECT_TRUE(connection_.connected()); | |
3193 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
3194 QuicConfig config; | |
3195 | |
3196 // Create a handshake message that also enables silent close. | |
3197 CryptoHandshakeMessage msg; | |
3198 string error_details; | |
3199 QuicConfig client_config; | |
3200 client_config.SetInitialStreamFlowControlWindowToSend( | |
3201 kInitialStreamFlowControlWindowForTest); | |
3202 client_config.SetInitialSessionFlowControlWindowToSend( | |
3203 kInitialSessionFlowControlWindowForTest); | |
3204 client_config.SetIdleConnectionStateLifetime( | |
3205 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs), | |
3206 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs)); | |
3207 client_config.ToHandshakeMessage(&msg); | |
3208 const QuicErrorCode error = | |
3209 config.ProcessPeerHello(msg, CLIENT, &error_details); | |
3210 EXPECT_EQ(QUIC_NO_ERROR, error); | |
3211 | |
3212 connection_.SetFromConfig(config); | |
3213 EXPECT_TRUE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_)); | |
3214 | |
3215 const QuicTime::Delta default_idle_timeout = | |
3216 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs - 1); | |
3217 const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); | |
3218 QuicTime default_timeout = clock_.ApproximateNow() + default_idle_timeout; | |
3219 | |
3220 // When we send a packet, the timeout will change to 5ms + | |
3221 // kInitialIdleTimeoutSecs. | |
3222 clock_.AdvanceTime(five_ms); | |
3223 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, kFin, nullptr); | |
3224 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3225 | |
3226 // Now send more data. This will not move the timeout becase | |
3227 // no data has been recieved since the previous write. | |
3228 clock_.AdvanceTime(five_ms); | |
3229 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, kFin, nullptr); | |
3230 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3231 | |
3232 // The original alarm will fire. We should not time out because we had a | |
3233 // network event at t=5ms. The alarm will reregister. | |
3234 clock_.AdvanceTime(default_idle_timeout - five_ms - five_ms); | |
3235 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | |
3236 connection_.GetTimeoutAlarm()->Fire(); | |
3237 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
3238 EXPECT_TRUE(connection_.connected()); | |
3239 EXPECT_EQ(default_timeout + five_ms + five_ms, | |
3240 connection_.GetTimeoutAlarm()->deadline()); | |
3241 | |
3242 // This time, we should time out. | |
3243 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NETWORK_IDLE_TIMEOUT, _, | |
3244 ConnectionCloseSource::FROM_SELF)); | |
3245 clock_.AdvanceTime(five_ms); | |
3246 EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); | |
3247 connection_.GetTimeoutAlarm()->Fire(); | |
3248 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
3249 EXPECT_FALSE(connection_.connected()); | |
3250 } | |
3251 | |
3252 TEST_P(QuicConnectionTest, TimeoutAfterReceive) { | |
3253 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3254 EXPECT_TRUE(connection_.connected()); | |
3255 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
3256 QuicConfig config; | |
3257 connection_.SetFromConfig(config); | |
3258 EXPECT_FALSE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_)); | |
3259 | |
3260 const QuicTime::Delta initial_idle_timeout = | |
3261 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); | |
3262 const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); | |
3263 QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; | |
3264 | |
3265 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
3266 nullptr); | |
3267 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 3, !kFin, | |
3268 nullptr); | |
3269 | |
3270 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3271 clock_.AdvanceTime(five_ms); | |
3272 | |
3273 // When we receive a packet, the timeout will change to 5ms + | |
3274 // kInitialIdleTimeoutSecs. | |
3275 QuicAckFrame ack = InitAckFrame(2); | |
3276 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
3277 ProcessAckPacket(&ack); | |
3278 | |
3279 // The original alarm will fire. We should not time out because we had a | |
3280 // network event at t=5ms. The alarm will reregister. | |
3281 clock_.AdvanceTime(initial_idle_timeout - five_ms); | |
3282 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | |
3283 connection_.GetTimeoutAlarm()->Fire(); | |
3284 EXPECT_TRUE(connection_.connected()); | |
3285 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
3286 EXPECT_EQ(default_timeout + five_ms, | |
3287 connection_.GetTimeoutAlarm()->deadline()); | |
3288 | |
3289 // This time, we should time out. | |
3290 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NETWORK_IDLE_TIMEOUT, _, | |
3291 ConnectionCloseSource::FROM_SELF)); | |
3292 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3293 clock_.AdvanceTime(five_ms); | |
3294 EXPECT_EQ(default_timeout + five_ms, clock_.ApproximateNow()); | |
3295 connection_.GetTimeoutAlarm()->Fire(); | |
3296 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
3297 EXPECT_FALSE(connection_.connected()); | |
3298 } | |
3299 | |
3300 TEST_P(QuicConnectionTest, TimeoutAfterReceiveNotSendWhenUnacked) { | |
3301 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3302 EXPECT_TRUE(connection_.connected()); | |
3303 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
3304 QuicConfig config; | |
3305 connection_.SetFromConfig(config); | |
3306 EXPECT_FALSE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_)); | |
3307 | |
3308 const QuicTime::Delta initial_idle_timeout = | |
3309 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs - 1); | |
3310 connection_.SetNetworkTimeouts( | |
3311 QuicTime::Delta::Infinite(), | |
3312 initial_idle_timeout + QuicTime::Delta::FromSeconds(1)); | |
3313 const QuicTime::Delta five_ms = QuicTime::Delta::FromMilliseconds(5); | |
3314 QuicTime default_timeout = clock_.ApproximateNow() + initial_idle_timeout; | |
3315 | |
3316 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3317 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
3318 nullptr); | |
3319 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3320 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 3, !kFin, | |
3321 nullptr); | |
3322 | |
3323 EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline()); | |
3324 | |
3325 clock_.AdvanceTime(five_ms); | |
3326 | |
3327 // When we receive a packet, the timeout will change to 5ms + | |
3328 // kInitialIdleTimeoutSecs. | |
3329 QuicAckFrame ack = InitAckFrame(2); | |
3330 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
3331 ProcessAckPacket(&ack); | |
3332 | |
3333 // The original alarm will fire. We should not time out because we had a | |
3334 // network event at t=5ms. The alarm will reregister. | |
3335 clock_.AdvanceTime(initial_idle_timeout - five_ms); | |
3336 EXPECT_EQ(default_timeout, clock_.ApproximateNow()); | |
3337 connection_.GetTimeoutAlarm()->Fire(); | |
3338 EXPECT_TRUE(connection_.connected()); | |
3339 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
3340 EXPECT_EQ(default_timeout + five_ms, | |
3341 connection_.GetTimeoutAlarm()->deadline()); | |
3342 | |
3343 // Now, send packets while advancing the time and verify that the connection | |
3344 // eventually times out. | |
3345 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NETWORK_IDLE_TIMEOUT, _, | |
3346 ConnectionCloseSource::FROM_SELF)); | |
3347 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(AnyNumber()); | |
3348 for (int i = 0; i < 100 && connection_.connected(); ++i) { | |
3349 VLOG(1) << "sending data packet"; | |
3350 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
3351 nullptr); | |
3352 connection_.GetTimeoutAlarm()->Fire(); | |
3353 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
3354 } | |
3355 EXPECT_FALSE(connection_.connected()); | |
3356 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
3357 } | |
3358 | |
3359 TEST_P(QuicConnectionTest, TimeoutAfter5RTOs) { | |
3360 connection_.SetMaxTailLossProbes(kDefaultPathId, 2); | |
3361 EXPECT_TRUE(connection_.connected()); | |
3362 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
3363 QuicConfig config; | |
3364 QuicTagVector connection_options; | |
3365 connection_options.push_back(k5RTO); | |
3366 config.SetConnectionOptionsToSend(connection_options); | |
3367 connection_.SetFromConfig(config); | |
3368 | |
3369 // Send stream data. | |
3370 SendStreamDataToPeer(kClientDataStreamId1, "foo", 0, kFin, nullptr); | |
3371 | |
3372 EXPECT_CALL(visitor_, OnPathDegrading()); | |
3373 // Fire the retransmission alarm 6 times, twice for TLP and 4 times for RTO. | |
3374 for (int i = 0; i < 6; ++i) { | |
3375 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3376 connection_.GetRetransmissionAlarm()->Fire(); | |
3377 EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet()); | |
3378 EXPECT_TRUE(connection_.connected()); | |
3379 } | |
3380 | |
3381 EXPECT_EQ(2u, connection_.sent_packet_manager().GetConsecutiveTlpCount()); | |
3382 EXPECT_EQ(4u, connection_.sent_packet_manager().GetConsecutiveRtoCount()); | |
3383 // This time, we should time out. | |
3384 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_TOO_MANY_RTOS, _, | |
3385 ConnectionCloseSource::FROM_SELF)); | |
3386 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3387 connection_.GetRetransmissionAlarm()->Fire(); | |
3388 EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet()); | |
3389 EXPECT_FALSE(connection_.connected()); | |
3390 } | |
3391 | |
3392 TEST_P(QuicConnectionTest, SendScheduler) { | |
3393 // Test that if we send a packet without delay, it is not queued. | |
3394 QuicPacket* packet = | |
3395 ConstructDataPacket(kDefaultPathId, 1, !kEntropyFlag, !kHasStopWaiting); | |
3396 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
3397 connection_.SendPacket(ENCRYPTION_NONE, kDefaultPathId, 1, packet, | |
3398 kTestEntropyHash, HAS_RETRANSMITTABLE_DATA, false, | |
3399 false); | |
3400 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
3401 } | |
3402 | |
3403 TEST_P(QuicConnectionTest, FailToSendFirstPacket) { | |
3404 // Test that the connection does not crash when it fails to send the first | |
3405 // packet at which point self_address_ might be uninitialized. | |
3406 EXPECT_CALL(visitor_, OnConnectionClosed(_, _, _)).Times(1); | |
3407 QuicPacket* packet = | |
3408 ConstructDataPacket(kDefaultPathId, 1, !kEntropyFlag, !kHasStopWaiting); | |
3409 writer_->SetShouldWriteFail(); | |
3410 connection_.SendPacket(ENCRYPTION_NONE, kDefaultPathId, 1, packet, | |
3411 kTestEntropyHash, HAS_RETRANSMITTABLE_DATA, false, | |
3412 false); | |
3413 } | |
3414 | |
3415 TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) { | |
3416 QuicPacket* packet = | |
3417 ConstructDataPacket(kDefaultPathId, 1, !kEntropyFlag, !kHasStopWaiting); | |
3418 BlockOnNextWrite(); | |
3419 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _)).Times(0); | |
3420 connection_.SendPacket(ENCRYPTION_NONE, kDefaultPathId, 1, packet, | |
3421 kTestEntropyHash, HAS_RETRANSMITTABLE_DATA, false, | |
3422 false); | |
3423 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
3424 } | |
3425 | |
3426 TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) { | |
3427 // All packets carry version info till version is negotiated. | |
3428 size_t payload_length; | |
3429 size_t length = GetPacketLengthForOneStream( | |
3430 connection_.version(), kIncludeVersion, !kIncludePathId, | |
3431 !kIncludeDiversificationNonce, PACKET_8BYTE_CONNECTION_ID, | |
3432 PACKET_1BYTE_PACKET_NUMBER, &payload_length); | |
3433 connection_.SetMaxPacketLength(length); | |
3434 | |
3435 // Queue the first packet. | |
3436 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
3437 .WillOnce(testing::Return(QuicTime::Delta::FromMicroseconds(10))); | |
3438 const string payload(payload_length, 'a'); | |
3439 EXPECT_EQ(0u, | |
3440 connection_.SendStreamDataWithString(3, payload, 0, !kFin, nullptr) | |
3441 .bytes_consumed); | |
3442 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
3443 } | |
3444 | |
3445 TEST_P(QuicConnectionTest, LoopThroughSendingPackets) { | |
3446 // All packets carry version info till version is negotiated. | |
3447 size_t payload_length; | |
3448 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining | |
3449 // packet length. The size of the offset field in a stream frame is 0 for | |
3450 // offset 0, and 2 for non-zero offsets up through 16K. Increase | |
3451 // max_packet_length by 2 so that subsequent packets containing subsequent | |
3452 // stream frames with non-zero offets will fit within the packet length. | |
3453 size_t length = | |
3454 2 + GetPacketLengthForOneStream( | |
3455 connection_.version(), kIncludeVersion, !kIncludePathId, | |
3456 !kIncludeDiversificationNonce, PACKET_8BYTE_CONNECTION_ID, | |
3457 PACKET_1BYTE_PACKET_NUMBER, &payload_length); | |
3458 connection_.SetMaxPacketLength(length); | |
3459 | |
3460 // Queue the first packet. | |
3461 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(7); | |
3462 // The first stream frame will have 2 fewer overhead bytes than the other six. | |
3463 const string payload(payload_length * 7 + 2, 'a'); | |
3464 EXPECT_EQ(payload.size(), | |
3465 connection_.SendStreamDataWithString(1, payload, 0, !kFin, nullptr) | |
3466 .bytes_consumed); | |
3467 } | |
3468 | |
3469 TEST_P(QuicConnectionTest, LoopThroughSendingPacketsWithTruncation) { | |
3470 // Set up a larger payload than will fit in one packet. | |
3471 const string payload(connection_.max_packet_length(), 'a'); | |
3472 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)).Times(AnyNumber()); | |
3473 | |
3474 // Now send some packets with no truncation. | |
3475 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
3476 EXPECT_EQ(payload.size(), | |
3477 connection_.SendStreamDataWithString(3, payload, 0, !kFin, nullptr) | |
3478 .bytes_consumed); | |
3479 // Track the size of the second packet here. The overhead will be the largest | |
3480 // we see in this test, due to the non-truncated connection id. | |
3481 size_t non_truncated_packet_size = writer_->last_packet_size(); | |
3482 | |
3483 // Change to a 0 byte connection id. | |
3484 QuicConfig config; | |
3485 QuicConfigPeer::SetReceivedBytesForConnectionId(&config, 0); | |
3486 connection_.SetFromConfig(config); | |
3487 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2); | |
3488 EXPECT_EQ(payload.size(), | |
3489 connection_.SendStreamDataWithString(3, payload, 0, !kFin, nullptr) | |
3490 .bytes_consumed); | |
3491 // Just like above, we save 8 bytes on payload, and 8 on truncation. | |
3492 EXPECT_EQ(non_truncated_packet_size, writer_->last_packet_size() + 8 * 2); | |
3493 } | |
3494 | |
3495 TEST_P(QuicConnectionTest, SendDelayedAck) { | |
3496 QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); | |
3497 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3498 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3499 const uint8_t tag = 0x07; | |
3500 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3501 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3502 // Process a packet from the non-crypto stream. | |
3503 frame1_.stream_id = 3; | |
3504 | |
3505 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3506 // instead of ENCRYPTION_NONE. | |
3507 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3508 ProcessDataPacketAtLevel(kDefaultPathId, 1, !kEntropyFlag, !kHasStopWaiting, | |
3509 ENCRYPTION_INITIAL); | |
3510 | |
3511 // Check if delayed ack timer is running for the expected interval. | |
3512 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3513 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3514 // Simulate delayed ack alarm firing. | |
3515 connection_.GetAckAlarm()->Fire(); | |
3516 // Check that ack is sent and that delayed ack alarm is reset. | |
3517 EXPECT_EQ(2u, writer_->frame_count()); | |
3518 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3519 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3520 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3521 } | |
3522 | |
3523 TEST_P(QuicConnectionTest, SendDelayedAckDecimation) { | |
3524 QuicConnectionPeer::SetAckMode(&connection_, QuicConnection::ACK_DECIMATION); | |
3525 | |
3526 const size_t kMinRttMs = 40; | |
3527 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3528 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3529 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3530 // The ack time should be based on min_rtt/4, since it's less than the | |
3531 // default delayed ack time. | |
3532 QuicTime ack_time = clock_.ApproximateNow() + | |
3533 QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); | |
3534 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3535 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3536 const uint8_t tag = 0x07; | |
3537 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3538 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3539 // Process a packet from the non-crypto stream. | |
3540 frame1_.stream_id = 3; | |
3541 | |
3542 // Process all the initial packets in order so there aren't missing packets. | |
3543 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3544 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3545 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3546 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3547 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3548 } | |
3549 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3550 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3551 // instead of ENCRYPTION_NONE. | |
3552 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3553 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3554 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3555 | |
3556 // Check if delayed ack timer is running for the expected interval. | |
3557 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3558 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3559 | |
3560 // The 10th received packet causes an ack to be sent. | |
3561 for (int i = 0; i < 9; ++i) { | |
3562 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3563 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3564 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3565 !kEntropyFlag, !kHasStopWaiting, | |
3566 ENCRYPTION_INITIAL); | |
3567 } | |
3568 // Check that ack is sent and that delayed ack alarm is reset. | |
3569 EXPECT_EQ(2u, writer_->frame_count()); | |
3570 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3571 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3572 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3573 } | |
3574 | |
3575 TEST_P(QuicConnectionTest, SendDelayedAckDecimationEighthRtt) { | |
3576 QuicConnectionPeer::SetAckMode(&connection_, QuicConnection::ACK_DECIMATION); | |
3577 QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); | |
3578 | |
3579 const size_t kMinRttMs = 40; | |
3580 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3581 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3582 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3583 // The ack time should be based on min_rtt/8, since it's less than the | |
3584 // default delayed ack time. | |
3585 QuicTime ack_time = clock_.ApproximateNow() + | |
3586 QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); | |
3587 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3588 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3589 const uint8_t tag = 0x07; | |
3590 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3591 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3592 // Process a packet from the non-crypto stream. | |
3593 frame1_.stream_id = 3; | |
3594 | |
3595 // Process all the initial packets in order so there aren't missing packets. | |
3596 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3597 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3598 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3599 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3600 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3601 } | |
3602 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3603 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3604 // instead of ENCRYPTION_NONE. | |
3605 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3606 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3607 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3608 | |
3609 // Check if delayed ack timer is running for the expected interval. | |
3610 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3611 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3612 | |
3613 // The 10th received packet causes an ack to be sent. | |
3614 for (int i = 0; i < 9; ++i) { | |
3615 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3616 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3617 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3618 !kEntropyFlag, !kHasStopWaiting, | |
3619 ENCRYPTION_INITIAL); | |
3620 } | |
3621 // Check that ack is sent and that delayed ack alarm is reset. | |
3622 EXPECT_EQ(2u, writer_->frame_count()); | |
3623 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3624 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3625 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3626 } | |
3627 | |
3628 TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithReordering) { | |
3629 QuicConnectionPeer::SetAckMode( | |
3630 &connection_, QuicConnection::ACK_DECIMATION_WITH_REORDERING); | |
3631 | |
3632 const size_t kMinRttMs = 40; | |
3633 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3634 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3635 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3636 // The ack time should be based on min_rtt/4, since it's less than the | |
3637 // default delayed ack time. | |
3638 QuicTime ack_time = clock_.ApproximateNow() + | |
3639 QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); | |
3640 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3641 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3642 const uint8_t tag = 0x07; | |
3643 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3644 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3645 // Process a packet from the non-crypto stream. | |
3646 frame1_.stream_id = 3; | |
3647 | |
3648 // Process all the initial packets in order so there aren't missing packets. | |
3649 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3650 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3651 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3652 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3653 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3654 } | |
3655 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3656 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3657 // instead of ENCRYPTION_NONE. | |
3658 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3659 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3660 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3661 | |
3662 // Check if delayed ack timer is running for the expected interval. | |
3663 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3664 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3665 | |
3666 // Process packet 10 first and ensure the alarm is one eighth min_rtt. | |
3667 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3668 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 9, | |
3669 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3670 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); | |
3671 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3672 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3673 | |
3674 // The 10th received packet causes an ack to be sent. | |
3675 for (int i = 0; i < 8; ++i) { | |
3676 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3677 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3678 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3679 !kEntropyFlag, !kHasStopWaiting, | |
3680 ENCRYPTION_INITIAL); | |
3681 } | |
3682 // Check that ack is sent and that delayed ack alarm is reset. | |
3683 EXPECT_EQ(2u, writer_->frame_count()); | |
3684 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3685 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3686 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3687 } | |
3688 | |
3689 TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithLargeReordering) { | |
3690 QuicConnectionPeer::SetAckMode( | |
3691 &connection_, QuicConnection::ACK_DECIMATION_WITH_REORDERING); | |
3692 | |
3693 const size_t kMinRttMs = 40; | |
3694 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3695 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3696 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3697 // The ack time should be based on min_rtt/4, since it's less than the | |
3698 // default delayed ack time. | |
3699 QuicTime ack_time = clock_.ApproximateNow() + | |
3700 QuicTime::Delta::FromMilliseconds(kMinRttMs / 4); | |
3701 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3702 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3703 const uint8_t tag = 0x07; | |
3704 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3705 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3706 // Process a packet from the non-crypto stream. | |
3707 frame1_.stream_id = 3; | |
3708 | |
3709 // Process all the initial packets in order so there aren't missing packets. | |
3710 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3711 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3712 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3713 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3714 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3715 } | |
3716 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3717 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3718 // instead of ENCRYPTION_NONE. | |
3719 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3720 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3721 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3722 | |
3723 // Check if delayed ack timer is running for the expected interval. | |
3724 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3725 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3726 | |
3727 // Process packet 10 first and ensure the alarm is one eighth min_rtt. | |
3728 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3729 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 19, | |
3730 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3731 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); | |
3732 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3733 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3734 | |
3735 // The 10th received packet causes an ack to be sent. | |
3736 for (int i = 0; i < 8; ++i) { | |
3737 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3738 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3739 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3740 !kEntropyFlag, !kHasStopWaiting, | |
3741 ENCRYPTION_INITIAL); | |
3742 } | |
3743 // Check that ack is sent and that delayed ack alarm is reset. | |
3744 EXPECT_EQ(2u, writer_->frame_count()); | |
3745 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3746 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3747 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3748 | |
3749 // The next packet received in order will cause an immediate ack, | |
3750 // because it fills a hole. | |
3751 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3752 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3753 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 10, | |
3754 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3755 // Check that ack is sent and that delayed ack alarm is reset. | |
3756 EXPECT_EQ(2u, writer_->frame_count()); | |
3757 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3758 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3759 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3760 } | |
3761 | |
3762 TEST_P(QuicConnectionTest, SendDelayedAckDecimationWithReorderingEighthRtt) { | |
3763 QuicConnectionPeer::SetAckMode( | |
3764 &connection_, QuicConnection::ACK_DECIMATION_WITH_REORDERING); | |
3765 QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); | |
3766 | |
3767 const size_t kMinRttMs = 40; | |
3768 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3769 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3770 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3771 // The ack time should be based on min_rtt/8, since it's less than the | |
3772 // default delayed ack time. | |
3773 QuicTime ack_time = clock_.ApproximateNow() + | |
3774 QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); | |
3775 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3776 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3777 const uint8_t tag = 0x07; | |
3778 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3779 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3780 // Process a packet from the non-crypto stream. | |
3781 frame1_.stream_id = 3; | |
3782 | |
3783 // Process all the initial packets in order so there aren't missing packets. | |
3784 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3785 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3786 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3787 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3788 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3789 } | |
3790 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3791 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3792 // instead of ENCRYPTION_NONE. | |
3793 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3794 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3795 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3796 | |
3797 // Check if delayed ack timer is running for the expected interval. | |
3798 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3799 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3800 | |
3801 // Process packet 10 first and ensure the alarm is one eighth min_rtt. | |
3802 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3803 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 9, | |
3804 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3805 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); | |
3806 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3807 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3808 | |
3809 // The 10th received packet causes an ack to be sent. | |
3810 for (int i = 0; i < 8; ++i) { | |
3811 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3812 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3813 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3814 !kEntropyFlag, !kHasStopWaiting, | |
3815 ENCRYPTION_INITIAL); | |
3816 } | |
3817 // Check that ack is sent and that delayed ack alarm is reset. | |
3818 EXPECT_EQ(2u, writer_->frame_count()); | |
3819 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3820 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3821 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3822 } | |
3823 | |
3824 TEST_P(QuicConnectionTest, | |
3825 SendDelayedAckDecimationWithLargeReorderingEighthRtt) { | |
3826 QuicConnectionPeer::SetAckMode( | |
3827 &connection_, QuicConnection::ACK_DECIMATION_WITH_REORDERING); | |
3828 QuicConnectionPeer::SetAckDecimationDelay(&connection_, 0.125); | |
3829 | |
3830 const size_t kMinRttMs = 40; | |
3831 RttStats* rtt_stats = const_cast<RttStats*>(manager_->GetRttStats()); | |
3832 rtt_stats->UpdateRtt(QuicTime::Delta::FromMilliseconds(kMinRttMs), | |
3833 QuicTime::Delta::Zero(), QuicTime::Zero()); | |
3834 // The ack time should be based on min_rtt/8, since it's less than the | |
3835 // default delayed ack time. | |
3836 QuicTime ack_time = clock_.ApproximateNow() + | |
3837 QuicTime::Delta::FromMilliseconds(kMinRttMs / 8); | |
3838 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3839 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3840 const uint8_t tag = 0x07; | |
3841 connection_.SetDecrypter(ENCRYPTION_INITIAL, new StrictTaggingDecrypter(tag)); | |
3842 framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag)); | |
3843 // Process a packet from the non-crypto stream. | |
3844 frame1_.stream_id = 3; | |
3845 | |
3846 // Process all the initial packets in order so there aren't missing packets. | |
3847 QuicPacketNumber kFirstDecimatedPacket = 101; | |
3848 for (unsigned int i = 0; i < kFirstDecimatedPacket - 1; ++i) { | |
3849 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3850 ProcessDataPacketAtLevel(kDefaultPathId, 1 + i, !kEntropyFlag, | |
3851 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3852 } | |
3853 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3854 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used | |
3855 // instead of ENCRYPTION_NONE. | |
3856 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3857 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket, !kEntropyFlag, | |
3858 !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3859 | |
3860 // Check if delayed ack timer is running for the expected interval. | |
3861 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3862 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3863 | |
3864 // Process packet 10 first and ensure the alarm is one eighth min_rtt. | |
3865 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3866 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 19, | |
3867 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3868 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5); | |
3869 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3870 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3871 | |
3872 // The 10th received packet causes an ack to be sent. | |
3873 for (int i = 0; i < 8; ++i) { | |
3874 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3875 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3876 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 1 + i, | |
3877 !kEntropyFlag, !kHasStopWaiting, | |
3878 ENCRYPTION_INITIAL); | |
3879 } | |
3880 // Check that ack is sent and that delayed ack alarm is reset. | |
3881 EXPECT_EQ(2u, writer_->frame_count()); | |
3882 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3883 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3884 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3885 | |
3886 // The next packet received in order will cause an immediate ack, | |
3887 // because it fills a hole. | |
3888 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3889 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
3890 ProcessDataPacketAtLevel(kDefaultPathId, kFirstDecimatedPacket + 10, | |
3891 !kEntropyFlag, !kHasStopWaiting, ENCRYPTION_INITIAL); | |
3892 // Check that ack is sent and that delayed ack alarm is reset. | |
3893 EXPECT_EQ(2u, writer_->frame_count()); | |
3894 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3895 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3896 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3897 } | |
3898 | |
3899 TEST_P(QuicConnectionTest, SendDelayedAckOnHandshakeConfirmed) { | |
3900 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3901 ProcessPacket(kDefaultPathId, 1); | |
3902 // Check that ack is sent and that delayed ack alarm is set. | |
3903 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3904 QuicTime ack_time = clock_.ApproximateNow() + DefaultDelayedAckTime(); | |
3905 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3906 | |
3907 // Completing the handshake as the server does nothing. | |
3908 QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_SERVER); | |
3909 connection_.OnHandshakeComplete(); | |
3910 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3911 EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline()); | |
3912 | |
3913 // Complete the handshake as the client decreases the delayed ack time to 0ms. | |
3914 QuicConnectionPeer::SetPerspective(&connection_, Perspective::IS_CLIENT); | |
3915 connection_.OnHandshakeComplete(); | |
3916 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3917 EXPECT_EQ(clock_.ApproximateNow(), connection_.GetAckAlarm()->deadline()); | |
3918 } | |
3919 | |
3920 TEST_P(QuicConnectionTest, SendDelayedAckOnSecondPacket) { | |
3921 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3922 ProcessPacket(kDefaultPathId, 1); | |
3923 ProcessPacket(kDefaultPathId, 2); | |
3924 // Check that ack is sent and that delayed ack alarm is reset. | |
3925 EXPECT_EQ(2u, writer_->frame_count()); | |
3926 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3927 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3928 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3929 } | |
3930 | |
3931 TEST_P(QuicConnectionTest, NoAckOnOldNacks) { | |
3932 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3933 // Drop one packet, triggering a sequence of acks. | |
3934 ProcessPacket(kDefaultPathId, 2); | |
3935 size_t frames_per_ack = 2; | |
3936 EXPECT_EQ(frames_per_ack, writer_->frame_count()); | |
3937 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3938 writer_->Reset(); | |
3939 ProcessPacket(kDefaultPathId, 3); | |
3940 EXPECT_EQ(frames_per_ack, writer_->frame_count()); | |
3941 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3942 writer_->Reset(); | |
3943 ProcessPacket(kDefaultPathId, 4); | |
3944 EXPECT_EQ(frames_per_ack, writer_->frame_count()); | |
3945 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3946 writer_->Reset(); | |
3947 ProcessPacket(kDefaultPathId, 5); | |
3948 EXPECT_EQ(frames_per_ack, writer_->frame_count()); | |
3949 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3950 writer_->Reset(); | |
3951 // Now only set the timer on the 6th packet, instead of sending another ack. | |
3952 ProcessPacket(kDefaultPathId, 6); | |
3953 EXPECT_EQ(0u, writer_->frame_count()); | |
3954 EXPECT_TRUE(connection_.GetAckAlarm()->IsSet()); | |
3955 } | |
3956 | |
3957 TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) { | |
3958 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3959 ProcessPacket(kDefaultPathId, 1); | |
3960 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
3961 nullptr); | |
3962 // Check that ack is bundled with outgoing data and that delayed ack | |
3963 // alarm is reset. | |
3964 EXPECT_EQ(3u, writer_->frame_count()); | |
3965 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
3966 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3967 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3968 } | |
3969 | |
3970 TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingCryptoPacket) { | |
3971 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3972 ProcessPacket(kDefaultPathId, 1); | |
3973 connection_.SendStreamDataWithString(kCryptoStreamId, "foo", 0, !kFin, | |
3974 nullptr); | |
3975 // Check that ack is bundled with outgoing crypto data. | |
3976 EXPECT_EQ(3u, writer_->frame_count()); | |
3977 EXPECT_FALSE(writer_->ack_frames().empty()); | |
3978 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3979 } | |
3980 | |
3981 TEST_P(QuicConnectionTest, BlockAndBufferOnFirstCHLOPacketOfTwo) { | |
3982 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3983 ProcessPacket(kDefaultPathId, 1); | |
3984 BlockOnNextWrite(); | |
3985 writer_->set_is_write_blocked_data_buffered(true); | |
3986 connection_.SendStreamDataWithString(kCryptoStreamId, "foo", 0, !kFin, | |
3987 nullptr); | |
3988 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
3989 EXPECT_FALSE(connection_.HasQueuedData()); | |
3990 connection_.SendStreamDataWithString(kCryptoStreamId, "bar", 3, !kFin, | |
3991 nullptr); | |
3992 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
3993 EXPECT_TRUE(connection_.HasQueuedData()); | |
3994 } | |
3995 | |
3996 TEST_P(QuicConnectionTest, BundleAckForSecondCHLO) { | |
3997 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
3998 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
3999 EXPECT_CALL(visitor_, OnCanWrite()) | |
4000 .WillOnce(IgnoreResult(InvokeWithoutArgs( | |
4001 &connection_, &TestConnection::SendCryptoStreamData))); | |
4002 // Process a packet from the crypto stream, which is frame1_'s default. | |
4003 // Receiving the CHLO as packet 2 first will cause the connection to | |
4004 // immediately send an ack, due to the packet gap. | |
4005 ProcessPacket(kDefaultPathId, 2); | |
4006 // Check that ack is sent and that delayed ack alarm is reset. | |
4007 EXPECT_EQ(3u, writer_->frame_count()); | |
4008 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
4009 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
4010 EXPECT_FALSE(writer_->ack_frames().empty()); | |
4011 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
4012 } | |
4013 | |
4014 TEST_P(QuicConnectionTest, BundleAckWithDataOnIncomingAck) { | |
4015 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4016 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
4017 nullptr); | |
4018 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 3, !kFin, | |
4019 nullptr); | |
4020 // Ack the second packet, which will retransmit the first packet. | |
4021 QuicAckFrame ack = InitAckFrame(2); | |
4022 NackPacket(1, &ack); | |
4023 SendAlgorithmInterface::CongestionVector lost_packets; | |
4024 lost_packets.push_back(std::make_pair(1, kMaxPacketSize)); | |
4025 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
4026 .WillOnce(SetArgPointee<4>(lost_packets)); | |
4027 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4028 ProcessAckPacket(&ack); | |
4029 EXPECT_EQ(1u, writer_->frame_count()); | |
4030 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
4031 writer_->Reset(); | |
4032 | |
4033 // Now ack the retransmission, which will both raise the high water mark | |
4034 // and see if there is more data to send. | |
4035 ack = InitAckFrame(3); | |
4036 NackPacket(1, &ack); | |
4037 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4038 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4039 ProcessAckPacket(&ack); | |
4040 | |
4041 // Check that no packet is sent and the ack alarm isn't set. | |
4042 EXPECT_EQ(0u, writer_->frame_count()); | |
4043 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
4044 writer_->Reset(); | |
4045 | |
4046 // Send the same ack, but send both data and an ack together. | |
4047 ack = InitAckFrame(3); | |
4048 NackPacket(1, &ack); | |
4049 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4050 EXPECT_CALL(visitor_, OnCanWrite()) | |
4051 .WillOnce(IgnoreResult(InvokeWithoutArgs( | |
4052 &connection_, &TestConnection::EnsureWritableAndSendStreamData5))); | |
4053 ProcessAckPacket(&ack); | |
4054 | |
4055 // Check that ack is bundled with outgoing data and the delayed ack | |
4056 // alarm is reset. | |
4057 EXPECT_EQ(3u, writer_->frame_count()); | |
4058 EXPECT_FALSE(writer_->stop_waiting_frames().empty()); | |
4059 EXPECT_FALSE(writer_->ack_frames().empty()); | |
4060 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
4061 EXPECT_FALSE(connection_.GetAckAlarm()->IsSet()); | |
4062 } | |
4063 | |
4064 TEST_P(QuicConnectionTest, NoAckSentForClose) { | |
4065 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4066 ProcessPacket(kDefaultPathId, 1); | |
4067 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, _, | |
4068 ConnectionCloseSource::FROM_PEER)); | |
4069 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
4070 ProcessClosePacket(kDefaultPathId, 2); | |
4071 } | |
4072 | |
4073 TEST_P(QuicConnectionTest, SendWhenDisconnected) { | |
4074 EXPECT_TRUE(connection_.connected()); | |
4075 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, _, | |
4076 ConnectionCloseSource::FROM_SELF)); | |
4077 connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason", | |
4078 ConnectionCloseBehavior::SILENT_CLOSE); | |
4079 EXPECT_FALSE(connection_.connected()); | |
4080 EXPECT_FALSE(connection_.CanWriteStreamData()); | |
4081 QuicPacket* packet = | |
4082 ConstructDataPacket(kDefaultPathId, 1, !kEntropyFlag, !kHasStopWaiting); | |
4083 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _)).Times(0); | |
4084 connection_.SendPacket(ENCRYPTION_NONE, kDefaultPathId, 1, packet, | |
4085 kTestEntropyHash, HAS_RETRANSMITTABLE_DATA, false, | |
4086 false); | |
4087 } | |
4088 | |
4089 TEST_P(QuicConnectionTest, PublicReset) { | |
4090 QuicPublicResetPacket header; | |
4091 header.public_header.connection_id = connection_id_; | |
4092 header.public_header.reset_flag = true; | |
4093 header.public_header.version_flag = false; | |
4094 header.rejected_packet_number = 10101; | |
4095 std::unique_ptr<QuicEncryptedPacket> packet( | |
4096 framer_.BuildPublicResetPacket(header)); | |
4097 std::unique_ptr<QuicReceivedPacket> received( | |
4098 ConstructReceivedPacket(*packet, QuicTime::Zero())); | |
4099 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PUBLIC_RESET, _, | |
4100 ConnectionCloseSource::FROM_PEER)); | |
4101 connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); | |
4102 } | |
4103 | |
4104 TEST_P(QuicConnectionTest, GoAway) { | |
4105 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4106 | |
4107 QuicGoAwayFrame goaway; | |
4108 goaway.last_good_stream_id = 1; | |
4109 goaway.error_code = QUIC_PEER_GOING_AWAY; | |
4110 goaway.reason_phrase = "Going away."; | |
4111 EXPECT_CALL(visitor_, OnGoAway(_)); | |
4112 ProcessGoAwayPacket(&goaway); | |
4113 } | |
4114 | |
4115 TEST_P(QuicConnectionTest, WindowUpdate) { | |
4116 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4117 | |
4118 QuicWindowUpdateFrame window_update; | |
4119 window_update.stream_id = 3; | |
4120 window_update.byte_offset = 1234; | |
4121 EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); | |
4122 ProcessFramePacket(QuicFrame(&window_update)); | |
4123 } | |
4124 | |
4125 TEST_P(QuicConnectionTest, Blocked) { | |
4126 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4127 | |
4128 QuicBlockedFrame blocked; | |
4129 blocked.stream_id = 3; | |
4130 EXPECT_CALL(visitor_, OnBlockedFrame(_)); | |
4131 ProcessFramePacket(QuicFrame(&blocked)); | |
4132 } | |
4133 | |
4134 TEST_P(QuicConnectionTest, PathClose) { | |
4135 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4136 | |
4137 QuicPathCloseFrame path_close = QuicPathCloseFrame(1); | |
4138 ProcessPathClosePacket(&path_close); | |
4139 EXPECT_TRUE(QuicFramerPeer::IsPathClosed( | |
4140 QuicConnectionPeer::GetFramer(&connection_), 1)); | |
4141 } | |
4142 | |
4143 TEST_P(QuicConnectionTest, ZeroBytePacket) { | |
4144 // Don't close the connection for zero byte packets. | |
4145 EXPECT_CALL(visitor_, OnConnectionClosed(_, _, _)).Times(0); | |
4146 QuicReceivedPacket encrypted(nullptr, 0, QuicTime::Zero()); | |
4147 connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, encrypted); | |
4148 } | |
4149 | |
4150 TEST_P(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) { | |
4151 // Set the packet number of the ack packet to be least unacked (4). | |
4152 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 3); | |
4153 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4154 QuicStopWaitingFrame frame = InitStopWaitingFrame(4); | |
4155 ProcessStopWaitingPacket(&frame); | |
4156 if (outgoing_ack()->missing) { | |
4157 EXPECT_TRUE(outgoing_ack()->packets.Empty()); | |
4158 } else { | |
4159 EXPECT_FALSE(outgoing_ack()->packets.Empty()); | |
4160 } | |
4161 } | |
4162 | |
4163 TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculation) { | |
4164 if (GetParam().version > QUIC_VERSION_33) { | |
4165 return; | |
4166 } | |
4167 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AtLeast(1)); | |
4168 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4169 ProcessDataPacket(kDefaultPathId, 1, kEntropyFlag); | |
4170 ProcessDataPacket(kDefaultPathId, 4, kEntropyFlag); | |
4171 ProcessDataPacket(kDefaultPathId, 3, !kEntropyFlag); | |
4172 ProcessDataPacket(kDefaultPathId, 7, kEntropyFlag); | |
4173 EXPECT_EQ(146u, outgoing_ack()->entropy_hash); | |
4174 } | |
4175 | |
4176 TEST_P(QuicConnectionTest, UpdateEntropyForReceivedPackets) { | |
4177 if (GetParam().version > QUIC_VERSION_33) { | |
4178 return; | |
4179 } | |
4180 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AtLeast(1)); | |
4181 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4182 ProcessDataPacket(kDefaultPathId, 1, kEntropyFlag); | |
4183 ProcessDataPacket(kDefaultPathId, 5, kEntropyFlag); | |
4184 ProcessDataPacket(kDefaultPathId, 4, !kEntropyFlag); | |
4185 EXPECT_EQ(34u, outgoing_ack()->entropy_hash); | |
4186 // Make 4th packet my least unacked, and update entropy for 2, 3 packets. | |
4187 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 5); | |
4188 QuicPacketEntropyHash six_packet_entropy_hash = 0; | |
4189 QuicPacketEntropyHash random_entropy_hash = 129u; | |
4190 QuicStopWaitingFrame frame = InitStopWaitingFrame(4); | |
4191 frame.entropy_hash = random_entropy_hash; | |
4192 if (ProcessStopWaitingPacket(&frame)) { | |
4193 six_packet_entropy_hash = 1 << 6; | |
4194 } | |
4195 | |
4196 EXPECT_EQ((random_entropy_hash + (1 << 5) + six_packet_entropy_hash), | |
4197 outgoing_ack()->entropy_hash); | |
4198 } | |
4199 | |
4200 TEST_P(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) { | |
4201 if (GetParam().version > QUIC_VERSION_33) { | |
4202 return; | |
4203 } | |
4204 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AtLeast(1)); | |
4205 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4206 ProcessDataPacket(kDefaultPathId, 1, kEntropyFlag); | |
4207 ProcessDataPacket(kDefaultPathId, 5, !kEntropyFlag); | |
4208 ProcessDataPacket(kDefaultPathId, 22, kEntropyFlag); | |
4209 EXPECT_EQ(66u, outgoing_ack()->entropy_hash); | |
4210 QuicPacketCreatorPeer::SetPacketNumber(&peer_creator_, 22); | |
4211 QuicPacketEntropyHash random_entropy_hash = 85u; | |
4212 // Current packet is the least unacked packet. | |
4213 QuicPacketEntropyHash ack_entropy_hash; | |
4214 QuicStopWaitingFrame frame = InitStopWaitingFrame(23); | |
4215 frame.entropy_hash = random_entropy_hash; | |
4216 ack_entropy_hash = ProcessStopWaitingPacket(&frame); | |
4217 EXPECT_EQ((random_entropy_hash + ack_entropy_hash), | |
4218 outgoing_ack()->entropy_hash); | |
4219 ProcessDataPacket(kDefaultPathId, 25, kEntropyFlag); | |
4220 EXPECT_EQ((random_entropy_hash + ack_entropy_hash + (1 << (25 % 8))), | |
4221 outgoing_ack()->entropy_hash); | |
4222 } | |
4223 | |
4224 TEST_P(QuicConnectionTest, EntropyCalculationForTruncatedAck) { | |
4225 if (GetParam().version > QUIC_VERSION_33) { | |
4226 return; | |
4227 } | |
4228 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(AtLeast(1)); | |
4229 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4230 QuicPacketEntropyHash entropy[51]; | |
4231 entropy[0] = 0; | |
4232 for (int i = 1; i < 51; ++i) { | |
4233 bool should_send = i % 10 != 1; | |
4234 bool entropy_flag = (i & (i - 1)) != 0; | |
4235 if (!should_send) { | |
4236 entropy[i] = entropy[i - 1]; | |
4237 continue; | |
4238 } | |
4239 if (entropy_flag) { | |
4240 entropy[i] = entropy[i - 1] ^ (1 << (i % 8)); | |
4241 } else { | |
4242 entropy[i] = entropy[i - 1]; | |
4243 } | |
4244 ProcessDataPacket(kDefaultPathId, i, entropy_flag); | |
4245 } | |
4246 for (int i = 1; i < 50; ++i) { | |
4247 EXPECT_EQ(entropy[i], | |
4248 QuicConnectionPeer::ReceivedEntropyHash(&connection_, i)); | |
4249 } | |
4250 } | |
4251 | |
4252 TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacket) { | |
4253 connection_.SetSupportedVersions(QuicSupportedVersions()); | |
4254 set_perspective(Perspective::IS_SERVER); | |
4255 peer_framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); | |
4256 | |
4257 QuicPacketHeader header; | |
4258 header.public_header.connection_id = connection_id_; | |
4259 header.public_header.version_flag = true; | |
4260 header.path_id = kDefaultPathId; | |
4261 header.packet_number = 12; | |
4262 | |
4263 QuicFrames frames; | |
4264 frames.push_back(QuicFrame(&frame1_)); | |
4265 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
4266 char buffer[kMaxPacketSize]; | |
4267 size_t encrypted_length = framer_.EncryptPayload( | |
4268 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
4269 | |
4270 framer_.set_version(version()); | |
4271 connection_.ProcessUdpPacket( | |
4272 kSelfAddress, kPeerAddress, | |
4273 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
4274 EXPECT_TRUE(writer_->version_negotiation_packet() != nullptr); | |
4275 | |
4276 size_t num_versions = arraysize(kSupportedQuicVersions); | |
4277 ASSERT_EQ(num_versions, | |
4278 writer_->version_negotiation_packet()->versions.size()); | |
4279 | |
4280 // We expect all versions in kSupportedQuicVersions to be | |
4281 // included in the packet. | |
4282 for (size_t i = 0; i < num_versions; ++i) { | |
4283 EXPECT_EQ(kSupportedQuicVersions[i], | |
4284 writer_->version_negotiation_packet()->versions[i]); | |
4285 } | |
4286 } | |
4287 | |
4288 TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) { | |
4289 connection_.SetSupportedVersions(QuicSupportedVersions()); | |
4290 set_perspective(Perspective::IS_SERVER); | |
4291 peer_framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); | |
4292 | |
4293 QuicPacketHeader header; | |
4294 header.public_header.connection_id = connection_id_; | |
4295 header.public_header.version_flag = true; | |
4296 header.packet_number = 12; | |
4297 | |
4298 QuicFrames frames; | |
4299 frames.push_back(QuicFrame(&frame1_)); | |
4300 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
4301 char buffer[kMaxPacketSize]; | |
4302 size_t encrypted_length = framer_.EncryptPayload( | |
4303 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
4304 | |
4305 framer_.set_version(version()); | |
4306 BlockOnNextWrite(); | |
4307 connection_.ProcessUdpPacket( | |
4308 kSelfAddress, kPeerAddress, | |
4309 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
4310 EXPECT_EQ(0u, writer_->last_packet_size()); | |
4311 EXPECT_TRUE(connection_.HasQueuedData()); | |
4312 | |
4313 writer_->SetWritable(); | |
4314 connection_.OnCanWrite(); | |
4315 EXPECT_TRUE(writer_->version_negotiation_packet() != nullptr); | |
4316 | |
4317 size_t num_versions = arraysize(kSupportedQuicVersions); | |
4318 ASSERT_EQ(num_versions, | |
4319 writer_->version_negotiation_packet()->versions.size()); | |
4320 | |
4321 // We expect all versions in kSupportedQuicVersions to be | |
4322 // included in the packet. | |
4323 for (size_t i = 0; i < num_versions; ++i) { | |
4324 EXPECT_EQ(kSupportedQuicVersions[i], | |
4325 writer_->version_negotiation_packet()->versions[i]); | |
4326 } | |
4327 } | |
4328 | |
4329 TEST_P(QuicConnectionTest, | |
4330 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered) { | |
4331 connection_.SetSupportedVersions(QuicSupportedVersions()); | |
4332 set_perspective(Perspective::IS_SERVER); | |
4333 peer_framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED); | |
4334 | |
4335 QuicPacketHeader header; | |
4336 header.public_header.connection_id = connection_id_; | |
4337 header.public_header.version_flag = true; | |
4338 header.packet_number = 12; | |
4339 | |
4340 QuicFrames frames; | |
4341 frames.push_back(QuicFrame(&frame1_)); | |
4342 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
4343 char buffer[kMaxPacketSize]; | |
4344 size_t encryped_length = framer_.EncryptPayload( | |
4345 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
4346 | |
4347 framer_.set_version(version()); | |
4348 set_perspective(Perspective::IS_SERVER); | |
4349 BlockOnNextWrite(); | |
4350 writer_->set_is_write_blocked_data_buffered(true); | |
4351 connection_.ProcessUdpPacket( | |
4352 kSelfAddress, kPeerAddress, | |
4353 QuicReceivedPacket(buffer, encryped_length, QuicTime::Zero(), false)); | |
4354 EXPECT_EQ(0u, writer_->last_packet_size()); | |
4355 EXPECT_FALSE(connection_.HasQueuedData()); | |
4356 } | |
4357 | |
4358 TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) { | |
4359 // Start out with some unsupported version. | |
4360 QuicConnectionPeer::GetFramer(&connection_) | |
4361 ->set_version_for_tests(QUIC_VERSION_UNSUPPORTED); | |
4362 | |
4363 // Send a version negotiation packet. | |
4364 std::unique_ptr<QuicEncryptedPacket> encrypted( | |
4365 framer_.BuildVersionNegotiationPacket(connection_id_, | |
4366 QuicSupportedVersions())); | |
4367 std::unique_ptr<QuicReceivedPacket> received( | |
4368 ConstructReceivedPacket(*encrypted, QuicTime::Zero())); | |
4369 connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); | |
4370 | |
4371 // Now force another packet. The connection should transition into | |
4372 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion. | |
4373 QuicPacketHeader header; | |
4374 header.public_header.connection_id = connection_id_; | |
4375 header.path_id = kDefaultPathId; | |
4376 header.packet_number = 12; | |
4377 header.public_header.version_flag = false; | |
4378 QuicFrames frames; | |
4379 frames.push_back(QuicFrame(&frame1_)); | |
4380 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
4381 char buffer[kMaxPacketSize]; | |
4382 size_t encrypted_length = framer_.EncryptPayload( | |
4383 ENCRYPTION_NONE, kDefaultPathId, 12, *packet, buffer, kMaxPacketSize); | |
4384 ASSERT_NE(0u, encrypted_length); | |
4385 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
4386 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4387 connection_.ProcessUdpPacket( | |
4388 kSelfAddress, kPeerAddress, | |
4389 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
4390 | |
4391 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(creator_)); | |
4392 } | |
4393 | |
4394 TEST_P(QuicConnectionTest, BadVersionNegotiation) { | |
4395 // Send a version negotiation packet with the version the client started with. | |
4396 // It should be rejected. | |
4397 EXPECT_CALL(visitor_, | |
4398 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, _, | |
4399 ConnectionCloseSource::FROM_SELF)); | |
4400 std::unique_ptr<QuicEncryptedPacket> encrypted( | |
4401 framer_.BuildVersionNegotiationPacket(connection_id_, | |
4402 QuicSupportedVersions())); | |
4403 std::unique_ptr<QuicReceivedPacket> received( | |
4404 ConstructReceivedPacket(*encrypted, QuicTime::Zero())); | |
4405 connection_.ProcessUdpPacket(kSelfAddress, kPeerAddress, *received); | |
4406 } | |
4407 | |
4408 TEST_P(QuicConnectionTest, CheckSendStats) { | |
4409 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
4410 | |
4411 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
4412 connection_.SendStreamDataWithString(3, "first", 0, !kFin, nullptr); | |
4413 size_t first_packet_size = writer_->last_packet_size(); | |
4414 | |
4415 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
4416 connection_.SendStreamDataWithString(5, "second", 0, !kFin, nullptr); | |
4417 size_t second_packet_size = writer_->last_packet_size(); | |
4418 | |
4419 // 2 retransmissions due to rto, 1 due to explicit nack. | |
4420 EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true)); | |
4421 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3); | |
4422 | |
4423 // Retransmit due to RTO. | |
4424 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10)); | |
4425 connection_.GetRetransmissionAlarm()->Fire(); | |
4426 | |
4427 // Retransmit due to explicit nacks. | |
4428 QuicAckFrame nack_three = InitAckFrame(4); | |
4429 NackPacket(3, &nack_three); | |
4430 NackPacket(1, &nack_three); | |
4431 SendAlgorithmInterface::CongestionVector lost_packets; | |
4432 lost_packets.push_back(std::make_pair(1, kMaxPacketSize)); | |
4433 lost_packets.push_back(std::make_pair(3, kMaxPacketSize)); | |
4434 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
4435 .WillOnce(SetArgPointee<4>(lost_packets)); | |
4436 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4437 EXPECT_CALL(visitor_, OnCanWrite()); | |
4438 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4439 ProcessAckPacket(&nack_three); | |
4440 | |
4441 EXPECT_CALL(*send_algorithm_, BandwidthEstimate()) | |
4442 .WillOnce(Return(QuicBandwidth::Zero())); | |
4443 | |
4444 const QuicConnectionStats& stats = connection_.GetStats(); | |
4445 EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size - kQuicVersionSize, | |
4446 stats.bytes_sent); | |
4447 EXPECT_EQ(5u, stats.packets_sent); | |
4448 EXPECT_EQ(2 * first_packet_size + second_packet_size - kQuicVersionSize, | |
4449 stats.bytes_retransmitted); | |
4450 EXPECT_EQ(3u, stats.packets_retransmitted); | |
4451 EXPECT_EQ(1u, stats.rto_count); | |
4452 EXPECT_EQ(kDefaultMaxPacketSize, stats.max_packet_size); | |
4453 } | |
4454 | |
4455 TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) { | |
4456 // Construct a packet with stream frame and connection close frame. | |
4457 QuicPacketHeader header; | |
4458 header.public_header.connection_id = connection_id_; | |
4459 header.packet_number = 1; | |
4460 header.public_header.version_flag = false; | |
4461 | |
4462 QuicConnectionCloseFrame qccf; | |
4463 qccf.error_code = QUIC_PEER_GOING_AWAY; | |
4464 | |
4465 QuicFrames frames; | |
4466 frames.push_back(QuicFrame(&frame1_)); | |
4467 frames.push_back(QuicFrame(&qccf)); | |
4468 std::unique_ptr<QuicPacket> packet(ConstructPacket(header, frames)); | |
4469 EXPECT_TRUE(nullptr != packet.get()); | |
4470 char buffer[kMaxPacketSize]; | |
4471 size_t encrypted_length = framer_.EncryptPayload( | |
4472 ENCRYPTION_NONE, kDefaultPathId, 1, *packet, buffer, kMaxPacketSize); | |
4473 | |
4474 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, _, | |
4475 ConnectionCloseSource::FROM_PEER)); | |
4476 EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1); | |
4477 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4478 | |
4479 connection_.ProcessUdpPacket( | |
4480 kSelfAddress, kPeerAddress, | |
4481 QuicReceivedPacket(buffer, encrypted_length, QuicTime::Zero(), false)); | |
4482 } | |
4483 | |
4484 TEST_P(QuicConnectionTest, SelectMutualVersion) { | |
4485 connection_.SetSupportedVersions(QuicSupportedVersions()); | |
4486 // Set the connection to speak the lowest quic version. | |
4487 connection_.set_version(QuicVersionMin()); | |
4488 EXPECT_EQ(QuicVersionMin(), connection_.version()); | |
4489 | |
4490 // Pass in available versions which includes a higher mutually supported | |
4491 // version. The higher mutually supported version should be selected. | |
4492 QuicVersionVector supported_versions; | |
4493 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { | |
4494 supported_versions.push_back(kSupportedQuicVersions[i]); | |
4495 } | |
4496 EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions)); | |
4497 EXPECT_EQ(QuicVersionMax(), connection_.version()); | |
4498 | |
4499 // Expect that the lowest version is selected. | |
4500 // Ensure the lowest supported version is less than the max, unless they're | |
4501 // the same. | |
4502 EXPECT_LE(QuicVersionMin(), QuicVersionMax()); | |
4503 QuicVersionVector lowest_version_vector; | |
4504 lowest_version_vector.push_back(QuicVersionMin()); | |
4505 EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector)); | |
4506 EXPECT_EQ(QuicVersionMin(), connection_.version()); | |
4507 | |
4508 // Shouldn't be able to find a mutually supported version. | |
4509 QuicVersionVector unsupported_version; | |
4510 unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED); | |
4511 EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version)); | |
4512 } | |
4513 | |
4514 TEST_P(QuicConnectionTest, ConnectionCloseWhenWritable) { | |
4515 EXPECT_FALSE(writer_->IsWriteBlocked()); | |
4516 | |
4517 // Send a packet. | |
4518 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
4519 EXPECT_EQ(0u, connection_.NumQueuedPackets()); | |
4520 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
4521 | |
4522 TriggerConnectionClose(); | |
4523 EXPECT_EQ(2u, writer_->packets_write_attempts()); | |
4524 } | |
4525 | |
4526 TEST_P(QuicConnectionTest, ConnectionCloseGettingWriteBlocked) { | |
4527 BlockOnNextWrite(); | |
4528 TriggerConnectionClose(); | |
4529 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
4530 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
4531 } | |
4532 | |
4533 TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) { | |
4534 BlockOnNextWrite(); | |
4535 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
4536 EXPECT_EQ(1u, connection_.NumQueuedPackets()); | |
4537 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
4538 EXPECT_TRUE(writer_->IsWriteBlocked()); | |
4539 TriggerConnectionClose(); | |
4540 EXPECT_EQ(1u, writer_->packets_write_attempts()); | |
4541 } | |
4542 | |
4543 TEST_P(QuicConnectionTest, AckNotifierTriggerCallback) { | |
4544 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4545 | |
4546 // Create a listener which we expect to be called. | |
4547 scoped_refptr<MockAckListener> listener(new MockAckListener); | |
4548 EXPECT_CALL(*listener, OnPacketAcked(_, _)).Times(1); | |
4549 | |
4550 // Send some data, which will register the listener to be notified. | |
4551 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, listener.get()); | |
4552 | |
4553 // Process an ACK from the server which should trigger the callback. | |
4554 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4555 QuicAckFrame frame = InitAckFrame(1); | |
4556 ProcessAckPacket(&frame); | |
4557 } | |
4558 | |
4559 TEST_P(QuicConnectionTest, AckNotifierFailToTriggerCallback) { | |
4560 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4561 | |
4562 // Create a listener which we don't expect to be called. | |
4563 scoped_refptr<MockAckListener> listener(new MockAckListener); | |
4564 EXPECT_CALL(*listener, OnPacketAcked(_, _)).Times(0); | |
4565 | |
4566 // Send some data, which will register the listener to be notified. This will | |
4567 // not be ACKed and so the listener should never be called. | |
4568 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, listener.get()); | |
4569 | |
4570 // Send some other data which we will ACK. | |
4571 connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr); | |
4572 connection_.SendStreamDataWithString(1, "bar", 0, !kFin, nullptr); | |
4573 | |
4574 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1 | |
4575 // which we registered to be notified about. | |
4576 QuicAckFrame frame = InitAckFrame(3); | |
4577 NackPacket(1, &frame); | |
4578 SendAlgorithmInterface::CongestionVector lost_packets; | |
4579 lost_packets.push_back(std::make_pair(1, kMaxPacketSize)); | |
4580 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
4581 .WillOnce(SetArgPointee<4>(lost_packets)); | |
4582 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4583 ProcessAckPacket(&frame); | |
4584 } | |
4585 | |
4586 TEST_P(QuicConnectionTest, AckNotifierCallbackAfterRetransmission) { | |
4587 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4588 | |
4589 // Create a listener which we expect to be called. | |
4590 scoped_refptr<MockAckListener> listener(new MockAckListener); | |
4591 EXPECT_CALL(*listener, OnPacketRetransmitted(3)).Times(1); | |
4592 EXPECT_CALL(*listener, OnPacketAcked(3, _)).Times(1); | |
4593 | |
4594 // Send four packets, and register to be notified on ACK of packet 2. | |
4595 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
4596 connection_.SendStreamDataWithString(3, "bar", 0, !kFin, listener.get()); | |
4597 connection_.SendStreamDataWithString(3, "baz", 0, !kFin, nullptr); | |
4598 connection_.SendStreamDataWithString(3, "qux", 0, !kFin, nullptr); | |
4599 | |
4600 // Now we receive ACK for packets 1, 3, and 4 and lose 2. | |
4601 QuicAckFrame frame = InitAckFrame(4); | |
4602 NackPacket(2, &frame); | |
4603 SendAlgorithmInterface::CongestionVector lost_packets; | |
4604 lost_packets.push_back(std::make_pair(2, kMaxPacketSize)); | |
4605 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
4606 .WillOnce(SetArgPointee<4>(lost_packets)); | |
4607 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4608 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
4609 ProcessAckPacket(&frame); | |
4610 | |
4611 // Now we get an ACK for packet 5 (retransmitted packet 2), which should | |
4612 // trigger the callback. | |
4613 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4614 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4615 QuicAckFrame second_ack_frame = InitAckFrame(5); | |
4616 ProcessAckPacket(&second_ack_frame); | |
4617 } | |
4618 | |
4619 // AckNotifierCallback is triggered by the ack of a packet that timed | |
4620 // out and was retransmitted, even though the retransmission has a | |
4621 // different packet number. | |
4622 TEST_P(QuicConnectionTest, AckNotifierCallbackForAckAfterRTO) { | |
4623 connection_.SetMaxTailLossProbes(kDefaultPathId, 0); | |
4624 | |
4625 // Create a listener which we expect to be called. | |
4626 scoped_refptr<MockAckListener> listener(new StrictMock<MockAckListener>); | |
4627 | |
4628 QuicTime default_retransmission_time = | |
4629 clock_.ApproximateNow() + DefaultRetransmissionTime(); | |
4630 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, listener.get()); | |
4631 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
4632 | |
4633 EXPECT_EQ(1u, writer_->header().packet_number); | |
4634 EXPECT_EQ(default_retransmission_time, | |
4635 connection_.GetRetransmissionAlarm()->deadline()); | |
4636 // Simulate the retransmission alarm firing. | |
4637 clock_.AdvanceTime(DefaultRetransmissionTime()); | |
4638 EXPECT_CALL(*listener, OnPacketRetransmitted(3)); | |
4639 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2u, _, _)); | |
4640 connection_.GetRetransmissionAlarm()->Fire(); | |
4641 EXPECT_EQ(2u, writer_->header().packet_number); | |
4642 // We do not raise the high water mark yet. | |
4643 EXPECT_EQ(1u, stop_waiting()->least_unacked); | |
4644 | |
4645 // Ack the original packet, which will revert the RTO. | |
4646 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4647 EXPECT_CALL(*listener, OnPacketAcked(3, _)); | |
4648 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4649 QuicAckFrame ack_frame = InitAckFrame(1); | |
4650 ProcessAckPacket(&ack_frame); | |
4651 | |
4652 // listener is not notified again when the retransmit is acked. | |
4653 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4654 QuicAckFrame second_ack_frame = InitAckFrame(2); | |
4655 ProcessAckPacket(&second_ack_frame); | |
4656 } | |
4657 | |
4658 // AckNotifierCallback is triggered by the ack of a packet that was | |
4659 // previously nacked, even though the retransmission has a different | |
4660 // packet number. | |
4661 TEST_P(QuicConnectionTest, AckNotifierCallbackForAckOfNackedPacket) { | |
4662 // Create a listener which we expect to be called. | |
4663 scoped_refptr<MockAckListener> listener(new StrictMock<MockAckListener>); | |
4664 | |
4665 // Send four packets, and register to be notified on ACK of packet 2. | |
4666 connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr); | |
4667 connection_.SendStreamDataWithString(3, "bar", 0, !kFin, listener.get()); | |
4668 connection_.SendStreamDataWithString(3, "baz", 0, !kFin, nullptr); | |
4669 connection_.SendStreamDataWithString(3, "qux", 0, !kFin, nullptr); | |
4670 | |
4671 // Now we receive ACK for packets 1, 3, and 4 and lose 2. | |
4672 QuicAckFrame frame = InitAckFrame(4); | |
4673 NackPacket(2, &frame); | |
4674 EXPECT_CALL(*listener, OnPacketRetransmitted(_)); | |
4675 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4676 SendAlgorithmInterface::CongestionVector lost_packets; | |
4677 lost_packets.push_back(std::make_pair(2, kMaxPacketSize)); | |
4678 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)) | |
4679 .WillOnce(SetArgPointee<4>(lost_packets)); | |
4680 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4681 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)); | |
4682 ProcessAckPacket(&frame); | |
4683 | |
4684 // Now we get an ACK for packet 2, which was previously nacked. | |
4685 EXPECT_CALL(*listener, OnPacketAcked(3, _)); | |
4686 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4687 QuicAckFrame second_ack_frame = InitAckFrame(4); | |
4688 ProcessAckPacket(&second_ack_frame); | |
4689 | |
4690 // Verify that the listener is not notified again when the | |
4691 // retransmit is acked. | |
4692 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4693 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4694 QuicAckFrame third_ack_frame = InitAckFrame(5); | |
4695 ProcessAckPacket(&third_ack_frame); | |
4696 } | |
4697 | |
4698 TEST_P(QuicConnectionTest, OnPacketHeaderDebugVisitor) { | |
4699 QuicPacketHeader header; | |
4700 | |
4701 std::unique_ptr<MockQuicConnectionDebugVisitor> debug_visitor( | |
4702 new MockQuicConnectionDebugVisitor()); | |
4703 connection_.set_debug_visitor(debug_visitor.get()); | |
4704 EXPECT_CALL(*debug_visitor, OnPacketHeader(Ref(header))).Times(1); | |
4705 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)).Times(1); | |
4706 EXPECT_CALL(*debug_visitor, OnSuccessfulVersionNegotiation(_)).Times(1); | |
4707 connection_.OnPacketHeader(header); | |
4708 } | |
4709 | |
4710 TEST_P(QuicConnectionTest, Pacing) { | |
4711 // static_cast here does not work if using multipath_sent_packet_manager. | |
4712 FLAGS_quic_enable_multipath = false; | |
4713 TestConnection server(connection_id_, kSelfAddress, helper_.get(), | |
4714 alarm_factory_.get(), writer_.get(), | |
4715 Perspective::IS_SERVER, version()); | |
4716 TestConnection client(connection_id_, kPeerAddress, helper_.get(), | |
4717 alarm_factory_.get(), writer_.get(), | |
4718 Perspective::IS_CLIENT, version()); | |
4719 EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( | |
4720 static_cast<const QuicSentPacketManager*>( | |
4721 &client.sent_packet_manager()))); | |
4722 EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing( | |
4723 static_cast<const QuicSentPacketManager*>( | |
4724 &server.sent_packet_manager()))); | |
4725 } | |
4726 | |
4727 TEST_P(QuicConnectionTest, WindowUpdateInstigateAcks) { | |
4728 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4729 | |
4730 // Send a WINDOW_UPDATE frame. | |
4731 QuicWindowUpdateFrame window_update; | |
4732 window_update.stream_id = 3; | |
4733 window_update.byte_offset = 1234; | |
4734 EXPECT_CALL(visitor_, OnWindowUpdateFrame(_)); | |
4735 ProcessFramePacket(QuicFrame(&window_update)); | |
4736 | |
4737 // Ensure that this has caused the ACK alarm to be set. | |
4738 QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_); | |
4739 EXPECT_TRUE(ack_alarm->IsSet()); | |
4740 } | |
4741 | |
4742 TEST_P(QuicConnectionTest, BlockedFrameInstigateAcks) { | |
4743 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4744 | |
4745 // Send a BLOCKED frame. | |
4746 QuicBlockedFrame blocked; | |
4747 blocked.stream_id = 3; | |
4748 EXPECT_CALL(visitor_, OnBlockedFrame(_)); | |
4749 ProcessFramePacket(QuicFrame(&blocked)); | |
4750 | |
4751 // Ensure that this has caused the ACK alarm to be set. | |
4752 QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_); | |
4753 EXPECT_TRUE(ack_alarm->IsSet()); | |
4754 } | |
4755 | |
4756 TEST_P(QuicConnectionTest, NoDataNoFin) { | |
4757 // Make sure that a call to SendStreamWithData, with no data and no FIN, does | |
4758 // not result in a QuicAckNotifier being used-after-free (fail under ASAN). | |
4759 // Regression test for b/18594622 | |
4760 scoped_refptr<MockAckListener> listener(new MockAckListener); | |
4761 EXPECT_DFATAL( | |
4762 connection_.SendStreamDataWithString(3, "", 0, !kFin, listener.get()), | |
4763 "Attempt to send empty stream frame"); | |
4764 } | |
4765 | |
4766 TEST_P(QuicConnectionTest, DoNotSendGoAwayTwice) { | |
4767 EXPECT_FALSE(connection_.goaway_sent()); | |
4768 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
4769 connection_.SendGoAway(QUIC_PEER_GOING_AWAY, kHeadersStreamId, "Going Away."); | |
4770 EXPECT_TRUE(connection_.goaway_sent()); | |
4771 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0); | |
4772 connection_.SendGoAway(QUIC_PEER_GOING_AWAY, kHeadersStreamId, "Going Away."); | |
4773 } | |
4774 | |
4775 TEST_P(QuicConnectionTest, ReevaluateTimeUntilSendOnAck) { | |
4776 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4777 connection_.SendStreamDataWithString(kClientDataStreamId1, "foo", 0, !kFin, | |
4778 nullptr); | |
4779 | |
4780 // Evaluate CanWrite, and have it return a non-Zero value. | |
4781 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
4782 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1))); | |
4783 connection_.OnCanWrite(); | |
4784 EXPECT_TRUE(connection_.GetSendAlarm()->IsSet()); | |
4785 EXPECT_EQ(clock_.Now() + QuicTime::Delta::FromMilliseconds(1), | |
4786 connection_.GetSendAlarm()->deadline()); | |
4787 | |
4788 // Process an ack and the send alarm will be set to the new 2ms delay. | |
4789 QuicAckFrame ack = InitAckFrame(1); | |
4790 EXPECT_CALL(*loss_algorithm_, DetectLosses(_, _, _, _, _)); | |
4791 EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _)); | |
4792 EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _)) | |
4793 .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(2))); | |
4794 ProcessAckPacket(&ack); | |
4795 EXPECT_EQ(1u, writer_->frame_count()); | |
4796 EXPECT_EQ(1u, writer_->stream_frames().size()); | |
4797 EXPECT_TRUE(connection_.GetSendAlarm()->IsSet()); | |
4798 EXPECT_EQ(clock_.Now() + QuicTime::Delta::FromMilliseconds(2), | |
4799 connection_.GetSendAlarm()->deadline()); | |
4800 writer_->Reset(); | |
4801 } | |
4802 | |
4803 TEST_P(QuicConnectionTest, SendAcksImmediately) { | |
4804 CongestionBlockWrites(); | |
4805 SendAckPacketToPeer(); | |
4806 } | |
4807 | |
4808 TEST_P(QuicConnectionTest, SendPingImmediately) { | |
4809 CongestionBlockWrites(); | |
4810 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1); | |
4811 connection_.SendPing(); | |
4812 EXPECT_FALSE(connection_.HasQueuedData()); | |
4813 } | |
4814 | |
4815 TEST_P(QuicConnectionTest, SendingUnencryptedStreamDataFails) { | |
4816 FLAGS_quic_never_write_unencrypted_data = true; | |
4817 EXPECT_CALL(visitor_, | |
4818 OnConnectionClosed(QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA, | |
4819 _, ConnectionCloseSource::FROM_SELF)); | |
4820 EXPECT_DFATAL(connection_.SendStreamDataWithString(3, "", 0, kFin, nullptr), | |
4821 "Cannot send stream data without encryption."); | |
4822 EXPECT_FALSE(connection_.connected()); | |
4823 } | |
4824 | |
4825 TEST_P(QuicConnectionTest, EnableMultipathNegotiation) { | |
4826 // Test multipath negotiation during crypto handshake. Multipath is enabled | |
4827 // when both endpoints enable multipath. | |
4828 ValueRestore<bool> old_flag(&FLAGS_quic_enable_multipath, true); | |
4829 EXPECT_TRUE(connection_.connected()); | |
4830 EXPECT_FALSE(QuicConnectionPeer::IsMultipathEnabled(&connection_)); | |
4831 EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _)); | |
4832 QuicConfig config; | |
4833 // Enable multipath on server side. | |
4834 config.SetMultipathEnabled(true); | |
4835 | |
4836 // Create a handshake message enables multipath. | |
4837 CryptoHandshakeMessage msg; | |
4838 string error_details; | |
4839 QuicConfig client_config; | |
4840 // Enable multipath on client side. | |
4841 client_config.SetMultipathEnabled(true); | |
4842 client_config.ToHandshakeMessage(&msg); | |
4843 const QuicErrorCode error = | |
4844 config.ProcessPeerHello(msg, CLIENT, &error_details); | |
4845 EXPECT_EQ(QUIC_NO_ERROR, error); | |
4846 | |
4847 connection_.SetFromConfig(config); | |
4848 EXPECT_TRUE(QuicConnectionPeer::IsMultipathEnabled(&connection_)); | |
4849 } | |
4850 | |
4851 TEST_P(QuicConnectionTest, ClosePath) { | |
4852 QuicPathId kTestPathId = 1; | |
4853 connection_.SendPathClose(kTestPathId); | |
4854 EXPECT_TRUE(QuicFramerPeer::IsPathClosed( | |
4855 QuicConnectionPeer::GetFramer(&connection_), kTestPathId)); | |
4856 } | |
4857 | |
4858 TEST_P(QuicConnectionTest, BadMultipathFlag) { | |
4859 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_BAD_MULTIPATH_FLAG, _, | |
4860 ConnectionCloseSource::FROM_SELF)); | |
4861 | |
4862 // Receieve a packet with multipath flag on when multipath is not enabled. | |
4863 EXPECT_TRUE(connection_.connected()); | |
4864 EXPECT_FALSE(QuicConnectionPeer::IsMultipathEnabled(&connection_)); | |
4865 peer_creator_.SetCurrentPath(/*path_id=*/1u, 1u, 10u); | |
4866 QuicStreamFrame stream_frame(1u, false, 0u, StringPiece()); | |
4867 EXPECT_DFATAL( | |
4868 ProcessFramePacket(QuicFrame(&stream_frame)), | |
4869 "Received a packet with multipath flag but multipath is not enabled."); | |
4870 EXPECT_FALSE(connection_.connected()); | |
4871 } | |
4872 | |
4873 TEST_P(QuicConnectionTest, OnPathDegrading) { | |
4874 QuicByteCount packet_size; | |
4875 const size_t kMinTimeoutsBeforePathDegrading = 2; | |
4876 | |
4877 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)) | |
4878 .WillOnce(DoAll(SaveArg<3>(&packet_size), Return(true))); | |
4879 connection_.SendStreamDataWithString(3, "packet", 0, !kFin, nullptr); | |
4880 size_t num_timeouts = kMinTimeoutsBeforePathDegrading + | |
4881 QuicSentPacketManagerPeer::GetMaxTailLossProbes( | |
4882 QuicConnectionPeer::GetSentPacketManager( | |
4883 &connection_, kDefaultPathId)); | |
4884 for (size_t i = 1; i < num_timeouts; ++i) { | |
4885 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10 * i)); | |
4886 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, packet_size, _)); | |
4887 connection_.GetRetransmissionAlarm()->Fire(); | |
4888 } | |
4889 // Next RTO should cause OnPathDegrading to be called before the | |
4890 // retransmission is sent out. | |
4891 clock_.AdvanceTime( | |
4892 QuicTime::Delta::FromSeconds(kMinTimeoutsBeforePathDegrading * 10)); | |
4893 { | |
4894 InSequence s; | |
4895 EXPECT_CALL(visitor_, OnPathDegrading()); | |
4896 EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, packet_size, _)); | |
4897 } | |
4898 connection_.GetRetransmissionAlarm()->Fire(); | |
4899 } | |
4900 | |
4901 TEST_P(QuicConnectionTest, MultipleCallsToCloseConnection) { | |
4902 // Verifies that multiple calls to CloseConnection do not | |
4903 // result in multiple attempts to close the connection - it will be marked as | |
4904 // disconnected after the first call. | |
4905 EXPECT_CALL(visitor_, OnConnectionClosed(_, _, _)).Times(1); | |
4906 connection_.CloseConnection(QUIC_NO_ERROR, "no reason", | |
4907 ConnectionCloseBehavior::SILENT_CLOSE); | |
4908 connection_.CloseConnection(QUIC_NO_ERROR, "no reason", | |
4909 ConnectionCloseBehavior::SILENT_CLOSE); | |
4910 } | |
4911 | |
4912 TEST_P(QuicConnectionTest, ServerReceivesChloOnNonCryptoStream) { | |
4913 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4914 | |
4915 set_perspective(Perspective::IS_SERVER); | |
4916 QuicPacketCreatorPeer::SetSendVersionInPacket(creator_, false); | |
4917 | |
4918 CryptoHandshakeMessage message; | |
4919 CryptoFramer framer; | |
4920 message.set_tag(kCHLO); | |
4921 std::unique_ptr<QuicData> data(framer.ConstructHandshakeMessage(message)); | |
4922 frame1_.stream_id = 10; | |
4923 frame1_.data_buffer = data->data(); | |
4924 frame1_.data_length = data->length(); | |
4925 | |
4926 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_MAYBE_CORRUPTED_MEMORY, _, | |
4927 ConnectionCloseSource::FROM_SELF)); | |
4928 ProcessFramePacket(QuicFrame(&frame1_)); | |
4929 } | |
4930 | |
4931 TEST_P(QuicConnectionTest, ClientReceivesRejOnNonCryptoStream) { | |
4932 EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_)); | |
4933 | |
4934 CryptoHandshakeMessage message; | |
4935 CryptoFramer framer; | |
4936 message.set_tag(kREJ); | |
4937 std::unique_ptr<QuicData> data(framer.ConstructHandshakeMessage(message)); | |
4938 frame1_.stream_id = 10; | |
4939 frame1_.data_buffer = data->data(); | |
4940 frame1_.data_length = data->length(); | |
4941 | |
4942 EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_MAYBE_CORRUPTED_MEMORY, _, | |
4943 ConnectionCloseSource::FROM_SELF)); | |
4944 ProcessFramePacket(QuicFrame(&frame1_)); | |
4945 } | |
4946 | |
4947 } // namespace | |
4948 } // namespace test | |
4949 } // namespace net | |
OLD | NEW |