RpcMethodTest.cc 51 KB

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