2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
2012-12-31 07:54:54 +01:00
|
|
|
Copyright (c) 2008-2012, Arvid Norberg
|
2008-07-01 20:59:13 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-09-24 20:51:04 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
2008-04-05 23:18:27 +02:00
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2015-08-31 03:51:32 +02:00
|
|
|
#include "libtorrent/socket_io.hpp" // for print_endpoint
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "libtorrent/hex.hpp" // from_hex
|
2016-10-25 14:03:26 +02:00
|
|
|
#include "libtorrent/fingerprint.hpp"
|
2007-05-22 22:44:18 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
2013-06-26 21:16:21 +02:00
|
|
|
#include "setup_transfer.hpp"
|
2007-05-22 22:44:18 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2007-05-22 22:44:18 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
TORRENT_TEST(retry_interval)
|
2007-05-22 22:44:18 +02:00
|
|
|
{
|
2012-03-24 16:15:36 +01:00
|
|
|
// make sure the retry interval keeps growing
|
|
|
|
// on failing announces
|
|
|
|
announce_entry ae("dummy");
|
2019-12-11 17:36:43 +01:00
|
|
|
ae.endpoints.emplace_back(aux::listen_socket_handle(), false);
|
2012-03-24 16:15:36 +01:00
|
|
|
int last = 0;
|
2017-02-06 01:18:06 +01:00
|
|
|
auto const tracker_backoff = 250;
|
2012-03-24 16:15:36 +01:00
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
{
|
2017-04-28 05:34:39 +02:00
|
|
|
ae.endpoints.front().failed(tracker_backoff, seconds32(5));
|
|
|
|
int const delay = static_cast<int>(total_seconds(ae.endpoints.front().next_announce - clock_type::now()));
|
2012-03-24 16:15:36 +01:00
|
|
|
TEST_CHECK(delay > last);
|
|
|
|
last = delay;
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("%d, ", delay);
|
2012-03-24 16:15:36 +01:00
|
|
|
}
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("\n");
|
2018-07-20 13:04:57 +02:00
|
|
|
}
|
2012-03-24 16:15:36 +01:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
TORRENT_TEST(error_code)
|
|
|
|
{
|
2009-12-06 00:03:48 +01:00
|
|
|
TEST_CHECK(error_code(errors::http_error).message() == "HTTP error");
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_CHECK(error_code(errors::missing_file_sizes).message()
|
|
|
|
== "missing or invalid 'file sizes' entry");
|
2018-06-12 00:46:22 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_CHECK(error_code(errors::unsupported_protocol_version).message()
|
|
|
|
== "unsupported protocol version");
|
2018-06-12 00:46:22 +02:00
|
|
|
#endif
|
2009-12-06 00:03:48 +01:00
|
|
|
TEST_CHECK(error_code(errors::no_i2p_router).message() == "no i2p router is set up");
|
|
|
|
TEST_CHECK(error_code(errors::http_parse_error).message() == "Invalid HTTP header");
|
|
|
|
TEST_CHECK(error_code(errors::error_code_max).message() == "Unknown error");
|
|
|
|
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_CHECK(error_code(errors::unauthorized, http_category()).message()
|
|
|
|
== "401 Unauthorized");
|
|
|
|
TEST_CHECK(error_code(errors::service_unavailable, http_category()).message()
|
|
|
|
== "503 Service Unavailable");
|
2018-07-20 13:04:57 +02:00
|
|
|
}
|
2013-02-24 11:02:26 +01:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 7
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-truncation="
|
|
|
|
#endif
|
2009-07-20 03:54:51 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
TORRENT_TEST(snprintf)
|
|
|
|
{
|
2009-07-20 03:54:51 +02:00
|
|
|
char msg[10];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(msg, sizeof(msg), "too %s format string", "long");
|
2009-07-20 03:54:51 +02:00
|
|
|
TEST_CHECK(strcmp(msg, "too long ") == 0);
|
2018-07-20 13:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined __GNUC__ && __GNUC__ >= 7
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_TEST(address_to_from_string)
|
|
|
|
{
|
|
|
|
if (!supports_ipv6()) return;
|
2009-07-20 03:54:51 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
error_code ec;
|
|
|
|
// make sure the assumption we use in peer list hold
|
|
|
|
std::multimap<address, int> peers;
|
|
|
|
std::multimap<address, int>::iterator i;
|
|
|
|
peers.insert(std::make_pair(address::from_string("::1", ec), 0));
|
|
|
|
peers.insert(std::make_pair(address::from_string("::2", ec), 3));
|
|
|
|
peers.insert(std::make_pair(address::from_string("::3", ec), 5));
|
|
|
|
i = peers.find(address::from_string("::2", ec));
|
|
|
|
TEST_CHECK(i != peers.end());
|
|
|
|
if (i != peers.end())
|
2009-04-13 07:08:47 +02:00
|
|
|
{
|
2018-07-20 13:04:57 +02:00
|
|
|
TEST_CHECK(i->first == address::from_string("::2", ec));
|
|
|
|
TEST_CHECK(i->second == 3);
|
2009-04-13 07:08:47 +02:00
|
|
|
}
|
2018-07-20 13:04:57 +02:00
|
|
|
}
|
2009-04-13 07:08:47 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
TORRENT_TEST(address_endpoint_io)
|
|
|
|
{
|
2017-03-31 04:55:54 +02:00
|
|
|
// test print_endpoint, print_address
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_EQUAL(print_endpoint(ep("127.0.0.1", 23)), "127.0.0.1:23");
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(print_address(addr4("241.124.23.5")), "241.124.23.5");
|
2017-03-31 04:55:54 +02:00
|
|
|
|
|
|
|
TEST_EQUAL(print_endpoint(ep("ff::1", 1214)), "[ff::1]:1214");
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(print_address(addr6("2001:ff::1")), "2001:ff::1");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// test address_to_bytes
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(address_to_bytes(addr4("10.11.12.13")), "\x0a\x0b\x0c\x0d");
|
|
|
|
TEST_EQUAL(address_to_bytes(addr4("16.5.127.1")), "\x10\x05\x7f\x01");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// test endpoint_to_bytes
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(endpoint_to_bytes(uep("10.11.12.13", 8080)), "\x0a\x0b\x0c\x0d\x1f\x90");
|
|
|
|
TEST_EQUAL(endpoint_to_bytes(uep("16.5.127.1", 12345)), "\x10\x05\x7f\x01\x30\x39");
|
2018-07-20 13:04:57 +02:00
|
|
|
}
|
2016-10-25 14:03:26 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
TORRENT_TEST(gen_fingerprint)
|
|
|
|
{
|
2016-10-25 14:03:26 +02:00
|
|
|
TEST_EQUAL(generate_fingerprint("AB", 1, 2, 3, 4), "-AB1234-");
|
|
|
|
TEST_EQUAL(generate_fingerprint("AB", 1, 2), "-AB1200-");
|
|
|
|
TEST_EQUAL(generate_fingerprint("..", 1, 10), "-..1A00-");
|
|
|
|
TEST_EQUAL(generate_fingerprint("CZ", 1, 15), "-CZ1F00-");
|
|
|
|
TEST_EQUAL(generate_fingerprint("CZ", 1, 15, 16, 17), "-CZ1FGH-");
|
2007-05-22 22:44:18 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:03:58 +02:00
|
|
|
TORRENT_TEST(printf_int64)
|
|
|
|
{
|
|
|
|
char buffer[100];
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t val = 345678901234567ll;
|
2016-05-16 03:03:58 +02:00
|
|
|
std::snprintf(buffer, sizeof(buffer), "%" PRId64 " %s", val, "end");
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_EQUAL(buffer, std::string("345678901234567 end"));
|
2016-05-16 03:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(printf_uint64)
|
|
|
|
{
|
|
|
|
char buffer[100];
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t val = 18446744073709551615ull;
|
2016-05-16 03:03:58 +02:00
|
|
|
std::snprintf(buffer, sizeof(buffer), "%" PRIu64 " %s", val, "end");
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_EQUAL(buffer, std::string("18446744073709551615 end"));
|
2016-05-16 03:03:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 7
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-truncation="
|
|
|
|
#endif
|
|
|
|
|
2016-05-16 03:03:58 +02:00
|
|
|
TORRENT_TEST(printf_trunc)
|
|
|
|
{
|
|
|
|
char buffer[4];
|
|
|
|
int val = 184;
|
|
|
|
std::snprintf(buffer, sizeof(buffer), "%d %s", val, "end");
|
2019-09-23 08:32:14 +02:00
|
|
|
TEST_EQUAL(buffer, std::string("184"));
|
2016-05-16 03:03:58 +02:00
|
|
|
}
|
2016-10-02 21:27:50 +02:00
|
|
|
|
2018-07-20 13:04:57 +02:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 7
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-10-02 21:27:50 +02:00
|
|
|
TORRENT_TEST(error_condition)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
error_code ec(ERROR_FILE_NOT_FOUND, system_category());
|
|
|
|
#else
|
|
|
|
error_code ec(ENOENT, system_category());
|
|
|
|
#endif
|
|
|
|
TEST_CHECK(ec == boost::system::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|