Files
UA-.NETStandard/tests/Opc.Ua.Gds.Tests/ApplicationsDatabaseBaseTests.cs
romanett 9b034f8291 Enable nullable reference types centrally; migrate all unset and annotations-only projects (#4700)
# Description

Nullable reference types are now enabled once, in `common.props`, and
every project in the repository builds warning-clean with them.

**Configuration**
- `<Nullable>enable</Nullable>` lives in `common.props`. All per-project
`<Nullable>` settings are removed: 178 project files (enable,
annotations, and the `disable` in the three Aot test projects).
- 419 per-file `#nullable enable`/`restore` directives and the 4
`#nullable disable` directives are removed.
- Kept on purpose:
- The 5 `<auto-generated>` schema files (`UANodeSet.cs`, `UA Type
Dictionary.cs`, `OPCBinarySchema.cs`, `SecuredApplication.cs`,
`PInvokeHelper.cs`) keep `#nullable enable`. The compiler ignores the
project setting in generated code, so the directive is required there.
- `tests/Opc.Ua.Interop.LegacyPeer` keeps `disable`. It builds against
the 1.5.378 packages and does not import the repository props.
- `docs/DeveloperGuide.md` and `.github/copilot-instructions.md` now
describe the central setting.

**Migration** (the 43 unset and 13 annotations-only projects, roughly
10k diagnostics)
- **Production code: annotated at the declarations.** Covers
`tools/Opc.Ua.SourceGeneration.Core`, `src/Opc.Ua.Lds.Server` and the
LDS sample.
- Members, parameters and returns are nullable where null actually
flows.
- Null-check helpers carry `[NotNullWhen]` (`IsNull`, `IsValidXmlName`,
`IsMethodTypeDesign`).
- `CoreUtils.Format`, `Utils.Format`, `ITemplateWriter.WriteLine` take
`params object?[]`. `Template.AddReplacement` takes `string?` (rendering
already skips null replacements).
- `IRegisteredServerStore.RegisterAsync` returns
`Task<RegistrationEntry?>`: it returns null when a registration is
removed.
- Dereferences backed by an invariant (for example, the validated model)
use `!`, so runtime behaviour is unchanged.
- **Behavioural changes in production code** (small, deliberate):
- `NodeSetToModelDesign` throws `InvalidOperationException` when the
file is not a NodeSet. Before, it threw a NullReferenceException on the
next line.
- `ModelDesignValidator` starts its lookup tables empty instead of null.
A redundant `if (m_nodes == null) return;` guard is gone.
- **Test code: minimal annotations.**
  - `?` on values that can be null, `!` where the test asserts non-null.
  - `= null!` for fixture members assigned in SetUp.
- `TryGetStructure<T>(out T? x)` where the inferred `T?` broke the
`IEncodeable` constraint.
- A few redundant `== null` checks on structs (`NodeId`, `ArrayOf<T>`)
folded into the adjacent `IsNull`/`Count` checks.

**Notes for reviewers**
- The diff is large (~1,500 files) but almost entirely mechanical: `?`,
`!` and directive removal. The non-annotation changes are listed above.
- Known follow-up, not done here:
`IModelDesign.Namespaces/TargetNamespace/NamespaceUris` stay
non-nullable even though the validator returns null before it loads a
dictionary. Making them nullable would ripple through every generator
call site.

## Related Issues

- Fixes #4439

## Checklist

- [ ] 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.
- [ ] 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.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

The "no new warnings" box covers net10.0 and net48: full non-incremental
builds of `UA.slnx`, `tools/SourceGeneration.slnx`,
`tools/MigrationAnalyzer.slnx` and the Aot test projects finished with 0
warnings and 0 errors under the repository's warnings-as-errors
settings. net9.0 and net8.0 also had no nullable diagnostics, but they
were not rebuilt after the final fixes. Tests have not been run locally
yet.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:48:13 +02:00

339 lines
14 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.Gds.Server.Database;
namespace Opc.Ua.Gds.Tests
{
[TestFixture]
[Category("GDS")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[Parallelizable]
public class ApplicationsDatabaseBaseTests
{
[Test]
public void RegisterApplicationNullThrows()
{
var database = new TestApplicationsDatabase();
Assert.That(
() => database.RegisterApplication(null!),
Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void RegisterApplicationInvalidApplicationUriThrows()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationUri = "not a uri";
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
[Test]
public void RegisterApplicationServerWithoutDiscoveryUrlsThrows()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.DiscoveryUrls = [];
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
[Test]
public void RegisterApplicationClientWithDiscoveryUrlsThrows()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationType = ApplicationType.Client;
application.ServerCapabilities = [];
application.DiscoveryUrls = ["opc.tcp://localhost:4840"];
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
[Test]
public void RegisterApplicationValidServerReturnsDefaultNodeId()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
NodeId applicationId = database.RegisterApplication(application);
Assert.That(applicationId.IsNull, Is.True);
}
[Test]
public void RegisterApplicationValidServerWithDiscoveryUrlsDoesNotThrow()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
Assert.That(
() => database.RegisterApplication(application),
Throws.Nothing);
}
[Test]
public void FindApplicationsWhitespaceReturnsNoRecord()
{
// OPC 10000-12 §6.5.4: FindApplications is not a wildcard query; the
// node manager rejects an empty ApplicationUri with BadInvalidArgument.
var database = new TestApplicationsDatabase();
ApplicationRecordDataType[]? results = database.FindApplications(" ");
Assert.That(results, Is.Null.Or.Empty);
}
[Test]
public void QueryApplicationsInvalidTypeThrows()
{
// applicationType filter values per OPC UA Part 12 §6.3.10 / Part 4:
// 0 = ALL, 1 = SERVER, 2 = CLIENT, 3 = DISCOVERY_SERVER.
// Anything outside this range is invalid.
var database = new TestApplicationsDatabase();
Assert.That(
() => database.QueryApplications(
0,
10,
null!,
null!,
99,
null!,
[],
out _,
out _),
Throws.TypeOf<ServiceResultException>()
.With.Property(nameof(ServiceResultException.StatusCode)).EqualTo(StatusCodes.BadInvalidArgument));
}
[TestCase("mainserver", "main%", true)]
[TestCase("green", "%en%", true)]
[TestCase("content", "%en%", true)]
[TestCase("alpha", "%en%", false)]
[TestCase("would", "_ould", true)]
[TestCase("could", "_ould", true)]
[TestCase("xould", "_ould", true)]
[TestCase("mould", "_ould", true)]
[TestCase("moulder", "_ould", false)]
[TestCase("5%", "5[%]", true)]
[TestCase("5_", "5[_]", true)]
[TestCase("5a", "5[_]", false)]
[TestCase("abc1", "abc[13-68]", true)]
[TestCase("abc4", "abc[13-68]", true)]
[TestCase("abc5", "abc[13-68]", true)]
[TestCase("abc6", "abc[13-68]", true)]
[TestCase("abc8", "abc[13-68]", true)]
[TestCase("abc2", "abc[13-68]", false)]
[TestCase("abc7", "abc[13-68]", false)]
[TestCase("abc-", "abc[13-68]", false)]
[TestCase("xyzc", "xyz[c-f]", true)]
[TestCase("xyzf", "xyz[c-f]", true)]
[TestCase("xyzg", "xyz[c-f]", false)]
[TestCase("ABC2", "ABC[^13-5]", true)]
[TestCase("ABC0", "ABC[^13-5]", true)]
[TestCase("ABC6", "ABC[^13-5]", true)]
[TestCase("ABC9", "ABC[^13-5]", true)]
[TestCase("ABC1", "ABC[^13-5]", false)]
[TestCase("ABC3", "ABC[^13-5]", false)]
[TestCase("ABC4", "ABC[^13-5]", false)]
[TestCase("ABC0", "ABC[^1-5]", true)]
[TestCase("ABC3", "ABC[^1-5]", false)]
[TestCase("xyza", "xyz[^dgh]", true)]
[TestCase("xyzd", "xyz[^dgh]", false)]
[TestCase("xyzg", "xyz[^dgh]", false)]
[TestCase("xyzh", "xyz[^dgh]", false)]
[TestCase("5%", "5\\%", true)]
[TestCase("5_", "5\\_", true)]
[TestCase("\\", "\\\\", true)]
[TestCase("[", "\\[", true)]
[TestCase("]", "\\]", true)]
[TestCase("5a", "5\\%", false)]
// CTT GDS Application Directory / Query Applications patterns
[TestCase("urn:OPCFoundation:ServerApplication", "%_erver%", true)]
[TestCase("urn:OPCFoundation:ComplianceTestTool", "%_erver%", false)]
[TestCase("urn:OPCFoundation:ComplianceTestToolEmbeddedServer", "%e_", true)]
[TestCase("cab:other_foundation:ClientAndServer", "%\\_%", true)]
[TestCase("urn:OPCFoundation:ServerApplication", "%\\_%", false)]
[TestCase("Name with % wildcard", "%\\%%", true)]
[TestCase("Name without wildcard", "%\\%%", false)]
[TestCase("Example_Vendor - ClientAndServer", "%[q-s]", true)]
[TestCase("OPC Foundation Compliance Test Tool", "%[^q-s]", true)]
[TestCase("OPC Foundation Compliance Test Tool", "%_ompliance%", true)]
[TestCase("a_b", "[_]", false)]
[TestCase("target", "%[a^j-l]%", false)]
public void MatchImplementsUaWildcardSpecification(
string target,
string pattern,
bool expected)
{
bool result = ApplicationsDatabaseBase.Match(target, pattern);
Assert.That(result, Is.EqualTo(expected));
}
/// <summary>
/// OPC 10000-4 §7.7.3: the '^' shall be the first character of a list;
/// malformed filters are rejected (CTT GDS Application Directory 078.js).
/// </summary>
[TestCase("%[a^j-l]%")]
[TestCase("abc[")]
[TestCase("abc\\")]
[TestCase("abc[]")]
[TestCase("abc[z-a]")]
public void QueryWithInvalidPatternThrowsBadInvalidArgument(string pattern)
{
var database = new TestApplicationsDatabase();
foreach (Action query in new Action[] {
() => database.QueryServers(0, 0, pattern, null!, null!, [], out _),
() => database.QueryServers(0, 0, null!, pattern, null!, [], out _),
() => database.QueryServers(0, 0, null!, null!, pattern, [], out _),
() => database.QueryApplications(0, 0, pattern, null!, 0, null!, [], out _, out _),
() => database.QueryApplications(0, 0, null!, pattern, 0, null!, [], out _, out _),
() => database.QueryApplications(0, 0, null!, null!, 0, pattern, [], out _, out _)
})
{
Assert.That(
() => query(),
Throws.TypeOf<ServiceResultException>()
.With.Property(nameof(ServiceResultException.StatusCode)).EqualTo(StatusCodes.BadInvalidArgument));
}
}
[Test]
public void RegisterApplicationClientWithRcpDiscoveryUrlsAndRcpCapabilityDoesNotThrow()
{
// Per OPC 10000-12 §6.5.5 a Client that supports reverse-connect
// may register DiscoveryUrls beginning with the rcp+ prefix and
// must list the RCP ServerCapability.
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationType = ApplicationType.Client;
application.DiscoveryUrls = ["rcp+opc.tcp://localhost:4840"];
application.ServerCapabilities = ["RCP"];
Assert.That(
() => database.RegisterApplication(application),
Throws.Nothing);
}
[Test]
public void RegisterApplicationClientWithRcpDiscoveryUrlsAndMissingCapabilityThrows()
{
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationType = ApplicationType.Client;
application.DiscoveryUrls = ["rcp+opc.tcp://localhost:4840"];
application.ServerCapabilities = [];
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
[Test]
public void RegisterApplicationServerWithRcpDiscoveryUrlThrows()
{
// Servers must not register reverse-connect listening URLs; the
// rcp+ prefix is reserved for Clients / ClientAndServer.
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.DiscoveryUrls = ["rcp+opc.tcp://localhost:4840"];
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
[Test]
public void RegisterApplicationClientAndServerWithMixedUrlsDoesNotThrow()
{
// ClientAndServer may include both regular Server DiscoveryUrls
// and reverse-connect URLs; the RCP ServerCapability is required
// when reverse-connect URLs are present.
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationType = ApplicationType.ClientAndServer;
application.DiscoveryUrls =
[
"opc.tcp://localhost:4840",
"rcp+opc.tcp://localhost:4841"
];
application.ServerCapabilities = ["LDS", "RCP"];
Assert.That(
() => database.RegisterApplication(application),
Throws.Nothing);
}
[Test]
public void RegisterApplicationClientAndServerWithOnlyRcpDiscoveryUrlsThrows()
{
// ClientAndServer must always expose at least one non-rcp+
// Server DiscoveryUrl.
var database = new TestApplicationsDatabase();
ApplicationRecordDataType application = CreateValidServerApplication();
application.ApplicationType = ApplicationType.ClientAndServer;
application.DiscoveryUrls = ["rcp+opc.tcp://localhost:4841"];
application.ServerCapabilities = ["LDS", "RCP"];
Assert.That(
() => database.RegisterApplication(application),
Throws.TypeOf<ArgumentException>());
}
private static ApplicationRecordDataType CreateValidServerApplication()
{
return new ApplicationRecordDataType
{
ApplicationUri = "urn:test:application",
ApplicationType = ApplicationType.Server,
ApplicationNames = [new LocalizedText("en", "TestApp")],
ProductUri = "urn:test:product",
DiscoveryUrls = ["opc.tcp://localhost:4840"],
ServerCapabilities = ["LDS"]
};
}
private sealed class TestApplicationsDatabase : ApplicationsDatabaseBase;
}
}