1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
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"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/add_torrent_params.hpp"
#include "libtorrent/magnet_uri.hpp"
#include "libtorrent/extensions/ut_metadata.hpp"
using namespace lt;
#ifndef TORRENT_DISABLE_EXTENSIONS
enum flags_t
{
// disconnect immediately after receiving the metadata (to test that
// edge case, it caused a crash once)
disconnect = 1,
// force encryption (to make sure the plugin uses the peer_connection
// API in a compatible way)
full_encryption = 2,
// have the downloader connect to the seeder
// (instead of the other way around)
reverse = 4,
// only use uTP
utp = 8,
// upload-only mode
upload_only = 16,
// re-add the torrent after removing
readd = 32,
// token limit is too low
token_limit = 64,
};
void run_metadata_test(int flags)
{
int metadata_alerts = 0;
int metadata_failed_alerts = 0;
sim::default_config cfg;
sim::simulation sim{cfg};
lt::settings_pack default_settings = settings();
if (flags & full_encryption)
enable_enc(default_settings);
if (flags & utp)
utp_only(default_settings);
if (flags & token_limit)
default_settings.set_int(settings_pack::metadata_token_limit, 10);
lt::add_torrent_params default_add_torrent;
if (flags & upload_only)
{
default_add_torrent.flags |= torrent_flags::upload_mode;
}
std::shared_ptr<lt::torrent_info> ti;
// TODO: we use real_disk here because the test disk io doesn't support
// multiple torrents, and readd will add back the same torrent before the
// first one is done being removed
setup_swarm(2, ((flags & reverse) ? swarm_test::upload : swarm_test::download)
| ((flags & readd) ? swarm_test::real_disk : swarm_test_t{})
, sim
, default_settings
, default_add_torrent
// add session
, [](lt::settings_pack&) {}
// add torrent
, [&ti](lt::add_torrent_params& params) {
// we want to add the torrent via magnet link
error_code ec;
ti = params.ti;
params.ti.reset();
add_torrent_params const p = parse_magnet_uri(
lt::make_magnet_uri(*ti), ec);
TEST_CHECK(!ec);
params.name = p.name;
params.trackers = p.trackers;
params.tracker_tiers = p.tracker_tiers;
params.url_seeds = p.url_seeds;
params.info_hashes = p.info_hashes;
params.peers = p.peers;
#ifndef TORRENT_DISABLE_DHT
params.dht_nodes = p.dht_nodes;
#endif
params.flags &= ~torrent_flags::upload_mode;
}
// on alert
, [&](lt::alert const* a, lt::session& ses) {
if (alert_cast<metadata_failed_alert>(a))
{
metadata_failed_alerts += 1;
}
else if (alert_cast<metadata_received_alert>(a))
{
metadata_alerts += 1;
if (flags & disconnect)
{
ses.remove_torrent(ses.get_torrents()[0]);
}
if (flags & readd)
{
add_torrent_params p = default_add_torrent;
p.ti = ti;
p.save_path = ".";
ses.add_torrent(p);
}
}
}
// terminate
, [&](int ticks, lt::session& ses) -> bool
{
if (flags & reverse)
{
return true;
}
if (ticks > 70)
{
TEST_ERROR("timeout");
return true;
}
if ((flags & token_limit) && metadata_failed_alerts > 0)
{
return true;
}
if ((flags & disconnect) && metadata_alerts > 0)
{
return true;
}
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;
});
if (flags & token_limit)
{
TEST_EQUAL(metadata_failed_alerts, 1);
}
else
{
TEST_EQUAL(metadata_alerts, 1);
}
}
TORRENT_TEST(ut_metadata_encryption_reverse)
{
run_metadata_test(full_encryption | reverse);
}
TORRENT_TEST(ut_metadata_encryption_utp)
{
run_metadata_test(full_encryption | utp);
}
TORRENT_TEST(ut_metadata_reverse)
{
run_metadata_test(reverse);
}
TORRENT_TEST(ut_metadata_upload_only)
{
run_metadata_test(upload_only);
}
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);
}
TORRENT_TEST(ut_metadata_token_limit)
{
run_metadata_test(token_limit);
}
#else
TORRENT_TEST(disabled) {}
#endif // TORRENT_DISABLE_EXTENSIONS
|