aria2/test/UTMetadataPostDownloadHandl...

142 lines
4.8 KiB
C++
Raw Normal View History

#include "UTMetadataPostDownloadHandler.h"
#include <cppunit/extensions/HelperMacros.h>
#include "DownloadContext.h"
#include "RequestGroup.h"
#include "Option.h"
#include "FileEntry.h"
#include "bittorrent_helper.h"
#include "A2STR.h"
#include "ByteArrayDiskWriterFactory.h"
#include "PieceStorage.h"
#include "DiskAdaptor.h"
#include "util.h"
2010-11-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Replaced MessageDigestContext with MessageDigest. Cleaned up unnecessary functions in MessageDigestHelper. * src/BtPieceMessage.cc * src/Checksum.h * src/DHTTokenTracker.cc * src/DownloadCommand.cc * src/DownloadCommand.h * src/HashFuncEntry.h * src/IteratableChecksumValidator.cc * src/IteratableChecksumValidator.h * src/IteratableChunkChecksumValidator.cc * src/IteratableChunkChecksumValidator.h * src/LibgcryptMessageDigestImpl.cc * src/LibgcryptMessageDigestImpl.h * src/LibsslMessageDigestImpl.cc * src/LibsslMessageDigestImpl.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/MessageDigest.cc * src/MessageDigest.h * src/MessageDigestHelper.cc * src/MessageDigestHelper.h * src/MessageDigestImpl.h * src/MetalinkParserController.cc * src/Piece.cc * src/Piece.h * src/UTMetadataDataExtensionMessage.cc * src/bittorrent_helper.cc * src/messageDigest.cc: Removed * src/messageDigest.h: Removed * src/util.cc * src/version_usage.cc * test/BittorrentHelperTest.cc * test/GZipDecoderTest.cc * test/GZipDecodingStreamFilterTest.cc * test/IteratableChecksumValidatorTest.cc * test/IteratableChunkChecksumValidatorTest.cc * test/Makefile.am * test/MessageDigestHelperTest.cc * test/MessageDigestTest.cc * test/Metalink2RequestGroupTest.cc * test/MetalinkProcessorTest.cc * test/PieceTest.cc * test/TestUtil.cc * test/TestUtil.h * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc
2010-11-11 02:56:24 +00:00
#include "MessageDigest.h"
#include "message_digest_helper.h"
#include "prefs.h"
2010-11-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Replaced MessageDigestContext with MessageDigest. Cleaned up unnecessary functions in MessageDigestHelper. * src/BtPieceMessage.cc * src/Checksum.h * src/DHTTokenTracker.cc * src/DownloadCommand.cc * src/DownloadCommand.h * src/HashFuncEntry.h * src/IteratableChecksumValidator.cc * src/IteratableChecksumValidator.h * src/IteratableChunkChecksumValidator.cc * src/IteratableChunkChecksumValidator.h * src/LibgcryptMessageDigestImpl.cc * src/LibgcryptMessageDigestImpl.h * src/LibsslMessageDigestImpl.cc * src/LibsslMessageDigestImpl.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/MessageDigest.cc * src/MessageDigest.h * src/MessageDigestHelper.cc * src/MessageDigestHelper.h * src/MessageDigestImpl.h * src/MetalinkParserController.cc * src/Piece.cc * src/Piece.h * src/UTMetadataDataExtensionMessage.cc * src/bittorrent_helper.cc * src/messageDigest.cc: Removed * src/messageDigest.h: Removed * src/util.cc * src/version_usage.cc * test/BittorrentHelperTest.cc * test/GZipDecoderTest.cc * test/GZipDecodingStreamFilterTest.cc * test/IteratableChecksumValidatorTest.cc * test/IteratableChunkChecksumValidatorTest.cc * test/Makefile.am * test/MessageDigestHelperTest.cc * test/MessageDigestTest.cc * test/Metalink2RequestGroupTest.cc * test/MetalinkProcessorTest.cc * test/PieceTest.cc * test/TestUtil.cc * test/TestUtil.h * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc
2010-11-11 02:56:24 +00:00
#include "RecoverableException.h"
namespace aria2 {
class UTMetadataPostDownloadHandlerTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UTMetadataPostDownloadHandlerTest);
CPPUNIT_TEST(testCanHandle);
CPPUNIT_TEST(testGetNextRequestGroups);
CPPUNIT_TEST_SUITE_END();
private:
std::shared_ptr<Option> option_;
std::shared_ptr<DownloadContext> dctx_;
std::shared_ptr<RequestGroup> requestGroup_;
public:
void setUp()
{
option_.reset(new Option());
option_->put(PREF_DIR, A2_TEST_OUT_DIR);
dctx_.reset(new DownloadContext(0, 0, A2_TEST_OUT_DIR"/something"));
requestGroup_.reset(new RequestGroup(GroupId::create(), option_));
requestGroup_->setDownloadContext(dctx_);
}
void testCanHandle();
void testGetNextRequestGroups();
};
CPPUNIT_TEST_SUITE_REGISTRATION( UTMetadataPostDownloadHandlerTest );
void UTMetadataPostDownloadHandlerTest::testCanHandle()
{
UTMetadataPostDownloadHandler handler;
CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));
dctx_->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
CPPUNIT_ASSERT(handler.canHandle(requestGroup_.get()));
auto attrs = bittorrent::getTorrentAttrs(dctx_);
2010-06-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Introduced ValueBase class, which is a replacement of BDE. In this change ValueBase is used instead of BDE except DHT messages, UTMetadata messages and XML-RPC. They'll be replaced in the later commits. DownloadContext::_attrs is now ContextAttribute rather than BDE. * src/ActivePeerConnectionCommand.cc * src/AnnounceList.cc * src/AnnounceList.h * src/BtDependency.cc * src/BtRegistry.cc * src/BtSetup.cc * src/ConsoleStatCalc.cc * src/ContextAttribute.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DownloadContext.cc * src/DownloadContext.h * src/HandshakeExtensionMessage.cc * src/InitiateConnectionCommand.cc * src/LpdReceiveMessageCommand.cc * src/MSEHandshake.cc * src/Makefile.am * src/Makefile.in * src/PeerInteractionCommand.cc * src/PeerListProcessor.h * src/ProtocolDetector.cc * src/RequestGroup.cc * src/RequestGroupMan.cc * src/TorrentAttribute.h * src/TrackerWatcherCommand.cc * src/UTMetadataDataExtensionMessage.cc * src/UTMetadataPostDownloadHandler.cc * src/UTMetadataRequestExtensionMessage.cc * src/ValueBase.cc * src/ValueBase.h * src/XmlRpcMethodImpl.cc * src/XmlRpcMethodImpl.h * src/bencode2.cc * src/bencode2.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/magnet.cc * src/magnet.h * test/AnnounceListTest.cc * test/Bencode2Test.cc * test/BencodeTest.cc * test/BittorrentHelperTest.cc * test/BtDependencyTest.cc * test/BtRegistryTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/HandshakeExtensionMessageTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/Makefile.am * test/Makefile.in * test/RequestGroupManTest.cc * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc * test/UTMetadataRequestExtensionMessageTest.cc * test/ValueBaseTest.cc * test/XmlRpcMethodTest.cc
2010-06-18 14:47:09 +00:00
// Only checks whether metadata is empty or not
attrs->metadata = "metadata";
CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));
}
void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
{
File trfile(A2_TEST_OUT_DIR"/cd41c7fdddfd034a15a04d7ff881216e01c4ceaf.torrent");
if(trfile.exists()) {
trfile.remove();
}
std::string metadata =
"d6:lengthi384e4:name19:aria2-0.8.2.tar.bz212:piece lengthi128e"
"6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe";
unsigned char infoHash[20];
message_digest::digest
2013-07-02 16:13:13 +00:00
(infoHash, sizeof(infoHash), MessageDigest::sha1().get(),
reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size());
dctx_->getFirstFileEntry()->setLength(metadata.size());
2010-06-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Introduced ValueBase class, which is a replacement of BDE. In this change ValueBase is used instead of BDE except DHT messages, UTMetadata messages and XML-RPC. They'll be replaced in the later commits. DownloadContext::_attrs is now ContextAttribute rather than BDE. * src/ActivePeerConnectionCommand.cc * src/AnnounceList.cc * src/AnnounceList.h * src/BtDependency.cc * src/BtRegistry.cc * src/BtSetup.cc * src/ConsoleStatCalc.cc * src/ContextAttribute.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DownloadContext.cc * src/DownloadContext.h * src/HandshakeExtensionMessage.cc * src/InitiateConnectionCommand.cc * src/LpdReceiveMessageCommand.cc * src/MSEHandshake.cc * src/Makefile.am * src/Makefile.in * src/PeerInteractionCommand.cc * src/PeerListProcessor.h * src/ProtocolDetector.cc * src/RequestGroup.cc * src/RequestGroupMan.cc * src/TorrentAttribute.h * src/TrackerWatcherCommand.cc * src/UTMetadataDataExtensionMessage.cc * src/UTMetadataPostDownloadHandler.cc * src/UTMetadataRequestExtensionMessage.cc * src/ValueBase.cc * src/ValueBase.h * src/XmlRpcMethodImpl.cc * src/XmlRpcMethodImpl.h * src/bencode2.cc * src/bencode2.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/magnet.cc * src/magnet.h * test/AnnounceListTest.cc * test/Bencode2Test.cc * test/BencodeTest.cc * test/BittorrentHelperTest.cc * test/BtDependencyTest.cc * test/BtRegistryTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/HandshakeExtensionMessageTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/Makefile.am * test/Makefile.in * test/RequestGroupManTest.cc * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc * test/UTMetadataRequestExtensionMessageTest.cc * test/ValueBaseTest.cc * test/XmlRpcMethodTest.cc
2010-06-18 14:47:09 +00:00
std::vector<std::vector<std::string> > announceList;
std::vector<std::string> announceTier;
announceTier.push_back("http://tracker");
announceList.push_back(announceTier);
{
auto attrs = make_unique<TorrentAttribute>();
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
attrs->announceList = announceList;
dctx_->setAttribute(CTX_ATTR_BT, std::move(attrs));
}
requestGroup_->setDiskWriterFactory
(std::shared_ptr<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
requestGroup_->initPieceStorage();
requestGroup_->getPieceStorage()->getDiskAdaptor()->writeData
(reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size(),
0);
UTMetadataPostDownloadHandler handler;
std::vector<std::shared_ptr<RequestGroup> > results;
handler.getNextRequestGroups(results, requestGroup_.get());
CPPUNIT_ASSERT_EQUAL((size_t)1, results.size());
std::shared_ptr<RequestGroup> newRg = results.front();
std::shared_ptr<DownloadContext> newDctx = newRg->getDownloadContext();
auto newAttrs = bittorrent::getTorrentAttrs(newDctx);
CPPUNIT_ASSERT_EQUAL(bittorrent::getInfoHashString(dctx_),
2010-06-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Introduced ValueBase class, which is a replacement of BDE. In this change ValueBase is used instead of BDE except DHT messages, UTMetadata messages and XML-RPC. They'll be replaced in the later commits. DownloadContext::_attrs is now ContextAttribute rather than BDE. * src/ActivePeerConnectionCommand.cc * src/AnnounceList.cc * src/AnnounceList.h * src/BtDependency.cc * src/BtRegistry.cc * src/BtSetup.cc * src/ConsoleStatCalc.cc * src/ContextAttribute.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DownloadContext.cc * src/DownloadContext.h * src/HandshakeExtensionMessage.cc * src/InitiateConnectionCommand.cc * src/LpdReceiveMessageCommand.cc * src/MSEHandshake.cc * src/Makefile.am * src/Makefile.in * src/PeerInteractionCommand.cc * src/PeerListProcessor.h * src/ProtocolDetector.cc * src/RequestGroup.cc * src/RequestGroupMan.cc * src/TorrentAttribute.h * src/TrackerWatcherCommand.cc * src/UTMetadataDataExtensionMessage.cc * src/UTMetadataPostDownloadHandler.cc * src/UTMetadataRequestExtensionMessage.cc * src/ValueBase.cc * src/ValueBase.h * src/XmlRpcMethodImpl.cc * src/XmlRpcMethodImpl.h * src/bencode2.cc * src/bencode2.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/magnet.cc * src/magnet.h * test/AnnounceListTest.cc * test/Bencode2Test.cc * test/BencodeTest.cc * test/BittorrentHelperTest.cc * test/BtDependencyTest.cc * test/BtRegistryTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/HandshakeExtensionMessageTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/Makefile.am * test/Makefile.in * test/RequestGroupManTest.cc * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc * test/UTMetadataRequestExtensionMessageTest.cc * test/ValueBaseTest.cc * test/XmlRpcMethodTest.cc
2010-06-18 14:47:09 +00:00
bittorrent::getInfoHashString(newDctx));
const std::vector<std::vector<std::string> >& newAnnounceList =
newAttrs->announceList;
CPPUNIT_ASSERT_EQUAL((size_t)1, newAnnounceList.size());
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker"), newAnnounceList[0][0]);
CPPUNIT_ASSERT_EQUAL(option_->get(PREF_DIR),
newRg->getOption()->get(PREF_DIR));
CPPUNIT_ASSERT
(std::find(requestGroup_->followedBy().begin(),
requestGroup_->followedBy().end(),
newRg->getGID()) != requestGroup_->followedBy().end());
CPPUNIT_ASSERT(!trfile.exists());
results.clear();
requestGroup_->getOption()->put(PREF_BT_SAVE_METADATA, A2_V_TRUE);
handler.getNextRequestGroups(results, requestGroup_.get());
CPPUNIT_ASSERT(trfile.exists());
results.clear();
// See failure with bad metadata
metadata = "d6:lengthi384e4:name19:aria2-0.8.2.tar.bz212:piece lengthi128e";
requestGroup_->initPieceStorage();
requestGroup_->getPieceStorage()->getDiskAdaptor()->writeData
(reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size(),
0);
try {
handler.getNextRequestGroups(results, requestGroup_.get());
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// success
}
}
} // namespace aria2