premiere-libtorrent/test/test_peer_classes.cpp

151 lines
5.1 KiB
C++
Raw Normal View History

2014-07-06 21:18:00 +02:00
/*
Copyright (c) 2012, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "libtorrent/peer_class.hpp"
#include "libtorrent/peer_class_set.hpp"
#include "libtorrent/peer_class_type_filter.hpp"
#include "libtorrent/aux_/path.hpp"
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/session.hpp"
2014-07-06 21:18:00 +02:00
2017-04-12 20:05:53 +02:00
using namespace lt;
2014-07-06 21:18:00 +02:00
2018-01-29 15:58:22 +01:00
namespace {
2014-07-06 21:18:00 +02:00
std::string class_name(peer_class_t id, peer_class_pool const& p)
{
peer_class const* c = p.at(id);
2016-07-09 22:26:26 +02:00
TEST_CHECK(c != nullptr);
if (c == nullptr) return "";
2014-07-06 21:18:00 +02:00
peer_class_info i;
c->get_info(&i);
return i.label;
}
2018-01-29 15:58:22 +01:00
} // anonymous namespace
2014-07-06 21:18:00 +02:00
TORRENT_TEST(peer_class)
2014-07-06 21:18:00 +02:00
{
peer_class_pool pool;
peer_class_t id1 = pool.new_peer_class("test1");
peer_class_t id2 = pool.new_peer_class("test2");
// make sure there's no leak
for (int i = 0; i < 1000; ++i)
{
peer_class_t tmp = pool.new_peer_class("temp");
pool.decref(tmp);
}
peer_class_t id3 = pool.new_peer_class("test3");
TEST_CHECK(id3 == next(id2));
2014-07-06 21:18:00 +02:00
// make sure refcounting works
2017-01-21 05:30:05 +01:00
TEST_EQUAL(class_name(id3, pool), "test3");
2014-07-06 21:18:00 +02:00
pool.incref(id3);
2017-01-21 05:30:05 +01:00
TEST_EQUAL(class_name(id3, pool), "test3");
2014-07-06 21:18:00 +02:00
pool.decref(id3);
2017-01-21 05:30:05 +01:00
TEST_EQUAL(class_name(id3, pool), "test3");
2014-07-06 21:18:00 +02:00
pool.decref(id3);
// it should have been deleted now
2016-07-09 22:26:26 +02:00
TEST_CHECK(pool.at(id3) == nullptr);
2014-07-06 21:18:00 +02:00
// test setting and retrieving upload and download rates
pool.at(id2)->set_upload_limit(1000);
pool.at(id2)->set_download_limit(2000);
peer_class_info cls;
pool.at(id2)->get_info(&cls);
TEST_EQUAL(cls.upload_limit, 1000);
TEST_EQUAL(cls.download_limit, 2000);
2014-07-06 21:18:00 +02:00
// test peer_class_type_filter
peer_class_type_filter filter;
for (int i = 0; i < 5; ++i)
{
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(i)
2014-07-06 21:18:00 +02:00
, 0xffffffff) == 0xffffffff);
}
filter.disallow(static_cast<lt::peer_class_type_filter::socket_type_t>(0), peer_class_t{0});
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(0)
2014-07-06 21:18:00 +02:00
, 0xffffffff) == 0xfffffffe);
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(1)
2014-07-06 21:18:00 +02:00
, 0xffffffff) == 0xffffffff);
filter.allow(static_cast<lt::peer_class_type_filter::socket_type_t>(0), peer_class_t{0});
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(0)
2014-07-06 21:18:00 +02:00
, 0xffffffff) == 0xffffffff);
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(0), 0) == 0);
filter.add(static_cast<lt::peer_class_type_filter::socket_type_t>(0), peer_class_t{0});
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(0), 0) == 1);
filter.remove(static_cast<lt::peer_class_type_filter::socket_type_t>(0), peer_class_t{0});
TEST_CHECK(filter.apply(static_cast<lt::peer_class_type_filter::socket_type_t>(0), 0) == 0);
2014-07-06 21:18:00 +02:00
pool.decref(id2);
pool.decref(id1);
2016-07-09 22:26:26 +02:00
TEST_CHECK(pool.at(id2) == nullptr);
TEST_CHECK(pool.at(id1) == nullptr);
2014-07-06 21:18:00 +02:00
}
TORRENT_TEST(session_peer_class_filter)
{
using namespace libtorrent;
session ses;
peer_class_t my_class = ses.create_peer_class("200.1.x.x IP range");
ip_filter f;
f.add_rule(address_v4::from_string("200.1.1.0")
, address_v4::from_string("200.1.255.255")
2017-12-23 17:42:01 +01:00
, 1 << static_cast<std::uint32_t>(my_class));
ses.set_peer_class_filter(f);
2017-12-23 17:42:01 +01:00
TEST_CHECK(std::get<0>(ses.get_peer_class_filter().export_filter())
== std::get<0>(f.export_filter()));
}
TORRENT_TEST(session_peer_class_type_filter)
{
using namespace libtorrent;
session ses;
peer_class_t my_class = ses.create_peer_class("all utp sockets");
peer_class_type_filter f;
f.add(peer_class_type_filter::utp_socket, my_class);
f.disallow(peer_class_type_filter::utp_socket, session::global_peer_class_id);
ses.set_peer_class_type_filter(f);
TEST_CHECK(ses.get_peer_class_type_filter() == f);
}