public void SetSmallKernelMinimumBytesToCopyTest() { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.SetKernelMinimumBytesToCopy(1); Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac); for (int i = 0; i != 100; ++i) { communicator.SendPacket(expectedPacket); Packet packet; DateTime start = DateTime.Now; PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet); DateTime end = DateTime.Now; Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); Assert.AreEqual(expectedPacket, packet); MoreAssert.IsSmallerOrEqual(TimeSpan.FromSeconds(0.07), end - start); } } }
public static PacketCommunicator OpenLiveDevice() { NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault( ni => !ni.IsReceiveOnly && ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && ni.OperationalStatus == OperationalStatus.Up); LivePacketDevice device = networkInterface.GetLivePacketDevice(); MoreAssert.IsMatch(@"Network adapter '.*' on local host", device.Description); Assert.AreEqual(DeviceAttributes.None, device.Attributes); Assert.AreNotEqual(MacAddress.Zero, device.GetMacAddress()); Assert.AreNotEqual(string.Empty, device.GetPnpDeviceId()); MoreAssert.IsBiggerOrEqual(1, device.Addresses.Count); foreach (DeviceAddress address in device.Addresses) { if (address.Address.Family == SocketAddressFamily.Internet) { MoreAssert.IsMatch("Address: " + SocketAddressFamily.Internet + @" [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ " + "Netmask: " + SocketAddressFamily.Internet + @" 255\.[0-9]+\.[0-9]+\.[0-9]+ " + "Broadcast: " + SocketAddressFamily.Internet + @" 255.255.255.255", address.ToString()); } else { Assert.AreEqual(SocketAddressFamily.Internet6, address.Address.Family); MoreAssert.IsMatch("Address: " + SocketAddressFamily.Internet6 + @" (?:[0-9A-F]{4}:){7}[0-9A-F]{4} " + "Netmask: " + SocketAddressFamily.Unspecified + @" " + IpV6Address.Zero + " " + "Broadcast: " + SocketAddressFamily.Unspecified + @" " + IpV6Address.Zero, address.ToString()); } } PacketCommunicator communicator = device.Open(); try { MoreAssert.AreSequenceEqual(new[] { DataLinkKind.Ethernet, DataLinkKind.Docsis }.Select(kind => new PcapDataLink(kind)), communicator.SupportedDataLinks); PacketTotalStatistics totalStatistics = communicator.TotalStatistics; Assert.AreEqual <object>(totalStatistics, totalStatistics); Assert.AreNotEqual(null, totalStatistics); Assert.AreEqual(totalStatistics.GetHashCode(), totalStatistics.GetHashCode()); Assert.IsTrue(totalStatistics.Equals(totalStatistics)); Assert.IsFalse(totalStatistics.Equals(null)); Assert.AreNotEqual(null, totalStatistics); Assert.AreNotEqual(totalStatistics, 2); MoreAssert.IsSmallerOrEqual <uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured"); Assert.AreEqual <uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver"); Assert.AreEqual <uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface"); MoreAssert.IsSmallerOrEqual <uint>(1, totalStatistics.PacketsReceived, "PacketsReceived"); Assert.IsNotNull(totalStatistics.ToString()); communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1 communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy communicator.SetSamplingMethod(new SamplingMethodNone()); Assert.AreEqual(DataLinkKind.Ethernet, communicator.DataLink.Kind); communicator.DataLink = communicator.DataLink; Assert.AreEqual("EN10MB (Ethernet)", communicator.DataLink.ToString()); Assert.AreEqual(communicator.DataLink, new PcapDataLink(communicator.DataLink.Name)); Assert.IsTrue(communicator.IsFileSystemByteOrder); Assert.AreEqual(PacketCommunicatorMode.Capture, communicator.Mode); Assert.IsFalse(communicator.NonBlocking); Assert.AreEqual(PacketDevice.DefaultSnapshotLength, communicator.SnapshotLength); return(communicator); } catch (Exception) { communicator.Dispose(); throw; } }
public void RandomIgmpTest() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("00:01:02:03:04:05"), Destination = new MacAddress("A0:A1:A2:A3:A4:A5") }; int seed = new Random().Next(); Console.WriteLine("Seed: " + seed); Random random = new Random(seed); for (int i = 0; i != 200; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.InternetGroupManagementProtocol, false); IgmpLayer igmpLayer = random.NextIgmpLayer(); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, igmpLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // Ethernet ethernetLayer.EtherType = ipLayer == ipV4Layer ? EthernetType.IpV4 : EthernetType.IpV6; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); ethernetLayer.EtherType = EthernetType.None; // IP. if (ipV4Layer == ipLayer) { // IPv4. ipV4Layer.Protocol = IpV4Protocol.InternetGroupManagementProtocol; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer"); ipV4Layer.HeaderChecksum = null; } else { // IPv6. Assert.AreEqual(ipLayer, packet.Ethernet.IpV6.ExtractLayer(), "IPv6 Layer"); } // IGMP Assert.IsTrue(packet.Ethernet.Ip.Igmp.IsChecksumCorrect); Assert.AreEqual(igmpLayer, packet.Ethernet.Ip.Igmp.ExtractLayer(), "IGMP Layer"); Assert.AreEqual(igmpLayer.GetHashCode(), packet.Ethernet.Ip.Igmp.ExtractLayer().GetHashCode(), "IGMP Layer"); Assert.AreNotEqual(igmpLayer, null); Assert.AreNotEqual(igmpLayer, random.NextPayloadLayer(igmpLayer.Length)); Assert.AreNotEqual(igmpLayer.GetHashCode(), random.NextPayloadLayer(igmpLayer.Length).GetHashCode()); if (packet.Ethernet.Ip.Igmp.QueryVersion != IgmpQueryVersion.Version3) { MoreAssert.IsSmallerOrEqual(IgmpDatagram.MaxMaxResponseTime, packet.Ethernet.Ip.Igmp.MaxResponseTime); } if (packet.Ethernet.Ip.Igmp.MessageType != IgmpMessageType.MembershipQuery) { Assert.AreEqual(IgmpQueryVersion.None, packet.Ethernet.Ip.Igmp.QueryVersion); } switch (igmpLayer.MessageTypeValue) { case IgmpMessageType.CreateGroupRequestVersion0: case IgmpMessageType.CreateGroupReplyVersion0: case IgmpMessageType.JoinGroupRequestVersion0: case IgmpMessageType.JoinGroupReplyVersion0: case IgmpMessageType.LeaveGroupRequestVersion0: case IgmpMessageType.LeaveGroupReplyVersion0: case IgmpMessageType.ConfirmGroupRequestVersion0: case IgmpMessageType.ConfirmGroupReplyVersion0: Assert.AreEqual(0, packet.Ethernet.Ip.Igmp.Version); IgmpVersion0Layer igmpVersion0Layer = (IgmpVersion0Layer)igmpLayer; Assert.AreEqual(igmpVersion0Layer.IdentifierValue, packet.Ethernet.Ip.Igmp.Identifier); Assert.AreEqual(igmpVersion0Layer.AccessKeyValue, packet.Ethernet.Ip.Igmp.AccessKey); switch (igmpLayer.MessageTypeValue) { case IgmpMessageType.CreateGroupRequestVersion0: Assert.AreEqual(((IgmpCreateGroupRequestVersion0Layer)igmpLayer).CreateGroupRequestCode, packet.Ethernet.Ip.Igmp.CreateGroupRequestCode); break; case IgmpMessageType.CreateGroupReplyVersion0: case IgmpMessageType.JoinGroupReplyVersion0: case IgmpMessageType.LeaveGroupReplyVersion0: case IgmpMessageType.ConfirmGroupReplyVersion0: IgmpReplyVersion0Layer igmpReplyVersion0Layer = (IgmpReplyVersion0Layer)igmpVersion0Layer; Assert.AreEqual(igmpReplyVersion0Layer.Code, packet.Ethernet.Ip.Igmp.ReplyCode); if (packet.Ethernet.Ip.Igmp.ReplyCode == IgmpVersion0ReplyCode.RequestPendingRetryInThisManySeconds) { Assert.AreEqual(igmpReplyVersion0Layer.RetryInThisManySeconds, packet.Ethernet.Ip.Igmp.RetryInThisManySeconds); } break; } break; case IgmpMessageType.MembershipQuery: switch (igmpLayer.QueryVersion) { case IgmpQueryVersion.Version1: Assert.AreEqual(1, packet.Ethernet.Ip.Igmp.Version); break; case IgmpQueryVersion.Version2: Assert.AreEqual(2, packet.Ethernet.Ip.Igmp.Version); break; case IgmpQueryVersion.Version3: Assert.AreEqual(3, packet.Ethernet.Ip.Igmp.Version); break; default: Assert.Fail(igmpLayer.QueryVersion.ToString()); break; } break; case IgmpMessageType.MembershipReportVersion1: Assert.AreEqual(1, packet.Ethernet.Ip.Igmp.Version); break; case IgmpMessageType.MembershipReportVersion2: case IgmpMessageType.LeaveGroupVersion2: Assert.AreEqual(2, packet.Ethernet.Ip.Igmp.Version); break; case IgmpMessageType.MembershipReportVersion3: Assert.AreEqual(3, packet.Ethernet.Ip.Igmp.Version); break; default: Assert.Fail(igmpLayer.MessageTypeValue.ToString()); break; } foreach (IgmpGroupRecordDatagram groupRecord in packet.Ethernet.Ip.Igmp.GroupRecords) { Assert.IsNotNull(groupRecord.ToString()); } } }
public void RandomIgmpTest() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("00:01:02:03:04:05"), Destination = new MacAddress("A0:A1:A2:A3:A4:A5") }; Random random = new Random(); IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; for (int i = 0; i != 200; ++i) { IgmpLayer igmpLayer = random.NextIgmpLayer(); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, igmpLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // Ethernet ethernetLayer.EtherType = EthernetType.IpV4; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); // IPv4 ipV4Layer.Protocol = IpV4Protocol.InternetGroupManagementProtocol; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer"); ipV4Layer.HeaderChecksum = null; // IGMP Assert.IsTrue(packet.Ethernet.IpV4.Igmp.IsChecksumCorrect); Assert.AreEqual(igmpLayer, packet.Ethernet.IpV4.Igmp.ExtractLayer(), "IGMP Layer"); Assert.AreEqual(igmpLayer.GetHashCode(), packet.Ethernet.IpV4.Igmp.ExtractLayer().GetHashCode(), "IGMP Layer"); Assert.AreNotEqual(igmpLayer, null); Assert.AreNotEqual(igmpLayer, random.NextPayloadLayer(igmpLayer.Length)); Assert.AreNotEqual(igmpLayer.GetHashCode(), random.NextPayloadLayer(igmpLayer.Length).GetHashCode()); if (packet.Ethernet.IpV4.Igmp.QueryVersion != IgmpQueryVersion.Version3) { MoreAssert.IsSmallerOrEqual(IgmpDatagram.MaxMaxResponseTime, packet.Ethernet.IpV4.Igmp.MaxResponseTime); } if (packet.Ethernet.IpV4.Igmp.MessageType != IgmpMessageType.MembershipQuery) { Assert.AreEqual(IgmpQueryVersion.None, packet.Ethernet.IpV4.Igmp.QueryVersion); } switch (igmpLayer.MessageType) { case IgmpMessageType.MembershipQuery: switch (igmpLayer.QueryVersion) { case IgmpQueryVersion.Version1: Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version); break; case IgmpQueryVersion.Version2: Assert.AreEqual(2, packet.Ethernet.IpV4.Igmp.Version); break; case IgmpQueryVersion.Version3: Assert.AreEqual(3, packet.Ethernet.IpV4.Igmp.Version); break; default: Assert.Fail(igmpLayer.QueryVersion.ToString()); break; } break; case IgmpMessageType.MembershipReportVersion1: Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version); break; case IgmpMessageType.MembershipReportVersion2: case IgmpMessageType.LeaveGroupVersion2: Assert.AreEqual(2, packet.Ethernet.IpV4.Igmp.Version); break; case IgmpMessageType.MembershipReportVersion3: Assert.AreEqual(3, packet.Ethernet.IpV4.Igmp.Version); break; default: Assert.Fail(igmpLayer.MessageType.ToString()); break; } foreach (IgmpGroupRecordDatagram groupRecord in packet.Ethernet.IpV4.Igmp.GroupRecords) { Assert.IsNotNull(groupRecord.ToString()); } } }