XmlRpcMethodTest.cc 39 KB

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