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 | |
| 5 | #include "net/third_party/quiche/src/quic/quic_transport/quic_transport_client_session.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "url/gurl.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_server_id.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 14 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 15 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 16 | |
| 17 | namespace quic { |
| 18 | namespace test { |
| 19 | namespace { |
| 20 | |
| 21 | using testing::_; |
| 22 | using testing::ElementsAre; |
| 23 | |
| 24 | const char* kTestOrigin = "https://test-origin.test"; |
| 25 | const char* kTestOriginInsecure = "http://test-origin.test"; |
| 26 | url::Origin GetTestOrigin() { |
| 27 | GURL origin_url(kTestOrigin); |
| 28 | return url::Origin::Create(origin_url); |
| 29 | } |
| 30 | |
| 31 | ParsedQuicVersionVector GetVersions() { |
| 32 | return {ParsedQuicVersion{PROTOCOL_TLS1_3, QUIC_VERSION_99}}; |
| 33 | } |
| 34 | |
| 35 | class TestClientSession : public QuicTransportClientSession { |
| 36 | public: |
| 37 | using QuicTransportClientSession::QuicTransportClientSession; |
| 38 | |
| 39 | class Stream : public QuicStream { |
| 40 | public: |
| 41 | using QuicStream::QuicStream; |
| 42 | void OnDataAvailable() override {} |
| 43 | }; |
| 44 | |
| 45 | QuicStream* CreateIncomingStream(QuicStreamId id) override { |
| 46 | auto stream = std::make_unique<Stream>( |
| 47 | id, this, /*is_static=*/false, |
| 48 | QuicUtils::GetStreamType(id, connection()->perspective(), |
| 49 | /*peer_initiated=*/true)); |
| 50 | QuicStream* result = stream.get(); |
| 51 | ActivateStream(std::move(stream)); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | QuicStream* CreateIncomingStream(PendingStream* /*pending*/) override { |
| 56 | QUIC_NOTREACHED(); |
| 57 | return nullptr; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class QuicTransportClientSessionTest : public QuicTest { |
| 62 | protected: |
| 63 | QuicTransportClientSessionTest() |
| 64 | : connection_(&helper_, |
| 65 | &alarm_factory_, |
| 66 | Perspective::IS_CLIENT, |
| 67 | GetVersions()), |
| 68 | server_id_("test.example.com", 443), |
| 69 | crypto_config_(crypto_test_utils::ProofVerifierForTesting()) { |
| 70 | SetQuicReloadableFlag(quic_supports_tls_handshake, true); |
| 71 | session_ = std::make_unique<TestClientSession>( |
| 72 | &connection_, nullptr, DefaultQuicConfig(), GetVersions(), server_id_, |
| 73 | &crypto_config_, GetTestOrigin()); |
| 74 | session_->Initialize(); |
| 75 | crypto_stream_ = static_cast<QuicCryptoClientStream*>( |
| 76 | session_->GetMutableCryptoStream()); |
| 77 | } |
| 78 | |
| 79 | void ConnectWithOriginList(std::string accepted_origins) { |
| 80 | session_->CryptoConnect(); |
| 81 | QuicConfig server_config = DefaultQuicConfig(); |
| 82 | server_config |
| 83 | .custom_transport_parameters_to_send()[WebAcceptedOriginsParameter()] = |
| 84 | accepted_origins; |
| 85 | crypto_test_utils::HandshakeWithFakeServer( |
| 86 | &server_config, &helper_, &alarm_factory_, &connection_, crypto_stream_, |
| 87 | kQuicTransportAlpn); |
| 88 | } |
| 89 | |
| 90 | MockAlarmFactory alarm_factory_; |
| 91 | MockQuicConnectionHelper helper_; |
| 92 | |
| 93 | PacketSavingConnection connection_; |
| 94 | QuicServerId server_id_; |
| 95 | QuicCryptoClientConfig crypto_config_; |
| 96 | std::unique_ptr<TestClientSession> session_; |
| 97 | QuicCryptoClientStream* crypto_stream_; |
| 98 | }; |
| 99 | |
| 100 | TEST_F(QuicTransportClientSessionTest, HasValidAlpn) { |
| 101 | EXPECT_THAT(session_->GetAlpnsToOffer(), ElementsAre(kQuicTransportAlpn)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(QuicTransportClientSessionTest, SuccessfulConnection) { |
| 105 | ConnectWithOriginList(GetTestOrigin().Serialize()); |
| 106 | EXPECT_TRUE(session_->IsSessionReady()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(QuicTransportClientSessionTest, SuccessfulConnectionManyOrigins) { |
| 110 | ConnectWithOriginList( |
| 111 | QuicStrCat("http://example.org,", kTestOrigin, ",https://example.com")); |
| 112 | EXPECT_TRUE(session_->IsSessionReady()); |
| 113 | } |
| 114 | |
| 115 | TEST_F(QuicTransportClientSessionTest, SuccessfulConnectionWildcardOrigin) { |
| 116 | ConnectWithOriginList("*"); |
| 117 | EXPECT_TRUE(session_->IsSessionReady()); |
| 118 | } |
| 119 | |
| 120 | TEST_F(QuicTransportClientSessionTest, OriginMismatch) { |
| 121 | EXPECT_CALL(connection_, |
| 122 | CloseConnection(_, "QuicTransport origin check failed", _)); |
| 123 | ConnectWithOriginList("https://obviously-wrong-website.test"); |
| 124 | EXPECT_FALSE(session_->IsSessionReady()); |
| 125 | } |
| 126 | |
| 127 | TEST_F(QuicTransportClientSessionTest, OriginSchemaMismatch) { |
| 128 | EXPECT_CALL(connection_, |
| 129 | CloseConnection(_, "QuicTransport origin check failed", _)); |
| 130 | ConnectWithOriginList(kTestOriginInsecure); |
| 131 | EXPECT_FALSE(session_->IsSessionReady()); |
| 132 | } |
| 133 | |
| 134 | TEST_F(QuicTransportClientSessionTest, OriginListMissing) { |
| 135 | EXPECT_CALL( |
| 136 | connection_, |
| 137 | CloseConnection( |
| 138 | _, "QuicTransport requires web_accepted_origins transport parameter", |
| 139 | _)); |
| 140 | session_->CryptoConnect(); |
| 141 | QuicConfig server_config = DefaultQuicConfig(); |
| 142 | crypto_test_utils::HandshakeWithFakeServer( |
| 143 | &server_config, &helper_, &alarm_factory_, &connection_, crypto_stream_, |
| 144 | kQuicTransportAlpn); |
| 145 | EXPECT_FALSE(session_->IsSessionReady()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(QuicTransportClientSessionTest, OriginListEmpty) { |
| 149 | EXPECT_CALL(connection_, |
| 150 | CloseConnection(_, "QuicTransport origin check failed", _)); |
| 151 | ConnectWithOriginList(""); |
| 152 | EXPECT_FALSE(session_->IsSessionReady()); |
| 153 | } |
| 154 | |
| 155 | } // namespace |
| 156 | } // namespace test |
| 157 | } // namespace quic |