RpcMethodTest.cc 45 KB

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