1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00
transmission/tests/libtransmission/blocklist-test.cc
Charles Kerr 9a44eeaa27
refactor: tr_address cleanup (#3422)
* refactor: remove tr_address_compare()

* refactor: remove tr_address_to_string()

* refactor: remove NUM_TR_AF_INET_TYPES

* refactor: replace tr_sessionGetPublicAddress with tr_session::getPublicAddress()

* refactor: tr_peerIo() takes tr_address by value

* refactor: replace tr_sessionIsAddressBlocked with tr_session::isAddressBlocked()

* refactor: tr_peerMgrAddIncoming now takes tr_address by value

* refactor: replace tr_address_is_valid_for_peers() with tr_address.isValidForPeers()

* refactor: tr_netOpenPeerSocket takes tr_address by value

* refactor: remove tr_generateAllowedSet()

* refactor: setup_sockaddr takes a tr_address by value

* refactor: tr_netBindTCP() takes a tr_address by value

* refactor: tr_dhtAddNode() takes a tr_address by value

* refactor: remove tr_address_from_string()

* refactor: rename tr_address.isValidForPeers() to .isValidPeerAddress()

* refactor: replace tr_address_from_sockaddr_storage() with tr_address::fromSockaddrStorage()

* refactor: minor cleanup to tr_address::readable()
2022-07-08 13:23:41 -05:00

138 lines
4.3 KiB
C++

// This file Copyright (C) 2013-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.
#include <cstring> // strlen()
// #include <unistd.h> // sync()
#include "transmission.h"
#include "blocklist.h"
#include "file.h"
#include "net.h"
#include "peer-socket.h"
#include "session.h" // tr_sessionIsAddressBlocked()
#include "tr-strbuf.h"
#include "utils.h"
#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)
{
auto const dir = tr_sys_path_dirname(path);
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700);
auto const fd = tr_sys_file_open(path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600);
blockingFileWrite(fd, contents, strlen(contents));
tr_sys_file_close(fd);
sync();
}
#endif
bool addressIsBlocked(std::string_view address_str)
{
auto const addr = tr_address::fromString(address_str);
return addr && session_->isAddressBlocked(*addr);
}
};
TEST_F(BlocklistTest, parsing)
{
EXPECT_EQ(0U, tr_blocklistGetRuleCount(session_));
// init the blocklist
auto const path = tr_pathbuf{ tr_sessionGetConfigDir(session_), "/blocklists/level1" };
createFileWithContents(path, Contents1);
tr_sessionReloadBlocklists(session_);
EXPECT_TRUE(tr_blocklistExists(session_));
EXPECT_EQ(size_t{ 5 }, tr_blocklistGetRuleCount(session_));
// 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
auto const path = tr_pathbuf{ tr_sessionGetConfigDir(session_), "/blocklists/level1" };
// no blocklist to start with...
EXPECT_EQ(0U, tr_blocklistGetRuleCount(session_));
// test that updated source files will get loaded
createFileWithContents(path, Contents1);
tr_sessionReloadBlocklists(session_);
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
// test that updated source files will get loaded
createFileWithContents(path, Contents2);
tr_sessionReloadBlocklists(session_);
EXPECT_EQ(6U, tr_blocklistGetRuleCount(session_));
// test that updated source files will get loaded
createFileWithContents(path, Contents1);
tr_sessionReloadBlocklists(session_);
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
// ensure that new files, if bad, get skipped
createFileWithContents(path, "# nothing useful\n");
tr_sessionReloadBlocklists(session_);
EXPECT_EQ(5U, tr_blocklistGetRuleCount(session_));
// cleanup
}
} // namespace test
} // namespace libtransmission