RpcMethodTest.cc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. #include "RpcMethod.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "DownloadEngine.h"
  4. #include "SelectEventPoll.h"
  5. #include "Option.h"
  6. #include "RequestGroupMan.h"
  7. #include "RequestGroup.h"
  8. #include "RpcMethodImpl.h"
  9. #include "OptionParser.h"
  10. #include "OptionHandler.h"
  11. #include "RpcRequest.h"
  12. #include "RpcResponse.h"
  13. #include "prefs.h"
  14. #include "TestUtil.h"
  15. #include "DownloadContext.h"
  16. #include "FeatureConfig.h"
  17. #include "util.h"
  18. #include "array_fun.h"
  19. #include "download_helper.h"
  20. #include "FileEntry.h"
  21. #ifdef ENABLE_BITTORRENT
  22. # include "BtRegistry.h"
  23. # include "BtRuntime.h"
  24. # include "bittorrent_helper.h"
  25. #endif // ENABLE_BITTORRENT
  26. namespace aria2 {
  27. namespace rpc {
  28. class RpcMethodTest:public CppUnit::TestFixture {
  29. CPPUNIT_TEST_SUITE(RpcMethodTest);
  30. CPPUNIT_TEST(testAuthorize);
  31. CPPUNIT_TEST(testAddUri);
  32. CPPUNIT_TEST(testAddUri_withoutUri);
  33. CPPUNIT_TEST(testAddUri_notUri);
  34. CPPUNIT_TEST(testAddUri_withBadOption);
  35. CPPUNIT_TEST(testAddUri_withPosition);
  36. CPPUNIT_TEST(testAddUri_withBadPosition);
  37. #ifdef ENABLE_BITTORRENT
  38. CPPUNIT_TEST(testAddTorrent);
  39. CPPUNIT_TEST(testAddTorrent_withoutTorrent);
  40. CPPUNIT_TEST(testAddTorrent_notBase64Torrent);
  41. CPPUNIT_TEST(testAddTorrent_withPosition);
  42. #endif // ENABLE_BITTORRENT
  43. #ifdef ENABLE_METALINK
  44. CPPUNIT_TEST(testAddMetalink);
  45. CPPUNIT_TEST(testAddMetalink_withoutMetalink);
  46. CPPUNIT_TEST(testAddMetalink_notBase64Metalink);
  47. CPPUNIT_TEST(testAddMetalink_withPosition);
  48. #endif // ENABLE_METALINK
  49. CPPUNIT_TEST(testGetOption);
  50. CPPUNIT_TEST(testChangeOption);
  51. CPPUNIT_TEST(testChangeOption_withBadOption);
  52. CPPUNIT_TEST(testChangeOption_withNotAllowedOption);
  53. CPPUNIT_TEST(testChangeOption_withoutGid);
  54. CPPUNIT_TEST(testChangeGlobalOption);
  55. CPPUNIT_TEST(testChangeGlobalOption_withBadOption);
  56. CPPUNIT_TEST(testChangeGlobalOption_withNotAllowedOption);
  57. CPPUNIT_TEST(testTellStatus_withoutGid);
  58. CPPUNIT_TEST(testTellWaiting);
  59. CPPUNIT_TEST(testTellWaiting_fail);
  60. CPPUNIT_TEST(testGetVersion);
  61. CPPUNIT_TEST(testNoSuchMethod);
  62. CPPUNIT_TEST(testGatherStoppedDownload);
  63. CPPUNIT_TEST(testGatherProgressCommon);
  64. #ifdef ENABLE_BITTORRENT
  65. CPPUNIT_TEST(testGatherBitTorrentMetadata);
  66. #endif // ENABLE_BITTORRENT
  67. CPPUNIT_TEST(testChangePosition);
  68. CPPUNIT_TEST(testChangePosition_fail);
  69. CPPUNIT_TEST(testGetSessionInfo);
  70. CPPUNIT_TEST(testChangeUri);
  71. CPPUNIT_TEST(testChangeUri_fail);
  72. CPPUNIT_TEST(testPause);
  73. CPPUNIT_TEST(testSystemMulticall);
  74. CPPUNIT_TEST(testSystemMulticall_fail);
  75. CPPUNIT_TEST_SUITE_END();
  76. private:
  77. std::shared_ptr<DownloadEngine> e_;
  78. std::shared_ptr<Option> option_;
  79. public:
  80. void setUp()
  81. {
  82. option_ = std::make_shared<Option>();
  83. option_->put(PREF_DIR, A2_TEST_OUT_DIR"/aria2_RpcMethodTest");
  84. option_->put(PREF_PIECE_LENGTH, "1048576");
  85. option_->put(PREF_MAX_DOWNLOAD_RESULT, "10");
  86. File(option_->get(PREF_DIR)).mkdirs();
  87. e_ = make_unique<DownloadEngine>(make_unique<SelectEventPoll>());
  88. e_->setOption(option_.get());
  89. e_->setRequestGroupMan
  90. (make_unique<RequestGroupMan>
  91. (std::vector<std::shared_ptr<RequestGroup>>{}, 1, option_.get()));
  92. }
  93. void testAuthorize();
  94. void testAddUri();
  95. void testAddUri_withoutUri();
  96. void testAddUri_notUri();
  97. void testAddUri_withBadOption();
  98. void testAddUri_withPosition();
  99. void testAddUri_withBadPosition();
  100. #ifdef ENABLE_BITTORRENT
  101. void testAddTorrent();
  102. void testAddTorrent_withoutTorrent();
  103. void testAddTorrent_notBase64Torrent();
  104. void testAddTorrent_withPosition();
  105. #endif // ENABLE_BITTORRENT
  106. #ifdef ENABLE_METALINK
  107. void testAddMetalink();
  108. void testAddMetalink_withoutMetalink();
  109. void testAddMetalink_notBase64Metalink();
  110. void testAddMetalink_withPosition();
  111. #endif // ENABLE_METALINK
  112. void testGetOption();
  113. void testChangeOption();
  114. void testChangeOption_withBadOption();
  115. void testChangeOption_withNotAllowedOption();
  116. void testChangeOption_withoutGid();
  117. void testChangeGlobalOption();
  118. void testChangeGlobalOption_withBadOption();
  119. void testChangeGlobalOption_withNotAllowedOption();
  120. void testTellStatus_withoutGid();
  121. void testTellWaiting();
  122. void testTellWaiting_fail();
  123. void testGetVersion();
  124. void testNoSuchMethod();
  125. void testGatherStoppedDownload();
  126. void testGatherProgressCommon();
  127. #ifdef ENABLE_BITTORRENT
  128. void testGatherBitTorrentMetadata();
  129. #endif // ENABLE_BITTORRENT
  130. void testChangePosition();
  131. void testChangePosition_fail();
  132. void testGetSessionInfo();
  133. void testChangeUri();
  134. void testChangeUri_fail();
  135. void testPause();
  136. void testSystemMulticall();
  137. void testSystemMulticall_fail();
  138. };
  139. CPPUNIT_TEST_SUITE_REGISTRATION(RpcMethodTest);
  140. namespace {
  141. std::string getString(const Dict* dict, const std::string& key)
  142. {
  143. return downcast<String>(dict->get(key))->s();
  144. }
  145. } // namespace
  146. namespace {
  147. RpcRequest createReq(std::string methodName)
  148. {
  149. return {std::move(methodName), List::g()};
  150. }
  151. } // namespace
  152. void RpcMethodTest::testAuthorize()
  153. {
  154. // Select RPC method which takes non-string parameter to make sure
  155. // that token: prefixed parameter is stripped before the call.
  156. TellActiveRpcMethod m;
  157. // no secret token set and no token: prefixed parameter is given
  158. {
  159. auto req = createReq(TellActiveRpcMethod::getMethodName());
  160. auto res = m.execute(std::move(req), e_.get());
  161. CPPUNIT_ASSERT_EQUAL(0, res.code);
  162. }
  163. // no secret token set and token: prefixed parameter is given
  164. {
  165. auto req = createReq(GetVersionRpcMethod::getMethodName());
  166. req.params->append("token:foo");
  167. auto res = m.execute(std::move(req), e_.get());
  168. CPPUNIT_ASSERT_EQUAL(0, res.code);
  169. }
  170. e_->getOption()->put(PREF_RPC_SECRET, "foo");
  171. // secret token set and no token: prefixed parameter is given
  172. {
  173. auto req = createReq(GetVersionRpcMethod::getMethodName());
  174. auto res = m.execute(std::move(req), e_.get());
  175. CPPUNIT_ASSERT_EQUAL(1, res.code);
  176. }
  177. // secret token set and token: prefixed parameter is given
  178. {
  179. auto req = createReq(GetVersionRpcMethod::getMethodName());
  180. req.params->append("token:foo");
  181. auto res = m.execute(std::move(req), e_.get());
  182. CPPUNIT_ASSERT_EQUAL(0, res.code);
  183. }
  184. // secret token set and bad token: prefixed parameter is given
  185. {
  186. auto req = createReq(GetVersionRpcMethod::getMethodName());
  187. req.params->append("token:foo2");
  188. auto res = m.execute(std::move(req), e_.get());
  189. CPPUNIT_ASSERT_EQUAL(1, res.code);
  190. }
  191. }
  192. void RpcMethodTest::testAddUri()
  193. {
  194. AddUriRpcMethod m;
  195. {
  196. auto req = createReq(AddUriRpcMethod::getMethodName());
  197. auto urisParam = List::g();
  198. urisParam->append("http://localhost/");
  199. req.params->append(std::move(urisParam));
  200. auto res = m.execute(std::move(req), e_.get());
  201. CPPUNIT_ASSERT_EQUAL(0, res.code);
  202. const RequestGroupList& rgs =
  203. e_->getRequestGroupMan()->getReservedGroups();
  204. CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size());
  205. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"),
  206. (*rgs.begin())->getDownloadContext()->
  207. getFirstFileEntry()->getRemainingUris().front());
  208. }
  209. {
  210. auto req = createReq(AddUriRpcMethod::getMethodName());
  211. auto urisParam = List::g();
  212. urisParam->append("http://localhost/");
  213. req.params->append(std::move(urisParam));
  214. // with options
  215. auto opt = Dict::g();
  216. opt->put(PREF_DIR->k, "/sink");
  217. req.params->append(std::move(opt));
  218. auto res = m.execute(std::move(req), e_.get());
  219. CPPUNIT_ASSERT_EQUAL(0, res.code);
  220. a2_gid_t gid;
  221. CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
  222. (gid, downcast<String>(res.param)->s().c_str()));
  223. CPPUNIT_ASSERT_EQUAL(std::string("/sink"),
  224. findReservedGroup(e_->getRequestGroupMan().get(),
  225. gid)->
  226. getOption()->get(PREF_DIR));
  227. }
  228. }
  229. void RpcMethodTest::testAddUri_withoutUri()
  230. {
  231. AddUriRpcMethod m;
  232. auto res = m.execute(createReq(AddUriRpcMethod::getMethodName()), e_.get());
  233. CPPUNIT_ASSERT_EQUAL(1, res.code);
  234. }
  235. void RpcMethodTest::testAddUri_notUri()
  236. {
  237. AddUriRpcMethod m;
  238. auto req = createReq(AddUriRpcMethod::getMethodName());
  239. auto urisParam = List::g();
  240. urisParam->append("not uri");
  241. req.params->append(std::move(urisParam));
  242. auto res = m.execute(std::move(req), e_.get());
  243. CPPUNIT_ASSERT_EQUAL(1, res.code);
  244. }
  245. void RpcMethodTest::testAddUri_withBadOption()
  246. {
  247. AddUriRpcMethod m;
  248. auto req = createReq(AddUriRpcMethod::getMethodName());
  249. auto urisParam = List::g();
  250. urisParam->append("http://localhost");
  251. req.params->append(std::move(urisParam));
  252. auto opt = Dict::g();
  253. opt->put(PREF_FILE_ALLOCATION->k, "badvalue");
  254. req.params->append(std::move(opt));
  255. auto res = m.execute(std::move(req), e_.get());
  256. CPPUNIT_ASSERT_EQUAL(1, res.code);
  257. }
  258. void RpcMethodTest::testAddUri_withPosition()
  259. {
  260. AddUriRpcMethod m;
  261. auto req1 = createReq(AddUriRpcMethod::getMethodName());
  262. auto urisParam1 = List::g();
  263. urisParam1->append("http://uri1");
  264. req1.params->append(std::move(urisParam1));
  265. auto res1 = m.execute(std::move(req1), e_.get());
  266. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  267. auto req2 = createReq(AddUriRpcMethod::getMethodName());
  268. auto urisParam2 = List::g();
  269. urisParam2->append("http://uri2");
  270. req2.params->append(std::move(urisParam2));
  271. req2.params->append(Dict::g());
  272. req2.params->append(Integer::g(0));
  273. m.execute(std::move(req2), e_.get());
  274. std::string uri =
  275. getReservedGroup(e_->getRequestGroupMan().get(), 0)->
  276. getDownloadContext()->getFirstFileEntry()->getRemainingUris()[0];
  277. CPPUNIT_ASSERT_EQUAL(std::string("http://uri2"), uri);
  278. }
  279. void RpcMethodTest::testAddUri_withBadPosition()
  280. {
  281. AddUriRpcMethod m;
  282. auto req = createReq(AddUriRpcMethod::getMethodName());
  283. auto urisParam = List::g();
  284. urisParam->append("http://localhost/");
  285. req.params->append(std::move(urisParam));
  286. req.params->append(Dict::g());
  287. req.params->append(Integer::g(-1));
  288. auto res = m.execute(std::move(req), e_.get());
  289. CPPUNIT_ASSERT_EQUAL(1, res.code);
  290. }
  291. #ifdef ENABLE_BITTORRENT
  292. namespace {
  293. RpcRequest createAddTorrentReq()
  294. {
  295. auto req = createReq(AddTorrentRpcMethod::getMethodName());
  296. req.params->append(readFile(A2_TEST_DIR"/single.torrent"));
  297. auto uris = List::g();
  298. uris->append("http://localhost/aria2-0.8.2.tar.bz2");
  299. req.params->append(std::move(uris));
  300. return req;
  301. }
  302. } // namespace
  303. void RpcMethodTest::testAddTorrent()
  304. {
  305. File(e_->getOption()->get(PREF_DIR)+
  306. "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
  307. AddTorrentRpcMethod m;
  308. {
  309. // Saving upload metadata is disabled by option.
  310. auto res = m.execute(createAddTorrentReq(), e_.get());
  311. CPPUNIT_ASSERT
  312. (!File(e_->getOption()->get(PREF_DIR)+
  313. "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
  314. CPPUNIT_ASSERT_EQUAL(0, res.code);
  315. CPPUNIT_ASSERT_EQUAL(sizeof(a2_gid_t)*2,
  316. downcast<String>(res.param)->s().size());
  317. }
  318. e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
  319. {
  320. auto res = m.execute(createAddTorrentReq(), e_.get());
  321. CPPUNIT_ASSERT
  322. (File(e_->getOption()->get(PREF_DIR)+
  323. "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
  324. CPPUNIT_ASSERT_EQUAL(0, res.code);
  325. a2_gid_t gid;
  326. CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
  327. (gid, downcast<String>(res.param)->s().c_str()));
  328. auto group = findReservedGroup(e_->getRequestGroupMan().get(), gid);
  329. CPPUNIT_ASSERT(group);
  330. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-0.8.2.tar.bz2",
  331. group->getFirstFilePath());
  332. CPPUNIT_ASSERT_EQUAL((size_t)1,
  333. group->getDownloadContext()->getFirstFileEntry()->
  334. getRemainingUris().size());
  335. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/aria2-0.8.2.tar.bz2"),
  336. group->getDownloadContext()->getFirstFileEntry()->
  337. getRemainingUris()[0]);
  338. }
  339. {
  340. auto req = createAddTorrentReq();
  341. // with options
  342. std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddTorrent";
  343. File(dir).mkdirs();
  344. auto opt = Dict::g();
  345. opt->put(PREF_DIR->k, dir);
  346. File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
  347. req.params->append(std::move(opt));
  348. auto res = m.execute(std::move(req), e_.get());
  349. CPPUNIT_ASSERT_EQUAL(0, res.code);
  350. a2_gid_t gid;
  351. CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
  352. (gid, downcast<String>(res.param)->s().c_str()));
  353. CPPUNIT_ASSERT_EQUAL
  354. (dir+"/aria2-0.8.2.tar.bz2",
  355. findReservedGroup(e_->getRequestGroupMan().get(), gid)
  356. ->getFirstFilePath());
  357. CPPUNIT_ASSERT
  358. (File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
  359. }
  360. }
  361. void RpcMethodTest::testAddTorrent_withoutTorrent()
  362. {
  363. AddTorrentRpcMethod m;
  364. auto res = m.execute(createReq(AddTorrentRpcMethod::getMethodName()),
  365. e_.get());
  366. CPPUNIT_ASSERT_EQUAL(1, res.code);
  367. }
  368. void RpcMethodTest::testAddTorrent_notBase64Torrent()
  369. {
  370. AddTorrentRpcMethod m;
  371. auto req = createReq(AddTorrentRpcMethod::getMethodName());
  372. req.params->append("not torrent");
  373. auto res = m.execute(std::move(req), e_.get());
  374. CPPUNIT_ASSERT_EQUAL(1, res.code);
  375. }
  376. void RpcMethodTest::testAddTorrent_withPosition()
  377. {
  378. AddTorrentRpcMethod m;
  379. auto req1 = createReq(AddTorrentRpcMethod::getMethodName());
  380. req1.params->append(readFile(A2_TEST_DIR"/test.torrent"));
  381. req1.params->append(List::g());
  382. req1.params->append(Dict::g());
  383. auto res1 = m.execute(std::move(req1), e_.get());
  384. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  385. auto req2 = createReq(AddTorrentRpcMethod::getMethodName());
  386. req2.params->append(readFile(A2_TEST_DIR"/single.torrent"));
  387. req2.params->append(List::g());
  388. req2.params->append(Dict::g());
  389. req2.params->append(Integer::g(0));
  390. m.execute(std::move(req2), e_.get());
  391. CPPUNIT_ASSERT_EQUAL((size_t)1,
  392. getReservedGroup(e_->getRequestGroupMan().get(), 0)->
  393. getDownloadContext()->getFileEntries().size());
  394. }
  395. #endif // ENABLE_BITTORRENT
  396. #ifdef ENABLE_METALINK
  397. namespace {
  398. RpcRequest createAddMetalinkReq()
  399. {
  400. auto req = createReq(AddMetalinkRpcMethod::getMethodName());
  401. req.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
  402. return req;
  403. }
  404. } // namespace
  405. void RpcMethodTest::testAddMetalink()
  406. {
  407. File(e_->getOption()->get(PREF_DIR)+
  408. "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
  409. AddMetalinkRpcMethod m;
  410. {
  411. // Saving upload metadata is disabled by option.
  412. auto res = m.execute(createAddMetalinkReq(), e_.get());
  413. CPPUNIT_ASSERT_EQUAL(0, res.code);
  414. const List* resParams = downcast<List>(res.param);
  415. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  416. a2_gid_t gid1, gid2;
  417. CPPUNIT_ASSERT_EQUAL
  418. (0, GroupId::toNumericId
  419. (gid1, downcast<String>(resParams->get(0))->s().c_str()));
  420. CPPUNIT_ASSERT_EQUAL
  421. (0, GroupId::toNumericId
  422. (gid2, downcast<String>(resParams->get(1))->s().c_str()));
  423. CPPUNIT_ASSERT
  424. (!File(e_->getOption()->get(PREF_DIR)+
  425. "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
  426. }
  427. e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
  428. {
  429. auto res = m.execute(createAddMetalinkReq(), e_.get());
  430. CPPUNIT_ASSERT_EQUAL(0, res.code);
  431. const List* resParams = downcast<List>(res.param);
  432. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  433. a2_gid_t gid3, gid4;
  434. CPPUNIT_ASSERT_EQUAL
  435. (0, GroupId::toNumericId
  436. (gid3, downcast<String>(resParams->get(0))->s().c_str()));
  437. CPPUNIT_ASSERT_EQUAL
  438. (0, GroupId::toNumericId
  439. (gid4, downcast<String>(resParams->get(1))->s().c_str()));
  440. #ifdef ENABLE_MESSAGE_DIGEST
  441. CPPUNIT_ASSERT
  442. (File(e_->getOption()->get(PREF_DIR)+
  443. "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
  444. #endif // ENABLE_MESSAGE_DIGEST
  445. auto tar = findReservedGroup(e_->getRequestGroupMan().get(), gid3);
  446. CPPUNIT_ASSERT(tar);
  447. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
  448. tar->getFirstFilePath());
  449. auto deb = findReservedGroup(e_->getRequestGroupMan().get(), gid4);
  450. CPPUNIT_ASSERT(deb);
  451. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.deb",
  452. deb->getFirstFilePath());
  453. }
  454. {
  455. auto req = createAddMetalinkReq();
  456. // with options
  457. std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddMetalink";
  458. File(dir).mkdirs();
  459. auto opt = Dict::g();
  460. opt->put(PREF_DIR->k, dir);
  461. File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
  462. req.params->append(std::move(opt));
  463. auto res = m.execute(std::move(req), e_.get());
  464. CPPUNIT_ASSERT_EQUAL(0, res.code);
  465. const List* resParams = downcast<List>(res.param);
  466. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  467. a2_gid_t gid5;
  468. CPPUNIT_ASSERT_EQUAL
  469. (0, GroupId::toNumericId
  470. (gid5, downcast<String>(resParams->get(0))->s().c_str()));
  471. CPPUNIT_ASSERT_EQUAL(dir+"/aria2-5.0.0.tar.bz2",
  472. findReservedGroup
  473. (e_->getRequestGroupMan().get(), gid5)->
  474. getFirstFilePath());
  475. #ifdef ENABLE_MESSAGE_DIGEST
  476. CPPUNIT_ASSERT
  477. (File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
  478. #endif // ENABLE_MESSAGE_DIGEST
  479. }
  480. }
  481. void RpcMethodTest::testAddMetalink_withoutMetalink()
  482. {
  483. AddMetalinkRpcMethod m;
  484. auto res = m.execute(createReq(AddMetalinkRpcMethod::getMethodName()),
  485. e_.get());
  486. CPPUNIT_ASSERT_EQUAL(1, res.code);
  487. }
  488. void RpcMethodTest::testAddMetalink_notBase64Metalink()
  489. {
  490. AddMetalinkRpcMethod m;
  491. auto req = createReq(AddMetalinkRpcMethod::getMethodName());
  492. req.params->append("not metalink");
  493. auto res = m.execute(std::move(req), e_.get());
  494. CPPUNIT_ASSERT_EQUAL(1, res.code);
  495. }
  496. void RpcMethodTest::testAddMetalink_withPosition()
  497. {
  498. AddUriRpcMethod m1;
  499. auto req1 = createReq(AddUriRpcMethod::getMethodName());
  500. auto urisParam1 = List::g();
  501. urisParam1->append("http://uri");
  502. req1.params->append(std::move(urisParam1));
  503. auto res1 = m1.execute(std::move(req1), e_.get());
  504. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  505. AddMetalinkRpcMethod m2;
  506. auto req2 = createReq(AddMetalinkRpcMethod::getMethodName());
  507. req2.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
  508. req2.params->append(Dict::g());
  509. req2.params->append(Integer::g(0));
  510. auto res2 = m2.execute(std::move(req2), e_.get());
  511. CPPUNIT_ASSERT_EQUAL(0, res2.code);
  512. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
  513. getReservedGroup(e_->getRequestGroupMan().get(), 0)->
  514. getFirstFilePath());
  515. }
  516. #endif // ENABLE_METALINK
  517. void RpcMethodTest::testGetOption()
  518. {
  519. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  520. group->getOption()->put(PREF_DIR, "alpha");
  521. e_->getRequestGroupMan()->addReservedGroup(group);
  522. auto dr = createDownloadResult(error_code::FINISHED, "http://host/fin");
  523. dr->option->put(PREF_DIR, "bravo");
  524. e_->getRequestGroupMan()->addDownloadResult(dr);
  525. GetOptionRpcMethod m;
  526. auto req = createReq(GetOptionRpcMethod::getMethodName());
  527. req.params->append(GroupId::toHex(group->getGID()));
  528. auto res = m.execute(std::move(req), e_.get());
  529. CPPUNIT_ASSERT_EQUAL(0, res.code);
  530. const Dict* resopt = downcast<Dict>(res.param);
  531. CPPUNIT_ASSERT_EQUAL(std::string("alpha"),
  532. downcast<String>(resopt->get(PREF_DIR->k))->s());
  533. req = createReq(GetOptionRpcMethod::getMethodName());
  534. req.params->append(dr->gid->toHex());
  535. res = m.execute(std::move(req), e_.get());
  536. CPPUNIT_ASSERT_EQUAL(0, res.code);
  537. resopt = downcast<Dict>(res.param);
  538. CPPUNIT_ASSERT_EQUAL(std::string("bravo"),
  539. downcast<String>(resopt->get(PREF_DIR->k))->s());
  540. // Invalid GID
  541. req = createReq(GetOptionRpcMethod::getMethodName());
  542. req.params->append(GroupId::create()->toHex());
  543. res = m.execute(std::move(req), e_.get());
  544. CPPUNIT_ASSERT_EQUAL(1, res.code);
  545. }
  546. void RpcMethodTest::testChangeOption()
  547. {
  548. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  549. e_->getRequestGroupMan()->addReservedGroup(group);
  550. ChangeOptionRpcMethod m;
  551. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  552. req.params->append(GroupId::toHex(group->getGID()));
  553. auto opt = Dict::g();
  554. opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "100K");
  555. #ifdef ENABLE_BITTORRENT
  556. opt->put(PREF_BT_MAX_PEERS->k, "100");
  557. opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K");
  558. opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K");
  559. {
  560. auto btObject = make_unique<BtObject>();
  561. btObject->btRuntime = std::make_shared<BtRuntime>();
  562. e_->getBtRegistry()->put(group->getGID(), std::move(btObject));
  563. }
  564. #endif // ENABLE_BITTORRENT
  565. req.params->append(std::move(opt));
  566. auto res = m.execute(std::move(req), e_.get());
  567. auto option = group->getOption();
  568. CPPUNIT_ASSERT_EQUAL(0, res.code);
  569. CPPUNIT_ASSERT_EQUAL(100*1024,
  570. group->getMaxDownloadSpeedLimit());
  571. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  572. option->get(PREF_MAX_DOWNLOAD_LIMIT));
  573. #ifdef ENABLE_BITTORRENT
  574. CPPUNIT_ASSERT_EQUAL(std::string("307200"),
  575. option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
  576. CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
  577. CPPUNIT_ASSERT_EQUAL(100,
  578. e_->getBtRegistry()->get(group->getGID())
  579. ->btRuntime->getMaxPeers());
  580. CPPUNIT_ASSERT_EQUAL(50*1024,
  581. group->getMaxUploadSpeedLimit());
  582. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  583. option->get(PREF_MAX_UPLOAD_LIMIT));
  584. #endif // ENABLE_BITTORRENT
  585. }
  586. void RpcMethodTest::testChangeOption_withBadOption()
  587. {
  588. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  589. e_->getRequestGroupMan()->addReservedGroup(group);
  590. ChangeOptionRpcMethod m;
  591. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  592. req.params->append(GroupId::toHex(group->getGID()));
  593. auto opt = Dict::g();
  594. opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "badvalue");
  595. req.params->append(std::move(opt));
  596. auto res = m.execute(std::move(req), e_.get());
  597. CPPUNIT_ASSERT_EQUAL(1, res.code);
  598. }
  599. void RpcMethodTest::testChangeOption_withNotAllowedOption()
  600. {
  601. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  602. e_->getRequestGroupMan()->addReservedGroup(group);
  603. ChangeOptionRpcMethod m;
  604. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  605. req.params->append(GroupId::toHex(group->getGID()));
  606. auto opt = Dict::g();
  607. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
  608. req.params->append(std::move(opt));
  609. auto res = m.execute(std::move(req), e_.get());
  610. // The unacceptable options are just ignored.
  611. CPPUNIT_ASSERT_EQUAL(0, res.code);
  612. }
  613. void RpcMethodTest::testChangeOption_withoutGid()
  614. {
  615. ChangeOptionRpcMethod m;
  616. auto res = m.execute
  617. (createReq(ChangeOptionRpcMethod::getMethodName()), e_.get());
  618. CPPUNIT_ASSERT_EQUAL(1, res.code);
  619. }
  620. void RpcMethodTest::testChangeGlobalOption()
  621. {
  622. ChangeGlobalOptionRpcMethod m;
  623. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  624. auto opt = Dict::g();
  625. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
  626. #ifdef ENABLE_BITTORRENT
  627. opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT->k, "50K");
  628. #endif // ENABLE_BITTORRENT
  629. req.params->append(std::move(opt));
  630. auto res = m.execute(std::move(req), e_.get());
  631. CPPUNIT_ASSERT_EQUAL(0, res.code);
  632. CPPUNIT_ASSERT_EQUAL
  633. (100*1024,
  634. e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit());
  635. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  636. e_->getOption()->get(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
  637. #ifdef ENABLE_BITTORRENT
  638. CPPUNIT_ASSERT_EQUAL
  639. (50*1024,
  640. e_->getRequestGroupMan()->getMaxOverallUploadSpeedLimit());
  641. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  642. e_->getOption()->get(PREF_MAX_OVERALL_UPLOAD_LIMIT));
  643. #endif // ENABLE_BITTORRENT
  644. }
  645. void RpcMethodTest::testChangeGlobalOption_withBadOption()
  646. {
  647. ChangeGlobalOptionRpcMethod m;
  648. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  649. auto opt = Dict::g();
  650. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "badvalue");
  651. req.params->append(std::move(opt));
  652. auto res = m.execute(std::move(req), e_.get());
  653. CPPUNIT_ASSERT_EQUAL(1, res.code);
  654. }
  655. void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
  656. {
  657. ChangeGlobalOptionRpcMethod m;
  658. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  659. auto opt = Dict::g();
  660. opt->put(PREF_ENABLE_RPC->k, "100K");
  661. req.params->append(std::move(opt));
  662. auto res = m.execute(std::move(req), e_.get());
  663. // The unacceptable options are just ignored.
  664. CPPUNIT_ASSERT_EQUAL(0, res.code);
  665. }
  666. void RpcMethodTest::testNoSuchMethod()
  667. {
  668. NoSuchMethodRpcMethod m;
  669. auto res = m.execute(createReq("make.hamburger"), nullptr);
  670. CPPUNIT_ASSERT_EQUAL(1, res.code);
  671. CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
  672. getString(downcast<Dict>(res.param), "faultString"));
  673. }
  674. void RpcMethodTest::testTellStatus_withoutGid()
  675. {
  676. TellStatusRpcMethod m;
  677. auto res = m.execute
  678. (createReq(TellStatusRpcMethod::getMethodName()), e_.get());
  679. CPPUNIT_ASSERT_EQUAL(1, res.code);
  680. }
  681. namespace {
  682. void addUri(const std::string& uri, const std::shared_ptr<DownloadEngine>& e)
  683. {
  684. AddUriRpcMethod m;
  685. auto req = createReq(AddUriRpcMethod::getMethodName());
  686. auto urisParam = List::g();
  687. urisParam->append(uri);
  688. req.params->append(std::move(urisParam));
  689. CPPUNIT_ASSERT_EQUAL(0, m.execute(std::move(req), e.get()).code);
  690. }
  691. } // namespace
  692. #ifdef ENABLE_BITTORRENT
  693. namespace {
  694. void addTorrent
  695. (const std::string& torrentFile, const std::shared_ptr<DownloadEngine>& e)
  696. {
  697. AddTorrentRpcMethod m;
  698. auto req = createReq(AddTorrentRpcMethod::getMethodName());
  699. req.params->append(readFile(torrentFile));
  700. auto res = m.execute(std::move(req), e.get());
  701. }
  702. } // namespace
  703. #endif // ENABLE_BITTORRENT
  704. void RpcMethodTest::testTellWaiting()
  705. {
  706. addUri("http://1/", e_);
  707. addUri("http://2/", e_);
  708. addUri("http://3/", e_);
  709. #ifdef ENABLE_BITTORRENT
  710. addTorrent(A2_TEST_DIR"/single.torrent", e_);
  711. #else // !ENABLE_BITTORRENT
  712. addUri("http://4/", e_);
  713. #endif // !ENABLE_BITTORRENT
  714. auto& rgman = e_->getRequestGroupMan();
  715. TellWaitingRpcMethod m;
  716. auto req = createReq(TellWaitingRpcMethod::getMethodName());
  717. req.params->append(Integer::g(1));
  718. req.params->append(Integer::g(2));
  719. auto res = m.execute(std::move(req), e_.get());
  720. CPPUNIT_ASSERT_EQUAL(0, res.code);
  721. const List* resParams = downcast<List>(res.param);
  722. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  723. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 1)
  724. ->getGID()),
  725. getString(downcast<Dict>(resParams->get(0)), "gid"));
  726. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 2)
  727. ->getGID()),
  728. getString(downcast<Dict>(resParams->get(1)), "gid"));
  729. // waiting.size() == offset+num
  730. req = createReq(TellWaitingRpcMethod::getMethodName());
  731. req.params->append(Integer::g(1));
  732. req.params->append(Integer::g(3));
  733. res = m.execute(std::move(req), e_.get());
  734. CPPUNIT_ASSERT_EQUAL(0, res.code);
  735. resParams = downcast<List>(res.param);
  736. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  737. // waiting.size() < offset+num
  738. req = createReq(TellWaitingRpcMethod::getMethodName());
  739. req.params->append(Integer::g(1));
  740. req.params->append(Integer::g(4));
  741. res = m.execute(std::move(req), e_.get());
  742. CPPUNIT_ASSERT_EQUAL(0, res.code);
  743. resParams = downcast<List>(res.param);
  744. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  745. // offset = INT32_MAX
  746. req = createReq(TellWaitingRpcMethod::getMethodName());
  747. req.params->append(Integer::g(INT32_MAX));
  748. req.params->append(Integer::g(1));
  749. res = m.execute(std::move(req), e_.get());
  750. CPPUNIT_ASSERT_EQUAL(0, res.code);
  751. resParams = downcast<List>(res.param);
  752. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  753. // num = INT32_MAX
  754. req = createReq(TellWaitingRpcMethod::getMethodName());
  755. req.params->append(Integer::g(1));
  756. req.params->append(Integer::g(INT32_MAX));
  757. res = m.execute(std::move(req), e_.get());
  758. CPPUNIT_ASSERT_EQUAL(0, res.code);
  759. resParams = downcast<List>(res.param);
  760. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  761. // offset=INT32_MAX and num = INT32_MAX
  762. req = createReq(TellWaitingRpcMethod::getMethodName());
  763. req.params->append(Integer::g(INT32_MAX));
  764. req.params->append(Integer::g(INT32_MAX));
  765. res = m.execute(std::move(req), e_.get());
  766. CPPUNIT_ASSERT_EQUAL(0, res.code);
  767. resParams = downcast<List>(res.param);
  768. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  769. // offset=INT32_MIN and num = INT32_MAX
  770. req = createReq(TellWaitingRpcMethod::getMethodName());
  771. req.params->append(Integer::g(INT32_MIN));
  772. req.params->append(Integer::g(INT32_MAX));
  773. res = m.execute(std::move(req), e_.get());
  774. CPPUNIT_ASSERT_EQUAL(0, res.code);
  775. resParams = downcast<List>(res.param);
  776. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  777. // negative offset
  778. req = createReq(TellWaitingRpcMethod::getMethodName());
  779. req.params->append(Integer::g(-1));
  780. req.params->append(Integer::g(2));
  781. res = m.execute(std::move(req), e_.get());
  782. CPPUNIT_ASSERT_EQUAL(0, res.code);
  783. resParams = downcast<List>(res.param);
  784. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  785. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(),
  786. 3)->getGID()),
  787. getString(downcast<Dict>(resParams->get(0)), "gid"));
  788. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(),
  789. 2)->getGID()),
  790. getString(downcast<Dict>(resParams->get(1)), "gid"));
  791. // negative offset and size < num
  792. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  793. req.params->append(Integer::g(-1));
  794. req.params->append(Integer::g(100));
  795. res = m.execute(std::move(req), e_.get());
  796. CPPUNIT_ASSERT_EQUAL(0, res.code);
  797. resParams = downcast<List>(res.param);
  798. CPPUNIT_ASSERT_EQUAL((size_t)4, resParams->size());
  799. // nagative offset and normalized offset < 0
  800. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  801. req.params->append(Integer::g(-5));
  802. req.params->append(Integer::g(100));
  803. res = m.execute(std::move(req), e_.get());
  804. CPPUNIT_ASSERT_EQUAL(0, res.code);
  805. resParams = downcast<List>(res.param);
  806. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  807. // nagative offset and normalized offset == 0
  808. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  809. req.params->append(Integer::g(-4));
  810. req.params->append(Integer::g(100));
  811. res = m.execute(std::move(req), e_.get());
  812. CPPUNIT_ASSERT_EQUAL(0, res.code);
  813. resParams = downcast<List>(res.param);
  814. CPPUNIT_ASSERT_EQUAL((size_t)1, resParams->size());
  815. }
  816. void RpcMethodTest::testTellWaiting_fail()
  817. {
  818. TellWaitingRpcMethod m;
  819. auto res = m.execute(createReq(TellWaitingRpcMethod::getMethodName()),
  820. e_.get());
  821. CPPUNIT_ASSERT_EQUAL(1, res.code);
  822. }
  823. void RpcMethodTest::testGetVersion()
  824. {
  825. GetVersionRpcMethod m;
  826. auto res = m.execute(createReq(GetVersionRpcMethod::getMethodName()),
  827. e_.get());
  828. CPPUNIT_ASSERT_EQUAL(0, res.code);
  829. const Dict* resParams = downcast<Dict>(res.param);
  830. CPPUNIT_ASSERT_EQUAL(std::string(PACKAGE_VERSION),
  831. getString(resParams, "version"));
  832. const List* featureList = downcast<List>(resParams->get("enabledFeatures"));
  833. std::string features;
  834. for(auto i = featureList->begin(); i != featureList->end(); ++i) {
  835. const String* s = downcast<String>(*i);
  836. features += s->s();
  837. features += ", ";
  838. }
  839. CPPUNIT_ASSERT_EQUAL(featureSummary()+", ", features);
  840. }
  841. void RpcMethodTest::testGatherStoppedDownload()
  842. {
  843. std::vector<std::shared_ptr<FileEntry>> fileEntries;
  844. std::vector<a2_gid_t> followedBy;
  845. followedBy.push_back(3);
  846. followedBy.push_back(4);
  847. auto d = std::make_shared<DownloadResult>();
  848. d->gid = GroupId::create();
  849. d->fileEntries = fileEntries;
  850. d->inMemoryDownload = false;
  851. d->sessionDownloadLength = UINT64_MAX;
  852. d->sessionTime = 1000;
  853. d->result = error_code::FINISHED;
  854. d->followedBy = followedBy;
  855. d->belongsTo = 2;
  856. auto entry = Dict::g();
  857. std::vector<std::string> keys;
  858. gatherStoppedDownload(entry.get(), d, keys);
  859. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  860. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(3),
  861. downcast<String>(followedByRes->get(0))->s());
  862. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(4),
  863. downcast<String>(followedByRes->get(1))->s());
  864. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(2),
  865. downcast<String>(entry->get("belongsTo"))->s());
  866. keys.push_back("gid");
  867. entry = Dict::g();
  868. gatherStoppedDownload(entry.get(), d, keys);
  869. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  870. CPPUNIT_ASSERT(entry->containsKey("gid"));
  871. }
  872. void RpcMethodTest::testGatherProgressCommon()
  873. {
  874. auto dctx = std::make_shared<DownloadContext>(0, 0,"aria2.tar.bz2");
  875. std::string uris[] = { "http://localhost/aria2.tar.bz2" };
  876. dctx->getFirstFileEntry()->addUris(std::begin(uris), std::end(uris));
  877. auto group = std::make_shared<RequestGroup>(GroupId::create(),
  878. util::copy(option_));
  879. group->setDownloadContext(dctx);
  880. std::vector<std::shared_ptr<RequestGroup>> followedBy;
  881. for(int i = 0; i < 2; ++i) {
  882. followedBy.push_back(std::make_shared<RequestGroup>(GroupId::create(),
  883. util::copy(option_)));
  884. }
  885. group->followedBy(followedBy.begin(), followedBy.end());
  886. auto parent = GroupId::create();
  887. group->belongsTo(parent->getNumericId());
  888. auto entry = Dict::g();
  889. std::vector<std::string> keys;
  890. gatherProgressCommon(entry.get(), group, keys);
  891. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  892. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[0]->getGID()),
  893. downcast<String>(followedByRes->get(0))->s());
  894. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[1]->getGID()),
  895. downcast<String>(followedByRes->get(1))->s());
  896. CPPUNIT_ASSERT_EQUAL(parent->toHex(),
  897. downcast<String>(entry->get("belongsTo"))->s());
  898. const List* files = downcast<List>(entry->get("files"));
  899. CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
  900. const Dict* file = downcast<Dict>(files->get(0));
  901. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  902. downcast<String>(file->get("path"))->s());
  903. CPPUNIT_ASSERT_EQUAL(uris[0],
  904. downcast<String>
  905. (downcast<Dict>
  906. (downcast<List>(file->get("uris"))->get(0))
  907. ->get("uri"))
  908. ->s());
  909. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR),
  910. downcast<String>(entry->get("dir"))->s());
  911. keys.push_back("gid");
  912. entry = Dict::g();
  913. gatherProgressCommon(entry.get(), group, keys);
  914. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  915. CPPUNIT_ASSERT(entry->containsKey("gid"));
  916. }
  917. #ifdef ENABLE_BITTORRENT
  918. void RpcMethodTest::testGatherBitTorrentMetadata()
  919. {
  920. auto option = std::make_shared<Option>();
  921. option->put(PREF_DIR, ".");
  922. auto dctx = std::make_shared<DownloadContext>();
  923. bittorrent::load(A2_TEST_DIR"/test.torrent", dctx, option);
  924. auto btDict = Dict::g();
  925. gatherBitTorrentMetadata(btDict.get(), bittorrent::getTorrentAttrs(dctx));
  926. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  927. downcast<String>(btDict->get("comment"))->s());
  928. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  929. downcast<Integer>(btDict->get("creationDate"))->i());
  930. CPPUNIT_ASSERT_EQUAL(std::string("multi"),
  931. downcast<String>(btDict->get("mode"))->s());
  932. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"),
  933. downcast<String>
  934. (downcast<Dict>
  935. (btDict->get("info"))
  936. ->get("name"))
  937. ->s());
  938. const List* announceList = downcast<List>(btDict->get("announceList"));
  939. CPPUNIT_ASSERT_EQUAL((size_t)3, announceList->size());
  940. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  941. downcast<String>(downcast<List>(announceList->get(0))->get(0))->s());
  942. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  943. downcast<String>(downcast<List>(announceList->get(1))->get(0))->s());
  944. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  945. downcast<String>(downcast<List>(announceList->get(2))->get(0))->s());
  946. // Remove some keys
  947. auto modBtAttrs = bittorrent::getTorrentAttrs(dctx);
  948. modBtAttrs->comment.clear();
  949. modBtAttrs->creationDate = 0;
  950. modBtAttrs->mode = BT_FILE_MODE_NONE;
  951. modBtAttrs->metadata.clear();
  952. btDict = Dict::g();
  953. gatherBitTorrentMetadata(btDict.get(), modBtAttrs);
  954. CPPUNIT_ASSERT(!btDict->containsKey("comment"));
  955. CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
  956. CPPUNIT_ASSERT(!btDict->containsKey("mode"));
  957. CPPUNIT_ASSERT(!btDict->containsKey("info"));
  958. CPPUNIT_ASSERT(btDict->containsKey("announceList"));
  959. }
  960. #endif // ENABLE_BITTORRENT
  961. void RpcMethodTest::testChangePosition()
  962. {
  963. e_->getRequestGroupMan()->addReservedGroup
  964. (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
  965. e_->getRequestGroupMan()->addReservedGroup
  966. (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
  967. a2_gid_t gid = getReservedGroup(e_->getRequestGroupMan().get(), 0)->getGID();
  968. ChangePositionRpcMethod m;
  969. auto req = createReq(ChangePositionRpcMethod::getMethodName());
  970. req.params->append(GroupId::toHex(gid));
  971. req.params->append(Integer::g(1));
  972. req.params->append("POS_SET");
  973. auto res = m.execute(std::move(req), e_.get());
  974. CPPUNIT_ASSERT_EQUAL(0, res.code);
  975. CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
  976. CPPUNIT_ASSERT_EQUAL
  977. (gid, getReservedGroup(e_->getRequestGroupMan().get(), 1)->getGID());
  978. }
  979. void RpcMethodTest::testChangePosition_fail()
  980. {
  981. ChangePositionRpcMethod m;
  982. auto res = m.execute
  983. (createReq(ChangePositionRpcMethod::getMethodName()), e_.get());
  984. CPPUNIT_ASSERT_EQUAL(1, res.code);
  985. auto req = createReq(ChangePositionRpcMethod::getMethodName());
  986. req.params->append("1");
  987. req.params->append(Integer::g(2));
  988. req.params->append("bad keyword");
  989. res = m.execute(std::move(req), e_.get());
  990. CPPUNIT_ASSERT_EQUAL(1, res.code);
  991. }
  992. namespace {
  993. RpcRequest createChangeUriReq(a2_gid_t gid, size_t fileIndex)
  994. {
  995. auto req = createReq(ChangeUriRpcMethod::getMethodName());
  996. req.params->append(GroupId::toHex(gid)); // GID
  997. req.params->append(Integer::g(fileIndex)); // index of FileEntry
  998. auto removeuris = List::g();
  999. removeuris->append("http://example.org/mustremove1");
  1000. removeuris->append("http://example.org/mustremove2");
  1001. removeuris->append("http://example.org/notexist");
  1002. req.params->append(std::move(removeuris));
  1003. return req;
  1004. }
  1005. } // namespace
  1006. void RpcMethodTest::testChangeUri()
  1007. {
  1008. std::shared_ptr<FileEntry> files[3];
  1009. for(int i = 0; i < 3; ++i) {
  1010. files[i].reset(new FileEntry());
  1011. }
  1012. files[1]->addUri("http://example.org/aria2.tar.bz2");
  1013. files[1]->addUri("http://example.org/mustremove1");
  1014. files[1]->addUri("http://example.org/mustremove2");
  1015. auto dctx = std::make_shared<DownloadContext>();
  1016. dctx->setFileEntries(&files[0], &files[3]);
  1017. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  1018. group->setDownloadContext(dctx);
  1019. e_->getRequestGroupMan()->addReservedGroup(group);
  1020. ChangeUriRpcMethod m;
  1021. auto req = createChangeUriReq(group->getGID(), 2);
  1022. auto adduris = List::g();
  1023. adduris->append("http://example.org/added1");
  1024. adduris->append("http://example.org/added2");
  1025. adduris->append("baduri");
  1026. adduris->append("http://example.org/added3");
  1027. req.params->append(std::move(adduris));
  1028. auto res = m.execute(std::move(req), e_.get());
  1029. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1030. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1031. CPPUNIT_ASSERT_EQUAL((int64_t)3, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1032. CPPUNIT_ASSERT_EQUAL((size_t)0, files[0]->getRemainingUris().size());
  1033. CPPUNIT_ASSERT_EQUAL((size_t)0, files[2]->getRemainingUris().size());
  1034. std::deque<std::string> uris = files[1]->getRemainingUris();
  1035. CPPUNIT_ASSERT_EQUAL((size_t)4, uris.size());
  1036. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/aria2.tar.bz2"),uris[0]);
  1037. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1"), uris[1]);
  1038. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
  1039. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
  1040. req = createChangeUriReq(group->getGID(), 2);
  1041. // Change adduris
  1042. adduris = List::g();
  1043. adduris->append("http://example.org/added1-1");
  1044. adduris->append("http://example.org/added1-2");
  1045. req.params->append(std::move(adduris));
  1046. // Set position parameter
  1047. req.params->append(Integer::g(2));
  1048. res = m.execute(std::move(req), e_.get());
  1049. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1050. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1051. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1052. uris = files[1]->getRemainingUris();
  1053. CPPUNIT_ASSERT_EQUAL((size_t)6, uris.size());
  1054. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[2]);
  1055. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
  1056. // Change index of FileEntry
  1057. req = createChangeUriReq(group->getGID(), 1);
  1058. adduris = List::g();
  1059. adduris->append("http://example.org/added1-1");
  1060. adduris->append("http://example.org/added1-2");
  1061. req.params->append(std::move(adduris));
  1062. // Set position far beyond the size of uris in FileEntry.
  1063. req.params->append(Integer::g(1000));
  1064. res = m.execute(std::move(req), e_.get());
  1065. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1066. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1067. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1068. uris = files[0]->getRemainingUris();
  1069. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  1070. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[0]);
  1071. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
  1072. }
  1073. namespace {
  1074. RpcRequest createChangeUriEmptyReq(a2_gid_t gid, size_t fileIndex)
  1075. {
  1076. auto req = createReq(ChangeUriRpcMethod::getMethodName());
  1077. req.params->append(GroupId::toHex(gid)); // GID
  1078. req.params->append(Integer::g(fileIndex)); // index of FileEntry
  1079. req.params->append(List::g()); // remove uris
  1080. req.params->append(List::g()); // append uris
  1081. return req;
  1082. }
  1083. } // namespace
  1084. void RpcMethodTest::testChangeUri_fail()
  1085. {
  1086. std::shared_ptr<FileEntry> files[3];
  1087. for(int i = 0; i < 3; ++i) {
  1088. files[i] = std::make_shared<FileEntry>();
  1089. }
  1090. auto dctx = std::make_shared<DownloadContext>();
  1091. dctx->setFileEntries(&files[0], &files[3]);
  1092. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  1093. group->setDownloadContext(dctx);
  1094. e_->getRequestGroupMan()->addReservedGroup(group);
  1095. ChangeUriRpcMethod m;
  1096. auto req = createChangeUriEmptyReq(group->getGID(), 1);
  1097. auto res = m.execute(std::move(req), e_.get());
  1098. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1099. req = createChangeUriEmptyReq(group->getGID(), 0);
  1100. res = m.execute(std::move(req), e_.get());
  1101. // RPC request fails because 2nd argument is less than 1.
  1102. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1103. req = createChangeUriEmptyReq(GroupId::create()->getNumericId(), 1);
  1104. res = m.execute(std::move(req), e_.get());
  1105. // RPC request fails because the given GID does not exist.
  1106. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1107. req = createChangeUriEmptyReq(group->getGID(), 4);
  1108. res = m.execute(std::move(req), e_.get());
  1109. // RPC request fails because FileEntry#3 does not exist.
  1110. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1111. req = createChangeUriEmptyReq(group->getGID(), 1);
  1112. req.params->set(1, String::g("0"));
  1113. res = m.execute(std::move(req), e_.get());
  1114. // RPC request fails because index of FileEntry is string.
  1115. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1116. req = createChangeUriEmptyReq(group->getGID(), 1);
  1117. req.params->set(2, String::g("http://url"));
  1118. res = m.execute(std::move(req), e_.get());
  1119. // RPC request fails because 3rd param is not list.
  1120. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1121. req = createChangeUriEmptyReq(group->getGID(), 1);
  1122. req.params->set(2, List::g());
  1123. req.params->set(3, String::g("http://url"));
  1124. res = m.execute(std::move(req), e_.get());
  1125. // RPC request fails because 4th param is not list.
  1126. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1127. }
  1128. void RpcMethodTest::testGetSessionInfo()
  1129. {
  1130. GetSessionInfoRpcMethod m;
  1131. auto res = m.execute
  1132. (createReq(GetSessionInfoRpcMethod::getMethodName()), e_.get());
  1133. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1134. CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
  1135. getString(downcast<Dict>(res.param), "sessionId"));
  1136. }
  1137. void RpcMethodTest::testPause()
  1138. {
  1139. std::vector<std::string> uris {
  1140. "http://url1",
  1141. "http://url2",
  1142. "http://url3",
  1143. };
  1144. option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
  1145. std::vector<std::shared_ptr<RequestGroup>> groups;
  1146. createRequestGroupForUri(groups, option_, uris);
  1147. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  1148. e_->getRequestGroupMan()->addReservedGroup(groups);
  1149. {
  1150. PauseRpcMethod m;
  1151. auto req = createReq(PauseRpcMethod::getMethodName());
  1152. req.params->append(GroupId::toHex(groups[0]->getGID()));
  1153. auto res = m.execute(std::move(req), e_.get());
  1154. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1155. }
  1156. CPPUNIT_ASSERT(groups[0]->isPauseRequested());
  1157. {
  1158. UnpauseRpcMethod m;
  1159. auto req = createReq(UnpauseRpcMethod::getMethodName());
  1160. req.params->append(GroupId::toHex(groups[0]->getGID()));
  1161. auto res = m.execute(std::move(req), e_.get());
  1162. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1163. }
  1164. CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
  1165. {
  1166. PauseAllRpcMethod m;
  1167. auto req = createReq(PauseAllRpcMethod::getMethodName());
  1168. auto res = m.execute(std::move(req), e_.get());
  1169. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1170. }
  1171. for(size_t i = 0; i < groups.size(); ++i) {
  1172. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  1173. }
  1174. {
  1175. UnpauseAllRpcMethod m;
  1176. auto req = createReq(UnpauseAllRpcMethod::getMethodName());
  1177. auto res = m.execute(std::move(req), e_.get());
  1178. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1179. }
  1180. for(size_t i = 0; i < groups.size(); ++i) {
  1181. CPPUNIT_ASSERT(!groups[i]->isPauseRequested());
  1182. }
  1183. {
  1184. ForcePauseAllRpcMethod m;
  1185. auto req = createReq(ForcePauseAllRpcMethod::getMethodName());
  1186. auto res = m.execute(std::move(req), e_.get());
  1187. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1188. }
  1189. for(size_t i = 0; i < groups.size(); ++i) {
  1190. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  1191. }
  1192. }
  1193. void RpcMethodTest::testSystemMulticall()
  1194. {
  1195. SystemMulticallRpcMethod m;
  1196. auto req = createReq("system.multicall");
  1197. auto reqparams = List::g();
  1198. for(int i = 0; i < 2; ++i) {
  1199. auto dict = Dict::g();
  1200. dict->put("methodName", AddUriRpcMethod::getMethodName());
  1201. auto params = List::g();
  1202. auto urisParam = List::g();
  1203. urisParam->append("http://localhost/"+util::itos(i));
  1204. params->append(std::move(urisParam));
  1205. dict->put("params", std::move(params));
  1206. reqparams->append(std::move(dict));
  1207. }
  1208. {
  1209. auto dict = Dict::g();
  1210. dict->put("methodName", "not exists");
  1211. dict->put("params", List::g());
  1212. reqparams->append(std::move(dict));
  1213. }
  1214. {
  1215. reqparams->append("not struct");
  1216. }
  1217. {
  1218. auto dict = Dict::g();
  1219. dict->put("methodName", "system.multicall");
  1220. dict->put("params", List::g());
  1221. reqparams->append(std::move(dict));
  1222. }
  1223. {
  1224. // missing params
  1225. auto dict = Dict::g();
  1226. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1227. reqparams->append(std::move(dict));
  1228. }
  1229. {
  1230. auto dict = Dict::g();
  1231. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1232. dict->put("params", List::g());
  1233. reqparams->append(std::move(dict));
  1234. }
  1235. req.params->append(std::move(reqparams));
  1236. auto res = m.execute(std::move(req), e_.get());
  1237. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1238. const List* resParams = downcast<List>(res.param);
  1239. CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
  1240. auto& rgman = e_->getRequestGroupMan();
  1241. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 0)
  1242. ->getGID()),
  1243. downcast<String>(downcast<List>(resParams->get(0))->get(0))->s());
  1244. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 1)
  1245. ->getGID()),
  1246. downcast<String>(downcast<List>(resParams->get(1))->get(0))->s());
  1247. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1248. downcast<Integer>
  1249. (downcast<Dict>(resParams->get(2))->get("faultCode"))
  1250. ->i());
  1251. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1252. downcast<Integer>
  1253. (downcast<Dict>(resParams->get(3))->get("faultCode"))
  1254. ->i());
  1255. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1256. downcast<Integer>
  1257. (downcast<Dict>(resParams->get(4))->get("faultCode"))
  1258. ->i());
  1259. CPPUNIT_ASSERT(downcast<List>(resParams->get(5)));
  1260. CPPUNIT_ASSERT(downcast<List>(resParams->get(6)));
  1261. }
  1262. void RpcMethodTest::testSystemMulticall_fail()
  1263. {
  1264. SystemMulticallRpcMethod m;
  1265. auto res = m.execute(createReq("system.multicall"), e_.get());
  1266. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1267. }
  1268. } // namespace rpc
  1269. } // namespace aria2