#include "download_helper.h" #include #include #include #include #include #include "RequestGroup.h" #include "DownloadContext.h" #include "Option.h" #include "array_fun.h" #include "prefs.h" #include "Exception.h" #include "Util.h" namespace aria2 { class DownloadHelperTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DownloadHelperTest); CPPUNIT_TEST(testCreateRequestGroupForUri); CPPUNIT_TEST(testCreateRequestGroupForUri_parameterized); CPPUNIT_TEST(testCreateRequestGroupForUriList); #ifdef ENABLE_BITTORRENT CPPUNIT_TEST(testCreateRequestGroupForUri_BitTorrent); CPPUNIT_TEST(testCreateRequestGroupForBitTorrent); #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK CPPUNIT_TEST(testCreateRequestGroupForUri_Metalink); CPPUNIT_TEST(testCreateRequestGroupForMetalink); #endif // ENABLE_METALINK CPPUNIT_TEST_SUITE_END(); public: void setUp() {} void tearDown() {} void testCreateRequestGroupForUri(); void testCreateRequestGroupForUri_parameterized(); void testCreateRequestGroupForUriList(); #ifdef ENABLE_BITTORRENT void testCreateRequestGroupForUri_BitTorrent(); void testCreateRequestGroupForBitTorrent(); #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK void testCreateRequestGroupForUri_Metalink(); void testCreateRequestGroupForMetalink(); #endif // ENABLE_METALINK }; CPPUNIT_TEST_SUITE_REGISTRATION(DownloadHelperTest); void DownloadHelperTest::testCreateRequestGroupForUri() { std::string array[] = { "http://alpha/file", "http://bravo/file", "http://charlie/file" }; std::deque uris(&array[0], &array[arrayLength(array)]); Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); for(size_t i = 0; i < arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i]); } CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getActualBasePath()); } op.put(PREF_SPLIT, "5"); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)5, uris.size()); for(size_t i = 0; i < arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i]); } for(size_t i = 0; i < 5-arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i+arrayLength(array)]); } CPPUNIT_ASSERT_EQUAL((unsigned int)5, group->getNumConcurrentCommand()); } op.put(PREF_SPLIT, "2"); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); for(size_t i = 0; i < arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i]); } CPPUNIT_ASSERT_EQUAL((unsigned int)2, group->getNumConcurrentCommand()); } op.put(PREF_FORCE_SEQUENTIAL, V_TRUE); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)3, result.size()); // for alpha server SharedHandle alphaGroup = result[0]; std::deque alphaURIs; alphaGroup->getURIs(alphaURIs); CPPUNIT_ASSERT_EQUAL((size_t)2, alphaURIs.size()); for(size_t i = 0; i < 2; ++i) { CPPUNIT_ASSERT_EQUAL(array[0], uris[0]); } CPPUNIT_ASSERT_EQUAL((unsigned int)2, alphaGroup->getNumConcurrentCommand()); SharedHandle alphaCtx = alphaGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), alphaCtx->getDir()); // See the value of PREF_OUT is not used as a file name. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/index.html"), alphaCtx->getActualBasePath()); } } void DownloadHelperTest::testCreateRequestGroupForUri_parameterized() { std::string array[] = { "http://{alpha, bravo}/file", "http://charlie/file" }; std::deque uris(&array[0], &array[arrayLength(array)]); Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); op.put(PREF_PARAMETERIZED_URI, V_TRUE); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), uris[0]); CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"), uris[1]); CPPUNIT_ASSERT_EQUAL(std::string("http://charlie/file"), uris[2]); CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getActualBasePath()); } } #ifdef ENABLE_BITTORRENT void DownloadHelperTest::testCreateRequestGroupForUri_BitTorrent() { std::string array[] = { "http://alpha/file", "test.torrent", "http://bravo/file", "http://charlie/file" }; std::deque uris(&array[0], &array[arrayLength(array)]); Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); { std::deque > result; createRequestGroupForUri(result, &op, uris); CPPUNIT_ASSERT_EQUAL((size_t)2, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); CPPUNIT_ASSERT_EQUAL(array[0], uris[0]); CPPUNIT_ASSERT_EQUAL(array[2], uris[1]); CPPUNIT_ASSERT_EQUAL(array[3], uris[2]); CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getActualBasePath()); SharedHandle torrentGroup = result[1]; std::deque auxURIs; torrentGroup->getURIs(auxURIs); CPPUNIT_ASSERT(auxURIs.empty()); CPPUNIT_ASSERT_EQUAL((unsigned int)3, torrentGroup->getNumConcurrentCommand()); SharedHandle btctx = torrentGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), btctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-test"), btctx->getActualBasePath()); } } #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK void DownloadHelperTest::testCreateRequestGroupForUri_Metalink() { std::string array[] = { "http://alpha/file", "http://bravo/file", "http://charlie/file", "test.xml" }; std::deque uris(&array[0], &array[arrayLength(array)]); Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_METALINK_SERVERS, "2"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); { std::deque > result; createRequestGroupForUri(result, &op, uris); // group1: http://alpha/file, ... // group2-7: 6 file entry in Metalink and 1 torrent file download #ifdef ENABLE_BITTORRENT CPPUNIT_ASSERT_EQUAL((size_t)7, result.size()); #else // !ENABLE_BITTORRENT CPPUNIT_ASSERT_EQUAL((size_t)6, result.size()); #endif // !ENABLE_BITTORRENT SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); for(size_t i = 0; i < 3; ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i]); } CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getActualBasePath()); SharedHandle aria2052Group = result[1]; CPPUNIT_ASSERT_EQUAL((unsigned int)1, // because of maxconnections attribute aria2052Group->getNumConcurrentCommand()); SharedHandle aria2052Ctx = aria2052Group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), aria2052Ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-0.5.2.tar.bz2"), aria2052Ctx->getActualBasePath()); SharedHandle aria2051Group = result[2]; CPPUNIT_ASSERT_EQUAL((unsigned int)2, aria2051Group->getNumConcurrentCommand()); } } #endif // ENABLE_METALINK void DownloadHelperTest::testCreateRequestGroupForUriList() { Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_INPUT_FILE, "input_uris.txt"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); std::deque > result; createRequestGroupForUriList(result, &op); CPPUNIT_ASSERT_EQUAL((size_t)2, result.size()); SharedHandle fileGroup = result[0]; std::deque fileURIs; fileGroup->getURIs(fileURIs); CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), fileURIs[0]); CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"), fileURIs[1]); CPPUNIT_ASSERT_EQUAL(std::string("http://charlie/file"), fileURIs[2]); CPPUNIT_ASSERT_EQUAL((unsigned int)3, fileGroup->getNumConcurrentCommand()); SharedHandle fileCtx = fileGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/mydownloads"), fileCtx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/mydownloads/myfile.out"), fileCtx->getActualBasePath()); SharedHandle fileISOGroup = result[1]; SharedHandle fileISOCtx = fileISOGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), fileISOCtx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), fileISOCtx->getActualBasePath()); } #ifdef ENABLE_BITTORRENT void DownloadHelperTest::testCreateRequestGroupForBitTorrent() { std::string array[] = { "http://alpha/file", "http://bravo/file", "http://charlie/file" }; std::deque auxURIs(&array[0], &array[arrayLength(array)]); Option op; op.put(PREF_SPLIT, "5"); op.put(PREF_TORRENT_FILE, "test.torrent"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); { std::deque > result; createRequestGroupForBitTorrent(result, &op, auxURIs); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)5, uris.size()); for(size_t i = 0; i < arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i]); } for(size_t i = 0; i < 5-arrayLength(array); ++i) { CPPUNIT_ASSERT_EQUAL(array[i], uris[i+arrayLength(array)]); } CPPUNIT_ASSERT_EQUAL((unsigned int)5, group->getNumConcurrentCommand()); } { // no URIs are given std::deque > result; std::deque emptyURIs; createRequestGroupForBitTorrent(result, &op, emptyURIs); CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)0, uris.size()); } op.put(PREF_FORCE_SEQUENTIAL, V_TRUE); { std::deque > result; createRequestGroupForBitTorrent(result, &op, auxURIs); // See --force-requencial is ignored CPPUNIT_ASSERT_EQUAL((size_t)1, result.size()); } } #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK void DownloadHelperTest::testCreateRequestGroupForMetalink() { Option op; op.put(PREF_SPLIT, "3"); op.put(PREF_METALINK_FILE, "test.xml"); op.put(PREF_METALINK_SERVERS, "5"); op.put(PREF_DIR, "/tmp"); op.put(PREF_OUT, "file.out"); { std::deque > result; createRequestGroupForMetalink(result, &op); #ifdef ENABLE_BITTORRENT CPPUNIT_ASSERT_EQUAL((size_t)6, result.size()); #else // !ENABLE_BITTORRENT CPPUNIT_ASSERT_EQUAL((size_t)5, result.size()); #endif // !ENABLE_BITTORRENT SharedHandle group = result[0]; std::deque uris; group->getURIs(uris); std::sort(uris.begin(), uris.end()); CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size()); 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]); // See numConcurrentCommand is 1 because of maxconnections attribute. CPPUNIT_ASSERT_EQUAL((unsigned int)1, group->getNumConcurrentCommand()); } } #endif // ENABLE_METALINK } // namespace aria2