• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/upload_file_element_reader.h"
6 
7 #include <stdint.h>
8 
9 #include <limits>
10 
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/run_loop.h"
14 #include "base/task/single_thread_task_runner.h"
15 #include "build/build_config.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/test/gtest_util.h"
20 #include "net/test/test_with_task_environment.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 
24 #if BUILDFLAG(IS_APPLE)
25 #include "base/apple/scoped_nsautorelease_pool.h"
26 #include "base/memory/stack_allocated.h"
27 #endif
28 
29 using net::test::IsError;
30 using net::test::IsOk;
31 
32 namespace net {
33 
34 // When the parameter is false, the UploadFileElementReader is passed only a
35 // FilePath and needs to open the file itself. When it's true, it's passed an
36 // already open base::File.
37 class UploadFileElementReaderTest : public testing::TestWithParam<bool>,
38                                     public WithTaskEnvironment {
39  protected:
SetUp()40   void SetUp() override {
41     // Some tests (*.ReadPartially) rely on bytes_.size() being even.
42     bytes_.assign({'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
43                    'd', 'e', 'f', 'g', 'h', 'i'});
44 
45     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 
47     ASSERT_TRUE(
48         base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file_path_));
49     ASSERT_TRUE(base::WriteFile(
50         temp_file_path_, std::string_view(bytes_.data(), bytes_.size())));
51 
52     reader_ =
53         CreateReader(0, std::numeric_limits<uint64_t>::max(), base::Time());
54 
55     TestCompletionCallback callback;
56     ASSERT_THAT(reader_->Init(callback.callback()), IsError(ERR_IO_PENDING));
57     EXPECT_THAT(callback.WaitForResult(), IsOk());
58     EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
59     EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
60     EXPECT_FALSE(reader_->IsInMemory());
61   }
62 
~UploadFileElementReaderTest()63   ~UploadFileElementReaderTest() override {
64     reader_.reset();
65     base::RunLoop().RunUntilIdle();
66   }
67 
68   // Creates a UploadFileElementReader based on the value of GetParam().
CreateReader(int64_t offset,int64_t length,base::Time expected_modification_time)69   std::unique_ptr<UploadFileElementReader> CreateReader(
70       int64_t offset,
71       int64_t length,
72       base::Time expected_modification_time) {
73     if (GetParam()) {
74       return std::make_unique<UploadFileElementReader>(
75           base::SingleThreadTaskRunner::GetCurrentDefault().get(),
76           temp_file_path_, offset, length, expected_modification_time);
77     }
78 
79     // The base::File::FLAG_WIN_SHARE_DELETE lets the file be deleted without
80     // the test fixture waiting on it to be closed.
81     int open_flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
82                      base::File::FLAG_WIN_SHARE_DELETE;
83 #if BUILDFLAG(IS_WIN)
84     // On Windows, file must be opened for asynchronous operation.
85     open_flags |= base::File::FLAG_ASYNC;
86 #endif  // BUILDFLAG(IS_WIN)
87 
88     base::File file(temp_file_path_, open_flags);
89     EXPECT_TRUE(file.IsValid());
90     return std::make_unique<UploadFileElementReader>(
91         base::SingleThreadTaskRunner::GetCurrentDefault().get(),
92         std::move(file),
93         // Use an incorrect path, to make sure that the file is never re-opened.
94         base::FilePath(FILE_PATH_LITERAL("this_should_be_ignored")), offset,
95         length, expected_modification_time);
96   }
97 
98 #if BUILDFLAG(IS_APPLE)
99   // May be needed to avoid leaks on the Mac.
100   STACK_ALLOCATED_IGNORE("https://crbug.com/1424190")
101   base::apple::ScopedNSAutoreleasePool scoped_pool_;
102 #endif
103 
104   std::vector<char> bytes_;
105   std::unique_ptr<UploadElementReader> reader_;
106   base::ScopedTempDir temp_dir_;
107   base::FilePath temp_file_path_;
108 };
109 
TEST_P(UploadFileElementReaderTest,ReadPartially)110 TEST_P(UploadFileElementReaderTest, ReadPartially) {
111   const size_t kHalfSize = bytes_.size() / 2;
112   ASSERT_EQ(bytes_.size(), kHalfSize * 2);
113   std::vector<char> buf(kHalfSize);
114   scoped_refptr<IOBuffer> wrapped_buffer =
115       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
116   TestCompletionCallback read_callback1;
117   ASSERT_EQ(ERR_IO_PENDING,
118             reader_->Read(
119                 wrapped_buffer.get(), buf.size(), read_callback1.callback()));
120   EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult());
121   EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
122   EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + kHalfSize), buf);
123 
124   TestCompletionCallback read_callback2;
125   EXPECT_EQ(ERR_IO_PENDING,
126             reader_->Read(
127                 wrapped_buffer.get(), buf.size(), read_callback2.callback()));
128   EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult());
129   EXPECT_EQ(0U, reader_->BytesRemaining());
130   EXPECT_EQ(std::vector<char>(bytes_.begin() + kHalfSize, bytes_.end()), buf);
131 }
132 
TEST_P(UploadFileElementReaderTest,ReadAll)133 TEST_P(UploadFileElementReaderTest, ReadAll) {
134   std::vector<char> buf(bytes_.size());
135   scoped_refptr<IOBuffer> wrapped_buffer =
136       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
137   TestCompletionCallback read_callback;
138   ASSERT_EQ(ERR_IO_PENDING,
139             reader_->Read(
140                 wrapped_buffer.get(), buf.size(), read_callback.callback()));
141   EXPECT_EQ(static_cast<int>(buf.size()), read_callback.WaitForResult());
142   EXPECT_EQ(0U, reader_->BytesRemaining());
143   EXPECT_EQ(bytes_, buf);
144   // Try to read again.
145   EXPECT_EQ(0,
146             reader_->Read(
147                 wrapped_buffer.get(), buf.size(), read_callback.callback()));
148 }
149 
TEST_P(UploadFileElementReaderTest,ReadTooMuch)150 TEST_P(UploadFileElementReaderTest, ReadTooMuch) {
151   const size_t kTooLargeSize = bytes_.size() * 2;
152   std::vector<char> buf(kTooLargeSize);
153   scoped_refptr<IOBuffer> wrapped_buffer =
154       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
155   TestCompletionCallback read_callback;
156   ASSERT_EQ(ERR_IO_PENDING,
157             reader_->Read(
158                 wrapped_buffer.get(), buf.size(), read_callback.callback()));
159   EXPECT_EQ(static_cast<int>(bytes_.size()), read_callback.WaitForResult());
160   EXPECT_EQ(0U, reader_->BytesRemaining());
161   buf.resize(bytes_.size());  // Resize to compare.
162   EXPECT_EQ(bytes_, buf);
163 }
164 
TEST_P(UploadFileElementReaderTest,MultipleInit)165 TEST_P(UploadFileElementReaderTest, MultipleInit) {
166   std::vector<char> buf(bytes_.size());
167   scoped_refptr<IOBuffer> wrapped_buffer =
168       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
169 
170   // Read all.
171   TestCompletionCallback read_callback1;
172   ASSERT_EQ(ERR_IO_PENDING,
173             reader_->Read(
174                 wrapped_buffer.get(), buf.size(), read_callback1.callback()));
175   EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult());
176   EXPECT_EQ(0U, reader_->BytesRemaining());
177   EXPECT_EQ(bytes_, buf);
178 
179   // Call Init() again to reset the state.
180   TestCompletionCallback init_callback;
181   ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING));
182   EXPECT_THAT(init_callback.WaitForResult(), IsOk());
183   EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
184   EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
185 
186   // Read again.
187   TestCompletionCallback read_callback2;
188   ASSERT_EQ(ERR_IO_PENDING,
189             reader_->Read(
190                 wrapped_buffer.get(), buf.size(), read_callback2.callback()));
191   EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult());
192   EXPECT_EQ(0U, reader_->BytesRemaining());
193   EXPECT_EQ(bytes_, buf);
194 }
195 
TEST_P(UploadFileElementReaderTest,InitDuringAsyncOperation)196 TEST_P(UploadFileElementReaderTest, InitDuringAsyncOperation) {
197   std::vector<char> buf(bytes_.size());
198   scoped_refptr<IOBuffer> wrapped_buffer =
199       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
200 
201   // Start reading all.
202   TestCompletionCallback read_callback1;
203   EXPECT_EQ(ERR_IO_PENDING,
204             reader_->Read(
205                 wrapped_buffer.get(), buf.size(), read_callback1.callback()));
206 
207   // Call Init to cancel the previous read.
208   TestCompletionCallback init_callback1;
209   EXPECT_THAT(reader_->Init(init_callback1.callback()),
210               IsError(ERR_IO_PENDING));
211 
212   // Call Init again to cancel the previous init.
213   TestCompletionCallback init_callback2;
214   EXPECT_THAT(reader_->Init(init_callback2.callback()),
215               IsError(ERR_IO_PENDING));
216   EXPECT_THAT(init_callback2.WaitForResult(), IsOk());
217   EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
218   EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
219 
220   // Read half.
221   std::vector<char> buf2(bytes_.size() / 2);
222   scoped_refptr<IOBuffer> wrapped_buffer2 =
223       base::MakeRefCounted<WrappedIOBuffer>(buf2.data(), buf2.size());
224   TestCompletionCallback read_callback2;
225   EXPECT_EQ(ERR_IO_PENDING,
226             reader_->Read(
227                 wrapped_buffer2.get(), buf2.size(), read_callback2.callback()));
228   EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult());
229   EXPECT_EQ(bytes_.size() - buf2.size(), reader_->BytesRemaining());
230   EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + buf2.size()),
231             buf2);
232 
233   // Make sure callbacks are not called for cancelled operations.
234   EXPECT_FALSE(read_callback1.have_result());
235   EXPECT_FALSE(init_callback1.have_result());
236 }
237 
TEST_P(UploadFileElementReaderTest,RepeatedInitDuringInit)238 TEST_P(UploadFileElementReaderTest, RepeatedInitDuringInit) {
239   std::vector<char> buf(bytes_.size());
240   scoped_refptr<IOBuffer> wrapped_buffer =
241       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
242 
243   TestCompletionCallback init_callback1;
244   EXPECT_THAT(reader_->Init(init_callback1.callback()),
245               IsError(ERR_IO_PENDING));
246 
247   // Call Init again to cancel the previous init.
248   TestCompletionCallback init_callback2;
249   EXPECT_THAT(reader_->Init(init_callback2.callback()),
250               IsError(ERR_IO_PENDING));
251 
252   // Call Init yet again to cancel the previous init.
253   TestCompletionCallback init_callback3;
254   EXPECT_THAT(reader_->Init(init_callback3.callback()),
255               IsError(ERR_IO_PENDING));
256 
257   EXPECT_THAT(init_callback3.WaitForResult(), IsOk());
258   EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
259   EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
260 
261   // Read all.
262   TestCompletionCallback read_callback;
263   int result =
264       reader_->Read(wrapped_buffer.get(), buf.size(), read_callback.callback());
265   EXPECT_EQ(static_cast<int>(buf.size()), read_callback.GetResult(result));
266   EXPECT_EQ(0U, reader_->BytesRemaining());
267   EXPECT_EQ(bytes_, buf);
268 
269   EXPECT_FALSE(init_callback1.have_result());
270   EXPECT_FALSE(init_callback2.have_result());
271 }
272 
TEST_P(UploadFileElementReaderTest,Range)273 TEST_P(UploadFileElementReaderTest, Range) {
274   const uint64_t kOffset = 2;
275   const uint64_t kLength = bytes_.size() - kOffset * 3;
276   reader_ = CreateReader(kOffset, kLength, base::Time());
277   TestCompletionCallback init_callback;
278   ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING));
279   EXPECT_THAT(init_callback.WaitForResult(), IsOk());
280   EXPECT_EQ(kLength, reader_->GetContentLength());
281   EXPECT_EQ(kLength, reader_->BytesRemaining());
282   std::vector<char> buf(kLength);
283   scoped_refptr<IOBuffer> wrapped_buffer =
284       base::MakeRefCounted<WrappedIOBuffer>(buf.data(), buf.size());
285   TestCompletionCallback read_callback;
286   ASSERT_EQ(
287       ERR_IO_PENDING,
288       reader_->Read(wrapped_buffer.get(), kLength, read_callback.callback()));
289   EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult());
290   const std::vector<char> expected(bytes_.begin() + kOffset,
291                                    bytes_.begin() + kOffset + kLength);
292   EXPECT_EQ(expected, buf);
293 }
294 
TEST_P(UploadFileElementReaderTest,FileChanged)295 TEST_P(UploadFileElementReaderTest, FileChanged) {
296   base::File::Info info;
297   ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info));
298 
299   // Expect one second before the actual modification time to simulate change.
300   const base::Time expected_modification_time =
301       info.last_modified - base::Seconds(1);
302   reader_ = CreateReader(0, std::numeric_limits<uint64_t>::max(),
303                          expected_modification_time);
304   TestCompletionCallback init_callback;
305   ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING));
306   EXPECT_THAT(init_callback.WaitForResult(), IsError(ERR_UPLOAD_FILE_CHANGED));
307 }
308 
TEST_P(UploadFileElementReaderTest,InexactExpectedTimeStamp)309 TEST_P(UploadFileElementReaderTest, InexactExpectedTimeStamp) {
310   base::File::Info info;
311   ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info));
312 
313   const base::Time expected_modification_time =
314       info.last_modified - base::Milliseconds(900);
315   reader_ = CreateReader(0, std::numeric_limits<uint64_t>::max(),
316                          expected_modification_time);
317   TestCompletionCallback init_callback;
318   ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING));
319   EXPECT_THAT(init_callback.WaitForResult(), IsOk());
320 }
321 
TEST_P(UploadFileElementReaderTest,WrongPath)322 TEST_P(UploadFileElementReaderTest, WrongPath) {
323   const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path"));
324   reader_ = std::make_unique<UploadFileElementReader>(
325       base::SingleThreadTaskRunner::GetCurrentDefault().get(), wrong_path, 0,
326       std::numeric_limits<uint64_t>::max(), base::Time());
327   TestCompletionCallback init_callback;
328   ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING));
329   EXPECT_THAT(init_callback.WaitForResult(), IsError(ERR_FILE_NOT_FOUND));
330 }
331 
332 INSTANTIATE_TEST_SUITE_P(All,
333                          UploadFileElementReaderTest,
334                          testing::ValuesIn({false, true}));
335 
336 }  // namespace net
337