Skip to content

Commit

Permalink
[tests] Add support for ipv4 and ipv6 fake connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alanmcgovern committed Mar 8, 2023
1 parent 42acbc5 commit a3cbbf8
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;

using MonoTorrent.Connections;
Expand All @@ -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 ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading.Tasks;

using MonoTorrent.Trackers;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


using System;
using System.Net.Sockets;

using MonoTorrent.Connections;
using MonoTorrent.Trackers;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


using System;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ class FakeListener : IPeerConnectionListener
public IPEndPoint PreferredLocalEndPoint { get; set; }
public ListenerStatus Status { get; }

#pragma warning disable 0067
public event EventHandler<PeerConnectionEventArgs> ConnectionReceived;
public event EventHandler<EventArgs> StatusChanged;
#pragma warning restore 0067

public FakeListener (int port)
=> (PreferredLocalEndPoint) = (new IPEndPoint (IPAddress.Any, port));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;

using MonoTorrent.BEncoding;
Expand Down Expand Up @@ -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[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NetworkIOTests
[SetUp]
public void Setup ()
{
pair = new ConnectionPair ().WithTimeout ();
pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout ();
}

[TearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


using System;
using System.Net.Sockets;
using System.Threading.Tasks;

using MonoTorrent.Connections.Peer.Encryption;
Expand All @@ -46,7 +47,7 @@ public class PeerIOTests
[SetUp]
public void Setup ()
{
pair = new ConnectionPair ().WithTimeout ();
pair = new ConnectionPair (AddressFamily.InterNetwork).WithTimeout ();
}

[TearDown]
Expand Down
17 changes: 14 additions & 3 deletions src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client/TestRig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down

0 comments on commit a3cbbf8

Please sign in to comment.