using System.Reflection;
using System.Text.RegularExpressions;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Cluster;
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.Configuration;
///
/// lmxopcua#1003: Akka untrusted-mode is ON by default, so a cross-node ActorSelection to a path missing from
/// is dropped. These guards keep the trusted set in step with the
/// code: every cluster singleton the ControlPlane registers, the driver host actor's name, and the one application-level
/// selection in src/.
///
public sealed class RemotingTrustedPathsCoverageTests
{
[Fact]
public void Every_registered_cluster_singleton_is_trusted()
{
var names = typeof(ZB.MOM.WW.OtOpcUa.ControlPlane.ServiceCollectionExtensions)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(f => f.IsLiteral && f.Name.EndsWith("SingletonName", StringComparison.Ordinal))
.Select(f => (string)f.GetRawConstantValue()!)
.ToArray();
names.ShouldNotBeEmpty("positive control: the constants are found");
foreach (var name in names)
{
RemotingHardening.SingletonNames.ShouldContain(name,
$"singleton '{name}' must be added to RemotingHardening.SingletonNames or its proxy/hand-over selections are dropped");
}
}
[Fact]
public void The_driver_host_path_is_the_runtime_actor_name()
=> RemotingHardening.DriverHostPath.ShouldBe("/user/" + ZB.MOM.WW.OtOpcUa.Runtime.ServiceCollectionExtensions.DriverHostActorName);
///
/// The only ActorSelection in src/ is the diagnostics client (which targets /user/driver-host).
/// A new one must come with its path in the trusted set — then add its file here.
///
[Fact]
public void No_new_cross_node_actor_selection_appears_without_a_trusted_path()
{
var allowed = new HashSet(StringComparer.Ordinal) { "FleetDiagnosticsClient.cs" };
var call = new Regex(@"\.ActorSelection\s*\(", RegexOptions.Compiled);
var offenders = Directory.EnumerateFiles(Path.Combine(SolutionRoot(), "src"), "*.cs", SearchOption.AllDirectories)
.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal))
.Where(f => File.ReadLines(f).Any(l => !l.TrimStart().StartsWith("//", StringComparison.Ordinal) && call.IsMatch(l)))
.Select(Path.GetFileName)
.Where(n => !allowed.Contains(n!))
.ToArray();
offenders.ShouldBeEmpty();
}
private static string SolutionRoot()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
dir = dir.Parent;
dir.ShouldNotBeNull("Could not find solution root (ZB.MOM.WW.OtOpcUa.slnx).");
return dir!.FullName;
}
}