RpcMethodTest.cc 51 KB

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