1 // Copyright 2012 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 #include "net/base/mock_file_stream.h"
6
7 #include <utility>
8
9 #include "base/functional/bind.h"
10 #include "base/location.h"
11 #include "base/task/single_thread_task_runner.h"
12
13 namespace net::testing {
14
MockFileStream(const scoped_refptr<base::TaskRunner> & task_runner)15 MockFileStream::MockFileStream(
16 const scoped_refptr<base::TaskRunner>& task_runner)
17 : FileStream(task_runner) {}
18
MockFileStream(base::File file,const scoped_refptr<base::TaskRunner> & task_runner)19 MockFileStream::MockFileStream(
20 base::File file,
21 const scoped_refptr<base::TaskRunner>& task_runner)
22 : FileStream(std::move(file), task_runner) {}
23
24 MockFileStream::~MockFileStream() = default;
25
Seek(int64_t offset,Int64CompletionOnceCallback callback)26 int MockFileStream::Seek(int64_t offset, Int64CompletionOnceCallback callback) {
27 Int64CompletionOnceCallback wrapped_callback =
28 base::BindOnce(&MockFileStream::DoCallback64, weak_factory_.GetWeakPtr(),
29 std::move(callback));
30 if (forced_error_ == OK)
31 return FileStream::Seek(offset, std::move(wrapped_callback));
32 return ErrorCallback64(std::move(wrapped_callback));
33 }
34
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)35 int MockFileStream::Read(IOBuffer* buf,
36 int buf_len,
37 CompletionOnceCallback callback) {
38 CompletionOnceCallback wrapped_callback =
39 base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
40 std::move(callback));
41 if (forced_error_ == OK)
42 return FileStream::Read(buf, buf_len, std::move(wrapped_callback));
43 return ErrorCallback(std::move(wrapped_callback));
44 }
45
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)46 int MockFileStream::Write(IOBuffer* buf,
47 int buf_len,
48 CompletionOnceCallback callback) {
49 CompletionOnceCallback wrapped_callback =
50 base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
51 std::move(callback));
52 if (forced_error_ == OK)
53 return FileStream::Write(buf, buf_len, std::move(wrapped_callback));
54 return ErrorCallback(std::move(wrapped_callback));
55 }
56
Flush(CompletionOnceCallback callback)57 int MockFileStream::Flush(CompletionOnceCallback callback) {
58 CompletionOnceCallback wrapped_callback =
59 base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
60 std::move(callback));
61 if (forced_error_ == OK)
62 return FileStream::Flush(std::move(wrapped_callback));
63 return ErrorCallback(std::move(wrapped_callback));
64 }
65
ThrottleCallbacks()66 void MockFileStream::ThrottleCallbacks() {
67 CHECK(!throttled_);
68 throttled_ = true;
69 }
70
ReleaseCallbacks()71 void MockFileStream::ReleaseCallbacks() {
72 CHECK(throttled_);
73 throttled_ = false;
74
75 if (!throttled_task_.is_null()) {
76 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
77 FROM_HERE, std::move(throttled_task_));
78 }
79 }
80
DoCallback(CompletionOnceCallback callback,int result)81 void MockFileStream::DoCallback(CompletionOnceCallback callback, int result) {
82 if (!throttled_) {
83 std::move(callback).Run(result);
84 return;
85 }
86 CHECK(throttled_task_.is_null());
87 throttled_task_ = base::BindOnce(std::move(callback), result);
88 }
89
DoCallback64(Int64CompletionOnceCallback callback,int64_t result)90 void MockFileStream::DoCallback64(Int64CompletionOnceCallback callback,
91 int64_t result) {
92 if (!throttled_) {
93 std::move(callback).Run(result);
94 return;
95 }
96 CHECK(throttled_task_.is_null());
97 throttled_task_ = base::BindOnce(std::move(callback), result);
98 }
99
ErrorCallback(CompletionOnceCallback callback)100 int MockFileStream::ErrorCallback(CompletionOnceCallback callback) {
101 CHECK_NE(OK, forced_error_);
102 if (async_error_) {
103 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
104 FROM_HERE, base::BindOnce(std::move(callback), forced_error_));
105 clear_forced_error();
106 return ERR_IO_PENDING;
107 }
108 int ret = forced_error_;
109 clear_forced_error();
110 return ret;
111 }
112
ErrorCallback64(Int64CompletionOnceCallback callback)113 int64_t MockFileStream::ErrorCallback64(Int64CompletionOnceCallback callback) {
114 CHECK_NE(OK, forced_error_);
115 if (async_error_) {
116 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
117 FROM_HERE, base::BindOnce(std::move(callback), forced_error_));
118 clear_forced_error();
119 return ERR_IO_PENDING;
120 }
121 int64_t ret = forced_error_;
122 clear_forced_error();
123 return ret;
124 }
125
126 } // namespace net::testing
127