Skip to content

Commit

Permalink
Don't throw if used abort
Browse files Browse the repository at this point in the history
  • Loading branch information
kayoub5 authored and chmorgan committed Mar 14, 2020
1 parent 1141a2d commit b5f35bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 76 deletions.
20 changes: 1 addition & 19 deletions SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public virtual void StopCapture()
if (Started)
{
threadCancellationTokenSource.Cancel();
threadCancellationTokenSource = new CancellationTokenSource();
LibPcapSafeNativeMethods.pcap_breakloop(PcapHandle);
if (!captureThread.Join(StopCaptureTimeout))
{
Expand All @@ -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
}
}
Expand Down
74 changes: 23 additions & 51 deletions Test/PcapDeviceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<InvalidOperationDuringBackgroundCaptureException>(
() => device.GetNextPacket()
);

device.Close();
}

/// <summary>
Expand All @@ -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<DeviceNotReadyException>(
() => device.StartCapture()
);

device.Close();
}

void HandleOnPacketArrival(object sender, CaptureEventArgs e)
Expand All @@ -121,13 +93,13 @@ void HandleOnPacketArrival(object sender, CaptureEventArgs e)
[SetUp]
public void SetUp()
{
TestHelper.ConfirmIdleState();
ConfirmIdleState();
}

[TearDown]
public void Cleanup()
{
TestHelper.ConfirmIdleState();
ConfirmIdleState();
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions Test/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?");
}

/// <summary>
Expand All @@ -74,11 +75,6 @@ internal static PcapDevice GetPcapDevice()
internal static List<RawCapture> RunCapture(string filter, Action<PcapDevice> 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<RawCapture>();
device.Open(DeviceMode.Normal, 1);
Expand Down

0 comments on commit b5f35bb

Please sign in to comment.