| 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/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // Make sure the data has been properly written to disk. | 72 // Make sure the data has been properly written to disk. |
| 73 std::string disk_data; | 73 std::string disk_data; |
| 74 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); | 74 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); |
| 75 EXPECT_EQ(expected_data_, disk_data); | 75 EXPECT_EQ(expected_data_, disk_data); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 78 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 79 // thread checks inside it. | 79 // thread checks inside it. |
| 80 base_file_.reset(); | 80 base_file_.reset(); |
| 81 | 81 |
| 82 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); | 82 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void ResetHash() { | 85 void ResetHash() { |
| 86 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 86 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 87 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); | 87 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void UpdateHash(const char* data, size_t length) { | 90 void UpdateHash(const char* data, size_t length) { |
| 91 secure_hash_->Update(data, length); | 91 secure_hash_->Update(data, length); |
| 92 } | 92 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // Test the most basic scenario: just create the object and do a sanity check | 232 // Test the most basic scenario: just create the object and do a sanity check |
| 233 // on all its accessors. This is actually a case that rarely happens | 233 // on all its accessors. This is actually a case that rarely happens |
| 234 // in production, where we would at least Initialize it. | 234 // in production, where we would at least Initialize it. |
| 235 TEST_F(BaseFileTest, CreateDestroy) { | 235 TEST_F(BaseFileTest, CreateDestroy) { |
| 236 EXPECT_EQ(base::FilePath().value(), base_file_->full_path().value()); | 236 EXPECT_EQ(base::FilePath().value(), base_file_->full_path().value()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Cancel the download explicitly. | 239 // Cancel the download explicitly. |
| 240 TEST_F(BaseFileTest, Cancel) { | 240 TEST_F(BaseFileTest, Cancel) { |
| 241 ASSERT_TRUE(InitializeFile()); | 241 ASSERT_TRUE(InitializeFile()); |
| 242 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 242 EXPECT_TRUE(base::PathExists(base_file_->full_path())); |
| 243 base_file_->Cancel(); | 243 base_file_->Cancel(); |
| 244 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 244 EXPECT_FALSE(base::PathExists(base_file_->full_path())); |
| 245 EXPECT_NE(base::FilePath().value(), base_file_->full_path().value()); | 245 EXPECT_NE(base::FilePath().value(), base_file_->full_path().value()); |
| 246 } | 246 } |
| 247 | 247 |
| 248 // Write data to the file and detach it, so it doesn't get deleted | 248 // Write data to the file and detach it, so it doesn't get deleted |
| 249 // automatically when base_file_ is destructed. | 249 // automatically when base_file_ is destructed. |
| 250 TEST_F(BaseFileTest, WriteAndDetach) { | 250 TEST_F(BaseFileTest, WriteAndDetach) { |
| 251 ASSERT_TRUE(InitializeFile()); | 251 ASSERT_TRUE(InitializeFile()); |
| 252 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 252 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 253 base_file_->Finish(); | 253 base_file_->Finish(); |
| 254 base_file_->Detach(); | 254 base_file_->Detach(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 277 | 277 |
| 278 base_file_->Detach(); | 278 base_file_->Detach(); |
| 279 expect_file_survives_ = true; | 279 expect_file_survives_ = true; |
| 280 } | 280 } |
| 281 | 281 |
| 282 // Rename the file after writing to it, then detach. | 282 // Rename the file after writing to it, then detach. |
| 283 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 283 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 284 ASSERT_TRUE(InitializeFile()); | 284 ASSERT_TRUE(InitializeFile()); |
| 285 | 285 |
| 286 base::FilePath initial_path(base_file_->full_path()); | 286 base::FilePath initial_path(base_file_->full_path()); |
| 287 EXPECT_TRUE(file_util::PathExists(initial_path)); | 287 EXPECT_TRUE(base::PathExists(initial_path)); |
| 288 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 288 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 289 EXPECT_FALSE(file_util::PathExists(new_path)); | 289 EXPECT_FALSE(base::PathExists(new_path)); |
| 290 | 290 |
| 291 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 291 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 292 | 292 |
| 293 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 293 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 294 EXPECT_FALSE(file_util::PathExists(initial_path)); | 294 EXPECT_FALSE(base::PathExists(initial_path)); |
| 295 EXPECT_TRUE(file_util::PathExists(new_path)); | 295 EXPECT_TRUE(base::PathExists(new_path)); |
| 296 | 296 |
| 297 base_file_->Finish(); | 297 base_file_->Finish(); |
| 298 base_file_->Detach(); | 298 base_file_->Detach(); |
| 299 expect_file_survives_ = true; | 299 expect_file_survives_ = true; |
| 300 } | 300 } |
| 301 | 301 |
| 302 // Write data to the file once. | 302 // Write data to the file once. |
| 303 TEST_F(BaseFileTest, SingleWrite) { | 303 TEST_F(BaseFileTest, SingleWrite) { |
| 304 ASSERT_TRUE(InitializeFile()); | 304 ASSERT_TRUE(InitializeFile()); |
| 305 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 305 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 // This will fail until getting the hash state is supported in SecureHash. | 415 // This will fail until getting the hash state is supported in SecureHash. |
| 416 EXPECT_STREQ(expected_hash_hex.c_str(), | 416 EXPECT_STREQ(expected_hash_hex.c_str(), |
| 417 base::HexEncode(hash.data(), hash.size()).c_str()); | 417 base::HexEncode(hash.data(), hash.size()).c_str()); |
| 418 } | 418 } |
| 419 | 419 |
| 420 // Rename the file after all writes to it. | 420 // Rename the file after all writes to it. |
| 421 TEST_F(BaseFileTest, WriteThenRename) { | 421 TEST_F(BaseFileTest, WriteThenRename) { |
| 422 ASSERT_TRUE(InitializeFile()); | 422 ASSERT_TRUE(InitializeFile()); |
| 423 | 423 |
| 424 base::FilePath initial_path(base_file_->full_path()); | 424 base::FilePath initial_path(base_file_->full_path()); |
| 425 EXPECT_TRUE(file_util::PathExists(initial_path)); | 425 EXPECT_TRUE(base::PathExists(initial_path)); |
| 426 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 426 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 427 EXPECT_FALSE(file_util::PathExists(new_path)); | 427 EXPECT_FALSE(base::PathExists(new_path)); |
| 428 | 428 |
| 429 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 429 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 430 | 430 |
| 431 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 431 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 432 base_file_->Rename(new_path)); | 432 base_file_->Rename(new_path)); |
| 433 EXPECT_FALSE(file_util::PathExists(initial_path)); | 433 EXPECT_FALSE(base::PathExists(initial_path)); |
| 434 EXPECT_TRUE(file_util::PathExists(new_path)); | 434 EXPECT_TRUE(base::PathExists(new_path)); |
| 435 | 435 |
| 436 base_file_->Finish(); | 436 base_file_->Finish(); |
| 437 } | 437 } |
| 438 | 438 |
| 439 // Rename the file while the download is still in progress. | 439 // Rename the file while the download is still in progress. |
| 440 TEST_F(BaseFileTest, RenameWhileInProgress) { | 440 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 441 ASSERT_TRUE(InitializeFile()); | 441 ASSERT_TRUE(InitializeFile()); |
| 442 | 442 |
| 443 base::FilePath initial_path(base_file_->full_path()); | 443 base::FilePath initial_path(base_file_->full_path()); |
| 444 EXPECT_TRUE(file_util::PathExists(initial_path)); | 444 EXPECT_TRUE(base::PathExists(initial_path)); |
| 445 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 445 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 446 EXPECT_FALSE(file_util::PathExists(new_path)); | 446 EXPECT_FALSE(base::PathExists(new_path)); |
| 447 | 447 |
| 448 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 448 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 449 | 449 |
| 450 EXPECT_TRUE(base_file_->in_progress()); | 450 EXPECT_TRUE(base_file_->in_progress()); |
| 451 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 451 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 452 EXPECT_FALSE(file_util::PathExists(initial_path)); | 452 EXPECT_FALSE(base::PathExists(initial_path)); |
| 453 EXPECT_TRUE(file_util::PathExists(new_path)); | 453 EXPECT_TRUE(base::PathExists(new_path)); |
| 454 | 454 |
| 455 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 455 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 456 | 456 |
| 457 base_file_->Finish(); | 457 base_file_->Finish(); |
| 458 } | 458 } |
| 459 | 459 |
| 460 // Test that a failed rename reports the correct error. | 460 // Test that a failed rename reports the correct error. |
| 461 TEST_F(BaseFileTest, RenameWithError) { | 461 TEST_F(BaseFileTest, RenameWithError) { |
| 462 ASSERT_TRUE(InitializeFile()); | 462 ASSERT_TRUE(InitializeFile()); |
| 463 | 463 |
| 464 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | 464 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so |
| 465 // that the rename will fail. | 465 // that the rename will fail. |
| 466 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 466 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
| 467 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 467 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
| 468 | 468 |
| 469 base::FilePath new_path(test_dir.AppendASCII("TestFile")); | 469 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 470 EXPECT_FALSE(file_util::PathExists(new_path)); | 470 EXPECT_FALSE(base::PathExists(new_path)); |
| 471 | 471 |
| 472 { | 472 { |
| 473 file_util::PermissionRestorer restore_permissions_for(test_dir); | 473 file_util::PermissionRestorer restore_permissions_for(test_dir); |
| 474 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | 474 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
| 475 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 475 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 476 base_file_->Rename(new_path)); | 476 base_file_->Rename(new_path)); |
| 477 } | 477 } |
| 478 | 478 |
| 479 base_file_->Finish(); | 479 base_file_->Finish(); |
| 480 } | 480 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 base::FilePath temp_file; | 625 base::FilePath temp_file; |
| 626 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 626 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 627 &temp_file)); | 627 &temp_file)); |
| 628 ASSERT_FALSE(temp_file.empty()); | 628 ASSERT_FALSE(temp_file.empty()); |
| 629 EXPECT_STREQ(temp_file.DirName().value().c_str(), | 629 EXPECT_STREQ(temp_file.DirName().value().c_str(), |
| 630 base_file_->full_path().DirName().value().c_str()); | 630 base_file_->full_path().DirName().value().c_str()); |
| 631 base_file_->Finish(); | 631 base_file_->Finish(); |
| 632 } | 632 } |
| 633 | 633 |
| 634 } // namespace content | 634 } // namespace content |
| OLD | NEW |