Example #1
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()), ""); });
        }
        public void TestGetPiecesInfoHashWithNoParseOnMetaInfoFile()
        {
            MetaInfoFile        torrentFile = new MetaInfoFile(Constants.SingleFileTorrent);
            BitTorrentException error       = Assert.Throws <BitTorrentException>(() => { byte[] piecesInfoHash = torrentFile.GetPiecesInfoHash(); });

            Assert.Equal("BitTorrent Error: File has not been parsed.", error.Message);
        }
Example #3
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); });
        }
Example #4
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);
        }
        public void TestMultiFileTorrentContainsValidKey(string key)
        {
            MetaInfoFile torrentFile = new MetaInfoFile(Constants.MultiFileTorrent);

            torrentFile.Parse();
            Assert.True(torrentFile.metaInfoDict.ContainsKey(key));
        }
        public void TestGetPieceLengthOnParsedFile(string file, string expected)
        {
            MetaInfoFile torrentFile = new MetaInfoFile(file);

            torrentFile.Parse();
            Assert.Equal(expected, torrentFile.GetPieceLength().ToString());
        }
        public void TestGetTrackerWithNoParseOnMetaInfoFile()
        {
            MetaInfoFile        torrentFile = new MetaInfoFile(Constants.SingleFileTorrent);
            BitTorrentException error       = Assert.Throws <BitTorrentException>(() => { string tracker = torrentFile.GetTracker(); });

            Assert.Equal("BitTorrent Error: File has not been parsed.", error.Message);
        }
Example #8
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);
        }
        public void TestGetURLOnParsedFile(string file, string expected)
        {
            MetaInfoFile torrentFile = new MetaInfoFile(file);

            torrentFile.Parse();
            Assert.Equal(expected, torrentFile.GetTracker());
        }
Example #10
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());
        }
Example #11
0
        public void TestUnparsedMetInfoFilePassInCreationOfTorrentContext()
        {
            MetaInfoFile        file  = new MetaInfoFile(Constants.SingleFileTorrent);
            BitTorrentException error = Assert.Throws <BitTorrentException>(() => { TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory); });

            Assert.Equal("BitTorrent Error: File has not been parsed.", error.Message);
        }
Example #12
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);
        }
Example #13
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(""));
        }
        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);
        }
Example #15
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));
        }
Example #16
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);
        }
        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));
        }
Example #18
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));
        }
Example #19
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); });
        }
Example #20
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));
        }
Example #21
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));
        }
Example #22
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);
        }
Example #23
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);
        }
        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());
        }
Example #25
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); });
        }
Example #26
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);
        }
Example #27
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);
        }
Example #28
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);
        }
Example #29
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);
        }
Example #30
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);
        }