OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/upload_file_element_reader.h" | 5 #include "net/base/upload_file_element_reader.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <limits> |
| 10 |
7 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
9 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
10 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
11 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
13 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
16 | 20 |
(...skipping 10 matching lines...) Expand all Loading... |
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
28 | 32 |
29 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 33 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
30 &temp_file_path_)); | 34 &temp_file_path_)); |
31 ASSERT_EQ( | 35 ASSERT_EQ( |
32 static_cast<int>(bytes_.size()), | 36 static_cast<int>(bytes_.size()), |
33 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); | 37 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); |
34 | 38 |
35 reader_.reset(new UploadFileElementReader( | 39 reader_.reset(new UploadFileElementReader( |
36 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, | 40 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
37 kuint64max, base::Time())); | 41 std::numeric_limits<uint64_t>::max(), base::Time())); |
38 TestCompletionCallback callback; | 42 TestCompletionCallback callback; |
39 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); | 43 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); |
40 EXPECT_EQ(OK, callback.WaitForResult()); | 44 EXPECT_EQ(OK, callback.WaitForResult()); |
41 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 45 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
42 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 46 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
43 EXPECT_FALSE(reader_->IsInMemory()); | 47 EXPECT_FALSE(reader_->IsInMemory()); |
44 } | 48 } |
45 | 49 |
46 ~UploadFileElementReaderTest() override { | 50 ~UploadFileElementReaderTest() override { |
47 reader_.reset(); | 51 reader_.reset(); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 } | 202 } |
199 | 203 |
200 TEST_F(UploadFileElementReaderTest, FileChanged) { | 204 TEST_F(UploadFileElementReaderTest, FileChanged) { |
201 base::File::Info info; | 205 base::File::Info info; |
202 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 206 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
203 | 207 |
204 // Expect one second before the actual modification time to simulate change. | 208 // Expect one second before the actual modification time to simulate change. |
205 const base::Time expected_modification_time = | 209 const base::Time expected_modification_time = |
206 info.last_modified - base::TimeDelta::FromSeconds(1); | 210 info.last_modified - base::TimeDelta::FromSeconds(1); |
207 reader_.reset(new UploadFileElementReader( | 211 reader_.reset(new UploadFileElementReader( |
208 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, kuint64max, | 212 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
209 expected_modification_time)); | 213 std::numeric_limits<uint64_t>::max(), expected_modification_time)); |
210 TestCompletionCallback init_callback; | 214 TestCompletionCallback init_callback; |
211 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 215 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
212 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); | 216 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); |
213 } | 217 } |
214 | 218 |
215 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { | 219 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { |
216 base::File::Info info; | 220 base::File::Info info; |
217 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 221 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
218 | 222 |
219 const base::Time expected_modification_time = | 223 const base::Time expected_modification_time = |
220 info.last_modified - base::TimeDelta::FromMilliseconds(900); | 224 info.last_modified - base::TimeDelta::FromMilliseconds(900); |
221 reader_.reset(new UploadFileElementReader( | 225 reader_.reset(new UploadFileElementReader( |
222 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, kuint64max, | 226 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
223 expected_modification_time)); | 227 std::numeric_limits<uint64_t>::max(), expected_modification_time)); |
224 TestCompletionCallback init_callback; | 228 TestCompletionCallback init_callback; |
225 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 229 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
226 EXPECT_EQ(OK, init_callback.WaitForResult()); | 230 EXPECT_EQ(OK, init_callback.WaitForResult()); |
227 } | 231 } |
228 | 232 |
229 TEST_F(UploadFileElementReaderTest, WrongPath) { | 233 TEST_F(UploadFileElementReaderTest, WrongPath) { |
230 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 234 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
231 reader_.reset( | 235 reader_.reset(new UploadFileElementReader( |
232 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), | 236 base::ThreadTaskRunnerHandle::Get().get(), wrong_path, 0, |
233 wrong_path, 0, kuint64max, base::Time())); | 237 std::numeric_limits<uint64_t>::max(), base::Time())); |
234 TestCompletionCallback init_callback; | 238 TestCompletionCallback init_callback; |
235 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 239 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
236 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); | 240 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); |
237 } | 241 } |
238 | 242 |
239 } // namespace net | 243 } // namespace net |
OLD | NEW |