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.
224 lines
8.6 KiB
C#
224 lines
8.6 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Validators;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Validators;
|
|
|
|
public class OpcUaEndpointConfigValidatorTests
|
|
{
|
|
private static OpcUaEndpointConfig Valid() => new()
|
|
{
|
|
EndpointUrl = "opc.tcp://plant-a:4840",
|
|
// Defaults satisfy the spec: Lifetime(30) >= 3 * KeepAlive(10).
|
|
};
|
|
|
|
/// <summary>Verifies that the default settings with a valid endpoint URL validate successfully.</summary>
|
|
[Fact]
|
|
public void Validate_DefaultsWithValidUrl_IsValid()
|
|
{
|
|
var result = OpcUaEndpointConfigValidator.Validate(Valid());
|
|
Assert.True(result.IsValid);
|
|
Assert.Empty(result.Errors);
|
|
}
|
|
|
|
/// <summary>Verifies that a missing endpoint URL fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_MissingEndpointUrl_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.EndpointUrl = "";
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.False(r.IsValid);
|
|
Assert.Contains(r.Errors, e =>
|
|
e.EntityName == "EndpointUrl"
|
|
&& e.Category == ValidationCategory.ConnectionConfig
|
|
&& e.Message.Contains("required", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>Verifies that a malformed or non-OPC UA endpoint URL fails validation.</summary>
|
|
/// <param name="url">The invalid endpoint URL to validate.</param>
|
|
[Theory]
|
|
[InlineData("http://x")]
|
|
[InlineData("opc.tcp://")]
|
|
[InlineData("not a url")]
|
|
public void Validate_BadEndpointUrl_Fails(string url)
|
|
{
|
|
var c = Valid();
|
|
c.EndpointUrl = url;
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.False(r.IsValid);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "EndpointUrl");
|
|
}
|
|
|
|
/// <summary>Verifies that a lifetime count below three times the keep-alive count fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_LifetimeLessThanThreeTimesKeepAlive_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.KeepAliveCount = 10;
|
|
c.LifetimeCount = 29; // 3*10 = 30; 29 < 30 → invalid
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.False(r.IsValid);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "LifetimeCount");
|
|
}
|
|
|
|
/// <summary>Verifies that a non-positive timing value fails validation.</summary>
|
|
/// <param name="field">The name of the timing field set to a non-positive value.</param>
|
|
[Theory]
|
|
[InlineData(nameof(OpcUaEndpointConfig.SessionTimeoutMs))]
|
|
[InlineData(nameof(OpcUaEndpointConfig.OperationTimeoutMs))]
|
|
[InlineData(nameof(OpcUaEndpointConfig.PublishingIntervalMs))]
|
|
[InlineData(nameof(OpcUaEndpointConfig.SamplingIntervalMs))]
|
|
public void Validate_NonPositiveTiming_Fails(string field)
|
|
{
|
|
var c = Valid();
|
|
typeof(OpcUaEndpointConfig).GetProperty(field)!.SetValue(c, 0);
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.False(r.IsValid);
|
|
Assert.Contains(r.Errors, e => e.EntityName == field);
|
|
}
|
|
|
|
/// <summary>Verifies that a queue size of zero fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_QueueSizeZero_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.QueueSize = 0;
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "QueueSize");
|
|
}
|
|
|
|
/// <summary>Verifies that enabling the heartbeat without a tag path fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_HeartbeatEnabledNoTagPath_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.Heartbeat = new OpcUaHeartbeatConfig { TagPath = "", MaxSilenceSeconds = 30 };
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Heartbeat.TagPath");
|
|
}
|
|
|
|
/// <summary>Verifies that a non-positive heartbeat silence threshold fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_HeartbeatNonPositiveSilence_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.Heartbeat = new OpcUaHeartbeatConfig { TagPath = "Hb", MaxSilenceSeconds = 0 };
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Heartbeat.MaxSilenceSeconds");
|
|
}
|
|
|
|
/// <summary>Verifies that the field prefix is applied to every validation error.</summary>
|
|
[Fact]
|
|
public void Validate_FieldPrefix_AppliedToEveryError()
|
|
{
|
|
var c = Valid();
|
|
c.EndpointUrl = "";
|
|
c.QueueSize = 0;
|
|
var r = OpcUaEndpointConfigValidator.Validate(c, fieldPrefix: "Primary.");
|
|
Assert.All(r.Errors, e => Assert.StartsWith("Primary.", e.EntityName!));
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Primary.EndpointUrl");
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Primary.QueueSize");
|
|
}
|
|
|
|
// ── Layer B extensions: auth, deadband, subscription display name ──
|
|
|
|
/// <summary>Verifies that an empty subscription display name fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_EmptySubscriptionDisplayName_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.SubscriptionDisplayName = "";
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "SubscriptionDisplayName");
|
|
}
|
|
|
|
/// <summary>Verifies that an anonymous user identity requires no additional fields.</summary>
|
|
[Fact]
|
|
public void Validate_UserIdentityAnonymous_NoExtraFieldsRequired()
|
|
{
|
|
var c = Valid();
|
|
c.UserIdentity = new OpcUaUserIdentityConfig { TokenType = OpcUaUserTokenType.Anonymous };
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.True(r.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that a username/password identity without a username fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_UsernamePasswordWithoutUsername_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.UserIdentity = new OpcUaUserIdentityConfig
|
|
{
|
|
TokenType = OpcUaUserTokenType.UsernamePassword,
|
|
Username = "",
|
|
Password = "secret"
|
|
};
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "UserIdentity.Username");
|
|
}
|
|
|
|
/// <summary>Verifies that an X.509 identity without a certificate path fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_X509WithoutCertificatePath_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.UserIdentity = new OpcUaUserIdentityConfig
|
|
{
|
|
TokenType = OpcUaUserTokenType.X509Certificate,
|
|
CertificatePath = ""
|
|
};
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "UserIdentity.CertificatePath");
|
|
}
|
|
|
|
/// <summary>Verifies that a username/password identity with a username passes validation.</summary>
|
|
[Fact]
|
|
public void Validate_UsernamePasswordWithUsername_Passes()
|
|
{
|
|
var c = Valid();
|
|
c.UserIdentity = new OpcUaUserIdentityConfig
|
|
{
|
|
TokenType = OpcUaUserTokenType.UsernamePassword,
|
|
Username = "alice",
|
|
Password = ""
|
|
};
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.True(r.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that a deadband with a non-positive value fails validation.</summary>
|
|
[Fact]
|
|
public void Validate_DeadbandWithNonPositiveValue_Fails()
|
|
{
|
|
var c = Valid();
|
|
c.Deadband = new OpcUaDeadbandConfig { Type = OpcUaDeadbandType.Absolute, Value = 0 };
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Deadband.Value");
|
|
}
|
|
|
|
/// <summary>Verifies that a deadband with a positive value passes validation.</summary>
|
|
[Fact]
|
|
public void Validate_DeadbandWithPositiveValue_Passes()
|
|
{
|
|
var c = Valid();
|
|
c.Deadband = new OpcUaDeadbandConfig { Type = OpcUaDeadbandType.Percent, Value = 1.5 };
|
|
var r = OpcUaEndpointConfigValidator.Validate(c);
|
|
Assert.True(r.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that user identity errors carry the field prefix.</summary>
|
|
[Fact]
|
|
public void Validate_UserIdentityErrorsPrefixed_WithFieldPrefix()
|
|
{
|
|
var c = Valid();
|
|
c.UserIdentity = new OpcUaUserIdentityConfig
|
|
{
|
|
TokenType = OpcUaUserTokenType.UsernamePassword,
|
|
Username = ""
|
|
};
|
|
var r = OpcUaEndpointConfigValidator.Validate(c, fieldPrefix: "Primary.");
|
|
Assert.Contains(r.Errors, e => e.EntityName == "Primary.UserIdentity.Username");
|
|
}
|
|
}
|