| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 BaseFileTest() | 56 BaseFileTest() |
| 57 : expect_file_survives_(false), | 57 : expect_file_survives_(false), |
| 58 expect_in_progress_(true), | 58 expect_in_progress_(true), |
| 59 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), | 59 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 60 file_thread_(BrowserThread::FILE, &message_loop_) { | 60 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 61 } | 61 } |
| 62 | 62 |
| 63 void SetUp() override { | 63 void SetUp() override { |
| 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 65 base_file_.reset(new BaseFile(net::BoundNetLog())); | 65 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void TearDown() override { | 68 void TearDown() override { |
| 69 EXPECT_FALSE(base_file_->in_progress()); | 69 EXPECT_FALSE(base_file_->in_progress()); |
| 70 if (!expected_error_) { | 70 if (!expected_error_) { |
| 71 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), | 71 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
| 72 base_file_->bytes_so_far()); | 72 base_file_->bytes_so_far()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 base::FilePath full_path = base_file_->full_path(); | 75 base::FilePath full_path = base_file_->full_path(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 } | 113 } |
| 114 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 114 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void set_expected_data(const std::string& data) { expected_data_ = data; } | 117 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 118 | 118 |
| 119 // Helper functions. | 119 // Helper functions. |
| 120 // Create a file. Returns the complete file path. | 120 // Create a file. Returns the complete file path. |
| 121 base::FilePath CreateTestFile() { | 121 base::FilePath CreateTestFile() { |
| 122 base::FilePath file_name; | 122 base::FilePath file_name; |
| 123 BaseFile file((net::BoundNetLog())); | 123 BaseFile file((net::NetLogWithSource())); |
| 124 | 124 |
| 125 EXPECT_EQ( | 125 EXPECT_EQ( |
| 126 DOWNLOAD_INTERRUPT_REASON_NONE, | 126 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 127 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0, | 127 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0, |
| 128 std::string(), std::unique_ptr<crypto::SecureHash>())); | 128 std::string(), std::unique_ptr<crypto::SecureHash>())); |
| 129 file_name = file.full_path(); | 129 file_name = file.full_path(); |
| 130 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 130 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 131 | 131 |
| 132 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 132 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 133 file.AppendDataToFile(kTestData4, kTestDataLength4)); | 133 file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 134 | 134 |
| 135 // Keep the file from getting deleted when existing_file_name is deleted. | 135 // Keep the file from getting deleted when existing_file_name is deleted. |
| 136 file.Detach(); | 136 file.Detach(); |
| 137 | 137 |
| 138 return file_name; | 138 return file_name; |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Create a file with the specified file name. | 141 // Create a file with the specified file name. |
| 142 void CreateFileWithName(const base::FilePath& file_name) { | 142 void CreateFileWithName(const base::FilePath& file_name) { |
| 143 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 143 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 144 BaseFile duplicate_file((net::BoundNetLog())); | 144 BaseFile duplicate_file((net::NetLogWithSource())); |
| 145 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 145 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 146 duplicate_file.Initialize(file_name, temp_dir_.GetPath(), | 146 duplicate_file.Initialize(file_name, temp_dir_.GetPath(), |
| 147 base::File(), 0, std::string(), | 147 base::File(), 0, std::string(), |
| 148 std::unique_ptr<crypto::SecureHash>())); | 148 std::unique_ptr<crypto::SecureHash>())); |
| 149 // Write something into it. | 149 // Write something into it. |
| 150 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 150 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 151 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 151 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 152 duplicate_file.Detach(); | 152 duplicate_file.Detach(); |
| 153 } | 153 } |
| 154 | 154 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 287 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 288 // Get the hash state and file name. | 288 // Get the hash state and file name. |
| 289 std::unique_ptr<crypto::SecureHash> hash_state = base_file_->Finish(); | 289 std::unique_ptr<crypto::SecureHash> hash_state = base_file_->Finish(); |
| 290 | 290 |
| 291 base::FilePath new_file_path(temp_dir_.GetPath().Append( | 291 base::FilePath new_file_path(temp_dir_.GetPath().Append( |
| 292 base::FilePath(FILE_PATH_LITERAL("second_file")))); | 292 base::FilePath(FILE_PATH_LITERAL("second_file")))); |
| 293 | 293 |
| 294 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); | 294 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); |
| 295 | 295 |
| 296 // Create another file | 296 // Create another file |
| 297 BaseFile second_file((net::BoundNetLog())); | 297 BaseFile second_file((net::NetLogWithSource())); |
| 298 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 298 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 299 second_file.Initialize(new_file_path, | 299 second_file.Initialize(new_file_path, |
| 300 base::FilePath(), | 300 base::FilePath(), |
| 301 base::File(), | 301 base::File(), |
| 302 base_file_->bytes_so_far(), | 302 base_file_->bytes_so_far(), |
| 303 std::string(), | 303 std::string(), |
| 304 std::move(hash_state))); | 304 std::move(hash_state))); |
| 305 std::string data(kTestData3); | 305 std::string data(kTestData3); |
| 306 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 306 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 307 second_file.AppendDataToFile(data.data(), data.size())); | 307 second_file.AppendDataToFile(data.data(), data.size())); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 } | 412 } |
| 413 | 413 |
| 414 // Test that a failed write reports an error. | 414 // Test that a failed write reports an error. |
| 415 TEST_F(BaseFileTest, WriteWithError) { | 415 TEST_F(BaseFileTest, WriteWithError) { |
| 416 base::FilePath path; | 416 base::FilePath path; |
| 417 ASSERT_TRUE(base::CreateTemporaryFile(&path)); | 417 ASSERT_TRUE(base::CreateTemporaryFile(&path)); |
| 418 | 418 |
| 419 // Pass a file handle which was opened without the WRITE flag. | 419 // Pass a file handle which was opened without the WRITE flag. |
| 420 // This should result in an error when writing. | 420 // This should result in an error when writing. |
| 421 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 421 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
| 422 base_file_.reset(new BaseFile(net::BoundNetLog())); | 422 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 423 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 423 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 424 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, | 424 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, |
| 425 std::string(), | 425 std::string(), |
| 426 std::unique_ptr<crypto::SecureHash>())); | 426 std::unique_ptr<crypto::SecureHash>())); |
| 427 #if defined(OS_WIN) | 427 #if defined(OS_WIN) |
| 428 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 428 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 429 #elif defined (OS_POSIX) | 429 #elif defined (OS_POSIX) |
| 430 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 430 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 431 #endif | 431 #endif |
| 432 ASSERT_FALSE(AppendDataToFile(kTestData1)); | 432 ASSERT_FALSE(AppendDataToFile(kTestData1)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 453 base_file_->Finish(); | 453 base_file_->Finish(); |
| 454 } | 454 } |
| 455 | 455 |
| 456 // Create a file and append to it. | 456 // Create a file and append to it. |
| 457 TEST_F(BaseFileTest, AppendToBaseFile) { | 457 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 458 // Create a new file. | 458 // Create a new file. |
| 459 base::FilePath existing_file_name = CreateTestFile(); | 459 base::FilePath existing_file_name = CreateTestFile(); |
| 460 set_expected_data(kTestData4); | 460 set_expected_data(kTestData4); |
| 461 | 461 |
| 462 // Use the file we've just created. | 462 // Use the file we've just created. |
| 463 base_file_.reset(new BaseFile(net::BoundNetLog())); | 463 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 464 ASSERT_EQ( | 464 ASSERT_EQ( |
| 465 DOWNLOAD_INTERRUPT_REASON_NONE, | 465 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 466 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), | 466 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), |
| 467 kTestDataLength4, std::string(), | 467 kTestDataLength4, std::string(), |
| 468 std::unique_ptr<crypto::SecureHash>())); | 468 std::unique_ptr<crypto::SecureHash>())); |
| 469 | 469 |
| 470 const base::FilePath file_name = base_file_->full_path(); | 470 const base::FilePath file_name = base_file_->full_path(); |
| 471 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 471 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 472 | 472 |
| 473 // Write into the file. | 473 // Write into the file. |
| 474 EXPECT_TRUE(AppendDataToFile(kTestData1)); | 474 EXPECT_TRUE(AppendDataToFile(kTestData1)); |
| 475 | 475 |
| 476 base_file_->Finish(); | 476 base_file_->Finish(); |
| 477 base_file_->Detach(); | 477 base_file_->Detach(); |
| 478 expect_file_survives_ = true; | 478 expect_file_survives_ = true; |
| 479 } | 479 } |
| 480 | 480 |
| 481 // Create a read-only file and attempt to write to it. | 481 // Create a read-only file and attempt to write to it. |
| 482 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 482 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 483 // Create a new file. | 483 // Create a new file. |
| 484 base::FilePath readonly_file_name = CreateTestFile(); | 484 base::FilePath readonly_file_name = CreateTestFile(); |
| 485 | 485 |
| 486 // Restore permissions to the file when we are done with this test. | 486 // Restore permissions to the file when we are done with this test. |
| 487 base::FilePermissionRestorer restore_permissions(readonly_file_name); | 487 base::FilePermissionRestorer restore_permissions(readonly_file_name); |
| 488 | 488 |
| 489 // Make it read-only. | 489 // Make it read-only. |
| 490 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); | 490 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); |
| 491 | 491 |
| 492 // Try to overwrite it. | 492 // Try to overwrite it. |
| 493 base_file_.reset(new BaseFile(net::BoundNetLog())); | 493 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 494 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 494 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 495 base_file_->Initialize(readonly_file_name, base::FilePath(), | 495 base_file_->Initialize(readonly_file_name, base::FilePath(), |
| 496 base::File(), 0, std::string(), | 496 base::File(), 0, std::string(), |
| 497 std::unique_ptr<crypto::SecureHash>())); | 497 std::unique_ptr<crypto::SecureHash>())); |
| 498 | 498 |
| 499 expect_in_progress_ = false; | 499 expect_in_progress_ = false; |
| 500 | 500 |
| 501 const base::FilePath file_name = base_file_->full_path(); | 501 const base::FilePath file_name = base_file_->full_path(); |
| 502 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 502 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 503 | 503 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 | 727 |
| 728 const char kData[] = "hello"; | 728 const char kData[] = "hello"; |
| 729 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 729 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
| 730 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 730 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
| 731 // The file that we created here should stick around when the BaseFile is | 731 // The file that we created here should stick around when the BaseFile is |
| 732 // destroyed during TearDown. | 732 // destroyed during TearDown. |
| 733 expect_file_survives_ = true; | 733 expect_file_survives_ = true; |
| 734 } | 734 } |
| 735 | 735 |
| 736 } // namespace content | 736 } // namespace content |
| OLD | NEW |