Files
UA-.NETStandard/tests/Opc.Ua.Core.Diagnostics.Tests/Capture/DefaultCaptureSourceFactoryTests.cs
2026-07-16 06:25:54 +02:00

144 lines
5.1 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;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Opc.Ua.Pcap.Bindings;
using Opc.Ua.Pcap.Capture;
using Opc.Ua.Pcap.Capture.Sources;
using Opc.Ua.Pcap.Models;
namespace Opc.Ua.Pcap.Tests.Capture
{
[TestFixture]
public sealed class DefaultCaptureSourceFactoryTests : TempDirectoryFixture
{
[Test]
public void CtorRejectsNullRegistry()
{
Assert.That(
() => new DefaultCaptureSourceFactory(registry: null!),
Throws.TypeOf<ArgumentNullException>()
.With.Property("ParamName").EqualTo("registry"));
}
[Test]
public async Task CreateInProcessClientReturnsInProcessClientCaptureSource()
{
var registry = new ChannelCaptureRegistry();
var factory = new DefaultCaptureSourceFactory(registry);
ICaptureSource source = factory.Create(
CaptureSourceKind.InProcessClient,
TempDirectory,
NullLoggerFactory.Instance);
try
{
Assert.That(source, Is.Not.Null);
Assert.That(source, Is.TypeOf<InProcessClientCaptureSource>());
}
finally
{
if (source is IAsyncDisposable disposable)
{
await disposable.DisposeAsync().ConfigureAwait(false);
}
}
}
[Test]
public async Task CreateInProcessServerReturnsInProcessServerCaptureSource()
{
var registry = new ChannelCaptureRegistry();
var factory = new DefaultCaptureSourceFactory(registry);
ICaptureSource source = factory.Create(
CaptureSourceKind.InProcessServer,
TempDirectory,
NullLoggerFactory.Instance);
try
{
Assert.That(source, Is.Not.Null);
Assert.That(source, Is.TypeOf<InProcessServerCaptureSource>());
}
finally
{
if (source is IAsyncDisposable disposable)
{
await disposable.DisposeAsync().ConfigureAwait(false);
}
}
}
[Test]
public async Task CreateReplayReturnsReplayCaptureSource()
{
var registry = new ChannelCaptureRegistry();
var factory = new DefaultCaptureSourceFactory(registry);
ICaptureSource source = factory.Create(
CaptureSourceKind.Replay,
TempDirectory,
NullLoggerFactory.Instance);
try
{
Assert.That(source, Is.Not.Null);
Assert.That(source, Is.TypeOf<ReplayCaptureSource>());
}
finally
{
if (source is IAsyncDisposable disposable)
{
await disposable.DisposeAsync().ConfigureAwait(false);
}
}
}
[Test]
public void CreateUnsupportedKindThrowsDiagnostics()
{
var registry = new ChannelCaptureRegistry();
var factory = new DefaultCaptureSourceFactory(registry);
// 99 is intentionally not a defined CaptureSourceKind value.
const CaptureSourceKind invalid = (CaptureSourceKind)99;
Assert.That(
() => factory.Create(
invalid,
TempDirectory,
NullLoggerFactory.Instance),
Throws.TypeOf<PcapDiagnosticsException>()
.With.Message.Contains("Unsupported capture source kind"));
}
}
}