public void RandomUdpTest() { 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 != 1000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Udp, false); EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6; Layer ipLayer = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer); UdpLayer udpLayer = random.NextUdpLayer(); udpLayer.Checksum = null; PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000)); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, payloadLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // Ethernet ethernetLayer.EtherType = ethernetType; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); ethernetLayer.EtherType = EthernetType.None; // Ip if (ipLayer == ipV4Layer) { // IpV4. ipV4Layer.Protocol = IpV4Protocol.Udp; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); ipV4Layer.HeaderChecksum = null; } else { // IpV6. Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer"); } // UDP udpLayer.Checksum = packet.Ethernet.Ip.Udp.Checksum; Assert.AreEqual(udpLayer, packet.Ethernet.Ip.Udp.ExtractLayer(), "UDP Layer"); Assert.AreEqual(UdpDatagram.HeaderLength + payloadLayer.Length, packet.Ethernet.Ip.Udp.TotalLength, "Total Length"); Assert.IsTrue(!udpLayer.CalculateChecksum && packet.Ethernet.Ip.Udp.Checksum == 0 || udpLayer.CalculateChecksum && packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect"); Assert.IsTrue(packet.Ethernet.Ip.Udp.IsChecksumOptional, "IsChecksumOptional"); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Udp.Payload, "Payload"); } }
public void RandomDnsTest() { 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 != 1000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(true); UdpLayer udpLayer = random.NextUdpLayer(); udpLayer.Checksum = null; DnsLayer dnsLayer; do { dnsLayer = random.NextDnsLayer(); } while (dnsLayer.Length > 65000 - ipLayer.Length); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, dnsLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // DNS DnsLayer actualLayer = (DnsLayer)packet.Ethernet.Ip.Udp.Dns.ExtractLayer(); Assert.AreEqual(dnsLayer, actualLayer, "DNS Layer"); Assert.IsTrue(packet.Ethernet.Ip.Udp.Dns.IsValid); DnsDataResourceRecord opt = packet.Ethernet.Ip.Udp.Dns.Additionals.FirstOrDefault(additional => additional.DnsType == DnsType.Opt); Assert.AreEqual(opt, packet.Ethernet.Ip.Udp.Dns.OptionsRecord); foreach (var record in packet.Ethernet.Ip.Udp.Dns.ResourceRecords) { Assert.IsTrue(record.Equals(record)); Assert.IsTrue(record.DomainName.Equals((object)record.DomainName)); Assert.IsTrue(record.DomainName.Equals((object)record.DomainName)); Assert.AreEqual(record.GetHashCode(), record.GetHashCode()); } foreach (var record in packet.Ethernet.Ip.Udp.Dns.DataResourceRecords) { MoreAssert.IsBiggerOrEqual(9, record.ToString().Length); Assert.IsTrue(record.Equals((object)record)); Assert.IsInstanceOfType(record.Data, DnsResourceData.GetDnsResourceDataType(record.DnsType) ?? typeof(DnsResourceDataAnything)); Assert.IsTrue(record.DomainName.Equals((object)record.DomainName)); Assert.IsFalse(record.Data.Equals(null)); } } }
public void AutoSetEtherTypeTest() { Random random = new Random(); EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None); VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer(EthernetType.None); IpV4Layer ipV4Layer = random.NextIpV4Layer(); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, vLanTaggedFrameLayer, ipV4Layer); ethernetLayer.EtherType = EthernetType.VLanTaggedFrame; vLanTaggedFrameLayer.EtherType = EthernetType.IpV4; // Test equality. Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer()); Assert.AreEqual(EthernetType.IpV4, packet.Ethernet.VLanTaggedFrame.EtherType); Assert.AreEqual(vLanTaggedFrameLayer, packet.Ethernet.VLanTaggedFrame.ExtractLayer()); ipV4Layer.HeaderChecksum = packet.Ethernet.VLanTaggedFrame.IpV4.HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.VLanTaggedFrame.IpV4.ExtractLayer()); }
public void RandomUdpTest() { 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 != 1000; ++i) { UdpLayer udpLayer = random.NextUdpLayer(); udpLayer.Checksum = null; PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000)); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, udpLayer, payloadLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // Ethernet ethernetLayer.EtherType = EthernetType.IpV4; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); // IpV4 ipV4Layer.Protocol = IpV4Protocol.Udp; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); ipV4Layer.HeaderChecksum = null; // UDP udpLayer.Checksum = packet.Ethernet.IpV4.Udp.Checksum; Assert.AreEqual(udpLayer, packet.Ethernet.IpV4.Udp.ExtractLayer(), "UDP Layer"); Assert.AreEqual(UdpDatagram.HeaderLength + payloadLayer.Length, packet.Ethernet.IpV4.Udp.TotalLength, "Total Length"); Assert.IsTrue(!udpLayer.CalculateChecksum && packet.Ethernet.IpV4.Udp.Checksum == 0 || udpLayer.CalculateChecksum && packet.Ethernet.IpV4.IsTransportChecksumCorrect, "IsTransportChecksumCorrect"); Assert.IsTrue(packet.Ethernet.IpV4.Udp.IsChecksumOptional, "IsChecksumOptional"); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.IpV4.Udp.Payload, "Payload"); } }
public void RandomTcpTest() { MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05"); MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5"); EthernetLayer ethernetLayer = new EthernetLayer { Source = ethernetSource, Destination = ethernetDestination }; int seed = new Random().Next(); Console.WriteLine("Seed: " + seed); Random random = new Random(seed); for (int i = 0; i != 1000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Tcp, false); EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6; Layer ipLayer = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer); TcpLayer tcpLayer = random.NextTcpLayer(); PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000)); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, tcpLayer, payloadLayer); Assert.IsTrue(packet.IsValid); // Ethernet ethernetLayer.EtherType = ethernetType; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); ethernetLayer.EtherType = EthernetType.None; // Ip. if (ipLayer == ipV4Layer) { // IpV4. ipV4Layer.Protocol = IpV4Protocol.Tcp; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer"); ipV4Layer.HeaderChecksum = null; } else { Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IPv6 Layer"); } // TCP tcpLayer.Checksum = packet.Ethernet.Ip.Tcp.Checksum; Assert.AreEqual(tcpLayer, packet.Ethernet.Ip.Tcp.ExtractLayer(), "TCP Layer"); Assert.AreNotEqual(random.NextTcpLayer(), packet.Ethernet.Ip.Tcp.ExtractLayer(), "TCP Layer"); Assert.AreEqual(tcpLayer.GetHashCode(), packet.Ethernet.Ip.Tcp.ExtractLayer().GetHashCode(), "TCP Layer"); Assert.AreNotEqual(random.NextTcpLayer().GetHashCode(), packet.Ethernet.Ip.Tcp.ExtractLayer().GetHashCode(), "TCP Layer"); Assert.AreEqual((uint)(packet.Ethernet.Ip.Tcp.SequenceNumber + packet.Ethernet.Ip.Tcp.PayloadLength), packet.Ethernet.Ip.Tcp.NextSequenceNumber); foreach (TcpOption option in packet.Ethernet.Ip.Tcp.Options.OptionsCollection) { Assert.AreEqual(option, option); Assert.AreEqual(option.GetHashCode(), option.GetHashCode()); Assert.IsFalse(string.IsNullOrEmpty(option.ToString())); Assert.IsFalse(option.Equals(null)); Assert.IsFalse(option.Equals(2)); } Assert.AreEqual(tcpLayer.Options, packet.Ethernet.Ip.Tcp.Options, "Options"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.Ip.Tcp.IsAcknowledgment, "IsAcknowledgment"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.CongestionWindowReduced) == TcpControlBits.CongestionWindowReduced, packet.Ethernet.Ip.Tcp.IsCongestionWindowReduced, "IsCongestionWindowReduced"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.ExplicitCongestionNotificationEcho) == TcpControlBits.ExplicitCongestionNotificationEcho, packet.Ethernet.Ip.Tcp.IsExplicitCongestionNotificationEcho, "IsExplicitCongestionNotificationEcho"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin, packet.Ethernet.Ip.Tcp.IsFin, "IsFin"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Push) == TcpControlBits.Push, packet.Ethernet.Ip.Tcp.IsPush, "IsPush"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Reset) == TcpControlBits.Reset, packet.Ethernet.Ip.Tcp.IsReset, "IsReset"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Synchronize) == TcpControlBits.Synchronize, packet.Ethernet.Ip.Tcp.IsSynchronize, "IsSynchronize"); Assert.AreEqual((tcpLayer.ControlBits & TcpControlBits.Urgent) == TcpControlBits.Urgent, packet.Ethernet.Ip.Tcp.IsUrgent, "IsUrgent"); Assert.AreEqual(0, packet.Ethernet.Ip.Tcp.Reserved); Assert.IsFalse(packet.Ethernet.Ip.Tcp.IsChecksumOptional, "IsChecksumOptional"); Assert.AreEqual(TcpDatagram.HeaderMinimumLength + tcpLayer.Options.BytesLength + payloadLayer.Length, packet.Ethernet.Ip.Tcp.Length, "Total Length"); Assert.IsTrue(packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect"); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Tcp.Payload, "Payload"); } }
public void RandomIpV4Test() { MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05"); MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5"); const EthernetType EthernetType = EthernetType.IpV4; EthernetLayer ethernetLayer = new EthernetLayer { Source = ethernetSource, Destination = ethernetDestination, EtherType = EthernetType }; Random random = new Random(); for (int i = 0; i != 1000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(); ipV4Layer.HeaderChecksum = null; PayloadLayer payloadLayer = random.NextPayloadLayer(random.NextInt(0, 50 * 1024)); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, payloadLayer); Assert.IsTrue(ipV4Layer.Protocol == IpV4Protocol.Udp || ipV4Layer.Protocol == IpV4Protocol.Tcp || ipV4Layer.Protocol == IpV4Protocol.InternetGroupManagementProtocol || ipV4Layer.Protocol == IpV4Protocol.InternetControlMessageProtocol || ipV4Layer.Protocol == IpV4Protocol.Gre || packet.IsValid, "IsValid (" + ipV4Layer.Protocol + ")"); // Ethernet Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength"); Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); // IpV4 ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); Assert.AreEqual(ipV4Layer.Destination, packet.Ethernet.IpV4.Destination, "Destination"); Assert.AreNotEqual(ipV4Layer, null); Assert.AreNotEqual(ipV4Layer, new PayloadLayer()); Assert.IsNotNull(ipV4Layer.ToString()); Assert.AreEqual(IpV4Datagram.HeaderMinimumLength + ipV4Layer.Options.BytesLength, packet.Ethernet.IpV4.HeaderLength, "IP HeaderLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.IpV4.TotalLength, "IP TotalLength"); Assert.AreNotEqual(2, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation"); Assert.IsTrue(ipV4Layer.Fragmentation == packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation"); Assert.IsFalse(ipV4Layer.Fragmentation != packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation"); Assert.IsFalse(ipV4Layer.Fragmentation.Equals(0), "IP Fragmentation"); if (ipV4Layer.Fragmentation.Offset != 0) Assert.AreNotEqual(ipV4Layer.Fragmentation, IpV4Fragmentation.None, "IP Fragmentation"); Assert.AreEqual(ipV4Layer.Fragmentation.GetHashCode(), packet.Ethernet.IpV4.Fragmentation.GetHashCode(), "IP Fragmentation"); Assert.AreEqual(ipV4Layer.Fragmentation.Options, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation"); Assert.AreEqual(ipV4Layer.Fragmentation.Offset, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation"); if (ipV4Layer.Fragmentation.Equals(IpV4Fragmentation.None)) { Assert.AreEqual(IpV4FragmentationOptions.None, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation"); Assert.AreEqual(0, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation"); } Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect"); Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options, "IP Options"); Assert.AreNotEqual(packet.Ethernet.IpV4.Options, null, "IP Options"); Assert.AreNotEqual(new IpV4Options(new IpV4OptionUnknown(0, new byte[35])), packet.Ethernet.IpV4.Options, "IP Options"); Assert.AreEqual(ipV4Layer.Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode"); Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString()); for (int optionIndex = 0; optionIndex != ipV4Layer.Options.Count; ++optionIndex) { IpV4Option option = ipV4Layer.Options[optionIndex]; Assert.AreEqual(option, packet.Ethernet.IpV4.Options[optionIndex]); Assert.IsFalse(option.Equals(null)); } if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp) Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(TcpDatagram)); else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp) Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(UdpDatagram)); else Assert.IsNull(packet.Ethernet.IpV4.Transport); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.IpV4.Payload, "IP Payload"); } }
private static Packet BuildPacket(string httpString) { MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05"); MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5"); EthernetLayer ethernetLayer = new EthernetLayer { Source = ethernetSource, Destination = ethernetDestination }; Random random = new Random(); IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; TcpLayer tcpLayer = random.NextTcpLayer(); PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes(httpString)) }; Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); Assert.IsTrue(packet.IsValid); // Ethernet ethernetLayer.EtherType = EthernetType.IpV4; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); // IpV4 ipV4Layer.Protocol = IpV4Protocol.Tcp; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); ipV4Layer.HeaderChecksum = null; // TCP tcpLayer.Checksum = packet.Ethernet.IpV4.Tcp.Checksum; Assert.AreEqual(tcpLayer, packet.Ethernet.IpV4.Tcp.ExtractLayer(), "TCP Layer"); return packet; }
public void RandomHttpTest() { int seed = new Random().Next(); Console.WriteLine("Seed: " + seed); Random random = new Random(seed); for (int i = 0; i != 200; ++i) { EthernetLayer ethernetLayer = random.NextEthernetLayer(EthernetType.None); IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(true); TcpLayer tcpLayer = random.NextTcpLayer(); tcpLayer.Checksum = null; HttpLayer httpLayer = random.NextHttpLayer(); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, tcpLayer, httpLayer); Assert.IsTrue(packet.IsValid, "IsValid"); HttpDatagram httpDatagram = packet.Ethernet.Ip.Tcp.Http; Assert.AreEqual(httpLayer.Version, httpDatagram.Version); if (httpLayer.Version != null) Assert.AreEqual(httpLayer.Version.GetHashCode(), httpDatagram.Version.GetHashCode()); if (httpLayer is HttpRequestLayer) { Assert.IsTrue(httpDatagram.IsRequest); Assert.IsTrue(httpLayer.IsRequest); Assert.IsFalse(httpDatagram.IsResponse); Assert.IsFalse(httpLayer.IsResponse); HttpRequestLayer httpRequestLayer = (HttpRequestLayer)httpLayer; HttpRequestDatagram httpRequestDatagram = (HttpRequestDatagram)httpDatagram; Assert.AreEqual(httpRequestLayer.Method, httpRequestDatagram.Method); if (httpRequestLayer.Method != null) { Assert.AreEqual(httpRequestLayer.Method.GetHashCode(), httpRequestDatagram.Method.GetHashCode()); Assert.AreEqual(httpRequestLayer.Method.KnownMethod, httpRequestDatagram.Method.KnownMethod); } Assert.AreEqual(httpRequestLayer.Uri, httpRequestDatagram.Uri); } else { Assert.IsFalse(httpDatagram.IsRequest); Assert.IsFalse(httpLayer.IsRequest); Assert.IsTrue(httpDatagram.IsResponse); Assert.IsTrue(httpLayer.IsResponse); HttpResponseLayer httpResponseLayer = (HttpResponseLayer)httpLayer; HttpResponseDatagram httpResponseDatagram = (HttpResponseDatagram)httpDatagram; Assert.AreEqual(httpResponseLayer.StatusCode, httpResponseDatagram.StatusCode); Assert.AreEqual(httpResponseLayer.ReasonPhrase, httpResponseDatagram.ReasonPhrase); } Assert.AreEqual(httpLayer.Header, httpDatagram.Header); if (httpLayer.Header != null) { Assert.AreEqual(httpLayer.Header.GetHashCode(), httpDatagram.Header.GetHashCode()); foreach (var field in httpLayer.Header) Assert.IsFalse(field.Equals("abc")); foreach (var field in (IEnumerable)httpLayer.Header) Assert.IsFalse(field.Equals("abc")); MoreAssert.AreSequenceEqual(httpLayer.Header.Select(field => field.GetHashCode()), httpDatagram.Header.Select(field => field.GetHashCode())); if (httpLayer.Header.ContentType != null) { var parameters = httpLayer.Header.ContentType.Parameters; Assert.IsNotNull(((IEnumerable)parameters).GetEnumerator()); Assert.AreEqual<object>(parameters, httpDatagram.Header.ContentType.Parameters); Assert.AreEqual(parameters.GetHashCode(), httpDatagram.Header.ContentType.Parameters.GetHashCode()); Assert.AreEqual(parameters.Count, httpDatagram.Header.ContentType.Parameters.Count); int maxParameterNameLength = parameters.Any() ? parameters.Max(pair => pair.Key.Length) : 0; Assert.IsNull(parameters[new string('a', maxParameterNameLength + 1)]); } } Assert.AreEqual(httpLayer.Body, httpDatagram.Body); Assert.AreEqual(httpLayer, httpDatagram.ExtractLayer(), "HTTP Layer"); Assert.AreEqual(httpLayer.Length, httpDatagram.Length); } }
public void RandomGreTest() { 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) { GreLayer greLayer = random.NextGreLayer(); PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100)); PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer, payloadLayer); Packet packet = packetBuilder.Build(DateTime.Now); if (greLayer.Checksum == null && !new[] { EthernetType.IpV4, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(packet.Ethernet.IpV4.Gre.ProtocolType)) { Assert.IsTrue(packet.IsValid, "IsValid, ProtocolType=" + packet.Ethernet.IpV4.Gre.ProtocolType); } // Ethernet ethernetLayer.EtherType = EthernetType.IpV4; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); // IPv4 ipV4Layer.Protocol = IpV4Protocol.Gre; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer()); ipV4Layer.HeaderChecksum = null; Assert.AreEqual(ipV4Layer.Length, packet.Ethernet.IpV4.HeaderLength); Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect); Assert.AreEqual(ipV4Layer.Length + greLayer.Length + payloadLayer.Length, packet.Ethernet.IpV4.TotalLength); Assert.AreEqual(IpV4Datagram.DefaultVersion, packet.Ethernet.IpV4.Version); // GRE GreDatagram actualGre = packet.Ethernet.IpV4.Gre; GreLayer actualGreLayer = (GreLayer)actualGre.ExtractLayer(); if (greLayer.ChecksumPresent && greLayer.Checksum == null) { Assert.IsTrue(actualGre.IsChecksumCorrect); greLayer.Checksum = actualGre.Checksum; } Assert.AreEqual(greLayer, actualGreLayer, "Layer"); if (actualGreLayer.Key != null) actualGreLayer.SetKey(actualGreLayer.KeyPayloadLength.Value, actualGreLayer.KeyCallId.Value); else { Assert.IsNull(actualGreLayer.KeyPayloadLength); Assert.IsNull(actualGreLayer.KeyCallId); } Assert.AreEqual(greLayer, actualGreLayer, "Layer"); if (actualGre.KeyPresent) { Assert.AreEqual(greLayer.KeyPayloadLength, actualGre.KeyPayloadLength, "KeyPayloadLength"); Assert.AreEqual(greLayer.KeyCallId, actualGre.KeyCallId, "KeyCallId"); } Assert.AreNotEqual(random.NextGreLayer(), actualGreLayer, "Not Layer"); Assert.AreEqual(greLayer.Length, actualGre.HeaderLength); Assert.IsTrue(actualGre.KeyPresent ^ (greLayer.Key == null)); MoreAssert.IsSmaller(8, actualGre.RecursionControl); MoreAssert.IsSmaller(32, actualGre.FutureUseBits); Assert.IsTrue(actualGre.RoutingPresent ^ (greLayer.Routing == null && greLayer.RoutingOffset == null)); Assert.IsTrue(actualGre.SequenceNumberPresent ^ (greLayer.SequenceNumber == null)); Assert.IsTrue(!actualGre.StrictSourceRoute || actualGre.RoutingPresent); if (actualGre.RoutingPresent) { Assert.IsNotNull(actualGre.ActiveSourceRouteEntryIndex); if (actualGre.ActiveSourceRouteEntryIndex < actualGre.Routing.Count) Assert.IsNotNull(actualGre.ActiveSourceRouteEntry); foreach (GreSourceRouteEntry entry in actualGre.Routing) { Assert.AreNotEqual(entry, 2); Assert.AreEqual(entry.GetHashCode(), entry.GetHashCode()); switch (entry.AddressFamily) { case GreSourceRouteEntryAddressFamily.AsSourceRoute: GreSourceRouteEntryAs asEntry = (GreSourceRouteEntryAs)entry; MoreAssert.IsInRange(0, asEntry.AsNumbers.Count, asEntry.NextAsNumberIndex); if (asEntry.NextAsNumberIndex != asEntry.AsNumbers.Count) Assert.AreEqual(asEntry.AsNumbers[asEntry.NextAsNumberIndex], asEntry.NextAsNumber); break; case GreSourceRouteEntryAddressFamily.IpSourceRoute: GreSourceRouteEntryIp ipEntry = (GreSourceRouteEntryIp)entry; MoreAssert.IsInRange(0, ipEntry.Addresses.Count, ipEntry.NextAddressIndex); if (ipEntry.NextAddressIndex != ipEntry.Addresses.Count) Assert.AreEqual(ipEntry.Addresses[ipEntry.NextAddressIndex], ipEntry.NextAddress); break; default: GreSourceRouteEntryUnknown unknownEntry = (GreSourceRouteEntryUnknown)entry; MoreAssert.IsInRange(0, unknownEntry.Data.Length, unknownEntry.PayloadOffset); break; } } } else { Assert.IsNull(actualGre.ActiveSourceRouteEntry); } Assert.IsNotNull(actualGre.Payload); switch (actualGre.ProtocolType) { case EthernetType.IpV4: Assert.IsNotNull(actualGre.IpV4); break; case EthernetType.Arp: Assert.IsNotNull(actualGre.Arp); break; } } }
public void InvalidGreTest() { 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(); for (int i = 0; i != 100; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(IpV4Protocol.Gre); ipV4Layer.HeaderChecksum = null; GreLayer greLayer = random.NextGreLayer(); greLayer.Checksum = null; greLayer.Routing = new List<GreSourceRouteEntry> { new GreSourceRouteEntryAs(new List<ushort> {123}.AsReadOnly(), 0), new GreSourceRouteEntryIp(new List<IpV4Address> {random.NextIpV4Address()}.AsReadOnly(), 0) }.AsReadOnly(); PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer); Packet packet = packetBuilder.Build(DateTime.Now); Assert.IsTrue(packet.IsValid || new[] {EthernetType.IpV4, EthernetType.Arp, EthernetType.VLanTaggedFrame}.Contains(greLayer.ProtocolType), "IsValid. ProtoclType=" + greLayer.ProtocolType); GreDatagram gre = packet.Ethernet.IpV4.Gre; // Remove a byte from routing Datagram newIpPayload = new Datagram(gre.Take(gre.Length - 1).ToArray()); packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, new PayloadLayer {Data = newIpPayload}); packet = packetBuilder.Build(DateTime.Now); Assert.IsNull(packet.Ethernet.IpV4.Gre.Payload); Assert.IsFalse(packet.IsValid); // SreLength is too big byte[] buffer = gre.ToArray(); buffer[buffer.Length - 1] = 200; newIpPayload = new Datagram(buffer); packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, new PayloadLayer {Data = newIpPayload}); packet = packetBuilder.Build(DateTime.Now); Assert.IsFalse(packet.IsValid); // PayloadOffset is too big buffer = gre.ToArray(); buffer[gre.Length - 10] = 100; newIpPayload = new Datagram(buffer); packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, new PayloadLayer {Data = newIpPayload}); packet = packetBuilder.Build(DateTime.Now); Assert.IsFalse(packet.IsValid); // PayloadOffset isn't aligned to ip buffer = gre.ToArray(); buffer[gre.Length - 10] = 3; newIpPayload = new Datagram(buffer); packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, new PayloadLayer {Data = newIpPayload}); packet = packetBuilder.Build(DateTime.Now); Assert.IsFalse(packet.IsValid); // PayloadOffset isn't aligned to as buffer = gre.ToArray(); buffer[gre.Length - 16] = 1; newIpPayload = new Datagram(buffer); packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, new PayloadLayer {Data = newIpPayload}); packet = packetBuilder.Build(DateTime.Now); Assert.IsFalse(packet.IsValid); } }
private static void CreateRandomIpPayload(Random random, Layer ipLayer, List<ILayer> layers) { IpV6Layer ipV6Layer = ipLayer as IpV6Layer; if (ipV6Layer != null) { var headers = ipV6Layer.ExtensionHeaders.Headers; if (headers.Any() && headers.Last().Protocol == IpV4Protocol.EncapsulatingSecurityPayload) return; } if (ipV6Layer != null ? ipV6Layer.LastNextHeader != null : random.NextBool(20)) { // Finish with payload. PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100)); layers.Add(payloadLayer); return; } IpV4Layer ipV4Layer = ipLayer as IpV4Layer; if (ipV4Layer != null) { ipV4Layer.Protocol = null; if (random.NextBool()) ipV4Layer.Fragmentation = IpV4Fragmentation.None; } switch (random.Next(0, 11)) { case 0: // IpV4. case 1: IpV4Layer innerIpV4Layer = random.NextIpV4Layer(); layers.Add(innerIpV4Layer); CreateRandomIpPayload(random, innerIpV4Layer, layers); return; case 2: // IpV6. case 3: IpV6Layer innerIpV6Layer = random.NextIpV6Layer(random.NextBool(20)); layers.Add(innerIpV6Layer); CreateRandomIpPayload(random, innerIpV6Layer, layers); return; case 4: // Igmp. layers.Add(random.NextIgmpLayer()); return; case 5: // Icmp. IcmpLayer icmpLayer = random.NextIcmpLayer(); layers.Add(icmpLayer); layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer)); return; case 6: // Gre. GreLayer greLayer = random.NextGreLayer(); layers.Add(greLayer); CreateRandomEthernetPayload(random, greLayer, layers); return; case 7: // Udp. case 8: UdpLayer udpLayer = random.NextUdpLayer(); layers.Add(udpLayer); CreateRandomUdpPayload(random, udpLayer, layers); return; case 9: // Tcp. case 10: TcpLayer tcpLayer = random.NextTcpLayer(); layers.Add(tcpLayer); CreateRandomTcpPayload(random, tcpLayer, layers); return; default: throw new InvalidOperationException("Invalid value."); } }
private static void CreateRandomEthernetPayload(Random random, EthernetBaseLayer ethernetBaseLayer, List<ILayer> layers) { if (random.NextBool(20)) { // Finish with payload. PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100)); layers.Add(payloadLayer); return; } ethernetBaseLayer.EtherType = EthernetType.None; switch (random.NextInt(0, 7)) { case 0: // VLanTaggedFrame. case 1: VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer(); layers.Add(vLanTaggedFrameLayer); CreateRandomEthernetPayload(random, vLanTaggedFrameLayer, layers); return; case 2: // ARP. EthernetLayer ethernetLayer = (ethernetBaseLayer as EthernetLayer); if (ethernetLayer != null) ethernetLayer.Destination = MacAddress.Zero; layers.Add(random.NextArpLayer()); return; case 3: // IPv4. case 4: IpV4Layer ipV4Layer = random.NextIpV4Layer(); layers.Add(ipV4Layer); CreateRandomIpPayload(random, ipV4Layer, layers); return; case 5: // IPv6 case 6: IpV6Layer ipV6Layer = random.NextIpV6Layer(random.NextBool(20)); layers.Add(ipV6Layer); CreateRandomIpPayload(random, ipV6Layer, layers); return; default: throw new InvalidOperationException("Invalid value."); } }
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()); } }
private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List<ILayer> layers) { if (random.NextBool(20)) { // Finish with payload. PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100)); layers.Add(payloadLayer); return; } ipV4Layer.Protocol = null; if (random.NextBool()) ipV4Layer.Fragmentation = IpV4Fragmentation.None; switch (random.Next(0, 9)) { case 0: // IpV4. case 1: IpV4Layer innerIpV4Layer = random.NextIpV4Layer(); layers.Add(innerIpV4Layer); CreateRandomIpV4Payload(random, innerIpV4Layer, layers); return; case 2: // Igmp. layers.Add(random.NextIgmpLayer()); return; case 3: // Icmp. IcmpLayer icmpLayer = random.NextIcmpLayer(); layers.Add(icmpLayer); layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer)); return; case 4: // Gre. GreLayer greLayer = random.NextGreLayer(); layers.Add(greLayer); CreateRandomEthernetPayload(random, greLayer, layers); return; case 5: // Udp. case 6: UdpLayer udpLayer = random.NextUdpLayer(); layers.Add(udpLayer); CreateRandomUdpPayload(random, udpLayer, layers); return; case 7: // Tcp. case 8: TcpLayer tcpLayer = random.NextTcpLayer(); layers.Add(tcpLayer); CreateRandomTcpPayload(random, tcpLayer, layers); return; default: throw new InvalidOperationException("Invalid value."); } }
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 RandomIcmpTest() { 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 != 2000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.InternetControlMessageProtocol, false); IcmpLayer icmpLayer = random.NextIcmpLayer(); icmpLayer.Checksum = null; if (icmpLayer.MessageType == IcmpMessageType.DestinationUnreachable && icmpLayer.MessageTypeAndCode != IcmpMessageTypeAndCode.DestinationUnreachableFragmentationNeededAndDoNotFragmentSet) { ((IcmpDestinationUnreachableLayer)icmpLayer).NextHopMaximumTransmissionUnit = 0; } IEnumerable<ILayer> icmpPayloadLayers = random.NextIcmpPayloadLayers(icmpLayer); int icmpPayloadLength = icmpPayloadLayers.Select(layer => layer.Length).Sum(); switch (icmpLayer.MessageType) { case IcmpMessageType.ParameterProblem: if (icmpPayloadLength % 4 != 0) icmpPayloadLayers = icmpPayloadLayers.Concat(new[] {new PayloadLayer {Data = random.NextDatagram(4 - icmpPayloadLength % 4)}}); icmpPayloadLength = icmpPayloadLayers.Select(layer => layer.Length).Sum(); IcmpParameterProblemLayer icmpParameterProblemLayer = (IcmpParameterProblemLayer)icmpLayer; icmpParameterProblemLayer.Pointer = (byte)(icmpParameterProblemLayer.Pointer % icmpPayloadLength); icmpParameterProblemLayer.OriginalDatagramLength = icmpPayloadLength - icmpPayloadLayers.First().Length; break; case IcmpMessageType.SecurityFailures: ((IcmpSecurityFailuresLayer)icmpLayer).Pointer %= (ushort)icmpPayloadLength; break; } PacketBuilder packetBuilder = new PacketBuilder(new ILayer[] { ethernetLayer, ipLayer, icmpLayer }.Concat(icmpPayloadLayers)); Packet packet = packetBuilder.Build(DateTime.Now); Assert.IsTrue(packet.IsValid, "IsValid"); byte[] buffer = (byte[])packet.Buffer.Clone(); buffer.Write(ethernetLayer.Length + ipLayer.Length, random.NextDatagram(icmpLayer.Length)); Packet illegalPacket = new Packet(buffer, DateTime.Now, packet.DataLink); Assert.IsFalse(illegalPacket.IsValid, "IsInvalid"); if (illegalPacket.Ethernet.Ip.Icmp is IcmpUnknownDatagram) { byte[] icmpBuffer = new byte[illegalPacket.Ethernet.Ip.Icmp.ExtractLayer().Length]; ILayer layer = illegalPacket.Ethernet.Ip.Icmp.ExtractLayer(); layer.Write(icmpBuffer,0,icmpBuffer.Length, null,null); layer.Finalize(icmpBuffer,0,icmpBuffer.Length,null); MoreAssert.AreSequenceEqual(illegalPacket.Ethernet.Ip.Icmp.ToArray(), icmpBuffer); Assert.AreEqual(illegalPacket, PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, illegalPacket.Ethernet.Ip.Icmp.ExtractLayer())); } // Ethernet ethernetLayer.EtherType = ipLayer == ipV4Layer ? EthernetType.IpV4 : EthernetType.IpV6; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); ethernetLayer.EtherType = EthernetType.None; // IP. if (ipLayer == ipV4Layer) { // IPv4. ipV4Layer.Protocol = IpV4Protocol.InternetControlMessageProtocol; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer()); ipV4Layer.HeaderChecksum = null; Assert.AreEqual(ipV4Layer.Length, packet.Ethernet.IpV4.HeaderLength); Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect); Assert.AreEqual(ipV4Layer.Length + icmpLayer.Length + icmpPayloadLength, packet.Ethernet.IpV4.TotalLength); Assert.AreEqual(IpV4Datagram.DefaultVersion, packet.Ethernet.IpV4.Version); } else { // IPv6. Assert.AreEqual(ipLayer, packet.Ethernet.IpV6.ExtractLayer()); } // ICMP IcmpDatagram actualIcmp = packet.Ethernet.Ip.Icmp; IcmpLayer actualIcmpLayer = (IcmpLayer)actualIcmp.ExtractLayer(); icmpLayer.Checksum = actualIcmpLayer.Checksum; Assert.AreEqual(icmpLayer, actualIcmpLayer); Assert.AreEqual(icmpLayer.GetHashCode(), actualIcmpLayer.GetHashCode()); if (actualIcmpLayer.MessageType != IcmpMessageType.RouterSolicitation) { Assert.AreNotEqual(random.NextIcmpLayer(), actualIcmpLayer); IcmpLayer otherIcmpLayer = random.NextIcmpLayer(); Assert.AreNotEqual(otherIcmpLayer.GetHashCode(), actualIcmpLayer.GetHashCode()); } Assert.IsTrue(actualIcmp.IsChecksumCorrect); Assert.AreEqual(icmpLayer.MessageType, actualIcmp.MessageType); Assert.AreEqual(icmpLayer.CodeValue, actualIcmp.Code); Assert.AreEqual(icmpLayer.MessageTypeAndCode, actualIcmp.MessageTypeAndCode); Assert.AreEqual(packet.Length - ethernetLayer.Length - ipLayer.Length - IcmpDatagram.HeaderLength, actualIcmp.Payload.Length); Assert.IsNotNull(icmpLayer.ToString()); switch (packet.Ethernet.Ip.Icmp.MessageType) { case IcmpMessageType.RouterSolicitation: case IcmpMessageType.SourceQuench: case IcmpMessageType.TimeExceeded: Assert.AreEqual<uint>(0, actualIcmp.Variable); break; case IcmpMessageType.DestinationUnreachable: case IcmpMessageType.ParameterProblem: case IcmpMessageType.Redirect: case IcmpMessageType.ConversionFailed: case IcmpMessageType.Echo: case IcmpMessageType.EchoReply: case IcmpMessageType.Timestamp: case IcmpMessageType.TimestampReply: case IcmpMessageType.InformationRequest: case IcmpMessageType.InformationReply: case IcmpMessageType.RouterAdvertisement: case IcmpMessageType.AddressMaskRequest: case IcmpMessageType.AddressMaskReply: break; case IcmpMessageType.TraceRoute: Assert.AreEqual(((IcmpTraceRouteLayer)icmpLayer).OutboundHopCount == 0xFFFF, ((IcmpTraceRouteDatagram)actualIcmp).IsOutbound); break; case IcmpMessageType.DomainNameRequest: case IcmpMessageType.SecurityFailures: break; case IcmpMessageType.DomainNameReply: default: throw new InvalidOperationException("Invalid icmpMessageType " + packet.Ethernet.Ip.Icmp.MessageType); } } }