Metalink2RequestGroupTest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "Metalink2RequestGroup.h"
  2. #include <algorithm>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "DownloadContext.h"
  5. #include "prefs.h"
  6. #include "Option.h"
  7. #include "RequestGroup.h"
  8. #include "FileEntry.h"
  9. #include "Signature.h"
  10. namespace aria2 {
  11. class Metalink2RequestGroupTest:public CppUnit::TestFixture {
  12. CPPUNIT_TEST_SUITE(Metalink2RequestGroupTest);
  13. CPPUNIT_TEST(testGenerate);
  14. CPPUNIT_TEST(testGenerate_with_local_metaurl);
  15. CPPUNIT_TEST(testGenerate_groupByMetaurl);
  16. CPPUNIT_TEST(testGenerate_dosDirTraversal);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. std::shared_ptr<Option> option_;
  20. public:
  21. void setUp()
  22. {
  23. option_.reset(new Option());
  24. option_->put(PREF_SPLIT, "1");
  25. }
  26. void testGenerate();
  27. void testGenerate_with_local_metaurl();
  28. void testGenerate_groupByMetaurl();
  29. void testGenerate_dosDirTraversal();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION( Metalink2RequestGroupTest );
  32. void Metalink2RequestGroupTest::testGenerate()
  33. {
  34. std::vector<std::shared_ptr<RequestGroup> > groups;
  35. option_->put(PREF_DIR, "/tmp");
  36. Metalink2RequestGroup().generate(groups, A2_TEST_DIR"/test.xml", option_);
  37. // first file
  38. {
  39. std::shared_ptr<RequestGroup> rg = groups[0];
  40. std::vector<std::string> uris;
  41. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  42. std::sort(uris.begin(), uris.end());
  43. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  44. CPPUNIT_ASSERT_EQUAL
  45. (std::string("ftp://ftphost/aria2-0.5.2.tar.bz2"), uris[0]);
  46. CPPUNIT_ASSERT_EQUAL
  47. (std::string("http://httphost/aria2-0.5.2.tar.bz2"), uris[1]);
  48. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  49. CPPUNIT_ASSERT(dctx);
  50. CPPUNIT_ASSERT_EQUAL((int64_t)0LL, dctx->getTotalLength());
  51. #ifdef ENABLE_MESSAGE_DIGEST
  52. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
  53. CPPUNIT_ASSERT_EQUAL
  54. (std::string("a96cf3f0266b91d87d5124cf94326422800b627d"),
  55. util::toHex(dctx->getDigest()));
  56. #endif // ENABLE_MESSAGE_DIGEST
  57. CPPUNIT_ASSERT(dctx->getSignature());
  58. CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType());
  59. }
  60. // second file
  61. {
  62. std::shared_ptr<RequestGroup> rg = groups[1];
  63. std::vector<std::string> uris;
  64. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  65. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  66. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  67. CPPUNIT_ASSERT(dctx);
  68. #ifdef ENABLE_MESSAGE_DIGEST
  69. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
  70. CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size());
  71. CPPUNIT_ASSERT_EQUAL(262144, dctx->getPieceLength());
  72. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
  73. CPPUNIT_ASSERT_EQUAL
  74. (std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"),
  75. util::toHex(dctx->getDigest()));
  76. #endif // ENABLE_MESSAGE_DIGEST
  77. CPPUNIT_ASSERT(!dctx->getSignature());
  78. }
  79. #ifdef ENABLE_BITTORRENT
  80. // fifth file <- downloading .torrent file
  81. {
  82. std::shared_ptr<RequestGroup> rg = groups[4];
  83. std::vector<std::string> uris;
  84. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  85. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  86. CPPUNIT_ASSERT_EQUAL
  87. (std::string("http://host/torrent-http.integrated.torrent"), uris[0]);
  88. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  89. CPPUNIT_ASSERT(dctx);
  90. CPPUNIT_ASSERT_EQUAL(groups[5]->getGID(), rg->belongsTo());
  91. }
  92. #endif // ENABLE_BITTORRENT
  93. // sixth file <- depends on fifth file to download .torrent file.
  94. {
  95. #ifdef ENABLE_BITTORRENT
  96. std::shared_ptr<RequestGroup> rg = groups[5];
  97. #else
  98. std::shared_ptr<RequestGroup> rg = groups[4];
  99. #endif // ENABLE_BITTORRENT
  100. std::vector<std::string> uris;
  101. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  102. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  103. CPPUNIT_ASSERT_EQUAL
  104. (std::string("http://host/torrent-http.integrated"), uris[0]);
  105. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  106. CPPUNIT_ASSERT(dctx);
  107. }
  108. }
  109. void Metalink2RequestGroupTest::testGenerate_with_local_metaurl()
  110. {
  111. std::vector<std::shared_ptr<RequestGroup> > groups;
  112. option_->put(PREF_DIR, "/tmp");
  113. // local metaurl does not work without --metalink-base-uri option.
  114. // Make sure that it does not crash with local metaurl.
  115. Metalink2RequestGroup().generate(groups, A2_TEST_DIR"/local-metaurl.meta4",
  116. option_);
  117. CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
  118. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/README"),
  119. groups[0]->getDownloadContext()->getFirstFileEntry()
  120. ->getRemainingUris()[0]);
  121. }
  122. void Metalink2RequestGroupTest::testGenerate_groupByMetaurl()
  123. {
  124. std::vector<std::shared_ptr<RequestGroup> > groups;
  125. Metalink2RequestGroup().generate(groups,
  126. A2_TEST_DIR"/metalink4-groupbymetaurl.xml",
  127. option_);
  128. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  129. #ifdef ENABLE_BITTORRENT
  130. // first RequestGroup is torrent for second RequestGroup
  131. {
  132. std::shared_ptr<RequestGroup> rg = groups[0];
  133. std::vector<std::string> uris;
  134. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  135. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  136. CPPUNIT_ASSERT_EQUAL(std::string("http://torrent"), uris[0]);
  137. }
  138. // second
  139. {
  140. std::shared_ptr<RequestGroup> rg = groups[1];
  141. std::shared_ptr<DownloadContext> dctx = rg->getDownloadContext();
  142. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  143. dctx->getFileEntries();
  144. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  145. CPPUNIT_ASSERT_EQUAL(std::string("./file1"), fileEntries[0]->getPath());
  146. CPPUNIT_ASSERT_EQUAL(std::string("file1"), fileEntries[0]->getOriginalName());
  147. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
  148. CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"),
  149. fileEntries[0]->getRemainingUris()[0]);
  150. CPPUNIT_ASSERT_EQUAL(std::string("./file3"), fileEntries[1]->getPath());
  151. CPPUNIT_ASSERT_EQUAL(std::string("file3"), fileEntries[1]->getOriginalName());
  152. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[1]->getRemainingUris().size());
  153. CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"),
  154. fileEntries[1]->getRemainingUris()[0]);
  155. }
  156. // third
  157. {
  158. std::shared_ptr<RequestGroup> rg = groups[2];
  159. std::shared_ptr<DownloadContext> dctx = rg->getDownloadContext();
  160. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  161. dctx->getFileEntries();
  162. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  163. CPPUNIT_ASSERT_EQUAL(std::string("./file2"), fileEntries[0]->getPath());
  164. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
  165. CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"),
  166. fileEntries[0]->getRemainingUris()[0]);
  167. }
  168. #else // !ENABLE_BITTORRENT
  169. {
  170. std::shared_ptr<RequestGroup> rg = groups[0];
  171. std::vector<std::string> uris;
  172. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  173. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  174. CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"), uris[0]);
  175. }
  176. {
  177. std::shared_ptr<RequestGroup> rg = groups[1];
  178. std::vector<std::string> uris;
  179. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  180. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  181. CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"), uris[0]);
  182. }
  183. {
  184. std::shared_ptr<RequestGroup> rg = groups[2];
  185. std::vector<std::string> uris;
  186. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  187. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  188. CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"), uris[0]);
  189. }
  190. #endif // !ENABLE_BITTORRENT
  191. }
  192. void Metalink2RequestGroupTest::testGenerate_dosDirTraversal()
  193. {
  194. #ifdef __MINGW32__
  195. #ifdef ENABLE_BITTORRENT
  196. std::vector<std::shared_ptr<RequestGroup> > groups;
  197. option_->put(PREF_DIR, "/tmp");
  198. Metalink2RequestGroup().generate
  199. (groups, A2_TEST_DIR"/metalink4-dosdirtraversal.xml", option_);
  200. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  201. std::shared_ptr<RequestGroup> rg = groups[0];
  202. std::shared_ptr<FileEntry> file = rg->getDownloadContext()->getFirstFileEntry();
  203. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cexample.ext"),
  204. file->getPath());
  205. rg = groups[2];
  206. file = rg->getDownloadContext()->getFileEntries()[0];
  207. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile1.ext"),
  208. file->getPath());
  209. file = rg->getDownloadContext()->getFileEntries()[1];
  210. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile2.ext"),
  211. file->getPath());
  212. #endif // ENABLE_BITTORRENT
  213. #endif // __MINGW32__
  214. }
  215. } // namespace aria2