mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-18 14:43:08 +08:00
2a619cdf05
# Description Two client fixes, one commit each. **1. `CoreClientUtils.SelectEndpoint` skips OpenAPI endpoints.** - An HTTPS listener with the REST binding announces its OpenAPI endpoint with the same URL and message mode as its binary mode-None endpoint. `SelectEndpoint` matched on the URL scheme and the message mode only, so the first of the two in the GetEndpoints result won, in the main pass and in the HTTPS fallback for `useSecurity`. - The OpenAPI mapping (OPC 10000-6 §G.3) is the REST binding, not the binary transport the caller connects with. With the default channel bindings no channel factory is registered for the OpenAPI profile (`ClientChannelManager` maps it to `opc.https+webapi`), so a Session cannot be created on it; with the REST channel registered, the Session silently runs over REST. - Following OPC 10000-4 §5.5.4 (select by `TransportProfileUri`), both passes now skip endpoints with the HTTPS or WSS OpenAPI transport profile. Endpoints without a `TransportProfileUri` are selected as before. **2. A closed Session does not send requests without authenticationToken.** - A request with a null `authenticationToken` is a session-less invocation (OPC 10000-4 §6.3.1). - After `CloseAsync(closeChannel: false)`, a request sent through the same `Session` object went out that way. A server that supports session-less invocation processed it as one instead of rejecting it. - `Session` now records that a session was created (`SessionCreated` with a non-null cookie; `OpenAsync` now calls it through the override instead of `base.SessionCreated`). From then on `UpdateRequestHeader` fails a request without token locally with `Bad_SessionIdInvalid`, and the request is never sent. CreateSession and ActivateSession are exempt, so reopening and reactivating keep working. A Session that was never created behaves as before. ## Changes - `src/Opc.Ua.Client/CoreClientUtils.cs`: `IsOpenApiEndpoint`, used in both selection passes; the `<returns>` documentation says that an OpenAPI endpoint is never returned. - `src/Opc.Ua.Client/Session/Session.cs`: overrides of `SessionCreated` and `UpdateRequestHeader` (both virtual in `SessionClient` / `ClientBase`), one `volatile bool` field. ## Tests - **New, part 1** (`Opc.Ua.Client.Tests`, `CoreClientUtilsSelectEndpointTests`): `SelectEndpointSkipsTheOpenApiEndpointListedFirst`, `SelectEndpointWithSecurityFallbackSkipsTheOpenApiEndpoint`, `SelectEndpointSkipsTheWssOpenApiEndpoint`, `SelectEndpointReturnsNullWhenOnlyOpenApiEndpointsMatch` (with and without `useSecurity`), `SelectEndpointKeepsTheFirstBinaryEndpointWithoutTransportProfile`. - **New, part 2** (`Opc.Ua.Client.Tests`, `SessionTests` with the channel mock): `RequestAfterCloseFailsLocallyWithBadSessionIdInvalidAsync` (the Read never reaches the channel), `ActivateSessionIsStillSentAfterCloseAsync`, `RequestAfterANewSessionIsCreatedCarriesItsTokenAsync`, `RequestBeforeASessionIsCreatedIsNotRefusedAsync`. - **Updated:** `Opc.Ua.Sessions.Tests` `ClientTest.ReconnectSessionOnAlternateChannelAsync` closes the Session and then the channel. The read that follows now fails with `Bad_SessionIdInvalid` before it reaches the closed channel, and the test expects that code instead of the codes of the closed channel (on master it gets `Bad_NotConnected`). - **Fail without the fix:** with `CoreClientUtils.cs` and `Session.cs` from master, all five SelectEndpoint tests that involve an OpenAPI endpoint (6 cases) fail, as does `RequestAfterCloseFailsLocallyWithBadSessionIdInvalidAsync`, and `ReconnectSessionOnAlternateChannelAsync` fails with `Bad_NotConnected`. `SelectEndpointKeepsTheFirstBinaryEndpointWithoutTransportProfile` and the three other Session tests pass there by design: they pin behaviour that must not change. - `Opc.Ua.Client.Tests` 2743/2743 and `Opc.Ua.Sessions.Tests` 672 passed / 287 skipped / 0 failed on net10.0 and net8.0 (macOS). - `Opc.Ua.Client` builds for net48, net8.0, net9.0 and net10.0 with 0 warnings; `dotnet format` (whitespace, style, analyzers) reports nothing on the changed lines; changed lines covered 26/26 (100 %) with the two suites above. - Fork CI (full matrix incl. macOS) and CodeQL: [CI run 37128535726](https://github.com/biancode/UA-.NETStandard/actions/runs/37128535726) (154 jobs) and [CodeQL run 37128537502](https://github.com/biancode/UA-.NETStandard/actions/runs/37128537502). CodeQL is green; CI is green except `Opc.Ua.Interop.Tests` `LegacyClientExtendedCheckAsync("ComplexTypes")` / `LoadAndDecodeLegacyComplexTypesAsync` ("1 of N structures were not decoded"), which came with #4630 and fails the same way on other open pull requests; this change does not touch ComplexTypes. ## Notes for review - Part 2 changes the code a caller sees for a request on a closed Session: `Bad_SessionIdInvalid` is raised locally instead of whatever the server or the closed channel answers. Reconnect paths are not affected, because they use ActivateSession or a new Session object. If you prefer to keep part 2 out, its commit can be dropped without affecting part 1. - The test framework's own endpoint selection (`ClientFixture`) is left as it is; the upstream test servers do not need the change. ## Related Issues - Fixes #4645 ## 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. - [x] 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. (Affected suites on net10.0 and net8.0 locally on macOS; .NET Framework is covered by the fork CI's Windows net48 legs.) - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received. --------- Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
144 lines
5.6 KiB
C#
144 lines
5.6 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/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Client.Tests
|
|
{
|
|
/// <summary>
|
|
/// <c>CoreClientUtils.SelectEndpoint</c> selects by TransportProfileUri
|
|
/// (OPC 10000-4 §5.5.4) and never returns an endpoint of the OpenAPI
|
|
/// mapping (OPC 10000-6 §G.3), whatever the order of the endpoints
|
|
/// returned by GetEndpoints.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("Client")]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
public sealed class CoreClientUtilsSelectEndpointTests
|
|
{
|
|
private static readonly Uri s_httpsUrl = new("https://localhost:4843/");
|
|
private static readonly Uri s_wssUrl = new("opc.wss://localhost:4843/");
|
|
|
|
[Test]
|
|
public void SelectEndpointSkipsTheOpenApiEndpointListedFirst()
|
|
{
|
|
EndpointDescription openApi = CreateEndpoint(s_httpsUrl, Profiles.HttpsOpenApiTransport);
|
|
EndpointDescription binary = CreateEndpoint(s_httpsUrl, Profiles.HttpsBinaryTransport);
|
|
|
|
EndpointDescription? selected = CoreClientUtils.SelectEndpoint(
|
|
null!,
|
|
s_httpsUrl,
|
|
[openApi, binary],
|
|
useSecurity: false,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(selected, Is.SameAs(binary));
|
|
}
|
|
|
|
[Test]
|
|
public void SelectEndpointWithSecurityFallbackSkipsTheOpenApiEndpoint()
|
|
{
|
|
// No endpoint signs, so the HTTPS fallback for useSecurity applies.
|
|
EndpointDescription openApi = CreateEndpoint(s_httpsUrl, Profiles.HttpsOpenApiTransport);
|
|
EndpointDescription binary = CreateEndpoint(s_httpsUrl, Profiles.HttpsBinaryTransport);
|
|
|
|
EndpointDescription? selected = CoreClientUtils.SelectEndpoint(
|
|
null!,
|
|
s_httpsUrl,
|
|
[openApi, binary],
|
|
useSecurity: true,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(selected, Is.SameAs(binary));
|
|
}
|
|
|
|
[Test]
|
|
public void SelectEndpointSkipsTheWssOpenApiEndpoint()
|
|
{
|
|
EndpointDescription openApi = CreateEndpoint(s_wssUrl, Profiles.WssOpenApiTransport);
|
|
EndpointDescription binary = CreateEndpoint(s_wssUrl, Profiles.UaWssTransport);
|
|
|
|
EndpointDescription? selected = CoreClientUtils.SelectEndpoint(
|
|
null!,
|
|
s_wssUrl,
|
|
[openApi, binary],
|
|
useSecurity: false,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(selected, Is.SameAs(binary));
|
|
}
|
|
|
|
[TestCase(false)]
|
|
[TestCase(true)]
|
|
public void SelectEndpointReturnsNullWhenOnlyOpenApiEndpointsMatch(bool useSecurity)
|
|
{
|
|
EndpointDescription? selected = CoreClientUtils.SelectEndpoint(
|
|
null!,
|
|
s_httpsUrl,
|
|
[CreateEndpoint(s_httpsUrl, Profiles.HttpsOpenApiTransport)],
|
|
useSecurity,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(selected, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void SelectEndpointKeepsTheFirstBinaryEndpointWithoutTransportProfile()
|
|
{
|
|
// Endpoints without TransportProfileUri are still selected by URL
|
|
// scheme and mode, as before.
|
|
EndpointDescription first = CreateEndpoint(s_httpsUrl, null);
|
|
EndpointDescription second = CreateEndpoint(s_httpsUrl, Profiles.HttpsBinaryTransport);
|
|
|
|
EndpointDescription? selected = CoreClientUtils.SelectEndpoint(
|
|
null!,
|
|
s_httpsUrl,
|
|
[first, second],
|
|
useSecurity: false,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(selected, Is.SameAs(first));
|
|
}
|
|
|
|
private static EndpointDescription CreateEndpoint(Uri url, string? transportProfileUri)
|
|
{
|
|
return new EndpointDescription
|
|
{
|
|
EndpointUrl = url.ToString(),
|
|
SecurityMode = MessageSecurityMode.None,
|
|
SecurityPolicyUri = SecurityPolicies.None,
|
|
TransportProfileUri = transportProfileUri
|
|
};
|
|
}
|
|
}
|
|
}
|