diff --git a/SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs b/SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs index 057d28cd..e9345329 100644 --- a/SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs +++ b/SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs @@ -198,6 +198,7 @@ public virtual void StopCapture() if (Started) { threadCancellationTokenSource.Cancel(); + threadCancellationTokenSource = new CancellationTokenSource(); LibPcapSafeNativeMethods.pcap_breakloop(PcapHandle); if (!captureThread.Join(StopCaptureTimeout)) { @@ -209,26 +210,7 @@ public virtual void StopCapture() // ignore exception, .net platforms lack support for Thread.Abort() and aborting threads // is a hack } - - captureThread = null; - string error; - - if (isLibPcap && !MonoUnixFound) - { - error = string.Format("captureThread was aborted after {0}. Using a Mono" + - " version >= 2.4 and installing Mono.Posix should" + - " enable smooth thread shutdown", - StopCaptureTimeout.ToString()); - } - else - { - error = string.Format("captureThread was aborted after {0}", - StopCaptureTimeout.ToString()); - } - - throw new PcapException(error); } - captureThread = null; // otherwise we will always return true from PcapDevice.Started } } diff --git a/Test/PcapDeviceTest.cs b/Test/PcapDeviceTest.cs index 5dacfcbb..2bdcdd02 100644 --- a/Test/PcapDeviceTest.cs +++ b/Test/PcapDeviceTest.cs @@ -21,7 +21,7 @@ You should have received a copy of the GNU Lesser General Public License using System; using NUnit.Framework; using SharpPcap; -using SharpPcap.LibPcap; +using static Test.TestHelper; namespace Test { @@ -46,38 +46,25 @@ public class PcapDeviceTest [Test] public void GetNextPacketExceptionIfCaptureLoopRunning() { - 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)?"); - } + var device = GetPcapDevice(); - Assert.IsFalse(devices[0].Started, "Expected device not to be Started"); + Assert.IsFalse(device.Started, "Expected device not to be Started"); - devices[0].Open(); - devices[0].OnPacketArrival += HandleOnPacketArrival; + device.Open(); + device.OnPacketArrival += HandleOnPacketArrival; // start background capture - devices[0].StartCapture(); + device.StartCapture(); - Assert.IsTrue(devices[0].Started, "Expected device to be Started"); + Assert.IsTrue(device.Started, "Expected device to be Started"); // attempt to get the next packet via GetNextPacket() // to ensure that we get the exception we expect - bool caughtExpectedException = false; - try - { - devices[0].GetNextPacket(); - } - catch (InvalidOperationDuringBackgroundCaptureException) - { - caughtExpectedException = true; - } - - devices[0].Close(); - - Assert.IsTrue(caughtExpectedException); + Assert.Throws( + () => device.GetNextPacket() + ); + + device.Close(); } /// @@ -87,30 +74,15 @@ public void GetNextPacketExceptionIfCaptureLoopRunning() [Test] public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival() { - 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)?"); - } - - devices[0].Open(); - - bool caughtExpectedException = false; - - try - { - // start background capture - devices[0].StartCapture(); - } - catch (DeviceNotReadyException) - { - caughtExpectedException = true; - } - - devices[0].Close(); - - Assert.IsTrue(caughtExpectedException); + var device = GetPcapDevice(); + + device.Open(); + + Assert.Throws( + () => device.StartCapture() + ); + + device.Close(); } void HandleOnPacketArrival(object sender, CaptureEventArgs e) @@ -121,13 +93,13 @@ void HandleOnPacketArrival(object sender, CaptureEventArgs e) [SetUp] public void SetUp() { - TestHelper.ConfirmIdleState(); + ConfirmIdleState(); } [TearDown] public void Cleanup() { - TestHelper.ConfirmIdleState(); + ConfirmIdleState(); } } } diff --git a/Test/TestHelper.cs b/Test/TestHelper.cs index 4b6989bd..f0754f68 100644 --- a/Test/TestHelper.cs +++ b/Test/TestHelper.cs @@ -62,7 +62,8 @@ internal static PcapDevice GetPcapDevice() return device; } } - return null; + throw new InvalidOperationException("No ethernet pcap supported devices found, are you running" + + " as a user with access to adapters (root on Linux)?"); } /// @@ -74,11 +75,6 @@ internal static PcapDevice GetPcapDevice() internal static List RunCapture(string filter, Action routine) { var device = GetPcapDevice(); - if (device == null) - { - throw new InvalidOperationException("No ethernet pcap supported devices found, are you running" + - " as a user with access to adapters (root on Linux)?"); - } Console.WriteLine($"Using device {device}"); var received = new List(); device.Open(DeviceMode.Normal, 1);