| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 BaseFileTest() | 44 BaseFileTest() |
| 45 : expect_file_survives_(false), | 45 : expect_file_survives_(false), |
| 46 expect_in_progress_(true), | 46 expect_in_progress_(true), |
| 47 expected_error_(false), | 47 expected_error_(false), |
| 48 file_thread_(BrowserThread::FILE, &message_loop_) { | 48 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 49 } | 49 } |
| 50 | 50 |
| 51 virtual void SetUp() { | 51 virtual void SetUp() { |
| 52 ResetHash(); | 52 ResetHash(); |
| 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 54 base_file_.reset(new BaseFile(FilePath(), | 54 base_file_.reset(new BaseFile(temp_dir_.path(), |
| 55 FilePath(), |
| 55 GURL(), | 56 GURL(), |
| 56 GURL(), | 57 GURL(), |
| 57 0, | 58 0, |
| 58 false, | 59 false, |
| 59 "", | 60 "", |
| 60 file_stream_, | 61 file_stream_, |
| 61 net::BoundNetLog())); | 62 net::BoundNetLog())); |
| 62 } | 63 } |
| 63 | 64 |
| 64 virtual void TearDown() { | 65 virtual void TearDown() { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 95 | 96 |
| 96 std::string GetFinalHash() { | 97 std::string GetFinalHash() { |
| 97 std::string hash; | 98 std::string hash; |
| 98 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 99 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
| 99 hash.assign(reinterpret_cast<const char*>(sha256_hash_), | 100 hash.assign(reinterpret_cast<const char*>(sha256_hash_), |
| 100 sizeof(sha256_hash_)); | 101 sizeof(sha256_hash_)); |
| 101 return hash; | 102 return hash; |
| 102 } | 103 } |
| 103 | 104 |
| 104 void MakeFileWithHash() { | 105 void MakeFileWithHash() { |
| 105 base_file_.reset(new BaseFile(FilePath(), | 106 base_file_.reset(new BaseFile(temp_dir_.path(), |
| 107 FilePath(), |
| 106 GURL(), | 108 GURL(), |
| 107 GURL(), | 109 GURL(), |
| 108 0, | 110 0, |
| 109 true, | 111 true, |
| 110 "", | 112 "", |
| 111 file_stream_, | 113 file_stream_, |
| 112 net::BoundNetLog())); | 114 net::BoundNetLog())); |
| 113 } | 115 } |
| 114 | 116 |
| 115 bool OpenMockFileStream() { | 117 bool OpenMockFileStream() { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 base_file_->bytes_so_far()); | 152 base_file_->bytes_so_far()); |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 return appended; | 155 return appended; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void set_expected_data(const std::string& data) { expected_data_ = data; } | 158 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 157 | 159 |
| 158 // Helper functions. | 160 // Helper functions. |
| 159 // Create a file. Returns the complete file path. | 161 // Create a file. Returns the complete file path. |
| 160 static FilePath CreateTestFile() { | 162 FilePath CreateTestFile() { |
| 161 FilePath file_name; | 163 FilePath file_name; |
| 162 linked_ptr<net::FileStream> dummy_file_stream; | 164 linked_ptr<net::FileStream> dummy_file_stream; |
| 163 BaseFile file(FilePath(), | 165 BaseFile file(temp_dir_.path(), |
| 166 FilePath(), |
| 164 GURL(), | 167 GURL(), |
| 165 GURL(), | 168 GURL(), |
| 166 0, | 169 0, |
| 167 false, | 170 false, |
| 168 "", | 171 "", |
| 169 dummy_file_stream, | 172 dummy_file_stream, |
| 170 net::BoundNetLog()); | 173 net::BoundNetLog()); |
| 171 | 174 |
| 172 EXPECT_EQ(net::OK, file.Initialize()); | 175 EXPECT_EQ(net::OK, file.Initialize()); |
| 173 file_name = file.full_path(); | 176 file_name = file.full_path(); |
| 174 EXPECT_NE(FilePath::StringType(), file_name.value()); | 177 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 175 | 178 |
| 176 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); | 179 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 177 | 180 |
| 178 // Keep the file from getting deleted when existing_file_name is deleted. | 181 // Keep the file from getting deleted when existing_file_name is deleted. |
| 179 file.Detach(); | 182 file.Detach(); |
| 180 | 183 |
| 181 return file_name; | 184 return file_name; |
| 182 } | 185 } |
| 183 | 186 |
| 184 // Create a file with the specified file name. | 187 // Create a file with the specified file name. |
| 185 static void CreateFileWithName(const FilePath& file_name) { | 188 void CreateFileWithName(const FilePath& file_name) { |
| 186 EXPECT_NE(FilePath::StringType(), file_name.value()); | 189 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 187 linked_ptr<net::FileStream> dummy_file_stream; | 190 linked_ptr<net::FileStream> dummy_file_stream; |
| 188 BaseFile duplicate_file(file_name, | 191 BaseFile duplicate_file(temp_dir_.path(), |
| 192 file_name, |
| 189 GURL(), | 193 GURL(), |
| 190 GURL(), | 194 GURL(), |
| 191 0, | 195 0, |
| 192 false, | 196 false, |
| 193 "", | 197 "", |
| 194 dummy_file_stream, | 198 dummy_file_stream, |
| 195 net::BoundNetLog()); | 199 net::BoundNetLog()); |
| 196 EXPECT_EQ(net::OK, duplicate_file.Initialize()); | 200 EXPECT_EQ(net::OK, duplicate_file.Initialize()); |
| 197 // Write something into it. | 201 // Write something into it. |
| 198 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 202 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 403 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 400 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 404 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 401 // Get the hash state and file name. | 405 // Get the hash state and file name. |
| 402 std::string hash_state; | 406 std::string hash_state; |
| 403 hash_state = base_file_->GetHashState(); | 407 hash_state = base_file_->GetHashState(); |
| 404 // Finish the file. | 408 // Finish the file. |
| 405 base_file_->Finish(); | 409 base_file_->Finish(); |
| 406 | 410 |
| 407 // Create another file | 411 // Create another file |
| 408 linked_ptr<net::FileStream> second_stream; | 412 linked_ptr<net::FileStream> second_stream; |
| 409 BaseFile second_file(FilePath(), | 413 BaseFile second_file(temp_dir_.path(), |
| 414 FilePath(), |
| 410 GURL(), | 415 GURL(), |
| 411 GURL(), | 416 GURL(), |
| 412 base_file_->bytes_so_far(), | 417 base_file_->bytes_so_far(), |
| 413 true, | 418 true, |
| 414 hash_state, | 419 hash_state, |
| 415 second_stream, | 420 second_stream, |
| 416 net::BoundNetLog()); | 421 net::BoundNetLog()); |
| 417 ASSERT_EQ(net::OK, second_file.Initialize()); | 422 ASSERT_EQ(net::OK, second_file.Initialize()); |
| 418 std::string data(kTestData3); | 423 std::string data(kTestData3); |
| 419 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); | 424 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | 487 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
| 483 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path)); | 488 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path)); |
| 484 } | 489 } |
| 485 | 490 |
| 486 base_file_->Finish(); | 491 base_file_->Finish(); |
| 487 } | 492 } |
| 488 | 493 |
| 489 // Write data to the file multiple times. | 494 // Write data to the file multiple times. |
| 490 TEST_F(BaseFileTest, MultipleWritesWithError) { | 495 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 491 ASSERT_TRUE(OpenMockFileStream()); | 496 ASSERT_TRUE(OpenMockFileStream()); |
| 492 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), | 497 base_file_.reset(new BaseFile(temp_dir_.path(), |
| 498 mock_file_stream_->get_path(), |
| 493 GURL(), | 499 GURL(), |
| 494 GURL(), | 500 GURL(), |
| 495 0, | 501 0, |
| 496 false, | 502 false, |
| 497 "", | 503 "", |
| 498 mock_file_stream_, | 504 mock_file_stream_, |
| 499 net::BoundNetLog())); | 505 net::BoundNetLog())); |
| 500 EXPECT_EQ(net::OK, base_file_->Initialize()); | 506 EXPECT_EQ(net::OK, base_file_->Initialize()); |
| 501 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 507 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 502 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 508 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 527 } | 533 } |
| 528 | 534 |
| 529 // Create a file and append to it. | 535 // Create a file and append to it. |
| 530 TEST_F(BaseFileTest, AppendToBaseFile) { | 536 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 531 // Create a new file. | 537 // Create a new file. |
| 532 FilePath existing_file_name = CreateTestFile(); | 538 FilePath existing_file_name = CreateTestFile(); |
| 533 | 539 |
| 534 set_expected_data(kTestData4); | 540 set_expected_data(kTestData4); |
| 535 | 541 |
| 536 // Use the file we've just created. | 542 // Use the file we've just created. |
| 537 base_file_.reset(new BaseFile(existing_file_name, | 543 base_file_.reset(new BaseFile(temp_dir_.path(), |
| 544 existing_file_name, |
| 538 GURL(), | 545 GURL(), |
| 539 GURL(), | 546 GURL(), |
| 540 kTestDataLength4, | 547 kTestDataLength4, |
| 541 false, | 548 false, |
| 542 "", | 549 "", |
| 543 file_stream_, | 550 file_stream_, |
| 544 net::BoundNetLog())); | 551 net::BoundNetLog())); |
| 545 | 552 |
| 546 EXPECT_EQ(net::OK, base_file_->Initialize()); | 553 EXPECT_EQ(net::OK, base_file_->Initialize()); |
| 547 | 554 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 561 // Create a new file. | 568 // Create a new file. |
| 562 FilePath readonly_file_name = CreateTestFile(); | 569 FilePath readonly_file_name = CreateTestFile(); |
| 563 | 570 |
| 564 // Restore permissions to the file when we are done with this test. | 571 // Restore permissions to the file when we are done with this test. |
| 565 file_util::PermissionRestorer restore_permissions(readonly_file_name); | 572 file_util::PermissionRestorer restore_permissions(readonly_file_name); |
| 566 | 573 |
| 567 // Make it read-only. | 574 // Make it read-only. |
| 568 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 575 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
| 569 | 576 |
| 570 // Try to overwrite it. | 577 // Try to overwrite it. |
| 571 base_file_.reset(new BaseFile(readonly_file_name, | 578 base_file_.reset(new BaseFile(temp_dir_.path(), |
| 579 readonly_file_name, |
| 572 GURL(), | 580 GURL(), |
| 573 GURL(), | 581 GURL(), |
| 574 0, | 582 0, |
| 575 false, | 583 false, |
| 576 "", | 584 "", |
| 577 file_stream_, | 585 file_stream_, |
| 578 net::BoundNetLog())); | 586 net::BoundNetLog())); |
| 579 | 587 |
| 580 expect_in_progress_ = false; | 588 expect_in_progress_ = false; |
| 581 | 589 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 base_file_->Finish(); | 642 base_file_->Finish(); |
| 635 } | 643 } |
| 636 | 644 |
| 637 // Test that calculating speed after no delay - should not divide by 0. | 645 // Test that calculating speed after no delay - should not divide by 0. |
| 638 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { | 646 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { |
| 639 ASSERT_EQ(net::OK, base_file_->Initialize()); | 647 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 640 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 648 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 641 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); | 649 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); |
| 642 base_file_->Finish(); | 650 base_file_->Finish(); |
| 643 } | 651 } |
| 652 |
| 653 // Test that a temporary file is created in the default download directory. |
| 654 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { |
| 655 ASSERT_TRUE(base_file_->full_path().empty()); |
| 656 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 657 EXPECT_FALSE(base_file_->full_path().empty()); |
| 658 // The default download directory was set to be temp_dir_.path() in SetUp(). |
| 659 EXPECT_EQ(temp_dir_.path(), base_file_->full_path().DirName()); |
| 660 base_file_->Finish(); |
| 661 } |
| OLD | NEW |