| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/test/test_file_util.h" | 8 #include "base/test/test_file_util.h" |
| 9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/browser/download/byte_stream.h" | 10 #include "content/browser/download/byte_stream.h" |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_)); | 238 EXPECT_CALL(*(observer_.get()), DestinationCompleted(_)); |
| 239 loop_.RunAllPending(); | 239 loop_.RunAllPending(); |
| 240 ::testing::Mock::VerifyAndClearExpectations(observer_.get()); | 240 ::testing::Mock::VerifyAndClearExpectations(observer_.get()); |
| 241 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _)) | 241 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _, _)) |
| 242 .Times(AnyNumber()) | 242 .Times(AnyNumber()) |
| 243 .WillRepeatedly(Invoke(this, | 243 .WillRepeatedly(Invoke(this, |
| 244 &DownloadFileTest::SetUpdateDownloadInfo)); | 244 &DownloadFileTest::SetUpdateDownloadInfo)); |
| 245 } | 245 } |
| 246 } | 246 } |
| 247 | 247 |
| 248 DownloadInterruptReason Rename( | 248 DownloadInterruptReason RenameAndUniquify( |
| 249 const FilePath& full_path, bool overwrite_existing_file, | 249 const FilePath& full_path, |
| 250 FilePath* result_path_p) { | 250 FilePath* result_path_p) { |
| 251 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); | 251 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
| 252 DownloadInterruptReason result_reason(DOWNLOAD_INTERRUPT_REASON_NONE); | 252 DownloadInterruptReason result_reason(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 253 bool callback_was_called(false); | 253 bool callback_was_called(false); |
| 254 FilePath result_path; | 254 FilePath result_path; |
| 255 | 255 |
| 256 download_file_->Rename(full_path, overwrite_existing_file, | 256 download_file_->RenameAndUniquify( |
| 257 base::Bind(&DownloadFileTest::SetRenameResult, | 257 full_path, base::Bind(&DownloadFileTest::SetRenameResult, |
| 258 weak_ptr_factory.GetWeakPtr(), | 258 weak_ptr_factory.GetWeakPtr(), |
| 259 &callback_was_called, | 259 &callback_was_called, |
| 260 &result_reason, result_path_p)); | 260 &result_reason, result_path_p)); |
| 261 loop_.RunUntilIdle(); |
| 262 |
| 263 EXPECT_TRUE(callback_was_called); |
| 264 return result_reason; |
| 265 } |
| 266 |
| 267 DownloadInterruptReason RenameAndAnnotate( |
| 268 const FilePath& full_path, |
| 269 FilePath* result_path_p) { |
| 270 base::WeakPtrFactory<DownloadFileTest> weak_ptr_factory(this); |
| 271 DownloadInterruptReason result_reason(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 272 bool callback_was_called(false); |
| 273 FilePath result_path; |
| 274 |
| 275 download_file_->RenameAndAnnotate( |
| 276 full_path, base::Bind(&DownloadFileTest::SetRenameResult, |
| 277 weak_ptr_factory.GetWeakPtr(), |
| 278 &callback_was_called, |
| 279 &result_reason, result_path_p)); |
| 261 loop_.RunAllPending(); | 280 loop_.RunAllPending(); |
| 262 | 281 |
| 263 EXPECT_TRUE(callback_was_called); | 282 EXPECT_TRUE(callback_was_called); |
| 264 return result_reason; | 283 return result_reason; |
| 265 } | 284 } |
| 266 | 285 |
| 267 protected: | 286 protected: |
| 268 scoped_ptr<StrictMock<MockDownloadDestinationObserver> > observer_; | 287 scoped_ptr<StrictMock<MockDownloadDestinationObserver> > observer_; |
| 269 base::WeakPtrFactory<DownloadDestinationObserver> observer_factory_; | 288 base::WeakPtrFactory<DownloadDestinationObserver> observer_factory_; |
| 270 | 289 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 EXPECT_TRUE(file_util::PathExists(initial_path)); | 346 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 328 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 347 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| 329 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | 348 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); |
| 330 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | 349 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); |
| 331 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | 350 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); |
| 332 FilePath path_5(initial_path.InsertBeforeExtensionASCII("_5")); | 351 FilePath path_5(initial_path.InsertBeforeExtensionASCII("_5")); |
| 333 FilePath output_path; | 352 FilePath output_path; |
| 334 | 353 |
| 335 // Rename the file before downloading any data. | 354 // Rename the file before downloading any data. |
| 336 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 355 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 337 Rename(path_1, false, &output_path)); | 356 RenameAndUniquify(path_1, &output_path)); |
| 338 FilePath renamed_path = download_file_->FullPath(); | 357 FilePath renamed_path = download_file_->FullPath(); |
| 339 EXPECT_EQ(path_1, renamed_path); | 358 EXPECT_EQ(path_1, renamed_path); |
| 340 EXPECT_EQ(path_1, output_path); | 359 EXPECT_EQ(path_1, output_path); |
| 341 | 360 |
| 342 // Check the files. | 361 // Check the files. |
| 343 EXPECT_FALSE(file_util::PathExists(initial_path)); | 362 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 344 EXPECT_TRUE(file_util::PathExists(path_1)); | 363 EXPECT_TRUE(file_util::PathExists(path_1)); |
| 345 | 364 |
| 346 // Download the data. | 365 // Download the data. |
| 347 const char* chunks1[] = { kTestData1, kTestData2 }; | 366 const char* chunks1[] = { kTestData1, kTestData2 }; |
| 348 AppendDataToFile(chunks1, 2); | 367 AppendDataToFile(chunks1, 2); |
| 349 | 368 |
| 350 // Rename the file after downloading some data. | 369 // Rename the file after downloading some data. |
| 351 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 370 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 352 Rename(path_2, false, &output_path)); | 371 RenameAndUniquify(path_2, &output_path)); |
| 353 renamed_path = download_file_->FullPath(); | 372 renamed_path = download_file_->FullPath(); |
| 354 EXPECT_EQ(path_2, renamed_path); | 373 EXPECT_EQ(path_2, renamed_path); |
| 355 EXPECT_EQ(path_2, output_path); | 374 EXPECT_EQ(path_2, output_path); |
| 356 | 375 |
| 357 // Check the files. | 376 // Check the files. |
| 358 EXPECT_FALSE(file_util::PathExists(path_1)); | 377 EXPECT_FALSE(file_util::PathExists(path_1)); |
| 359 EXPECT_TRUE(file_util::PathExists(path_2)); | 378 EXPECT_TRUE(file_util::PathExists(path_2)); |
| 360 | 379 |
| 361 const char* chunks2[] = { kTestData3 }; | 380 const char* chunks2[] = { kTestData3 }; |
| 362 AppendDataToFile(chunks2, 1); | 381 AppendDataToFile(chunks2, 1); |
| 363 | 382 |
| 364 // Rename the file after downloading all the data. | 383 // Rename the file after downloading all the data. |
| 365 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 384 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 366 Rename(path_3, false, &output_path)); | 385 RenameAndUniquify(path_3, &output_path)); |
| 367 renamed_path = download_file_->FullPath(); | 386 renamed_path = download_file_->FullPath(); |
| 368 EXPECT_EQ(path_3, renamed_path); | 387 EXPECT_EQ(path_3, renamed_path); |
| 369 EXPECT_EQ(path_3, output_path); | 388 EXPECT_EQ(path_3, output_path); |
| 370 | 389 |
| 371 // Check the files. | 390 // Check the files. |
| 372 EXPECT_FALSE(file_util::PathExists(path_2)); | 391 EXPECT_FALSE(file_util::PathExists(path_2)); |
| 373 EXPECT_TRUE(file_util::PathExists(path_3)); | 392 EXPECT_TRUE(file_util::PathExists(path_3)); |
| 374 | 393 |
| 375 // Should not be able to get the hash until the file is closed. | 394 // Should not be able to get the hash until the file is closed. |
| 376 std::string hash; | 395 std::string hash; |
| 377 EXPECT_FALSE(download_file_->GetHash(&hash)); | 396 EXPECT_FALSE(download_file_->GetHash(&hash)); |
| 378 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 397 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 379 loop_.RunAllPending(); | 398 loop_.RunAllPending(); |
| 380 | 399 |
| 381 // Rename the file after downloading all the data and closing the file. | 400 // Rename the file after downloading all the data and closing the file. |
| 382 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 401 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 383 Rename(path_4, false, &output_path)); | 402 RenameAndUniquify(path_4, &output_path)); |
| 384 renamed_path = download_file_->FullPath(); | 403 renamed_path = download_file_->FullPath(); |
| 385 EXPECT_EQ(path_4, renamed_path); | 404 EXPECT_EQ(path_4, renamed_path); |
| 386 EXPECT_EQ(path_4, output_path); | 405 EXPECT_EQ(path_4, output_path); |
| 387 | 406 |
| 388 // Check the files. | 407 // Check the files. |
| 389 EXPECT_FALSE(file_util::PathExists(path_3)); | 408 EXPECT_FALSE(file_util::PathExists(path_3)); |
| 390 EXPECT_TRUE(file_util::PathExists(path_4)); | 409 EXPECT_TRUE(file_util::PathExists(path_4)); |
| 391 | 410 |
| 392 // Check the hash. | 411 // Check the hash. |
| 393 EXPECT_TRUE(download_file_->GetHash(&hash)); | 412 EXPECT_TRUE(download_file_->GetHash(&hash)); |
| 394 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 413 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
| 395 | 414 |
| 396 // Check that a rename with overwrite to an existing file succeeds. | 415 // Check that a rename with overwrite to an existing file succeeds. |
| 397 std::string file_contents; | 416 std::string file_contents; |
| 398 ASSERT_FALSE(file_util::PathExists(path_5)); | 417 ASSERT_FALSE(file_util::PathExists(path_5)); |
| 399 static const char file_data[] = "xyzzy"; | 418 static const char file_data[] = "xyzzy"; |
| 400 ASSERT_EQ(static_cast<int>(sizeof(file_data) - 1), | 419 ASSERT_EQ(static_cast<int>(sizeof(file_data) - 1), |
| 401 file_util::WriteFile(path_5, file_data, sizeof(file_data) - 1)); | 420 file_util::WriteFile(path_5, file_data, sizeof(file_data) - 1)); |
| 402 ASSERT_TRUE(file_util::PathExists(path_5)); | 421 ASSERT_TRUE(file_util::PathExists(path_5)); |
| 403 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | 422 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); |
| 404 EXPECT_EQ(std::string(file_data), file_contents); | 423 EXPECT_EQ(std::string(file_data), file_contents); |
| 405 | 424 |
| 406 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 425 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 407 Rename(path_5, true, &output_path)); | 426 RenameAndAnnotate(path_5, &output_path)); |
| 408 EXPECT_EQ(path_5, output_path); | 427 EXPECT_EQ(path_5, output_path); |
| 409 | 428 |
| 410 file_contents = ""; | 429 file_contents = ""; |
| 411 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | 430 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); |
| 412 EXPECT_NE(std::string(file_data), file_contents); | 431 EXPECT_NE(std::string(file_data), file_contents); |
| 413 | 432 |
| 414 DestroyDownloadFile(0); | 433 DestroyDownloadFile(0); |
| 415 } | 434 } |
| 416 | 435 |
| 417 // Test to make sure the rename uniquifies if we aren't overwriting | 436 // Test to make sure the rename uniquifies if we aren't overwriting |
| 418 // and there's a file where we're aiming. | 437 // and there's a file where we're aiming. |
| 419 TEST_F(DownloadFileTest, RenameUniquifies) { | 438 TEST_F(DownloadFileTest, RenameUniquifies) { |
| 420 ASSERT_TRUE(CreateDownloadFile(0, true)); | 439 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| 421 FilePath initial_path(download_file_->FullPath()); | 440 FilePath initial_path(download_file_->FullPath()); |
| 422 EXPECT_TRUE(file_util::PathExists(initial_path)); | 441 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 423 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | 442 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); |
| 424 FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)")); | 443 FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)")); |
| 425 | 444 |
| 426 ASSERT_FALSE(file_util::PathExists(path_1)); | 445 ASSERT_FALSE(file_util::PathExists(path_1)); |
| 427 static const char file_data[] = "xyzzy"; | 446 static const char file_data[] = "xyzzy"; |
| 428 ASSERT_EQ(static_cast<int>(sizeof(file_data)), | 447 ASSERT_EQ(static_cast<int>(sizeof(file_data)), |
| 429 file_util::WriteFile(path_1, file_data, sizeof(file_data))); | 448 file_util::WriteFile(path_1, file_data, sizeof(file_data))); |
| 430 ASSERT_TRUE(file_util::PathExists(path_1)); | 449 ASSERT_TRUE(file_util::PathExists(path_1)); |
| 431 | 450 |
| 432 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 451 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, RenameAndUniquify(path_1, NULL)); |
| 433 Rename(path_1, false, NULL)); | |
| 434 EXPECT_TRUE(file_util::PathExists(path_1_suffixed)); | 452 EXPECT_TRUE(file_util::PathExists(path_1_suffixed)); |
| 435 | 453 |
| 436 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 454 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 437 loop_.RunAllPending(); | 455 loop_.RunAllPending(); |
| 438 DestroyDownloadFile(0); | 456 DestroyDownloadFile(0); |
| 439 } | 457 } |
| 440 | 458 |
| 441 // Test to make sure we get the proper error on failure. | 459 // Test to make sure we get the proper error on failure. |
| 442 TEST_F(DownloadFileTest, RenameError) { | 460 TEST_F(DownloadFileTest, RenameError) { |
| 443 ASSERT_TRUE(CreateDownloadFile(0, true)); | 461 ASSERT_TRUE(CreateDownloadFile(0, true)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 454 ASSERT_FALSE(file_util::PathExists(target_path_suffixed)); | 472 ASSERT_FALSE(file_util::PathExists(target_path_suffixed)); |
| 455 | 473 |
| 456 // Make the directory unwritable and try to rename within it. | 474 // Make the directory unwritable and try to rename within it. |
| 457 { | 475 { |
| 458 file_util::PermissionRestorer restorer(tempdir); | 476 file_util::PermissionRestorer restorer(tempdir); |
| 459 ASSERT_TRUE(file_util::MakeFileUnwritable(tempdir)); | 477 ASSERT_TRUE(file_util::MakeFileUnwritable(tempdir)); |
| 460 | 478 |
| 461 // Expect nulling out of further processing. | 479 // Expect nulling out of further processing. |
| 462 EXPECT_CALL(*input_stream_, RegisterCallback(IsNullCallback())); | 480 EXPECT_CALL(*input_stream_, RegisterCallback(IsNullCallback())); |
| 463 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 481 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 464 Rename(target_path, true, NULL)); | 482 RenameAndAnnotate(target_path, NULL)); |
| 465 EXPECT_FALSE(file_util::PathExists(target_path_suffixed)); | 483 EXPECT_FALSE(file_util::PathExists(target_path_suffixed)); |
| 466 } | 484 } |
| 467 | 485 |
| 468 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 486 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 469 loop_.RunAllPending(); | 487 loop_.RunAllPending(); |
| 470 DestroyDownloadFile(0); | 488 DestroyDownloadFile(0); |
| 471 } | 489 } |
| 472 | 490 |
| 473 // Various tests of the StreamActive method. | 491 // Various tests of the StreamActive method. |
| 474 TEST_F(DownloadFileTest, StreamEmptySuccess) { | 492 TEST_F(DownloadFileTest, StreamEmptySuccess) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 | 596 |
| 579 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 597 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
| 580 bytes_); | 598 bytes_); |
| 581 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 599 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
| 582 | 600 |
| 583 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 601 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
| 584 DestroyDownloadFile(0); | 602 DestroyDownloadFile(0); |
| 585 } | 603 } |
| 586 | 604 |
| 587 } // namespace content | 605 } // namespace content |
| OLD | NEW |