aria2/test/MetalinkEntryTest.cc

138 lines
4.7 KiB
C++
Raw Normal View History

2006-07-03 14:19:59 +00:00
#include "MetalinkEntry.h"
2006-07-03 14:19:59 +00:00
#include <cppunit/extensions/HelperMacros.h>
#include "MetalinkResource.h"
#include "a2functional.h"
namespace aria2 {
2006-07-03 14:19:59 +00:00
class MetalinkEntryTest : public CppUnit::TestFixture {
2006-07-03 14:19:59 +00:00
CPPUNIT_TEST_SUITE(MetalinkEntryTest);
CPPUNIT_TEST(testDropUnsupportedResource);
CPPUNIT_TEST(testReorderResourcesByPriority);
CPPUNIT_TEST(testSetLocationPriority);
CPPUNIT_TEST(testSetProtocolPriority);
2006-07-03 14:19:59 +00:00
CPPUNIT_TEST_SUITE_END();
private:
2006-07-03 14:19:59 +00:00
public:
void setUp() {}
void tearDown() {}
2006-07-03 14:19:59 +00:00
void testDropUnsupportedResource();
void testReorderResourcesByPriority();
void testSetLocationPriority();
void testSetProtocolPriority();
2006-07-03 14:19:59 +00:00
};
CPPUNIT_TEST_SUITE_REGISTRATION(MetalinkEntryTest);
2006-07-03 14:19:59 +00:00
std::unique_ptr<MetalinkEntry> createTestEntry()
{
auto entry = make_unique<MetalinkEntry>();
auto res1 = make_unique<MetalinkResource>();
2006-07-03 14:19:59 +00:00
res1->url = "ftp://myhost/aria2.tar.bz2";
res1->type = MetalinkResource::TYPE_FTP;
res1->location = "ro";
res1->priority = 50;
auto res2 = make_unique<MetalinkResource>();
2006-07-03 14:19:59 +00:00
res2->url = "http://myhost/aria2.tar.bz2";
res2->type = MetalinkResource::TYPE_HTTP;
res2->location = "at";
res2->priority = 1;
auto res3 = make_unique<MetalinkResource>();
2006-07-03 14:19:59 +00:00
res3->url = "http://myhost/aria2.torrent";
res3->type = MetalinkResource::TYPE_BITTORRENT;
res3->location = "al";
res3->priority = 40;
auto res4 = make_unique<MetalinkResource>();
2006-07-03 14:19:59 +00:00
res4->url = "http://myhost/aria2.ext";
res4->type = MetalinkResource::TYPE_NOT_SUPPORTED;
res4->location = "ad";
res4->priority = 90;
auto res5 = make_unique<MetalinkResource>();
2006-08-08 Tatsuhiro Tsujikawa <tujikawa@rednoah.com> * src/FtpNegotiateCommand.cc (recvGreeting): Call disableWriteCheckSocket just after socket->setBlockingMode(). This avoids unnecessary CPU-hog loop. 2006-08-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * src/PeerChokeCommand.h (PeerChokeCommand): Rearranged the arguments. * src/PeerChokeCommand.cc (PeerChokeCommand): Rearranged the arguments. * src/MetalinkEntry.h (Checksum.h): Included. (md5): Removed. (sha1): Removed. (checksum): New variable. (operator=): Updated. (check): Removed. * src/prefs.h (PREF_LOWEST_SPEED_LIMIT): New definition. (PREF_FOLLOW_TORRENT): New definition. (PREF_SELECT_FILE): New definition. (PREF_FOLLOW_METALINK): New definition. * src/MetalinkResource.h (TYPE_HTTPS): Added to enum TYPE. * src/TorrentMan.cc (~TorrentMan): Rewritten. * src/MultiDiskWriter.cc (MultiDiskAdaptor): Updated according to the changes in MessageDigestContext. (~MultiDiskAdaptor): Updated according to the changes in MessageDigestContext. (hashUpdate): Updated according to the changes in MessageDigestContext. (sha1Sum): Updated according to the changes in MessageDigestContext. * src/Util.h (fileChecksum): Updated according to the changes in MessageDigestContext. * src/Util.cc (sha1Sum): Updated according to the changes in MessageDigestContext. (fileChecksum): Updated according to the changes in MessageDigestContext. * src/messageDigest.h: Rewritten. * src/MetalinkEntry.cc (check): Removed. (Supported): Updated. (dropUnsupportedResource): Fixed a memory leak. * src/ShaVisitor.cc (ShaVisitor): Updated according to the changes in MessageDigestContext. (~ShaVisitor): Updated according to the changes in MessageDigestContext. (visit): Updated according to the changes in MessageDigestContext. * src/main.cc (main): Rewritten the portion of download loop. --lowest-speed-limit command-line option added. * src/DownloadEngine.h (SocketEntry): New class. (SocketEntries): New definition. (PairFind): Removed. (SockCmdMap): Removed. * src/DownloadEngine.cc (DownloadEngine): Rewritten. (~DownloadEngine): Rewritten. (run): Renamed activeCommandUuids as activeUuids. (SetDescriptor): Rewritten. (AccumulateActiveCommandUuid): Removed. (AccumulateActiveUuid): New function object. (waitData): Rewritten. (updateFdSet): Rewritten. (addSocket): Rewritten. (deleteSocket): Rewritten. (addSocketForReadCheck): Rewritten. (deleteSocketForReadCheck): Rewritten. (addSocketForWriteCheck): Rewritten. (deleteSocketForWriteCheck): Rewritten. * src/Xml2MetalinkProcessor.cc (getEntry): Rewritten the portion of MetalinkEntry creation. * src/DownloadCommand.cc (executeInternal): Added the check routine for lowest speed limit. * src/AbstractDiskWriter.cc (AbstractDiskWriter): Updated according to the changes in MessageDigestContext. (~AbstractDiskWriter): Updated according to the changes in MessageDigestContext. (sha1Sum): Updated according to the changes in MessageDigestContext. * src/RequestInfo.h: New class. * src/UrlRequestInfo.h: New class. * src/UrlRequestInfo.cc: New class. * src/TorrentRequestInfo.h: New class. * src/TorrentRequestInfo.cc: New class. * src/MetalinkRequestInfo.h: New class. * src/MetalinkRequestInfo.cc: New class. * src/Checksum.h: New class. * src/DownloadEngineFactory.h: New class. * src/DownloadEngineFactory.cc: New class.
2006-08-07 16:05:00 +00:00
res5->url = "https://myhost/aria2.tar.bz2";
res5->type = MetalinkResource::TYPE_HTTPS;
res5->location = "jp";
res5->priority = 10;
2006-07-03 14:19:59 +00:00
entry->resources.push_back(std::move(res1));
entry->resources.push_back(std::move(res2));
entry->resources.push_back(std::move(res3));
entry->resources.push_back(std::move(res4));
entry->resources.push_back(std::move(res5));
2006-07-03 14:19:59 +00:00
return entry;
}
void MetalinkEntryTest::testDropUnsupportedResource()
{
auto entry = createTestEntry();
2006-07-03 14:19:59 +00:00
entry->dropUnsupportedResource();
2013-09-17 14:17:08 +00:00
#if defined(ENABLE_SSL) && defined(ENABLE_BITTORRENT)
CPPUNIT_ASSERT_EQUAL((size_t)4, entry->resources.size());
2013-09-17 14:17:08 +00:00
#elif defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
CPPUNIT_ASSERT_EQUAL((size_t)3, entry->resources.size());
#else // defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
CPPUNIT_ASSERT_EQUAL((size_t)2, entry->resources.size());
#endif // defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
2012-10-01 14:52:22 +00:00
auto itr = std::begin(entry->resources);
CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, (*itr++)->type);
CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTP, (*itr++)->type);
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> MessageDigestHelper is introduced in order to simplify the use of message digest. Removed repeated code. The message digest algorithm is now specified by string, like "sha1", "md5". * src/messageDigest.{h, cc} * src/MessageDigestHelper.{h, cc}: New class. * src/DefaultPieceStorage.cc * src/DefaultBtContext.{h, cc} (computeFastSet): New function. (setInfoHash): Added for unit testing. (setNumPieces): Added for unit testing. * src/DefaultBtInteractive.cc * src/BtPieceMessage.cc * src/Peer.cc * src/Checksum.h * src/message.h * src/IteratableChecksumValidator.h * src/ChunkChecksumValidator.{h, cc}: Use IteratableChecksumValidator inside it. * src/SegmentMan.{h, cc} (checkIntegrity): Removed. * src/IteratableChunkChecksumValidator.{h, cc} * src/Util.h (sha1Sum): Removed. (simpleMessageDigest): Removed. (fileChecksum): Removed. (computeFastSet): Removed. * src/ShaVisitor.cc * src/ChunkChecksum.h * src/DownloadCommand.cc Removed messageDigest virtual functions. * src/MultiDiskAdaptor.{h, cc} * src/DiskAdaptor.h * src/ByteArrayDiskWriter.h * src/DiskWriter.h * src/DiskAdaptorWriter.h * src/AbstractSingleDiskAdaptor.{h, cc} * src/AbstractDiskWriter.{h, cc} Fixed comilation error when message digest is disabled. * src/MetalinkEntry.{h, cc} * src/MetalinkRequestInfo.cc Removed srandom and random. * src/SimpleRandomizer.h Added size() virtual function to DiskAdaptor * src/MultiDiskAdaptor.h Fixed the bug that causes that files are not opened correctly in multi-file torrent. * src/TorrentRequestInfo.cc * src/MultiDiskAdaptor.cc Added SHA256 support * src/messageDigest.cc * src/Xml2MetalinkProcessor.cc Show supported message digest algorithms * src/main.cc Updated contact info. * src/main.cc
2007-08-08 14:40:11 +00:00
#ifdef ENABLE_BITTORRENT
CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_BITTORRENT, (*itr++)->type);
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> MessageDigestHelper is introduced in order to simplify the use of message digest. Removed repeated code. The message digest algorithm is now specified by string, like "sha1", "md5". * src/messageDigest.{h, cc} * src/MessageDigestHelper.{h, cc}: New class. * src/DefaultPieceStorage.cc * src/DefaultBtContext.{h, cc} (computeFastSet): New function. (setInfoHash): Added for unit testing. (setNumPieces): Added for unit testing. * src/DefaultBtInteractive.cc * src/BtPieceMessage.cc * src/Peer.cc * src/Checksum.h * src/message.h * src/IteratableChecksumValidator.h * src/ChunkChecksumValidator.{h, cc}: Use IteratableChecksumValidator inside it. * src/SegmentMan.{h, cc} (checkIntegrity): Removed. * src/IteratableChunkChecksumValidator.{h, cc} * src/Util.h (sha1Sum): Removed. (simpleMessageDigest): Removed. (fileChecksum): Removed. (computeFastSet): Removed. * src/ShaVisitor.cc * src/ChunkChecksum.h * src/DownloadCommand.cc Removed messageDigest virtual functions. * src/MultiDiskAdaptor.{h, cc} * src/DiskAdaptor.h * src/ByteArrayDiskWriter.h * src/DiskWriter.h * src/DiskAdaptorWriter.h * src/AbstractSingleDiskAdaptor.{h, cc} * src/AbstractDiskWriter.{h, cc} Fixed comilation error when message digest is disabled. * src/MetalinkEntry.{h, cc} * src/MetalinkRequestInfo.cc Removed srandom and random. * src/SimpleRandomizer.h Added size() virtual function to DiskAdaptor * src/MultiDiskAdaptor.h Fixed the bug that causes that files are not opened correctly in multi-file torrent. * src/TorrentRequestInfo.cc * src/MultiDiskAdaptor.cc Added SHA256 support * src/messageDigest.cc * src/Xml2MetalinkProcessor.cc Show supported message digest algorithms * src/main.cc Updated contact info. * src/main.cc
2007-08-08 14:40:11 +00:00
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_SSL
CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTPS, (*itr++)->type);
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> MessageDigestHelper is introduced in order to simplify the use of message digest. Removed repeated code. The message digest algorithm is now specified by string, like "sha1", "md5". * src/messageDigest.{h, cc} * src/MessageDigestHelper.{h, cc}: New class. * src/DefaultPieceStorage.cc * src/DefaultBtContext.{h, cc} (computeFastSet): New function. (setInfoHash): Added for unit testing. (setNumPieces): Added for unit testing. * src/DefaultBtInteractive.cc * src/BtPieceMessage.cc * src/Peer.cc * src/Checksum.h * src/message.h * src/IteratableChecksumValidator.h * src/ChunkChecksumValidator.{h, cc}: Use IteratableChecksumValidator inside it. * src/SegmentMan.{h, cc} (checkIntegrity): Removed. * src/IteratableChunkChecksumValidator.{h, cc} * src/Util.h (sha1Sum): Removed. (simpleMessageDigest): Removed. (fileChecksum): Removed. (computeFastSet): Removed. * src/ShaVisitor.cc * src/ChunkChecksum.h * src/DownloadCommand.cc Removed messageDigest virtual functions. * src/MultiDiskAdaptor.{h, cc} * src/DiskAdaptor.h * src/ByteArrayDiskWriter.h * src/DiskWriter.h * src/DiskAdaptorWriter.h * src/AbstractSingleDiskAdaptor.{h, cc} * src/AbstractDiskWriter.{h, cc} Fixed comilation error when message digest is disabled. * src/MetalinkEntry.{h, cc} * src/MetalinkRequestInfo.cc Removed srandom and random. * src/SimpleRandomizer.h Added size() virtual function to DiskAdaptor * src/MultiDiskAdaptor.h Fixed the bug that causes that files are not opened correctly in multi-file torrent. * src/TorrentRequestInfo.cc * src/MultiDiskAdaptor.cc Added SHA256 support * src/messageDigest.cc * src/Xml2MetalinkProcessor.cc Show supported message digest algorithms * src/main.cc Updated contact info. * src/main.cc
2007-08-08 14:40:11 +00:00
#endif // ENABLE_SSL
2006-07-03 14:19:59 +00:00
}
void MetalinkEntryTest::testReorderResourcesByPriority()
{
auto entry = createTestEntry();
2012-10-01 14:52:22 +00:00
entry->reorderResourcesByPriority();
2006-07-03 14:19:59 +00:00
CPPUNIT_ASSERT_EQUAL(1, entry->resources.at(0)->priority);
CPPUNIT_ASSERT_EQUAL(10, entry->resources.at(1)->priority);
CPPUNIT_ASSERT_EQUAL(40, entry->resources.at(2)->priority);
CPPUNIT_ASSERT_EQUAL(50, entry->resources.at(3)->priority);
CPPUNIT_ASSERT_EQUAL(90, entry->resources.at(4)->priority);
2006-07-03 14:19:59 +00:00
}
void MetalinkEntryTest::testSetLocationPriority()
{
auto entry = createTestEntry();
auto locations = std::vector<std::string>{"jp", "al", "ro"};
entry->setLocationPriority(locations, -100);
CPPUNIT_ASSERT_EQUAL(std::string("ro"), entry->resources[0]->location);
CPPUNIT_ASSERT_EQUAL(-50, entry->resources[0]->priority);
CPPUNIT_ASSERT_EQUAL(std::string("at"), entry->resources[1]->location);
CPPUNIT_ASSERT_EQUAL(1, entry->resources[1]->priority);
CPPUNIT_ASSERT_EQUAL(std::string("al"), entry->resources[2]->location);
CPPUNIT_ASSERT_EQUAL(-60, entry->resources[2]->priority);
CPPUNIT_ASSERT_EQUAL(std::string("ad"), entry->resources[3]->location);
CPPUNIT_ASSERT_EQUAL(90, entry->resources[3]->priority);
CPPUNIT_ASSERT_EQUAL(std::string("jp"), entry->resources[4]->location);
CPPUNIT_ASSERT_EQUAL(-90, entry->resources[4]->priority);
}
void MetalinkEntryTest::testSetProtocolPriority()
{
auto entry = createTestEntry();
entry->setProtocolPriority("http", -1);
CPPUNIT_ASSERT_EQUAL(50, entry->resources[0]->priority); // ftp
CPPUNIT_ASSERT_EQUAL(0, entry->resources[1]->priority); // http, -1
CPPUNIT_ASSERT_EQUAL(40, entry->resources[2]->priority); // bittorrent
CPPUNIT_ASSERT_EQUAL(90, entry->resources[3]->priority); // not supported
CPPUNIT_ASSERT_EQUAL(10, entry->resources[4]->priority); // https
}
} // namespace aria2