Commit 28d88cd9 authored by Brickner_cp's avatar Brickner_cp

IPv6

Code coverage 94.78%
Options implement IEnumerable.
parent 6fda5cb1
...@@ -101,6 +101,7 @@ namespace PcapDotNet.Core.Test ...@@ -101,6 +101,7 @@ namespace PcapDotNet.Core.Test
protocol == IpV4Protocol.EncapsulationHeader || protocol == IpV4Protocol.EncapsulationHeader ||
protocol == IpV4Protocol.GatewayToGateway || protocol == IpV4Protocol.GatewayToGateway ||
protocol == IpV4Protocol.SatMon || protocol == IpV4Protocol.SatMon ||
protocol == IpV4Protocol.VersatileMessageTransactionProtocol ||
protocol == IpV4Protocol.RemoteVirtualDiskProtocol)) protocol == IpV4Protocol.RemoteVirtualDiskProtocol))
return false; return false;
......
...@@ -83,6 +83,50 @@ namespace PcapDotNet.Packets.Test ...@@ -83,6 +83,50 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer"); Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer");
Assert.IsNotNull(ipV6Layer.GetHashCode()); Assert.IsNotNull(ipV6Layer.GetHashCode());
Assert.AreEqual(string.Format("{0} -> {1} ({2})", ipV6Layer.Source, ipV6Layer.CurrentDestination, ipV6Layer.NextHeader), ipV6Layer.ToString()); Assert.AreEqual(string.Format("{0} -> {1} ({2})", ipV6Layer.Source, ipV6Layer.CurrentDestination, ipV6Layer.NextHeader), ipV6Layer.ToString());
foreach (IpV6ExtensionHeader extensionHeader in packet.Ethernet.IpV6.ExtensionHeaders)
{
IpV6ExtensionHeaderMobility extensionHeaderMobility = extensionHeader as IpV6ExtensionHeaderMobility;
if (extensionHeaderMobility != null)
{
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:
IpV6AccessNetworkIdentifierSubOptionGeoLocation subOptionGeoLocation = (IpV6AccessNetworkIdentifierSubOptionGeoLocation)subOption;
MoreAssert.IsBiggerOrEqual(-90, subOptionGeoLocation.LatitudeDegreesReal);
MoreAssert.IsSmallerOrEqual(90, subOptionGeoLocation.LatitudeDegreesReal);
MoreAssert.IsBiggerOrEqual(-180, subOptionGeoLocation.LongitudeDegreesReal);
MoreAssert.IsSmallerOrEqual(180, subOptionGeoLocation.LongitudeDegreesReal);
break;
}
}
break;
case IpV6MobilityOptionType.Timestamp:
IpV6MobilityOptionTimestamp optionTimestamp = (IpV6MobilityOptionTimestamp)option;
MoreAssert.IsBiggerOrEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), optionTimestamp.TimestampDateTime);
break;
}
}
}
}
/* /*
if (packet.Ethernet.IpV6.NextHeader == IpV4Protocol.Tcp) if (packet.Ethernet.IpV6.NextHeader == IpV4Protocol.Tcp)
Assert.IsInstanceOfType(packet.Ethernet.IpV6.Transport, typeof(TcpDatagram)); Assert.IsInstanceOfType(packet.Ethernet.IpV6.Transport, typeof(TcpDatagram));
...@@ -124,7 +168,7 @@ namespace PcapDotNet.Packets.Test ...@@ -124,7 +168,7 @@ namespace PcapDotNet.Packets.Test
Packet packet = PacketBuilder.Build( Packet packet = PacketBuilder.Build(
DateTime.Now, DateTime.Now,
new EthernetLayer(), new EthernetLayer(),
new IpV6Layer() new IpV6Layer
{ {
ExtensionHeaders = new IpV6ExtensionHeaders( ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderDestinationOptions( new IpV6ExtensionHeaderDestinationOptions(
...@@ -135,5 +179,89 @@ namespace PcapDotNet.Packets.Test ...@@ -135,5 +179,89 @@ namespace PcapDotNet.Packets.Test
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.IsTrue(((IpV6OptionCalipso)((IpV6ExtensionHeaderDestinationOptions)packet.Ethernet.IpV6.ExtensionHeaders[0]).Options[0]).IsChecksumCorrect); Assert.IsTrue(((IpV6OptionCalipso)((IpV6ExtensionHeaderDestinationOptions)packet.Ethernet.IpV6.ExtensionHeaders[0]).Options[0]).IsChecksumCorrect);
} }
[TestMethod]
public void IpV6AccessNetworkIdentifierSubOptionUnknown()
{
IpV6AccessNetworkIdentifierSubOptionUnknown subOption =
new IpV6AccessNetworkIdentifierSubOptionUnknown((IpV6AccessNetworkIdentifierSubOptionType)100, DataSegment.Empty);
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderMobilityBindingError(
IpV4Protocol.Skip, 0, IpV6BindingErrorStatus.UnrecognizedMhTypeValue, IpV6Address.Zero,
new IpV6MobilityOptions(
new IpV6MobilityOptionAccessNetworkIdentifier(
new IpV6AccessNetworkIdentifierSubOptions(subOption)))))
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(subOption,
((IpV6MobilityOptionAccessNetworkIdentifier)
((IpV6ExtensionHeaderMobility)packet.Ethernet.IpV6.ExtensionHeaders[0]).MobilityOptions[0]).SubOptions[0]);
}
[TestMethod]
public void IpV6FlowIdentificationSubOptionUnknown()
{
IpV6FlowIdentificationSubOptionUnknown subOption =
new IpV6FlowIdentificationSubOptionUnknown((IpV6FlowIdentificationSubOptionType)100, DataSegment.Empty);
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderMobilityBindingError(
IpV4Protocol.Skip, 0, IpV6BindingErrorStatus.UnrecognizedMhTypeValue, IpV6Address.Zero,
new IpV6MobilityOptions(
new IpV6MobilityOptionFlowIdentification(0, 0, IpV6FlowIdentificationStatus.FlowIdentifierNotFound,
new IpV6FlowIdentificationSubOptions(subOption)))))
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(subOption,
((IpV6MobilityOptionFlowIdentification)
((IpV6ExtensionHeaderMobility)packet.Ethernet.IpV6.ExtensionHeaders[0]).MobilityOptions[0]).SubOptions[0]);
}
[TestMethod]
public void IpV6OptionUnknown()
{
IpV6OptionUnknown option = new IpV6OptionUnknown((IpV6OptionType)0xBB, DataSegment.Empty);
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderDestinationOptions(IpV4Protocol.Skip, new IpV6Options(option)))
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(option, ((IpV6ExtensionHeaderDestinationOptions)packet.Ethernet.IpV6.ExtensionHeaders[0]).Options[0]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6ExtensionHeaderRoutingRplCommonPrefixLengthForNonLastAddressesTooBig()
{
Assert.IsNull(new IpV6ExtensionHeaderRoutingRpl(IpV4Protocol.Skip, 0, 16, 0, new IpV6Address[0]));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6ExtensionHeaderRoutingRplCommonPrefixLengthForLastAddressTooBig()
{
Assert.IsNull(new IpV6ExtensionHeaderRoutingRpl(IpV4Protocol.Skip, 0, 0, 16, new IpV6Address[0]));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException), AllowDerivedTypes = false)]
public void IpV6ExtensionHeadersEncapsulatingSecurityPayloadBeforeLast()
{
Assert.IsNull(new IpV6ExtensionHeaders(new IpV6ExtensionHeaderEncapsulatingSecurityPayload(0, 0, DataSegment.Empty),
new IpV6ExtensionHeaderFragmentData(IpV4Protocol.Skip, 0, false, 0)));
}
} }
} }
...@@ -407,7 +407,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -407,7 +407,7 @@ namespace PcapDotNet.Packets.TestUtils
return new IpV6MobilityOptionLinkLocalAddress(random.NextIpV6Address()); return new IpV6MobilityOptionLinkLocalAddress(random.NextIpV6Address());
case IpV6MobilityOptionType.Timestamp: case IpV6MobilityOptionType.Timestamp:
return new IpV6MobilityOptionTimestamp(random.NextULong()); return new IpV6MobilityOptionTimestamp(random.NextULong() % (500000000000L << 16));
case IpV6MobilityOptionType.RestartCounter: case IpV6MobilityOptionType.RestartCounter:
return new IpV6MobilityOptionRestartCounter(random.NextUInt()); return new IpV6MobilityOptionRestartCounter(random.NextUInt());
...@@ -516,7 +516,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -516,7 +516,7 @@ namespace PcapDotNet.Packets.TestUtils
public static IpV6FlowIdentificationSubOptions NextIpV6FlowIdentificationSubOptions(this Random random) public static IpV6FlowIdentificationSubOptions NextIpV6FlowIdentificationSubOptions(this Random random)
{ {
return new IpV6FlowIdentificationSubOptions(((Func<IpV6FlowIdentificationSubOption>)(random.NextIpV6FlowIdentificationSubOption)).GenerateArray(random.NextInt(0, 10))); return new IpV6FlowIdentificationSubOptions(((Func<IpV6FlowIdentificationSubOption>)(random.NextIpV6FlowIdentificationSubOption)).GenerateArray(random.NextInt(0, 9)));
} }
public static IpV6FlowIdentificationSubOption NextIpV6FlowIdentificationSubOption(this Random random) public static IpV6FlowIdentificationSubOption NextIpV6FlowIdentificationSubOption(this Random random)
...@@ -567,7 +567,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -567,7 +567,7 @@ namespace PcapDotNet.Packets.TestUtils
random.NextDataSegment(random.NextInt(0, 100))); random.NextDataSegment(random.NextInt(0, 100)));
case IpV6AccessNetworkIdentifierSubOptionType.GeoLocation: case IpV6AccessNetworkIdentifierSubOptionType.GeoLocation:
return new IpV6AccessNetworkIdentifierSubOptionGeoLocation(random.NextUInt24(), random.NextUInt24()); return new IpV6AccessNetworkIdentifierSubOptionGeoLocation((UInt24)(random.NextUInt24() & 0x9FFFFF), random.NextUInt24());
case IpV6AccessNetworkIdentifierSubOptionType.OperatorIdentifier: case IpV6AccessNetworkIdentifierSubOptionType.OperatorIdentifier:
return new IpV6AccessNetworkIdentifierSubOptionOperatorIdentifier(random.NextEnum<IpV6AccessNetworkIdentifierOperatorIdentifierType>(), return new IpV6AccessNetworkIdentifierSubOptionOperatorIdentifier(random.NextEnum<IpV6AccessNetworkIdentifierOperatorIdentifierType>(),
......
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
...@@ -11,7 +12,7 @@ namespace PcapDotNet.Packets.Ip ...@@ -11,7 +12,7 @@ namespace PcapDotNet.Packets.Ip
/// Represents a list of options (either IPv4 options, IPv6 options or TCP options). /// Represents a list of options (either IPv4 options, IPv6 options or TCP options).
/// </summary> /// </summary>
/// <typeparam name="T">The Option type this collection contains.</typeparam> /// <typeparam name="T">The Option type this collection contains.</typeparam>
public abstract class Options<T> where T : Option, IEquatable<T> public abstract class Options<T> : IEnumerable<T> where T : Option, IEquatable<T>
{ {
/// <summary> /// <summary>
/// Returns the collection of options. /// Returns the collection of options.
...@@ -80,6 +81,16 @@ namespace PcapDotNet.Packets.Ip ...@@ -80,6 +81,16 @@ namespace PcapDotNet.Packets.Ip
OptionsCollection.SequenceGetHashCode(); OptionsCollection.SequenceGetHashCode();
} }
public IEnumerator<T> GetEnumerator()
{
return OptionsCollection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary> /// <summary>
/// A string of all the option type names. /// A string of all the option type names.
/// </summary> /// </summary>
......
using System;
using PcapDotNet.Base; using PcapDotNet.Base;
namespace PcapDotNet.Packets.IpV6 namespace PcapDotNet.Packets.IpV6
...@@ -38,6 +39,14 @@ namespace PcapDotNet.Packets.IpV6 ...@@ -38,6 +39,14 @@ namespace PcapDotNet.Packets.IpV6
{ {
LatitudeDegrees = latitudeDegrees; LatitudeDegrees = latitudeDegrees;
LongitudeDegrees = longitudeDegrees; LongitudeDegrees = longitudeDegrees;
double latitudeDegreesReal = LatitudeDegreesReal;
if (latitudeDegreesReal < -90 || latitudeDegreesReal > 90)
throw new ArgumentOutOfRangeException("latitudeDegrees", latitudeDegrees, string.Format("LatitudeDegreesReal is {0} and must be in [-90, 90] range.", latitudeDegreesReal));
double longtitudeDegreesReal = LongitudeDegreesReal;
if (longtitudeDegreesReal < -180 || longtitudeDegreesReal > 180)
throw new ArgumentOutOfRangeException("longitudeDegrees", longitudeDegrees, string.Format("LongitudeDegreesReal is {0} and must be in [-180, 180] range.", longtitudeDegreesReal));
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment