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 69 private: 70 struct QueuedResult { 71 QueuedResult(const char* data, int len, Error error, Mode mode); 72 73 const char* data; 74 const int len; 75 const Error error; 76 const Mode mode; 77 }; 78 79 bool read_one_byte_at_a_time_ = false; 80 bool always_report_has_more_bytes_ = true; 81 base::queue<QueuedResult> results_; 82 bool awaiting_completion_ = false; 83 scoped_refptr<IOBuffer> dest_buffer_; 84 CompletionOnceCallback callback_; 85 int dest_buffer_size_ = 0; 86 }; 87 88 } // namespace net 89 90 #endif // NET_FILTER_MOCK_SOURCE_STREAM_H_ 91