RpcMethodTest.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. // The unacceptable options are just ignored.
  465. CPPUNIT_ASSERT_EQUAL(0, res.code);
  466. }
  467. void RpcMethodTest::testChangeOption_withoutGid()
  468. {
  469. ChangeOptionRpcMethod m;
  470. RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
  471. RpcResponse res = m.execute(req, e_.get());
  472. CPPUNIT_ASSERT_EQUAL(1, res.code);
  473. }
  474. void RpcMethodTest::testChangeGlobalOption()
  475. {
  476. ChangeGlobalOptionRpcMethod m;
  477. RpcRequest req
  478. (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
  479. SharedHandle<Dict> opt = Dict::g();
  480. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
  481. #ifdef ENABLE_BITTORRENT
  482. opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT->k, "50K");
  483. #endif // ENABLE_BITTORRENT
  484. req.params->append(opt);
  485. RpcResponse res = m.execute(req, e_.get());
  486. CPPUNIT_ASSERT_EQUAL(0, res.code);
  487. CPPUNIT_ASSERT_EQUAL
  488. (100*1024,
  489. e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit());
  490. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  491. e_->getOption()->get(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
  492. #ifdef ENABLE_BITTORRENT
  493. CPPUNIT_ASSERT_EQUAL
  494. (50*1024,
  495. e_->getRequestGroupMan()->getMaxOverallUploadSpeedLimit());
  496. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  497. e_->getOption()->get(PREF_MAX_OVERALL_UPLOAD_LIMIT));
  498. #endif // ENABLE_BITTORRENT
  499. }
  500. void RpcMethodTest::testChangeGlobalOption_withBadOption()
  501. {
  502. ChangeGlobalOptionRpcMethod m;
  503. RpcRequest req
  504. (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
  505. SharedHandle<Dict> opt = Dict::g();
  506. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "badvalue");
  507. req.params->append(opt);
  508. RpcResponse res = m.execute(req, e_.get());
  509. CPPUNIT_ASSERT_EQUAL(1, res.code);
  510. }
  511. void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
  512. {
  513. ChangeGlobalOptionRpcMethod m;
  514. RpcRequest req
  515. (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
  516. SharedHandle<Dict> opt = Dict::g();
  517. opt->put(PREF_ENABLE_RPC->k, "100K");
  518. req.params->append(opt);
  519. RpcResponse res = m.execute(req, e_.get());
  520. // The unacceptable options are just ignored.
  521. CPPUNIT_ASSERT_EQUAL(0, res.code);
  522. }
  523. void RpcMethodTest::testNoSuchMethod()
  524. {
  525. NoSuchMethodRpcMethod m;
  526. RpcRequest req("make.hamburger", List::g());
  527. RpcResponse res = m.execute(req, 0);
  528. CPPUNIT_ASSERT_EQUAL(1, res.code);
  529. CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
  530. getString(downcast<Dict>(res.param), "faultString"));
  531. }
  532. void RpcMethodTest::testTellStatus_withoutGid()
  533. {
  534. TellStatusRpcMethod m;
  535. RpcRequest req(TellStatusRpcMethod::getMethodName(), List::g());
  536. RpcResponse res = m.execute(req, e_.get());
  537. CPPUNIT_ASSERT_EQUAL(1, res.code);
  538. }
  539. namespace {
  540. void addUri(const std::string& uri, const SharedHandle<DownloadEngine>& e)
  541. {
  542. AddUriRpcMethod m;
  543. RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
  544. SharedHandle<List> urisParam = List::g();
  545. urisParam->append(uri);
  546. req.params->append(urisParam);
  547. CPPUNIT_ASSERT_EQUAL(0, m.execute(req, e.get()).code);
  548. }
  549. } // namespace
  550. #ifdef ENABLE_BITTORRENT
  551. namespace {
  552. void addTorrent
  553. (const std::string& torrentFile, const SharedHandle<DownloadEngine>& e)
  554. {
  555. AddTorrentRpcMethod m;
  556. RpcRequest req(AddTorrentRpcMethod::getMethodName(), List::g());
  557. req.params->append(readFile(torrentFile));
  558. RpcResponse res = m.execute(req, e.get());
  559. }
  560. } // namespace
  561. #endif // ENABLE_BITTORRENT
  562. void RpcMethodTest::testTellWaiting()
  563. {
  564. addUri("http://1/", e_);
  565. addUri("http://2/", e_);
  566. addUri("http://3/", e_);
  567. #ifdef ENABLE_BITTORRENT
  568. addTorrent(A2_TEST_DIR"/single.torrent", e_);
  569. #else // !ENABLE_BITTORRENT
  570. addUri("http://4/", e_);
  571. #endif // !ENABLE_BITTORRENT
  572. TellWaitingRpcMethod m;
  573. RpcRequest req(TellWaitingRpcMethod::getMethodName(), List::g());
  574. req.params->append(Integer::g(1));
  575. req.params->append(Integer::g(2));
  576. RpcResponse res = m.execute(req, e_.get());
  577. CPPUNIT_ASSERT_EQUAL(0, res.code);
  578. const List* resParams = downcast<List>(res.param);
  579. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  580. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  581. getString(downcast<Dict>(resParams->get(0)), "gid"));
  582. CPPUNIT_ASSERT_EQUAL(std::string("3"),
  583. getString(downcast<Dict>(resParams->get(1)), "gid"));
  584. // waiting.size() == offset+num
  585. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  586. req.params->append(Integer::g(1));
  587. req.params->append(Integer::g(3));
  588. res = m.execute(req, e_.get());
  589. CPPUNIT_ASSERT_EQUAL(0, res.code);
  590. resParams = downcast<List>(res.param);
  591. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  592. // waiting.size() < offset+num
  593. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  594. req.params->append(Integer::g(1));
  595. req.params->append(Integer::g(4));
  596. res = m.execute(req, e_.get());
  597. CPPUNIT_ASSERT_EQUAL(0, res.code);
  598. resParams = downcast<List>(res.param);
  599. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  600. // offset = INT32_MAX
  601. req.params->set(0, Integer::g(INT32_MAX));
  602. req.params->set(1, Integer::g(1));
  603. res = m.execute(req, e_.get());
  604. CPPUNIT_ASSERT_EQUAL(0, res.code);
  605. resParams = downcast<List>(res.param);
  606. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  607. // num = INT32_MAX
  608. req.params->set(0, Integer::g(1));
  609. req.params->set(1, Integer::g(INT32_MAX));
  610. res = m.execute(req, e_.get());
  611. CPPUNIT_ASSERT_EQUAL(0, res.code);
  612. resParams = downcast<List>(res.param);
  613. CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
  614. // offset=INT32_MAX and num = INT32_MAX
  615. req.params->set(0, Integer::g(INT32_MAX));
  616. req.params->set(1, Integer::g(INT32_MAX));
  617. res = m.execute(req, e_.get());
  618. CPPUNIT_ASSERT_EQUAL(0, res.code);
  619. resParams = downcast<List>(res.param);
  620. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  621. // offset=INT32_MIN and num = INT32_MAX
  622. req.params->set(0, Integer::g(INT32_MIN));
  623. req.params->set(1, Integer::g(INT32_MAX));
  624. res = m.execute(req, e_.get());
  625. CPPUNIT_ASSERT_EQUAL(0, res.code);
  626. resParams = downcast<List>(res.param);
  627. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  628. // negative offset
  629. req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
  630. req.params->append(Integer::g(-1));
  631. req.params->append(Integer::g(2));
  632. res = m.execute(req, e_.get());
  633. CPPUNIT_ASSERT_EQUAL(0, res.code);
  634. resParams = downcast<List>(res.param);
  635. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  636. CPPUNIT_ASSERT_EQUAL(std::string("4"),
  637. getString(downcast<Dict>(resParams->get(0)), "gid"));
  638. CPPUNIT_ASSERT_EQUAL(std::string("3"),
  639. getString(downcast<Dict>(resParams->get(1)), "gid"));
  640. // negative offset and size < num
  641. req.params->set(1, Integer::g(100));
  642. res = m.execute(req, e_.get());
  643. CPPUNIT_ASSERT_EQUAL(0, res.code);
  644. resParams = downcast<List>(res.param);
  645. CPPUNIT_ASSERT_EQUAL((size_t)4, resParams->size());
  646. // nagative offset and normalized offset < 0
  647. req.params->set(0, Integer::g(-5));
  648. res = m.execute(req, e_.get());
  649. CPPUNIT_ASSERT_EQUAL(0, res.code);
  650. resParams = downcast<List>(res.param);
  651. CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
  652. // nagative offset and normalized offset == 0
  653. req.params->set(0, Integer::g(-4));
  654. res = m.execute(req, e_.get());
  655. CPPUNIT_ASSERT_EQUAL(0, res.code);
  656. resParams = downcast<List>(res.param);
  657. CPPUNIT_ASSERT_EQUAL((size_t)1, resParams->size());
  658. }
  659. void RpcMethodTest::testTellWaiting_fail()
  660. {
  661. TellWaitingRpcMethod m;
  662. RpcRequest req(TellWaitingRpcMethod::getMethodName(), List::g());
  663. RpcResponse res = m.execute(req, e_.get());
  664. CPPUNIT_ASSERT_EQUAL(1, res.code);
  665. }
  666. void RpcMethodTest::testGetVersion()
  667. {
  668. GetVersionRpcMethod m;
  669. RpcRequest req(GetVersionRpcMethod::getMethodName(), List::g());
  670. RpcResponse res = m.execute(req, e_.get());
  671. CPPUNIT_ASSERT_EQUAL(0, res.code);
  672. const Dict* resParams = downcast<Dict>(res.param);
  673. CPPUNIT_ASSERT_EQUAL(std::string(PACKAGE_VERSION),
  674. getString(resParams, "version"));
  675. const List* featureList = downcast<List>(resParams->get("enabledFeatures"));
  676. std::string features;
  677. for(List::ValueType::const_iterator i = featureList->begin();
  678. i != featureList->end(); ++i) {
  679. const String* s = downcast<String>(*i);
  680. features += s->s();
  681. features += ", ";
  682. }
  683. CPPUNIT_ASSERT_EQUAL(FeatureConfig::getInstance()->featureSummary()+", ",
  684. features);
  685. }
  686. void RpcMethodTest::testGatherStoppedDownload()
  687. {
  688. std::vector<SharedHandle<FileEntry> > fileEntries;
  689. std::vector<a2_gid_t> followedBy;
  690. followedBy.push_back(3);
  691. followedBy.push_back(4);
  692. SharedHandle<DownloadResult> d(new DownloadResult());
  693. d->gid = 1;
  694. d->fileEntries = fileEntries;
  695. d->inMemoryDownload = false;
  696. d->sessionDownloadLength = UINT64_MAX;
  697. d->sessionTime = 1000;
  698. d->result = error_code::FINISHED;
  699. d->followedBy = followedBy;
  700. d->belongsTo = 2;
  701. SharedHandle<Dict> entry = Dict::g();
  702. std::vector<std::string> keys;
  703. gatherStoppedDownload(entry, d, keys);
  704. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  705. CPPUNIT_ASSERT_EQUAL(std::string("3"), downcast<String>(followedByRes->get(0))->s());
  706. CPPUNIT_ASSERT_EQUAL(std::string("4"), downcast<String>(followedByRes->get(1))->s());
  707. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  708. downcast<String>(entry->get("belongsTo"))->s());
  709. keys.push_back("gid");
  710. entry = Dict::g();
  711. gatherStoppedDownload(entry, d, keys);
  712. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  713. CPPUNIT_ASSERT(entry->containsKey("gid"));
  714. }
  715. void RpcMethodTest::testGatherProgressCommon()
  716. {
  717. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0,"aria2.tar.bz2"));
  718. std::string uris[] = { "http://localhost/aria2.tar.bz2" };
  719. dctx->getFirstFileEntry()->addUris(vbegin(uris), vend(uris));
  720. SharedHandle<RequestGroup> group(new RequestGroup(util::copy(option_)));
  721. group->setDownloadContext(dctx);
  722. std::vector<SharedHandle<RequestGroup> > followedBy;
  723. for(int i = 0; i < 2; ++i) {
  724. followedBy.push_back(SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
  725. }
  726. group->followedBy(followedBy.begin(), followedBy.end());
  727. group->belongsTo(2);
  728. SharedHandle<Dict> entry = Dict::g();
  729. std::vector<std::string> keys;
  730. gatherProgressCommon(entry, group, keys);
  731. const List* followedByRes = downcast<List>(entry->get("followedBy"));
  732. CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[0]->getGID()),
  733. downcast<String>(followedByRes->get(0))->s());
  734. CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[1]->getGID()),
  735. downcast<String>(followedByRes->get(1))->s());
  736. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  737. downcast<String>(entry->get("belongsTo"))->s());
  738. const List* files = downcast<List>(entry->get("files"));
  739. CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
  740. const Dict* file = downcast<Dict>(files->get(0));
  741. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  742. downcast<String>(file->get("path"))->s());
  743. CPPUNIT_ASSERT_EQUAL(uris[0],
  744. downcast<String>
  745. (downcast<Dict>
  746. (downcast<List>(file->get("uris"))->get(0))
  747. ->get("uri"))
  748. ->s());
  749. CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR),
  750. downcast<String>(entry->get("dir"))->s());
  751. keys.push_back("gid");
  752. entry = Dict::g();
  753. gatherProgressCommon(entry, group, keys);
  754. CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
  755. CPPUNIT_ASSERT(entry->containsKey("gid"));
  756. }
  757. #ifdef ENABLE_BITTORRENT
  758. void RpcMethodTest::testGatherBitTorrentMetadata()
  759. {
  760. SharedHandle<Option> option(new Option());
  761. option->put(PREF_DIR, ".");
  762. SharedHandle<DownloadContext> dctx(new DownloadContext());
  763. bittorrent::load(A2_TEST_DIR"/test.torrent", dctx, option);
  764. SharedHandle<Dict> btDict = Dict::g();
  765. gatherBitTorrentMetadata(btDict, bittorrent::getTorrentAttrs(dctx));
  766. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  767. downcast<String>(btDict->get("comment"))->s());
  768. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  769. downcast<Integer>(btDict->get("creationDate"))->i());
  770. CPPUNIT_ASSERT_EQUAL(std::string("multi"),
  771. downcast<String>(btDict->get("mode"))->s());
  772. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"),
  773. downcast<String>
  774. (downcast<Dict>
  775. (btDict->get("info"))
  776. ->get("name"))
  777. ->s());
  778. const List* announceList = downcast<List>(btDict->get("announceList"));
  779. CPPUNIT_ASSERT_EQUAL((size_t)3, announceList->size());
  780. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  781. downcast<String>(downcast<List>(announceList->get(0))->get(0))->s());
  782. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  783. downcast<String>(downcast<List>(announceList->get(1))->get(0))->s());
  784. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  785. downcast<String>(downcast<List>(announceList->get(2))->get(0))->s());
  786. // Remove some keys
  787. SharedHandle<TorrentAttribute> modBtAttrs = bittorrent::getTorrentAttrs(dctx);
  788. modBtAttrs->comment.clear();
  789. modBtAttrs->creationDate = 0;
  790. modBtAttrs->mode.clear();
  791. modBtAttrs->metadata.clear();
  792. btDict = Dict::g();
  793. gatherBitTorrentMetadata(btDict, modBtAttrs);
  794. CPPUNIT_ASSERT(!btDict->containsKey("comment"));
  795. CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
  796. CPPUNIT_ASSERT(!btDict->containsKey("mode"));
  797. CPPUNIT_ASSERT(!btDict->containsKey("info"));
  798. CPPUNIT_ASSERT(btDict->containsKey("announceList"));
  799. }
  800. #endif // ENABLE_BITTORRENT
  801. void RpcMethodTest::testChangePosition()
  802. {
  803. e_->getRequestGroupMan()->addReservedGroup
  804. (SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
  805. e_->getRequestGroupMan()->addReservedGroup
  806. (SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
  807. ChangePositionRpcMethod m;
  808. RpcRequest req(ChangePositionRpcMethod::getMethodName(), List::g());
  809. req.params->append("1");
  810. req.params->append(Integer::g(1));
  811. req.params->append("POS_SET");
  812. RpcResponse res = m.execute(req, e_.get());
  813. CPPUNIT_ASSERT_EQUAL(0, res.code);
  814. CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
  815. CPPUNIT_ASSERT_EQUAL
  816. ((a2_gid_t)1, e_->getRequestGroupMan()->getReservedGroups()[1]->getGID());
  817. }
  818. void RpcMethodTest::testChangePosition_fail()
  819. {
  820. ChangePositionRpcMethod m;
  821. RpcRequest req(ChangePositionRpcMethod::getMethodName(), List::g());
  822. RpcResponse res = m.execute(req, e_.get());
  823. CPPUNIT_ASSERT_EQUAL(1, res.code);
  824. req.params->append("1");
  825. req.params->append(Integer::g(2));
  826. req.params->append("bad keyword");
  827. CPPUNIT_ASSERT_EQUAL(1, res.code);
  828. }
  829. void RpcMethodTest::testChangeUri()
  830. {
  831. SharedHandle<FileEntry> files[3];
  832. for(int i = 0; i < 3; ++i) {
  833. files[i].reset(new FileEntry());
  834. }
  835. files[1]->addUri("http://example.org/aria2.tar.bz2");
  836. files[1]->addUri("http://example.org/mustremove1");
  837. files[1]->addUri("http://example.org/mustremove2");
  838. SharedHandle<DownloadContext> dctx(new DownloadContext());
  839. dctx->setFileEntries(&files[0], &files[3]);
  840. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  841. group->setDownloadContext(dctx);
  842. e_->getRequestGroupMan()->addReservedGroup(group);
  843. ChangeUriRpcMethod m;
  844. RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
  845. req.params->append("1"); // GID
  846. req.params->append(Integer::g(2)); // index of FileEntry
  847. SharedHandle<List> removeuris = List::g();
  848. removeuris->append("http://example.org/mustremove1");
  849. removeuris->append("http://example.org/mustremove2");
  850. removeuris->append("http://example.org/notexist");
  851. req.params->append(removeuris);
  852. SharedHandle<List> adduris = List::g();
  853. adduris->append("http://example.org/added1");
  854. adduris->append("http://example.org/added2");
  855. adduris->append("baduri");
  856. adduris->append("http://example.org/added3");
  857. req.params->append(adduris);
  858. RpcResponse res = m.execute(req, e_.get());
  859. CPPUNIT_ASSERT_EQUAL(0, res.code);
  860. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  861. CPPUNIT_ASSERT_EQUAL((int64_t)3, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  862. CPPUNIT_ASSERT_EQUAL((size_t)0, files[0]->getRemainingUris().size());
  863. CPPUNIT_ASSERT_EQUAL((size_t)0, files[2]->getRemainingUris().size());
  864. std::deque<std::string> uris = files[1]->getRemainingUris();
  865. CPPUNIT_ASSERT_EQUAL((size_t)4, uris.size());
  866. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/aria2.tar.bz2"),uris[0]);
  867. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1"), uris[1]);
  868. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
  869. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
  870. // Change adduris
  871. adduris = List::g();
  872. adduris->append("http://example.org/added1-1");
  873. adduris->append("http://example.org/added1-2");
  874. req.params->set(3, adduris);
  875. // Set position parameter
  876. req.params->append(Integer::g(2));
  877. res = m.execute(req, e_.get());
  878. CPPUNIT_ASSERT_EQUAL(0, res.code);
  879. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  880. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  881. uris = files[1]->getRemainingUris();
  882. CPPUNIT_ASSERT_EQUAL((size_t)6, uris.size());
  883. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[2]);
  884. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
  885. // Change index of FileEntry
  886. req.params->set(1, Integer::g(1));
  887. // Set position far beyond the size of uris in FileEntry.
  888. req.params->set(4, Integer::g(1000));
  889. res = m.execute(req, e_.get());
  890. CPPUNIT_ASSERT_EQUAL(0, res.code);
  891. CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
  892. CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
  893. uris = files[0]->getRemainingUris();
  894. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  895. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[0]);
  896. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
  897. }
  898. void RpcMethodTest::testChangeUri_fail()
  899. {
  900. SharedHandle<FileEntry> files[3];
  901. for(int i = 0; i < 3; ++i) {
  902. files[i].reset(new FileEntry());
  903. }
  904. SharedHandle<DownloadContext> dctx(new DownloadContext());
  905. dctx->setFileEntries(&files[0], &files[3]);
  906. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  907. group->setDownloadContext(dctx);
  908. e_->getRequestGroupMan()->addReservedGroup(group);
  909. ChangeUriRpcMethod m;
  910. RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
  911. req.params->append("1"); // GID
  912. req.params->append(Integer::g(1)); // index of FileEntry
  913. SharedHandle<List> removeuris = List::g();
  914. req.params->append(removeuris);
  915. SharedHandle<List> adduris = List::g();
  916. req.params->append(adduris);
  917. RpcResponse res = m.execute(req, e_.get());
  918. CPPUNIT_ASSERT_EQUAL(0, res.code);
  919. req.params->set(1, Integer::g(0));
  920. res = m.execute(req, e_.get());
  921. // RPC request fails because 2nd argument is less than 1.
  922. CPPUNIT_ASSERT_EQUAL(1, res.code);
  923. req.params->set(1, Integer::g(1));
  924. req.params->set(0, String::g("2"));
  925. res = m.execute(req, e_.get());
  926. // RPC request fails because GID#2 does not exist.
  927. CPPUNIT_ASSERT_EQUAL(1, res.code);
  928. req.params->set(0, String::g("1"));
  929. req.params->set(1, Integer::g(4));
  930. res = m.execute(req, e_.get());
  931. // RPC request fails because FileEntry#3 does not exist.
  932. CPPUNIT_ASSERT_EQUAL(1, res.code);
  933. req.params->set(1, String::g("0"));
  934. res = m.execute(req, e_.get());
  935. // RPC request fails because index of FileEntry is string.
  936. CPPUNIT_ASSERT_EQUAL(1, res.code);
  937. req.params->set(1, Integer::g(1));
  938. req.params->set(2, String::g("http://url"));
  939. res = m.execute(req, e_.get());
  940. // RPC request fails because 3rd param is not list.
  941. CPPUNIT_ASSERT_EQUAL(1, res.code);
  942. req.params->set(2, List::g());
  943. req.params->set(3, String::g("http://url"));
  944. res = m.execute(req, e_.get());
  945. // RPC request fails because 4th param is not list.
  946. CPPUNIT_ASSERT_EQUAL(1, res.code);
  947. }
  948. void RpcMethodTest::testGetSessionInfo()
  949. {
  950. GetSessionInfoRpcMethod m;
  951. RpcRequest req(GetSessionInfoRpcMethod::getMethodName(), List::g());
  952. RpcResponse res = m.execute(req, e_.get());
  953. CPPUNIT_ASSERT_EQUAL(0, res.code);
  954. CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
  955. getString(downcast<Dict>(res.param), "sessionId"));
  956. }
  957. void RpcMethodTest::testPause()
  958. {
  959. const std::string URIS[] = {
  960. "http://url1",
  961. "http://url2",
  962. "http://url3",
  963. };
  964. std::vector<std::string> uris(vbegin(URIS), vend(URIS));
  965. option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
  966. std::vector<SharedHandle<RequestGroup> > groups;
  967. createRequestGroupForUri(groups, option_, uris);
  968. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  969. e_->getRequestGroupMan()->addReservedGroup(groups);
  970. {
  971. PauseRpcMethod m;
  972. RpcRequest req(PauseRpcMethod::getMethodName(), List::g());
  973. req.params->append("1");
  974. RpcResponse res = m.execute(req, e_.get());
  975. CPPUNIT_ASSERT_EQUAL(0, res.code);
  976. }
  977. CPPUNIT_ASSERT(groups[0]->isPauseRequested());
  978. {
  979. UnpauseRpcMethod m;
  980. RpcRequest req(UnpauseRpcMethod::getMethodName(), List::g());
  981. req.params->append("1");
  982. RpcResponse res = m.execute(req, e_.get());
  983. CPPUNIT_ASSERT_EQUAL(0, res.code);
  984. }
  985. CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
  986. {
  987. PauseAllRpcMethod m;
  988. RpcRequest req(PauseAllRpcMethod::getMethodName(), List::g());
  989. RpcResponse res = m.execute(req, e_.get());
  990. CPPUNIT_ASSERT_EQUAL(0, res.code);
  991. }
  992. for(size_t i = 0; i < groups.size(); ++i) {
  993. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  994. }
  995. {
  996. UnpauseAllRpcMethod m;
  997. RpcRequest req(UnpauseAllRpcMethod::getMethodName(), List::g());
  998. RpcResponse res = m.execute(req, e_.get());
  999. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1000. }
  1001. for(size_t i = 0; i < groups.size(); ++i) {
  1002. CPPUNIT_ASSERT(!groups[i]->isPauseRequested());
  1003. }
  1004. {
  1005. ForcePauseAllRpcMethod m;
  1006. RpcRequest req(ForcePauseAllRpcMethod::getMethodName(), List::g());
  1007. RpcResponse res = m.execute(req, e_.get());
  1008. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1009. }
  1010. for(size_t i = 0; i < groups.size(); ++i) {
  1011. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  1012. }
  1013. }
  1014. void RpcMethodTest::testSystemMulticall()
  1015. {
  1016. SystemMulticallRpcMethod m;
  1017. RpcRequest req("system.multicall", List::g());
  1018. SharedHandle<List> reqparams = List::g();
  1019. req.params->append(reqparams);
  1020. for(int i = 0; i < 2; ++i) {
  1021. SharedHandle<Dict> dict = Dict::g();
  1022. dict->put("methodName", AddUriRpcMethod::getMethodName());
  1023. SharedHandle<List> params = List::g();
  1024. SharedHandle<List> urisParam = List::g();
  1025. urisParam->append("http://localhost/"+util::itos(i));
  1026. params->append(urisParam);
  1027. dict->put("params", params);
  1028. reqparams->append(dict);
  1029. }
  1030. {
  1031. SharedHandle<Dict> dict = Dict::g();
  1032. dict->put("methodName", "not exists");
  1033. dict->put("params", List::g());
  1034. reqparams->append(dict);
  1035. }
  1036. {
  1037. reqparams->append("not struct");
  1038. }
  1039. {
  1040. SharedHandle<Dict> dict = Dict::g();
  1041. dict->put("methodName", "system.multicall");
  1042. dict->put("params", List::g());
  1043. reqparams->append(dict);
  1044. }
  1045. {
  1046. // missing params
  1047. SharedHandle<Dict> dict = Dict::g();
  1048. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1049. reqparams->append(dict);
  1050. }
  1051. {
  1052. SharedHandle<Dict> dict = Dict::g();
  1053. dict->put("methodName", GetVersionRpcMethod::getMethodName());
  1054. dict->put("params", List::g());
  1055. reqparams->append(dict);
  1056. }
  1057. RpcResponse res = m.execute(req, e_.get());
  1058. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1059. const List* resParams = downcast<List>(res.param);
  1060. CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
  1061. CPPUNIT_ASSERT_EQUAL(std::string("1"),
  1062. downcast<String>(downcast<List>(resParams->get(0))->get(0))->s());
  1063. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  1064. downcast<String>(downcast<List>(resParams->get(1))->get(0))->s());
  1065. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1066. downcast<Integer>
  1067. (downcast<Dict>(resParams->get(2))->get("faultCode"))
  1068. ->i());
  1069. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1070. downcast<Integer>
  1071. (downcast<Dict>(resParams->get(3))->get("faultCode"))
  1072. ->i());
  1073. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1074. downcast<Integer>
  1075. (downcast<Dict>(resParams->get(4))->get("faultCode"))
  1076. ->i());
  1077. CPPUNIT_ASSERT(downcast<List>(resParams->get(5)));
  1078. CPPUNIT_ASSERT(downcast<List>(resParams->get(6)));
  1079. }
  1080. void RpcMethodTest::testSystemMulticall_fail()
  1081. {
  1082. SystemMulticallRpcMethod m;
  1083. RpcRequest req("system.multicall", List::g());
  1084. RpcResponse res = m.execute(req, e_.get());
  1085. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1086. }
  1087. } // namespace rpc
  1088. } // namespace aria2