|
@@ -2,7 +2,9 @@
|
|
|
#include "Option.h"
|
|
|
#include "Util.h"
|
|
|
#include "Exception.h"
|
|
|
+#ifdef ENABLE_BITTORRENT
|
|
|
#include "MockBtContext.h"
|
|
|
+#endif // ENABLE_BITTORRENT
|
|
|
#include "MockPeerStorage.h"
|
|
|
#include "MockPieceStorage.h"
|
|
|
#include "BtRuntime.h"
|
|
@@ -19,15 +21,21 @@ namespace aria2 {
|
|
|
class DefaultBtProgressInfoFileTest:public CppUnit::TestFixture {
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultBtProgressInfoFileTest);
|
|
|
+#ifdef ENABLE_BITTORRENT
|
|
|
CPPUNIT_TEST(testSave);
|
|
|
- CPPUNIT_TEST(testSave_nonBt);
|
|
|
CPPUNIT_TEST(testLoad);
|
|
|
+#endif // ENABLE_BITTORRENT
|
|
|
+ CPPUNIT_TEST(testSave_nonBt);
|
|
|
CPPUNIT_TEST(testLoad_nonBt);
|
|
|
CPPUNIT_TEST(testLoad_nonBt_pieceLengthShorter);
|
|
|
CPPUNIT_TEST(testUpdateFilename);
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
private:
|
|
|
+
|
|
|
+#ifdef ENABLE_BITTORRENT
|
|
|
SharedHandle<MockBtContext> _btContext;
|
|
|
+#endif // ENABLE_BITTORRENT
|
|
|
+
|
|
|
SharedHandle<MockPieceStorage> _pieceStorage;
|
|
|
SharedHandle<Option> _option;
|
|
|
SharedHandle<BitfieldMan> _bitfield;
|
|
@@ -38,22 +46,23 @@ public:
|
|
|
|
|
|
void initializeMembers(int32_t pieceLength, int64_t totalLength)
|
|
|
{
|
|
|
- static unsigned char infoHash[] = {
|
|
|
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
|
|
|
- 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff,
|
|
|
- };
|
|
|
-
|
|
|
_option.reset(new Option());
|
|
|
_option->put(PREF_DIR, ".");
|
|
|
|
|
|
- _btContext.reset(new MockBtContext());
|
|
|
- _btContext->setInfoHash(infoHash);
|
|
|
-
|
|
|
_bitfield.reset(new BitfieldMan(pieceLength, totalLength));
|
|
|
|
|
|
_pieceStorage.reset(new MockPieceStorage());
|
|
|
_pieceStorage->setBitfield(_bitfield.get());
|
|
|
|
|
|
+#ifdef ENABLE_BITTORRENT
|
|
|
+ static unsigned char infoHash[] = {
|
|
|
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
|
|
|
+ 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff,
|
|
|
+ };
|
|
|
+
|
|
|
+ _btContext.reset(new MockBtContext());
|
|
|
+ _btContext->setInfoHash(infoHash);
|
|
|
+
|
|
|
SharedHandle<MockPeerStorage> peerStorage(new MockPeerStorage());
|
|
|
|
|
|
SharedHandle<BtRuntime> btRuntime(new BtRuntime());
|
|
@@ -66,6 +75,7 @@ public:
|
|
|
peerStorage);
|
|
|
BtRegistry::registerBtRuntime(_btContext->getInfoHashAsString(),
|
|
|
btRuntime);
|
|
|
+#endif // ENABLE_BITTORRENT
|
|
|
}
|
|
|
|
|
|
void tearDown()
|
|
@@ -86,6 +96,8 @@ public:
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtProgressInfoFileTest);
|
|
|
|
|
|
+#ifdef ENABLE_BITTORRENT
|
|
|
+
|
|
|
void DefaultBtProgressInfoFileTest::testLoad()
|
|
|
{
|
|
|
initializeMembers(1024, 81920);
|
|
@@ -130,78 +142,19 @@ void DefaultBtProgressInfoFileTest::testLoad()
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)512, piece2->getLength());
|
|
|
}
|
|
|
|
|
|
-void DefaultBtProgressInfoFileTest::testLoad_nonBt()
|
|
|
-{
|
|
|
- initializeMembers(1024, 81920);
|
|
|
-
|
|
|
- SharedHandle<SingleFileDownloadContext> dctx
|
|
|
- (new SingleFileDownloadContext(1024, 81920, "load-nonBt"));
|
|
|
-
|
|
|
- DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), infoFile.getFilename());
|
|
|
- infoFile.load();
|
|
|
-
|
|
|
- // check the contents of objects
|
|
|
-
|
|
|
- // bitfield
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffe"),
|
|
|
- Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength()));
|
|
|
-
|
|
|
- // the number of in-flight pieces
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)2,
|
|
|
- _pieceStorage->countInFlightPiece());
|
|
|
-
|
|
|
- // piece index 1
|
|
|
- std::deque<SharedHandle<Piece> > inFlightPieces;
|
|
|
- _pieceStorage->getInFlightPieces(inFlightPieces);
|
|
|
-
|
|
|
- SharedHandle<Piece> piece1 = inFlightPieces[0];
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)1, piece1->getIndex());
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)1024, piece1->getLength());
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)1, piece1->getBitfieldLength());
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("00"), Util::toHex(piece1->getBitfield(),
|
|
|
- piece1->getBitfieldLength()));
|
|
|
-
|
|
|
- // piece index 2
|
|
|
- SharedHandle<Piece> piece2 = inFlightPieces[1];
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)2, piece2->getIndex());
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)512, piece2->getLength());
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-void DefaultBtProgressInfoFileTest::testLoad_nonBt_pieceLengthShorter()
|
|
|
-{
|
|
|
- initializeMembers(512, 81920);
|
|
|
- _option->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_TRUE);
|
|
|
-
|
|
|
- SharedHandle<SingleFileDownloadContext> dctx
|
|
|
- (new SingleFileDownloadContext(512, 81920, "load-nonBt"));
|
|
|
-
|
|
|
- DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), infoFile.getFilename());
|
|
|
- infoFile.load();
|
|
|
-
|
|
|
- // check the contents of objects
|
|
|
-
|
|
|
- // bitfield
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffffffffffffffffffffffc"),
|
|
|
- Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength()));
|
|
|
-
|
|
|
- // the number of in-flight pieces
|
|
|
- CPPUNIT_ASSERT_EQUAL((size_t)0,
|
|
|
- _pieceStorage->countInFlightPiece());
|
|
|
-}
|
|
|
-
|
|
|
-void DefaultBtProgressInfoFileTest::testSave_nonBt()
|
|
|
+void DefaultBtProgressInfoFileTest::testSave()
|
|
|
{
|
|
|
initializeMembers(1024, 81920);
|
|
|
|
|
|
- SharedHandle<SingleFileDownloadContext> dctx
|
|
|
- (new SingleFileDownloadContext(1024, 81920, "save-temp"));
|
|
|
-
|
|
|
+ _btContext->setName("save-temp");
|
|
|
+ _btContext->setPieceLength(1024);
|
|
|
+ _btContext->setTotalLength(81920);
|
|
|
_bitfield->setAllBit();
|
|
|
_bitfield->unsetBit(79);
|
|
|
_pieceStorage->setCompletedLength(80896);
|
|
|
+ TransferStat stat;
|
|
|
+ stat.setAllTimeUploadLength(1024);
|
|
|
+ dynamic_pointer_cast<MockPeerStorage>(PEER_STORAGE(_btContext))->setStat(stat);
|
|
|
|
|
|
SharedHandle<Piece> p1(new Piece(1, 1024));
|
|
|
SharedHandle<Piece> p2(new Piece(2, 512));
|
|
@@ -210,11 +163,11 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
|
|
|
inFlightPieces.push_back(p2);
|
|
|
_pieceStorage->addInFlightPiece(inFlightPieces);
|
|
|
|
|
|
- DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
+ DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get());
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./save-temp.aria2"), infoFile.getFilename());
|
|
|
|
|
|
infoFile.save();
|
|
|
-
|
|
|
+
|
|
|
// read and validate
|
|
|
std::ifstream in(infoFile.getFilename().c_str());
|
|
|
|
|
@@ -226,23 +179,28 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
|
|
|
|
|
|
unsigned char extension[4];
|
|
|
in.read((char*)extension, sizeof(extension));
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("00000000"), Util::toHex(extension, sizeof(extension)));
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("00000001"), Util::toHex(extension, sizeof(extension)));
|
|
|
|
|
|
uint32_t infoHashLength;
|
|
|
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
|
|
|
- CPPUNIT_ASSERT_EQUAL((uint32_t)0, infoHashLength);
|
|
|
+ CPPUNIT_ASSERT_EQUAL((uint32_t)20, infoHashLength);
|
|
|
+
|
|
|
+ unsigned char infoHashRead[20];
|
|
|
+ in.read((char*)infoHashRead, sizeof(infoHashRead));
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("112233445566778899aabbccddeeff00ffffffff"),
|
|
|
+ Util::toHex(infoHashRead, sizeof(infoHashRead)));
|
|
|
|
|
|
uint32_t pieceLength;
|
|
|
in.read((char*)&pieceLength, sizeof(pieceLength));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength);
|
|
|
-
|
|
|
+
|
|
|
uint64_t totalLength;
|
|
|
in.read((char*)&totalLength, sizeof(totalLength));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength);
|
|
|
|
|
|
uint64_t uploadLength;
|
|
|
in.read((char*)&uploadLength, sizeof(uploadLength));
|
|
|
- CPPUNIT_ASSERT_EQUAL((uint64_t)0, uploadLength);
|
|
|
+ CPPUNIT_ASSERT_EQUAL((uint64_t)1024, uploadLength);
|
|
|
|
|
|
uint32_t bitfieldLength;
|
|
|
in.read((char*)&bitfieldLength, sizeof(bitfieldLength));
|
|
@@ -284,21 +242,83 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
|
|
|
in.read((char*)&pieceLength2, sizeof(pieceLength2));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2);
|
|
|
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-void DefaultBtProgressInfoFileTest::testSave()
|
|
|
+#endif // ENABLE_BITTORRENT
|
|
|
+
|
|
|
+void DefaultBtProgressInfoFileTest::testLoad_nonBt()
|
|
|
{
|
|
|
initializeMembers(1024, 81920);
|
|
|
|
|
|
- _btContext->setName("save-temp");
|
|
|
- _btContext->setPieceLength(1024);
|
|
|
- _btContext->setTotalLength(81920);
|
|
|
+ SharedHandle<SingleFileDownloadContext> dctx
|
|
|
+ (new SingleFileDownloadContext(1024, 81920, "load-nonBt"));
|
|
|
+
|
|
|
+ DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), infoFile.getFilename());
|
|
|
+ infoFile.load();
|
|
|
+
|
|
|
+ // check the contents of objects
|
|
|
+
|
|
|
+ // bitfield
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffe"),
|
|
|
+ Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength()));
|
|
|
+
|
|
|
+ // the number of in-flight pieces
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)2,
|
|
|
+ _pieceStorage->countInFlightPiece());
|
|
|
+
|
|
|
+ // piece index 1
|
|
|
+ std::deque<SharedHandle<Piece> > inFlightPieces;
|
|
|
+ _pieceStorage->getInFlightPieces(inFlightPieces);
|
|
|
+
|
|
|
+ SharedHandle<Piece> piece1 = inFlightPieces[0];
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)1, piece1->getIndex());
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)1024, piece1->getLength());
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)1, piece1->getBitfieldLength());
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("00"), Util::toHex(piece1->getBitfield(),
|
|
|
+ piece1->getBitfieldLength()));
|
|
|
+
|
|
|
+ // piece index 2
|
|
|
+ SharedHandle<Piece> piece2 = inFlightPieces[1];
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)2, piece2->getIndex());
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)512, piece2->getLength());
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void DefaultBtProgressInfoFileTest::testLoad_nonBt_pieceLengthShorter()
|
|
|
+{
|
|
|
+ initializeMembers(512, 81920);
|
|
|
+ _option->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_TRUE);
|
|
|
+
|
|
|
+ SharedHandle<SingleFileDownloadContext> dctx
|
|
|
+ (new SingleFileDownloadContext(512, 81920, "load-nonBt"));
|
|
|
+
|
|
|
+ DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), infoFile.getFilename());
|
|
|
+ infoFile.load();
|
|
|
+
|
|
|
+ // check the contents of objects
|
|
|
+
|
|
|
+ // bitfield
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffffffffffffffffffffffc"),
|
|
|
+ Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength()));
|
|
|
+
|
|
|
+ // the number of in-flight pieces
|
|
|
+ CPPUNIT_ASSERT_EQUAL((size_t)0,
|
|
|
+ _pieceStorage->countInFlightPiece());
|
|
|
+}
|
|
|
+
|
|
|
+void DefaultBtProgressInfoFileTest::testSave_nonBt()
|
|
|
+{
|
|
|
+ initializeMembers(1024, 81920);
|
|
|
+
|
|
|
+ SharedHandle<SingleFileDownloadContext> dctx
|
|
|
+ (new SingleFileDownloadContext(1024, 81920, "save-temp"));
|
|
|
+
|
|
|
_bitfield->setAllBit();
|
|
|
_bitfield->unsetBit(79);
|
|
|
_pieceStorage->setCompletedLength(80896);
|
|
|
- TransferStat stat;
|
|
|
- stat.setAllTimeUploadLength(1024);
|
|
|
- dynamic_pointer_cast<MockPeerStorage>(PEER_STORAGE(_btContext))->setStat(stat);
|
|
|
|
|
|
SharedHandle<Piece> p1(new Piece(1, 1024));
|
|
|
SharedHandle<Piece> p2(new Piece(2, 512));
|
|
@@ -307,11 +327,11 @@ void DefaultBtProgressInfoFileTest::testSave()
|
|
|
inFlightPieces.push_back(p2);
|
|
|
_pieceStorage->addInFlightPiece(inFlightPieces);
|
|
|
|
|
|
- DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get());
|
|
|
+ DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./save-temp.aria2"), infoFile.getFilename());
|
|
|
|
|
|
infoFile.save();
|
|
|
-
|
|
|
+
|
|
|
// read and validate
|
|
|
std::ifstream in(infoFile.getFilename().c_str());
|
|
|
|
|
@@ -323,28 +343,23 @@ void DefaultBtProgressInfoFileTest::testSave()
|
|
|
|
|
|
unsigned char extension[4];
|
|
|
in.read((char*)extension, sizeof(extension));
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("00000001"), Util::toHex(extension, sizeof(extension)));
|
|
|
+ CPPUNIT_ASSERT_EQUAL(std::string("00000000"), Util::toHex(extension, sizeof(extension)));
|
|
|
|
|
|
uint32_t infoHashLength;
|
|
|
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
|
|
|
- CPPUNIT_ASSERT_EQUAL((uint32_t)20, infoHashLength);
|
|
|
-
|
|
|
- unsigned char infoHashRead[20];
|
|
|
- in.read((char*)infoHashRead, sizeof(infoHashRead));
|
|
|
- CPPUNIT_ASSERT_EQUAL(std::string("112233445566778899aabbccddeeff00ffffffff"),
|
|
|
- Util::toHex(infoHashRead, sizeof(infoHashRead)));
|
|
|
+ CPPUNIT_ASSERT_EQUAL((uint32_t)0, infoHashLength);
|
|
|
|
|
|
uint32_t pieceLength;
|
|
|
in.read((char*)&pieceLength, sizeof(pieceLength));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength);
|
|
|
-
|
|
|
+
|
|
|
uint64_t totalLength;
|
|
|
in.read((char*)&totalLength, sizeof(totalLength));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength);
|
|
|
|
|
|
uint64_t uploadLength;
|
|
|
in.read((char*)&uploadLength, sizeof(uploadLength));
|
|
|
- CPPUNIT_ASSERT_EQUAL((uint64_t)1024, uploadLength);
|
|
|
+ CPPUNIT_ASSERT_EQUAL((uint64_t)0, uploadLength);
|
|
|
|
|
|
uint32_t bitfieldLength;
|
|
|
in.read((char*)&bitfieldLength, sizeof(bitfieldLength));
|
|
@@ -386,7 +401,6 @@ void DefaultBtProgressInfoFileTest::testSave()
|
|
|
in.read((char*)&pieceLength2, sizeof(pieceLength2));
|
|
|
CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2);
|
|
|
|
|
|
-
|
|
|
}
|
|
|
|
|
|
void DefaultBtProgressInfoFileTest::testUpdateFilename()
|