| 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 "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | |
| 10 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 class UploadFileElementReaderTest : public PlatformTest { | 19 class UploadFileElementReaderTest : public PlatformTest { |
| 20 protected: | 20 protected: |
| 21 void SetUp() override { | 21 void SetUp() override { |
| 22 PlatformTest::SetUp(); | 22 PlatformTest::SetUp(); |
| 23 // Some tests (*.ReadPartially) rely on bytes_.size() being even. | 23 // Some tests (*.ReadPartially) rely on bytes_.size() being even. |
| 24 const char kData[] = "123456789abcdefghi"; | 24 const char kData[] = "123456789abcdefghi"; |
| 25 bytes_.assign(kData, kData + arraysize(kData) - 1); | 25 bytes_.assign(kData, kData + arraysize(kData) - 1); |
| 26 | 26 |
| 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 28 | 28 |
| 29 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 29 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
| 30 &temp_file_path_)); | 30 &temp_file_path_)); |
| 31 ASSERT_EQ( | 31 ASSERT_EQ( |
| 32 static_cast<int>(bytes_.size()), | 32 static_cast<int>(bytes_.size()), |
| 33 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); | 33 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); |
| 34 | 34 |
| 35 reader_.reset( | 35 reader_.reset(new UploadFileElementReader( |
| 36 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 36 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
| 37 temp_file_path_, | 37 kuint64max, base::Time())); |
| 38 0, | |
| 39 kuint64max, | |
| 40 base::Time())); | |
| 41 TestCompletionCallback callback; | 38 TestCompletionCallback callback; |
| 42 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); | 39 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); |
| 43 EXPECT_EQ(OK, callback.WaitForResult()); | 40 EXPECT_EQ(OK, callback.WaitForResult()); |
| 44 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 41 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
| 45 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 42 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
| 46 EXPECT_FALSE(reader_->IsInMemory()); | 43 EXPECT_FALSE(reader_->IsInMemory()); |
| 47 } | 44 } |
| 48 | 45 |
| 49 ~UploadFileElementReaderTest() override { | 46 ~UploadFileElementReaderTest() override { |
| 50 reader_.reset(); | 47 reader_.reset(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 buf2); | 170 buf2); |
| 174 | 171 |
| 175 // Make sure callbacks are not called for cancelled operations. | 172 // Make sure callbacks are not called for cancelled operations. |
| 176 EXPECT_FALSE(read_callback1.have_result()); | 173 EXPECT_FALSE(read_callback1.have_result()); |
| 177 EXPECT_FALSE(init_callback1.have_result()); | 174 EXPECT_FALSE(init_callback1.have_result()); |
| 178 } | 175 } |
| 179 | 176 |
| 180 TEST_F(UploadFileElementReaderTest, Range) { | 177 TEST_F(UploadFileElementReaderTest, Range) { |
| 181 const uint64 kOffset = 2; | 178 const uint64 kOffset = 2; |
| 182 const uint64 kLength = bytes_.size() - kOffset * 3; | 179 const uint64 kLength = bytes_.size() - kOffset * 3; |
| 183 reader_.reset( | 180 reader_.reset(new UploadFileElementReader( |
| 184 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 181 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, kOffset, |
| 185 temp_file_path_, | 182 kLength, base::Time())); |
| 186 kOffset, | |
| 187 kLength, | |
| 188 base::Time())); | |
| 189 TestCompletionCallback init_callback; | 183 TestCompletionCallback init_callback; |
| 190 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 184 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
| 191 EXPECT_EQ(OK, init_callback.WaitForResult()); | 185 EXPECT_EQ(OK, init_callback.WaitForResult()); |
| 192 EXPECT_EQ(kLength, reader_->GetContentLength()); | 186 EXPECT_EQ(kLength, reader_->GetContentLength()); |
| 193 EXPECT_EQ(kLength, reader_->BytesRemaining()); | 187 EXPECT_EQ(kLength, reader_->BytesRemaining()); |
| 194 std::vector<char> buf(kLength); | 188 std::vector<char> buf(kLength); |
| 195 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); | 189 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); |
| 196 TestCompletionCallback read_callback; | 190 TestCompletionCallback read_callback; |
| 197 ASSERT_EQ( | 191 ASSERT_EQ( |
| 198 ERR_IO_PENDING, | 192 ERR_IO_PENDING, |
| 199 reader_->Read(wrapped_buffer.get(), kLength, read_callback.callback())); | 193 reader_->Read(wrapped_buffer.get(), kLength, read_callback.callback())); |
| 200 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); | 194 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); |
| 201 const std::vector<char> expected(bytes_.begin() + kOffset, | 195 const std::vector<char> expected(bytes_.begin() + kOffset, |
| 202 bytes_.begin() + kOffset + kLength); | 196 bytes_.begin() + kOffset + kLength); |
| 203 EXPECT_EQ(expected, buf); | 197 EXPECT_EQ(expected, buf); |
| 204 } | 198 } |
| 205 | 199 |
| 206 TEST_F(UploadFileElementReaderTest, FileChanged) { | 200 TEST_F(UploadFileElementReaderTest, FileChanged) { |
| 207 base::File::Info info; | 201 base::File::Info info; |
| 208 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 202 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
| 209 | 203 |
| 210 // Expect one second before the actual modification time to simulate change. | 204 // Expect one second before the actual modification time to simulate change. |
| 211 const base::Time expected_modification_time = | 205 const base::Time expected_modification_time = |
| 212 info.last_modified - base::TimeDelta::FromSeconds(1); | 206 info.last_modified - base::TimeDelta::FromSeconds(1); |
| 213 reader_.reset(new UploadFileElementReader( | 207 reader_.reset(new UploadFileElementReader( |
| 214 base::MessageLoopProxy::current().get(), temp_file_path_, 0, kuint64max, | 208 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, kuint64max, |
| 215 expected_modification_time)); | 209 expected_modification_time)); |
| 216 TestCompletionCallback init_callback; | 210 TestCompletionCallback init_callback; |
| 217 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 211 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
| 218 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); | 212 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); |
| 219 } | 213 } |
| 220 | 214 |
| 221 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { | 215 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { |
| 222 base::File::Info info; | 216 base::File::Info info; |
| 223 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 217 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
| 224 | 218 |
| 225 const base::Time expected_modification_time = | 219 const base::Time expected_modification_time = |
| 226 info.last_modified - base::TimeDelta::FromMilliseconds(900); | 220 info.last_modified - base::TimeDelta::FromMilliseconds(900); |
| 227 reader_.reset(new UploadFileElementReader( | 221 reader_.reset(new UploadFileElementReader( |
| 228 base::MessageLoopProxy::current().get(), temp_file_path_, 0, kuint64max, | 222 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, kuint64max, |
| 229 expected_modification_time)); | 223 expected_modification_time)); |
| 230 TestCompletionCallback init_callback; | 224 TestCompletionCallback init_callback; |
| 231 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 225 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
| 232 EXPECT_EQ(OK, init_callback.WaitForResult()); | 226 EXPECT_EQ(OK, init_callback.WaitForResult()); |
| 233 } | 227 } |
| 234 | 228 |
| 235 TEST_F(UploadFileElementReaderTest, WrongPath) { | 229 TEST_F(UploadFileElementReaderTest, WrongPath) { |
| 236 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 230 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
| 237 reader_.reset( | 231 reader_.reset( |
| 238 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 232 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), |
| 239 wrong_path, 0, kuint64max, base::Time())); | 233 wrong_path, 0, kuint64max, base::Time())); |
| 240 TestCompletionCallback init_callback; | 234 TestCompletionCallback init_callback; |
| 241 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 235 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
| 242 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); | 236 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); |
| 243 } | 237 } |
| 244 | 238 |
| 245 } // namespace net | 239 } // namespace net |
| OLD | NEW |