QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015 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/test_tools/quic_test_server.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/quic_epoll_alarm_factory.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_epoll_connection_helper.h" |
| 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 10 | #include "net/third_party/quiche/src/quic/tools/quic_simple_crypto_server_stream_helper.h" |
| 11 | #include "net/third_party/quiche/src/quic/tools/quic_simple_dispatcher.h" |
| 12 | #include "net/third_party/quiche/src/quic/tools/quic_simple_server_session.h" |
| 13 | |
| 14 | namespace quic { |
| 15 | |
| 16 | namespace test { |
| 17 | |
| 18 | class CustomStreamSession : public QuicSimpleServerSession { |
| 19 | public: |
| 20 | CustomStreamSession( |
| 21 | const QuicConfig& config, |
| 22 | const ParsedQuicVersionVector& supported_versions, |
| 23 | QuicConnection* connection, |
| 24 | QuicSession::Visitor* visitor, |
| 25 | QuicCryptoServerStream::Helper* helper, |
| 26 | const QuicCryptoServerConfig* crypto_config, |
| 27 | QuicCompressedCertsCache* compressed_certs_cache, |
| 28 | QuicTestServer::StreamFactory* stream_factory, |
| 29 | QuicTestServer::CryptoStreamFactory* crypto_stream_factory, |
| 30 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 31 | : QuicSimpleServerSession(config, |
| 32 | supported_versions, |
| 33 | connection, |
| 34 | visitor, |
| 35 | helper, |
| 36 | crypto_config, |
| 37 | compressed_certs_cache, |
| 38 | quic_simple_server_backend), |
| 39 | stream_factory_(stream_factory), |
| 40 | crypto_stream_factory_(crypto_stream_factory) {} |
| 41 | |
| 42 | QuicSpdyStream* CreateIncomingStream(QuicStreamId id) override { |
| 43 | if (!ShouldCreateIncomingStream(id)) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | if (stream_factory_) { |
| 47 | QuicSpdyStream* stream = |
| 48 | stream_factory_->CreateStream(id, this, server_backend()); |
| 49 | ActivateStream(QuicWrapUnique(stream)); |
| 50 | return stream; |
| 51 | } |
| 52 | return QuicSimpleServerSession::CreateIncomingStream(id); |
| 53 | } |
| 54 | |
| 55 | QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( |
| 56 | const QuicCryptoServerConfig* crypto_config, |
| 57 | QuicCompressedCertsCache* compressed_certs_cache) override { |
| 58 | if (crypto_stream_factory_) { |
| 59 | return crypto_stream_factory_->CreateCryptoStream(crypto_config, this); |
| 60 | } |
| 61 | return QuicSimpleServerSession::CreateQuicCryptoServerStream( |
| 62 | crypto_config, compressed_certs_cache); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
| 67 | QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
| 68 | }; |
| 69 | |
| 70 | class QuicTestDispatcher : public QuicSimpleDispatcher { |
| 71 | public: |
| 72 | QuicTestDispatcher( |
| 73 | const QuicConfig* config, |
| 74 | const QuicCryptoServerConfig* crypto_config, |
| 75 | QuicVersionManager* version_manager, |
| 76 | std::unique_ptr<QuicConnectionHelperInterface> helper, |
| 77 | std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |
| 78 | std::unique_ptr<QuicAlarmFactory> alarm_factory, |
| 79 | QuicSimpleServerBackend* quic_simple_server_backend, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 80 | uint8_t expected_server_connection_id_length) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | : QuicSimpleDispatcher(config, |
| 82 | crypto_config, |
| 83 | version_manager, |
| 84 | std::move(helper), |
| 85 | std::move(session_helper), |
| 86 | std::move(alarm_factory), |
| 87 | quic_simple_server_backend, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 88 | expected_server_connection_id_length), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | session_factory_(nullptr), |
| 90 | stream_factory_(nullptr), |
| 91 | crypto_stream_factory_(nullptr) {} |
| 92 | |
| 93 | QuicServerSessionBase* CreateQuicSession( |
| 94 | QuicConnectionId id, |
| 95 | const QuicSocketAddress& client, |
| 96 | QuicStringPiece alpn, |
| 97 | const ParsedQuicVersion& version) override { |
| 98 | QuicReaderMutexLock lock(&factory_lock_); |
| 99 | if (session_factory_ == nullptr && stream_factory_ == nullptr && |
| 100 | crypto_stream_factory_ == nullptr) { |
| 101 | return QuicSimpleDispatcher::CreateQuicSession(id, client, alpn, version); |
| 102 | } |
| 103 | QuicConnection* connection = |
| 104 | new QuicConnection(id, client, helper(), alarm_factory(), writer(), |
| 105 | /* owns_writer= */ false, Perspective::IS_SERVER, |
| 106 | ParsedQuicVersionVector{version}); |
| 107 | |
| 108 | QuicServerSessionBase* session = nullptr; |
| 109 | if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { |
| 110 | session = new CustomStreamSession( |
| 111 | config(), GetSupportedVersions(), connection, this, session_helper(), |
| 112 | crypto_config(), compressed_certs_cache(), stream_factory_, |
| 113 | crypto_stream_factory_, server_backend()); |
| 114 | } else { |
| 115 | session = session_factory_->CreateSession( |
| 116 | config(), connection, this, session_helper(), crypto_config(), |
| 117 | compressed_certs_cache(), server_backend()); |
| 118 | } |
| 119 | session->Initialize(); |
| 120 | return session; |
| 121 | } |
| 122 | |
| 123 | void SetSessionFactory(QuicTestServer::SessionFactory* factory) { |
| 124 | QuicWriterMutexLock lock(&factory_lock_); |
| 125 | DCHECK(session_factory_ == nullptr); |
| 126 | DCHECK(stream_factory_ == nullptr); |
| 127 | DCHECK(crypto_stream_factory_ == nullptr); |
| 128 | session_factory_ = factory; |
| 129 | } |
| 130 | |
| 131 | void SetStreamFactory(QuicTestServer::StreamFactory* factory) { |
| 132 | QuicWriterMutexLock lock(&factory_lock_); |
| 133 | DCHECK(session_factory_ == nullptr); |
| 134 | DCHECK(stream_factory_ == nullptr); |
| 135 | stream_factory_ = factory; |
| 136 | } |
| 137 | |
| 138 | void SetCryptoStreamFactory(QuicTestServer::CryptoStreamFactory* factory) { |
| 139 | QuicWriterMutexLock lock(&factory_lock_); |
| 140 | DCHECK(session_factory_ == nullptr); |
| 141 | DCHECK(crypto_stream_factory_ == nullptr); |
| 142 | crypto_stream_factory_ = factory; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | QuicMutex factory_lock_; |
| 147 | QuicTestServer::SessionFactory* session_factory_; // Not owned. |
| 148 | QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
| 149 | QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
| 150 | }; |
| 151 | |
| 152 | QuicTestServer::QuicTestServer( |
| 153 | std::unique_ptr<ProofSource> proof_source, |
| 154 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 155 | : QuicServer(std::move(proof_source), quic_simple_server_backend) {} |
| 156 | |
| 157 | QuicTestServer::QuicTestServer( |
| 158 | std::unique_ptr<ProofSource> proof_source, |
| 159 | const QuicConfig& config, |
| 160 | const ParsedQuicVersionVector& supported_versions, |
| 161 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 162 | : QuicTestServer(std::move(proof_source), |
| 163 | config, |
| 164 | supported_versions, |
| 165 | quic_simple_server_backend, |
| 166 | kQuicDefaultConnectionIdLength) {} |
| 167 | |
| 168 | QuicTestServer::QuicTestServer( |
| 169 | std::unique_ptr<ProofSource> proof_source, |
| 170 | const QuicConfig& config, |
| 171 | const ParsedQuicVersionVector& supported_versions, |
| 172 | QuicSimpleServerBackend* quic_simple_server_backend, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 173 | uint8_t expected_server_connection_id_length) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | : QuicServer(std::move(proof_source), |
| 175 | config, |
| 176 | QuicCryptoServerConfig::ConfigOptions(), |
| 177 | supported_versions, |
| 178 | quic_simple_server_backend, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 179 | expected_server_connection_id_length) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 180 | |
| 181 | QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { |
| 182 | return new QuicTestDispatcher( |
| 183 | &config(), &crypto_config(), version_manager(), |
| 184 | QuicMakeUnique<QuicEpollConnectionHelper>(epoll_server(), |
| 185 | QuicAllocator::BUFFER_POOL), |
| 186 | std::unique_ptr<QuicCryptoServerStream::Helper>( |
wub | 662a3d6 | 2019-08-16 14:10:50 -0700 | [diff] [blame] | 187 | new QuicSimpleCryptoServerStreamHelper()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 188 | QuicMakeUnique<QuicEpollAlarmFactory>(epoll_server()), server_backend(), |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 189 | expected_server_connection_id_length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void QuicTestServer::SetSessionFactory(SessionFactory* factory) { |
| 193 | DCHECK(dispatcher()); |
| 194 | static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); |
| 195 | } |
| 196 | |
| 197 | void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { |
| 198 | static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); |
| 199 | } |
| 200 | |
| 201 | void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { |
| 202 | static_cast<QuicTestDispatcher*>(dispatcher()) |
| 203 | ->SetCryptoStreamFactory(factory); |
| 204 | } |
| 205 | |
| 206 | /////////////////////////// TEST SESSIONS /////////////////////////////// |
| 207 | |
| 208 | ImmediateGoAwaySession::ImmediateGoAwaySession( |
| 209 | const QuicConfig& config, |
| 210 | QuicConnection* connection, |
| 211 | QuicSession::Visitor* visitor, |
| 212 | QuicCryptoServerStream::Helper* helper, |
| 213 | const QuicCryptoServerConfig* crypto_config, |
| 214 | QuicCompressedCertsCache* compressed_certs_cache, |
| 215 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 216 | : QuicSimpleServerSession(config, |
| 217 | CurrentSupportedVersions(), |
| 218 | connection, |
| 219 | visitor, |
| 220 | helper, |
| 221 | crypto_config, |
| 222 | compressed_certs_cache, |
| 223 | quic_simple_server_backend) {} |
| 224 | |
| 225 | void ImmediateGoAwaySession::OnStreamFrame(const QuicStreamFrame& frame) { |
| 226 | SendGoAway(QUIC_PEER_GOING_AWAY, ""); |
| 227 | QuicSimpleServerSession::OnStreamFrame(frame); |
| 228 | } |
| 229 | |
| 230 | } // namespace test |
| 231 | |
| 232 | } // namespace quic |