| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/tools/quic/test_tools/quic_test_server.h" | 5 #include "net/tools/quic/test_tools/quic_test_server.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/tools/quic/quic_epoll_connection_helper.h" | 21 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 22 #include "net/tools/quic/quic_server_session.h" | 22 #include "net/tools/quic/quic_server_session.h" |
| 23 #include "net/tools/quic/quic_spdy_server_stream.h" | 23 #include "net/tools/quic/quic_spdy_server_stream.h" |
| 24 | 24 |
| 25 namespace net { | 25 namespace net { |
| 26 namespace tools { | 26 namespace tools { |
| 27 namespace test { | 27 namespace test { |
| 28 | 28 |
| 29 class CustomStreamSession : public QuicServerSession { | 29 class CustomStreamSession : public QuicServerSession { |
| 30 public: | 30 public: |
| 31 CustomStreamSession(const QuicConfig& config, | 31 CustomStreamSession( |
| 32 QuicConnection* connection, | 32 const QuicConfig& config, |
| 33 QuicServerSessionVisitor* visitor, | 33 QuicConnection* connection, |
| 34 const QuicCryptoServerConfig* crypto_config, | 34 QuicServerSessionVisitor* visitor, |
| 35 QuicTestServer::StreamFactory* factory) | 35 const QuicCryptoServerConfig* crypto_config, |
| 36 QuicTestServer::StreamFactory* factory, |
| 37 QuicTestServer::CryptoStreamFactory* crypto_stream_factory) |
| 36 : QuicServerSession(config, connection, visitor, crypto_config), | 38 : QuicServerSession(config, connection, visitor, crypto_config), |
| 37 stream_factory_(factory) {} | 39 stream_factory_(factory), |
| 40 crypto_stream_factory_(crypto_stream_factory) {} |
| 38 | 41 |
| 39 QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { | 42 QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { |
| 40 if (!ShouldCreateIncomingDynamicStream(id)) { | 43 if (!ShouldCreateIncomingDynamicStream(id)) { |
| 41 return nullptr; | 44 return nullptr; |
| 42 } | 45 } |
| 43 return stream_factory_->CreateStream(id, this); | 46 if (stream_factory_) { |
| 47 return stream_factory_->CreateStream(id, this); |
| 48 } |
| 49 return QuicServerSession::CreateIncomingDynamicStream(id); |
| 50 } |
| 51 |
| 52 QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( |
| 53 const QuicCryptoServerConfig* crypto_config) override { |
| 54 if (crypto_stream_factory_) { |
| 55 return crypto_stream_factory_->CreateCryptoStream(crypto_config, this); |
| 56 } |
| 57 return QuicServerSession::CreateQuicCryptoServerStream(crypto_config); |
| 44 } | 58 } |
| 45 | 59 |
| 46 private: | 60 private: |
| 47 QuicTestServer::StreamFactory* stream_factory_; // Not owned. | 61 QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
| 62 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
| 48 }; | 63 }; |
| 49 | 64 |
| 50 class QuicTestDispatcher : public QuicDispatcher { | 65 class QuicTestDispatcher : public QuicDispatcher { |
| 51 public: | 66 public: |
| 52 QuicTestDispatcher(const QuicConfig& config, | 67 QuicTestDispatcher(const QuicConfig& config, |
| 53 const QuicCryptoServerConfig* crypto_config, | 68 const QuicCryptoServerConfig* crypto_config, |
| 54 const QuicVersionVector& versions, | 69 const QuicVersionVector& versions, |
| 55 PacketWriterFactory* factory, | 70 PacketWriterFactory* factory, |
| 56 QuicConnectionHelperInterface* helper) | 71 QuicConnectionHelperInterface* helper) |
| 57 : QuicDispatcher(config, crypto_config, versions, factory, helper), | 72 : QuicDispatcher(config, crypto_config, versions, factory, helper), |
| 58 session_factory_(nullptr), | 73 session_factory_(nullptr), |
| 59 stream_factory_(nullptr) {} | 74 stream_factory_(nullptr), |
| 75 crypto_stream_factory_(nullptr) {} |
| 60 | 76 |
| 61 QuicServerSession* CreateQuicSession(QuicConnectionId id, | 77 QuicServerSession* CreateQuicSession(QuicConnectionId id, |
| 62 const IPEndPoint& client) override { | 78 const IPEndPoint& client) override { |
| 63 if (session_factory_ == nullptr && stream_factory_ == nullptr) { | 79 if (session_factory_ == nullptr && stream_factory_ == nullptr && |
| 80 crypto_stream_factory_ == nullptr) { |
| 64 return QuicDispatcher::CreateQuicSession(id, client); | 81 return QuicDispatcher::CreateQuicSession(id, client); |
| 65 } | 82 } |
| 66 QuicConnection* connection = new QuicConnection( | 83 QuicConnection* connection = new QuicConnection( |
| 67 id, client, helper(), connection_writer_factory(), | 84 id, client, helper(), connection_writer_factory(), |
| 68 /* owns_writer= */ true, Perspective::IS_SERVER, supported_versions()); | 85 /* owns_writer= */ true, Perspective::IS_SERVER, supported_versions()); |
| 69 | 86 |
| 70 QuicServerSession* session = nullptr; | 87 QuicServerSession* session = nullptr; |
| 71 if (stream_factory_ != nullptr) { | 88 if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { |
| 72 session = new CustomStreamSession(config(), connection, this, | 89 session = |
| 73 crypto_config(), stream_factory_); | 90 new CustomStreamSession(config(), connection, this, crypto_config(), |
| 91 stream_factory_, crypto_stream_factory_); |
| 74 } else { | 92 } else { |
| 75 session = session_factory_->CreateSession(config(), connection, this, | 93 session = session_factory_->CreateSession(config(), connection, this, |
| 76 crypto_config()); | 94 crypto_config()); |
| 77 } | 95 } |
| 78 session->Initialize(); | 96 session->Initialize(); |
| 79 return session; | 97 return session; |
| 80 } | 98 } |
| 81 | 99 |
| 82 void set_session_factory(QuicTestServer::SessionFactory* factory) { | 100 void set_session_factory(QuicTestServer::SessionFactory* factory) { |
| 83 DCHECK(session_factory_ == nullptr); | 101 DCHECK(session_factory_ == nullptr); |
| 84 DCHECK(stream_factory_ == nullptr); | 102 DCHECK(stream_factory_ == nullptr); |
| 103 DCHECK(crypto_stream_factory_ == nullptr); |
| 85 session_factory_ = factory; | 104 session_factory_ = factory; |
| 86 } | 105 } |
| 87 | 106 |
| 88 void set_stream_factory(QuicTestServer::StreamFactory* factory) { | 107 void set_stream_factory(QuicTestServer::StreamFactory* factory) { |
| 89 DCHECK(session_factory_ == nullptr); | 108 DCHECK(session_factory_ == nullptr); |
| 90 DCHECK(stream_factory_ == nullptr); | 109 DCHECK(stream_factory_ == nullptr); |
| 91 stream_factory_ = factory; | 110 stream_factory_ = factory; |
| 92 } | 111 } |
| 93 | 112 |
| 113 void set_crypto_stream_factory(QuicTestServer::CryptoStreamFactory* factory) { |
| 114 DCHECK(session_factory_ == nullptr); |
| 115 DCHECK(crypto_stream_factory_ == nullptr); |
| 116 crypto_stream_factory_ = factory; |
| 117 } |
| 118 |
| 94 QuicTestServer::SessionFactory* session_factory() { return session_factory_; } | 119 QuicTestServer::SessionFactory* session_factory() { return session_factory_; } |
| 95 | 120 |
| 96 QuicTestServer::StreamFactory* stream_factory() { return stream_factory_; } | 121 QuicTestServer::StreamFactory* stream_factory() { return stream_factory_; } |
| 97 | 122 |
| 98 private: | 123 private: |
| 99 QuicTestServer::SessionFactory* session_factory_; // Not owned. | 124 QuicTestServer::SessionFactory* session_factory_; // Not owned. |
| 100 QuicTestServer::StreamFactory* stream_factory_; // Not owned. | 125 QuicTestServer::StreamFactory* stream_factory_; // Not owned. |
| 126 QuicTestServer::CryptoStreamFactory* crypto_stream_factory_; // Not owned. |
| 101 }; | 127 }; |
| 102 | 128 |
| 103 QuicTestServer::QuicTestServer(ProofSource* proof_source) | 129 QuicTestServer::QuicTestServer(ProofSource* proof_source) |
| 104 : QuicServer(proof_source) {} | 130 : QuicServer(proof_source) {} |
| 105 | 131 |
| 106 QuicTestServer::QuicTestServer(ProofSource* proof_source, | 132 QuicTestServer::QuicTestServer(ProofSource* proof_source, |
| 107 const QuicConfig& config, | 133 const QuicConfig& config, |
| 108 const QuicVersionVector& supported_versions) | 134 const QuicVersionVector& supported_versions) |
| 109 : QuicServer(proof_source, config, supported_versions) {} | 135 : QuicServer(proof_source, config, supported_versions) {} |
| 110 | 136 |
| 111 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { | 137 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { |
| 112 return new QuicTestDispatcher( | 138 return new QuicTestDispatcher( |
| 113 config(), &crypto_config(), supported_versions(), | 139 config(), &crypto_config(), supported_versions(), |
| 114 new QuicDispatcher::DefaultPacketWriterFactory(), | 140 new QuicDispatcher::DefaultPacketWriterFactory(), |
| 115 new QuicEpollConnectionHelper(epoll_server())); | 141 new QuicEpollConnectionHelper(epoll_server())); |
| 116 } | 142 } |
| 117 | 143 |
| 118 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { | 144 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { |
| 119 DCHECK(dispatcher()); | 145 DCHECK(dispatcher()); |
| 120 static_cast<QuicTestDispatcher*>(dispatcher())->set_session_factory(factory); | 146 static_cast<QuicTestDispatcher*>(dispatcher())->set_session_factory(factory); |
| 121 } | 147 } |
| 122 | 148 |
| 123 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { | 149 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { |
| 124 static_cast<QuicTestDispatcher*>(dispatcher())->set_stream_factory(factory); | 150 static_cast<QuicTestDispatcher*>(dispatcher())->set_stream_factory(factory); |
| 125 } | 151 } |
| 126 | 152 |
| 153 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { |
| 154 static_cast<QuicTestDispatcher*>(dispatcher()) |
| 155 ->set_crypto_stream_factory(factory); |
| 156 } |
| 157 |
| 127 /////////////////////////// TEST SESSIONS /////////////////////////////// | 158 /////////////////////////// TEST SESSIONS /////////////////////////////// |
| 128 | 159 |
| 129 ImmediateGoAwaySession::ImmediateGoAwaySession( | 160 ImmediateGoAwaySession::ImmediateGoAwaySession( |
| 130 const QuicConfig& config, | 161 const QuicConfig& config, |
| 131 QuicConnection* connection, | 162 QuicConnection* connection, |
| 132 QuicServerSessionVisitor* visitor, | 163 QuicServerSessionVisitor* visitor, |
| 133 const QuicCryptoServerConfig* crypto_config) | 164 const QuicCryptoServerConfig* crypto_config) |
| 134 : QuicServerSession(config, connection, visitor, crypto_config) { | 165 : QuicServerSession(config, connection, visitor, crypto_config) { |
| 135 SendGoAway(QUIC_PEER_GOING_AWAY, ""); | 166 SendGoAway(QUIC_PEER_GOING_AWAY, ""); |
| 136 } | 167 } |
| 137 | 168 |
| 138 } // namespace test | 169 } // namespace test |
| 139 } // namespace tools | 170 } // namespace tools |
| 140 } // namespace net | 171 } // namespace net |
| OLD | NEW |