2008-07-01 18:59:13 +00:00
/*
2012-12-31 06:54:54 +00:00
Copyright ( c ) 2008 - 2012 , Arvid Norberg
2008-07-01 18:59:13 +00: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 .
*/
2010-03-01 06:46:57 +00:00
# include "libtorrent/magnet_uri.hpp"
2008-05-17 14:19:34 +00:00
# include "libtorrent/parse_url.hpp"
2007-05-22 20:44:18 +00:00
# include "libtorrent/http_tracker_connection.hpp"
# include "libtorrent/buffer.hpp"
2007-09-24 18:51:04 +00:00
# include "libtorrent/entry.hpp"
2008-05-28 02:35:02 +00:00
# include "libtorrent/bitfield.hpp"
2007-09-24 18:51:04 +00:00
# include "libtorrent/torrent_info.hpp"
2007-12-02 18:34:37 +00:00
# include "libtorrent/escape_string.hpp"
2008-04-05 21:18:27 +00:00
# include "libtorrent/broadcast_socket.hpp"
2009-04-12 18:46:07 +00:00
# include "libtorrent/identify_client.hpp"
2009-10-26 01:29:39 +00:00
# include "libtorrent/file.hpp"
2010-11-29 01:33:05 +00:00
# include "libtorrent/packet_buffer.hpp"
2009-12-03 05:11:57 +00:00
# include "libtorrent/session.hpp"
# include "libtorrent/bencode.hpp"
2010-11-29 01:33:05 +00:00
# include "libtorrent/timestamp_history.hpp"
# include "libtorrent/enum_net.hpp"
2010-12-24 01:31:41 +00:00
# include "libtorrent/bloom_filter.hpp"
# include "libtorrent/aux_/session_impl.hpp"
2011-05-23 23:42:52 +00:00
# include "libtorrent/rsa.hpp"
2013-01-01 23:12:16 +00:00
# include "libtorrent/ip_voter.hpp"
2007-08-07 01:32:38 +00:00
# include <boost/bind.hpp>
2009-12-15 13:11:07 +00:00
# include <iostream>
2013-08-01 04:58:43 +00:00
# include <set>
2007-05-22 20:44:18 +00:00
# include "test.hpp"
2013-06-26 19:16:21 +00:00
# include "setup_transfer.hpp"
2007-05-22 20:44:18 +00:00
using namespace libtorrent ;
2009-05-28 07:25:09 +00:00
namespace libtorrent {
2009-12-02 17:46:25 +00:00
TORRENT_EXPORT std : : string sanitize_path ( std : : string const & p ) ;
2009-05-28 07:25:09 +00:00
}
2009-04-04 21:44:04 +00:00
sha1_hash to_hash ( char const * s )
{
sha1_hash ret ;
from_hex ( s , 40 , ( char * ) & ret [ 0 ] ) ;
return ret ;
}
2009-09-09 17:56:25 +00:00
namespace libtorrent
{
// defined in torrent_info.cpp
2009-12-02 17:46:25 +00:00
TORRENT_EXPORT bool verify_encoding ( std : : string & target , bool path = true ) ;
2009-09-09 17:56:25 +00:00
}
2010-12-24 01:31:41 +00:00
address rand_v4 ( )
{
2010-12-30 03:46:11 +00:00
return address_v4 ( ( rand ( ) < < 16 | rand ( ) ) & 0xffffffff ) ;
2010-12-24 01:31:41 +00:00
}
2013-06-16 00:59:29 +00:00
# if TORRENT_USE_IPV6
2013-01-01 23:12:16 +00:00
address rand_v6 ( )
{
address_v6 : : bytes_type bytes ;
for ( int i = 0 ; i < bytes . size ( ) ; + + i ) bytes [ i ] = rand ( ) ;
return address_v6 ( bytes ) ;
}
2013-06-16 00:59:29 +00:00
# endif
2013-01-01 23:12:16 +00:00
2007-05-22 20:44:18 +00:00
int test_main ( )
{
using namespace libtorrent ;
2011-01-19 05:57:44 +00:00
using namespace libtorrent : : dht ;
2009-10-26 01:29:39 +00:00
error_code ec ;
2009-12-03 05:11:57 +00:00
int ret = 0 ;
2012-03-24 15:15:36 +00:00
// make sure the retry interval keeps growing
// on failing announces
announce_entry ae ( " dummy " ) ;
int last = 0 ;
2012-03-29 03:51:22 +00:00
session_settings sett ;
sett . tracker_backoff = 250 ;
2012-03-24 15:15:36 +00:00
for ( int i = 0 ; i < 10 ; + + i )
{
2012-03-29 03:51:22 +00:00
ae . failed ( sett , 5 ) ;
2012-03-24 15:15:36 +00:00
int delay = ae . next_announce_in ( ) ;
TEST_CHECK ( delay > last ) ;
last = delay ;
fprintf ( stderr , " %d, " , delay ) ;
}
fprintf ( stderr , " \n " ) ;
2011-05-23 23:42:52 +00:00
# if defined TORRENT_USE_OPENSSL
// test sign_rsa and verify_rsa
2011-05-25 02:26:07 +00:00
char private_key [ 1192 ] ;
2011-05-23 23:42:52 +00:00
int private_len = sizeof ( private_key ) ;
2011-05-25 02:26:07 +00:00
char public_key [ 268 ] ;
2011-05-23 23:42:52 +00:00
int public_len = sizeof ( public_key ) ;
ret = generate_rsa_keys ( public_key , & public_len , private_key , & private_len , 2048 ) ;
2011-05-25 02:26:07 +00:00
fprintf ( stderr , " keysizes: pub: %d priv: %d \n " , public_len , private_len ) ;
2011-05-23 23:42:52 +00:00
TEST_CHECK ( ret ) ;
char test_message [ 1024 ] ;
std : : generate ( test_message , test_message + 1024 , & std : : rand ) ;
char signature [ 256 ] ;
2011-05-25 02:26:07 +00:00
int sig_len = sign_rsa ( hasher ( test_message , sizeof ( test_message ) ) . final ( )
2011-05-23 23:42:52 +00:00
, private_key , private_len , signature , sizeof ( signature ) ) ;
TEST_CHECK ( sig_len = = 256 ) ;
2011-05-25 02:26:07 +00:00
ret = verify_rsa ( hasher ( test_message , sizeof ( test_message ) ) . final ( )
, public_key , public_len , signature , sig_len ) ;
2011-05-23 23:42:52 +00:00
TEST_CHECK ( ret = = 1 ) ;
# endif
2010-12-24 01:31:41 +00:00
// test external ip voting
2013-01-01 23:12:16 +00:00
external_ip ipv1 ;
2010-12-24 01:31:41 +00:00
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 malicious responses from the same peer
2013-01-06 19:53:17 +00:00
address real_external = address_v4 : : from_string ( " 5.5.5.5 " , ec ) ;
TEST_CHECK ( ! ec ) ;
address malicious = address_v4 : : from_string ( " 4.4.4.4 " , ec ) ;
TEST_CHECK ( ! ec ) ;
2010-12-24 01:31:41 +00:00
for ( int i = 0 ; i < 50 ; + + i )
{
2013-01-01 23:12:16 +00:00
ipv1 . cast_vote ( real_external , aux : : session_impl : : source_dht , rand_v4 ( ) ) ;
ipv1 . cast_vote ( rand_v4 ( ) , aux : : session_impl : : source_dht , malicious ) ;
2010-12-24 01:31:41 +00:00
}
2013-01-01 23:12:16 +00:00
TEST_CHECK ( ipv1 . external_address ( rand_v4 ( ) ) = = real_external ) ;
external_ip ipv2 ;
2010-12-24 01:31:41 +00:00
// test a single malicious node
// adds 50 legitimate responses from different peers
// and 50 consistent malicious responses from the same peer
2013-01-06 19:53:17 +00:00
address real_external1 = address_v4 : : from_string ( " 5.5.5.5 " , ec ) ;
TEST_CHECK ( ! ec ) ;
address real_external2 ;
2013-06-16 00:59:29 +00:00
# if TORRENT_USE_IPV6
2013-01-06 19:53:17 +00:00
if ( supports_ipv6 ( ) )
{
real_external2 = address_v6 : : from_string ( " 2f80:: " , ec ) ;
TEST_CHECK ( ! ec ) ;
}
2013-06-16 00:59:29 +00:00
# endif
2013-01-06 19:53:17 +00:00
malicious = address_v4 : : from_string ( " 4.4.4.4 " , ec ) ;
TEST_CHECK ( ! ec ) ;
address malicious_external = address_v4 : : from_string ( " 3.3.3.3 " , ec ) ;
TEST_CHECK ( ! ec ) ;
2010-12-24 01:31:41 +00:00
for ( int i = 0 ; i < 50 ; + + i )
{
2013-01-01 23:12:16 +00:00
ipv2 . cast_vote ( real_external1 , aux : : session_impl : : source_dht , rand_v4 ( ) ) ;
2013-06-16 00:59:29 +00:00
# if TORRENT_USE_IPV6
2013-01-06 19:53:17 +00:00
if ( supports_ipv6 ( ) )
ipv2 . cast_vote ( real_external2 , aux : : session_impl : : source_dht , rand_v6 ( ) ) ;
2013-06-16 00:59:29 +00:00
# endif
2013-01-01 23:12:16 +00:00
ipv2 . cast_vote ( malicious_external , aux : : session_impl : : source_dht , malicious ) ;
2010-12-24 01:31:41 +00:00
}
2013-01-01 23:12:16 +00:00
TEST_CHECK ( ipv2 . external_address ( rand_v4 ( ) ) = = real_external1 ) ;
2013-06-16 00:59:29 +00:00
# if TORRENT_USE_IPV6
2013-01-06 19:53:17 +00:00
if ( supports_ipv6 ( ) )
TEST_CHECK ( ipv2 . external_address ( rand_v6 ( ) ) = = real_external2 ) ;
2013-06-16 00:59:29 +00:00
# endif
2010-12-24 01:31:41 +00:00
// test bloom_filter
bloom_filter < 32 > filter ;
sha1_hash k1 = hasher ( " test1 " , 5 ) . final ( ) ;
sha1_hash k2 = hasher ( " test2 " , 5 ) . final ( ) ;
sha1_hash k3 = hasher ( " test3 " , 5 ) . final ( ) ;
sha1_hash k4 = hasher ( " test4 " , 5 ) . final ( ) ;
TEST_CHECK ( ! filter . find ( k1 ) ) ;
TEST_CHECK ( ! filter . find ( k2 ) ) ;
TEST_CHECK ( ! filter . find ( k3 ) ) ;
TEST_CHECK ( ! filter . find ( k4 ) ) ;
filter . set ( k1 ) ;
TEST_CHECK ( filter . find ( k1 ) ) ;
TEST_CHECK ( ! filter . find ( k2 ) ) ;
TEST_CHECK ( ! filter . find ( k3 ) ) ;
TEST_CHECK ( ! filter . find ( k4 ) ) ;
filter . set ( k4 ) ;
TEST_CHECK ( filter . find ( k1 ) ) ;
TEST_CHECK ( ! filter . find ( k2 ) ) ;
TEST_CHECK ( ! filter . find ( k3 ) ) ;
TEST_CHECK ( filter . find ( k4 ) ) ;
2010-11-29 01:33:05 +00:00
// test timestamp_history
{
timestamp_history h ;
TEST_EQUAL ( h . add_sample ( 0x32 , false ) , 0 ) ;
TEST_EQUAL ( h . base ( ) , 0x32 ) ;
TEST_EQUAL ( h . add_sample ( 0x33 , false ) , 0x1 ) ;
TEST_EQUAL ( h . base ( ) , 0x32 ) ;
TEST_EQUAL ( h . add_sample ( 0x3433 , false ) , 0x3401 ) ;
TEST_EQUAL ( h . base ( ) , 0x32 ) ;
TEST_EQUAL ( h . add_sample ( 0x30 , false ) , 0 ) ;
TEST_EQUAL ( h . base ( ) , 0x30 ) ;
// test that wrapping of the timestamp is properly handled
h . add_sample ( 0xfffffff3 , false ) ;
TEST_EQUAL ( h . base ( ) , 0xfffffff3 ) ;
2013-02-24 10:02:26 +00:00
// TODO: test the case where we have > 120 samples (and have the base delay actually be updated)
// TODO: test the case where a sample is lower than the history entry but not lower than the base
2010-11-29 01:33:05 +00:00
}
// test packet_buffer
{
packet_buffer pb ;
TEST_EQUAL ( pb . capacity ( ) , 0 ) ;
TEST_EQUAL ( pb . size ( ) , 0 ) ;
TEST_EQUAL ( pb . span ( ) , 0 ) ;
pb . insert ( 123 , ( void * ) 123 ) ;
TEST_EQUAL ( pb . at ( 123 + 16 ) , 0 ) ;
TEST_CHECK ( pb . at ( 123 ) = = ( void * ) 123 ) ;
TEST_CHECK ( pb . capacity ( ) > 0 ) ;
TEST_EQUAL ( pb . size ( ) , 1 ) ;
TEST_EQUAL ( pb . span ( ) , 1 ) ;
TEST_EQUAL ( pb . cursor ( ) , 123 ) ;
pb . insert ( 125 , ( void * ) 125 ) ;
TEST_CHECK ( pb . at ( 125 ) = = ( void * ) 125 ) ;
TEST_EQUAL ( pb . size ( ) , 2 ) ;
TEST_EQUAL ( pb . span ( ) , 3 ) ;
TEST_EQUAL ( pb . cursor ( ) , 123 ) ;
pb . insert ( 500 , ( void * ) 500 ) ;
TEST_EQUAL ( pb . size ( ) , 3 ) ;
TEST_EQUAL ( pb . span ( ) , 501 - 123 ) ;
TEST_EQUAL ( pb . capacity ( ) , 512 ) ;
2012-03-25 05:40:00 +00:00
pb . insert ( 500 , ( void * ) 501 ) ;
TEST_EQUAL ( pb . size ( ) , 3 ) ;
pb . insert ( 500 , ( void * ) 500 ) ;
TEST_EQUAL ( pb . size ( ) , 3 ) ;
2010-11-29 01:33:05 +00:00
TEST_CHECK ( pb . remove ( 123 ) = = ( void * ) 123 ) ;
TEST_EQUAL ( pb . size ( ) , 2 ) ;
TEST_EQUAL ( pb . span ( ) , 501 - 125 ) ;
TEST_EQUAL ( pb . cursor ( ) , 125 ) ;
TEST_CHECK ( pb . remove ( 125 ) = = ( void * ) 125 ) ;
TEST_EQUAL ( pb . size ( ) , 1 ) ;
TEST_EQUAL ( pb . span ( ) , 1 ) ;
TEST_EQUAL ( pb . cursor ( ) , 500 ) ;
TEST_CHECK ( pb . remove ( 500 ) = = ( void * ) 500 ) ;
TEST_EQUAL ( pb . size ( ) , 0 ) ;
TEST_EQUAL ( pb . span ( ) , 0 ) ;
for ( int i = 0 ; i < 0xff ; + + i )
{
int index = ( i + 0xfff0 ) & 0xffff ;
pb . insert ( index , ( void * ) ( index + 1 ) ) ;
fprintf ( stderr , " insert: %u (mask: %x) \n " , index , int ( pb . capacity ( ) - 1 ) ) ;
TEST_EQUAL ( pb . capacity ( ) , 512 ) ;
if ( i > = 14 )
{
index = ( index - 14 ) & 0xffff ;
fprintf ( stderr , " remove: %u \n " , index ) ;
TEST_CHECK ( pb . remove ( index ) = = ( void * ) ( index + 1 ) ) ;
TEST_EQUAL ( pb . size ( ) , 14 ) ;
}
}
}
{
// test wrapping the indices
packet_buffer pb ;
TEST_EQUAL ( pb . size ( ) , 0 ) ;
pb . insert ( 0xfffe , ( void * ) 1 ) ;
TEST_CHECK ( pb . at ( 0xfffe ) = = ( void * ) 1 ) ;
pb . insert ( 2 , ( void * ) 2 ) ;
TEST_CHECK ( pb . at ( 2 ) = = ( void * ) 2 ) ;
pb . remove ( 0xfffe ) ;
TEST_CHECK ( pb . at ( 0xfffe ) = = ( void * ) 0 ) ;
TEST_CHECK ( pb . at ( 2 ) = = ( void * ) 2 ) ;
}
2012-11-25 07:03:37 +00:00
{
// test wrapping the indices
packet_buffer pb ;
TEST_EQUAL ( pb . size ( ) , 0 ) ;
pb . insert ( 0xfff3 , ( void * ) 1 ) ;
TEST_CHECK ( pb . at ( 0xfff3 ) = = ( void * ) 1 ) ;
int new_index = ( 0xfff3 + pb . capacity ( ) ) & 0xffff ;
pb . insert ( new_index , ( void * ) 2 ) ;
TEST_CHECK ( pb . at ( new_index ) = = ( void * ) 2 ) ;
void * old = pb . remove ( 0xfff3 ) ;
TEST_CHECK ( old = = ( void * ) 1 ) ;
TEST_CHECK ( pb . at ( 0xfff3 ) = = ( void * ) 0 ) ;
TEST_CHECK ( pb . at ( new_index ) = = ( void * ) 2 ) ;
}
2013-02-24 10:02:26 +00:00
{
// test wrapping the indices backwards
packet_buffer pb ;
TEST_EQUAL ( pb . size ( ) , 0 ) ;
pb . insert ( 0xfff3 , ( void * ) 1 ) ;
TEST_CHECK ( pb . at ( 0xfff3 ) = = ( void * ) 1 ) ;
int new_index = ( 0xfff3 + pb . capacity ( ) ) & 0xffff ;
pb . insert ( new_index , ( void * ) 2 ) ;
TEST_CHECK ( pb . at ( new_index ) = = ( void * ) 2 ) ;
void * old = pb . remove ( 0xfff3 ) ;
TEST_CHECK ( old = = ( void * ) 1 ) ;
TEST_CHECK ( pb . at ( 0xfff3 ) = = ( void * ) 0 ) ;
TEST_CHECK ( pb . at ( new_index ) = = ( void * ) 2 ) ;
pb . insert ( 0xffff , ( void * ) 0xffff ) ;
}
// test error codes
2009-12-05 23:03:48 +00:00
TEST_CHECK ( error_code ( errors : : http_error ) . message ( ) = = " HTTP error " ) ;
TEST_CHECK ( error_code ( errors : : missing_file_sizes ) . message ( ) = = " missing or invalid 'file sizes' entry " ) ;
TEST_CHECK ( error_code ( errors : : unsupported_protocol_version ) . message ( ) = = " unsupported protocol version " ) ;
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 " ) ;
2013-02-24 10:02:26 +00:00
TEST_CHECK ( error_code ( errors : : unauthorized , get_http_category ( ) ) . message ( ) = = " 401 Unauthorized " ) ;
TEST_CHECK ( error_code ( errors : : service_unavailable , get_http_category ( ) ) . message ( ) = = " 503 Service Unavailable " ) ;
2013-09-01 01:10:50 +00:00
session_proxy p1 ;
session_proxy p2 ;
2009-12-03 05:11:57 +00:00
{
// test session state load/restore
session * s = new session ( fingerprint ( " LT " , 0 , 0 , 0 , 0 ) , 0 ) ;
session_settings sett ;
sett . user_agent = " test " ;
sett . tracker_receive_timeout = 1234 ;
sett . file_pool_size = 543 ;
sett . urlseed_wait_retry = 74 ;
sett . file_pool_size = 754 ;
sett . initial_picker_threshold = 351 ;
sett . upnp_ignore_nonrouters = 5326 ;
sett . coalesce_writes = 623 ;
sett . auto_scrape_interval = 753 ;
sett . close_redundant_connections = 245 ;
sett . auto_scrape_interval = 235 ;
sett . auto_scrape_min_interval = 62 ;
s - > set_settings ( sett ) ;
2010-10-10 02:22:57 +00:00
# ifndef TORRENT_DISABLE_DHT
dht_settings dhts ;
dhts . max_peers_reply = 70 ;
s - > set_dht_settings ( dhts ) ;
# endif
2009-12-03 05:11:57 +00:00
/*
# ifndef TORRENT_DISABLE_DHT
dht_settings dht_sett ;
s - > set_dht_settings ( dht_sett ) ;
# endif
*/
entry session_state ;
s - > save_state ( session_state ) ;
2010-03-01 06:46:57 +00:00
// test magnet link parsing
add_torrent_params p ;
p . save_path = " . " ;
error_code ec ;
2012-03-19 04:02:13 +00:00
p . url = " magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd "
2012-03-21 16:27:50 +00:00
" &tr=http://1 "
" &tr=http://2 "
" &tr=http://3 "
" &dn=foo "
" &dht=127.0.0.1:43 " ;
2012-03-19 04:02:13 +00:00
torrent_handle t = s - > add_torrent ( p , ec ) ;
2010-03-01 06:46:57 +00:00
TEST_CHECK ( ! ec ) ;
if ( ec ) fprintf ( stderr , " %s \n " , ec . message ( ) . c_str ( ) ) ;
2012-03-21 16:27:50 +00:00
std : : vector < announce_entry > trackers = t . trackers ( ) ;
TEST_EQUAL ( trackers . size ( ) , 3 ) ;
2013-08-01 04:58:43 +00:00
std : : set < std : : string > trackers_set ;
for ( std : : vector < announce_entry > : : iterator i = trackers . begin ( )
, end ( trackers . end ( ) ) ; i ! = end ; + + i )
trackers_set . insert ( i - > url ) ;
TEST_CHECK ( trackers_set . count ( " http://1 " ) = = 1 ) ;
TEST_CHECK ( trackers_set . count ( " http://2 " ) = = 1 ) ;
TEST_CHECK ( trackers_set . count ( " http://3 " ) = = 1 ) ;
2012-03-21 16:27:50 +00:00
2012-03-19 04:02:13 +00:00
p . url = " magnet: "
2012-03-21 16:27:50 +00:00
" ?tr=http://1 "
" &tr=http://2 "
" &dn=foo "
" &dht=127.0.0.1:43 "
2011-05-25 16:26:39 +00:00
" &xt=urn:btih:c352cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd " ;
2012-03-19 04:02:13 +00:00
torrent_handle t2 = s - > add_torrent ( p , ec ) ;
2011-05-25 16:26:39 +00:00
TEST_CHECK ( ! ec ) ;
if ( ec ) fprintf ( stderr , " %s \n " , ec . message ( ) . c_str ( ) ) ;
2012-03-21 16:27:50 +00:00
trackers = t2 . trackers ( ) ;
TEST_EQUAL ( trackers . size ( ) , 2 ) ;
2012-03-19 04:02:13 +00:00
p . url = " magnet: "
2011-05-25 16:26:39 +00:00
" ?tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80 "
" &tr=udp%3A%2F%2Ftracker.publicbt.com%3A80 "
" &tr=udp%3A%2F%2Ftracker.ccc.de%3A80 "
2012-03-21 16:27:50 +00:00
" &xt=urn:btih:a38d02c287893842a32825aa866e00828a318f07 "
" &dn=Ubuntu+11.04+%28Final%29 " ;
2012-03-19 04:02:13 +00:00
torrent_handle t3 = s - > add_torrent ( p , ec ) ;
2011-05-25 16:26:39 +00:00
TEST_CHECK ( ! ec ) ;
if ( ec ) fprintf ( stderr , " %s \n " , ec . message ( ) . c_str ( ) ) ;
2012-03-21 16:27:50 +00:00
trackers = t3 . trackers ( ) ;
2010-03-01 06:46:57 +00:00
TEST_EQUAL ( trackers . size ( ) , 3 ) ;
if ( trackers . size ( ) > 0 )
{
2012-03-21 16:27:50 +00:00
TEST_EQUAL ( trackers [ 0 ] . url , " udp://tracker.openbittorrent.com:80 " ) ;
2010-03-01 06:46:57 +00:00
fprintf ( stderr , " 1: %s \n " , trackers [ 0 ] . url . c_str ( ) ) ;
}
if ( trackers . size ( ) > 1 )
{
2012-03-21 16:27:50 +00:00
TEST_EQUAL ( trackers [ 1 ] . url , " udp://tracker.publicbt.com:80 " ) ;
2010-03-01 06:46:57 +00:00
fprintf ( stderr , " 2: %s \n " , trackers [ 1 ] . url . c_str ( ) ) ;
}
if ( trackers . size ( ) > 2 )
{
2012-03-21 16:27:50 +00:00
TEST_EQUAL ( trackers [ 2 ] . url , " udp://tracker.ccc.de:80 " ) ;
2010-03-01 06:46:57 +00:00
fprintf ( stderr , " 3: %s \n " , trackers [ 2 ] . url . c_str ( ) ) ;
}
TEST_EQUAL ( to_hex ( t . info_hash ( ) . to_string ( ) ) , " cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd " ) ;
2013-09-01 01:10:50 +00:00
p1 = s - > abort ( ) ;
2009-12-03 05:11:57 +00:00
delete s ;
s = new session ( fingerprint ( " LT " , 0 , 0 , 0 , 0 ) , 0 ) ;
std : : vector < char > buf ;
bencode ( std : : back_inserter ( buf ) , session_state ) ;
lazy_entry session_state2 ;
2010-10-28 04:01:59 +00:00
ret = lazy_bdecode ( & buf [ 0 ] , & buf [ 0 ] + buf . size ( ) , session_state2 , ec ) ;
2009-12-03 05:11:57 +00:00
TEST_CHECK ( ret = = 0 ) ;
2010-10-10 02:22:57 +00:00
fprintf ( stderr , " session_state \n %s \n " , print_entry ( session_state2 ) . c_str ( ) ) ;
2013-02-24 10:02:26 +00:00
// parse_magnet_uri
parse_magnet_uri ( " magnet:?dn=foo&dht=127.0.0.1:43 " , p , ec ) ;
TEST_CHECK ( ec = = error_code ( errors : : missing_info_hash_in_uri ) ) ;
ec . clear ( ) ;
parse_magnet_uri ( " magnet:?xt=blah&dn=foo&dht=127.0.0.1:43 " , p , ec ) ;
TEST_CHECK ( ec = = error_code ( errors : : missing_info_hash_in_uri ) ) ;
ec . clear ( ) ;
2013-04-09 01:04:43 +00:00
# ifndef TORRENT_DISABLE_DHT
2013-02-24 10:02:26 +00:00
parse_magnet_uri ( " magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd&dn=foo&dht=127.0.0.1:43 " , p , ec ) ;
TEST_CHECK ( ! ec ) ;
if ( ec ) fprintf ( stderr , " %s \n " , ec . message ( ) . c_str ( ) ) ;
ec . clear ( ) ;
TEST_CHECK ( p . dht_nodes . size ( ) = = 1 ) ;
TEST_CHECK ( p . dht_nodes [ 0 ] . first = = " 127.0.0.1 " ) ;
TEST_CHECK ( p . dht_nodes [ 0 ] . second = = 43 ) ;
2013-04-09 01:04:43 +00:00
# endif
2013-02-24 10:02:26 +00:00
2010-10-10 02:22:57 +00:00
// make sure settings that haven't been changed from their defaults are not saved
TEST_CHECK ( session_state2 . dict_find ( " settings " ) - > dict_find ( " optimistic_disk_retry " ) = = 0 ) ;
2009-12-03 05:11:57 +00:00
s - > load_state ( session_state2 ) ;
# define CMP_SET(x) TEST_CHECK(s->settings().x == sett.x)
CMP_SET ( user_agent ) ;
CMP_SET ( tracker_receive_timeout ) ;
CMP_SET ( file_pool_size ) ;
CMP_SET ( urlseed_wait_retry ) ;
CMP_SET ( file_pool_size ) ;
CMP_SET ( initial_picker_threshold ) ;
CMP_SET ( upnp_ignore_nonrouters ) ;
CMP_SET ( coalesce_writes ) ;
CMP_SET ( auto_scrape_interval ) ;
CMP_SET ( close_redundant_connections ) ;
CMP_SET ( auto_scrape_interval ) ;
CMP_SET ( auto_scrape_min_interval ) ;
CMP_SET ( max_peerlist_size ) ;
CMP_SET ( max_paused_peerlist_size ) ;
CMP_SET ( min_announce_interval ) ;
CMP_SET ( prioritize_partial_pieces ) ;
CMP_SET ( auto_manage_startup ) ;
CMP_SET ( rate_limit_ip_overhead ) ;
CMP_SET ( announce_to_all_trackers ) ;
CMP_SET ( announce_to_all_tiers ) ;
CMP_SET ( prefer_udp_trackers ) ;
CMP_SET ( strict_super_seeding ) ;
CMP_SET ( seeding_piece_quota ) ;
2013-09-01 01:10:50 +00:00
p2 = s - > abort ( ) ;
2009-12-03 05:11:57 +00:00
delete s ;
}
2009-10-26 01:29:39 +00:00
2009-07-20 01:54:51 +00:00
// test snprintf
char msg [ 10 ] ;
snprintf ( msg , sizeof ( msg ) , " too %s format string " , " long " ) ;
TEST_CHECK ( strcmp ( msg , " too long " ) = = 0 ) ;
2009-06-19 18:18:49 +00:00
// test sanitize_path
2009-05-28 07:25:09 +00:00
# ifdef TORRENT_WINDOWS
2009-10-26 01:29:39 +00:00
TEST_EQUAL ( sanitize_path ( " /a/b/c " ) , " a \\ b \\ c " ) ;
TEST_EQUAL ( sanitize_path ( " a/../c " ) , " a \\ c " ) ;
# else
TEST_EQUAL ( sanitize_path ( " /a/b/c " ) , " a/b/c " ) ;
TEST_EQUAL ( sanitize_path ( " a/../c " ) , " a/c " ) ;
# endif
TEST_EQUAL ( sanitize_path ( " /.././c " ) , " c " ) ;
TEST_EQUAL ( sanitize_path ( " dev: " ) , " " ) ;
TEST_EQUAL ( sanitize_path ( " c:/b " ) , " b " ) ;
# ifdef TORRENT_WINDOWS
TEST_EQUAL ( sanitize_path ( " c: \\ . \\ c " ) , " c " ) ;
TEST_EQUAL ( sanitize_path ( " \\ c " ) , " c " ) ;
2009-05-28 07:25:09 +00:00
# else
2009-10-26 01:29:39 +00:00
TEST_EQUAL ( sanitize_path ( " //./c " ) , " c " ) ;
2009-05-28 07:25:09 +00:00
# endif
2009-04-25 21:51:34 +00:00
// make sure the time classes have correct semantics
2009-10-26 01:29:39 +00:00
TEST_EQUAL ( total_milliseconds ( milliseconds ( 100 ) ) , 100 ) ;
TEST_EQUAL ( total_milliseconds ( milliseconds ( 1 ) ) , 1 ) ;
TEST_EQUAL ( total_milliseconds ( seconds ( 1 ) ) , 1000 ) ;
2009-04-25 21:51:34 +00:00
2009-10-26 01:29:39 +00:00
if ( supports_ipv6 ( ) )
2009-04-13 05:08:47 +00:00
{
2009-10-26 01:29:39 +00:00
// make sure the assumption we use in policy's 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 ( ) )
{
TEST_CHECK ( i - > first = = address : : from_string ( " ::2 " , ec ) ) ;
TEST_CHECK ( i - > second = = 3 ) ;
}
2009-04-13 05:08:47 +00:00
}
2009-04-12 18:46:07 +00:00
// test identify_client
TEST_CHECK ( identify_client ( peer_id ( " -AZ1234-............ " ) ) = = " Azureus 1.2.3.4 " ) ;
TEST_CHECK ( identify_client ( peer_id ( " -AZ1230-............ " ) ) = = " Azureus 1.2.3 " ) ;
TEST_CHECK ( identify_client ( peer_id ( " S123--.............. " ) ) = = " Shadow 1.2.3 " ) ;
TEST_CHECK ( identify_client ( peer_id ( " M1-2-3--............ " ) ) = = " Mainline 1.2.3 " ) ;
2009-09-09 17:56:25 +00:00
// verify_encoding
2013-09-01 01:10:50 +00:00
std : : string test = " \b ?filename=4 " ;
2009-09-09 17:56:25 +00:00
TEST_CHECK ( ! verify_encoding ( test ) ) ;
# ifdef TORRENT_WINDOWS
2009-09-27 01:33:36 +00:00
TEST_CHECK ( test = = " __filename=4 " ) ;
2009-09-09 17:56:25 +00:00
# else
2009-09-27 01:33:36 +00:00
TEST_CHECK ( test = = " _?filename=4 " ) ;
2009-09-09 17:56:25 +00:00
# endif
test = " filename=4 " ;
TEST_CHECK ( verify_encoding ( test ) ) ;
TEST_CHECK ( test = = " filename=4 " ) ;
2007-05-22 20:44:18 +00:00
2013-02-24 10:02:26 +00:00
// valid 2-byte sequence
test = " filename \xc2 \xa1 " ;
TEST_CHECK ( verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
2013-02-24 10:02:26 +00:00
TEST_CHECK ( test = = " filename \xc2 \xa1 " ) ;
// truncated 2-byte sequence
test = " filename \xc2 " ;
TEST_CHECK ( ! verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename_ " ) ;
2013-02-24 10:02:26 +00:00
// valid 3-byte sequence
test = " filename \xe2 \x9f \xb9 " ;
TEST_CHECK ( verify_encoding ( test ) ) ;
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename \xe2 \x9f \xb9 " ) ;
// truncated 3-byte sequence
test = " filename \xe2 \x9f " ;
TEST_CHECK ( ! verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename_ " ) ;
2013-02-24 10:02:26 +00:00
// truncated 3-byte sequence
test = " filename \xe2 " ;
TEST_CHECK ( ! verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename_ " ) ;
2013-02-24 10:02:26 +00:00
// valid 4-byte sequence
test = " filename \xf0 \x9f \x92 \x88 " ;
TEST_CHECK ( verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
2013-02-24 10:02:26 +00:00
TEST_CHECK ( test = = " filename \xf0 \x9f \x92 \x88 " ) ;
// truncated 4-byte sequence
test = " filename \xf0 \x9f \x92 " ;
TEST_CHECK ( ! verify_encoding ( test ) ) ;
2013-02-25 01:54:38 +00:00
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename_ " ) ;
// 5-byte utf-8 sequence (not allowed)
test = " filename \xf8 \x9f \x9f \x9f \x9f " " foobar " ;
TEST_CHECK ( ! verify_encoding ( test ) ) ;
fprintf ( stderr , " %s \n " , test . c_str ( ) ) ;
TEST_CHECK ( test = = " filename_____foobar " ) ;
2013-02-24 10:02:26 +00:00
// trim_path_element
fprintf ( stderr , " TORRENT_MAX_PATH: %d \n " , TORRENT_MAX_PATH ) ;
// 1100 characters
test = " abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij012345.txt " ;
std : : string comparison = test ;
trim_path_element ( test ) ;
2013-04-27 22:35:06 +00:00
if ( comparison . size ( ) > TORRENT_MAX_PATH )
{
comparison . resize ( TORRENT_MAX_PATH - 4 ) ;
comparison + = " .txt " ; // the extension is supposed to be preserved
}
2013-02-24 10:02:26 +00:00
TEST_EQUAL ( test , comparison ) ;
// extensions > 15 characters are ignored
test = " abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789 "
" abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij.123456789abcdefghij0123456789 " ;
comparison = test ;
trim_path_element ( test ) ;
2013-04-27 22:35:06 +00:00
if ( comparison . size ( ) > TORRENT_MAX_PATH )
comparison . resize ( TORRENT_MAX_PATH ) ;
2013-02-24 10:02:26 +00:00
TEST_EQUAL ( test , comparison ) ;
2007-08-07 07:29:51 +00:00
// test network functions
2007-09-24 18:58:25 +00:00
TEST_CHECK ( is_local ( address : : from_string ( " 192.168.0.1 " , ec ) ) ) ;
TEST_CHECK ( is_local ( address : : from_string ( " 10.1.1.56 " , ec ) ) ) ;
TEST_CHECK ( ! is_local ( address : : from_string ( " 14.14.251.63 " , ec ) ) ) ;
TEST_CHECK ( is_loopback ( address : : from_string ( " 127.0.0.1 " , ec ) ) ) ;
2009-11-27 21:41:24 +00:00
# if TORRENT_USE_IPV6
2009-04-13 05:11:44 +00:00
if ( supports_ipv6 ( ) )
2009-04-30 01:44:50 +00:00
{
2009-04-13 05:11:44 +00:00
TEST_CHECK ( is_loopback ( address : : from_string ( " ::1 " , ec ) ) ) ;
2009-04-30 01:44:50 +00:00
TEST_CHECK ( is_any ( address_v6 : : any ( ) ) ) ;
}
2009-11-27 21:41:24 +00:00
# endif
2007-09-24 18:58:25 +00:00
TEST_CHECK ( is_any ( address_v4 : : any ( ) ) ) ;
TEST_CHECK ( ! is_any ( address : : from_string ( " 31.53.21.64 " , ec ) ) ) ;
2010-11-29 01:33:05 +00:00
TEST_CHECK ( match_addr_mask (
address : : from_string ( " 10.0.1.3 " , ec ) ,
address : : from_string ( " 10.0.3.3 " , ec ) ,
address : : from_string ( " 255.255.0.0 " , ec ) ) ) ;
TEST_CHECK ( ! match_addr_mask (
address : : from_string ( " 10.0.1.3 " , ec ) ,
address : : from_string ( " 10.1.3.3 " , ec ) ,
address : : from_string ( " 255.255.0.0 " , ec ) ) ) ;
2007-09-24 18:51:04 +00:00
// test torrent parsing
entry info ;
info [ " pieces " ] = " aaaaaaaaaaaaaaaaaaaa " ;
info [ " name.utf-8 " ] = " test1 " ;
info [ " name " ] = " test__ " ;
info [ " piece length " ] = 16 * 1024 ;
info [ " length " ] = 3245 ;
entry torrent ;
torrent [ " info " ] = info ;
2010-03-05 06:27:23 +00:00
std : : vector < char > buf ;
bencode ( std : : back_inserter ( buf ) , torrent ) ;
torrent_info ti ( & buf [ 0 ] , buf . size ( ) , ec ) ;
2008-01-06 07:35:32 +00:00
std : : cerr < < ti . name ( ) < < std : : endl ;
2007-09-24 18:51:04 +00:00
TEST_CHECK ( ti . name ( ) = = " test1 " ) ;
2008-03-21 09:00:54 +00:00
# ifdef TORRENT_WINDOWS
info [ " name.utf-8 " ] = " c:/test1/test2/test3 " ;
# else
2008-01-06 07:35:32 +00:00
info [ " name.utf-8 " ] = " /test1/test2/test3 " ;
2008-03-21 09:00:54 +00:00
# endif
2008-01-06 07:35:32 +00:00
torrent [ " info " ] = info ;
2010-03-05 06:27:23 +00:00
buf . clear ( ) ;
bencode ( std : : back_inserter ( buf ) , torrent ) ;
torrent_info ti2 ( & buf [ 0 ] , buf . size ( ) , ec ) ;
2008-01-06 07:35:32 +00:00
std : : cerr < < ti2 . name ( ) < < std : : endl ;
2009-10-26 01:29:39 +00:00
# ifdef TORRENT_WINDOWS
TEST_CHECK ( ti2 . name ( ) = = " test1 \\ test2 \\ test3 " ) ;
# else
2009-05-28 07:25:09 +00:00
TEST_CHECK ( ti2 . name ( ) = = " test1/test2/test3 " ) ;
2009-10-26 01:29:39 +00:00
# endif
2008-01-06 07:35:32 +00:00
info [ " name.utf-8 " ] = " test2/../test3/.././../../test4 " ;
torrent [ " info " ] = info ;
2010-03-05 06:27:23 +00:00
buf . clear ( ) ;
bencode ( std : : back_inserter ( buf ) , torrent ) ;
torrent_info ti3 ( & buf [ 0 ] , buf . size ( ) , ec ) ;
2008-01-06 07:35:32 +00:00
std : : cerr < < ti3 . name ( ) < < std : : endl ;
2009-10-26 01:29:39 +00:00
# ifdef TORRENT_WINDOWS
TEST_CHECK ( ti3 . name ( ) = = " test2 \\ test3 \\ test4 " ) ;
# else
2008-01-06 07:35:32 +00:00
TEST_CHECK ( ti3 . name ( ) = = " test2/test3/test4 " ) ;
2009-10-26 01:29:39 +00:00
# endif
2008-01-06 07:35:32 +00:00
2008-01-07 04:39:18 +00:00
// test peer_id/sha1_hash type
sha1_hash h1 ( 0 ) ;
sha1_hash h2 ( 0 ) ;
TEST_CHECK ( h1 = = h2 ) ;
TEST_CHECK ( ! ( h1 ! = h2 ) ) ;
TEST_CHECK ( ! ( h1 < h2 ) ) ;
TEST_CHECK ( ! ( h1 < h2 ) ) ;
TEST_CHECK ( h1 . is_all_zeros ( ) ) ;
2009-04-04 21:44:04 +00:00
h1 = to_hash ( " 0123456789012345678901234567890123456789 " ) ;
h2 = to_hash ( " 0113456789012345678901234567890123456789 " ) ;
2008-01-07 04:39:18 +00:00
TEST_CHECK ( h2 < h1 ) ;
TEST_CHECK ( h2 = = h2 ) ;
TEST_CHECK ( h1 = = h1 ) ;
h2 . clear ( ) ;
TEST_CHECK ( h2 . is_all_zeros ( ) ) ;
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " ffffffffff0000000000ffffffffff0000000000 " ) ;
h1 = to_hash ( " fffff00000fffff00000fffff00000fffff00000 " ) ;
2008-01-07 04:39:18 +00:00
h1 & = h2 ;
2009-04-04 21:44:04 +00:00
TEST_CHECK ( h1 = = to_hash ( " fffff000000000000000fffff000000000000000 " ) ) ;
2008-01-07 04:39:18 +00:00
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " ffffffffff0000000000ffffffffff0000000000 " ) ;
h1 = to_hash ( " fffff00000fffff00000fffff00000fffff00000 " ) ;
2008-01-07 04:39:18 +00:00
h1 | = h2 ;
2009-04-04 21:44:04 +00:00
TEST_CHECK ( h1 = = to_hash ( " fffffffffffffff00000fffffffffffffff00000 " ) ) ;
2008-01-07 04:39:18 +00:00
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " 0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f " ) ;
2008-01-07 04:39:18 +00:00
h1 ^ = h2 ;
2009-04-04 21:44:04 +00:00
# if TORRENT_USE_IOSTREAM
2008-01-07 04:39:18 +00:00
std : : cerr < < h1 < < std : : endl ;
2009-04-04 21:44:04 +00:00
# endif
TEST_CHECK ( h1 = = to_hash ( " f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f " ) ) ;
2008-01-07 04:39:18 +00:00
TEST_CHECK ( h1 ! = h2 ) ;
h2 = sha1_hash ( " " ) ;
2009-04-04 21:44:04 +00:00
TEST_CHECK ( h2 = = to_hash ( " 2020202020202020202020202020202020202020 " ) ) ;
2010-12-06 06:39:16 +00:00
h1 = to_hash ( " ffffffffff0000000000ffffffffff0000000000 " ) ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
h1 < < = 12 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " fffffff0000000000ffffffffff0000000000000 " ) ) ;
h1 > > = 12 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " 000fffffff0000000000ffffffffff0000000000 " ) ) ;
2013-01-20 19:35:47 +00:00
h1 = to_hash ( " 7000000000000000000000000000000000000000 " ) ;
h1 < < = 1 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " e000000000000000000000000000000000000000 " ) ) ;
h1 = to_hash ( " 0000000000000000000000000000000000000007 " ) ;
h1 < < = 1 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " 000000000000000000000000000000000000000e " ) ) ;
h1 = to_hash ( " 0000000000000000000000000000000000000007 " ) ;
h1 > > = 1 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " 0000000000000000000000000000000000000003 " ) ) ;
h1 = to_hash ( " 7000000000000000000000000000000000000000 " ) ;
h1 > > = 1 ;
# if TORRENT_USE_IOSTREAM
std : : cerr < < h1 < < std : : endl ;
# endif
TEST_CHECK ( h1 = = to_hash ( " 3800000000000000000000000000000000000000 " ) ) ;
2008-01-07 04:39:18 +00:00
2008-02-28 03:09:34 +00:00
// CIDR distance test
2009-04-04 21:44:04 +00:00
h1 = to_hash ( " 0123456789abcdef01232456789abcdef0123456 " ) ;
h2 = to_hash ( " 0123456789abcdef01232456789abcdef0123456 " ) ;
2008-02-28 03:09:34 +00:00
TEST_CHECK ( common_bits ( & h1 [ 0 ] , & h2 [ 0 ] , 20 ) = = 160 ) ;
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " 0120456789abcdef01232456789abcdef0123456 " ) ;
2008-02-28 03:09:34 +00:00
TEST_CHECK ( common_bits ( & h1 [ 0 ] , & h2 [ 0 ] , 20 ) = = 14 ) ;
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " 012f456789abcdef01232456789abcdef0123456 " ) ;
2008-02-28 03:09:34 +00:00
TEST_CHECK ( common_bits ( & h1 [ 0 ] , & h2 [ 0 ] , 20 ) = = 12 ) ;
2009-04-04 21:44:04 +00:00
h2 = to_hash ( " 0123456789abcdef11232456789abcdef0123456 " ) ;
2008-02-28 03:09:34 +00:00
TEST_CHECK ( common_bits ( & h1 [ 0 ] , & h2 [ 0 ] , 20 ) = = 16 * 4 + 3 ) ;
2008-05-28 02:35:02 +00:00
// test bitfield
bitfield test1 ( 10 , false ) ;
TEST_CHECK ( test1 . count ( ) = = 0 ) ;
test1 . set_bit ( 9 ) ;
TEST_CHECK ( test1 . count ( ) = = 1 ) ;
test1 . clear_bit ( 9 ) ;
TEST_CHECK ( test1 . count ( ) = = 0 ) ;
test1 . set_bit ( 2 ) ;
TEST_CHECK ( test1 . count ( ) = = 1 ) ;
test1 . set_bit ( 1 ) ;
test1 . set_bit ( 9 ) ;
TEST_CHECK ( test1 . count ( ) = = 3 ) ;
2012-05-02 18:55:58 +00:00
TEST_CHECK ( test1 . all_set ( ) = = false ) ;
2008-05-28 02:35:02 +00:00
test1 . clear_bit ( 2 ) ;
TEST_CHECK ( test1 . count ( ) = = 2 ) ;
int distance = std : : distance ( test1 . begin ( ) , test1 . end ( ) ) ;
std : : cerr < < distance < < std : : endl ;
TEST_CHECK ( distance = = 10 ) ;
test1 . set_all ( ) ;
TEST_CHECK ( test1 . count ( ) = = 10 ) ;
test1 . clear_all ( ) ;
TEST_CHECK ( test1 . count ( ) = = 0 ) ;
2008-08-29 12:19:21 +00:00
test1 . resize ( 2 ) ;
test1 . set_bit ( 0 ) ;
test1 . resize ( 16 , true ) ;
TEST_CHECK ( test1 . count ( ) = = 15 ) ;
test1 . resize ( 20 , true ) ;
TEST_CHECK ( test1 . count ( ) = = 19 ) ;
test1 . set_bit ( 1 ) ;
test1 . resize ( 1 ) ;
TEST_CHECK ( test1 . count ( ) = = 1 ) ;
2012-05-02 18:55:58 +00:00
test1 . resize ( 100 , true ) ;
TEST_CHECK ( test1 . all_set ( ) = = true ) ;
2013-01-21 07:33:48 +00:00
// test merkle_*() functions
// this is the structure:
// 0
// 1 2
// 3 4 5 6
// 7 8 9 10 11 12 13 14
// num_leafs = 8
TEST_EQUAL ( merkle_num_leafs ( 1 ) , 1 ) ;
TEST_EQUAL ( merkle_num_leafs ( 2 ) , 2 ) ;
TEST_EQUAL ( merkle_num_leafs ( 3 ) , 4 ) ;
TEST_EQUAL ( merkle_num_leafs ( 4 ) , 4 ) ;
TEST_EQUAL ( merkle_num_leafs ( 5 ) , 8 ) ;
TEST_EQUAL ( merkle_num_leafs ( 6 ) , 8 ) ;
TEST_EQUAL ( merkle_num_leafs ( 7 ) , 8 ) ;
TEST_EQUAL ( merkle_num_leafs ( 8 ) , 8 ) ;
TEST_EQUAL ( merkle_num_leafs ( 9 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 10 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 11 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 12 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 13 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 14 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 15 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 16 ) , 16 ) ;
TEST_EQUAL ( merkle_num_leafs ( 17 ) , 32 ) ;
TEST_EQUAL ( merkle_num_leafs ( 18 ) , 32 ) ;
// parents
TEST_EQUAL ( merkle_get_parent ( 1 ) , 0 ) ;
TEST_EQUAL ( merkle_get_parent ( 2 ) , 0 ) ;
TEST_EQUAL ( merkle_get_parent ( 3 ) , 1 ) ;
TEST_EQUAL ( merkle_get_parent ( 4 ) , 1 ) ;
TEST_EQUAL ( merkle_get_parent ( 5 ) , 2 ) ;
TEST_EQUAL ( merkle_get_parent ( 6 ) , 2 ) ;
TEST_EQUAL ( merkle_get_parent ( 7 ) , 3 ) ;
TEST_EQUAL ( merkle_get_parent ( 8 ) , 3 ) ;
TEST_EQUAL ( merkle_get_parent ( 9 ) , 4 ) ;
TEST_EQUAL ( merkle_get_parent ( 10 ) , 4 ) ;
TEST_EQUAL ( merkle_get_parent ( 11 ) , 5 ) ;
TEST_EQUAL ( merkle_get_parent ( 12 ) , 5 ) ;
TEST_EQUAL ( merkle_get_parent ( 13 ) , 6 ) ;
TEST_EQUAL ( merkle_get_parent ( 14 ) , 6 ) ;
// siblings
TEST_EQUAL ( merkle_get_sibling ( 1 ) , 2 ) ;
TEST_EQUAL ( merkle_get_sibling ( 2 ) , 1 ) ;
TEST_EQUAL ( merkle_get_sibling ( 3 ) , 4 ) ;
TEST_EQUAL ( merkle_get_sibling ( 4 ) , 3 ) ;
TEST_EQUAL ( merkle_get_sibling ( 5 ) , 6 ) ;
TEST_EQUAL ( merkle_get_sibling ( 6 ) , 5 ) ;
TEST_EQUAL ( merkle_get_sibling ( 7 ) , 8 ) ;
TEST_EQUAL ( merkle_get_sibling ( 8 ) , 7 ) ;
TEST_EQUAL ( merkle_get_sibling ( 9 ) , 10 ) ;
TEST_EQUAL ( merkle_get_sibling ( 10 ) , 9 ) ;
TEST_EQUAL ( merkle_get_sibling ( 11 ) , 12 ) ;
TEST_EQUAL ( merkle_get_sibling ( 12 ) , 11 ) ;
TEST_EQUAL ( merkle_get_sibling ( 13 ) , 14 ) ;
TEST_EQUAL ( merkle_get_sibling ( 14 ) , 13 ) ;
// total number of nodes given the number of leafs
TEST_EQUAL ( merkle_num_nodes ( 1 ) , 1 ) ;
TEST_EQUAL ( merkle_num_nodes ( 2 ) , 3 ) ;
TEST_EQUAL ( merkle_num_nodes ( 4 ) , 7 ) ;
TEST_EQUAL ( merkle_num_nodes ( 8 ) , 15 ) ;
TEST_EQUAL ( merkle_num_nodes ( 16 ) , 31 ) ;
2013-04-27 20:48:29 +00:00
// make_magnet_uri
{
entry info ;
info [ " pieces " ] = " aaaaaaaaaaaaaaaaaaaa " ;
info [ " name " ] = " slightly shorter name, it's kind of sad that people started the trend of incorrectly encoding the regular name field and then adding another one with correct encoding " ;
info [ " name.utf-8 " ] = " this is a long ass name in order to try to make make_magnet_uri overflow and hopefully crash. Although, by the time you read this that particular bug should have been fixed " ;
info [ " piece length " ] = 16 * 1024 ;
info [ " length " ] = 3245 ;
entry torrent ;
torrent [ " info " ] = info ;
entry : : list_type & al1 = torrent [ " announce-list " ] . list ( ) ;
al1 . push_back ( entry : : list_type ( ) ) ;
entry : : list_type & al = al1 . back ( ) . list ( ) ;
al . push_back ( entry ( " http://bigtorrent.org:2710/announce " ) ) ;
al . push_back ( entry ( " http://bt.careland.com.cn:6969/announce " ) ) ;
al . push_back ( entry ( " http://bt.e-burg.org:2710/announce " ) ) ;
al . push_back ( entry ( " http://bttrack.9you.com/announce " ) ) ;
al . push_back ( entry ( " http://coppersurfer.tk:6969/announce " ) ) ;
al . push_back ( entry ( " http://erdgeist.org/arts/software/opentracker/announce " ) ) ;
al . push_back ( entry ( " http://exodus.desync.com/announce " ) ) ;
al . push_back ( entry ( " http://fr33dom.h33t.com:3310/announce " ) ) ;
al . push_back ( entry ( " http://genesis.1337x.org:1337/announce " ) ) ;
al . push_back ( entry ( " http://inferno.demonoid.me:3390/announce " ) ) ;
al . push_back ( entry ( " http://inferno.demonoid.ph:3390/announce " ) ) ;
al . push_back ( entry ( " http://ipv6.tracker.harry.lu/announce " ) ) ;
al . push_back ( entry ( " http://lnxroot.com:6969/announce " ) ) ;
al . push_back ( entry ( " http://nemesis.1337x.org/announce " ) ) ;
al . push_back ( entry ( " http://puto.me:6969/announce " ) ) ;
al . push_back ( entry ( " http://sline.net:2710/announce " ) ) ;
al . push_back ( entry ( " http://tracker.beeimg.com:6969/announce " ) ) ;
al . push_back ( entry ( " http://tracker.ccc.de/announce " ) ) ;
al . push_back ( entry ( " http://tracker.coppersurfer.tk/announce " ) ) ;
al . push_back ( entry ( " http://tracker.coppersurfer.tk:6969/announce " ) ) ;
al . push_back ( entry ( " http://tracker.cpleft.com:2710/announce " ) ) ;
al . push_back ( entry ( " http://tracker.istole.it/announce " ) ) ;
al . push_back ( entry ( " http://tracker.kamyu.net/announce " ) ) ;
al . push_back ( entry ( " http://tracker.novalayer.org:6969/announce " ) ) ;
al . push_back ( entry ( " http://tracker.torrent.to:2710/announce " ) ) ;
al . push_back ( entry ( " http://tracker.torrentbay.to:6969/announce " ) ) ;
al . push_back ( entry ( " udp://tracker.openbittorrent.com:80 " ) ) ;
al . push_back ( entry ( " udp://tracker.publicbt.com:80 " ) ) ;
std : : vector < char > buf ;
bencode ( std : : back_inserter ( buf ) , torrent ) ;
printf ( " %s \n " , & buf [ 0 ] ) ;
torrent_info ti ( & buf [ 0 ] , buf . size ( ) , ec ) ;
TEST_EQUAL ( al . size ( ) , ti . trackers ( ) . size ( ) ) ;
std : : string magnet = make_magnet_uri ( ti ) ;
printf ( " %s len: %d \n " , magnet . c_str ( ) , int ( magnet . size ( ) ) ) ;
}
2007-05-22 20:44:18 +00:00
return 0 ;
}