xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/quic_test_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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,
37         absl::string_view alpn) = 0;
38   };
39 
40   // Factory for creating QuicSimpleServerStreams.
41   class StreamFactory {
42    public:
~StreamFactory()43     virtual ~StreamFactory() {}
44 
45     // Returns a new stream owned by the caller.
46     virtual QuicSimpleServerStream* CreateStream(
47         QuicStreamId id, QuicSpdySession* session,
48         QuicSimpleServerBackend* quic_simple_server_backend) = 0;
49 
50     virtual QuicSimpleServerStream* CreateStream(
51         PendingStream* pending, QuicSpdySession* session,
52         QuicSimpleServerBackend* quic_simple_server_backend) = 0;
53   };
54 
55   class CryptoStreamFactory {
56    public:
~CryptoStreamFactory()57     virtual ~CryptoStreamFactory() {}
58 
59     // Returns a new QuicCryptoServerStreamBase owned by the caller
60     virtual std::unique_ptr<QuicCryptoServerStreamBase> CreateCryptoStream(
61         const QuicCryptoServerConfig* crypto_config,
62         QuicServerSessionBase* session) = 0;
63   };
64 
65   QuicTestServer(std::unique_ptr<ProofSource> proof_source,
66                  QuicSimpleServerBackend* quic_simple_server_backend);
67   QuicTestServer(std::unique_ptr<ProofSource> proof_source,
68                  const QuicConfig& config,
69                  const ParsedQuicVersionVector& supported_versions,
70                  QuicSimpleServerBackend* quic_simple_server_backend);
71   QuicTestServer(std::unique_ptr<ProofSource> proof_source,
72                  const QuicConfig& config,
73                  const ParsedQuicVersionVector& supported_versions,
74                  QuicSimpleServerBackend* quic_simple_server_backend,
75                  uint8_t expected_server_connection_id_length);
76 
77   // Create a custom dispatcher which creates custom sessions.
78   QuicDispatcher* CreateQuicDispatcher() override;
79 
80   // Sets a custom session factory, owned by the caller, for easy custom
81   // session logic. This is incompatible with setting a stream factory or a
82   // crypto stream factory.
83   void SetSessionFactory(SessionFactory* factory);
84 
85   // Sets a custom stream factory, owned by the caller, for easy custom
86   // stream logic. This is incompatible with setting a session factory.
87   void SetSpdyStreamFactory(StreamFactory* factory);
88 
89   // Sets a custom crypto stream factory, owned by the caller, for easy custom
90   // crypto logic.  This is incompatible with setting a session factory.
91   void SetCryptoStreamFactory(CryptoStreamFactory* factory);
92 
93   // Sets the override for the default event loop factory used by the server.
94   void SetEventLoopFactory(QuicEventLoopFactory* factory);
95 
96  protected:
97   std::unique_ptr<QuicEventLoop> CreateEventLoop() override;
98 
99  private:
100   QuicEventLoopFactory* event_loop_factory_ = nullptr;
101 };
102 
103 // Useful test sessions for the QuicTestServer.
104 
105 // Test session which sends a GOAWAY immedaitely on creation, before crypto
106 // credentials have even been established.
107 class ImmediateGoAwaySession : public QuicSimpleServerSession {
108  public:
109   ImmediateGoAwaySession(const QuicConfig& config, QuicConnection* connection,
110                          QuicSession::Visitor* visitor,
111                          QuicCryptoServerStreamBase::Helper* helper,
112                          const QuicCryptoServerConfig* crypto_config,
113                          QuicCompressedCertsCache* compressed_certs_cache,
114                          QuicSimpleServerBackend* quic_simple_server_backend);
115 
116   // Override to send GoAway.
117   void OnStreamFrame(const QuicStreamFrame& frame) override;
118   void OnCryptoFrame(const QuicCryptoFrame& frame) override;
119   void OnNewEncryptionKeyAvailable(
120       EncryptionLevel level, std::unique_ptr<QuicEncrypter> encrypter) override;
121 };
122 
123 }  // namespace test
124 
125 }  // namespace quic
126 
127 #endif  // QUICHE_QUIC_TEST_TOOLS_QUIC_TEST_SERVER_H_
128