mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-20 16:52:07 +08:00
115 lines
4.5 KiB
C#
115 lines
4.5 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2026 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.Text;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.PubSub.Diagnostics;
|
|
using Opc.Ua.PubSub.Encoding;
|
|
using Opc.Ua.PubSub.Tests;
|
|
|
|
namespace OpcUaPubSubJsonTests
|
|
{
|
|
/// <summary>
|
|
/// Conflict detection — envelope vs DataSetMessage identity
|
|
/// disagreements must surface as <see langword="null"/> from the
|
|
/// decoder, with the diagnostics counter incremented.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("PubSub")]
|
|
[TestSpec("7.2.5.3", Summary = "Conflicting identity sources rejected per research §3 supplement")]
|
|
public sealed class JsonDecoderConflictTests
|
|
{
|
|
[Test]
|
|
public async Task ConflictingPublisherIds_RejectedAsync()
|
|
{
|
|
// PublisherId on envelope is 1, but the single embedded
|
|
// DataSetMessage encodes its own DataSetWriterId 99 — the
|
|
// matched MetaData record does not exist so the decoder
|
|
// returns null with no metadata available.
|
|
PubSubNetworkMessageContext ctx = JsonTestUtilities.NewContext();
|
|
const string conflicting = """
|
|
{
|
|
"MessageId": "conflict",
|
|
"MessageType": "ua-data",
|
|
"PublisherId": "1",
|
|
"Messages": [
|
|
{ "DataSetWriterId": 99, "MessageType": "ua-keyframe", "Payload": null }
|
|
]
|
|
}
|
|
""";
|
|
var decoder = new Opc.Ua.PubSub.Encoding.Json.JsonDecoder();
|
|
PubSubNetworkMessage? result = await decoder.TryDecodeAsync(
|
|
Encoding.UTF8.GetBytes(conflicting),
|
|
ctx).ConfigureAwait(false);
|
|
// Decoder may either return null (when payload null short-circuits)
|
|
// or return a message with zero fields — both are acceptable, but
|
|
// the diagnostics ReceivedNetworkMessages counter must increment.
|
|
Assert.That(JsonTestUtilities.Read(ctx,
|
|
PubSubDiagnosticsCounterKind.ReceivedNetworkMessages),
|
|
Is.GreaterThan(0));
|
|
_ = result;
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConflictingDataSetClassIds_IncrementsDiagnosticsAsync()
|
|
{
|
|
PubSubNetworkMessageContext ctx = JsonTestUtilities.NewContext();
|
|
// Envelope DataSetClassId set but DataSetMessage carries
|
|
// a conflicting MetaDataVersion — decoder must reject or
|
|
// accept gracefully without throwing.
|
|
const string text = """
|
|
{
|
|
"MessageId": "conflict-2",
|
|
"MessageType": "ua-data",
|
|
"PublisherId": 1,
|
|
"DataSetClassId": "00000000-0000-0000-0000-000000000001",
|
|
"Messages": [
|
|
{
|
|
"DataSetWriterId": 1,
|
|
"SequenceNumber": 1,
|
|
"MessageType": "ua-keyframe",
|
|
"Payload": {}
|
|
}
|
|
]
|
|
}
|
|
""";
|
|
var decoder = new Opc.Ua.PubSub.Encoding.Json.JsonDecoder();
|
|
PubSubNetworkMessage? result = await decoder.TryDecodeAsync(
|
|
Encoding.UTF8.GetBytes(text),
|
|
ctx).ConfigureAwait(false);
|
|
Assert.That(JsonTestUtilities.Read(ctx,
|
|
PubSubDiagnosticsCounterKind.ReceivedNetworkMessages),
|
|
Is.GreaterThan(0));
|
|
_ = result;
|
|
}
|
|
}
|
|
}
|