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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 { | 469 { |
470 file_util::PermissionRestorer restore_permissions_for(test_dir); | 470 file_util::PermissionRestorer restore_permissions_for(test_dir); |
471 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | 471 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
472 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 472 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
473 base_file_->Rename(new_path)); | 473 base_file_->Rename(new_path)); |
474 } | 474 } |
475 | 475 |
476 base_file_->Finish(); | 476 base_file_->Finish(); |
477 } | 477 } |
478 | 478 |
| 479 // Test that if a rename fails for an in-progress BaseFile, it remains writeable |
| 480 // and renameable. |
| 481 TEST_F(BaseFileTest, RenameWithErrorInProgress) { |
| 482 ASSERT_TRUE(InitializeFile()); |
| 483 |
| 484 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
| 485 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 486 |
| 487 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 488 EXPECT_FALSE(base::PathExists(new_path)); |
| 489 |
| 490 // Write some data to start with. |
| 491 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 492 ASSERT_TRUE(base_file_->in_progress()); |
| 493 |
| 494 base::FilePath old_path = base_file_->full_path(); |
| 495 |
| 496 { |
| 497 file_util::PermissionRestorer restore_permissions_for(test_dir); |
| 498 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
| 499 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 500 base_file_->Rename(new_path)); |
| 501 |
| 502 // The file should still be open and we should be able to continue writing |
| 503 // to it. |
| 504 ASSERT_TRUE(base_file_->in_progress()); |
| 505 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 506 ASSERT_EQ(old_path.value(), base_file_->full_path().value()); |
| 507 |
| 508 // Try to rename again, just for kicks. It should still fail with |
| 509 // ACCESS_DENIED. |
| 510 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 511 base_file_->Rename(new_path)); |
| 512 } |
| 513 |
| 514 // Now that TestDir is writeable again, we should be able to successfully |
| 515 // rename the file. |
| 516 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 517 ASSERT_EQ(new_path.value(), base_file_->full_path().value()); |
| 518 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 519 |
| 520 base_file_->Finish(); |
| 521 |
| 522 // The contents of the file should be intact. |
| 523 std::string file_contents; |
| 524 std::string expected_contents(kTestData1); |
| 525 expected_contents += kTestData2; |
| 526 expected_contents += kTestData3; |
| 527 ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents)); |
| 528 EXPECT_EQ(expected_contents, file_contents); |
| 529 } |
| 530 |
479 // Test that a failed write reports an error. | 531 // Test that a failed write reports an error. |
480 TEST_F(BaseFileTest, WriteWithError) { | 532 TEST_F(BaseFileTest, WriteWithError) { |
481 base::FilePath path; | 533 base::FilePath path; |
482 ASSERT_TRUE(base::CreateTemporaryFile(&path)); | 534 ASSERT_TRUE(base::CreateTemporaryFile(&path)); |
483 | 535 |
484 // Pass a file handle which was opened without the WRITE flag. | 536 // Pass a file handle which was opened without the WRITE flag. |
485 // This should result in an error when writing. | 537 // This should result in an error when writing. |
486 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 538 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
487 base_file_.reset(new BaseFile(path, | 539 base_file_.reset(new BaseFile(path, |
488 GURL(), | 540 GURL(), |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 | 682 |
631 const char kData[] = "hello"; | 683 const char kData[] = "hello"; |
632 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 684 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
633 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 685 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
634 // The file that we created here should stick around when the BaseFile is | 686 // The file that we created here should stick around when the BaseFile is |
635 // destroyed during TearDown. | 687 // destroyed during TearDown. |
636 expect_file_survives_ = true; | 688 expect_file_survives_ = true; |
637 } | 689 } |
638 | 690 |
639 } // namespace content | 691 } // namespace content |
OLD | NEW |