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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop_proxy.h" |
9 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
11 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 class UploadFileElementReaderTest : public PlatformTest { | 18 class UploadFileElementReaderTest : public PlatformTest { |
18 protected: | 19 protected: |
19 virtual void SetUp() OVERRIDE { | 20 virtual void SetUp() OVERRIDE { |
20 // Some tests (*.ReadPartially) rely on bytes_.size() being even. | 21 // Some tests (*.ReadPartially) rely on bytes_.size() being even. |
21 const char kData[] = "123456789abcdefghi"; | 22 const char kData[] = "123456789abcdefghi"; |
22 bytes_.assign(kData, kData + arraysize(kData) - 1); | 23 bytes_.assign(kData, kData + arraysize(kData) - 1); |
23 | 24 |
24 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
25 | 26 |
26 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 27 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
27 &temp_file_path_)); | 28 &temp_file_path_)); |
28 ASSERT_EQ( | 29 ASSERT_EQ( |
29 static_cast<int>(bytes_.size()), | 30 static_cast<int>(bytes_.size()), |
30 file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); | 31 file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); |
31 | 32 |
32 reader_.reset(new UploadFileElementReader( | 33 reader_.reset(new UploadFileElementReader( |
| 34 base::MessageLoopProxy::current(), |
33 temp_file_path_, 0, kuint64max, base::Time())); | 35 temp_file_path_, 0, kuint64max, base::Time())); |
34 TestCompletionCallback callback; | 36 TestCompletionCallback callback; |
35 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); | 37 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); |
36 EXPECT_EQ(OK, callback.WaitForResult()); | 38 EXPECT_EQ(OK, callback.WaitForResult()); |
37 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 39 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
38 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 40 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
39 EXPECT_FALSE(reader_->IsInMemory()); | 41 EXPECT_FALSE(reader_->IsInMemory()); |
40 } | 42 } |
41 | 43 |
42 std::vector<char> bytes_; | 44 std::vector<char> bytes_; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 155 |
154 // Make sure callbacks are not called for cancelled operations. | 156 // Make sure callbacks are not called for cancelled operations. |
155 EXPECT_FALSE(read_callback1.have_result()); | 157 EXPECT_FALSE(read_callback1.have_result()); |
156 EXPECT_FALSE(init_callback1.have_result()); | 158 EXPECT_FALSE(init_callback1.have_result()); |
157 } | 159 } |
158 | 160 |
159 TEST_F(UploadFileElementReaderTest, Range) { | 161 TEST_F(UploadFileElementReaderTest, Range) { |
160 const uint64 kOffset = 2; | 162 const uint64 kOffset = 2; |
161 const uint64 kLength = bytes_.size() - kOffset * 3; | 163 const uint64 kLength = bytes_.size() - kOffset * 3; |
162 reader_.reset(new UploadFileElementReader( | 164 reader_.reset(new UploadFileElementReader( |
| 165 base::MessageLoopProxy::current(), |
163 temp_file_path_, kOffset, kLength, base::Time())); | 166 temp_file_path_, kOffset, kLength, base::Time())); |
164 TestCompletionCallback init_callback; | 167 TestCompletionCallback init_callback; |
165 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 168 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
166 EXPECT_EQ(OK, init_callback.WaitForResult()); | 169 EXPECT_EQ(OK, init_callback.WaitForResult()); |
167 EXPECT_EQ(kLength, reader_->GetContentLength()); | 170 EXPECT_EQ(kLength, reader_->GetContentLength()); |
168 EXPECT_EQ(kLength, reader_->BytesRemaining()); | 171 EXPECT_EQ(kLength, reader_->BytesRemaining()); |
169 std::vector<char> buf(kLength); | 172 std::vector<char> buf(kLength); |
170 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); | 173 scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]); |
171 TestCompletionCallback read_callback; | 174 TestCompletionCallback read_callback; |
172 ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, kLength, | 175 ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, kLength, |
173 read_callback.callback())); | 176 read_callback.callback())); |
174 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); | 177 EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult()); |
175 const std::vector<char> expected(bytes_.begin() + kOffset, | 178 const std::vector<char> expected(bytes_.begin() + kOffset, |
176 bytes_.begin() + kOffset + kLength); | 179 bytes_.begin() + kOffset + kLength); |
177 EXPECT_EQ(expected, buf); | 180 EXPECT_EQ(expected, buf); |
178 } | 181 } |
179 | 182 |
180 TEST_F(UploadFileElementReaderTest, FileChanged) { | 183 TEST_F(UploadFileElementReaderTest, FileChanged) { |
181 base::PlatformFileInfo info; | 184 base::PlatformFileInfo info; |
182 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path_, &info)); | 185 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path_, &info)); |
183 | 186 |
184 // Expect one second before the actual modification time to simulate change. | 187 // Expect one second before the actual modification time to simulate change. |
185 const base::Time expected_modification_time = | 188 const base::Time expected_modification_time = |
186 info.last_modified - base::TimeDelta::FromSeconds(1); | 189 info.last_modified - base::TimeDelta::FromSeconds(1); |
187 reader_.reset(new UploadFileElementReader( | 190 reader_.reset(new UploadFileElementReader( |
| 191 base::MessageLoopProxy::current(), |
188 temp_file_path_, 0, kuint64max, expected_modification_time)); | 192 temp_file_path_, 0, kuint64max, expected_modification_time)); |
189 TestCompletionCallback init_callback; | 193 TestCompletionCallback init_callback; |
190 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 194 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
191 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); | 195 EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult()); |
192 } | 196 } |
193 | 197 |
194 TEST_F(UploadFileElementReaderTest, WrongPath) { | 198 TEST_F(UploadFileElementReaderTest, WrongPath) { |
195 const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 199 const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
196 reader_.reset(new UploadFileElementReader( | 200 reader_.reset(new UploadFileElementReader( |
| 201 base::MessageLoopProxy::current(), |
197 wrong_path, 0, kuint64max, base::Time())); | 202 wrong_path, 0, kuint64max, base::Time())); |
198 TestCompletionCallback init_callback; | 203 TestCompletionCallback init_callback; |
199 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); | 204 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback())); |
200 EXPECT_EQ(OK, init_callback.WaitForResult()); | 205 EXPECT_EQ(OK, init_callback.WaitForResult()); |
201 EXPECT_EQ(0U, reader_->GetContentLength()); | 206 EXPECT_EQ(0U, reader_->GetContentLength()); |
202 EXPECT_EQ(0U, reader_->BytesRemaining()); | 207 EXPECT_EQ(0U, reader_->BytesRemaining()); |
203 } | 208 } |
204 | 209 |
205 | 210 |
206 class UploadFileElementReaderSyncTest : public PlatformTest { | 211 class UploadFileElementReaderSyncTest : public PlatformTest { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 TEST_F(UploadFileElementReaderSyncTest, WrongPath) { | 329 TEST_F(UploadFileElementReaderSyncTest, WrongPath) { |
325 const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 330 const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
326 reader_.reset(new UploadFileElementReaderSync( | 331 reader_.reset(new UploadFileElementReaderSync( |
327 wrong_path, 0, kuint64max, base::Time())); | 332 wrong_path, 0, kuint64max, base::Time())); |
328 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); | 333 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); |
329 EXPECT_EQ(0U, reader_->GetContentLength()); | 334 EXPECT_EQ(0U, reader_->GetContentLength()); |
330 EXPECT_EQ(0U, reader_->BytesRemaining()); | 335 EXPECT_EQ(0U, reader_->BytesRemaining()); |
331 } | 336 } |
332 | 337 |
333 } // namespace net | 338 } // namespace net |
OLD | NEW |