Example #1
0
        public async Task AddPeers_Dht_Private()
        {
            // You can't manually add peers to private torrents
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            var editor = new TorrentEditor(rig.TorrentDict)
            {
                CanEditSecureMetadata = true,
                Private = true
            };

            var manager = await rig.Engine.AddAsync(editor.ToTorrent(), "path", new TorrentSettings());

            var dht = (ManualDhtEngine)rig.Engine.DhtEngine;

            var tcs = new TaskCompletionSource <DhtPeersAdded> ();

            manager.PeersFound += (o, e) => {
                if (e is DhtPeersAdded args)
                {
                    tcs.TrySetResult(args);
                }
            };

            var peer = rig.CreatePeer(false).Peer;

            dht.RaisePeersFound(manager.InfoHash, new[] { new PeerInfo(peer.ConnectionUri, peer.PeerId.AsMemory()) });
            var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(5));

            Assert.AreEqual(0, result.NewPeers, "#2");
            Assert.AreEqual(0, result.ExistingPeers, "#3");
            Assert.AreEqual(0, manager.Peers.AvailablePeers.Count, "#4");
        }
Example #2
0
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();

            bitfield = new BitField(40).SetAll(true)
                       .Set(4, false)
                       .Set(6, false)
                       .Set(24, false)
                       .Set(36, false);
            picker = new EndGamePicker();
            pieces = new List <Piece>(new Piece[] {
                new Piece(4, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(6, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(24, rig.Torrent.PieceLength, rig.Torrent.Size),
                new Piece(36, rig.Torrent.PieceLength, rig.Torrent.Size)
            });

            id           = new PeerId(new Peer("peerid", new Uri("tcp://weburl.com")), rig.Manager);
            id.IsChoking = false;
            id.BitField.SetAll(false);

            other           = new PeerId(new Peer("other", new Uri("tcp://other.com")), rig.Manager);
            other.IsChoking = false;
            other.BitField.SetAll(false);
        }
Example #3
0
        public void ReregisterManager()
        {
            ManualResetEvent handle = new ManualResetEvent(false);

            rig.Manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs e)
            {
                if (e.OldState == TorrentState.Hashing)
                {
                    handle.Set();
                }
            };
            rig.Manager.HashCheck(false);

            handle.WaitOne();
            handle.Reset();

            rig.Engine.Unregister(rig.Manager);
            TestRig rig2 = TestRig.CreateMultiFile(new TestWriter());

            rig2.Engine.Unregister(rig2.Manager);
            rig.Engine.Register(rig2.Manager);
            rig2.Manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs e)
            {
                if (e.OldState == TorrentState.Hashing)
                {
                    handle.Set();
                }
            };
            rig2.Manager.HashCheck(true);
            handle.WaitOne();
            rig2.Dispose();
        }
Example #4
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;
            int i;

            for (i = 0; i < 1000; i++)
            {
                try
                {
                    listener = new HttpListener();
                    listener.Prefixes.Add(string.Format(listenerURL, i));
                    listener.Start();
                    break;
                }
                catch
                {
                }
            }
            listener.BeginGetContext(GotContext, null);
            rig                = TestRig.CreateMultiFile();
            connection         = new HttpConnection(new Uri(string.Format(listenerURL, i)));
            connection.Manager = rig.Manager;

            id              = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager);
            id.Connection   = connection;
            id.IsChoking    = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = rig.Manager.PieceManager.Picker.PickPiece(id, new List <PeerId>(), numberOfPieces);
        }
Example #5
0
        public void Setup()
        {
            for (int i = 0; i < 10; i++)
            {
                try {
                    listener    = new HttpListener();
                    ListenerURL = $"http://127.0.0.1:{new Random ().Next (10000, 50000)}/announce/";
                    listener.Prefixes.Add(ListenerURL);
                    listener.Start();
                    break;
                } catch {
                }
            }
            listener.BeginGetContext(GotContext, listener);

            requestedUrl.Clear();
            partialData = false;

            rig = TestRig.CreateMultiFile();

            connection = new HttpPeerConnection(rig.Manager, rig.Engine.Factories, new Uri(ListenerURL));
            rig.Manager.UnhashedPieces.SetAll(false);

            id                    = new PeerId(new Peer("this is my id", connection.Uri), connection, new MutableBitField(rig.Manager.PieceCount()).SetAll(true));
            id.IsChoking          = false;
            id.AmInterested       = true;
            id.MaxPendingRequests = numberOfPieces;
            id.MessageQueue.SetReady();

            rig.Manager.PieceManager.AddPieceRequests(id);
            requests = (RequestBundle)id.MessageQueue.TryDequeue();
        }
Example #6
0
 public void Setup()
 {
     pair = new ConnectionPair(55432);
     rig  = TestRig.CreateMultiFile();
     rig.Manager.HashChecked = true;
     rig.Manager.Start();
 }
Example #7
0
        public async Task AddPeers_LocalPeerDiscovery_Private()
        {
            // You can't manually add peers to private torrents
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            var editor = new TorrentEditor(rig.TorrentDict)
            {
                CanEditSecureMetadata = true,
                Private = true
            };

            var manager = await rig.Engine.AddAsync(editor.ToTorrent(), "path", new TorrentSettings());

            var localPeer = (ManualLocalPeerListener)rig.Engine.LocalPeerDiscovery;

            var tcs = new TaskCompletionSource <LocalPeersAdded> ();

            manager.PeersFound += (o, e) => {
                if (e is LocalPeersAdded args)
                {
                    tcs.TrySetResult(args);
                }
            };

            localPeer.RaisePeerFound(manager.InfoHash, rig.CreatePeer(false).Uri);
            var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(5));

            Assert.AreEqual(0, result.NewPeers, "#2");
            Assert.AreEqual(0, result.ExistingPeers, "#3");
            Assert.AreEqual(0, manager.Peers.AvailablePeers.Count, "#4");
        }
        public void Setup()
        {
            DhtEngineFactory.Creator = listener => new ManualDhtEngine();

            rig  = TestRig.CreateMultiFile(new TestWriter());
            conn = new ConnectionPair().WithTimeout();
        }
Example #9
0
 public void FixtureSetup()
 {
     rig = TestRig.CreateMultiFile();
     id  = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < id.BitField.Length; i += 2)
     {
         id.BitField[i] = true;
     }
 }
        public void Setup()
        {
            rig         = TestRig.CreateMultiFile();
            diskManager = rig.Engine.DiskManager;

            writer             = new ExceptionWriter();
            diskManager.Writer = writer;
            handle             = new ManualResetEvent(false);
            rig.Manager.Stop();
        }
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();

            rig.Manager.UploadingTo          = 0;
            rig.Manager.Settings.UploadSlots = 4;
            peer     = rig.CreatePeer(true);
            unchoker = new InitialSeedUnchoker(rig.Manager);
            unchoker.PeerConnected(peer);
        }
Example #12
0
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();

            buffer = new byte[100000];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0xff;
            }
        }
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();

            conn = new ConnectionPair(13253);
            conn.Incoming.Name = "Incoming";
            conn.Outgoing.Name = "Outgoing";
            rig.Engine.Settings.AllowedEncryption = EncryptionTypes.All;
            rig.Manager.HashChecked = true;
        }
Example #14
0
        public async Task Setup()
        {
            pair = new ConnectionPair(55432);

            Cancellation = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.FromHours(1) : TimeSpan.FromSeconds(5));
            Cancellation.Token.Register(pair.Dispose);

            rig = TestRig.CreateMultiFile();
            rig.Manager.HashChecked = true;
            await rig.Manager.StartAsync();
        }
Example #15
0
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();
            id  = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
            for (int i = 0; i < id.BitField.Length; i += 2)
            {
                id.BitField[i] = true;
            }

            tester = new TestPicker();
            picker = new RandomisedPicker(tester);
        }
 public void Setup()
 {
     rig = TestRig.CreateMultiFile();
     id  = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     id.BitField.SetAll(true);
     tester = new TestPicker();
     picker = new PriorityPicker(tester);
     picker.Initialise(rig.Manager.Bitfield, rig.Torrent.Files, new List <Piece>());
     foreach (TorrentFile file in rig.Torrent.Files)
     {
         file.Priority = Priority.Normal;
     }
 }
Example #17
0
        public async Task StopTest()
        {
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            var hashingState = rig.Manager.WaitForState(TorrentState.Hashing);
            var stoppedState = rig.Manager.WaitForState(TorrentState.Stopped);

            await rig.Manager.StartAsync();

            Assert.IsTrue(hashingState.Wait(5000), "Started");
            await rig.Manager.StopAsync();

            Assert.IsTrue(stoppedState.Wait(5000), "Stopped");
        }
Example #18
0
        public async Task ContainingDirectory_PathBusting()
        {
            // You can't manually add peers to private torrents
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            await rig.Engine.RemoveAsync(rig.Engine.Torrents[0]);

            var editor = new TorrentEditor(rig.TorrentDict);

            editor.CanEditSecureMetadata = true;
            editor.Name = $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}test{Path.GetInvalidPathChars ()[0]}";

            Assert.ThrowsAsync <ArgumentException> (() => rig.Engine.AddAsync(editor.ToTorrent(), "path", new TorrentSettings()));
        }
Example #19
0
 public virtual void Setup()
 {
     // Yes, this is horrible. Deal with it.
     rig    = TestRig.CreateMultiFile();
     peers  = new List <PeerId>();
     picker = new IgnoringPicker(rig.Manager.Bitfield, new StandardPicker());
     picker.Initialise(rig.Manager.Bitfield, rig.Manager.Torrent.Files, new List <Piece>());
     peer = new PeerId(new Peer(new string('a', 20), new Uri("tcp://BLAH")), rig.Manager);
     for (int i = 0; i < 20; i++)
     {
         PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("tcp://" + i)), rig.Manager);
         p.SupportsFastPeer = true;
         peers.Add(p);
     }
 }
Example #20
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;

            rig                = TestRig.CreateMultiFile();
            connection         = new HttpConnection(new Uri(ListenerURL));
            connection.Manager = rig.Manager;

            id              = new PeerId(new Peer("this is my id", connection.Uri), rig.Manager, connection);
            id.IsChoking    = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            requests = new MessageBundle(rig.Manager.PieceManager.Picker.PickPiece(id, new List <PeerId>(), numberOfPieces));
        }
Example #21
0
        public async Task ContainingDirectory_InvalidCharacters()
        {
            // You can't manually add peers to private torrents
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            await rig.Engine.RemoveAsync(rig.Engine.Torrents[0]);

            var editor = new TorrentEditor(rig.TorrentDict);

            editor.CanEditSecureMetadata = true;
            editor.Name = $"{Path.GetInvalidPathChars()[0]}test{Path.GetInvalidPathChars ()[0]}";

            var manager = await rig.Engine.AddAsync(editor.ToTorrent(), "path", new TorrentSettings());

            Assert.IsFalse(manager.ContainingDirectory.Contains(manager.Torrent.Name));
            Assert.IsTrue(manager.ContainingDirectory.StartsWith(manager.SavePath));
            Assert.AreEqual(Path.GetFullPath(manager.ContainingDirectory), manager.ContainingDirectory);
            Assert.AreEqual(Path.GetFullPath(manager.SavePath), manager.SavePath);
        }
Example #22
0
        public virtual void Setup()
        {
            // Yes, this is horrible. Deal with it.
            rig   = TestRig.CreateMultiFile();
            peers = new List <PeerId>();

            manager = new PieceManager();
            manager.ChangePicker((standardPicker = new StandardPicker()), rig.Manager.Bitfield, rig.Manager.Torrent.Files);
            this.picker = manager.Picker;

            peer = new PeerId(new Peer(new string('a', 20), new Uri("ipv4://BLAH")), rig.Manager);
            for (int i = 0; i < 20; i++)
            {
                PeerId p = new PeerId(new Peer(new string(i.ToString()[0], 20), new Uri("ipv4://" + i)), rig.Manager);
                p.SupportsFastPeer = true;
                peers.Add(p);
            }
        }
Example #23
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;

            rig = TestRig.CreateMultiFile();

            connection         = new HttpConnection(new Uri(ListenerURL));
            connection.Manager = rig.Manager;
            rig.Manager.UnhashedPieces.SetAll(false);

            id                    = new PeerId(new Peer("this is my id", connection.Uri), connection, new MutableBitField(rig.Manager.PieceCount()).SetAll(true));
            id.IsChoking          = false;
            id.AmInterested       = true;
            id.MaxPendingRequests = numberOfPieces;
            id.MessageQueue.SetReady();

            rig.Manager.PieceManager.AddPieceRequests(id);
            requests = (RequestBundle)id.MessageQueue.TryDequeue();
        }
Example #24
0
        public void Setup()
        {
            requestedUrl.Clear();
            partialData = false;

            rig = TestRig.CreateMultiFile();
            rig.Manager.ChangePicker(new StandardPicker());

            connection         = new HttpConnection(new Uri(ListenerURL));
            connection.Manager = rig.Manager;
            rig.Manager.UnhashedPieces.SetAll(false);

            id              = new PeerId(new Peer("this is my id", connection.Uri), connection, rig.Manager.Bitfield?.Clone().SetAll(false));
            id.IsChoking    = false;
            id.AmInterested = true;
            id.BitField.SetAll(true);
            id.MaxPendingRequests = numberOfPieces;

            rig.Manager.PieceManager.AddPieceRequests(id);
            requests = (RequestBundle)id.Dequeue();
        }
Example #25
0
        public void Setup()
        {
            rig = TestRig.CreateMultiFile();

            tester = new TestPicker();
            rarest = new RarestFirstPicker(tester);
            rarest.Initialise(rig.Manager.Bitfield, rig.Torrent.Files, new List <Piece>());
            peers = new List <PeerId>();
            for (int i = 0; i < 5; i++)
            {
                peers.Add(new PeerId(new Peer(new string((char)(i + 'a'), 20), new Uri("tcp://aaa")), rig.Manager));
            }

            for (int i = 0; i < rig.Manager.Bitfield.Length; i++)
            {
                for (int j = 0; j < peers.Count; j++)
                {
                    peers[j].BitField[i] = i % (j + 1) == 0;
                }
            }
            peers[0].BitField.SetAll(true);
        }
Example #26
0
        public async Task AddPeers_LocalPeerDiscovery()
        {
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            var localPeer = (ManualLocalPeerListener)rig.Engine.LocalPeerDiscovery;

            var tcs     = new TaskCompletionSource <LocalPeersAdded> ();
            var manager = rig.Engine.Torrents[0];

            manager.PeersFound += (o, e) => {
                if (e is LocalPeersAdded args)
                {
                    tcs.TrySetResult(args);
                }
            };

            localPeer.RaisePeerFound(manager.InfoHash, rig.CreatePeer(false).Uri);
            var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(5));

            Assert.AreEqual(1, result.NewPeers, "#2");
            Assert.AreEqual(0, result.ExistingPeers, "#3");
            Assert.AreEqual(1, manager.Peers.AvailablePeers.Count, "#4");
        }
Example #27
0
        public async Task ReregisterManager()
        {
            var hashingTask = rig.Manager.WaitForState(TorrentState.Stopped);
            await rig.Manager.HashCheckAsync(false);

            await hashingTask;

            await rig.Engine.Unregister(rig.Manager);

            TestRig rig2 = TestRig.CreateMultiFile(new TestWriter());
            await rig2.Engine.Unregister(rig2.Manager);

            await rig.Engine.Register(rig2.Manager);

            hashingTask = rig2.Manager.WaitForState(TorrentState.Downloading);
            await rig2.Manager.HashCheckAsync(true);

            await hashingTask;
            await rig2.Manager.StopAsync();

            rig2.Dispose();
        }
Example #28
0
        public async Task AddPeers_Dht()
        {
            using var rig = TestRig.CreateMultiFile(new TestWriter());
            var dht = (ManualDhtEngine)rig.Engine.DhtEngine;

            var tcs     = new TaskCompletionSource <DhtPeersAdded> ();
            var manager = rig.Engine.Torrents[0];

            manager.PeersFound += (o, e) => {
                if (e is DhtPeersAdded args)
                {
                    tcs.TrySetResult(args);
                }
            };

            var peer = rig.CreatePeer(false).Peer;

            dht.RaisePeersFound(manager.InfoHash, new[] { new PeerInfo(peer.ConnectionUri, peer.PeerId.AsMemory()) });
            var result = await tcs.Task.WithTimeout(TimeSpan.FromSeconds(5));

            Assert.AreEqual(1, result.NewPeers, "#2");
            Assert.AreEqual(0, result.ExistingPeers, "#3");
            Assert.AreEqual(1, manager.Peers.AvailablePeers.Count, "#4");
        }
Example #29
0
 public void FixtureSetup()
 {
     rig         = TestRig.CreateMultiFile();
     diskManager = rig.Engine.DiskManager;
 }
Example #30
0
 public void Setup()
 {
     rig  = TestRig.CreateMultiFile(new TestWriter());
     conn = new ConnectionPair(51515);
 }