Chromium Code Reviews| 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/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 EXPECT_TRUE(base_file_->in_progress()); | 458 EXPECT_TRUE(base_file_->in_progress()); |
| 459 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 459 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); |
| 460 EXPECT_FALSE(file_util::PathExists(initial_path)); | 460 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 461 EXPECT_TRUE(file_util::PathExists(new_path)); | 461 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 462 | 462 |
| 463 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 463 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 464 | 464 |
| 465 base_file_->Finish(); | 465 base_file_->Finish(); |
| 466 } | 466 } |
| 467 | 467 |
| 468 // Test that a failed rename reports the correct error. | |
| 469 TEST_F(BaseFileTest, RenameWithError) { | |
| 470 ASSERT_EQ(net::OK, base_file_->Initialize()); | |
| 471 | |
| 472 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | |
| 473 // that the rename will fail. | |
| 474 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | |
| 475 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | |
| 476 | |
| 477 FilePath new_path(test_dir.AppendASCII("TestFile")); | |
| 478 EXPECT_FALSE(file_util::PathExists(new_path)); | |
| 479 | |
| 480 { | |
| 481 file_util::PermissionRestorer restore_permissions_for(test_dir); | |
| 482 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | |
| 483 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path)); | |
|
Randy Smith (Not in Mondays)
2012/07/11 17:13:17
I'm concerned about this test, for the reasons you
| |
| 484 } | |
| 485 | |
| 486 base_file_->Finish(); | |
| 487 } | |
| 488 | |
| 468 // Write data to the file multiple times. | 489 // Write data to the file multiple times. |
| 469 TEST_F(BaseFileTest, MultipleWritesWithError) { | 490 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 470 ASSERT_TRUE(OpenMockFileStream()); | 491 ASSERT_TRUE(OpenMockFileStream()); |
| 471 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), | 492 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), |
| 472 GURL(), | 493 GURL(), |
| 473 GURL(), | 494 GURL(), |
| 474 0, | 495 0, |
| 475 false, | 496 false, |
| 476 "", | 497 "", |
| 477 mock_file_stream_, | 498 mock_file_stream_, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 base_file_->Finish(); | 554 base_file_->Finish(); |
| 534 base_file_->Detach(); | 555 base_file_->Detach(); |
| 535 expect_file_survives_ = true; | 556 expect_file_survives_ = true; |
| 536 } | 557 } |
| 537 | 558 |
| 538 // Create a read-only file and attempt to write to it. | 559 // Create a read-only file and attempt to write to it. |
| 539 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 560 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 540 // Create a new file. | 561 // Create a new file. |
| 541 FilePath readonly_file_name = CreateTestFile(); | 562 FilePath readonly_file_name = CreateTestFile(); |
| 542 | 563 |
| 564 // Restore permissions to the file when we are done with this test. | |
| 565 file_util::PermissionRestorer restore_permissions(readonly_file_name); | |
| 566 | |
| 543 // Make it read-only. | 567 // Make it read-only. |
| 544 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 568 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
| 545 | 569 |
| 546 // Try to overwrite it. | 570 // Try to overwrite it. |
| 547 base_file_.reset(new BaseFile(readonly_file_name, | 571 base_file_.reset(new BaseFile(readonly_file_name, |
| 548 GURL(), | 572 GURL(), |
| 549 GURL(), | 573 GURL(), |
| 550 0, | 574 0, |
| 551 false, | 575 false, |
| 552 "", | 576 "", |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 610 base_file_->Finish(); | 634 base_file_->Finish(); |
| 611 } | 635 } |
| 612 | 636 |
| 613 // Test that calculating speed after no delay - should not divide by 0. | 637 // Test that calculating speed after no delay - should not divide by 0. |
| 614 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { | 638 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { |
| 615 ASSERT_EQ(net::OK, base_file_->Initialize()); | 639 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 616 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 640 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 617 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); | 641 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); |
| 618 base_file_->Finish(); | 642 base_file_->Finish(); |
| 619 } | 643 } |
| OLD | NEW |