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_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 ASSERT_TRUE(stream.IsEOF()); | 170 ASSERT_TRUE(stream.IsEOF()); |
171 } | 171 } |
172 | 172 |
173 TEST_F(UploadDataStreamTest, File) { | 173 TEST_F(UploadDataStreamTest, File) { |
174 FilePath temp_file_path; | 174 FilePath temp_file_path; |
175 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 175 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
176 &temp_file_path)); | 176 &temp_file_path)); |
177 ASSERT_EQ(static_cast<int>(kTestDataSize), | 177 ASSERT_EQ(static_cast<int>(kTestDataSize), |
178 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 178 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
179 | 179 |
180 std::vector<UploadElement> elements; | 180 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); |
181 UploadElement element; | |
182 element.SetToFilePath(temp_file_path); | |
183 elements.push_back(element); | |
184 upload_data_->SetElements(elements); | |
185 | 181 |
186 UploadDataStream stream(upload_data_); | 182 UploadDataStream stream(upload_data_); |
187 ASSERT_EQ(OK, stream.InitSync()); | 183 ASSERT_EQ(OK, stream.InitSync()); |
188 EXPECT_FALSE(stream.IsInMemory()); | 184 EXPECT_FALSE(stream.IsInMemory()); |
189 EXPECT_EQ(kTestDataSize, stream.size()); | 185 EXPECT_EQ(kTestDataSize, stream.size()); |
190 EXPECT_EQ(0U, stream.position()); | 186 EXPECT_EQ(0U, stream.position()); |
191 EXPECT_FALSE(stream.IsEOF()); | 187 EXPECT_FALSE(stream.IsEOF()); |
192 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 188 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
193 while (!stream.IsEOF()) { | 189 while (!stream.IsEOF()) { |
194 int bytes_read = stream.ReadSync(buf, kTestBufferSize); | 190 int bytes_read = stream.ReadSync(buf, kTestBufferSize); |
195 ASSERT_LE(0, bytes_read); // Not an error. | 191 ASSERT_LE(0, bytes_read); // Not an error. |
196 } | 192 } |
197 EXPECT_EQ(kTestDataSize, stream.position()); | 193 EXPECT_EQ(kTestDataSize, stream.position()); |
198 ASSERT_TRUE(stream.IsEOF()); | 194 ASSERT_TRUE(stream.IsEOF()); |
199 } | 195 } |
200 | 196 |
201 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { | 197 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { |
202 FilePath temp_file_path; | 198 FilePath temp_file_path; |
203 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 199 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
204 &temp_file_path)); | 200 &temp_file_path)); |
205 ASSERT_EQ(static_cast<int>(kTestDataSize), | 201 ASSERT_EQ(static_cast<int>(kTestDataSize), |
206 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 202 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
207 const uint64 kFakeSize = kTestDataSize*2; | 203 const uint64 kFakeSize = kTestDataSize*2; |
208 | 204 |
209 UploadFileElementReader::ScopedOverridingContentLengthForTests | 205 UploadFileElementReader::ScopedOverridingContentLengthForTests |
210 overriding_content_length(kFakeSize); | 206 overriding_content_length(kFakeSize); |
211 | 207 |
212 std::vector<UploadElement> elements; | 208 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); |
213 UploadElement element; | |
214 element.SetToFilePath(temp_file_path); | |
215 elements.push_back(element); | |
216 upload_data_->SetElements(elements); | |
217 | 209 |
218 UploadDataStream stream(upload_data_); | 210 UploadDataStream stream(upload_data_); |
219 ASSERT_EQ(OK, stream.InitSync()); | 211 ASSERT_EQ(OK, stream.InitSync()); |
220 EXPECT_FALSE(stream.IsInMemory()); | 212 EXPECT_FALSE(stream.IsInMemory()); |
221 EXPECT_EQ(kFakeSize, stream.size()); | 213 EXPECT_EQ(kFakeSize, stream.size()); |
222 EXPECT_EQ(0U, stream.position()); | 214 EXPECT_EQ(0U, stream.position()); |
223 EXPECT_FALSE(stream.IsEOF()); | 215 EXPECT_FALSE(stream.IsEOF()); |
224 uint64 read_counter = 0; | 216 uint64 read_counter = 0; |
225 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); | 217 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
226 while (!stream.IsEOF()) { | 218 while (!stream.IsEOF()) { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 // Consume the third and the fourth elements. | 427 // Consume the third and the fourth elements. |
436 EXPECT_CALL(mock_callback, Run(kTestDataSize*2)).Times(1); | 428 EXPECT_CALL(mock_callback, Run(kTestDataSize*2)).Times(1); |
437 EXPECT_EQ(ERR_IO_PENDING, | 429 EXPECT_EQ(ERR_IO_PENDING, |
438 stream.Read(buf, kTestDataSize*2, mock_callback.CreateCallback())); | 430 stream.Read(buf, kTestDataSize*2, mock_callback.CreateCallback())); |
439 MessageLoop::current()->RunAllPending(); | 431 MessageLoop::current()->RunAllPending(); |
440 } | 432 } |
441 | 433 |
442 void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, | 434 void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, |
443 const base::Time& time, | 435 const base::Time& time, |
444 bool error_expected) { | 436 bool error_expected) { |
445 std::vector<UploadElement> elements; | |
446 UploadElement element; | |
447 element.SetToFilePathRange(file_path, 1, 2, time); | |
448 elements.push_back(element); | |
449 // Don't use upload_data_ here, as this function is called twice, and | 437 // Don't use upload_data_ here, as this function is called twice, and |
450 // reusing upload_data_ is wrong. | 438 // reusing upload_data_ is wrong. |
451 scoped_refptr<UploadData> upload_data(new UploadData); | 439 scoped_refptr<UploadData> upload_data(new UploadData); |
452 upload_data->SetElements(elements); | 440 upload_data->AppendFileRange(file_path, 1, 2, time); |
453 | 441 |
454 UploadDataStream stream(upload_data); | 442 UploadDataStream stream(upload_data); |
455 int error_code = stream.InitSync(); | 443 int error_code = stream.InitSync(); |
456 if (error_expected) | 444 if (error_expected) |
457 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code); | 445 ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code); |
458 else | 446 else |
459 ASSERT_EQ(OK, error_code); | 447 ASSERT_EQ(OK, error_code); |
460 } | 448 } |
461 | 449 |
462 TEST_F(UploadDataStreamTest, FileChanged) { | 450 TEST_F(UploadDataStreamTest, FileChanged) { |
(...skipping 16 matching lines...) Expand all Loading... |
479 } | 467 } |
480 | 468 |
481 TEST_F(UploadDataStreamTest, UploadDataReused) { | 469 TEST_F(UploadDataStreamTest, UploadDataReused) { |
482 FilePath temp_file_path; | 470 FilePath temp_file_path; |
483 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 471 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
484 &temp_file_path)); | 472 &temp_file_path)); |
485 ASSERT_EQ(static_cast<int>(kTestDataSize), | 473 ASSERT_EQ(static_cast<int>(kTestDataSize), |
486 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); | 474 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
487 | 475 |
488 // Prepare |upload_data_| that contains a file. | 476 // Prepare |upload_data_| that contains a file. |
489 std::vector<UploadElement> elements; | 477 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); |
490 UploadElement element; | |
491 element.SetToFilePath(temp_file_path); | |
492 elements.push_back(element); | |
493 upload_data_->SetElements(elements); | |
494 | 478 |
495 // Confirm that the file is read properly. | 479 // Confirm that the file is read properly. |
496 { | 480 { |
497 UploadDataStream stream(upload_data_); | 481 UploadDataStream stream(upload_data_); |
498 ASSERT_EQ(OK, stream.InitSync()); | 482 ASSERT_EQ(OK, stream.InitSync()); |
499 EXPECT_EQ(kTestDataSize, stream.size()); | 483 EXPECT_EQ(kTestDataSize, stream.size()); |
500 EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); | 484 EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); |
501 } | 485 } |
502 | 486 |
503 // Reuse |upload_data_| for another UploadDataStream, and confirm that the | 487 // Reuse |upload_data_| for another UploadDataStream, and confirm that the |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 read_callback2.callback())); | 700 read_callback2.callback())); |
717 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); | 701 EXPECT_EQ(static_cast<int>(buf2.size()), read_callback2.WaitForResult()); |
718 EXPECT_EQ(expected_data, buf2); | 702 EXPECT_EQ(expected_data, buf2); |
719 EXPECT_TRUE(stream.IsEOF()); | 703 EXPECT_TRUE(stream.IsEOF()); |
720 | 704 |
721 // Make sure callbacks are not called for cancelled operations. | 705 // Make sure callbacks are not called for cancelled operations. |
722 EXPECT_FALSE(read_callback1.have_result()); | 706 EXPECT_FALSE(read_callback1.have_result()); |
723 } | 707 } |
724 | 708 |
725 } // namespace net | 709 } // namespace net |
OLD | NEW |