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 "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 188 |
189 base::TimeTicks StartTick() { | 189 base::TimeTicks StartTick() { |
190 EXPECT_TRUE(base_file_.get()); | 190 EXPECT_TRUE(base_file_.get()); |
191 return base_file_->start_tick_; | 191 return base_file_->start_tick_; |
192 } | 192 } |
193 | 193 |
194 void set_expected_error(DownloadInterruptReason err) { | 194 void set_expected_error(DownloadInterruptReason err) { |
195 expected_error_ = err; | 195 expected_error_ = err; |
196 } | 196 } |
197 | 197 |
| 198 void ExpectPermissionError(DownloadInterruptReason err) { |
| 199 EXPECT_TRUE(err == DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR || |
| 200 err == DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED) |
| 201 << "Interrupt reason = " << err; |
| 202 } |
| 203 |
198 protected: | 204 protected: |
199 // BaseClass instance we are testing. | 205 // BaseClass instance we are testing. |
200 scoped_ptr<BaseFile> base_file_; | 206 scoped_ptr<BaseFile> base_file_; |
201 | 207 |
202 // Temporary directory for renamed downloads. | 208 // Temporary directory for renamed downloads. |
203 base::ScopedTempDir temp_dir_; | 209 base::ScopedTempDir temp_dir_; |
204 | 210 |
205 // Expect the file to survive deletion of the BaseFile instance. | 211 // Expect the file to survive deletion of the BaseFile instance. |
206 bool expect_file_survives_; | 212 bool expect_file_survives_; |
207 | 213 |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 // that the rename will fail. | 468 // that the rename will fail. |
463 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 469 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
464 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 470 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
465 | 471 |
466 base::FilePath new_path(test_dir.AppendASCII("TestFile")); | 472 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
467 EXPECT_FALSE(base::PathExists(new_path)); | 473 EXPECT_FALSE(base::PathExists(new_path)); |
468 | 474 |
469 { | 475 { |
470 base::FilePermissionRestorer restore_permissions_for(test_dir); | 476 base::FilePermissionRestorer restore_permissions_for(test_dir); |
471 ASSERT_TRUE(base::MakeFileUnwritable(test_dir)); | 477 ASSERT_TRUE(base::MakeFileUnwritable(test_dir)); |
472 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 478 ExpectPermissionError(base_file_->Rename(new_path)); |
473 base_file_->Rename(new_path)); | |
474 } | 479 } |
475 | 480 |
476 base_file_->Finish(); | 481 base_file_->Finish(); |
477 } | 482 } |
478 | 483 |
| 484 // Test that if a rename fails for an in-progress BaseFile, it remains writeable |
| 485 // and renameable. |
| 486 TEST_F(BaseFileTest, RenameWithErrorInProgress) { |
| 487 ASSERT_TRUE(InitializeFile()); |
| 488 |
| 489 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
| 490 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 491 |
| 492 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 493 EXPECT_FALSE(base::PathExists(new_path)); |
| 494 |
| 495 // Write some data to start with. |
| 496 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 497 ASSERT_TRUE(base_file_->in_progress()); |
| 498 |
| 499 base::FilePath old_path = base_file_->full_path(); |
| 500 |
| 501 { |
| 502 base::FilePermissionRestorer restore_permissions_for(test_dir); |
| 503 ASSERT_TRUE(base::MakeFileUnwritable(test_dir)); |
| 504 ExpectPermissionError(base_file_->Rename(new_path)); |
| 505 |
| 506 // The file should still be open and we should be able to continue writing |
| 507 // to it. |
| 508 ASSERT_TRUE(base_file_->in_progress()); |
| 509 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 510 ASSERT_EQ(old_path.value(), base_file_->full_path().value()); |
| 511 |
| 512 // Try to rename again, just for kicks. It should still fail. |
| 513 ExpectPermissionError(base_file_->Rename(new_path)); |
| 514 } |
| 515 |
| 516 // Now that TestDir is writeable again, we should be able to successfully |
| 517 // rename the file. |
| 518 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 519 ASSERT_EQ(new_path.value(), base_file_->full_path().value()); |
| 520 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 521 |
| 522 base_file_->Finish(); |
| 523 |
| 524 // The contents of the file should be intact. |
| 525 std::string file_contents; |
| 526 std::string expected_contents(kTestData1); |
| 527 expected_contents += kTestData2; |
| 528 expected_contents += kTestData3; |
| 529 ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents)); |
| 530 EXPECT_EQ(expected_contents, file_contents); |
| 531 } |
| 532 |
479 // Test that a failed write reports an error. | 533 // Test that a failed write reports an error. |
480 TEST_F(BaseFileTest, WriteWithError) { | 534 TEST_F(BaseFileTest, WriteWithError) { |
481 base::FilePath path; | 535 base::FilePath path; |
482 ASSERT_TRUE(base::CreateTemporaryFile(&path)); | 536 ASSERT_TRUE(base::CreateTemporaryFile(&path)); |
483 | 537 |
484 // Pass a file handle which was opened without the WRITE flag. | 538 // Pass a file handle which was opened without the WRITE flag. |
485 // This should result in an error when writing. | 539 // This should result in an error when writing. |
486 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 540 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
487 base_file_.reset(new BaseFile(path, | 541 base_file_.reset(new BaseFile(path, |
488 GURL(), | 542 GURL(), |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 | 684 |
631 const char kData[] = "hello"; | 685 const char kData[] = "hello"; |
632 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 686 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
633 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 687 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
634 // The file that we created here should stick around when the BaseFile is | 688 // The file that we created here should stick around when the BaseFile is |
635 // destroyed during TearDown. | 689 // destroyed during TearDown. |
636 expect_file_survives_ = true; | 690 expect_file_survives_ = true; |
637 } | 691 } |
638 | 692 |
639 } // namespace content | 693 } // namespace content |
OLD | NEW |