xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/test_ticket_crypter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2020 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_TEST_TICKET_CRYPTER_H_
6 #define QUICHE_QUIC_TEST_TOOLS_TEST_TICKET_CRYPTER_H_
7 
8 #include "quiche/quic/core/crypto/proof_source.h"
9 
10 namespace quic {
11 namespace test {
12 
13 // Provides a simple implementation of ProofSource::TicketCrypter for testing.
14 // THIS IMPLEMENTATION IS NOT SECURE. It is only intended for testing purposes.
15 class TestTicketCrypter : public ProofSource::TicketCrypter {
16  public:
17   TestTicketCrypter();
18   ~TestTicketCrypter() override = default;
19 
20   // TicketCrypter interface
21   size_t MaxOverhead() override;
22   std::vector<uint8_t> Encrypt(absl::string_view in,
23                                absl::string_view encryption_key) override;
24   void Decrypt(absl::string_view in,
25                std::shared_ptr<ProofSource::DecryptCallback> callback) override;
26 
27   void SetRunCallbacksAsync(bool run_async);
28   size_t NumPendingCallbacks();
29   void RunPendingCallback(size_t n);
30 
31   // Allows configuring this TestTicketCrypter to fail decryption.
set_fail_decrypt(bool fail_decrypt)32   void set_fail_decrypt(bool fail_decrypt) { fail_decrypt_ = fail_decrypt; }
set_fail_encrypt(bool fail_encrypt)33   void set_fail_encrypt(bool fail_encrypt) { fail_encrypt_ = fail_encrypt; }
34 
35  private:
36   // Performs the Decrypt operation synchronously.
37   std::vector<uint8_t> Decrypt(absl::string_view in);
38 
39   struct PendingCallback {
40     std::shared_ptr<ProofSource::DecryptCallback> callback;
41     std::vector<uint8_t> decrypted_ticket;
42   };
43 
44   bool fail_decrypt_ = false;
45   bool fail_encrypt_ = false;
46   bool run_async_ = false;
47   std::vector<PendingCallback> pending_callbacks_;
48   std::vector<uint8_t> ticket_prefix_;
49 };
50 
51 }  // namespace test
52 }  // namespace quic
53 
54 #endif  // QUICHE_QUIC_TEST_TOOLS_TEST_TICKET_CRYPTER_H_
55