This source file includes following definitions.
- OnChange
- Register
- Reset
- Success
- RecordChange
- OnFileChanged
- SetupWatchCallback
- SetUp
- TearDown
- DeleteDelegateOnFileThread
- test_file
- test_link
- WriteFile
- WaitForEvents
- collector
- SetupWatch
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- loop_
- OnFileChanged
- watcher
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- ChangeFilePermissions
- TEST_F
#include "base/files/file_path_watcher.h"
#if defined(OS_WIN)
#include <windows.h>
#include <aclapi.h>
#elif defined(OS_POSIX)
#include <sys/stat.h>
#endif
#include <set>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_file_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class TestDelegate;
class NotificationCollector
: public base::RefCountedThreadSafe<NotificationCollector> {
public:
NotificationCollector()
: loop_(base::MessageLoopProxy::current()) {}
void OnChange(TestDelegate* delegate) {
loop_->PostTask(FROM_HERE,
base::Bind(&NotificationCollector::RecordChange, this,
base::Unretained(delegate)));
}
void Register(TestDelegate* delegate) {
delegates_.insert(delegate);
}
void Reset() {
signaled_.clear();
}
bool Success() {
return signaled_ == delegates_;
}
private:
friend class base::RefCountedThreadSafe<NotificationCollector>;
~NotificationCollector() {}
void RecordChange(TestDelegate* delegate) {
ASSERT_TRUE(loop_->BelongsToCurrentThread());
ASSERT_TRUE(delegates_.count(delegate));
signaled_.insert(delegate);
if (signaled_ == delegates_)
loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
}
std::set<TestDelegate*> delegates_;
std::set<TestDelegate*> signaled_;
scoped_refptr<base::MessageLoopProxy> loop_;
};
class TestDelegateBase : public SupportsWeakPtr<TestDelegateBase> {
public:
TestDelegateBase() {}
virtual ~TestDelegateBase() {}
virtual void OnFileChanged(const FilePath& path, bool error) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(TestDelegateBase);
};
class TestDelegate : public TestDelegateBase {
public:
explicit TestDelegate(NotificationCollector* collector)
: collector_(collector) {
collector_->Register(this);
}
virtual ~TestDelegate() {}
virtual void OnFileChanged(const FilePath& path, bool error) OVERRIDE {
if (error)
ADD_FAILURE() << "Error " << path.value();
else
collector_->OnChange(this);
}
private:
scoped_refptr<NotificationCollector> collector_;
DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};
void SetupWatchCallback(const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
bool recursive_watch,
bool* result,
base::WaitableEvent* completion) {
*result = watcher->Watch(target, recursive_watch,
base::Bind(&TestDelegateBase::OnFileChanged,
delegate->AsWeakPtr()));
completion->Signal();
}
class FilePathWatcherTest : public testing::Test {
public:
FilePathWatcherTest()
: file_thread_("FilePathWatcherTest") {}
virtual ~FilePathWatcherTest() {}
protected:
virtual void SetUp() OVERRIDE {
base::Thread::Options options(MessageLoop::TYPE_IO, 0);
ASSERT_TRUE(file_thread_.StartWithOptions(options));
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
collector_ = new NotificationCollector();
}
virtual void TearDown() OVERRIDE {
RunLoop().RunUntilIdle();
}
void DeleteDelegateOnFileThread(TestDelegate* delegate) {
file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, delegate);
}
FilePath test_file() {
return temp_dir_.path().AppendASCII("FilePathWatcherTest");
}
FilePath test_link() {
return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk");
}
bool WriteFile(const FilePath& file, const std::string& content) {
int write_size = ::base::WriteFile(file, content.c_str(), content.length());
return write_size == static_cast<int>(content.length());
}
bool SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
bool recursive_watch) WARN_UNUSED_RESULT;
bool WaitForEvents() WARN_UNUSED_RESULT {
collector_->Reset();
loop_.Run();
return collector_->Success();
}
NotificationCollector* collector() { return collector_.get(); }
MessageLoop loop_;
base::Thread file_thread_;
ScopedTempDir temp_dir_;
scoped_refptr<NotificationCollector> collector_;
DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest);
};
bool FilePathWatcherTest::SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
bool recursive_watch) {
base::WaitableEvent completion(false, false);
bool result;
file_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(SetupWatchCallback,
target, watcher, delegate, recursive_watch, &result,
&completion));
completion.Wait();
return result;
}
TEST_F(FilePathWatcherTest, NewFile) {
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, ModifiedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, MovedFile) {
FilePath source_file(temp_dir_.path().AppendASCII("source"));
ASSERT_TRUE(WriteFile(source_file, "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
ASSERT_TRUE(base::Move(source_file, test_file()));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, DeletedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
base::DeleteFile(test_file(), false);
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
class Deleter : public TestDelegateBase {
public:
Deleter(FilePathWatcher* watcher, MessageLoop* loop)
: watcher_(watcher),
loop_(loop) {
}
virtual ~Deleter() {}
virtual void OnFileChanged(const FilePath&, bool) OVERRIDE {
watcher_.reset();
loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
}
FilePathWatcher* watcher() const { return watcher_.get(); }
private:
scoped_ptr<FilePathWatcher> watcher_;
MessageLoop* loop_;
DISALLOW_COPY_AND_ASSIGN(Deleter);
};
TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
FilePathWatcher* watcher = new FilePathWatcher;
scoped_ptr<Deleter> deleter(new Deleter(watcher, &loop_));
ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(deleter->watcher() == NULL);
}
TEST_F(FilePathWatcherTest, DISABLED_DestroyWithPendingNotification) {
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
FilePathWatcher* watcher = new FilePathWatcher;
ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "content"));
file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
FilePathWatcher watcher1, watcher2;
scoped_ptr<TestDelegate> delegate1(new TestDelegate(collector()));
scoped_ptr<TestDelegate> delegate2(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get(), false));
ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate1.release());
DeleteDelegateOnFileThread(delegate2.release());
}
TEST_F(FilePathWatcherTest, NonExistentDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, DirectoryChain) {
FilePath path(temp_dir_.path());
std::vector<std::string> dir_names;
for (int i = 0; i < 20; i++) {
std::string dir(base::StringPrintf("d%d", i));
dir_names.push_back(dir);
path = path.AppendASCII(dir);
}
FilePathWatcher watcher;
FilePath file(path.AppendASCII("file"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false));
FilePath sub_path(temp_dir_.path());
for (std::vector<std::string>::const_iterator d(dir_names.begin());
d != dir_names.end(); ++d) {
sub_path = sub_path.AppendASCII(*d);
ASSERT_TRUE(base::CreateDirectory(sub_path));
}
VLOG(1) << "Create File";
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file modification";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
#if defined(OS_MACOSX)
#define DisappearingDirectory DISABLED_DisappearingDirectory
#endif
TEST_F(FilePathWatcherTest, DisappearingDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false));
ASSERT_TRUE(base::DeleteFile(dir, true));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
ASSERT_TRUE(base::DeleteFile(test_file(), false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(test_file(), "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, WatchDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath file1(dir.AppendASCII("file1"));
FilePath file2(dir.AppendASCII("file2"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false));
ASSERT_TRUE(base::CreateDirectory(dir));
VLOG(1) << "Waiting for directory creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file1, "content"));
VLOG(1) << "Waiting for file1 creation";
ASSERT_TRUE(WaitForEvents());
#if !defined(OS_MACOSX)
ASSERT_TRUE(WriteFile(file1, "content v2"));
VLOG(1) << "Waiting for file1 modification";
ASSERT_TRUE(WaitForEvents());
#endif
ASSERT_TRUE(base::DeleteFile(file1, false));
VLOG(1) << "Waiting for file1 deletion";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file2, "content"));
VLOG(1) << "Waiting for file2 creation";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, MoveParent) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath dest(temp_dir_.path().AppendASCII("dest"));
FilePath subdir(dir.AppendASCII("subdir"));
FilePath file(subdir.AppendASCII("file"));
scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false));
scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(),
false));
ASSERT_TRUE(base::CreateDirectory(subdir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
base::Move(dir, dest);
VLOG(1) << "Waiting for directory move";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(file_delegate.release());
DeleteDelegateOnFileThread(subdir_delegate.release());
}
#if defined(OS_WIN)
TEST_F(FilePathWatcherTest, RecursiveWatch) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), true));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WaitForEvents());
FilePath file1(dir.AppendASCII("file1"));
ASSERT_TRUE(WriteFile(file1, "content"));
ASSERT_TRUE(WaitForEvents());
FilePath subdir(dir.AppendASCII("subdir"));
ASSERT_TRUE(base::CreateDirectory(subdir));
ASSERT_TRUE(WaitForEvents());
FilePath subdir_file1(subdir.AppendASCII("subdir_file1"));
ASSERT_TRUE(WriteFile(subdir_file1, "content"));
ASSERT_TRUE(WaitForEvents());
FilePath subdir_child_dir(subdir.AppendASCII("subdir_child_dir"));
ASSERT_TRUE(base::CreateDirectory(subdir_child_dir));
ASSERT_TRUE(WaitForEvents());
FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1"));
ASSERT_TRUE(WriteFile(child_dir_file1, "content v2"));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(child_dir_file1, "content"));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(file_util::MakeFileUnreadable(child_dir_file1));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(subdir_file1, false));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(child_dir_file1, false));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
#else
TEST_F(FilePathWatcherTest, RecursiveWatch) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_FALSE(SetupWatch(dir, &watcher, delegate.get(), true));
DeleteDelegateOnFileThread(delegate.release());
}
#endif
TEST_F(FilePathWatcherTest, MoveChild) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath source_dir(temp_dir_.path().AppendASCII("source"));
FilePath source_subdir(source_dir.AppendASCII("subdir"));
FilePath source_file(source_subdir.AppendASCII("file"));
FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
FilePath dest_file(dest_subdir.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(source_subdir));
ASSERT_TRUE(WriteFile(source_file, "content"));
scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false));
scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(),
false));
ASSERT_TRUE(base::Move(source_dir, dest_dir));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(file_delegate.release());
DeleteDelegateOnFileThread(subdir_delegate.release());
}
#if !defined(OS_LINUX)
TEST_F(FilePathWatcherTest, FileAttributesChanged) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
#endif
#if defined(OS_LINUX)
TEST_F(FilePathWatcherTest, CreateLink) {
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, DeleteLink) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
ASSERT_TRUE(base::DeleteFile(test_link(), false));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, ModifiedLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) {
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
ASSERT_TRUE(base::DeleteFile(test_file(), false));
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
VLOG(1) << "Waiting for link creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for dir/file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(base::DeleteFile(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
DeleteDelegateOnFileThread(delegate.release());
}
#endif
enum Permission {
Read,
Write,
Execute
};
#if defined(OS_MACOSX)
bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) {
struct stat stat_buf;
if (stat(path.value().c_str(), &stat_buf) != 0)
return false;
mode_t mode = 0;
switch (perm) {
case Read:
mode = S_IRUSR | S_IRGRP | S_IROTH;
break;
case Write:
mode = S_IWUSR | S_IWGRP | S_IWOTH;
break;
case Execute:
mode = S_IXUSR | S_IXGRP | S_IXOTH;
break;
default:
ADD_FAILURE() << "unknown perm " << perm;
return false;
}
if (allow) {
stat_buf.st_mode |= mode;
} else {
stat_buf.st_mode &= ~mode;
}
return chmod(path.value().c_str(), stat_buf.st_mode) == 0;
}
#endif
#if defined(OS_MACOSX)
TEST_F(FilePathWatcherTest, DirAttributesChanged) {
FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
ASSERT_TRUE(base::CreateDirectory(test_dir1));
ASSERT_TRUE(base::CreateDirectory(test_dir2));
ASSERT_TRUE(WriteFile(test_file, "content"));
FilePathWatcher watcher;
scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(), false));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
loop_.PostDelayedTask(FROM_HERE,
MessageLoop::QuitWhenIdleClosure(),
TestTimeouts::tiny_timeout());
ASSERT_FALSE(WaitForEvents());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
DeleteDelegateOnFileThread(delegate.release());
}
#endif
}
}