Aria2ApiTest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "aria2api.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "prefs.h"
  4. #include "OptionParser.h"
  5. #include "OptionHandler.h"
  6. namespace aria2 {
  7. class Aria2ApiTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(Aria2ApiTest);
  9. CPPUNIT_TEST(testAddUri);
  10. CPPUNIT_TEST(testAddMetalink);
  11. CPPUNIT_TEST(testAddTorrent);
  12. CPPUNIT_TEST(testRemovePause);
  13. CPPUNIT_TEST(testChangePosition);
  14. CPPUNIT_TEST(testChangeOption);
  15. CPPUNIT_TEST(testChangeGlobalOption);
  16. CPPUNIT_TEST_SUITE_END();
  17. Session* session_;
  18. public:
  19. void setUp()
  20. {
  21. SessionConfig config;
  22. KeyVals options;
  23. session_ = sessionNew(options, config);
  24. }
  25. void tearDown()
  26. {
  27. sessionFinal(session_);
  28. }
  29. void testAddUri();
  30. void testAddMetalink();
  31. void testAddTorrent();
  32. void testRemovePause();
  33. void testChangePosition();
  34. void testChangeOption();
  35. void testChangeGlobalOption();
  36. };
  37. CPPUNIT_TEST_SUITE_REGISTRATION(Aria2ApiTest);
  38. void Aria2ApiTest::testAddUri()
  39. {
  40. A2Gid gid;
  41. std::vector<std::string> uris(1);
  42. KeyVals options;
  43. uris[0] = "http://localhost/1";
  44. CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));
  45. CPPUNIT_ASSERT(!isNull(gid));
  46. DownloadHandle* hd = getDownloadHandle(session_, gid);
  47. CPPUNIT_ASSERT(hd);
  48. CPPUNIT_ASSERT_EQUAL(1, hd->getNumFiles());
  49. FileData file = hd->getFile(1);
  50. CPPUNIT_ASSERT_EQUAL((size_t)1, file.uris.size());
  51. CPPUNIT_ASSERT_EQUAL(uris[0], file.uris[0].uri);
  52. deleteDownloadHandle(hd);
  53. options.push_back(KeyVals::value_type("file-allocation", "foo"));
  54. CPPUNIT_ASSERT_EQUAL(-1, addUri(session_, &gid, uris, options));
  55. }
  56. void Aria2ApiTest::testAddMetalink()
  57. {
  58. std::string metalinkPath = A2_TEST_DIR"/metalink4.xml";
  59. std::vector<A2Gid> gids;
  60. KeyVals options;
  61. #ifdef ENABLE_METALINK
  62. CPPUNIT_ASSERT_EQUAL(0, addMetalink(session_, &gids, metalinkPath, options));
  63. #ifdef ENABLE_BITTORRENT
  64. CPPUNIT_ASSERT_EQUAL((size_t)2, gids.size());
  65. #else // !ENABLE_BITTORRENT
  66. CPPUNIT_ASSERT_EQUAL((size_t)1, gids.size());
  67. #endif // !ENABLE_BITTORRENT
  68. gids.clear();
  69. options.push_back(KeyVals::value_type("file-allocation", "foo"));
  70. CPPUNIT_ASSERT_EQUAL(-1, addMetalink(session_, &gids, metalinkPath,
  71. options));
  72. #else // !ENABLE_METALINK
  73. CPPUNIT_ASSERT_EQUAL(-1, addMetalink(session_, &gids, metalinkPath,
  74. options));
  75. #endif // !ENABLE_METALINK
  76. }
  77. void Aria2ApiTest::testAddTorrent()
  78. {
  79. std::string torrentPath = A2_TEST_DIR"/test.torrent";
  80. A2Gid gid;
  81. KeyVals options;
  82. #ifdef ENABLE_BITTORRENT
  83. CPPUNIT_ASSERT_EQUAL(0, addTorrent(session_, &gid, torrentPath, options));
  84. CPPUNIT_ASSERT(!isNull(gid));
  85. options.push_back(KeyVals::value_type("file-allocation", "foo"));
  86. CPPUNIT_ASSERT_EQUAL(-1, addTorrent(session_, &gid, torrentPath, options));
  87. #else // !ENABLE_BITTORRENT
  88. CPPUNIT_ASSERT_EQUAL(-1, addTorrent(session_, &gid, torrentPath, options));
  89. #endif // !ENABLE_BITTORRENT
  90. }
  91. void Aria2ApiTest::testRemovePause()
  92. {
  93. A2Gid gid;
  94. std::vector<std::string> uris(1);
  95. KeyVals options;
  96. uris[0] = "http://localhost/1";
  97. CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));
  98. DownloadHandle* hd = getDownloadHandle(session_, gid);
  99. CPPUNIT_ASSERT(hd);
  100. CPPUNIT_ASSERT_EQUAL(DOWNLOAD_WAITING, hd->getStatus());
  101. deleteDownloadHandle(hd);
  102. CPPUNIT_ASSERT_EQUAL(-1, pauseDownload(session_, (A2Gid)0));
  103. CPPUNIT_ASSERT_EQUAL(0, pauseDownload(session_, gid));
  104. hd = getDownloadHandle(session_, gid);
  105. CPPUNIT_ASSERT(hd);
  106. CPPUNIT_ASSERT_EQUAL(DOWNLOAD_PAUSED, hd->getStatus());
  107. deleteDownloadHandle(hd);
  108. CPPUNIT_ASSERT_EQUAL(-1, unpauseDownload(session_, (A2Gid)0));
  109. CPPUNIT_ASSERT_EQUAL(0, unpauseDownload(session_, gid));
  110. hd = getDownloadHandle(session_, gid);
  111. CPPUNIT_ASSERT(hd);
  112. CPPUNIT_ASSERT_EQUAL(DOWNLOAD_WAITING, hd->getStatus());
  113. deleteDownloadHandle(hd);
  114. CPPUNIT_ASSERT_EQUAL(-1, removeDownload(session_, (A2Gid)0));
  115. CPPUNIT_ASSERT_EQUAL(0, removeDownload(session_, gid));
  116. hd = getDownloadHandle(session_, gid);
  117. CPPUNIT_ASSERT(!hd);
  118. }
  119. void Aria2ApiTest::testChangePosition()
  120. {
  121. int N = 10;
  122. std::vector<A2Gid> gids(N);
  123. std::vector<std::string> uris(1);
  124. KeyVals options;
  125. uris[0] = "http://localhost/";
  126. for(int i = 0; i < N; ++i) {
  127. CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gids[i], uris, options));
  128. }
  129. CPPUNIT_ASSERT_EQUAL(-1, changePosition(session_, (A2Gid)0, -2,
  130. OFFSET_MODE_CUR));
  131. CPPUNIT_ASSERT_EQUAL(2, changePosition(session_, gids[4], -2,
  132. OFFSET_MODE_CUR));
  133. CPPUNIT_ASSERT_EQUAL(5, changePosition(session_, gids[4], 5,
  134. OFFSET_MODE_SET));
  135. CPPUNIT_ASSERT_EQUAL(7, changePosition(session_, gids[4], -2,
  136. OFFSET_MODE_END));
  137. }
  138. void Aria2ApiTest::testChangeOption()
  139. {
  140. A2Gid gid;
  141. std::vector<std::string> uris(1);
  142. KeyVals options;
  143. uris[0] = "http://localhost/1";
  144. options.push_back(KeyVals::value_type("dir", "mydownload"));
  145. CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));
  146. DownloadHandle* hd = getDownloadHandle(session_, gid);
  147. CPPUNIT_ASSERT(hd);
  148. CPPUNIT_ASSERT_EQUAL(1, hd->getNumFiles());
  149. FileData file = hd->getFile(1);
  150. CPPUNIT_ASSERT_EQUAL((size_t)1, file.uris.size());
  151. CPPUNIT_ASSERT_EQUAL(uris[0], file.uris[0].uri);
  152. CPPUNIT_ASSERT_EQUAL(std::string("mydownload"), hd->getOption("dir"));
  153. CPPUNIT_ASSERT(hd->getOption("unknown").empty());
  154. KeyVals retopts = hd->getOptions();
  155. CPPUNIT_ASSERT(std::find(retopts.begin(), retopts.end(),
  156. KeyVals::value_type("dir", "mydownload"))
  157. != retopts.end());
  158. // Don't return hidden option
  159. CPPUNIT_ASSERT(hd->getOption(PREF_STARTUP_IDLE_TIME->k).empty());
  160. deleteDownloadHandle(hd);
  161. // failure with null gid
  162. CPPUNIT_ASSERT_EQUAL(-1, changeOption(session_, (A2Gid)0, options));
  163. // change option
  164. options.clear();
  165. options.push_back(KeyVals::value_type("dir", "newlocation"));
  166. CPPUNIT_ASSERT_EQUAL(0, changeOption(session_, gid, options));
  167. hd = getDownloadHandle(session_, gid);
  168. CPPUNIT_ASSERT_EQUAL(std::string("newlocation"), hd->getOption("dir"));
  169. deleteDownloadHandle(hd);
  170. // failure with bad option value
  171. options.clear();
  172. options.push_back(KeyVals::value_type("file-allocation", "foo"));
  173. CPPUNIT_ASSERT_EQUAL(-1, changeOption(session_, gid, options));
  174. }
  175. void Aria2ApiTest::testChangeGlobalOption()
  176. {
  177. CPPUNIT_ASSERT_EQUAL(OptionParser::getInstance()->find(PREF_FILE_ALLOCATION)
  178. ->getDefaultValue(),
  179. getGlobalOption(session_, PREF_FILE_ALLOCATION->k));
  180. KeyVals options;
  181. options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "none"));
  182. CPPUNIT_ASSERT_EQUAL(0, changeGlobalOption(session_, options));
  183. CPPUNIT_ASSERT_EQUAL(std::string("none"),
  184. getGlobalOption(session_, PREF_FILE_ALLOCATION->k));
  185. // Don't return hidden option
  186. CPPUNIT_ASSERT(getGlobalOption(session_, PREF_STARTUP_IDLE_TIME->k).empty());
  187. // failure with bad option value
  188. options.clear();
  189. options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "foo"));
  190. CPPUNIT_ASSERT_EQUAL(-1, changeGlobalOption(session_, options));
  191. }
  192. } // namespace aria2