• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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 NET_QUIC_MOCK_CRYPTO_CLIENT_STREAM_H_
6 #define NET_QUIC_MOCK_CRYPTO_CLIENT_STREAM_H_
7 
8 #include "base/memory/raw_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/quic/crypto/proof_verifier_chromium.h"
11 #include "net/third_party/quiche/src/quiche/quic/core/crypto/crypto_handshake.h"
12 #include "net/third_party/quiche/src/quiche/quic/core/crypto/crypto_protocol.h"
13 #include "net/third_party/quiche/src/quiche/quic/core/http/quic_spdy_client_session_base.h"
14 #include "net/third_party/quiche/src/quiche/quic/core/quic_crypto_client_stream.h"
15 #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
16 #include "net/third_party/quiche/src/quiche/quic/core/quic_session.h"
17 
18 namespace net {
19 
20 class MockCryptoClientStream : public quic::QuicCryptoClientStream,
21                                public quic::QuicCryptoHandshaker {
22  public:
23   // HandshakeMode enumerates the handshake mode MockCryptoClientStream should
24   // mock in CryptoConnect.
25   enum HandshakeMode {
26     // CONFIRM_HANDSHAKE indicates that CryptoConnect will immediately confirm
27     // the handshake and establish encryption.  This behavior will never happen
28     // in the field, but is convenient for higher level tests.
29     CONFIRM_HANDSHAKE,
30 
31     // ZERO_RTT indicates that CryptoConnect will establish encryption but will
32     // not confirm the handshake.
33     ZERO_RTT,
34 
35     // ASYNC_ZERO_RTT indicates that 0-RTT setup will be completed
36     // asynchronously. This is possible in TLS. Tests need to call
37     // NotifySessionZeroRttComplete() to setup 0-RTT encryption.
38     ASYNC_ZERO_RTT,
39 
40     // COLD_START indicates that CryptoConnect will neither establish encryption
41     // nor confirm the handshake.
42     COLD_START,
43 
44     // COLD_START_WITH_CHLO_SENT indicates that CryptoConnection will attempt to
45     // establish encryption by sending the initial CHLO packet on wire, which
46     // contains an empty CryptoHandshakeMessage. It will not confirm the
47     // hanshake though.
48     COLD_START_WITH_CHLO_SENT,
49   };
50 
51   MockCryptoClientStream(
52       const quic::QuicServerId& server_id,
53       quic::QuicSpdyClientSessionBase* session,
54       std::unique_ptr<quic::ProofVerifyContext> verify_context,
55       const quic::QuicConfig& config,
56       quic::QuicCryptoClientConfig* crypto_config,
57       HandshakeMode handshake_mode,
58       const net::ProofVerifyDetailsChromium* proof_verify_details_,
59       bool use_mock_crypter);
60 
61   MockCryptoClientStream(const MockCryptoClientStream&) = delete;
62   MockCryptoClientStream& operator=(const MockCryptoClientStream&) = delete;
63 
64   ~MockCryptoClientStream() override;
65 
66   // CryptoFramerVisitorInterface implementation.
67   void OnHandshakeMessage(const quic::CryptoHandshakeMessage& message) override;
68 
69   // QuicCryptoClientStream implementation.
70   bool CryptoConnect() override;
71   bool encryption_established() const override;
72   bool one_rtt_keys_available() const override;
73   quic::HandshakeState GetHandshakeState() const override;
74   const quic::QuicCryptoNegotiatedParameters& crypto_negotiated_params()
75       const override;
76   quic::CryptoMessageParser* crypto_message_parser() override;
77   void OnOneRttPacketAcknowledged() override;
78   std::unique_ptr<quic::QuicDecrypter>
79   AdvanceKeysAndCreateCurrentOneRttDecrypter() override;
80   bool EarlyDataAccepted() const override;
81   // Override QuicCryptoClientStream::SetServerApplicationStateForResumption()
82   // to avoid tripping over the DCHECK on handshaker state.
SetServerApplicationStateForResumption(std::unique_ptr<quic::ApplicationState> application_state)83   void SetServerApplicationStateForResumption(
84       std::unique_ptr<quic::ApplicationState> application_state) override {}
85 
86   // Notify session that 1-RTT key is available.
87   void NotifySessionOneRttKeyAvailable();
88 
89   // Notify session that 0-RTT setup is complete.
90   void NotifySessionZeroRttComplete();
91 
GetWeakPtr()92   base::WeakPtr<MockCryptoClientStream> GetWeakPtr() {
93     return weak_factory_.GetWeakPtr();
94   }
95 
96   static quic::CryptoHandshakeMessage GetDummyCHLOMessage();
97 
98  protected:
99   using quic::QuicCryptoClientStream::session;
100 
101  private:
102   void SetConfigNegotiated();
103 
104   // Called from CryptoConnect to set appropriate values in
105   // |crypto_negotiated_params_|.
106   void FillCryptoParams();
107 
108   HandshakeMode handshake_mode_;
109   bool encryption_established_ = false;
110   bool handshake_confirmed_ = false;
111   quiche::QuicheReferenceCountedPointer<quic::QuicCryptoNegotiatedParameters>
112       crypto_negotiated_params_;
113   quic::CryptoFramer crypto_framer_;
114   bool use_mock_crypter_;
115 
116   const quic::QuicServerId server_id_;
117   raw_ptr<const net::ProofVerifyDetailsChromium> proof_verify_details_;
118   const quic::QuicConfig config_;
119   base::WeakPtrFactory<MockCryptoClientStream> weak_factory_{this};
120 };
121 
122 }  // namespace net
123 
124 #endif  // NET_QUIC_MOCK_CRYPTO_CLIENT_STREAM_H_
125