2010-04-08 12:54:14 +00:00
|
|
|
#include "SessionSerializer.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
2011-08-06 15:36:44 +00:00
|
|
|
#include <fstream>
|
2010-04-08 12:54:14 +00:00
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "array_fun.h"
|
|
|
|
#include "download_helper.h"
|
|
|
|
#include "prefs.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "a2functional.h"
|
2011-05-29 11:59:45 +00:00
|
|
|
#include "FileEntry.h"
|
2010-04-08 12:54:14 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class SessionSerializerTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(SessionSerializerTest);
|
|
|
|
CPPUNIT_TEST(testSave);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
public:
|
|
|
|
void testSave();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(SessionSerializerTest);
|
|
|
|
|
2011-05-29 11:59:45 +00:00
|
|
|
namespace {
|
|
|
|
SharedHandle<DownloadResult> createDownloadResult
|
|
|
|
(error_code::Value result, const std::string& uri)
|
|
|
|
{
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
uris.push_back(uri);
|
|
|
|
SharedHandle<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
|
|
|
|
std::vector<SharedHandle<FileEntry> > entries;
|
|
|
|
entries.push_back(entry);
|
|
|
|
SharedHandle<DownloadResult> dr(new DownloadResult());
|
|
|
|
dr->fileEntries = entries;
|
|
|
|
dr->result = result;
|
|
|
|
dr->belongsTo = 0;
|
|
|
|
dr->inMemoryDownload = false;
|
|
|
|
dr->option = SharedHandle<Option>(new Option());
|
|
|
|
return dr;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2010-04-08 12:54:14 +00:00
|
|
|
void SessionSerializerTest::testSave()
|
|
|
|
{
|
|
|
|
#if defined(ENABLE_BITTORRENT) && defined(ENABLE_METALINK)
|
|
|
|
const std::string URIs[] =
|
|
|
|
{ "http://localhost/file",
|
|
|
|
"http://mirror/file",
|
2010-12-02 13:38:36 +00:00
|
|
|
A2_TEST_DIR"/test.torrent",
|
|
|
|
A2_TEST_DIR"/serialize_session.meta4",
|
2010-04-08 12:54:14 +00:00
|
|
|
"magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"};
|
|
|
|
std::vector<std::string> uris(vbegin(URIs), vend(URIs));
|
|
|
|
std::vector<SharedHandle<RequestGroup> > result;
|
|
|
|
SharedHandle<Option> option(new Option());
|
|
|
|
option->put(PREF_DIR, "/tmp");
|
|
|
|
createRequestGroupForUri(result, option, uris);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)5, result.size());
|
2011-05-29 11:59:45 +00:00
|
|
|
option->put(PREF_MAX_DOWNLOAD_RESULT, "10");
|
2010-04-08 12:54:14 +00:00
|
|
|
SharedHandle<RequestGroupMan> rgman
|
|
|
|
(new RequestGroupMan(result, 1, option.get()));
|
|
|
|
SessionSerializer s(rgman);
|
2011-05-29 11:59:45 +00:00
|
|
|
// REMOVED downloads will not be saved.
|
|
|
|
rgman->addDownloadResult
|
|
|
|
(createDownloadResult(error_code::REMOVED, "http://removed"));
|
|
|
|
rgman->addDownloadResult
|
|
|
|
(createDownloadResult(error_code::TIME_OUT, "http://error"));
|
2011-08-06 15:36:44 +00:00
|
|
|
std::string filename = A2_TEST_OUT_DIR"/aria2_SessionSerializerTest_testSave";
|
|
|
|
s.save(filename);
|
|
|
|
std::ifstream ss(filename.c_str(), std::ios::binary);
|
2010-04-08 12:54:14 +00:00
|
|
|
std::string line;
|
|
|
|
std::getline(ss, line);
|
2011-05-29 11:59:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://error\t"), line);
|
|
|
|
std::getline(ss, line);
|
2010-04-08 12:54:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(strconcat(uris[0], "\t", uris[1], "\t"), line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(uris[2], line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(uris[3], line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(uris[4], line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
|
|
|
|
std::getline(ss, line);
|
|
|
|
CPPUNIT_ASSERT(!ss);
|
|
|
|
#endif // defined(ENABLE_BITTORRENT) && defined(ENABLE_METALINK)
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|