public void BadFilterErrorTest()
 {
     using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
     {
         communicator.SetFilter("illegal filter string");
     }
 }
Пример #2
0
 public void TransmitNullTest()
 {
     using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
     {
         communicator.Transmit(null, false);
     }
     Assert.Fail();
 }
Пример #3
0
        private static void TestTransmitQueueToLive(int numPacketsToSend, int packetSize, double secondsBetweenTimestamps, bool isSynced)
        {
            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            List <Packet> packetsToSend;

            using (PacketSendBuffer queue = BuildQueue(out packetsToSend, numPacketsToSend, packetSize, SourceMac, DestinationMac, secondsBetweenTimestamps))
            {
                using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
                {
                    communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
                    communicator.Transmit(queue, isSynced);

                    DateTime lastTimestamp     = DateTime.MinValue;
                    int      numPacketsHandled = 0;
                    int      numPacketsGot;
                    PacketCommunicatorReceiveResult result =
                        communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToSend,
                                                        delegate(Packet packet)
                    {
                        Assert.AreEqual(packetsToSend[numPacketsHandled], packet);
                        if (numPacketsHandled > 0)
                        {
                            TimeSpan expectedDiff;
                            if (isSynced)
                            {
                                expectedDiff =
                                    packetsToSend[numPacketsHandled].Timestamp -
                                    packetsToSend[numPacketsHandled - 1].Timestamp;
                            }
                            else
                            {
                                expectedDiff = TimeSpan.Zero;
                            }
                            TimeSpan actualDiff = packet.Timestamp - lastTimestamp;
                            MoreAssert.IsInRange(
                                expectedDiff.Subtract(TimeSpan.FromSeconds(0.06)),
                                expectedDiff.Add(TimeSpan.FromSeconds(0.1)),
                                actualDiff, "actualDiff");
                        }
                        lastTimestamp = packet.Timestamp;
                        ++numPacketsHandled;
                    });

                    Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
                    Assert.AreEqual(numPacketsToSend, numPacketsGot, "numPacketsGot");
                    Assert.AreEqual(numPacketsToSend, numPacketsHandled, "numPacketsHandled");
                }
            }
        }
Пример #4
0
        public static OfflinePacketDevice GetOfflineDevice(int numPackets, Packet packet, TimeSpan intervalBetweenPackets, string dumpFilename, string readFilename = null)
        {
            if (readFilename == null)
            {
                readFilename = dumpFilename;
            }
            PacketCommunicator communicator;

            using (communicator = LivePacketDeviceTests.OpenLiveDevice())
            {
                using (PacketDumpFile dumpFile = communicator.OpenDump(dumpFilename))
                {
                    int lastPosition = 0;
                    for (int i = 0; i != numPackets; ++i)
                    {
                        if (intervalBetweenPackets != TimeSpan.Zero && i != 0)
                        {
                            DateTime timestamp = packet.Timestamp;
                            timestamp = timestamp.Add(intervalBetweenPackets);
                            packet    = new Packet(packet.Buffer, timestamp, packet.DataLink);
                        }
                        dumpFile.Dump(packet);
                        MoreAssert.IsBigger(lastPosition, dumpFile.Position);
                        lastPosition = dumpFile.Position;
                        dumpFile.Flush();
                    }
                }
            }

            if (readFilename != dumpFilename)
            {
                if (File.Exists(readFilename))
                {
                    File.Delete(readFilename);
                }
                File.Move(dumpFilename, readFilename);
            }

            OfflinePacketDevice device = new OfflinePacketDevice(readFilename);

            Assert.AreEqual(0, device.Addresses.Count);
            Assert.AreEqual(string.Empty, device.Description);
            Assert.AreEqual(DeviceAttributes.None, device.Attributes);
            Assert.AreEqual(readFilename, device.Name);

            return(device);
        }
        public void NoCommunicatorConstructorWithNetmaskTest()
        {
            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            Random random           = new Random();
            Packet expectedPacket   = random.NextEthernetPacket(1000, SourceMac, DestinationMac);
            Packet unexpectedPacket = random.NextEthernetPacket(1000, DestinationMac, SourceMac);

            using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
            {
                using (BerkeleyPacketFilter filter = new BerkeleyPacketFilter("ether src " + SourceMac + " and ether dst " + DestinationMac, 1000, DataLinkKind.Ethernet, communicator.IpV4Netmask))
                {
                    TestFilter(communicator, filter, expectedPacket, unexpectedPacket);
                }
            }
        }