Commit 695fd69a authored by Brickner_cp's avatar Brickner_cp

IPv6

Code coverage 94.93%
parent 28d88cd9
......@@ -102,6 +102,10 @@ namespace PcapDotNet.Core.Test
protocol == IpV4Protocol.GatewayToGateway ||
protocol == IpV4Protocol.SatMon ||
protocol == IpV4Protocol.VersatileMessageTransactionProtocol ||
protocol == IpV4Protocol.ReliableDatagramProtocol ||
protocol == IpV4Protocol.InternetReliableTransactionProtocol ||
protocol == IpV4Protocol.MeritInternodalProtocol ||
protocol == IpV4Protocol.Skip ||
protocol == IpV4Protocol.RemoteVirtualDiskProtocol))
return false;
......
......@@ -114,6 +114,11 @@ namespace PcapDotNet.Packets.Test
MoreAssert.IsSmallerOrEqual(90, subOptionGeoLocation.LatitudeDegreesReal);
MoreAssert.IsBiggerOrEqual(-180, subOptionGeoLocation.LongitudeDegreesReal);
MoreAssert.IsSmallerOrEqual(180, subOptionGeoLocation.LongitudeDegreesReal);
IpV6AccessNetworkIdentifierSubOptionGeoLocation subOptionGetLocationFromReal =
IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(
subOptionGeoLocation.LatitudeDegreesReal,
subOptionGeoLocation.LongitudeDegreesReal);
Assert.AreEqual(subOptionGeoLocation, subOptionGetLocationFromReal);
break;
}
}
......@@ -263,5 +268,91 @@ namespace PcapDotNet.Packets.Test
Assert.IsNull(new IpV6ExtensionHeaders(new IpV6ExtensionHeaderEncapsulatingSecurityPayload(0, 0, DataSegment.Empty),
new IpV6ExtensionHeaderFragmentData(IpV4Protocol.Skip, 0, false, 0)));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException), AllowDerivedTypes = false)]
public void IpV6ExtensionHeaderAuthenticationNonIntegralMultipleOf4Bytes()
{
Assert.IsNull(new IpV6ExtensionHeaderAuthentication(IpV4Protocol.Skip, 0, 0, new DataSegment(new byte[6])));
}
[TestMethod]
public void IpV6ExtensionHeadersConstructors()
{
IEnumerable<IpV6ExtensionHeader> extensionHeadersEnumerable = new IpV6ExtensionHeader[0];
IList<IpV6ExtensionHeader> extensionHeadersIList = new IpV6ExtensionHeader[0];
Assert.AreEqual(new IpV6ExtensionHeaders(extensionHeadersEnumerable), new IpV6ExtensionHeaders(extensionHeadersIList));
}
[TestMethod]
public void IpV6MobilityOptionFlowSummaryConstructors()
{
IEnumerable<ushort> flowIdentifiersEnumerable = new ushort[1];
IList<ushort> flowIdentifiersIList = new ushort[1];
Assert.AreEqual(new IpV6MobilityOptionFlowSummary(flowIdentifiersEnumerable), new IpV6MobilityOptionFlowSummary(flowIdentifiersIList));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6MobilityOptionFlowSummaryNoIdentifiers()
{
Assert.IsNull(new IpV6MobilityOptionFlowSummary(new ushort[0]));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationLatitudeIntegerTooBig()
{
Assert.IsNull(new IpV6AccessNetworkIdentifierSubOptionGeoLocation((UInt24)0x7FFFFF, 0));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationLatitudeIntegerTooSmall()
{
Assert.IsNull(new IpV6AccessNetworkIdentifierSubOptionGeoLocation((UInt24)0x800000, 0));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationLongtitudeIntegerTooBig()
{
Assert.IsNull(new IpV6AccessNetworkIdentifierSubOptionGeoLocation(0, (UInt24)0x7FFFFF));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationLongtitudeIntegerTooSmall()
{
Assert.IsNull(new IpV6AccessNetworkIdentifierSubOptionGeoLocation(0, (UInt24)0x800000));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationCreateFromRealValuesLatitudeTooBig()
{
Assert.IsNull(IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(90.1, 0));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationCreateFromRealValuesLatitudeTooSmall()
{
Assert.IsNull(IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(-90.1, 0));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationCreateFromRealValuesLongtitudeTooBig()
{
Assert.IsNull(IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(0, 180.1));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException), AllowDerivedTypes = false)]
public void IpV6AccessNetworkIdentifierSubOptionGeoLocationCreateFromRealValuesLongtitudeTooSmall()
{
Assert.IsNull(IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(0, -180.1));
}
}
}
......@@ -567,7 +567,9 @@ namespace PcapDotNet.Packets.TestUtils
random.NextDataSegment(random.NextInt(0, 100)));
case IpV6AccessNetworkIdentifierSubOptionType.GeoLocation:
return new IpV6AccessNetworkIdentifierSubOptionGeoLocation((UInt24)(random.NextUInt24() & 0x9FFFFF), random.NextUInt24());
double latitude = random.NextDouble() * 180 - 90;
double longtitude = random.NextDouble() * 360 - 180;
return IpV6AccessNetworkIdentifierSubOptionGeoLocation.CreateFromRealValues(latitude, longtitude);
case IpV6AccessNetworkIdentifierSubOptionType.OperatorIdentifier:
return new IpV6AccessNetworkIdentifierSubOptionOperatorIdentifier(random.NextEnum<IpV6AccessNetworkIdentifierOperatorIdentifierType>(),
......
......@@ -8,7 +8,7 @@ using PcapDotNet.Packets.IpV4;
namespace PcapDotNet.Packets.IpV6
{
public class IpV6ExtensionHeaders : IEnumerable<IpV6ExtensionHeader>
public class IpV6ExtensionHeaders : IEnumerable<IpV6ExtensionHeader>, IEquatable<IpV6ExtensionHeaders>
{
public IpV6ExtensionHeaders(ReadOnlyCollection<IpV6ExtensionHeader> extensionHeaders)
{
......@@ -93,6 +93,16 @@ namespace PcapDotNet.Packets.IpV6
get { return _empty; }
}
public override bool Equals(object obj)
{
return Equals(obj as IpV6ExtensionHeaders);
}
public bool Equals(IpV6ExtensionHeaders other)
{
return other != null && this.SequenceEqual(other);
}
internal IpV6ExtensionHeaders(DataSegment data, IpV4Protocol firstHeader)
{
IpV4Protocol? nextHeader = firstHeader;
......@@ -111,12 +121,12 @@ namespace PcapDotNet.Packets.IpV6
IsValid = (!nextHeader.HasValue || !IpV6ExtensionHeader.IsExtensionHeader(nextHeader.Value)) && headers.All(header => header.IsValid);
}
private static readonly IpV6ExtensionHeaders _empty = new IpV6ExtensionHeaders();
public void Write(byte[] buffer, int offset)
internal void Write(byte[] buffer, int offset)
{
foreach (IpV6ExtensionHeader extensionHeader in this)
extensionHeader.Write(buffer, ref offset);
}
private static readonly IpV6ExtensionHeaders _empty = new IpV6ExtensionHeaders();
}
}
\ No newline at end of file
......@@ -49,6 +49,17 @@ namespace PcapDotNet.Packets.IpV6
throw new ArgumentOutOfRangeException("longitudeDegrees", longitudeDegrees, string.Format("LongitudeDegreesReal is {0} and must be in [-180, 180] range.", longtitudeDegreesReal));
}
public static IpV6AccessNetworkIdentifierSubOptionGeoLocation CreateFromRealValues(double latitudeDegreesReal, double longtitudeDegreesReal)
{
if (latitudeDegreesReal < -90 || latitudeDegreesReal > 90)
throw new ArgumentOutOfRangeException("latitudeDegreesReal", latitudeDegreesReal, string.Format("LatitudeDegreesReal is {0} and must be in [-90, 90] range.", latitudeDegreesReal));
if (longtitudeDegreesReal < -180 || longtitudeDegreesReal > 180)
throw new ArgumentOutOfRangeException("longtitudeDegreesReal", longtitudeDegreesReal, string.Format("LongitudeDegreesReal is {0} and must be in [-180, 180] range.", longtitudeDegreesReal));
return new IpV6AccessNetworkIdentifierSubOptionGeoLocation(ToInteger(latitudeDegreesReal), ToInteger(longtitudeDegreesReal));
}
/// <summary>
/// A 24-bit latitude degree value encoded as a two's complement, fixed point number with 9 whole bits.
/// Positive degrees correspond to the Northern Hemisphere and negative degrees correspond to the Southern Hemisphere.
......@@ -76,7 +87,7 @@ namespace PcapDotNet.Packets.IpV6
/// </summary>
public double LongitudeDegreesReal
{
get { return ToReal(LatitudeDegrees); }
get { return ToReal(LongitudeDegrees); }
}
internal override IpV6AccessNetworkIdentifierSubOption CreateInstance(DataSegment data)
......@@ -114,11 +125,14 @@ namespace PcapDotNet.Packets.IpV6
private static double ToReal(UInt24 twosComplementFixedPointWith9WholeBits)
{
bool isPositive = twosComplementFixedPointWith9WholeBits >> 23 == 1;
int integerPart = (twosComplementFixedPointWith9WholeBits & 0x7F8000) >> 15;
int fractionPart = twosComplementFixedPointWith9WholeBits & 0x007FFF;
return (isPositive ? 1 : -1) *
(integerPart + (((double)fractionPart) / (1 << 15)));
return ((double)(-(twosComplementFixedPointWith9WholeBits >> 23) * (1 << 23)) + (twosComplementFixedPointWith9WholeBits & 0x7FFFFF)) / (1 << 15);
}
private static UInt24 ToInteger(double realValue)
{
if (realValue >= 0)
return (UInt24)((int)(realValue * (1 << 15)));
return (UInt24)((~ToInteger(-realValue)) + 1);
}
private bool EqualsData(IpV6AccessNetworkIdentifierSubOptionGeoLocation other)
......
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