本文整理汇总了C#中System.Random.NextIpV6Layer方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextIpV6Layer方法的具体用法?C# Random.NextIpV6Layer怎么用?C# Random.NextIpV6Layer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextIpV6Layer方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomUdpTest
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");
}
}
示例2: RandomDnsTest
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));
}
}
}
示例3: RandomTcpTest
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");
}
}
示例4: RandomHttpTest
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);
}
}
示例5: CreateRandomIpPayload
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.");
}
}
示例6: CreateRandomEthernetPayload
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.");
}
}
示例7: RandomGreTest
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")
};
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.Gre, false);
GreLayer greLayer = random.NextGreLayer();
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, greLayer, payloadLayer);
Packet packet = packetBuilder.Build(DateTime.Now);
if (greLayer.Checksum == null &&
!new[] { EthernetType.IpV4, EthernetType.IpV6, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(packet.Ethernet.Ip.Gre.ProtocolType))
{
Assert.IsTrue(packet.IsValid, "IsValid, ProtocolType=" + packet.Ethernet.Ip.Gre.ProtocolType);
}
// 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.Gre;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.Ip.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.Ip.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.Ip.TotalLength);
Assert.AreEqual(IpV4Datagram.DefaultVersion, packet.Ethernet.Ip.Version);
}
else
{
// IPv6.
Assert.AreEqual(ipLayer, packet.Ethernet.Ip.ExtractLayer());
}
// GRE
GreDatagram actualGre = packet.Ethernet.Ip.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);
//.........这里部分代码省略.........
示例8: InvalidGreTest
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")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 100; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(IpV4Protocol.Gre);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.Gre, false);
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, ipLayer, greLayer);
Packet packet = packetBuilder.Build(DateTime.Now);
Assert.IsTrue(packet.IsValid ||
new[] { EthernetType.IpV4, EthernetType.IpV6, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(greLayer.ProtocolType),
"IsValid. ProtoclType=" + greLayer.ProtocolType);
GreDatagram gre = packet.Ethernet.Ip.Gre;
// Remove a byte from routing
Datagram newIpPayload = new Datagram(gre.Take(gre.Length - 1).ToArray());
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsNull(packet.Ethernet.Ip.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, ipLayer, 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, ipLayer, 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, ipLayer, 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, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsFalse(packet.IsValid);
}
}
示例9: RandomIpV6Test
public void RandomIpV6Test()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
const EthernetType EthernetType = EthernetType.IpV6;
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = ethernetSource,
Destination = ethernetDestination,
EtherType = EthernetType
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 1000; ++i)
{
IpV6Layer ipV6Layer = random.NextIpV6Layer(false);
PayloadLayer payloadLayer = random.NextPayloadLayer(random.NextInt(0, 50 * 1024));
List<ILayer> layers = new List<ILayer> {ethernetLayer, ipV6Layer};
if (ipV6Layer.ExtensionHeaders.LastHeader != IpV4Protocol.EncapsulatingSecurityPayload)
layers.Add(payloadLayer);
Packet packet = PacketBuilder.Build(DateTime.Now, layers);
Assert.IsTrue(ipV6Layer.LastNextHeader == IpV4Protocol.Ip ||
ipV6Layer.LastNextHeader == IpV4Protocol.IpV6||
ipV6Layer.LastNextHeader == IpV4Protocol.Udp ||
ipV6Layer.LastNextHeader == IpV4Protocol.Tcp ||
ipV6Layer.LastNextHeader == IpV4Protocol.InternetGroupManagementProtocol ||
ipV6Layer.LastNextHeader == IpV4Protocol.InternetControlMessageProtocol ||
ipV6Layer.LastNextHeader == IpV4Protocol.Gre ||
packet.IsValid, string.Format("IsValid ({0}...{1})", ipV6Layer.NextHeader, ipV6Layer.ExtensionHeaders.NextHeader));
// Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
// IpV6
Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer");
Assert.AreEqual(ipV6Layer.GetHashCode(), packet.Ethernet.IpV6.ExtractLayer().GetHashCode(), "IP Layer");
Assert.AreEqual(string.Format("{0} -> {1} ({2})", ipV6Layer.Source, ipV6Layer.CurrentDestination, ipV6Layer.NextHeader), ipV6Layer.ToString());
IEnumerator extensionHeadersEnumerator = ((IEnumerable)packet.Ethernet.IpV6.ExtensionHeaders).GetEnumerator();
for (int extensionHeaderIndex = 0; extensionHeaderIndex != packet.Ethernet.IpV6.ExtensionHeaders.Headers.Count; ++extensionHeaderIndex)
{
IpV6ExtensionHeader extensionHeader = packet.Ethernet.IpV6.ExtensionHeaders[extensionHeaderIndex];
Assert.IsTrue(extensionHeadersEnumerator.MoveNext());
Assert.AreEqual(extensionHeader, extensionHeadersEnumerator.Current);
IpV6ExtensionHeader layerExtensionheader = ipV6Layer.ExtensionHeaders[extensionHeaderIndex];
Assert.AreEqual(extensionHeader, layerExtensionheader);
Assert.AreEqual(extensionHeader.GetHashCode(), layerExtensionheader.GetHashCode());
IpV6ExtensionHeaderOptions extensionHeaderOptions = extensionHeader as IpV6ExtensionHeaderOptions;
if (extensionHeaderOptions != null)
{
foreach (IpV6Option option in extensionHeaderOptions.Options)
{
switch (option.OptionType)
{
case IpV6OptionType.SimplifiedMulticastForwardingDuplicatePacketDetection:
IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetection optionSimplifiedMulticastForwardingDuplicatePacketDetection = (IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetection)option;
Assert.AreEqual(optionSimplifiedMulticastForwardingDuplicatePacketDetection is IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetectionSequenceHashAssistValue, optionSimplifiedMulticastForwardingDuplicatePacketDetection.HashIndicator);
break;
case IpV6OptionType.QuickStart:
IpV6OptionQuickStart optionQuickStart = (IpV6OptionQuickStart)option;
MoreAssert.IsBiggerOrEqual(0, optionQuickStart.RateKbps);
break;
}
}
}
IpV6ExtensionHeaderMobility extensionHeaderMobility = extensionHeader as IpV6ExtensionHeaderMobility;
if (extensionHeaderMobility != null)
{
Assert.IsFalse(extensionHeaderMobility.Equals(2));
Assert.IsTrue(extensionHeaderMobility.Equals((object)extensionHeader));
Assert.AreEqual(extensionHeaderMobility.MobilityOptions, new IpV6MobilityOptions(extensionHeaderMobility.MobilityOptions).AsEnumerable());
foreach (IpV6MobilityOption option in extensionHeaderMobility.MobilityOptions)
{
switch (option.OptionType)
{
case IpV6MobilityOptionType.BindingIdentifier:
IpV6MobilityOptionBindingIdentifier optionBindingIdentifier = (IpV6MobilityOptionBindingIdentifier)option;
if (optionBindingIdentifier.IpV4CareOfAddress.HasValue)
Assert.AreEqual(optionBindingIdentifier.IpV4CareOfAddress.Value, optionBindingIdentifier.CareOfAddress);
else if (optionBindingIdentifier.IpV6CareOfAddress.HasValue)
Assert.AreEqual(optionBindingIdentifier.IpV6CareOfAddress.Value, optionBindingIdentifier.CareOfAddress);
else
Assert.IsNull(optionBindingIdentifier.CareOfAddress);
break;
case IpV6MobilityOptionType.AccessNetworkIdentifier:
IpV6MobilityOptionAccessNetworkIdentifier optionAccessNetworkIdentifier = (IpV6MobilityOptionAccessNetworkIdentifier)option;
foreach (IpV6AccessNetworkIdentifierSubOption subOption in optionAccessNetworkIdentifier.SubOptions)
{
switch (subOption.OptionType)
{
case IpV6AccessNetworkIdentifierSubOptionType.GeoLocation:
//.........这里部分代码省略.........
示例10: RandomIgmpTest
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;
//.........这里部分代码省略.........
示例11: RandomIcmpTest
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)
//.........这里部分代码省略.........