2007-05-31 15:56:20 +00:00
|
|
|
#include "IteratableChunkChecksumValidator.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "SingleFileDownloadContext.h"
|
|
|
|
#include "DefaultPieceStorage.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "DiskAdaptor.h"
|
2007-05-31 15:56:20 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class IteratableChunkChecksumValidatorTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(IteratableChunkChecksumValidatorTest);
|
|
|
|
CPPUNIT_TEST(testValidate);
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChunkChecksumValidatorTest );
|
|
|
|
|
|
|
|
const char* IteratableChunkChecksumValidatorTest::csArray[] = { "29b0e7878271645fffb7eec7db4a7473a1c00bc1",
|
|
|
|
"4df75a661cb7eb2733d9cdaa7f772eae3a4e2976",
|
|
|
|
"0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb" };
|
|
|
|
|
|
|
|
void IteratableChunkChecksumValidatorTest::testValidate() {
|
2007-10-11 16:58:24 +00:00
|
|
|
Option option;
|
|
|
|
SingleFileDownloadContextHandle dctx =
|
|
|
|
new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt");
|
|
|
|
dctx->setPieceHashes(Strings(&csArray[0], &csArray[3]));
|
|
|
|
dctx->setPieceHashAlgo("sha1");
|
|
|
|
DefaultPieceStorageHandle ps = new DefaultPieceStorage(dctx, &option);
|
|
|
|
ps->initStorage();
|
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChunkChecksumValidator validator(dctx, ps);
|
|
|
|
|
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
|
|
|
validator.updatePieceStorage();
|
|
|
|
CPPUNIT_ASSERT(ps->downloadFinished());
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
// make the test fail
|
|
|
|
Strings badHashes(&csArray[0], &csArray[3]);
|
|
|
|
badHashes[1] = "ffffffffffffffffffffffffffffffffffffffff";
|
|
|
|
dctx->setPieceHashes(badHashes);
|
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
|
|
|
validator.updatePieceStorage();
|
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(0));
|
|
|
|
CPPUNIT_ASSERT(!ps->hasPiece(1));
|
|
|
|
CPPUNIT_ASSERT(ps->hasPiece(2));
|
2007-05-31 15:56:20 +00:00
|
|
|
}
|