Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase coverage to more than 50% #117

Merged
merged 1 commit into from Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Test/CheckFilterTest.cs
Expand Up @@ -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);
}
}
}
90 changes: 90 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2020 Ayoub Kaanich <kayoub5@live.com>
*/

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<ICaptureDevice> GetDevices()
{
var lists = new Dictionary<string, IEnumerable<ICaptureDevice>>();
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<PcapDevice>()
.Select(d => new DeviceFixture(d))
.ToArray();
}
}

}

53 changes: 24 additions & 29 deletions Test/LivePcapDeviceSetFilterTest.cs
@@ -1,7 +1,8 @@
using System;
using System.Linq;
using NUnit.Framework;
using PacketDotNet;
using SharpPcap;
using SharpPcap.LibPcap;

namespace Test
{
Expand All @@ -10,45 +11,39 @@ 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
}

/// <summary>
/// Test that we get the expected exception if PcapDevice.SetFilter()
/// is called on a PcapDevice that has not been opened
/// </summary>
[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<DeviceNotReadyException>(
() => device.Filter = "tcp port 80",
"Did not catch the expected DeviceNotReadyException"
);
}

[SetUp]
Expand Down
13 changes: 8 additions & 5 deletions Test/PcapDeviceTest.cs
Expand Up @@ -44,9 +44,11 @@ public class PcapDeviceTest
/// </summary>
[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");

Expand All @@ -72,10 +74,11 @@ public void GetNextPacketExceptionIfCaptureLoopRunning()
/// there hasn't been any delegates assigned to PcapDevice.OnPacketArrival
/// </summary>
[Test]
public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival()
public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival(
[CaptureDevices] DeviceFixture fixture
)
{
var device = GetPcapDevice();

var device = fixture.GetDevice();
device.Open();

Assert.Throws<DeviceNotReadyException>(
Expand Down
2 changes: 1 addition & 1 deletion Test/TestHelper.cs
Expand Up @@ -103,7 +103,7 @@ internal static List<RawCapture> RunCapture(string filter, Action<PcapDevice> ro

internal static void ConfirmIdleState()
{
var devices = LibPcapLiveDeviceList.Instance;
var devices = DeviceFixture.GetDevices().OfType<PcapDevice>();
foreach (var d in devices)
{
var isOpened = d.Opened;
Expand Down