RpcMethodTest.cc 49 KB

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