2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
#include <cstring> // strlen()
|
|
|
|
// #include <unistd.h> // sync()
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2022-04-22 16:35:56 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "blocklist.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "net.h"
|
2022-04-22 16:35:56 +00:00
|
|
|
#include "peer-socket.h"
|
2022-08-14 19:41:57 +00:00
|
|
|
#include "session.h" // tr_session.tr_session.addressIsBlocked()
|
2022-04-22 16:35:56 +00:00
|
|
|
#include "tr-strbuf.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
class BlocklistTest : public SessionTest
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
static char const constexpr* const Contents1 =
|
|
|
|
"10.5.6.7/8\n"
|
|
|
|
"Austin Law Firm:216.16.1.144-216.16.1.151\n"
|
|
|
|
"Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
|
|
|
|
"Corel Corporation:216.21.157.192-216.21.157.223\n"
|
|
|
|
"Fox Speed Channel:216.79.131.192-216.79.131.223\n";
|
|
|
|
|
|
|
|
static char const constexpr* const Contents2 =
|
|
|
|
"10.5.6.7/8\n"
|
|
|
|
"Austin Law Firm:216.16.1.144-216.16.1.151\n"
|
|
|
|
"Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
|
|
|
|
"Corel Corporation:216.21.157.192-216.21.157.223\n"
|
|
|
|
"Fox Speed Channel:216.79.131.192-216.79.131.223\n"
|
|
|
|
"Evilcorp:216.88.88.0-216.88.88.255\n";
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void createFileWithContents(char const* path, char const* contents)
|
|
|
|
{
|
2022-03-21 20:22:50 +00:00
|
|
|
auto const dir = tr_sys_path_dirname(path);
|
2022-05-22 01:17:00 +00:00
|
|
|
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
auto const fd = tr_sys_file_open(path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600);
|
2020-08-11 18:11:55 +00:00
|
|
|
blockingFileWrite(fd, contents, strlen(contents));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-07-09 23:44:20 +00:00
|
|
|
bool addressIsBlocked(char const* address_str)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-07-25 22:25:55 +00:00
|
|
|
auto const addr = tr_address::fromString(address_str);
|
2022-08-14 19:41:57 +00:00
|
|
|
return !addr || session_->addressIsBlocked(*addr);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BlocklistTest, parsing)
|
|
|
|
{
|
2022-05-23 02:22:34 +00:00
|
|
|
EXPECT_EQ(0U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// init the blocklist
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const path = tr_pathbuf{ session_->configDir(), "/blocklists/level1"sv };
|
2020-08-11 18:11:55 +00:00
|
|
|
createFileWithContents(path, Contents1);
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
|
|
|
EXPECT_TRUE(tr_blocklistExists(session_));
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(size_t{ 5 }, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// enable the blocklist
|
|
|
|
EXPECT_FALSE(tr_blocklistIsEnabled(session_));
|
|
|
|
tr_blocklistSetEnabled(session_, true);
|
|
|
|
EXPECT_TRUE(tr_blocklistIsEnabled(session_));
|
|
|
|
|
|
|
|
// test blocked addresses
|
|
|
|
EXPECT_FALSE(addressIsBlocked("0.0.0.1"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("10.1.2.3"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("216.16.1.143"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.144"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.145"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.146"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.147"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.148"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.149"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.150"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("216.16.1.151"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("216.16.1.152"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("216.16.1.153"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("217.0.0.1"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("255.0.0.1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
TEST_F(BlocklistTest, updating)
|
|
|
|
{
|
|
|
|
// init the session
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const path = tr_pathbuf{ session_->configDir(), "/blocklists/level1"sv };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// no blocklist to start with...
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(0U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// test that updated source files will get loaded
|
|
|
|
createFileWithContents(path, Contents1);
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// test that updated source files will get loaded
|
|
|
|
createFileWithContents(path, Contents2);
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(6U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// test that updated source files will get loaded
|
|
|
|
createFileWithContents(path, Contents1);
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// ensure that new files, if bad, get skipped
|
|
|
|
createFileWithContents(path, "# nothing useful\n");
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
2022-07-05 23:32:30 +00:00
|
|
|
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
} // namespace libtransmission
|