RpcMethodTest.cc 47 KB

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