コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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(""));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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); });
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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));
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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); });
        }
コード例 #14
0
        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());
        }
コード例 #15
0
        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);
        }
コード例 #16
0
 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);
 }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
 /// <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);
         });
     }
 }
コード例 #22
0
        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));
        }
コード例 #23
0
        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)));
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        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);
        }