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 void AddConnect(MockConnectCompleter* completer); 35 36 // Adds a read at the next sequence number which will read |packet| 37 // synchronously or asynchronously based on |mode|. The QuicReceivedPacket 38 // version includes an ECN codepoint. 39 void AddRead(IoMode mode, std::unique_ptr<quic::QuicReceivedPacket> packet); 40 void AddRead(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet); 41 42 // Adds a read at the next sequence number which will return |rv| either 43 // synchronously or asynchronously based on |mode|. 44 void AddRead(IoMode mode, int rv); 45 46 // Adds a pause, meaning that reads will return ERR_IO_PENDING until 47 // `Resume()` is called. Read and write cannot both be paused simultaneously. 48 void AddReadPause(); 49 50 // Like `AddReadPause`, but cannot be resumed. 51 void AddReadPauseForever(); 52 53 // Adds a write at the next sequence number which will write |packet| 54 // synchronously or asynchronously based on |mode|. 55 void AddWrite(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet); 56 57 // Adds a write at the next sequence number which will return |rv| either 58 // synchronously or asynchronously based on |mode|. 59 void AddWrite(IoMode mode, int rv); 60 61 // Adds a write at the next sequence number which will write |packet| 62 // synchronously or asynchronously based on |mode| and return |rv|. 63 void AddWrite(IoMode mode, 64 int rv, 65 std::unique_ptr<quic::QuicEncryptedPacket> packet); 66 67 // Adds a pause, meaning that writes will return ERR_IO_PENDING until 68 // `Resume()` is called. Read and write cannot both be paused simultaneously. 69 void AddWritePause(); 70 71 // Adds the reads and writes to |factory|. 72 void AddSocketDataToFactory(MockClientSocketFactory* factory); 73 74 // Returns true if all reads have been consumed. 75 bool AllReadDataConsumed(); 76 77 // Returns true if all writes have been consumed. 78 bool AllWriteDataConsumed(); 79 80 // EXPECTs that all data has been consumed, printing any un-consumed data. 81 void ExpectAllReadDataConsumed(); 82 void ExpectAllWriteDataConsumed(); 83 84 // Resumes I/O after it is paused. 85 void Resume(); 86 87 // Creates a new `SequencedSocketData` owned by this instance of 88 // `MockQuicData`. Returns a pointer to the newly created 89 // `SequencedSocketData`. 90 SequencedSocketData* InitializeAndGetSequencedSocketData(); 91 92 // Get the `SequencedSocketData` created by `AddSocketDataToFactory` or 93 // `InitializeAndGetSequencedSocketData`. 94 SequencedSocketData* GetSequencedSocketData(); 95 96 private: 97 std::vector<std::unique_ptr<quic::QuicEncryptedPacket>> packets_; 98 std::unique_ptr<MockConnect> connect_; 99 std::vector<MockWrite> writes_; 100 std::vector<MockRead> reads_; 101 size_t sequence_number_ = 0; 102 std::unique_ptr<SequencedSocketData> socket_data_; 103 QuicPacketPrinter printer_; 104 }; 105 106 } // namespace net::test 107 108 #endif // NET_QUIC_MOCK_QUIC_DATA_H_ 109