DefaultBtContextTest.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(testComputeFastSet);
  24. CPPUNIT_TEST_SUITE_END();
  25. public:
  26. void setUp() {
  27. }
  28. void testGetInfoHash();
  29. void testGetPieceHash();
  30. void testGetFileEntries();
  31. void testGetTotalLength();
  32. void testGetFileEntriesSingle();
  33. void testGetTotalLengthSingle();
  34. void testGetFileModeMulti();
  35. void testGetFileModeSingle();
  36. void testGetNameMulti();
  37. void testGetNameSingle();
  38. void testGetAnnounceTier();
  39. void testGetAnnounceTierAnnounceList();
  40. void testGetPieceLength();
  41. void testGetInfoHashAsString();
  42. void testGetPeerId();
  43. void testComputeFastSet();
  44. };
  45. CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtContextTest);
  46. void DefaultBtContextTest::testGetInfoHash() {
  47. DefaultBtContext btContext;
  48. btContext.load("test.torrent");
  49. string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
  50. CPPUNIT_ASSERT_EQUAL((int32_t)20, btContext.getInfoHashLength());
  51. CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(),
  52. btContext.getInfoHashLength()));
  53. }
  54. void DefaultBtContextTest::testGetPieceHash() {
  55. DefaultBtContext btContext;
  56. btContext.load("test.torrent");
  57. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"AAAAAAAAAAAAAAAAAAAA", 20),
  58. btContext.getPieceHash(0));
  59. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"BBBBBBBBBBBBBBBBBBBB", 20),
  60. btContext.getPieceHash(1));
  61. CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"CCCCCCCCCCCCCCCCCCCC", 20),
  62. btContext.getPieceHash(2));
  63. CPPUNIT_ASSERT_EQUAL(string(""),
  64. btContext.getPieceHash(-1));
  65. CPPUNIT_ASSERT_EQUAL(string(""),
  66. btContext.getPieceHash(3));
  67. }
  68. void DefaultBtContextTest::testGetFileEntries() {
  69. DefaultBtContext btContext;
  70. btContext.load("test.torrent");
  71. // This is multi-file torrent.
  72. FileEntries fileEntries = btContext.getFileEntries();
  73. // There are 2 file entries.
  74. CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size());
  75. FileEntries::iterator itr = fileEntries.begin();
  76. FileEntryHandle fileEntry1 = *itr;
  77. CPPUNIT_ASSERT_EQUAL(string("aria2/src/aria2c"),
  78. fileEntry1->getPath());
  79. itr++;
  80. FileEntryHandle fileEntry2 = *itr;
  81. CPPUNIT_ASSERT_EQUAL(string("aria2-0.2.2.tar.bz2"),
  82. fileEntry2->getPath());
  83. }
  84. void DefaultBtContextTest::testGetFileEntriesSingle() {
  85. DefaultBtContext btContext;
  86. btContext.load("single.torrent");
  87. // This is multi-file torrent.
  88. FileEntries fileEntries = btContext.getFileEntries();
  89. // There is 1 file entry.
  90. CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size());
  91. FileEntries::iterator itr = fileEntries.begin();
  92. FileEntryHandle fileEntry1 = *itr;
  93. CPPUNIT_ASSERT_EQUAL(string("aria2-0.8.2.tar.bz2"),
  94. fileEntry1->getPath());
  95. }
  96. void DefaultBtContextTest::testGetTotalLength() {
  97. DefaultBtContext btContext;
  98. btContext.load("test.torrent");
  99. CPPUNIT_ASSERT_EQUAL((long long int)384,
  100. btContext.getTotalLength());
  101. }
  102. void DefaultBtContextTest::testGetTotalLengthSingle() {
  103. DefaultBtContext btContext;
  104. btContext.load("single.torrent");
  105. CPPUNIT_ASSERT_EQUAL((long long int)384,
  106. btContext.getTotalLength());
  107. }
  108. void DefaultBtContextTest::testGetFileModeMulti() {
  109. DefaultBtContext btContext;
  110. btContext.load("test.torrent");
  111. CPPUNIT_ASSERT_EQUAL(BtContext::MULTI,
  112. btContext.getFileMode());
  113. }
  114. void DefaultBtContextTest::testGetFileModeSingle() {
  115. DefaultBtContext btContext;
  116. btContext.load("single.torrent");
  117. CPPUNIT_ASSERT_EQUAL(BtContext::SINGLE,
  118. btContext.getFileMode());
  119. }
  120. void DefaultBtContextTest::testGetNameMulti() {
  121. DefaultBtContext btContext;
  122. btContext.load("test.torrent");
  123. CPPUNIT_ASSERT_EQUAL(string("aria2-test"),
  124. btContext.getName());
  125. }
  126. void DefaultBtContextTest::testGetNameSingle() {
  127. DefaultBtContext btContext;
  128. btContext.load("single.torrent");
  129. CPPUNIT_ASSERT_EQUAL(string("aria2-0.8.2.tar.bz2"),
  130. btContext.getName());
  131. }
  132. void DefaultBtContextTest::testGetAnnounceTier() {
  133. DefaultBtContext btContext;
  134. btContext.load("single.torrent");
  135. AnnounceTiers tiers = btContext.getAnnounceTiers();
  136. // There is 1 tier.
  137. CPPUNIT_ASSERT_EQUAL((size_t)1,
  138. tiers.size());
  139. AnnounceTiers::iterator itr = tiers.begin();
  140. AnnounceTierHandle tier1 = *itr;
  141. CPPUNIT_ASSERT_EQUAL((size_t)1,
  142. tier1->urls.size());
  143. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/announce.php"),
  144. tier1->urls.at(0));
  145. }
  146. void DefaultBtContextTest::testGetAnnounceTierAnnounceList() {
  147. DefaultBtContext btContext;
  148. btContext.load("test.torrent");
  149. AnnounceTiers tiers = btContext.getAnnounceTiers();
  150. // There are 3 tiers.
  151. CPPUNIT_ASSERT_EQUAL((size_t)3,
  152. tiers.size());
  153. AnnounceTierHandle tier1 = tiers.at(0);
  154. CPPUNIT_ASSERT_EQUAL((size_t)1,
  155. tier1->urls.size());
  156. CPPUNIT_ASSERT_EQUAL(string("http://tracker1"),
  157. tier1->urls.at(0));
  158. AnnounceTierHandle tier2 = tiers.at(1);
  159. CPPUNIT_ASSERT_EQUAL((size_t)1,
  160. tier2->urls.size());
  161. CPPUNIT_ASSERT_EQUAL(string("http://tracker2"),
  162. tier2->urls.at(0));
  163. AnnounceTierHandle tier3 = tiers.at(2);
  164. CPPUNIT_ASSERT_EQUAL((size_t)1,
  165. tier3->urls.size());
  166. CPPUNIT_ASSERT_EQUAL(string("http://tracker3"),
  167. tier3->urls.at(0));
  168. }
  169. void DefaultBtContextTest::testGetPieceLength() {
  170. DefaultBtContext btContext;
  171. btContext.load("test.torrent");
  172. CPPUNIT_ASSERT_EQUAL((int32_t)128,
  173. btContext.getPieceLength());
  174. }
  175. void DefaultBtContextTest::testGetInfoHashAsString() {
  176. DefaultBtContext btContext;
  177. btContext.load("test.torrent");
  178. CPPUNIT_ASSERT_EQUAL(string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  179. btContext.getInfoHashAsString());
  180. }
  181. void DefaultBtContextTest::testGetPeerId() {
  182. DefaultBtContext btContext;
  183. Util::torrentUrlencode(btContext.getPeerId(), 20);
  184. }
  185. void DefaultBtContextTest::testComputeFastSet()
  186. {
  187. string ipaddr = "192.168.0.1";
  188. unsigned char infoHash[20];
  189. memset(infoHash, 0, sizeof(infoHash));
  190. infoHash[0] = 0xff;
  191. int pieces = 1000;
  192. int fastSetSize = 10;
  193. DefaultBtContext btContext;
  194. btContext.setInfoHash(infoHash);
  195. btContext.setNumPieces(pieces);
  196. Integers fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  197. //for_each(fastSet.begin(), fastSet.end(), Printer());
  198. //cerr << endl;
  199. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  200. Integers ansSet1(&ans1[0], &ans1[10]);
  201. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  202. ipaddr = "10.0.0.1";
  203. fastSet = btContext.computeFastSet(ipaddr, fastSetSize);
  204. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  205. Integers ansSet2(&ans2[0], &ans2[10]);
  206. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  207. }