-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/libp2p/Libp2p.Protocols.Tls.Tests/Libp2p.Protocols.Tls.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters