2007-05-31 15:56:20 +00:00
|
|
|
#include "IteratableChunkChecksumValidator.h"
|
2009-02-01 02:50:38 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DefaultPieceStorage.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "DiskAdaptor.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2009-02-01 02:50:38 +00:00
|
|
|
#include "PieceSelector.h"
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
class IteratableChunkChecksumValidatorTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(IteratableChunkChecksumValidatorTest);
|
|
|
|
CPPUNIT_TEST(testValidate);
|
2009-05-03 06:26:44 +00:00
|
|
|
CPPUNIT_TEST(testValidate_readError);
|
2007-05-31 15:56:20 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
static const char* csArray[];
|
2007-05-31 15:56:20 +00:00
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testValidate();
|
2009-05-03 06:26:44 +00:00
|
|
|
void testValidate_readError();
|
2007-05-31 15:56:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChunkChecksumValidatorTest );
|
|
|
|
|
|
|
|
const char* IteratableChunkChecksumValidatorTest::csArray[] = { "29b0e7878271645fffb7eec7db4a7473a1c00bc1",
|
2010-01-05 16:01:46 +00:00
|
|
|
"4df75a661cb7eb2733d9cdaa7f772eae3a4e2976",
|
|
|
|
"0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb" };
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
void IteratableChunkChecksumValidatorTest::testValidate() {
|
2007-10-11 16:58:24 +00:00
|
|
|
Option option;
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> dctx
|
|
|
|
(new DownloadContext(100, 250, "chunkChecksumTestFile250.txt"));
|
|
|
|
dctx->setPieceHashes(&csArray[0], &csArray[3]);
|
2010-11-11 02:56:24 +00:00
|
|
|
dctx->setPieceHashAlgo("sha-1");
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<DefaultPieceStorage> ps
|
|
|
|
(new DefaultPieceStorage(dctx, &option));
|
2007-10-11 16:58:24 +00:00
|
|
|
ps->initStorage();
|
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChunkChecksumValidator validator(dctx, ps);
|
2007-12-05 13:41:56 +00:00
|
|
|
validator.init();
|
|
|
|
|
2007-05-31 15:56:20 +00:00
|
|
|
validator.validateChunk();
|
|
|
|
CPPUNIT_ASSERT(!validator.finished());
|
|
|
|
validator.validateChunk();
|
|
|
|
CPPUNIT_ASSERT(!validator.finished());
|
|
|
|
validator.validateChunk();
|
|
|
|
CPPUNIT_ASSERT(validator.finished());
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT(ps->downloadFinished());
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
// make the test fail
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
|
2007-10-11 16:58:24 +00:00
|
|
|
badHashes[1] = "ffffffffffffffffffffffffffffffffffffffff";
|
2009-06-28 10:37:15 +00:00
|
|
|
dctx->setPieceHashes(badHashes.begin(), badHashes.end());
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
validator.init();
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2007-05-31 15:56:20 +00:00
|
|
|
while(!validator.finished()) {
|
|
|
|
validator.validateChunk();
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(0));
|
|
|
|
CPPUNIT_ASSERT(!ps->hasPiece(1));
|
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(2));
|
2007-05-31 15:56:20 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-05-03 06:26:44 +00:00
|
|
|
void IteratableChunkChecksumValidatorTest::testValidate_readError() {
|
|
|
|
Option option;
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> dctx
|
|
|
|
(new DownloadContext(100, 500, "chunkChecksumTestFile250.txt"));
|
2009-05-03 06:26:44 +00:00
|
|
|
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
|
|
|
|
hashes.push_back("ffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
hashes.push_back("ffffffffffffffffffffffffffffffffffffffff");
|
2009-06-28 10:37:15 +00:00
|
|
|
dctx->setPieceHashes(hashes.begin(), hashes.end());
|
2010-11-11 02:56:24 +00:00
|
|
|
dctx->setPieceHashAlgo("sha-1");
|
2009-05-03 06:26:44 +00:00
|
|
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
|
|
|
ps->initStorage();
|
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChunkChecksumValidator validator(dctx, ps);
|
|
|
|
validator.init();
|
|
|
|
|
|
|
|
while(!validator.finished()) {
|
|
|
|
validator.validateChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(0));
|
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(1));
|
|
|
|
CPPUNIT_ASSERT(!ps->hasPiece(2)); // #2 piece is not valid because
|
2010-01-05 16:01:46 +00:00
|
|
|
// #program expects its size is
|
|
|
|
// #100, but it reads only 50
|
|
|
|
// #bytes and raises error.
|
2009-05-03 06:26:44 +00:00
|
|
|
CPPUNIT_ASSERT(!ps->hasPiece(3));
|
|
|
|
CPPUNIT_ASSERT(!ps->hasPiece(4));
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|