Aria2ApiTest.cc 8.35 KB
Newer Older
1 2 3 4
#include "aria2api.h"

#include <cppunit/extensions/HelperMacros.h>

5
#include "TestUtil.h"
6 7 8
#include "prefs.h"
#include "OptionParser.h"
#include "OptionHandler.h"
9 10 11 12 13
#include "RequestGroupMan.h"
#include "Context.h"
#include "MultiUrlRequestInfo.h"
#include "DownloadEngine.h"
#include "Option.h"
14 15 16

namespace aria2 {

17
class Aria2ApiTest : public CppUnit::TestFixture {
18 19 20 21 22 23 24 25 26

  CPPUNIT_TEST_SUITE(Aria2ApiTest);
  CPPUNIT_TEST(testAddUri);
  CPPUNIT_TEST(testAddMetalink);
  CPPUNIT_TEST(testAddTorrent);
  CPPUNIT_TEST(testRemovePause);
  CPPUNIT_TEST(testChangePosition);
  CPPUNIT_TEST(testChangeOption);
  CPPUNIT_TEST(testChangeGlobalOption);
27
  CPPUNIT_TEST(testDownloadResultDH);
28 29 30
  CPPUNIT_TEST_SUITE_END();

  Session* session_;
31

32 33 34 35
public:
  void setUp()
  {
    SessionConfig config;
36
    KeyVals options = {{"no-conf", "true"}};
37 38 39
    session_ = sessionNew(options, config);
  }

40
  void tearDown() { sessionFinal(session_); }
41 42 43 44 45 46 47 48

  void testAddUri();
  void testAddMetalink();
  void testAddTorrent();
  void testRemovePause();
  void testChangePosition();
  void testChangeOption();
  void testChangeGlobalOption();
49
  void testDownloadResultDH();
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
};

CPPUNIT_TEST_SUITE_REGISTRATION(Aria2ApiTest);

void Aria2ApiTest::testAddUri()
{
  A2Gid gid;
  std::vector<std::string> uris(1);
  KeyVals options;
  uris[0] = "http://localhost/1";
  CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));
  CPPUNIT_ASSERT(!isNull(gid));

  DownloadHandle* hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(hd);
  CPPUNIT_ASSERT_EQUAL(1, hd->getNumFiles());
  FileData file = hd->getFile(1);
  CPPUNIT_ASSERT_EQUAL((size_t)1, file.uris.size());
  CPPUNIT_ASSERT_EQUAL(uris[0], file.uris[0].uri);
  deleteDownloadHandle(hd);

  options.push_back(KeyVals::value_type("file-allocation", "foo"));
  CPPUNIT_ASSERT_EQUAL(-1, addUri(session_, &gid, uris, options));
}

void Aria2ApiTest::testAddMetalink()
{
77
  std::string metalinkPath = A2_TEST_DIR "/metalink4.xml";
78 79 80 81 82 83
  std::vector<A2Gid> gids;
  KeyVals options;
#ifdef ENABLE_METALINK
  CPPUNIT_ASSERT_EQUAL(0, addMetalink(session_, &gids, metalinkPath, options));
#ifdef ENABLE_BITTORRENT
  CPPUNIT_ASSERT_EQUAL((size_t)2, gids.size());
84
#else  // !ENABLE_BITTORRENT
85 86 87 88 89
  CPPUNIT_ASSERT_EQUAL((size_t)1, gids.size());
#endif // !ENABLE_BITTORRENT

  gids.clear();
  options.push_back(KeyVals::value_type("file-allocation", "foo"));
90 91 92
  CPPUNIT_ASSERT_EQUAL(-1, addMetalink(session_, &gids, metalinkPath, options));
#else  // !ENABLE_METALINK
  CPPUNIT_ASSERT_EQUAL(-1, addMetalink(session_, &gids, metalinkPath, options));
93 94 95 96 97
#endif // !ENABLE_METALINK
}

void Aria2ApiTest::testAddTorrent()
{
98
  std::string torrentPath = A2_TEST_DIR "/test.torrent";
99 100 101 102 103 104 105 106
  A2Gid gid;
  KeyVals options;
#ifdef ENABLE_BITTORRENT
  CPPUNIT_ASSERT_EQUAL(0, addTorrent(session_, &gid, torrentPath, options));
  CPPUNIT_ASSERT(!isNull(gid));

  options.push_back(KeyVals::value_type("file-allocation", "foo"));
  CPPUNIT_ASSERT_EQUAL(-1, addTorrent(session_, &gid, torrentPath, options));
107
#else  // !ENABLE_BITTORRENT
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  CPPUNIT_ASSERT_EQUAL(-1, addTorrent(session_, &gid, torrentPath, options));
#endif // !ENABLE_BITTORRENT
}

void Aria2ApiTest::testRemovePause()
{
  A2Gid gid;
  std::vector<std::string> uris(1);
  KeyVals options;
  uris[0] = "http://localhost/1";
  CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));

  DownloadHandle* hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(hd);
  CPPUNIT_ASSERT_EQUAL(DOWNLOAD_WAITING, hd->getStatus());
  deleteDownloadHandle(hd);

  CPPUNIT_ASSERT_EQUAL(-1, pauseDownload(session_, (A2Gid)0));
  CPPUNIT_ASSERT_EQUAL(0, pauseDownload(session_, gid));
  hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(hd);
  CPPUNIT_ASSERT_EQUAL(DOWNLOAD_PAUSED, hd->getStatus());
  deleteDownloadHandle(hd);

  CPPUNIT_ASSERT_EQUAL(-1, unpauseDownload(session_, (A2Gid)0));
  CPPUNIT_ASSERT_EQUAL(0, unpauseDownload(session_, gid));
  hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(hd);
  CPPUNIT_ASSERT_EQUAL(DOWNLOAD_WAITING, hd->getStatus());
  deleteDownloadHandle(hd);

  CPPUNIT_ASSERT_EQUAL(-1, removeDownload(session_, (A2Gid)0));
  CPPUNIT_ASSERT_EQUAL(0, removeDownload(session_, gid));
  hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(!hd);
}

void Aria2ApiTest::testChangePosition()
{
  int N = 10;
  std::vector<A2Gid> gids(N);
  std::vector<std::string> uris(1);
  KeyVals options;
  uris[0] = "http://localhost/";
152
  for (int i = 0; i < N; ++i) {
153 154
    CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gids[i], uris, options));
  }
155 156 157 158
  CPPUNIT_ASSERT_EQUAL(-1,
                       changePosition(session_, (A2Gid)0, -2, OFFSET_MODE_CUR));
  CPPUNIT_ASSERT_EQUAL(2,
                       changePosition(session_, gids[4], -2, OFFSET_MODE_CUR));
159

160 161
  CPPUNIT_ASSERT_EQUAL(5,
                       changePosition(session_, gids[4], 5, OFFSET_MODE_SET));
162

163 164
  CPPUNIT_ASSERT_EQUAL(7,
                       changePosition(session_, gids[4], -2, OFFSET_MODE_END));
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

void Aria2ApiTest::testChangeOption()
{
  A2Gid gid;
  std::vector<std::string> uris(1);
  KeyVals options;
  uris[0] = "http://localhost/1";
  options.push_back(KeyVals::value_type("dir", "mydownload"));
  CPPUNIT_ASSERT_EQUAL(0, addUri(session_, &gid, uris, options));

  DownloadHandle* hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT(hd);
  CPPUNIT_ASSERT_EQUAL(1, hd->getNumFiles());
  FileData file = hd->getFile(1);
  CPPUNIT_ASSERT_EQUAL((size_t)1, file.uris.size());
  CPPUNIT_ASSERT_EQUAL(uris[0], file.uris[0].uri);

  CPPUNIT_ASSERT_EQUAL(std::string("mydownload"), hd->getOption("dir"));
  CPPUNIT_ASSERT(hd->getOption("unknown").empty());
  KeyVals retopts = hd->getOptions();
  CPPUNIT_ASSERT(std::find(retopts.begin(), retopts.end(),
187 188
                           KeyVals::value_type("dir", "mydownload")) !=
                 retopts.end());
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  // Don't return hidden option
  CPPUNIT_ASSERT(hd->getOption(PREF_STARTUP_IDLE_TIME->k).empty());
  deleteDownloadHandle(hd);
  // failure with null gid
  CPPUNIT_ASSERT_EQUAL(-1, changeOption(session_, (A2Gid)0, options));
  // change option
  options.clear();
  options.push_back(KeyVals::value_type("dir", "newlocation"));
  CPPUNIT_ASSERT_EQUAL(0, changeOption(session_, gid, options));
  hd = getDownloadHandle(session_, gid);
  CPPUNIT_ASSERT_EQUAL(std::string("newlocation"), hd->getOption("dir"));
  deleteDownloadHandle(hd);
  // failure with bad option value
  options.clear();
  options.push_back(KeyVals::value_type("file-allocation", "foo"));
  CPPUNIT_ASSERT_EQUAL(-1, changeOption(session_, gid, options));
}

void Aria2ApiTest::testChangeGlobalOption()
{
209 210 211
  CPPUNIT_ASSERT_EQUAL(OptionParser::getInstance()
                           ->find(PREF_FILE_ALLOCATION)
                           ->getDefaultValue(),
212 213 214 215 216 217 218 219 220 221 222 223 224 225
                       getGlobalOption(session_, PREF_FILE_ALLOCATION->k));
  KeyVals options;
  options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "none"));
  CPPUNIT_ASSERT_EQUAL(0, changeGlobalOption(session_, options));
  CPPUNIT_ASSERT_EQUAL(std::string("none"),
                       getGlobalOption(session_, PREF_FILE_ALLOCATION->k));
  // Don't return hidden option
  CPPUNIT_ASSERT(getGlobalOption(session_, PREF_STARTUP_IDLE_TIME->k).empty());
  // failure with bad option value
  options.clear();
  options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "foo"));
  CPPUNIT_ASSERT_EQUAL(-1, changeGlobalOption(session_, options));
}

226 227 228
void Aria2ApiTest::testDownloadResultDH()
{
  std::shared_ptr<DownloadResult> dr1 =
229
      createDownloadResult(error_code::TIME_OUT, "http://example.org/timeout");
230
  dr1->option->put(PREF_DIR, "mydownload");
231 232
  std::shared_ptr<DownloadResult> dr2 = createDownloadResult(
      error_code::NETWORK_PROBLEM, "http://example.org/network");
233
  auto& gman =
234
      session_->context->reqinfo->getDownloadEngine()->getRequestGroupMan();
235 236 237 238 239 240
  gman->addDownloadResult(dr1);
  gman->addDownloadResult(dr2);

  DownloadHandle* hd = getDownloadHandle(session_, dr1->gid->getNumericId());
  CPPUNIT_ASSERT_EQUAL(DOWNLOAD_ERROR, hd->getStatus());
  CPPUNIT_ASSERT_EQUAL((int)error_code::TIME_OUT, hd->getErrorCode());
241
  CPPUNIT_ASSERT_EQUAL(std::string("mydownload"), hd->getOption(PREF_DIR->k));
242 243 244 245
  // Don't return hidden option
  CPPUNIT_ASSERT(hd->getOption(PREF_STARTUP_IDLE_TIME->k).empty());
  KeyVals retopts = hd->getOptions();
  CPPUNIT_ASSERT(std::find(retopts.begin(), retopts.end(),
246 247
                           KeyVals::value_type(PREF_DIR->k, "mydownload")) !=
                 retopts.end());
248 249 250
  deleteDownloadHandle(hd);
}

251
} // namespace aria2