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 "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/files/file_path.h" 13 #include "net/base/file_stream.h" 14 #include "net/base/net_errors.h" 15 16 namespace net { 17 18 class IOBuffer; 19 20 namespace testing { 21 22 class MockFileStream : public net::FileStream { 23 public: 24 MockFileStream(net::NetLog* net_log) 25 : net::FileStream(net_log), forced_error_(net::OK) {} 26 27 MockFileStream(base::PlatformFile file, int flags, net::NetLog* net_log) 28 : net::FileStream(file, flags, net_log), forced_error_(net::OK) {} 29 30 // FileStream methods. 31 virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; 32 virtual int Seek(net::Whence whence, int64 offset, 33 const Int64CompletionCallback& callback) OVERRIDE; 34 virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; 35 virtual int64 Available() OVERRIDE; 36 virtual int Read(IOBuffer* buf, 37 int buf_len, 38 const CompletionCallback& callback) OVERRIDE; 39 virtual int ReadSync(char* buf, int buf_len) OVERRIDE; 40 virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; 41 virtual int Write(IOBuffer* buf, 42 int buf_len, 43 const CompletionCallback& callback) OVERRIDE; 44 virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; 45 virtual int64 Truncate(int64 bytes) OVERRIDE; 46 virtual int Flush(const CompletionCallback& callback) OVERRIDE; 47 virtual int FlushSync() OVERRIDE; 48 49 void set_forced_error(int error) { forced_error_ = error; } 50 void clear_forced_error() { forced_error_ = net::OK; } 51 int forced_error() const { return forced_error_; } 52 const base::FilePath& get_path() const { return path_; } 53 54 private: 55 int ReturnError(int function_error) { 56 if (forced_error_ != net::OK) { 57 int ret = forced_error_; 58 clear_forced_error(); 59 return ret; 60 } 61 62 return function_error; 63 } 64 65 int64 ReturnError64(int64 function_error) { 66 if (forced_error_ != net::OK) { 67 int64 ret = forced_error_; 68 clear_forced_error(); 69 return ret; 70 } 71 72 return function_error; 73 } 74 75 int forced_error_; 76 base::FilePath path_; 77 }; 78 79 } // namespace testing 80 81 } // namespace net 82 83 #endif // NET_BASE_MOCK_FILE_STREAM_H_ 84