コード例 #1
0
        internal static void ConfirmIdleState()
        {
            var devices = DeviceFixture.GetDevices().OfType <PcapDevice>();

            foreach (var d in devices)
            {
                var isOpened  = d.Opened;
                var isStarted = d.Started;
                if (isStarted)
                {
                    d.StopCapture();
                }
                if (isOpened)
                {
                    d.Close();
                }
                var status = TestContext.CurrentContext.Result.Outcome.Status;
                // If test already failed, no point asserting here
                if (status != TestStatus.Failed)
                {
                    Assert.IsFalse(isOpened, "Expected device to not to be Opened");
                    Assert.IsFalse(isStarted, "Expected device to not be Started");
                }
            }
        }
コード例 #2
0
 public IEnumerable GetData(IParameterInfo parameter)
 {
     return(DeviceFixture.GetDevices()
            .OfType <PcapDevice>()
            .Select(d => new DeviceFixture(d))
            .ToArray());
 }
コード例 #3
0
        public void DeviceOpen([CaptureDevices] DeviceFixture fixture)
        {
            using var device = (PcapDevice)fixture.GetDevice();

            device.Open();
            DeviceTest(device, null);
        }
コード例 #4
0
        public void DeviceOpenWithTimestampType([CaptureDevices] DeviceFixture fixture)
        {
            using var device = (PcapDevice)fixture.GetDevice();
            if (!(device is LibPcapLiveDevice))
            {
                return;
            }

            var liveDevice = device as LibPcapLiveDevice;

            var timestampTypes = liveDevice.Interface.TimestampsSupported;

            Assert.IsNotEmpty(timestampTypes);

            // open the device with each of its supported timestamp types
            foreach (var pcapClock in timestampTypes)
            {
                var configuration = new DeviceConfiguration();
                configuration.TimestampType = pcapClock.TimestampType;
                liveDevice.Open(configuration);

                Assert.IsNotNull(liveDevice.Interface.TimestampsSupported);

                liveDevice.Close();
            }
        }
コード例 #5
0
ファイル: DeviceFixture.cs プロジェクト: zhouzu/sharppcap
 public IEnumerable GetData(IParameterInfo parameter)
 {
     return(DeviceFixture.GetDevices()
            .Cast <LibPcapLiveDevice>()
            .Select(d => new DeviceFixture(d))
            .ToArray());
 }
コード例 #6
0
        public void SetFilterExceptionIfDeviceIsClosed([CaptureDevices] DeviceFixture fixture)
        {
            var device = fixture.GetDevice();

            Assert.Throws <DeviceNotReadyException>(
                () => device.Filter = "tcp port 80",
                "Did not catch the expected DeviceNotReadyException"
                );
        }
コード例 #7
0
        public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPacketArrival(
            [CaptureDevices] DeviceFixture fixture
            )
        {
            var device = fixture.GetDevice();

            device.Open();

            Assert.Throws <DeviceNotReadyException>(
                () => device.StartCapture()
                );

            device.Close();
        }
コード例 #8
0
 public void DeviceOpenWithTimestampPrecision(
     [CaptureDevices] DeviceFixture fixture,
     [Values] TimestampResolution resolution
     )
 {
     using var device = (PcapDevice)fixture.GetDevice();
     try
     {
         var configuration = new DeviceConfiguration();
         configuration.TimestampResolution = resolution;
         device.Open(configuration);
         DeviceTest(device, resolution);
     }
     catch (PcapException ex)
     {
         // its ok if the device does not support setting the precision, all other PcapError
         // types are considered test failures
         Assert.AreEqual(PcapError.TimestampPrecisionNotSupported, ex.Error);
         Assert.Ignore("Device does not support this timestamp precision");
     }
 }
コード例 #9
0
        public void SimpleFilter([CaptureDevices] DeviceFixture fixture)
        {
            // BPF is known to support those link layers,
            // support for other link layers such as NFLOG and USB is unknown
            var supportedLinks = new[]
            {
                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");
            }
            device.Filter = "tcp port 80";
            device.Close(); // close the device
        }
コード例 #10
0
        public void GetNextPacketExceptionIfCaptureLoopRunning(
            [CaptureDevices] DeviceFixture fixture
            )
        {
            using var device = fixture.GetDevice();

            Assert.IsFalse(device.Started, "Expected device not to be Started");

            device.Open();
            device.OnPacketArrival += HandleOnPacketArrival;

            // start background capture
            device.StartCapture();

            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
            Assert.Throws <InvalidOperationDuringBackgroundCaptureException>(
                () => device.GetNextPacket(out var _)
                );
        }
コード例 #11
0
        public void DeviceProperties([PcapDevices] DeviceFixture fixture)
        {
            var device = (PcapDevice)fixture.GetDevice();

            device.Open();
            var pcapIf = device.Interface;

            Assert.IsTrue(device.Opened);

            Assert.IsNotEmpty(device.Name);
            Assert.AreEqual(device.Name, pcapIf.Name);
            Assert.AreEqual(device.Description, pcapIf.Description);

            Assert.IsNotNull(pcapIf.GatewayAddresses);
            Assert.IsNotNull(pcapIf.Addresses);

            if (pcapIf.MacAddress != null)
            {
                Assert.That(pcapIf.MacAddress.GetAddressBytes(), Has.Length.EqualTo(6));
            }

            device.Close();
        }