Пример #1
0
        public void TestNullPassedForDiskIOInCreationOfTorrentContext()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Assert.Throws <ArgumentNullException>(() => { TorrentContext tc = new TorrentContext(file, new Selector(), null, Constants.DestinationDirectory); });
        }
Пример #2
0
        public void TestGetTorrentDetailsReturnsCorrectDetails()
        {
            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);
            TorrentDetails details = agent.GetTorrentDetails(tc);

            Assert.Equal(0, details.deadPeers);
            Assert.Equal(0, (int)details.downloadedBytes);
            Assert.Equal(Constants.SingleFileTorrent, details.fileName);
            Assert.Equal(file.GetInfoHash(), details.infoHash);
            Assert.Equal(22, details.missingPiecesCount);
            Assert.Equal(0, (int)details.peers.Count);
            Assert.Equal(TorrentStatus.Initialised, details.status);
            Assert.Equal(0, details.swarmSize);
            Assert.Equal(TrackerStatus.Stopped, details.trackerStatus);
            Assert.Null(details.trackerStatusMessage);
            Assert.Equal(0, (int)details.uploadedBytes);
        }
Пример #3
0
        public void TestAllUniquePiecesChosenByNextPiece()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager          manager   = new Manager();
            TorrentContext   tc        = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            HashSet <UInt32> pieces    = new HashSet <UInt32>();
            Selector         selector  = new Selector();
            UInt32           nextPiece = 0;

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
            }
            while (selector.NextPiece(tc, ref nextPiece))
            {
                if ((nextPiece >= 0) && (nextPiece < tc.numberOfPieces))
                {
                    pieces.Add(nextPiece);
                    tc.MarkPieceMissing(nextPiece, false);
                }
            }
            Assert.False(selector.NextPiece(tc, ref nextPiece));
            Assert.Equal(tc.numberOfPieces, pieces.Count);
        }
Пример #4
0
        public void TestSmptyStringPassedForDestinationDirectoryInCreationOfTorrentContext()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Assert.Throws <ArgumentException>(() => { TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), ""); });
        }
Пример #5
0
        public void TestDataReadFromTorrentFile()
        {
            MetaInfoFile         file        = new MetaInfoFile(Constants.SingleFileTorrent);
            Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>();

            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);

            Assert.Equal(1, tracker.Compact);
            Assert.Equal(TrackerEvent.None, tracker.Event);
            Assert.Equal(2000, tracker.Interval);
            Assert.Equal(Host.GetIP(), tracker.Ip);
            Assert.Equal(351874, (int)tracker.Left);
            Assert.Equal(5, tracker.NumWanted);
            Assert.Equal("-AZ1000-BMt9tgTUwEiH", tracker.PeerID);
            Assert.Equal(6881, tracker.Port);
            Assert.Equal("http://192.168.1.215:9005/announce", tracker.TrackerURL);
            Assert.Equal(0, (int)tracker.Uploaded);
            byte[]        infoHash = tracker.InfoHash;
            StringBuilder actual   = new StringBuilder(infoHash.Length);

            foreach (byte b in infoHash)
            {
                actual.AppendFormat("{0:x2}", b);
            }
            Assert.Equal("7fd1a2631b385a4cc68bf15040fa375c8e68cb7e", actual.ToString());
        }
Пример #6
0
        public void TestSetSeedingIntervalWhenSeeding()
        {
            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, true);
            Tracker        tracker = new Tracker(tc, mockAnnoucerFactory);

            agent.AddTorrent(tc);
            agent.AttachPeerSwarmQueue(tracker);
            tracker.StartAnnouncing();
            agent.StartTorrent(tc);
            try
            {
                tracker.SetSeedingInterval(30);
            }
            catch (Exception ex)
            {
                Assert.True(false, "Should not throw execption here but it did. " + ex.Message);
            }
            Assert.True(true);
        }
Пример #7
0
        public void TestIsPieceMissingWithInvalidPieceNumber()
        {
            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.IsPieceMissing(Constants.InvalidPieceNumber));
        }
Пример #8
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(""));
        }
Пример #9
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);
        }
Пример #10
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));
        }
Пример #11
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));
        }
Пример #12
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); });
        }
Пример #13
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));
        }
Пример #14
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));
        }
Пример #15
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);
        }
Пример #16
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);
        }
Пример #17
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); });
        }
Пример #18
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);
        }
Пример #19
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);
        }
Пример #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
        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);
        }
Пример #22
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);
 }
Пример #23
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);
        }
Пример #24
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);
         });
     }
 }
Пример #25
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));
        }
Пример #26
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)));
        }
Пример #27
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);
        }
Пример #28
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);
        }
Пример #29
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);
        }
Пример #30
0
        /// <summary>
        /// Update torrent download progress bar (this is the torrent context progress callback).
        /// On completion of download copy torrent file to seeding directory and clear the main
        /// download information screen to allow a new torrent to be downloaded.
        /// </summary>
        /// <param name="obj"></param>
        private void UpdateDownloadProgress(Object obj)
        {
            TorrentClient main = (TorrentClient)obj;

            Application.MainLoop.Invoke(() =>
            {
                main.ClientWindow.UpdatProgressBar((float)((double)_tc.TotalBytesDownloaded / (double)_tc.TotalBytesToDownload));
            });
            if (_tc.TotalBytesToDownload - _tc.TotalBytesDownloaded == 0)
            {
                _tracker.CallBack     = null;
                _tracker.CallBackData = null;
                _tracker         = null;
                _tc.CallBack     = null;
                _tc.CallBackData = null;
                _tc = null;
                main.ResetWindowAndCopySeedingFile();
            }
        }