XmlRpcMethodTest.cc 40 KB

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