2007-10-11 16:58:24 +00:00
|
|
|
#include "Metalink2RequestGroup.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "DownloadContext.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "RequestGroup.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "Signature.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
class Metalink2RequestGroupTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(Metalink2RequestGroupTest);
|
|
|
|
CPPUNIT_TEST(testGenerate);
|
2010-02-27 15:04:48 +00:00
|
|
|
CPPUNIT_TEST(testGenerate_groupByMetaurl);
|
2010-05-06 14:18:29 +00:00
|
|
|
CPPUNIT_TEST(testGenerate_dosDirTraversal);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<Option> option_;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
option_.reset(new Option());
|
|
|
|
option_->put(PREF_SPLIT, "1");
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testGenerate();
|
2010-02-27 15:04:48 +00:00
|
|
|
void testGenerate_groupByMetaurl();
|
2010-05-06 14:18:29 +00:00
|
|
|
void testGenerate_dosDirTraversal();
|
2007-10-11 16:58:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( Metalink2RequestGroupTest );
|
|
|
|
|
|
|
|
void Metalink2RequestGroupTest::testGenerate()
|
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<SharedHandle<RequestGroup> > groups;
|
2010-06-21 13:51:56 +00:00
|
|
|
option_->put(PREF_DIR, "/tmp");
|
2010-12-02 13:38:36 +00:00
|
|
|
Metalink2RequestGroup().generate(groups, A2_TEST_DIR"/test.xml", option_);
|
2007-10-11 16:58:24 +00:00
|
|
|
// first file
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<RequestGroup> rg = groups[0];
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> uris;
|
2009-06-29 08:42:58 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
2008-02-08 15:53:45 +00:00
|
|
|
std::sort(uris.begin(), uris.end());
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
|
2008-11-11 14:56:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("ftp://ftphost/aria2-0.5.2.tar.bz2"), uris[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("http://httphost/aria2-0.5.2.tar.bz2"), uris[1]);
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext();
|
2008-11-11 14:56:46 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(dctx);
|
2012-06-25 14:35:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)0LL, dctx->getTotalLength());
|
2007-11-14 10:10:38 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-07-27 13:44:30 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
|
2008-11-11 14:56:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("a96cf3f0266b91d87d5124cf94326422800b627d"),
|
2011-10-13 12:40:07 +00:00
|
|
|
util::toHex(dctx->getDigest()));
|
2007-11-14 10:10:38 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(dctx->getSignature());
|
2008-07-12 13:35:35 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType());
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
// second file
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<RequestGroup> rg = groups[1];
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> uris;
|
2009-06-29 08:42:58 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
|
2008-11-11 14:56:46 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext();
|
2008-11-11 14:56:46 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(dctx);
|
2007-11-14 10:10:38 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-07-27 13:44:30 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size());
|
2011-12-07 15:03:25 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(262144, dctx->getPieceLength());
|
2011-07-27 13:44:30 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
|
2010-05-14 15:18:59 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"),
|
2011-10-13 12:40:07 +00:00
|
|
|
util::toHex(dctx->getDigest()));
|
2007-11-14 10:10:38 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(!dctx->getSignature());
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2007-11-14 10:10:38 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2007-10-11 16:58:24 +00:00
|
|
|
// fifth file <- downloading .torrent file
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<RequestGroup> rg = groups[4];
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> uris;
|
2009-06-29 08:42:58 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
2008-11-11 14:56:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("http://host/torrent-http.integrated.torrent"), uris[0]);
|
2009-06-28 10:37:15 +00:00
|
|
|
const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext();
|
2008-11-11 14:56:46 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(dctx);
|
2009-12-20 09:49:43 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(groups[5]->getGID(), rg->belongsTo());
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2007-11-14 10:10:38 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
|
|
|
|
2009-12-20 09:49:43 +00:00
|
|
|
// sixth file <- depends on fifth file to download .torrent file.
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2007-11-14 10:10:38 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<RequestGroup> rg = groups[5];
|
2007-11-14 10:10:38 +00:00
|
|
|
#else
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<RequestGroup> rg = groups[4];
|
2007-11-14 10:10:38 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> uris;
|
2009-06-29 08:42:58 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
2007-10-11 16:58:24 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
2008-11-11 14:56:46 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("http://host/torrent-http.integrated"), uris[0]);
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext();
|
2008-11-11 14:56:46 +00:00
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(dctx);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2010-02-27 15:04:48 +00:00
|
|
|
void Metalink2RequestGroupTest::testGenerate_groupByMetaurl()
|
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<SharedHandle<RequestGroup> > groups;
|
2010-12-02 13:38:36 +00:00
|
|
|
Metalink2RequestGroup().generate(groups,
|
|
|
|
A2_TEST_DIR"/metalink4-groupbymetaurl.xml",
|
2010-06-21 13:51:56 +00:00
|
|
|
option_);
|
2010-02-27 15:04:48 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
|
2010-03-07 15:37:18 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_BITTORRENT
|
2010-02-27 15:04:48 +00:00
|
|
|
// first RequestGroup is torrent for second RequestGroup
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[0];
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> uris;
|
2010-02-27 15:04:48 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://torrent"), uris[0]);
|
|
|
|
}
|
|
|
|
// second
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[1];
|
|
|
|
SharedHandle<DownloadContext> dctx = rg->getDownloadContext();
|
|
|
|
const std::vector<SharedHandle<FileEntry> >& fileEntries =
|
|
|
|
dctx->getFileEntries();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./file1"), fileEntries[0]->getPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("file1"), fileEntries[0]->getOriginalName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"),
|
|
|
|
fileEntries[0]->getRemainingUris()[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./file3"), fileEntries[1]->getPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("file3"), fileEntries[1]->getOriginalName());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[1]->getRemainingUris().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"),
|
|
|
|
fileEntries[1]->getRemainingUris()[0]);
|
|
|
|
}
|
|
|
|
// third
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[2];
|
|
|
|
SharedHandle<DownloadContext> dctx = rg->getDownloadContext();
|
|
|
|
const std::vector<SharedHandle<FileEntry> >& fileEntries =
|
|
|
|
dctx->getFileEntries();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./file2"), fileEntries[0]->getPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"),
|
|
|
|
fileEntries[0]->getRemainingUris()[0]);
|
|
|
|
}
|
2010-03-07 15:37:18 +00:00
|
|
|
#else // !ENABLE_BITTORRENT
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[0];
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"), uris[0]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[1];
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"), uris[0]);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SharedHandle<RequestGroup> rg = groups[2];
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"), uris[0]);
|
|
|
|
}
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2010-03-07 15:37:18 +00:00
|
|
|
#endif // !ENABLE_BITTORRENT
|
2010-02-27 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 14:18:29 +00:00
|
|
|
void Metalink2RequestGroupTest::testGenerate_dosDirTraversal()
|
|
|
|
{
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
std::vector<SharedHandle<RequestGroup> > groups;
|
2010-06-21 13:51:56 +00:00
|
|
|
option_->put(PREF_DIR, "/tmp");
|
2010-05-06 14:18:29 +00:00
|
|
|
Metalink2RequestGroup().generate
|
2010-12-02 13:38:36 +00:00
|
|
|
(groups, A2_TEST_DIR"/metalink4-dosdirtraversal.xml", option_);
|
2010-05-06 14:18:29 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
|
|
|
|
SharedHandle<RequestGroup> rg = groups[0];
|
|
|
|
SharedHandle<FileEntry> file = rg->getDownloadContext()->getFirstFileEntry();
|
2010-10-04 14:33:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cexample.ext"),
|
2010-05-06 14:18:29 +00:00
|
|
|
file->getPath());
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2010-05-06 14:18:29 +00:00
|
|
|
rg = groups[2];
|
|
|
|
file = rg->getDownloadContext()->getFileEntries()[0];
|
2010-10-04 14:33:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile1.ext"),
|
2010-05-06 14:18:29 +00:00
|
|
|
file->getPath());
|
|
|
|
file = rg->getDownloadContext()->getFileEntries()[1];
|
2010-10-04 14:33:49 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile2.ext"),
|
2010-05-06 14:18:29 +00:00
|
|
|
file->getPath());
|
|
|
|
#endif // ENABLE_BITTORRENT
|
|
|
|
#endif // __MINGW32__
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|