Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the rest of the tests #99

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 17 additions & 79 deletions src/libp2p/Libp2p.Protocols.Noise.Tests/NoiseProtocolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public async Task Test_ConnectionEstablished_AfterHandshake()

channelFactory.SubProtocols.Returns(new[] { proto1, proto2 });

IChannel upChannel = new TestChannel();
channelFactory.SubDialAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.FromResult(upChannel));
channelFactory.SubListenAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.CompletedTask);
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);

var i_multiplexerSettings = new MultiplexerSettings();
var r_multiplexerSettings = new MultiplexerSettings();
Expand All @@ -55,19 +58,18 @@ public async Task Test_ConnectionEstablished_AfterHandshake()
Task listenTask = proto_responder.ListenAsync(downChannel, channelFactory, listenerContext);
Task dialTask = proto_initiator.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);

ValueTask<IOResult> writeTask = downChannelFromProtocolPov.WriteVarintAsync(1);
Task<int> readTask = downChannel.ReadVarintAsync();
await Task.WhenAll(writeTask.AsTask(), readTask);
await Task.Delay(TimeSpan.FromSeconds(2));
int str = await readTask;

// Assert
Assert.That(str, Is.EqualTo(1));
int sent = 42;
ValueTask<IOResult> writeTask = upChannel.Reverse().WriteVarintAsync(sent);
int received = await listenerUpChannel.Reverse().ReadVarintAsync();
await writeTask;

// Cleanup
await downChannel.CloseAsync();
await upChannel.CloseAsync();
await listenerUpChannel.CloseAsync();
await downChannel.CloseAsync();

Assert.That(received, Is.EqualTo(sent));
}

[Test]
public async Task Test_ConnectionEstablished_With_PreSelectedMuxer()
{
Expand All @@ -86,11 +88,6 @@ public async Task Test_ConnectionEstablished_With_PreSelectedMuxer()

channelFactory.SubProtocols.Returns(new[] { proto1, proto2 });

IChannel upChannel = new TestChannel();
channelFactory.SubDialAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.FromResult(upChannel));
channelFactory.SubListenAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.CompletedTask);

var i_multiplexerSettings = new MultiplexerSettings();
var r_multiplexerSettings = new MultiplexerSettings();
Expand Down Expand Up @@ -122,64 +119,5 @@ public async Task Test_ConnectionEstablished_With_PreSelectedMuxer()

// Cleanup
await downChannel.CloseAsync();
await upChannel.CloseAsync();
}
[Test]
public async Task Test_ConnectionClosed_ForBrokenHandshake()
{
// 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>();

IProtocol? proto1 = Substitute.For<IProtocol>();
proto1.Id.Returns("proto1");

IProtocol? proto2 = Substitute.For<IProtocol>();
proto2.Id.Returns("proto2");

channelFactory.SubProtocols.Returns(new[] { proto1, proto2 });

IChannel upChannel = new TestChannel();
channelFactory.SubDialAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.FromResult(upChannel));
channelFactory.SubListenAndBind(Arg.Any<IChannel>(), Arg.Any<IPeerContext>(), Arg.Any<IProtocol>())
.Returns(Task.CompletedTask);

var i_multiplexerSettings = new MultiplexerSettings();
var r_multiplexerSettings = new MultiplexerSettings();
r_multiplexerSettings.Add(proto2);
r_multiplexerSettings.Add(proto1);
i_multiplexerSettings.Add(proto1);

NoiseProtocol proto_initiator = new(i_multiplexerSettings);
NoiseProtocol proto_responder = new(r_multiplexerSettings);

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.RemotePeer.Address.Returns(localAddr);

string listenerPeerId = listenerContext.LocalPeer.Identity.PeerId.ToString();
Multiaddress listenerAddr = $"/ip4/0.0.0.0/tcp/0/p2p/{listenerPeerId}";
listenerContext.RemotePeer.Address.Returns(listenerAddr);

// Act
Task listenTask = proto_responder.ListenAsync(downChannel, channelFactory, listenerContext);
Task dialTask = proto_initiator.DialAsync(downChannelFromProtocolPov, channelFactory, peerContext);

await downChannelFromProtocolPov.WriteVarintAsync(1);
int str = await downChannel.ReadVarintAsync();

// Assert
Assert.That(str, Is.Not.EqualTo(1));

// Cleanup
await downChannel.CloseAsync();
await upChannel.CloseAsync();
}
}
17 changes: 8 additions & 9 deletions src/libp2p/Libp2p.Protocols.Noise/NoiseProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public async Task DialAsync(IChannel downChannel, IChannelFactory? upChannelFact
PublicKey? msg1KeyDecoded = PublicKey.Parser.ParseFrom(msg1Decoded.IdentityKey);
//var key = new byte[] { 0x1 }.Concat(clientStatic.PublicKey).ToArray();
List<string> responderMuxers = msg1Decoded.Extensions.StreamMuxers
.Where(m => !string.IsNullOrEmpty(m))
.ToList();
IProtocol commonMuxer = multiplexerSettings.Multiplexers.FirstOrDefault(m => responderMuxers.Contains(m.Id));
if (commonMuxer != null)
.Where(m => !string.IsNullOrEmpty(m))
.ToList();
IProtocol? commonMuxer = multiplexerSettings?.Multiplexers.FirstOrDefault(m => responderMuxers.Contains(m.Id));
if (commonMuxer is not null)
{
context.SpecificProtocolRequest = new ChannelRequest
{
Expand Down Expand Up @@ -158,11 +158,10 @@ public async Task ListenAsync(IChannel downChannel, IChannelFactory? upChannelFa

PeerId remotePeerId = new(msg2KeyDecoded);

List<string> initiatorMuxers = msg2Decoded.Extensions.StreamMuxers
.Where(m => !string.IsNullOrEmpty(m))
.ToList();
IProtocol commonMuxer = multiplexerSettings.Multiplexers.FirstOrDefault(m => initiatorMuxers.Contains(m.Id));
if (commonMuxer != null)
List<string> initiatorMuxers = msg2Decoded.Extensions.StreamMuxers.Where(m => !string.IsNullOrEmpty(m)).ToList();
IProtocol? commonMuxer = multiplexerSettings?.Multiplexers.FirstOrDefault(m => initiatorMuxers.Contains(m.Id));

if (commonMuxer is not null)
{
context.SpecificProtocolRequest = new ChannelRequest
{
Expand Down
1 change: 0 additions & 1 deletion src/samples/chat/Chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Chat</RootNamespace>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

Expand Down
Loading