test: verify SDK 2 driver boundaries and lifecycle contracts
This commit is contained in:
@@ -65,4 +65,49 @@ public sealed class OpcUaClientCertAuthTests
|
||||
try { File.Delete(tmpPath); } catch { /* best-effort */ }
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public async Task Certificate_identity_signs_repeatedly_and_requires_reopen_after_file_rotation()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"opcua-cert-rotation-{Guid.NewGuid():N}.pfx");
|
||||
try
|
||||
{
|
||||
using var key = RSA.Create(2048);
|
||||
var request = new CertificateRequest("CN=OpcUaSdk2Signing", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
using var original = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-5), DateTimeOffset.UtcNow.AddHours(1));
|
||||
File.WriteAllBytes(path, original.Export(X509ContentType.Pfx, "test-password"));
|
||||
var options = new OpcUaClientDriverOptions { AuthType = OpcUaAuthType.Certificate,
|
||||
UserCertificatePath = path, UserCertificatePassword = "test-password" };
|
||||
var identity = OpcUaClientDriver.BuildCertificateIdentity(options);
|
||||
byte[] challenge = [1, 2, 3, 4];
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
var signature = await identity.TokenHandler.SignAsync(challenge, Opc.Ua.SecurityPolicies.Basic256Sha256,
|
||||
TestContext.Current.CancellationToken);
|
||||
using var publicKey = original.GetRSAPublicKey();
|
||||
publicKey.ShouldNotBeNull().VerifyData(challenge, signature.Signature.Span,
|
||||
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1).ShouldBeTrue();
|
||||
}
|
||||
using var cancelled = new CancellationTokenSource();
|
||||
cancelled.Cancel();
|
||||
await Should.ThrowAsync<OperationCanceledException>(async () =>
|
||||
await identity.TokenHandler.SignAsync(challenge, Opc.Ua.SecurityPolicies.Basic256Sha256, cancelled.Token));
|
||||
|
||||
using var rotatedKey = RSA.Create(2048);
|
||||
var rotatedRequest = new CertificateRequest("CN=OpcUaSdk2Signing", rotatedKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
using var rotated = rotatedRequest.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-5), DateTimeOffset.UtcNow.AddHours(1));
|
||||
File.WriteAllBytes(path, rotated.Export(X509ContentType.Pfx, "test-password"));
|
||||
var error = await Should.ThrowAsync<Opc.Ua.ServiceResultException>(async () =>
|
||||
await identity.TokenHandler.SignAsync(challenge, Opc.Ua.SecurityPolicies.Basic256Sha256,
|
||||
TestContext.Current.CancellationToken));
|
||||
error.StatusCode.ShouldBe(Opc.Ua.StatusCodes.BadIdentityTokenInvalid);
|
||||
var fresh = OpcUaClientDriver.BuildCertificateIdentity(options);
|
||||
var freshSignature = await fresh.TokenHandler.SignAsync(challenge, Opc.Ua.SecurityPolicies.Basic256Sha256,
|
||||
TestContext.Current.CancellationToken);
|
||||
using var rotatedPublicKey = rotated.GetRSAPublicKey();
|
||||
rotatedPublicKey.ShouldNotBeNull().VerifyData(challenge, freshSignature.Signature.Span,
|
||||
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1).ShouldBeTrue();
|
||||
}
|
||||
finally { File.Delete(path); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user