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"
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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"
|
2022-09-30 17:11:52 +00:00
|
|
|
"Fox Speed Channel:216.79.131.192-216.79.131.223\n"
|
|
|
|
"IPv6 example:2001:db8::-2001:db8:ffff:ffff:ffff:ffff:ffff:ffff\n";
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
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"
|
2022-09-30 17:11:52 +00:00
|
|
|
"IPv6 example:2001:db8::-2001:db8:ffff:ffff:ffff:ffff:ffff:ffff\n"
|
2020-08-11 18:11:55 +00:00
|
|
|
"Evilcorp:216.88.88.0-216.88.88.255\n";
|
|
|
|
|
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-09-30 17:11:52 +00:00
|
|
|
EXPECT_EQ(size_t{ 6 }, 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"));
|
2022-09-30 17:11:52 +00:00
|
|
|
// IPv6
|
|
|
|
EXPECT_TRUE(addressIsBlocked("2001:db8:dead:beef:dead:beef:dead:beef"));
|
|
|
|
EXPECT_TRUE(addressIsBlocked("2001:db8:ffff:ffff:ffff:ffff:ffff:fffe"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("fe80:1:1:1:1:1:1:1337"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("2a05:d012:8b5:6501:b6d2:c4fb:b11:5181"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("1::1"));
|
|
|
|
EXPECT_FALSE(addressIsBlocked("ffff::ffff"));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
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-09-30 17:11:52 +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, Contents2);
|
|
|
|
tr_sessionReloadBlocklists(session_);
|
2022-09-30 17:11:52 +00:00
|
|
|
EXPECT_EQ(7U, 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-09-30 17:11:52 +00:00
|
|
|
EXPECT_EQ(6U, 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-09-30 17:11:52 +00:00
|
|
|
EXPECT_EQ(6U, tr_blocklistGetRuleCount(session_));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
}
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|