diff --git a/Test/CheckFilterTest.cs b/Test/CheckFilterTest.cs
index 2590ad39..888696fc 100644
--- a/Test/CheckFilterTest.cs
+++ b/Test/CheckFilterTest.cs
@@ -11,9 +11,12 @@ public void TestFilters()
{
// test a known failing filter
Assert.IsFalse(LibPcapLiveDevice.CheckFilter("some bogus filter", out string errorString));
+ Assert.IsNotNull(errorString);
+ Assert.IsNotEmpty(errorString);
// test a known working filter
Assert.IsTrue(LibPcapLiveDevice.CheckFilter("port 23", out errorString));
+ Assert.IsNull(errorString);
}
}
}
diff --git a/Test/DeviceFixture.cs b/Test/DeviceFixture.cs
new file mode 100644
index 00000000..5ee1f789
--- /dev/null
+++ b/Test/DeviceFixture.cs
@@ -0,0 +1,90 @@
+/*
+This file is part of SharpPcap.
+
+SharpPcap is free software: you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SharpPcap is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with SharpPcap. If not, see .
+*/
+/*
+ * Copyright 2020 Ayoub Kaanich
+ */
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using SharpPcap;
+using SharpPcap.LibPcap;
+using SharpPcap.Npcap;
+using SharpPcap.WinPcap;
+
+namespace Test
+{
+
+ public class DeviceFixture
+ {
+ private readonly ICaptureDevice Device;
+ public DeviceFixture(ICaptureDevice device)
+ {
+ Device = device;
+ }
+
+ public ICaptureDevice GetDevice() => Device;
+
+ public override string ToString()
+ {
+ return Device.Name;
+ }
+
+ public static IEnumerable GetDevices()
+ {
+ var lists = new Dictionary>();
+ lists.Add(nameof(CaptureDeviceList), CaptureDeviceList.Instance);
+ lists.Add(nameof(LibPcapLiveDeviceList), LibPcapLiveDeviceList.Instance);
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ lists.Add(nameof(WinPcapDeviceList), WinPcapDeviceList.Instance);
+ lists.Add(nameof(NpcapDeviceList), NpcapDeviceList.Instance);
+ }
+ foreach (var list in lists)
+ {
+ Assert.IsNotEmpty(list.Value, "{0} should not be empty", list.Key);
+ }
+ return lists.SelectMany(l => l.Value).Distinct();
+ }
+ }
+ class CaptureDevicesAttribute : NUnitAttribute, IParameterDataSource
+ {
+ public IEnumerable GetData(IParameterInfo parameter)
+ {
+ return DeviceFixture.GetDevices()
+ .Select(d => new DeviceFixture(d))
+ .ToArray();
+ }
+ }
+
+ class PcapDevicesAttribute : NUnitAttribute, IParameterDataSource
+ {
+ public IEnumerable GetData(IParameterInfo parameter)
+ {
+ return DeviceFixture.GetDevices()
+ .OfType()
+ .Select(d => new DeviceFixture(d))
+ .ToArray();
+ }
+ }
+
+}
+
diff --git a/Test/LivePcapDeviceSetFilterTest.cs b/Test/LivePcapDeviceSetFilterTest.cs
index ff7b49c9..af484695 100644
--- a/Test/LivePcapDeviceSetFilterTest.cs
+++ b/Test/LivePcapDeviceSetFilterTest.cs
@@ -1,7 +1,8 @@
using System;
+using System.Linq;
using NUnit.Framework;
+using PacketDotNet;
using SharpPcap;
-using SharpPcap.LibPcap;
namespace Test
{
@@ -10,18 +11,25 @@ namespace Test
public class LivePcapDeviceSetFilterTest
{
[Test]
- public void SimpleFilter()
+ public void SimpleFilter([CaptureDevices] DeviceFixture fixture)
{
- var devices = LibPcapLiveDeviceList.Instance;
- if (devices.Count == 0)
+ // BPF is known to support those link layers,
+ // support for other link layers such as NFLOG and USB is unknown
+ var supportedLinks = new[]
{
- throw new InvalidOperationException("No pcap supported devices found, are you running" +
- " as a user with access to adapters (root on Linux)?");
+ LinkLayers.Ethernet,
+ LinkLayers.Raw,
+ LinkLayers.Null
+ };
+ var device = fixture.GetDevice();
+ device.Open();
+ if (!supportedLinks.Contains(device.LinkType))
+ {
+ device.Close();
+ Assert.Inconclusive("NFLOG link-layer not supported");
}
-
- devices[0].Open();
- devices[0].Filter = "tcp port 80";
- devices[0].Close(); // close the device
+ device.Filter = "tcp port 80";
+ device.Close(); // close the device
}
///
@@ -29,26 +37,13 @@ public void SimpleFilter()
/// is called on a PcapDevice that has not been opened
///
[Test]
- public void SetFilterExceptionIfDeviceIsClosed()
+ public void SetFilterExceptionIfDeviceIsClosed([CaptureDevices] DeviceFixture fixture)
{
- var devices = LibPcapLiveDeviceList.Instance;
- if (devices.Count == 0)
- {
- throw new InvalidOperationException("No pcap supported devices found, are you running" +
- " as a user with access to adapters (root on Linux)?");
- }
-
- bool caughtExpectedException = false;
- try
- {
- devices[0].Filter = "tcp port 80";
- }
- catch (DeviceNotReadyException)
- {
- caughtExpectedException = true;
- }
-
- Assert.IsTrue(caughtExpectedException, "Did not catch the expected PcapDeviceNotReadyException");
+ var device = fixture.GetDevice();
+ Assert.Throws(
+ () => device.Filter = "tcp port 80",
+ "Did not catch the expected DeviceNotReadyException"
+ );
}
[SetUp]
diff --git a/Test/PcapDeviceTest.cs b/Test/PcapDeviceTest.cs
index 2bdcdd02..1fcdd79e 100644
--- a/Test/PcapDeviceTest.cs
+++ b/Test/PcapDeviceTest.cs
@@ -44,9 +44,11 @@ public class PcapDeviceTest
///
[NonParallelizable]
[Test]
- public void GetNextPacketExceptionIfCaptureLoopRunning()
+ public void GetNextPacketExceptionIfCaptureLoopRunning(
+ [CaptureDevices] DeviceFixture fixture
+ )
{
- var device = GetPcapDevice();
+ var device = fixture.GetDevice();
Assert.IsFalse(device.Started, "Expected device not to be Started");
@@ -72,10 +74,11 @@ public void GetNextPacketExceptionIfCaptureLoopRunning()
/// there hasn't been any delegates assigned to PcapDevice.OnPacketArrival
///
[Test]
- public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival()
+ public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival(
+ [CaptureDevices] DeviceFixture fixture
+ )
{
- var device = GetPcapDevice();
-
+ var device = fixture.GetDevice();
device.Open();
Assert.Throws(
diff --git a/Test/TestHelper.cs b/Test/TestHelper.cs
index f0754f68..f1d13875 100644
--- a/Test/TestHelper.cs
+++ b/Test/TestHelper.cs
@@ -103,7 +103,7 @@ internal static List RunCapture(string filter, Action ro
internal static void ConfirmIdleState()
{
- var devices = LibPcapLiveDeviceList.Instance;
+ var devices = DeviceFixture.GetDevices().OfType();
foreach (var d in devices)
{
var isOpened = d.Opened;