2007-11-05 15:13:55 +00:00
|
|
|
#include "FileEntry.h"
|
2009-06-29 16:43:41 +00:00
|
|
|
|
2007-11-05 15:13:55 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-06-29 16:43:41 +00:00
|
|
|
#include "InOrderURISelector.h"
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2007-11-05 15:13:55 +00:00
|
|
|
class FileEntryTest : public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(FileEntryTest);
|
|
|
|
CPPUNIT_TEST(testSetupDir);
|
2009-06-29 08:42:58 +00:00
|
|
|
CPPUNIT_TEST(testRemoveURIWhoseHostnameIs);
|
|
|
|
CPPUNIT_TEST(testExtractURIResult);
|
2009-06-29 16:43:41 +00:00
|
|
|
CPPUNIT_TEST(testGetRequest);
|
|
|
|
CPPUNIT_TEST(testGetRequest_disableSingleHostMultiConnection);
|
2007-11-05 15:13:55 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
public:
|
|
|
|
void setUp() {}
|
|
|
|
|
|
|
|
void testSetupDir();
|
2009-06-29 08:42:58 +00:00
|
|
|
void testRemoveURIWhoseHostnameIs();
|
|
|
|
void testExtractURIResult();
|
2009-06-29 16:43:41 +00:00
|
|
|
void testGetRequest();
|
|
|
|
void testGetRequest_disableSingleHostMultiConnection();
|
2007-11-05 15:13:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( FileEntryTest );
|
|
|
|
|
2009-06-29 16:43:41 +00:00
|
|
|
static SharedHandle<FileEntry> createFileEntry()
|
|
|
|
{
|
|
|
|
const char* uris[] = { "http://localhost/aria2.zip",
|
|
|
|
"ftp://localhost/aria2.zip",
|
|
|
|
"http://mirror/aria2.zip" };
|
|
|
|
SharedHandle<FileEntry> fileEntry(new FileEntry());
|
|
|
|
fileEntry->setUris(std::deque<std::string>(&uris[0], &uris[3]));
|
|
|
|
return fileEntry;
|
|
|
|
}
|
|
|
|
|
2007-11-05 15:13:55 +00:00
|
|
|
void FileEntryTest::testSetupDir()
|
|
|
|
{
|
2009-03-09 15:10:26 +00:00
|
|
|
std::string dir = "/tmp/aria2-FileEntryTest-testSetupDir";
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string filename = "filename";
|
2009-03-09 15:10:26 +00:00
|
|
|
std::string path = dir+"/"+filename;
|
|
|
|
File d(dir);
|
2007-11-05 15:13:55 +00:00
|
|
|
if(d.exists()) {
|
|
|
|
CPPUNIT_ASSERT(d.remove());
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT(!d.exists());
|
2009-03-09 15:10:26 +00:00
|
|
|
FileEntry fileEntry(path, 0, 0);
|
|
|
|
fileEntry.setupDir();
|
2007-11-05 15:13:55 +00:00
|
|
|
CPPUNIT_ASSERT(d.isDir());
|
|
|
|
File f(path);
|
|
|
|
CPPUNIT_ASSERT(!f.exists());
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
void FileEntryTest::testRemoveURIWhoseHostnameIs()
|
|
|
|
{
|
2009-06-29 16:43:41 +00:00
|
|
|
SharedHandle<FileEntry> fileEntry = createFileEntry();
|
|
|
|
fileEntry->removeURIWhoseHostnameIs("localhost");
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntry->getRemainingUris().size());
|
2009-06-29 08:42:58 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/aria2.zip"),
|
2009-06-29 16:43:41 +00:00
|
|
|
fileEntry->getRemainingUris()[0]);
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileEntryTest::testExtractURIResult()
|
|
|
|
{
|
|
|
|
FileEntry fileEntry;
|
|
|
|
fileEntry.addURIResult("http://timeout/file", downloadresultcode::TIME_OUT);
|
|
|
|
fileEntry.addURIResult("http://finished/file", downloadresultcode::FINISHED);
|
|
|
|
fileEntry.addURIResult("http://timeout/file2", downloadresultcode::TIME_OUT);
|
|
|
|
fileEntry.addURIResult("http://unknownerror/file", downloadresultcode::UNKNOWN_ERROR);
|
|
|
|
|
|
|
|
std::deque<URIResult> res;
|
|
|
|
fileEntry.extractURIResult(res, downloadresultcode::TIME_OUT);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://timeout/file"), res[0].getURI());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://timeout/file2"), res[1].getURI());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry.getURIResults().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://finished/file"),
|
|
|
|
fileEntry.getURIResults()[0].getURI());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://unknownerror/file"),
|
|
|
|
fileEntry.getURIResults()[1].getURI());
|
|
|
|
|
|
|
|
res.clear();
|
|
|
|
|
|
|
|
fileEntry.extractURIResult(res, downloadresultcode::TIME_OUT);
|
|
|
|
CPPUNIT_ASSERT(res.empty());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntry.getURIResults().size());
|
|
|
|
}
|
|
|
|
|
2009-06-29 16:43:41 +00:00
|
|
|
void FileEntryTest::testGetRequest()
|
|
|
|
{
|
|
|
|
SharedHandle<FileEntry> fileEntry = createFileEntry();
|
|
|
|
SharedHandle<InOrderURISelector> selector(new InOrderURISelector());
|
|
|
|
SharedHandle<Request> req = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), req->getHost());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http"), req->getProtocol());
|
|
|
|
|
|
|
|
fileEntry->poolRequest(req);
|
|
|
|
|
|
|
|
SharedHandle<Request> req2nd = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), req2nd->getHost());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http"), req2nd->getProtocol());
|
|
|
|
|
|
|
|
SharedHandle<Request> req3rd = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), req3rd->getHost());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("ftp"), req3rd->getProtocol());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileEntryTest::testGetRequest_disableSingleHostMultiConnection()
|
|
|
|
{
|
|
|
|
SharedHandle<FileEntry> fileEntry = createFileEntry();
|
|
|
|
fileEntry->disableSingleHostMultiConnection();
|
|
|
|
SharedHandle<InOrderURISelector> selector(new InOrderURISelector());
|
|
|
|
SharedHandle<Request> req = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), req->getHost());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http"), req->getProtocol());
|
|
|
|
|
|
|
|
SharedHandle<Request> req2nd = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("mirror"), req2nd->getHost());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http"), req2nd->getProtocol());
|
|
|
|
|
|
|
|
SharedHandle<Request> req3rd = fileEntry->getRequest(selector);
|
|
|
|
CPPUNIT_ASSERT(req3rd.isNull());
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|