• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class MockQuicData {
17  public:
18   explicit MockQuicData(quic::ParsedQuicVersion version);
19   ~MockQuicData();
20 
21   // Makes the Connect() call return |rv| either
22   // synchronusly or asynchronously based on |mode|.
23   void AddConnect(IoMode mode, int rv);
24 
25   // Adds a read at the next sequence number which will read |packet|
26   // synchronously or asynchronously based on |mode|.
27   void AddRead(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet);
28 
29   // Adds a read at the next sequence number which will return |rv| either
30   // synchronously or asynchronously based on |mode|.
31   void AddRead(IoMode mode, int rv);
32 
33   // Adds a write at the next sequence number which will write |packet|
34   // synchronously or asynchronously based on |mode|.
35   void AddWrite(IoMode mode, std::unique_ptr<quic::QuicEncryptedPacket> packet);
36 
37   // Adds a write at the next sequence number which will return |rv| either
38   // synchronously or asynchronously based on |mode|.
39   void AddWrite(IoMode mode, int rv);
40 
41   // Adds a write at the next sequence number which will write |packet|
42   // synchronously or asynchronously based on |mode| and return |rv|.
43   void AddWrite(IoMode mode,
44                 int rv,
45                 std::unique_ptr<quic::QuicEncryptedPacket> packet);
46 
47   // Adds the reads and writes to |factory|.
48   void AddSocketDataToFactory(MockClientSocketFactory* factory);
49 
50   // Returns true if all reads have been consumed.
51   bool AllReadDataConsumed();
52 
53   // Returns true if all writes have been consumed.
54   bool AllWriteDataConsumed();
55 
56   // Resumes I/O after it is paused.
57   void Resume();
58 
59   // Creates a new SequencedSocketData owned by this instance of MockQuicData.
60   // Returns a pointer to the newly created SequencedSocketData.
61   SequencedSocketData* InitializeAndGetSequencedSocketData();
62 
63   SequencedSocketData* GetSequencedSocketData();
64 
65  private:
66   std::vector<std::unique_ptr<quic::QuicEncryptedPacket>> packets_;
67   std::unique_ptr<MockConnect> connect_;
68   std::vector<MockWrite> writes_;
69   std::vector<MockRead> reads_;
70   size_t sequence_number_ = 0;
71   std::unique_ptr<SequencedSocketData> socket_data_;
72   QuicPacketPrinter printer_;
73 };
74 
75 }  // namespace net::test
76 
77 #endif  // NET_QUIC_MOCK_QUIC_DATA_H_
78