| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 84 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 85 // thread checks inside it. | 85 // thread checks inside it. |
| 86 base_file_.reset(); | 86 base_file_.reset(); |
| 87 | 87 |
| 88 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); | 88 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool InitializeFile() { | 91 bool InitializeFile() { |
| 92 DownloadInterruptReason result = base_file_->Initialize( | 92 DownloadInterruptReason result = base_file_->Initialize( |
| 93 base::FilePath(), temp_dir_.path(), base::File(), 0, std::string(), | 93 base::FilePath(), temp_dir_.GetPath(), base::File(), 0, std::string(), |
| 94 std::unique_ptr<crypto::SecureHash>()); | 94 std::unique_ptr<crypto::SecureHash>()); |
| 95 EXPECT_EQ(expected_error_, result); | 95 EXPECT_EQ(expected_error_, result); |
| 96 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 96 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool AppendDataToFile(const std::string& data) { | 99 bool AppendDataToFile(const std::string& data) { |
| 100 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); | 100 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); |
| 101 DownloadInterruptReason result = | 101 DownloadInterruptReason result = |
| 102 base_file_->AppendDataToFile(data.data(), data.size()); | 102 base_file_->AppendDataToFile(data.data(), data.size()); |
| 103 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) | 103 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 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::BoundNetLog())); |
| 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_.path(), 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::BoundNetLog())); |
| 145 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 145 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 146 duplicate_file.Initialize(file_name, temp_dir_.path(), | 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 |
| 155 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { | 155 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { |
| 156 EXPECT_TRUE(base_file_.get()); | 156 EXPECT_TRUE(base_file_.get()); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 base_file_->Detach(); | 241 base_file_->Detach(); |
| 242 expect_file_survives_ = true; | 242 expect_file_survives_ = true; |
| 243 } | 243 } |
| 244 | 244 |
| 245 // Rename the file after writing to it, then detach. | 245 // Rename the file after writing to it, then detach. |
| 246 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 246 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 247 ASSERT_TRUE(InitializeFile()); | 247 ASSERT_TRUE(InitializeFile()); |
| 248 | 248 |
| 249 base::FilePath initial_path(base_file_->full_path()); | 249 base::FilePath initial_path(base_file_->full_path()); |
| 250 EXPECT_TRUE(base::PathExists(initial_path)); | 250 EXPECT_TRUE(base::PathExists(initial_path)); |
| 251 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 251 base::FilePath new_path(temp_dir_.GetPath().AppendASCII("NewFile")); |
| 252 EXPECT_FALSE(base::PathExists(new_path)); | 252 EXPECT_FALSE(base::PathExists(new_path)); |
| 253 | 253 |
| 254 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 254 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 255 | 255 |
| 256 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 256 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 257 EXPECT_FALSE(base::PathExists(initial_path)); | 257 EXPECT_FALSE(base::PathExists(initial_path)); |
| 258 EXPECT_TRUE(base::PathExists(new_path)); | 258 EXPECT_TRUE(base::PathExists(new_path)); |
| 259 | 259 |
| 260 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); | 260 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 261 base_file_->Detach(); | 261 base_file_->Detach(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 281 // Write data to the file multiple times, interrupt it, and continue using | 281 // Write data to the file multiple times, interrupt it, and continue using |
| 282 // another file. Calculate the resulting combined sha256 hash. | 282 // another file. Calculate the resulting combined sha256 hash. |
| 283 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { | 283 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { |
| 284 ASSERT_TRUE(InitializeFile()); | 284 ASSERT_TRUE(InitializeFile()); |
| 285 // Write some data | 285 // Write some data |
| 286 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 286 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 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_.path().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::BoundNetLog())); |
| 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())); |
| 308 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); | 308 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); |
| 309 } | 309 } |
| 310 | 310 |
| 311 // Rename the file after all writes to it. | 311 // Rename the file after all writes to it. |
| 312 TEST_F(BaseFileTest, WriteThenRename) { | 312 TEST_F(BaseFileTest, WriteThenRename) { |
| 313 ASSERT_TRUE(InitializeFile()); | 313 ASSERT_TRUE(InitializeFile()); |
| 314 | 314 |
| 315 base::FilePath initial_path(base_file_->full_path()); | 315 base::FilePath initial_path(base_file_->full_path()); |
| 316 EXPECT_TRUE(base::PathExists(initial_path)); | 316 EXPECT_TRUE(base::PathExists(initial_path)); |
| 317 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 317 base::FilePath new_path(temp_dir_.GetPath().AppendASCII("NewFile")); |
| 318 EXPECT_FALSE(base::PathExists(new_path)); | 318 EXPECT_FALSE(base::PathExists(new_path)); |
| 319 | 319 |
| 320 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 320 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 321 | 321 |
| 322 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 322 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 323 base_file_->Rename(new_path)); | 323 base_file_->Rename(new_path)); |
| 324 EXPECT_FALSE(base::PathExists(initial_path)); | 324 EXPECT_FALSE(base::PathExists(initial_path)); |
| 325 EXPECT_TRUE(base::PathExists(new_path)); | 325 EXPECT_TRUE(base::PathExists(new_path)); |
| 326 | 326 |
| 327 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); | 327 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 328 } | 328 } |
| 329 | 329 |
| 330 // Rename the file while the download is still in progress. | 330 // Rename the file while the download is still in progress. |
| 331 TEST_F(BaseFileTest, RenameWhileInProgress) { | 331 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 332 ASSERT_TRUE(InitializeFile()); | 332 ASSERT_TRUE(InitializeFile()); |
| 333 | 333 |
| 334 base::FilePath initial_path(base_file_->full_path()); | 334 base::FilePath initial_path(base_file_->full_path()); |
| 335 EXPECT_TRUE(base::PathExists(initial_path)); | 335 EXPECT_TRUE(base::PathExists(initial_path)); |
| 336 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 336 base::FilePath new_path(temp_dir_.GetPath().AppendASCII("NewFile")); |
| 337 EXPECT_FALSE(base::PathExists(new_path)); | 337 EXPECT_FALSE(base::PathExists(new_path)); |
| 338 | 338 |
| 339 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 339 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 340 | 340 |
| 341 EXPECT_TRUE(base_file_->in_progress()); | 341 EXPECT_TRUE(base_file_->in_progress()); |
| 342 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 342 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 343 EXPECT_FALSE(base::PathExists(initial_path)); | 343 EXPECT_FALSE(base::PathExists(initial_path)); |
| 344 EXPECT_TRUE(base::PathExists(new_path)); | 344 EXPECT_TRUE(base::PathExists(new_path)); |
| 345 | 345 |
| 346 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 346 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 347 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 347 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 348 | 348 |
| 349 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 349 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 350 } | 350 } |
| 351 | 351 |
| 352 // Test that a failed rename reports the correct error. | 352 // Test that a failed rename reports the correct error. |
| 353 TEST_F(BaseFileTest, RenameWithError) { | 353 TEST_F(BaseFileTest, RenameWithError) { |
| 354 ASSERT_TRUE(InitializeFile()); | 354 ASSERT_TRUE(InitializeFile()); |
| 355 | 355 |
| 356 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | 356 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so |
| 357 // that the rename will fail. | 357 // that the rename will fail. |
| 358 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 358 base::FilePath test_dir(temp_dir_.GetPath().AppendASCII("TestDir")); |
| 359 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 359 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 360 | 360 |
| 361 base::FilePath new_path(test_dir.AppendASCII("TestFile")); | 361 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 362 EXPECT_FALSE(base::PathExists(new_path)); | 362 EXPECT_FALSE(base::PathExists(new_path)); |
| 363 | 363 |
| 364 { | 364 { |
| 365 base::FilePermissionRestorer restore_permissions_for(test_dir); | 365 base::FilePermissionRestorer restore_permissions_for(test_dir); |
| 366 ASSERT_TRUE(base::MakeFileUnwritable(test_dir)); | 366 ASSERT_TRUE(base::MakeFileUnwritable(test_dir)); |
| 367 ExpectPermissionError(base_file_->Rename(new_path)); | 367 ExpectPermissionError(base_file_->Rename(new_path)); |
| 368 } | 368 } |
| 369 | 369 |
| 370 base_file_->Finish(); | 370 base_file_->Finish(); |
| 371 } | 371 } |
| 372 | 372 |
| 373 // Test that if a rename fails for an in-progress BaseFile, it remains writeable | 373 // Test that if a rename fails for an in-progress BaseFile, it remains writeable |
| 374 // and renameable. | 374 // and renameable. |
| 375 TEST_F(BaseFileTest, RenameWithErrorInProgress) { | 375 TEST_F(BaseFileTest, RenameWithErrorInProgress) { |
| 376 ASSERT_TRUE(InitializeFile()); | 376 ASSERT_TRUE(InitializeFile()); |
| 377 | 377 |
| 378 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 378 base::FilePath test_dir(temp_dir_.GetPath().AppendASCII("TestDir")); |
| 379 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 379 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 380 | 380 |
| 381 base::FilePath new_path(test_dir.AppendASCII("TestFile")); | 381 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 382 EXPECT_FALSE(base::PathExists(new_path)); | 382 EXPECT_FALSE(base::PathExists(new_path)); |
| 383 | 383 |
| 384 // Write some data to start with. | 384 // Write some data to start with. |
| 385 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 385 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 386 ASSERT_TRUE(base_file_->in_progress()); | 386 ASSERT_TRUE(base_file_->in_progress()); |
| 387 | 387 |
| 388 base::FilePath old_path = base_file_->full_path(); | 388 base::FilePath old_path = base_file_->full_path(); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 EXPECT_FALSE(AppendDataToFile(kTestData1)); | 506 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
| 507 | 507 |
| 508 base_file_->Finish(); | 508 base_file_->Finish(); |
| 509 base_file_->Detach(); | 509 base_file_->Detach(); |
| 510 expect_file_survives_ = true; | 510 expect_file_survives_ = true; |
| 511 } | 511 } |
| 512 | 512 |
| 513 // Open an existing file and continue writing to it. The hash of the partial | 513 // Open an existing file and continue writing to it. The hash of the partial |
| 514 // file is known and matches the existing contents. | 514 // file is known and matches the existing contents. |
| 515 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { | 515 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { |
| 516 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 516 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 517 ASSERT_EQ(kTestDataLength1, | 517 ASSERT_EQ(kTestDataLength1, |
| 518 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 518 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 519 | 519 |
| 520 std::string hash_so_far(std::begin(kHashOfTestData1), | 520 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 521 std::end(kHashOfTestData1)); | 521 std::end(kHashOfTestData1)); |
| 522 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 522 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 523 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 523 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 524 kTestDataLength1, hash_so_far, | 524 kTestDataLength1, hash_so_far, |
| 525 std::unique_ptr<crypto::SecureHash>())); | 525 std::unique_ptr<crypto::SecureHash>())); |
| 526 set_expected_data(kTestData1); | 526 set_expected_data(kTestData1); |
| 527 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 527 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 528 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 528 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 529 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 529 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 530 } | 530 } |
| 531 | 531 |
| 532 // Open an existing file and continue writing to it. The hash of the partial | 532 // Open an existing file and continue writing to it. The hash of the partial |
| 533 // file is unknown. | 533 // file is unknown. |
| 534 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { | 534 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { |
| 535 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 535 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 536 ASSERT_EQ(kTestDataLength1, | 536 ASSERT_EQ(kTestDataLength1, |
| 537 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 537 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 538 | 538 |
| 539 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 539 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 540 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 540 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 541 kTestDataLength1, std::string(), | 541 kTestDataLength1, std::string(), |
| 542 std::unique_ptr<crypto::SecureHash>())); | 542 std::unique_ptr<crypto::SecureHash>())); |
| 543 set_expected_data(kTestData1); | 543 set_expected_data(kTestData1); |
| 544 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 544 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 545 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 545 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 546 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 546 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 547 } | 547 } |
| 548 | 548 |
| 549 // Open an existing file. The contentsof the file doesn't match the known hash. | 549 // Open an existing file. The contentsof the file doesn't match the known hash. |
| 550 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { | 550 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { |
| 551 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 551 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 552 ASSERT_EQ(kTestDataLength2, | 552 ASSERT_EQ(kTestDataLength2, |
| 553 base::WriteFile(file_path, kTestData2, kTestDataLength2)); | 553 base::WriteFile(file_path, kTestData2, kTestDataLength2)); |
| 554 | 554 |
| 555 std::string hash_so_far(std::begin(kHashOfTestData1), | 555 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 556 std::end(kHashOfTestData1)); | 556 std::end(kHashOfTestData1)); |
| 557 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | 557 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, |
| 558 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 558 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 559 kTestDataLength2, hash_so_far, | 559 kTestDataLength2, hash_so_far, |
| 560 std::unique_ptr<crypto::SecureHash>())); | 560 std::unique_ptr<crypto::SecureHash>())); |
| 561 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | 561 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); |
| 562 } | 562 } |
| 563 | 563 |
| 564 // Open a large existing file with a known hash and continue writing to it. | 564 // Open a large existing file with a known hash and continue writing to it. |
| 565 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { | 565 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { |
| 566 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 566 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 567 std::string big_buffer(1024 * 200, 'a'); | 567 std::string big_buffer(1024 * 200, 'a'); |
| 568 ASSERT_EQ(static_cast<int>(big_buffer.size()), | 568 ASSERT_EQ(static_cast<int>(big_buffer.size()), |
| 569 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | 569 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); |
| 570 | 570 |
| 571 // Hash of partial file (1024*200 * 'a') | 571 // Hash of partial file (1024*200 * 'a') |
| 572 const uint8_t kExpectedPartialHash[] = { | 572 const uint8_t kExpectedPartialHash[] = { |
| 573 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, | 573 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, |
| 574 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, | 574 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, |
| 575 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; | 575 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; |
| 576 | 576 |
| 577 // Hash of entire file (1024*400 * 'a') | 577 // Hash of entire file (1024*400 * 'a') |
| 578 const uint8_t kExpectedFullHash[] = { | 578 const uint8_t kExpectedFullHash[] = { |
| 579 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, | 579 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, |
| 580 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, | 580 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, |
| 581 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; | 581 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; |
| 582 | 582 |
| 583 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 583 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 584 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 584 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 585 big_buffer.size(), | 585 big_buffer.size(), |
| 586 std::string(std::begin(kExpectedPartialHash), | 586 std::string(std::begin(kExpectedPartialHash), |
| 587 std::end(kExpectedPartialHash)), | 587 std::end(kExpectedPartialHash)), |
| 588 std::unique_ptr<crypto::SecureHash>())); | 588 std::unique_ptr<crypto::SecureHash>())); |
| 589 set_expected_data(big_buffer); // Contents of the file on Open. | 589 set_expected_data(big_buffer); // Contents of the file on Open. |
| 590 ASSERT_TRUE(AppendDataToFile(big_buffer)); | 590 ASSERT_TRUE(AppendDataToFile(big_buffer)); |
| 591 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); | 591 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); |
| 592 } | 592 } |
| 593 | 593 |
| 594 // Open a large existing file. The contents doesn't match the known hash. | 594 // Open a large existing file. The contents doesn't match the known hash. |
| 595 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { | 595 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { |
| 596 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 596 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 597 std::string big_buffer(1024 * 200, 'a'); | 597 std::string big_buffer(1024 * 200, 'a'); |
| 598 ASSERT_EQ(static_cast<int>(big_buffer.size()), | 598 ASSERT_EQ(static_cast<int>(big_buffer.size()), |
| 599 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | 599 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); |
| 600 | 600 |
| 601 // Incorrect hash of partial file (1024*200 * 'a') | 601 // Incorrect hash of partial file (1024*200 * 'a') |
| 602 const uint8_t kExpectedPartialHash[] = { | 602 const uint8_t kExpectedPartialHash[] = { |
| 603 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, | 603 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, |
| 604 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, | 604 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, |
| 605 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; | 605 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; |
| 606 | 606 |
| 607 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | 607 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, |
| 608 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 608 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 609 big_buffer.size(), | 609 big_buffer.size(), |
| 610 std::string(std::begin(kExpectedPartialHash), | 610 std::string(std::begin(kExpectedPartialHash), |
| 611 std::end(kExpectedPartialHash)), | 611 std::end(kExpectedPartialHash)), |
| 612 std::unique_ptr<crypto::SecureHash>())); | 612 std::unique_ptr<crypto::SecureHash>())); |
| 613 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | 613 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); |
| 614 } | 614 } |
| 615 | 615 |
| 616 // Open an existing file. The size of the file is too short. | 616 // Open an existing file. The size of the file is too short. |
| 617 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { | 617 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { |
| 618 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 618 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 619 ASSERT_EQ(kTestDataLength1, | 619 ASSERT_EQ(kTestDataLength1, |
| 620 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 620 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 621 | 621 |
| 622 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, | 622 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, |
| 623 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 623 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 624 kTestDataLength1 + 1, std::string(), | 624 kTestDataLength1 + 1, std::string(), |
| 625 std::unique_ptr<crypto::SecureHash>())); | 625 std::unique_ptr<crypto::SecureHash>())); |
| 626 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); | 626 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); |
| 627 } | 627 } |
| 628 | 628 |
| 629 // Open an existing file. The size is larger than expected. | 629 // Open an existing file. The size is larger than expected. |
| 630 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { | 630 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { |
| 631 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 631 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 632 std::string contents; | 632 std::string contents; |
| 633 contents.append(kTestData1); | 633 contents.append(kTestData1); |
| 634 contents.append("Something extra"); | 634 contents.append("Something extra"); |
| 635 ASSERT_EQ(static_cast<int>(contents.size()), | 635 ASSERT_EQ(static_cast<int>(contents.size()), |
| 636 base::WriteFile(file_path, contents.data(), contents.size())); | 636 base::WriteFile(file_path, contents.data(), contents.size())); |
| 637 | 637 |
| 638 std::string hash_so_far(std::begin(kHashOfTestData1), | 638 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 639 std::end(kHashOfTestData1)); | 639 std::end(kHashOfTestData1)); |
| 640 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 640 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 641 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 641 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 642 kTestDataLength1, hash_so_far, | 642 kTestDataLength1, hash_so_far, |
| 643 std::unique_ptr<crypto::SecureHash>())); | 643 std::unique_ptr<crypto::SecureHash>())); |
| 644 set_expected_data(kTestData1); // Our starting position. | 644 set_expected_data(kTestData1); // Our starting position. |
| 645 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 645 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 646 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 646 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 647 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 647 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 648 } | 648 } |
| 649 | 649 |
| 650 // Open an existing file. The size is large than expected and the hash is | 650 // Open an existing file. The size is large than expected and the hash is |
| 651 // unknown. | 651 // unknown. |
| 652 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { | 652 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { |
| 653 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 653 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 654 std::string contents; | 654 std::string contents; |
| 655 contents.append(kTestData1); | 655 contents.append(kTestData1); |
| 656 contents.append("Something extra"); | 656 contents.append("Something extra"); |
| 657 ASSERT_EQ(static_cast<int>(contents.size()), | 657 ASSERT_EQ(static_cast<int>(contents.size()), |
| 658 base::WriteFile(file_path, contents.data(), contents.size())); | 658 base::WriteFile(file_path, contents.data(), contents.size())); |
| 659 | 659 |
| 660 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 660 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 661 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 661 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 662 kTestDataLength1, std::string(), | 662 kTestDataLength1, std::string(), |
| 663 std::unique_ptr<crypto::SecureHash>())); | 663 std::unique_ptr<crypto::SecureHash>())); |
| 664 set_expected_data(kTestData1); | 664 set_expected_data(kTestData1); |
| 665 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 665 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 666 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 666 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 667 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 667 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 668 } | 668 } |
| 669 | 669 |
| 670 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large | 670 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large |
| 671 // enough to requre multiple Read()s to complete. This provides additional code | 671 // enough to requre multiple Read()s to complete. This provides additional code |
| 672 // coverage for the CalculatePartialHash() logic. | 672 // coverage for the CalculatePartialHash() logic. |
| 673 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) { | 673 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) { |
| 674 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | 674 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 675 const size_t kFileSize = 1024 * 1024; | 675 const size_t kFileSize = 1024 * 1024; |
| 676 const size_t kIntermediateSize = kFileSize / 2 + 111; | 676 const size_t kIntermediateSize = kFileSize / 2 + 111; |
| 677 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the | 677 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the |
| 678 // expected size. | 678 // expected size. |
| 679 std::string contents(kIntermediateSize + 100, 'a'); | 679 std::string contents(kIntermediateSize + 100, 'a'); |
| 680 ASSERT_EQ(static_cast<int>(contents.size()), | 680 ASSERT_EQ(static_cast<int>(contents.size()), |
| 681 base::WriteFile(file_path, contents.data(), contents.size())); | 681 base::WriteFile(file_path, contents.data(), contents.size())); |
| 682 | 682 |
| 683 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 683 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 684 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 684 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 697 ExpectHashValue(kExpectedHash, base_file_->Finish()); | 697 ExpectHashValue(kExpectedHash, base_file_->Finish()); |
| 698 } | 698 } |
| 699 | 699 |
| 700 // Test that a temporary file is created in the default download directory. | 700 // Test that a temporary file is created in the default download directory. |
| 701 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { | 701 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { |
| 702 ASSERT_TRUE(base_file_->full_path().empty()); | 702 ASSERT_TRUE(base_file_->full_path().empty()); |
| 703 ASSERT_TRUE(InitializeFile()); | 703 ASSERT_TRUE(InitializeFile()); |
| 704 EXPECT_FALSE(base_file_->full_path().empty()); | 704 EXPECT_FALSE(base_file_->full_path().empty()); |
| 705 | 705 |
| 706 // On Windows, CreateTemporaryFileInDir() will cause a path with short names | 706 // On Windows, CreateTemporaryFileInDir() will cause a path with short names |
| 707 // to be expanded into a path with long names. Thus temp_dir.path() might not | 707 // to be expanded into a path with long names. Thus temp_dir.GetPath() might |
| 708 // not |
| 708 // be a string-wise match to base_file_->full_path().DirName() even though | 709 // be a string-wise match to base_file_->full_path().DirName() even though |
| 709 // they are in the same directory. | 710 // they are in the same directory. |
| 710 base::FilePath temp_file; | 711 base::FilePath temp_file; |
| 711 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file)); | 712 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file)); |
| 712 ASSERT_FALSE(temp_file.empty()); | 713 ASSERT_FALSE(temp_file.empty()); |
| 713 EXPECT_STREQ(temp_file.DirName().value().c_str(), | 714 EXPECT_STREQ(temp_file.DirName().value().c_str(), |
| 714 base_file_->full_path().DirName().value().c_str()); | 715 base_file_->full_path().DirName().value().c_str()); |
| 715 base_file_->Finish(); | 716 base_file_->Finish(); |
| 716 } | 717 } |
| 717 | 718 |
| 718 TEST_F(BaseFileTest, NoDoubleDeleteAfterCancel) { | 719 TEST_F(BaseFileTest, NoDoubleDeleteAfterCancel) { |
| 719 ASSERT_TRUE(InitializeFile()); | 720 ASSERT_TRUE(InitializeFile()); |
| 720 base::FilePath full_path = base_file_->full_path(); | 721 base::FilePath full_path = base_file_->full_path(); |
| 721 ASSERT_FALSE(full_path.empty()); | 722 ASSERT_FALSE(full_path.empty()); |
| 722 ASSERT_TRUE(base::PathExists(full_path)); | 723 ASSERT_TRUE(base::PathExists(full_path)); |
| 723 | 724 |
| 724 base_file_->Cancel(); | 725 base_file_->Cancel(); |
| 725 ASSERT_FALSE(base::PathExists(full_path)); | 726 ASSERT_FALSE(base::PathExists(full_path)); |
| 726 | 727 |
| 727 const char kData[] = "hello"; | 728 const char kData[] = "hello"; |
| 728 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 729 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
| 729 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 730 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
| 730 // 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 |
| 731 // destroyed during TearDown. | 732 // destroyed during TearDown. |
| 732 expect_file_survives_ = true; | 733 expect_file_survives_ = true; |
| 733 } | 734 } |
| 734 | 735 |
| 735 } // namespace content | 736 } // namespace content |
| OLD | NEW |