mirror of
https://github.com/aria2/aria2.git
synced 2025-02-26 08:22:11 +00:00
Rewritten DownloadContext and removed SingleFileDownloadContext and BtContext and its derived classes. BitTorrent specific attributes are now set by DownloadContext::setAttribute() with key bittorrent::BITTORRENT. * src/AbstractBtMessage.cc * src/AbstractBtMessage.h * src/AbstractCommand.cc * src/AbstractCommand.h * src/AbstractProxyRequestCommand.cc * src/AbstractProxyResponseCommand.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/AnnounceList.cc * src/BtCheckIntegrityEntry.cc * src/BtConstants.h * src/BtContext.h: Removed * src/BtDependency.cc * src/BtExtendedMessage.cc * src/BtPieceMessage.cc * src/BtPieceMessage.h * src/BtPostDownloadHandler.cc * src/BtRegistry.cc * src/BtRegistry.h * src/BtSetup.cc * src/CheckIntegrityCommand.cc * src/CheckIntegrityEntry.cc * src/ChecksumCheckIntegrityEntry.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtAnnounce.h * src/DefaultBtContext.cc: Removed * src/DefaultBtContext.h: Removed * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtMessageReceiver.h * src/DefaultBtProgressInfoFile.cc * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DefaultPieceStorage.cc * src/DefaultSegmentManFactory.cc * src/DiskAdaptor.cc * src/DiskAdaptor.h * src/DownloadContext.cc * src/DownloadContext.h * src/DownloadEngine.cc * src/DownloadEngineFactory.cc * src/DownloadHandler.cc * src/DownloadResult.h * src/FileAllocationCommand.cc * src/FileAllocationDispatcherCommand.cc * src/FileAllocationEntry.cc * src/FileEntry.h * src/FillRequestGroupCommand.cc * src/FtpDownloadCommand.cc * src/FtpFinishDownloadCommand.cc * src/FtpInitiateConnectionCommand.cc * src/FtpNegotiationCommand.cc * src/FtpTunnelRequestCommand.cc * src/FtpTunnelResponseCommand.cc * src/HandshakeExtensionMessage.cc * src/HandshakeExtensionMessage.h * src/HaveEraseCommand.cc * src/HttpDownloadCommand.cc * src/HttpInitiateConnectionCommand.cc * src/HttpProxyRequestCommand.cc * src/HttpProxyResponseCommand.cc * src/HttpRequest.cc * src/HttpRequest.h * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/HttpServerBodyCommand.cc * src/HttpServerCommand.cc * src/HttpSkipResponseCommand.cc * src/InitiateConnectionCommandFactory.cc * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/IteratableChecksumValidator.cc * src/IteratableChecksumValidator.h * src/IteratableChunkChecksumValidator.cc * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/Metalink2RequestGroup.cc * src/MetalinkPostDownloadHandler.cc * src/MultiDiskAdaptor.cc * src/MultiDiskAdaptor.h * src/MultiFileAllocationIterator.cc * src/MultiUrlRequestInfo.cc * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerReceiveHandshakeCommand.cc * src/PieceHashCheckIntegrityEntry.cc * src/RealtimeCommand.cc * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupEntry.cc * src/RequestGroupMan.cc * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/SegmentMan.cc * src/ShareRatioSeedCriteria.h * src/SingleFileDownloadContext.cc: Removed * src/SingleFileDownloadContext.h: Removed * src/SleepCommand.cc * src/StreamCheckIntegrityEntry.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UnknownLengthPieceStorage.cc * src/Util.cc * src/Util.h * src/XmlRpcMethodImpl.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/main.cc * src/option_processing.cc * test/BitfieldManTest.cc * test/BittorrentHelperTest.cc * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtDependencyTest.cc * test/BtPieceMessageTest.cc * test/BtPostDownloadHandlerTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtContextTest.cc: Removed * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/DefaultPeerStorageTest.cc * test/DefaultPieceStorageTest.cc * test/DirectDiskAdaptorTest.cc * test/DownloadContextTest.cc * test/DownloadHandlerFactoryTest.cc * test/DownloadHelperTest.cc * test/HandshakeExtensionMessageTest.cc * test/HttpRequestTest.cc * test/HttpResponseTest.cc * test/IteratableChecksumValidatorTest.cc * test/IteratableChunkChecksumValidatorTest.cc * test/MSEHandshakeTest.cc * test/Makefile.am * test/Makefile.in * test/Metalink2RequestGroupTest.cc * test/MetalinkPostDownloadHandlerTest.cc * test/MockBtContext.h: Removed * test/MockDHTTaskFactory.h * test/MockDownloadContext.h: Removed * test/MockPieceStorage.h * test/MultiDiskAdaptorTest.cc * test/MultiFileAllocationIteratorTest.cc * test/RequestGroupManTest.cc * test/RequestGroupTest.cc * test/SegmentManTest.cc * test/ShareRatioSeedCriteriaTest.cc * test/SingleFileDownloadContextTest.cc: Removed * test/UtilTest.cc * test/XmlRpcMethodTest.cc
224 lines
7.6 KiB
C++
224 lines
7.6 KiB
C++
#include "MultiFileAllocationIterator.h"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include "File.h"
|
|
#include "MultiDiskAdaptor.h"
|
|
#include "FileEntry.h"
|
|
#include "Exception.h"
|
|
#include "array_fun.h"
|
|
#include "TestUtil.h"
|
|
#include "DiskWriter.h"
|
|
|
|
namespace aria2 {
|
|
|
|
class MultiFileAllocationIteratorTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(MultiFileAllocationIteratorTest);
|
|
CPPUNIT_TEST(testAllocate);
|
|
CPPUNIT_TEST(testMakeDiskWriterEntries);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
private:
|
|
|
|
public:
|
|
void setUp() {}
|
|
|
|
void testAllocate();
|
|
void testMakeDiskWriterEntries();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( MultiFileAllocationIteratorTest );
|
|
|
|
void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
|
|
{
|
|
std::string storeDir = "/tmp/aria2_MultiFileAllocationIteratorTest"
|
|
"_testMakeDiskWriterEntries";
|
|
|
|
SharedHandle<FileEntry> fs[] = {
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file1", 1536, 0)),
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file2", 2048, 1536)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file3", 1024, 3584)),
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file4", 1024, 4608)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file5", 1024, 5632)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file6", 1024, 6656)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file7", 256, 7680)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file8", 255, 7936)),
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/file9", 1025, 8191)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileA", 1024, 9216)),// req no
|
|
SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileB", 1024, 10240)),
|
|
};
|
|
fs[1]->setRequested(false); // file2
|
|
fs[3]->setRequested(false); // file4
|
|
fs[4]->setRequested(false); // file5
|
|
fs[5]->setRequested(false); // file6
|
|
fs[6]->setRequested(false); // file7
|
|
fs[8]->setRequested(false); // file9
|
|
fs[9]->setRequested(false); // fileA
|
|
|
|
|
|
// create empty file4
|
|
createFile(storeDir+std::string("/file4"), 0);
|
|
|
|
SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
|
|
diskAdaptor->setFileEntries(&fs[0], &fs[arrayLength(fs)]);
|
|
diskAdaptor->setPieceLength(1024);
|
|
diskAdaptor->openFile();
|
|
|
|
SharedHandle<MultiFileAllocationIterator> itr
|
|
(dynamic_pointer_cast<MultiFileAllocationIterator>
|
|
(diskAdaptor->fileAllocationIterator()));
|
|
|
|
const std::deque<SharedHandle<DiskWriterEntry> >& entries =
|
|
itr->getDiskWriterEntries();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size());
|
|
|
|
// file1
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file1"),
|
|
entries[0]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[0]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull());
|
|
// file2
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file2"),
|
|
entries[1]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[1]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull());
|
|
// file3
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file3"),
|
|
entries[2]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[2]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[2]->getDiskWriter().isNull());
|
|
// file4, diskWriter is not null, because file exists.
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file4"),
|
|
entries[3]->getFilePath());
|
|
CPPUNIT_ASSERT(!entries[3]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[3]->getDiskWriter().isNull());
|
|
// file5
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file5"),
|
|
entries[4]->getFilePath());
|
|
CPPUNIT_ASSERT(!entries[4]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(entries[4]->getDiskWriter().isNull());
|
|
// file6
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file6"),
|
|
entries[5]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[5]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[5]->getDiskWriter().isNull());
|
|
// file7
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file7"),
|
|
entries[6]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[6]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[6]->getDiskWriter().isNull());
|
|
// file8
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file8"),
|
|
entries[7]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[7]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[7]->getDiskWriter().isNull());
|
|
// file9
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file9"),
|
|
entries[8]->getFilePath());
|
|
CPPUNIT_ASSERT(!entries[8]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[8]->getDiskWriter().isNull());
|
|
// fileA
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileA"),
|
|
entries[9]->getFilePath());
|
|
CPPUNIT_ASSERT(!entries[9]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(entries[9]->getDiskWriter().isNull());
|
|
// fileB
|
|
CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileB"),
|
|
entries[10]->getFilePath());
|
|
CPPUNIT_ASSERT(entries[10]->needsFileAllocation());
|
|
CPPUNIT_ASSERT(!entries[10]->getDiskWriter().isNull());
|
|
}
|
|
|
|
void MultiFileAllocationIteratorTest::testAllocate()
|
|
{
|
|
std::string storeDir =
|
|
"/tmp/aria2_MultiFileAllocationIteratorTest_testAllocate";
|
|
|
|
std::string fname1 = "file1";
|
|
std::string fname2 = "file2";
|
|
std::string fname3 = "file3";
|
|
std::string fname4 = "file4";
|
|
std::string fname5 = "file5";
|
|
std::string fname6 = "file6";
|
|
int64_t length1 = 32769;
|
|
int64_t length2 = 0;
|
|
int64_t length3 = 8;
|
|
int64_t length4 = 10;
|
|
int64_t length5 = 20;
|
|
int64_t length6 = 30;
|
|
|
|
try {
|
|
SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
|
|
diskAdaptor->setPieceLength(1);
|
|
|
|
int64_t offset = 0;
|
|
SharedHandle<FileEntry> fileEntry1(new FileEntry(storeDir+"/"+fname1,
|
|
length1,
|
|
offset));
|
|
offset += length1;
|
|
SharedHandle<FileEntry> fileEntry2(new FileEntry(storeDir+"/"+fname2,
|
|
length2,
|
|
offset));
|
|
|
|
offset += length2;
|
|
SharedHandle<FileEntry> fileEntry3(new FileEntry(storeDir+"/"+fname3,
|
|
length3,
|
|
offset));
|
|
|
|
offset += length3;
|
|
SharedHandle<FileEntry> fileEntry4(new FileEntry(storeDir+"/"+fname4,
|
|
length4,
|
|
offset));
|
|
fileEntry4->setRequested(false);
|
|
|
|
offset += length4;
|
|
SharedHandle<FileEntry> fileEntry5(new FileEntry(storeDir+"/"+fname5,
|
|
length5,
|
|
offset));
|
|
|
|
offset += length5;
|
|
SharedHandle<FileEntry> fileEntry6(new FileEntry(storeDir+"/"+fname6,
|
|
length6,
|
|
offset));
|
|
fileEntry6->setRequested(false);
|
|
|
|
std::vector<SharedHandle<FileEntry> > fs;
|
|
fs.push_back(fileEntry1);
|
|
fs.push_back(fileEntry2);
|
|
fs.push_back(fileEntry3);
|
|
fs.push_back(fileEntry4);
|
|
fs.push_back(fileEntry5);
|
|
fs.push_back(fileEntry6);
|
|
diskAdaptor->setFileEntries(fs.begin(), fs.end());
|
|
|
|
for(std::vector<SharedHandle<FileEntry> >::const_iterator i = fs.begin();
|
|
i != fs.end(); ++i) {
|
|
File((*i)->getPath()).remove();
|
|
}
|
|
|
|
// we have to open file first.
|
|
diskAdaptor->initAndOpenFile();
|
|
SharedHandle<MultiFileAllocationIterator> itr
|
|
(dynamic_pointer_cast<MultiFileAllocationIterator>
|
|
(diskAdaptor->fileAllocationIterator()));
|
|
while(!itr->finished()) {
|
|
itr->allocateChunk();
|
|
}
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length1, File(fileEntry1->getPath()).size());
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length2, File(fileEntry2->getPath()).size());
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length3, File(fileEntry3->getPath()).size());
|
|
CPPUNIT_ASSERT(!File(fileEntry4->getPath()).isFile());
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)length5, File(fileEntry5->getPath()).size());
|
|
CPPUNIT_ASSERT(!File(fileEntry6->getPath()).isFile());
|
|
} catch(Exception& e) {
|
|
CPPUNIT_FAIL(e.stackTrace());
|
|
}
|
|
}
|
|
|
|
} // namespace aria2
|