mock_crypto_client_stream.h 4.8 KB

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