From a3cbbf80ff98912563deec037c036663fd856eb7 Mon Sep 17 00:00:00 2001 From: Alan McGovern Date: Wed, 1 Mar 2023 00:02:35 +0000 Subject: [PATCH] [tests] Add support for ipv4 and ipv6 fake connections --- .../DownloadModeTests.cs | 13 ++++++++++++- .../HashingModeTests.cs | 3 ++- .../MetadataModeTests.cs | 3 ++- .../StartingModeTests.cs | 3 ++- .../StoppedModeTests.cs | 3 ++- .../StoppingModeTests.cs | 3 ++- .../MonoTorrent.Client/ClientEngineTests.cs | 2 ++ .../MonoTorrent.Client/EncryptorFactoryTests.cs | 3 ++- .../MonoTorrent.Client/NetworkIOTests.cs | 2 +- .../MonoTorrent.Client/PeerIOTests.cs | 3 ++- .../MonoTorrent.Client/TestRig.cs | 17 ++++++++++++++--- 11 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/DownloadModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/DownloadModeTests.cs index 54b4953b4..e798be9c1 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/DownloadModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/DownloadModeTests.cs @@ -31,6 +31,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Net.Sockets; using System.Threading.Tasks; using MonoTorrent.BEncoding; @@ -59,7 +60,7 @@ public class DownloadModeTests [SetUp] public void Setup () { - conn = new ConnectionPair ().WithTimeout (); + conn = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); Settings = new EngineSettings (); PieceWriter = new TestWriter (); DiskManager = new DiskManager (Settings, Factories.Default, PieceWriter); @@ -262,6 +263,16 @@ public async Task AnnounceWhenComplete () Assert.AreEqual (TorrentEvent.Completed, TrackerManager.Announces[1].Item2, "#4"); } + [Test] + public void DhtPortMessage_IPv4 () + { + using var pair = new ConnectionPair (AddressFamily.InterNetwork); + var peer = new PeerId (new Peer (new PeerInfo (pair.Outgoing.Uri), Manager.InfoHashes.V1OrV2), pair.Outgoing, new BitField (Manager.Torrent.PieceCount ())); + Manager.Mode = new DownloadMode (Manager, DiskManager, ConnectionManager, Settings); + Manager.Mode.HandleMessage (peer, new DhtPortMessage (1234), default); + Assert.AreEqual (1234, peer.DhtPort); + } + [Test] public void MismatchedInfoHash () { diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/HashingModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/HashingModeTests.cs index 4529dafe6..c0c669549 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/HashingModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/HashingModeTests.cs @@ -30,6 +30,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Sockets; using System.Threading.Tasks; using MonoTorrent.Connections; @@ -54,7 +55,7 @@ public class HashingModeTests [SetUp] public void Setup () { - conn = new ConnectionPair ().WithTimeout (); + conn = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); PieceWriter = new TestWriter (); TrackerManager = new ManualTrackerManager (); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/MetadataModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/MetadataModeTests.cs index be85f0a72..e9d1c0d11 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/MetadataModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/MetadataModeTests.cs @@ -32,6 +32,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Net.Sockets; using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; @@ -56,7 +57,7 @@ public class MetadataModeTests public async Task Setup (bool metadataMode, bool multiFile = false, bool metadataOnly = false) { - pair = new ConnectionPair ().WithTimeout (); + pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); rig = multiFile ? TestRig.CreateMultiFile (32768, metadataMode) : TestRig.CreateSingleFile (Constants.BlockSize * 27, Constants.BlockSize * 2, metadataMode); rig.RecreateManager ().Wait (); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StartingModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StartingModeTests.cs index 8569154ef..fce536a0c 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StartingModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StartingModeTests.cs @@ -29,6 +29,7 @@ using System; using System.Collections.Generic; +using System.Net.Sockets; using System.Threading.Tasks; using MonoTorrent.Trackers; @@ -52,7 +53,7 @@ public class StartingModeTests [SetUp] public void Setup () { - conn = new ConnectionPair ().WithTimeout (); + conn = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); Settings = new EngineSettings (); PieceWriter = new TestWriter (); DiskManager = new DiskManager (Settings, Factories.Default, PieceWriter); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppedModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppedModeTests.cs index 059548623..9cf9ec6fe 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppedModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppedModeTests.cs @@ -28,6 +28,7 @@ using System; +using System.Net.Sockets; using MonoTorrent.Connections; using MonoTorrent.Trackers; @@ -51,7 +52,7 @@ public class StoppedModeTests [SetUp] public void Setup () { - conn = new ConnectionPair ().WithTimeout (); + conn = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); Settings = new EngineSettings (); DiskManager = new DiskManager (Settings, Factories.Default, new NullWriter ()); ConnectionManager = new ConnectionManager ("LocalPeerId", Settings, Factories.Default, DiskManager); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppingModeTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppingModeTests.cs index b59753800..29c4e8d3c 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppingModeTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/StoppingModeTests.cs @@ -28,6 +28,7 @@ using System; +using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; @@ -52,7 +53,7 @@ public class StoppingModeTests [SetUp] public void Setup () { - conn = new ConnectionPair ().WithTimeout (); + conn = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); Settings = new EngineSettings (); DiskManager = new DiskManager (Settings, Factories.Default, new NullWriter ()); ConnectionManager = new ConnectionManager ("LocalPeerId", Settings, Factories.Default, DiskManager); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/ClientEngineTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/ClientEngineTests.cs index 3b65b506d..75bd62f50 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/ClientEngineTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/ClientEngineTests.cs @@ -335,8 +335,10 @@ class FakeListener : IPeerConnectionListener public IPEndPoint PreferredLocalEndPoint { get; set; } public ListenerStatus Status { get; } +#pragma warning disable 0067 public event EventHandler ConnectionReceived; public event EventHandler StatusChanged; +#pragma warning restore 0067 public FakeListener (int port) => (PreferredLocalEndPoint) = (new IPEndPoint (IPAddress.Any, port)); diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/EncryptorFactoryTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/EncryptorFactoryTests.cs index 3b24be2ab..ad780d171 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/EncryptorFactoryTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/EncryptorFactoryTests.cs @@ -30,6 +30,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Sockets; using System.Threading.Tasks; using MonoTorrent.BEncoding; @@ -58,7 +59,7 @@ public class EncryptorFactoryTests [SetUp] public void Setup () { - pair = new ConnectionPair ().WithTimeout (); + pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); InfoHash = new InfoHash (Enumerable.Repeat ((byte) 255, 20).ToArray ()); SKeys = new[] { diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/NetworkIOTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/NetworkIOTests.cs index 597298295..7d60afe9d 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/NetworkIOTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/NetworkIOTests.cs @@ -52,7 +52,7 @@ public class NetworkIOTests [SetUp] public void Setup () { - pair = new ConnectionPair ().WithTimeout (); + pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); } [TearDown] diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/PeerIOTests.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/PeerIOTests.cs index d65db8dd9..88cf15f87 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/PeerIOTests.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/PeerIOTests.cs @@ -28,6 +28,7 @@ using System; +using System.Net.Sockets; using System.Threading.Tasks; using MonoTorrent.Connections.Peer.Encryption; @@ -46,7 +47,7 @@ public class PeerIOTests [SetUp] public void Setup () { - pair = new ConnectionPair ().WithTimeout (); + pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout (); } [TearDown] diff --git a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/TestRig.cs b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/TestRig.cs index d0776fc31..99e4b0a0f 100644 --- a/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/TestRig.cs +++ b/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/TestRig.cs @@ -283,12 +283,23 @@ public class ConnectionPair : IDisposable public CustomConnection Incoming { get; } public CustomConnection Outgoing { get; } - public ConnectionPair () + public ConnectionPair (AddressFamily family) { var incoming = new SocketStream (); var outgoing = new SocketStream (); - Incoming = new CustomConnection (incoming, outgoing, true); - Outgoing = new CustomConnection (outgoing, incoming, false); + + var uriIncoming = family switch { + AddressFamily.InterNetwork => new Uri ("ipv4://1.2.3.4:1111"), + AddressFamily.InterNetworkV6 => new Uri ("ipv6://[DE:AD::BE:EF]:1111"), + _ => throw new NotSupportedException () + }; + var uriOutgoing = family switch { + AddressFamily.InterNetwork => new Uri ("ipv4://4.3.2.1:2222"), + AddressFamily.InterNetworkV6 => new Uri ("ipv6://[DE:AF::BE:EF]:2222"), + _ => throw new NotSupportedException () + }; + Incoming = new CustomConnection (incoming, outgoing, true, uriIncoming); + Outgoing = new CustomConnection (outgoing, incoming, false, uriOutgoing); } public void Dispose ()