RpcMethodTest.cc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. CPPUNIT_ASSERT
  441. (File(e_->getOption()->get(PREF_DIR)+
  442. "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
  443. auto tar = findReservedGroup(e_->getRequestGroupMan().get(), gid3);
  444. CPPUNIT_ASSERT(tar);
  445. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
  446. tar->getFirstFilePath());
  447. auto deb = findReservedGroup(e_->getRequestGroupMan().get(), gid4);
  448. CPPUNIT_ASSERT(deb);
  449. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.deb",
  450. deb->getFirstFilePath());
  451. }
  452. {
  453. auto req = createAddMetalinkReq();
  454. // with options
  455. std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddMetalink";
  456. File(dir).mkdirs();
  457. auto opt = Dict::g();
  458. opt->put(PREF_DIR->k, dir);
  459. File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
  460. req.params->append(std::move(opt));
  461. auto res = m.execute(std::move(req), e_.get());
  462. CPPUNIT_ASSERT_EQUAL(0, res.code);
  463. const List* resParams = downcast<List>(res.param);
  464. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  465. a2_gid_t gid5;
  466. CPPUNIT_ASSERT_EQUAL
  467. (0, GroupId::toNumericId
  468. (gid5, downcast<String>(resParams->get(0))->s().c_str()));
  469. CPPUNIT_ASSERT_EQUAL(dir+"/aria2-5.0.0.tar.bz2",
  470. findReservedGroup
  471. (e_->getRequestGroupMan().get(), gid5)->
  472. getFirstFilePath());
  473. CPPUNIT_ASSERT
  474. (File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
  475. }
  476. }
  477. void RpcMethodTest::testAddMetalink_withoutMetalink()
  478. {
  479. AddMetalinkRpcMethod m;
  480. auto res = m.execute(createReq(AddMetalinkRpcMethod::getMethodName()),
  481. e_.get());
  482. CPPUNIT_ASSERT_EQUAL(1, res.code);
  483. }
  484. void RpcMethodTest::testAddMetalink_notBase64Metalink()
  485. {
  486. AddMetalinkRpcMethod m;
  487. auto req = createReq(AddMetalinkRpcMethod::getMethodName());
  488. req.params->append("not metalink");
  489. auto res = m.execute(std::move(req), e_.get());
  490. CPPUNIT_ASSERT_EQUAL(1, res.code);
  491. }
  492. void RpcMethodTest::testAddMetalink_withPosition()
  493. {
  494. AddUriRpcMethod m1;
  495. auto req1 = createReq(AddUriRpcMethod::getMethodName());
  496. auto urisParam1 = List::g();
  497. urisParam1->append("http://uri");
  498. req1.params->append(std::move(urisParam1));
  499. auto res1 = m1.execute(std::move(req1), e_.get());
  500. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  501. AddMetalinkRpcMethod m2;
  502. auto req2 = createReq(AddMetalinkRpcMethod::getMethodName());
  503. req2.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
  504. req2.params->append(Dict::g());
  505. req2.params->append(Integer::g(0));
  506. auto res2 = m2.execute(std::move(req2), e_.get());
  507. CPPUNIT_ASSERT_EQUAL(0, res2.code);
  508. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
  509. getReservedGroup(e_->getRequestGroupMan().get(), 0)->
  510. getFirstFilePath());
  511. }
  512. #endif // ENABLE_METALINK
  513. void RpcMethodTest::testGetOption()
  514. {
  515. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  516. group->getOption()->put(PREF_DIR, "alpha");
  517. e_->getRequestGroupMan()->addReservedGroup(group);
  518. auto dr = createDownloadResult(error_code::FINISHED, "http://host/fin");
  519. dr->option->put(PREF_DIR, "bravo");
  520. e_->getRequestGroupMan()->addDownloadResult(dr);
  521. GetOptionRpcMethod m;
  522. auto req = createReq(GetOptionRpcMethod::getMethodName());
  523. req.params->append(GroupId::toHex(group->getGID()));
  524. auto res = m.execute(std::move(req), e_.get());
  525. CPPUNIT_ASSERT_EQUAL(0, res.code);
  526. const Dict* resopt = downcast<Dict>(res.param);
  527. CPPUNIT_ASSERT_EQUAL(std::string("alpha"),
  528. downcast<String>(resopt->get(PREF_DIR->k))->s());
  529. req = createReq(GetOptionRpcMethod::getMethodName());
  530. req.params->append(dr->gid->toHex());
  531. res = m.execute(std::move(req), e_.get());
  532. CPPUNIT_ASSERT_EQUAL(0, res.code);
  533. resopt = downcast<Dict>(res.param);
  534. CPPUNIT_ASSERT_EQUAL(std::string("bravo"),
  535. downcast<String>(resopt->get(PREF_DIR->k))->s());
  536. // Invalid GID
  537. req = createReq(GetOptionRpcMethod::getMethodName());
  538. req.params->append(GroupId::create()->toHex());
  539. res = m.execute(std::move(req), e_.get());
  540. CPPUNIT_ASSERT_EQUAL(1, res.code);
  541. }
  542. void RpcMethodTest::testChangeOption()
  543. {
  544. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  545. e_->getRequestGroupMan()->addReservedGroup(group);
  546. ChangeOptionRpcMethod m;
  547. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  548. req.params->append(GroupId::toHex(group->getGID()));
  549. auto opt = Dict::g();
  550. opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "100K");
  551. #ifdef ENABLE_BITTORRENT
  552. opt->put(PREF_BT_MAX_PEERS->k, "100");
  553. opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K");
  554. opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K");
  555. {
  556. auto btObject = make_unique<BtObject>();
  557. btObject->btRuntime = std::make_shared<BtRuntime>();
  558. e_->getBtRegistry()->put(group->getGID(), std::move(btObject));
  559. }
  560. #endif // ENABLE_BITTORRENT
  561. req.params->append(std::move(opt));
  562. auto res = m.execute(std::move(req), e_.get());
  563. auto option = group->getOption();
  564. CPPUNIT_ASSERT_EQUAL(0, res.code);
  565. CPPUNIT_ASSERT_EQUAL(100*1024,
  566. group->getMaxDownloadSpeedLimit());
  567. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  568. option->get(PREF_MAX_DOWNLOAD_LIMIT));
  569. #ifdef ENABLE_BITTORRENT
  570. CPPUNIT_ASSERT_EQUAL(std::string("307200"),
  571. option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
  572. CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
  573. CPPUNIT_ASSERT_EQUAL(100,
  574. e_->getBtRegistry()->get(group->getGID())
  575. ->btRuntime->getMaxPeers());
  576. CPPUNIT_ASSERT_EQUAL(50*1024,
  577. group->getMaxUploadSpeedLimit());
  578. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  579. option->get(PREF_MAX_UPLOAD_LIMIT));
  580. #endif // ENABLE_BITTORRENT
  581. }
  582. void RpcMethodTest::testChangeOption_withBadOption()
  583. {
  584. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  585. e_->getRequestGroupMan()->addReservedGroup(group);
  586. ChangeOptionRpcMethod m;
  587. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  588. req.params->append(GroupId::toHex(group->getGID()));
  589. auto opt = Dict::g();
  590. opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "badvalue");
  591. req.params->append(std::move(opt));
  592. auto res = m.execute(std::move(req), e_.get());
  593. CPPUNIT_ASSERT_EQUAL(1, res.code);
  594. }
  595. void RpcMethodTest::testChangeOption_withNotAllowedOption()
  596. {
  597. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  598. e_->getRequestGroupMan()->addReservedGroup(group);
  599. ChangeOptionRpcMethod m;
  600. auto req = createReq(ChangeOptionRpcMethod::getMethodName());
  601. req.params->append(GroupId::toHex(group->getGID()));
  602. auto opt = Dict::g();
  603. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
  604. req.params->append(std::move(opt));
  605. auto res = m.execute(std::move(req), e_.get());
  606. // The unacceptable options are just ignored.
  607. CPPUNIT_ASSERT_EQUAL(0, res.code);
  608. }
  609. void RpcMethodTest::testChangeOption_withoutGid()
  610. {
  611. ChangeOptionRpcMethod m;
  612. auto res = m.execute
  613. (createReq(ChangeOptionRpcMethod::getMethodName()), e_.get());
  614. CPPUNIT_ASSERT_EQUAL(1, res.code);
  615. }
  616. void RpcMethodTest::testChangeGlobalOption()
  617. {
  618. ChangeGlobalOptionRpcMethod m;
  619. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  620. auto opt = Dict::g();
  621. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
  622. #ifdef ENABLE_BITTORRENT
  623. opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT->k, "50K");
  624. #endif // ENABLE_BITTORRENT
  625. req.params->append(std::move(opt));
  626. auto res = m.execute(std::move(req), e_.get());
  627. CPPUNIT_ASSERT_EQUAL(0, res.code);
  628. CPPUNIT_ASSERT_EQUAL
  629. (100*1024,
  630. e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit());
  631. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  632. e_->getOption()->get(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
  633. #ifdef ENABLE_BITTORRENT
  634. CPPUNIT_ASSERT_EQUAL
  635. (50*1024,
  636. e_->getRequestGroupMan()->getMaxOverallUploadSpeedLimit());
  637. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  638. e_->getOption()->get(PREF_MAX_OVERALL_UPLOAD_LIMIT));
  639. #endif // ENABLE_BITTORRENT
  640. }
  641. void RpcMethodTest::testChangeGlobalOption_withBadOption()
  642. {
  643. ChangeGlobalOptionRpcMethod m;
  644. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  645. auto opt = Dict::g();
  646. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "badvalue");
  647. req.params->append(std::move(opt));
  648. auto res = m.execute(std::move(req), e_.get());
  649. CPPUNIT_ASSERT_EQUAL(1, res.code);
  650. }
  651. void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
  652. {
  653. ChangeGlobalOptionRpcMethod m;
  654. auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
  655. auto opt = Dict::g();
  656. opt->put(PREF_ENABLE_RPC->k, "100K");
  657. req.params->append(std::move(opt));
  658. auto res = m.execute(std::move(req), e_.get());
  659. // The unacceptable options are just ignored.
  660. CPPUNIT_ASSERT_EQUAL(0, res.code);
  661. }
  662. void RpcMethodTest::testNoSuchMethod()
  663. {
  664. NoSuchMethodRpcMethod m;
  665. auto res = m.execute(createReq("make.hamburger"), e_.get());
  666. CPPUNIT_ASSERT_EQUAL(1, res.code);
  667. CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
  668. getString(downcast<Dict>(res.param), "faultString"));
  669. }
  670. void RpcMethodTest::testTellStatus_withoutGid()
  671. {
  672. TellStatusRpcMethod m;
  673. auto res = m.execute
  674. (createReq(TellStatusRpcMethod::getMethodName()), e_.get());
  675. CPPUNIT_ASSERT_EQUAL(1, res.code);
  676. }
  677. namespace {
  678. void addUri(const std::string& uri, const std::shared_ptr<DownloadEngine>& e)
  679. {
  680. AddUriRpcMethod m;
  681. auto req = createReq(AddUriRpcMethod::getMethodName());
  682. auto urisParam = List::g();
  683. urisParam->append(uri);
  684. req.params->append(std::move(urisParam));
  685. CPPUNIT_ASSERT_EQUAL(0, m.execute(std::move(req), e.get()).code);
  686. }
  687. } // namespace
  688. #ifdef ENABLE_BITTORRENT
  689. namespace {
  690. void addTorrent
  691. (const std::string& torrentFile, const std::shared_ptr<DownloadEngine>& e)
  692. {
  693. AddTorrentRpcMethod m;
  694. auto req = createReq(AddTorrentRpcMethod::getMethodName());
  695. req.params->append(readFile(torrentFile));
  696. auto res = m.execute(std::move(req), e.get());
  697. }
  698. } // namespace
  699. #endif // ENABLE_BITTORRENT
  700. void RpcMethodTest::testTellWaiting()
  701. {
  702. addUri("http://1/", e_);
  703. addUri("http://2/", e_);
  704. addUri("http://3/", e_);
  705. #ifdef ENABLE_BITTORRENT
  706. addTorrent(A2_TEST_DIR"/single.torrent", e_);
  707. #else // !ENABLE_BITTORRENT
  708. addUri("http://4/", e_);
  709. #endif // !ENABLE_BITTORRENT
  710. auto& rgman = e_->getRequestGroupMan();
  711. TellWaitingRpcMethod m;
  712. auto req = createReq(TellWaitingRpcMethod::getMethodName());
  713. req.params->append(Integer::g(1));
  714. req.params->append(Integer::g(2));
  715. auto res = m.execute(std::move(req), e_.get());
  716. CPPUNIT_ASSERT_EQUAL(0, res.code);
  717. const List* resParams = downcast<List>(res.param);
  718. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  719. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 1)
  720. ->getGID()),
  721. getString(downcast<Dict>(resParams->get(0)), "gid"));
  722. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 2)
  723. ->getGID()),
  724. getString(downcast<Dict>(resParams->get(1)), "gid"));
  725. // waiting.size() == offset+num
  726. req = createReq(TellWaitingRpcMethod::getMethodName());
  727. req.params->append(Integer::g(1));
  728. req.params->append(Integer::g(3));
  729. res = m.execute(std::move(req), e_.get());
  730. CPPUNIT_ASSERT_EQUAL(0, res.code);
  731. resParams = downcast<List>(res.param);
  732. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  733. // waiting.size() < offset+num
  734. req = createReq(TellWaitingRpcMethod::getMethodName());
  735. req.params->append(Integer::g(1));
  736. req.params->append(Integer::g(4));
  737. res = m.execute(std::move(req), e_.get());
  738. CPPUNIT_ASSERT_EQUAL(0, res.code);
  739. resParams = downcast<List>(res.param);
  740. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  741. // offset = INT32_MAX
  742. req = createReq(TellWaitingRpcMethod::getMethodName());
  743. req.params->append(Integer::g(INT32_MAX));
  744. req.params->append(Integer::g(1));
  745. res = m.execute(std::move(req), e_.get());
  746. CPPUNIT_ASSERT_EQUAL(0, res.code);
  747. resParams = downcast<List>(res.param);
  748. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  749. // num = INT32_MAX
  750. req = createReq(TellWaitingRpcMethod::getMethodName());
  751. req.params->append(Integer::g(1));
  752. req.params->append(Integer::g(INT32_MAX));
  753. res = m.execute(std::move(req), e_.get());
  754. CPPUNIT_ASSERT_EQUAL(0, res.code);
  755. resParams = downcast<List>(res.param);
  756. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  757. // offset=INT32_MAX and num = INT32_MAX
  758. req = createReq(TellWaitingRpcMethod::getMethodName());
  759. req.params->append(Integer::g(INT32_MAX));
  760. req.params->append(Integer::g(INT32_MAX));
  761. res = m.execute(std::move(req), e_.get());
  762. CPPUNIT_ASSERT_EQUAL(0, res.code);
  763. resParams = downcast<List>(res.param);
  764. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  765. // offset=INT32_MIN and num = INT32_MAX
  766. req = createReq(TellWaitingRpcMethod::getMethodName());
  767. req.params->append(Integer::g(INT32_MIN));
  768. req.params->append(Integer::g(INT32_MAX));
  769. res = m.execute(std::move(req), e_.get());
  770. CPPUNIT_ASSERT_EQUAL(0, res.code);
  771. resParams = downcast<List>(res.param);
  772. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  773. // negative offset
  774. req = createReq(TellWaitingRpcMethod::getMethodName());
  775. req.params->append(Integer::g(-1));
  776. req.params->append(Integer::g(2));
  777. res = m.execute(std::move(req), e_.get());
  778. CPPUNIT_ASSERT_EQUAL(0, res.code);
  779. resParams = downcast<List>(res.param);
  780. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  781. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(),
  782. 3)->getGID()),
  783. getString(downcast<Dict>(resParams->get(0)), "gid"));
  784. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(),
  785. 2)->getGID()),
  786. getString(downcast<Dict>(resParams->get(1)), "gid"));
  787. // negative offset and size < num
  788. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  789. req.params->append(Integer::g(-1));
  790. req.params->append(Integer::g(100));
  791. res = m.execute(std::move(req), e_.get());
  792. CPPUNIT_ASSERT_EQUAL(0, res.code);
  793. resParams = downcast<List>(res.param);
  794. CPPUNIT_ASSERT_EQUAL((size_t)4, resParams->size());
  795. // nagative offset and normalized offset < 0
  796. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  797. req.params->append(Integer::g(-5));
  798. req.params->append(Integer::g(100));
  799. res = m.execute(std::move(req), e_.get());
  800. CPPUNIT_ASSERT_EQUAL(0, res.code);
  801. resParams = downcast<List>(res.param);
  802. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  803. // nagative offset and normalized offset == 0
  804. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  805. req.params->append(Integer::g(-4));
  806. req.params->append(Integer::g(100));
  807. res = m.execute(std::move(req), e_.get());
  808. CPPUNIT_ASSERT_EQUAL(0, res.code);
  809. resParams = downcast<List>(res.param);
  810. CPPUNIT_ASSERT_EQUAL((size_t)1, resParams->size());
  811. }
  812. void RpcMethodTest::testTellWaiting_fail()
  813. {
  814. TellWaitingRpcMethod m;
  815. auto res = m.execute(createReq(TellWaitingRpcMethod::getMethodName()),
  816. e_.get());
  817. CPPUNIT_ASSERT_EQUAL(1, res.code);
  818. }
  819. void RpcMethodTest::testGetVersion()
  820. {
  821. GetVersionRpcMethod m;
  822. auto res = m.execute(createReq(GetVersionRpcMethod::getMethodName()),
  823. e_.get());
  824. CPPUNIT_ASSERT_EQUAL(0, res.code);
  825. const Dict* resParams = downcast<Dict>(res.param);
  826. CPPUNIT_ASSERT_EQUAL(std::string(PACKAGE_VERSION),
  827. getString(resParams, "version"));
  828. const List* featureList = downcast<List>(resParams->get("enabledFeatures"));
  829. std::string features;
  830. for(auto i = featureList->begin(); i != featureList->end(); ++i) {
  831. const String* s = downcast<String>(*i);
  832. features += s->s();
  833. features += ", ";
  834. }
  835. CPPUNIT_ASSERT_EQUAL(featureSummary()+", ", features);
  836. }
  837. void RpcMethodTest::testGatherStoppedDownload()
  838. {
  839. std::vector<std::shared_ptr<FileEntry>> fileEntries;
  840. std::vector<a2_gid_t> followedBy;
  841. followedBy.push_back(3);
  842. followedBy.push_back(4);
  843. auto d = std::make_shared<DownloadResult>();
  844. d->gid = GroupId::create();
  845. d->fileEntries = fileEntries;
  846. d->inMemoryDownload = false;
  847. d->sessionDownloadLength = UINT64_MAX;
  848. d->sessionTime = 1000;
  849. d->result = error_code::FINISHED;
  850. d->followedBy = followedBy;
  851. d->belongsTo = 2;
  852. auto entry = Dict::g();
  853. std::vector<std::string> keys;
  854. gatherStoppedDownload(entry.get(), d, keys);
  855. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  856. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(3),
  857. downcast<String>(followedByRes->get(0))->s());
  858. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(4),
  859. downcast<String>(followedByRes->get(1))->s());
  860. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(2),
  861. downcast<String>(entry->get("belongsTo"))->s());
  862. keys.push_back("gid");
  863. entry = Dict::g();
  864. gatherStoppedDownload(entry.get(), d, keys);
  865. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  866. CPPUNIT_ASSERT(entry->containsKey("gid"));
  867. }
  868. void RpcMethodTest::testGatherProgressCommon()
  869. {
  870. auto dctx = std::make_shared<DownloadContext>(0, 0,"aria2.tar.bz2");
  871. std::string uris[] = { "http://localhost/aria2.tar.bz2" };
  872. dctx->getFirstFileEntry()->addUris(std::begin(uris), std::end(uris));
  873. auto group = std::make_shared<RequestGroup>(GroupId::create(),
  874. util::copy(option_));
  875. group->setDownloadContext(dctx);
  876. std::vector<std::shared_ptr<RequestGroup>> followedBy;
  877. for(int i = 0; i < 2; ++i) {
  878. followedBy.push_back(std::make_shared<RequestGroup>(GroupId::create(),
  879. util::copy(option_)));
  880. }
  881. group->followedBy(followedBy.begin(), followedBy.end());
  882. auto parent = GroupId::create();
  883. group->belongsTo(parent->getNumericId());
  884. auto entry = Dict::g();
  885. std::vector<std::string> keys;
  886. gatherProgressCommon(entry.get(), group, keys);
  887. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  888. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[0]->getGID()),
  889. downcast<String>(followedByRes->get(0))->s());
  890. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[1]->getGID()),
  891. downcast<String>(followedByRes->get(1))->s());
  892. CPPUNIT_ASSERT_EQUAL(parent->toHex(),
  893. downcast<String>(entry->get("belongsTo"))->s());
  894. const List* files = downcast<List>(entry->get("files"));
  895. CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
  896. const Dict* file = downcast<Dict>(files->get(0));
  897. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  898. downcast<String>(file->get("path"))->s());
  899. CPPUNIT_ASSERT_EQUAL(uris[0],
  900. downcast<String>
  901. (downcast<Dict>
  902. (downcast<List>(file->get("uris"))->get(0))
  903. ->get("uri"))
  904. ->s());
  905. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR),
  906. downcast<String>(entry->get("dir"))->s());
  907. keys.push_back("gid");
  908. entry = Dict::g();
  909. gatherProgressCommon(entry.get(), group, keys);
  910. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  911. CPPUNIT_ASSERT(entry->containsKey("gid"));
  912. }
  913. #ifdef ENABLE_BITTORRENT
  914. void RpcMethodTest::testGatherBitTorrentMetadata()
  915. {
  916. auto option = std::make_shared<Option>();
  917. option->put(PREF_DIR, ".");
  918. auto dctx = std::make_shared<DownloadContext>();
  919. bittorrent::load(A2_TEST_DIR"/test.torrent", dctx, option);
  920. auto btDict = Dict::g();
  921. gatherBitTorrentMetadata(btDict.get(), bittorrent::getTorrentAttrs(dctx));
  922. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  923. downcast<String>(btDict->get("comment"))->s());
  924. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  925. downcast<Integer>(btDict->get("creationDate"))->i());
  926. CPPUNIT_ASSERT_EQUAL(std::string("multi"),
  927. downcast<String>(btDict->get("mode"))->s());
  928. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"),
  929. downcast<String>
  930. (downcast<Dict>
  931. (btDict->get("info"))
  932. ->get("name"))
  933. ->s());
  934. const List* announceList = downcast<List>(btDict->get("announceList"));
  935. CPPUNIT_ASSERT_EQUAL((size_t)3, announceList->size());
  936. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  937. downcast<String>(downcast<List>(announceList->get(0))->get(0))->s());
  938. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  939. downcast<String>(downcast<List>(announceList->get(1))->get(0))->s());
  940. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  941. downcast<String>(downcast<List>(announceList->get(2))->get(0))->s());
  942. // Remove some keys
  943. auto modBtAttrs = bittorrent::getTorrentAttrs(dctx);
  944. modBtAttrs->comment.clear();
  945. modBtAttrs->creationDate = 0;
  946. modBtAttrs->mode = BT_FILE_MODE_NONE;
  947. modBtAttrs->metadata.clear();
  948. btDict = Dict::g();
  949. gatherBitTorrentMetadata(btDict.get(), modBtAttrs);
  950. CPPUNIT_ASSERT(!btDict->containsKey("comment"));
  951. CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
  952. CPPUNIT_ASSERT(!btDict->containsKey("mode"));
  953. CPPUNIT_ASSERT(!btDict->containsKey("info"));
  954. CPPUNIT_ASSERT(btDict->containsKey("announceList"));
  955. }
  956. #endif // ENABLE_BITTORRENT
  957. void RpcMethodTest::testChangePosition()
  958. {
  959. e_->getRequestGroupMan()->addReservedGroup
  960. (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
  961. e_->getRequestGroupMan()->addReservedGroup
  962. (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
  963. a2_gid_t gid = getReservedGroup(e_->getRequestGroupMan().get(), 0)->getGID();
  964. ChangePositionRpcMethod m;
  965. auto req = createReq(ChangePositionRpcMethod::getMethodName());
  966. req.params->append(GroupId::toHex(gid));
  967. req.params->append(Integer::g(1));
  968. req.params->append("POS_SET");
  969. auto res = m.execute(std::move(req), e_.get());
  970. CPPUNIT_ASSERT_EQUAL(0, res.code);
  971. CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
  972. CPPUNIT_ASSERT_EQUAL
  973. (gid, getReservedGroup(e_->getRequestGroupMan().get(), 1)->getGID());
  974. }
  975. void RpcMethodTest::testChangePosition_fail()
  976. {
  977. ChangePositionRpcMethod m;
  978. auto res = m.execute
  979. (createReq(ChangePositionRpcMethod::getMethodName()), e_.get());
  980. CPPUNIT_ASSERT_EQUAL(1, res.code);
  981. auto req = createReq(ChangePositionRpcMethod::getMethodName());
  982. req.params->append("1");
  983. req.params->append(Integer::g(2));
  984. req.params->append("bad keyword");
  985. res = m.execute(std::move(req), e_.get());
  986. CPPUNIT_ASSERT_EQUAL(1, res.code);
  987. }
  988. namespace {
  989. RpcRequest createChangeUriReq(a2_gid_t gid, size_t fileIndex)
  990. {
  991. auto req = createReq(ChangeUriRpcMethod::getMethodName());
  992. req.params->append(GroupId::toHex(gid)); // GID
  993. req.params->append(Integer::g(fileIndex)); // index of FileEntry
  994. auto removeuris = List::g();
  995. removeuris->append("http://example.org/mustremove1");
  996. removeuris->append("http://example.org/mustremove2");
  997. removeuris->append("http://example.org/notexist");
  998. req.params->append(std::move(removeuris));
  999. return req;
  1000. }
  1001. } // namespace
  1002. void RpcMethodTest::testChangeUri()
  1003. {
  1004. std::shared_ptr<FileEntry> files[3];
  1005. for(int i = 0; i < 3; ++i) {
  1006. files[i].reset(new FileEntry());
  1007. }
  1008. files[1]->addUri("http://example.org/aria2.tar.bz2");
  1009. files[1]->addUri("http://example.org/mustremove1");
  1010. files[1]->addUri("http://example.org/mustremove2");
  1011. auto dctx = std::make_shared<DownloadContext>();
  1012. dctx->setFileEntries(&files[0], &files[3]);
  1013. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  1014. group->setDownloadContext(dctx);
  1015. e_->getRequestGroupMan()->addReservedGroup(group);
  1016. ChangeUriRpcMethod m;
  1017. auto req = createChangeUriReq(group->getGID(), 2);
  1018. auto adduris = List::g();
  1019. adduris->append("http://example.org/added1");
  1020. adduris->append("http://example.org/added2");
  1021. adduris->append("baduri");
  1022. adduris->append("http://example.org/added3");
  1023. req.params->append(std::move(adduris));
  1024. auto res = m.execute(std::move(req), e_.get());
  1025. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1026. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1027. CPPUNIT_ASSERT_EQUAL((int64_t)3, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1028. CPPUNIT_ASSERT_EQUAL((size_t)0, files[0]->getRemainingUris().size());
  1029. CPPUNIT_ASSERT_EQUAL((size_t)0, files[2]->getRemainingUris().size());
  1030. std::deque<std::string> uris = files[1]->getRemainingUris();
  1031. CPPUNIT_ASSERT_EQUAL((size_t)4, uris.size());
  1032. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/aria2.tar.bz2"),uris[0]);
  1033. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1"), uris[1]);
  1034. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
  1035. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
  1036. req = createChangeUriReq(group->getGID(), 2);
  1037. // Change adduris
  1038. adduris = List::g();
  1039. adduris->append("http://example.org/added1-1");
  1040. adduris->append("http://example.org/added1-2");
  1041. req.params->append(std::move(adduris));
  1042. // Set position parameter
  1043. req.params->append(Integer::g(2));
  1044. res = m.execute(std::move(req), e_.get());
  1045. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1046. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1047. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1048. uris = files[1]->getRemainingUris();
  1049. CPPUNIT_ASSERT_EQUAL((size_t)6, uris.size());
  1050. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[2]);
  1051. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
  1052. // Change index of FileEntry
  1053. req = createChangeUriReq(group->getGID(), 1);
  1054. adduris = List::g();
  1055. adduris->append("http://example.org/added1-1");
  1056. adduris->append("http://example.org/added1-2");
  1057. req.params->append(std::move(adduris));
  1058. // Set position far beyond the size of uris in FileEntry.
  1059. req.params->append(Integer::g(1000));
  1060. res = m.execute(std::move(req), e_.get());
  1061. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1062. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  1063. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  1064. uris = files[0]->getRemainingUris();
  1065. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  1066. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[0]);
  1067. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
  1068. }
  1069. namespace {
  1070. RpcRequest createChangeUriEmptyReq(a2_gid_t gid, size_t fileIndex)
  1071. {
  1072. auto req = createReq(ChangeUriRpcMethod::getMethodName());
  1073. req.params->append(GroupId::toHex(gid)); // GID
  1074. req.params->append(Integer::g(fileIndex)); // index of FileEntry
  1075. req.params->append(List::g()); // remove uris
  1076. req.params->append(List::g()); // append uris
  1077. return req;
  1078. }
  1079. } // namespace
  1080. void RpcMethodTest::testChangeUri_fail()
  1081. {
  1082. std::shared_ptr<FileEntry> files[3];
  1083. for(int i = 0; i < 3; ++i) {
  1084. files[i] = std::make_shared<FileEntry>();
  1085. }
  1086. auto dctx = std::make_shared<DownloadContext>();
  1087. dctx->setFileEntries(&files[0], &files[3]);
  1088. auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
  1089. group->setDownloadContext(dctx);
  1090. e_->getRequestGroupMan()->addReservedGroup(group);
  1091. ChangeUriRpcMethod m;
  1092. auto req = createChangeUriEmptyReq(group->getGID(), 1);
  1093. auto res = m.execute(std::move(req), e_.get());
  1094. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1095. req = createChangeUriEmptyReq(group->getGID(), 0);
  1096. res = m.execute(std::move(req), e_.get());
  1097. // RPC request fails because 2nd argument is less than 1.
  1098. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1099. req = createChangeUriEmptyReq(GroupId::create()->getNumericId(), 1);
  1100. res = m.execute(std::move(req), e_.get());
  1101. // RPC request fails because the given GID does not exist.
  1102. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1103. req = createChangeUriEmptyReq(group->getGID(), 4);
  1104. res = m.execute(std::move(req), e_.get());
  1105. // RPC request fails because FileEntry#3 does not exist.
  1106. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1107. req = createChangeUriEmptyReq(group->getGID(), 1);
  1108. req.params->set(1, String::g("0"));
  1109. res = m.execute(std::move(req), e_.get());
  1110. // RPC request fails because index of FileEntry is string.
  1111. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1112. req = createChangeUriEmptyReq(group->getGID(), 1);
  1113. req.params->set(2, String::g("http://url"));
  1114. res = m.execute(std::move(req), e_.get());
  1115. // RPC request fails because 3rd param is not list.
  1116. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1117. req = createChangeUriEmptyReq(group->getGID(), 1);
  1118. req.params->set(2, List::g());
  1119. req.params->set(3, String::g("http://url"));
  1120. res = m.execute(std::move(req), e_.get());
  1121. // RPC request fails because 4th param is not list.
  1122. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1123. }
  1124. void RpcMethodTest::testGetSessionInfo()
  1125. {
  1126. GetSessionInfoRpcMethod m;
  1127. auto res = m.execute
  1128. (createReq(GetSessionInfoRpcMethod::getMethodName()), e_.get());
  1129. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1130. CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
  1131. getString(downcast<Dict>(res.param), "sessionId"));
  1132. }
  1133. void RpcMethodTest::testPause()
  1134. {
  1135. std::vector<std::string> uris {
  1136. "http://url1",
  1137. "http://url2",
  1138. "http://url3",
  1139. };
  1140. option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
  1141. std::vector<std::shared_ptr<RequestGroup>> groups;
  1142. createRequestGroupForUri(groups, option_, uris);
  1143. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  1144. e_->getRequestGroupMan()->addReservedGroup(groups);
  1145. {
  1146. PauseRpcMethod m;
  1147. auto req = createReq(PauseRpcMethod::getMethodName());
  1148. req.params->append(GroupId::toHex(groups[0]->getGID()));
  1149. auto res = m.execute(std::move(req), e_.get());
  1150. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1151. }
  1152. CPPUNIT_ASSERT(groups[0]->isPauseRequested());
  1153. {
  1154. UnpauseRpcMethod m;
  1155. auto req = createReq(UnpauseRpcMethod::getMethodName());
  1156. req.params->append(GroupId::toHex(groups[0]->getGID()));
  1157. auto res = m.execute(std::move(req), e_.get());
  1158. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1159. }
  1160. CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
  1161. {
  1162. PauseAllRpcMethod m;
  1163. auto req = createReq(PauseAllRpcMethod::getMethodName());
  1164. auto res = m.execute(std::move(req), e_.get());
  1165. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1166. }
  1167. for(size_t i = 0; i < groups.size(); ++i) {
  1168. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  1169. }
  1170. {
  1171. UnpauseAllRpcMethod m;
  1172. auto req = createReq(UnpauseAllRpcMethod::getMethodName());
  1173. auto res = m.execute(std::move(req), e_.get());
  1174. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1175. }
  1176. for(size_t i = 0; i < groups.size(); ++i) {
  1177. CPPUNIT_ASSERT(!groups[i]->isPauseRequested());
  1178. }
  1179. {
  1180. ForcePauseAllRpcMethod m;
  1181. auto req = createReq(ForcePauseAllRpcMethod::getMethodName());
  1182. auto res = m.execute(std::move(req), e_.get());
  1183. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1184. }
  1185. for(size_t i = 0; i < groups.size(); ++i) {
  1186. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  1187. }
  1188. }
  1189. void RpcMethodTest::testSystemMulticall()
  1190. {
  1191. SystemMulticallRpcMethod m;
  1192. auto req = createReq("system.multicall");
  1193. auto reqparams = List::g();
  1194. for(int i = 0; i < 2; ++i) {
  1195. auto dict = Dict::g();
  1196. dict->put("methodName", AddUriRpcMethod::getMethodName());
  1197. auto params = List::g();
  1198. auto urisParam = List::g();
  1199. urisParam->append("http://localhost/"+util::itos(i));
  1200. params->append(std::move(urisParam));
  1201. dict->put("params", std::move(params));
  1202. reqparams->append(std::move(dict));
  1203. }
  1204. {
  1205. auto dict = Dict::g();
  1206. dict->put("methodName", "not exists");
  1207. dict->put("params", List::g());
  1208. reqparams->append(std::move(dict));
  1209. }
  1210. {
  1211. reqparams->append("not struct");
  1212. }
  1213. {
  1214. auto dict = Dict::g();
  1215. dict->put("methodName", "system.multicall");
  1216. dict->put("params", List::g());
  1217. reqparams->append(std::move(dict));
  1218. }
  1219. {
  1220. // missing params
  1221. auto dict = Dict::g();
  1222. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1223. reqparams->append(std::move(dict));
  1224. }
  1225. {
  1226. auto dict = Dict::g();
  1227. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1228. dict->put("params", List::g());
  1229. reqparams->append(std::move(dict));
  1230. }
  1231. req.params->append(std::move(reqparams));
  1232. auto res = m.execute(std::move(req), e_.get());
  1233. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1234. const List* resParams = downcast<List>(res.param);
  1235. CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
  1236. auto& rgman = e_->getRequestGroupMan();
  1237. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 0)
  1238. ->getGID()),
  1239. downcast<String>(downcast<List>(resParams->get(0))->get(0))->s());
  1240. CPPUNIT_ASSERT_EQUAL(GroupId::toHex(getReservedGroup(rgman.get(), 1)
  1241. ->getGID()),
  1242. downcast<String>(downcast<List>(resParams->get(1))->get(0))->s());
  1243. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1244. downcast<Integer>
  1245. (downcast<Dict>(resParams->get(2))->get("faultCode"))
  1246. ->i());
  1247. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1248. downcast<Integer>
  1249. (downcast<Dict>(resParams->get(3))->get("faultCode"))
  1250. ->i());
  1251. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1252. downcast<Integer>
  1253. (downcast<Dict>(resParams->get(4))->get("faultCode"))
  1254. ->i());
  1255. CPPUNIT_ASSERT(downcast<List>(resParams->get(5)));
  1256. CPPUNIT_ASSERT(downcast<List>(resParams->get(6)));
  1257. }
  1258. void RpcMethodTest::testSystemMulticall_fail()
  1259. {
  1260. SystemMulticallRpcMethod m;
  1261. auto res = m.execute(createReq("system.multicall"), e_.get());
  1262. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1263. }
  1264. } // namespace rpc
  1265. } // namespace aria2