Skip to content

Commit

Permalink
Stabilise some more tests (#1438)
Browse files Browse the repository at this point in the history
* Stabilise some more tests

* just increase the timeout
  • Loading branch information
Rob-Hague authored Jul 16, 2024
1 parent f512a41 commit 5ab8f3e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
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

0 comments on commit 5ab8f3e

Please sign in to comment.