| 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> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
| 18 #include "net/test/gtest_util.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 20 | 22 |
| 23 using net::test::IsError; |
| 24 using net::test::IsOk; |
| 25 |
| 21 namespace net { | 26 namespace net { |
| 22 | 27 |
| 23 class UploadFileElementReaderTest : public PlatformTest { | 28 class UploadFileElementReaderTest : public PlatformTest { |
| 24 protected: | 29 protected: |
| 25 void SetUp() override { | 30 void SetUp() override { |
| 26 PlatformTest::SetUp(); | 31 PlatformTest::SetUp(); |
| 27 // Some tests (*.ReadPartially) rely on bytes_.size() being even. | 32 // Some tests (*.ReadPartially) rely on bytes_.size() being even. |
| 28 const char kData[] = "123456789abcdefghi"; | 33 const char kData[] = "123456789abcdefghi"; |
| 29 bytes_.assign(kData, kData + arraysize(kData) - 1); | 34 bytes_.assign(kData, kData + arraysize(kData) - 1); |
| 30 | 35 |
| 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 32 | 37 |
| 33 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), | 38 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
| 34 &temp_file_path_)); | 39 &temp_file_path_)); |
| 35 ASSERT_EQ( | 40 ASSERT_EQ( |
| 36 static_cast<int>(bytes_.size()), | 41 static_cast<int>(bytes_.size()), |
| 37 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); | 42 base::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); |
| 38 | 43 |
| 39 reader_.reset(new UploadFileElementReader( | 44 reader_.reset(new UploadFileElementReader( |
| 40 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, | 45 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
| 41 std::numeric_limits<uint64_t>::max(), base::Time())); | 46 std::numeric_limits<uint64_t>::max(), base::Time())); |
| 42 TestCompletionCallback callback; | 47 TestCompletionCallback callback; |
| 43 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); | 48 ASSERT_THAT(reader_->Init(callback.callback()), IsError(ERR_IO_PENDING)); |
| 44 EXPECT_EQ(OK, callback.WaitForResult()); | 49 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 45 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 50 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
| 46 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 51 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
| 47 EXPECT_FALSE(reader_->IsInMemory()); | 52 EXPECT_FALSE(reader_->IsInMemory()); |
| 48 } | 53 } |
| 49 | 54 |
| 50 ~UploadFileElementReaderTest() override { | 55 ~UploadFileElementReaderTest() override { |
| 51 reader_.reset(); | 56 reader_.reset(); |
| 52 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
| 53 } | 58 } |
| 54 | 59 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 TestCompletionCallback read_callback1; | 123 TestCompletionCallback read_callback1; |
| 119 ASSERT_EQ(ERR_IO_PENDING, | 124 ASSERT_EQ(ERR_IO_PENDING, |
| 120 reader_->Read( | 125 reader_->Read( |
| 121 wrapped_buffer.get(), buf.size(), read_callback1.callback())); | 126 wrapped_buffer.get(), buf.size(), read_callback1.callback())); |
| 122 EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult()); | 127 EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult()); |
| 123 EXPECT_EQ(0U, reader_->BytesRemaining()); | 128 EXPECT_EQ(0U, reader_->BytesRemaining()); |
| 124 EXPECT_EQ(bytes_, buf); | 129 EXPECT_EQ(bytes_, buf); |
| 125 | 130 |
| 126 // Call Init() again to reset the state. | 131 // Call Init() again to reset the state. |
| 127 TestCompletionCallback init_callback; | 132 TestCompletionCallback init_callback; |
| 128 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 133 ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING)); |
| 129 EXPECT_EQ(OK, init_callback.WaitForResult()); | 134 EXPECT_THAT(init_callback.WaitForResult(), IsOk()); |
| 130 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 135 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
| 131 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 136 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
| 132 | 137 |
| 133 // Read again. | 138 // Read again. |
| 134 TestCompletionCallback read_callback2; | 139 TestCompletionCallback read_callback2; |
| 135 ASSERT_EQ(ERR_IO_PENDING, | 140 ASSERT_EQ(ERR_IO_PENDING, |
| 136 reader_->Read( | 141 reader_->Read( |
| 137 wrapped_buffer.get(), buf.size(), read_callback2.callback())); | 142 wrapped_buffer.get(), buf.size(), read_callback2.callback())); |
| 138 EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult()); | 143 EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult()); |
| 139 EXPECT_EQ(0U, reader_->BytesRemaining()); | 144 EXPECT_EQ(0U, reader_->BytesRemaining()); |
| 140 EXPECT_EQ(bytes_, buf); | 145 EXPECT_EQ(bytes_, buf); |
| 141 } | 146 } |
| 142 | 147 |
| 143 TEST_F(UploadFileElementReaderTest, InitDuringAsyncOperation) { | 148 TEST_F(UploadFileElementReaderTest, InitDuringAsyncOperation) { |
| 144 std::vector<char> buf(bytes_.size()); | 149 std::vector<char> buf(bytes_.size()); |
| 145 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); | 150 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); |
| 146 | 151 |
| 147 // Start reading all. | 152 // Start reading all. |
| 148 TestCompletionCallback read_callback1; | 153 TestCompletionCallback read_callback1; |
| 149 EXPECT_EQ(ERR_IO_PENDING, | 154 EXPECT_EQ(ERR_IO_PENDING, |
| 150 reader_->Read( | 155 reader_->Read( |
| 151 wrapped_buffer.get(), buf.size(), read_callback1.callback())); | 156 wrapped_buffer.get(), buf.size(), read_callback1.callback())); |
| 152 | 157 |
| 153 // Call Init to cancel the previous read. | 158 // Call Init to cancel the previous read. |
| 154 TestCompletionCallback init_callback1; | 159 TestCompletionCallback init_callback1; |
| 155 EXPECT_EQ(ERR_IO_PENDING, reader_->Init(init_callback1.callback())); | 160 EXPECT_THAT(reader_->Init(init_callback1.callback()), |
| 161 IsError(ERR_IO_PENDING)); |
| 156 | 162 |
| 157 // Call Init again to cancel the previous init. | 163 // Call Init again to cancel the previous init. |
| 158 TestCompletionCallback init_callback2; | 164 TestCompletionCallback init_callback2; |
| 159 EXPECT_EQ(ERR_IO_PENDING, reader_->Init(init_callback2.callback())); | 165 EXPECT_THAT(reader_->Init(init_callback2.callback()), |
| 160 EXPECT_EQ(OK, init_callback2.WaitForResult()); | 166 IsError(ERR_IO_PENDING)); |
| 167 EXPECT_THAT(init_callback2.WaitForResult(), IsOk()); |
| 161 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 168 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
| 162 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 169 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
| 163 | 170 |
| 164 // Read half. | 171 // Read half. |
| 165 std::vector<char> buf2(bytes_.size() / 2); | 172 std::vector<char> buf2(bytes_.size() / 2); |
| 166 scoped_refptr<IOBuffer> wrapped_buffer2 = new WrappedIOBuffer(&buf2[0]); | 173 scoped_refptr<IOBuffer> wrapped_buffer2 = new WrappedIOBuffer(&buf2[0]); |
| 167 TestCompletionCallback read_callback2; | 174 TestCompletionCallback read_callback2; |
| 168 EXPECT_EQ(ERR_IO_PENDING, | 175 EXPECT_EQ(ERR_IO_PENDING, |
| 169 reader_->Read( | 176 reader_->Read( |
| 170 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); | 177 wrapped_buffer2.get(), buf2.size(), read_callback2.callback())); |
| 171 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); | 178 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); |
| 172 EXPECT_EQ(bytes_.size() - buf2.size(), reader_->BytesRemaining()); | 179 EXPECT_EQ(bytes_.size() - buf2.size(), reader_->BytesRemaining()); |
| 173 EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + buf2.size()), | 180 EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + buf2.size()), |
| 174 buf2); | 181 buf2); |
| 175 | 182 |
| 176 // Make sure callbacks are not called for cancelled operations. | 183 // Make sure callbacks are not called for cancelled operations. |
| 177 EXPECT_FALSE(read_callback1.have_result()); | 184 EXPECT_FALSE(read_callback1.have_result()); |
| 178 EXPECT_FALSE(init_callback1.have_result()); | 185 EXPECT_FALSE(init_callback1.have_result()); |
| 179 } | 186 } |
| 180 | 187 |
| 181 TEST_F(UploadFileElementReaderTest, Range) { | 188 TEST_F(UploadFileElementReaderTest, Range) { |
| 182 const uint64_t kOffset = 2; | 189 const uint64_t kOffset = 2; |
| 183 const uint64_t kLength = bytes_.size() - kOffset * 3; | 190 const uint64_t kLength = bytes_.size() - kOffset * 3; |
| 184 reader_.reset(new UploadFileElementReader( | 191 reader_.reset(new UploadFileElementReader( |
| 185 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, kOffset, | 192 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, kOffset, |
| 186 kLength, base::Time())); | 193 kLength, base::Time())); |
| 187 TestCompletionCallback init_callback; | 194 TestCompletionCallback init_callback; |
| 188 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 195 ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING)); |
| 189 EXPECT_EQ(OK, init_callback.WaitForResult()); | 196 EXPECT_THAT(init_callback.WaitForResult(), IsOk()); |
| 190 EXPECT_EQ(kLength, reader_->GetContentLength()); | 197 EXPECT_EQ(kLength, reader_->GetContentLength()); |
| 191 EXPECT_EQ(kLength, reader_->BytesRemaining()); | 198 EXPECT_EQ(kLength, reader_->BytesRemaining()); |
| 192 std::vector<char> buf(kLength); | 199 std::vector<char> buf(kLength); |
| 193 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); | 200 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); |
| 194 TestCompletionCallback read_callback; | 201 TestCompletionCallback read_callback; |
| 195 ASSERT_EQ( | 202 ASSERT_EQ( |
| 196 ERR_IO_PENDING, | 203 ERR_IO_PENDING, |
| 197 reader_->Read(wrapped_buffer.get(), kLength, read_callback.callback())); | 204 reader_->Read(wrapped_buffer.get(), kLength, read_callback.callback())); |
| 198 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); | 205 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); |
| 199 const std::vector<char> expected(bytes_.begin() + kOffset, | 206 const std::vector<char> expected(bytes_.begin() + kOffset, |
| 200 bytes_.begin() + kOffset + kLength); | 207 bytes_.begin() + kOffset + kLength); |
| 201 EXPECT_EQ(expected, buf); | 208 EXPECT_EQ(expected, buf); |
| 202 } | 209 } |
| 203 | 210 |
| 204 TEST_F(UploadFileElementReaderTest, FileChanged) { | 211 TEST_F(UploadFileElementReaderTest, FileChanged) { |
| 205 base::File::Info info; | 212 base::File::Info info; |
| 206 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 213 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
| 207 | 214 |
| 208 // Expect one second before the actual modification time to simulate change. | 215 // Expect one second before the actual modification time to simulate change. |
| 209 const base::Time expected_modification_time = | 216 const base::Time expected_modification_time = |
| 210 info.last_modified - base::TimeDelta::FromSeconds(1); | 217 info.last_modified - base::TimeDelta::FromSeconds(1); |
| 211 reader_.reset(new UploadFileElementReader( | 218 reader_.reset(new UploadFileElementReader( |
| 212 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, | 219 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
| 213 std::numeric_limits<uint64_t>::max(), expected_modification_time)); | 220 std::numeric_limits<uint64_t>::max(), expected_modification_time)); |
| 214 TestCompletionCallback init_callback; | 221 TestCompletionCallback init_callback; |
| 215 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 222 ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING)); |
| 216 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); | 223 EXPECT_THAT(init_callback.WaitForResult(), IsError(ERR_UPLOAD_FILE_CHANGED)); |
| 217 } | 224 } |
| 218 | 225 |
| 219 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { | 226 TEST_F(UploadFileElementReaderTest, InexactExpectedTimeStamp) { |
| 220 base::File::Info info; | 227 base::File::Info info; |
| 221 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); | 228 ASSERT_TRUE(base::GetFileInfo(temp_file_path_, &info)); |
| 222 | 229 |
| 223 const base::Time expected_modification_time = | 230 const base::Time expected_modification_time = |
| 224 info.last_modified - base::TimeDelta::FromMilliseconds(900); | 231 info.last_modified - base::TimeDelta::FromMilliseconds(900); |
| 225 reader_.reset(new UploadFileElementReader( | 232 reader_.reset(new UploadFileElementReader( |
| 226 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, | 233 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path_, 0, |
| 227 std::numeric_limits<uint64_t>::max(), expected_modification_time)); | 234 std::numeric_limits<uint64_t>::max(), expected_modification_time)); |
| 228 TestCompletionCallback init_callback; | 235 TestCompletionCallback init_callback; |
| 229 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 236 ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING)); |
| 230 EXPECT_EQ(OK, init_callback.WaitForResult()); | 237 EXPECT_THAT(init_callback.WaitForResult(), IsOk()); |
| 231 } | 238 } |
| 232 | 239 |
| 233 TEST_F(UploadFileElementReaderTest, WrongPath) { | 240 TEST_F(UploadFileElementReaderTest, WrongPath) { |
| 234 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 241 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
| 235 reader_.reset(new UploadFileElementReader( | 242 reader_.reset(new UploadFileElementReader( |
| 236 base::ThreadTaskRunnerHandle::Get().get(), wrong_path, 0, | 243 base::ThreadTaskRunnerHandle::Get().get(), wrong_path, 0, |
| 237 std::numeric_limits<uint64_t>::max(), base::Time())); | 244 std::numeric_limits<uint64_t>::max(), base::Time())); |
| 238 TestCompletionCallback init_callback; | 245 TestCompletionCallback init_callback; |
| 239 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 246 ASSERT_THAT(reader_->Init(init_callback.callback()), IsError(ERR_IO_PENDING)); |
| 240 EXPECT_EQ(ERR_FILE_NOT_FOUND, init_callback.WaitForResult()); | 247 EXPECT_THAT(init_callback.WaitForResult(), IsError(ERR_FILE_NOT_FOUND)); |
| 241 } | 248 } |
| 242 | 249 |
| 243 } // namespace net | 250 } // namespace net |
| OLD | NEW |