QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/quartc/quartc_session.h" |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 9 | #include "net/third_party/quiche/src/quic/core/tls_client_handshaker.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/tls_server_handshaker.h" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_clock.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_utils.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_test_mem_slice_vector.h" |
| 16 | #include "net/third_party/quiche/src/quic/quartc/counting_packet_filter.h" |
| 17 | #include "net/third_party/quiche/src/quic/quartc/quartc_endpoint.h" |
| 18 | #include "net/third_party/quiche/src/quic/quartc/quartc_fakes.h" |
| 19 | #include "net/third_party/quiche/src/quic/quartc/quartc_packet_writer.h" |
| 20 | #include "net/third_party/quiche/src/quic/quartc/simulated_packet_transport.h" |
| 21 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 22 | #include "net/third_party/quiche/src/quic/test_tools/simulator/packet_filter.h" |
| 23 | #include "net/third_party/quiche/src/quic/test_tools/simulator/simulator.h" |
| 24 | |
| 25 | namespace quic { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | constexpr QuicTime::Delta kPropagationDelay = |
| 30 | QuicTime::Delta::FromMilliseconds(10); |
| 31 | // Propagation delay and a bit, but no more than full RTT. |
| 32 | constexpr QuicTime::Delta kPropagationDelayAndABit = |
| 33 | QuicTime::Delta::FromMilliseconds(12); |
| 34 | |
| 35 | static QuicByteCount kDefaultMaxPacketSize = 1200; |
| 36 | |
| 37 | class QuartcSessionTest : public QuicTest { |
| 38 | public: |
| 39 | ~QuartcSessionTest() override {} |
| 40 | |
| 41 | void Init(bool create_client_endpoint = true) { |
| 42 | client_transport_ = |
| 43 | QuicMakeUnique<simulator::SimulatedQuartcPacketTransport>( |
| 44 | &simulator_, "client_transport", "server_transport", |
| 45 | 10 * kDefaultMaxPacketSize); |
| 46 | server_transport_ = |
| 47 | QuicMakeUnique<simulator::SimulatedQuartcPacketTransport>( |
| 48 | &simulator_, "server_transport", "client_transport", |
| 49 | 10 * kDefaultMaxPacketSize); |
| 50 | |
| 51 | client_filter_ = QuicMakeUnique<simulator::CountingPacketFilter>( |
| 52 | &simulator_, "client_filter", client_transport_.get()); |
| 53 | |
| 54 | client_server_link_ = QuicMakeUnique<simulator::SymmetricLink>( |
| 55 | client_filter_.get(), server_transport_.get(), |
| 56 | QuicBandwidth::FromKBitsPerSecond(10 * 1000), kPropagationDelay); |
| 57 | |
| 58 | client_stream_delegate_ = QuicMakeUnique<FakeQuartcStreamDelegate>(); |
| 59 | client_session_delegate_ = QuicMakeUnique<FakeQuartcSessionDelegate>( |
| 60 | client_stream_delegate_.get(), simulator_.GetClock()); |
| 61 | client_endpoint_delegate_ = QuicMakeUnique<FakeQuartcEndpointDelegate>( |
| 62 | client_session_delegate_.get()); |
| 63 | |
| 64 | server_stream_delegate_ = QuicMakeUnique<FakeQuartcStreamDelegate>(); |
| 65 | server_session_delegate_ = QuicMakeUnique<FakeQuartcSessionDelegate>( |
| 66 | server_stream_delegate_.get(), simulator_.GetClock()); |
| 67 | server_endpoint_delegate_ = QuicMakeUnique<FakeQuartcEndpointDelegate>( |
| 68 | server_session_delegate_.get()); |
| 69 | |
| 70 | // No 0-rtt setup, because server config is empty. |
| 71 | // CannotCreateDataStreamBeforeHandshake depends on 1-rtt setup. |
| 72 | if (create_client_endpoint) { |
| 73 | client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>( |
| 74 | simulator_.GetAlarmFactory(), simulator_.GetClock(), |
QUICHE team | 73957f1 | 2019-04-18 16:21:52 -0700 | [diff] [blame] | 75 | simulator_.GetRandomGenerator(), client_endpoint_delegate_.get(), |
| 76 | quic::QuartcSessionConfig(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | /*serialized_server_config=*/""); |
| 78 | } |
| 79 | server_endpoint_ = QuicMakeUnique<QuartcServerEndpoint>( |
| 80 | simulator_.GetAlarmFactory(), simulator_.GetClock(), |
QUICHE team | 73957f1 | 2019-04-18 16:21:52 -0700 | [diff] [blame] | 81 | simulator_.GetRandomGenerator(), server_endpoint_delegate_.get(), |
| 82 | quic::QuartcSessionConfig()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Note that input session config will apply to both server and client. |
| 86 | // Perspective and packet_transport will be overwritten. |
| 87 | void CreateClientAndServerSessions(const QuartcSessionConfig& session_config, |
| 88 | bool init = true) { |
| 89 | if (init) { |
| 90 | Init(); |
| 91 | } |
| 92 | |
| 93 | server_endpoint_->Connect(server_transport_.get()); |
| 94 | client_endpoint_->Connect(client_transport_.get()); |
| 95 | |
| 96 | CHECK(simulator_.RunUntil([this] { |
| 97 | return client_endpoint_delegate_->session() != nullptr && |
| 98 | server_endpoint_delegate_->session() != nullptr; |
| 99 | })); |
| 100 | |
| 101 | client_peer_ = client_endpoint_delegate_->session(); |
| 102 | server_peer_ = server_endpoint_delegate_->session(); |
| 103 | } |
| 104 | |
| 105 | // Runs all tasks scheduled in the next 200 ms. |
| 106 | void RunTasks() { simulator_.RunFor(QuicTime::Delta::FromMilliseconds(200)); } |
| 107 | |
| 108 | void AwaitHandshake() { |
| 109 | simulator_.RunUntil([this] { |
| 110 | return client_peer_->IsCryptoHandshakeConfirmed() && |
| 111 | server_peer_->IsCryptoHandshakeConfirmed(); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | // Test handshake establishment and sending/receiving of data for two |
| 116 | // directions. |
| 117 | void TestSendReceiveStreams() { |
| 118 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 119 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 120 | ASSERT_TRUE(server_peer_->IsEncryptionEstablished()); |
| 121 | ASSERT_TRUE(client_peer_->IsEncryptionEstablished()); |
| 122 | |
| 123 | // Now we can establish encrypted outgoing stream. |
| 124 | QuartcStream* outgoing_stream = |
| 125 | server_peer_->CreateOutgoingBidirectionalStream(); |
| 126 | QuicStreamId stream_id = outgoing_stream->id(); |
| 127 | ASSERT_NE(nullptr, outgoing_stream); |
| 128 | EXPECT_TRUE(server_peer_->ShouldKeepConnectionAlive()); |
| 129 | |
| 130 | outgoing_stream->SetDelegate(server_stream_delegate_.get()); |
| 131 | |
| 132 | // Send a test message from peer 1 to peer 2. |
| 133 | char kTestMessage[] = "Hello"; |
| 134 | test::QuicTestMemSliceVector data( |
| 135 | {std::make_pair(kTestMessage, strlen(kTestMessage))}); |
| 136 | outgoing_stream->WriteMemSlices(data.span(), /*fin=*/false); |
| 137 | RunTasks(); |
| 138 | |
| 139 | // Wait for peer 2 to receive messages. |
| 140 | ASSERT_TRUE(client_stream_delegate_->has_data()); |
| 141 | |
| 142 | QuartcStream* incoming = client_session_delegate_->last_incoming_stream(); |
| 143 | ASSERT_TRUE(incoming); |
| 144 | EXPECT_EQ(incoming->id(), stream_id); |
| 145 | EXPECT_TRUE(client_peer_->ShouldKeepConnectionAlive()); |
| 146 | |
| 147 | EXPECT_EQ(client_stream_delegate_->data()[stream_id], kTestMessage); |
| 148 | // Send a test message from peer 2 to peer 1. |
| 149 | char kTestResponse[] = "Response"; |
| 150 | test::QuicTestMemSliceVector response( |
| 151 | {std::make_pair(kTestResponse, strlen(kTestResponse))}); |
| 152 | incoming->WriteMemSlices(response.span(), /*fin=*/false); |
| 153 | RunTasks(); |
| 154 | // Wait for peer 1 to receive messages. |
| 155 | ASSERT_TRUE(server_stream_delegate_->has_data()); |
| 156 | |
| 157 | EXPECT_EQ(server_stream_delegate_->data()[stream_id], kTestResponse); |
| 158 | } |
| 159 | |
| 160 | // Test sending/receiving of messages for two directions. |
| 161 | void TestSendReceiveMessage() { |
| 162 | ASSERT_TRUE(server_peer_->CanSendMessage()); |
| 163 | ASSERT_TRUE(client_peer_->CanSendMessage()); |
| 164 | |
| 165 | // Send message from peer 1 to peer 2. |
| 166 | ASSERT_TRUE(server_peer_->SendOrQueueMessage("Message from server")); |
| 167 | |
| 168 | // First message in each direction should not be queued. |
| 169 | EXPECT_EQ(server_peer_->send_message_queue_size(), 0u); |
| 170 | |
| 171 | // Wait for peer 2 to receive message. |
| 172 | RunTasks(); |
| 173 | |
| 174 | EXPECT_THAT(client_session_delegate_->incoming_messages(), |
| 175 | testing::ElementsAre("Message from server")); |
| 176 | |
| 177 | // Send message from peer 2 to peer 1. |
| 178 | ASSERT_TRUE(client_peer_->SendOrQueueMessage("Message from client")); |
| 179 | |
| 180 | // First message in each direction should not be queued. |
| 181 | EXPECT_EQ(client_peer_->send_message_queue_size(), 0u); |
| 182 | |
| 183 | // Wait for peer 1 to receive message. |
| 184 | RunTasks(); |
| 185 | |
| 186 | EXPECT_THAT(server_session_delegate_->incoming_messages(), |
| 187 | testing::ElementsAre("Message from client")); |
| 188 | } |
| 189 | |
| 190 | // Test for sending multiple messages that also result in queueing. |
| 191 | // This is one-way test, which is run in given direction. |
| 192 | void TestSendReceiveQueuedMessages(bool direction_from_server) { |
| 193 | // Send until queue_size number of messages are queued. |
| 194 | constexpr size_t queue_size = 10; |
| 195 | |
| 196 | ASSERT_TRUE(server_peer_->CanSendMessage()); |
| 197 | ASSERT_TRUE(client_peer_->CanSendMessage()); |
| 198 | |
| 199 | QuartcSession* const peer_sending = |
| 200 | direction_from_server ? server_peer_ : client_peer_; |
| 201 | |
| 202 | FakeQuartcSessionDelegate* const delegate_receiving = |
| 203 | direction_from_server ? client_session_delegate_.get() |
| 204 | : server_session_delegate_.get(); |
| 205 | |
| 206 | // There should be no messages in the queue before we start sending. |
| 207 | EXPECT_EQ(peer_sending->send_message_queue_size(), 0u); |
| 208 | |
| 209 | // Send messages from peer 1 to peer 2 until required number of messages |
| 210 | // are queued in unsent message queue. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 211 | std::vector<std::string> sent_messages; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 212 | while (peer_sending->send_message_queue_size() < queue_size) { |
| 213 | sent_messages.push_back( |
| 214 | QuicStrCat("Sending message, index=", sent_messages.size())); |
| 215 | ASSERT_TRUE(peer_sending->SendOrQueueMessage(sent_messages.back())); |
| 216 | } |
| 217 | |
| 218 | // Wait for peer 2 to receive all messages. |
| 219 | RunTasks(); |
| 220 | |
| 221 | EXPECT_EQ(delegate_receiving->incoming_messages(), sent_messages); |
| 222 | } |
| 223 | |
| 224 | // Test sending long messages: |
| 225 | // - message of maximum allowed length should succeed |
| 226 | // - message of > maximum allowed length should fail. |
| 227 | void TestSendLongMessage() { |
| 228 | ASSERT_TRUE(server_peer_->CanSendMessage()); |
| 229 | ASSERT_TRUE(client_peer_->CanSendMessage()); |
| 230 | |
| 231 | // Send message of maximum allowed length. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 232 | std::string message_max_long = |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 233 | std::string(server_peer_->GetCurrentLargestMessagePayload(), 'A'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 234 | ASSERT_TRUE(server_peer_->SendOrQueueMessage(message_max_long)); |
| 235 | |
| 236 | // Send long message which should fail. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 237 | std::string message_too_long = |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 238 | std::string(server_peer_->GetCurrentLargestMessagePayload() + 1, 'B'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 239 | ASSERT_FALSE(server_peer_->SendOrQueueMessage(message_too_long)); |
| 240 | |
| 241 | // Wait for peer 2 to receive message. |
| 242 | RunTasks(); |
| 243 | |
| 244 | // Client should only receive one message of allowed length. |
| 245 | EXPECT_THAT(client_session_delegate_->incoming_messages(), |
| 246 | testing::ElementsAre(message_max_long)); |
| 247 | } |
| 248 | |
| 249 | // Test that client and server are not connected after handshake failure. |
| 250 | void TestDisconnectAfterFailedHandshake() { |
| 251 | EXPECT_TRUE(!client_session_delegate_->connected()); |
| 252 | EXPECT_TRUE(!server_session_delegate_->connected()); |
| 253 | |
| 254 | EXPECT_FALSE(client_peer_->IsEncryptionEstablished()); |
| 255 | EXPECT_FALSE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 256 | |
| 257 | EXPECT_FALSE(server_peer_->IsEncryptionEstablished()); |
| 258 | EXPECT_FALSE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 259 | } |
| 260 | |
| 261 | protected: |
| 262 | simulator::Simulator simulator_; |
| 263 | |
| 264 | std::unique_ptr<simulator::SimulatedQuartcPacketTransport> client_transport_; |
| 265 | std::unique_ptr<simulator::SimulatedQuartcPacketTransport> server_transport_; |
| 266 | std::unique_ptr<simulator::CountingPacketFilter> client_filter_; |
| 267 | std::unique_ptr<simulator::SymmetricLink> client_server_link_; |
| 268 | |
| 269 | std::unique_ptr<FakeQuartcStreamDelegate> client_stream_delegate_; |
| 270 | std::unique_ptr<FakeQuartcSessionDelegate> client_session_delegate_; |
| 271 | std::unique_ptr<FakeQuartcEndpointDelegate> client_endpoint_delegate_; |
| 272 | std::unique_ptr<FakeQuartcStreamDelegate> server_stream_delegate_; |
| 273 | std::unique_ptr<FakeQuartcSessionDelegate> server_session_delegate_; |
| 274 | std::unique_ptr<FakeQuartcEndpointDelegate> server_endpoint_delegate_; |
| 275 | |
| 276 | std::unique_ptr<QuartcClientEndpoint> client_endpoint_; |
| 277 | std::unique_ptr<QuartcServerEndpoint> server_endpoint_; |
| 278 | |
| 279 | QuartcSession* client_peer_ = nullptr; |
| 280 | QuartcSession* server_peer_ = nullptr; |
| 281 | }; |
| 282 | |
| 283 | TEST_F(QuartcSessionTest, SendReceiveStreams) { |
| 284 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 285 | AwaitHandshake(); |
| 286 | TestSendReceiveStreams(); |
| 287 | } |
| 288 | |
| 289 | TEST_F(QuartcSessionTest, SendReceiveMessages) { |
| 290 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 291 | AwaitHandshake(); |
| 292 | TestSendReceiveMessage(); |
| 293 | } |
| 294 | |
| 295 | TEST_F(QuartcSessionTest, SendReceiveQueuedMessages) { |
| 296 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 297 | AwaitHandshake(); |
| 298 | TestSendReceiveQueuedMessages(/*direction_from_server=*/true); |
| 299 | TestSendReceiveQueuedMessages(/*direction_from_server=*/false); |
| 300 | } |
| 301 | |
| 302 | TEST_F(QuartcSessionTest, SendMessageFails) { |
| 303 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 304 | AwaitHandshake(); |
| 305 | TestSendLongMessage(); |
| 306 | } |
| 307 | |
| 308 | TEST_F(QuartcSessionTest, TestCryptoHandshakeCanWriteTriggers) { |
| 309 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 310 | |
| 311 | AwaitHandshake(); |
| 312 | |
| 313 | RunTasks(); |
| 314 | |
| 315 | ASSERT_TRUE(client_session_delegate_->writable_time().IsInitialized()); |
| 316 | ASSERT_TRUE( |
| 317 | client_session_delegate_->crypto_handshake_time().IsInitialized()); |
| 318 | // On client, we are writable 1-rtt before crypto handshake is complete. |
| 319 | ASSERT_LT(client_session_delegate_->writable_time(), |
| 320 | client_session_delegate_->crypto_handshake_time()); |
| 321 | |
| 322 | ASSERT_TRUE(server_session_delegate_->writable_time().IsInitialized()); |
| 323 | ASSERT_TRUE( |
| 324 | server_session_delegate_->crypto_handshake_time().IsInitialized()); |
| 325 | // On server, the writable time and crypto handshake are the same. (when SHLO |
| 326 | // is sent). |
| 327 | ASSERT_EQ(server_session_delegate_->writable_time(), |
| 328 | server_session_delegate_->crypto_handshake_time()); |
| 329 | } |
| 330 | |
| 331 | TEST_F(QuartcSessionTest, PreSharedKeyHandshake) { |
| 332 | QuartcSessionConfig config; |
| 333 | config.pre_shared_key = "foo"; |
| 334 | CreateClientAndServerSessions(config); |
| 335 | AwaitHandshake(); |
| 336 | TestSendReceiveStreams(); |
| 337 | TestSendReceiveMessage(); |
| 338 | } |
| 339 | |
| 340 | // Test that data streams are not created before handshake. |
| 341 | TEST_F(QuartcSessionTest, CannotCreateDataStreamBeforeHandshake) { |
| 342 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 343 | EXPECT_EQ(nullptr, server_peer_->CreateOutgoingBidirectionalStream()); |
| 344 | EXPECT_EQ(nullptr, client_peer_->CreateOutgoingBidirectionalStream()); |
| 345 | } |
| 346 | |
| 347 | TEST_F(QuartcSessionTest, CancelQuartcStream) { |
| 348 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 349 | AwaitHandshake(); |
| 350 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 351 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 352 | |
| 353 | QuartcStream* stream = client_peer_->CreateOutgoingBidirectionalStream(); |
| 354 | ASSERT_NE(nullptr, stream); |
| 355 | |
| 356 | uint32_t id = stream->id(); |
| 357 | EXPECT_FALSE(client_peer_->IsClosedStream(id)); |
| 358 | stream->SetDelegate(client_stream_delegate_.get()); |
| 359 | client_peer_->CancelStream(id); |
| 360 | EXPECT_EQ(stream->stream_error(), |
| 361 | QuicRstStreamErrorCode::QUIC_STREAM_CANCELLED); |
| 362 | EXPECT_TRUE(client_peer_->IsClosedStream(id)); |
| 363 | } |
| 364 | |
| 365 | // TODO(b/112561077): This is the wrong layer for this test. We should write a |
| 366 | // test specifically for QuartcPacketWriter with a stubbed-out |
| 367 | // QuartcPacketTransport and remove |
| 368 | // SimulatedQuartcPacketTransport::last_packet_number(). |
| 369 | TEST_F(QuartcSessionTest, WriterGivesPacketNumberToTransport) { |
| 370 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 371 | AwaitHandshake(); |
| 372 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 373 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 374 | |
| 375 | QuartcStream* stream = client_peer_->CreateOutgoingBidirectionalStream(); |
| 376 | stream->SetDelegate(client_stream_delegate_.get()); |
| 377 | |
| 378 | char kClientMessage[] = "Hello"; |
| 379 | test::QuicTestMemSliceVector stream_data( |
| 380 | {std::make_pair(kClientMessage, strlen(kClientMessage))}); |
| 381 | stream->WriteMemSlices(stream_data.span(), /*fin=*/false); |
| 382 | RunTasks(); |
| 383 | |
| 384 | // The transport should see the latest packet number sent by QUIC. |
| 385 | EXPECT_EQ( |
| 386 | client_transport_->last_packet_number(), |
| 387 | client_peer_->connection()->sent_packet_manager().GetLargestSentPacket()); |
| 388 | } |
| 389 | |
| 390 | TEST_F(QuartcSessionTest, CloseConnection) { |
| 391 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 392 | AwaitHandshake(); |
| 393 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 394 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 395 | |
| 396 | client_peer_->CloseConnection("Connection closed by client"); |
| 397 | EXPECT_FALSE(client_session_delegate_->connected()); |
| 398 | RunTasks(); |
| 399 | EXPECT_FALSE(server_session_delegate_->connected()); |
| 400 | } |
| 401 | |
| 402 | TEST_F(QuartcSessionTest, StreamRetransmissionEnabled) { |
| 403 | CreateClientAndServerSessions(QuartcSessionConfig()); |
| 404 | AwaitHandshake(); |
| 405 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 406 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 407 | |
| 408 | QuartcStream* stream = client_peer_->CreateOutgoingBidirectionalStream(); |
| 409 | QuicStreamId stream_id = stream->id(); |
| 410 | stream->SetDelegate(client_stream_delegate_.get()); |
| 411 | stream->set_cancel_on_loss(false); |
| 412 | |
| 413 | client_filter_->set_packets_to_drop(1); |
| 414 | |
| 415 | char kClientMessage[] = "Hello"; |
| 416 | test::QuicTestMemSliceVector stream_data( |
| 417 | {std::make_pair(kClientMessage, strlen(kClientMessage))}); |
| 418 | stream->WriteMemSlices(stream_data.span(), /*fin=*/false); |
| 419 | RunTasks(); |
| 420 | |
| 421 | // Stream data should make it despite packet loss. |
| 422 | ASSERT_TRUE(server_stream_delegate_->has_data()); |
| 423 | EXPECT_EQ(server_stream_delegate_->data()[stream_id], kClientMessage); |
| 424 | } |
| 425 | |
| 426 | TEST_F(QuartcSessionTest, StreamRetransmissionDisabled) { |
| 427 | // Disable tail loss probe, otherwise test maybe flaky because dropped |
| 428 | // message will be retransmitted to detect tail loss. |
| 429 | QuartcSessionConfig session_config; |
| 430 | session_config.enable_tail_loss_probe = false; |
| 431 | CreateClientAndServerSessions(session_config); |
| 432 | |
| 433 | // Disable probing retransmissions, otherwise test maybe flaky because dropped |
| 434 | // message will be retransmitted to to probe for more bandwidth. |
| 435 | client_peer_->connection()->set_fill_up_link_during_probing(false); |
| 436 | |
| 437 | AwaitHandshake(); |
| 438 | ASSERT_TRUE(client_peer_->IsCryptoHandshakeConfirmed()); |
| 439 | ASSERT_TRUE(server_peer_->IsCryptoHandshakeConfirmed()); |
| 440 | |
| 441 | // The client sends an ACK for the crypto handshake next. This must be |
| 442 | // flushed before we set the filter to drop the next packet, in order to |
| 443 | // ensure that the filter drops a data-bearing packet instead of just an ack. |
| 444 | RunTasks(); |
| 445 | |
| 446 | QuartcStream* stream = client_peer_->CreateOutgoingBidirectionalStream(); |
| 447 | QuicStreamId stream_id = stream->id(); |
| 448 | stream->SetDelegate(client_stream_delegate_.get()); |
| 449 | stream->set_cancel_on_loss(true); |
| 450 | |
| 451 | client_filter_->set_packets_to_drop(1); |
| 452 | |
| 453 | char kMessage[] = "Hello"; |
| 454 | test::QuicTestMemSliceVector stream_data( |
| 455 | {std::make_pair(kMessage, strlen(kMessage))}); |
| 456 | stream->WriteMemSlices(stream_data.span(), /*fin=*/false); |
| 457 | simulator_.RunFor(QuicTime::Delta::FromMilliseconds(1)); |
| 458 | |
| 459 | // Send another packet to trigger loss detection. |
| 460 | QuartcStream* stream_1 = client_peer_->CreateOutgoingBidirectionalStream(); |
| 461 | stream_1->SetDelegate(client_stream_delegate_.get()); |
| 462 | |
| 463 | char kMessage1[] = "Second message"; |
| 464 | test::QuicTestMemSliceVector stream_data_1( |
| 465 | {std::make_pair(kMessage1, strlen(kMessage1))}); |
| 466 | stream_1->WriteMemSlices(stream_data_1.span(), /*fin=*/false); |
| 467 | RunTasks(); |
| 468 | |
| 469 | // QUIC should try to retransmit the first stream by loss detection. Instead, |
| 470 | // it will cancel itself. |
| 471 | EXPECT_THAT(server_stream_delegate_->data()[stream_id], testing::IsEmpty()); |
| 472 | |
| 473 | EXPECT_TRUE(client_peer_->IsClosedStream(stream_id)); |
| 474 | EXPECT_TRUE(server_peer_->IsClosedStream(stream_id)); |
| 475 | EXPECT_EQ(client_stream_delegate_->stream_error(stream_id), |
| 476 | QUIC_STREAM_CANCELLED); |
| 477 | EXPECT_EQ(server_stream_delegate_->stream_error(stream_id), |
| 478 | QUIC_STREAM_CANCELLED); |
| 479 | } |
| 480 | |
| 481 | TEST_F(QuartcSessionTest, ServerRegistersAsWriteBlocked) { |
| 482 | // Initialize client and server session, but with the server write-blocked. |
| 483 | Init(); |
| 484 | server_transport_->SetWritable(false); |
| 485 | CreateClientAndServerSessions(QuartcSessionConfig(), /*init=*/false); |
| 486 | |
| 487 | // Let the client send a few copies of the CHLO. The server can't respond, as |
| 488 | // it's still write-blocked. |
| 489 | RunTasks(); |
| 490 | |
| 491 | // Making the server's transport writable should trigger a callback that |
| 492 | // reaches the server session, allowing it to write packets. |
| 493 | server_transport_->SetWritable(true); |
| 494 | |
| 495 | // Now the server should respond with the SHLO, encryption should be |
| 496 | // established, and data should flow normally. |
| 497 | // Note that if the server is *not* correctly registered as write-blocked, |
| 498 | // it will crash here (see b/124527328 for details). |
| 499 | AwaitHandshake(); |
| 500 | TestSendReceiveStreams(); |
| 501 | } |
| 502 | |
| 503 | TEST_F(QuartcSessionTest, PreSharedKeyHandshakeIs0RTT) { |
| 504 | QuartcSessionConfig session_config; |
| 505 | session_config.pre_shared_key = "foo"; |
| 506 | |
| 507 | // Client endpoint is created below. Destructing client endpoint |
| 508 | // causes issues with the simulator. |
| 509 | Init(/*create_client_endpoint=*/false); |
| 510 | |
| 511 | server_endpoint_->Connect(server_transport_.get()); |
| 512 | |
| 513 | client_endpoint_ = QuicMakeUnique<QuartcClientEndpoint>( |
| 514 | simulator_.GetAlarmFactory(), simulator_.GetClock(), |
QUICHE team | 73957f1 | 2019-04-18 16:21:52 -0700 | [diff] [blame] | 515 | simulator_.GetRandomGenerator(), client_endpoint_delegate_.get(), |
| 516 | QuartcSessionConfig(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 517 | // This is the key line here. It passes through the server config |
| 518 | // from the server to the client. |
| 519 | server_endpoint_->server_crypto_config()); |
| 520 | |
| 521 | client_endpoint_->Connect(client_transport_.get()); |
| 522 | |
| 523 | // Running for 1ms. This is shorter than the RTT, so the |
| 524 | // client session should be created, but server won't be created yet. |
| 525 | simulator_.RunFor(QuicTime::Delta::FromMilliseconds(1)); |
| 526 | |
| 527 | client_peer_ = client_endpoint_delegate_->session(); |
| 528 | server_peer_ = server_endpoint_delegate_->session(); |
| 529 | |
| 530 | ASSERT_NE(client_peer_, nullptr); |
| 531 | ASSERT_EQ(server_peer_, nullptr); |
| 532 | |
| 533 | // Write data to the client before running tasks. This should be sent by the |
| 534 | // client and received by the server if the handshake is 0RTT. |
| 535 | // If this test fails, add 'RunTasks()' above, and see what error is sent |
| 536 | // by the server in the rejection message. |
| 537 | QuartcStream* stream = client_peer_->CreateOutgoingBidirectionalStream(); |
| 538 | ASSERT_NE(stream, nullptr); |
| 539 | QuicStreamId stream_id = stream->id(); |
| 540 | stream->SetDelegate(client_stream_delegate_.get()); |
| 541 | |
| 542 | char message[] = "Hello in 0RTTs!"; |
| 543 | test::QuicTestMemSliceVector data({std::make_pair(message, strlen(message))}); |
| 544 | stream->WriteMemSlices(data.span(), /*fin=*/false); |
| 545 | |
| 546 | // This will now run the rest of the connection. But the |
| 547 | // Server peer will receive the CHLO and message after 1 delay. |
| 548 | simulator_.RunFor(kPropagationDelayAndABit); |
| 549 | |
| 550 | // If we can decrypt the data, it means that 0 rtt was successful. |
| 551 | // This is because we waited only a propagation delay. So if the decryption |
| 552 | // failed, we would send sREJ instead of SHLO, but it wouldn't be delivered to |
| 553 | // the client yet. |
| 554 | ASSERT_TRUE(server_stream_delegate_->has_data()); |
| 555 | EXPECT_EQ(server_stream_delegate_->data()[stream_id], message); |
| 556 | } |
| 557 | |
| 558 | } // namespace |
| 559 | |
| 560 | } // namespace quic |