XmlRpcMethodTest.cc 41 KB

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