2007-05-31 15:56:20 +00:00
|
|
|
#include "IteratableChunkChecksumValidator.h"
|
2009-02-01 02:50:38 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2011-11-05 09:05:23 +00:00
|
|
|
#include "TestUtil.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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class IteratableChunkChecksumValidatorTest : public CppUnit::TestFixture {
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
private:
|
2011-10-13 12:40:07 +00:00
|
|
|
static const std::string csArray[];
|
2015-12-27 09:39:47 +00:00
|
|
|
|
2007-05-31 15:56:20 +00:00
|
|
|
public:
|
2015-12-27 09:39:47 +00:00
|
|
|
void setUp() {}
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
void testValidate();
|
2009-05-03 06:26:44 +00:00
|
|
|
void testValidate_readError();
|
2007-05-31 15:56:20 +00:00
|
|
|
};
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(IteratableChunkChecksumValidatorTest);
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
const std::string IteratableChunkChecksumValidatorTest::csArray[] = {
|
|
|
|
fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
|
2011-11-05 09:05:23 +00:00
|
|
|
fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
|
2015-12-27 09:39:47 +00:00
|
|
|
fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb")};
|
2007-05-31 15:56:20 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void IteratableChunkChecksumValidatorTest::testValidate()
|
|
|
|
{
|
2007-10-11 16:58:24 +00:00
|
|
|
Option option;
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<DownloadContext> dctx(new DownloadContext(
|
|
|
|
100, 250, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
|
2011-07-27 13:44:30 +00:00
|
|
|
dctx->setPieceHashes("sha-1", &csArray[0], &csArray[3]);
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<DefaultPieceStorage> ps(
|
|
|
|
new DefaultPieceStorage(dctx, &option));
|
2007-10-11 16:58:24 +00:00
|
|
|
ps->initStorage();
|
2010-12-02 13:38:36 +00:00
|
|
|
ps->getDiskAdaptor()->enableReadOnly();
|
2007-10-11 16:58:24 +00:00
|
|
|
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]);
|
2011-11-05 09:05:23 +00:00
|
|
|
badHashes[1] = fromHex("ffffffffffffffffffffffffffffffffffffffff");
|
2011-07-27 13:44:30 +00:00
|
|
|
dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());
|
2007-05-31 15:56:20 +00:00
|
|
|
|
|
|
|
validator.init();
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
while (!validator.finished()) {
|
2007-05-31 15:56:20 +00:00
|
|
|
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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void IteratableChunkChecksumValidatorTest::testValidate_readError()
|
|
|
|
{
|
2009-05-03 06:26:44 +00:00
|
|
|
Option option;
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<DownloadContext> dctx(new DownloadContext(
|
|
|
|
100, 500, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
|
2009-05-03 06:26:44 +00:00
|
|
|
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
|
2011-11-05 09:05:23 +00:00
|
|
|
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
|
|
|
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
2011-07-27 13:44:30 +00:00
|
|
|
dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<DefaultPieceStorage> ps(
|
|
|
|
new DefaultPieceStorage(dctx, &option));
|
2009-05-03 06:26:44 +00:00
|
|
|
ps->initStorage();
|
2010-12-02 13:38:36 +00:00
|
|
|
ps->getDiskAdaptor()->enableReadOnly();
|
2009-05-03 06:26:44 +00:00
|
|
|
ps->getDiskAdaptor()->openFile();
|
|
|
|
|
|
|
|
IteratableChunkChecksumValidator validator(dctx, ps);
|
|
|
|
validator.init();
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
while (!validator.finished()) {
|
2009-05-03 06:26:44 +00:00
|
|
|
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
|