PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/com/limegroup/bittorrent/BTDownloaderImplTest.java

https://github.com/r3n/limewire5-ruby
Java | 395 lines | 282 code | 82 blank | 31 comment | 12 complexity | ca06191458f81b17127df2e433a2b2e2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. package com.limegroup.bittorrent;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.security.NoSuchAlgorithmException;
  5. import junit.framework.Test;
  6. import org.limewire.core.settings.ConnectionSettings;
  7. import org.limewire.util.AssertComparisons;
  8. import org.limewire.util.FileUtils;
  9. import org.limewire.util.TestUtils;
  10. import com.google.inject.Guice;
  11. import com.google.inject.Injector;
  12. import com.google.inject.Stage;
  13. import com.limegroup.gnutella.ActivityCallbackAdapter;
  14. import com.limegroup.gnutella.LimeWireCoreModule;
  15. import com.limegroup.gnutella.downloader.CoreDownloaderFactory;
  16. import com.limegroup.gnutella.util.LimeTestCase;
  17. /**
  18. * Test cases for the BTDownloader.
  19. *
  20. * It takes a torrent file and downloads it, then checks the files contents
  21. * against an MD5.
  22. *
  23. */
  24. public class BTDownloaderImplTest extends LimeTestCase {
  25. private static final int TEST_PORT = 8080;
  26. /**
  27. * A directory containing the torrent data for this unit test.
  28. */
  29. public static final File TORRENT_DIR = TestUtils
  30. .getResourceFile("org/limewire/swarm/bittorrent/public_html/torrents");
  31. /**
  32. * A directory containing the download data for this unit test.
  33. */
  34. public static final File FILE_DIR = TestUtils
  35. .getResourceFile("org/limewire/swarm/bittorrent/public_html");
  36. private boolean localIsPrivateBackup = false;
  37. private boolean forceIPAddressBackup = false;
  38. private String forceIPAddressStringBackup = null;
  39. private FileServer fileServer = null;
  40. public BTDownloaderImplTest(String name) {
  41. super(name);
  42. }
  43. public static Test suite() {
  44. return buildTestSuite(BTDownloaderImplTest.class);
  45. }
  46. @Override
  47. protected void setUp() throws Exception {
  48. localIsPrivateBackup = ConnectionSettings.LOCAL_IS_PRIVATE.getValue();
  49. ConnectionSettings.LOCAL_IS_PRIVATE.setValue(false);
  50. forceIPAddressBackup = ConnectionSettings.FORCE_IP_ADDRESS.getValue();
  51. ConnectionSettings.FORCE_IP_ADDRESS.setValue(true);
  52. forceIPAddressStringBackup = ConnectionSettings.FORCED_IP_ADDRESS_STRING.getValue();
  53. ConnectionSettings.FORCED_IP_ADDRESS_STRING.setValue("127.0.0.1");
  54. fileServer = new FileServer(TEST_PORT, FILE_DIR);
  55. fileServer.start();
  56. super.setUp();
  57. }
  58. @Override
  59. protected void tearDown() throws Exception {
  60. ConnectionSettings.LOCAL_IS_PRIVATE.setValue(localIsPrivateBackup);
  61. ConnectionSettings.FORCE_IP_ADDRESS.setValue(forceIPAddressBackup);
  62. ConnectionSettings.FORCED_IP_ADDRESS_STRING.setValue(forceIPAddressStringBackup);
  63. fileServer.stop();
  64. fileServer.destroy();
  65. super.tearDown();
  66. }
  67. /**
  68. * This test tries to download a single file torrent from a tracker/peer
  69. * setup on the www.limewire.org server.
  70. */
  71. public void testSingleFilePeer() throws Exception {
  72. File torrentFile = createFile("test-peer-dl-single-file.torrent");
  73. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  74. TorrentContext torrentContext = downloader.getTorrentContext();
  75. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  76. File completeFile = torrentFileSystem.getCompleteFile();
  77. completeFile.delete();
  78. completeFile.deleteOnExit();
  79. File incompleteFile = torrentFileSystem.getIncompleteFiles().get(0);
  80. incompleteFile.delete();
  81. incompleteFile.deleteOnExit();
  82. downloader.startDownload();
  83. finishDownload(downloader);
  84. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile, 44425);
  85. }
  86. /**
  87. * This test tries to download a multi file torrent from a tracker/peer
  88. * setup on the www.limewire.org server.
  89. */
  90. public void testMultipleFilePeer() throws Exception {
  91. File torrentFile = createFile("test-peer-dl-multiple-file.torrent");
  92. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  93. TorrentContext torrentContext = downloader.getTorrentContext();
  94. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  95. File rootFile = torrentFileSystem.getCompleteFile();
  96. try {
  97. FileUtils.deleteRecursive(rootFile);
  98. rootFile.deleteOnExit();
  99. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  100. incompleteFile1.delete();
  101. incompleteFile1.deleteOnExit();
  102. File completeFile1 = torrentFileSystem.getFiles().get(0);
  103. completeFile1.delete();
  104. completeFile1.deleteOnExit();
  105. File incompleteFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  106. incompleteFile2.delete();
  107. incompleteFile2.deleteOnExit();
  108. File completeFile2 = torrentFileSystem.getFiles().get(1);
  109. completeFile2.delete();
  110. completeFile2.deleteOnExit();
  111. downloader.startDownload();
  112. finishDownload(downloader);
  113. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  114. assertDownload("db1dc452e77d30ce14acca6bac8c66bc", completeFile2, 411090);
  115. } finally {
  116. if (rootFile != null) {
  117. FileUtils.deleteRecursive(rootFile);
  118. }
  119. }
  120. }
  121. public void testSingleWebSeedSingleFileNoPeer() throws Exception {
  122. File torrentFile = createFile("test-single-webseed-single-file-no-peer.torrent");
  123. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  124. TorrentContext torrentContext = downloader.getTorrentContext();
  125. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  126. File rootFile = torrentFileSystem.getCompleteFile();
  127. rootFile.delete();
  128. rootFile.deleteOnExit();
  129. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  130. incompleteFile1.delete();
  131. incompleteFile1.deleteOnExit();
  132. File completeFile1 = torrentFileSystem.getFiles().get(0);
  133. completeFile1.delete();
  134. completeFile1.deleteOnExit();
  135. downloader.startDownload();
  136. finishDownload(downloader);
  137. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  138. }
  139. public void testMultiWebSeedSingleFileNoPeer() throws Exception {
  140. File torrentFile = createFile("test-multiple-webseed-single-file-no-peer.torrent");
  141. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  142. TorrentContext torrentContext = downloader.getTorrentContext();
  143. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  144. File rootFile = torrentFileSystem.getCompleteFile();
  145. rootFile.delete();
  146. rootFile.deleteOnExit();
  147. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  148. incompleteFile1.delete();
  149. incompleteFile1.deleteOnExit();
  150. File completeFile1 = torrentFileSystem.getFiles().get(0);
  151. completeFile1.delete();
  152. completeFile1.deleteOnExit();
  153. downloader.startDownload();
  154. finishDownload(downloader);
  155. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  156. }
  157. public void testSingleWebSeedMultipleFileNoPeer() throws Exception {
  158. File torrentFile = createFile("test-single-webseed-multiple-file-no-peer.torrent");
  159. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  160. TorrentContext torrentContext = downloader.getTorrentContext();
  161. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  162. File rootFile = torrentFileSystem.getCompleteFile();
  163. try {
  164. FileUtils.deleteRecursive(rootFile);
  165. rootFile.deleteOnExit();
  166. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  167. incompleteFile1.delete();
  168. incompleteFile1.deleteOnExit();
  169. File completeFile1 = torrentFileSystem.getFiles().get(0);
  170. completeFile1.delete();
  171. completeFile1.deleteOnExit();
  172. File incompleteFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  173. incompleteFile2.delete();
  174. incompleteFile2.deleteOnExit();
  175. File completeFile2 = torrentFileSystem.getFiles().get(1);
  176. completeFile2.delete();
  177. completeFile2.deleteOnExit();
  178. downloader.startDownload();
  179. finishDownload(downloader);
  180. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  181. assertDownload("db1dc452e77d30ce14acca6bac8c66bc", completeFile2, 411090);
  182. } finally {
  183. if (rootFile != null) {
  184. FileUtils.deleteRecursive(rootFile);
  185. }
  186. }
  187. }
  188. public void testMultiWebSeedMultipleFileNoPeer() throws Exception {
  189. File torrentFile = createFile("test-multiple-webseed-multiple-file-no-peer.torrent");
  190. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  191. TorrentContext torrentContext = downloader.getTorrentContext();
  192. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  193. File rootFile = torrentFileSystem.getCompleteFile();
  194. try {
  195. FileUtils.deleteRecursive(rootFile);
  196. rootFile.deleteOnExit();
  197. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  198. incompleteFile1.delete();
  199. incompleteFile1.deleteOnExit();
  200. File completeFile1 = torrentFileSystem.getFiles().get(0);
  201. completeFile1.delete();
  202. completeFile1.deleteOnExit();
  203. File incompleteFile2 = torrentFileSystem.getIncompleteFiles().get(1);
  204. incompleteFile2.delete();
  205. incompleteFile2.deleteOnExit();
  206. File completeFile2 = torrentFileSystem.getFiles().get(1);
  207. completeFile2.delete();
  208. completeFile2.deleteOnExit();
  209. downloader.startDownload();
  210. finishDownload(downloader);
  211. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  212. assertDownload("db1dc452e77d30ce14acca6bac8c66bc", completeFile2, 411090);
  213. } finally {
  214. if (rootFile != null) {
  215. FileUtils.deleteRecursive(rootFile);
  216. }
  217. }
  218. }
  219. public void testSingleWebSeedSingleFilePeers() throws Exception {
  220. // TODO force peers and webseed to not have all pieces
  221. // only when used together have all the pieces
  222. File torrentFile = createFile("test-single-webseed-single-file-peer.torrent");
  223. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  224. TorrentContext torrentContext = downloader.getTorrentContext();
  225. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  226. File rootFile = torrentFileSystem.getCompleteFile();
  227. try {
  228. FileUtils.deleteRecursive(rootFile);
  229. rootFile.deleteOnExit();
  230. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  231. incompleteFile1.delete();
  232. incompleteFile1.deleteOnExit();
  233. File completeFile1 = torrentFileSystem.getFiles().get(0);
  234. completeFile1.delete();
  235. completeFile1.deleteOnExit();
  236. downloader.startDownload();
  237. finishDownload(downloader);
  238. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  239. } finally {
  240. if (rootFile != null) {
  241. FileUtils.deleteRecursive(rootFile);
  242. }
  243. }
  244. }
  245. /**
  246. * This test has a peer and a bad webseed address.
  247. * The bad address should be ignored and the download will happen from the peer.
  248. */
  249. public void testSingleBadWebSeedSingleFilePeers() throws Exception {
  250. File torrentFile = createFile("test-single-badwebseed-single-file-peer.torrent");
  251. BTDownloaderImpl downloader = createBTDownloader(torrentFile);
  252. TorrentContext torrentContext = downloader.getTorrentContext();
  253. TorrentFileSystem torrentFileSystem = torrentContext.getFileSystem();
  254. File rootFile = torrentFileSystem.getCompleteFile();
  255. try {
  256. FileUtils.deleteRecursive(rootFile);
  257. rootFile.deleteOnExit();
  258. File incompleteFile1 = torrentFileSystem.getIncompleteFiles().get(0);
  259. incompleteFile1.delete();
  260. incompleteFile1.deleteOnExit();
  261. File completeFile1 = torrentFileSystem.getFiles().get(0);
  262. completeFile1.delete();
  263. completeFile1.deleteOnExit();
  264. downloader.startDownload();
  265. finishDownload(downloader);
  266. assertDownload("8055d620ba0c507c1af957b43648c99f", completeFile1, 44425);
  267. } finally {
  268. if (rootFile != null) {
  269. FileUtils.deleteRecursive(rootFile);
  270. }
  271. }
  272. }
  273. private File createFile(String fileName) {
  274. String torrentfilePath = TORRENT_DIR.getAbsolutePath() + "/" + fileName;
  275. File torrentFile = new File(torrentfilePath);
  276. return torrentFile;
  277. }
  278. private BTDownloaderImpl createBTDownloader(File torrentFile) throws IOException {
  279. AssertComparisons.assertTrue(torrentFile.exists());
  280. Injector injector = Guice.createInjector(Stage.PRODUCTION, new LimeWireCoreModule(
  281. ActivityCallbackAdapter.class));
  282. final BTMetaInfo metaInfo = injector.getInstance(BTMetaInfoFactory.class).createMetaInfo(torrentFile);
  283. CoreDownloaderFactory coreDownloaderFactory = injector
  284. .getInstance(CoreDownloaderFactory.class);
  285. BTDownloaderImpl downloader = (BTDownloaderImpl) coreDownloaderFactory
  286. .createBTDownloader(metaInfo);
  287. downloader.initBtMetaInfo(metaInfo);
  288. return downloader;
  289. }
  290. private void finishDownload(BTDownloader downloader) throws InterruptedException {
  291. int maxIterations = 100;
  292. int index = 0;
  293. while (!downloader.isCompleted()) {
  294. if (index++ > maxIterations) {
  295. AssertComparisons.fail("Failure downloading the file. Taking too long.");
  296. }
  297. Thread.sleep(1000);
  298. }
  299. }
  300. /**
  301. * Asserts that the given file has the correct size, and matches the given
  302. * md5sum.
  303. */
  304. private void assertDownload(String md5, File file, long fileSize) throws InterruptedException,
  305. NoSuchAlgorithmException, IOException {
  306. long sleepTime = (long) ((fileSize * 0.0001) + 3000);
  307. Thread.sleep(sleepTime);
  308. AssertComparisons.assertTrue(file.exists());
  309. AssertComparisons.assertEquals(fileSize, file.length());
  310. String testmd5 = FileUtils.getMD5(file);
  311. AssertComparisons.assertEquals(md5, testmd5);
  312. }
  313. }