2007-05-31 15:56:20 +00:00
|
|
|
#include "IteratableChecksumValidator.h"
|
2007-11-12 11:28:16 +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 IteratableChecksumValidatorTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(IteratableChecksumValidatorTest);
|
|
|
|
CPPUNIT_TEST(testValidate);
|
2007-11-12 11:28:16 +00:00
|
|
|
CPPUNIT_TEST(testValidate_fail);
|
2007-05-31 15:56:20 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testValidate();
|
2007-11-12 11:28:16 +00:00
|
|
|
void testValidate_fail();
|
2007-05-31 15:56:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChecksumValidatorTest );
|
|
|
|
|
|
|
|
void IteratableChecksumValidatorTest::testValidate() {
|
2007-11-12 11:28:16 +00:00
|
|
|
Option option;
|
|
|
|
SingleFileDownloadContextHandle dctx =
|
|
|
|
new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt");
|
|
|
|
dctx->setChecksum("898a81b8e0181280ae2ee1b81e269196d91e869a");
|
|
|
|
dctx->setChecksumHashAlgo("sha1");
|
|
|
|
DefaultPieceStorageHandle ps = new DefaultPieceStorage(dctx, &option);
|
|
|
|
ps->initStorage();
|
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChecksumValidator validator(dctx, ps);
|
2007-05-31 15:56:20 +00:00
|
|
|
validator.init();
|
|
|
|
while(!validator.finished()) {
|
|
|
|
validator.validateChunk();
|
|
|
|
}
|
|
|
|
|
2007-11-12 11:28:16 +00:00
|
|
|
CPPUNIT_ASSERT(ps->downloadFinished());
|
|
|
|
}
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2007-11-12 11:28:16 +00:00
|
|
|
void IteratableChecksumValidatorTest::testValidate_fail() {
|
|
|
|
Option option;
|
|
|
|
SingleFileDownloadContextHandle dctx =
|
|
|
|
new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt");
|
|
|
|
dctx->setChecksum(string(40, '0')); // set wrong checksum
|
|
|
|
dctx->setChecksumHashAlgo("sha1");
|
|
|
|
DefaultPieceStorageHandle ps = new DefaultPieceStorage(dctx, &option);
|
|
|
|
ps->initStorage();
|
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChecksumValidator validator(dctx, ps);
|
2007-05-31 15:56:20 +00:00
|
|
|
validator.init();
|
2007-11-12 11:28:16 +00:00
|
|
|
|
2007-05-31 15:56:20 +00:00
|
|
|
while(!validator.finished()) {
|
|
|
|
validator.validateChunk();
|
|
|
|
}
|
|
|
|
|
2007-11-12 11:28:16 +00:00
|
|
|
CPPUNIT_ASSERT(!ps->downloadFinished());
|
2007-05-31 15:56:20 +00:00
|
|
|
}
|