1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This file defines MockFileStream, a test class for FileStream. 6 7 #ifndef NET_BASE_MOCK_FILE_STREAM_H_ 8 #define NET_BASE_MOCK_FILE_STREAM_H_ 9 10 #include <stdint.h> 11 12 #include "base/compiler_specific.h" 13 #include "base/files/file_path.h" 14 #include "base/memory/weak_ptr.h" 15 #include "net/base/completion_once_callback.h" 16 #include "net/base/file_stream.h" 17 #include "net/base/net_errors.h" 18 19 namespace net { 20 21 class IOBuffer; 22 23 namespace testing { 24 25 class MockFileStream : public FileStream { 26 public: 27 explicit MockFileStream(const scoped_refptr<base::TaskRunner>& task_runner); 28 MockFileStream(base::File file, 29 const scoped_refptr<base::TaskRunner>& task_runner); 30 ~MockFileStream() override; 31 32 // FileStream methods. 33 int Seek(int64_t offset, Int64CompletionOnceCallback callback) override; 34 int Read(IOBuffer* buf, 35 int buf_len, 36 CompletionOnceCallback callback) override; 37 int Write(IOBuffer* buf, 38 int buf_len, 39 CompletionOnceCallback callback) override; 40 int Flush(CompletionOnceCallback callback) override; 41 set_forced_error_async(int error)42 void set_forced_error_async(int error) { 43 forced_error_ = error; 44 async_error_ = true; 45 } set_forced_error(int error)46 void set_forced_error(int error) { 47 forced_error_ = error; 48 async_error_ = false; 49 } clear_forced_error()50 void clear_forced_error() { 51 forced_error_ = OK; 52 async_error_ = false; 53 } forced_error()54 int forced_error() const { return forced_error_; } get_path()55 const base::FilePath& get_path() const { return path_; } 56 57 // Throttles all asynchronous callbacks, including forced errors, until a 58 // matching ReleaseCallbacks call. 59 void ThrottleCallbacks(); 60 61 // Resumes running asynchronous callbacks and runs any throttled callbacks. 62 void ReleaseCallbacks(); 63 64 private: ReturnError(int function_error)65 int ReturnError(int function_error) { 66 if (forced_error_ != OK) { 67 int ret = forced_error_; 68 clear_forced_error(); 69 return ret; 70 } 71 72 return function_error; 73 } 74 ReturnError64(int64_t function_error)75 int64_t ReturnError64(int64_t function_error) { 76 if (forced_error_ != OK) { 77 int64_t ret = forced_error_; 78 clear_forced_error(); 79 return ret; 80 } 81 82 return function_error; 83 } 84 85 // Wrappers for callbacks to make them honor ThrottleCallbacks and 86 // ReleaseCallbacks. 87 void DoCallback(CompletionOnceCallback callback, int result); 88 void DoCallback64(Int64CompletionOnceCallback callback, int64_t result); 89 90 // Depending on |async_error_|, either synchronously returns |forced_error_| 91 // asynchronously calls |callback| with |async_error_|. 92 int ErrorCallback(CompletionOnceCallback callback); 93 int64_t ErrorCallback64(Int64CompletionOnceCallback callback); 94 95 int forced_error_; 96 bool async_error_; 97 bool throttled_; 98 base::OnceClosure throttled_task_; 99 base::FilePath path_; 100 101 base::WeakPtrFactory<MockFileStream> weak_factory_{this}; 102 }; 103 104 } // namespace testing 105 106 } // namespace net 107 108 #endif // NET_BASE_MOCK_FILE_STREAM_H_ 109