Skip to content

Stabilise some more tests #1438

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

Merged
merged 2 commits into from
Jul 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ protected override void Act()
{
_client.KeepAliveInterval = _keepAliveInterval;

// allow keep-alive to be sent a few times. .NET 8 is faster and
// we need to wait less because we want exactly three messages in a session.
#if NETFRAMEWORK
Thread.Sleep(195);
#else
Thread.Sleep(170);
#endif
Thread.Sleep(200);
}

[TestMethod]
Expand Down Expand Up @@ -102,7 +96,9 @@ public void IsConnectedOnSessionShouldBeInvokedOnce()
[TestMethod]
public void SendMessageOnSessionShouldBeInvokedThreeTimes()
{
SessionMock.Verify(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()), Times.Exactly(3));
#pragma warning disable IDE0002 // Name can be simplified; "Ambiguous reference between Moq.Range and System.Range"
SessionMock.Verify(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()), Times.Between(2, 4, Moq.Range.Inclusive));
#pragma warning restore IDE0002
}

private class MyClient : BaseClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class ChannelForwardedTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpe
private TimeSpan _channelCloseTimeout;
private IPEndPoint _remoteEndpoint;
private AsyncSocketListener _remoteListener;
private EventWaitHandle _channelBindFinishedWaitHandle;
private Exception _channelException;
private IList<Socket> _connectedRegister;
private IList<Socket> _disconnectedRegister;
Expand Down Expand Up @@ -76,7 +75,6 @@ private void Arrange()
_remoteWindowSize = (uint)random.Next(0, int.MaxValue);
_remotePacketSize = (uint)random.Next(100, 200);
_channelCloseTimeout = TimeSpan.FromSeconds(random.Next(10, 20));
_channelBindFinishedWaitHandle = new ManualResetEvent(false);
_channelException = null;
_connectedRegister = new List<Socket>();
_disconnectedRegister = new List<Socket>();
Expand Down Expand Up @@ -148,29 +146,27 @@ private void Arrange()
_remotePacketSize);

_channelThread = new Thread(() =>
{
try
{
_channel.Bind(_remoteEndpoint, _forwardedPortMock.Object);
}
catch (Exception ex)
{
try
{
_channel.Bind(_remoteEndpoint, _forwardedPortMock.Object);
}
catch (Exception ex)
{
_channelException = ex;
}
finally
{
_ = _channelBindFinishedWaitHandle.Set();
}
});
_channelException = ex;
}
});
_channelThread.Start();

// give channel time to bind to remote endpoint
Thread.Sleep(100);
Thread.Sleep(300);
}

private void Act()
{
_channel.Dispose();

Assert.IsTrue(_channelThread.Join(TimeSpan.FromSeconds(1)));
}

[TestMethod]
Expand All @@ -185,7 +181,6 @@ public void ChannelShouldShutdownSocketToRemoteListener()
public void BindShouldHaveFinishedWithoutException()
{
Assert.IsNull(_channelException, _channelException?.ToString());
Assert.IsTrue(_channelBindFinishedWaitHandle.WaitOne(0));
}

[TestMethod]
Expand Down
27 changes: 20 additions & 7 deletions test/Renci.SshNet.Tests/Classes/SessionTest_Connected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,29 @@ public void IncludeStrictKexPseudoAlgorithmInInitKex()
[TestMethod]
public void ShouldNotIncludeStrictKexPseudoAlgorithmInSubsequentKex()
{
ServerBytesReceivedRegister.Clear();
Session.SendMessage(Session.ClientInitMessage);
using var kexReceived = new ManualResetEventSlim();
bool kexContainsPseudoAlg = true;

Thread.Sleep(100);
ServerListener.BytesReceived += ServerListener_BytesReceived;

Assert.IsTrue(ServerBytesReceivedRegister.Count > 0);
void ServerListener_BytesReceived(byte[] bytesReceived, System.Net.Sockets.Socket socket)
{
if (bytesReceived.Length > 5 && bytesReceived[5] == 20)
{
// SSH_MSG_KEXINIT = 20
var kexInitMessage = new KeyExchangeInitMessage();
kexInitMessage.Load(bytesReceived, 6, bytesReceived.Length - 6);
kexContainsPseudoAlg = kexInitMessage.KeyExchangeAlgorithms.Contains("[email protected]");
kexReceived.Set();
}
}

var kexInitMessage = new KeyExchangeInitMessage();
kexInitMessage.Load(ServerBytesReceivedRegister[0], 4 + 1 + 1, ServerBytesReceivedRegister[0].Length - 4 - 1 - 1);
Assert.IsFalse(kexInitMessage.KeyExchangeAlgorithms.Contains("[email protected]"));
Session.SendMessage(Session.ClientInitMessage);

Assert.IsTrue(kexReceived.Wait(1000));
Assert.IsFalse(kexContainsPseudoAlg);

ServerListener.BytesReceived -= ServerListener_BytesReceived;
}

[TestMethod]
Expand Down