mirror of
https://github.com/aria2/aria2.git
synced 2025-04-05 13:37:40 +03:00
Rewritten a portion of bittorrent implementation: * src/BtMessageValidator.h: New class. * src/BtBitfieldMessageValidator.h: New class. * src/BtHandshakeMessageValidator.h: New class. * src/BtRequestMessageValidator.h: New class. * src/BtSuggestPieceMessageValidator.h: New class. * src/BtAllowedFastMessageValidator.h: New class. * src/BtRejectMessageValidator.h: New class. * src/BtCancelMessageValidator.h: New class. * src/BtPieceMessageValidator.h: New class. * src/BtHaveMessageValidator.h: New class. * src/BtEventListener.h: New class. * src/AbstractBtEventListener.h: New class. * src/BtEvent.h: New class. * src/BtChokingEvent.h: New class. * src/BtChokedEvent.h: New class. * src/BtCancelSendingPieceEvent.h: New class. * src/BtAbortOutstandingRequestEvent.h: New class. * src/Randomizer.h: New class. * src/SimpleRandomizer.h: New class. * src/BtMessage.h: New class. * src/AbstractBtMessage.h: New class. * src/SimpleBtMessage.h: New class. * src/BtHaveMessage.h: New class. * src/BtInterestedMessage.h: New class. * src/BtAllowedFastMessage.h: New class. * src/BtUnchokeMessage.h: New class. * src/BtCancelMessage.h: New class. * src/BtNotInterestedMessage.h: New class. * src/BtChokeMessage.h: New class. * src/BtHaveNoneMessage.h: New class. * src/BtHandshakeMessage.h: New class. * src/BtSuggestPieceMessage.h: New class. * src/BtHaveMessage.h: New class. * src/BtPieceMessage.h: New class. * src/BtHaveAllMessage.h: New class. * src/BtKeepAliveMessage.h: New class. * src/BtPortMessage.h: New class. * src/BtRejectMessage.h: New class. * src/BtBitfieldMessage.h: New class. * src/BtRequestMessage.h: New class. * src/DefaultBtRequestFactory.h: New class. * src/DefaultBtMessageReceiver.h: New class. * src/BtInteractive.h: New class. * src/BtMessageDispatcher.h: New class. * src/DefaultBtMessageDispatcher.h: New class. * src/DefaultBtInteractive.h: New class. * src/BitfieldManFactory.h: New class. * src/HandleRegistry.h: New class. * src/BtMessageFactory.h: New class. * src/BtMessageReceiver.h: New class. * src/DefaultBtMessageFactory.h: New class. * src/PeerObject.h: New class. * src/BtRequestFactory.h: New class.
152 lines
4.5 KiB
C++
152 lines
4.5 KiB
C++
#include "DefaultPieceStorage.h"
|
|
#include "DefaultBtContext.h"
|
|
#include "Util.h"
|
|
#include "Exception.h"
|
|
#include "FixedNumberRandomizer.h"
|
|
#include "BitfieldManFactory.h"
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
using namespace std;
|
|
|
|
class DefaultPieceStorageTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultPieceStorageTest);
|
|
CPPUNIT_TEST(testGetTotalLength);
|
|
CPPUNIT_TEST(testGetMissingPiece);
|
|
CPPUNIT_TEST(testGetMissingFastPiece);
|
|
CPPUNIT_TEST(testHasMissingPiece);
|
|
CPPUNIT_TEST(testCompletePiece);
|
|
CPPUNIT_TEST(testGetPiece);
|
|
CPPUNIT_TEST(testGetPieceInUsedPieces);
|
|
CPPUNIT_TEST(testGetPieceCompletedPiece);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
private:
|
|
BtContextHandle btContext;
|
|
PeerHandle peer;
|
|
Option* option;
|
|
public:
|
|
DefaultPieceStorageTest():btContext(0), peer(0) {
|
|
FixedNumberRandomizer* randomizer = new FixedNumberRandomizer();
|
|
randomizer->setFixedNumber(0);
|
|
BitfieldManFactory::setDefaultRandomizer(randomizer);
|
|
}
|
|
|
|
void setUp() {
|
|
btContext = BtContextHandle(new DefaultBtContext());
|
|
btContext->load("test.torrent");
|
|
peer = PeerHandle(new Peer("192.168.0.1", 6889,
|
|
btContext->getPieceLength(),
|
|
btContext->getTotalLength()));
|
|
option = new Option();
|
|
}
|
|
|
|
void testGetTotalLength();
|
|
void testGetMissingPiece();
|
|
void testGetMissingFastPiece();
|
|
void testHasMissingPiece();
|
|
void testCompletePiece();
|
|
void testGetPiece();
|
|
void testGetPieceInUsedPieces();
|
|
void testGetPieceCompletedPiece();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultPieceStorageTest);
|
|
|
|
void DefaultPieceStorageTest::testGetTotalLength() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((long long int)384,
|
|
pss.getTotalLength());
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testGetMissingPiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
pss.setEndGamePieceNum(0);
|
|
|
|
peer->setAllBitfield();
|
|
PieceHandle piece = pss.getMissingPiece(peer);
|
|
CPPUNIT_ASSERT_EQUAL(string("piece: index=0, length=128"),
|
|
piece->toString());
|
|
piece = pss.getMissingPiece(peer);
|
|
CPPUNIT_ASSERT_EQUAL(string("piece: index=1, length=128"),
|
|
piece->toString());
|
|
piece = pss.getMissingPiece(peer);
|
|
CPPUNIT_ASSERT_EQUAL(string("piece: index=2, length=128"),
|
|
piece->toString());
|
|
piece = pss.getMissingPiece(peer);
|
|
CPPUNIT_ASSERT(piece.isNull());
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testGetMissingFastPiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
pss.setEndGamePieceNum(0);
|
|
|
|
peer->setAllBitfield();
|
|
peer->setFastExtensionEnabled(true);
|
|
peer->addFastSetIndex(2);
|
|
|
|
PieceHandle piece = pss.getMissingFastPiece(peer);
|
|
CPPUNIT_ASSERT_EQUAL(string("piece: index=2, length=128"),
|
|
piece->toString());
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testHasMissingPiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
|
|
CPPUNIT_ASSERT(!pss.hasMissingPiece(peer));
|
|
|
|
peer->setAllBitfield();
|
|
|
|
CPPUNIT_ASSERT(pss.hasMissingPiece(peer));
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testCompletePiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
pss.setEndGamePieceNum(0);
|
|
|
|
peer->setAllBitfield();
|
|
|
|
PieceHandle piece = pss.getMissingPiece(peer);
|
|
CPPUNIT_ASSERT_EQUAL(string("piece: index=0, length=128"),
|
|
piece->toString());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((long long int)0,
|
|
pss.getCompletedLength());
|
|
|
|
pss.completePiece(piece);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((long long int)128,
|
|
pss.getCompletedLength());
|
|
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testGetPiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
|
|
PieceHandle pieceGot = pss.getPiece(0);
|
|
CPPUNIT_ASSERT_EQUAL(0, pieceGot->getIndex());
|
|
CPPUNIT_ASSERT_EQUAL(128, pieceGot->getLength());
|
|
CPPUNIT_ASSERT_EQUAL(false, pieceGot->pieceComplete());
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testGetPieceInUsedPieces() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
PieceHandle piece = PieceHandle(new Piece(0, 128));
|
|
piece->completeBlock(0);
|
|
pss.addUsedPiece(piece);
|
|
PieceHandle pieceGot = pss.getPiece(0);
|
|
CPPUNIT_ASSERT_EQUAL(0, pieceGot->getIndex());
|
|
CPPUNIT_ASSERT_EQUAL(128, pieceGot->getLength());
|
|
CPPUNIT_ASSERT_EQUAL(1, pieceGot->countCompleteBlock());
|
|
}
|
|
|
|
void DefaultPieceStorageTest::testGetPieceCompletedPiece() {
|
|
DefaultPieceStorage pss(btContext, option);
|
|
PieceHandle piece = PieceHandle(new Piece(0, 128));
|
|
pss.completePiece(piece);
|
|
PieceHandle pieceGot = pss.getPiece(0);
|
|
CPPUNIT_ASSERT_EQUAL(0, pieceGot->getIndex());
|
|
CPPUNIT_ASSERT_EQUAL(128, pieceGot->getLength());
|
|
CPPUNIT_ASSERT_EQUAL(true, pieceGot->pieceComplete());
|
|
}
|