76 lines
2.8 KiB
C#
76 lines
2.8 KiB
C#
using System.IO;
|
|
using System.Net.Sockets;
|
|
using ZB.MOM.WW.CBDDC.Core;
|
|
using ZB.MOM.WW.CBDDC.Core.Network;
|
|
using ZB.MOM.WW.CBDDC.Core.Storage;
|
|
using ZB.MOM.WW.CBDDC.Network.Security;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.CBDDC.Network.Tests;
|
|
|
|
public class HandshakeRegressionTests
|
|
{
|
|
/// <summary>
|
|
/// Verifies that the server invokes the handshake service when a client connects.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Server_Should_Call_HandshakeService_On_Client_Connection()
|
|
{
|
|
// Arrange
|
|
var oplogStore = Substitute.For<IOplogStore>();
|
|
oplogStore.GetLatestTimestampAsync(Arg.Any<CancellationToken>())
|
|
.Returns(new HlcTimestamp(0, 0, "node"));
|
|
oplogStore.GetVectorClockAsync(Arg.Any<CancellationToken>())
|
|
.Returns(new VectorClock());
|
|
oplogStore.GetOplogAfterAsync(Arg.Any<HlcTimestamp>(), Arg.Any<IEnumerable<string>?>(), Arg.Any<CancellationToken>())
|
|
.Returns(Array.Empty<OplogEntry>());
|
|
oplogStore.GetOplogForNodeAfterAsync(Arg.Any<string>(), Arg.Any<HlcTimestamp>(), Arg.Any<IEnumerable<string>?>(), Arg.Any<CancellationToken>())
|
|
.Returns(Array.Empty<OplogEntry>());
|
|
|
|
var configProvider = Substitute.For<IPeerNodeConfigurationProvider>();
|
|
configProvider.GetConfiguration().Returns(new PeerNodeConfiguration
|
|
{
|
|
NodeId = "server-node",
|
|
AuthToken = "auth-token",
|
|
TcpPort = 0
|
|
});
|
|
|
|
var snapshotService = Substitute.For<ISnapshotService>();
|
|
|
|
var documentStore = Substitute.For<IDocumentStore>();
|
|
documentStore.InterestedCollection.Returns(["Users"]);
|
|
|
|
var authenticator = Substitute.For<IAuthenticator>();
|
|
authenticator.ValidateAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(true);
|
|
|
|
var handshakeService = Substitute.For<IPeerHandshakeService>();
|
|
handshakeService.HandshakeAsync(Arg.Any<Stream>(), Arg.Any<bool>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
|
|
.Returns((CipherState?)null);
|
|
|
|
var server = new TcpSyncServer(
|
|
oplogStore,
|
|
documentStore,
|
|
snapshotService,
|
|
configProvider,
|
|
NullLogger<TcpSyncServer>.Instance,
|
|
authenticator,
|
|
handshakeService);
|
|
|
|
await server.Start();
|
|
var port = server.ListeningPort ?? throw new Exception("Server did not start or report port");
|
|
|
|
// Act
|
|
using (var client = new TcpClient())
|
|
{
|
|
await client.ConnectAsync("127.0.0.1", port);
|
|
await Task.Delay(500);
|
|
}
|
|
|
|
await server.Stop();
|
|
|
|
// Assert
|
|
await handshakeService.Received(1)
|
|
.HandshakeAsync(Arg.Any<Stream>(), false, "server-node", Arg.Any<CancellationToken>());
|
|
}
|
|
}
|