#include "base/files/file_path_watcher.h"
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_file_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <aclapi.h>
#elif BUILDFLAG(IS_POSIX)
#include <sys/stat.h>
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/path_utils.h"
#endif
#if BUILDFLAG(IS_POSIX)
#include "base/files/file_descriptor_watcher_posix.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include "base/files/file_path_watcher_inotify.h"
#include "base/format_macros.h"
#endif
namespace base {
namespace {
class TestDelegate;
class NotificationCollector
: public base::RefCountedThreadSafe<NotificationCollector> {
public:
NotificationCollector()
: task_runner_(SingleThreadTaskRunner::GetCurrentDefault()) {}
void OnChange(TestDelegate* delegate) {
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&NotificationCollector::RecordChange, this,
base::Unretained(delegate)));
}
void Register(TestDelegate* delegate) {
delegates_.insert(delegate);
}
void Reset(base::OnceClosure signal_closure) {
signal_closure_ = std::move(signal_closure);
signaled_.clear();
}
bool Success() {
return signaled_ == delegates_;
}
private:
friend class base::RefCountedThreadSafe<NotificationCollector>;
~NotificationCollector() = default;
void RecordChange(TestDelegate* delegate) {
ASSERT_TRUE(task_runner_->BelongsToCurrentThread());
ASSERT_TRUE(delegates_.count(delegate));
signaled_.insert(delegate);
if (signal_closure_ && signaled_ == delegates_)
std::move(signal_closure_).Run();
}
std::set<TestDelegate*> delegates_;
std::set<TestDelegate*> signaled_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::OnceClosure signal_closure_;
};
class TestDelegateBase : public SupportsWeakPtr<TestDelegateBase> {
public:
TestDelegateBase() = default;
TestDelegateBase(const TestDelegateBase&) = delete;
TestDelegateBase& operator=(const TestDelegateBase&) = delete;
virtual ~TestDelegateBase() = default;
virtual void OnFileChanged(const FilePath& path, bool error) = 0;
};
class TestDelegate : public TestDelegateBase {
public:
explicit TestDelegate(NotificationCollector* collector)
: collector_(collector) {
collector_->Register(this);
}
TestDelegate(const TestDelegate&) = delete;
TestDelegate& operator=(const TestDelegate&) = delete;
~TestDelegate() override = default;
void set_expect_error() { expect_error_ = true; }
std::vector<FilePath> get_observed_paths() const { return observed_paths_; }
void OnFileChanged(const FilePath& path, bool error) override {
observed_paths_.push_back(path);
if (error != expect_error_) {
ADD_FAILURE() << "Unexpected change for \"" << path
<< "\" with |error| = " << (error ? "true" : "false");
} else {
collector_->OnChange(this);
}
}
private:
scoped_refptr<NotificationCollector> collector_;
bool expect_error_ = false;
std::vector<FilePath> observed_paths_;
};
class FilePathWatcherTest : public testing::Test {
public:
FilePathWatcherTest()
#if BUILDFLAG(IS_POSIX)
: task_environment_(test::TaskEnvironment::MainThreadType::IO)
#endif
{
}
FilePathWatcherTest(const FilePathWatcherTest&) = delete;
FilePathWatcherTest& operator=(const FilePathWatcherTest&) = delete;
~FilePathWatcherTest() override = default;
protected:
void SetUp() override {
#if BUILDFLAG(IS_ANDROID)
FilePath parent_dir;
ASSERT_TRUE(android::GetExternalStorageDirectory(&parent_dir));
ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(parent_dir));
#else
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
#endif
collector_ = new NotificationCollector();
}
void TearDown() override { RunLoop().RunUntilIdle(); }
FilePath test_file() {
return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest");
}
FilePath test_link() {
return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest.lnk");
}
[[nodiscard]] bool SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
FilePathWatcher::Type watch_type);
[[nodiscard]] bool SetupWatchWithOptions(
const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
FilePathWatcher::WatchOptions watch_options);
[[nodiscard]] bool WaitForEvent() {
return WaitForEventWithTimeout(TestTimeouts::action_timeout());
}
[[nodiscard]] bool WaitForEventWithTimeout(TimeDelta timeout) {
RunLoop run_loop;
collector_->Reset(run_loop.QuitClosure());
SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), timeout);
run_loop.Run();
return collector_->Success();
}
NotificationCollector* collector() { return collector_.get(); }
test::TaskEnvironment task_environment_;
ScopedTempDir temp_dir_;
scoped_refptr<NotificationCollector> collector_;
};
bool FilePathWatcherTest::SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
FilePathWatcher::Type watch_type) {
return watcher->Watch(target, watch_type,
base::BindRepeating(&TestDelegateBase::OnFileChanged,
delegate->AsWeakPtr()));
}
bool FilePathWatcherTest::SetupWatchWithOptions(
const FilePath& target,
FilePathWatcher* watcher,
TestDelegateBase* delegate,
FilePathWatcher::WatchOptions watch_options) {
return watcher->WatchWithOptions(
target, watch_options,
base::BindRepeating(&TestDelegateBase::OnFileChanged,
delegate->AsWeakPtr()));
}
TEST_F(FilePathWatcherTest, NewFile) {
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, ModifiedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, MovedFile) {
FilePath source_file(temp_dir_.GetPath().AppendASCII("source"));
ASSERT_TRUE(WriteFile(source_file, "content"));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::Move(source_file, test_file()));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DeletedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
base::DeleteFile(test_file());
ASSERT_TRUE(WaitForEvent());
}
class Deleter : public TestDelegateBase {
public:
explicit Deleter(base::OnceClosure done_closure)
: watcher_(std::make_unique<FilePathWatcher>()),
done_closure_(std::move(done_closure)) {}
Deleter(const Deleter&) = delete;
Deleter& operator=(const Deleter&) = delete;
~Deleter() override = default;
void OnFileChanged(const FilePath&, bool) override {
watcher_.reset();
std::move(done_closure_).Run();
}
FilePathWatcher* watcher() const { return watcher_.get(); }
private:
std::unique_ptr<FilePathWatcher> watcher_;
base::OnceClosure done_closure_;
};
TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
base::RunLoop run_loop;
Deleter deleter(run_loop.QuitClosure());
ASSERT_TRUE(SetupWatch(test_file(), deleter.watcher(), &deleter,
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "content"));
run_loop.Run();
ASSERT_TRUE(deleter.watcher() == nullptr);
}
TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) {
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
FilePathWatcher watcher;
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "content"));
}
TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
FilePathWatcher watcher1, watcher2;
std::unique_ptr<TestDelegate> delegate1(new TestDelegate(collector()));
std::unique_ptr<TestDelegate> delegate2(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, NonExistentDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::DeleteFile(file));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DirectoryChain) {
FilePath path(temp_dir_.GetPath());
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"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
FilePath sub_path(temp_dir_.GetPath());
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(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file modification";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DisappearingDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::DeletePathRecursively(dir));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::DeleteFile(test_file()));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(test_file(), "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, WatchDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath file1(dir.AppendASCII("file1"));
FilePath file2(dir.AppendASCII("file2"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::CreateDirectory(dir));
VLOG(1) << "Waiting for directory creation";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file1, "content"));
VLOG(1) << "Waiting for file1 creation";
ASSERT_TRUE(WaitForEvent());
#if !BUILDFLAG(IS_APPLE)
ASSERT_TRUE(WriteFile(file1, "content v2"));
VLOG(1) << "Waiting for file1 modification";
ASSERT_TRUE(WaitForEvent());
#endif
ASSERT_TRUE(base::DeleteFile(file1));
VLOG(1) << "Waiting for file1 deletion";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file2, "content"));
VLOG(1) << "Waiting for file2 creation";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, MoveParent) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath dest(temp_dir_.GetPath().AppendASCII("dest"));
FilePath subdir(dir.AppendASCII("subdir"));
FilePath file(subdir.AppendASCII("file"));
std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(),
FilePathWatcher::Type::kNonRecursive));
std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::CreateDirectory(subdir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvent());
base::Move(dir, dest);
VLOG(1) << "Waiting for directory move";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, RecursiveWatch) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
bool setup_result = SetupWatch(dir, &watcher, delegate.get(),
FilePathWatcher::Type::kRecursive);
if (!FilePathWatcher::RecursiveWatchAvailable()) {
#if defined(OHOS_UNITTESTS)
ASSERT_TRUE(setup_result);
#else
ASSERT_FALSE(setup_result);
#endif
return;
}
ASSERT_TRUE(setup_result);
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WaitForEvent());
FilePath file1(dir.AppendASCII("file1"));
ASSERT_TRUE(WriteFile(file1, "content"));
ASSERT_TRUE(WaitForEvent());
FilePath subdir(dir.AppendASCII("subdir"));
ASSERT_TRUE(base::CreateDirectory(subdir));
ASSERT_TRUE(WaitForEvent());
FilePath subdir2(subdir.AppendASCII("subdir2"));
ASSERT_TRUE(base::CreateDirectory(subdir2));
ASSERT_TRUE(WaitForEvent());
FilePath subdir2b(subdir.AppendASCII("subdir2b"));
base::Move(subdir2, subdir2b);
ASSERT_TRUE(WaitForEvent());
#if !(BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID))
Time access_time;
ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00", &access_time));
ASSERT_TRUE(base::TouchFile(dir, access_time, access_time));
ASSERT_TRUE(WaitForEvent());
#endif
FilePath subdir_file1(subdir.AppendASCII("subdir_file1"));
ASSERT_TRUE(WriteFile(subdir_file1, "content"));
ASSERT_TRUE(WaitForEvent());
FilePath subdir_child_dir(subdir.AppendASCII("subdir_child_dir"));
ASSERT_TRUE(base::CreateDirectory(subdir_child_dir));
ASSERT_TRUE(WaitForEvent());
FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1"));
ASSERT_TRUE(WriteFile(child_dir_file1, "content v2"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(child_dir_file1, "content"));
ASSERT_TRUE(WaitForEvent());
#if !BUILDFLAG(IS_ANDROID)
ASSERT_TRUE(base::MakeFileUnreadable(child_dir_file1));
ASSERT_TRUE(WaitForEvent());
#endif
ASSERT_TRUE(base::DeleteFile(subdir_file1));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::DeleteFile(child_dir_file1));
ASSERT_TRUE(WaitForEvent());
}
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
TEST_F(FilePathWatcherTest, RecursiveWithSymLink) {
if (!FilePathWatcher::RecursiveWatchAvailable())
return;
FilePathWatcher watcher;
FilePath test_dir(temp_dir_.GetPath().AppendASCII("test_dir"));
ASSERT_TRUE(base::CreateDirectory(test_dir));
FilePath symlink(test_dir.AppendASCII("symlink"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(),
FilePathWatcher::Type::kRecursive));
FilePath target1(temp_dir_.GetPath().AppendASCII("target1"));
ASSERT_TRUE(base::CreateSymbolicLink(target1, symlink));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::CreateDirectory(target1));
ASSERT_TRUE(WaitForEvent());
FilePath target1_file(target1.AppendASCII("file"));
ASSERT_TRUE(WriteFile(target1_file, "content"));
ASSERT_TRUE(WaitForEvent());
FilePath target2(temp_dir_.GetPath().AppendASCII("target2"));
ASSERT_TRUE(base::CreateDirectory(target2));
ASSERT_TRUE(base::DeleteFile(symlink));
ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink));
ASSERT_TRUE(WaitForEvent());
FilePath target2_file(target2.AppendASCII("file"));
ASSERT_TRUE(WriteFile(target2_file, "content"));
ASSERT_TRUE(WaitForEvent());
}
#endif
TEST_F(FilePathWatcherTest, MoveChild) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath source_dir(temp_dir_.GetPath().AppendASCII("source"));
FilePath source_subdir(source_dir.AppendASCII("subdir"));
FilePath source_file(source_subdir.AppendASCII("file"));
FilePath dest_dir(temp_dir_.GetPath().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"));
std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(),
FilePathWatcher::Type::kNonRecursive));
std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::Move(source_dir, dest_dir));
ASSERT_TRUE(WaitForEvent());
}
#if BUILDFLAG(IS_ANDROID)
#define FileAttributesChanged DISABLED_FileAttributesChanged
#endif
TEST_F(FilePathWatcherTest, FileAttributesChanged) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::MakeFileUnreadable(test_file()));
ASSERT_TRUE(WaitForEvent());
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST_F(FilePathWatcherTest, CreateLink) {
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DeleteLink) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::DeleteFile(test_link()));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, ModifiedLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) {
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::DeleteFile(test_file()));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
VLOG(1) << "Waiting for link creation";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::DeleteFile(file));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for dir/file creation";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::DeleteFile(file));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(base::CreateDirectory(dir));
ASSERT_TRUE(CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(base::DeleteFile(file));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, RacyRecursiveWatch) {
if (!FilePathWatcher::RecursiveWatchAvailable())
GTEST_SKIP();
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
std::vector<FilePath> subdirs;
for (int i = 0; i < 20; ++i)
subdirs.emplace_back(dir.AppendASCII(base::StringPrintf("subdir_%d", i)));
Thread subdir_updater("SubDir Updater");
ASSERT_TRUE(subdir_updater.Start());
auto subdir_update_task = base::BindLambdaForTesting([&]() {
for (const auto& subdir : subdirs) {
ASSERT_TRUE(CreateDirectory(subdir));
FilePath subdir_file(subdir.AppendASCII("subdir_file"));
ASSERT_TRUE(WriteFile(subdir_file, "content"));
ASSERT_TRUE(DeletePathRecursively(subdir));
}
});
for (int i = 0; i < 100; ++i) {
RunLoop run_loop;
auto watcher = std::make_unique<FilePathWatcher>();
auto watcher_callback =
base::BindLambdaForTesting([&](const FilePath& path, bool error) {
watcher.reset();
run_loop.Quit();
});
bool setup_result = watcher->Watch(dir, FilePathWatcher::Type::kRecursive,
watcher_callback);
ASSERT_TRUE(setup_result);
subdir_updater.task_runner()->PostTask(FROM_HERE, subdir_update_task);
run_loop.Run();
ASSERT_FALSE(watcher);
ASSERT_FALSE(FilePathWatcher::HasWatchesForTest());
}
}
TEST_F(FilePathWatcherTest, InotifyLimitInWatch) {
auto watcher = std::make_unique<FilePathWatcher>();
ScopedMaxNumberOfInotifyWatchesOverrideForTest max_inotify_watches(1);
ASSERT_FALSE(watcher->Watch(
test_file(), FilePathWatcher::Type::kNonRecursive,
base::BindLambdaForTesting(
[&](const FilePath& path, bool error) { ADD_FAILURE(); })));
ASSERT_TRUE(WriteFile(test_file(), "content"));
base::RunLoop().RunUntilIdle();
}
TEST_F(FilePathWatcherTest, InotifyLimitInUpdate) {
enum kTestType {
kDestroyWatcher,
kDoNothing,
};
for (auto callback_type : {kDestroyWatcher, kDoNothing}) {
SCOPED_TRACE(testing::Message() << "type=" << callback_type);
base::RunLoop run_loop;
auto watcher = std::make_unique<FilePathWatcher>();
bool error_callback_called = false;
auto watcher_callback =
base::BindLambdaForTesting([&](const FilePath& path, bool error) {
ASSERT_FALSE(error_callback_called);
if (!error)
return;
error_callback_called = true;
if (callback_type == kDestroyWatcher)
watcher.reset();
run_loop.Quit();
});
ASSERT_TRUE(watcher->Watch(
test_file(), FilePathWatcher::Type::kNonRecursive, watcher_callback));
ScopedMaxNumberOfInotifyWatchesOverrideForTest max_inotify_watches(1);
ASSERT_TRUE(WriteFile(test_file(), "content"));
run_loop.Run();
ASSERT_TRUE(DeleteFile(test_file()));
base::RunLoop().RunUntilIdle();
}
}
TEST_F(FilePathWatcherTest, InotifyLimitInUpdateRecursive) {
enum kTestType {
kDestroyWatcher,
kDoNothing,
};
FilePath dir(temp_dir_.GetPath().AppendASCII("dir"));
for (auto callback_type : {kDestroyWatcher, kDoNothing}) {
SCOPED_TRACE(testing::Message() << "type=" << callback_type);
base::RunLoop run_loop;
auto watcher = std::make_unique<FilePathWatcher>();
bool error_callback_called = false;
auto watcher_callback =
base::BindLambdaForTesting([&](const FilePath& path, bool error) {
ASSERT_FALSE(error_callback_called);
if (!error)
return;
error_callback_called = true;
if (callback_type == kDestroyWatcher)
watcher.reset();
run_loop.Quit();
});
ASSERT_TRUE(watcher->Watch(dir, FilePathWatcher::Type::kRecursive,
watcher_callback));
constexpr size_t kMaxLimit = 10u;
ScopedMaxNumberOfInotifyWatchesOverrideForTest max_inotify_watches(
kMaxLimit);
for (size_t i = 0; i < kMaxLimit; ++i) {
base::FilePath subdir =
dir.AppendASCII(base::StringPrintf("subdir_%" PRIuS, i));
ASSERT_TRUE(CreateDirectory(subdir));
}
run_loop.Run();
for (size_t i = 0; i < kMaxLimit; ++i) {
base::FilePath subdir =
dir.AppendASCII(base::StringPrintf("subdir_%" PRIuS, i));
ASSERT_TRUE(DeleteFile(subdir));
}
base::RunLoop().RunUntilIdle();
}
}
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
TEST_F(FilePathWatcherTest, ReturnFullPath_RecursiveInRootFolder) {
FilePathWatcher directory_watcher;
FilePath watched_folder(temp_dir_.GetPath().AppendASCII("watched_folder"));
FilePath file(watched_folder.AppendASCII("file"));
ASSERT_TRUE(CreateDirectory(watched_folder));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(
SetupWatchWithOptions(watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kRecursive,
.report_modified_path = true}));
ASSERT_TRUE(WriteFile(file, "test"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test123"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(file));
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths{file, file, file, file};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
TEST_F(FilePathWatcherTest, ReturnFullPath_RecursiveInNestedFolder) {
FilePathWatcher directory_watcher;
FilePath watched_folder(temp_dir_.GetPath().AppendASCII("watched_folder"));
FilePath subfolder(watched_folder.AppendASCII("subfolder"));
FilePath file(subfolder.AppendASCII("file"));
ASSERT_TRUE(CreateDirectory(watched_folder));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(
SetupWatchWithOptions(watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kRecursive,
.report_modified_path = true}));
ASSERT_TRUE(CreateDirectory(subfolder));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test123"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(file));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(subfolder));
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths{subfolder, file, file,
file, file, subfolder};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
TEST_F(FilePathWatcherTest, ReturnFullPath_NonRecursiveInRootFolder) {
FilePathWatcher directory_watcher;
FilePath watched_folder(temp_dir_.GetPath().AppendASCII("watched_folder"));
FilePath file(watched_folder.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(watched_folder));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(
SetupWatchWithOptions(watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kNonRecursive,
.report_modified_path = true}));
ASSERT_TRUE(WriteFile(file, "test"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test123"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(file));
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths{file, file, file, file};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
TEST_F(FilePathWatcherTest, ReturnFullPath_NonRecursiveRemoveEnclosingFolder) {
FilePathWatcher directory_watcher;
FilePath root_folder(temp_dir_.GetPath().AppendASCII("root_folder"));
FilePath folder(root_folder.AppendASCII("folder"));
FilePath watched_folder(folder.AppendASCII("watched_folder"));
FilePath file(watched_folder.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(watched_folder));
ASSERT_TRUE(WriteFile(file, "test"));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(
SetupWatchWithOptions(watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kNonRecursive,
.report_modified_path = true}));
ASSERT_TRUE(DeletePathRecursively(folder));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths{file, watched_folder,
watched_folder};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
TEST_F(FilePathWatcherTest, ReturnWatchedPath_RecursiveInRootFolder) {
FilePathWatcher directory_watcher;
FilePath watched_folder(temp_dir_.GetPath().AppendASCII("watched_folder"));
FilePath file(watched_folder.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(watched_folder));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(
SetupWatchWithOptions(watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kRecursive}));
ASSERT_TRUE(WriteFile(file, "test"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test123"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(file));
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths{watched_folder, watched_folder,
watched_folder, watched_folder};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
TEST_F(FilePathWatcherTest, ReturnWatchedPath_NonRecursiveInRootFolder) {
FilePathWatcher directory_watcher;
FilePath watched_folder(temp_dir_.GetPath().AppendASCII("watched_folder"));
FilePath file(watched_folder.AppendASCII("file"));
ASSERT_TRUE(base::CreateDirectory(watched_folder));
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(SetupWatchWithOptions(
watched_folder, &directory_watcher, delegate.get(),
{.type = base::FilePathWatcher::Type::kNonRecursive}));
ASSERT_TRUE(WriteFile(file, "test"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(WriteFile(file, "test123"));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(DeleteFile(file));
ASSERT_TRUE(WaitForEvent());
std::vector<base::FilePath> expected_paths {watched_folder, watched_folder,
watched_folder, watched_folder};
EXPECT_EQ(delegate->get_observed_paths(), expected_paths);
}
#endif
enum Permission {
Read,
Write,
Execute
};
#if BUILDFLAG(IS_APPLE)
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 BUILDFLAG(IS_APPLE)
TEST_F(FilePathWatcherTest, DirAttributesChanged) {
FilePath test_dir1(
temp_dir_.GetPath().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;
std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(),
FilePathWatcher::Type::kNonRecursive));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
ASSERT_FALSE(WaitForEventWithTimeout(TestTimeouts::tiny_timeout()));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
ASSERT_TRUE(WaitForEvent());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
}
#endif
#if BUILDFLAG(IS_APPLE)
TEST_F(FilePathWatcherTest, TrivialNoDir) {
const FilePath tmp_dir = temp_dir_.GetPath();
const FilePath non_existent = tmp_dir.Append(FILE_PATH_LITERAL("nope"));
FilePathWatcher watcher;
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_FALSE(SetupWatch(non_existent, &watcher, delegate.get(),
FilePathWatcher::Type::kTrivial));
}
TEST_F(FilePathWatcherTest, TrivialDirStart) {
const FilePath tmp_dir = temp_dir_.GetPath();
FilePathWatcher watcher;
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(SetupWatch(tmp_dir, &watcher, delegate.get(),
FilePathWatcher::Type::kTrivial));
}
TEST_F(FilePathWatcherTest, TrivialDirChange) {
const FilePath tmp_dir = temp_dir_.GetPath();
FilePathWatcher watcher;
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(SetupWatch(tmp_dir, &watcher, delegate.get(),
FilePathWatcher::Type::kTrivial));
ASSERT_TRUE(TouchFile(tmp_dir, base::Time::Now(), base::Time::Now()));
ASSERT_TRUE(WaitForEvent());
}
TEST_F(FilePathWatcherTest, TrivialParentDirChange) {
const FilePath tmp_dir = temp_dir_.GetPath();
const FilePath sub_dir1 = tmp_dir.Append(FILE_PATH_LITERAL("subdir"));
const FilePath sub_dir2 = sub_dir1.Append(FILE_PATH_LITERAL("subdir_redux"));
ASSERT_TRUE(CreateDirectory(sub_dir1));
ASSERT_TRUE(CreateDirectory(sub_dir2));
FilePathWatcher watcher;
auto delegate = std::make_unique<TestDelegate>(collector());
ASSERT_TRUE(SetupWatch(sub_dir2, &watcher, delegate.get(),
FilePathWatcher::Type::kTrivial));
ASSERT_TRUE(Move(sub_dir1, tmp_dir.Append(FILE_PATH_LITERAL("over_here"))));
ASSERT_FALSE(WaitForEventWithTimeout(TestTimeouts::tiny_timeout()));
}
TEST_F(FilePathWatcherTest, TrivialDirMove) {
const FilePath tmp_dir = temp_dir_.GetPath();
const FilePath sub_dir = tmp_dir.Append(FILE_PATH_LITERAL("subdir"));
ASSERT_TRUE(CreateDirectory(sub_dir));
FilePathWatcher watcher;
auto delegate = std::make_unique<TestDelegate>(collector());
delegate->set_expect_error();
ASSERT_TRUE(SetupWatch(sub_dir, &watcher, delegate.get(),
FilePathWatcher::Type::kTrivial));
ASSERT_TRUE(Move(sub_dir, tmp_dir.Append(FILE_PATH_LITERAL("over_here"))));
ASSERT_TRUE(WaitForEvent());
}
#endif
}
}