public void Setup() { // Three pieces of length 32kb. SmallTorrent = new TestTorrentData { Files = new[] { new TorrentFile("foo", length: Piece.BlockSize * 2 * 3, startIndex: 0, endIndex: 2) }, PieceLength = Piece.BlockSize * 2, Size = Piece.BlockSize * 2 * 3, Bitfield = new BitField(3), Seeder = PeerId.CreateNull(3, seeder: true, isChoking: false, amInterested: true) }; // Three hundred pieces of length 4MB. LargeTorrent = new TestTorrentData { Files = new[] { new TorrentFile("foo", length: Piece.BlockSize * 2 * 300, startIndex: 0, endIndex: 299) }, PieceLength = Piece.BlockSize * 256, Size = Piece.BlockSize * 256 * 300, Bitfield = new BitField(300), Seeder = PeerId.CreateNull(300, seeder: true, isChoking: false, amInterested: true) }; Standard = new StandardPicker(); Endgame = new EndGamePicker(); Switcher = new EndGameSwitcher(Standard, Endgame, null); }
public void Setup() { checker = new PiecePickerFilterChecker(new StandardPicker()); picker = new RandomisedPicker(checker); seeder = PeerId.CreateNull(40, true, false, true); picker.Initialise(new TestTorrentData()); }
public void Setup() { int pieceCount = 40; int pieceLength = 256 * 1024; var torrentData = new TestTorrentData { Files = TorrentFileInfo.Create(pieceLength, ("File", pieceLength * pieceCount, "full/path/File")), PieceLength = pieceLength, Size = pieceLength * pieceCount }; peers = new List <PeerId> (); torrentManager = TestRig.CreateSingleFileManager(torrentData.Size, torrentData.PieceLength); torrentManager.LoadFastResume(new FastResume(torrentManager.InfoHash, new BitField(pieceCount).SetAll(true), new BitField(pieceCount).SetAll(false))); manager = new PieceManager(torrentManager); manager.Initialise(); peer = PeerId.CreateNull(pieceCount); for (int i = 0; i < 20; i++) { PeerId p = PeerId.CreateNull(pieceCount); p.SupportsFastPeer = true; peers.Add(p); } }
public void Setup() { int pieceLength = 16 * Piece.BlockSize; int pieces = 40; int size = pieces * pieceLength; bitfield = new BitField(pieces); torrentData = new TestTorrentData { Files = new [] { new TorrentFile("Test", size) }, PieceLength = pieceLength, Size = size }; checker = new TestPicker(); picker = new RarestFirstPicker(checker); picker.Initialise(bitfield, torrentData, new List <Piece>()); peer = PeerId.CreateNull(pieces); peer.BitField.SetAll(true); peers = new List <PeerId>(); for (int i = 0; i < 5; i++) { peers.Add(PeerId.CreateNull(pieces)); } }
public void Setup() { var pieceCount = 40; var pieceLength = 256 * 1024; torrentData = new TestTorrentData { Files = new[] { new TorrentFileInfo(new TorrentFile("One File", pieceLength * pieceCount, 0, pieceCount)) }, PieceLength = pieceLength, Size = pieceLength * pieceCount }; bitfield = new BitField(torrentData.PieceCount) .SetAll(true) .Set(4, false) .Set(6, false) .Set(24, false) .Set(36, false); picker = new EndGamePicker(); pieces = new List <Piece> (new[] { new Piece(4, torrentData.PieceLength, torrentData.Size), new Piece(6, torrentData.PieceLength, torrentData.Size), new Piece(24, torrentData.PieceLength, torrentData.Size), new Piece(36, torrentData.PieceLength, torrentData.Size) }); id = PeerId.CreateNull(torrentData.PieceCount); id.IsChoking = false; other = PeerId.CreateNull(torrentData.PieceCount); other.IsChoking = false; }
public void Setup() { int pieceCount = 40; int pieceLength = 256 * 1024; bitfield = new BitField(pieceCount); torrentData = new TestTorrentData { Files = new [] { new TorrentFile("File", pieceLength * pieceCount) }, PieceLength = pieceLength, Size = pieceLength * pieceCount }; peers = new List <PeerId>(); magnetLink = new MagnetLink(new InfoHash(new byte[20])); torrentManager = new TorrentManager(magnetLink, "", new TorrentSettings(), ""); manager = new PieceManager(torrentManager); manager.ChangePicker(new StandardPicker(), bitfield, torrentData); peer = PeerId.CreateNull(pieceCount); for (int i = 0; i < 20; i++) { PeerId p = PeerId.CreateNull(pieceCount); p.SupportsFastPeer = true; peers.Add(p); } }
public void Setup() { int pieceLength = 16 * Piece.BlockSize; int pieces = 40; int size = pieces * pieceLength; bitfield = new MutableBitField(pieces); torrentData = new TestTorrentData { Files = TorrentFileInfo.Create(pieceLength, ("Test", size, "Full/Path/Test")), PieceLength = pieceLength, Size = size }; checker = new PiecePickerFilterChecker(); picker = new RarestFirstPicker(checker); picker.Initialise(torrentData); peer = PeerId.CreateNull(pieces); peer.MutableBitField.SetAll(true); peers = new List <PeerId> (); for (int i = 0; i < 5; i++) { peers.Add(PeerId.CreateNull(pieces)); } }
public void DupeRequests_PickSameBlockTwiceWhenAllRequested() { var seeder1 = PeerId.CreateNull(bitfield.Length, true, false, true); var seeder2 = PeerId.CreateNull(bitfield.Length, true, false, true); var singlePiece = peers[0].BitField.SetAll(false).Set(3, true); Assert.IsNull(picker.ContinueAnyExistingRequest(seeder1, 0, bitfield.Length)); BlockInfo?req; var requests1 = new List <BlockInfo> (); while ((req = picker.PickPiece(seeder1, singlePiece)) != null) { requests1.Add(req.Value); } // There are no pieces owned by this peer, so there's nothing to continue. Assert.IsNull(picker.ContinueExistingRequest(seeder2, 0, bitfield.Length)); // Every block has been requested once and no duplicates are allowed. Assert.IsNull(picker.ContinueAnyExistingRequest(seeder2, 0, bitfield.Length)); // Every block has been requested once and no duplicates are allowed. Assert.IsNull(picker.ContinueAnyExistingRequest(seeder2, 0, bitfield.Length, 1)); var requests2 = new List <BlockInfo> (); while ((req = picker.ContinueAnyExistingRequest(seeder2, 0, bitfield.Length, 2)) != null) { requests2.Add(req.Value); } CollectionAssert.AreEquivalent(requests1, requests2); }
public void Setup() { int pieceCount = 40; int pieceLength = 256 * 1024; bitfield = new BitField(pieceCount); torrentData = new TestTorrentData { Files = new[] { new TorrentFileInfo(new TorrentFile("File", pieceLength * pieceCount)) }, PieceLength = pieceLength, Size = pieceLength * pieceCount }; peers = new List <PeerId> (); torrentManager = TestRig.CreateSingleFileManager(torrentData.Size, torrentData.PieceLength); torrentManager.LoadFastResume(new FastResume(torrentManager.InfoHash, new BitField(pieceCount).SetAll(true), new BitField(pieceCount).SetAll(false))); manager = new PieceManager(torrentManager); manager.ChangePicker(new StandardPicker(), bitfield); manager.Picker.Initialise(bitfield, torrentData, Enumerable.Empty <Piece> ()); peer = PeerId.CreateNull(pieceCount); for (int i = 0; i < 20; i++) { PeerId p = PeerId.CreateNull(pieceCount); p.SupportsFastPeer = true; peers.Add(p); } }
public async Task AddPeers_PeerExchangeMessage_Private() { var peer = new byte[] { 192, 168, 0, 1, 100, 0 }; var dotF = new byte[] { 1 << 0 | 1 << 2 }; // 0x1 means supports encryption, 0x2 means is a seeder var id = PeerId.CreateNull(40); id.SupportsFastPeer = true; id.SupportsLTMessages = true; var manager = TestRig.CreatePrivate(); manager.Mode = new DownloadMode(manager, DiskManager, ConnectionManager, Settings); var peersTask = new TaskCompletionSource <PeerExchangePeersAdded> (); manager.PeersFound += (o, e) => { if (e is PeerExchangePeersAdded args) { peersTask.TrySetResult(args); } }; var exchangeMessage = new PeerExchangeMessage(13, peer, dotF, null); manager.Mode.HandleMessage(id, exchangeMessage); var addedArgs = await peersTask.Task.WithTimeout(); Assert.AreEqual(0, addedArgs.NewPeers, "#1"); }
public void SmallTorrent_RequestAll_TriggerEndgame() { Switcher.Initialise(SmallTorrent.Bitfield, SmallTorrent, Enumerable.Empty <Piece> ()); // Pretend we have all the pieces except one. SmallTorrent.Bitfield.SetAll(true).Set(0, false); // When picking we should indicate there's 1 piece that we desire - the one we're missing. var onePieceLeft = SmallTorrent.Bitfield.Clone().Not(); // Only 2 blocks should be left to be requested, so add 1 request per person. var seeders = new[] { PeerId.CreateNull(SmallTorrent.Bitfield.Length, seeder: true, isChoking: false, amInterested: true), PeerId.CreateNull(SmallTorrent.Bitfield.Length, seeder: true, isChoking: false, amInterested: true), }; foreach (var peer in seeders) { Assert.IsNotNull(Switcher.PickPiece(peer, onePieceLeft, Array.Empty <IPieceRequester> ()), "#1"); Assert.AreEqual(1, peer.AmRequestingPiecesCount, "#2"); Assert.AreSame(Standard, Switcher.ActivePicker, "#3"); } // The next request *should* trigger endgame mode and give a valid request. Assert.IsNotNull(Switcher.PickPiece(SmallTorrent.Seeder, onePieceLeft, Array.Empty <IPieceRequester> ()), "#4"); Assert.AreSame(Endgame, Switcher.ActivePicker, "#5"); }
public void LargeTorrent_RequestAll_TriggerEndgame() { Switcher.Initialise(LargeTorrent.Bitfield, LargeTorrent, Enumerable.Empty <Piece> ()); // Pretend we have all the pieces except one. LargeTorrent.Bitfield.SetAll(true).Set(0, false); // When picking we should indicate there's 1 piece that we desire - the one we're missing. var onePieceLeft = LargeTorrent.Bitfield.Clone().Not(); // Only 2 blocks should be left to be requested, so add 1 request per person. var seeders = new List <PeerId> (); for (int i = 0; i < LargeTorrent.PieceLength / Piece.BlockSize; i++) { seeders.Add(PeerId.CreateNull(LargeTorrent.Bitfield.Length, seeder: true, isChoking: false, amInterested: true)); } // 256 blocks per piece, request 1 block per peer. foreach (var peer in seeders) { Assert.IsNotNull(Switcher.PickPiece(peer, onePieceLeft, Array.Empty <IPieceRequester> ()) ?? Switcher.ContinueAnyExisting(peer, 0, LargeTorrent.Bitfield.Length - 1), "#1"); Assert.AreEqual(1, peer.AmRequestingPiecesCount, "#2"); Assert.AreSame(Standard, Switcher.ActivePicker, "#3"); } // Trigger endgame mode, but don't pick a piece. Assert.IsInstanceOf <StandardPicker> (Switcher.ActivePicker); Assert.IsNull(Switcher.ContinueAnyExisting(LargeTorrent.Seeder, 0, LargeTorrent.Bitfield.Length - 1)); Assert.IsInstanceOf <EndGamePicker> (Switcher.ActivePicker); // The next request *should* get a valid request. Assert.IsNotNull(Switcher.PickPiece(LargeTorrent.Seeder, onePieceLeft, Array.Empty <IPieceRequester> ()), "#4"); Assert.AreSame(Endgame, Switcher.ActivePicker, "#5"); }
public void MismatchedInfoHash() { Manager.Mode = new DownloadMode(Manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(Manager.Bitfield.Length); var handshake = new HandshakeMessage(new InfoHash(Enumerable.Repeat((byte)15, 20).ToArray()), "peerid", VersionInfo.ProtocolStringV100); Assert.Throws <TorrentException> (() => Manager.Mode.HandleMessage(peer, handshake)); }
public void MismatchedProtocolString() { Manager.Mode = new DownloadMode(Manager, DiskManager, ConnectionManager, Settings); var peerId = PeerId.CreateNull(Manager.Bitfield.Length); var handshake = new HandshakeMessage(Manager.InfoHash, "peerid", "bleurgh"); Assert.Throws <ProtocolException> (() => Manager.Mode.HandleMessage(peerId, handshake)); }
public void Setup() { checker = new TestPicker(); picker = new RandomisedPicker(checker); pieceCount = 40; peer = PeerId.CreateNull(pieceCount); peer.BitField.SetAll(true); }
public async Task UnknownMetadataLength() { await Setup(true); ExtendedHandshakeMessage exHand = new ExtendedHandshakeMessage(false, null, 5555); exHand.Supports.Add(LTMetadata.Support); Assert.DoesNotThrow(() => rig.Manager.Mode.HandleMessage(PeerId.CreateNull(1), exHand)); }
public void MismatchedPeerId_PublicTorrent() { Manager.Mode = new DownloadMode(Manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(Manager.Bitfield.Length); var handshake = new HandshakeMessage(Manager.InfoHash, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), VersionInfo.ProtocolStringV100, false); Assert.DoesNotThrow(() => Manager.Mode.HandleMessage(peer, handshake)); Assert.AreEqual(peer.PeerID, handshake.PeerId); }
public void MismatchedPeerId_PrivateTorrent() { var manager = TestRig.CreatePrivate(); manager.Mode = new DownloadMode(manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(manager.Bitfield.Length); var handshake = new HandshakeMessage(manager.InfoHash, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), VersionInfo.ProtocolStringV100, false); Assert.Throws <TorrentException> (() => manager.Mode.HandleMessage(peer, handshake)); }
public void ReceivedPiecesAreNotRequested() { for (int i = 2; i < pieces[0].BlockCount; i++) { pieces[0].Blocks[i].CreateRequest(PeerId.CreateNull(torrentData.PieceCount)); pieces[0].Blocks[i].Received = true; } picker.Initialise(bitfield, torrentData, pieces); Assert.IsTrue(picker.Requests.All(t => !t.Block.Received), "#1"); }
public void EmptyPeerId_PublicTorrent() { Manager.Mode = new DownloadMode(Manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(Manager.Bitfield.Length); peer.Peer.PeerId = null; var handshake = new HandshakeMessage(Manager.InfoHashes.V1OrV2, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), Constants.ProtocolStringV100, false); Manager.Mode.HandleMessage(peer, handshake, default); Assert.AreEqual(handshake.PeerId, peer.PeerID); }
public void EmptyPeerId_PrivateTorrent() { var manager = TestRig.CreatePrivate(); manager.Mode = new DownloadMode(manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(manager.Bitfield.Length); peer.Peer.PeerId = null; var handshake = new HandshakeMessage(manager.InfoHash, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), VersionInfo.ProtocolStringV100, false); manager.Mode.HandleMessage(peer, handshake); Assert.AreEqual(handshake.PeerId, peer.PeerID); }
public async Task MismatchedPeerId_PrivateTorrent() { var torrent = TestRig.CreatePrivate(); using var engine = new ClientEngine(EngineSettingsBuilder.CreateForTests()); var manager = await engine.AddAsync(torrent, ""); manager.Mode = new DownloadMode(manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(manager.Bitfield.Length); var handshake = new HandshakeMessage(manager.InfoHash, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), VersionInfo.ProtocolStringV100, false); Assert.Throws <TorrentException> (() => manager.Mode.HandleMessage(peer, handshake)); }
public void Setup() { singleFile = CreateSingleFile(); singleBitfield = new BitField(singleFile.Files.Single().EndPieceIndex + 1).SetAll(true); singlePeer = PeerId.CreateNull(singleBitfield.Length); multiFile = CreateMultiFile(); multiBitfield = new BitField(multiFile.Files.Last().EndPieceIndex + 1).SetAll(true); multiPeer = PeerId.CreateNull(multiBitfield.Length); tester = new TestPicker(); picker = new PriorityPicker(tester); peers = new List <PeerId>(); }
public void Setup() { singleFile = CreateSingleFile(); singleBitfield = new MutableBitField(singleFile.PieceCount()).SetAll(true); singlePeer = PeerId.CreateNull(singleBitfield.Length); multiFile = CreateMultiFile(); multiBitfield = new MutableBitField(multiFile.PieceCount()).SetAll(true); multiPeer = PeerId.CreateNull(multiBitfield.Length); checker = new PiecePickerFilterChecker(); picker = new PriorityPicker(checker); peers = new List <PeerId> (); }
public void Setup() { blocksPerPiece = 4; pieceCount = 40; bitfield = new BitField(pieceCount); data = new TorrentData { PieceLength = Piece.BlockSize * blocksPerPiece, Files = new[] { new TorrentFile("Test", Piece.BlockSize * blocksPerPiece * pieceCount, 0, pieceCount - 1) }, Size = Piece.BlockSize * blocksPerPiece * pieceCount - 10 }; peer = PeerId.CreateNull(pieceCount, seeder: true, isChoking: false, amInterested: true); }
public async Task EmptyPeerId_PrivateTorrent() { var torrent = TestRig.CreatePrivate(); using var engine = new ClientEngine(EngineSettingsBuilder.CreateForTests()); var manager = await engine.AddAsync(torrent, ""); manager.Mode = new DownloadMode(manager, DiskManager, ConnectionManager, Settings); var peer = PeerId.CreateNull(manager.Bitfield.Length); peer.Peer.PeerId = null; var handshake = new HandshakeMessage(manager.InfoHashes.V1OrV2, new BEncodedString(Enumerable.Repeat('c', 20).ToArray()), Constants.ProtocolStringV100, false); manager.Mode.HandleMessage(peer, handshake, default); Assert.AreEqual(handshake.PeerId, peer.PeerID); }
public void DupeRequests_CanRequestInTriplicate() { var seeders = new IPeer[] { PeerId.CreateNull(bitfield.Length, true, false, true), PeerId.CreateNull(bitfield.Length, true, false, true), PeerId.CreateNull(bitfield.Length, true, false, true), }; var queue = new Queue <IPeer> (seeders); var requests = seeders.ToDictionary(t => t, t => new List <BlockInfo> ()); var singlePiece = seeders[0].BitField.Clone().SetAll(false).Set(3, true); // Request an entire piece using 1 peer first to ensure we have collisions when // issuing duplicates. In the end all peers should have the same set though. while (true) { var req = picker.PickPiece(seeders[0], singlePiece) ?? picker.ContinueAnyExistingRequest(seeders[0], 0, bitfield.Length - 1, 3); if (req.HasValue) { requests[seeders[0]].Add(req.Value); } else { break; } } Assert.AreEqual(torrentData.BlocksPerPiece, requests[seeders[0]].Count); while (queue.Count > 0) { var seeder = queue.Dequeue(); var req = picker.PickPiece(seeder, singlePiece) ?? picker.ContinueAnyExistingRequest(seeder, 0, bitfield.Length - 1, 3); if (req.HasValue) { queue.Enqueue(seeder); requests[seeder].Add(req.Value); } } CollectionAssert.AreEquivalent(requests[seeders[0]], requests[seeders[1]]); CollectionAssert.AreEquivalent(requests[seeders[1]], requests[seeders[2]]); Assert.AreEqual(torrentData.BlocksPerPiece, requests.Values.First().Count); }
public void DupeRequests_PeerCannotDuplicateOwnRequest() { var seeder = PeerId.CreateNull(bitfield.Length, true, false, true); var singlePiece = seeder.BitField.Clone().SetAll(false).Set(3, true); Assert.IsNull(picker.ContinueAnyExistingRequest(seeder, 0, bitfield.Length - 1)); BlockInfo?req; var requests = new List <BlockInfo> (); while ((req = picker.PickPiece(seeder, singlePiece)) != null) { requests.Add(req.Value); } Assert.IsNull(picker.ContinueAnyExistingRequest(seeder, 0, bitfield.Length - 1)); Assert.IsNull(picker.ContinueAnyExistingRequest(seeder, 0, bitfield.Length - 1, 2)); }
public void MultiPick() { id.BitField.Set(pieces[0].Index, true); other.BitField.Set(pieces[0].Index, true); for (int i = 2; i < pieces[0].BlockCount; i++) { pieces[0].Blocks[i].CreateRequest(PeerId.CreateNull(torrentData.PieceCount)); pieces[0].Blocks[i].Received = true; } picker.Initialise(bitfield, torrentData, pieces); // Pick blocks 1 and 2 for both peers while (picker.PickPiece(id, id.BitField, new List <PeerId>()) != null) { } while (picker.PickPiece(other, id.BitField, new List <PeerId>()) != null) { } Assert.AreEqual(2, id.AmRequestingPiecesCount, "#1"); Assert.AreEqual(2, other.AmRequestingPiecesCount, "#1"); Piece piece; if (!picker.ValidatePiece(id, pieces[0].Index, pieces[0][0].StartOffset, pieces[0][0].RequestLength, out piece)) { Assert.Fail("I should've validated!"); } if (picker.ValidatePiece(other, pieces[0].Index, pieces[0][0].StartOffset, pieces[0][0].RequestLength, out piece)) { Assert.Fail("I should not have validated!"); } Assert.AreEqual(1, id.AmRequestingPiecesCount, "#1"); Assert.AreEqual(1, other.AmRequestingPiecesCount, "#1"); Assert.IsTrue(pieces[0][0].Received, "#5"); Assert.AreEqual(16, pieces[0].TotalRequested, "#6"); Assert.AreEqual(15, pieces[0].TotalReceived, "#7"); }
public void PickHighestPriority() { var data = new TorrentData(); var ignoringBitfield = new MutableBitField(data.PieceCount()) .SetAll(false); var requester = new StreamingPieceRequester(); requester.Initialise(data, new[] { ignoringBitfield }); requester.SeekToPosition(data.Files[0], data.PieceLength * 3); var peer = PeerId.CreateNull(ignoringBitfield.Length, true, false, true); requester.AddRequests(peer, Array.Empty <IPeerWithMessaging> ()); Assert.AreEqual(4, peer.AmRequestingPiecesCount); var requests = GetRequests(peer); Assert.AreEqual(4, requests.Count); Assert.IsTrue(requests.All(r => r.PieceIndex == 3)); }