RpcMethodTest.cc 49 KB

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