xref: /aosp_15_r20/external/cronet/net/quic/mock_quic_data.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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_QUIC_DATA_H_
6 #define NET_QUIC_MOCK_QUIC_DATA_H_
7 
8 #include "net/quic/quic_test_packet_printer.h"
9 #include "net/socket/socket_test_util.h"
10 #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
11 
12 namespace net::test {
13 
14 // Helper class to encapsulate MockReads and MockWrites for QUIC.
15 // Simplify ownership issues and the interaction with the MockSocketFactory.
16 //
17 // To use, construct an instance, call the `Add*` methods in the desired order,
18 // and then call `AddSocketDataToFactory(socket_factory)` to add a socket with
19 // the defined behavior to the socket factory. Alternately, use
20 // `InitializeAndGetSequencedSocketData()` and pass the result to a mock socket
21 // like `MockUDPClientSocket`.
22 //
23 // The MockQuicData instance must remain live until the socket is created and
24 // ultimately closed.
25 class MockQuicData {
26  public:
27   explicit MockQuicData(quic::ParsedQuicVersion version);
28   ~MockQuicData();
29 
30   // Makes the Connect() call return |rv| either
31   // synchronusly or asynchronously based on |mode|.
32   void AddConnect(IoMode mode, int rv);
33 
34   // Adds a read at the next sequence number which will read |packet|
35   // synchronously or asynchronously based on |mode|. The QuicReceivedPacket
36   // version includes an ECN codepoint.
37   void AddRead(IoMode mode, std::unique_ptr<quic::QuicReceivedPacket> packet);
38   void AddRead(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet);
39 
40   // Adds a read at the next sequence number which will return |rv| either
41   // synchronously or asynchronously based on |mode|.
42   void AddRead(IoMode mode, int rv);
43 
44   // Adds a pause, meaning that reads will return ERR_IO_PENDING until
45   // `Resume()` is called. Read and write cannot both be paused simultaneously.
46   void AddReadPause();
47 
48   // Like `AddReadPause`, but cannot be resumed.
49   void AddReadPauseForever();
50 
51   // Adds a write at the next sequence number which will write |packet|
52   // synchronously or asynchronously based on |mode|.
53   void AddWrite(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet);
54 
55   // Adds a write at the next sequence number which will return |rv| either
56   // synchronously or asynchronously based on |mode|.
57   void AddWrite(IoMode mode, int rv);
58 
59   // Adds a write at the next sequence number which will write |packet|
60   // synchronously or asynchronously based on |mode| and return |rv|.
61   void AddWrite(IoMode mode,
62                 int rv,
63                 std::unique_ptr<quic::QuicEncryptedPacket> packet);
64 
65   // Adds a pause, meaning that writes will return ERR_IO_PENDING until
66   // `Resume()` is called. Read and write cannot both be paused simultaneously.
67   void AddWritePause();
68 
69   // Adds the reads and writes to |factory|.
70   void AddSocketDataToFactory(MockClientSocketFactory* factory);
71 
72   // Returns true if all reads have been consumed.
73   bool AllReadDataConsumed();
74 
75   // Returns true if all writes have been consumed.
76   bool AllWriteDataConsumed();
77 
78   // EXPECTs that all data has been consumed, printing any un-consumed data.
79   void ExpectAllReadDataConsumed();
80   void ExpectAllWriteDataConsumed();
81 
82   // Resumes I/O after it is paused.
83   void Resume();
84 
85   // Creates a new `SequencedSocketData` owned by this instance of
86   // `MockQuicData`. Returns a pointer to the newly created
87   // `SequencedSocketData`.
88   SequencedSocketData* InitializeAndGetSequencedSocketData();
89 
90   // Get the `SequencedSocketData` created by `AddSocketDataToFactory` or
91   // `InitializeAndGetSequencedSocketData`.
92   SequencedSocketData* GetSequencedSocketData();
93 
94  private:
95   std::vector<std::unique_ptr<quic::QuicEncryptedPacket>> packets_;
96   std::unique_ptr<MockConnect> connect_;
97   std::vector<MockWrite> writes_;
98   std::vector<MockRead> reads_;
99   size_t sequence_number_ = 0;
100   std::unique_ptr<SequencedSocketData> socket_data_;
101   QuicPacketPrinter printer_;
102 };
103 
104 }  // namespace net::test
105 
106 #endif  // NET_QUIC_MOCK_QUIC_DATA_H_
107