#include "MetalinkProcessorFactory.h" #include "MetalinkParserStateMachine.h" #include "Exception.h" #include "DefaultDiskWriter.h" #include "ByteArrayDiskWriter.h" #include "MetalinkProcessor.h" #include "Metalinker.h" #include "MetalinkEntry.h" #include "MetalinkResource.h" #ifdef ENABLE_MESSAGE_DIGEST # include "ChunkChecksum.h" # include "Checksum.h" #endif // ENABLE_MESSAGE_DIGEST #include #include namespace aria2 { class MetalinkProcessorTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(MetalinkProcessorTest); CPPUNIT_TEST(testParseFile); CPPUNIT_TEST(testParseFromBinaryStream); CPPUNIT_TEST(testMalformedXML); CPPUNIT_TEST(testMalformedXML2); CPPUNIT_TEST(testBadSize); CPPUNIT_TEST(testBadMaxConn); CPPUNIT_TEST(testNoName); CPPUNIT_TEST(testBadURLPrefs); CPPUNIT_TEST(testBadURLMaxConn); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_TEST(testUnsupportedType); CPPUNIT_TEST(testMultiplePieces); CPPUNIT_TEST(testBadPieceNo); CPPUNIT_TEST(testBadPieceLength); CPPUNIT_TEST(testUnsupportedType_piece); #endif // ENABLE_MESSAGE_DIGEST CPPUNIT_TEST(testLargeFileSize); CPPUNIT_TEST_SUITE_END(); private: public: void testParseFile(); void testParseFromBinaryStream(); void testMalformedXML(); void testMalformedXML2(); void testBadSize(); void testBadMaxConn(); void testNoName(); void testBadURLPrefs(); void testBadURLMaxConn(); #ifdef ENABLE_MESSAGE_DIGEST void testUnsupportedType(); void testMultiplePieces(); void testBadPieceNo(); void testBadPieceLength(); void testUnsupportedType_piece(); #endif // ENABLE_MESSAGE_DIGEST void testLargeFileSize(); }; CPPUNIT_TEST_SUITE_REGISTRATION( MetalinkProcessorTest ); void MetalinkProcessorTest::testParseFile() { SharedHandle proc(MetalinkProcessorFactory::newInstance()); try { SharedHandle metalinker = proc->parseFile("test.xml"); std::deque >::iterator entryItr = metalinker->entries.begin(); SharedHandle entry1 = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.5.2.tar.bz2"), entry1->getPath()); CPPUNIT_ASSERT_EQUAL(0ULL, entry1->getLength()); CPPUNIT_ASSERT_EQUAL(std::string("0.5.2"), entry1->version); CPPUNIT_ASSERT_EQUAL(std::string("en-US"), entry1->language); CPPUNIT_ASSERT_EQUAL(std::string("Linux-x86"), entry1->os); CPPUNIT_ASSERT_EQUAL(1, entry1->maxConnections); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT_EQUAL(std::string("a96cf3f0266b91d87d5124cf94326422800b627d"), entry1->checksum->getMessageDigest()); CPPUNIT_ASSERT_EQUAL(std::string("sha1"), entry1->checksum->getAlgo()); #endif // ENABLE_MESSAGE_DIGEST std::deque >::iterator resourceItr1 = entry1->resources.begin(); SharedHandle resource1 = *resourceItr1; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, resource1->type); CPPUNIT_ASSERT_EQUAL(std::string("JP"), resource1->location); CPPUNIT_ASSERT_EQUAL(100, resource1->preference); CPPUNIT_ASSERT_EQUAL(std::string("ftp://ftphost/aria2-0.5.2.tar.bz2"), resource1->url); CPPUNIT_ASSERT_EQUAL(1, resource1->maxConnections); resourceItr1++; SharedHandle resource2 = *resourceItr1; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTP, resource2->type); CPPUNIT_ASSERT_EQUAL(std::string("US"), resource2->location); CPPUNIT_ASSERT_EQUAL(100, resource2->preference); CPPUNIT_ASSERT_EQUAL(std::string("http://httphost/aria2-0.5.2.tar.bz2"), resource2->url); CPPUNIT_ASSERT_EQUAL(-1, resource2->maxConnections); entryItr++; SharedHandle entry2 = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.5.1.tar.bz2"), entry2->getPath()); CPPUNIT_ASSERT_EQUAL(345689ULL, entry2->getLength()); CPPUNIT_ASSERT_EQUAL(std::string("0.5.1"), entry2->version); CPPUNIT_ASSERT_EQUAL(std::string("ja-JP"), entry2->language); CPPUNIT_ASSERT_EQUAL(std::string("Linux-m68k"), entry2->os); CPPUNIT_ASSERT_EQUAL(-1, entry2->maxConnections); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT_EQUAL(std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"), entry2->checksum->getMessageDigest()); CPPUNIT_ASSERT_EQUAL((size_t)2, entry2->chunkChecksum->countChecksum()); CPPUNIT_ASSERT_EQUAL((size_t)262144, entry2->chunkChecksum->getChecksumLength()); CPPUNIT_ASSERT_EQUAL(std::string("179463a88d79cbf0b1923991708aead914f26142"), entry2->chunkChecksum->getChecksum(0)); CPPUNIT_ASSERT_EQUAL(std::string("fecf8bc9a1647505fe16746f94e97a477597dbf3"), entry2->chunkChecksum->getChecksum(1)); CPPUNIT_ASSERT_EQUAL(std::string("sha1"), entry2->checksum->getAlgo()); #endif // ENABLE_MESSAGE_DIGEST entryItr++; // test case: verification hash is not provided SharedHandle entry3 = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("NoVerificationHash"), entry3->getPath()); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT(entry3->checksum.isNull()); CPPUNIT_ASSERT(entry3->chunkChecksum.isNull()); #endif // ENABLE_MESSAGE_DIGEST entryItr++; // test case: unsupported verification hash is included SharedHandle entry4 = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("UnsupportedVerificationHashTypeIncluded"), entry4->getPath()); #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT_EQUAL(std::string("sha1"), entry4->checksum->getAlgo()); CPPUNIT_ASSERT_EQUAL(std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"), entry4->checksum->getMessageDigest()); CPPUNIT_ASSERT_EQUAL(std::string("sha1"), entry4->chunkChecksum->getAlgo()); #endif // ENABLE_MESSAGE_DIGEST } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testParseFromBinaryStream() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); DefaultDiskWriterHandle dw(new DefaultDiskWriter()); dw->openExistingFile("test.xml"); try { SharedHandle m = proc->parseFromBinaryStream(dw); std::deque >::iterator entryItr = m->entries.begin(); SharedHandle entry1 = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.5.2.tar.bz2"), entry1->getPath()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testMalformedXML() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString(""); try { SharedHandle m = proc->parseFromBinaryStream(dw); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; } } void MetalinkProcessorTest::testMalformedXML2() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString(""); try { SharedHandle m = proc->parseFromBinaryStream(dw); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; } } void MetalinkProcessorTest::testBadSize() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " abc" " 0.5.2" " en-US" " Linux-x86" "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); std::deque >::iterator entryItr = m->entries.begin(); SharedHandle e = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.5.2.tar.bz2"), e->getPath()); CPPUNIT_ASSERT_EQUAL(0ULL, e->getLength()); CPPUNIT_ASSERT_EQUAL(std::string("0.5.2"), e->version); CPPUNIT_ASSERT_EQUAL(std::string("en-US"), e->language); CPPUNIT_ASSERT_EQUAL(std::string("Linux-x86"), e->os); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testBadMaxConn() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 43743838" " 0.5.2" " en-US" " Linux-x86" " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); std::deque >::iterator entryItr = m->entries.begin(); SharedHandle e = *entryItr; CPPUNIT_ASSERT_EQUAL(43743838ULL, e->getLength()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testNoName() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 1024" " 0.0.1" " GB" " Linux-x64" "" "" " 43743838" " 0.5.2" " en-US" " Linux-x86" "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); CPPUNIT_ASSERT_EQUAL((size_t)1, m->entries.size()); std::deque >::iterator entryItr = m->entries.begin(); SharedHandle e = *entryItr; CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.5.2.tar.bz2"), e->getPath()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testBadURLPrefs() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 43743838" " 0.5.2" " en-US" " Linux-x86" " " " ftp://mirror/" " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle r = e->resources[0]; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, r->type); CPPUNIT_ASSERT_EQUAL(0, r->preference); CPPUNIT_ASSERT_EQUAL(1, r->maxConnections); CPPUNIT_ASSERT_EQUAL(std::string("JP"), r->location); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testBadURLMaxConn() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 43743838" " 0.5.2" " en-US" " Linux-x86" " " " ftp://mirror/" " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle r = e->resources[0]; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, r->type); CPPUNIT_ASSERT_EQUAL(100, r->preference); CPPUNIT_ASSERT_EQUAL(-1, r->maxConnections); CPPUNIT_ASSERT_EQUAL(std::string("JP"), r->location); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } #ifdef ENABLE_MESSAGE_DIGEST void MetalinkProcessorTest::testUnsupportedType() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 43743838" " 0.5.2" " en-US" " Linux-x86" " " " ftp://mirror/" " magnet:xt=XYZ" " http://mirror/" " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; CPPUNIT_ASSERT_EQUAL((size_t)3, e->resources.size()); SharedHandle r1 = e->resources[0]; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, r1->type); SharedHandle r2 = e->resources[1]; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_NOT_SUPPORTED, r2->type); SharedHandle r3 = e->resources[2]; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTP, r3->type); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testMultiplePieces() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " " " " " " " " " " " " " " " " "" "" ""); try { // aria2 prefers sha1 SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle c = e->chunkChecksum; CPPUNIT_ASSERT_EQUAL(std::string("sha1"), c->getAlgo()); CPPUNIT_ASSERT_EQUAL((size_t)1024, c->getChecksumLength()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testBadPieceNo() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " " " " " abc" " xyz" " " " " " abc" " " " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle c = e->chunkChecksum; CPPUNIT_ASSERT_EQUAL(std::string("sha256"), c->getAlgo()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testBadPieceLength() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " " " " " abc" " " " " " abc" " " " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle c = e->chunkChecksum; CPPUNIT_ASSERT_EQUAL(std::string("sha256"), c->getAlgo()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } void MetalinkProcessorTest::testUnsupportedType_piece() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " " " " " abc" " " " " " abc" " " " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; SharedHandle c = e->chunkChecksum; CPPUNIT_ASSERT_EQUAL(std::string("sha256"), c->getAlgo()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } #endif // ENABLE_MESSAGE_DIGEST void MetalinkProcessorTest::testLargeFileSize() { SharedHandle proc = MetalinkProcessorFactory::newInstance(); SharedHandle dw(new ByteArrayDiskWriter()); dw->setString("" "" "" " 9223372036854775807" " " " ftp://mirror/" " " "" "" ""); try { SharedHandle m = proc->parseFromBinaryStream(dw); SharedHandle e = m->entries[0]; CPPUNIT_ASSERT_EQUAL(9223372036854775807ULL, e->getLength()); } catch(Exception& e) { CPPUNIT_FAIL(e.stackTrace()); } } } // namespace aria2