RpcMethodTest.cc 43 KB

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