Esempio n. 1
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);
        }
        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);
        }
        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);
        }
Esempio n. 4
0
        public void TestStartupAlreadyStartedAgent()
        {
            Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>();
            Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object);

            agent.Startup();
            BitTorrentException error = Assert.Throws <BitTorrentException>(() => { agent.Startup(); }); Assert.Equal("BitTorrent Error: Failure to startup agent.Agent is already running.", error.Message);
        }
Esempio n. 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);
        }
Esempio n. 6
0
        public void TestShutdownAlreadyShutdownAgent()
        {
            Mock <IAgentNetwork> networkMock = new Mock <IAgentNetwork>();
            Agent agent = new Agent(new Manager(), new Assembler(), networkMock.Object);

            agent.Startup();
            agent.Shutdown();
            BitTorrentException error = Assert.Throws <BitTorrentException>(() => { agent.Shutdown(); });

            Assert.Equal("BitTorrent Error: Failed to shutdown agent.Agent already shutdown.", error.Message);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        public void TestSetSeedingIntervalWhenNotSeeding()
        {
            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);
            tracker.StartAnnouncing();
            BitTorrentException error = Assert.Throws <BitTorrentException>(() => tracker.SetSeedingInterval(30));

            Assert.Equal("BitTorrent Error: Cannot change interval as torrent is not seeding.", error.Message);
        }
Esempio n. 16
0
        /// <summary>
        /// Announce the tracker server
        /// </summary>
        /// <returns>Return the response of announce information</returns>
        public async void Announce()
        {
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Uri);
                try
                {
                    BEncodedNode node;
                    using (WebResponse webResponse = await httpRequest.GetResponseAsync())
                    {
                        Stream stream = webResponse.GetResponseStream();
                        Debug.Assert(stream != null);

                        int count = Setting.TrackerBufferLength;

                        byte[] rcvBuf = new byte[Setting.TrackerBufferLength];
                        using (MemoryStream ms = new MemoryStream(Setting.TrackerBufferLength))
                        {
                            int readLength;
                            do
                            {
                                readLength = await stream.ReadAsync(rcvBuf, 0, count);

                                ms.Write(rcvBuf, 0, readLength);
                            } while (readLength != 0);
                            node = BEncodingFactory.Decode(ms.ToArray());
                        }
                    }

                    DictNode responseNode = node as DictNode;
                    if (responseNode != null)
                    {
                        AnnounceResponse response = Parse(responseNode);
                        if (response != null)
                        {
                            _timer.Interval = response.Interval * 1000;
                            GotAnnounceResponse(this, response);
                        }
                        else
                        {
                            _timer.Interval = Setting.TrackerFailInterval;
                            BitTorrentException exception = new BitTorrentException("Tracker returns fail message.");
                            ReturnMessageFail(this, exception);
                        }
                    }
                    else
                    {
                        _timer.Interval = Setting.TrackerFailInterval;
                        BitTorrentException exception = new BitTorrentException("Tracker returns fail message.");
                        ReturnMessageFail(this, exception);
                    }
                }
                catch (WebException e)
                {
                    _timer.Interval = Setting.TrackerFailInterval;
                    Debug.Assert(ConnectFail != null);
                    ConnectFail(this, e);
                }
                finally
                {
                    _timer.Start();
                }
            }
            catch (NullReferenceException)
            {
                //Nothing to be done.
            }
        }