merged back the asio development branch

This commit is contained in:
Arvid Norberg
2006-04-25 21:04:48 +00:00
parent 410af930a7
commit 41810b1166
71 changed files with 8271 additions and 4661 deletions

View File

@@ -15,5 +15,6 @@ test-suite libtorrent :
[ run test_bencoding.cpp ]
[ run test_ip_filter.cpp ]
[ run test_hasher.cpp ]
[ run test_metadata_extension.cpp ]
;

View File

@@ -1,4 +1,4 @@
bin_PROGRAMS = test_hasher test_bencoding test_ip_filter test_piece_picker test_storage
bin_PROGRAMS = test_hasher test_bencoding test_ip_filter test_piece_picker test_storage test_metadata_extension test_buffer
EXTRA_DIST = Jamfile
test_hasher_SOURCES = main.cpp test_hasher.cpp
@@ -16,7 +16,13 @@ test_piece_picker_LDADD = $(top_builddir)/src/libtorrent.la
test_storage_SOURCES = main.cpp test_storage.cpp
test_storage_LDADD = $(top_builddir)/src/libtorrent.la
test_buffer_SOURCES = main.cpp test_buffer.cpp
test_buffer_LDADD = $(top_builddir)/src/libtorrent.la
test_metadata_extension_SOURCES = main.cpp test_metadata_extension.cpp
test_metadata_extension_LDADD = $(top_builddir)/src/libtorrent.la
noinst_HEADERS = test.hpp
AM_CXXFLAGS=-ftemplate-depth-50 -I$(top_srcdir)/include @DEBUGFLAGS@ @PTHREAD_CFLAGS@
AM_CXXFLAGS=-ftemplate-depth-50 -I$(top_srcdir)/include -I$(top_srcdir)/asio/include @DEBUGFLAGS@ @PTHREAD_CFLAGS@
AM_LDFLAGS= -L./ -l@BOOST_DATE_TIME_LIB@ -l@BOOST_FILESYSTEM_LIB@ -l@BOOST_THREAD_LIB@ @PTHREAD_LIBS@

View File

@@ -63,8 +63,8 @@ int test_main()
// ** dictionaries **
{
entry e(entry::dictionary_t);
e["cow"] = entry("moo");
e["spam"] = entry("eggs");
e["cow"] = entry("moo");
TEST_CHECK(encode(e) == "d3:cow3:moo4:spam4:eggse");
TEST_CHECK(decode(encode(e)) == e);
}

View File

@@ -19,8 +19,8 @@ void test_rules_invariant(std::vector<ip_filter::ip_range> const& r, ip_filter c
TEST_CHECK(!r.empty());
if (r.empty()) return;
TEST_CHECK(r.front().first == address(0,0,0,0,0));
TEST_CHECK(r.back().last == address(255,255,255,255,0));
TEST_CHECK(r.front().first == address("0.0.0.0"));
TEST_CHECK(r.back().last == address("255.255.255.255"));
iterator i = r.begin();
iterator j = boost::next(i);
@@ -29,7 +29,7 @@ void test_rules_invariant(std::vector<ip_filter::ip_range> const& r, ip_filter c
{
TEST_CHECK(f.access(i->last) == i->flags);
TEST_CHECK(f.access(j->first) == j->flags);
TEST_CHECK(i->last.ip() + 1 == j->first.ip());
TEST_CHECK(i->last.to_ulong() + 1 == j->first.to_ulong());
}
}
@@ -41,15 +41,15 @@ int test_main()
// **** test joining of ranges at the end ****
ip_filter::ip_range expected1[] =
{
{address(0,0,0,0,0), address(0,255,255,255,0), 0}
, {address(1,0,0,0,0), address(3,0,0,0,0), ip_filter::blocked}
, {address(3,0,0,1,0), address(255,255,255,255,0), 0}
{address("0.0.0.0"), address("0.255.255.255"), 0}
, {address("1.0.0.0"), address("3.0.0.0"), ip_filter::blocked}
, {address("3.0.0.1"), address("255.255.255.255"), 0}
};
{
ip_filter f;
f.add_rule(address(1,0,0,0,0), address(2,0,0,0,0), ip_filter::blocked);
f.add_rule(address(2,0,0,1,0), address(3,0,0,0,0), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.0.0.0"), ip_filter::blocked);
f.add_rule(address("2.0.0.1"), address("3.0.0.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -62,8 +62,8 @@ int test_main()
{
ip_filter f;
f.add_rule(address(2,0,0,1,0), address(3,0,0,0,0), ip_filter::blocked);
f.add_rule(address(1,0,0,0,0), address(2,0,0,0,0), ip_filter::blocked);
f.add_rule(address("2.0.0.1"), address("3.0.0.0"), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.0.0.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -77,8 +77,8 @@ int test_main()
{
ip_filter f;
f.add_rule(address(2,0,0,1,0), address(3,0,0,0,0), ip_filter::blocked);
f.add_rule(address(1,0,0,0,0), address(2,4,0,0,0), ip_filter::blocked);
f.add_rule(address("2.0.0.1"), address("3.0.0.0"), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.4.0.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -92,8 +92,8 @@ int test_main()
{
ip_filter f;
f.add_rule(address(1,0,0,0,0), address(2,4,0,0,0), ip_filter::blocked);
f.add_rule(address(2,0,0,1,0), address(3,0,0,0,0), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.4.0.0"), ip_filter::blocked);
f.add_rule(address("2.0.0.1"), address("3.0.0.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -107,12 +107,12 @@ int test_main()
{
ip_filter f;
f.add_rule(address(1,0,0,0,0), address(2,0,0,0,0), ip_filter::blocked);
f.add_rule(address(3,0,0,0,0), address(4,0,0,0,0), ip_filter::blocked);
f.add_rule(address(5,0,0,0,0), address(6,0,0,0,0), ip_filter::blocked);
f.add_rule(address(7,0,0,0,0), address(8,0,0,0,0), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.0.0.0"), ip_filter::blocked);
f.add_rule(address("3.0.0.0"), address("4.0.0.0"), ip_filter::blocked);
f.add_rule(address("5.0.0.0"), address("6.0.0.0"), ip_filter::blocked);
f.add_rule(address("7.0.0.0"), address("8.0.0.0"), ip_filter::blocked);
f.add_rule(address(1,0,1,0,0), address(9,0,0,0,0), ip_filter::blocked);
f.add_rule(address("1.0.1.0"), address("9.0.0.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -120,9 +120,9 @@ int test_main()
TEST_CHECK(range.size() == 3);
ip_filter::ip_range expected[] =
{
{address(0,0,0,0,0), address(0,255,255,255,0), 0}
, {address(1,0,0,0,0), address(9,0,0,0,0), ip_filter::blocked}
, {address(9,0,0,1,0), address(255,255,255,255,0), 0}
{address("0.0.0.0"), address("0.255.255.255"), 0}
, {address("1.0.0.0"), address("9.0.0.0"), ip_filter::blocked}
, {address("9.0.0.1"), address("255.255.255.255"), 0}
};
TEST_CHECK(std::equal(range.begin(), range.end(), expected, &compare));
@@ -132,12 +132,12 @@ int test_main()
{
ip_filter f;
f.add_rule(address(1,0,0,0,0), address(2,0,0,0,0), ip_filter::blocked);
f.add_rule(address(3,0,0,0,0), address(4,0,0,0,0), ip_filter::blocked);
f.add_rule(address(5,0,0,0,0), address(6,0,0,0,0), ip_filter::blocked);
f.add_rule(address(7,0,0,0,0), address(8,0,0,0,0), ip_filter::blocked);
f.add_rule(address("1.0.0.0"), address("2.0.0.0"), ip_filter::blocked);
f.add_rule(address("3.0.0.0"), address("4.0.0.0"), ip_filter::blocked);
f.add_rule(address("5.0.0.0"), address("6.0.0.0"), ip_filter::blocked);
f.add_rule(address("7.0.0.0"), address("8.0.0.0"), ip_filter::blocked);
f.add_rule(address(0,0,1,0,0), address(7,0,4,0,0), ip_filter::blocked);
f.add_rule(address("0.0.1.0"), address("7.0.4.0"), ip_filter::blocked);
range = f.export_filter();
test_rules_invariant(range, f);
@@ -145,9 +145,9 @@ int test_main()
TEST_CHECK(range.size() == 3);
ip_filter::ip_range expected[] =
{
{address(0,0,0,0,0), address(0,0,0,255,0), 0}
, {address(0,0,1,0,0), address(8,0,0,0,0), ip_filter::blocked}
, {address(8,0,0,1,0), address(255,255,255,255,0), 0}
{address("0.0.0.0"), address("0.0.0.255"), 0}
, {address("0.0.1.0"), address("8.0.0.0"), ip_filter::blocked}
, {address("8.0.0.1"), address("255.255.255.255"), 0}
};
TEST_CHECK(std::equal(range.begin(), range.end(), expected, &compare));

View File

@@ -0,0 +1,106 @@
#include "libtorrent/session.hpp"
#include "libtorrent/hasher.hpp"
#include <boost/thread.hpp>
#include "test.hpp"
void sleep(int msec)
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.nsec += msec * 1000000;
boost::thread::sleep(xt);
}
void test_transfer(char const* tracker_url, libtorrent::torrent_info const& t)
{
using namespace libtorrent;
session ses1;
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49000, 50000));
// they should not use the same save dir, because the
// file pool will complain if two torrents are trying to
// use the same files
torrent_handle tor1 = ses1.add_torrent(t, "./tmp1");
torrent_handle tor2 = ses2.add_torrent(tracker_url
, t.info_hash(), "./tmp2");
std::cerr << "waiting for file check to complete\n";
// wait for 5 seconds or until the torrent is in a state
// were it can accept connections
for (int i = 0; i < 50; ++i)
{
torrent_status st = tor1.status();
if (st.state != torrent_status::queued_for_checking
&&st.state != torrent_status::checking_files)
break;
sleep(100);
}
std::cerr << "connecting peer\n";
tor1.connect_peer(tcp::endpoint(ses2.listen_port(), "127.0.0.1"));
for (int i = 0; i < 50; ++i)
{
// make sure this function can be called on
// torrents without metadata
tor2.status();
if (tor2.has_metadata()) break;
sleep(100);
}
std::cerr << "metadata received. waiting for transfer to complete\n";
TEST_CHECK(tor2.has_metadata());
for (int i = 0; i < 50; ++i)
{
tor2.status();
if (tor2.is_seed()) break;
sleep(100);
}
std::cerr << "done\n";
TEST_CHECK(tor2.is_seed());
}
int test_main()
{
using namespace libtorrent;
using namespace boost::filesystem;
char const* tracker_url = "http://non-existant-name.com/announce";
torrent_info t;
t.add_file(path("temporary"), 42);
t.set_piece_size(256 * 1024);
t.add_tracker(tracker_url);
std::vector<char> piece(42);
std::fill(piece.begin(), piece.end(), 0xfe);
// calculate the hash for all pieces
int num = t.num_pieces();
for (int i = 0; i < num; ++i)
{
t.set_hash(i, hasher(&piece[0], piece.size()).final());
}
create_directory("./tmp1");
std::ofstream file("./tmp1/temporary");
file.write(&piece[0], piece.size());
file.close();
remove_all("./tmp2/temporary");
t.create_torrent();
// test where one has data and one doesn't
test_transfer(tracker_url, t);
// test where both have data (to trigger the file check)
test_transfer(tracker_url, t);
return 0;
}

View File

@@ -12,7 +12,7 @@ int test_main()
const int num_pieces = 6;
// 4 blocks per piece
piece_picker p(4, num_pieces * 4);
piece_picker p(4, num_pieces * 4, 7);
// we have the first piece
std::vector<bool> have(num_pieces, false);
@@ -86,21 +86,21 @@ int test_main()
std::vector<piece_block> picked;
picked.clear();
p.pick_pieces(peer1, picked, 1, false, address());
p.pick_pieces(peer1, picked, 1, false, tcp::endpoint());
TEST_CHECK(picked.size() == 1);
TEST_CHECK(picked.front().piece_index == 2);
// now pick a piece from peer2. The block is supposed to be
// from piece 3, since it is the rarest piece that peer has.
picked.clear();
p.pick_pieces(peer2, picked, 1, false, address());
p.pick_pieces(peer2, picked, 1, false, tcp::endpoint());
TEST_CHECK(picked.size() == 1);
TEST_CHECK(picked.front().piece_index == 3);
// same thing for peer3.
picked.clear();
p.pick_pieces(peer3, picked, 1, false, address());
p.pick_pieces(peer3, picked, 1, false, tcp::endpoint());
TEST_CHECK(picked.size() == 1);
TEST_CHECK(picked.front().piece_index == 5);
@@ -114,7 +114,7 @@ int test_main()
p.inc_refcount(1);
picked.clear();
p.pick_pieces(peer3, picked, 1, false, address());
p.pick_pieces(peer3, picked, 1, false, tcp::endpoint());
TEST_CHECK(picked.size() == 1);
TEST_CHECK(picked.front().piece_index == 1);
// and the block picked should not be 0 or 2
@@ -138,9 +138,9 @@ int test_main()
// we have block 0 and 2 already, so we can't mark
// them as begin downloaded.
p.mark_as_downloading(piece_block(1, 1), address(1,1,1,1,0));
p.mark_as_downloading(piece_block(1, 3), address(1,1,1,1,0));
p.mark_as_downloading(piece_block(2, 0), address(1,1,1,1,0));
p.mark_as_downloading(piece_block(1, 1), tcp::endpoint(0, address("1.1.1.1")));
p.mark_as_downloading(piece_block(1, 3), tcp::endpoint(0, address("1.1.1.1")));
p.mark_as_downloading(piece_block(2, 0), tcp::endpoint(0, address("1.1.1.1")));
std::vector<piece_picker::downloading_piece> const& downloads = p.get_download_queue();
TEST_CHECK(downloads.size() == 2);
@@ -168,7 +168,7 @@ int test_main()
TEST_CHECK(!p.is_downloading(piece_block(2, 1)));
picked.clear();
p.pick_pieces(peer1, picked, 1, false, address());
p.pick_pieces(peer1, picked, 1, false, tcp::endpoint());
TEST_CHECK(picked.size() == 2);
piece_block expected3[] = { piece_block(2, 0), piece_block(2, 1) };
@@ -181,7 +181,7 @@ int test_main()
// partially selected)
picked.clear();
p.pick_pieces(peer1, picked, 1, true, address());
p.pick_pieces(peer1, picked, 1, true, tcp::endpoint());
// it will pick 4 blocks, since we said we
// wanted whole pieces.
@@ -199,7 +199,7 @@ int test_main()
// to make sure it can still fall back on partial pieces
picked.clear();
p.pick_pieces(peer1, picked, 100, true, address());
p.pick_pieces(peer1, picked, 100, true, tcp::endpoint());
TEST_CHECK(picked.size() == 14);
@@ -221,13 +221,8 @@ int test_main()
// to make sure it can still fall back on partial pieces
picked.clear();
p.pick_pieces(peer1, picked, 100, true, address(1,1,1,1,0));
p.pick_pieces(peer1, picked, 100, true, tcp::endpoint(0, address("1.1.1.1")));
for (std::vector<piece_block>::iterator i = picked.begin(); i != picked.end(); ++i)
{
std::cerr << "(" << i->piece_index << "," << i->block_index << ")\n";
}
TEST_CHECK(picked.size() == 11);
piece_block expected6[] =
@@ -245,7 +240,7 @@ int test_main()
// make sure the piece picker allows filtered pieces
// to become available
p.mark_as_finished(piece_block(4, 2), address());
p.mark_as_finished(piece_block(4, 2), tcp::endpoint());
}
return 0;

View File

@@ -79,12 +79,17 @@ int test_main()
libtorrent::detail::piece_checker_data d;
std::vector<bool> pieces;
TEST_CHECK(pm.check_fastresume(d, pieces, true) == false);
num_pieces = 0;
TEST_CHECK(pm.check_fastresume(d, pieces, num_pieces, true) == false);
bool finished = false;
float progress;
num_pieces = 0;
while (!finished)
boost::tie(finished, progress) = pm.check_files(pieces);
boost::tie(finished, progress) = pm.check_files(pieces, num_pieces);
TEST_CHECK(num_pieces == std::count(pieces.begin(), pieces.end()
, true));
pm.read(piece, 0, 0, piece_size);
TEST_CHECK(std::equal(piece, piece + piece_size, piece0));