mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-21 17:16:16 +08:00
1cca744b5c
Closes #4201. Closes #4202. Closes #4203. Closes #4215. Closes #4216. (#4215 and #4216 are @marcschier's originals from the #4188 review; #4202 and #4201 are my later duplicates of the same two findings. All four describe the two changes in section 1 and are closed together.) **This PR now carries what was #4221 as well.** The two were a stack; they have been unstacked and combined here, and #4221 is closed. Everything below the "Read the server diagnostics" section is the work that was previously reviewed as #4221. --- ## 1. The browse path guards itself, and the last node locks are gone `AsyncCustomNodeManager` kept two `lock (source)` sites around `CreateBrowser`, carrying a NOTE that browser creation was not internally synchronized. `NodeBrowser` still handed its lock to derived types through `protected Lock DataLock`. `NodeState.CreateBrowser` now holds a browse lock across `PopulateBrowser` and `OnPopulateBrowser`, so browser construction on a node is serialized — which is what the external `lock (source)` was actually providing, and what a handler that mutates the node during population (the historian's lazy config installer) depends on. Both `lock (source)` statements and the NOTE are gone. **What that lock does not do**, stated plainly in the XML docs and in `docs/NodeManagers.md` rather than over-promised: it is not an atomic snapshot across the node's children, notifiers and references. Writers take those collections' own locks, not the browse lock. The browser is a point-in-time copy and each collection is read consistently, but the combination is not a transaction. No `lock` takes a `NodeState` as its sync root any more. That was the last thing keeping `NodeState` from being documentably self-guarding, so the other eleven went with it: `lock (activeNode)`, `lock (target)`, `lock (source.Node)`, `lock (target.Node)`, `lock (notifier)`, `lock (handle.Node)` in `AsyncCustomNodeManager`, and `lock (node)` in `CustomNodeManager2`. Most were a check-then-act pair now served by `NodeState.AddReferenceIfMissing`, which does the check and the insert under the node's own lock. `NodeBrowser.DataLock` is removed. A browser is **single-consumer**: it belongs to whoever created it, and both server browse paths already serialize on the owning side. The exposed lock only added an inheritance-level locking contract that a derived browser could not reason about. ## 2. Read the server diagnostics under the lock every writer takes `OnUpdateDiagnostics` locked the diagnostics **payload** rather than the lock every writer takes through `UpdateServerDiagnostics`, so the snapshot a client reads from the `ServerDiagnostics` node was never excluded from a concurrent writer. Both halves matter, and the second is the one that is easy to miss: ```csharp lock (m_diagnosticsLock) // was: lock (ServerDiagnostics) { value = Variant.FromStructure(ServerDiagnostics, copy: true); // was: copy defaulted to false } ``` `copy: true` is what makes it a snapshot. `FromStructure(value, copy: false)` does `new ExtensionObject(value, copy)` — it wraps the **live** structure without copying it, so the caller would read the fields long after the lock was released and see them change underneath. Swapping the lock alone would have looked like a fix while still excluding nothing. ## 3. Analyzer numbering The browser rule is **UA0027**, not UA0025. This branch had independently taken UA0025 for `NodeBrowser.DataLock` while #4188 published UA0025 for `ILocalNode.DataLock` and UA0026 for `BaseVariableValue.Lock`. Master's are shipped, so they keep the numbers: | | UA0025 | UA0026 | UA0027 | |---|---|---|---| | master (#4188) | `ILocalNode.DataLock` | `BaseVariableValue.Lock` | — | | **this PR** | `ILocalNode.DataLock` | `BaseVariableValue.Lock` | **`NodeBrowser.DataLock`** | Both sides had written a different `UA0025Tests.cs`, which arrived as an add/add conflict. They are now split: `UA0025Tests.cs` holds master's `ILocalNode` tests, `UA0027Tests.cs` holds the `NodeBrowser` ones. UA0024's help text also drops its reference to `ReadServerDiagnostics`, which #4193 removed as unused, and names `ISession.ReadDiagnostics` / `ISubscription.ReadDiagnostics` instead — those do still exist. ## 4. Two more locks reachable from outside their owner **`ApplicationConfiguration.PropertiesLock` is removed.** It returned the properties dictionary itself, so `lock (configuration.PropertiesLock)` was `lock (configuration.Properties)` — the lock *was* the data it guarded, the same shape as #4203. It had exactly one production consumer (`OAuth2CredentialCollection.Load`, a get-or-add with `ParseExtension` called inside the lock), so it could not simply be deleted. `Properties` is now a `ConcurrentDictionary` — same public type, every operation atomic on its own — and the one multi-operation need gets a member of its own: ```csharp public T GetOrAddProperty<T>(string key, Func<T> valueFactory); ``` It deliberately does **not** invoke the factory under a lock, so `ParseExtension` now runs outside any critical section. Holding a lock across a caller-supplied callback is exactly the shape that produced the attribute/diagnostics deadlock earlier in this work. The two existing tests asserted only `PropertiesLock is not null`, so they tested nothing but its presence. They are replaced by tests of the contract: factory runs once for a missing key, not at all for a present one, 32 concurrent callers all observe the same instance, null arguments throw. `PropertiesLock` ships in 1.5.378, so this is a breaking removal — migration guide section plus analyzer **UA0028**. **`SharedDefinitionCache` no longer hands out its lock.** Three sites in `SharedKeyValueSubscriptionStore` took the nested cache's `Lock` to operate on the cache's own dictionary. Both are private now, behind `ReplaceAll` and `CloneWhere`. `SnapshotCommitLock` stays exposed on purpose: it is a `SemaphoreSlim` sequencing an async commit across `await` points, which `System.Threading.Lock` cannot span, so folding it in would change behaviour rather than hide a lock. ## Verification Merged `master` (`0edfe237a`, including #4234) with no conflicts. - `dotnet build UA.slnx` — 0 warnings, 0 errors - `Opc.Ua.Server.Tests` — 4074 passed - `Opc.Ua.Types.Tests` — 8501 passed - `Opc.Ua.MigrationAnalyzer.Tests` — 151 passed on all five TFMs (138 → 151, the new ones being UA0027 and UA0028) - `Opc.Ua.Core.Tests` — 4249 passed - `Opc.Ua.Redundancy.Server.Tests` — 573 passed ## Note for the reviewer @romanett — your approval was given when this PR's diff was the 7 diagnostics files, because it was measured against `marcschier/node-browse-locks`. Now that the two are combined and the base is `master`, the diff is 23 files, and the extra 16 are the node/browser lock work that was #4221. GitHub kept the approval across the retarget, but it did not cover that code, so please re-review rather than let it ride. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e9c13dd0-a6e3-4ad2-8ad6-1c3fe6323041 Copilot-Session: b3c0e146-5b9e-4d08-a0f6-db3f940484b1 Copilot-Session: d18f87e9-2484-44ce-98ce-463ba6cf5f63 Copilot-Session: 0ee22c1d-c319-4257-bf78-a5a077a1824a
1030 lines
37 KiB
C#
1030 lines
37 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.Threading;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Core.Tests
|
|
{
|
|
[TestFixture]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
public class ApplicationConfigurationTests
|
|
{
|
|
private ITelemetryContext m_telemetry;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
m_telemetry = NUnitTelemetryContext.Create();
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Parameterless constructor creates a valid instance.
|
|
/// </summary>
|
|
public void ConstructorDefault()
|
|
{
|
|
var config = new ApplicationConfiguration();
|
|
Assert.That(config, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Constructor with telemetry creates a valid instance.
|
|
/// </summary>
|
|
public void ConstructorWithTelemetry()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Copy constructor copies all supported properties.
|
|
/// </summary>
|
|
public void ConstructorCopy()
|
|
{
|
|
var original = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "TestApp",
|
|
ApplicationUri = "urn:test:app",
|
|
ApplicationType = ApplicationType.Server,
|
|
DisableHiResClock = true
|
|
};
|
|
|
|
var copy = new ApplicationConfiguration(original);
|
|
Assert.That(copy.ApplicationName, Is.EqualTo("TestApp"));
|
|
Assert.That(copy.ApplicationUri, Is.EqualTo("urn:test:app"));
|
|
Assert.That(copy.ApplicationType, Is.EqualTo(ApplicationType.Server));
|
|
Assert.That(copy.DisableHiResClock, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationName get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ApplicationNameGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "MyApp"
|
|
};
|
|
Assert.That(config.ApplicationName, Is.EqualTo("MyApp"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationName defaults to null.
|
|
/// </summary>
|
|
public void ApplicationNameDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ApplicationName, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationUri get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ApplicationUriGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationUri = "urn:test:myapp"
|
|
};
|
|
Assert.That(config.ApplicationUri, Is.EqualTo("urn:test:myapp"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationUri defaults to null.
|
|
/// </summary>
|
|
public void ApplicationUriDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ApplicationUri, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ProductUri get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ProductUriGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ProductUri = "urn:test:product"
|
|
};
|
|
Assert.That(config.ProductUri, Is.EqualTo("urn:test:product"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ProductUri defaults to null.
|
|
/// </summary>
|
|
public void ProductUriDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ProductUri, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationType get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ApplicationTypeGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationType = ApplicationType.ClientAndServer
|
|
};
|
|
Assert.That(config.ApplicationType, Is.EqualTo(ApplicationType.ClientAndServer));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ApplicationType defaults to Server.
|
|
/// </summary>
|
|
public void ApplicationTypeDefaultIsServer()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ApplicationType, Is.EqualTo(ApplicationType.Server));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ServerConfiguration get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ServerConfigurationGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
var serverConfig = new ServerConfiguration();
|
|
config.ServerConfiguration = serverConfig;
|
|
Assert.That(config.ServerConfiguration, Is.SameAs(serverConfig));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ServerConfiguration defaults to null.
|
|
/// </summary>
|
|
public void ServerConfigurationDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ServerConfiguration, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ClientConfiguration get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ClientConfigurationGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
var clientConfig = new ClientConfiguration();
|
|
config.ClientConfiguration = clientConfig;
|
|
Assert.That(config.ClientConfiguration, Is.SameAs(clientConfig));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ClientConfiguration defaults to null.
|
|
/// </summary>
|
|
public void ClientConfigurationDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ClientConfiguration, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// SecurityConfiguration is created by default constructor.
|
|
/// </summary>
|
|
public void SecurityConfigurationDefaultIsNotNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.SecurityConfiguration, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// SecurityConfiguration set with null reverts to default.
|
|
/// </summary>
|
|
public void SecurityConfigurationSetNullReturnsDefault()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
SecurityConfiguration = null
|
|
};
|
|
Assert.That(config.SecurityConfiguration, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// SecurityConfiguration get/set round-trips correctly.
|
|
/// </summary>
|
|
public void SecurityConfigurationGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
var secConfig = new SecurityConfiguration();
|
|
config.SecurityConfiguration = secConfig;
|
|
Assert.That(config.SecurityConfiguration, Is.SameAs(secConfig));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TransportQuotas get/set round-trips correctly.
|
|
/// </summary>
|
|
public void TransportQuotasGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
var quotas = new TransportQuotas();
|
|
config.TransportQuotas = quotas;
|
|
Assert.That(config.TransportQuotas, Is.SameAs(quotas));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TransportQuotas defaults to null.
|
|
/// </summary>
|
|
public void TransportQuotasDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.TransportQuotas, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TransportConfigurations is created by default constructor.
|
|
/// </summary>
|
|
public void TransportConfigurationsDefaultIsNotNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.TransportConfigurations.IsNull, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TransportConfigurations get/set round-trips correctly.
|
|
/// </summary>
|
|
public void TransportConfigurationsGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
TransportConfigurations = new List<TransportConfiguration>
|
|
{
|
|
new("opc.tcp", typeof(object))
|
|
}.ToArrayOf()
|
|
};
|
|
Assert.That(config.TransportConfigurations.Count, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CertificateManager is null by default before validation.
|
|
/// </summary>
|
|
public void CertificateManagerDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.CertificateManager, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CertificateManager get/set round-trips correctly.
|
|
/// </summary>
|
|
public void CertificateManagerGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
using var manager = new CertificateManager(m_telemetry);
|
|
config.CertificateManager = manager;
|
|
Assert.That(config.CertificateManager, Is.SameAs(manager));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TraceConfiguration get/set round-trips correctly.
|
|
/// </summary>
|
|
public void TraceConfigurationGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
TraceConfiguration = new TraceConfiguration { OutputFilePath = "trace.log" }
|
|
};
|
|
Assert.That(config.TraceConfiguration, Is.Not.Null);
|
|
Assert.That(config.TraceConfiguration.OutputFilePath, Is.EqualTo("trace.log"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TraceConfiguration defaults to null.
|
|
/// </summary>
|
|
public void TraceConfigurationDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.TraceConfiguration, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// DisableHiResClock get/set round-trips correctly.
|
|
/// </summary>
|
|
public void DisableHiResClockGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
DisableHiResClock = true
|
|
};
|
|
Assert.That(config.DisableHiResClock, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// DisableHiResClock defaults to false.
|
|
/// </summary>
|
|
public void DisableHiResClockDefaultIsFalse()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.DisableHiResClock, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// DiscoveryServerConfiguration get/set round-trips correctly.
|
|
/// </summary>
|
|
public void DiscoveryServerConfigurationGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
var dsc = new DiscoveryServerConfiguration();
|
|
config.DiscoveryServerConfiguration = dsc;
|
|
Assert.That(config.DiscoveryServerConfiguration, Is.SameAs(dsc));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// DiscoveryServerConfiguration defaults to null.
|
|
/// </summary>
|
|
public void DiscoveryServerConfigurationDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.DiscoveryServerConfiguration, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Properties dictionary is accessible and initially empty.
|
|
/// </summary>
|
|
public void PropertiesDictionaryIsAccessible()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.Properties, Is.Not.Null);
|
|
Assert.That(config.Properties, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetOrAddProperty runs the factory once when the key is absent.
|
|
/// </summary>
|
|
public void GetOrAddPropertyAddsWhenKeyIsAbsent()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
int calls = 0;
|
|
|
|
string value = config.GetOrAddProperty("key", () => { calls++; return "made"; });
|
|
|
|
Assert.That(value, Is.EqualTo("made"));
|
|
Assert.That(calls, Is.EqualTo(1));
|
|
Assert.That(config.Properties["key"], Is.EqualTo("made"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetOrAddProperty does not run the factory when a value is present.
|
|
/// </summary>
|
|
public void GetOrAddPropertyDoesNotRunFactoryWhenKeyIsPresent()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
config.Properties["key"] = "existing";
|
|
|
|
string value = config.GetOrAddProperty<string>(
|
|
"key",
|
|
() => throw new InvalidOperationException("factory must not run"));
|
|
|
|
Assert.That(value, Is.EqualTo("existing"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Concurrent callers all observe the same instance, which is what the
|
|
/// removed PropertiesLock was taken for.
|
|
/// </summary>
|
|
public void GetOrAddPropertyPublishesOneInstanceToConcurrentCallers()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
const int callers = 32;
|
|
using var start = new ManualResetEventSlim(false);
|
|
|
|
var results = new object[callers];
|
|
var threads = new Thread[callers];
|
|
for (int i = 0; i < callers; i++)
|
|
{
|
|
int index = i;
|
|
threads[i] = new Thread(() =>
|
|
{
|
|
start.Wait();
|
|
results[index] = config.GetOrAddProperty<object>("key", () => new object());
|
|
});
|
|
threads[i].Start();
|
|
}
|
|
|
|
start.Set();
|
|
foreach (Thread thread in threads)
|
|
{
|
|
thread.Join();
|
|
}
|
|
|
|
Assert.That(results, Is.All.SameAs(results[0]));
|
|
Assert.That(config.Properties["key"], Is.SameAs(results[0]));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetOrAddProperty rejects a null key or factory.
|
|
/// </summary>
|
|
public void GetOrAddPropertyThrowsOnNullArguments()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
|
|
Assert.That(
|
|
() => config.GetOrAddProperty<string>(null!, () => "x"),
|
|
Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(
|
|
() => config.GetOrAddProperty<string>("key", null!),
|
|
Throws.TypeOf<ArgumentNullException>());
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ExtensionObjects list is accessible.
|
|
/// </summary>
|
|
public void ExtensionObjectsIsAccessible()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.ExtensionObjects, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CreateMessageContext returns a valid ServiceMessageContext.
|
|
/// </summary>
|
|
public void CreateMessageContextReturnsValidContext()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
ServiceMessageContext context = config.CreateMessageContext();
|
|
Assert.That(context, Is.Not.Null);
|
|
Assert.That(context.NamespaceUris, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CreateMessageContext with factory returns context with that factory.
|
|
/// </summary>
|
|
public void CreateMessageContextWithFactory()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
IEncodeableFactory factory = EncodeableFactory.Create();
|
|
ServiceMessageContext context = config.CreateMessageContext(factory);
|
|
Assert.That(context, Is.Not.Null);
|
|
Assert.That(context.Factory, Is.SameAs(factory));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CreateMessageContext applies TransportQuotas when set.
|
|
/// </summary>
|
|
public void CreateMessageContextAppliesTransportQuotas()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
TransportQuotas = new TransportQuotas
|
|
{
|
|
MaxArrayLength = 42,
|
|
MaxStringLength = 99,
|
|
MaxByteStringLength = 101,
|
|
MaxMessageSize = 5000,
|
|
MaxEncodingNestingLevels = 7,
|
|
MaxDecoderRecoveries = 3
|
|
}
|
|
};
|
|
|
|
ServiceMessageContext context = config.CreateMessageContext();
|
|
Assert.That(context.MaxArrayLength, Is.EqualTo(42));
|
|
Assert.That(context.MaxStringLength, Is.EqualTo(99));
|
|
Assert.That(context.MaxByteStringLength, Is.EqualTo(101));
|
|
Assert.That(context.MaxMessageSize, Is.EqualTo(5000));
|
|
Assert.That(context.MaxEncodingNestingLevels, Is.EqualTo(7));
|
|
Assert.That(context.MaxDecoderRecoveries, Is.EqualTo(3));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// CreateMessageContext without TransportQuotas uses defaults.
|
|
/// </summary>
|
|
public void CreateMessageContextWithoutTransportQuotasUsesDefaults()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
ServiceMessageContext context = config.CreateMessageContext();
|
|
Assert.That(context.MaxArrayLength, Is.EqualTo(DefaultEncodingLimits.MaxArrayLength));
|
|
Assert.That(context.MaxStringLength, Is.EqualTo(DefaultEncodingLimits.MaxStringLength));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetServerDomainNames returns empty when ServerConfiguration is null.
|
|
/// </summary>
|
|
public void GetServerDomainNamesReturnsEmptyWhenNoServerConfig()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
ArrayOf<string> domains = config.GetServerDomainNames();
|
|
Assert.That(domains.Count, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetServerDomainNames extracts domains from base addresses.
|
|
/// </summary>
|
|
public void GetServerDomainNamesExtractsFromBaseAddresses()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ServerConfiguration = new ServerConfiguration()
|
|
};
|
|
config.ServerConfiguration.BaseAddresses = new List<string>
|
|
{
|
|
"opc.tcp://myserver:4840"
|
|
}.ToArrayOf();
|
|
|
|
ArrayOf<string> domains = config.GetServerDomainNames();
|
|
Assert.That(domains.Count, Is.GreaterThan(0));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetServerDomainNames deduplicates domain names.
|
|
/// </summary>
|
|
public void GetServerDomainNamesDeduplicates()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ServerConfiguration = new ServerConfiguration()
|
|
};
|
|
config.ServerConfiguration.BaseAddresses = new List<string>
|
|
{
|
|
"opc.tcp://myserver:4840",
|
|
"https://myserver:4843"
|
|
}.ToArrayOf();
|
|
|
|
ArrayOf<string> domains = config.GetServerDomainNames();
|
|
Assert.That(domains.Count, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetServerDomainNames includes alternate base addresses.
|
|
/// </summary>
|
|
public void GetServerDomainNamesIncludesAlternateAddresses()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ServerConfiguration = new ServerConfiguration()
|
|
};
|
|
config.ServerConfiguration.BaseAddresses = new List<string>
|
|
{
|
|
"opc.tcp://server1:4840"
|
|
}.ToArrayOf();
|
|
config.ServerConfiguration.AlternateBaseAddresses = new List<string>
|
|
{
|
|
"opc.tcp://server2:4840"
|
|
}.ToArrayOf();
|
|
|
|
ArrayOf<string> domains = config.GetServerDomainNames();
|
|
Assert.That(domains.Count, Is.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ValidateAsync throws when ApplicationName is empty.
|
|
/// </summary>
|
|
public void ValidateAsyncThrowsWhenApplicationNameEmpty()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.ThrowsAsync<ServiceResultException>(
|
|
async () => await config.ValidateAsync(ApplicationType.Server).ConfigureAwait(false));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ValidateAsync throws when SecurityConfiguration is null.
|
|
/// </summary>
|
|
public void ValidateAsyncThrowsWhenSecurityConfigurationNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "TestApp"
|
|
};
|
|
// Force SecurityConfiguration to a new instance with no trust lists,
|
|
// then null it out through reflection since the setter prevents null.
|
|
// Instead, test with a valid name but rely on the SecurityConfiguration.Validate
|
|
// to throw since there are no certificate stores configured.
|
|
// The setter prevents null, so SecurityConfiguration is always non-null.
|
|
// The validate call will proceed past the null check.
|
|
Assert.ThrowsAsync<ServiceResultException>(
|
|
async () => await config.ValidateAsync(ApplicationType.Server).ConfigureAwait(false));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ValidateAsync throws for Server type when ServerConfiguration is null.
|
|
/// </summary>
|
|
public async Task ValidateAsyncThrowsForServerWithNoServerConfiguration()
|
|
{
|
|
ApplicationConfiguration config = CreateMinimalValidatableConfig();
|
|
config.ApplicationType = ApplicationType.Server;
|
|
|
|
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(
|
|
async () => await config.ValidateAsync(ApplicationType.Server).ConfigureAwait(false));
|
|
Assert.That(ex, Is.Not.Null);
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ValidateAsync throws for Client type when ClientConfiguration is null.
|
|
/// </summary>
|
|
public async Task ValidateAsyncThrowsForClientWithNoClientConfiguration()
|
|
{
|
|
ApplicationConfiguration config = CreateMinimalValidatableConfig();
|
|
|
|
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(
|
|
async () => await config.ValidateAsync(ApplicationType.Client).ConfigureAwait(false));
|
|
Assert.That(ex, Is.Not.Null);
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// ValidateAsync throws for DiscoveryServer type when DiscoveryServerConfiguration is null.
|
|
/// </summary>
|
|
public async Task ValidateAsyncThrowsForDiscoveryServerWithNoConfig()
|
|
{
|
|
ApplicationConfiguration config = CreateMinimalValidatableConfig();
|
|
|
|
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(
|
|
async () => await config.ValidateAsync(ApplicationType.DiscoveryServer).ConfigureAwait(false));
|
|
Assert.That(ex, Is.Not.Null);
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// SourceFilePath is null by default.
|
|
/// </summary>
|
|
public void SourceFilePathDefaultIsNull()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
Assert.That(config.SourceFilePath, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Extensions property get/set round-trips correctly.
|
|
/// </summary>
|
|
public void ExtensionsGetSet()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
Extensions = new List<XmlElement>().ToArrayOf()
|
|
};
|
|
Assert.That(config.Extensions.IsNull, Is.False);
|
|
Assert.That(config.Extensions.Count, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Properties dictionary supports adding and retrieving values.
|
|
/// </summary>
|
|
public void PropertiesCanStoreAndRetrieveValues()
|
|
{
|
|
var config = new ApplicationConfiguration(m_telemetry);
|
|
config.Properties["key1"] = "value1";
|
|
Assert.That(config.Properties["key1"], Is.EqualTo("value1"));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Copy constructor copies ServerConfiguration reference.
|
|
/// </summary>
|
|
public void CopyConstructorCopiesServerConfiguration()
|
|
{
|
|
var serverConfig = new ServerConfiguration();
|
|
var original = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "CopyTest",
|
|
ServerConfiguration = serverConfig
|
|
};
|
|
|
|
var copy = new ApplicationConfiguration(original);
|
|
Assert.That(copy.ServerConfiguration, Is.SameAs(serverConfig));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Copy constructor copies ClientConfiguration reference.
|
|
/// </summary>
|
|
public void CopyConstructorCopiesClientConfiguration()
|
|
{
|
|
var clientConfig = new ClientConfiguration();
|
|
var original = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "CopyTest",
|
|
ClientConfiguration = clientConfig
|
|
};
|
|
|
|
var copy = new ApplicationConfiguration(original);
|
|
Assert.That(copy.ClientConfiguration, Is.SameAs(clientConfig));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Copy constructor copies CertificateManager by reference.
|
|
/// </summary>
|
|
public void CopyConstructorCopiesCertificateManager()
|
|
{
|
|
using var manager = new CertificateManager(m_telemetry);
|
|
var original = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "CopyTest",
|
|
CertificateManager = manager
|
|
};
|
|
|
|
var copy = new ApplicationConfiguration(original);
|
|
Assert.That(copy.CertificateManager, Is.SameAs(manager));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// Copy constructor copies TransportQuotas reference.
|
|
/// </summary>
|
|
public void CopyConstructorCopiesTransportQuotas()
|
|
{
|
|
var quotas = new TransportQuotas();
|
|
var original = new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "CopyTest",
|
|
TransportQuotas = quotas
|
|
};
|
|
|
|
var copy = new ApplicationConfiguration(original);
|
|
Assert.That(copy.TransportQuotas, Is.SameAs(quotas));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// TransportQuotas default values match DefaultEncodingLimits.
|
|
/// </summary>
|
|
public void TransportQuotasDefaultValues()
|
|
{
|
|
var quotas = new TransportQuotas();
|
|
Assert.That(quotas.MaxArrayLength, Is.EqualTo(DefaultEncodingLimits.MaxArrayLength));
|
|
Assert.That(quotas.MaxStringLength, Is.EqualTo(DefaultEncodingLimits.MaxStringLength));
|
|
Assert.That(quotas.MaxByteStringLength, Is.EqualTo(DefaultEncodingLimits.MaxByteStringLength));
|
|
Assert.That(quotas.MaxMessageSize, Is.EqualTo(DefaultEncodingLimits.MaxMessageSize));
|
|
Assert.That(quotas.MaxEncodingNestingLevels, Is.EqualTo(DefaultEncodingLimits.MaxEncodingNestingLevels));
|
|
Assert.That(quotas.MaxDecoderRecoveries, Is.EqualTo(DefaultEncodingLimits.MaxDecoderRecoveries));
|
|
}
|
|
|
|
[Test]
|
|
/// <summary>
|
|
/// GetFilePathFromAppConfig returns a non-null path.
|
|
/// </summary>
|
|
public void GetFilePathFromAppConfigReturnsPath()
|
|
{
|
|
string path = ApplicationConfiguration.GetFilePathFromAppConfig(
|
|
"TestSection",
|
|
m_telemetry.CreateLogger<ApplicationConfigurationTests>());
|
|
Assert.That(path, Is.Not.Null);
|
|
Assert.That(path, Does.Contain("TestSection"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// ValidateAsync defaults TransportQuotas when it is null so the transport
|
|
/// layer does not fail with a NullReferenceException on server start.
|
|
/// </summary>
|
|
[Test]
|
|
public async Task ValidateAsyncDefaultsTransportQuotasWhenNull()
|
|
{
|
|
string pkiPath = Path.Combine(
|
|
Path.GetTempPath(),
|
|
"OpcUaTestTransportQuotas_" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
ApplicationConfiguration config = CreateValidatableServerConfig(pkiPath);
|
|
config.TransportQuotas = null;
|
|
|
|
await config.ValidateAsync(ApplicationType.Server).ConfigureAwait(false);
|
|
|
|
Assert.That(config.TransportQuotas, Is.Not.Null);
|
|
Assert.That(
|
|
config.TransportQuotas!.MaxMessageSize,
|
|
Is.EqualTo(DefaultEncodingLimits.MaxMessageSize));
|
|
}
|
|
finally
|
|
{
|
|
TryDeleteDirectory(pkiPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ValidateAsync preserves an explicitly configured TransportQuotas instance.
|
|
/// </summary>
|
|
[Test]
|
|
public async Task ValidateAsyncKeepsExplicitTransportQuotas()
|
|
{
|
|
string pkiPath = Path.Combine(
|
|
Path.GetTempPath(),
|
|
"OpcUaTestTransportQuotas_" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
ApplicationConfiguration config = CreateValidatableServerConfig(pkiPath);
|
|
var quotas = new TransportQuotas { MaxMessageSize = 1234567 };
|
|
config.TransportQuotas = quotas;
|
|
|
|
await config.ValidateAsync(ApplicationType.Server).ConfigureAwait(false);
|
|
|
|
Assert.That(config.TransportQuotas, Is.SameAs(quotas));
|
|
}
|
|
finally
|
|
{
|
|
TryDeleteDirectory(pkiPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ValidateAsync defaults TransportQuotas when it is null on the client path,
|
|
/// ensuring the behavior is consistent regardless of ApplicationType.
|
|
/// </summary>
|
|
[Test]
|
|
public async Task ValidateAsyncDefaultsTransportQuotasWhenNullForClient()
|
|
{
|
|
string pkiPath = Path.Combine(
|
|
Path.GetTempPath(),
|
|
"OpcUaTestTransportQuotasClient_" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
ApplicationConfiguration config = CreateValidatableClientConfig(pkiPath);
|
|
config.TransportQuotas = null;
|
|
|
|
await config.ValidateAsync(ApplicationType.Client).ConfigureAwait(false);
|
|
|
|
Assert.That(config.TransportQuotas, Is.Not.Null);
|
|
Assert.That(
|
|
config.TransportQuotas!.MaxMessageSize,
|
|
Is.EqualTo(DefaultEncodingLimits.MaxMessageSize));
|
|
}
|
|
finally
|
|
{
|
|
TryDeleteDirectory(pkiPath);
|
|
}
|
|
}
|
|
|
|
private ApplicationConfiguration CreateValidatableClientConfig(string pkiPath)
|
|
{
|
|
const string applicationName = "TransportQuotasTestClient";
|
|
return new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = applicationName,
|
|
ApplicationUri = "urn:test:" + applicationName,
|
|
ApplicationType = ApplicationType.Client,
|
|
ClientConfiguration = new ClientConfiguration(),
|
|
SecurityConfiguration = new SecurityConfiguration
|
|
{
|
|
ApplicationCertificate = new CertificateIdentifier
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "own"),
|
|
SubjectName = "CN=" + applicationName
|
|
},
|
|
TrustedPeerCertificates = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "trusted")
|
|
},
|
|
TrustedIssuerCertificates = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "issuers")
|
|
},
|
|
RejectedCertificateStore = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "rejected")
|
|
},
|
|
AutoAcceptUntrustedCertificates = true
|
|
}
|
|
};
|
|
}
|
|
|
|
private ApplicationConfiguration CreateValidatableServerConfig(string pkiPath)
|
|
{
|
|
const string applicationName = "TransportQuotasTestServer";
|
|
return new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = applicationName,
|
|
ApplicationUri = "urn:test:" + applicationName,
|
|
ApplicationType = ApplicationType.Server,
|
|
ServerConfiguration = new ServerConfiguration(),
|
|
SecurityConfiguration = new SecurityConfiguration
|
|
{
|
|
ApplicationCertificate = new CertificateIdentifier
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "own"),
|
|
SubjectName = "CN=" + applicationName
|
|
},
|
|
TrustedPeerCertificates = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "trusted")
|
|
},
|
|
TrustedIssuerCertificates = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "issuers")
|
|
},
|
|
RejectedCertificateStore = new CertificateTrustList
|
|
{
|
|
StoreType = CertificateStoreType.Directory,
|
|
StorePath = Path.Combine(pkiPath, "rejected")
|
|
},
|
|
AutoAcceptUntrustedCertificates = true
|
|
}
|
|
};
|
|
}
|
|
|
|
private static void TryDeleteDirectory(string path)
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(path))
|
|
{
|
|
Directory.Delete(path, true);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// best effort cleanup
|
|
}
|
|
}
|
|
|
|
private ApplicationConfiguration CreateMinimalValidatableConfig()
|
|
{
|
|
return new ApplicationConfiguration(m_telemetry)
|
|
{
|
|
ApplicationName = "TestApp",
|
|
ApplicationUri = "urn:test:app"
|
|
};
|
|
}
|
|
}
|
|
}
|