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/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <aclapi.h> | 9 #include <aclapi.h> |
10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 | 598 |
599 // Verify that creating a symlink is caught. | 599 // Verify that creating a symlink is caught. |
600 TEST_F(FilePathWatcherTest, CreateLink) { | 600 TEST_F(FilePathWatcherTest, CreateLink) { |
601 FilePathWatcher watcher; | 601 FilePathWatcher watcher; |
602 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 602 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
603 // Note that we are watching the symlink | 603 // Note that we are watching the symlink |
604 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 604 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
605 | 605 |
606 // Now make sure we get notified if the link is created. | 606 // Now make sure we get notified if the link is created. |
607 // Note that test_file() doesn't have to exist. | 607 // Note that test_file() doesn't have to exist. |
608 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 608 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
609 ASSERT_TRUE(WaitForEvents()); | 609 ASSERT_TRUE(WaitForEvents()); |
610 DeleteDelegateOnFileThread(delegate.release()); | 610 DeleteDelegateOnFileThread(delegate.release()); |
611 } | 611 } |
612 | 612 |
613 // Verify that deleting a symlink is caught. | 613 // Verify that deleting a symlink is caught. |
614 TEST_F(FilePathWatcherTest, DeleteLink) { | 614 TEST_F(FilePathWatcherTest, DeleteLink) { |
615 // Unfortunately this test case only works if the link target exists. | 615 // Unfortunately this test case only works if the link target exists. |
616 // TODO(craig) fix this as part of crbug.com/91561. | 616 // TODO(craig) fix this as part of crbug.com/91561. |
617 ASSERT_TRUE(WriteFile(test_file(), "content")); | 617 ASSERT_TRUE(WriteFile(test_file(), "content")); |
618 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 618 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
619 FilePathWatcher watcher; | 619 FilePathWatcher watcher; |
620 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 620 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
621 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 621 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
622 | 622 |
623 // Now make sure we get notified if the link is deleted. | 623 // Now make sure we get notified if the link is deleted. |
624 ASSERT_TRUE(base::DeleteFile(test_link(), false)); | 624 ASSERT_TRUE(base::DeleteFile(test_link(), false)); |
625 ASSERT_TRUE(WaitForEvents()); | 625 ASSERT_TRUE(WaitForEvents()); |
626 DeleteDelegateOnFileThread(delegate.release()); | 626 DeleteDelegateOnFileThread(delegate.release()); |
627 } | 627 } |
628 | 628 |
629 // Verify that modifying a target file that a link is pointing to | 629 // Verify that modifying a target file that a link is pointing to |
630 // when we are watching the link is caught. | 630 // when we are watching the link is caught. |
631 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { | 631 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { |
632 ASSERT_TRUE(WriteFile(test_file(), "content")); | 632 ASSERT_TRUE(WriteFile(test_file(), "content")); |
633 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 633 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
634 FilePathWatcher watcher; | 634 FilePathWatcher watcher; |
635 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 635 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
636 // Note that we are watching the symlink. | 636 // Note that we are watching the symlink. |
637 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 637 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
638 | 638 |
639 // Now make sure we get notified if the file is modified. | 639 // Now make sure we get notified if the file is modified. |
640 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 640 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
641 ASSERT_TRUE(WaitForEvents()); | 641 ASSERT_TRUE(WaitForEvents()); |
642 DeleteDelegateOnFileThread(delegate.release()); | 642 DeleteDelegateOnFileThread(delegate.release()); |
643 } | 643 } |
644 | 644 |
645 // Verify that creating a target file that a link is pointing to | 645 // Verify that creating a target file that a link is pointing to |
646 // when we are watching the link is caught. | 646 // when we are watching the link is caught. |
647 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { | 647 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { |
648 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 648 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
649 FilePathWatcher watcher; | 649 FilePathWatcher watcher; |
650 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 650 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
651 // Note that we are watching the symlink. | 651 // Note that we are watching the symlink. |
652 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 652 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
653 | 653 |
654 // Now make sure we get notified if the target file is created. | 654 // Now make sure we get notified if the target file is created. |
655 ASSERT_TRUE(WriteFile(test_file(), "content")); | 655 ASSERT_TRUE(WriteFile(test_file(), "content")); |
656 ASSERT_TRUE(WaitForEvents()); | 656 ASSERT_TRUE(WaitForEvents()); |
657 DeleteDelegateOnFileThread(delegate.release()); | 657 DeleteDelegateOnFileThread(delegate.release()); |
658 } | 658 } |
659 | 659 |
660 // Verify that deleting a target file that a link is pointing to | 660 // Verify that deleting a target file that a link is pointing to |
661 // when we are watching the link is caught. | 661 // when we are watching the link is caught. |
662 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { | 662 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { |
663 ASSERT_TRUE(WriteFile(test_file(), "content")); | 663 ASSERT_TRUE(WriteFile(test_file(), "content")); |
664 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 664 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
665 FilePathWatcher watcher; | 665 FilePathWatcher watcher; |
666 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 666 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
667 // Note that we are watching the symlink. | 667 // Note that we are watching the symlink. |
668 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 668 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
669 | 669 |
670 // Now make sure we get notified if the target file is deleted. | 670 // Now make sure we get notified if the target file is deleted. |
671 ASSERT_TRUE(base::DeleteFile(test_file(), false)); | 671 ASSERT_TRUE(base::DeleteFile(test_file(), false)); |
672 ASSERT_TRUE(WaitForEvents()); | 672 ASSERT_TRUE(WaitForEvents()); |
673 DeleteDelegateOnFileThread(delegate.release()); | 673 DeleteDelegateOnFileThread(delegate.release()); |
674 } | 674 } |
675 | 675 |
676 // Verify that watching a file whose parent directory is a link that | 676 // Verify that watching a file whose parent directory is a link that |
677 // doesn't exist yet works if the symlink is created eventually. | 677 // doesn't exist yet works if the symlink is created eventually. |
678 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { | 678 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { |
679 FilePathWatcher watcher; | 679 FilePathWatcher watcher; |
680 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 680 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
681 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 681 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
682 FilePath file(dir.AppendASCII("file")); | 682 FilePath file(dir.AppendASCII("file")); |
683 FilePath linkfile(link_dir.AppendASCII("file")); | 683 FilePath linkfile(link_dir.AppendASCII("file")); |
684 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 684 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
685 // dir/file should exist. | 685 // dir/file should exist. |
686 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 686 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
687 ASSERT_TRUE(WriteFile(file, "content")); | 687 ASSERT_TRUE(WriteFile(file, "content")); |
688 // Note that we are watching dir.lnk/file which doesn't exist yet. | 688 // Note that we are watching dir.lnk/file which doesn't exist yet. |
689 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 689 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
690 | 690 |
691 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 691 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
692 VLOG(1) << "Waiting for link creation"; | 692 VLOG(1) << "Waiting for link creation"; |
693 ASSERT_TRUE(WaitForEvents()); | 693 ASSERT_TRUE(WaitForEvents()); |
694 | 694 |
695 ASSERT_TRUE(WriteFile(file, "content v2")); | 695 ASSERT_TRUE(WriteFile(file, "content v2")); |
696 VLOG(1) << "Waiting for file change"; | 696 VLOG(1) << "Waiting for file change"; |
697 ASSERT_TRUE(WaitForEvents()); | 697 ASSERT_TRUE(WaitForEvents()); |
698 | 698 |
699 ASSERT_TRUE(base::DeleteFile(file, false)); | 699 ASSERT_TRUE(base::DeleteFile(file, false)); |
700 VLOG(1) << "Waiting for file deletion"; | 700 VLOG(1) << "Waiting for file deletion"; |
701 ASSERT_TRUE(WaitForEvents()); | 701 ASSERT_TRUE(WaitForEvents()); |
702 DeleteDelegateOnFileThread(delegate.release()); | 702 DeleteDelegateOnFileThread(delegate.release()); |
703 } | 703 } |
704 | 704 |
705 // Verify that watching a file whose parent directory is a | 705 // Verify that watching a file whose parent directory is a |
706 // dangling symlink works if the directory is created eventually. | 706 // dangling symlink works if the directory is created eventually. |
707 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { | 707 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { |
708 FilePathWatcher watcher; | 708 FilePathWatcher watcher; |
709 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 709 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
710 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 710 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
711 FilePath file(dir.AppendASCII("file")); | 711 FilePath file(dir.AppendASCII("file")); |
712 FilePath linkfile(link_dir.AppendASCII("file")); | 712 FilePath linkfile(link_dir.AppendASCII("file")); |
713 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 713 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
714 // Now create the link from dir.lnk pointing to dir but | 714 // Now create the link from dir.lnk pointing to dir but |
715 // neither dir nor dir/file exist yet. | 715 // neither dir nor dir/file exist yet. |
716 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 716 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
717 // Note that we are watching dir.lnk/file. | 717 // Note that we are watching dir.lnk/file. |
718 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 718 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
719 | 719 |
720 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 720 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
721 ASSERT_TRUE(WriteFile(file, "content")); | 721 ASSERT_TRUE(WriteFile(file, "content")); |
722 VLOG(1) << "Waiting for dir/file creation"; | 722 VLOG(1) << "Waiting for dir/file creation"; |
723 ASSERT_TRUE(WaitForEvents()); | 723 ASSERT_TRUE(WaitForEvents()); |
724 | 724 |
725 ASSERT_TRUE(WriteFile(file, "content v2")); | 725 ASSERT_TRUE(WriteFile(file, "content v2")); |
726 VLOG(1) << "Waiting for file change"; | 726 VLOG(1) << "Waiting for file change"; |
727 ASSERT_TRUE(WaitForEvents()); | 727 ASSERT_TRUE(WaitForEvents()); |
728 | 728 |
729 ASSERT_TRUE(base::DeleteFile(file, false)); | 729 ASSERT_TRUE(base::DeleteFile(file, false)); |
730 VLOG(1) << "Waiting for file deletion"; | 730 VLOG(1) << "Waiting for file deletion"; |
731 ASSERT_TRUE(WaitForEvents()); | 731 ASSERT_TRUE(WaitForEvents()); |
732 DeleteDelegateOnFileThread(delegate.release()); | 732 DeleteDelegateOnFileThread(delegate.release()); |
733 } | 733 } |
734 | 734 |
735 // Verify that watching a file with a symlink on the path | 735 // Verify that watching a file with a symlink on the path |
736 // to the file works. | 736 // to the file works. |
737 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { | 737 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { |
738 FilePathWatcher watcher; | 738 FilePathWatcher watcher; |
739 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 739 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
740 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 740 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
741 FilePath file(dir.AppendASCII("file")); | 741 FilePath file(dir.AppendASCII("file")); |
742 FilePath linkfile(link_dir.AppendASCII("file")); | 742 FilePath linkfile(link_dir.AppendASCII("file")); |
743 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 743 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
744 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 744 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
745 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 745 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
746 // Note that we are watching dir.lnk/file but the file doesn't exist yet. | 746 // Note that we are watching dir.lnk/file but the file doesn't exist yet. |
747 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 747 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
748 | 748 |
749 ASSERT_TRUE(WriteFile(file, "content")); | 749 ASSERT_TRUE(WriteFile(file, "content")); |
750 VLOG(1) << "Waiting for file creation"; | 750 VLOG(1) << "Waiting for file creation"; |
751 ASSERT_TRUE(WaitForEvents()); | 751 ASSERT_TRUE(WaitForEvents()); |
752 | 752 |
753 ASSERT_TRUE(WriteFile(file, "content v2")); | 753 ASSERT_TRUE(WriteFile(file, "content v2")); |
754 VLOG(1) << "Waiting for file change"; | 754 VLOG(1) << "Waiting for file change"; |
755 ASSERT_TRUE(WaitForEvents()); | 755 ASSERT_TRUE(WaitForEvents()); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 834 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
835 ASSERT_TRUE(WaitForEvents()); | 835 ASSERT_TRUE(WaitForEvents()); |
836 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 836 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
837 DeleteDelegateOnFileThread(delegate.release()); | 837 DeleteDelegateOnFileThread(delegate.release()); |
838 } | 838 } |
839 | 839 |
840 #endif // OS_MACOSX | 840 #endif // OS_MACOSX |
841 } // namespace | 841 } // namespace |
842 | 842 |
843 } // namespace base | 843 } // namespace base |
OLD | NEW |