DefaultBtContextTest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "DefaultBtContext.h"
  2. #include "Util.h"
  3. #include "Exception.h"
  4. #include <cppunit/extensions/HelperMacros.h>
  5. using namespace std;
  6. class DefaultBtContextTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(DefaultBtContextTest);
  8. CPPUNIT_TEST(testGetInfoHash);
  9. CPPUNIT_TEST(testGetPieceHash);
  10. CPPUNIT_TEST(testGetFileEntries);
  11. CPPUNIT_TEST(testGetTotalLength);
  12. CPPUNIT_TEST(testGetFileEntriesSingle);
  13. CPPUNIT_TEST(testGetTotalLengthSingle);
  14. CPPUNIT_TEST(testGetFileModeMulti);
  15. CPPUNIT_TEST(testGetFileModeSingle);
  16. CPPUNIT_TEST(testGetNameMulti);
  17. CPPUNIT_TEST(testGetNameSingle);
  18. CPPUNIT_TEST(testGetAnnounceTier);
  19. CPPUNIT_TEST(testGetAnnounceTierAnnounceList);
  20. CPPUNIT_TEST(testGetPieceLength);
  21. CPPUNIT_TEST(testGetInfoHashAsString);
  22. CPPUNIT_TEST(testGetPeerId);
  23. CPPUNIT_TEST_SUITE_END();
  24. public:
  25. void setUp() {
  26. }
  27. void testGetInfoHash();
  28. void testGetPieceHash();
  29. void testGetFileEntries();
  30. void testGetTotalLength();
  31. void testGetFileEntriesSingle();
  32. void testGetTotalLengthSingle();
  33. void testGetFileModeMulti();
  34. void testGetFileModeSingle();
  35. void testGetNameMulti();
  36. void testGetNameSingle();
  37. void testGetAnnounceTier();
  38. void testGetAnnounceTierAnnounceList();
  39. void testGetPieceLength();
  40. void testGetInfoHashAsString();
  41. void testGetPeerId();
  42. };
  43. CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtContextTest);
  44. void DefaultBtContextTest::testGetInfoHash() {
  45. DefaultBtContext btContext;
  46. btContext.load("test.torrent");
  47. string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  48. CPPUNIT_ASSERT_EQUAL(20, btContext.getInfoHashLength());
  49. CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(),
  50. btContext.getInfoHashLength()));
  51. }
  52. void DefaultBtContextTest::testGetPieceHash() {
  53. DefaultBtContext btContext;
  54. btContext.load("test.torrent");
  55. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"AAAAAAAAAAAAAAAAAAAA", 20),
  56. btContext.getPieceHash(0));
  57. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"BBBBBBBBBBBBBBBBBBBB", 20),
  58. btContext.getPieceHash(1));
  59. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"CCCCCCCCCCCCCCCCCCCC", 20),
  60. btContext.getPieceHash(2));
  61. CPPUNIT_ASSERT_EQUAL(string(""),
  62. btContext.getPieceHash(-1));
  63. CPPUNIT_ASSERT_EQUAL(string(""),
  64. btContext.getPieceHash(3));
  65. }
  66. void DefaultBtContextTest::testGetFileEntries() {
  67. DefaultBtContext btContext;
  68. btContext.load("test.torrent");
  69. // This is multi-file torrent.
  70. FileEntries fileEntries = btContext.getFileEntries();
  71. // There are 2 file entries.
  72. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  73. FileEntries::iterator itr = fileEntries.begin();
  74. FileEntryHandle fileEntry1 = *itr;
  75. CPPUNIT_ASSERT_EQUAL(string("aria2/src/aria2c"),
  76. fileEntry1->getPath());
  77. itr++;
  78. FileEntryHandle fileEntry2 = *itr;
  79. CPPUNIT_ASSERT_EQUAL(string("aria2-0.2.2.tar.bz2"),
  80. fileEntry2->getPath());
  81. }
  82. void DefaultBtContextTest::testGetFileEntriesSingle() {
  83. DefaultBtContext btContext;
  84. btContext.load("single.torrent");
  85. // This is multi-file torrent.
  86. FileEntries fileEntries = btContext.getFileEntries();
  87. // There is 1 file entry.
  88. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  89. FileEntries::iterator itr = fileEntries.begin();
  90. FileEntryHandle fileEntry1 = *itr;
  91. CPPUNIT_ASSERT_EQUAL(string("aria2-0.8.2.tar.bz2"),
  92. fileEntry1->getPath());
  93. }
  94. void DefaultBtContextTest::testGetTotalLength() {
  95. DefaultBtContext btContext;
  96. btContext.load("test.torrent");
  97. CPPUNIT_ASSERT_EQUAL((long long int)384,
  98. btContext.getTotalLength());
  99. }
  100. void DefaultBtContextTest::testGetTotalLengthSingle() {
  101. DefaultBtContext btContext;
  102. btContext.load("single.torrent");
  103. CPPUNIT_ASSERT_EQUAL((long long int)384,
  104. btContext.getTotalLength());
  105. }
  106. void DefaultBtContextTest::testGetFileModeMulti() {
  107. DefaultBtContext btContext;
  108. btContext.load("test.torrent");
  109. CPPUNIT_ASSERT_EQUAL(BtContext::MULTI,
  110. btContext.getFileMode());
  111. }
  112. void DefaultBtContextTest::testGetFileModeSingle() {
  113. DefaultBtContext btContext;
  114. btContext.load("single.torrent");
  115. CPPUNIT_ASSERT_EQUAL(BtContext::SINGLE,
  116. btContext.getFileMode());
  117. }
  118. void DefaultBtContextTest::testGetNameMulti() {
  119. DefaultBtContext btContext;
  120. btContext.load("test.torrent");
  121. CPPUNIT_ASSERT_EQUAL(string("aria2-test"),
  122. btContext.getName());
  123. }
  124. void DefaultBtContextTest::testGetNameSingle() {
  125. DefaultBtContext btContext;
  126. btContext.load("single.torrent");
  127. CPPUNIT_ASSERT_EQUAL(string("aria2-0.8.2.tar.bz2"),
  128. btContext.getName());
  129. }
  130. void DefaultBtContextTest::testGetAnnounceTier() {
  131. DefaultBtContext btContext;
  132. btContext.load("single.torrent");
  133. AnnounceTiers tiers = btContext.getAnnounceTiers();
  134. // There is 1 tier.
  135. CPPUNIT_ASSERT_EQUAL((size_t)1,
  136. tiers.size());
  137. AnnounceTiers::iterator itr = tiers.begin();
  138. AnnounceTierHandle tier1 = *itr;
  139. CPPUNIT_ASSERT_EQUAL((size_t)1,
  140. tier1->urls.size());
  141. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/announce.php"),
  142. tier1->urls.at(0));
  143. }
  144. void DefaultBtContextTest::testGetAnnounceTierAnnounceList() {
  145. DefaultBtContext btContext;
  146. btContext.load("test.torrent");
  147. AnnounceTiers tiers = btContext.getAnnounceTiers();
  148. // There are 3 tiers.
  149. CPPUNIT_ASSERT_EQUAL((size_t)3,
  150. tiers.size());
  151. AnnounceTierHandle tier1 = tiers.at(0);
  152. CPPUNIT_ASSERT_EQUAL((size_t)1,
  153. tier1->urls.size());
  154. CPPUNIT_ASSERT_EQUAL(string("http://tracker1"),
  155. tier1->urls.at(0));
  156. AnnounceTierHandle tier2 = tiers.at(1);
  157. CPPUNIT_ASSERT_EQUAL((size_t)1,
  158. tier2->urls.size());
  159. CPPUNIT_ASSERT_EQUAL(string("http://tracker2"),
  160. tier2->urls.at(0));
  161. AnnounceTierHandle tier3 = tiers.at(2);
  162. CPPUNIT_ASSERT_EQUAL((size_t)1,
  163. tier3->urls.size());
  164. CPPUNIT_ASSERT_EQUAL(string("http://tracker3"),
  165. tier3->urls.at(0));
  166. }
  167. void DefaultBtContextTest::testGetPieceLength() {
  168. DefaultBtContext btContext;
  169. btContext.load("test.torrent");
  170. CPPUNIT_ASSERT_EQUAL(128,
  171. btContext.getPieceLength());
  172. }
  173. void DefaultBtContextTest::testGetInfoHashAsString() {
  174. DefaultBtContext btContext;
  175. btContext.load("test.torrent");
  176. CPPUNIT_ASSERT_EQUAL(string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  177. btContext.getInfoHashAsString());
  178. }
  179. void DefaultBtContextTest::testGetPeerId() {
  180. DefaultBtContext btContext;
  181. Util::torrentUrlencode(btContext.getPeerId(), 20);
  182. }