| 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/memory/ptr_util.h" |    8 #include "base/memory/ptr_util.h" | 
|    9 #include "base/run_loop.h" |    9 #include "base/run_loop.h" | 
|   10 #include "base/synchronization/lock.h" |   10 #include "base/synchronization/lock.h" | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|   30  |   30  | 
|   31 class CustomStreamSession : public QuicSimpleServerSession { |   31 class CustomStreamSession : public QuicSimpleServerSession { | 
|   32  public: |   32  public: | 
|   33   CustomStreamSession( |   33   CustomStreamSession( | 
|   34       const QuicConfig& config, |   34       const QuicConfig& config, | 
|   35       QuicConnection* connection, |   35       QuicConnection* connection, | 
|   36       QuicSession::Visitor* visitor, |   36       QuicSession::Visitor* visitor, | 
|   37       QuicCryptoServerStream::Helper* helper, |   37       QuicCryptoServerStream::Helper* helper, | 
|   38       const QuicCryptoServerConfig* crypto_config, |   38       const QuicCryptoServerConfig* crypto_config, | 
|   39       QuicCompressedCertsCache* compressed_certs_cache, |   39       QuicCompressedCertsCache* compressed_certs_cache, | 
|   40       QuicTestServer::StreamFactory* factory, |   40       QuicTestServer::StreamFactory* stream_factory, | 
|   41       QuicTestServer::CryptoStreamFactory* crypto_stream_factory) |   41       QuicTestServer::CryptoStreamFactory* crypto_stream_factory, | 
 |   42       QuicInMemoryCache* in_memory_cache) | 
|   42       : QuicSimpleServerSession(config, |   43       : QuicSimpleServerSession(config, | 
|   43                                 connection, |   44                                 connection, | 
|   44                                 visitor, |   45                                 visitor, | 
|   45                                 helper, |   46                                 helper, | 
|   46                                 crypto_config, |   47                                 crypto_config, | 
|   47                                 compressed_certs_cache), |   48                                 compressed_certs_cache, | 
|   48         stream_factory_(factory), |   49                                 in_memory_cache), | 
 |   50         stream_factory_(stream_factory), | 
|   49         crypto_stream_factory_(crypto_stream_factory) {} |   51         crypto_stream_factory_(crypto_stream_factory) {} | 
|   50  |   52  | 
|   51   QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { |   53   QuicSpdyStream* CreateIncomingDynamicStream(QuicStreamId id) override { | 
|   52     if (!ShouldCreateIncomingDynamicStream(id)) { |   54     if (!ShouldCreateIncomingDynamicStream(id)) { | 
|   53       return nullptr; |   55       return nullptr; | 
|   54     } |   56     } | 
|   55     if (stream_factory_) { |   57     if (stream_factory_) { | 
|   56       QuicSpdyStream* stream = stream_factory_->CreateStream(id, this); |   58       QuicSpdyStream* stream = | 
 |   59           stream_factory_->CreateStream(id, this, in_memory_cache()); | 
|   57       ActivateStream(base::WrapUnique(stream)); |   60       ActivateStream(base::WrapUnique(stream)); | 
|   58       return stream; |   61       return stream; | 
|   59     } |   62     } | 
|   60     return QuicSimpleServerSession::CreateIncomingDynamicStream(id); |   63     return QuicSimpleServerSession::CreateIncomingDynamicStream(id); | 
|   61   } |   64   } | 
|   62  |   65  | 
|   63   QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( |   66   QuicCryptoServerStreamBase* CreateQuicCryptoServerStream( | 
|   64       const QuicCryptoServerConfig* crypto_config, |   67       const QuicCryptoServerConfig* crypto_config, | 
|   65       QuicCompressedCertsCache* compressed_certs_cache) override { |   68       QuicCompressedCertsCache* compressed_certs_cache) override { | 
|   66     if (crypto_stream_factory_) { |   69     if (crypto_stream_factory_) { | 
|   67       return crypto_stream_factory_->CreateCryptoStream(crypto_config, this); |   70       return crypto_stream_factory_->CreateCryptoStream(crypto_config, this); | 
|   68     } |   71     } | 
|   69     return QuicSimpleServerSession::CreateQuicCryptoServerStream( |   72     return QuicSimpleServerSession::CreateQuicCryptoServerStream( | 
|   70         crypto_config, compressed_certs_cache); |   73         crypto_config, compressed_certs_cache); | 
|   71   } |   74   } | 
|   72  |   75  | 
|   73  private: |   76  private: | 
|   74   QuicTestServer::StreamFactory* stream_factory_;               // Not owned. |   77   QuicTestServer::StreamFactory* stream_factory_;               // Not owned. | 
|   75   QuicTestServer::CryptoStreamFactory* crypto_stream_factory_;  // Not owned. |   78   QuicTestServer::CryptoStreamFactory* crypto_stream_factory_;  // Not owned. | 
|   76 }; |   79 }; | 
|   77  |   80  | 
|   78 class QuicTestDispatcher : public QuicSimpleDispatcher { |   81 class QuicTestDispatcher : public QuicSimpleDispatcher { | 
|   79  public: |   82  public: | 
|   80   QuicTestDispatcher( |   83   QuicTestDispatcher( | 
|   81       const QuicConfig& config, |   84       const QuicConfig& config, | 
|   82       const QuicCryptoServerConfig* crypto_config, |   85       const QuicCryptoServerConfig* crypto_config, | 
|   83       QuicVersionManager* version_manager, |   86       QuicVersionManager* version_manager, | 
|   84       std::unique_ptr<QuicConnectionHelperInterface> helper, |   87       std::unique_ptr<QuicConnectionHelperInterface> helper, | 
|   85       std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |   88       std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, | 
|   86       std::unique_ptr<QuicAlarmFactory> alarm_factory) |   89       std::unique_ptr<QuicAlarmFactory> alarm_factory, | 
 |   90       QuicInMemoryCache* in_memory_cache) | 
|   87       : QuicSimpleDispatcher(config, |   91       : QuicSimpleDispatcher(config, | 
|   88                              crypto_config, |   92                              crypto_config, | 
|   89                              version_manager, |   93                              version_manager, | 
|   90                              std::move(helper), |   94                              std::move(helper), | 
|   91                              std::move(session_helper), |   95                              std::move(session_helper), | 
|   92                              std::move(alarm_factory)), |   96                              std::move(alarm_factory), | 
 |   97                              in_memory_cache), | 
|   93         session_factory_(nullptr), |   98         session_factory_(nullptr), | 
|   94         stream_factory_(nullptr), |   99         stream_factory_(nullptr), | 
|   95         crypto_stream_factory_(nullptr) {} |  100         crypto_stream_factory_(nullptr) {} | 
|   96  |  101  | 
|   97   QuicServerSessionBase* CreateQuicSession( |  102   QuicServerSessionBase* CreateQuicSession( | 
|   98       QuicConnectionId id, |  103       QuicConnectionId id, | 
|   99       const QuicSocketAddress& client) override { |  104       const QuicSocketAddress& client) override { | 
|  100     base::AutoLock lock(factory_lock_); |  105     base::AutoLock lock(factory_lock_); | 
|  101     if (session_factory_ == nullptr && stream_factory_ == nullptr && |  106     if (session_factory_ == nullptr && stream_factory_ == nullptr && | 
|  102         crypto_stream_factory_ == nullptr) { |  107         crypto_stream_factory_ == nullptr) { | 
|  103       return QuicSimpleDispatcher::CreateQuicSession(id, client); |  108       return QuicSimpleDispatcher::CreateQuicSession(id, client); | 
|  104     } |  109     } | 
|  105     QuicConnection* connection = new QuicConnection( |  110     QuicConnection* connection = new QuicConnection( | 
|  106         id, client, helper(), alarm_factory(), CreatePerConnectionWriter(), |  111         id, client, helper(), alarm_factory(), CreatePerConnectionWriter(), | 
|  107         /* owns_writer= */ true, Perspective::IS_SERVER, |  112         /* owns_writer= */ true, Perspective::IS_SERVER, | 
|  108         GetSupportedVersions()); |  113         GetSupportedVersions()); | 
|  109  |  114  | 
|  110     QuicServerSessionBase* session = nullptr; |  115     QuicServerSessionBase* session = nullptr; | 
|  111     if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { |  116     if (stream_factory_ != nullptr || crypto_stream_factory_ != nullptr) { | 
|  112       session = new CustomStreamSession( |  117       session = new CustomStreamSession( | 
|  113           config(), connection, this, session_helper(), crypto_config(), |  118           config(), connection, this, session_helper(), crypto_config(), | 
|  114           compressed_certs_cache(), stream_factory_, crypto_stream_factory_); |  119           compressed_certs_cache(), stream_factory_, crypto_stream_factory_, | 
 |  120           in_memory_cache()); | 
|  115     } else { |  121     } else { | 
|  116       session = session_factory_->CreateSession( |  122       session = session_factory_->CreateSession( | 
|  117           config(), connection, this, session_helper(), crypto_config(), |  123           config(), connection, this, session_helper(), crypto_config(), | 
|  118           compressed_certs_cache()); |  124           compressed_certs_cache(), in_memory_cache()); | 
|  119     } |  125     } | 
|  120     session->Initialize(); |  126     session->Initialize(); | 
|  121     return session; |  127     return session; | 
|  122   } |  128   } | 
|  123  |  129  | 
|  124   void SetSessionFactory(QuicTestServer::SessionFactory* factory) { |  130   void SetSessionFactory(QuicTestServer::SessionFactory* factory) { | 
|  125     base::AutoLock lock(factory_lock_); |  131     base::AutoLock lock(factory_lock_); | 
|  126     DCHECK(session_factory_ == nullptr); |  132     DCHECK(session_factory_ == nullptr); | 
|  127     DCHECK(stream_factory_ == nullptr); |  133     DCHECK(stream_factory_ == nullptr); | 
|  128     DCHECK(crypto_stream_factory_ == nullptr); |  134     DCHECK(crypto_stream_factory_ == nullptr); | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|  143     crypto_stream_factory_ = factory; |  149     crypto_stream_factory_ = factory; | 
|  144   } |  150   } | 
|  145  |  151  | 
|  146  private: |  152  private: | 
|  147   base::Lock factory_lock_; |  153   base::Lock factory_lock_; | 
|  148   QuicTestServer::SessionFactory* session_factory_;             // Not owned. |  154   QuicTestServer::SessionFactory* session_factory_;             // Not owned. | 
|  149   QuicTestServer::StreamFactory* stream_factory_;               // Not owned. |  155   QuicTestServer::StreamFactory* stream_factory_;               // Not owned. | 
|  150   QuicTestServer::CryptoStreamFactory* crypto_stream_factory_;  // Not owned. |  156   QuicTestServer::CryptoStreamFactory* crypto_stream_factory_;  // Not owned. | 
|  151 }; |  157 }; | 
|  152  |  158  | 
|  153 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source) |  159 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, | 
|  154     : QuicServer(std::move(proof_source)) {} |  160                                QuicInMemoryCache* in_memory_cache) | 
 |  161     : QuicServer(std::move(proof_source), in_memory_cache) {} | 
|  155  |  162  | 
|  156 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, |  163 QuicTestServer::QuicTestServer(std::unique_ptr<ProofSource> proof_source, | 
|  157                                const QuicConfig& config, |  164                                const QuicConfig& config, | 
|  158                                const QuicVersionVector& supported_versions) |  165                                const QuicVersionVector& supported_versions, | 
 |  166                                QuicInMemoryCache* in_memory_cache) | 
|  159     : QuicServer(std::move(proof_source), |  167     : QuicServer(std::move(proof_source), | 
|  160                  config, |  168                  config, | 
|  161                  QuicCryptoServerConfig::ConfigOptions(), |  169                  QuicCryptoServerConfig::ConfigOptions(), | 
|  162                  supported_versions) {} |  170                  supported_versions, | 
 |  171                  in_memory_cache) {} | 
|  163  |  172  | 
|  164 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { |  173 QuicDispatcher* QuicTestServer::CreateQuicDispatcher() { | 
|  165   return new QuicTestDispatcher( |  174   return new QuicTestDispatcher( | 
|  166       config(), &crypto_config(), version_manager(), |  175       config(), &crypto_config(), version_manager(), | 
|  167       std::unique_ptr<QuicEpollConnectionHelper>(new QuicEpollConnectionHelper( |  176       std::unique_ptr<QuicEpollConnectionHelper>(new QuicEpollConnectionHelper( | 
|  168           epoll_server(), QuicAllocator::BUFFER_POOL)), |  177           epoll_server(), QuicAllocator::BUFFER_POOL)), | 
|  169       std::unique_ptr<QuicCryptoServerStream::Helper>( |  178       std::unique_ptr<QuicCryptoServerStream::Helper>( | 
|  170           new QuicSimpleCryptoServerStreamHelper(QuicRandom::GetInstance())), |  179           new QuicSimpleCryptoServerStreamHelper(QuicRandom::GetInstance())), | 
|  171       std::unique_ptr<QuicEpollAlarmFactory>( |  180       std::unique_ptr<QuicEpollAlarmFactory>( | 
|  172           new QuicEpollAlarmFactory(epoll_server()))); |  181           new QuicEpollAlarmFactory(epoll_server())), | 
 |  182       in_memory_cache()); | 
|  173 } |  183 } | 
|  174  |  184  | 
|  175 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { |  185 void QuicTestServer::SetSessionFactory(SessionFactory* factory) { | 
|  176   DCHECK(dispatcher()); |  186   DCHECK(dispatcher()); | 
|  177   static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); |  187   static_cast<QuicTestDispatcher*>(dispatcher())->SetSessionFactory(factory); | 
|  178 } |  188 } | 
|  179  |  189  | 
|  180 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { |  190 void QuicTestServer::SetSpdyStreamFactory(StreamFactory* factory) { | 
|  181   static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); |  191   static_cast<QuicTestDispatcher*>(dispatcher())->SetStreamFactory(factory); | 
|  182 } |  192 } | 
|  183  |  193  | 
|  184 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { |  194 void QuicTestServer::SetCryptoStreamFactory(CryptoStreamFactory* factory) { | 
|  185   static_cast<QuicTestDispatcher*>(dispatcher()) |  195   static_cast<QuicTestDispatcher*>(dispatcher()) | 
|  186       ->SetCryptoStreamFactory(factory); |  196       ->SetCryptoStreamFactory(factory); | 
|  187 } |  197 } | 
|  188  |  198  | 
|  189 ///////////////////////////   TEST SESSIONS /////////////////////////////// |  199 ///////////////////////////   TEST SESSIONS /////////////////////////////// | 
|  190  |  200  | 
|  191 ImmediateGoAwaySession::ImmediateGoAwaySession( |  201 ImmediateGoAwaySession::ImmediateGoAwaySession( | 
|  192     const QuicConfig& config, |  202     const QuicConfig& config, | 
|  193     QuicConnection* connection, |  203     QuicConnection* connection, | 
|  194     QuicSession::Visitor* visitor, |  204     QuicSession::Visitor* visitor, | 
|  195     QuicCryptoServerStream::Helper* helper, |  205     QuicCryptoServerStream::Helper* helper, | 
|  196     const QuicCryptoServerConfig* crypto_config, |  206     const QuicCryptoServerConfig* crypto_config, | 
|  197     QuicCompressedCertsCache* compressed_certs_cache) |  207     QuicCompressedCertsCache* compressed_certs_cache, | 
 |  208     QuicInMemoryCache* in_memory_cache) | 
|  198     : QuicSimpleServerSession(config, |  209     : QuicSimpleServerSession(config, | 
|  199                               connection, |  210                               connection, | 
|  200                               visitor, |  211                               visitor, | 
|  201                               helper, |  212                               helper, | 
|  202                               crypto_config, |  213                               crypto_config, | 
|  203                               compressed_certs_cache) {} |  214                               compressed_certs_cache, | 
 |  215                               in_memory_cache) {} | 
|  204  |  216  | 
|  205 void ImmediateGoAwaySession::OnStreamFrame(const QuicStreamFrame& frame) { |  217 void ImmediateGoAwaySession::OnStreamFrame(const QuicStreamFrame& frame) { | 
|  206   SendGoAway(QUIC_PEER_GOING_AWAY, ""); |  218   SendGoAway(QUIC_PEER_GOING_AWAY, ""); | 
|  207   QuicSimpleServerSession::OnStreamFrame(frame); |  219   QuicSimpleServerSession::OnStreamFrame(frame); | 
|  208 } |  220 } | 
|  209  |  221  | 
|  210 }  // namespace test |  222 }  // namespace test | 
|  211 }  // namespace net |  223 }  // namespace net | 
| OLD | NEW |