public void TestMultiFileTorrentCheckKeyContents(string key, string expected) { MetaInfoFile torrentFile = new MetaInfoFile(Constants.MultiFileTorrent); torrentFile.Parse(); string actual = System.Text.Encoding.UTF8.GetString(torrentFile.metaInfoDict[key]); Assert.Equal(expected, actual); }
public void TestInvalidPieceNumberPassedToIncrementPieceCount() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); Assert.Throws <IndexOutOfRangeException>(() => tc.IncrementPeerCount(Constants.InvalidPieceNumber)); }
public void TestMarkPieceMissingWithInvalidPieceNumber() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); Assert.Throws <IndexOutOfRangeException>(() => tc.MarkPieceMissing(Constants.InvalidPieceNumber, true)); }
public void TestEmptyStringPassedToIsPeerInSwarm() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); Assert.Throws <ArgumentException>(() => tc.IsPeerInSwarm("")); }
public void TestSetPieceLengthLargerThanDefaultPieceSize() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); BitTorrentException error = Assert.Throws <BitTorrentException>(() => tc.SetPieceLength(0, UInt32.MaxValue)); Assert.Equal("BitTorrent Error: Piece length larger than maximum for torrent.", error.Message); }
public void TestPassNullForBufferToAddBlockFromPacket() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); PieceBuffer pieceBuffer = new PieceBuffer(tc, BitTorrentLibrary.Constants.BlockSize); Assert.Throws <ArgumentNullException> (() => pieceBuffer.AddBlockFromPacket(null, BitTorrentLibrary.Constants.BlockSize)); }
public void TestPassNullForTorrentContextToGetListOfPeers() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Selector selector = new Selector(); Assert.Throws <ArgumentNullException>(() => { Peer[] peers = selector.GetListOfPeers(null, 0, 10); }); }
public void TestSetPieceOnRemotePeerWithInvalidPieceNumber() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Peer peer = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)); Assert.Throws <IndexOutOfRangeException>(() => peer.SetPieceOnRemotePeer(1000)); }
public void TestPassNullAsPeerToLocalPieceSuggessions() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Selector selector = new Selector(); Assert.Throws <ArgumentNullException>(() => selector.LocalPieceSuggestions(null, 10)); }
public void TestNoPeersHavePieceOnCallToNextPiece() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); UInt32 nextPiece = 0; Selector selector = new Selector(); Assert.False(selector.NextPiece(tc, ref nextPiece)); }
public void TestEmptyPeerSwarmCallGetListOfPeers() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Selector selector = new Selector(); Peer[] peers = selector.GetListOfPeers(tc, 0, 10); Assert.Empty(peers); }
public void TestCheckPropertiesSucessfullyCreatePeer() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Peer peer = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)); Assert.Equal("127.0.0.1", peer.Ip); Assert.Equal(6881, peer.Port); Assert.Equal(22, peer.NumberOfMissingPieces); }
public void TestNullPassedAsTorrentContext() { Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Assert.Throws <ArgumentNullException>(() => { Tracker tracker = new Tracker(null); }); }
public void TestGetInfoHashOentCheckInfoHash(string file, string expected) { MetaInfoFile torrentFile = new MetaInfoFile(file); torrentFile.Parse(); byte[] infoHash = torrentFile.GetInfoHash(); StringBuilder actual = new StringBuilder(infoHash.Length); foreach (byte b in infoHash) { actual.AppendFormat("{0:x2}", b); } Assert.Equal(expected, actual.ToString()); }
public void TestAddTorrentThatNotDoesHaveTracker() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); BitTorrentException error = Assert.Throws <BitTorrentException>(() => agent.AddTorrent(tc)); Assert.Equal("BitTorrent Error: Failed to add torrent context.Torrent does not have a tracker associated with it.", error.Message); }
public void TestSucessfullyCreatePeer() { try { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Peer peer = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)); } catch (Exception ex) { Assert.True(false, "Should not throw execption here but it did. " + ex.Message); } Assert.True(true); }
public void TestStartAddedTorrentWhenAgentHasntBeenStarted() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); _ = new Tracker(tc); agent.AddTorrent(tc); BitTorrentException error = Assert.Throws <BitTorrentException>(() => agent.StartTorrent(tc)); Assert.Equal("BitTorrent Error: Failure to start torrent context.Agent has not been started.", error.Message); }
public void TestGetTorrentDetailsOfTorrentNotAddedToAgent() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc); BitTorrentException error = Assert.Throws <BitTorrentException>(() => { TorrentDetails details = agent.GetTorrentDetails(tc); }); Assert.Equal("BitTorrent Error: Failure to get torrent details.Torrent hasnt been added to agent.", error.Message); }
public void TestCloseTorrentNotAdded() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc); BitTorrentException error = Assert.Throws <BitTorrentException>(() => agent.CloseTorrent(tc)); Assert.Equal("BitTorrent Error: Failure to close torrent context.Torrent hasnt been added to agent or may already have been closed.", error.Message); }
public void TestRemoveTorrentThatHasNotBeenAddedToAgent() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc); BitTorrentException error = Assert.Throws <BitTorrentException>(() => agent.RemoveTorrent(tc)); Assert.Equal("BitTorrent Error: Failed to remove torrent context.It probably has been removed alrady or never added.", error.Message); }
/// <summary> /// Initiate torrent download. /// </summary> /// <param name="mainWindow"></param> public void Download(TorrentClient main) { try { // Update status bar for starting download Application.MainLoop.Invoke(() => { main.MainStatusBar.Display(Status.StartingUp); }); // Load torrent file and parse MetaInfoFile torrentFile = new MetaInfoFile(_fileName); torrentFile.Parse(); Application.MainLoop.Invoke(() => { main.ClientWindow.UpdatProgressBar(0); main.ClientWindow.InfoWindow.SetTracker(torrentFile.GetTracker()); }); // Create torrent context and tracker _tc = new TorrentContext(torrentFile, _selector, _diskIO, main.Configuration.DestinationDirectory) { CallBack = UpdateDownloadProgress, CallBackData = main }; _tracker = new Tracker(_tc) { CallBack = UpdateDownloadInformation, CallBackData = main }; // Hookup tracker to agent, add torrent and startup everyhing up _agent.AddTorrent(_tc); _agent.AttachPeerSwarmQueue(_tracker); _tracker.StartAnnouncing(); _agent.StartTorrent(_tc); Application.MainLoop.Invoke(() => { main.MainStatusBar.Display(Status.Downloading); }); } catch (Exception ex) { Application.MainLoop.Invoke(() => { MessageBox.Query("Error", ex.Message, "Ok"); main.MainStatusBar.Display(Status.Shutdown); }); } }
public void TestNoMissingPiecesOnCallToNextPiece() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); UInt32 nextPiece = 0; Selector selector = new Selector(); for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++) { tc.IncrementPeerCount(pieceNumber); tc.MarkPieceMissing(pieceNumber, false); } Assert.False(selector.NextPiece(tc, ref nextPiece)); }
public void TestAtLeastOnePeersHasPieceOnCallToNextPiece() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); UInt32 nextPiece = 0; Selector selector = new Selector(); for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++) { tc.IncrementPeerCount(pieceNumber); } Assert.True(selector.NextPiece(tc, ref nextPiece)); Assert.True((nextPiece >= 0 && (nextPiece < tc.numberOfPieces))); }
public void TestStartAddedTorrent() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); _ = new Tracker(tc); agent.AddTorrent(tc); agent.StartTorrent(tc); Assert.True(tc.Status == TorrentStatus.Downloading || tc.Status == TorrentStatus.Seeding); }
public void TestAskForZeroPiecesFromLocalPeerSuggestions() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Selector selector = new Selector(); Peer peer = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)); for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++) { tc.IncrementPeerCount(pieceNumber); tc.MarkPieceLocal(pieceNumber, true); } UInt32[] pieces = selector.LocalPieceSuggestions(peer, 0); Assert.Empty(pieces); }
public void TestPauseTorrentNotStarted() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc); agent.AddTorrent(tc); BitTorrentException error = Assert.Throws <BitTorrentException>(() => agent.PauseTorrent(tc)); Assert.Equal("BitTorrent Error: Failure to pause torrent context.The torrent is currently not in a running state.", error.Message); }
public void TestStartAnnouncingWhenNoPeerSwarmQueueHasBeenAttached() { MockAnnouncerFactory mockAnnoucerFactory = new MockAnnouncerFactory(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc, mockAnnoucerFactory); agent.AddTorrent(tc);; BitTorrentException error = Assert.Throws <BitTorrentException>(() => tracker.StartAnnouncing()); Assert.Equal("BitTorrent Error: Peer swarm queue has not been set.", error.Message); }
public void TestReturnCorrectLastMissingPieceOnCallToNextPiece(UInt32 expected) { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); UInt32 actual = 0; Selector selector = new Selector(); for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++) { tc.IncrementPeerCount(pieceNumber); tc.MarkPieceMissing(pieceNumber, false); } tc.MarkPieceMissing(expected, true); Assert.True(selector.NextPiece(tc, ref actual)); Assert.Equal(expected, actual); }
public void TestStopAnnouncingOnOneThatHasNotBeenStarted() { MockAnnouncerFactory mockAnnoucerFactory = new MockAnnouncerFactory(); Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>(); MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object); agent.Startup(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Tracker tracker = new Tracker(tc, mockAnnoucerFactory); agent.AddTorrent(tc); agent.AttachPeerSwarmQueue(tracker); BitTorrentException error = Assert.Throws <BitTorrentException>(() => tracker.StopAnnouncing()); Assert.Equal("BitTorrent Error: Tracker is not running so cannot be stopped.", error.Message); }
public void TestOnePeerInSwarmCallGetListOfPeers() { MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent); file.Parse(); Manager manager = new Manager(); TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory); Selector selector = new Selector(); Peer peer = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)); tc.peerSwarm.TryAdd(peer.Ip, peer); // Peers eligible must be connected, unchoked and have piece peer.PeerChoking.Set(); peer.Connected = true; peer.SetPieceOnRemotePeer(0); Peer[] peers = selector.GetListOfPeers(tc, 0, 10); Assert.Equal(1, (int)peers.Length); peer.Connected = false; // Make un-eligible then try again peer.SetPieceOnRemotePeer(0); peers = selector.GetListOfPeers(tc, 0, 10); Assert.Empty(peers); }