From cbf6690f45b41c4f9223762409eb405ed8902818 Mon Sep 17 00:00:00 2001 From: Rose Jethani <101273941+rose2221@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:11:47 +0530 Subject: [PATCH] Added TLS Tests (#107) --- .../Libp2p.Protocols.Tls.Tests.csproj | 30 +++++++++ .../TlsProtocolTests.cs | 63 +++++++++++++++++++ .../Libp2p.Protocols.Tls.Tests/Using.cs | 11 ++++ src/libp2p/Libp2p.sln | 6 ++ 4 files changed, 110 insertions(+) create mode 100644 src/libp2p/Libp2p.Protocols.Tls.Tests/Libp2p.Protocols.Tls.Tests.csproj create mode 100644 src/libp2p/Libp2p.Protocols.Tls.Tests/TlsProtocolTests.cs create mode 100644 src/libp2p/Libp2p.Protocols.Tls.Tests/Using.cs diff --git a/src/libp2p/Libp2p.Protocols.Tls.Tests/Libp2p.Protocols.Tls.Tests.csproj b/src/libp2p/Libp2p.Protocols.Tls.Tests/Libp2p.Protocols.Tls.Tests.csproj new file mode 100644 index 00000000..59e583e5 --- /dev/null +++ b/src/libp2p/Libp2p.Protocols.Tls.Tests/Libp2p.Protocols.Tls.Tests.csproj @@ -0,0 +1,30 @@ + + + enable + enable + Nethermind.$(MSBuildProjectName.Replace(" ", "_")) + false + Nethermind.$(MSBuildProjectName) + + + + + + + + + + + + all + runtime;build;native;contentfiles;analyzers;buildtransitive + + + + + + + + + + diff --git a/src/libp2p/Libp2p.Protocols.Tls.Tests/TlsProtocolTests.cs b/src/libp2p/Libp2p.Protocols.Tls.Tests/TlsProtocolTests.cs new file mode 100644 index 00000000..25239947 --- /dev/null +++ b/src/libp2p/Libp2p.Protocols.Tls.Tests/TlsProtocolTests.cs @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: MIT + +namespace Nethermind.Libp2p.Protocols.TLS.Tests; + +[TestFixture] +[Parallelizable(scope: ParallelScope.All)] +public class TlsProtocolTests +{ + [Test] + public async Task Test_ConnectionEstablished_AfterHandshake() + { + // Arrange + IChannel downChannel = new TestChannel(); + IChannel downChannelFromProtocolPov = ((TestChannel)downChannel).Reverse(); + IChannelFactory channelFactory = Substitute.For(); + IPeerContext peerContext = Substitute.For(); + IPeerContext listenerContext = Substitute.For(); + ILoggerFactory loggerFactory = Substitute.For(); + + TestChannel upChannel = new TestChannel(); + channelFactory.SubDial(Arg.Any(), Arg.Any()) + .Returns(upChannel); + + TestChannel listenerUpChannel = new TestChannel(); + channelFactory.SubListen(Arg.Any(), Arg.Any()) + .Returns(listenerUpChannel); + + peerContext.LocalPeer.Identity.Returns(new Identity()); + listenerContext.LocalPeer.Identity.Returns(new Identity()); + + string peerId = peerContext.LocalPeer.Identity.PeerId.ToString(); + Multiaddress localAddr = $"/ip4/0.0.0.0/tcp/0/p2p/{peerId}"; + peerContext.LocalPeer.Address.Returns(localAddr); + listenerContext.RemotePeer.Address.Returns(localAddr); + + string listenerPeerId = listenerContext.LocalPeer.Identity.PeerId.ToString(); + Multiaddress listenerAddr = $"/ip4/0.0.0.0/tcp/0/p2p/{listenerPeerId}"; + peerContext.RemotePeer.Address.Returns(listenerAddr); + listenerContext.LocalPeer.Address.Returns(listenerAddr); + + var i_multiplexerSettings = new MultiplexerSettings(); + var r_multiplexerSettings = new MultiplexerSettings(); + TlsProtocol tlsProtocolListener = new TlsProtocol(i_multiplexerSettings, loggerFactory); + TlsProtocol tlsProtocolInitiator = new TlsProtocol(r_multiplexerSettings, loggerFactory); + + // Act + Task listenTask = tlsProtocolListener.ListenAsync(downChannel, channelFactory, listenerContext); + Task dialTask = tlsProtocolInitiator.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext); + + int sent = 42; + ValueTask writeTask = listenerUpChannel.Reverse().WriteVarintAsync(sent); + int received = await upChannel.Reverse().ReadVarintAsync(); + await writeTask; + + await upChannel.CloseAsync(); + await listenerUpChannel.CloseAsync(); + await downChannel.CloseAsync(); + + // Assert + Assert.That(received, Is.EqualTo(sent)); + } +} diff --git a/src/libp2p/Libp2p.Protocols.Tls.Tests/Using.cs b/src/libp2p/Libp2p.Protocols.Tls.Tests/Using.cs new file mode 100644 index 00000000..2048bd47 --- /dev/null +++ b/src/libp2p/Libp2p.Protocols.Tls.Tests/Using.cs @@ -0,0 +1,11 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: MIT + +global using Nethermind.Libp2p.Core; +global using Nethermind.Libp2p.Core.TestsBase; +global using NSubstitute; +global using NUnit.Framework; +global using Multiformats.Address; +global using System.Threading.Tasks; +global using Microsoft.Extensions.Logging; + diff --git a/src/libp2p/Libp2p.sln b/src/libp2p/Libp2p.sln index 241485d4..98b2591e 100644 --- a/src/libp2p/Libp2p.sln +++ b/src/libp2p/Libp2p.sln @@ -67,6 +67,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransportInterop", "..\samp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libp2p.Protocols.Tls", "Libp2p.Protocols.Tls\Libp2p.Protocols.Tls.csproj", "{C3CDBAAE-C790-443A-A293-D6E2330160F7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libp2p.Protocols.Tls.Tests", "Libp2p.Protocols.Tls.Tests\Libp2p.Protocols.Tls.Tests.csproj", "{89BD907E-1399-4BE7-98CC-E541EAB21842}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -181,6 +183,10 @@ Global {C3CDBAAE-C790-443A-A293-D6E2330160F7}.Debug|Any CPU.Build.0 = Debug|Any CPU {C3CDBAAE-C790-443A-A293-D6E2330160F7}.Release|Any CPU.ActiveCfg = Release|Any CPU {C3CDBAAE-C790-443A-A293-D6E2330160F7}.Release|Any CPU.Build.0 = Release|Any CPU + {89BD907E-1399-4BE7-98CC-E541EAB21842}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89BD907E-1399-4BE7-98CC-E541EAB21842}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89BD907E-1399-4BE7-98CC-E541EAB21842}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89BD907E-1399-4BE7-98CC-E541EAB21842}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE