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 <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include <fstream> | 7 #include <fstream> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 WorkItem::CreateCopyTreeWorkItem(file_name_from, | 96 WorkItem::CreateCopyTreeWorkItem(file_name_from, |
97 file_name_to, | 97 file_name_to, |
98 temp_dir_.path(), | 98 temp_dir_.path(), |
99 WorkItem::ALWAYS, | 99 WorkItem::ALWAYS, |
100 base::FilePath())); | 100 base::FilePath())); |
101 | 101 |
102 EXPECT_TRUE(work_item->Do()); | 102 EXPECT_TRUE(work_item->Do()); |
103 | 103 |
104 EXPECT_TRUE(base::PathExists(file_name_from)); | 104 EXPECT_TRUE(base::PathExists(file_name_from)); |
105 EXPECT_TRUE(base::PathExists(file_name_to)); | 105 EXPECT_TRUE(base::PathExists(file_name_to)); |
106 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to)); | 106 EXPECT_TRUE(base::ContentsEqual(file_name_from, file_name_to)); |
107 | 107 |
108 // test rollback() | 108 // test rollback() |
109 work_item->Rollback(); | 109 work_item->Rollback(); |
110 | 110 |
111 EXPECT_FALSE(base::PathExists(file_name_to)); | 111 EXPECT_FALSE(base::PathExists(file_name_to)); |
112 EXPECT_TRUE(base::PathExists(file_name_from)); | 112 EXPECT_TRUE(base::PathExists(file_name_from)); |
113 } | 113 } |
114 | 114 |
115 // Copy one file, overwriting the existing one in destination. | 115 // Copy one file, overwriting the existing one in destination. |
116 // Test with always_overwrite being true or false. The file is overwritten | 116 // Test with always_overwrite being true or false. The file is overwritten |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 base::FilePath backup_file(work_item->backup_path_.path()); | 365 base::FilePath backup_file(work_item->backup_path_.path()); |
366 EXPECT_FALSE(backup_file.empty()); | 366 EXPECT_FALSE(backup_file.empty()); |
367 backup_file = backup_file.AppendASCII("File_To"); | 367 backup_file = backup_file.AppendASCII("File_To"); |
368 | 368 |
369 EXPECT_TRUE(base::PathExists(file_name_from)); | 369 EXPECT_TRUE(base::PathExists(file_name_from)); |
370 EXPECT_TRUE(base::PathExists(file_name_to)); | 370 EXPECT_TRUE(base::PathExists(file_name_to)); |
371 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 371 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
372 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); | 372 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); |
373 // verify the file in used is moved to backup place. | 373 // verify the file in used is moved to backup place. |
374 EXPECT_TRUE(base::PathExists(backup_file)); | 374 EXPECT_TRUE(base::PathExists(backup_file)); |
375 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file)); | 375 EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file)); |
376 | 376 |
377 // test rollback() | 377 // test rollback() |
378 work_item->Rollback(); | 378 work_item->Rollback(); |
379 | 379 |
380 EXPECT_TRUE(base::PathExists(file_name_from)); | 380 EXPECT_TRUE(base::PathExists(file_name_from)); |
381 EXPECT_TRUE(base::PathExists(file_name_to)); | 381 EXPECT_TRUE(base::PathExists(file_name_to)); |
382 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 382 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
383 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 383 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
384 // the backup file should be gone after rollback | 384 // the backup file should be gone after rollback |
385 EXPECT_FALSE(base::PathExists(backup_file)); | 385 EXPECT_FALSE(base::PathExists(backup_file)); |
386 | 386 |
387 TerminateProcess(pi.hProcess, 0); | 387 TerminateProcess(pi.hProcess, 0); |
388 // make sure the handle is closed. | 388 // make sure the handle is closed. |
389 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 389 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
390 CloseHandle(pi.hProcess); | 390 CloseHandle(pi.hProcess); |
391 CloseHandle(pi.hThread); | 391 CloseHandle(pi.hThread); |
392 } | 392 } |
393 | 393 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 file_name_to, | 437 file_name_to, |
438 temp_dir_.path(), | 438 temp_dir_.path(), |
439 WorkItem::NEW_NAME_IF_IN_USE, | 439 WorkItem::NEW_NAME_IF_IN_USE, |
440 alternate_to)); | 440 alternate_to)); |
441 | 441 |
442 EXPECT_TRUE(work_item->Do()); | 442 EXPECT_TRUE(work_item->Do()); |
443 | 443 |
444 EXPECT_TRUE(base::PathExists(file_name_from)); | 444 EXPECT_TRUE(base::PathExists(file_name_from)); |
445 EXPECT_TRUE(base::PathExists(file_name_to)); | 445 EXPECT_TRUE(base::PathExists(file_name_to)); |
446 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 446 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
447 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 447 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
448 // verify that the backup path does not exist | 448 // verify that the backup path does not exist |
449 EXPECT_TRUE(work_item->backup_path_.path().empty()); | 449 EXPECT_TRUE(work_item->backup_path_.path().empty()); |
450 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, alternate_to)); | 450 EXPECT_TRUE(base::ContentsEqual(file_name_from, alternate_to)); |
451 | 451 |
452 // test rollback() | 452 // test rollback() |
453 work_item->Rollback(); | 453 work_item->Rollback(); |
454 | 454 |
455 EXPECT_TRUE(base::PathExists(file_name_from)); | 455 EXPECT_TRUE(base::PathExists(file_name_from)); |
456 EXPECT_TRUE(base::PathExists(file_name_to)); | 456 EXPECT_TRUE(base::PathExists(file_name_to)); |
457 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 457 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
458 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 458 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
459 EXPECT_TRUE(work_item->backup_path_.path().empty()); | 459 EXPECT_TRUE(work_item->backup_path_.path().empty()); |
460 // the alternate file should be gone after rollback | 460 // the alternate file should be gone after rollback |
461 EXPECT_FALSE(base::PathExists(alternate_to)); | 461 EXPECT_FALSE(base::PathExists(alternate_to)); |
462 | 462 |
463 TerminateProcess(pi.hProcess, 0); | 463 TerminateProcess(pi.hProcess, 0); |
464 // make sure the handle is closed. | 464 // make sure the handle is closed. |
465 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 465 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
466 CloseHandle(pi.hProcess); | 466 CloseHandle(pi.hProcess); |
467 CloseHandle(pi.hThread); | 467 CloseHandle(pi.hThread); |
468 | 468 |
469 // Now the process has terminated, lets try overwriting the file again | 469 // Now the process has terminated, lets try overwriting the file again |
470 work_item.reset(WorkItem::CreateCopyTreeWorkItem( | 470 work_item.reset(WorkItem::CreateCopyTreeWorkItem( |
471 file_name_from, file_name_to, | 471 file_name_from, file_name_to, |
472 temp_dir_.path(), WorkItem::NEW_NAME_IF_IN_USE, | 472 temp_dir_.path(), WorkItem::NEW_NAME_IF_IN_USE, |
473 alternate_to)); | 473 alternate_to)); |
474 if (IsFileInUse(file_name_to)) | 474 if (IsFileInUse(file_name_to)) |
475 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(2)); | 475 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(2)); |
476 // If file is still in use, the rest of the test will fail. | 476 // If file is still in use, the rest of the test will fail. |
477 ASSERT_FALSE(IsFileInUse(file_name_to)); | 477 ASSERT_FALSE(IsFileInUse(file_name_to)); |
478 EXPECT_TRUE(work_item->Do()); | 478 EXPECT_TRUE(work_item->Do()); |
479 | 479 |
480 // Get the path of backup file | 480 // Get the path of backup file |
481 base::FilePath backup_file(work_item->backup_path_.path()); | 481 base::FilePath backup_file(work_item->backup_path_.path()); |
482 EXPECT_FALSE(backup_file.empty()); | 482 EXPECT_FALSE(backup_file.empty()); |
483 backup_file = backup_file.AppendASCII("File_To"); | 483 backup_file = backup_file.AppendASCII("File_To"); |
484 | 484 |
485 EXPECT_TRUE(base::PathExists(file_name_from)); | 485 EXPECT_TRUE(base::PathExists(file_name_from)); |
486 EXPECT_TRUE(base::PathExists(file_name_to)); | 486 EXPECT_TRUE(base::PathExists(file_name_to)); |
487 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 487 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
488 EXPECT_TRUE(file_util::ContentsEqual(file_name_from, file_name_to)); | 488 EXPECT_TRUE(base::ContentsEqual(file_name_from, file_name_to)); |
489 // verify that the backup path does exist | 489 // verify that the backup path does exist |
490 EXPECT_TRUE(base::PathExists(backup_file)); | 490 EXPECT_TRUE(base::PathExists(backup_file)); |
491 EXPECT_FALSE(base::PathExists(alternate_to)); | 491 EXPECT_FALSE(base::PathExists(alternate_to)); |
492 | 492 |
493 // test rollback() | 493 // test rollback() |
494 work_item->Rollback(); | 494 work_item->Rollback(); |
495 | 495 |
496 EXPECT_TRUE(base::PathExists(file_name_from)); | 496 EXPECT_TRUE(base::PathExists(file_name_from)); |
497 EXPECT_TRUE(base::PathExists(file_name_to)); | 497 EXPECT_TRUE(base::PathExists(file_name_to)); |
498 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 498 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
499 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 499 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
500 // the backup file should be gone after rollback | 500 // the backup file should be gone after rollback |
501 EXPECT_FALSE(base::PathExists(backup_file)); | 501 EXPECT_FALSE(base::PathExists(backup_file)); |
502 EXPECT_FALSE(base::PathExists(alternate_to)); | 502 EXPECT_FALSE(base::PathExists(alternate_to)); |
503 } | 503 } |
504 | 504 |
505 // Test overwrite option IF_NOT_PRESENT: | 505 // Test overwrite option IF_NOT_PRESENT: |
506 // 1. If destination file/directory exist, the source should not be copied | 506 // 1. If destination file/directory exist, the source should not be copied |
507 // 2. If destination file/directory do not exist, the source should be copied | 507 // 2. If destination file/directory do not exist, the source should be copied |
508 // in the destination folder after Do() and should be rolled back after | 508 // in the destination folder after Do() and should be rolled back after |
509 // Rollback(). | 509 // Rollback(). |
(...skipping 30 matching lines...) Expand all Loading... |
540 file_name_to, temp_dir_.path(), | 540 file_name_to, temp_dir_.path(), |
541 WorkItem::IF_NOT_PRESENT, | 541 WorkItem::IF_NOT_PRESENT, |
542 base::FilePath())); | 542 base::FilePath())); |
543 EXPECT_TRUE(work_item->Do()); | 543 EXPECT_TRUE(work_item->Do()); |
544 | 544 |
545 // verify that the source, destination have not changed and backup path | 545 // verify that the source, destination have not changed and backup path |
546 // does not exist | 546 // does not exist |
547 EXPECT_TRUE(base::PathExists(file_name_from)); | 547 EXPECT_TRUE(base::PathExists(file_name_from)); |
548 EXPECT_TRUE(base::PathExists(file_name_to)); | 548 EXPECT_TRUE(base::PathExists(file_name_to)); |
549 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 549 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
550 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 550 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
551 EXPECT_FALSE(base::PathExists(backup_file)); | 551 EXPECT_FALSE(base::PathExists(backup_file)); |
552 | 552 |
553 // test rollback() | 553 // test rollback() |
554 work_item->Rollback(); | 554 work_item->Rollback(); |
555 | 555 |
556 // verify that the source, destination have not changed and backup path | 556 // verify that the source, destination have not changed and backup path |
557 // does not exist after rollback also | 557 // does not exist after rollback also |
558 EXPECT_TRUE(base::PathExists(file_name_from)); | 558 EXPECT_TRUE(base::PathExists(file_name_from)); |
559 EXPECT_TRUE(base::PathExists(file_name_to)); | 559 EXPECT_TRUE(base::PathExists(file_name_to)); |
560 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 560 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
561 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, file_name_to)); | 561 EXPECT_TRUE(base::ContentsEqual(exe_full_path, file_name_to)); |
562 EXPECT_FALSE(base::PathExists(backup_file)); | 562 EXPECT_FALSE(base::PathExists(backup_file)); |
563 | 563 |
564 // Now delete the destination and try copying the file again. | 564 // Now delete the destination and try copying the file again. |
565 base::Delete(file_name_to, true); | 565 base::Delete(file_name_to, true); |
566 work_item.reset(WorkItem::CreateCopyTreeWorkItem( | 566 work_item.reset(WorkItem::CreateCopyTreeWorkItem( |
567 file_name_from, file_name_to, | 567 file_name_from, file_name_to, |
568 temp_dir_.path(), WorkItem::IF_NOT_PRESENT, | 568 temp_dir_.path(), WorkItem::IF_NOT_PRESENT, |
569 base::FilePath())); | 569 base::FilePath())); |
570 EXPECT_TRUE(work_item->Do()); | 570 EXPECT_TRUE(work_item->Do()); |
571 | 571 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 backup_file = work_item->backup_path_.path(); | 640 backup_file = work_item->backup_path_.path(); |
641 EXPECT_FALSE(backup_file.empty()); | 641 EXPECT_FALSE(backup_file.empty()); |
642 backup_file = backup_file.AppendASCII("File_To"); | 642 backup_file = backup_file.AppendASCII("File_To"); |
643 | 643 |
644 EXPECT_TRUE(base::PathExists(file_name_from)); | 644 EXPECT_TRUE(base::PathExists(file_name_from)); |
645 EXPECT_TRUE(base::PathExists(file_name_to)); | 645 EXPECT_TRUE(base::PathExists(file_name_to)); |
646 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); | 646 EXPECT_EQ(0, ReadTextFile(file_name_from.value()).compare(text_content_1)); |
647 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); | 647 EXPECT_EQ(0, ReadTextFile(file_name_to.value()).compare(text_content_1)); |
648 // verify the file in used is moved to backup place. | 648 // verify the file in used is moved to backup place. |
649 EXPECT_TRUE(base::PathExists(backup_file)); | 649 EXPECT_TRUE(base::PathExists(backup_file)); |
650 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file)); | 650 EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file)); |
651 } | 651 } |
652 | 652 |
653 // verify the file in used should be still at the backup place. | 653 // verify the file in used should be still at the backup place. |
654 EXPECT_TRUE(base::PathExists(backup_file)); | 654 EXPECT_TRUE(base::PathExists(backup_file)); |
655 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, backup_file)); | 655 EXPECT_TRUE(base::ContentsEqual(exe_full_path, backup_file)); |
656 | 656 |
657 TerminateProcess(pi.hProcess, 0); | 657 TerminateProcess(pi.hProcess, 0); |
658 // make sure the handle is closed. | 658 // make sure the handle is closed. |
659 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 659 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
660 CloseHandle(pi.hProcess); | 660 CloseHandle(pi.hProcess); |
661 CloseHandle(pi.hThread); | 661 CloseHandle(pi.hThread); |
662 } | 662 } |
663 | 663 |
664 // Copy a tree from source to destination. | 664 // Copy a tree from source to destination. |
665 // Flaky, http://crbug.com/59784. | 665 // Flaky, http://crbug.com/59784. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 | 704 |
705 EXPECT_TRUE(work_item->Do()); | 705 EXPECT_TRUE(work_item->Do()); |
706 } | 706 } |
707 | 707 |
708 base::FilePath file_name_to_1(dir_name_to); | 708 base::FilePath file_name_to_1(dir_name_to); |
709 file_name_to_1 = file_name_to_1.AppendASCII("1"); | 709 file_name_to_1 = file_name_to_1.AppendASCII("1"); |
710 file_name_to_1 = file_name_to_1.AppendASCII("File_1.txt"); | 710 file_name_to_1 = file_name_to_1.AppendASCII("File_1.txt"); |
711 EXPECT_TRUE(base::PathExists(file_name_to_1)); | 711 EXPECT_TRUE(base::PathExists(file_name_to_1)); |
712 VLOG(1) << "compare " << file_name_from_1.value() | 712 VLOG(1) << "compare " << file_name_from_1.value() |
713 << " and " << file_name_to_1.value(); | 713 << " and " << file_name_to_1.value(); |
714 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_1, file_name_to_1)); | 714 EXPECT_TRUE(base::ContentsEqual(file_name_from_1, file_name_to_1)); |
715 | 715 |
716 base::FilePath file_name_to_2(dir_name_to); | 716 base::FilePath file_name_to_2(dir_name_to); |
717 file_name_to_2 = file_name_to_2.AppendASCII("2"); | 717 file_name_to_2 = file_name_to_2.AppendASCII("2"); |
718 file_name_to_2 = file_name_to_2.AppendASCII("File_2.txt"); | 718 file_name_to_2 = file_name_to_2.AppendASCII("File_2.txt"); |
719 EXPECT_TRUE(base::PathExists(file_name_to_2)); | 719 EXPECT_TRUE(base::PathExists(file_name_to_2)); |
720 VLOG(1) << "compare " << file_name_from_2.value() | 720 VLOG(1) << "compare " << file_name_from_2.value() |
721 << " and " << file_name_to_2.value(); | 721 << " and " << file_name_to_2.value(); |
722 EXPECT_TRUE(file_util::ContentsEqual(file_name_from_2, file_name_to_2)); | 722 EXPECT_TRUE(base::ContentsEqual(file_name_from_2, file_name_to_2)); |
723 } | 723 } |
OLD | NEW |