2006-12-24 06:25:21 +00:00
|
|
|
#include "DefaultBtRequestFactory.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "MockBtMessage.h"
|
|
|
|
#include "MockBtMessageFactory.h"
|
|
|
|
#include "MockBtMessageDispatcher.h"
|
|
|
|
#include "MockPieceStorage.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include "FileEntry.h"
|
2008-04-20 00:50:22 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "bittorrent_helper.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class DefaultBtRequestFactoryTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultBtRequestFactoryTest);
|
|
|
|
CPPUNIT_TEST(testAddTargetPiece);
|
|
|
|
CPPUNIT_TEST(testRemoveCompletedPiece);
|
|
|
|
CPPUNIT_TEST(testCreateRequestMessages);
|
|
|
|
CPPUNIT_TEST(testCreateRequestMessages_onEndGame);
|
|
|
|
CPPUNIT_TEST(testRemoveTargetPiece);
|
2008-09-19 14:11:41 +00:00
|
|
|
CPPUNIT_TEST(testGetTargetPieceIndexes);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer_;
|
2013-06-30 07:55:15 +00:00
|
|
|
std::unique_ptr<DefaultBtRequestFactory> requestFactory_;
|
|
|
|
std::unique_ptr<MockPieceStorage> pieceStorage_;
|
|
|
|
std::unique_ptr<MockBtMessageFactory> messageFactory_;
|
|
|
|
std::unique_ptr<MockBtMessageDispatcher> dispatcher_;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
|
|
|
void testAddTargetPiece();
|
|
|
|
void testRemoveCompletedPiece();
|
|
|
|
void testCreateRequestMessages();
|
|
|
|
void testCreateRequestMessages_onEndGame();
|
|
|
|
void testRemoveTargetPiece();
|
2008-09-19 14:11:41 +00:00
|
|
|
void testGetTargetPieceIndexes();
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class MockBtRequestMessage : public MockBtMessage {
|
|
|
|
public:
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t index;
|
|
|
|
size_t blockIndex;
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
MockBtRequestMessage(size_t index, size_t blockIndex):
|
|
|
|
index(index), blockIndex(blockIndex) {}
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
|
|
|
public:
|
2013-06-30 07:55:15 +00:00
|
|
|
virtual std::unique_ptr<BtRequestMessage>
|
|
|
|
createRequestMessage(const std::shared_ptr<Piece>& piece,
|
2013-07-06 06:54:03 +00:00
|
|
|
size_t blockIndex) CXX11_OVERRIDE
|
2013-06-30 07:55:15 +00:00
|
|
|
{
|
|
|
|
return make_unique<BtRequestMessage>(piece->getIndex(), 0, 0,
|
|
|
|
blockIndex);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
|
|
|
|
public:
|
2013-07-06 06:54:03 +00:00
|
|
|
virtual bool isOutstandingRequest(size_t index, size_t blockIndex)
|
|
|
|
CXX11_OVERRIDE
|
2013-06-30 07:55:15 +00:00
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
return index == 0 && blockIndex == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
class BtRequestMessageSorter {
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
public:
|
2013-06-30 07:55:15 +00:00
|
|
|
bool operator()(const std::unique_ptr<BtRequestMessage>& a,
|
|
|
|
const std::unique_ptr<BtRequestMessage>& b)
|
|
|
|
{
|
|
|
|
return a->getIndex() < b->getIndex() ||
|
|
|
|
(a->getIndex() == b->getIndex() &&
|
|
|
|
a->getBlockIndex() < b->getBlockIndex());
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
void setUp()
|
|
|
|
{
|
2013-06-30 07:55:15 +00:00
|
|
|
pieceStorage_ = make_unique<MockPieceStorage>();
|
|
|
|
peer_ = std::make_shared<Peer>("host", 6969);
|
|
|
|
messageFactory_ = make_unique<MockBtMessageFactory2>();
|
|
|
|
dispatcher_ = make_unique<MockBtMessageDispatcher>();
|
|
|
|
requestFactory_ = make_unique<DefaultBtRequestFactory>();
|
2013-06-23 03:02:36 +00:00
|
|
|
requestFactory_->setPieceStorage(pieceStorage_.get());
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->setPeer(peer_);
|
2010-11-12 12:48:48 +00:00
|
|
|
requestFactory_->setBtMessageDispatcher(dispatcher_.get());
|
|
|
|
requestFactory_->setBtMessageFactory(messageFactory_.get());
|
2008-04-20 00:50:22 +00:00
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtRequestFactoryTest);
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testAddTargetPiece()
|
|
|
|
{
|
2008-06-17 11:43:29 +00:00
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
auto piece = std::make_shared<Piece>(0, 160_k);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, requestFactory_->countTargetPiece());
|
2008-06-17 11:43:29 +00:00
|
|
|
}
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
auto piece = std::make_shared<Piece>(1, 16_k * 9);
|
2008-06-17 11:43:29 +00:00
|
|
|
piece->completeBlock(0);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, requestFactory_->countTargetPiece());
|
2008-06-17 11:43:29 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)18, requestFactory_->countMissingBlock());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testRemoveCompletedPiece()
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
auto piece1 = std::make_shared<Piece>(0, 16_k);
|
|
|
|
auto piece2 = std::make_shared<Piece>(1, 16_k);
|
2006-12-24 06:25:21 +00:00
|
|
|
piece2->setAllBlock();
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece1);
|
|
|
|
requestFactory_->addTargetPiece(piece2);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, requestFactory_->countTargetPiece());
|
|
|
|
requestFactory_->removeCompletedPiece();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, requestFactory_->countTargetPiece());
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0,
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->getTargetPieces().front()->getIndex());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testCreateRequestMessages()
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
constexpr int PIECE_LENGTH = 32_k;
|
2013-06-30 07:55:15 +00:00
|
|
|
auto piece1 = std::make_shared<Piece>(0, PIECE_LENGTH);
|
|
|
|
auto piece2 = std::make_shared<Piece>(1, PIECE_LENGTH);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece1);
|
|
|
|
requestFactory_->addTargetPiece(piece2);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
auto msgs = requestFactory_->createRequestMessages(3, false);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, msgs.size());
|
2013-06-30 07:55:15 +00:00
|
|
|
auto msg = msgs[0].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getBlockIndex());
|
|
|
|
msg = msgs[1].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getBlockIndex());
|
|
|
|
msg = msgs[2].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getBlockIndex());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-05-12 10:54:36 +00:00
|
|
|
{
|
2013-06-30 07:55:15 +00:00
|
|
|
auto msgs = requestFactory_->createRequestMessages(3, false);
|
2008-05-12 10:54:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msgs.size());
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testCreateRequestMessages_onEndGame()
|
|
|
|
{
|
|
|
|
auto dispatcher = make_unique<MockBtMessageDispatcher2>();
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
requestFactory_->setBtMessageDispatcher(dispatcher.get());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-06-21 09:04:30 +00:00
|
|
|
constexpr int PIECE_LENGTH = 32_k;
|
2013-06-30 07:55:15 +00:00
|
|
|
auto piece1 = std::make_shared<Piece>(0, PIECE_LENGTH);
|
|
|
|
auto piece2 = std::make_shared<Piece>(1, PIECE_LENGTH);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece1);
|
|
|
|
requestFactory_->addTargetPiece(piece2);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
auto msgs = requestFactory_->createRequestMessages(3, true);
|
|
|
|
std::sort(std::begin(msgs), std::end(msgs), BtRequestMessageSorter());
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, msgs.size());
|
|
|
|
auto msg = msgs[0].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getBlockIndex());
|
|
|
|
msg = msgs[1].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, msg->getBlockIndex());
|
|
|
|
msg = msgs[2].get();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getIndex());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, msg->getBlockIndex());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testRemoveTargetPiece()
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
auto piece1 = std::make_shared<Piece>(0, 16_k);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece1);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-11-11 07:33:43 +00:00
|
|
|
CPPUNIT_ASSERT(std::find_if(requestFactory_->getTargetPieces().begin(),
|
|
|
|
requestFactory_->getTargetPieces().end(),
|
|
|
|
derefEqual(piece1)) !=
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->getTargetPieces().end());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->removeTargetPiece(piece1);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-11-11 07:33:43 +00:00
|
|
|
CPPUNIT_ASSERT(std::find_if(requestFactory_->getTargetPieces().begin(),
|
|
|
|
requestFactory_->getTargetPieces().end(),
|
|
|
|
derefEqual(piece1)) ==
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->getTargetPieces().end());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-09-19 14:11:41 +00:00
|
|
|
void DefaultBtRequestFactoryTest::testGetTargetPieceIndexes()
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
auto piece1 = std::make_shared<Piece>(1, 16_k);
|
|
|
|
auto piece3 = std::make_shared<Piece>(3, 16_k);
|
|
|
|
auto piece5 = std::make_shared<Piece>(5, 16_k);
|
2008-09-19 14:11:41 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
requestFactory_->addTargetPiece(piece3);
|
|
|
|
requestFactory_->addTargetPiece(piece1);
|
|
|
|
requestFactory_->addTargetPiece(piece5);
|
2008-09-19 14:11:41 +00:00
|
|
|
|
2013-06-30 07:55:15 +00:00
|
|
|
auto indexes = requestFactory_->getTargetPieceIndexes();
|
2008-09-19 14:11:41 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, indexes.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, indexes[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, indexes[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)5, indexes[2]);
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|