XmlRpcMethodTest.cc 40 KB

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