2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2015-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "transmission.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "watchdir.h"
|
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
extern struct timeval tr_watchdir_generic_interval;
|
|
|
|
extern size_t tr_watchdir_retry_limit;
|
|
|
|
extern struct timeval tr_watchdir_retry_start_interval;
|
|
|
|
extern struct timeval tr_watchdir_retry_max_interval;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto constexpr FiftyMsec = timeval{ 0, 50000 };
|
|
|
|
auto constexpr OneHundredMsec = timeval{ 0, 100000 };
|
|
|
|
auto constexpr TwoHundredMsec = timeval{ 0, 200000 };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
} // namespace
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class WatchMode
|
|
|
|
{
|
|
|
|
NATIVE,
|
|
|
|
GENERIC
|
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
class WatchDirTest
|
|
|
|
: public SandboxedTest
|
|
|
|
, public ::testing::WithParamInterface<WatchMode>
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<struct event_base> ev_base_;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
SandboxedTest::SetUp();
|
|
|
|
ev_base_.reset(event_base_new(), event_base_free);
|
|
|
|
|
|
|
|
// speed up generic implementation
|
|
|
|
tr_watchdir_generic_interval = OneHundredMsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
{
|
|
|
|
ev_base_.reset();
|
|
|
|
|
|
|
|
SandboxedTest::TearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto createWatchDir(std::string const& path, tr_watchdir_cb cb, void* cb_data)
|
|
|
|
{
|
|
|
|
auto const force_generic = GetParam() == WatchMode::GENERIC;
|
|
|
|
return tr_watchdir_new(path.c_str(), cb, cb_data, ev_base_.get(), force_generic);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string createFile(std::string const& parent_dir, std::string const& name)
|
|
|
|
{
|
|
|
|
auto path = parent_dir;
|
|
|
|
path += TR_PATH_DELIMITER;
|
|
|
|
path += name;
|
|
|
|
|
|
|
|
createFileWithContents(path, "");
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
static std::string createDir(std::string const& parent_dir, std::string const& name)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto path = parent_dir;
|
|
|
|
path += TR_PATH_DELIMITER;
|
|
|
|
path += name;
|
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
tr_sys_dir_create(path, 0, 0700);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void processEvents()
|
|
|
|
{
|
|
|
|
event_base_loopexit(ev_base_.get(), &TwoHundredMsec);
|
|
|
|
event_base_dispatch(ev_base_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CallbackData
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
explicit CallbackData(tr_watchdir_status status = TR_WATCHDIR_ACCEPT)
|
|
|
|
: result{ status }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
tr_watchdir_status result{};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_watchdir_t wd = {};
|
|
|
|
std::string name = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
static tr_watchdir_status callback(tr_watchdir_t wd, char const* name, void* vdata) noexcept
|
|
|
|
{
|
|
|
|
auto* data = static_cast<CallbackData*>(vdata);
|
|
|
|
auto const result = data->result;
|
|
|
|
|
|
|
|
if (result != TR_WATCHDIR_RETRY)
|
|
|
|
{
|
|
|
|
data->wd = wd;
|
|
|
|
data->name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, construct)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
auto wd = createWatchDir(path, &callback, nullptr);
|
|
|
|
EXPECT_NE(nullptr, wd);
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_path_is_same(path.c_str(), tr_watchdir_get_path(wd)));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
processEvents();
|
|
|
|
|
|
|
|
tr_watchdir_free(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, initialScan)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
// setup: start with an empty directory.
|
|
|
|
// this block confirms that it's empty
|
|
|
|
{
|
|
|
|
auto wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
auto wd = createWatchDir(path, &callback, &wd_data);
|
|
|
|
EXPECT_NE(nullptr, wd);
|
|
|
|
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd_data.wd);
|
|
|
|
EXPECT_EQ("", wd_data.name);
|
|
|
|
|
|
|
|
tr_watchdir_free(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a file
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const base_name = std::string{ "test.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(path, base_name);
|
|
|
|
|
|
|
|
// confirm that a wd will pick up the file that
|
|
|
|
// was created before the wd was instantiated
|
|
|
|
{
|
|
|
|
auto wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
auto wd = createWatchDir(path, &callback, &wd_data);
|
|
|
|
EXPECT_NE(nullptr, wd);
|
|
|
|
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd, wd_data.wd);
|
|
|
|
EXPECT_EQ(base_name, wd_data.name);
|
|
|
|
|
|
|
|
tr_watchdir_free(wd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, watch)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
// create a new watchdir and confirm it's empty
|
|
|
|
auto wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
auto wd = createWatchDir(path, &callback, &wd_data);
|
|
|
|
EXPECT_NE(nullptr, wd);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd_data.wd);
|
|
|
|
EXPECT_EQ("", wd_data.name);
|
|
|
|
|
|
|
|
// test that a new file in an empty directory shows up
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file1 = std::string{ "test1" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(path, file1);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd, wd_data.wd);
|
|
|
|
EXPECT_EQ(file1, wd_data.name);
|
|
|
|
|
|
|
|
// test that a new file in a nonempty directory shows up
|
|
|
|
wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file2 = std::string{ "test2" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(path, file2);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd, wd_data.wd);
|
|
|
|
EXPECT_EQ(file2, wd_data.name);
|
|
|
|
|
|
|
|
// test that folders don't trigger the callback
|
|
|
|
wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
createDir(path, "test3");
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd_data.wd);
|
|
|
|
EXPECT_EQ("", wd_data.name);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_watchdir_free(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, watchTwoDirs)
|
|
|
|
{
|
|
|
|
auto top = sandboxDir();
|
|
|
|
|
|
|
|
// create two empty directories and watch them
|
|
|
|
auto wd1_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
auto const dir1 = createDir(top, "a");
|
|
|
|
auto wd1 = createWatchDir(dir1, &callback, &wd1_data);
|
|
|
|
EXPECT_NE(wd1, nullptr);
|
|
|
|
auto wd2_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
auto const dir2 = createDir(top, "b");
|
|
|
|
auto wd2 = createWatchDir(dir2, &callback, &wd2_data);
|
|
|
|
EXPECT_NE(wd2, nullptr);
|
|
|
|
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd1_data.wd);
|
|
|
|
EXPECT_EQ("", wd1_data.name);
|
|
|
|
EXPECT_EQ(nullptr, wd2_data.wd);
|
|
|
|
EXPECT_EQ("", wd2_data.name);
|
|
|
|
|
|
|
|
// add a file into directory 1 and confirm it triggers
|
|
|
|
// a callback with the right wd
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file1 = std::string{ "test.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(dir1, file1);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd1, wd1_data.wd);
|
|
|
|
EXPECT_EQ(file1, wd1_data.name);
|
|
|
|
EXPECT_EQ(nullptr, wd2_data.wd);
|
|
|
|
EXPECT_EQ("", wd2_data.name);
|
|
|
|
|
|
|
|
// add a file into directory 2 and confirm it triggers
|
|
|
|
// a callback with the right wd
|
|
|
|
wd1_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
wd2_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file2 = std::string{ "test2.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(dir2, file2);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd1_data.wd);
|
|
|
|
EXPECT_EQ("", wd1_data.name);
|
|
|
|
EXPECT_EQ(wd2, wd2_data.wd);
|
|
|
|
EXPECT_EQ(file2, wd2_data.name);
|
|
|
|
|
|
|
|
// TODO(ckerr): watchdir.c seems to treat IGNORE and ACCEPT identically
|
|
|
|
// so I'm not sure what's intended or what this is supposed to
|
|
|
|
// be testing.
|
|
|
|
wd1_data = CallbackData(TR_WATCHDIR_IGNORE);
|
|
|
|
wd2_data = CallbackData(TR_WATCHDIR_IGNORE);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file3 = std::string{ "test3.txt" };
|
|
|
|
auto const file4 = std::string{ "test4.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(dir1, file3);
|
|
|
|
createFile(dir2, file4);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd1, wd1_data.wd);
|
|
|
|
EXPECT_EQ(file3, wd1_data.name);
|
|
|
|
EXPECT_EQ(wd2, wd2_data.wd);
|
|
|
|
EXPECT_EQ(file4, wd2_data.name);
|
|
|
|
|
|
|
|
// confirm that callbacks don't get confused
|
|
|
|
// when there's a new file in directory 'a'
|
|
|
|
// and a new directory in directory 'b'
|
|
|
|
wd1_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
wd2_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file5 = std::string{ "test5.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(dir1, file5);
|
|
|
|
createDir(dir2, file5);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd1, wd1_data.wd);
|
|
|
|
EXPECT_EQ(file5, wd1_data.name);
|
|
|
|
EXPECT_EQ(nullptr, wd2_data.wd);
|
|
|
|
EXPECT_EQ("", wd2_data.name);
|
|
|
|
|
|
|
|
// reverse the order of the previous test:
|
|
|
|
// confirm that callbacks don't get confused
|
|
|
|
// when there's a new file in directory 'b'
|
|
|
|
// and a new directory in directory 'a'
|
|
|
|
wd1_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
wd2_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file6 = std::string{ "test6.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createDir(dir1, file6);
|
|
|
|
createFile(dir2, file6);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd1_data.wd);
|
|
|
|
EXPECT_EQ("", wd1_data.name);
|
|
|
|
EXPECT_EQ(wd2, wd2_data.wd);
|
|
|
|
EXPECT_EQ(file6, wd2_data.name);
|
|
|
|
|
|
|
|
// confirm that creating new directories in BOTH
|
|
|
|
// watchdirs still triggers no callbacks
|
|
|
|
wd1_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
wd2_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const file7 = std::string{ "test7.txt" };
|
|
|
|
auto const file8 = std::string{ "test8.txt" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createDir(dir1, file7);
|
|
|
|
createDir(dir2, file8);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd1_data.wd);
|
|
|
|
EXPECT_EQ("", wd1_data.name);
|
|
|
|
EXPECT_EQ(nullptr, wd2_data.wd);
|
|
|
|
EXPECT_EQ("", wd2_data.name);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_watchdir_free(wd2);
|
|
|
|
tr_watchdir_free(wd1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(WatchDirTest, retry)
|
|
|
|
{
|
|
|
|
auto const path = sandboxDir();
|
|
|
|
|
|
|
|
// tune retry logic
|
|
|
|
tr_watchdir_retry_limit = 10;
|
|
|
|
tr_watchdir_retry_start_interval = FiftyMsec;
|
|
|
|
tr_watchdir_retry_max_interval = tr_watchdir_retry_start_interval;
|
|
|
|
|
|
|
|
// test setup:
|
|
|
|
// Start watching the test directory.
|
|
|
|
// Create a file and return 'retry' back to the watchdir code
|
|
|
|
// from our callback. This should cause the wd to wait a bit
|
|
|
|
// and try again.
|
|
|
|
auto wd_data = CallbackData(TR_WATCHDIR_RETRY);
|
|
|
|
auto wd = createWatchDir(path, &callback, &wd_data);
|
|
|
|
EXPECT_NE(nullptr, wd);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd_data.wd);
|
|
|
|
EXPECT_EQ("", wd_data.name);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const test_file = std::string{ "test" };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFile(path, test_file);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(nullptr, wd_data.wd);
|
|
|
|
EXPECT_EQ("", wd_data.name);
|
|
|
|
|
|
|
|
// confirm that wd retries.
|
|
|
|
// return 'accept' in the callback so it won't keep retrying.
|
|
|
|
wd_data = CallbackData(TR_WATCHDIR_ACCEPT);
|
|
|
|
processEvents();
|
|
|
|
EXPECT_EQ(wd, wd_data.wd);
|
|
|
|
EXPECT_EQ(test_file, wd_data.name);
|
2021-09-29 17:25:52 +00:00
|
|
|
|
|
|
|
tr_watchdir_free(wd);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P( //
|
2020-08-11 18:11:55 +00:00
|
|
|
WatchDir,
|
|
|
|
WatchDirTest,
|
2021-08-15 09:41:48 +00:00
|
|
|
::testing::Values( //
|
|
|
|
WatchMode::NATIVE,
|
|
|
|
WatchMode::GENERIC));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
} // namespace libtransmission
|