QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/core/quic_crypto_stream.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_string.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 19 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 20 | #include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h" |
| 21 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 22 | |
| 23 | using testing::_; |
| 24 | using testing::InSequence; |
| 25 | using testing::Invoke; |
| 26 | using testing::InvokeWithoutArgs; |
| 27 | |
| 28 | namespace quic { |
| 29 | namespace test { |
| 30 | namespace { |
| 31 | |
| 32 | class MockQuicCryptoStream : public QuicCryptoStream, |
| 33 | public QuicCryptoHandshaker { |
| 34 | public: |
| 35 | explicit MockQuicCryptoStream(QuicSession* session) |
| 36 | : QuicCryptoStream(session), |
| 37 | QuicCryptoHandshaker(this, session), |
| 38 | params_(new QuicCryptoNegotiatedParameters) {} |
| 39 | MockQuicCryptoStream(const MockQuicCryptoStream&) = delete; |
| 40 | MockQuicCryptoStream& operator=(const MockQuicCryptoStream&) = delete; |
| 41 | |
| 42 | void OnHandshakeMessage(const CryptoHandshakeMessage& message) override { |
| 43 | messages_.push_back(message); |
| 44 | } |
| 45 | |
| 46 | std::vector<CryptoHandshakeMessage>* messages() { return &messages_; } |
| 47 | |
| 48 | bool encryption_established() const override { return false; } |
| 49 | bool handshake_confirmed() const override { return false; } |
| 50 | |
| 51 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 52 | const override { |
| 53 | return *params_; |
| 54 | } |
| 55 | CryptoMessageParser* crypto_message_parser() override { |
| 56 | return QuicCryptoHandshaker::crypto_message_parser(); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_; |
| 61 | std::vector<CryptoHandshakeMessage> messages_; |
| 62 | }; |
| 63 | |
| 64 | class QuicCryptoStreamTest : public QuicTest { |
| 65 | public: |
| 66 | QuicCryptoStreamTest() |
| 67 | : connection_(new MockQuicConnection(&helper_, |
| 68 | &alarm_factory_, |
| 69 | Perspective::IS_CLIENT)), |
| 70 | session_(connection_, /*create_mock_crypto_stream=*/false) { |
| 71 | stream_ = new MockQuicCryptoStream(&session_); |
| 72 | session_.SetCryptoStream(stream_); |
| 73 | session_.Initialize(); |
| 74 | message_.set_tag(kSHLO); |
| 75 | message_.SetStringPiece(1, "abc"); |
| 76 | message_.SetStringPiece(2, "def"); |
| 77 | ConstructHandshakeMessage(); |
| 78 | } |
| 79 | QuicCryptoStreamTest(const QuicCryptoStreamTest&) = delete; |
| 80 | QuicCryptoStreamTest& operator=(const QuicCryptoStreamTest&) = delete; |
| 81 | |
| 82 | void ConstructHandshakeMessage() { |
| 83 | CryptoFramer framer; |
| 84 | message_data_.reset(framer.ConstructHandshakeMessage(message_)); |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | MockQuicConnectionHelper helper_; |
| 89 | MockAlarmFactory alarm_factory_; |
| 90 | MockQuicConnection* connection_; |
| 91 | MockQuicSpdySession session_; |
| 92 | MockQuicCryptoStream* stream_; |
| 93 | CryptoHandshakeMessage message_; |
| 94 | std::unique_ptr<QuicData> message_data_; |
| 95 | }; |
| 96 | |
| 97 | TEST_F(QuicCryptoStreamTest, NotInitiallyConected) { |
| 98 | EXPECT_FALSE(stream_->encryption_established()); |
| 99 | EXPECT_FALSE(stream_->handshake_confirmed()); |
| 100 | } |
| 101 | |
| 102 | TEST_F(QuicCryptoStreamTest, ProcessRawData) { |
| 103 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 104 | stream_->OnStreamFrame(QuicStreamFrame( |
| 105 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 106 | /*fin=*/false, |
| 107 | /*offset=*/0, message_data_->AsStringPiece())); |
| 108 | } else { |
| 109 | stream_->OnCryptoFrame(QuicCryptoFrame(ENCRYPTION_NONE, /*offset*/ 0, |
| 110 | message_data_->AsStringPiece())); |
| 111 | } |
| 112 | ASSERT_EQ(1u, stream_->messages()->size()); |
| 113 | const CryptoHandshakeMessage& message = (*stream_->messages())[0]; |
| 114 | EXPECT_EQ(kSHLO, message.tag()); |
| 115 | EXPECT_EQ(2u, message.tag_value_map().size()); |
| 116 | EXPECT_EQ("abc", crypto_test_utils::GetValueForTag(message, 1)); |
| 117 | EXPECT_EQ("def", crypto_test_utils::GetValueForTag(message, 2)); |
| 118 | } |
| 119 | |
| 120 | TEST_F(QuicCryptoStreamTest, ProcessBadData) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 121 | std::string bad(message_data_->data(), message_data_->length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | const int kFirstTagIndex = sizeof(uint32_t) + // message tag |
| 123 | sizeof(uint16_t) + // number of tag-value pairs |
| 124 | sizeof(uint16_t); // padding |
| 125 | EXPECT_EQ(1, bad[kFirstTagIndex]); |
| 126 | bad[kFirstTagIndex] = 0x7F; // out of order tag |
| 127 | |
| 128 | EXPECT_CALL(*connection_, CloseConnection(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, |
| 129 | testing::_, testing::_)); |
| 130 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 131 | stream_->OnStreamFrame(QuicStreamFrame( |
| 132 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 133 | /*fin=*/false, /*offset=*/0, bad)); |
| 134 | } else { |
| 135 | stream_->OnCryptoFrame(QuicCryptoFrame(ENCRYPTION_NONE, /*offset*/ 0, bad)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | TEST_F(QuicCryptoStreamTest, NoConnectionLevelFlowControl) { |
| 140 | EXPECT_FALSE( |
| 141 | QuicStreamPeer::StreamContributesToConnectionFlowControl(stream_)); |
| 142 | } |
| 143 | |
| 144 | TEST_F(QuicCryptoStreamTest, RetransmitCryptoData) { |
| 145 | if (connection_->transport_version() >= QUIC_VERSION_47) { |
| 146 | return; |
| 147 | } |
| 148 | InSequence s; |
| 149 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 150 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 151 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | EXPECT_CALL( |
| 153 | session_, |
| 154 | WritevData(_, |
| 155 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 156 | 1350, 0, _)) |
| 157 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 158 | stream_->WriteOrBufferData(data, false, nullptr); |
| 159 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 160 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 161 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 162 | EXPECT_CALL( |
| 163 | session_, |
| 164 | WritevData(_, |
| 165 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 166 | 1350, 1350, _)) |
| 167 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 168 | stream_->WriteOrBufferData(data, false, nullptr); |
| 169 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 170 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 171 | |
| 172 | // Lost [0, 1000). |
| 173 | stream_->OnStreamFrameLost(0, 1000, false); |
| 174 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 175 | // Lost [1200, 2000). |
| 176 | stream_->OnStreamFrameLost(1200, 800, false); |
| 177 | EXPECT_CALL( |
| 178 | session_, |
| 179 | WritevData(_, |
| 180 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 181 | 1000, 0, _)) |
| 182 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 183 | // Verify [1200, 2000) are sent in [1200, 1350) and [1350, 2000) because of |
| 184 | // they are in different encryption levels. |
| 185 | EXPECT_CALL( |
| 186 | session_, |
| 187 | WritevData(_, |
| 188 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 189 | 150, 1200, _)) |
| 190 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 191 | EXPECT_CALL( |
| 192 | session_, |
| 193 | WritevData(_, |
| 194 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 195 | 650, 1350, _)) |
| 196 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 197 | stream_->OnCanWrite(); |
| 198 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 199 | // Verify connection's encryption level has restored. |
| 200 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 201 | } |
| 202 | |
| 203 | TEST_F(QuicCryptoStreamTest, RetransmitCryptoDataInCryptoFrames) { |
| 204 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 205 | return; |
| 206 | } |
| 207 | EXPECT_CALL(*connection_, SendCryptoData(_, _, _)).Times(0); |
| 208 | InSequence s; |
| 209 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 210 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 211 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 212 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 1350, 0)) |
| 213 | .WillOnce(Invoke(connection_, |
| 214 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 215 | stream_->WriteCryptoData(ENCRYPTION_NONE, data); |
| 216 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 217 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 218 | std::unique_ptr<NullEncrypter> encrypter = |
| 219 | QuicMakeUnique<NullEncrypter>(Perspective::IS_CLIENT); |
| 220 | connection_->SetEncrypter(ENCRYPTION_ZERO_RTT, std::move(encrypter)); |
| 221 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 222 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 1350, 0)) |
| 223 | .WillOnce(Invoke(connection_, |
| 224 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 225 | stream_->WriteCryptoData(ENCRYPTION_ZERO_RTT, data); |
| 226 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 227 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 228 | |
| 229 | // Lost [0, 1000). |
| 230 | QuicCryptoFrame lost_frame(ENCRYPTION_NONE, 0, 1000); |
| 231 | stream_->OnCryptoFrameLost(&lost_frame); |
| 232 | EXPECT_TRUE(stream_->HasPendingCryptoRetransmission()); |
| 233 | // Lost [1200, 2000). |
| 234 | lost_frame = QuicCryptoFrame(ENCRYPTION_NONE, 1200, 150); |
| 235 | stream_->OnCryptoFrameLost(&lost_frame); |
| 236 | lost_frame = QuicCryptoFrame(ENCRYPTION_ZERO_RTT, 0, 650); |
| 237 | stream_->OnCryptoFrameLost(&lost_frame); |
| 238 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 1000, 0)) |
| 239 | .WillOnce(Invoke(connection_, |
| 240 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 241 | // Verify [1200, 2000) are sent in [1200, 1350) and [1350, 2000) because of |
| 242 | // they are in different encryption levels. |
| 243 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 150, 1200)) |
| 244 | .WillOnce(Invoke(connection_, |
| 245 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 246 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 650, 0)) |
| 247 | .WillOnce(Invoke(connection_, |
| 248 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 249 | stream_->WritePendingCryptoRetransmission(); |
| 250 | EXPECT_FALSE(stream_->HasPendingCryptoRetransmission()); |
| 251 | // Verify connection's encryption level has restored. |
| 252 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 253 | } |
| 254 | |
| 255 | TEST_F(QuicCryptoStreamTest, NeuterUnencryptedStreamData) { |
| 256 | if (connection_->transport_version() >= QUIC_VERSION_47) { |
| 257 | return; |
| 258 | } |
| 259 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 260 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 261 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 262 | EXPECT_CALL( |
| 263 | session_, |
| 264 | WritevData(_, |
| 265 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 266 | 1350, 0, _)) |
| 267 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 268 | stream_->WriteOrBufferData(data, false, nullptr); |
| 269 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 270 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 271 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 272 | EXPECT_CALL( |
| 273 | session_, |
| 274 | WritevData(_, |
| 275 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 276 | 1350, 1350, _)) |
| 277 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 278 | stream_->WriteOrBufferData(data, false, nullptr); |
| 279 | |
| 280 | // Lost [0, 1350). |
| 281 | stream_->OnStreamFrameLost(0, 1350, false); |
| 282 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 283 | // Neuters [0, 1350). |
| 284 | stream_->NeuterUnencryptedStreamData(); |
| 285 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 286 | // Lost [0, 1350) again. |
| 287 | stream_->OnStreamFrameLost(0, 1350, false); |
| 288 | EXPECT_FALSE(stream_->HasPendingRetransmission()); |
| 289 | |
| 290 | // Lost [1350, 2000). |
| 291 | stream_->OnStreamFrameLost(1350, 650, false); |
| 292 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 293 | stream_->NeuterUnencryptedStreamData(); |
| 294 | EXPECT_TRUE(stream_->HasPendingRetransmission()); |
| 295 | } |
| 296 | |
| 297 | TEST_F(QuicCryptoStreamTest, NeuterUnencryptedCryptoData) { |
| 298 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 299 | return; |
| 300 | } |
| 301 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 302 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 303 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 304 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 1350, 0)) |
| 305 | .WillOnce(Invoke(connection_, |
| 306 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 307 | stream_->WriteCryptoData(ENCRYPTION_NONE, data); |
| 308 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 309 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 310 | std::unique_ptr<NullEncrypter> encrypter = |
| 311 | QuicMakeUnique<NullEncrypter>(Perspective::IS_CLIENT); |
| 312 | connection_->SetEncrypter(ENCRYPTION_ZERO_RTT, std::move(encrypter)); |
| 313 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 314 | EXPECT_CALL(*connection_, SendCryptoData(_, _, _)).Times(0); |
| 315 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 1350, 0)) |
| 316 | .WillOnce(Invoke(connection_, |
| 317 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 318 | stream_->WriteCryptoData(ENCRYPTION_ZERO_RTT, data); |
| 319 | |
| 320 | // Lost [0, 1350). |
| 321 | QuicCryptoFrame lost_frame(ENCRYPTION_NONE, 0, 1350); |
| 322 | stream_->OnCryptoFrameLost(&lost_frame); |
| 323 | EXPECT_TRUE(stream_->HasPendingCryptoRetransmission()); |
| 324 | // Neuters [0, 1350). |
| 325 | stream_->NeuterUnencryptedStreamData(); |
| 326 | EXPECT_FALSE(stream_->HasPendingCryptoRetransmission()); |
| 327 | // Lost [0, 1350) again. |
| 328 | stream_->OnCryptoFrameLost(&lost_frame); |
| 329 | EXPECT_FALSE(stream_->HasPendingCryptoRetransmission()); |
| 330 | |
| 331 | // Lost [1350, 2000), which starts at offset 0 at the ENCRYPTION_ZERO_RTT |
| 332 | // level. |
| 333 | lost_frame = QuicCryptoFrame(ENCRYPTION_ZERO_RTT, 0, 650); |
| 334 | stream_->OnCryptoFrameLost(&lost_frame); |
| 335 | EXPECT_TRUE(stream_->HasPendingCryptoRetransmission()); |
| 336 | stream_->NeuterUnencryptedStreamData(); |
| 337 | EXPECT_TRUE(stream_->HasPendingCryptoRetransmission()); |
| 338 | } |
| 339 | |
| 340 | TEST_F(QuicCryptoStreamTest, RetransmitStreamData) { |
| 341 | if (connection_->transport_version() >= QUIC_VERSION_47) { |
| 342 | return; |
| 343 | } |
| 344 | InSequence s; |
| 345 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 346 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 347 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 348 | EXPECT_CALL( |
| 349 | session_, |
| 350 | WritevData(_, |
| 351 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 352 | 1350, 0, _)) |
| 353 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 354 | stream_->WriteOrBufferData(data, false, nullptr); |
| 355 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 356 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 357 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 358 | EXPECT_CALL( |
| 359 | session_, |
| 360 | WritevData(_, |
| 361 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 362 | 1350, 1350, _)) |
| 363 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 364 | stream_->WriteOrBufferData(data, false, nullptr); |
| 365 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 366 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 367 | |
| 368 | // Ack [2000, 2500). |
| 369 | QuicByteCount newly_acked_length = 0; |
| 370 | stream_->OnStreamFrameAcked(2000, 500, false, QuicTime::Delta::Zero(), |
| 371 | &newly_acked_length); |
| 372 | EXPECT_EQ(500u, newly_acked_length); |
| 373 | |
| 374 | // Force crypto stream to send [1350, 2700) and only [1350, 1500) is consumed. |
| 375 | EXPECT_CALL( |
| 376 | session_, |
| 377 | WritevData(_, |
| 378 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 379 | 650, 1350, _)) |
| 380 | .WillOnce(InvokeWithoutArgs([this]() { |
| 381 | return MockQuicSession::ConsumeData( |
| 382 | stream_, |
| 383 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), 150, |
| 384 | 1350, NO_FIN); |
| 385 | })); |
| 386 | |
| 387 | EXPECT_FALSE(stream_->RetransmitStreamData(1350, 1350, false)); |
| 388 | // Verify connection's encryption level has restored. |
| 389 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 390 | |
| 391 | // Force session to send [1350, 1500) again and all data is consumed. |
| 392 | EXPECT_CALL( |
| 393 | session_, |
| 394 | WritevData(_, |
| 395 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 396 | 650, 1350, _)) |
| 397 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 398 | EXPECT_CALL( |
| 399 | session_, |
| 400 | WritevData(_, |
| 401 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 402 | 200, 2500, _)) |
| 403 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 404 | EXPECT_TRUE(stream_->RetransmitStreamData(1350, 1350, false)); |
| 405 | // Verify connection's encryption level has restored. |
| 406 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 407 | |
| 408 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(0); |
| 409 | // Force to send an empty frame. |
| 410 | EXPECT_TRUE(stream_->RetransmitStreamData(0, 0, false)); |
| 411 | } |
| 412 | |
| 413 | TEST_F(QuicCryptoStreamTest, RetransmitStreamDataWithCryptoFrames) { |
| 414 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 415 | return; |
| 416 | } |
| 417 | InSequence s; |
| 418 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 419 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 420 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 421 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 1350, 0)) |
| 422 | .WillOnce(Invoke(connection_, |
| 423 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 424 | stream_->WriteCryptoData(ENCRYPTION_NONE, data); |
| 425 | // Send [1350, 2700) in ENCRYPTION_ZERO_RTT. |
| 426 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 427 | std::unique_ptr<NullEncrypter> encrypter = |
| 428 | QuicMakeUnique<NullEncrypter>(Perspective::IS_CLIENT); |
| 429 | connection_->SetEncrypter(ENCRYPTION_ZERO_RTT, std::move(encrypter)); |
| 430 | EXPECT_EQ(ENCRYPTION_ZERO_RTT, connection_->encryption_level()); |
| 431 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 1350, 0)) |
| 432 | .WillOnce(Invoke(connection_, |
| 433 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 434 | stream_->WriteCryptoData(ENCRYPTION_ZERO_RTT, data); |
| 435 | connection_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 436 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 437 | |
| 438 | // Ack [2000, 2500). |
| 439 | QuicCryptoFrame acked_frame(ENCRYPTION_ZERO_RTT, 650, 500); |
| 440 | EXPECT_TRUE( |
| 441 | stream_->OnCryptoFrameAcked(acked_frame, QuicTime::Delta::Zero())); |
| 442 | |
| 443 | // Retransmit only [1350, 1500). |
| 444 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 150, 0)) |
| 445 | .WillOnce(Invoke(connection_, |
| 446 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 447 | QuicCryptoFrame frame_to_retransmit(ENCRYPTION_ZERO_RTT, 0, 150); |
| 448 | stream_->RetransmitData(&frame_to_retransmit); |
| 449 | |
| 450 | // Verify connection's encryption level has restored. |
| 451 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 452 | |
| 453 | // Retransmit [1350, 2700) again and all data is sent. |
| 454 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 650, 0)) |
| 455 | .WillOnce(Invoke(connection_, |
| 456 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 457 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_ZERO_RTT, 200, 1150)) |
| 458 | .WillOnce(Invoke(connection_, |
| 459 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 460 | frame_to_retransmit = QuicCryptoFrame(ENCRYPTION_ZERO_RTT, 0, 1350); |
| 461 | stream_->RetransmitData(&frame_to_retransmit); |
| 462 | // Verify connection's encryption level has restored. |
| 463 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, connection_->encryption_level()); |
| 464 | |
| 465 | EXPECT_CALL(*connection_, SendCryptoData(_, _, _)).Times(0); |
| 466 | // Force to send an empty frame. |
| 467 | QuicCryptoFrame empty_frame(ENCRYPTION_FORWARD_SECURE, 0, 0); |
| 468 | stream_->RetransmitData(&empty_frame); |
| 469 | } |
| 470 | |
| 471 | // Regression test for b/115926584. |
| 472 | TEST_F(QuicCryptoStreamTest, HasUnackedCryptoData) { |
| 473 | if (connection_->transport_version() >= QUIC_VERSION_47) { |
| 474 | return; |
| 475 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 476 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 477 | EXPECT_CALL( |
| 478 | session_, |
| 479 | WritevData(_, |
| 480 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 481 | 1350, 0, _)) |
| 482 | .WillOnce(testing::Return(QuicConsumedData(0, false))); |
| 483 | stream_->WriteOrBufferData(data, false, nullptr); |
| 484 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 485 | // Although there is no outstanding data, verify session has pending crypto |
| 486 | // data. |
| 487 | EXPECT_EQ(GetQuicReloadableFlag(quic_fix_has_pending_crypto_data), |
| 488 | session_.HasUnackedCryptoData()); |
| 489 | |
| 490 | EXPECT_CALL( |
| 491 | session_, |
| 492 | WritevData(_, |
| 493 | QuicUtils::GetCryptoStreamId(connection_->transport_version()), |
| 494 | 1350, 0, _)) |
| 495 | .WillOnce(Invoke(MockQuicSession::ConsumeData)); |
| 496 | stream_->OnCanWrite(); |
| 497 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 498 | EXPECT_TRUE(session_.HasUnackedCryptoData()); |
| 499 | } |
| 500 | |
| 501 | TEST_F(QuicCryptoStreamTest, HasUnackedCryptoDataWithCryptoFrames) { |
| 502 | if (connection_->transport_version() < QUIC_VERSION_47) { |
| 503 | return; |
| 504 | } |
| 505 | // Send [0, 1350) in ENCRYPTION_NONE. |
| 506 | EXPECT_EQ(ENCRYPTION_NONE, connection_->encryption_level()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame^] | 507 | std::string data(1350, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 508 | EXPECT_CALL(*connection_, SendCryptoData(ENCRYPTION_NONE, 1350, 0)) |
| 509 | .WillOnce(Invoke(connection_, |
| 510 | &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 511 | stream_->WriteCryptoData(ENCRYPTION_NONE, data); |
| 512 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 513 | EXPECT_TRUE(session_.HasUnackedCryptoData()); |
| 514 | } |
| 515 | |
| 516 | } // namespace |
| 517 | } // namespace test |
| 518 | } // namespace quic |