12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457 |
- #include "RpcMethod.h"
- #include <cppunit/extensions/HelperMacros.h>
- #include "DownloadEngine.h"
- #include "SelectEventPoll.h"
- #include "Option.h"
- #include "RequestGroupMan.h"
- #include "RequestGroup.h"
- #include "RpcMethodImpl.h"
- #include "OptionParser.h"
- #include "OptionHandler.h"
- #include "RpcRequest.h"
- #include "RpcResponse.h"
- #include "prefs.h"
- #include "TestUtil.h"
- #include "DownloadContext.h"
- #include "FeatureConfig.h"
- #include "util.h"
- #include "array_fun.h"
- #include "download_helper.h"
- #include "FileEntry.h"
- #include "RpcMethodFactory.h"
- #ifdef ENABLE_BITTORRENT
- #include "BtRegistry.h"
- #include "BtRuntime.h"
- #include "bittorrent_helper.h"
- #endif // ENABLE_BITTORRENT
- namespace aria2 {
- namespace rpc {
- class RpcMethodTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(RpcMethodTest);
- CPPUNIT_TEST(testAuthorize);
- CPPUNIT_TEST(testAddUri);
- CPPUNIT_TEST(testAddUri_withoutUri);
- CPPUNIT_TEST(testAddUri_notUri);
- CPPUNIT_TEST(testAddUri_withBadOption);
- CPPUNIT_TEST(testAddUri_withPosition);
- CPPUNIT_TEST(testAddUri_withBadPosition);
- #ifdef ENABLE_BITTORRENT
- CPPUNIT_TEST(testAddTorrent);
- CPPUNIT_TEST(testAddTorrent_withoutTorrent);
- CPPUNIT_TEST(testAddTorrent_notBase64Torrent);
- CPPUNIT_TEST(testAddTorrent_withPosition);
- #endif // ENABLE_BITTORRENT
- #ifdef ENABLE_METALINK
- CPPUNIT_TEST(testAddMetalink);
- CPPUNIT_TEST(testAddMetalink_withoutMetalink);
- CPPUNIT_TEST(testAddMetalink_notBase64Metalink);
- CPPUNIT_TEST(testAddMetalink_withPosition);
- #endif // ENABLE_METALINK
- CPPUNIT_TEST(testGetOption);
- CPPUNIT_TEST(testChangeOption);
- CPPUNIT_TEST(testChangeOption_withBadOption);
- CPPUNIT_TEST(testChangeOption_withNotAllowedOption);
- CPPUNIT_TEST(testChangeOption_withoutGid);
- CPPUNIT_TEST(testChangeGlobalOption);
- CPPUNIT_TEST(testChangeGlobalOption_withBadOption);
- CPPUNIT_TEST(testChangeGlobalOption_withNotAllowedOption);
- CPPUNIT_TEST(testTellStatus_withoutGid);
- CPPUNIT_TEST(testTellWaiting);
- CPPUNIT_TEST(testTellWaiting_fail);
- CPPUNIT_TEST(testGetVersion);
- CPPUNIT_TEST(testNoSuchMethod);
- CPPUNIT_TEST(testGatherStoppedDownload);
- #ifdef ENABLE_BITTORRENT
- CPPUNIT_TEST(testGatherStoppedDownload_bt);
- #endif // ENABLE_BITTORRENT
- CPPUNIT_TEST(testGatherProgressCommon);
- #ifdef ENABLE_BITTORRENT
- CPPUNIT_TEST(testGatherBitTorrentMetadata);
- #endif // ENABLE_BITTORRENT
- CPPUNIT_TEST(testChangePosition);
- CPPUNIT_TEST(testChangePosition_fail);
- CPPUNIT_TEST(testGetSessionInfo);
- CPPUNIT_TEST(testChangeUri);
- CPPUNIT_TEST(testChangeUri_fail);
- CPPUNIT_TEST(testPause);
- CPPUNIT_TEST(testSystemMulticall);
- CPPUNIT_TEST(testSystemMulticall_fail);
- CPPUNIT_TEST(testSystemListMethods);
- CPPUNIT_TEST(testSystemListNotifications);
- CPPUNIT_TEST_SUITE_END();
- private:
- std::shared_ptr<DownloadEngine> e_;
- std::shared_ptr<Option> option_;
- public:
- void setUp()
- {
- option_ = std::make_shared<Option>();
- option_->put(PREF_DIR, A2_TEST_OUT_DIR "/aria2_RpcMethodTest");
- option_->put(PREF_PIECE_LENGTH, "1048576");
- option_->put(PREF_MAX_DOWNLOAD_RESULT, "10");
- File(option_->get(PREF_DIR)).mkdirs();
- e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
- e_->setOption(option_.get());
- e_->setRequestGroupMan(make_unique<RequestGroupMan>(
- std::vector<std::shared_ptr<RequestGroup>>{}, 1, option_.get()));
- }
- void testAuthorize();
- void testAddUri();
- void testAddUri_withoutUri();
- void testAddUri_notUri();
- void testAddUri_withBadOption();
- void testAddUri_withPosition();
- void testAddUri_withBadPosition();
- #ifdef ENABLE_BITTORRENT
- void testAddTorrent();
- void testAddTorrent_withoutTorrent();
- void testAddTorrent_notBase64Torrent();
- void testAddTorrent_withPosition();
- #endif // ENABLE_BITTORRENT
- #ifdef ENABLE_METALINK
- void testAddMetalink();
- void testAddMetalink_withoutMetalink();
- void testAddMetalink_notBase64Metalink();
- void testAddMetalink_withPosition();
- #endif // ENABLE_METALINK
- void testGetOption();
- void testChangeOption();
- void testChangeOption_withBadOption();
- void testChangeOption_withNotAllowedOption();
- void testChangeOption_withoutGid();
- void testChangeGlobalOption();
- void testChangeGlobalOption_withBadOption();
- void testChangeGlobalOption_withNotAllowedOption();
- void testTellStatus_withoutGid();
- void testTellWaiting();
- void testTellWaiting_fail();
- void testGetVersion();
- void testNoSuchMethod();
- void testGatherStoppedDownload();
- #ifdef ENABLE_BITTORRENT
- void testGatherStoppedDownload_bt();
- #endif // ENABLE_BITTORRENT
- void testGatherProgressCommon();
- #ifdef ENABLE_BITTORRENT
- void testGatherBitTorrentMetadata();
- #endif // ENABLE_BITTORRENT
- void testChangePosition();
- void testChangePosition_fail();
- void testGetSessionInfo();
- void testChangeUri();
- void testChangeUri_fail();
- void testPause();
- void testSystemMulticall();
- void testSystemMulticall_fail();
- void testSystemListMethods();
- void testSystemListNotifications();
- };
- CPPUNIT_TEST_SUITE_REGISTRATION(RpcMethodTest);
- namespace {
- std::string getString(const Dict* dict, const std::string& key)
- {
- return downcast<String>(dict->get(key))->s();
- }
- } // namespace
- namespace {
- RpcRequest createReq(std::string methodName)
- {
- return {std::move(methodName), List::g()};
- }
- } // namespace
- void RpcMethodTest::testAuthorize()
- {
- // Select RPC method which takes non-string parameter to make sure
- // that token: prefixed parameter is stripped before the call.
- TellActiveRpcMethod m;
- // no secret token set and no token: prefixed parameter is given
- {
- auto req = createReq(TellActiveRpcMethod::getMethodName());
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- // no secret token set and token: prefixed parameter is given
- {
- auto req = createReq(GetVersionRpcMethod::getMethodName());
- req.params->append("token:foo");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- e_->getOption()->put(PREF_RPC_SECRET, "foo");
- // secret token set and no token: prefixed parameter is given
- {
- auto req = createReq(GetVersionRpcMethod::getMethodName());
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- // secret token set and token: prefixed parameter is given
- {
- auto req = createReq(GetVersionRpcMethod::getMethodName());
- req.params->append("token:foo");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- // secret token set and bad token: prefixed parameter is given
- {
- auto req = createReq(GetVersionRpcMethod::getMethodName());
- req.params->append("token:foo2");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- }
- void RpcMethodTest::testAddUri()
- {
- AddUriRpcMethod m;
- {
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append("http://localhost/");
- req.params->append(std::move(urisParam));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const RequestGroupList& rgs = e_->getRequestGroupMan()->getReservedGroups();
- CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size());
- CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"),
- (*rgs.begin())
- ->getDownloadContext()
- ->getFirstFileEntry()
- ->getRemainingUris()
- .front());
- }
- {
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append("http://localhost/");
- req.params->append(std::move(urisParam));
- // with options
- auto opt = Dict::g();
- opt->put(PREF_DIR->k, "/sink");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- a2_gid_t gid;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(gid, downcast<String>(res.param)->s().c_str()));
- CPPUNIT_ASSERT_EQUAL(std::string("/sink"),
- findReservedGroup(e_->getRequestGroupMan().get(), gid)
- ->getOption()
- ->get(PREF_DIR));
- }
- }
- void RpcMethodTest::testAddUri_withoutUri()
- {
- AddUriRpcMethod m;
- auto res = m.execute(createReq(AddUriRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddUri_notUri()
- {
- AddUriRpcMethod m;
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append("not uri");
- req.params->append(std::move(urisParam));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddUri_withBadOption()
- {
- AddUriRpcMethod m;
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append("http://localhost");
- req.params->append(std::move(urisParam));
- auto opt = Dict::g();
- opt->put(PREF_FILE_ALLOCATION->k, "badvalue");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddUri_withPosition()
- {
- AddUriRpcMethod m;
- auto req1 = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam1 = List::g();
- urisParam1->append("http://uri1");
- req1.params->append(std::move(urisParam1));
- auto res1 = m.execute(std::move(req1), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res1.code);
- auto req2 = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam2 = List::g();
- urisParam2->append("http://uri2");
- req2.params->append(std::move(urisParam2));
- req2.params->append(Dict::g());
- req2.params->append(Integer::g(0));
- m.execute(std::move(req2), e_.get());
- std::string uri = getReservedGroup(e_->getRequestGroupMan().get(), 0)
- ->getDownloadContext()
- ->getFirstFileEntry()
- ->getRemainingUris()[0];
- CPPUNIT_ASSERT_EQUAL(std::string("http://uri2"), uri);
- }
- void RpcMethodTest::testAddUri_withBadPosition()
- {
- AddUriRpcMethod m;
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append("http://localhost/");
- req.params->append(std::move(urisParam));
- req.params->append(Dict::g());
- req.params->append(Integer::g(-1));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- #ifdef ENABLE_BITTORRENT
- namespace {
- RpcRequest createAddTorrentReq()
- {
- auto req = createReq(AddTorrentRpcMethod::getMethodName());
- req.params->append(readFile(A2_TEST_DIR "/single.torrent"));
- auto uris = List::g();
- uris->append("http://localhost/aria2-0.8.2.tar.bz2");
- req.params->append(std::move(uris));
- return req;
- }
- } // namespace
- void RpcMethodTest::testAddTorrent()
- {
- File(e_->getOption()->get(PREF_DIR) +
- "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent")
- .remove();
- AddTorrentRpcMethod m;
- {
- // Saving upload metadata is disabled by option.
- auto res = m.execute(createAddTorrentReq(), e_.get());
- CPPUNIT_ASSERT(!File(e_->getOption()->get(PREF_DIR) +
- "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent")
- .exists());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(sizeof(a2_gid_t) * 2,
- downcast<String>(res.param)->s().size());
- }
- e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
- {
- auto res = m.execute(createAddTorrentReq(), e_.get());
- CPPUNIT_ASSERT(File(e_->getOption()->get(PREF_DIR) +
- "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent")
- .exists());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- a2_gid_t gid;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(gid, downcast<String>(res.param)->s().c_str()));
- auto group = findReservedGroup(e_->getRequestGroupMan().get(), gid);
- CPPUNIT_ASSERT(group);
- CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR) +
- "/aria2-0.8.2.tar.bz2",
- group->getFirstFilePath());
- CPPUNIT_ASSERT_EQUAL((size_t)1, group->getDownloadContext()
- ->getFirstFileEntry()
- ->getRemainingUris()
- .size());
- CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/aria2-0.8.2.tar.bz2"),
- group->getDownloadContext()
- ->getFirstFileEntry()
- ->getRemainingUris()[0]);
- }
- {
- auto req = createAddTorrentReq();
- // with options
- std::string dir = A2_TEST_OUT_DIR "/aria2_RpcMethodTest_testAddTorrent";
- File(dir).mkdirs();
- auto opt = Dict::g();
- opt->put(PREF_DIR->k, dir);
- File(dir + "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- a2_gid_t gid;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(gid, downcast<String>(res.param)->s().c_str()));
- CPPUNIT_ASSERT_EQUAL(dir + "/aria2-0.8.2.tar.bz2",
- findReservedGroup(e_->getRequestGroupMan().get(), gid)
- ->getFirstFilePath());
- CPPUNIT_ASSERT(
- File(dir + "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent")
- .exists());
- }
- }
- void RpcMethodTest::testAddTorrent_withoutTorrent()
- {
- AddTorrentRpcMethod m;
- auto res =
- m.execute(createReq(AddTorrentRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddTorrent_notBase64Torrent()
- {
- AddTorrentRpcMethod m;
- auto req = createReq(AddTorrentRpcMethod::getMethodName());
- req.params->append("not torrent");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddTorrent_withPosition()
- {
- AddTorrentRpcMethod m;
- auto req1 = createReq(AddTorrentRpcMethod::getMethodName());
- req1.params->append(readFile(A2_TEST_DIR "/test.torrent"));
- req1.params->append(List::g());
- req1.params->append(Dict::g());
- auto res1 = m.execute(std::move(req1), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res1.code);
- auto req2 = createReq(AddTorrentRpcMethod::getMethodName());
- req2.params->append(readFile(A2_TEST_DIR "/single.torrent"));
- req2.params->append(List::g());
- req2.params->append(Dict::g());
- req2.params->append(Integer::g(0));
- m.execute(std::move(req2), e_.get());
- CPPUNIT_ASSERT_EQUAL((size_t)1,
- getReservedGroup(e_->getRequestGroupMan().get(), 0)
- ->getDownloadContext()
- ->getFileEntries()
- .size());
- }
- #endif // ENABLE_BITTORRENT
- #ifdef ENABLE_METALINK
- namespace {
- RpcRequest createAddMetalinkReq()
- {
- auto req = createReq(AddMetalinkRpcMethod::getMethodName());
- req.params->append(readFile(A2_TEST_DIR "/2files.metalink"));
- return req;
- }
- } // namespace
- void RpcMethodTest::testAddMetalink()
- {
- File(e_->getOption()->get(PREF_DIR) +
- "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4")
- .remove();
- AddMetalinkRpcMethod m;
- {
- // Saving upload metadata is disabled by option.
- auto res = m.execute(createAddMetalinkReq(), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const List* resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
- a2_gid_t gid1, gid2;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(
- gid1, downcast<String>(resParams->get(0))->s().c_str()));
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(
- gid2, downcast<String>(resParams->get(1))->s().c_str()));
- CPPUNIT_ASSERT(!File(e_->getOption()->get(PREF_DIR) +
- "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4")
- .exists());
- }
- e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
- {
- auto res = m.execute(createAddMetalinkReq(), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const List* resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
- a2_gid_t gid3, gid4;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(
- gid3, downcast<String>(resParams->get(0))->s().c_str()));
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(
- gid4, downcast<String>(resParams->get(1))->s().c_str()));
- CPPUNIT_ASSERT(File(e_->getOption()->get(PREF_DIR) +
- "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4")
- .exists());
- auto tar = findReservedGroup(e_->getRequestGroupMan().get(), gid3);
- CPPUNIT_ASSERT(tar);
- CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR) +
- "/aria2-5.0.0.tar.bz2",
- tar->getFirstFilePath());
- auto deb = findReservedGroup(e_->getRequestGroupMan().get(), gid4);
- CPPUNIT_ASSERT(deb);
- CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR) + "/aria2-5.0.0.deb",
- deb->getFirstFilePath());
- }
- {
- auto req = createAddMetalinkReq();
- // with options
- std::string dir = A2_TEST_OUT_DIR "/aria2_RpcMethodTest_testAddMetalink";
- File(dir).mkdirs();
- auto opt = Dict::g();
- opt->put(PREF_DIR->k, dir);
- File(dir + "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const List* resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
- a2_gid_t gid5;
- CPPUNIT_ASSERT_EQUAL(
- 0, GroupId::toNumericId(
- gid5, downcast<String>(resParams->get(0))->s().c_str()));
- CPPUNIT_ASSERT_EQUAL(dir + "/aria2-5.0.0.tar.bz2",
- findReservedGroup(e_->getRequestGroupMan().get(), gid5)
- ->getFirstFilePath());
- CPPUNIT_ASSERT(
- File(dir + "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
- }
- }
- void RpcMethodTest::testAddMetalink_withoutMetalink()
- {
- AddMetalinkRpcMethod m;
- auto res =
- m.execute(createReq(AddMetalinkRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddMetalink_notBase64Metalink()
- {
- AddMetalinkRpcMethod m;
- auto req = createReq(AddMetalinkRpcMethod::getMethodName());
- req.params->append("not metalink");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testAddMetalink_withPosition()
- {
- AddUriRpcMethod m1;
- auto req1 = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam1 = List::g();
- urisParam1->append("http://uri");
- req1.params->append(std::move(urisParam1));
- auto res1 = m1.execute(std::move(req1), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res1.code);
- AddMetalinkRpcMethod m2;
- auto req2 = createReq(AddMetalinkRpcMethod::getMethodName());
- req2.params->append(readFile(A2_TEST_DIR "/2files.metalink"));
- req2.params->append(Dict::g());
- req2.params->append(Integer::g(0));
- auto res2 = m2.execute(std::move(req2), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res2.code);
- CPPUNIT_ASSERT_EQUAL(
- e_->getOption()->get(PREF_DIR) + "/aria2-5.0.0.tar.bz2",
- getReservedGroup(e_->getRequestGroupMan().get(), 0)->getFirstFilePath());
- }
- #endif // ENABLE_METALINK
- void RpcMethodTest::testGetOption()
- {
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- group->getOption()->put(PREF_DIR, "alpha");
- e_->getRequestGroupMan()->addReservedGroup(group);
- auto dr = createDownloadResult(error_code::FINISHED, "http://host/fin");
- dr->option->put(PREF_DIR, "bravo");
- e_->getRequestGroupMan()->addDownloadResult(dr);
- GetOptionRpcMethod m;
- auto req = createReq(GetOptionRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(group->getGID()));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const Dict* resopt = downcast<Dict>(res.param);
- CPPUNIT_ASSERT_EQUAL(std::string("alpha"),
- downcast<String>(resopt->get(PREF_DIR->k))->s());
- req = createReq(GetOptionRpcMethod::getMethodName());
- req.params->append(dr->gid->toHex());
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resopt = downcast<Dict>(res.param);
- CPPUNIT_ASSERT_EQUAL(std::string("bravo"),
- downcast<String>(resopt->get(PREF_DIR->k))->s());
- // Invalid GID
- req = createReq(GetOptionRpcMethod::getMethodName());
- req.params->append(GroupId::create()->toHex());
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testChangeOption()
- {
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- e_->getRequestGroupMan()->addReservedGroup(group);
- ChangeOptionRpcMethod m;
- auto req = createReq(ChangeOptionRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(group->getGID()));
- auto opt = Dict::g();
- opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "100K");
- #ifdef ENABLE_BITTORRENT
- opt->put(PREF_BT_MAX_PEERS->k, "100");
- opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K");
- opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K");
- {
- auto btObject = make_unique<BtObject>();
- btObject->btRuntime = std::make_shared<BtRuntime>();
- e_->getBtRegistry()->put(group->getGID(), std::move(btObject));
- }
- #endif // ENABLE_BITTORRENT
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- auto option = group->getOption();
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL((int)100_k, group->getMaxDownloadSpeedLimit());
- CPPUNIT_ASSERT_EQUAL(std::string("102400"),
- option->get(PREF_MAX_DOWNLOAD_LIMIT));
- #ifdef ENABLE_BITTORRENT
- CPPUNIT_ASSERT_EQUAL(std::string("307200"),
- option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
- CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
- CPPUNIT_ASSERT_EQUAL(
- 100, e_->getBtRegistry()->get(group->getGID())->btRuntime->getMaxPeers());
- CPPUNIT_ASSERT_EQUAL((int)50_k, group->getMaxUploadSpeedLimit());
- CPPUNIT_ASSERT_EQUAL(std::string("51200"),
- option->get(PREF_MAX_UPLOAD_LIMIT));
- #endif // ENABLE_BITTORRENT
- }
- void RpcMethodTest::testChangeOption_withBadOption()
- {
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- e_->getRequestGroupMan()->addReservedGroup(group);
- ChangeOptionRpcMethod m;
- auto req = createReq(ChangeOptionRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(group->getGID()));
- auto opt = Dict::g();
- opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "badvalue");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testChangeOption_withNotAllowedOption()
- {
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- e_->getRequestGroupMan()->addReservedGroup(group);
- ChangeOptionRpcMethod m;
- auto req = createReq(ChangeOptionRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(group->getGID()));
- auto opt = Dict::g();
- opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- // The unacceptable options are just ignored.
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- void RpcMethodTest::testChangeOption_withoutGid()
- {
- ChangeOptionRpcMethod m;
- auto res =
- m.execute(createReq(ChangeOptionRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testChangeGlobalOption()
- {
- ChangeGlobalOptionRpcMethod m;
- auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
- auto opt = Dict::g();
- opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
- #ifdef ENABLE_BITTORRENT
- opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT->k, "50K");
- #endif // ENABLE_BITTORRENT
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(
- (int)100_k, e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit());
- CPPUNIT_ASSERT_EQUAL(std::string("102400"),
- e_->getOption()->get(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
- #ifdef ENABLE_BITTORRENT
- CPPUNIT_ASSERT_EQUAL(
- (int)50_k, e_->getRequestGroupMan()->getMaxOverallUploadSpeedLimit());
- CPPUNIT_ASSERT_EQUAL(std::string("51200"),
- e_->getOption()->get(PREF_MAX_OVERALL_UPLOAD_LIMIT));
- #endif // ENABLE_BITTORRENT
- }
- void RpcMethodTest::testChangeGlobalOption_withBadOption()
- {
- ChangeGlobalOptionRpcMethod m;
- auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
- auto opt = Dict::g();
- opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "badvalue");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
- {
- ChangeGlobalOptionRpcMethod m;
- auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
- auto opt = Dict::g();
- opt->put(PREF_ENABLE_RPC->k, "100K");
- req.params->append(std::move(opt));
- auto res = m.execute(std::move(req), e_.get());
- // The unacceptable options are just ignored.
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- void RpcMethodTest::testNoSuchMethod()
- {
- NoSuchMethodRpcMethod m;
- auto res = m.execute(createReq("make.hamburger"), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
- getString(downcast<Dict>(res.param), "faultString"));
- }
- void RpcMethodTest::testTellStatus_withoutGid()
- {
- TellStatusRpcMethod m;
- auto res =
- m.execute(createReq(TellStatusRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- namespace {
- void addUri(const std::string& uri, const std::shared_ptr<DownloadEngine>& e)
- {
- AddUriRpcMethod m;
- auto req = createReq(AddUriRpcMethod::getMethodName());
- auto urisParam = List::g();
- urisParam->append(uri);
- req.params->append(std::move(urisParam));
- CPPUNIT_ASSERT_EQUAL(0, m.execute(std::move(req), e.get()).code);
- }
- } // namespace
- #ifdef ENABLE_BITTORRENT
- namespace {
- void addTorrent(const std::string& torrentFile,
- const std::shared_ptr<DownloadEngine>& e)
- {
- AddTorrentRpcMethod m;
- auto req = createReq(AddTorrentRpcMethod::getMethodName());
- req.params->append(readFile(torrentFile));
- auto res = m.execute(std::move(req), e.get());
- }
- } // namespace
- #endif // ENABLE_BITTORRENT
- void RpcMethodTest::testTellWaiting()
- {
- addUri("http://1/", e_);
- addUri("http://2/", e_);
- addUri("http://3/", e_);
- #ifdef ENABLE_BITTORRENT
- addTorrent(A2_TEST_DIR "/single.torrent", e_);
- #else // !ENABLE_BITTORRENT
- addUri("http://4/", e_);
- #endif // !ENABLE_BITTORRENT
- auto& rgman = e_->getRequestGroupMan();
- TellWaitingRpcMethod m;
- auto req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(1));
- req.params->append(Integer::g(2));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const List* resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 1)->getGID()),
- getString(downcast<Dict>(resParams->get(0)), "gid"));
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 2)->getGID()),
- getString(downcast<Dict>(resParams->get(1)), "gid"));
- // waiting.size() == offset+num
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(1));
- req.params->append(Integer::g(3));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
- // waiting.size() < offset+num
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(1));
- req.params->append(Integer::g(4));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
- // offset = INT32_MAX
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(INT32_MAX));
- req.params->append(Integer::g(1));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
- // num = INT32_MAX
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(1));
- req.params->append(Integer::g(INT32_MAX));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
- // offset=INT32_MAX and num = INT32_MAX
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(INT32_MAX));
- req.params->append(Integer::g(INT32_MAX));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
- // offset=INT32_MIN and num = INT32_MAX
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(INT32_MIN));
- req.params->append(Integer::g(INT32_MAX));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
- // negative offset
- req = createReq(TellWaitingRpcMethod::getMethodName());
- req.params->append(Integer::g(-1));
- req.params->append(Integer::g(2));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 3)->getGID()),
- getString(downcast<Dict>(resParams->get(0)), "gid"));
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 2)->getGID()),
- getString(downcast<Dict>(resParams->get(1)), "gid"));
- // negative offset and size < num
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
- req.params->append(Integer::g(-1));
- req.params->append(Integer::g(100));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)4, resParams->size());
- // nagative offset and normalized offset < 0
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
- req.params->append(Integer::g(-5));
- req.params->append(Integer::g(100));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
- // nagative offset and normalized offset == 0
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
- req.params->append(Integer::g(-4));
- req.params->append(Integer::g(100));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)1, resParams->size());
- }
- void RpcMethodTest::testTellWaiting_fail()
- {
- TellWaitingRpcMethod m;
- auto res =
- m.execute(createReq(TellWaitingRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testGetVersion()
- {
- GetVersionRpcMethod m;
- auto res =
- m.execute(createReq(GetVersionRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const Dict* resParams = downcast<Dict>(res.param);
- CPPUNIT_ASSERT_EQUAL(std::string(PACKAGE_VERSION),
- getString(resParams, "version"));
- const List* featureList = downcast<List>(resParams->get("enabledFeatures"));
- std::string features;
- for (auto i = featureList->begin(); i != featureList->end(); ++i) {
- const String* s = downcast<String>(*i);
- features += s->s();
- features += ", ";
- }
- CPPUNIT_ASSERT_EQUAL(featureSummary() + ", ", features);
- }
- void RpcMethodTest::testGatherStoppedDownload()
- {
- std::vector<std::shared_ptr<FileEntry>> fileEntries;
- std::vector<a2_gid_t> followedBy;
- followedBy.push_back(3);
- followedBy.push_back(4);
- auto d = std::make_shared<DownloadResult>();
- d->gid = GroupId::create();
- d->fileEntries = fileEntries;
- d->inMemoryDownload = false;
- d->sessionDownloadLength = UINT64_MAX;
- d->sessionTime = 1_s;
- d->result = error_code::FINISHED;
- d->followedBy = followedBy;
- d->following = 1;
- d->belongsTo = 2;
- auto entry = Dict::g();
- std::vector<std::string> keys;
- gatherStoppedDownload(entry.get(), d, keys);
- const List* followedByRes = downcast<List>(entry->get("followedBy"));
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(3),
- downcast<String>(followedByRes->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(4),
- downcast<String>(followedByRes->get(1))->s());
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(1),
- downcast<String>(entry->get("following"))->s());
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(2),
- downcast<String>(entry->get("belongsTo"))->s());
- keys.push_back("gid");
- entry = Dict::g();
- gatherStoppedDownload(entry.get(), d, keys);
- CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
- CPPUNIT_ASSERT(entry->containsKey("gid"));
- }
- #ifdef ENABLE_BITTORRENT
- void RpcMethodTest::testGatherStoppedDownload_bt()
- {
- auto d = std::make_shared<DownloadResult>();
- d->gid = GroupId::create();
- d->infoHash = "2089b05ecca3d829cee5497d2703803b52216d19";
- d->attrs = std::vector<std::shared_ptr<ContextAttribute>>(MAX_CTX_ATTR);
- auto torrentAttr = std::make_shared<TorrentAttribute>();
- torrentAttr->creationDate = 1000000007;
- d->attrs[CTX_ATTR_BT] = torrentAttr;
- auto entry = Dict::g();
- gatherStoppedDownload(entry.get(), d, {});
- auto btDict = downcast<Dict>(entry->get("bittorrent"));
- CPPUNIT_ASSERT(btDict);
- CPPUNIT_ASSERT_EQUAL((int64_t)1000000007,
- downcast<Integer>(btDict->get("creationDate"))->i());
- }
- #endif // ENABLE_BITTORRENT
- void RpcMethodTest::testGatherProgressCommon()
- {
- auto dctx = std::make_shared<DownloadContext>(0, 0, "aria2.tar.bz2");
- std::string uris[] = {"http://localhost/aria2.tar.bz2"};
- dctx->getFirstFileEntry()->addUris(std::begin(uris), std::end(uris));
- auto group =
- std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_));
- group->setDownloadContext(dctx);
- std::vector<std::shared_ptr<RequestGroup>> followedBy;
- for (int i = 0; i < 2; ++i) {
- followedBy.push_back(
- std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
- }
- group->followedBy(followedBy.begin(), followedBy.end());
- auto leader = GroupId::create();
- group->following(leader->getNumericId());
- auto parent = GroupId::create();
- group->belongsTo(parent->getNumericId());
- auto entry = Dict::g();
- std::vector<std::string> keys;
- gatherProgressCommon(entry.get(), group, keys);
- const List* followedByRes = downcast<List>(entry->get("followedBy"));
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[0]->getGID()),
- downcast<String>(followedByRes->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[1]->getGID()),
- downcast<String>(followedByRes->get(1))->s());
- CPPUNIT_ASSERT_EQUAL(leader->toHex(),
- downcast<String>(entry->get("following"))->s());
- CPPUNIT_ASSERT_EQUAL(parent->toHex(),
- downcast<String>(entry->get("belongsTo"))->s());
- const List* files = downcast<List>(entry->get("files"));
- CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
- const Dict* file = downcast<Dict>(files->get(0));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
- downcast<String>(file->get("path"))->s());
- CPPUNIT_ASSERT_EQUAL(
- uris[0],
- downcast<String>(
- downcast<Dict>(downcast<List>(file->get("uris"))->get(0))->get("uri"))
- ->s());
- CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR),
- downcast<String>(entry->get("dir"))->s());
- keys.push_back("gid");
- entry = Dict::g();
- gatherProgressCommon(entry.get(), group, keys);
- CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
- CPPUNIT_ASSERT(entry->containsKey("gid"));
- }
- #ifdef ENABLE_BITTORRENT
- void RpcMethodTest::testGatherBitTorrentMetadata()
- {
- auto option = std::make_shared<Option>();
- option->put(PREF_DIR, ".");
- auto dctx = std::make_shared<DownloadContext>();
- bittorrent::load(A2_TEST_DIR "/test.torrent", dctx, option);
- auto btDict = Dict::g();
- gatherBitTorrentMetadata(btDict.get(), bittorrent::getTorrentAttrs(dctx));
- CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
- downcast<String>(btDict->get("comment"))->s());
- CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
- downcast<Integer>(btDict->get("creationDate"))->i());
- CPPUNIT_ASSERT_EQUAL(std::string("multi"),
- downcast<String>(btDict->get("mode"))->s());
- CPPUNIT_ASSERT_EQUAL(
- std::string("aria2-test"),
- downcast<String>(downcast<Dict>(btDict->get("info"))->get("name"))->s());
- const List* announceList = downcast<List>(btDict->get("announceList"));
- CPPUNIT_ASSERT_EQUAL((size_t)3, announceList->size());
- CPPUNIT_ASSERT_EQUAL(
- std::string("http://tracker1"),
- downcast<String>(downcast<List>(announceList->get(0))->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(
- std::string("http://tracker2"),
- downcast<String>(downcast<List>(announceList->get(1))->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(
- std::string("http://tracker3"),
- downcast<String>(downcast<List>(announceList->get(2))->get(0))->s());
- // Remove some keys
- auto modBtAttrs = bittorrent::getTorrentAttrs(dctx);
- modBtAttrs->comment.clear();
- modBtAttrs->creationDate = 0;
- modBtAttrs->mode = BT_FILE_MODE_NONE;
- modBtAttrs->metadata.clear();
- btDict = Dict::g();
- gatherBitTorrentMetadata(btDict.get(), modBtAttrs);
- CPPUNIT_ASSERT(!btDict->containsKey("comment"));
- CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
- CPPUNIT_ASSERT(!btDict->containsKey("mode"));
- CPPUNIT_ASSERT(!btDict->containsKey("info"));
- CPPUNIT_ASSERT(btDict->containsKey("announceList"));
- }
- #endif // ENABLE_BITTORRENT
- void RpcMethodTest::testChangePosition()
- {
- e_->getRequestGroupMan()->addReservedGroup(
- std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
- e_->getRequestGroupMan()->addReservedGroup(
- std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
- a2_gid_t gid = getReservedGroup(e_->getRequestGroupMan().get(), 0)->getGID();
- ChangePositionRpcMethod m;
- auto req = createReq(ChangePositionRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(gid));
- req.params->append(Integer::g(1));
- req.params->append("POS_SET");
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
- CPPUNIT_ASSERT_EQUAL(
- gid, getReservedGroup(e_->getRequestGroupMan().get(), 1)->getGID());
- }
- void RpcMethodTest::testChangePosition_fail()
- {
- ChangePositionRpcMethod m;
- auto res =
- m.execute(createReq(ChangePositionRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- auto req = createReq(ChangePositionRpcMethod::getMethodName());
- req.params->append("1");
- req.params->append(Integer::g(2));
- req.params->append("bad keyword");
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- namespace {
- RpcRequest createChangeUriReq(a2_gid_t gid, size_t fileIndex)
- {
- auto req = createReq(ChangeUriRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(gid)); // GID
- req.params->append(Integer::g(fileIndex)); // index of FileEntry
- auto removeuris = List::g();
- removeuris->append("http://example.org/mustremove1");
- removeuris->append("http://example.org/mustremove2");
- removeuris->append("http://example.org/notexist");
- req.params->append(std::move(removeuris));
- return req;
- }
- } // namespace
- void RpcMethodTest::testChangeUri()
- {
- std::shared_ptr<FileEntry> files[3];
- for (int i = 0; i < 3; ++i) {
- files[i].reset(new FileEntry());
- }
- files[1]->addUri("http://example.org/aria2.tar.bz2");
- files[1]->addUri("http://example.org/mustremove1");
- files[1]->addUri("http://example.org/mustremove2");
- auto dctx = std::make_shared<DownloadContext>();
- dctx->setFileEntries(&files[0], &files[3]);
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- group->setDownloadContext(dctx);
- e_->getRequestGroupMan()->addReservedGroup(group);
- ChangeUriRpcMethod m;
- auto req = createChangeUriReq(group->getGID(), 2);
- auto adduris = List::g();
- adduris->append("http://example.org/added1");
- adduris->append("http://example.org/added2");
- adduris->append("baduri");
- adduris->append("http://example.org/added3");
- req.params->append(std::move(adduris));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)3, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
- CPPUNIT_ASSERT_EQUAL((size_t)0, files[0]->getRemainingUris().size());
- CPPUNIT_ASSERT_EQUAL((size_t)0, files[2]->getRemainingUris().size());
- std::deque<std::string> uris = files[1]->getRemainingUris();
- CPPUNIT_ASSERT_EQUAL((size_t)4, uris.size());
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/aria2.tar.bz2"),
- uris[0]);
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1"), uris[1]);
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
- req = createChangeUriReq(group->getGID(), 2);
- // Change adduris
- adduris = List::g();
- adduris->append("http://example.org/added1-1");
- adduris->append("http://example.org/added1-2");
- req.params->append(std::move(adduris));
- // Set position parameter
- req.params->append(Integer::g(2));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
- uris = files[1]->getRemainingUris();
- CPPUNIT_ASSERT_EQUAL((size_t)6, uris.size());
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[2]);
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
- // Change index of FileEntry
- req = createChangeUriReq(group->getGID(), 1);
- adduris = List::g();
- adduris->append("http://example.org/added1-1");
- adduris->append("http://example.org/added1-2");
- req.params->append(std::move(adduris));
- // Set position far beyond the size of uris in FileEntry.
- req.params->append(Integer::g(1000));
- res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
- uris = files[0]->getRemainingUris();
- CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[0]);
- CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
- }
- namespace {
- RpcRequest createChangeUriEmptyReq(a2_gid_t gid, size_t fileIndex)
- {
- auto req = createReq(ChangeUriRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(gid)); // GID
- req.params->append(Integer::g(fileIndex)); // index of FileEntry
- req.params->append(List::g()); // remove uris
- req.params->append(List::g()); // append uris
- return req;
- }
- } // namespace
- void RpcMethodTest::testChangeUri_fail()
- {
- std::shared_ptr<FileEntry> files[3];
- for (int i = 0; i < 3; ++i) {
- files[i] = std::make_shared<FileEntry>();
- }
- auto dctx = std::make_shared<DownloadContext>();
- dctx->setFileEntries(&files[0], &files[3]);
- auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
- group->setDownloadContext(dctx);
- e_->getRequestGroupMan()->addReservedGroup(group);
- ChangeUriRpcMethod m;
- auto req = createChangeUriEmptyReq(group->getGID(), 1);
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- req = createChangeUriEmptyReq(group->getGID(), 0);
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because 2nd argument is less than 1.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- req = createChangeUriEmptyReq(GroupId::create()->getNumericId(), 1);
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because the given GID does not exist.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- req = createChangeUriEmptyReq(group->getGID(), 4);
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because FileEntry#3 does not exist.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- req = createChangeUriEmptyReq(group->getGID(), 1);
- req.params->set(1, String::g("0"));
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because index of FileEntry is string.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- req = createChangeUriEmptyReq(group->getGID(), 1);
- req.params->set(2, String::g("http://url"));
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because 3rd param is not list.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- req = createChangeUriEmptyReq(group->getGID(), 1);
- req.params->set(2, List::g());
- req.params->set(3, String::g("http://url"));
- res = m.execute(std::move(req), e_.get());
- // RPC request fails because 4th param is not list.
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testGetSessionInfo()
- {
- GetSessionInfoRpcMethod m;
- auto res =
- m.execute(createReq(GetSessionInfoRpcMethod::getMethodName()), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
- getString(downcast<Dict>(res.param), "sessionId"));
- }
- void RpcMethodTest::testPause()
- {
- std::vector<std::string> uris{
- "http://url1", "http://url2", "http://url3",
- };
- option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
- std::vector<std::shared_ptr<RequestGroup>> groups;
- createRequestGroupForUri(groups, option_, uris);
- CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
- e_->getRequestGroupMan()->addReservedGroup(groups);
- {
- PauseRpcMethod m;
- auto req = createReq(PauseRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(groups[0]->getGID()));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- CPPUNIT_ASSERT(groups[0]->isPauseRequested());
- {
- UnpauseRpcMethod m;
- auto req = createReq(UnpauseRpcMethod::getMethodName());
- req.params->append(GroupId::toHex(groups[0]->getGID()));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
- {
- PauseAllRpcMethod m;
- auto req = createReq(PauseAllRpcMethod::getMethodName());
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- for (size_t i = 0; i < groups.size(); ++i) {
- CPPUNIT_ASSERT(groups[i]->isPauseRequested());
- }
- {
- UnpauseAllRpcMethod m;
- auto req = createReq(UnpauseAllRpcMethod::getMethodName());
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- for (size_t i = 0; i < groups.size(); ++i) {
- CPPUNIT_ASSERT(!groups[i]->isPauseRequested());
- }
- {
- ForcePauseAllRpcMethod m;
- auto req = createReq(ForcePauseAllRpcMethod::getMethodName());
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- }
- for (size_t i = 0; i < groups.size(); ++i) {
- CPPUNIT_ASSERT(groups[i]->isPauseRequested());
- }
- }
- void RpcMethodTest::testSystemMulticall()
- {
- SystemMulticallRpcMethod m;
- auto req = createReq("system.multicall");
- auto reqparams = List::g();
- for (int i = 0; i < 2; ++i) {
- auto dict = Dict::g();
- dict->put("methodName", AddUriRpcMethod::getMethodName());
- auto params = List::g();
- auto urisParam = List::g();
- urisParam->append("http://localhost/" + util::itos(i));
- params->append(std::move(urisParam));
- dict->put("params", std::move(params));
- reqparams->append(std::move(dict));
- }
- {
- auto dict = Dict::g();
- dict->put("methodName", "not exists");
- dict->put("params", List::g());
- reqparams->append(std::move(dict));
- }
- {
- reqparams->append("not struct");
- }
- {
- auto dict = Dict::g();
- dict->put("methodName", "system.multicall");
- dict->put("params", List::g());
- reqparams->append(std::move(dict));
- }
- {
- // missing params
- auto dict = Dict::g();
- dict->put("methodName", GetVersionRpcMethod::getMethodName());
- reqparams->append(std::move(dict));
- }
- {
- auto dict = Dict::g();
- dict->put("methodName", GetVersionRpcMethod::getMethodName());
- dict->put("params", List::g());
- reqparams->append(std::move(dict));
- }
- req.params->append(std::move(reqparams));
- auto res = m.execute(std::move(req), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const List* resParams = downcast<List>(res.param);
- CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
- auto& rgman = e_->getRequestGroupMan();
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 0)->getGID()),
- downcast<String>(downcast<List>(resParams->get(0))->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(
- GroupId::toHex(getReservedGroup(rgman.get(), 1)->getGID()),
- downcast<String>(downcast<List>(resParams->get(1))->get(0))->s());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)1,
- downcast<Integer>(downcast<Dict>(resParams->get(2))->get("faultCode"))
- ->i());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)1,
- downcast<Integer>(downcast<Dict>(resParams->get(3))->get("faultCode"))
- ->i());
- CPPUNIT_ASSERT_EQUAL(
- (int64_t)1,
- downcast<Integer>(downcast<Dict>(resParams->get(4))->get("faultCode"))
- ->i());
- CPPUNIT_ASSERT(downcast<List>(resParams->get(5)));
- CPPUNIT_ASSERT(downcast<List>(resParams->get(6)));
- }
- void RpcMethodTest::testSystemMulticall_fail()
- {
- SystemMulticallRpcMethod m;
- auto res = m.execute(createReq("system.multicall"), e_.get());
- CPPUNIT_ASSERT_EQUAL(1, res.code);
- }
- void RpcMethodTest::testSystemListMethods()
- {
- SystemListMethodsRpcMethod m;
- auto res = m.execute(createReq("system.listMethods"), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const auto resParams = downcast<List>(res.param);
- auto& allNames = allMethodNames();
- CPPUNIT_ASSERT_EQUAL(allNames.size(), resParams->size());
- for (size_t i = 0; i < allNames.size(); ++i) {
- const auto s = downcast<String>(resParams->get(i));
- CPPUNIT_ASSERT(s);
- CPPUNIT_ASSERT_EQUAL(allNames[i], s->s());
- }
- }
- void RpcMethodTest::testSystemListNotifications()
- {
- SystemListNotificationsRpcMethod m;
- auto res = m.execute(createReq("system.listNotifications"), e_.get());
- CPPUNIT_ASSERT_EQUAL(0, res.code);
- const auto resParams = downcast<List>(res.param);
- auto& allNames = allNotificationsNames();
- CPPUNIT_ASSERT_EQUAL(allNames.size(), resParams->size());
- for (size_t i = 0; i < allNames.size(); ++i) {
- const auto s = downcast<String>(resParams->get(i));
- CPPUNIT_ASSERT(s);
- CPPUNIT_ASSERT_EQUAL(allNames[i], s->s());
- }
- }
- } // namespace rpc
- } // namespace aria2
|