mock_file_stream.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // This file defines MockFileStream, a test class for FileStream.
  5. #ifndef NET_BASE_MOCK_FILE_STREAM_H_
  6. #define NET_BASE_MOCK_FILE_STREAM_H_
  7. #include <stdint.h>
  8. #include "base/compiler_specific.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "net/base/completion_once_callback.h"
  12. #include "net/base/file_stream.h"
  13. #include "net/base/net_errors.h"
  14. namespace net {
  15. class IOBuffer;
  16. namespace testing {
  17. class MockFileStream : public FileStream {
  18. public:
  19. explicit MockFileStream(const scoped_refptr<base::TaskRunner>& task_runner);
  20. MockFileStream(base::File file,
  21. const scoped_refptr<base::TaskRunner>& task_runner);
  22. ~MockFileStream() override;
  23. // FileStream methods.
  24. int Seek(int64_t offset, Int64CompletionOnceCallback callback) override;
  25. int Read(IOBuffer* buf,
  26. int buf_len,
  27. CompletionOnceCallback callback) override;
  28. int Write(IOBuffer* buf,
  29. int buf_len,
  30. CompletionOnceCallback callback) override;
  31. int Flush(CompletionOnceCallback callback) override;
  32. void set_forced_error_async(int error) {
  33. forced_error_ = error;
  34. async_error_ = true;
  35. }
  36. void set_forced_error(int error) {
  37. forced_error_ = error;
  38. async_error_ = false;
  39. }
  40. void clear_forced_error() {
  41. forced_error_ = OK;
  42. async_error_ = false;
  43. }
  44. int forced_error() const { return forced_error_; }
  45. const base::FilePath& get_path() const { return path_; }
  46. // Throttles all asynchronous callbacks, including forced errors, until a
  47. // matching ReleaseCallbacks call.
  48. void ThrottleCallbacks();
  49. // Resumes running asynchronous callbacks and runs any throttled callbacks.
  50. void ReleaseCallbacks();
  51. private:
  52. int ReturnError(int function_error) {
  53. if (forced_error_ != OK) {
  54. int ret = forced_error_;
  55. clear_forced_error();
  56. return ret;
  57. }
  58. return function_error;
  59. }
  60. int64_t ReturnError64(int64_t function_error) {
  61. if (forced_error_ != OK) {
  62. int64_t ret = forced_error_;
  63. clear_forced_error();
  64. return ret;
  65. }
  66. return function_error;
  67. }
  68. // Wrappers for callbacks to make them honor ThrottleCallbacks and
  69. // ReleaseCallbacks.
  70. void DoCallback(CompletionOnceCallback callback, int result);
  71. void DoCallback64(Int64CompletionOnceCallback callback, int64_t result);
  72. // Depending on |async_error_|, either synchronously returns |forced_error_|
  73. // asynchronously calls |callback| with |async_error_|.
  74. int ErrorCallback(CompletionOnceCallback callback);
  75. int64_t ErrorCallback64(Int64CompletionOnceCallback callback);
  76. int forced_error_ = OK;
  77. bool async_error_ = false;
  78. bool throttled_ = false;
  79. base::OnceClosure throttled_task_;
  80. base::FilePath path_;
  81. base::WeakPtrFactory<MockFileStream> weak_factory_{this};
  82. };
  83. } // namespace testing
  84. } // namespace net
  85. #endif // NET_BASE_MOCK_FILE_STREAM_H_