XmlRpcMethodTest.cc 40 KB

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