mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-23 10:23:13 +08:00
20c2d77d57
> **Stacked on #4569.** The base branch is `romanett/practical-murdock-fd9c56`, so this diff shows only the commits of this PR. Retarget to `master` after #4569 merges. ## Summary Bug audit of `Opc.Ua.Core`, `Opc.Ua.Core.Types`, `Opc.Ua.Types` and `Opc.Ua.Core.Schema`, with the findings fixed and the fixes grounded against the OPC UA specification (reference.opcfoundation.org). 133 commits in four groups: 1. **Audit fixes** (`Fix <ID>: ...`): 116 findings from a read-only audit, each with a regression test. IDs follow the audit scopes: T1-T6 `Opc.Ua.Types`, C1-C4 `Opc.Ua.Core`, S1 `Opc.Ua.Core.Types` + `Opc.Ua.Core.Schema`. 2. **Review fixes** (`Fix review <ID>: ...`): issues a code review of group 1 found, plus lower-severity leftovers. 3. **Spec fixes** (`Spec: #<n> ...`): the 18 places where an earlier fix only partly matched the spec text. 4. Test and analyzer follow-ups. Where `master` (#4516, #4522, #4540, #4564) or #4569 already fixed the same issue, their version was kept and this PR only adds what they didn't cover. ## Behaviour and user-facing changes ### Wire format and interoperability (breaking) - **PubSub AES-CTR (Aes128-CTR / Aes256-CTR)** now follows Part 14 Tables 156/157: - The counter block is `KeyNonce[4] | MessageNonce[8] | BlockCounter[4]`, and the block counter starts at 1. - The SecurityHeader nonce is 8 bytes: `Random[4] | UInt32 SequenceNumber`, with the sequence number starting at 1. - SKS key data is split with a 4-byte KeyNonce, giving 52 / 68 bytes. - **Not interoperable with earlier releases of this stack**, but matches the spec. There is a test against the independent RFC 3686 vector. - **User token signature** (Part 4 6.1.8): - The signed data is now built from the user token policy's SecurityPolicy on both client and server. - It also supports the SecurityMode None layout `ServerNonce | HASH(ServerCertificate) | ClientNonce`. - New overload `SecurityPolicyInfo.GetUserTokenSignatureData(..., MessageSecurityMode)`. - **Secure channel:** - The ECC AEAD `LastSequenceNumber` is correct across the 32-bit sequence wrap (Part 6 6.8.1). - The incoming chunk counts against MaxChunkCount/MaxMessageSize. - The sender no longer lets a message through that is 24 bytes over MaxMessageSize. - The OPN response signature is verified without overrunning the buffer (RSA_DH with differing key sizes). ### Sessions (server) - **CreateSession nonce:** - The client nonce is kept on None channels, because the None-mode user token signature covers it. - A nonce over 128 bytes returns `Bad_NonceInvalid` on every channel (Part 4 5.7.2.3). - The duplicate-nonce check runs only on secured channels. - A non-empty nonce under 32 bytes is still accepted on None, for compatibility. - **ActivateSession:** an embedded `EccEncryptedSecret` signing certificate is validated with the server's peer certificate validator (Part 6 6.8.3). Byte equality with the client certificate is only the fallback when no validator is available. New opt-in interface `ICertificateValidatorProvider`. - **FindServers / GetEndpoints:** `GatewayServerUri` and `DiscoveryProfileUri` are no longer swapped. ### Write, Read and Call (NodeState / node managers) - **UserWriteMask:** - Enforced for non-value attributes and for VariableType Value writes (`ValueForVariableType`), returning `Bad_UserAccessDenied`. - An unconfigured UserWriteMask (0, no handler) is not enforced, and reads back as the WriteMask so Read and Write agree (Part 3 8.60). - A throwing or Bad `OnReadUserWriteMask` handler fails only that item. - **Index-range writes:** - Slices are merged into the cached value under the attribute lock, also for `OnWriteValueAsync` handlers. - A data-change notification is still produced when the cache cannot absorb the slice. - The slice must match the unclipped range, otherwise `Bad_IndexRangeDataMismatch` (new status code in `Opc.Ua.Types`). - A range past the target returns `Bad_IndexRangeNoData`. - A handler without index-range support returns `Bad_WriteNotSupported` (Part 4 7.27, 5.11.4.4). - **Index-range reads on matrices** return partial results clipped to the source, instead of allocating the requested size (a Read with `"0:40000,0:40000"` no longer allocates about 1.6e9 elements). - **DataTypeDefinition writes:** - A value that isn't a decoded DataTypeDefinition, or whose kind doesn't match the DataType category, returns `Bad_TypeMismatch`. - Null is rejected for concrete Structure/Enumeration/OptionSet types. - WriteMask is checked first (Part 3 5.8.3). - **AnalogItem:** a null write skips the InstrumentRange check again. - **Method call:** the async fallback passes the ObjectId to the method handlers. - **Redundancy:** `DistributedValueParticipation` merges index-range writes and applies the index range on reads. - **Change masks:** `ClearChangeMasks` takes the bits atomically, so concurrent changes are no longer lost. - **`AccessLevelEx`:** preserved by Clone/Export. ### Alarms & Conditions (Part 9) - **Acknowledge / Confirm / AddComment:** - Accept any EventId the condition or its branches reported while that state is still pending (5.7.3). - An unknown id returns `Bad_EventIdUnknown`. - An already handled state returns `Bad_ConditionBranchAlreadyAcked` / `...AlreadyConfirmed`. - AddComment on a branch no longer changes the trunk, and the branch table is re-keyed when a branch's EventId changes. - **Shelving:** - OneShotShelve without MaxTimeShelved no longer overflows the timer. - A timed shelve is measured on the monotonic clock, so wall-clock steps don't delay the unshelve. - The state is named `OneShotShelved`. - **Audit events:** - They report failure for rejected method calls. - `SelectedResponse` is a UInt32. - A rejected negative Respond leaves SelectedResponse unset. - **`ContentFilterElement.Validate`:** an unknown FilterOperator is reported per element as `Bad_FilterOperatorInvalid`, instead of failing the whole service call. ### ContentFilter evaluation (Part 4 7.7.3, on top of #4564) - **Bitwise operators:** - The result takes the size of the larger operand. - Boolean, numeric String and one-element array operands are converted to integers implicitly. - Types without an implicit integer conversion give NULL. - **Strings:** ordering is ordinal, no longer culture dependent. - **Cast:** restricted to the Table 121 conversions, and QualifiedName to LocalizedText is added. The general `Variant.ConvertTo` keeps its wider set. - **Null and empty operands:** - `Like` with a NULL operand is NULL. - Empty arrays of the same DataType compare equal. - Null-string operands are NULL everywhere. ### Encoders and built-in types - **XML:** - Strings keep leading, trailing and whitespace-only content (Part 6 5.3.1.5). NodeSet import opts back into treating whitespace-only strings as empty with the new `XmlDecoder.TreatWhitespaceOnlyStringsAsEmpty`. - Layout whitespace around `<Identifier>` values is ignored, and trailing control characters are stripped from `s=` ids. - `XmlElement` ignores DTDs without expanding entities. - **JSON:** - A DateTime without an offset is read as UTC. - An unmappable `nsu=` NodeId and an unknown `svu=` ExpandedNodeId decode to a namespace-0 String NodeId (5.4.2.10 / 5.4.2.11). - `svr=` is rejected in a NodeId field. - Bad matrix dimensions return `BadDecodingError`. - The non-strict body stack stays balanced after an error. - The nesting limits match between encoder and decoder. - **Binary:** - `MaxByteStringLength` no longer applies to strings. - ExtensionObject bodies count toward the nesting limit on seekable streams (and in the XML encoder). - **Parsing:** - The NodeId parser requires `i=`/`s=`/`g=`/`b=`. - `ExpandedNodeId.Parse` rejects an empty `nsu=`/`svu=`. - NumericRange rejects a trailing `,` with `Bad_IndexRangeInvalid`. - RelativePath escapes `#`. - **Status codes:** `StatusCode.WithSubCode`/`WithCodeBits` keep the severity bits and look up the symbolic id for the new code. - **Variant / VariantHelper:** - `TryCastTo` returns false on a type mismatch. So `DataValue.GetValue<T>(default)` returns the default, and `GetValueOrDefault` / `CastTo` throw `BadTypeMismatch` as documented. - `System.DateTime` casts are supported. - Concrete encodeable arrays (e.g. `Argument[]`) no longer throw. - `ConvertTo` keeps one-element and empty arrays as arrays and checks the conversion is legal for empty arrays. - `CastFrom(object[])`, `CastFrom(Enum[])` and the obsolete `Variant(object)` constructors keep the array shape (previously a one-element array collapsed to a scalar and an empty one to Null). - `TypeInfo.Unknown` / `Variant.Null.TypeInfo` report `ValueRanks.Any`. - **Equality and hashing contracts fixed:** - Types: Matrix/MatrixOf, typed-null Variants, NodeId (the namespace index is now hashed), ExpandedNodeId, LocalizedText, DiagnosticInfo, StructureDefinition/EnumDefinition, CertificateIdentifier, UserIdentity and DateTimeUtc. - `MatrixOf.Dimensions` returns a copy. - Decimal equality and hashing now run in bounded time. - **Localized text:** a LocalizedText with locale `"mul"` and no usable translations, or null client locale ids, no longer throw. ### NodeSet, address space and utilities - **NodeSet export/import:** - Server index 0 is the local server on import and export, so local ids are no longer written as `svr=65535`. - `StructureDefinition.BaseDataType` is filled from the supertype on import. - **`NodeTable.Import`:** replaces duplicates, keeping the references neighbouring nodes hold. `TypeTable.Add` detaches a re-parented type from its old supertype, and `Node.Clone` gets its own ReferenceTable. - **`StringTable` / `NamespaceTable`:** capped at UInt16 indexes (`BadEncodingLimitsExceeded`) for Append and Update, instead of silently wrapping. - **Small fixes:** - `UnsecureRandom.Shared` is seeded randomly. - The peer's RetryAfterMs is clamped to one day. - The `CancelAsync` and `string.GetHashCode(StringComparison)` polyfills match BCL behaviour. - `CoreUtils.IsEqual` compares strings ordinally. ### Schema generation and ComplexTypeSystem - **JSON schema:** describes the 1.05 layout with the compact/verbose null rules, and now validates real `JsonEncoder` output. - **XSD:** - `EncodingMask` (`xs:unsignedLong`) only for StructureWithOptionalFields. - Guid and StatusCode are complex types. - Standard types are in the `Types.xsd` namespace. - **Structure-backed OptionSets** are described as `{Value, ValidBits}`. New `UaTypeDescription.IsStructureOptionSet` property and constructor overload. - **ComplexTypeSystem:** - A malformed type in a server dictionary is skipped per type instead of discarding the whole dictionary. - Cyclic supertype walks are bounded. - Declared definitions are no longer changed. - A dictionary validator is kept only if the dictionary parsed. ### Certificates and trust lists (Part 4 6.1.3, Part 12) - **Revocation:** - Checked for issuers supplied in the peer's chain and for inline-trusted issuers, against the CRLs in the trusted and issuer stores. - A store without CRL support no longer hides "revocation unknown". - `RejectUnknownRevocationStatus` is honoured for inline-trusted issuers unless the entry sets `SuppressRevocationStatusUnknown`. This changes the strict case of `ExplicitIssuerChainIsHonoredWithoutStorePathAsync`. - **Basic constraints / path length** violations fail with `Bad_CertificateInvalid`. - **`TreatAsInvalid`:** - Now yields `Bad_CertificateUntrusted`, which can't be suppressed or auto-accepted. - It is honoured on individual trust-list entries even when the certificate is also in a store. - **`EccEncryptedSecret`:** enforces the result of validating the sender certificate. - **`TrustListTransaction`:** - Routes each CRL to the store holding its issuer (TrustedCrls/IssuerCrls). - Validates staged CRLs, including the store's CRL support, before changing anything. - Applies certificates before CRLs. - **Other:** - One failing `CertificateChanges` observer no longer stops the others. - `InMemory:` stores share their contents per path, and `InMemoryStoreProvider` is `IDisposable`. ### WoT (`Opc.Ua.Types/Wot`) - **Projection security:** projection security schemes whose qualified names collide are refused. New diagnostic `WotDiagnosticCode.ProjectionSecurityConflict = 6063`. - **Input handling:** - Invalid Unicode (lone surrogates, bad UTF-8) is rejected as `JsonException`. - External-schema results are keyed per affordance. - A duplicate security-floor member is rejected. - `nsu=` ids are percent-encoded (Part 6 5.1.12). - The configured JSON depth is honoured on re-parse. - An oversized document is reported before it is parsed back. - **Performance and limits:** - Subtype-graph validation, `componentOf` linking and the namespace table are now linear. - Namespaces are capped at UInt16. ### New or changed public API - `ICertificateValidatorProvider` (server) - `SecurityPolicyInfo.GetUserTokenSignatureData(..., MessageSecurityMode)` overload - `XmlDecoder.TreatWhitespaceOnlyStringsAsEmpty` - `UaTypeDescription.IsStructureOptionSet` plus a constructor overload - `WotDiagnosticCode.ProjectionSecurityConflict` - `StatusCodes.BadIndexRangeDataMismatch` in the `Opc.Ua.Types` list - `InMemoryStoreProvider : IDisposable` ## Not changed / open - **Deferred:** - S1-14 (BSD OptionSet size needs the base type in `UaTypeDescription`, and BSD is deprecated). - C2-7 (restoring the own-certificate trust exemption would accept certificates the spec says to reject; removing the dead code is a separate decision). - A1-4 (config values keep surrounding whitespace, as the pre-April-2026 loader did). - H-2 (JsonDecoder has no real nesting counter). - A2-3 (the StringTable cap also applies to diagnostics tables; default `MaxArrayLength` already bounds them). - **Needs a product decision:** - The general `ConvertToString` writes a QualifiedName as `ns:Name`, while Table 121 drops the namespace. - Short (< 32 byte) client nonces are accepted on None channels. ## Verification - Every commit added a regression test that fails without its fix, where practical. - Before the rebases, with all audit and review fixes (before the `Spec: #…` commits): 0 build warnings and 16 test suites green on net10.0. The suites: Types, Core, Core.Encoders, Core.Security, Core.Schema, Client, Client.ComplexTypes, Server, Sessions, Gds, History, Features, Redundancy.Server, PubSub, WotCon and WotCon.Bindings. Core hit its known intermittent end-of-run hang once, and reruns were clean. - The `Spec: #…` fixes were verified per group on their own suites. Those include PubSub.Udp, Redundancy.PubSub, PubSub.Server, Configuration and SourceGeneration.Core. - **Not yet built or tested on this base.** It was rebased onto master (resolving overlaps with #4522, #4540 and #4564) and then onto #4569. This PR stays a draft until the full run on this base is green. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: marcschier <11168470+marcschier@users.noreply.github.com> Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
1064 lines
42 KiB
C#
1064 lines
42 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/
|
|
* ======================================================================*/
|
|
|
|
#pragma warning disable CA2000
|
|
using System;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Features.Tests
|
|
{
|
|
/// <summary>
|
|
/// Tests for Part 9 alarm methods on the AlarmConditionState class:
|
|
/// Silence, Suppress/Unsuppress, OutOfService, Reset, GetGroupMemberships,
|
|
/// Latched alarms, ReAlarm helpers, Audible state.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("AlarmConditionState")]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
[Parallelizable]
|
|
public class AlarmConditionStateMethodTests
|
|
{
|
|
private ISystemContext m_context;
|
|
private ITelemetryContext m_telemetry;
|
|
|
|
[OneTimeSetUp]
|
|
protected void OneTimeSetUp()
|
|
{
|
|
m_telemetry = NUnitTelemetryContext.Create();
|
|
var messageContext = ServiceMessageContext.Create(m_telemetry);
|
|
messageContext.NamespaceUris.GetIndexOrAppend(Namespaces.OpcUa);
|
|
m_context = new SystemContext(m_telemetry)
|
|
{
|
|
NamespaceUris = messageContext.NamespaceUris
|
|
};
|
|
}
|
|
|
|
[OneTimeTearDown]
|
|
protected void OneTimeTearDown()
|
|
{
|
|
(m_context as IDisposable)?.Dispose();
|
|
}
|
|
|
|
private AlarmConditionState CreateAlarm()
|
|
{
|
|
var alarm = new AlarmConditionState(m_telemetry, null);
|
|
alarm.Create(m_context, new NodeId(1), QualifiedName.From("Alarm"), default, true);
|
|
alarm.SetEnableState(m_context, true);
|
|
return alarm;
|
|
}
|
|
|
|
private void AddTwoStateChild(AlarmConditionState alarm, string browseName, Action<TwoStateVariableState> setter)
|
|
{
|
|
var state = new TwoStateVariableState(alarm);
|
|
state.Create(m_context, default, QualifiedName.From(browseName), default, false);
|
|
setter(state);
|
|
}
|
|
|
|
[Test]
|
|
public void SetSilenceStateUpdatesValueAndTransitionTime()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
|
|
DateTimeUtc before = DateTimeUtc.Now;
|
|
alarm.SetSilenceState(m_context, true);
|
|
DateTimeUtc after = DateTimeUtc.Now;
|
|
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.True);
|
|
Assert.That(alarm.SilenceState.Timestamp, Is.GreaterThanOrEqualTo(before));
|
|
Assert.That(alarm.SilenceState.Timestamp, Is.LessThanOrEqualTo(after));
|
|
Assert.That(alarm.ChangeMasks, Is.EqualTo(NodeStateChangeMasks.None));
|
|
}
|
|
|
|
[Test]
|
|
public void SetSilenceStateWithoutSilenceVariableIsNoOp()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
// No SilenceState child created
|
|
|
|
Assert.DoesNotThrow(() => alarm.SetSilenceState(m_context, true));
|
|
}
|
|
|
|
[Test]
|
|
public void NewActivationClearsSilenceState()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
|
|
alarm.SetSilenceState(m_context, true);
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.True);
|
|
|
|
// New activation should clear silence
|
|
alarm.SetActiveState(m_context, true);
|
|
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void SetOutOfServiceStateUpdatesSuppressedOrShelved()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.False);
|
|
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.True);
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.True, "OutOfService must set SuppressedOrShelved");
|
|
|
|
alarm.SetOutOfServiceState(m_context, false);
|
|
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.False);
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.False, "Placing in service clears SuppressedOrShelved");
|
|
}
|
|
|
|
[Test]
|
|
public void OutOfServiceDoesNotClearSuppressedOrShelvedWhenAlsoSuppressed()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
|
|
alarm.SetSuppressedState(m_context, true);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.True);
|
|
|
|
// Place in service - suppressed remains, so SuppressedOrShelved must remain true
|
|
alarm.SetOutOfServiceState(m_context, false);
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.True, "Still suppressed");
|
|
|
|
// Unsuppress - now should clear
|
|
alarm.SetSuppressedState(m_context, false);
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void UnsuppressDoesNotClearSuppressedOrShelvedWhenOutOfService()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
|
|
alarm.SetSuppressedState(m_context, true);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.True);
|
|
|
|
alarm.SetSuppressedState(m_context, false);
|
|
Assert.That(alarm.SuppressedOrShelved.Value, Is.True, "OutOfService keeps SuppressedOrShelved");
|
|
}
|
|
|
|
[Test]
|
|
public void SetLatchedStateUpdatesValueAndTimestamp()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
|
|
DateTimeUtc before = DateTimeUtc.Now;
|
|
alarm.SetLatchedState(m_context, true);
|
|
DateTimeUtc after = DateTimeUtc.Now;
|
|
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True);
|
|
Assert.That(alarm.LatchedState.Timestamp, Is.GreaterThanOrEqualTo(before));
|
|
Assert.That(alarm.LatchedState.Timestamp, Is.LessThanOrEqualTo(after));
|
|
}
|
|
|
|
[Test]
|
|
public void ActivationSetsLatchedStateTrueWhenPresent()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.False);
|
|
|
|
alarm.SetActiveState(m_context, true);
|
|
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True, "Active alarm with LatchedState becomes latched");
|
|
}
|
|
|
|
[Test]
|
|
public void DeactivationDoesNotClearLatchedState()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
|
|
alarm.SetActiveState(m_context, true);
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True);
|
|
|
|
alarm.SetActiveState(m_context, false);
|
|
|
|
Assert.That(alarm.ActiveState.Id.Value, Is.False, "ActiveState reflects real process state");
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True, "LatchedState persists across deactivation");
|
|
}
|
|
|
|
[Test]
|
|
public void LatchedAlarmIsRetained()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
|
|
alarm.SetActiveState(m_context, true);
|
|
alarm.SetActiveState(m_context, false);
|
|
|
|
// Retain is updated via state setters which call UpdateRetainState internally;
|
|
// verify LatchedState alone keeps the alarm in retained state.
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void IsReAlarmEnabledReflectsReAlarmTime()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
|
|
Assert.That(alarm.IsReAlarmEnabled, Is.False, "Without ReAlarmTime, re-alarm is disabled");
|
|
|
|
alarm.ReAlarmTime = PropertyState<double>.With<VariantBuilder>(alarm);
|
|
alarm.ReAlarmTime.Create(m_context, default, QualifiedName.From(BrowseNames.ReAlarmTime), default, false);
|
|
alarm.ReAlarmTime.Value = 1000.0;
|
|
|
|
Assert.That(alarm.IsReAlarmEnabled, Is.True);
|
|
|
|
alarm.ReAlarmTime.Value = 0;
|
|
Assert.That(alarm.IsReAlarmEnabled, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void ProcessReAlarmIncrementsRepeatCount()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
alarm.ReAlarmRepeatCount = BaseDataVariableState<short>.With<VariantBuilder>(alarm);
|
|
alarm.ReAlarmRepeatCount.Create(m_context, default, QualifiedName.From(BrowseNames.ReAlarmRepeatCount), default, false);
|
|
alarm.ReAlarmRepeatCount.Value = 0;
|
|
alarm.SetActiveState(m_context, true);
|
|
|
|
alarm.ProcessReAlarm(m_context);
|
|
|
|
Assert.That(alarm.ReAlarmRepeatCount.Value, Is.EqualTo((short)1));
|
|
|
|
alarm.ProcessReAlarm(m_context);
|
|
|
|
Assert.That(alarm.ReAlarmRepeatCount.Value, Is.EqualTo((short)2));
|
|
}
|
|
|
|
[Test]
|
|
public void ProcessReAlarmIsNoOpWhenInactive()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
alarm.ReAlarmRepeatCount = BaseDataVariableState<short>.With<VariantBuilder>(alarm);
|
|
alarm.ReAlarmRepeatCount.Create(m_context, default, QualifiedName.From(BrowseNames.ReAlarmRepeatCount), default, false);
|
|
alarm.ReAlarmRepeatCount.Value = 0;
|
|
|
|
// Inactive alarm
|
|
alarm.ProcessReAlarm(m_context);
|
|
|
|
Assert.That(alarm.ReAlarmRepeatCount.Value, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public void ResetReAlarmRepeatCountZeroes()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
alarm.ReAlarmRepeatCount = BaseDataVariableState<short>.With<VariantBuilder>(alarm);
|
|
alarm.ReAlarmRepeatCount.Create(m_context, default, QualifiedName.From(BrowseNames.ReAlarmRepeatCount), default, false);
|
|
alarm.ReAlarmRepeatCount.Value = 5;
|
|
|
|
alarm.ResetReAlarmRepeatCount(m_context);
|
|
|
|
Assert.That(alarm.ReAlarmRepeatCount.Value, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAudibleStateNoOpWhenAudibleDisabled()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
alarm.SetSilenceState(m_context, true);
|
|
|
|
// AudibleEnabled not configured — should not affect silence
|
|
alarm.UpdateAudibleState(m_context, true);
|
|
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.True, "No change without AudibleEnabled");
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAudibleStateClearsSilenceOnActivation()
|
|
{
|
|
AlarmConditionState alarm = CreateAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
alarm.AudibleEnabled = PropertyState<bool>.With<VariantBuilder>(alarm);
|
|
alarm.AudibleEnabled.Create(m_context, default, QualifiedName.From(BrowseNames.AudibleEnabled), default, false);
|
|
alarm.AudibleEnabled.Value = true;
|
|
|
|
alarm.SetSilenceState(m_context, true);
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.True);
|
|
|
|
alarm.UpdateAudibleState(m_context, true);
|
|
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.False, "Audible activation clears silence");
|
|
}
|
|
|
|
// -----------------------------------------------------------------
|
|
// On*Called audit handler coverage
|
|
// -----------------------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// Test-only subclass that exposes the protected On*Called handlers
|
|
/// so the handler logic can be exercised directly.
|
|
/// </summary>
|
|
private sealed class TestableAlarm : AlarmConditionState
|
|
{
|
|
public TestableAlarm(ITelemetryContext telemetry)
|
|
: base(telemetry, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// A minimal method node with a valid NodeId so the On*Called
|
|
/// handlers can populate audit events (which read method.NodeId when
|
|
/// EventsMonitored() is true) without a NullReferenceException.
|
|
/// </summary>
|
|
private static readonly MethodState s_method = new(null) { NodeId = NodeId.Null };
|
|
|
|
public ServiceResult CallSilence(ISystemContext c)
|
|
{
|
|
return OnSilenceCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallSuppress(ISystemContext c)
|
|
{
|
|
return OnSuppressCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallSuppress2(ISystemContext c, LocalizedText comment)
|
|
{
|
|
return OnSuppress2Called(c, s_method, NodeId.Null, comment);
|
|
}
|
|
|
|
public ServiceResult CallUnsuppress(ISystemContext c)
|
|
{
|
|
return OnUnsuppressCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallUnsuppress2(ISystemContext c, LocalizedText comment)
|
|
{
|
|
return OnUnsuppress2Called(c, s_method, NodeId.Null, comment);
|
|
}
|
|
|
|
public ServiceResult CallRemoveFromService(ISystemContext c)
|
|
{
|
|
return OnRemoveFromServiceCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallRemoveFromService2(ISystemContext c, LocalizedText comment)
|
|
{
|
|
return OnRemoveFromService2Called(c, s_method, NodeId.Null, comment);
|
|
}
|
|
|
|
public ServiceResult CallPlaceInService(ISystemContext c)
|
|
{
|
|
return OnPlaceInServiceCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallPlaceInService2(ISystemContext c, LocalizedText comment)
|
|
{
|
|
return OnPlaceInService2Called(c, s_method, NodeId.Null, comment);
|
|
}
|
|
|
|
public ServiceResult CallReset(ISystemContext c)
|
|
{
|
|
return OnResetCalled(c, s_method, default, []);
|
|
}
|
|
|
|
public ServiceResult CallReset2(ISystemContext c, LocalizedText comment)
|
|
{
|
|
return OnReset2Called(c, s_method, NodeId.Null, comment);
|
|
}
|
|
|
|
public ServiceResult CallGetGroupMemberships(ISystemContext c, ref ArrayOf<NodeId> groups)
|
|
{
|
|
return OnGetGroupMembershipsCalled(c, s_method, NodeId.Null, ref groups);
|
|
}
|
|
|
|
public ServiceResult CallAcknowledge(ISystemContext c, ByteString eventId, LocalizedText comment)
|
|
{
|
|
return OnAcknowledgeCalled(c, s_method, NodeId.Null, eventId, comment);
|
|
}
|
|
}
|
|
|
|
private TestableAlarm CreateTestableAlarm()
|
|
{
|
|
var alarm = new TestableAlarm(m_telemetry);
|
|
alarm.Create(m_context, new NodeId(1), QualifiedName.From("Alarm"), default, true);
|
|
alarm.SetEnableState(m_context, true);
|
|
return alarm;
|
|
}
|
|
|
|
private void EnsureComment(AlarmConditionState alarm)
|
|
{
|
|
if (alarm.Comment == null)
|
|
{
|
|
alarm.Comment = ConditionVariableState<LocalizedText>.With<VariantBuilder>(alarm);
|
|
alarm.Comment.Create(m_context, default, QualifiedName.From(BrowseNames.Comment), default, false);
|
|
}
|
|
}
|
|
|
|
// ---------- Silence ----------
|
|
|
|
[Test]
|
|
public void OnSilenceCalledReturnsBadNotSupportedWhenStateMissing()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
// SilenceState intentionally not added.
|
|
|
|
ServiceResult result = alarm.CallSilence(m_context);
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
public void OnSilenceCalledReturnsBadNothingToDoWhenAlreadySilenced()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
alarm.SetSilenceState(m_context, true);
|
|
|
|
ServiceResult result = alarm.CallSilence(m_context);
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNothingToDo));
|
|
}
|
|
|
|
[Test]
|
|
public void OnSilenceCalledInvokesRequestedDelegateAndTransitions()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
int callCount = 0;
|
|
alarm.OnSilenceRequested = (c, a) =>
|
|
{
|
|
callCount++;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = alarm.CallSilence(m_context);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void OnSilenceCalledVetoedByRequestedDelegateDoesNotTransition()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SilenceState, s => alarm.SilenceState = s);
|
|
alarm.OnSilenceRequested = (c, a) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = alarm.CallSilence(m_context);
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.SilenceState.Id.Value, Is.False, "Veto must not transition");
|
|
}
|
|
|
|
// ---------- Suppress / Suppress2 ----------
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnSuppressCalledReturnsBadNotSupportedWhenStateMissing(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
// SuppressedState intentionally not added.
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallSuppress(m_context)
|
|
: alarm.CallSuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnSuppressCalledReturnsBadNothingToDoWhenAlreadySuppressed(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
alarm.SetSuppressedState(m_context, true);
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallSuppress(m_context)
|
|
: alarm.CallSuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNothingToDo));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnSuppressCalledInvokesRequestedDelegateAndTransitions(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
int callCount = 0;
|
|
bool? suppressingArg = null;
|
|
alarm.OnSuppressRequested = (c, a, suppressing) =>
|
|
{
|
|
callCount++;
|
|
suppressingArg = suppressing;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallSuppress(m_context)
|
|
: alarm.CallSuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(suppressingArg, Is.True);
|
|
Assert.That(alarm.SuppressedState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnSuppressCalledVetoedByRequestedDelegateDoesNotTransition(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
alarm.OnSuppressRequested = (c, a, suppressing) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallSuppress(m_context)
|
|
: alarm.CallSuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.SuppressedState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void OnSuppress2CalledAppliesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
EnsureComment(alarm);
|
|
var comment = new LocalizedText("en", "operator suppressed");
|
|
|
|
ServiceResult result = alarm.CallSuppress2(m_context, comment);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment.Value, Is.EqualTo(comment));
|
|
}
|
|
|
|
// ---------- Unsuppress / Unsuppress2 ----------
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnUnsuppressCalledReturnsBadNotSupportedWhenStateMissing(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallUnsuppress(m_context)
|
|
: alarm.CallUnsuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnUnsuppressCalledReturnsBadNothingToDoWhenNotSuppressed(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
// Not suppressed.
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallUnsuppress(m_context)
|
|
: alarm.CallUnsuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNothingToDo));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnUnsuppressCalledInvokesRequestedDelegateAndTransitions(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
alarm.SetSuppressedState(m_context, true);
|
|
|
|
int callCount = 0;
|
|
bool? suppressingArg = null;
|
|
alarm.OnSuppressRequested = (c, a, suppressing) =>
|
|
{
|
|
callCount++;
|
|
suppressingArg = suppressing;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallUnsuppress(m_context)
|
|
: alarm.CallUnsuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(suppressingArg, Is.False);
|
|
Assert.That(alarm.SuppressedState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnUnsuppressCalledVetoedByRequestedDelegateDoesNotTransition(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
alarm.SetSuppressedState(m_context, true);
|
|
alarm.OnSuppressRequested = (c, a, suppressing) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallUnsuppress(m_context)
|
|
: alarm.CallUnsuppress2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.SuppressedState.Id.Value, Is.True, "Veto preserves suppressed state");
|
|
}
|
|
|
|
[Test]
|
|
public void OnUnsuppress2CalledAppliesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.SuppressedState, s => alarm.SuppressedState = s);
|
|
alarm.SetSuppressedState(m_context, true);
|
|
EnsureComment(alarm);
|
|
var comment = new LocalizedText("en", "operator unsuppressed");
|
|
|
|
ServiceResult result = alarm.CallUnsuppress2(m_context, comment);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment.Value, Is.EqualTo(comment));
|
|
}
|
|
|
|
// ---------- RemoveFromService / RemoveFromService2 ----------
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnRemoveFromServiceCalledReturnsBadNotSupportedWhenStateMissing(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallRemoveFromService(m_context)
|
|
: alarm.CallRemoveFromService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnRemoveFromServiceCalledReturnsBadNothingToDoWhenAlreadyOutOfService(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallRemoveFromService(m_context)
|
|
: alarm.CallRemoveFromService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNothingToDo));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnRemoveFromServiceCalledInvokesRequestedDelegateAndTransitions(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
int callCount = 0;
|
|
bool? outOfServiceArg = null;
|
|
alarm.OnOutOfServiceRequested = (c, a, oos) =>
|
|
{
|
|
callCount++;
|
|
outOfServiceArg = oos;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallRemoveFromService(m_context)
|
|
: alarm.CallRemoveFromService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(outOfServiceArg, Is.True);
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnRemoveFromServiceCalledVetoedByRequestedDelegateDoesNotTransition(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
alarm.OnOutOfServiceRequested = (c, a, oos) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallRemoveFromService(m_context)
|
|
: alarm.CallRemoveFromService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void OnRemoveFromService2CalledAppliesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
EnsureComment(alarm);
|
|
var comment = new LocalizedText("en", "down for maintenance");
|
|
|
|
ServiceResult result = alarm.CallRemoveFromService2(m_context, comment);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment.Value, Is.EqualTo(comment));
|
|
}
|
|
|
|
// ---------- PlaceInService / PlaceInService2 ----------
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnPlaceInServiceCalledReturnsBadNotSupportedWhenStateMissing(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallPlaceInService(m_context)
|
|
: alarm.CallPlaceInService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnPlaceInServiceCalledReturnsBadNothingToDoWhenAlreadyInService(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
// Default OutOfService=false (in service).
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallPlaceInService(m_context)
|
|
: alarm.CallPlaceInService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNothingToDo));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnPlaceInServiceCalledInvokesRequestedDelegateAndTransitions(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
|
|
int callCount = 0;
|
|
bool? outOfServiceArg = null;
|
|
alarm.OnOutOfServiceRequested = (c, a, oos) =>
|
|
{
|
|
callCount++;
|
|
outOfServiceArg = oos;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallPlaceInService(m_context)
|
|
: alarm.CallPlaceInService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(outOfServiceArg, Is.False);
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnPlaceInServiceCalledVetoedByRequestedDelegateDoesNotTransition(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
alarm.OnOutOfServiceRequested = (c, a, oos) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallPlaceInService(m_context)
|
|
: alarm.CallPlaceInService2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.OutOfServiceState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void OnPlaceInService2CalledAppliesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.OutOfServiceState, s => alarm.OutOfServiceState = s);
|
|
alarm.SetOutOfServiceState(m_context, true);
|
|
EnsureComment(alarm);
|
|
var comment = new LocalizedText("en", "back in service");
|
|
|
|
ServiceResult result = alarm.CallPlaceInService2(m_context, comment);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment.Value, Is.EqualTo(comment));
|
|
}
|
|
|
|
// ---------- Reset / Reset2 ----------
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnResetCalledReturnsBadNotSupportedWhenLatchedStateMissing(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallReset(m_context)
|
|
: alarm.CallReset2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnResetCalledReturnsBadInvalidStateWhenAlarmIsNotLatched(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallReset(m_context)
|
|
: alarm.CallReset2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadInvalidState));
|
|
}
|
|
|
|
private TestableAlarm CreateResettableAlarm()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
AddTwoStateChild(alarm, BrowseNames.LatchedState, s => alarm.LatchedState = s);
|
|
// Activate then deactivate so the alarm is latched but not active.
|
|
alarm.SetActiveState(m_context, true);
|
|
// Acknowledge / confirm so Reset prerequisites pass.
|
|
if (alarm.AckedState is { } acked)
|
|
{
|
|
acked.Id!.Value = true;
|
|
}
|
|
if (alarm.ConfirmedState is { } confirmed)
|
|
{
|
|
confirmed.Id!.Value = true;
|
|
}
|
|
alarm.SetActiveState(m_context, false);
|
|
return alarm;
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnResetCalledInvokesRequestedDelegateAndClearsLatch(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateResettableAlarm();
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True, "precondition: alarm is latched");
|
|
|
|
int callCount = 0;
|
|
alarm.OnResetRequested = (c, a) =>
|
|
{
|
|
callCount++;
|
|
return ServiceResult.Good;
|
|
};
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallReset(m_context)
|
|
: alarm.CallReset2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(callCount, Is.EqualTo(1));
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
[TestCase("v1")]
|
|
[TestCase("v2")]
|
|
public void OnResetCalledVetoedByRequestedDelegateDoesNotClearLatch(string variant)
|
|
{
|
|
TestableAlarm alarm = CreateResettableAlarm();
|
|
alarm.OnResetRequested = (c, a) => StatusCodes.BadUserAccessDenied;
|
|
|
|
ServiceResult result = variant == "v1"
|
|
? alarm.CallReset(m_context)
|
|
: alarm.CallReset2(m_context, new LocalizedText("en", "x"));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadUserAccessDenied));
|
|
Assert.That(alarm.LatchedState.Id.Value, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void OnReset2CalledAppliesComment()
|
|
{
|
|
TestableAlarm alarm = CreateResettableAlarm();
|
|
EnsureComment(alarm);
|
|
var comment = new LocalizedText("en", "reset by operator");
|
|
|
|
ServiceResult result = alarm.CallReset2(m_context, comment);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment.Value, Is.EqualTo(comment));
|
|
}
|
|
|
|
// ---------- GetGroupMemberships ----------
|
|
|
|
[Test]
|
|
public void OnGetGroupMembershipsCalledReturnsEmptyArrayWhenNoGroups()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
ArrayOf<NodeId> groups = default;
|
|
|
|
ServiceResult result = alarm.CallGetGroupMemberships(m_context, ref groups);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(groups.Count, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public void OnGetGroupMembershipsCalledReturnsBadConditionDisabledWhenDisabled()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
alarm.SetEnableState(m_context, false);
|
|
|
|
ArrayOf<NodeId> groups = default;
|
|
ServiceResult result = alarm.CallGetGroupMemberships(m_context, ref groups);
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadConditionDisabled));
|
|
}
|
|
|
|
[Test]
|
|
public void OnGetGroupMembershipsCalledReturnsConfiguredGroups()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
var groupNodeId = new NodeId(4242);
|
|
alarm.AddReference(ReferenceTypeIds.AlarmGroupMember, isInverse: true, groupNodeId);
|
|
|
|
ArrayOf<NodeId> groups = default;
|
|
ServiceResult result = alarm.CallGetGroupMemberships(m_context, ref groups);
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(groups.Count, Is.EqualTo(1));
|
|
Assert.That(groups[0], Is.EqualTo(groupNodeId));
|
|
}
|
|
|
|
[Test]
|
|
public void OnAcknowledgeWithNullCommentPreservesExistingComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
EnsureComment(alarm);
|
|
alarm.Comment!.Value = new LocalizedText("en", "Original comment");
|
|
|
|
var eventId = new ByteString(new byte[] { 1, 2, 3, 4 });
|
|
MakeAcknowledgeable(alarm, eventId);
|
|
ServiceResult result = alarm.CallAcknowledge(
|
|
m_context, eventId, new LocalizedText(string.Empty));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.AckedState!.Id!.Value, Is.True);
|
|
Assert.That(alarm.Comment!.Value.Text, Is.EqualTo("Original comment"),
|
|
"A null/empty Acknowledge comment must not overwrite the existing Condition comment.");
|
|
}
|
|
|
|
[Test]
|
|
public void OnAcknowledgeWithCommentUpdatesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
EnsureComment(alarm);
|
|
alarm.Comment!.Value = new LocalizedText("en", "Original comment");
|
|
|
|
var eventId = new ByteString(new byte[] { 5, 6, 7, 8 });
|
|
MakeAcknowledgeable(alarm, eventId);
|
|
ServiceResult result = alarm.CallAcknowledge(
|
|
m_context, eventId, new LocalizedText("en", "Operator comment"));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment!.Value.Text, Is.EqualTo("Operator comment"));
|
|
}
|
|
|
|
[Test]
|
|
public void OnAcknowledgeWithLocaleOnlyCommentUpdatesComment()
|
|
{
|
|
TestableAlarm alarm = CreateTestableAlarm();
|
|
EnsureComment(alarm);
|
|
alarm.Comment!.Value = new LocalizedText("en", "Original comment");
|
|
|
|
ByteString eventId = ByteString.FromHexString("090A0B0C");
|
|
MakeAcknowledgeable(alarm, eventId);
|
|
ServiceResult result = alarm.CallAcknowledge(
|
|
m_context,
|
|
eventId,
|
|
new LocalizedText("en", string.Empty));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(alarm.Comment!.Value.Locale, Is.EqualTo("en"));
|
|
Assert.That(alarm.Comment.Value.Text, Is.EqualTo(string.Empty));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Part 9 5.7.3: only the current, unacknowledged state can be acknowledged.
|
|
/// </summary>
|
|
private void MakeAcknowledgeable(AlarmConditionState alarm, ByteString eventId)
|
|
{
|
|
alarm.EventId!.Value = eventId;
|
|
alarm.SetAcknowledgedState(m_context, false);
|
|
}
|
|
}
|
|
}
|