Skip to content

Commit

Permalink
Added TLS Tests (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
rose2221 authored Sep 30, 2024
1 parent 3464c85 commit cbf6690
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Nethermind.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<IsPackable>false</IsPackable>
<AssemblyName>Nethermind.$(MSBuildProjectName)</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NLog.Extensions.Logging" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="NUnit.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime;build;native;contentfiles;analyzers;buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Libp2p.Protocols.Tls/Libp2p.Protocols.Tls.csproj" />
<ProjectReference Include="..\Libp2p.Core.TestsBase\Libp2p.Core.TestsBase.csproj" />
</ItemGroup>
</Project>
63 changes: 63 additions & 0 deletions src/libp2p/Libp2p.Protocols.Tls.Tests/TlsProtocolTests.cs
Original file line number Diff line number Diff line change
@@ -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<IChannelFactory>();
IPeerContext peerContext = Substitute.For<IPeerContext>();
IPeerContext listenerContext = Substitute.For<IPeerContext>();
ILoggerFactory loggerFactory = Substitute.For<ILoggerFactory>();

TestChannel upChannel = new TestChannel();
channelFactory.SubDial(Arg.Any<IPeerContext>(), Arg.Any<IChannelRequest>())
.Returns(upChannel);

TestChannel listenerUpChannel = new TestChannel();
channelFactory.SubListen(Arg.Any<IPeerContext>(), Arg.Any<IChannelRequest>())
.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<IOResult> 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));
}
}
11 changes: 11 additions & 0 deletions src/libp2p/Libp2p.Protocols.Tls.Tests/Using.cs
Original file line number Diff line number Diff line change
@@ -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;

6 changes: 6 additions & 0 deletions src/libp2p/Libp2p.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cbf6690

Please sign in to comment.