aria2/test/UTMetadataDataExtensionMess...

114 lines
3.3 KiB
C++
Raw Normal View History

#include "UTMetadataDataExtensionMessage.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "BtConstants.h"
#include "PieceStorage.h"
#include "DownloadContext.h"
#include "DirectDiskAdaptor.h"
#include "ByteArrayDiskWriter.h"
#include "DownloadContext.h"
#include "MockPieceStorage.h"
#include "UTMetadataRequestTracker.h"
#include "bittorrent_helper.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 "MessageDigestHelper.h"
namespace aria2 {
class UTMetadataDataExtensionMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UTMetadataDataExtensionMessageTest);
CPPUNIT_TEST(testGetExtensionMessageID);
CPPUNIT_TEST(testGetBencodedData);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST_SUITE_END();
public:
void testGetExtensionMessageID();
void testGetBencodedData();
void testToString();
void testDoReceivedAction();
};
CPPUNIT_TEST_SUITE_REGISTRATION(UTMetadataDataExtensionMessageTest);
void UTMetadataDataExtensionMessageTest::testGetExtensionMessageID()
{
UTMetadataDataExtensionMessage msg(1);
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg.getExtensionMessageID());
}
void UTMetadataDataExtensionMessageTest::testGetBencodedData()
{
std::string data(METADATA_PIECE_SIZE, '0');
UTMetadataDataExtensionMessage msg(1);
msg.setIndex(1);
msg.setTotalSize(data.size());
msg.setData(data);
CPPUNIT_ASSERT_EQUAL
(std::string("d8:msg_typei1e5:piecei1e10:total_sizei16384ee")+data,
msg.getPayload());
}
void UTMetadataDataExtensionMessageTest::testToString()
{
UTMetadataDataExtensionMessage msg(1);
msg.setIndex(100);
CPPUNIT_ASSERT_EQUAL(std::string("ut_metadata data piece=100"),
msg.toString());
}
void UTMetadataDataExtensionMessageTest::testDoReceivedAction()
{
SharedHandle<DirectDiskAdaptor> diskAdaptor(new DirectDiskAdaptor());
SharedHandle<ByteArrayDiskWriter> diskWriter(new ByteArrayDiskWriter());
diskAdaptor->setDiskWriter(diskWriter);
SharedHandle<MockPieceStorage> pieceStorage(new MockPieceStorage());
pieceStorage->setDiskAdaptor(diskAdaptor);
SharedHandle<UTMetadataRequestTracker> tracker
(new UTMetadataRequestTracker());
SharedHandle<DownloadContext> dctx(new DownloadContext());
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
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
std::string piece0 = std::string(METADATA_PIECE_SIZE, '0');
std::string piece1 = std::string(METADATA_PIECE_SIZE, '1');
std::string metadata = piece0+piece1;
unsigned char infoHash[INFO_HASH_LENGTH];
MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
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
MessageDigest::sha1(),
metadata.data(), 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
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
dctx->setAttribute(bittorrent::BITTORRENT, attrs);
UTMetadataDataExtensionMessage m(1);
m.setPieceStorage(pieceStorage);
m.setUTMetadataRequestTracker(tracker.get());
m.setDownloadContext(dctx);
m.setIndex(1);
m.setData(piece1);
tracker->add(1);
m.doReceivedAction();
CPPUNIT_ASSERT(!tracker->tracks(1));
pieceStorage->setDownloadFinished(true);
// If piece is not tracked, it is ignored.
m.setIndex(0);
m.setData(piece0);
m.doReceivedAction();
tracker->add(0);
m.doReceivedAction();
CPPUNIT_ASSERT_EQUAL(metadata, diskWriter->getString());
}
} // namespace aria2