2008-08-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Bump up version number of .aria2 control file to 0001.
	New aria2 can still load version 0000 file but it saves the file 
in
	version 0001 format. It means that new aria2 can resume download
	started by old aria2 but the opposite is not true.
	* src/DefaultBtProgressInfoFile.cc
	* src/DefaultBtProgressInfoFile.h
	* test/DefaultBtProgressInfoFileTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2008-08-24 09:43:45 +00:00
parent 02dde388b8
commit 335a0fb36f
6 changed files with 216 additions and 41 deletions

View File

@ -1,3 +1,13 @@
2008-08-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Bump up version number of .aria2 control file to 0001.
New aria2 can still load version 0000 file but it saves the file in
version 0001 format. It means that new aria2 can resume download
started by old aria2 but the opposite is not true.
* src/DefaultBtProgressInfoFile.cc
* src/DefaultBtProgressInfoFile.h
* test/DefaultBtProgressInfoFileTest.cc
2008-08-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-08-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added ntoh64 and hton64 as inline functions. Added ntoh64 and hton64 as inline functions.

View File

@ -59,6 +59,7 @@
namespace aria2 { namespace aria2 {
const std::string DefaultBtProgressInfoFile::V0000("0000"); const std::string DefaultBtProgressInfoFile::V0000("0000");
const std::string DefaultBtProgressInfoFile::V0001("0001");
static std::string createFilename(const SharedHandle<DownloadContext>& dctx) static std::string createFilename(const SharedHandle<DownloadContext>& dctx)
{ {
@ -88,6 +89,7 @@ bool DefaultBtProgressInfoFile::isTorrentDownload()
return !dynamic_pointer_cast<BtContext>(_dctx).isNull(); return !dynamic_pointer_cast<BtContext>(_dctx).isNull();
} }
// Since version 0001, Integers are saved in binary form, network byte order.
void DefaultBtProgressInfoFile::save() { void DefaultBtProgressInfoFile::save() {
_logger->info(MSG_SAVING_SEGMENT_FILE, _filename.c_str()); _logger->info(MSG_SAVING_SEGMENT_FILE, _filename.c_str());
std::string filenameTemp = _filename+"__temp"; std::string filenameTemp = _filename+"__temp";
@ -96,9 +98,9 @@ void DefaultBtProgressInfoFile::save() {
o.exceptions(std::ios::failbit); o.exceptions(std::ios::failbit);
bool torrentDownload = isTorrentDownload(); bool torrentDownload = isTorrentDownload();
// file version: 16 bits // file version: 16 bits
// value: '0' // values: '1'
uint16_t version = 0; char version[] = { 0x00, 0x01 };
o.write(reinterpret_cast<const char*>(&version), sizeof(version)); o.write(version, sizeof(version));
// extension: 32 bits // extension: 32 bits
// If this is BitTorrent download, then 0x00000001 // If this is BitTorrent download, then 0x00000001
// Otherwise, 0x00000000 // Otherwise, 0x00000000
@ -112,9 +114,9 @@ void DefaultBtProgressInfoFile::save() {
// infoHashLength: // infoHashLength:
// length: 32 bits // length: 32 bits
BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx)); BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx));
uint32_t infoHashLength = btContext->getInfoHashLength(); uint32_t infoHashLengthNL = htonl(btContext->getInfoHashLength());
o.write(reinterpret_cast<const char*>(&infoHashLength), o.write(reinterpret_cast<const char*>(&infoHashLengthNL),
sizeof(infoHashLength)); sizeof(infoHashLengthNL));
// infoHash: // infoHash:
o.write(reinterpret_cast<const char*>(btContext->getInfoHash()), o.write(reinterpret_cast<const char*>(btContext->getInfoHash()),
btContext->getInfoHashLength()); btContext->getInfoHashLength());
@ -126,44 +128,47 @@ void DefaultBtProgressInfoFile::save() {
sizeof(infoHashLength)); sizeof(infoHashLength));
} }
// pieceLength: 32 bits // pieceLength: 32 bits
uint32_t pieceLength = _dctx->getPieceLength(); uint32_t pieceLengthNL = htonl(_dctx->getPieceLength());
o.write(reinterpret_cast<const char*>(&pieceLength), sizeof(pieceLength)); o.write(reinterpret_cast<const char*>(&pieceLengthNL),
sizeof(pieceLengthNL));
// totalLength: 64 bits // totalLength: 64 bits
uint64_t totalLength = _dctx->getTotalLength(); uint64_t totalLengthNL = hton64(_dctx->getTotalLength());
o.write(reinterpret_cast<const char*>(&totalLength), sizeof(totalLength)); o.write(reinterpret_cast<const char*>(&totalLengthNL),
sizeof(totalLengthNL));
// uploadLength: 64 bits // uploadLength: 64 bits
uint64_t uploadLength = 0; uint64_t uploadLengthNL = 0;
if(torrentDownload) { if(torrentDownload) {
BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx)); BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx));
TransferStat stat = PEER_STORAGE(btContext)->calculateStat(); TransferStat stat = PEER_STORAGE(btContext)->calculateStat();
uploadLength = stat.getAllTimeUploadLength(); uploadLengthNL = hton64(stat.getAllTimeUploadLength());
} }
o.write(reinterpret_cast<const char*>(&uploadLength), sizeof(uploadLength)); o.write(reinterpret_cast<const char*>(&uploadLengthNL),
sizeof(uploadLengthNL));
// bitfieldLength: 32 bits // bitfieldLength: 32 bits
uint32_t bitfieldLength = _pieceStorage->getBitfieldLength(); uint32_t bitfieldLengthNL = htonl(_pieceStorage->getBitfieldLength());
o.write(reinterpret_cast<const char*>(&bitfieldLength), o.write(reinterpret_cast<const char*>(&bitfieldLengthNL),
sizeof(bitfieldLength)); sizeof(bitfieldLengthNL));
// bitfield // bitfield
o.write(reinterpret_cast<const char*>(_pieceStorage->getBitfield()), o.write(reinterpret_cast<const char*>(_pieceStorage->getBitfield()),
_pieceStorage->getBitfieldLength()); _pieceStorage->getBitfieldLength());
// the number of in-flight piece: 32 bits // the number of in-flight piece: 32 bits
// TODO implement this // TODO implement this
uint32_t numInFlightPiece = _pieceStorage->countInFlightPiece(); uint32_t numInFlightPieceNL = htonl(_pieceStorage->countInFlightPiece());
o.write(reinterpret_cast<const char*>(&numInFlightPiece), o.write(reinterpret_cast<const char*>(&numInFlightPieceNL),
sizeof(numInFlightPiece)); sizeof(numInFlightPieceNL));
Pieces inFlightPieces; Pieces inFlightPieces;
_pieceStorage->getInFlightPieces(inFlightPieces); _pieceStorage->getInFlightPieces(inFlightPieces);
for(Pieces::const_iterator itr = inFlightPieces.begin(); for(Pieces::const_iterator itr = inFlightPieces.begin();
itr != inFlightPieces.end(); ++itr) { itr != inFlightPieces.end(); ++itr) {
uint32_t index = (*itr)->getIndex(); uint32_t indexNL = htonl((*itr)->getIndex());
o.write(reinterpret_cast<const char*>(&index), sizeof(index)); o.write(reinterpret_cast<const char*>(&indexNL), sizeof(indexNL));
uint32_t length = (*itr)->getLength(); uint32_t lengthNL = htonl((*itr)->getLength());
o.write(reinterpret_cast<const char*>(&length), sizeof(length)); o.write(reinterpret_cast<const char*>(&lengthNL), sizeof(lengthNL));
uint32_t bitfieldLength = (*itr)->getBitfieldLength(); uint32_t bitfieldLengthNL = htonl((*itr)->getBitfieldLength());
o.write(reinterpret_cast<const char*>(&bitfieldLength), o.write(reinterpret_cast<const char*>(&bitfieldLengthNL),
sizeof(bitfieldLength)); sizeof(bitfieldLengthNL));
o.write(reinterpret_cast<const char*>((*itr)->getBitfield()), o.write(reinterpret_cast<const char*>((*itr)->getBitfield()),
bitfieldLength); (*itr)->getBitfieldLength());
} }
o.close(); o.close();
@ -179,6 +184,9 @@ void DefaultBtProgressInfoFile::save() {
} }
} }
// It is assumed that integers are saved as:
// 1) host byte order if version == 0000
// 2) network byte order if version == 0001
void DefaultBtProgressInfoFile::load() void DefaultBtProgressInfoFile::load()
{ {
_logger->info(MSG_LOADING_SEGMENT_FILE, _filename.c_str()); _logger->info(MSG_LOADING_SEGMENT_FILE, _filename.c_str());
@ -187,13 +195,18 @@ void DefaultBtProgressInfoFile::load()
unsigned char* savedBitfield = 0; unsigned char* savedBitfield = 0;
try { try {
in.exceptions(std::ios::failbit); in.exceptions(std::ios::failbit);
unsigned char version[2]; unsigned char versionBuf[2];
in.read((char*)version, sizeof(version)); in.read((char*)versionBuf, sizeof(versionBuf));
if(DefaultBtProgressInfoFile::V0000 != std::string versionHex = Util::toHex(versionBuf, sizeof(versionBuf));
Util::toHex(version, sizeof(version))) { int version;
if(DefaultBtProgressInfoFile::V0000 == versionHex) {
version = 0;
} else if(DefaultBtProgressInfoFile::V0001 == versionHex) {
version = 1;
} else {
throw DlAbortEx throw DlAbortEx
(StringFormat("Unsupported ctrl file version: %s", (StringFormat("Unsupported ctrl file version: %s",
Util::toHex(version, sizeof(version)).c_str()).str()); versionHex.c_str()).str());
} }
unsigned char extension[4]; unsigned char extension[4];
in.read((char*)extension, sizeof(extension)); in.read((char*)extension, sizeof(extension));
@ -206,6 +219,9 @@ void DefaultBtProgressInfoFile::load()
uint32_t infoHashLength; uint32_t infoHashLength;
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength)); in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
if(version >= 1) {
infoHashLength = ntohl(infoHashLength);
}
if((infoHashLength < 0) || if((infoHashLength < 0) ||
((infoHashLength == 0) && infoHashCheckEnabled)) { ((infoHashLength == 0) && infoHashCheckEnabled)) {
throw DlAbortEx throw DlAbortEx
@ -230,9 +246,15 @@ void DefaultBtProgressInfoFile::load()
uint32_t pieceLength; uint32_t pieceLength;
in.read(reinterpret_cast<char*>(&pieceLength), sizeof(pieceLength)); in.read(reinterpret_cast<char*>(&pieceLength), sizeof(pieceLength));
if(version >= 1) {
pieceLength = ntohl(pieceLength);
}
uint64_t totalLength; uint64_t totalLength;
in.read(reinterpret_cast<char*>(&totalLength), sizeof(totalLength)); in.read(reinterpret_cast<char*>(&totalLength), sizeof(totalLength));
if(version >= 1) {
totalLength = ntoh64(totalLength);
}
if(totalLength != _dctx->getTotalLength()) { if(totalLength != _dctx->getTotalLength()) {
throw DlAbortEx throw DlAbortEx
(StringFormat("total length mismatch. expected: %s, actual: %s", (StringFormat("total length mismatch. expected: %s, actual: %s",
@ -241,6 +263,9 @@ void DefaultBtProgressInfoFile::load()
} }
uint64_t uploadLength; uint64_t uploadLength;
in.read(reinterpret_cast<char*>(&uploadLength), sizeof(uploadLength)); in.read(reinterpret_cast<char*>(&uploadLength), sizeof(uploadLength));
if(version >= 1) {
uploadLength = ntoh64(uploadLength);
}
if(isTorrentDownload()) { if(isTorrentDownload()) {
BT_RUNTIME(dynamic_pointer_cast<BtContext>(_dctx))-> BT_RUNTIME(dynamic_pointer_cast<BtContext>(_dctx))->
setUploadLengthAtStartup(uploadLength); setUploadLengthAtStartup(uploadLength);
@ -249,6 +274,9 @@ void DefaultBtProgressInfoFile::load()
// TODO implement the conversion mechanism between different piece length. // TODO implement the conversion mechanism between different piece length.
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read(reinterpret_cast<char*>(&bitfieldLength), sizeof(bitfieldLength)); in.read(reinterpret_cast<char*>(&bitfieldLength), sizeof(bitfieldLength));
if(version >= 1) {
bitfieldLength = ntohl(bitfieldLength);
}
uint32_t expectedBitfieldLength = uint32_t expectedBitfieldLength =
((totalLength+pieceLength-1)/pieceLength+7)/8; ((totalLength+pieceLength-1)/pieceLength+7)/8;
if(expectedBitfieldLength != bitfieldLength) { if(expectedBitfieldLength != bitfieldLength) {
@ -269,17 +297,25 @@ void DefaultBtProgressInfoFile::load()
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read(reinterpret_cast<char*>(&numInFlightPiece), in.read(reinterpret_cast<char*>(&numInFlightPiece),
sizeof(numInFlightPiece)); sizeof(numInFlightPiece));
if(version >= 1) {
numInFlightPiece = ntohl(numInFlightPiece);
}
Pieces inFlightPieces; Pieces inFlightPieces;
while(numInFlightPiece--) { while(numInFlightPiece--) {
uint32_t index; uint32_t index;
in.read(reinterpret_cast<char*>(&index), sizeof(index)); in.read(reinterpret_cast<char*>(&index), sizeof(index));
if(version >= 1) {
index = ntohl(index);
}
if(!(index < _dctx->getNumPieces())) { if(!(index < _dctx->getNumPieces())) {
throw DlAbortEx throw DlAbortEx
(StringFormat("piece index out of range: %u", index).str()); (StringFormat("piece index out of range: %u", index).str());
} }
uint32_t length; uint32_t length;
in.read(reinterpret_cast<char*>(&length), sizeof(length)); in.read(reinterpret_cast<char*>(&length), sizeof(length));
if(version >= 1) {
length = ntohl(length);
}
if(!(length <=_dctx->getPieceLength())) { if(!(length <=_dctx->getPieceLength())) {
throw DlAbortEx throw DlAbortEx
(StringFormat("piece length out of range: %u", length).str()); (StringFormat("piece length out of range: %u", length).str());
@ -288,6 +324,9 @@ void DefaultBtProgressInfoFile::load()
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read(reinterpret_cast<char*>(&bitfieldLength), in.read(reinterpret_cast<char*>(&bitfieldLength),
sizeof(bitfieldLength)); sizeof(bitfieldLength));
if(version >= 1) {
bitfieldLength = ntohl(bitfieldLength);
}
if(piece->getBitfieldLength() != bitfieldLength) { if(piece->getBitfieldLength() != bitfieldLength) {
throw DlAbortEx throw DlAbortEx
(StringFormat("piece bitfield length mismatch." (StringFormat("piece bitfield length mismatch."
@ -314,6 +353,9 @@ void DefaultBtProgressInfoFile::load()
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read(reinterpret_cast<char*>(&numInFlightPiece), in.read(reinterpret_cast<char*>(&numInFlightPiece),
sizeof(numInFlightPiece)); sizeof(numInFlightPiece));
if(version >= 1) {
numInFlightPiece = ntohl(numInFlightPiece);
}
BitfieldMan src(pieceLength, totalLength); BitfieldMan src(pieceLength, totalLength);
src.setBitfield(savedBitfield, bitfieldLength); src.setBitfield(savedBitfield, bitfieldLength);
if((src.getCompletedLength() || numInFlightPiece) && if((src.getCompletedLength() || numInFlightPiece) &&

View File

@ -55,6 +55,7 @@ private:
bool isTorrentDownload(); bool isTorrentDownload();
static const std::string V0000; static const std::string V0000;
static const std::string V0001;
public: public:
DefaultBtProgressInfoFile(const SharedHandle<DownloadContext>& btContext, DefaultBtProgressInfoFile(const SharedHandle<DownloadContext>& btContext,
const SharedHandle<PieceStorage>& pieceStorage, const SharedHandle<PieceStorage>& pieceStorage,

View File

@ -24,9 +24,11 @@ class DefaultBtProgressInfoFileTest:public CppUnit::TestFixture {
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
CPPUNIT_TEST(testSave); CPPUNIT_TEST(testSave);
CPPUNIT_TEST(testLoad); CPPUNIT_TEST(testLoad);
CPPUNIT_TEST(testLoad_compat);
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
CPPUNIT_TEST(testSave_nonBt); CPPUNIT_TEST(testSave_nonBt);
CPPUNIT_TEST(testLoad_nonBt); CPPUNIT_TEST(testLoad_nonBt);
CPPUNIT_TEST(testLoad_nonBt_compat);
CPPUNIT_TEST(testLoad_nonBt_pieceLengthShorter); CPPUNIT_TEST(testLoad_nonBt_pieceLengthShorter);
CPPUNIT_TEST(testUpdateFilename); CPPUNIT_TEST(testUpdateFilename);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
@ -85,8 +87,10 @@ public:
void testSave(); void testSave();
void testLoad(); void testLoad();
void testLoad_compat();
void testSave_nonBt(); void testSave_nonBt();
void testLoad_nonBt(); void testLoad_nonBt();
void testLoad_nonBt_compat();
void testLoad_nonBt_pieceLengthShorter(); void testLoad_nonBt_pieceLengthShorter();
void testUpdateFilename(); void testUpdateFilename();
}; };
@ -98,7 +102,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtProgressInfoFileTest);
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
void DefaultBtProgressInfoFileTest::testLoad() void DefaultBtProgressInfoFileTest::testLoad_compat()
{ {
initializeMembers(1024, 81920); initializeMembers(1024, 81920);
@ -114,6 +118,57 @@ void DefaultBtProgressInfoFileTest::testLoad()
// check the contents of objects // check the contents of objects
// total length
CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _btContext->getTotalLength());
// upload length
CPPUNIT_ASSERT_EQUAL((uint64_t)1024, BT_RUNTIME(_btContext)->getUploadLengthAtStartup());
// 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()
{
initializeMembers(1024, 81920);
_btContext->setName("load-v0001");
_btContext->setPieceLength(1024);
_btContext->setTotalLength(81920);
_btContext->setNumPieces(80);
DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get());
CPPUNIT_ASSERT_EQUAL(std::string("./load-v0001.aria2"),
infoFile.getFilename());
infoFile.load();
// check the contents of objects
// total length
CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _btContext->getTotalLength());
// upload length // upload length
CPPUNIT_ASSERT_EQUAL((uint64_t)1024, BT_RUNTIME(_btContext)->getUploadLengthAtStartup()); CPPUNIT_ASSERT_EQUAL((uint64_t)1024, BT_RUNTIME(_btContext)->getUploadLengthAtStartup());
@ -175,7 +230,8 @@ void DefaultBtProgressInfoFileTest::testSave()
unsigned char version[2]; unsigned char version[2];
in.read((char*)version, sizeof(version)); in.read((char*)version, sizeof(version));
CPPUNIT_ASSERT_EQUAL(std::string("0000"), Util::toHex(version, sizeof(version))); CPPUNIT_ASSERT_EQUAL(std::string("0001"),
Util::toHex(version, sizeof(version)));
unsigned char extension[4]; unsigned char extension[4];
in.read((char*)extension, sizeof(extension)); in.read((char*)extension, sizeof(extension));
@ -183,6 +239,7 @@ void DefaultBtProgressInfoFileTest::testSave()
uint32_t infoHashLength; uint32_t infoHashLength;
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength)); in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
infoHashLength = ntohl(infoHashLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)20, infoHashLength); CPPUNIT_ASSERT_EQUAL((uint32_t)20, infoHashLength);
unsigned char infoHashRead[20]; unsigned char infoHashRead[20];
@ -192,18 +249,22 @@ void DefaultBtProgressInfoFileTest::testSave()
uint32_t pieceLength; uint32_t pieceLength;
in.read((char*)&pieceLength, sizeof(pieceLength)); in.read((char*)&pieceLength, sizeof(pieceLength));
pieceLength = ntohl(pieceLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength);
uint64_t totalLength; uint64_t totalLength;
in.read((char*)&totalLength, sizeof(totalLength)); in.read((char*)&totalLength, sizeof(totalLength));
totalLength = ntoh64(totalLength);
CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength); CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength);
uint64_t uploadLength; uint64_t uploadLength;
in.read((char*)&uploadLength, sizeof(uploadLength)); in.read((char*)&uploadLength, sizeof(uploadLength));
uploadLength = ntoh64(uploadLength);
CPPUNIT_ASSERT_EQUAL((uint64_t)1024, uploadLength); CPPUNIT_ASSERT_EQUAL((uint64_t)1024, uploadLength);
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read((char*)&bitfieldLength, sizeof(bitfieldLength)); in.read((char*)&bitfieldLength, sizeof(bitfieldLength));
bitfieldLength = ntohl(bitfieldLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)10, bitfieldLength); CPPUNIT_ASSERT_EQUAL((uint32_t)10, bitfieldLength);
unsigned char bitfieldRead[10]; unsigned char bitfieldRead[10];
@ -213,19 +274,23 @@ void DefaultBtProgressInfoFileTest::testSave()
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read((char*)&numInFlightPiece, sizeof(numInFlightPiece)); in.read((char*)&numInFlightPiece, sizeof(numInFlightPiece));
numInFlightPiece = ntohl(numInFlightPiece);
CPPUNIT_ASSERT_EQUAL((uint32_t)2, numInFlightPiece); CPPUNIT_ASSERT_EQUAL((uint32_t)2, numInFlightPiece);
// piece index 1 // piece index 1
uint32_t index1; uint32_t index1;
in.read((char*)&index1, sizeof(index1)); in.read((char*)&index1, sizeof(index1));
index1 = ntohl(index1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1, index1); CPPUNIT_ASSERT_EQUAL((uint32_t)1, index1);
uint32_t pieceLength1; uint32_t pieceLength1;
in.read((char*)&pieceLength1, sizeof(pieceLength1)); in.read((char*)&pieceLength1, sizeof(pieceLength1));
pieceLength1 = ntohl(pieceLength1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength1); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength1);
uint32_t pieceBitfieldLength1; uint32_t pieceBitfieldLength1;
in.read((char*)&pieceBitfieldLength1, sizeof(pieceBitfieldLength1)); in.read((char*)&pieceBitfieldLength1, sizeof(pieceBitfieldLength1));
pieceBitfieldLength1 = ntohl(pieceBitfieldLength1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceBitfieldLength1); CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceBitfieldLength1);
unsigned char pieceBitfield1[1]; unsigned char pieceBitfield1[1];
@ -236,18 +301,18 @@ void DefaultBtProgressInfoFileTest::testSave()
// piece index 2 // piece index 2
uint32_t index2; uint32_t index2;
in.read((char*)&index2, sizeof(index2)); in.read((char*)&index2, sizeof(index2));
index2 = ntohl(index2);
CPPUNIT_ASSERT_EQUAL((uint32_t)2, index2); CPPUNIT_ASSERT_EQUAL((uint32_t)2, index2);
uint32_t pieceLength2; uint32_t pieceLength2;
in.read((char*)&pieceLength2, sizeof(pieceLength2)); in.read((char*)&pieceLength2, sizeof(pieceLength2));
pieceLength2 = ntohl(pieceLength2);
CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2); CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2);
} }
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
void DefaultBtProgressInfoFileTest::testLoad_nonBt() void DefaultBtProgressInfoFileTest::testLoad_nonBt_compat()
{ {
initializeMembers(1024, 81920); initializeMembers(1024, 81920);
@ -260,6 +325,9 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt()
// check the contents of objects // check the contents of objects
// total length
CPPUNIT_ASSERT_EQUAL((uint64_t)81920, dctx->getTotalLength());
// bitfield // bitfield
CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffe"), CPPUNIT_ASSERT_EQUAL(std::string("fffffffffffffffffffe"),
Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength())); Util::toHex(_bitfield->getBitfield(), _bitfield->getBitfieldLength()));
@ -283,7 +351,48 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt()
SharedHandle<Piece> piece2 = inFlightPieces[1]; SharedHandle<Piece> piece2 = inFlightPieces[1];
CPPUNIT_ASSERT_EQUAL((size_t)2, piece2->getIndex()); CPPUNIT_ASSERT_EQUAL((size_t)2, piece2->getIndex());
CPPUNIT_ASSERT_EQUAL((size_t)512, piece2->getLength()); CPPUNIT_ASSERT_EQUAL((size_t)512, piece2->getLength());
}
void DefaultBtProgressInfoFileTest::testLoad_nonBt()
{
initializeMembers(1024, 81920);
SharedHandle<SingleFileDownloadContext> dctx
(new SingleFileDownloadContext(1024, 81920, "load-nonBt-v0001"));
DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt-v0001.aria2"),
infoFile.getFilename());
infoFile.load();
// check the contents of objects
// total length
CPPUNIT_ASSERT_EQUAL((uint64_t)81920, dctx->getTotalLength());
// 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() void DefaultBtProgressInfoFileTest::testLoad_nonBt_pieceLengthShorter()
@ -292,10 +401,11 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt_pieceLengthShorter()
_option->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_TRUE); _option->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_TRUE);
SharedHandle<SingleFileDownloadContext> dctx SharedHandle<SingleFileDownloadContext> dctx
(new SingleFileDownloadContext(512, 81920, "load-nonBt")); (new SingleFileDownloadContext(512, 81920, "load-nonBt-v0001"));
DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get()); DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get());
CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), infoFile.getFilename()); CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt-v0001.aria2"),
infoFile.getFilename());
infoFile.load(); infoFile.load();
// check the contents of objects // check the contents of objects
@ -339,7 +449,8 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
unsigned char version[2]; unsigned char version[2];
in.read((char*)version, sizeof(version)); in.read((char*)version, sizeof(version));
CPPUNIT_ASSERT_EQUAL(std::string("0000"), Util::toHex(version, sizeof(version))); CPPUNIT_ASSERT_EQUAL(std::string("0001"),
Util::toHex(version, sizeof(version)));
unsigned char extension[4]; unsigned char extension[4];
in.read((char*)extension, sizeof(extension)); in.read((char*)extension, sizeof(extension));
@ -347,22 +458,27 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
uint32_t infoHashLength; uint32_t infoHashLength;
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength)); in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
infoHashLength = ntohl(infoHashLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)0, infoHashLength); CPPUNIT_ASSERT_EQUAL((uint32_t)0, infoHashLength);
uint32_t pieceLength; uint32_t pieceLength;
in.read((char*)&pieceLength, sizeof(pieceLength)); in.read((char*)&pieceLength, sizeof(pieceLength));
pieceLength = ntohl(pieceLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength);
uint64_t totalLength; uint64_t totalLength;
in.read((char*)&totalLength, sizeof(totalLength)); in.read((char*)&totalLength, sizeof(totalLength));
totalLength = ntoh64(totalLength);
CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength); CPPUNIT_ASSERT_EQUAL((uint64_t)81920/* 80*1024 */, totalLength);
uint64_t uploadLength; uint64_t uploadLength;
in.read((char*)&uploadLength, sizeof(uploadLength)); in.read((char*)&uploadLength, sizeof(uploadLength));
uploadLength = ntoh64(uploadLength);
CPPUNIT_ASSERT_EQUAL((uint64_t)0, uploadLength); CPPUNIT_ASSERT_EQUAL((uint64_t)0, uploadLength);
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read((char*)&bitfieldLength, sizeof(bitfieldLength)); in.read((char*)&bitfieldLength, sizeof(bitfieldLength));
bitfieldLength = ntohl(bitfieldLength);
CPPUNIT_ASSERT_EQUAL((uint32_t)10, bitfieldLength); CPPUNIT_ASSERT_EQUAL((uint32_t)10, bitfieldLength);
unsigned char bitfieldRead[10]; unsigned char bitfieldRead[10];
@ -372,19 +488,23 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read((char*)&numInFlightPiece, sizeof(numInFlightPiece)); in.read((char*)&numInFlightPiece, sizeof(numInFlightPiece));
numInFlightPiece = ntohl(numInFlightPiece);
CPPUNIT_ASSERT_EQUAL((uint32_t)2, numInFlightPiece); CPPUNIT_ASSERT_EQUAL((uint32_t)2, numInFlightPiece);
// piece index 1 // piece index 1
uint32_t index1; uint32_t index1;
in.read((char*)&index1, sizeof(index1)); in.read((char*)&index1, sizeof(index1));
index1 = ntohl(index1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1, index1); CPPUNIT_ASSERT_EQUAL((uint32_t)1, index1);
uint32_t pieceLength1; uint32_t pieceLength1;
in.read((char*)&pieceLength1, sizeof(pieceLength1)); in.read((char*)&pieceLength1, sizeof(pieceLength1));
pieceLength1 = ntohl(pieceLength1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength1); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pieceLength1);
uint32_t pieceBitfieldLength1; uint32_t pieceBitfieldLength1;
in.read((char*)&pieceBitfieldLength1, sizeof(pieceBitfieldLength1)); in.read((char*)&pieceBitfieldLength1, sizeof(pieceBitfieldLength1));
pieceBitfieldLength1 = ntohl(pieceBitfieldLength1);
CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceBitfieldLength1); CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceBitfieldLength1);
unsigned char pieceBitfield1[1]; unsigned char pieceBitfield1[1];
@ -395,10 +515,12 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt()
// piece index 2 // piece index 2
uint32_t index2; uint32_t index2;
in.read((char*)&index2, sizeof(index2)); in.read((char*)&index2, sizeof(index2));
index2 = ntohl(index2);
CPPUNIT_ASSERT_EQUAL((uint32_t)2, index2); CPPUNIT_ASSERT_EQUAL((uint32_t)2, index2);
uint32_t pieceLength2; uint32_t pieceLength2;
in.read((char*)&pieceLength2, sizeof(pieceLength2)); in.read((char*)&pieceLength2, sizeof(pieceLength2));
pieceLength2 = ntohl(pieceLength2);
CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2); CPPUNIT_ASSERT_EQUAL((uint32_t)512, pieceLength2);
} }

BIN
test/load-nonBt-v0001.aria2 Normal file

Binary file not shown.

BIN
test/load-v0001.aria2 Normal file

Binary file not shown.