Metalink2RequestGroupTest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
  52. CPPUNIT_ASSERT_EQUAL
  53. (std::string("a96cf3f0266b91d87d5124cf94326422800b627d"),
  54. util::toHex(dctx->getDigest()));
  55. CPPUNIT_ASSERT(dctx->getSignature());
  56. CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType());
  57. }
  58. // second file
  59. {
  60. std::shared_ptr<RequestGroup> rg = groups[1];
  61. std::vector<std::string> uris;
  62. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  63. CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
  64. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  65. CPPUNIT_ASSERT(dctx);
  66. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType());
  67. CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size());
  68. CPPUNIT_ASSERT_EQUAL(262144, dctx->getPieceLength());
  69. CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType());
  70. CPPUNIT_ASSERT_EQUAL
  71. (std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"),
  72. util::toHex(dctx->getDigest()));
  73. CPPUNIT_ASSERT(!dctx->getSignature());
  74. }
  75. #ifdef ENABLE_BITTORRENT
  76. // fifth file <- downloading .torrent file
  77. {
  78. std::shared_ptr<RequestGroup> rg = groups[4];
  79. std::vector<std::string> uris;
  80. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  81. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  82. CPPUNIT_ASSERT_EQUAL
  83. (std::string("http://host/torrent-http.integrated.torrent"), uris[0]);
  84. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  85. CPPUNIT_ASSERT(dctx);
  86. CPPUNIT_ASSERT_EQUAL(groups[5]->getGID(), rg->belongsTo());
  87. }
  88. #endif // ENABLE_BITTORRENT
  89. // sixth file <- depends on fifth file to download .torrent file.
  90. {
  91. #ifdef ENABLE_BITTORRENT
  92. std::shared_ptr<RequestGroup> rg = groups[5];
  93. #else
  94. std::shared_ptr<RequestGroup> rg = groups[4];
  95. #endif // ENABLE_BITTORRENT
  96. std::vector<std::string> uris;
  97. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  98. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  99. CPPUNIT_ASSERT_EQUAL
  100. (std::string("http://host/torrent-http.integrated"), uris[0]);
  101. const std::shared_ptr<DownloadContext>& dctx = rg->getDownloadContext();
  102. CPPUNIT_ASSERT(dctx);
  103. }
  104. }
  105. void Metalink2RequestGroupTest::testGenerate_with_local_metaurl()
  106. {
  107. std::vector<std::shared_ptr<RequestGroup> > groups;
  108. option_->put(PREF_DIR, "/tmp");
  109. // local metaurl does not work without --metalink-base-uri option.
  110. // Make sure that it does not crash with local metaurl.
  111. Metalink2RequestGroup().generate(groups, A2_TEST_DIR"/local-metaurl.meta4",
  112. option_);
  113. CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
  114. CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/README"),
  115. groups[0]->getDownloadContext()->getFirstFileEntry()
  116. ->getRemainingUris()[0]);
  117. }
  118. void Metalink2RequestGroupTest::testGenerate_groupByMetaurl()
  119. {
  120. std::vector<std::shared_ptr<RequestGroup> > groups;
  121. Metalink2RequestGroup().generate(groups,
  122. A2_TEST_DIR"/metalink4-groupbymetaurl.xml",
  123. option_);
  124. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  125. #ifdef ENABLE_BITTORRENT
  126. // first RequestGroup is torrent for second RequestGroup
  127. {
  128. std::shared_ptr<RequestGroup> rg = groups[0];
  129. std::vector<std::string> uris;
  130. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  131. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  132. CPPUNIT_ASSERT_EQUAL(std::string("http://torrent"), uris[0]);
  133. }
  134. // second
  135. {
  136. std::shared_ptr<RequestGroup> rg = groups[1];
  137. std::shared_ptr<DownloadContext> dctx = rg->getDownloadContext();
  138. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  139. dctx->getFileEntries();
  140. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  141. CPPUNIT_ASSERT_EQUAL(std::string("./file1"), fileEntries[0]->getPath());
  142. CPPUNIT_ASSERT_EQUAL(std::string("file1"), fileEntries[0]->getOriginalName());
  143. CPPUNIT_ASSERT_EQUAL(std::string("file1"),
  144. fileEntries[0]->getSuffixPath());
  145. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
  146. CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"),
  147. fileEntries[0]->getRemainingUris()[0]);
  148. CPPUNIT_ASSERT_EQUAL(std::string("./file3"), fileEntries[1]->getPath());
  149. CPPUNIT_ASSERT_EQUAL(std::string("file3"), fileEntries[1]->getOriginalName());
  150. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[1]->getRemainingUris().size());
  151. CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"),
  152. fileEntries[1]->getRemainingUris()[0]);
  153. }
  154. // third
  155. {
  156. std::shared_ptr<RequestGroup> rg = groups[2];
  157. std::shared_ptr<DownloadContext> dctx = rg->getDownloadContext();
  158. const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
  159. dctx->getFileEntries();
  160. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  161. CPPUNIT_ASSERT_EQUAL(std::string("./file2"), fileEntries[0]->getPath());
  162. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries[0]->getRemainingUris().size());
  163. CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"),
  164. fileEntries[0]->getRemainingUris()[0]);
  165. }
  166. #else // !ENABLE_BITTORRENT
  167. {
  168. std::shared_ptr<RequestGroup> rg = groups[0];
  169. std::vector<std::string> uris;
  170. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  171. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  172. CPPUNIT_ASSERT_EQUAL(std::string("http://file1p1"), uris[0]);
  173. }
  174. {
  175. std::shared_ptr<RequestGroup> rg = groups[1];
  176. std::vector<std::string> uris;
  177. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  178. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  179. CPPUNIT_ASSERT_EQUAL(std::string("http://file2p1"), uris[0]);
  180. }
  181. {
  182. std::shared_ptr<RequestGroup> rg = groups[2];
  183. std::vector<std::string> uris;
  184. rg->getDownloadContext()->getFirstFileEntry()->getUris(uris);
  185. CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
  186. CPPUNIT_ASSERT_EQUAL(std::string("http://file3p1"), uris[0]);
  187. }
  188. #endif // !ENABLE_BITTORRENT
  189. }
  190. void Metalink2RequestGroupTest::testGenerate_dosDirTraversal()
  191. {
  192. #ifdef __MINGW32__
  193. #ifdef ENABLE_BITTORRENT
  194. std::vector<std::shared_ptr<RequestGroup> > groups;
  195. option_->put(PREF_DIR, "/tmp");
  196. Metalink2RequestGroup().generate
  197. (groups, A2_TEST_DIR"/metalink4-dosdirtraversal.xml", option_);
  198. CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
  199. std::shared_ptr<RequestGroup> rg = groups[0];
  200. std::shared_ptr<FileEntry> file = rg->getDownloadContext()->getFirstFileEntry();
  201. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cexample.ext"),
  202. file->getPath());
  203. rg = groups[2];
  204. file = rg->getDownloadContext()->getFileEntries()[0];
  205. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile1.ext"),
  206. file->getPath());
  207. file = rg->getDownloadContext()->getFileEntries()[1];
  208. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/..%5C..%5Cfile2.ext"),
  209. file->getPath());
  210. #endif // ENABLE_BITTORRENT
  211. #endif // __MINGW32__
  212. }
  213. } // namespace aria2