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
3 // found in the LICENSE file.
5 #include "base/files/file_path_watcher.h"
10 #elif defined(OS_POSIX)
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/bind_helpers.h"
19 #include "base/compiler_specific.h"
20 #include "base/file_util.h"
21 #include "base/files/file_path.h"
22 #include "base/files/scoped_temp_dir.h"
23 #include "base/message_loop/message_loop.h"
24 #include "base/message_loop/message_loop_proxy.h"
25 #include "base/run_loop.h"
26 #include "base/stl_util.h"
27 #include "base/strings/stringprintf.h"
28 #include "base/synchronization/waitable_event.h"
29 #include "base/test/test_file_util.h"
30 #include "base/test/test_timeouts.h"
31 #include "base/threading/thread.h"
32 #include "testing/gtest/include/gtest/gtest.h"
40 // Aggregates notifications from the test delegates and breaks the message loop
41 // the test thread is waiting on once they all came in.
42 class NotificationCollector
43 : public base::RefCountedThreadSafe
<NotificationCollector
> {
45 NotificationCollector()
46 : loop_(base::MessageLoopProxy::current()) {}
48 // Called from the file thread by the delegates.
49 void OnChange(TestDelegate
* delegate
) {
50 loop_
->PostTask(FROM_HERE
,
51 base::Bind(&NotificationCollector::RecordChange
, this,
52 base::Unretained(delegate
)));
55 void Register(TestDelegate
* delegate
) {
56 delegates_
.insert(delegate
);
64 return signaled_
== delegates_
;
68 friend class base::RefCountedThreadSafe
<NotificationCollector
>;
69 ~NotificationCollector() {}
71 void RecordChange(TestDelegate
* delegate
) {
72 // Warning: |delegate| is Unretained. Do not dereference.
73 ASSERT_TRUE(loop_
->BelongsToCurrentThread());
74 ASSERT_TRUE(delegates_
.count(delegate
));
75 signaled_
.insert(delegate
);
77 // Check whether all delegates have been signaled.
78 if (signaled_
== delegates_
)
79 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitWhenIdleClosure());
82 // Set of registered delegates.
83 std::set
<TestDelegate
*> delegates_
;
85 // Set of signaled delegates.
86 std::set
<TestDelegate
*> signaled_
;
88 // The loop we should break after all delegates signaled.
89 scoped_refptr
<base::MessageLoopProxy
> loop_
;
92 class TestDelegateBase
: public SupportsWeakPtr
<TestDelegateBase
> {
95 virtual ~TestDelegateBase() {}
97 virtual void OnFileChanged(const FilePath
& path
, bool error
) = 0;
100 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase
);
103 // A mock class for testing. Gmock is not appropriate because it is not
104 // thread-safe for setting expectations. Thus the test code cannot safely
105 // reset expectations while the file watcher is running.
106 // Instead, TestDelegate gets the notifications from FilePathWatcher and uses
107 // NotificationCollector to aggregate the results.
108 class TestDelegate
: public TestDelegateBase
{
110 explicit TestDelegate(NotificationCollector
* collector
)
111 : collector_(collector
) {
112 collector_
->Register(this);
114 virtual ~TestDelegate() {}
116 virtual void OnFileChanged(const FilePath
& path
, bool error
) OVERRIDE
{
118 ADD_FAILURE() << "Error " << path
.value();
120 collector_
->OnChange(this);
124 scoped_refptr
<NotificationCollector
> collector_
;
126 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
129 void SetupWatchCallback(const FilePath
& target
,
130 FilePathWatcher
* watcher
,
131 TestDelegateBase
* delegate
,
132 bool recursive_watch
,
134 base::WaitableEvent
* completion
) {
135 *result
= watcher
->Watch(target
, recursive_watch
,
136 base::Bind(&TestDelegateBase::OnFileChanged
,
137 delegate
->AsWeakPtr()));
138 completion
->Signal();
141 class FilePathWatcherTest
: public testing::Test
{
143 FilePathWatcherTest()
144 : file_thread_("FilePathWatcherTest") {}
146 virtual ~FilePathWatcherTest() {}
149 virtual void SetUp() OVERRIDE
{
150 // Create a separate file thread in order to test proper thread usage.
151 base::Thread::Options
options(MessageLoop::TYPE_IO
, 0);
152 ASSERT_TRUE(file_thread_
.StartWithOptions(options
));
153 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
154 collector_
= new NotificationCollector();
157 virtual void TearDown() OVERRIDE
{
158 RunLoop().RunUntilIdle();
161 void DeleteDelegateOnFileThread(TestDelegate
* delegate
) {
162 file_thread_
.message_loop_proxy()->DeleteSoon(FROM_HERE
, delegate
);
165 FilePath
test_file() {
166 return temp_dir_
.path().AppendASCII("FilePathWatcherTest");
169 FilePath
test_link() {
170 return temp_dir_
.path().AppendASCII("FilePathWatcherTest.lnk");
173 // Write |content| to |file|. Returns true on success.
174 bool WriteFile(const FilePath
& file
, const std::string
& content
) {
175 int write_size
= ::base::WriteFile(file
, content
.c_str(), content
.length());
176 return write_size
== static_cast<int>(content
.length());
179 bool SetupWatch(const FilePath
& target
,
180 FilePathWatcher
* watcher
,
181 TestDelegateBase
* delegate
,
182 bool recursive_watch
) WARN_UNUSED_RESULT
;
184 bool WaitForEvents() WARN_UNUSED_RESULT
{
187 return collector_
->Success();
190 NotificationCollector
* collector() { return collector_
.get(); }
193 base::Thread file_thread_
;
194 ScopedTempDir temp_dir_
;
195 scoped_refptr
<NotificationCollector
> collector_
;
197 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest
);
200 bool FilePathWatcherTest::SetupWatch(const FilePath
& target
,
201 FilePathWatcher
* watcher
,
202 TestDelegateBase
* delegate
,
203 bool recursive_watch
) {
204 base::WaitableEvent
completion(false, false);
206 file_thread_
.message_loop_proxy()->PostTask(
208 base::Bind(SetupWatchCallback
,
209 target
, watcher
, delegate
, recursive_watch
, &result
,
215 // Basic test: Create the file and verify that we notice.
216 TEST_F(FilePathWatcherTest
, NewFile
) {
217 FilePathWatcher watcher
;
218 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
219 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
221 ASSERT_TRUE(WriteFile(test_file(), "content"));
222 ASSERT_TRUE(WaitForEvents());
223 DeleteDelegateOnFileThread(delegate
.release());
226 // Verify that modifying the file is caught.
227 TEST_F(FilePathWatcherTest
, ModifiedFile
) {
228 ASSERT_TRUE(WriteFile(test_file(), "content"));
230 FilePathWatcher watcher
;
231 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
232 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
234 // Now make sure we get notified if the file is modified.
235 ASSERT_TRUE(WriteFile(test_file(), "new content"));
236 ASSERT_TRUE(WaitForEvents());
237 DeleteDelegateOnFileThread(delegate
.release());
240 // Verify that moving the file into place is caught.
241 TEST_F(FilePathWatcherTest
, MovedFile
) {
242 FilePath
source_file(temp_dir_
.path().AppendASCII("source"));
243 ASSERT_TRUE(WriteFile(source_file
, "content"));
245 FilePathWatcher watcher
;
246 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
247 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
249 // Now make sure we get notified if the file is modified.
250 ASSERT_TRUE(base::Move(source_file
, test_file()));
251 ASSERT_TRUE(WaitForEvents());
252 DeleteDelegateOnFileThread(delegate
.release());
255 TEST_F(FilePathWatcherTest
, DeletedFile
) {
256 ASSERT_TRUE(WriteFile(test_file(), "content"));
258 FilePathWatcher watcher
;
259 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
260 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
262 // Now make sure we get notified if the file is deleted.
263 base::DeleteFile(test_file(), false);
264 ASSERT_TRUE(WaitForEvents());
265 DeleteDelegateOnFileThread(delegate
.release());
268 // Used by the DeleteDuringNotify test below.
269 // Deletes the FilePathWatcher when it's notified.
270 class Deleter
: public TestDelegateBase
{
272 Deleter(FilePathWatcher
* watcher
, MessageLoop
* loop
)
276 virtual ~Deleter() {}
278 virtual void OnFileChanged(const FilePath
&, bool) OVERRIDE
{
280 loop_
->PostTask(FROM_HERE
, MessageLoop::QuitWhenIdleClosure());
283 FilePathWatcher
* watcher() const { return watcher_
.get(); }
286 scoped_ptr
<FilePathWatcher
> watcher_
;
289 DISALLOW_COPY_AND_ASSIGN(Deleter
);
292 // Verify that deleting a watcher during the callback doesn't crash.
293 TEST_F(FilePathWatcherTest
, DeleteDuringNotify
) {
294 FilePathWatcher
* watcher
= new FilePathWatcher
;
295 // Takes ownership of watcher.
296 scoped_ptr
<Deleter
> deleter(new Deleter(watcher
, &loop_
));
297 ASSERT_TRUE(SetupWatch(test_file(), watcher
, deleter
.get(), false));
299 ASSERT_TRUE(WriteFile(test_file(), "content"));
300 ASSERT_TRUE(WaitForEvents());
302 // We win if we haven't crashed yet.
303 // Might as well double-check it got deleted, too.
304 ASSERT_TRUE(deleter
->watcher() == NULL
);
307 // Verify that deleting the watcher works even if there is a pending
309 // Flaky on MacOS (and ARM linux): http://crbug.com/85930
310 TEST_F(FilePathWatcherTest
, DISABLED_DestroyWithPendingNotification
) {
311 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
312 FilePathWatcher
* watcher
= new FilePathWatcher
;
313 ASSERT_TRUE(SetupWatch(test_file(), watcher
, delegate
.get(), false));
314 ASSERT_TRUE(WriteFile(test_file(), "content"));
315 file_thread_
.message_loop_proxy()->DeleteSoon(FROM_HERE
, watcher
);
316 DeleteDelegateOnFileThread(delegate
.release());
319 TEST_F(FilePathWatcherTest
, MultipleWatchersSingleFile
) {
320 FilePathWatcher watcher1
, watcher2
;
321 scoped_ptr
<TestDelegate
> delegate1(new TestDelegate(collector()));
322 scoped_ptr
<TestDelegate
> delegate2(new TestDelegate(collector()));
323 ASSERT_TRUE(SetupWatch(test_file(), &watcher1
, delegate1
.get(), false));
324 ASSERT_TRUE(SetupWatch(test_file(), &watcher2
, delegate2
.get(), false));
326 ASSERT_TRUE(WriteFile(test_file(), "content"));
327 ASSERT_TRUE(WaitForEvents());
328 DeleteDelegateOnFileThread(delegate1
.release());
329 DeleteDelegateOnFileThread(delegate2
.release());
332 // Verify that watching a file whose parent directory doesn't exist yet works if
333 // the directory and file are created eventually.
334 TEST_F(FilePathWatcherTest
, NonExistentDirectory
) {
335 FilePathWatcher watcher
;
336 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
337 FilePath
file(dir
.AppendASCII("file"));
338 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
339 ASSERT_TRUE(SetupWatch(file
, &watcher
, delegate
.get(), false));
341 ASSERT_TRUE(base::CreateDirectory(dir
));
343 ASSERT_TRUE(WriteFile(file
, "content"));
345 VLOG(1) << "Waiting for file creation";
346 ASSERT_TRUE(WaitForEvents());
348 ASSERT_TRUE(WriteFile(file
, "content v2"));
349 VLOG(1) << "Waiting for file change";
350 ASSERT_TRUE(WaitForEvents());
352 ASSERT_TRUE(base::DeleteFile(file
, false));
353 VLOG(1) << "Waiting for file deletion";
354 ASSERT_TRUE(WaitForEvents());
355 DeleteDelegateOnFileThread(delegate
.release());
358 // Exercises watch reconfiguration for the case that directories on the path
359 // are rapidly created.
360 TEST_F(FilePathWatcherTest
, DirectoryChain
) {
361 FilePath
path(temp_dir_
.path());
362 std::vector
<std::string
> dir_names
;
363 for (int i
= 0; i
< 20; i
++) {
364 std::string
dir(base::StringPrintf("d%d", i
));
365 dir_names
.push_back(dir
);
366 path
= path
.AppendASCII(dir
);
369 FilePathWatcher watcher
;
370 FilePath
file(path
.AppendASCII("file"));
371 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
372 ASSERT_TRUE(SetupWatch(file
, &watcher
, delegate
.get(), false));
374 FilePath
sub_path(temp_dir_
.path());
375 for (std::vector
<std::string
>::const_iterator
d(dir_names
.begin());
376 d
!= dir_names
.end(); ++d
) {
377 sub_path
= sub_path
.AppendASCII(*d
);
378 ASSERT_TRUE(base::CreateDirectory(sub_path
));
380 VLOG(1) << "Create File";
381 ASSERT_TRUE(WriteFile(file
, "content"));
382 VLOG(1) << "Waiting for file creation";
383 ASSERT_TRUE(WaitForEvents());
385 ASSERT_TRUE(WriteFile(file
, "content v2"));
386 VLOG(1) << "Waiting for file modification";
387 ASSERT_TRUE(WaitForEvents());
388 DeleteDelegateOnFileThread(delegate
.release());
391 #if defined(OS_MACOSX)
392 // http://crbug.com/85930
393 #define DisappearingDirectory DISABLED_DisappearingDirectory
395 TEST_F(FilePathWatcherTest
, DisappearingDirectory
) {
396 FilePathWatcher watcher
;
397 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
398 FilePath
file(dir
.AppendASCII("file"));
399 ASSERT_TRUE(base::CreateDirectory(dir
));
400 ASSERT_TRUE(WriteFile(file
, "content"));
401 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
402 ASSERT_TRUE(SetupWatch(file
, &watcher
, delegate
.get(), false));
404 ASSERT_TRUE(base::DeleteFile(dir
, true));
405 ASSERT_TRUE(WaitForEvents());
406 DeleteDelegateOnFileThread(delegate
.release());
409 // Tests that a file that is deleted and reappears is tracked correctly.
410 TEST_F(FilePathWatcherTest
, DeleteAndRecreate
) {
411 ASSERT_TRUE(WriteFile(test_file(), "content"));
412 FilePathWatcher watcher
;
413 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
414 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
416 ASSERT_TRUE(base::DeleteFile(test_file(), false));
417 VLOG(1) << "Waiting for file deletion";
418 ASSERT_TRUE(WaitForEvents());
420 ASSERT_TRUE(WriteFile(test_file(), "content"));
421 VLOG(1) << "Waiting for file creation";
422 ASSERT_TRUE(WaitForEvents());
423 DeleteDelegateOnFileThread(delegate
.release());
426 TEST_F(FilePathWatcherTest
, WatchDirectory
) {
427 FilePathWatcher watcher
;
428 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
429 FilePath
file1(dir
.AppendASCII("file1"));
430 FilePath
file2(dir
.AppendASCII("file2"));
431 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
432 ASSERT_TRUE(SetupWatch(dir
, &watcher
, delegate
.get(), false));
434 ASSERT_TRUE(base::CreateDirectory(dir
));
435 VLOG(1) << "Waiting for directory creation";
436 ASSERT_TRUE(WaitForEvents());
438 ASSERT_TRUE(WriteFile(file1
, "content"));
439 VLOG(1) << "Waiting for file1 creation";
440 ASSERT_TRUE(WaitForEvents());
442 #if !defined(OS_MACOSX)
443 // Mac implementation does not detect files modified in a directory.
444 ASSERT_TRUE(WriteFile(file1
, "content v2"));
445 VLOG(1) << "Waiting for file1 modification";
446 ASSERT_TRUE(WaitForEvents());
449 ASSERT_TRUE(base::DeleteFile(file1
, false));
450 VLOG(1) << "Waiting for file1 deletion";
451 ASSERT_TRUE(WaitForEvents());
453 ASSERT_TRUE(WriteFile(file2
, "content"));
454 VLOG(1) << "Waiting for file2 creation";
455 ASSERT_TRUE(WaitForEvents());
456 DeleteDelegateOnFileThread(delegate
.release());
459 TEST_F(FilePathWatcherTest
, MoveParent
) {
460 FilePathWatcher file_watcher
;
461 FilePathWatcher subdir_watcher
;
462 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
463 FilePath
dest(temp_dir_
.path().AppendASCII("dest"));
464 FilePath
subdir(dir
.AppendASCII("subdir"));
465 FilePath
file(subdir
.AppendASCII("file"));
466 scoped_ptr
<TestDelegate
> file_delegate(new TestDelegate(collector()));
467 ASSERT_TRUE(SetupWatch(file
, &file_watcher
, file_delegate
.get(), false));
468 scoped_ptr
<TestDelegate
> subdir_delegate(new TestDelegate(collector()));
469 ASSERT_TRUE(SetupWatch(subdir
, &subdir_watcher
, subdir_delegate
.get(),
472 // Setup a directory hierarchy.
473 ASSERT_TRUE(base::CreateDirectory(subdir
));
474 ASSERT_TRUE(WriteFile(file
, "content"));
475 VLOG(1) << "Waiting for file creation";
476 ASSERT_TRUE(WaitForEvents());
478 // Move the parent directory.
479 base::Move(dir
, dest
);
480 VLOG(1) << "Waiting for directory move";
481 ASSERT_TRUE(WaitForEvents());
482 DeleteDelegateOnFileThread(file_delegate
.release());
483 DeleteDelegateOnFileThread(subdir_delegate
.release());
486 #if defined(OS_WIN) || defined(OS_LINUX)
487 TEST_F(FilePathWatcherTest
, RecursiveWatch
) {
488 FilePathWatcher watcher
;
489 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
490 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
491 ASSERT_TRUE(SetupWatch(dir
, &watcher
, delegate
.get(), true));
493 // Main directory("dir") creation.
494 ASSERT_TRUE(base::CreateDirectory(dir
));
495 ASSERT_TRUE(WaitForEvents());
497 // Create "$dir/file1".
498 FilePath
file1(dir
.AppendASCII("file1"));
499 ASSERT_TRUE(WriteFile(file1
, "content"));
500 ASSERT_TRUE(WaitForEvents());
502 // Create "$dir/subdir".
503 FilePath
subdir(dir
.AppendASCII("subdir"));
504 ASSERT_TRUE(base::CreateDirectory(subdir
));
505 ASSERT_TRUE(WaitForEvents());
507 // Create "$dir/subdir/subdir_file1".
508 FilePath
subdir_file1(subdir
.AppendASCII("subdir_file1"));
509 ASSERT_TRUE(WriteFile(subdir_file1
, "content"));
510 ASSERT_TRUE(WaitForEvents());
512 // Create "$dir/subdir/subdir_child_dir".
513 FilePath
subdir_child_dir(subdir
.AppendASCII("subdir_child_dir"));
514 ASSERT_TRUE(base::CreateDirectory(subdir_child_dir
));
515 ASSERT_TRUE(WaitForEvents());
517 // Create "$dir/subdir/subdir_child_dir/child_dir_file1".
518 FilePath
child_dir_file1(subdir_child_dir
.AppendASCII("child_dir_file1"));
519 ASSERT_TRUE(WriteFile(child_dir_file1
, "content v2"));
520 ASSERT_TRUE(WaitForEvents());
522 // Write into "$dir/subdir/subdir_child_dir/child_dir_file1".
523 ASSERT_TRUE(WriteFile(child_dir_file1
, "content"));
524 ASSERT_TRUE(WaitForEvents());
526 // Modify "$dir/subdir/subdir_child_dir/child_dir_file1" attributes.
527 ASSERT_TRUE(file_util::MakeFileUnreadable(child_dir_file1
));
528 ASSERT_TRUE(WaitForEvents());
530 // Delete "$dir/subdir/subdir_file1".
531 ASSERT_TRUE(base::DeleteFile(subdir_file1
, false));
532 ASSERT_TRUE(WaitForEvents());
534 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1".
535 ASSERT_TRUE(base::DeleteFile(child_dir_file1
, false));
536 ASSERT_TRUE(WaitForEvents());
537 DeleteDelegateOnFileThread(delegate
.release());
540 TEST_F(FilePathWatcherTest
, RecursiveWatch
) {
541 FilePathWatcher watcher
;
542 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
543 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
544 // Only Windows/Linux support recursive watching. Other implementations
545 // should simply fail.
546 ASSERT_FALSE(SetupWatch(dir
, &watcher
, delegate
.get(), true));
547 DeleteDelegateOnFileThread(delegate
.release());
551 TEST_F(FilePathWatcherTest
, MoveChild
) {
552 FilePathWatcher file_watcher
;
553 FilePathWatcher subdir_watcher
;
554 FilePath
source_dir(temp_dir_
.path().AppendASCII("source"));
555 FilePath
source_subdir(source_dir
.AppendASCII("subdir"));
556 FilePath
source_file(source_subdir
.AppendASCII("file"));
557 FilePath
dest_dir(temp_dir_
.path().AppendASCII("dest"));
558 FilePath
dest_subdir(dest_dir
.AppendASCII("subdir"));
559 FilePath
dest_file(dest_subdir
.AppendASCII("file"));
561 // Setup a directory hierarchy.
562 ASSERT_TRUE(base::CreateDirectory(source_subdir
));
563 ASSERT_TRUE(WriteFile(source_file
, "content"));
565 scoped_ptr
<TestDelegate
> file_delegate(new TestDelegate(collector()));
566 ASSERT_TRUE(SetupWatch(dest_file
, &file_watcher
, file_delegate
.get(), false));
567 scoped_ptr
<TestDelegate
> subdir_delegate(new TestDelegate(collector()));
568 ASSERT_TRUE(SetupWatch(dest_subdir
, &subdir_watcher
, subdir_delegate
.get(),
571 // Move the directory into place, s.t. the watched file appears.
572 ASSERT_TRUE(base::Move(source_dir
, dest_dir
));
573 ASSERT_TRUE(WaitForEvents());
574 DeleteDelegateOnFileThread(file_delegate
.release());
575 DeleteDelegateOnFileThread(subdir_delegate
.release());
578 // Verify that changing attributes on a file is caught
579 TEST_F(FilePathWatcherTest
, FileAttributesChanged
) {
580 ASSERT_TRUE(WriteFile(test_file(), "content"));
581 FilePathWatcher watcher
;
582 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
583 ASSERT_TRUE(SetupWatch(test_file(), &watcher
, delegate
.get(), false));
585 // Now make sure we get notified if the file is modified.
586 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
587 ASSERT_TRUE(WaitForEvents());
588 DeleteDelegateOnFileThread(delegate
.release());
591 #if defined(OS_LINUX)
593 // Verify that creating a symlink is caught.
594 TEST_F(FilePathWatcherTest
, CreateLink
) {
595 FilePathWatcher watcher
;
596 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
597 // Note that we are watching the symlink
598 ASSERT_TRUE(SetupWatch(test_link(), &watcher
, delegate
.get(), false));
600 // Now make sure we get notified if the link is created.
601 // Note that test_file() doesn't have to exist.
602 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
603 ASSERT_TRUE(WaitForEvents());
604 DeleteDelegateOnFileThread(delegate
.release());
607 // Verify that deleting a symlink is caught.
608 TEST_F(FilePathWatcherTest
, DeleteLink
) {
609 // Unfortunately this test case only works if the link target exists.
610 // TODO(craig) fix this as part of crbug.com/91561.
611 ASSERT_TRUE(WriteFile(test_file(), "content"));
612 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
613 FilePathWatcher watcher
;
614 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
615 ASSERT_TRUE(SetupWatch(test_link(), &watcher
, delegate
.get(), false));
617 // Now make sure we get notified if the link is deleted.
618 ASSERT_TRUE(base::DeleteFile(test_link(), false));
619 ASSERT_TRUE(WaitForEvents());
620 DeleteDelegateOnFileThread(delegate
.release());
623 // Verify that modifying a target file that a link is pointing to
624 // when we are watching the link is caught.
625 TEST_F(FilePathWatcherTest
, ModifiedLinkedFile
) {
626 ASSERT_TRUE(WriteFile(test_file(), "content"));
627 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
628 FilePathWatcher watcher
;
629 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
630 // Note that we are watching the symlink.
631 ASSERT_TRUE(SetupWatch(test_link(), &watcher
, delegate
.get(), false));
633 // Now make sure we get notified if the file is modified.
634 ASSERT_TRUE(WriteFile(test_file(), "new content"));
635 ASSERT_TRUE(WaitForEvents());
636 DeleteDelegateOnFileThread(delegate
.release());
639 // Verify that creating a target file that a link is pointing to
640 // when we are watching the link is caught.
641 TEST_F(FilePathWatcherTest
, CreateTargetLinkedFile
) {
642 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
643 FilePathWatcher watcher
;
644 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
645 // Note that we are watching the symlink.
646 ASSERT_TRUE(SetupWatch(test_link(), &watcher
, delegate
.get(), false));
648 // Now make sure we get notified if the target file is created.
649 ASSERT_TRUE(WriteFile(test_file(), "content"));
650 ASSERT_TRUE(WaitForEvents());
651 DeleteDelegateOnFileThread(delegate
.release());
654 // Verify that deleting a target file that a link is pointing to
655 // when we are watching the link is caught.
656 TEST_F(FilePathWatcherTest
, DeleteTargetLinkedFile
) {
657 ASSERT_TRUE(WriteFile(test_file(), "content"));
658 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
659 FilePathWatcher watcher
;
660 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
661 // Note that we are watching the symlink.
662 ASSERT_TRUE(SetupWatch(test_link(), &watcher
, delegate
.get(), false));
664 // Now make sure we get notified if the target file is deleted.
665 ASSERT_TRUE(base::DeleteFile(test_file(), false));
666 ASSERT_TRUE(WaitForEvents());
667 DeleteDelegateOnFileThread(delegate
.release());
670 // Verify that watching a file whose parent directory is a link that
671 // doesn't exist yet works if the symlink is created eventually.
672 TEST_F(FilePathWatcherTest
, LinkedDirectoryPart1
) {
673 FilePathWatcher watcher
;
674 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
675 FilePath
link_dir(temp_dir_
.path().AppendASCII("dir.lnk"));
676 FilePath
file(dir
.AppendASCII("file"));
677 FilePath
linkfile(link_dir
.AppendASCII("file"));
678 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
679 // dir/file should exist.
680 ASSERT_TRUE(base::CreateDirectory(dir
));
681 ASSERT_TRUE(WriteFile(file
, "content"));
682 // Note that we are watching dir.lnk/file which doesn't exist yet.
683 ASSERT_TRUE(SetupWatch(linkfile
, &watcher
, delegate
.get(), false));
685 ASSERT_TRUE(CreateSymbolicLink(dir
, link_dir
));
686 VLOG(1) << "Waiting for link creation";
687 ASSERT_TRUE(WaitForEvents());
689 ASSERT_TRUE(WriteFile(file
, "content v2"));
690 VLOG(1) << "Waiting for file change";
691 ASSERT_TRUE(WaitForEvents());
693 ASSERT_TRUE(base::DeleteFile(file
, false));
694 VLOG(1) << "Waiting for file deletion";
695 ASSERT_TRUE(WaitForEvents());
696 DeleteDelegateOnFileThread(delegate
.release());
699 // Verify that watching a file whose parent directory is a
700 // dangling symlink works if the directory is created eventually.
701 TEST_F(FilePathWatcherTest
, LinkedDirectoryPart2
) {
702 FilePathWatcher watcher
;
703 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
704 FilePath
link_dir(temp_dir_
.path().AppendASCII("dir.lnk"));
705 FilePath
file(dir
.AppendASCII("file"));
706 FilePath
linkfile(link_dir
.AppendASCII("file"));
707 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
708 // Now create the link from dir.lnk pointing to dir but
709 // neither dir nor dir/file exist yet.
710 ASSERT_TRUE(CreateSymbolicLink(dir
, link_dir
));
711 // Note that we are watching dir.lnk/file.
712 ASSERT_TRUE(SetupWatch(linkfile
, &watcher
, delegate
.get(), false));
714 ASSERT_TRUE(base::CreateDirectory(dir
));
715 ASSERT_TRUE(WriteFile(file
, "content"));
716 VLOG(1) << "Waiting for dir/file creation";
717 ASSERT_TRUE(WaitForEvents());
719 ASSERT_TRUE(WriteFile(file
, "content v2"));
720 VLOG(1) << "Waiting for file change";
721 ASSERT_TRUE(WaitForEvents());
723 ASSERT_TRUE(base::DeleteFile(file
, false));
724 VLOG(1) << "Waiting for file deletion";
725 ASSERT_TRUE(WaitForEvents());
726 DeleteDelegateOnFileThread(delegate
.release());
729 // Verify that watching a file with a symlink on the path
730 // to the file works.
731 TEST_F(FilePathWatcherTest
, LinkedDirectoryPart3
) {
732 FilePathWatcher watcher
;
733 FilePath
dir(temp_dir_
.path().AppendASCII("dir"));
734 FilePath
link_dir(temp_dir_
.path().AppendASCII("dir.lnk"));
735 FilePath
file(dir
.AppendASCII("file"));
736 FilePath
linkfile(link_dir
.AppendASCII("file"));
737 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
738 ASSERT_TRUE(base::CreateDirectory(dir
));
739 ASSERT_TRUE(CreateSymbolicLink(dir
, link_dir
));
740 // Note that we are watching dir.lnk/file but the file doesn't exist yet.
741 ASSERT_TRUE(SetupWatch(linkfile
, &watcher
, delegate
.get(), false));
743 ASSERT_TRUE(WriteFile(file
, "content"));
744 VLOG(1) << "Waiting for file creation";
745 ASSERT_TRUE(WaitForEvents());
747 ASSERT_TRUE(WriteFile(file
, "content v2"));
748 VLOG(1) << "Waiting for file change";
749 ASSERT_TRUE(WaitForEvents());
751 ASSERT_TRUE(base::DeleteFile(file
, false));
752 VLOG(1) << "Waiting for file deletion";
753 ASSERT_TRUE(WaitForEvents());
754 DeleteDelegateOnFileThread(delegate
.release());
765 #if defined(OS_MACOSX)
766 bool ChangeFilePermissions(const FilePath
& path
, Permission perm
, bool allow
) {
767 struct stat stat_buf
;
769 if (stat(path
.value().c_str(), &stat_buf
) != 0)
775 mode
= S_IRUSR
| S_IRGRP
| S_IROTH
;
778 mode
= S_IWUSR
| S_IWGRP
| S_IWOTH
;
781 mode
= S_IXUSR
| S_IXGRP
| S_IXOTH
;
784 ADD_FAILURE() << "unknown perm " << perm
;
788 stat_buf
.st_mode
|= mode
;
790 stat_buf
.st_mode
&= ~mode
;
792 return chmod(path
.value().c_str(), stat_buf
.st_mode
) == 0;
794 #endif // defined(OS_MACOSX)
796 #if defined(OS_MACOSX)
797 // Linux implementation of FilePathWatcher doesn't catch attribute changes.
798 // http://crbug.com/78043
799 // Windows implementation of FilePathWatcher catches attribute changes that
800 // don't affect the path being watched.
801 // http://crbug.com/78045
803 // Verify that changing attributes on a directory works.
804 TEST_F(FilePathWatcherTest
, DirAttributesChanged
) {
805 FilePath
test_dir1(temp_dir_
.path().AppendASCII("DirAttributesChangedDir1"));
806 FilePath
test_dir2(test_dir1
.AppendASCII("DirAttributesChangedDir2"));
807 FilePath
test_file(test_dir2
.AppendASCII("DirAttributesChangedFile"));
808 // Setup a directory hierarchy.
809 ASSERT_TRUE(base::CreateDirectory(test_dir1
));
810 ASSERT_TRUE(base::CreateDirectory(test_dir2
));
811 ASSERT_TRUE(WriteFile(test_file
, "content"));
813 FilePathWatcher watcher
;
814 scoped_ptr
<TestDelegate
> delegate(new TestDelegate(collector()));
815 ASSERT_TRUE(SetupWatch(test_file
, &watcher
, delegate
.get(), false));
817 // We should not get notified in this case as it hasn't affected our ability
818 // to access the file.
819 ASSERT_TRUE(ChangeFilePermissions(test_dir1
, Read
, false));
820 loop_
.PostDelayedTask(FROM_HERE
,
821 MessageLoop::QuitWhenIdleClosure(),
822 TestTimeouts::tiny_timeout());
823 ASSERT_FALSE(WaitForEvents());
824 ASSERT_TRUE(ChangeFilePermissions(test_dir1
, Read
, true));
826 // We should get notified in this case because filepathwatcher can no
827 // longer access the file
828 ASSERT_TRUE(ChangeFilePermissions(test_dir1
, Execute
, false));
829 ASSERT_TRUE(WaitForEvents());
830 ASSERT_TRUE(ChangeFilePermissions(test_dir1
, Execute
, true));
831 DeleteDelegateOnFileThread(delegate
.release());