RpcMethodTest.cc 46 KB

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