vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/quic_transport/quic_transport_client_session.h" |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 8 | #include <utility> |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 9 | |
vasilvv | 035fe3d | 2020-10-20 08:38:37 -0700 | [diff] [blame] | 10 | #include "absl/base/macros.h" |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 11 | #include "url/gurl.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 12 | #include "quic/core/quic_data_writer.h" |
| 13 | #include "quic/core/quic_server_id.h" |
| 14 | #include "quic/core/quic_types.h" |
| 15 | #include "quic/core/quic_utils.h" |
| 16 | #include "quic/platform/api/quic_expect_bug.h" |
| 17 | #include "quic/platform/api/quic_test.h" |
| 18 | #include "quic/test_tools/crypto_test_utils.h" |
| 19 | #include "quic/test_tools/quic_session_peer.h" |
| 20 | #include "quic/test_tools/quic_stream_peer.h" |
| 21 | #include "quic/test_tools/quic_test_utils.h" |
| 22 | #include "quic/test_tools/quic_transport_test_tools.h" |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 23 | |
| 24 | namespace quic { |
| 25 | namespace test { |
| 26 | namespace { |
| 27 | |
| 28 | using testing::_; |
| 29 | using testing::ElementsAre; |
vasilvv | da373d4 | 2020-01-16 12:36:27 -0800 | [diff] [blame] | 30 | using testing::Eq; |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 31 | |
| 32 | const char* kTestOrigin = "https://test-origin.test"; |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 33 | url::Origin GetTestOrigin() { |
| 34 | GURL origin_url(kTestOrigin); |
| 35 | return url::Origin::Create(origin_url); |
| 36 | } |
| 37 | |
| 38 | ParsedQuicVersionVector GetVersions() { |
vasilvv | 7e5e609 | 2020-03-02 15:52:33 -0800 | [diff] [blame] | 39 | return {DefaultVersionForQuicTransport()}; |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 40 | } |
| 41 | |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 42 | std::string DataInStream(QuicStream* stream) { |
| 43 | QuicStreamSendBuffer& send_buffer = QuicStreamPeer::SendBuffer(stream); |
| 44 | std::string result; |
| 45 | result.resize(send_buffer.stream_offset()); |
| 46 | QuicDataWriter writer(result.size(), &result[0]); |
| 47 | EXPECT_TRUE( |
| 48 | send_buffer.WriteStreamData(0, send_buffer.stream_offset(), &writer)); |
| 49 | return result; |
| 50 | } |
| 51 | |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 52 | class QuicTransportClientSessionTest : public QuicTest { |
| 53 | protected: |
| 54 | QuicTransportClientSessionTest() |
| 55 | : connection_(&helper_, |
| 56 | &alarm_factory_, |
| 57 | Perspective::IS_CLIENT, |
| 58 | GetVersions()), |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 59 | crypto_config_(crypto_test_utils::ProofVerifierForTesting()) { |
vasilvv | 7e5e609 | 2020-03-02 15:52:33 -0800 | [diff] [blame] | 60 | QuicEnableVersion(DefaultVersionForQuicTransport()); |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 61 | CreateSession(GetTestOrigin(), ""); |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 64 | void CreateSession(url::Origin origin, std::string url_suffix) { |
vasilvv | dfbd3df | 2019-11-01 11:58:43 -0700 | [diff] [blame] | 65 | session_ = std::make_unique<QuicTransportClientSession>( |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 66 | &connection_, nullptr, DefaultQuicConfig(), GetVersions(), |
| 67 | GURL("quic-transport://test.example.com:50000" + url_suffix), |
QUICHE team | 20456ed | 2020-11-18 22:24:42 -0800 | [diff] [blame] | 68 | &crypto_config_, origin, &visitor_, /*datagram_observer=*/nullptr); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 69 | session_->Initialize(); |
| 70 | crypto_stream_ = static_cast<QuicCryptoClientStream*>( |
| 71 | session_->GetMutableCryptoStream()); |
| 72 | } |
| 73 | |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 74 | void Connect() { |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 75 | session_->CryptoConnect(); |
| 76 | QuicConfig server_config = DefaultQuicConfig(); |
rch | 83f29bd | 2019-11-13 11:47:34 -0800 | [diff] [blame] | 77 | std::unique_ptr<QuicCryptoServerConfig> crypto_config( |
| 78 | crypto_test_utils::CryptoServerConfigForTesting()); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 79 | crypto_test_utils::HandshakeWithFakeServer( |
rch | 83f29bd | 2019-11-13 11:47:34 -0800 | [diff] [blame] | 80 | &server_config, crypto_config.get(), &helper_, &alarm_factory_, |
| 81 | &connection_, crypto_stream_, QuicTransportAlpn()); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | MockAlarmFactory alarm_factory_; |
| 85 | MockQuicConnectionHelper helper_; |
| 86 | |
| 87 | PacketSavingConnection connection_; |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 88 | QuicCryptoClientConfig crypto_config_; |
vasilvv | dfbd3df | 2019-11-01 11:58:43 -0700 | [diff] [blame] | 89 | MockClientVisitor visitor_; |
| 90 | std::unique_ptr<QuicTransportClientSession> session_; |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 91 | QuicCryptoClientStream* crypto_stream_; |
| 92 | }; |
| 93 | |
| 94 | TEST_F(QuicTransportClientSessionTest, HasValidAlpn) { |
vasilvv | eba61c4 | 2019-10-08 14:23:14 -0700 | [diff] [blame] | 95 | EXPECT_THAT(session_->GetAlpnsToOffer(), ElementsAre(QuicTransportAlpn())); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(QuicTransportClientSessionTest, SuccessfulConnection) { |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 99 | constexpr char kTestOriginClientIndication[] = |
| 100 | "\0\0" // key (0x0000, origin) |
| 101 | "\0\x18" // length |
| 102 | "https://test-origin.test" // value |
| 103 | "\0\x01" // key (0x0001, path) |
| 104 | "\0\x01" // length |
| 105 | "/"; // value |
| 106 | |
vasilvv | 467b422 | 2019-12-09 16:22:11 -0800 | [diff] [blame] | 107 | EXPECT_CALL(visitor_, OnSessionReady()); |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 108 | Connect(); |
| 109 | EXPECT_TRUE(session_->IsSessionReady()); |
| 110 | |
renjietang | 647b3cf | 2020-08-04 13:23:12 -0700 | [diff] [blame] | 111 | QuicStream* client_indication_stream; |
renjietang | 4c33482 | 2020-09-28 15:44:55 -0700 | [diff] [blame] | 112 | client_indication_stream = |
| 113 | QuicSessionPeer::stream_map(session_.get())[ClientIndicationStream()] |
| 114 | .get(); |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 115 | ASSERT_TRUE(client_indication_stream != nullptr); |
| 116 | const std::string client_indication = DataInStream(client_indication_stream); |
| 117 | const std::string expected_client_indication{ |
| 118 | kTestOriginClientIndication, |
vasilvv | 035fe3d | 2020-10-20 08:38:37 -0700 | [diff] [blame] | 119 | ABSL_ARRAYSIZE(kTestOriginClientIndication) - 1}; |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 120 | EXPECT_EQ(client_indication, expected_client_indication); |
| 121 | } |
| 122 | |
| 123 | TEST_F(QuicTransportClientSessionTest, SuccessfulConnectionWithPath) { |
| 124 | constexpr char kSuffix[] = "/foo/bar?hello=world#not-sent"; |
| 125 | constexpr char kTestOriginClientIndication[] = |
| 126 | "\0\0" // key (0x0000, origin) |
| 127 | "\0\x18" // length |
| 128 | "https://test-origin.test" // value |
| 129 | "\0\x01" // key (0x0001, path) |
| 130 | "\0\x14" // length |
| 131 | "/foo/bar?hello=world"; // value |
| 132 | |
| 133 | CreateSession(GetTestOrigin(), kSuffix); |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 134 | Connect(); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 135 | EXPECT_TRUE(session_->IsSessionReady()); |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 136 | |
renjietang | 647b3cf | 2020-08-04 13:23:12 -0700 | [diff] [blame] | 137 | QuicStream* client_indication_stream; |
renjietang | 4c33482 | 2020-09-28 15:44:55 -0700 | [diff] [blame] | 138 | client_indication_stream = |
| 139 | QuicSessionPeer::stream_map(session_.get())[ClientIndicationStream()] |
| 140 | .get(); |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 141 | ASSERT_TRUE(client_indication_stream != nullptr); |
| 142 | const std::string client_indication = DataInStream(client_indication_stream); |
| 143 | const std::string expected_client_indication{ |
| 144 | kTestOriginClientIndication, |
vasilvv | 035fe3d | 2020-10-20 08:38:37 -0700 | [diff] [blame] | 145 | ABSL_ARRAYSIZE(kTestOriginClientIndication) - 1}; |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 146 | EXPECT_EQ(client_indication, expected_client_indication); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 147 | } |
| 148 | |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 149 | TEST_F(QuicTransportClientSessionTest, OriginTooLong) { |
| 150 | std::string long_string(68000, 'a'); |
| 151 | GURL bad_origin_url{"https://" + long_string + ".example/"}; |
| 152 | EXPECT_TRUE(bad_origin_url.is_valid()); |
vasilvv | e58d0f1 | 2019-12-04 14:35:25 -0800 | [diff] [blame] | 153 | CreateSession(url::Origin::Create(bad_origin_url), ""); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 154 | |
vasilvv | ddf5247 | 2019-10-04 15:14:02 -0700 | [diff] [blame] | 155 | EXPECT_QUIC_BUG(Connect(), "Client origin too long"); |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 156 | } |
| 157 | |
vasilvv | dfbd3df | 2019-11-01 11:58:43 -0700 | [diff] [blame] | 158 | TEST_F(QuicTransportClientSessionTest, ReceiveNewStreams) { |
| 159 | Connect(); |
| 160 | ASSERT_TRUE(session_->IsSessionReady()); |
| 161 | ASSERT_TRUE(session_->AcceptIncomingUnidirectionalStream() == nullptr); |
| 162 | |
| 163 | const QuicStreamId id = GetNthServerInitiatedUnidirectionalStreamId( |
| 164 | session_->transport_version(), 0); |
| 165 | QuicStreamFrame frame(id, /*fin=*/false, /*offset=*/0, "test"); |
| 166 | EXPECT_CALL(visitor_, OnIncomingUnidirectionalStreamAvailable()).Times(1); |
| 167 | session_->OnStreamFrame(frame); |
| 168 | |
| 169 | QuicTransportStream* stream = session_->AcceptIncomingUnidirectionalStream(); |
| 170 | ASSERT_TRUE(stream != nullptr); |
| 171 | EXPECT_EQ(stream->ReadableBytes(), 4u); |
| 172 | EXPECT_EQ(stream->id(), id); |
| 173 | } |
| 174 | |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 175 | TEST_F(QuicTransportClientSessionTest, ReceiveDatagram) { |
vasilvv | da373d4 | 2020-01-16 12:36:27 -0800 | [diff] [blame] | 176 | EXPECT_CALL(visitor_, OnDatagramReceived(Eq("test"))); |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 177 | session_->OnMessageReceived("test"); |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 178 | } |
| 179 | |
vasilvv | e6472f6 | 2019-10-02 06:50:56 -0700 | [diff] [blame] | 180 | } // namespace |
| 181 | } // namespace test |
| 182 | } // namespace quic |