Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/SessionCoverageTests.cs
T
romanett d98eb6b28c Fix server session defects found by a spec-checked review of CreateSession, ActivateSession, session lifecycle and continuation points (#4632)
# Description

Fixes defects found by a spec-checked review of the server session
implementation: CreateSession, ActivateSession, the session lifecycle
(per-request validation, CloseSession, Cancel, timeout, shutdown) and
session continuation points. Each finding was checked against OPC
10000-4 (§5.7, §7.9, §7.32), OPC 10000-5 (session diagnostics and audit
events) and OPC 10000-6 §6.8.2.

There is one commit per finding, and each commit message cites the spec
section. A max-effort code review of the fixes then found 15 more
issues, mostly regressions the first round introduced; they are fixed in
the `revfix` merges.

## What changed

**CreateSession**
- At MaxSessionCount the oldest session that was never activated is
closed to make room (§5.7.2.1). Sessions that are activated, already
closing, or have an activation in flight are never evicted. The victim
is claimed atomically, so a late activation commit on it is rejected.
- `requestedSessionTimeout` NaN is revised. A malformed `endpointUrl` no
longer fails the call with Bad_UnexpectedError. `serverUri` is ignored,
as §5.7.2.2 requires.
- Unnamed sessions get distinct names.
- Session diagnostics now report `MaxResponseMessageSize` and
`ServerUri`, and `SessionSecurityDiagnostics.Encoding` uses the
specified names ("UA Binary", "XML", "JSON").
- AuditCreateSessionEvent carries SecureChannelId and the request
certificate, also on failure.
- Every rejected CreateSession is counted, and only once. A rejection
during server startup no longer turns into Bad_UnexpectedError.
- If an `OnApplicationCertificateError` override accepts an error, the
certificate is now kept and the remaining checks still run. A
certificate accepted this way does not grant TrustedApplication or
application-based roles.

**ActivateSession**
- EphemeralKey handling follows OPC 10000-6 §6.8.2:
- A key used to decrypt a UserName or IssuedToken secret is retired and
replaced in the same commit.
  - A key that was not used is kept, and nothing is returned.
- ECDHPolicyUri is honoured; an unsupported policy gets
Bad_SecurityPolicyRejected.
  - Request parameters are no longer echoed back.
- After the activation commits, a failure or cancellation no longer
faults the response, which would hide the new server nonce. Post-commit
callbacks run uncancellably.
- An activation that overlaps a session close or timeout fails with
Bad_SessionClosed instead of returning Good.
- The PolicyId of an AnonymousIdentityToken is validated.
- Status codes now follow §5.7.3.3:
- Bad_ApplicationSignatureInvalid for a mismatched client-signature
algorithm
- Bad_UserSignatureInvalid for a mismatched X509 user-token signature
algorithm
- Bad_IdentityTokenInvalid for an undecodable token or an unknown policy
- `ClientUserIdOfSession` keeps the user of the first activation.

**Lifecycle and requests**
- Requests on an expired or closing session are rejected with
Bad_SessionClosed (§5.7.2.1). A CloseSession on an expired session that
is not yet closing is still processed as the client's close and honours
`deleteSubscriptions`.
- Closing a session aborts its outstanding requests with
Bad_SessionClosed. A request that registers after that abort pass is
rejected too.
- Cancel completes the matching requests with
Bad_RequestCancelledByClient (§5.7.5.1). It also reaches requests still
waiting in the request queue, but only when both timestamps prove the
request was sent earlier.
- There is one AuditCancelEvent per Cancel call, carrying the client's
audit entry id and user.
- Every close path goes through a single close claim, so
SessionAbortCount, SessionTimeoutCount and the "Session/Terminated" and
"Session/Timeout" audits are each counted once.
- Teardown that started cannot be cancelled. Requests rejected by
session validation are counted.

**Continuation points**
- HistoryRead returns Bad_NoContinuationPoints instead of evicting
points of the same request (§7.9). Only the request that saved a point
pins it, so points from prior requests can still be freed.
- Points saved by a HistoryRead that faults are released.
- Session-less Browse and BrowseNext no longer dereference a null
session.

## Behaviour changes reviewers should know about
- At the session cap, CreateSession evicts a non-activated session
instead of returning Bad_TooManySessions. The eviction is counted in
SessionAbortCount.
- Clients see Bad_SessionClosed for requests on expired or closing
sessions, and Bad_RequestCancelledByClient for cancelled requests.
- `ActivateSessionProcessAdditionalParameters` is now called on every
successful activation, with or without an AdditionalHeader.
- If an `OnApplicationCertificateError` override returns instead of
throwing, CreateSession now accepts the certificate.
- Timeout and termination audits are now raised after the session
teardown.
- The only public API changes are additive overloads in `AuditEvents`
and `RequestManager`.

**Known partial fixes:**
- The session's `maxResponseMessageSize` is reported but not enforced;
only the channel limit applies.
- With overlapping ActivateSession calls on one session, an earlier
response can still carry a key that a later activation has already
replaced. Closing that fully needs a new `ISession` member.

## Related Issues

- The Redundancy.Server session-mirroring findings from the same review
are tracked separately in #4627.

## Tests
- Regression tests cover each finding. The new files are:
  - `CreateSessionSpecRegressionTests`
  - `ActivateSessionValidationRegressionTests`
  - `StandardServerActivateSessionRegressionTests`
  - `SessionLifecycleRegressionTests`
  - `SessionRequestAdmissionRegressionTests`
  - `HistoryReadContinuationDispatchTests`
  
  Existing test classes were extended too.
- Local results on net10.0:
  - `Opc.Ua.Server.Tests`: 7883 passed / 0 failed / 5 skipped
- `Opc.Ua.Sessions.Tests`: 790 passed / 0 failed / 379 skipped (same as
master)
- net48 Release build of `Opc.Ua.Server.Tests`: 0 errors / 0 warnings.

## Checklist

- [x] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [ ] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 05:42:01 +02:00

1086 lines
46 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using NUnit.Framework;
using Opc.Ua.Security.Certificates;
using Opc.Ua.Tests;
using ServerSession = Opc.Ua.Server.Session;
namespace Opc.Ua.Server.Tests
{
/// <summary>
/// Direct-construction unit tests for <see cref="Session"/> that exercise the
/// branches reachable without a full secure-channel crypto handshake:
/// constructor guards, request validation, diagnostic counters, locale
/// updates, continuation points and the anonymous / username identity token
/// validation and activation paths under <see cref="MessageSecurityMode.None"/>.
/// </summary>
[TestFixture]
[Category("Session")]
[Parallelizable]
public class SessionCoverageTests
{
private static readonly ICertificateFactory s_factory = DefaultCertificateFactory.Instance;
private static readonly string[] s_preferredLocales = ["en-US", "de-DE"];
private static readonly string[] s_singlePreferredLocale = ["en-US"];
private ITelemetryContext m_telemetry = null!;
private Mock<IServerInternal> m_serverMock = null!;
private Certificate m_serverCertificate = null!;
[SetUp]
public void SetUp()
{
m_telemetry = NUnitTelemetryContext.Create();
m_serverMock = new Mock<IServerInternal>();
m_serverMock.Setup(s => s.Telemetry).Returns(m_telemetry);
m_serverMock.Setup(s => s.NamespaceUris).Returns(new NamespaceTable());
var sessionManagerMock = new Mock<ISessionManager>();
m_serverMock.Setup(s => s.SessionManager).Returns(sessionManagerMock.Object);
m_serverCertificate = s_factory.CreateCertificate("CN=SessionCoverageServer").CreateForRSA();
}
[TearDown]
public void TearDown()
{
m_serverCertificate?.Dispose();
}
private static EndpointDescription CreateEndpoint(
string securityPolicyUri = null!,
MessageSecurityMode securityMode = MessageSecurityMode.None,
UserTokenPolicy[]? tokens = null)
{
return new EndpointDescription
{
EndpointUrl = "opc.tcp://localhost:4840/Coverage",
SecurityMode = securityMode,
SecurityPolicyUri = securityPolicyUri ?? SecurityPolicies.None,
UserIdentityTokens = tokens != null ? new ArrayOf<UserTokenPolicy>(tokens) : default
};
}
private static OperationContext CreateContext(
EndpointDescription endpoint,
string channelId = "channel-1",
RequestType requestType = RequestType.ActivateSession)
{
var channelContext = new SecureChannelContext(channelId, endpoint, RequestEncoding.Binary);
var requestHeader = new RequestHeader();
return new OperationContext(requestHeader, channelContext, requestType, RequestLifetime.None);
}
private ServerSession CreateSession(
EndpointDescription endpoint,
string channelId = "channel-1",
Certificate? clientCertificate = null)
{
OperationContext context = CreateContext(endpoint, channelId);
return new ServerSession(
context,
m_serverMock.Object,
m_serverCertificate,
new NodeId(Guid.NewGuid()),
ByteString.Empty,
Nonce.CreateNonce(SecurityPolicies.None),
"CoverageSession",
new ApplicationDescription { ApplicationUri = "urn:coverage:client" },
endpoint.EndpointUrl!,
clientCertificate!,
[],
60_000,
10,
10);
}
[Test]
public void ConstructorWithNullContextThrows()
{
Assert.That(
() => new ServerSession(
null!,
m_serverMock.Object,
m_serverCertificate,
new NodeId(Guid.NewGuid()),
ByteString.Empty,
Nonce.CreateNonce(SecurityPolicies.None),
"s",
new ApplicationDescription(),
"opc.tcp://localhost:4840",
null!,
[],
60_000,
10,
10),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void ConstructorWithNullServerThrows()
{
EndpointDescription endpoint = CreateEndpoint();
OperationContext context = CreateContext(endpoint);
Assert.That(
() => new ServerSession(
context,
null!,
m_serverCertificate,
new NodeId(Guid.NewGuid()),
ByteString.Empty,
Nonce.CreateNonce(SecurityPolicies.None),
"s",
new ApplicationDescription(),
endpoint.EndpointUrl!,
null!,
[],
60_000,
10,
10),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void ConstructorInitializesDiagnostics()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(session.SecureChannelId, Is.EqualTo("channel-1"));
Assert.That(session.Activated, Is.False);
Assert.That(session.SessionDiagnostics, Is.Not.Null);
Assert.That(session.ReadDiagnostics(d => d.SessionName), Is.EqualTo("CoverageSession"));
Assert.That(session.Identity, Is.Not.Null);
}
[Test]
public void SetUserTokenSecurityPolicyThenGetNewEphemeralKeyReturnsNullForNoPolicy()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(session.GetNewEphemeralKey(), Is.Null);
}
[Test]
public void GetNewEphemeralKeyReturnsKeyWhenPolicySet()
{
using ServerSession session = CreateSession(CreateEndpoint());
session.SetUserTokenSecurityPolicy(SecurityPolicies.Basic256Sha256);
EphemeralKeyType? key = session.GetNewEphemeralKey();
Assert.That(key, Is.Not.Null);
Assert.That(key!.PublicKey.IsEmpty, Is.False);
}
[Test]
public void IsSecureChannelValidReflectsChannelId()
{
using ServerSession session = CreateSession(CreateEndpoint(), channelId: "abc");
Assert.That(session.IsSecureChannelValid("abc"), Is.True);
Assert.That(session.IsSecureChannelValid("other"), Is.False);
}
[Test]
public void UpdateLocaleIdsReturnsTrueOnChangeAndFalseOnRepeat()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(session.UpdateLocaleIds(new ArrayOf<string>(s_preferredLocales)), Is.True);
Assert.That(session.UpdateLocaleIds(new ArrayOf<string>(s_preferredLocales)), Is.False);
Assert.That(session.PreferredLocales, Is.EqualTo(s_preferredLocales));
}
[Test]
public void UpdateLocaleIdsDropsNullLocaleIds()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(session.UpdateLocaleIds(new ArrayOf<string>(new string[] { null!, "de-DE" })), Is.True);
Assert.That(session.PreferredLocales, Has.Length.EqualTo(1));
Assert.That(session.PreferredLocales[0], Is.EqualTo("de-DE"));
}
/// <summary>
/// SessionDiagnostics.LocaleIds reports the LocaleIds as specified by the client,
/// null entries included (Part 5 12.11); only PreferredLocales is filtered.
/// </summary>
[Test]
public void UpdateLocaleIdsKeepsClientLocaleIdsInDiagnostics()
{
using ServerSession session = CreateSession(CreateEndpoint());
string[] first = [null!, "de-DE"];
Assert.That(session.UpdateLocaleIds(new ArrayOf<string>(first)), Is.True);
string[] diagnostics = [.. session.SessionDiagnostics.LocaleIds];
Assert.That(diagnostics, Is.EqualTo(first));
// a change of the null entries alone still reaches the diagnostics.
string[] second = ["de-DE", null!];
Assert.That(session.UpdateLocaleIds(new ArrayOf<string>(second)), Is.False);
diagnostics = [.. session.SessionDiagnostics.LocaleIds];
Assert.That(diagnostics, Is.EqualTo(second));
Assert.That(session.PreferredLocales, Has.Length.EqualTo(1));
Assert.That(session.PreferredLocales[0], Is.EqualTo("de-DE"));
}
[Test]
public void ValidateRequestWithNullHeaderThrows()
{
using ServerSession session = CreateSession(CreateEndpoint());
var channelContext = new SecureChannelContext("channel-1", CreateEndpoint(), RequestEncoding.Binary);
Assert.That(
() => session.ValidateRequest(null!, channelContext, RequestType.Read),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void ValidateRequestWithNullChannelOnCloseThrowsSessionIdInvalid()
{
using ServerSession session = CreateSession(CreateEndpoint());
ServiceResultException? ex = Assert.Throws<ServiceResultException>(
() => session.ValidateRequest(new RequestHeader(), null!, RequestType.CloseSession));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSessionIdInvalid));
}
[Test]
public void ValidateRequestWithWrongChannelThrowsSecureChannelIdInvalid()
{
using ServerSession session = CreateSession(CreateEndpoint());
var channelContext = new SecureChannelContext("wrong-channel", CreateEndpoint(), RequestEncoding.Binary);
ServiceResultException? ex = Assert.Throws<ServiceResultException>(
() => session.ValidateRequest(new RequestHeader(), channelContext, RequestType.Read));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSecureChannelIdInvalid));
}
[Test]
public void ValidateRequestWhenNotActivatedThrowsSessionNotActivated()
{
using ServerSession session = CreateSession(CreateEndpoint());
var channelContext = new SecureChannelContext("channel-1", CreateEndpoint(), RequestEncoding.Binary);
ServiceResultException? ex = Assert.Throws<ServiceResultException>(
() => session.ValidateRequest(new RequestHeader(), channelContext, RequestType.Read));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSessionNotActivated));
}
[TestCase(RequestType.Read)]
[TestCase(RequestType.HistoryRead)]
[TestCase(RequestType.Write)]
[TestCase(RequestType.HistoryUpdate)]
[TestCase(RequestType.Call)]
[TestCase(RequestType.CreateMonitoredItems)]
[TestCase(RequestType.ModifyMonitoredItems)]
[TestCase(RequestType.SetMonitoringMode)]
[TestCase(RequestType.SetTriggering)]
[TestCase(RequestType.DeleteMonitoredItems)]
[TestCase(RequestType.CreateSubscription)]
[TestCase(RequestType.ModifySubscription)]
[TestCase(RequestType.SetPublishingMode)]
[TestCase(RequestType.Publish)]
[TestCase(RequestType.Republish)]
[TestCase(RequestType.TransferSubscriptions)]
[TestCase(RequestType.DeleteSubscriptions)]
[TestCase(RequestType.AddNodes)]
[TestCase(RequestType.AddReferences)]
[TestCase(RequestType.DeleteNodes)]
[TestCase(RequestType.DeleteReferences)]
[TestCase(RequestType.Browse)]
[TestCase(RequestType.BrowseNext)]
[TestCase(RequestType.TranslateBrowsePathsToNodeIds)]
[TestCase(RequestType.QueryFirst)]
[TestCase(RequestType.QueryNext)]
[TestCase(RequestType.RegisterNodes)]
[TestCase(RequestType.UnregisterNodes)]
public void ValidateRequestUpdatesDiagnosticCounterForRequestType(RequestType requestType)
{
using ServerSession session = CreateSession(CreateEndpoint());
var channelContext = new SecureChannelContext("channel-1", CreateEndpoint(), RequestEncoding.Binary);
// Non-activated session records the counter (error path) then throws.
Assert.That(
() => session.ValidateRequest(new RequestHeader(), channelContext, requestType),
Throws.TypeOf<ServiceResultException>());
Assert.That(session.ReadDiagnostics(d => d.TotalRequestCount.TotalCount), Is.GreaterThan(0));
}
[Test]
public void ValidateRequestWithUnknownRequestTypeThrowsUnexpected()
{
using ServerSession session = CreateSession(CreateEndpoint());
var channelContext = new SecureChannelContext("channel-1", CreateEndpoint(), RequestEncoding.Binary);
Assert.That(
() => session.ValidateRequest(new RequestHeader(), channelContext, (RequestType)9999),
Throws.TypeOf<ServiceResultException>());
}
[Test]
public void ValidateDiagnosticInfoWithoutAdditionalInfoMaskDoesNothing()
{
using ServerSession session = CreateSession(CreateEndpoint());
var header = new RequestHeader { ReturnDiagnostics = 0 };
Assert.That(() => session.ValidateDiagnosticInfo(header), Throws.Nothing);
Assert.That(header.ReturnDiagnostics, Is.Zero);
}
[Test]
public void SaveAndRestoreContinuationPointRoundTrips()
{
using ServerSession session = CreateSession(CreateEndpoint());
var id = Guid.NewGuid();
var cp = new ContinuationPoint { Id = id };
session.ContinuationPoints.SaveBrowse(cp);
ContinuationPoint? restored = session.ContinuationPoints.RestoreBrowse(
new ByteString(id.ToByteArray()));
Assert.That(restored, Is.Not.Null);
Assert.That(restored!.Id, Is.EqualTo(id));
restored.Dispose();
}
[Test]
public void SaveContinuationPointWithNullThrows()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(
() => session.ContinuationPoints.SaveBrowse(null!),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void RestoreContinuationPointReturnsNullForUnknownAndBadLength()
{
using ServerSession session = CreateSession(CreateEndpoint());
session.ContinuationPoints.SaveBrowse(new ContinuationPoint { Id = Guid.NewGuid() });
Assert.That(session.ContinuationPoints.RestoreBrowse(new ByteString(new byte[] { 1, 2, 3 })), Is.Null);
Assert.That(session.ContinuationPoints.RestoreBrowse(new ByteString(Guid.NewGuid().ToByteArray())), Is.Null);
}
[Test]
public void SaveContinuationPointEvictsOldestWhenOverCapacity()
{
using ServerSession session = CreateSession(CreateEndpoint());
// Capacity is 10 (maxBrowseContinuationPoints); adding 12 evicts the oldest.
var first = new ContinuationPoint { Id = Guid.NewGuid() };
session.ContinuationPoints.SaveBrowse(first);
for (int i = 0; i < 11; i++)
{
session.ContinuationPoints.SaveBrowse(new ContinuationPoint { Id = Guid.NewGuid() });
}
Assert.That(
session.ContinuationPoints.RestoreBrowse(new ByteString(first.Id.ToByteArray())),
Is.Null,
"The oldest continuation point should have been evicted.");
}
[Test]
public void ClientApplicationUriReadsTheDiagnosticsUnderTheLock()
{
using ServerSession session = CreateSession(CreateEndpoint());
// The four server-side callers that used to reach through SessionDiagnostics
// want exactly this, and it is read under the diagnostics lock rather than off
// a handed-out structure.
Assert.That(
session.ClientApplicationUri,
Is.EqualTo(session.ReadDiagnostics(d => d.ClientDescription?.ApplicationUri)));
}
[Test]
public void SessionNameDoesNotDependOnTheDiagnostics()
{
using ServerSession session = CreateSession(CreateEndpoint());
// Assigned once during construction, so it is read from the field: a caller
// must not have to take the diagnostics lock for a value that never changes.
Assert.That(
session.SessionName,
Is.EqualTo(session.ReadDiagnostics(d => d.SessionName)));
}
[Test]
public void SaveAndRestoreHistoryContinuationPointRoundTrips()
{
using ServerSession session = CreateSession(CreateEndpoint());
var id = Guid.NewGuid();
var payload = new TestHistoryContinuationPoint(id);
session.ContinuationPoints.SaveHistory(payload);
IHistoryContinuationPoint? restored = session.ContinuationPoints.RestoreHistory(new ByteString(id.ToByteArray()));
Assert.That(restored, Is.SameAs(payload));
}
[Test]
public void SaveHistoryContinuationPointWithNullThrows()
{
using ServerSession session = CreateSession(CreateEndpoint());
Assert.That(
() => session.ContinuationPoints.SaveHistory(null!),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void RestoreHistoryContinuationPointReturnsNullForBadLength()
{
using ServerSession session = CreateSession(CreateEndpoint());
session.ContinuationPoints.SaveHistory(new TestHistoryContinuationPoint());
Assert.That(session.ContinuationPoints.RestoreHistory(new ByteString(new byte[] { 1, 2 })), Is.Null);
}
[Test]
public void ConstructorWithNullChannelContextThrows()
{
var requestHeader = new RequestHeader();
var context = new OperationContext(
requestHeader, null, RequestType.ActivateSession, RequestLifetime.None);
ServiceResultException? ex = Assert.Throws<ServiceResultException>(
() => new ServerSession(
context,
m_serverMock.Object,
m_serverCertificate,
new NodeId(Guid.NewGuid()),
ByteString.Empty,
Nonce.CreateNonce(SecurityPolicies.None),
"s",
new ApplicationDescription(),
"opc.tcp://localhost:4840",
null!,
[],
60_000,
10,
10));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSecureChannelIdInvalid));
}
[Test]
public async Task ValidateDiagnosticInfoGrantsUserPermissionInfoForSecurityAdminAsync()
{
UserTokenPolicy[] tokens =
[
new UserTokenPolicy { PolicyId = "anon", TokenType = UserTokenType.Anonymous }
];
EndpointDescription endpoint = CreateEndpoint(tokens: tokens);
using ServerSession session = CreateSession(endpoint, channelId: "channel-1");
OperationContext context = CreateContext(endpoint, channelId: "channel-1");
(IUserIdentityTokenHandler handler, UserTokenPolicy? _) = await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false);
var effectiveIdentity = new Mock<IUserIdentity>();
effectiveIdentity.Setup(i => i.TokenType).Returns(UserTokenType.Anonymous);
effectiveIdentity.Setup(i => i.DisplayName).Returns("admin");
effectiveIdentity
.Setup(i => i.GrantedRoleIds)
.Returns(new ArrayOf<NodeId>(new[] { ObjectIds.WellKnownRole_SecurityAdmin }));
session.Activate(
context,
handler!,
new UserIdentity(),
effectiveIdentity.Object,
default,
Nonce.CreateNonce(SecurityPolicies.None));
const uint mask = (uint)DiagnosticsMasks.ServiceAdditionalInfo;
var header = new RequestHeader { ReturnDiagnostics = mask };
session.ValidateDiagnosticInfo(header);
Assert.That(
header.ReturnDiagnostics & (uint)DiagnosticsMasks.UserPermissionAdditionalInfo,
Is.Not.Zero);
}
[Test]
public void ValidateBeforeActivateWithClientCertificateAndEmptySignatureThrows()
{
using Certificate clientCertificate =
s_factory.CreateCertificate("CN=CoverageClient").CreateForRSA();
EndpointDescription endpoint = CreateEndpoint(
SecurityPolicies.Basic256Sha256, MessageSecurityMode.Sign);
using ServerSession session = CreateSession(
endpoint, channelId: "channel-1", clientCertificate: clientCertificate);
OperationContext context = CreateContext(endpoint, channelId: "channel-1");
ServiceResultException? ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadApplicationSignatureInvalid));
}
[Test]
public void SaveHistoryContinuationPointEvictsOldestWhenOverCapacity()
{
using ServerSession session = CreateSession(CreateEndpoint());
var first = Guid.NewGuid();
session.ContinuationPoints.SaveHistory(new TestHistoryContinuationPoint(first));
for (int i = 0; i < 11; i++)
{
session.ContinuationPoints.SaveHistory(new TestHistoryContinuationPoint());
}
Assert.That(
session.ContinuationPoints.RestoreHistory(new ByteString(first.ToByteArray())),
Is.Null,
"The oldest history continuation point should have been evicted.");
}
[Test]
public void ValidateBeforeActivateWithSecurityPolicyMismatchThrows()
{
using ServerSession session = CreateSession(CreateEndpoint(SecurityPolicies.None));
OperationContext context = CreateContext(
CreateEndpoint(SecurityPolicies.Basic256Sha256, MessageSecurityMode.Sign));
ServiceResultException? ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSecurityPolicyRejected));
}
/// <summary>
/// PR review 4166517637: a stale prepared replacement nonce that the
/// activation detaches is disposed even when the channel check throws.
/// </summary>
[Test]
public void ValidateBeforeActivateFailureDisposesStalePreparedNonce()
{
if (SecurityPolicies.Default.GetInfo(SecurityPolicies.ECC_nistP256) == null)
{
Assert.Ignore("ECC_nistP256 is not supported on this platform.");
}
using ServerSession session = CreateSession(CreateEndpoint(SecurityPolicies.None));
OperationContext context = CreateContext(
CreateEndpoint(SecurityPolicies.Basic256Sha256, MessageSecurityMode.Sign));
Nonce stale = Nonce.CreateNonce(SecurityPolicies.ECC_nistP256);
System.Reflection.FieldInfo ecdhField = typeof(Nonce).GetField(
"m_ecdh",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!;
Assert.That(ecdhField.GetValue(stale), Is.Not.Null);
typeof(ServerSession).GetField(
"m_preparedUserTokenNonce",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!
.SetValue(session, stale);
ServiceResultException? ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSecurityPolicyRejected));
Assert.That(ecdhField.GetValue(stale), Is.Null, "The stale prepared nonce was not disposed.");
}
[Test]
public void ValidateBeforeActivateWithWrongChannelThrows()
{
using ServerSession session = CreateSession(CreateEndpoint(), channelId: "channel-1");
OperationContext context = CreateContext(CreateEndpoint(), channelId: "other-channel");
ServiceResultException? ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadSecureChannelIdInvalid));
}
[Test]
public async Task ValidateBeforeActivateWithAnonymousTokenSucceedsAsync()
{
var tokens = new UserTokenPolicy[]
{
new() { PolicyId = "anon", TokenType = UserTokenType.Anonymous }
};
EndpointDescription endpoint = CreateEndpoint(tokens: tokens);
using ServerSession session = CreateSession(endpoint);
OperationContext context = CreateContext(endpoint);
(IUserIdentityTokenHandler handler, UserTokenPolicy? policy) =
await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false);
Assert.That(handler, Is.Not.Null);
Assert.That(policy, Is.Not.Null);
Assert.That(policy!.TokenType, Is.EqualTo(UserTokenType.Anonymous));
}
[Test]
public void ValidateBeforeActivateAnonymousNotSupportedThrows()
{
var tokens = new UserTokenPolicy[]
{
new() { PolicyId = "user", TokenType = UserTokenType.UserName }
};
EndpointDescription endpoint = CreateEndpoint(tokens: tokens);
using ServerSession session = CreateSession(endpoint);
OperationContext context = CreateContext(endpoint);
ServiceResultException? ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false));
Assert.That(ex!.StatusCode, Is.EqualTo(StatusCodes.BadIdentityTokenRejected));
}
[Test]
public async Task ValidateBeforeActivateWithUserNameTokenResolvesPolicyAsync()
{
var tokens = new UserTokenPolicy[]
{
new() {
PolicyId = "user",
TokenType = UserTokenType.UserName,
SecurityPolicyUri = SecurityPolicies.None
}
};
EndpointDescription endpoint = CreateEndpoint(tokens: tokens);
using ServerSession session = CreateSession(endpoint);
OperationContext context = CreateContext(endpoint);
var userToken = new UserNameIdentityToken
{
PolicyId = "user",
UserName = "bob",
Password = new ByteString(new byte[] { 1, 2, 3 })
};
(IUserIdentityTokenHandler handler, UserTokenPolicy? policy) =
await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
new ExtensionObject(userToken),
new SignatureData(),
CancellationToken.None).ConfigureAwait(false);
Assert.That(handler, Is.Not.Null);
Assert.That(policy, Is.Not.Null);
Assert.That(policy!.TokenType, Is.EqualTo(UserTokenType.UserName));
}
[Test]
public async Task ActivateFirstThenReactivateSwitchesChannelAsync()
{
var tokens = new UserTokenPolicy[]
{
new() { PolicyId = "anon", TokenType = UserTokenType.Anonymous }
};
EndpointDescription endpoint = CreateEndpoint(tokens: tokens);
using ServerSession session = CreateSession(endpoint, channelId: "channel-1");
OperationContext context = CreateContext(endpoint, channelId: "channel-1");
(IUserIdentityTokenHandler handler, UserTokenPolicy? _) = await session.ValidateBeforeActivateAsync(
context,
new SignatureData(),
default,
new SignatureData(),
CancellationToken.None).ConfigureAwait(false);
bool changed = session.Activate(
context,
handler!,
new UserIdentity(),
new UserIdentity(),
new ArrayOf<string>(s_singlePreferredLocale),
Nonce.CreateNonce(SecurityPolicies.None));
Assert.That(session.Activated, Is.True);
Assert.That(changed, Is.True);
// Re-activate on a different channel exercises the RE-ACTIVATION branch.
OperationContext reactivateContext = CreateContext(endpoint, channelId: "channel-2");
session.Activate(
reactivateContext,
handler!,
new UserIdentity(),
new UserIdentity(),
new ArrayOf<string>(s_singlePreferredLocale),
Nonce.CreateNonce(SecurityPolicies.None));
Assert.That(session.SecureChannelId, Is.EqualTo("channel-2"));
}
[Test]
public void ProcessCreateSessionAdditionalParametersReturnsNullForNullInput()
{
AdditionalParametersType? result =
SessionSecurityPolicyHelper.ProcessCreateSessionAdditionalParameters(
Mock.Of<ISession>(),
null!,
NullLogger<SessionCoverageTests>.Instance);
Assert.That(result, Is.Null);
}
[Test]
public void ProcessCreateSessionAdditionalParametersPreservesUnknownParameters()
{
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = new QualifiedName("Unknown"),
Value = Variant.From("value")
}
]
};
AdditionalParametersType? result =
SessionSecurityPolicyHelper.ProcessCreateSessionAdditionalParameters(
Mock.Of<ISession>(),
input,
NullLogger<SessionCoverageTests>.Instance);
Assert.That(result, Is.Not.Null);
Assert.That(result!.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(new QualifiedName("Unknown")));
Assert.That(result.Parameters[0].Value.TryGetValue(out string value), Is.True);
Assert.That(value, Is.EqualTo("value"));
}
[Test]
public void ProcessCreateSessionAdditionalParametersRejectsUnsupportedEcdhPolicy()
{
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = QualifiedName.From(AdditionalParameterNames.ECDHPolicyUri),
Value = Variant.From(SecurityPolicies.None)
}
]
};
AdditionalParametersType? result =
SessionSecurityPolicyHelper.ProcessCreateSessionAdditionalParameters(
Mock.Of<ISession>(),
input,
NullLogger<SessionCoverageTests>.Instance);
Assert.That(result, Is.Not.Null);
Assert.That(result!.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Assert.That(result.Parameters[0].Value.TryGetValue(out StatusCode status), Is.True);
Assert.That(status.Code, Is.EqualTo(StatusCodes.BadSecurityPolicyRejected));
}
[Test]
public void ProcessCreateSessionAdditionalParametersCreatesEcdhKeyForSupportedPolicy()
{
var key = new EphemeralKeyType();
const string policyUri = SecurityPolicies.ECC_nistP256;
var policies = new Mock<ISecurityPolicyRegistry>();
policies.Setup(p => p.GetInfo(policyUri)).Returns(SecurityPolicyInfo.ECC_nistP256);
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns(key);
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = QualifiedName.From(AdditionalParameterNames.ECDHPolicyUri),
Value = Variant.From(policyUri)
}
]
};
AdditionalParametersType? result =
SessionSecurityPolicyHelper.ProcessCreateSessionAdditionalParameters(
session.Object,
input,
NullLogger<SessionCoverageTests>.Instance,
policies.Object);
Assert.That(result, Is.Not.Null);
Assert.That(result!.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Variant value = result.Parameters[0].Value;
Assert.That(value.TypeInfo, Is.EqualTo(TypeInfo.Scalars.ExtensionObject));
Assert.That(
value.TryGetStructure<EphemeralKeyType>(out EphemeralKeyType? actualKey),
Is.True);
Assert.That(actualKey, Is.SameAs(key));
session.Verify(s => s.SetUserTokenSecurityPolicy(policyUri), Times.Once);
session.Verify(s => s.GetNewEphemeralKey(), Times.Once);
}
[Test]
public void ProcessCreateSessionAdditionalParametersHonorsRawEcdhPlatformSupport()
{
const string policyUri = SecurityPolicies.ECC_nistP256;
var key = new EphemeralKeyType();
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns(key);
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = QualifiedName.From(AdditionalParameterNames.ECDHPolicyUri),
Value = Variant.From(policyUri)
}
]
};
AdditionalParametersType? result =
SessionSecurityPolicyHelper.ProcessCreateSessionAdditionalParameters(
session.Object,
input,
NullLogger<SessionCoverageTests>.Instance);
Assert.That(result, Is.Not.Null);
Assert.That(result!.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Variant value = result.Parameters[0].Value;
#if NET8_0_OR_GREATER
Assert.That(SecurityPolicies.Default.GetInfo(policyUri), Is.Not.Null);
Assert.That(value.TypeInfo, Is.EqualTo(TypeInfo.Scalars.ExtensionObject));
Assert.That(value.TryGetStructure<EphemeralKeyType>(out EphemeralKeyType? actualKey), Is.True);
Assert.That(actualKey, Is.SameAs(key));
session.Verify(s => s.SetUserTokenSecurityPolicy(policyUri), Times.Once);
session.Verify(s => s.GetNewEphemeralKey(), Times.Once);
#else
Assert.That(SecurityPolicies.Default.GetInfo(policyUri), Is.Null);
Assert.That(value.TypeInfo, Is.EqualTo(TypeInfo.Scalars.StatusCode));
Assert.That(value.TryGetValue(out StatusCode status), Is.True);
Assert.That(status.Code, Is.EqualTo(StatusCodes.BadSecurityPolicyRejected));
session.Verify(s => s.SetUserTokenSecurityPolicy(It.IsAny<string>()), Times.Never);
session.Verify(s => s.GetNewEphemeralKey(), Times.Never);
#endif
}
/// <summary>
/// The response carries only the parameters the server produces; request
/// parameters such as "Existing" are not echoed back (OPC 10000-6 6.8.2).
/// </summary>
[Test]
public void ProcessActivateSessionAdditionalParametersReturnsOnlyTheNewKey()
{
var key = new EphemeralKeyType();
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns(key);
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = new QualifiedName("Existing"),
Value = Variant.From(1)
}
]
};
AdditionalParametersType result =
SessionSecurityPolicyHelper.ProcessActivateSessionAdditionalParameters(
session.Object,
input);
Assert.That(result, Is.Not.SameAs(input));
Assert.That(result.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Variant value = result.Parameters[0].Value;
Assert.That(value.TypeInfo, Is.EqualTo(TypeInfo.Scalars.ExtensionObject));
Assert.That(
value.TryGetStructure<EphemeralKeyType>(out EphemeralKeyType? actualKey),
Is.True);
Assert.That(actualKey, Is.SameAs(key));
session.Verify(s => s.GetNewEphemeralKey(), Times.Once);
}
[Test]
public void ProcessActivateSessionAdditionalParametersReturnsNoParametersWhenNoKeyAvailable()
{
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns((EphemeralKeyType?)null);
var input = new AdditionalParametersType();
AdditionalParametersType result =
SessionSecurityPolicyHelper.ProcessActivateSessionAdditionalParameters(
session.Object,
input);
Assert.That(result.Parameters.IsEmpty, Is.True);
}
/// <summary>
/// A client that omits the additional header still receives a new key once
/// the session uses an ECC user token policy (OPC 10000-6 6.8.2).
/// </summary>
[Test]
public void ProcessActivateSessionAdditionalParametersRotatesKeyWithoutRequestParameters()
{
var key = new EphemeralKeyType();
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns(key);
AdditionalParametersType result =
SessionSecurityPolicyHelper.ProcessActivateSessionAdditionalParameters(
session.Object,
null);
Assert.That(result.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Assert.That(
result.Parameters[0].Value.TryGetStructure<EphemeralKeyType>(out EphemeralKeyType? actualKey),
Is.True);
Assert.That(actualKey, Is.SameAs(key));
}
/// <summary>
/// A valid ECDHPolicyUri in ActivateSession selects the policy of the returned
/// key (OPC 10000-6 6.8.2).
/// </summary>
[Test]
public void ProcessActivateSessionAdditionalParametersHonorsEcdhPolicyUri()
{
const string policyUri = SecurityPolicies.ECC_nistP256;
var key = new EphemeralKeyType();
var policies = new Mock<ISecurityPolicyRegistry>();
policies.Setup(p => p.GetInfo(policyUri)).Returns(SecurityPolicyInfo.ECC_nistP256);
var session = new Mock<ISession>();
session.Setup(s => s.GetNewEphemeralKey()).Returns(key);
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = QualifiedName.From(AdditionalParameterNames.ECDHPolicyUri),
Value = Variant.From(policyUri)
}
]
};
AdditionalParametersType result =
SessionSecurityPolicyHelper.ProcessActivateSessionAdditionalParameters(
session.Object,
input,
NullLogger<SessionCoverageTests>.Instance,
policies.Object);
Assert.That(result.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Assert.That(
result.Parameters[0].Value.TryGetStructure<EphemeralKeyType>(out EphemeralKeyType? actualKey),
Is.True);
Assert.That(actualKey, Is.SameAs(key));
session.Verify(s => s.SetUserTokenSecurityPolicy(policyUri), Times.Once);
session.Verify(s => s.GetNewEphemeralKey(), Times.Once);
}
/// <summary>
/// An unsupported ECDHPolicyUri in ActivateSession is answered with
/// Bad_SecurityPolicyRejected and leaves the session's policy alone.
/// </summary>
[Test]
public void ProcessActivateSessionAdditionalParametersRejectsUnsupportedEcdhPolicy()
{
var session = new Mock<ISession>();
var input = new AdditionalParametersType
{
Parameters =
[
new KeyValuePair
{
Key = QualifiedName.From(AdditionalParameterNames.ECDHPolicyUri),
Value = Variant.From(SecurityPolicies.None)
}
]
};
AdditionalParametersType result =
SessionSecurityPolicyHelper.ProcessActivateSessionAdditionalParameters(
session.Object,
input,
NullLogger<SessionCoverageTests>.Instance);
Assert.That(result.Parameters, Has.Count.EqualTo(1));
Assert.That(result.Parameters[0].Key, Is.EqualTo(QualifiedName.From(AdditionalParameterNames.ECDHKey)));
Assert.That(result.Parameters[0].Value.TryGetValue(out StatusCode status), Is.True);
Assert.That(status.Code, Is.EqualTo(StatusCodes.BadSecurityPolicyRejected));
session.Verify(s => s.SetUserTokenSecurityPolicy(It.IsAny<string>()), Times.Never);
session.Verify(s => s.GetNewEphemeralKey(), Times.Never);
}
}
}