PageRenderTime 22ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/com/limegroup/bittorrent/swarm/BTSwarmCoordinatorTest.java

https://github.com/r3n/limewire5-ruby
Java | 352 lines | 255 code | 85 blank | 12 comment | 2 complexity | dd0e87668fb3d0f6cdec93bbd9227822 MD5 | raw file
Possible License(s): LGPL-2.1
  1. package com.limegroup.bittorrent.swarm;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.URI;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.concurrent.CountDownLatch;
  7. import java.util.concurrent.TimeUnit;
  8. import junit.framework.Test;
  9. import org.limewire.collection.Range;
  10. import org.limewire.http.reactor.LimeConnectingIOReactorFactory;
  11. import org.limewire.swarm.SwarmCoordinator;
  12. import org.limewire.swarm.SwarmCoordinatorListener;
  13. import org.limewire.swarm.SwarmFileSystem;
  14. import org.limewire.swarm.SwarmSourceType;
  15. import org.limewire.swarm.Swarmer;
  16. import org.limewire.swarm.http.SwarmHttpSource;
  17. import org.limewire.swarm.http.SwarmHttpSourceDownloader;
  18. import org.limewire.swarm.impl.EchoSwarmCoordinatorListener;
  19. import org.limewire.swarm.impl.SwarmerImpl;
  20. import org.limewire.util.AssertComparisons;
  21. import org.limewire.util.FileUtils;
  22. import org.limewire.util.TestUtils;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Injector;
  25. import com.limegroup.bittorrent.BTContext;
  26. import com.limegroup.bittorrent.BTMetaInfo;
  27. import com.limegroup.bittorrent.BTMetaInfoFactory;
  28. import com.limegroup.bittorrent.FileServer;
  29. import com.limegroup.bittorrent.TorrentContext;
  30. import com.limegroup.bittorrent.TorrentFileSystem;
  31. import com.limegroup.bittorrent.disk.DiskManagerFactory;
  32. import com.limegroup.bittorrent.disk.LoggingDiskListener;
  33. import com.limegroup.bittorrent.disk.TorrentDiskManager;
  34. import com.limegroup.bittorrent.handshaking.piecestrategy.LargestGapStartPieceStrategy;
  35. import com.limegroup.bittorrent.handshaking.piecestrategy.PieceStrategy;
  36. import com.limegroup.bittorrent.handshaking.piecestrategy.RandomGapStrategy;
  37. import com.limegroup.bittorrent.handshaking.piecestrategy.RandomPieceStrategy;
  38. import com.limegroup.gnutella.LimeWireCoreModule;
  39. import com.limegroup.gnutella.stubs.ActivityCallbackStub;
  40. import com.limegroup.gnutella.util.LimeTestCase;
  41. public class BTSwarmCoordinatorTest extends LimeTestCase {
  42. private static final int TEST_PORT = 8080;
  43. /**
  44. * A directory containing the torrent data for this unit test.
  45. */
  46. public static final File TORRENT_DIR = TestUtils
  47. .getResourceFile("org/limewire/swarm/bittorrent/public_html/torrents");
  48. /**
  49. * A directory containing the torrent data for this unit test.
  50. */
  51. public static final File FILE_DIR = TestUtils
  52. .getResourceFile("org/limewire/swarm/bittorrent/public_html");
  53. private FileServer fileServer = null;
  54. private Injector injector;
  55. private BTMetaInfoFactory metaInfoFactory;
  56. public BTSwarmCoordinatorTest(String name) {
  57. super(name);
  58. }
  59. public static Test suite() {
  60. return buildTestSuite(BTSwarmCoordinatorTest.class);
  61. }
  62. @Override
  63. protected void setUp() throws Exception {
  64. injector = Guice.createInjector(new LimeWireCoreModule(ActivityCallbackStub.class));
  65. metaInfoFactory = injector.getInstance(BTMetaInfoFactory.class);
  66. fileServer = new FileServer(TEST_PORT, FILE_DIR);
  67. fileServer.start();
  68. }
  69. @Override
  70. protected void tearDown() throws Exception {
  71. fileServer.stop();
  72. fileServer.destroy();
  73. }
  74. public void testSingleFileTorrent() throws Exception {
  75. File torrentFile = getFile("test-single-webseed-single-file-no-peer.torrent");
  76. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  77. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  78. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  79. File completeFile = torrentFileSystem.getCompleteFile();
  80. completeFile.delete();
  81. File downloadedFile = torrentFileSystem.getIncompleteFiles().get(0);
  82. downloadedFile.delete();
  83. completeFile.deleteOnExit();
  84. downloadedFile.deleteOnExit();
  85. final Swarmer swarmer = createSwarmer(torrentContext, null);
  86. swarmer.start();
  87. long totalSize = torrentFileSystem.getTotalSize();
  88. URI uri = metaInfo.getWebSeeds()[0];
  89. swarmer.addSource(new SwarmHttpSource(uri, totalSize));
  90. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile, 44425);
  91. }
  92. public void testMultiFileTorrentDefaultPieceStrategy() throws Exception {
  93. File torrentFile = getFile("test-single-webseed-multiple-file-no-peer.torrent");
  94. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  95. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  96. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  97. File completeFile = torrentFileSystem.getCompleteFile();
  98. completeFile.delete();
  99. File downloadedFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  100. File downloadedFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  101. downloadedFile1.delete();
  102. downloadedFile2.delete();
  103. completeFile.deleteOnExit();
  104. downloadedFile1.deleteOnExit();
  105. downloadedFile2.deleteOnExit();
  106. final Swarmer swarmer = createSwarmer(torrentContext, null);
  107. swarmer.start();
  108. long totalSize = torrentFileSystem.getTotalSize();
  109. URI uri = metaInfo.getWebSeeds()[0];
  110. swarmer.addSource(new SwarmHttpSource(uri, totalSize));
  111. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile1, 44425);
  112. assertDownload(swarmer, "db1dc452e77d30ce14acca6bac8c66bc", downloadedFile2, 411090);
  113. }
  114. public void testMultiFileTorrentRandomGapStrategy() throws Exception {
  115. File torrentFile = getFile("test-single-webseed-multiple-file-no-peer.torrent");
  116. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  117. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  118. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  119. File completeFile = torrentFileSystem.getCompleteFile();
  120. completeFile.delete();
  121. File downloadedFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  122. File downloadedFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  123. downloadedFile1.delete();
  124. downloadedFile2.delete();
  125. completeFile.deleteOnExit();
  126. downloadedFile1.deleteOnExit();
  127. downloadedFile2.deleteOnExit();
  128. final Swarmer swarmer = createSwarmer(torrentContext, new RandomGapStrategy(metaInfo));
  129. swarmer.start();
  130. long totalSize = torrentFileSystem.getTotalSize();
  131. URI uri = metaInfo.getWebSeeds()[0];
  132. swarmer.addSource(new SwarmHttpSource(uri, totalSize));
  133. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile1, 44425);
  134. assertDownload(swarmer, "db1dc452e77d30ce14acca6bac8c66bc", downloadedFile2, 411090);
  135. }
  136. public void testMultiFileTorrentLargestGapStartPieceStrategy() throws Exception {
  137. File torrentFile = getFile("test-single-webseed-multiple-file-no-peer.torrent");
  138. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  139. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  140. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  141. File completeFile = torrentFileSystem.getCompleteFile();
  142. completeFile.delete();
  143. File downloadedFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  144. File downloadedFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  145. downloadedFile1.delete();
  146. downloadedFile2.delete();
  147. completeFile.deleteOnExit();
  148. downloadedFile1.deleteOnExit();
  149. downloadedFile2.deleteOnExit();
  150. final Swarmer swarmer = createSwarmer(torrentContext, new LargestGapStartPieceStrategy(
  151. metaInfo));
  152. swarmer.start();
  153. long totalSize = torrentFileSystem.getTotalSize();
  154. URI uri = metaInfo.getWebSeeds()[0];
  155. swarmer.addSource(new SwarmHttpSource(uri, totalSize));
  156. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile1, 44425);
  157. assertDownload(swarmer, "db1dc452e77d30ce14acca6bac8c66bc", downloadedFile2, 411090);
  158. }
  159. public void testVuzeCreatedTorrent() throws Exception {
  160. File torrentFile = getFile("test_vuze_getright.torrent");
  161. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  162. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  163. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  164. File completeFile = torrentFileSystem.getCompleteFile();
  165. completeFile.delete();
  166. File downloadedFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  167. downloadedFile1.delete();
  168. completeFile.deleteOnExit();
  169. downloadedFile1.deleteOnExit();
  170. final Swarmer swarmer = createSwarmer(torrentContext, new LargestGapStartPieceStrategy(
  171. metaInfo));
  172. swarmer.start();
  173. long totalSize = torrentFileSystem.getTotalSize();
  174. URI uri1 = metaInfo.getWebSeeds()[0];
  175. URI uri2 = metaInfo.getWebSeeds()[1];
  176. swarmer.addSource(new SwarmHttpSource(uri1, totalSize));
  177. swarmer.addSource(new SwarmHttpSource(uri2, totalSize));
  178. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile1, 44425);
  179. }
  180. public void testMultipleWebseedSingleFileTorrent() throws Exception {
  181. File torrentFile = getFile("test-multiple-webseed-single-file-no-peer.torrent");
  182. final BTMetaInfo metaInfo = metaInfoFactory.createMetaInfo(torrentFile);
  183. final TorrentContext torrentContext = new BTContext(metaInfo, new DiskManagerFactory());
  184. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  185. File completeFile = torrentFileSystem.getCompleteFile();
  186. completeFile.delete();
  187. File downloadedFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  188. downloadedFile1.delete();
  189. completeFile.deleteOnExit();
  190. downloadedFile1.deleteOnExit();
  191. final Swarmer swarmer = createSwarmer(torrentContext, new LargestGapStartPieceStrategy(
  192. metaInfo));
  193. swarmer.start();
  194. long totalSize = torrentFileSystem.getTotalSize();
  195. URI uri1 = metaInfo.getWebSeeds()[0];
  196. URI uri2 = metaInfo.getWebSeeds()[1];
  197. URI uri3 = metaInfo.getWebSeeds()[2];
  198. swarmer.addSource(new SwarmHttpSource(uri1, totalSize));
  199. swarmer.addSource(new SwarmHttpSource(uri2, totalSize));
  200. swarmer.addSource(new SwarmHttpSource(uri3, totalSize));
  201. assertDownload(swarmer, "8055d620ba0c507c1af957b43648c99f", downloadedFile1, 44425);
  202. }
  203. private File getFile(String fileName) {
  204. File torrentFile = new File(TORRENT_DIR.getAbsoluteFile() + "/" + fileName);
  205. return torrentFile;
  206. }
  207. private Swarmer createSwarmer(TorrentContext torrentContext, PieceStrategy pieceStrategy)
  208. throws IOException {
  209. if (pieceStrategy == null) {
  210. pieceStrategy = new RandomPieceStrategy(torrentContext.getMetaInfo());
  211. }
  212. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  213. TorrentDiskManager torrentDiskManager = torrentContext.getDiskManager();
  214. torrentDiskManager.open(new LoggingDiskListener());
  215. final BTSwarmCoordinator btCoordinator = new BTSwarmCoordinator(torrentContext
  216. .getMetaInfo(), torrentFileSystem, torrentDiskManager, pieceStrategy);
  217. btCoordinator.addListener(new EchoSwarmCoordinatorListener());
  218. Swarmer swarmer = new SwarmerImpl(btCoordinator);
  219. swarmer.register(SwarmSourceType.HTTP, new SwarmHttpSourceDownloader(injector
  220. .getInstance(LimeConnectingIOReactorFactory.class), btCoordinator, "LimeTest/1.1"));
  221. return swarmer;
  222. }
  223. /**
  224. * Asserts that the given file has the correct size, and matches the given
  225. * md5sum.
  226. */
  227. private void assertDownload(Swarmer swarmer, String md5, File file, long fileSize)
  228. throws InterruptedException, NoSuchAlgorithmException, IOException {
  229. SwarmCoordinator swarmCoordinator = swarmer.getCoordinator();
  230. final CountDownLatch countDownLatch = new CountDownLatch(1);
  231. swarmCoordinator.addListener(new SwarmCoordinatorListener() {
  232. @Override
  233. public void blockLeased(SwarmCoordinator swarmCoordinator, Range block) {
  234. }
  235. @Override
  236. public void blockPending(SwarmCoordinator swarmCoordinator, Range block) {
  237. }
  238. @Override
  239. public void blockUnleased(SwarmCoordinator swarmCoordinator, Range block) {
  240. }
  241. @Override
  242. public void blockUnpending(SwarmCoordinator swarmCoordinator, Range block) {
  243. }
  244. @Override
  245. public void blockVerificationFailed(SwarmCoordinator swarmCoordinator, Range block) {
  246. }
  247. @Override
  248. public void blockVerified(SwarmCoordinator swarmCoordinator, Range block) {
  249. }
  250. @Override
  251. public void blockWritten(SwarmCoordinator swarmCoordinator, Range block) {
  252. }
  253. @Override
  254. public void downloadCompleted(SwarmCoordinator swarmCoordinator,
  255. SwarmFileSystem fileSystem) {
  256. countDownLatch.countDown();
  257. }
  258. });
  259. countDownLatch.await(5, TimeUnit.SECONDS);
  260. AssertComparisons.assertTrue(file.exists());
  261. AssertComparisons.assertEquals(fileSize, file.length());
  262. String testmd5 = FileUtils.getMD5(file);
  263. AssertComparisons.assertEquals(md5, testmd5);
  264. }
  265. // TODO test better variety of torrent files.
  266. // TODO test larger torrent files.
  267. }