Files
UA-.NETStandard/tests/Opc.Ua.Features.Tests/FileSystemClientIntegrationTests.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

472 lines
18 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 System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client.FileSystem;
using Opc.Ua.Client.TestFramework;
using Opc.Ua.Server.FileSystem;
using Opc.Ua.Server.TestFramework;
using Opc.Ua.Tests;
using Quickstarts.ReferenceServer;
using ISession = Opc.Ua.Client.ISession;
namespace Opc.Ua.Features.Tests
{
/// <summary>
/// End-to-end tests that exercise the client-side
/// <see cref="FileSystemClient"/> against a real reference server
/// configured with a
/// <see cref="PhysicalFileSystemProvider"/> rooted at a per-test
/// temp directory.
/// </summary>
/// <remarks>
/// One <c>ServerFixture</c> + <c>ClientFixture</c> + temp-dir
/// provider is created per test (via <c>[SetUp]</c>) so each test
/// gets a clean slate. Tests are tagged <c>Integration</c> +
/// <c>FileSystem</c> for selective filtering.
/// </remarks>
[TestFixture]
[Category("Client")]
[Category("FileSystem")]
[Category("Integration")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
public class FileSystemClientIntegrationTests
{
private ServerFixture<ReferenceServer> m_serverFixture;
private ClientFixture m_clientFixture;
private ReferenceServer m_server;
private ISession m_session;
private string m_pkiRoot;
private string m_providerRoot;
private ITelemetryContext m_telemetry;
[SetUp]
public async Task SetUpAsync()
{
m_telemetry = NUnitTelemetryContext.Create();
m_pkiRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
m_providerRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(m_providerRoot);
m_serverFixture = new ServerFixture<ReferenceServer>(
t => new ReferenceServer(t)
{
EnableFileSystemNodeManager = true,
FileSystemProvider = new PhysicalFileSystemProvider(
m_providerRoot,
mountName: "TestRoot")
})
{
UriScheme = Utils.UriSchemeOpcTcp,
SecurityNone = true,
AutoAccept = true,
AllNodeManagers = false,
OperationLimits = true
};
await m_serverFixture.LoadConfigurationAsync(m_pkiRoot).ConfigureAwait(false);
m_server = await m_serverFixture.StartAsync().ConfigureAwait(false);
Assert.That(m_server, Is.Not.Null);
m_clientFixture = new ClientFixture(false, false, m_telemetry);
await m_clientFixture.LoadClientConfigurationAsync(m_pkiRoot).ConfigureAwait(false);
string url = $"{Utils.UriSchemeOpcTcp}://localhost:{m_serverFixture.Port}";
m_session = await m_clientFixture
.ConnectAsync(new Uri(url), SecurityPolicies.None)
.ConfigureAwait(false);
}
[TearDown]
public async Task TearDownAsync()
{
try
{
if (m_session != null)
{
await m_session.CloseAsync().ConfigureAwait(false);
m_session.Dispose();
}
}
catch
{
}
if (m_serverFixture != null)
{
await m_serverFixture.StopAsync().ConfigureAwait(false);
}
m_clientFixture?.Dispose();
TryDeleteDirectory(m_pkiRoot);
TryDeleteDirectory(m_providerRoot);
}
private static void TryDeleteDirectory(string path)
{
try
{
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
catch
{
}
}
private static readonly string[] s_rootListing = ["a.txt", "b.txt", "sub"];
private static readonly string[] s_nestedListing = ["inside.txt"];
private FileSystemClient OpenClient()
{
NodeId mountRoot = ResolveMountRoot();
return new FileSystemClient(m_session, mountRoot);
}
private NodeId ResolveMountRoot()
{
var browseDescriptions = new BrowseDescription[]
{
new()
{
NodeId = ObjectIds.FileSystem,
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HasComponent,
IncludeSubtypes = true,
NodeClassMask = (uint)NodeClass.Object,
ResultMask = (uint)BrowseResultMask.All
}
};
BrowseResponse response = m_session
.BrowseAsync(default, null, 0, browseDescriptions.ToArrayOf(), default)
.AsTask().GetAwaiter().GetResult();
Assert.That(response.Results.Count, Is.EqualTo(1));
foreach (ReferenceDescription reference in response.Results[0].References)
{
if (reference.BrowseName.Name == "TestRoot")
{
return ExpandedNodeId.ToNodeId(reference.NodeId, m_session.NamespaceUris);
}
}
throw new AssertionException(
"FileSystem mount 'TestRoot' was not found under Server.FileSystem.");
}
[Test]
public async Task RoundTripWriteThenReadFileAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo file = await client.Root
.CreateFileAsync("hello.txt")
.ConfigureAwait(false);
byte[] payload = Encoding.UTF8.GetBytes("hello world");
await file.WriteAllBytesAsync(payload).ConfigureAwait(false);
byte[] roundTrip = await file.ReadAllBytesAsync().ConfigureAwait(false);
Assert.That(roundTrip, Is.EqualTo(payload));
}
[Test]
public async Task EnumerateDirectoryListsCreatedEntriesAsync()
{
FileSystemClient client = OpenClient();
await client.Root.CreateFileAsync("a.txt").ConfigureAwait(false);
await client.Root.CreateFileAsync("b.txt").ConfigureAwait(false);
UaDirectoryInfo sub = await client.Root
.CreateSubdirectoryAsync("sub")
.ConfigureAwait(false);
await sub.CreateFileAsync("c.txt").ConfigureAwait(false);
var names = new List<string>();
await foreach (UaFileSystemInfo info in client.Root.EnumerateAsync().ConfigureAwait(false))
{
names.Add(info.Name);
}
Assert.That(names, Is.EquivalentTo(s_rootListing));
}
[Test]
public async Task CreateDirectoryThenSubdirectoryAsync()
{
FileSystemClient client = OpenClient();
UaDirectoryInfo first = await client.Root
.CreateSubdirectoryAsync("level1")
.ConfigureAwait(false);
await first.CreateSubdirectoryAsync("level2").ConfigureAwait(false);
Assert.That(
Directory.Exists(Path.Combine(m_providerRoot, "level1", "level2")),
Is.True);
UaFileSystemInfo found = (await client
.GetInfoAsync("level1/level2").ConfigureAwait(false))!;
Assert.That(found, Is.InstanceOf<UaDirectoryInfo>());
}
[Test]
public async Task ExplicitlyQualifiedPathResolvesToTheSameNodeAsync()
{
FileSystemClient client = OpenClient();
UaDirectoryInfo created = await client.Root
.CreateSubdirectoryAsync("qualified")
.ConfigureAwait(false);
ushort namespaceIndex = created.NodeId.NamespaceIndex;
Assert.That(namespaceIndex, Is.Not.Zero,
"The provider is expected to own a namespace of its own.");
UaFileSystemInfo unqualified = (await client
.GetInfoAsync("qualified").ConfigureAwait(false))!;
UaFileSystemInfo qualified = (await client
.GetInfoAsync($"{namespaceIndex}:qualified").ConfigureAwait(false))!;
Assert.That(unqualified, Is.InstanceOf<UaDirectoryInfo>());
Assert.That(qualified, Is.InstanceOf<UaDirectoryInfo>());
Assert.That(qualified.NodeId, Is.EqualTo(created.NodeId));
}
[Test]
public async Task CreateDirectoryAsyncAcceptsUnqualifiedNamespacedParentAsync()
{
FileSystemClient mountClient = OpenClient();
UaDirectoryInfo uploads = await mountClient.Root
.CreateSubdirectoryAsync("Uploads")
.ConfigureAwait(false);
ushort namespaceIndex = uploads.BrowseName.NamespaceIndex;
Assert.That(namespaceIndex, Is.Not.Zero,
"The provider is expected to own a namespace of its own.");
FileSystemClient serverFileSystem = FileSystemClient.OpenServerFileSystem(m_session);
string parentPath = $"/{namespaceIndex}:TestRoot/Uploads";
UaDirectoryInfo created = await serverFileSystem
.CreateDirectoryAsync(
$"{parentPath}/probe",
createIntermediate: false)
.ConfigureAwait(false);
Assert.That(
Directory.Exists(Path.Combine(m_providerRoot, "Uploads", "probe")),
Is.True);
Assert.That(
created.FullPath,
Is.EqualTo(
$"/{namespaceIndex}:TestRoot/{namespaceIndex}:Uploads/{namespaceIndex}:probe"));
}
[Test]
public async Task DeleteFileRemovesFromEnumerationAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo doomed = await client.Root
.CreateFileAsync("doomed.txt")
.ConfigureAwait(false);
await doomed.DeleteAsync().ConfigureAwait(false);
bool exists = await client.ExistsAsync("doomed.txt").ConfigureAwait(false);
Assert.That(exists, Is.False);
Assert.That(File.Exists(Path.Combine(m_providerRoot, "doomed.txt")), Is.False);
}
[Test]
public async Task MoveFileChangesNodeIdAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo src = await client.Root
.CreateFileAsync("src.txt")
.ConfigureAwait(false);
NodeId originalNodeId = src.NodeId;
UaFileSystemInfo moved = await src
.MoveToAsync(client.Root, newName: "dest.txt")
.ConfigureAwait(false);
Assert.That(moved.NodeId, Is.Not.EqualTo(originalNodeId));
Assert.That(File.Exists(Path.Combine(m_providerRoot, "dest.txt")), Is.True);
Assert.That(File.Exists(Path.Combine(m_providerRoot, "src.txt")), Is.False);
}
[Test]
public async Task CopyFilePreservesOriginalAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo src = await client.Root
.CreateFileAsync("orig.txt")
.ConfigureAwait(false);
byte[] payload = Encoding.UTF8.GetBytes("payload");
await src.WriteAllBytesAsync(payload).ConfigureAwait(false);
await src.CopyToAsync(client.Root, newName: "copy.txt")
.ConfigureAwait(false);
Assert.That(File.Exists(Path.Combine(m_providerRoot, "orig.txt")), Is.True);
Assert.That(File.Exists(Path.Combine(m_providerRoot, "copy.txt")), Is.True);
Assert.That(
File.ReadAllBytes(Path.Combine(m_providerRoot, "copy.txt")),
Is.EqualTo(File.ReadAllBytes(Path.Combine(m_providerRoot, "orig.txt"))));
}
[Test]
public async Task LargeFileChunkedReadAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo file = await client.Root
.CreateFileAsync("big.bin")
.ConfigureAwait(false);
// Deterministic test payload generation, not cryptographic
// material - use the shared UnsecureRandom wrapper.
var rng = new UnsecureRandom(42);
byte[] payload = new byte[64 * 1024];
rng.NextBytes(payload);
await file.WriteAllBytesAsync(payload).ConfigureAwait(false);
byte[] roundTrip = await file.ReadAllBytesAsync().ConfigureAwait(false);
Assert.That(roundTrip, Is.EqualTo(payload));
}
[Test]
public async Task MountBrowseNameMatchesProviderAsync()
{
// FileSystemClient stores a synthetic local "FileSystem"
// BrowseName on Root, so verify the actual mount via a
// direct Read of the BrowseName attribute.
FileSystemClient client = OpenClient();
ArrayOf<ReadValueId> nodesToRead = new[]
{
new ReadValueId
{
NodeId = client.Root.NodeId,
AttributeId = Attributes.BrowseName
}
}.ToArrayOf();
ReadResponse response = await m_session.ReadAsync(
null,
0.0,
TimestampsToReturn.Neither,
nodesToRead,
default).ConfigureAwait(false);
QualifiedName actual = response.Results[0].GetValue(QualifiedName.Null);
Assert.That(actual.Name, Is.EqualTo("TestRoot"));
}
[Test]
public async Task GetInfoOnMissingPathReturnsNullAsync()
{
FileSystemClient client = OpenClient();
UaFileSystemInfo info = (await client
.GetInfoAsync("does-not-exist.txt")
.ConfigureAwait(false))!;
Assert.That(info, Is.Null);
}
[Test]
public async Task NestedDirectoryEnumerationReturnsCreatedChildrenAsync()
{
FileSystemClient client = OpenClient();
UaDirectoryInfo level1 = await client.Root
.CreateSubdirectoryAsync("nest1")
.ConfigureAwait(false);
UaDirectoryInfo level2 = await level1
.CreateSubdirectoryAsync("nest2")
.ConfigureAwait(false);
await level2.CreateFileAsync("inside.txt").ConfigureAwait(false);
var names = new List<string>();
await foreach (UaFileSystemInfo info in level2.EnumerateAsync().ConfigureAwait(false))
{
names.Add(info.Name);
}
Assert.That(names, Is.EquivalentTo(s_nestedListing));
}
[Test]
public async Task ReadWrittenTextRoundTripsAsync()
{
FileSystemClient client = OpenClient();
UaFileInfo file = await client.Root
.CreateFileAsync("text.txt")
.ConfigureAwait(false);
const string content = "Hello, OPC UA!";
await file.WriteAllTextAsync(content).ConfigureAwait(false);
string readBack = await file.ReadAllTextAsync().ConfigureAwait(false);
Assert.That(readBack, Is.EqualTo(content));
}
[Test]
public async Task ReadOnlyProviderRejectsWriteAsync()
{
// Re-mount the same content under a read-only provider by
// restarting the server stack in this single test.
await m_session.CloseAsync().ConfigureAwait(false);
m_session.Dispose();
await m_serverFixture.StopAsync().ConfigureAwait(false);
m_serverFixture = new ServerFixture<ReferenceServer>(
t => new ReferenceServer(t)
{
EnableFileSystemNodeManager = true,
FileSystemProvider = new PhysicalFileSystemProvider(
m_providerRoot,
mountName: "TestRoot",
isWritable: false)
})
{
UriScheme = Utils.UriSchemeOpcTcp,
SecurityNone = true,
AutoAccept = true,
AllNodeManagers = false,
OperationLimits = true
};
await m_serverFixture.LoadConfigurationAsync(m_pkiRoot).ConfigureAwait(false);
m_server = await m_serverFixture.StartAsync().ConfigureAwait(false);
string url = $"{Utils.UriSchemeOpcTcp}://localhost:{m_serverFixture.Port}";
m_session = await m_clientFixture
.ConnectAsync(new Uri(url), SecurityPolicies.None)
.ConfigureAwait(false);
FileSystemClient client = OpenClient();
Assert.ThrowsAsync<UnauthorizedAccessException>(async () =>
await client.Root.CreateFileAsync("denied.txt").ConfigureAwait(false));
}
}
}