xref: /aosp_15_r20/external/cronet/net/filter/mock_source_stream.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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_FILTER_MOCK_SOURCE_STREAM_H_
6 #define NET_FILTER_MOCK_SOURCE_STREAM_H_
7 
8 #include <string>
9 
10 #include "base/containers/queue.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "net/base/completion_once_callback.h"
13 #include "net/base/net_errors.h"
14 #include "net/filter/source_stream.h"
15 
16 namespace net {
17 
18 class IOBuffer;
19 
20 // A SourceStream implementation used in tests. This allows tests to specify
21 // what data to return for each Read() call.
22 class MockSourceStream : public SourceStream {
23  public:
24   enum Mode {
25     SYNC,
26     ASYNC,
27   };
28   MockSourceStream();
29 
30   MockSourceStream(const MockSourceStream&) = delete;
31   MockSourceStream& operator=(const MockSourceStream&) = delete;
32 
33   // The destructor will crash in debug build if there is any pending read.
34   ~MockSourceStream() override;
35 
36   // SourceStream implementation
37   int Read(IOBuffer* dest_buffer,
38            int buffer_size,
39            CompletionOnceCallback callback) override;
40   std::string Description() const override;
41   bool MayHaveMoreBytes() const override;
42 
43   // Enqueues a result to be returned by |Read|. This method does not make a
44   // copy of |data|, so |data| must outlive this object. If |mode| is SYNC,
45   // |Read| will return the supplied data synchronously; otherwise, consumer
46   // needs to call |CompleteNextRead|
47   void AddReadResult(const char* data, int len, Error error, Mode mode);
48 
49   // Completes a pending Read() call. Crash in debug build if there is no
50   // pending read.
51   void CompleteNextRead();
52 
53   // Affects behavior or AddReadResult.  When set to true, each character in
54   // |data| passed to AddReadResult will be read as an individual byte, instead
55   // of all at once. Default to false.
56   // Note that setting it only affects future calls to AddReadResult, not
57   // previous ones.
set_read_one_byte_at_a_time(bool read_one_byte_at_a_time)58   void set_read_one_byte_at_a_time(bool read_one_byte_at_a_time) {
59     read_one_byte_at_a_time_ = read_one_byte_at_a_time;
60   }
61 
set_always_report_has_more_bytes(bool always_report_has_more_bytes)62   void set_always_report_has_more_bytes(bool always_report_has_more_bytes) {
63     always_report_has_more_bytes_ = always_report_has_more_bytes;
64   }
65 
66   // Returns true if a read is waiting to be completed.
awaiting_completion()67   bool awaiting_completion() const { return awaiting_completion_; }
68 
set_expect_all_input_consumed(bool expect_all_input_consumed)69   void set_expect_all_input_consumed(bool expect_all_input_consumed) {
70     expect_all_input_consumed_ = expect_all_input_consumed;
71   }
72 
73  private:
74   struct QueuedResult {
75     QueuedResult(const char* data, int len, Error error, Mode mode);
76 
77     const char* data;
78     const int len;
79     const Error error;
80     const Mode mode;
81   };
82 
83   bool read_one_byte_at_a_time_ = false;
84   bool always_report_has_more_bytes_ = true;
85   base::queue<QueuedResult> results_;
86   bool awaiting_completion_ = false;
87   scoped_refptr<IOBuffer> dest_buffer_;
88   CompletionOnceCallback callback_;
89   int dest_buffer_size_ = 0;
90   bool expect_all_input_consumed_ = true;
91 };
92 
93 }  // namespace net
94 
95 #endif  // NET_FILTER_MOCK_SOURCE_STREAM_H_
96