sharppcap icon indicating copy to clipboard operation
sharppcap copied to clipboard

How can I (unit)test the CaptureDevices?

Open NeoMorfeo opened this issue 4 years ago • 3 comments

I wish to knwo if there is any way to mock the CaptureDevices (CaptureDeviceList).

I need that to test several code, that relays on the detection of the devices, but, i can't make a mock or substitute.

Can you provide some info about it?

thanks in advance.

NeoMorfeo avatar May 07 '21 11:05 NeoMorfeo

Thanks @NeoMorfeo for this post! I think we need a way to test our code and due to it doesn't use interfaces it is so hard to have a nice way to test

roberdcr avatar May 07 '21 12:05 roberdcr

@kayoub5 and I have discussed dropping the singleton use in CaptureDeviceList etc but changes here on master have yet to be made. Suggestions for improvements are welcome. 6.x is coming soon so now is a good time to propose API altering changes.

chmorgan avatar May 07 '21 12:05 chmorgan

The way I see it, we already have the PcapInterface.GetAllPcapInterfaces(), and a single class for PCAP device new LibPcapLiveDevice(PcapInterface pcapIf), so the CaptureDeviceList could be removed as far as I see.

For the original request, You can:

  • Mock a single device by mocking the ICaptureDevice
  • Mock a list of devices by mocking the base class of CaptureDeviceList: ReadOnlyCollection<> (the application should use a List of CaptureDevice instead of directly referencing CaptureDeviceList.Instance)
  • Create your own C library wpcap.dll or libpcap.so

kayoub5 avatar May 07 '21 22:05 kayoub5