RpcMethodTest.cc 40 KB

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