RpcMethodTest.cc 48 KB

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