xref: /aosp_15_r20/external/libbrillo/brillo/streams/fake_stream.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2015 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_STREAMS_FAKE_STREAM_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_STREAMS_FAKE_STREAM_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <queue>
9*1a96fba6SXin Li #include <string>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <base/callback_forward.h>
12*1a96fba6SXin Li #include <base/macros.h>
13*1a96fba6SXin Li #include <base/time/clock.h>
14*1a96fba6SXin Li #include <base/time/time.h>
15*1a96fba6SXin Li #include <brillo/secure_blob.h>
16*1a96fba6SXin Li #include <brillo/streams/stream.h>
17*1a96fba6SXin Li 
18*1a96fba6SXin Li namespace brillo {
19*1a96fba6SXin Li 
20*1a96fba6SXin Li // Fake stream implementation for testing.
21*1a96fba6SXin Li // This class allows to provide data for the stream in tests that can be later
22*1a96fba6SXin Li // read through the Stream interface. Also, data written into the stream can be
23*1a96fba6SXin Li // later inspected and verified.
24*1a96fba6SXin Li //
25*1a96fba6SXin Li // NOTE: This class provides a fake implementation for streams with separate
26*1a96fba6SXin Li // input and output channels. That is, read and write operations do not affect
27*1a96fba6SXin Li // each other. Also, the stream implementation is sequential only (no seeking).
28*1a96fba6SXin Li // Good examples of a use case for fake stream are:
29*1a96fba6SXin Li //  - read-only sequential streams (file, memory, pipe, ...)
30*1a96fba6SXin Li //  - write-only sequential streams (same as above)
31*1a96fba6SXin Li //  - independent channel read-write streams (sockets, ...)
32*1a96fba6SXin Li //
33*1a96fba6SXin Li // For more complex read/write stream test scenarios using a real MemoryStream
34*1a96fba6SXin Li // or temporary FileStream is probably a better choice.
35*1a96fba6SXin Li class FakeStream : public Stream {
36*1a96fba6SXin Li  public:
37*1a96fba6SXin Li   // Construct a new instance of the fake stream.
38*1a96fba6SXin Li   //   mode        - expected read/write mode supported by the stream.
39*1a96fba6SXin Li   //   clock       - the clock to use to get the current time.
40*1a96fba6SXin Li   FakeStream(Stream::AccessMode mode,
41*1a96fba6SXin Li              base::Clock* clock);
42*1a96fba6SXin Li 
43*1a96fba6SXin Li   // Add data packets to the read queue of the stream.
44*1a96fba6SXin Li   // Optional |delay| indicates that the data packet should be delayed.
45*1a96fba6SXin Li   void AddReadPacketData(base::TimeDelta delay, const void* data, size_t size);
46*1a96fba6SXin Li   void AddReadPacketData(base::TimeDelta delay, brillo::Blob data);
47*1a96fba6SXin Li   void AddReadPacketString(base::TimeDelta delay, const std::string& data);
48*1a96fba6SXin Li 
49*1a96fba6SXin Li   // Schedule a read error by adding a special error packet to the queue.
50*1a96fba6SXin Li   void QueueReadError(base::TimeDelta delay);
51*1a96fba6SXin Li   void QueueReadErrorWithMessage(base::TimeDelta delay,
52*1a96fba6SXin Li                                  const std::string& message);
53*1a96fba6SXin Li 
54*1a96fba6SXin Li   // Resets read queue and clears any input data buffers.
55*1a96fba6SXin Li   void ClearReadQueue();
56*1a96fba6SXin Li 
57*1a96fba6SXin Li   // Add expectations for output data packets to be written by the stream.
58*1a96fba6SXin Li   // Optional |delay| indicates that the initial write operation for the data in
59*1a96fba6SXin Li   // the packet should be delayed.
60*1a96fba6SXin Li   // ExpectWritePacketSize just limits the size of output packet while
61*1a96fba6SXin Li   // ExpectWritePacketData also validates that the data matches that of |data|.
62*1a96fba6SXin Li   void ExpectWritePacketSize(base::TimeDelta delay, size_t data_size);
63*1a96fba6SXin Li   void ExpectWritePacketData(base::TimeDelta delay,
64*1a96fba6SXin Li                              const void* data,
65*1a96fba6SXin Li                              size_t size);
66*1a96fba6SXin Li   void ExpectWritePacketData(base::TimeDelta delay, brillo::Blob data);
67*1a96fba6SXin Li   void ExpectWritePacketString(base::TimeDelta delay, const std::string& data);
68*1a96fba6SXin Li 
69*1a96fba6SXin Li   // Schedule a write error by adding a special error packet to the queue.
70*1a96fba6SXin Li   void QueueWriteError(base::TimeDelta delay);
71*1a96fba6SXin Li   void QueueWriteErrorWithMessage(base::TimeDelta delay,
72*1a96fba6SXin Li                                   const std::string& message);
73*1a96fba6SXin Li 
74*1a96fba6SXin Li   // Resets write queue and clears any output data buffers.
75*1a96fba6SXin Li   void ClearWriteQueue();
76*1a96fba6SXin Li 
77*1a96fba6SXin Li   // Returns the output data accumulated so far by all complete write packets,
78*1a96fba6SXin Li   // or explicitly flushed.
79*1a96fba6SXin Li   const brillo::Blob& GetFlushedOutputData() const;
80*1a96fba6SXin Li   std::string GetFlushedOutputDataAsString() const;
81*1a96fba6SXin Li 
82*1a96fba6SXin Li   // Overrides from brillo::Stream.
IsOpen()83*1a96fba6SXin Li   bool IsOpen() const override { return is_open_; }
84*1a96fba6SXin Li   bool CanRead() const override;
85*1a96fba6SXin Li   bool CanWrite() const override;
CanSeek()86*1a96fba6SXin Li   bool CanSeek() const override { return false; }
CanGetSize()87*1a96fba6SXin Li   bool CanGetSize() const override { return false; }
GetSize()88*1a96fba6SXin Li   uint64_t GetSize() const override { return 0; }
89*1a96fba6SXin Li   bool SetSizeBlocking(uint64_t size, ErrorPtr* error) override;
GetRemainingSize()90*1a96fba6SXin Li   uint64_t GetRemainingSize() const override { return 0; }
GetPosition()91*1a96fba6SXin Li   uint64_t GetPosition() const override { return 0; }
92*1a96fba6SXin Li   bool Seek(int64_t offset,
93*1a96fba6SXin Li             Whence whence,
94*1a96fba6SXin Li             uint64_t* new_position,
95*1a96fba6SXin Li             ErrorPtr* error) override;
96*1a96fba6SXin Li 
97*1a96fba6SXin Li   bool ReadNonBlocking(void* buffer,
98*1a96fba6SXin Li                        size_t size_to_read,
99*1a96fba6SXin Li                        size_t* size_read,
100*1a96fba6SXin Li                        bool* end_of_stream,
101*1a96fba6SXin Li                        ErrorPtr* error) override;
102*1a96fba6SXin Li   bool WriteNonBlocking(const void* buffer,
103*1a96fba6SXin Li                         size_t size_to_write,
104*1a96fba6SXin Li                         size_t* size_written,
105*1a96fba6SXin Li                         ErrorPtr* error) override;
106*1a96fba6SXin Li   bool FlushBlocking(ErrorPtr* error) override;
107*1a96fba6SXin Li   bool CloseBlocking(ErrorPtr* error) override;
108*1a96fba6SXin Li   bool WaitForData(AccessMode mode,
109*1a96fba6SXin Li                    const base::Callback<void(AccessMode)>& callback,
110*1a96fba6SXin Li                    ErrorPtr* error) override;
111*1a96fba6SXin Li   bool WaitForDataBlocking(AccessMode in_mode,
112*1a96fba6SXin Li                            base::TimeDelta timeout,
113*1a96fba6SXin Li                            AccessMode* out_mode,
114*1a96fba6SXin Li                            ErrorPtr* error) override;
115*1a96fba6SXin Li 
116*1a96fba6SXin Li  private:
117*1a96fba6SXin Li   // Input data packet to be placed on the read queue.
118*1a96fba6SXin Li   struct InputDataPacket {
119*1a96fba6SXin Li     brillo::Blob data;  // Data to be read.
120*1a96fba6SXin Li     base::TimeDelta delay_before;  // Possible delay for the first read.
121*1a96fba6SXin Li     bool read_error{false};  // Set to true if this packet generates an error.
122*1a96fba6SXin Li   };
123*1a96fba6SXin Li 
124*1a96fba6SXin Li   // Output data packet to be placed on the write queue.
125*1a96fba6SXin Li   struct OutputDataPacket {
126*1a96fba6SXin Li     size_t expected_size{0};  // Output packet size
127*1a96fba6SXin Li     brillo::Blob data;  // Possible data to verify the output with.
128*1a96fba6SXin Li     base::TimeDelta delay_before;  // Possible delay for the first write.
129*1a96fba6SXin Li     bool write_error{false};  // Set to true if this packet generates an error.
130*1a96fba6SXin Li   };
131*1a96fba6SXin Li 
132*1a96fba6SXin Li   // Check if there is any pending read data in the input buffer.
133*1a96fba6SXin Li   bool IsReadBufferEmpty() const;
134*1a96fba6SXin Li   // Pops the next read packet from the queue and sets its data into the
135*1a96fba6SXin Li   // internal input buffer.
136*1a96fba6SXin Li   bool PopReadPacket();
137*1a96fba6SXin Li 
138*1a96fba6SXin Li   // Check if the output buffer is full.
139*1a96fba6SXin Li   bool IsWriteBufferFull() const;
140*1a96fba6SXin Li 
141*1a96fba6SXin Li   // Moves the current full output buffer into |all_output_data_|, clears the
142*1a96fba6SXin Li   // buffer, and pops the information about the next expected output packet
143*1a96fba6SXin Li   // from the write queue.
144*1a96fba6SXin Li   bool PopWritePacket();
145*1a96fba6SXin Li 
146*1a96fba6SXin Li   bool is_open_{true};
147*1a96fba6SXin Li   Stream::AccessMode mode_;
148*1a96fba6SXin Li   base::Clock* clock_;
149*1a96fba6SXin Li 
150*1a96fba6SXin Li   // Internal data for read operations.
151*1a96fba6SXin Li   std::queue<InputDataPacket> incoming_queue_;
152*1a96fba6SXin Li   base::Time delay_input_until_;
153*1a96fba6SXin Li   brillo::Blob input_buffer_;
154*1a96fba6SXin Li   size_t input_ptr_{0};
155*1a96fba6SXin Li   bool report_read_error_{false};
156*1a96fba6SXin Li 
157*1a96fba6SXin Li   // Internal data for write operations.
158*1a96fba6SXin Li   std::queue<OutputDataPacket> outgoing_queue_;
159*1a96fba6SXin Li   base::Time delay_output_until_;
160*1a96fba6SXin Li   brillo::Blob output_buffer_;
161*1a96fba6SXin Li   brillo::Blob expected_output_data_;
162*1a96fba6SXin Li   size_t max_output_buffer_size_{0};
163*1a96fba6SXin Li   bool report_write_error_{false};
164*1a96fba6SXin Li   brillo::Blob all_output_data_;
165*1a96fba6SXin Li 
166*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(FakeStream);
167*1a96fba6SXin Li };
168*1a96fba6SXin Li 
169*1a96fba6SXin Li }  // namespace brillo
170*1a96fba6SXin Li 
171*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_STREAMS_FAKE_STREAM_H_
172