XmlRpcMethodTest.cc 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. #include "XmlRpcMethod.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 "ServerStatMan.h"
  8. #include "RequestGroup.h"
  9. #include "XmlRpcMethodImpl.h"
  10. #include "OptionParser.h"
  11. #include "OptionHandler.h"
  12. #include "XmlRpcRequest.h"
  13. #include "XmlRpcResponse.h"
  14. #include "prefs.h"
  15. #include "TestUtil.h"
  16. #include "DownloadContext.h"
  17. #include "FeatureConfig.h"
  18. #include "util.h"
  19. #include "array_fun.h"
  20. #include "download_helper.h"
  21. #include "FileAllocationEntry.h"
  22. #include "CheckIntegrityEntry.h"
  23. #ifdef ENABLE_BITTORRENT
  24. # include "BtRegistry.h"
  25. # include "BtRuntime.h"
  26. # include "PieceStorage.h"
  27. # include "PeerStorage.h"
  28. # include "BtProgressInfoFile.h"
  29. # include "BtAnnounce.h"
  30. # include "bittorrent_helper.h"
  31. #endif // ENABLE_BITTORRENT
  32. namespace aria2 {
  33. namespace xmlrpc {
  34. class XmlRpcMethodTest:public CppUnit::TestFixture {
  35. CPPUNIT_TEST_SUITE(XmlRpcMethodTest);
  36. CPPUNIT_TEST(testAddUri);
  37. CPPUNIT_TEST(testAddUri_withoutUri);
  38. CPPUNIT_TEST(testAddUri_notUri);
  39. CPPUNIT_TEST(testAddUri_withBadOption);
  40. CPPUNIT_TEST(testAddUri_withPosition);
  41. CPPUNIT_TEST(testAddUri_withBadPosition);
  42. #ifdef ENABLE_BITTORRENT
  43. CPPUNIT_TEST(testAddTorrent);
  44. CPPUNIT_TEST(testAddTorrent_withoutTorrent);
  45. CPPUNIT_TEST(testAddTorrent_notBase64Torrent);
  46. CPPUNIT_TEST(testAddTorrent_withPosition);
  47. #endif // ENABLE_BITTORRENT
  48. #ifdef ENABLE_METALINK
  49. CPPUNIT_TEST(testAddMetalink);
  50. CPPUNIT_TEST(testAddMetalink_withoutMetalink);
  51. CPPUNIT_TEST(testAddMetalink_notBase64Metalink);
  52. CPPUNIT_TEST(testAddMetalink_withPosition);
  53. #endif // ENABLE_METALINK
  54. CPPUNIT_TEST(testChangeOption);
  55. CPPUNIT_TEST(testChangeOption_withBadOption);
  56. CPPUNIT_TEST(testChangeOption_withNotAllowedOption);
  57. CPPUNIT_TEST(testChangeOption_withoutGid);
  58. CPPUNIT_TEST(testChangeGlobalOption);
  59. CPPUNIT_TEST(testChangeGlobalOption_withBadOption);
  60. CPPUNIT_TEST(testChangeGlobalOption_withNotAllowedOption);
  61. CPPUNIT_TEST(testTellStatus_withoutGid);
  62. CPPUNIT_TEST(testTellWaiting);
  63. CPPUNIT_TEST(testTellWaiting_fail);
  64. CPPUNIT_TEST(testGetVersion);
  65. CPPUNIT_TEST(testNoSuchMethod);
  66. CPPUNIT_TEST(testGatherStoppedDownload);
  67. CPPUNIT_TEST(testGatherProgressCommon);
  68. #ifdef ENABLE_BITTORRENT
  69. CPPUNIT_TEST(testGatherBitTorrentMetadata);
  70. #endif // ENABLE_BITTORRENT
  71. CPPUNIT_TEST(testChangePosition);
  72. CPPUNIT_TEST(testChangePosition_fail);
  73. CPPUNIT_TEST(testGetSessionInfo);
  74. CPPUNIT_TEST(testChangeUri);
  75. CPPUNIT_TEST(testChangeUri_fail);
  76. CPPUNIT_TEST(testPause);
  77. CPPUNIT_TEST(testSystemMulticall);
  78. CPPUNIT_TEST(testSystemMulticall_fail);
  79. CPPUNIT_TEST_SUITE_END();
  80. private:
  81. SharedHandle<DownloadEngine> e_;
  82. SharedHandle<Option> option_;
  83. public:
  84. void setUp()
  85. {
  86. RequestGroup::resetGIDCounter();
  87. option_.reset(new Option());
  88. option_->put(PREF_DIR, "/tmp");
  89. option_->put(PREF_SEGMENT_SIZE, "1048576");
  90. e_.reset
  91. (new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll())));
  92. e_->setOption(option_.get());
  93. e_->setRequestGroupMan
  94. (SharedHandle<RequestGroupMan>
  95. (new RequestGroupMan(std::vector<SharedHandle<RequestGroup> >(),
  96. 1, option_.get())));
  97. }
  98. void testAddUri();
  99. void testAddUri_withoutUri();
  100. void testAddUri_notUri();
  101. void testAddUri_withBadOption();
  102. void testAddUri_withPosition();
  103. void testAddUri_withBadPosition();
  104. #ifdef ENABLE_BITTORRENT
  105. void testAddTorrent();
  106. void testAddTorrent_withoutTorrent();
  107. void testAddTorrent_notBase64Torrent();
  108. void testAddTorrent_withPosition();
  109. #endif // ENABLE_BITTORRENT
  110. #ifdef ENABLE_METALINK
  111. void testAddMetalink();
  112. void testAddMetalink_withoutMetalink();
  113. void testAddMetalink_notBase64Metalink();
  114. void testAddMetalink_withPosition();
  115. #endif // ENABLE_METALINK
  116. void testChangeOption();
  117. void testChangeOption_withBadOption();
  118. void testChangeOption_withNotAllowedOption();
  119. void testChangeOption_withoutGid();
  120. void testChangeGlobalOption();
  121. void testChangeGlobalOption_withBadOption();
  122. void testChangeGlobalOption_withNotAllowedOption();
  123. void testTellStatus_withoutGid();
  124. void testTellWaiting();
  125. void testTellWaiting_fail();
  126. void testGetVersion();
  127. void testNoSuchMethod();
  128. void testGatherStoppedDownload();
  129. void testGatherProgressCommon();
  130. #ifdef ENABLE_BITTORRENT
  131. void testGatherBitTorrentMetadata();
  132. #endif // ENABLE_BITTORRENT
  133. void testChangePosition();
  134. void testChangePosition_fail();
  135. void testGetSessionInfo();
  136. void testChangeUri();
  137. void testChangeUri_fail();
  138. void testPause();
  139. void testSystemMulticall();
  140. void testSystemMulticall_fail();
  141. };
  142. CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcMethodTest);
  143. static std::string getString(const Dict* dict, const std::string& key)
  144. {
  145. return asString(dict->get(key))->s();
  146. }
  147. void XmlRpcMethodTest::testAddUri()
  148. {
  149. AddUriXmlRpcMethod m;
  150. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  151. SharedHandle<List> urisParam = List::g();
  152. urisParam->append("http://localhost/");
  153. req.params->append(urisParam);
  154. {
  155. XmlRpcResponse res = m.execute(req, e_.get());
  156. CPPUNIT_ASSERT_EQUAL(0, res.code);
  157. const std::deque<SharedHandle<RequestGroup> > rgs =
  158. e_->getRequestGroupMan()->getReservedGroups();
  159. CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size());
  160. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"),
  161. rgs.front()->getDownloadContext()->
  162. getFirstFileEntry()->getRemainingUris().front());
  163. }
  164. // with options
  165. SharedHandle<Dict> opt = Dict::g();
  166. opt->put(PREF_DIR, "/sink");
  167. req.params->append(opt);
  168. {
  169. XmlRpcResponse res = m.execute(req, e_.get());
  170. CPPUNIT_ASSERT_EQUAL(0, res.code);
  171. CPPUNIT_ASSERT_EQUAL(std::string("/sink"),
  172. e_->getRequestGroupMan()->findReservedGroup(2)->
  173. getDownloadContext()->getDir());
  174. }
  175. }
  176. void XmlRpcMethodTest::testAddUri_withoutUri()
  177. {
  178. AddUriXmlRpcMethod m;
  179. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  180. XmlRpcResponse res = m.execute(req, e_.get());
  181. CPPUNIT_ASSERT_EQUAL(1, res.code);
  182. }
  183. void XmlRpcMethodTest::testAddUri_notUri()
  184. {
  185. AddUriXmlRpcMethod m;
  186. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  187. SharedHandle<List> urisParam = List::g();
  188. urisParam->append("not uri");
  189. req.params->append(urisParam);
  190. XmlRpcResponse res = m.execute(req, e_.get());
  191. CPPUNIT_ASSERT_EQUAL(1, res.code);
  192. }
  193. void XmlRpcMethodTest::testAddUri_withBadOption()
  194. {
  195. AddUriXmlRpcMethod m;
  196. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  197. SharedHandle<List> urisParam = List::g();
  198. urisParam->append("http://localhost");
  199. req.params->append(urisParam);
  200. SharedHandle<Dict> opt = Dict::g();
  201. opt->put(PREF_FILE_ALLOCATION, "badvalue");
  202. req.params->append(opt);
  203. XmlRpcResponse res = m.execute(req, e_.get());
  204. CPPUNIT_ASSERT_EQUAL(1, res.code);
  205. }
  206. void XmlRpcMethodTest::testAddUri_withPosition()
  207. {
  208. AddUriXmlRpcMethod m;
  209. XmlRpcRequest req1(AddUriXmlRpcMethod::getMethodName(), List::g());
  210. SharedHandle<List> urisParam1 = List::g();
  211. urisParam1->append("http://uri1");
  212. req1.params->append(urisParam1);
  213. XmlRpcResponse res1 = m.execute(req1, e_.get());
  214. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  215. XmlRpcRequest req2(AddUriXmlRpcMethod::getMethodName(), List::g());
  216. SharedHandle<List> urisParam2 = List::g();
  217. urisParam2->append("http://uri2");
  218. req2.params->append(urisParam2);
  219. req2.params->append(Dict::g());
  220. req2.params->append(Integer::g(0));
  221. m.execute(req2, e_.get());
  222. std::string uri =
  223. e_->getRequestGroupMan()->getReservedGroups()[0]->
  224. getDownloadContext()->getFirstFileEntry()->getRemainingUris()[0];
  225. CPPUNIT_ASSERT_EQUAL(std::string("http://uri2"), uri);
  226. }
  227. void XmlRpcMethodTest::testAddUri_withBadPosition()
  228. {
  229. AddUriXmlRpcMethod m;
  230. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  231. SharedHandle<List> urisParam = List::g();
  232. urisParam->append("http://localhost/");
  233. req.params->append(urisParam);
  234. req.params->append(Dict::g());
  235. req.params->append(Integer::g(-1));
  236. XmlRpcResponse res = m.execute(req, e_.get());
  237. CPPUNIT_ASSERT_EQUAL(1, res.code);
  238. }
  239. #ifdef ENABLE_BITTORRENT
  240. void XmlRpcMethodTest::testAddTorrent()
  241. {
  242. AddTorrentXmlRpcMethod m;
  243. XmlRpcRequest req(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  244. req.params->append(readFile("single.torrent"));
  245. SharedHandle<List> uris = List::g();
  246. uris->append("http://localhost/aria2-0.8.2.tar.bz2");
  247. req.params->append(uris);
  248. {
  249. XmlRpcResponse res = m.execute(req, e_.get());
  250. CPPUNIT_ASSERT_EQUAL(0, res.code);
  251. CPPUNIT_ASSERT_EQUAL(std::string("1"), asString(res.param)->s());
  252. SharedHandle<RequestGroup> group =
  253. e_->getRequestGroupMan()->findReservedGroup(1);
  254. CPPUNIT_ASSERT(!group.isNull());
  255. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-0.8.2.tar.bz2"),
  256. group->getFirstFilePath());
  257. CPPUNIT_ASSERT_EQUAL((size_t)1,
  258. group->getDownloadContext()->getFirstFileEntry()->
  259. getRemainingUris().size());
  260. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/aria2-0.8.2.tar.bz2"),
  261. group->getDownloadContext()->getFirstFileEntry()->
  262. getRemainingUris()[0]);
  263. }
  264. // with options
  265. SharedHandle<Dict> opt = Dict::g();
  266. opt->put(PREF_DIR, "/sink");
  267. req.params->append(opt);
  268. {
  269. XmlRpcResponse res = m.execute(req, e_.get());
  270. CPPUNIT_ASSERT_EQUAL(0, res.code);
  271. CPPUNIT_ASSERT_EQUAL(std::string("/sink/aria2-0.8.2.tar.bz2"),
  272. e_->getRequestGroupMan()->findReservedGroup(2)->
  273. getFirstFilePath());
  274. }
  275. }
  276. void XmlRpcMethodTest::testAddTorrent_withoutTorrent()
  277. {
  278. AddTorrentXmlRpcMethod m;
  279. XmlRpcRequest req(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  280. XmlRpcResponse res = m.execute(req, e_.get());
  281. CPPUNIT_ASSERT_EQUAL(1, res.code);
  282. }
  283. void XmlRpcMethodTest::testAddTorrent_notBase64Torrent()
  284. {
  285. AddTorrentXmlRpcMethod m;
  286. XmlRpcRequest req(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  287. req.params->append("not torrent");
  288. XmlRpcResponse res = m.execute(req, e_.get());
  289. CPPUNIT_ASSERT_EQUAL(1, res.code);
  290. }
  291. void XmlRpcMethodTest::testAddTorrent_withPosition()
  292. {
  293. AddTorrentXmlRpcMethod m;
  294. XmlRpcRequest req1(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  295. req1.params->append(readFile("test.torrent"));
  296. req1.params->append(List::g());
  297. req1.params->append(Dict::g());
  298. XmlRpcResponse res1 = m.execute(req1, e_.get());
  299. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  300. XmlRpcRequest req2(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  301. req2.params->append(readFile("single.torrent"));
  302. req2.params->append(List::g());
  303. req2.params->append(Dict::g());
  304. req2.params->append(Integer::g(0));
  305. m.execute(req2, e_.get());
  306. CPPUNIT_ASSERT_EQUAL((size_t)1,
  307. e_->getRequestGroupMan()->getReservedGroups()[0]->
  308. getDownloadContext()->getFileEntries().size());
  309. }
  310. #endif // ENABLE_BITTORRENT
  311. #ifdef ENABLE_METALINK
  312. void XmlRpcMethodTest::testAddMetalink()
  313. {
  314. AddMetalinkXmlRpcMethod m;
  315. XmlRpcRequest req(AddMetalinkXmlRpcMethod::getMethodName(), List::g());
  316. req.params->append(readFile("2files.metalink"));
  317. {
  318. XmlRpcResponse res = m.execute(req, e_.get());
  319. CPPUNIT_ASSERT_EQUAL(0, res.code);
  320. const List* resParams = asList(res.param);
  321. CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
  322. CPPUNIT_ASSERT_EQUAL(std::string("1"), asString(resParams->get(0))->s());
  323. CPPUNIT_ASSERT_EQUAL(std::string("2"), asString(resParams->get(1))->s());
  324. SharedHandle<RequestGroup> tar =
  325. e_->getRequestGroupMan()->findReservedGroup(1);
  326. CPPUNIT_ASSERT(!tar.isNull());
  327. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.tar.bz2"),
  328. tar->getFirstFilePath());
  329. SharedHandle<RequestGroup> deb =
  330. e_->getRequestGroupMan()->findReservedGroup(2);
  331. CPPUNIT_ASSERT(!deb.isNull());
  332. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.deb"),
  333. deb->getFirstFilePath());
  334. }
  335. // with options
  336. SharedHandle<Dict> opt = Dict::g();
  337. opt->put(PREF_DIR, "/sink");
  338. req.params->append(opt);
  339. {
  340. XmlRpcResponse res = m.execute(req, e_.get());
  341. CPPUNIT_ASSERT_EQUAL(0, res.code);
  342. CPPUNIT_ASSERT_EQUAL(std::string("/sink/aria2-5.0.0.tar.bz2"),
  343. e_->getRequestGroupMan()->findReservedGroup(3)->
  344. getFirstFilePath());
  345. }
  346. }
  347. void XmlRpcMethodTest::testAddMetalink_withoutMetalink()
  348. {
  349. AddMetalinkXmlRpcMethod m;
  350. XmlRpcRequest req(AddMetalinkXmlRpcMethod::getMethodName(), List::g());
  351. XmlRpcResponse res = m.execute(req, e_.get());
  352. CPPUNIT_ASSERT_EQUAL(1, res.code);
  353. }
  354. void XmlRpcMethodTest::testAddMetalink_notBase64Metalink()
  355. {
  356. AddMetalinkXmlRpcMethod m;
  357. XmlRpcRequest req(AddMetalinkXmlRpcMethod::getMethodName(), List::g());
  358. req.params->append("not metalink");
  359. XmlRpcResponse res = m.execute(req, e_.get());
  360. CPPUNIT_ASSERT_EQUAL(1, res.code);
  361. }
  362. void XmlRpcMethodTest::testAddMetalink_withPosition()
  363. {
  364. AddUriXmlRpcMethod m1;
  365. XmlRpcRequest req1(AddUriXmlRpcMethod::getMethodName(), List::g());
  366. SharedHandle<List> urisParam1 = List::g();
  367. urisParam1->append("http://uri");
  368. req1.params->append(urisParam1);
  369. XmlRpcResponse res1 = m1.execute(req1, e_.get());
  370. CPPUNIT_ASSERT_EQUAL(0, res1.code);
  371. AddMetalinkXmlRpcMethod m2;
  372. XmlRpcRequest req2("ari2.addMetalink", List::g());
  373. req2.params->append(readFile("2files.metalink"));
  374. req2.params->append(Dict::g());
  375. req2.params->append(Integer::g(0));
  376. XmlRpcResponse res2 = m2.execute(req2, e_.get());
  377. CPPUNIT_ASSERT_EQUAL(0, res2.code);
  378. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.tar.bz2"),
  379. e_->getRequestGroupMan()->getReservedGroups()[0]->
  380. getFirstFilePath());
  381. }
  382. #endif // ENABLE_METALINK
  383. void XmlRpcMethodTest::testChangeOption()
  384. {
  385. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  386. e_->getRequestGroupMan()->addReservedGroup(group);
  387. ChangeOptionXmlRpcMethod m;
  388. XmlRpcRequest req(ChangeOptionXmlRpcMethod::getMethodName(), List::g());
  389. req.params->append("1");
  390. SharedHandle<Dict> opt = Dict::g();
  391. opt->put(PREF_MAX_DOWNLOAD_LIMIT, "100K");
  392. #ifdef ENABLE_BITTORRENT
  393. opt->put(PREF_BT_MAX_PEERS, "100");
  394. opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT, "300K");
  395. opt->put(PREF_MAX_UPLOAD_LIMIT, "50K");
  396. BtObject btObject;
  397. btObject.btRuntime_ = SharedHandle<BtRuntime>(new BtRuntime());
  398. e_->getBtRegistry()->put(group->getGID(), btObject);
  399. #endif // ENABLE_BITTORRENT
  400. req.params->append(opt);
  401. XmlRpcResponse res = m.execute(req, e_.get());
  402. SharedHandle<Option> option = group->getOption();
  403. CPPUNIT_ASSERT_EQUAL(0, res.code);
  404. CPPUNIT_ASSERT_EQUAL((unsigned int)100*1024,
  405. group->getMaxDownloadSpeedLimit());
  406. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  407. option->get(PREF_MAX_DOWNLOAD_LIMIT));
  408. #ifdef ENABLE_BITTORRENT
  409. CPPUNIT_ASSERT_EQUAL(std::string("307200"),
  410. option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
  411. CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
  412. CPPUNIT_ASSERT_EQUAL((unsigned int)100, btObject.btRuntime_->getMaxPeers());
  413. CPPUNIT_ASSERT_EQUAL((unsigned int)50*1024,
  414. group->getMaxUploadSpeedLimit());
  415. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  416. option->get(PREF_MAX_UPLOAD_LIMIT));
  417. #endif // ENABLE_BITTORRENT
  418. }
  419. void XmlRpcMethodTest::testChangeOption_withBadOption()
  420. {
  421. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  422. e_->getRequestGroupMan()->addReservedGroup(group);
  423. ChangeOptionXmlRpcMethod m;
  424. XmlRpcRequest req(ChangeOptionXmlRpcMethod::getMethodName(), List::g());
  425. req.params->append("1");
  426. SharedHandle<Dict> opt = Dict::g();
  427. opt->put(PREF_MAX_DOWNLOAD_LIMIT, "badvalue");
  428. req.params->append(opt);
  429. XmlRpcResponse res = m.execute(req, e_.get());
  430. CPPUNIT_ASSERT_EQUAL(1, res.code);
  431. }
  432. void XmlRpcMethodTest::testChangeOption_withNotAllowedOption()
  433. {
  434. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  435. e_->getRequestGroupMan()->addReservedGroup(group);
  436. ChangeOptionXmlRpcMethod m;
  437. XmlRpcRequest req(ChangeOptionXmlRpcMethod::getMethodName(), List::g());
  438. req.params->append("1");
  439. SharedHandle<Dict> opt = Dict::g();
  440. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT, "100K");
  441. req.params->append(opt);
  442. XmlRpcResponse res = m.execute(req, e_.get());
  443. CPPUNIT_ASSERT_EQUAL(1, res.code);
  444. }
  445. void XmlRpcMethodTest::testChangeOption_withoutGid()
  446. {
  447. ChangeOptionXmlRpcMethod m;
  448. XmlRpcRequest req(ChangeOptionXmlRpcMethod::getMethodName(), List::g());
  449. XmlRpcResponse res = m.execute(req, e_.get());
  450. CPPUNIT_ASSERT_EQUAL(1, res.code);
  451. }
  452. void XmlRpcMethodTest::testChangeGlobalOption()
  453. {
  454. ChangeGlobalOptionXmlRpcMethod m;
  455. XmlRpcRequest req
  456. (ChangeGlobalOptionXmlRpcMethod::getMethodName(), List::g());
  457. SharedHandle<Dict> opt = Dict::g();
  458. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT, "100K");
  459. #ifdef ENABLE_BITTORRENT
  460. opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT, "50K");
  461. #endif // ENABLE_BITTORRENT
  462. req.params->append(opt);
  463. XmlRpcResponse res = m.execute(req, e_.get());
  464. CPPUNIT_ASSERT_EQUAL(0, res.code);
  465. CPPUNIT_ASSERT_EQUAL
  466. ((unsigned int)100*1024,
  467. e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit());
  468. CPPUNIT_ASSERT_EQUAL(std::string("102400"),
  469. e_->getOption()->get(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
  470. #ifdef ENABLE_BITTORRENT
  471. CPPUNIT_ASSERT_EQUAL
  472. ((unsigned int)50*1024,
  473. e_->getRequestGroupMan()->getMaxOverallUploadSpeedLimit());
  474. CPPUNIT_ASSERT_EQUAL(std::string("51200"),
  475. e_->getOption()->get(PREF_MAX_OVERALL_UPLOAD_LIMIT));
  476. #endif // ENABLE_BITTORRENT
  477. }
  478. void XmlRpcMethodTest::testChangeGlobalOption_withBadOption()
  479. {
  480. ChangeGlobalOptionXmlRpcMethod m;
  481. XmlRpcRequest req
  482. (ChangeGlobalOptionXmlRpcMethod::getMethodName(), List::g());
  483. SharedHandle<Dict> opt = Dict::g();
  484. opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT, "badvalue");
  485. req.params->append(opt);
  486. XmlRpcResponse res = m.execute(req, e_.get());
  487. CPPUNIT_ASSERT_EQUAL(1, res.code);
  488. }
  489. void XmlRpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
  490. {
  491. ChangeGlobalOptionXmlRpcMethod m;
  492. XmlRpcRequest req
  493. (ChangeGlobalOptionXmlRpcMethod::getMethodName(), List::g());
  494. SharedHandle<Dict> opt = Dict::g();
  495. opt->put(PREF_MAX_DOWNLOAD_LIMIT, "100K");
  496. req.params->append(opt);
  497. XmlRpcResponse res = m.execute(req, e_.get());
  498. CPPUNIT_ASSERT_EQUAL(1, res.code);
  499. }
  500. void XmlRpcMethodTest::testNoSuchMethod()
  501. {
  502. NoSuchMethodXmlRpcMethod m;
  503. XmlRpcRequest req("make.hamburger", List::g());
  504. XmlRpcResponse res = m.execute(req, 0);
  505. CPPUNIT_ASSERT_EQUAL(1, res.code);
  506. CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
  507. getString(asDict(res.param), "faultString"));
  508. CPPUNIT_ASSERT_EQUAL
  509. (std::string("<?xml version=\"1.0\"?>"
  510. "<methodResponse>"
  511. "<fault>"
  512. "<value>"
  513. "<struct>"
  514. "<member>"
  515. "<name>faultCode</name><value><int>1</int></value>"
  516. "</member>"
  517. "<member>"
  518. "<name>faultString</name>"
  519. "<value>"
  520. "<string>No such method: make.hamburger</string>"
  521. "</value>"
  522. "</member>"
  523. "</struct>"
  524. "</value>"
  525. "</fault>"
  526. "</methodResponse>"),
  527. res.toXml());
  528. }
  529. void XmlRpcMethodTest::testTellStatus_withoutGid()
  530. {
  531. TellStatusXmlRpcMethod m;
  532. XmlRpcRequest req(TellStatusXmlRpcMethod::getMethodName(), List::g());
  533. XmlRpcResponse res = m.execute(req, e_.get());
  534. CPPUNIT_ASSERT_EQUAL(1, res.code);
  535. }
  536. static void addUri(const std::string& uri,
  537. const SharedHandle<DownloadEngine>& e)
  538. {
  539. AddUriXmlRpcMethod m;
  540. XmlRpcRequest req(AddUriXmlRpcMethod::getMethodName(), List::g());
  541. SharedHandle<List> urisParam = List::g();
  542. urisParam->append(uri);
  543. req.params->append(urisParam);
  544. CPPUNIT_ASSERT_EQUAL(0, m.execute(req, e.get()).code);
  545. }
  546. #ifdef ENABLE_BITTORRENT
  547. static void addTorrent
  548. (const std::string& torrentFile, const SharedHandle<DownloadEngine>& e)
  549. {
  550. AddTorrentXmlRpcMethod m;
  551. XmlRpcRequest req(AddTorrentXmlRpcMethod::getMethodName(), List::g());
  552. req.params->append(readFile(torrentFile));
  553. XmlRpcResponse res = m.execute(req, e.get());
  554. }
  555. #endif // ENABLE_BITTORRENT
  556. void XmlRpcMethodTest::testTellWaiting()
  557. {
  558. addUri("http://1/", e_);
  559. addUri("http://2/", e_);
  560. addUri("http://3/", e_);
  561. #ifdef ENABLE_BITTORRENT
  562. addTorrent("single.torrent", e_);
  563. #else // !ENABLE_BITTORRENT
  564. addUri("http://4/", e_);
  565. #endif // !ENABLE_BITTORRENT
  566. TellWaitingXmlRpcMethod m;
  567. XmlRpcRequest req(TellWaitingXmlRpcMethod::getMethodName(), List::g());
  568. req.params->append(Integer::g(1));
  569. req.params->append(Integer::g(2));
  570. XmlRpcResponse 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 = XmlRpcRequest(TellWaitingXmlRpcMethod::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 = XmlRpcRequest(TellWaitingXmlRpcMethod::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 = XmlRpcRequest(TellWaitingXmlRpcMethod::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 XmlRpcMethodTest::testTellWaiting_fail()
  626. {
  627. TellWaitingXmlRpcMethod m;
  628. XmlRpcRequest req(TellWaitingXmlRpcMethod::getMethodName(), List::g());
  629. XmlRpcResponse res = m.execute(req, e_.get());
  630. CPPUNIT_ASSERT_EQUAL(1, res.code);
  631. }
  632. void XmlRpcMethodTest::testGetVersion()
  633. {
  634. GetVersionXmlRpcMethod m;
  635. XmlRpcRequest req(GetVersionXmlRpcMethod::getMethodName(), List::g());
  636. XmlRpcResponse 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 XmlRpcMethodTest::testGatherStoppedDownload()
  653. {
  654. std::vector<SharedHandle<FileEntry> > fileEntries;
  655. std::vector<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 = downloadresultcode::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 XmlRpcMethodTest::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. dctx->setDir(option_->get(PREF_DIR));
  687. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  688. group->setDownloadContext(dctx);
  689. std::vector<SharedHandle<RequestGroup> > followedBy;
  690. for(int i = 0; i < 2; ++i) {
  691. followedBy.push_back(SharedHandle<RequestGroup>(new RequestGroup(option_)));
  692. }
  693. group->followedBy(followedBy.begin(), followedBy.end());
  694. group->belongsTo(2);
  695. SharedHandle<Dict> entry = Dict::g();
  696. std::vector<std::string> keys;
  697. gatherProgressCommon(entry, group, keys);
  698. const List* followedByRes = asList(entry->get("followedBy"));
  699. CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[0]->getGID()),
  700. asString(followedByRes->get(0))->s());
  701. CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[1]->getGID()),
  702. asString(followedByRes->get(1))->s());
  703. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  704. asString(entry->get("belongsTo"))->s());
  705. const List* files = asList(entry->get("files"));
  706. CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
  707. const Dict* file = asDict(files->get(0));
  708. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
  709. asString(file->get("path"))->s());
  710. CPPUNIT_ASSERT_EQUAL(uris[0],
  711. asString
  712. (asDict
  713. (asList(file->get("uris"))->get(0))
  714. ->get("uri"))
  715. ->s());
  716. CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), 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 XmlRpcMethodTest::testGatherBitTorrentMetadata()
  725. {
  726. SharedHandle<DownloadContext> dctx(new DownloadContext());
  727. bittorrent::load("test.torrent", dctx);
  728. SharedHandle<Dict> btDict = Dict::g();
  729. gatherBitTorrentMetadata(btDict, bittorrent::getTorrentAttrs(dctx));
  730. CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
  731. asString(btDict->get("comment"))->s());
  732. CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
  733. asInteger(btDict->get("creationDate"))->i());
  734. CPPUNIT_ASSERT_EQUAL(std::string("multi"),
  735. asString(btDict->get("mode"))->s());
  736. CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"),
  737. asString
  738. (asDict
  739. (btDict->get("info"))
  740. ->get("name"))
  741. ->s());
  742. const List* announceList = asList(btDict->get("announceList"));
  743. CPPUNIT_ASSERT_EQUAL((size_t)3, announceList->size());
  744. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"),
  745. asString(asList(announceList->get(0))->get(0))->s());
  746. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"),
  747. asString(asList(announceList->get(1))->get(0))->s());
  748. CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
  749. asString(asList(announceList->get(2))->get(0))->s());
  750. // Remove some keys
  751. SharedHandle<TorrentAttribute> modBtAttrs = bittorrent::getTorrentAttrs(dctx);
  752. modBtAttrs->comment.clear();
  753. modBtAttrs->creationDate = 0;
  754. modBtAttrs->mode.clear();
  755. modBtAttrs->metadata.clear();
  756. btDict = Dict::g();
  757. gatherBitTorrentMetadata(btDict, modBtAttrs);
  758. CPPUNIT_ASSERT(!btDict->containsKey("comment"));
  759. CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
  760. CPPUNIT_ASSERT(!btDict->containsKey("mode"));
  761. CPPUNIT_ASSERT(!btDict->containsKey("info"));
  762. CPPUNIT_ASSERT(btDict->containsKey("announceList"));
  763. }
  764. #endif // ENABLE_BITTORRENT
  765. void XmlRpcMethodTest::testChangePosition()
  766. {
  767. e_->getRequestGroupMan()->addReservedGroup
  768. (SharedHandle<RequestGroup>(new RequestGroup(option_)));
  769. e_->getRequestGroupMan()->addReservedGroup
  770. (SharedHandle<RequestGroup>(new RequestGroup(option_)));
  771. ChangePositionXmlRpcMethod m;
  772. XmlRpcRequest req(ChangePositionXmlRpcMethod::getMethodName(), List::g());
  773. req.params->append("1");
  774. req.params->append(Integer::g(1));
  775. req.params->append("POS_SET");
  776. XmlRpcResponse res = m.execute(req, e_.get());
  777. CPPUNIT_ASSERT_EQUAL(0, res.code);
  778. CPPUNIT_ASSERT_EQUAL((int64_t)1, asInteger(res.param)->i());
  779. CPPUNIT_ASSERT_EQUAL
  780. ((gid_t)1, e_->getRequestGroupMan()->getReservedGroups()[1]->getGID());
  781. }
  782. void XmlRpcMethodTest::testChangePosition_fail()
  783. {
  784. ChangePositionXmlRpcMethod m;
  785. XmlRpcRequest req(ChangePositionXmlRpcMethod::getMethodName(), List::g());
  786. XmlRpcResponse res = m.execute(req, e_.get());
  787. CPPUNIT_ASSERT_EQUAL(1, res.code);
  788. req.params->append("1");
  789. req.params->append(Integer::g(2));
  790. req.params->append("bad keyword");
  791. CPPUNIT_ASSERT_EQUAL(1, res.code);
  792. }
  793. void XmlRpcMethodTest::testChangeUri()
  794. {
  795. SharedHandle<FileEntry> files[3];
  796. for(int i = 0; i < 3; ++i) {
  797. files[i].reset(new FileEntry());
  798. }
  799. files[1]->addUri("http://example.org/aria2.tar.bz2");
  800. files[1]->addUri("http://example.org/mustremove1");
  801. files[1]->addUri("http://example.org/mustremove2");
  802. SharedHandle<DownloadContext> dctx(new DownloadContext());
  803. dctx->setFileEntries(&files[0], &files[3]);
  804. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  805. group->setDownloadContext(dctx);
  806. e_->getRequestGroupMan()->addReservedGroup(group);
  807. ChangeUriXmlRpcMethod m;
  808. XmlRpcRequest req(ChangeUriXmlRpcMethod::getMethodName(), List::g());
  809. req.params->append("1"); // GID
  810. req.params->append(Integer::g(2)); // index of FileEntry
  811. SharedHandle<List> removeuris = List::g();
  812. removeuris->append("http://example.org/mustremove1");
  813. removeuris->append("http://example.org/mustremove2");
  814. removeuris->append("http://example.org/notexist");
  815. req.params->append(removeuris);
  816. SharedHandle<List> adduris = List::g();
  817. adduris->append("http://example.org/added1");
  818. adduris->append("http://example.org/added2");
  819. adduris->append("baduri");
  820. adduris->append("http://example.org/added3");
  821. req.params->append(adduris);
  822. XmlRpcResponse res = m.execute(req, e_.get());
  823. CPPUNIT_ASSERT_EQUAL(0, res.code);
  824. CPPUNIT_ASSERT_EQUAL((int64_t)2, asInteger(asList(res.param)->get(0))->i());
  825. CPPUNIT_ASSERT_EQUAL((int64_t)3, asInteger(asList(res.param)->get(1))->i());
  826. CPPUNIT_ASSERT_EQUAL((size_t)0, files[0]->getRemainingUris().size());
  827. CPPUNIT_ASSERT_EQUAL((size_t)0, files[2]->getRemainingUris().size());
  828. std::deque<std::string> uris = files[1]->getRemainingUris();
  829. CPPUNIT_ASSERT_EQUAL((size_t)4, uris.size());
  830. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/aria2.tar.bz2"),uris[0]);
  831. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1"), uris[1]);
  832. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
  833. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
  834. // Change adduris
  835. adduris = List::g();
  836. adduris->append("http://example.org/added1-1");
  837. adduris->append("http://example.org/added1-2");
  838. req.params->set(3, adduris);
  839. // Set position parameter
  840. req.params->append(Integer::g(2));
  841. res = m.execute(req, e_.get());
  842. CPPUNIT_ASSERT_EQUAL(0, res.code);
  843. CPPUNIT_ASSERT_EQUAL((int64_t)0, asInteger(asList(res.param)->get(0))->i());
  844. CPPUNIT_ASSERT_EQUAL((int64_t)2, asInteger(asList(res.param)->get(1))->i());
  845. uris = files[1]->getRemainingUris();
  846. CPPUNIT_ASSERT_EQUAL((size_t)6, uris.size());
  847. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[2]);
  848. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
  849. // Change index of FileEntry
  850. req.params->set(1, Integer::g(1));
  851. // Set position far beyond the size of uris in FileEntry.
  852. req.params->set(4, Integer::g(1000));
  853. res = m.execute(req, e_.get());
  854. CPPUNIT_ASSERT_EQUAL(0, res.code);
  855. CPPUNIT_ASSERT_EQUAL((int64_t)0, asInteger(asList(res.param)->get(0))->i());
  856. CPPUNIT_ASSERT_EQUAL((int64_t)2, asInteger(asList(res.param)->get(1))->i());
  857. uris = files[0]->getRemainingUris();
  858. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  859. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-1"), uris[0]);
  860. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
  861. }
  862. void XmlRpcMethodTest::testChangeUri_fail()
  863. {
  864. SharedHandle<FileEntry> files[3];
  865. for(int i = 0; i < 3; ++i) {
  866. files[i].reset(new FileEntry());
  867. }
  868. SharedHandle<DownloadContext> dctx(new DownloadContext());
  869. dctx->setFileEntries(&files[0], &files[3]);
  870. SharedHandle<RequestGroup> group(new RequestGroup(option_));
  871. group->setDownloadContext(dctx);
  872. e_->getRequestGroupMan()->addReservedGroup(group);
  873. ChangeUriXmlRpcMethod m;
  874. XmlRpcRequest req(ChangeUriXmlRpcMethod::getMethodName(), List::g());
  875. req.params->append("1"); // GID
  876. req.params->append(Integer::g(1)); // index of FileEntry
  877. SharedHandle<List> removeuris = List::g();
  878. req.params->append(removeuris);
  879. SharedHandle<List> adduris = List::g();
  880. req.params->append(adduris);
  881. XmlRpcResponse res = m.execute(req, e_.get());
  882. CPPUNIT_ASSERT_EQUAL(0, res.code);
  883. req.params->set(0, String::g("2"));
  884. res = m.execute(req, e_.get());
  885. // RPC request fails because GID#2 does not exist.
  886. CPPUNIT_ASSERT_EQUAL(1, res.code);
  887. req.params->set(0, String::g("1"));
  888. req.params->set(1, Integer::g(4));
  889. res = m.execute(req, e_.get());
  890. // RPC request fails because FileEntry#3 does not exist.
  891. CPPUNIT_ASSERT_EQUAL(1, res.code);
  892. req.params->set(1, String::g("0"));
  893. res = m.execute(req, e_.get());
  894. // RPC request fails because index of FileEntry is string.
  895. CPPUNIT_ASSERT_EQUAL(1, res.code);
  896. req.params->set(1, Integer::g(1));
  897. req.params->set(2, String::g("http://url"));
  898. res = m.execute(req, e_.get());
  899. // RPC request fails because 3rd param is not list.
  900. CPPUNIT_ASSERT_EQUAL(1, res.code);
  901. req.params->set(2, List::g());
  902. req.params->set(3, String::g("http://url"));
  903. res = m.execute(req, e_.get());
  904. // RPC request fails because 4th param is not list.
  905. CPPUNIT_ASSERT_EQUAL(1, res.code);
  906. }
  907. void XmlRpcMethodTest::testGetSessionInfo()
  908. {
  909. GetSessionInfoXmlRpcMethod m;
  910. XmlRpcRequest req(GetSessionInfoXmlRpcMethod::getMethodName(), List::g());
  911. XmlRpcResponse res = m.execute(req, e_.get());
  912. CPPUNIT_ASSERT_EQUAL(0, res.code);
  913. CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
  914. getString(asDict(res.param), "sessionId"));
  915. }
  916. void XmlRpcMethodTest::testPause()
  917. {
  918. const std::string URIS[] = {
  919. "http://url1",
  920. "http://url2",
  921. "http://url3",
  922. };
  923. std::vector<std::string> uris(vbegin(URIS), vend(URIS));
  924. option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
  925. std::vector<SharedHandle<RequestGroup> > groups;
  926. createRequestGroupForUri(groups, option_, uris);
  927. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  928. e_->getRequestGroupMan()->addReservedGroup(groups);
  929. {
  930. PauseXmlRpcMethod m;
  931. XmlRpcRequest req(PauseXmlRpcMethod::getMethodName(), List::g());
  932. req.params->append("1");
  933. XmlRpcResponse res = m.execute(req, e_.get());
  934. CPPUNIT_ASSERT_EQUAL(0, res.code);
  935. }
  936. CPPUNIT_ASSERT(groups[0]->isPauseRequested());
  937. {
  938. UnpauseXmlRpcMethod m;
  939. XmlRpcRequest req(UnpauseXmlRpcMethod::getMethodName(), List::g());
  940. req.params->append("1");
  941. XmlRpcResponse res = m.execute(req, e_.get());
  942. CPPUNIT_ASSERT_EQUAL(0, res.code);
  943. }
  944. CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
  945. {
  946. PauseAllXmlRpcMethod m;
  947. XmlRpcRequest req(PauseAllXmlRpcMethod::getMethodName(), List::g());
  948. XmlRpcResponse res = m.execute(req, e_.get());
  949. CPPUNIT_ASSERT_EQUAL(0, res.code);
  950. }
  951. for(size_t i = 0; i < groups.size(); ++i) {
  952. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  953. }
  954. {
  955. UnpauseAllXmlRpcMethod m;
  956. XmlRpcRequest req(UnpauseAllXmlRpcMethod::getMethodName(), List::g());
  957. XmlRpcResponse res = m.execute(req, e_.get());
  958. CPPUNIT_ASSERT_EQUAL(0, res.code);
  959. }
  960. for(size_t i = 0; i < groups.size(); ++i) {
  961. CPPUNIT_ASSERT(!groups[i]->isPauseRequested());
  962. }
  963. {
  964. ForcePauseAllXmlRpcMethod m;
  965. XmlRpcRequest req(ForcePauseAllXmlRpcMethod::getMethodName(), List::g());
  966. XmlRpcResponse res = m.execute(req, e_.get());
  967. CPPUNIT_ASSERT_EQUAL(0, res.code);
  968. }
  969. for(size_t i = 0; i < groups.size(); ++i) {
  970. CPPUNIT_ASSERT(groups[i]->isPauseRequested());
  971. }
  972. }
  973. void XmlRpcMethodTest::testSystemMulticall()
  974. {
  975. SystemMulticallXmlRpcMethod m;
  976. XmlRpcRequest req("system.multicall", List::g());
  977. SharedHandle<List> reqparams = List::g();
  978. req.params->append(reqparams);
  979. for(int i = 0; i < 2; ++i) {
  980. SharedHandle<Dict> dict = Dict::g();
  981. dict->put("methodName", AddUriXmlRpcMethod::getMethodName());
  982. SharedHandle<List> params = List::g();
  983. SharedHandle<List> urisParam = List::g();
  984. urisParam->append("http://localhost/"+util::itos(i));
  985. params->append(urisParam);
  986. dict->put("params", params);
  987. reqparams->append(dict);
  988. }
  989. {
  990. SharedHandle<Dict> dict = Dict::g();
  991. dict->put("methodName", "not exists");
  992. dict->put("params", List::g());
  993. reqparams->append(dict);
  994. }
  995. {
  996. reqparams->append("not struct");
  997. }
  998. {
  999. SharedHandle<Dict> dict = Dict::g();
  1000. dict->put("methodName", "system.multicall");
  1001. dict->put("params", List::g());
  1002. reqparams->append(dict);
  1003. }
  1004. {
  1005. // missing params
  1006. SharedHandle<Dict> dict = Dict::g();
  1007. dict->put("methodName", GetVersionXmlRpcMethod::getMethodName());
  1008. reqparams->append(dict);
  1009. }
  1010. {
  1011. SharedHandle<Dict> dict = Dict::g();
  1012. dict->put("methodName", GetVersionXmlRpcMethod::getMethodName());
  1013. dict->put("params", List::g());
  1014. reqparams->append(dict);
  1015. }
  1016. XmlRpcResponse res = m.execute(req, e_.get());
  1017. CPPUNIT_ASSERT_EQUAL(0, res.code);
  1018. const List* resParams = asList(res.param);
  1019. CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
  1020. CPPUNIT_ASSERT_EQUAL(std::string("1"),
  1021. asString(asList(resParams->get(0))->get(0))->s());
  1022. CPPUNIT_ASSERT_EQUAL(std::string("2"),
  1023. asString(asList(resParams->get(1))->get(0))->s());
  1024. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1025. asInteger
  1026. (asDict(resParams->get(2))->get("faultCode"))
  1027. ->i());
  1028. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1029. asInteger
  1030. (asDict(resParams->get(3))->get("faultCode"))
  1031. ->i());
  1032. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1033. asInteger
  1034. (asDict(resParams->get(4))->get("faultCode"))
  1035. ->i());
  1036. CPPUNIT_ASSERT_EQUAL((int64_t)1,
  1037. asInteger
  1038. (asDict(resParams->get(5))->get("faultCode"))
  1039. ->i());
  1040. CPPUNIT_ASSERT(asList(resParams->get(6)));
  1041. }
  1042. void XmlRpcMethodTest::testSystemMulticall_fail()
  1043. {
  1044. SystemMulticallXmlRpcMethod m;
  1045. XmlRpcRequest req("system.multicall", List::g());
  1046. XmlRpcResponse res = m.execute(req, e_.get());
  1047. CPPUNIT_ASSERT_EQUAL(1, res.code);
  1048. }
  1049. } // namespace xmlrpc
  1050. } // namespace aria2