2015-08-10 15:17:41 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, 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 "test_utils.hpp"
|
2015-12-10 07:56:33 +01:00
|
|
|
#include "settings.hpp"
|
|
|
|
#include "setup_swarm.hpp"
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2015-12-10 07:56:33 +01:00
|
|
|
#include "libtorrent/add_torrent_params.hpp"
|
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2015-08-10 15:17:41 +02:00
|
|
|
#include "libtorrent/extensions/ut_metadata.hpp"
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
2015-12-10 07:56:33 +01:00
|
|
|
namespace lt = libtorrent;
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
enum flags_t
|
|
|
|
{
|
|
|
|
// disconnect immediately after receiving the metadata (to test that
|
|
|
|
// edge case, it caused a crash once)
|
2015-12-10 07:56:33 +01:00
|
|
|
disconnect = 1,
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
// force encryption (to make sure the plugin uses the peer_connection
|
|
|
|
// API in a compatible way)
|
2015-12-10 07:56:33 +01:00
|
|
|
full_encryption = 2,
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
// have the downloader connect to the seeder
|
|
|
|
// (instead of the other way around)
|
2015-12-10 07:56:33 +01:00
|
|
|
reverse = 4,
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
// only use uTP
|
2015-12-10 07:56:33 +01:00
|
|
|
utp = 8,
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
// upload-only mode
|
2016-11-04 00:39:42 +01:00
|
|
|
upload_only = 16,
|
|
|
|
|
|
|
|
// re-add the torrent after removing
|
|
|
|
readd = 32
|
2015-08-10 15:17:41 +02:00
|
|
|
};
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
void run_metadata_test(int flags)
|
2015-08-10 15:17:41 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
int metadata_alerts = 0;
|
2015-08-10 15:17:41 +02:00
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
sim::default_config cfg;
|
|
|
|
sim::simulation sim{cfg};
|
2015-08-10 15:17:41 +02:00
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
lt::settings_pack default_settings = settings();
|
2015-08-10 15:17:41 +02:00
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
if (flags & full_encryption)
|
2015-08-10 15:17:41 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
enable_enc(default_settings);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
if (flags & utp)
|
2015-08-10 15:17:41 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
utp_only(default_settings);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
lt::add_torrent_params default_add_torrent;
|
|
|
|
if (flags & upload_only)
|
2015-08-10 15:17:41 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
default_add_torrent.flags |= add_torrent_params::flag_upload_mode;
|
|
|
|
}
|
2015-08-10 16:02:01 +02:00
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
setup_swarm(2, (flags & reverse) ? swarm_test::upload : swarm_test::download
|
|
|
|
// add session
|
2016-06-19 01:24:27 +02:00
|
|
|
, [](lt::settings_pack&) {}
|
2015-12-10 07:56:33 +01:00
|
|
|
// add torrent
|
|
|
|
, [](lt::add_torrent_params& params) {
|
|
|
|
// we want to add the torrent via magnet link
|
|
|
|
params.url = lt::make_magnet_uri(*params.ti);
|
|
|
|
params.ti.reset();
|
|
|
|
params.flags &= ~add_torrent_params::flag_upload_mode;
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
2015-12-10 07:56:33 +01:00
|
|
|
// on alert
|
|
|
|
, [&](lt::alert const* a, lt::session& ses) {
|
|
|
|
|
|
|
|
if (alert_cast<metadata_received_alert>(a))
|
|
|
|
{
|
|
|
|
metadata_alerts += 1;
|
|
|
|
|
2016-11-04 04:58:35 +01:00
|
|
|
auto ti = std::make_shared<torrent_info>(
|
2016-11-04 00:39:42 +01:00
|
|
|
*ses.get_torrents()[0].torrent_file());
|
|
|
|
|
2015-12-10 07:56:33 +01:00
|
|
|
if (flags & disconnect)
|
|
|
|
{
|
|
|
|
ses.remove_torrent(ses.get_torrents()[0]);
|
|
|
|
}
|
2016-11-04 00:39:42 +01:00
|
|
|
|
|
|
|
if (flags & readd)
|
|
|
|
{
|
|
|
|
add_torrent_params p = default_add_torrent;
|
|
|
|
p.ti = ti;
|
|
|
|
p.save_path = ".";
|
|
|
|
ses.add_torrent(p);
|
|
|
|
}
|
2015-12-10 07:56:33 +01:00
|
|
|
}
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
2015-12-10 07:56:33 +01:00
|
|
|
// terminate
|
|
|
|
, [&](int ticks, lt::session& ses) -> bool
|
2015-08-10 15:17:41 +02:00
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
if (flags & reverse)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticks > 70)
|
|
|
|
{
|
|
|
|
TEST_ERROR("timeout");
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-04 00:39:42 +01:00
|
|
|
if ((flags & disconnect) && metadata_alerts > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-10 07:56:33 +01:00
|
|
|
if ((flags & upload_only) && has_metadata(ses))
|
|
|
|
{
|
|
|
|
// the other peer is in upload mode and should not have sent any
|
|
|
|
// actual payload to us
|
|
|
|
TEST_CHECK(!is_seed(ses));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_seed(ses))
|
|
|
|
{
|
|
|
|
TEST_CHECK((flags & upload_only) == 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
TEST_EQUAL(metadata_alerts, 1);
|
|
|
|
}
|
2015-08-10 15:17:41 +02:00
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_encryption_reverse)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
run_metadata_test(full_encryption | reverse);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_encryption_utp)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
run_metadata_test(full_encryption | utp);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_reverse)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
run_metadata_test(reverse);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_upload_only)
|
|
|
|
{
|
2015-12-10 07:56:33 +01:00
|
|
|
run_metadata_test(upload_only);
|
2015-08-10 15:17:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 00:39:42 +01:00
|
|
|
TORRENT_TEST(ut_metadata_disconnect)
|
|
|
|
{
|
|
|
|
run_metadata_test(disconnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_disconnect_readd)
|
|
|
|
{
|
|
|
|
run_metadata_test(disconnect | readd);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(ut_metadata_upload_only_disconnect_readd)
|
|
|
|
{
|
|
|
|
run_metadata_test(upload_only | disconnect | readd);
|
|
|
|
}
|
|
|
|
|