Consistent naming scheme for hash type and digest for DownloadContext.

This commit is contained in:
Tatsuhiro Tsujikawa 2011-07-27 22:44:30 +09:00
parent 7b86b294c9
commit 03f0774482
15 changed files with 51 additions and 65 deletions

View File

@ -390,7 +390,7 @@ void DefaultBtProgressInfoFile::load()
#ifdef ENABLE_MESSAGE_DIGEST
piece->setHashAlgo(dctx_->getPieceHashAlgo());
piece->setHashAlgo(dctx_->getPieceHashType());
#endif // ENABLE_MESSAGE_DIGEST

View File

@ -106,7 +106,7 @@ SharedHandle<Piece> DefaultPieceStorage::checkOutPiece
piece.reset(new Piece(index, bitfieldMan_->getBlockLength(index)));
#ifdef ENABLE_MESSAGE_DIGEST
piece->setHashAlgo(downloadContext_->getPieceHashAlgo());
piece->setHashAlgo(downloadContext_->getPieceHashType());
#endif // ENABLE_MESSAGE_DIGEST
@ -735,7 +735,7 @@ void DefaultPieceStorage::markPiecesDone(uint64_t length)
#ifdef ENABLE_MESSAGE_DIGEST
p->setHashAlgo(downloadContext_->getPieceHashAlgo());
p->setHashAlgo(downloadContext_->getPieceHashType());
#endif // ENABLE_MESSAGE_DIGEST

View File

@ -92,7 +92,7 @@ DownloadCommand::DownloadCommand
#ifdef ENABLE_MESSAGE_DIGEST
{
if(getOption()->getAsBool(PREF_REALTIME_CHUNK_CHECKSUM)) {
const std::string& algo = getDownloadContext()->getPieceHashAlgo();
const std::string& algo = getDownloadContext()->getPieceHashType();
if(MessageDigest::supports(algo)) {
messageDigest_ = MessageDigest::create(algo);
pieceHashValidationEnabled_ = true;
@ -297,7 +297,7 @@ bool DownloadCommand::prepareForNextSegment() {
}
}
#ifdef ENABLE_MESSAGE_DIGEST
if(getDownloadContext()->getPieceHashAlgo().empty()) {
if(getDownloadContext()->getPieceHashType().empty()) {
SharedHandle<CheckIntegrityEntry> entry
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
if(entry->isValidationReady()) {

View File

@ -239,18 +239,18 @@ size_t DownloadContext::countRequestedFileEntry() const
bool DownloadContext::isChecksumVerificationNeeded() const
{
return pieceHashAlgo_.empty() &&
!checksum_.empty() && !checksumHashAlgo_.empty() && !checksumVerified_;
return pieceHashType_.empty() &&
!digest_.empty() && !hashType_.empty() && !checksumVerified_;
}
bool DownloadContext::isChecksumVerificationAvailable() const
{
return !checksum_.empty() && !checksumHashAlgo_.empty();
return !digest_.empty() && !hashType_.empty();
}
bool DownloadContext::isPieceHashVerificationAvailable() const
{
return !pieceHashAlgo_.empty() &&
return !pieceHashType_.empty() &&
pieceHashes_.size() > 0 && pieceHashes_.size() == getNumPieces();
}
@ -263,19 +263,12 @@ const std::string& DownloadContext::getPieceHash(size_t index) const
}
}
void DownloadContext::setPieceHashAlgo(const std::string& algo)
void DownloadContext::setDigest
(const std::string& hashType,
const std::string& digest)
{
pieceHashAlgo_ = algo;
}
void DownloadContext::setChecksum(const std::string& checksum)
{
checksum_ = checksum;
}
void DownloadContext::setChecksumHashAlgo(const std::string& algo)
{
checksumHashAlgo_ = algo;
hashType_ = hashType;
digest_ = digest;
}
void DownloadContext::setBasePath(const std::string& basePath)

View File

@ -63,11 +63,11 @@ private:
size_t pieceLength_;
std::string pieceHashAlgo_;
std::string pieceHashType_;
std::string checksum_;
std::string digest_;
std::string checksumHashAlgo_;
std::string hashType_;
bool checksumVerified_;
@ -106,8 +106,11 @@ public:
}
template<typename InputIterator>
void setPieceHashes(InputIterator first, InputIterator last)
void setPieceHashes
(const std::string& hashType,
InputIterator first, InputIterator last)
{
pieceHashType_ = hashType;
pieceHashes_.assign(first, last);
}
@ -149,17 +152,13 @@ public:
size_t getNumPieces() const;
const std::string& getPieceHashAlgo() const { return pieceHashAlgo_; }
const std::string& getPieceHashType() const { return pieceHashType_; }
void setPieceHashAlgo(const std::string& algo);
const std::string& getDigest() const { return digest_; }
const std::string& getChecksum() const { return checksum_; }
const std::string& getHashType() const { return hashType_; }
void setChecksum(const std::string& checksum);
const std::string& getChecksumHashAlgo() const { return checksumHashAlgo_; }
void setChecksumHashAlgo(const std::string& algo);
void setDigest(const std::string& hashType, const std::string& digest);
// The representative path name for this context. It is used as a
// part of .aria2 control file. If basePath_ is set, returns

View File

@ -213,12 +213,11 @@ bool HttpResponseCommand::executeInternal()
httpResponse->getDigest(checksums);
for(std::vector<Checksum>::iterator i = checksums.begin(),
eoi = checksums.end(); i != eoi; ++i) {
if(getDownloadContext()->getChecksumHashAlgo().empty()) {
if(getDownloadContext()->getHashType().empty()) {
A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s",
(*i).getHashType().c_str(),
(*i).getDigest().c_str()));
getDownloadContext()->setChecksumHashAlgo((*i).getHashType());
getDownloadContext()->setChecksum((*i).getDigest());
getDownloadContext()->setDigest((*i).getHashType(), (*i).getDigest());
break;
} else {
if(checkChecksum(getDownloadContext(), *i)) {
@ -282,7 +281,7 @@ bool HttpResponseCommand::executeInternal()
}
} else {
#ifdef ENABLE_MESSAGE_DIGEST
if(!getDownloadContext()->getChecksumHashAlgo().empty() &&
if(!getDownloadContext()->getHashType().empty() &&
httpHeader->defined(HttpHeader::DIGEST)) {
std::vector<Checksum> checksums;
httpResponse->getDigest(checksums);
@ -559,8 +558,8 @@ bool HttpResponseCommand::checkChecksum
(const SharedHandle<DownloadContext>& dctx,
const Checksum& checksum)
{
if(dctx->getChecksumHashAlgo() == checksum.getHashType()) {
if(dctx->getChecksum() == checksum.getDigest()) {
if(dctx->getHashType() == checksum.getHashType()) {
if(dctx->getDigest() == checksum.getDigest()) {
A2_LOG_INFO("Valid hash found in Digest header field.");
return true;
} else {

View File

@ -73,8 +73,8 @@ void IteratableChecksumValidator::validateChunk()
ctx_->update(buffer_, length);
currentOffset_ += length;
if(finished()) {
std::string actualChecksum = ctx_->hexDigest();
if(dctx_->getChecksum() == actualChecksum) {
std::string actualDigest = ctx_->hexDigest();
if(dctx_->getDigest() == actualDigest) {
pieceStorage_->markAllPiecesDone();
} else {
BitfieldMan bitfield(dctx_->getPieceLength(), dctx_->getTotalLength());
@ -103,7 +103,7 @@ void IteratableChecksumValidator::init()
delete [] buffer_;
buffer_ = new unsigned char[BUFSIZE];
currentOffset_ = 0;
ctx_ = MessageDigest::create(dctx_->getChecksumHashAlgo());
ctx_ = MessageDigest::create(dctx_->getHashType());
}
} // namespace aria2

View File

@ -122,7 +122,7 @@ void IteratableChunkChecksumValidator::init()
{
delete [] buffer_;
buffer_ = new unsigned char[BUFSIZE];
ctx_ = MessageDigest::create(dctx_->getPieceHashAlgo());
ctx_ = MessageDigest::create(dctx_->getPieceHashType());
bitfield_->clearAllBit();
currentIndex_ = 0;
}

View File

@ -263,13 +263,13 @@ Metalink2RequestGroup::createRequestGroup
}
#ifdef ENABLE_MESSAGE_DIGEST
if(entry->checksum) {
dctx->setChecksum(entry->checksum->getDigest());
dctx->setChecksumHashAlgo(entry->checksum->getHashType());
dctx->setDigest(entry->checksum->getHashType(),
entry->checksum->getDigest());
}
if(entry->chunkChecksum) {
dctx->setPieceHashes(entry->chunkChecksum->getPieceHashes().begin(),
dctx->setPieceHashes(entry->chunkChecksum->getHashType(),
entry->chunkChecksum->getPieceHashes().begin(),
entry->chunkChecksum->getPieceHashes().end());
dctx->setPieceHashAlgo(entry->chunkChecksum->getHashType());
}
#endif // ENABLE_MESSAGE_DIGEST
dctx->setSignature(entry->getSignature());

View File

@ -122,8 +122,7 @@ void extractPieceHash(const SharedHandle<DownloadContext>& ctx,
pieceHashes.push_back(util::toHex(hashData.data()+i*hashLength,
hashLength));
}
ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end());
ctx->setPieceHashAlgo("sha-1");
ctx->setPieceHashes("sha-1", pieceHashes.begin(), pieceHashes.end());
}
} // namespace

View File

@ -157,7 +157,7 @@ void BittorrentHelperTest::testGetPieceHash() {
CPPUNIT_ASSERT_EQUAL(std::string(""),
dctx->getPieceHash(3));
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashAlgo());
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
}
void BittorrentHelperTest::testGetFileEntries() {

View File

@ -54,7 +54,7 @@ void DownloadContextTest::testGetPieceHash()
{
DownloadContext ctx;
const std::string pieceHashes[] = { "hash1","hash2","shash3" };
ctx.setPieceHashes(&pieceHashes[0], &pieceHashes[3]);
ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]);
CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0));
CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3));
}

View File

@ -34,8 +34,7 @@ void IteratableChecksumValidatorTest::testValidate() {
Option option;
SharedHandle<DownloadContext> dctx
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
dctx->setChecksum("898a81b8e0181280ae2ee1b81e269196d91e869a");
dctx->setChecksumHashAlgo("sha-1");
dctx->setDigest("sha-1", "898a81b8e0181280ae2ee1b81e269196d91e869a");
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
ps->initStorage();
ps->getDiskAdaptor()->enableReadOnly();
@ -54,8 +53,7 @@ void IteratableChecksumValidatorTest::testValidate_fail() {
Option option;
SharedHandle<DownloadContext> dctx
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
dctx->setChecksum(std::string(40, '0')); // set wrong checksum
dctx->setChecksumHashAlgo("sha-1");
dctx->setDigest("sha-1", std::string(40, '0')); // set wrong checksum
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
ps->initStorage();
ps->getDiskAdaptor()->enableReadOnly();

View File

@ -39,8 +39,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() {
Option option;
SharedHandle<DownloadContext> dctx
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
dctx->setPieceHashes(&csArray[0], &csArray[3]);
dctx->setPieceHashAlgo("sha-1");
dctx->setPieceHashes("sha-1", &csArray[0], &csArray[3]);
SharedHandle<DefaultPieceStorage> ps
(new DefaultPieceStorage(dctx, &option));
ps->initStorage();
@ -61,7 +60,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() {
// make the test fail
std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
badHashes[1] = "ffffffffffffffffffffffffffffffffffffffff";
dctx->setPieceHashes(badHashes.begin(), badHashes.end());
dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());
validator.init();
@ -80,8 +79,7 @@ void IteratableChunkChecksumValidatorTest::testValidate_readError() {
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
hashes.push_back("ffffffffffffffffffffffffffffffffffffffff");
hashes.push_back("ffffffffffffffffffffffffffffffffffffffff");
dctx->setPieceHashes(hashes.begin(), hashes.end());
dctx->setPieceHashAlgo("sha-1");
dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
ps->initStorage();
ps->getDiskAdaptor()->enableReadOnly();

View File

@ -60,10 +60,10 @@ void Metalink2RequestGroupTest::testGenerate()
CPPUNIT_ASSERT(dctx);
CPPUNIT_ASSERT_EQUAL((uint64_t)0ULL, dctx->getTotalLength());
#ifdef ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getChecksumHashAlgo());
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
CPPUNIT_ASSERT_EQUAL
(std::string("a96cf3f0266b91d87d5124cf94326422800b627d"),
dctx->getChecksum());
dctx->getDigest());
#endif // ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT(dctx->getSignature());
CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType());
@ -79,13 +79,13 @@ void Metalink2RequestGroupTest::testGenerate()
CPPUNIT_ASSERT(dctx);
#ifdef ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashAlgo());
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size());
CPPUNIT_ASSERT_EQUAL((size_t)262144, dctx->getPieceLength());
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getChecksumHashAlgo());
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
CPPUNIT_ASSERT_EQUAL
(std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"),
dctx->getChecksum());
dctx->getDigest());
#endif // ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT(!dctx->getSignature());
}