RpcMethodTest.cc 42 KB

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