RpcMethodTest.cc 40 KB

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