513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
208 lines
8.1 KiB
C#
208 lines
8.1 KiB
C#
using System.CommandLine;
|
|
using ZB.MOM.WW.ScadaBridge.CLI;
|
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
|
|
|
|
/// <summary>
|
|
/// Parse tests for the site-routed data-connection subcommands added for actor parity
|
|
/// (arch-review C4): <c>browse</c>, <c>search</c>, <c>verify-endpoint</c>, and
|
|
/// <c>certs list|trust|remove</c>. Each test parses a real command line through
|
|
/// System.CommandLine and asserts the exact management command record produced,
|
|
/// including the routed <c>--site</c> → <c>SiteIdentifier</c> mapping.
|
|
/// </summary>
|
|
public class ConnectionBrowseCommandsTests
|
|
{
|
|
/// <summary>
|
|
/// Builds a root command with the browse/search/verify/certs subcommands attached
|
|
/// under a bare <c>data-connection</c> group. The interceptor captures the built
|
|
/// command record instead of sending it over HTTP, so the tests never need a live
|
|
/// management server.
|
|
/// </summary>
|
|
private static (RootCommand root, List<object> captured) BuildHarness()
|
|
{
|
|
var url = new Option<string>("--url") { Recursive = true };
|
|
var username = new Option<string>("--username") { Recursive = true };
|
|
var password = new Option<string>("--password") { Recursive = true };
|
|
var format = CliOptions.CreateFormatOption();
|
|
|
|
var root = new RootCommand();
|
|
root.Add(url);
|
|
root.Add(username);
|
|
root.Add(password);
|
|
root.Add(format);
|
|
|
|
var captured = new List<object>();
|
|
var dataConnection = new Command("data-connection");
|
|
ConnectionBrowseCommands.AddTo(dataConnection, url, format, username, password, cmd =>
|
|
{
|
|
captured.Add(cmd);
|
|
return 0;
|
|
});
|
|
root.Add(dataConnection);
|
|
|
|
return (root, captured);
|
|
}
|
|
|
|
/// <summary>Verifies that the browse command builds a browse-node command addressed by site identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Browse_BuildsBrowseNodeCommand_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var exit = await root
|
|
.Parse(new[] { "data-connection", "browse", "--site", "SITE1", "--connection", "conn1", "--node-id", "ns=2;s=X" })
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<BrowseNodeCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Equal("conn1", cmd.ConnectionName);
|
|
Assert.Equal("ns=2;s=X", cmd.ParentNodeId);
|
|
}
|
|
|
|
/// <summary>Verifies that browsing without a node id starts from the address-space root.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Browse_WithoutNodeId_BrowsesFromRoot()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var exit = await root
|
|
.Parse(new[] { "data-connection", "browse", "--site", "SITE1", "--connection", "conn1" })
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<BrowseNodeCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Null(cmd.ParentNodeId);
|
|
}
|
|
|
|
/// <summary>Verifies that the search command builds a search-address-space command addressed by site identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Search_BuildsSearchAddressSpaceCommand_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var exit = await root
|
|
.Parse(new[]
|
|
{
|
|
"data-connection", "search", "--site", "SITE1", "--connection", "conn1",
|
|
"--query", "Motor", "--max-depth", "3", "--max-results", "50"
|
|
})
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<SearchAddressSpaceCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Equal("conn1", cmd.ConnectionName);
|
|
Assert.Equal("Motor", cmd.Query);
|
|
Assert.Equal(3, cmd.MaxDepth);
|
|
Assert.Equal(50, cmd.MaxResults);
|
|
}
|
|
|
|
/// <summary>Verifies that verify-endpoint reads the configuration JSON from a file and addresses the site by identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task VerifyEndpoint_ReadsConfigJsonFromFile_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var configPath = Path.GetTempFileName();
|
|
const string configJson = "{\"endpointUrl\":\"opc.tcp://server:4840\"}";
|
|
await File.WriteAllTextAsync(configPath, configJson);
|
|
try
|
|
{
|
|
var exit = await root
|
|
.Parse(new[]
|
|
{
|
|
"data-connection", "verify-endpoint", "--site", "SITE1",
|
|
"--protocol", "OpcUa", "--config-file", configPath
|
|
})
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<VerifyEndpointCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Equal("OpcUa", cmd.Protocol);
|
|
Assert.Equal(configJson, cmd.ConfigJson);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(configPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>Verifies that certs list builds a list-server-certs command addressed by site identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CertsList_BuildsListServerCertsCommand_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var exit = await root
|
|
.Parse(new[] { "data-connection", "certs", "list", "--site", "SITE1", "--connection", "conn1" })
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<ListServerCertsCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
}
|
|
|
|
/// <summary>Verifies that certs trust base64-encodes the DER certificate file and addresses the site by identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CertsTrust_Base64EncodesDerFile_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var derPath = Path.GetTempFileName();
|
|
var derBytes = new byte[] { 0x30, 0x82, 0x01, 0x0A, 0xDE, 0xAD, 0xBE, 0xEF };
|
|
await File.WriteAllBytesAsync(derPath, derBytes);
|
|
try
|
|
{
|
|
var exit = await root
|
|
.Parse(new[]
|
|
{
|
|
"data-connection", "certs", "trust", "--site", "SITE1", "--connection", "conn1",
|
|
"--der-file", derPath, "--thumbprint", "AABBCC"
|
|
})
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<TrustServerCertCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Equal("conn1", cmd.ConnectionName);
|
|
Assert.Equal("AABBCC", cmd.Thumbprint);
|
|
Assert.Equal(Convert.ToBase64String(derBytes), cmd.DerBase64);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(derPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>Verifies that certs remove builds a remove-server-cert command addressed by site identifier.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CertsRemove_BuildsRemoveServerCertCommand_WithSiteIdentifier()
|
|
{
|
|
var (root, captured) = BuildHarness();
|
|
|
|
var exit = await root
|
|
.Parse(new[]
|
|
{
|
|
"data-connection", "certs", "remove", "--site", "SITE1", "--connection", "conn1", "--thumbprint", "AABBCC"
|
|
})
|
|
.InvokeAsync();
|
|
|
|
Assert.Equal(0, exit);
|
|
var cmd = Assert.IsType<RemoveServerCertCommand>(Assert.Single(captured));
|
|
Assert.Equal("SITE1", cmd.SiteIdentifier);
|
|
Assert.Equal("AABBCC", cmd.Thumbprint);
|
|
}
|
|
}
|