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 #ifndef QUICHE_QUIC_TEST_TOOLS_QUIC_TEST_SERVER_H_ 6 #define QUICHE_QUIC_TEST_TOOLS_QUIC_TEST_SERVER_H_ 7 8 #include "quiche/quic/core/quic_dispatcher.h" 9 #include "quiche/quic/core/quic_session.h" 10 #include "quiche/quic/tools/quic_server.h" 11 #include "quiche/quic/tools/quic_simple_server_backend.h" 12 #include "quiche/quic/tools/quic_simple_server_session.h" 13 #include "quiche/quic/tools/quic_simple_server_stream.h" 14 15 namespace quic { 16 17 namespace test { 18 19 // A test server which enables easy creation of custom QuicServerSessions 20 // 21 // Eventually this may be extended to allow custom QuicConnections etc. 22 class QuicTestServer : public QuicServer { 23 public: 24 // Factory for creating QuicServerSessions. 25 class SessionFactory { 26 public: ~SessionFactory()27 virtual ~SessionFactory() {} 28 29 // Returns a new session owned by the caller. 30 virtual std::unique_ptr<QuicServerSessionBase> CreateSession( 31 const QuicConfig& config, QuicConnection* connection, 32 QuicSession::Visitor* visitor, 33 QuicCryptoServerStreamBase::Helper* helper, 34 const QuicCryptoServerConfig* crypto_config, 35 QuicCompressedCertsCache* compressed_certs_cache, 36 QuicSimpleServerBackend* quic_simple_server_backend) = 0; 37 }; 38 39 // Factory for creating QuicSimpleServerStreams. 40 class StreamFactory { 41 public: ~StreamFactory()42 virtual ~StreamFactory() {} 43 44 // Returns a new stream owned by the caller. 45 virtual QuicSimpleServerStream* CreateStream( 46 QuicStreamId id, QuicSpdySession* session, 47 QuicSimpleServerBackend* quic_simple_server_backend) = 0; 48 49 virtual QuicSimpleServerStream* CreateStream( 50 PendingStream* pending, QuicSpdySession* session, 51 QuicSimpleServerBackend* quic_simple_server_backend) = 0; 52 }; 53 54 class CryptoStreamFactory { 55 public: ~CryptoStreamFactory()56 virtual ~CryptoStreamFactory() {} 57 58 // Returns a new QuicCryptoServerStreamBase owned by the caller 59 virtual std::unique_ptr<QuicCryptoServerStreamBase> CreateCryptoStream( 60 const QuicCryptoServerConfig* crypto_config, 61 QuicServerSessionBase* session) = 0; 62 }; 63 64 QuicTestServer(std::unique_ptr<ProofSource> proof_source, 65 QuicSimpleServerBackend* quic_simple_server_backend); 66 QuicTestServer(std::unique_ptr<ProofSource> proof_source, 67 const QuicConfig& config, 68 const ParsedQuicVersionVector& supported_versions, 69 QuicSimpleServerBackend* quic_simple_server_backend); 70 QuicTestServer(std::unique_ptr<ProofSource> proof_source, 71 const QuicConfig& config, 72 const ParsedQuicVersionVector& supported_versions, 73 QuicSimpleServerBackend* quic_simple_server_backend, 74 uint8_t expected_server_connection_id_length); 75 76 // Create a custom dispatcher which creates custom sessions. 77 QuicDispatcher* CreateQuicDispatcher() override; 78 79 // Sets a custom session factory, owned by the caller, for easy custom 80 // session logic. This is incompatible with setting a stream factory or a 81 // crypto stream factory. 82 void SetSessionFactory(SessionFactory* factory); 83 84 // Sets a custom stream factory, owned by the caller, for easy custom 85 // stream logic. This is incompatible with setting a session factory. 86 void SetSpdyStreamFactory(StreamFactory* factory); 87 88 // Sets a custom crypto stream factory, owned by the caller, for easy custom 89 // crypto logic. This is incompatible with setting a session factory. 90 void SetCryptoStreamFactory(CryptoStreamFactory* factory); 91 92 // Sets the override for the default event loop factory used by the server. 93 void SetEventLoopFactory(QuicEventLoopFactory* factory); 94 95 protected: 96 std::unique_ptr<QuicEventLoop> CreateEventLoop() override; 97 98 private: 99 QuicEventLoopFactory* event_loop_factory_ = nullptr; 100 }; 101 102 // Useful test sessions for the QuicTestServer. 103 104 // Test session which sends a GOAWAY immedaitely on creation, before crypto 105 // credentials have even been established. 106 class ImmediateGoAwaySession : public QuicSimpleServerSession { 107 public: 108 ImmediateGoAwaySession(const QuicConfig& config, QuicConnection* connection, 109 QuicSession::Visitor* visitor, 110 QuicCryptoServerStreamBase::Helper* helper, 111 const QuicCryptoServerConfig* crypto_config, 112 QuicCompressedCertsCache* compressed_certs_cache, 113 QuicSimpleServerBackend* quic_simple_server_backend); 114 115 // Override to send GoAway. 116 void OnStreamFrame(const QuicStreamFrame& frame) override; 117 void OnCryptoFrame(const QuicCryptoFrame& frame) override; 118 void OnNewEncryptionKeyAvailable( 119 EncryptionLevel level, std::unique_ptr<QuicEncrypter> encrypter) override; 120 }; 121 122 } // namespace test 123 124 } // namespace quic 125 126 #endif // QUICHE_QUIC_TEST_TOOLS_QUIC_TEST_SERVER_H_ 127