RpcMethodTest.cc 47 KB

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