Commit fd4ee701 authored by Brickner_cp's avatar Brickner_cp

IPv6

Code Coverage 95.26%
parent a156a551
...@@ -107,6 +107,7 @@ namespace PcapDotNet.Core.Test ...@@ -107,6 +107,7 @@ namespace PcapDotNet.Core.Test
protocol == IpV4Protocol.MeritInternodalProtocol || protocol == IpV4Protocol.MeritInternodalProtocol ||
protocol == IpV4Protocol.Skip || protocol == IpV4Protocol.Skip ||
protocol == IpV4Protocol.Bna || protocol == IpV4Protocol.Bna ||
protocol == IpV4Protocol.InterDomainRoutingProtocol ||
protocol == IpV4Protocol.RemoteVirtualDiskProtocol)) protocol == IpV4Protocol.RemoteVirtualDiskProtocol))
return false; return false;
......
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
...@@ -83,7 +84,7 @@ namespace PcapDotNet.Packets.Test ...@@ -83,7 +84,7 @@ 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) foreach (IpV6ExtensionHeader extensionHeader in (IEnumerable)packet.Ethernet.IpV6.ExtensionHeaders)
{ {
IpV6ExtensionHeaderOptions extensionHeaderOptions = extensionHeader as IpV6ExtensionHeaderOptions; IpV6ExtensionHeaderOptions extensionHeaderOptions = extensionHeader as IpV6ExtensionHeaderOptions;
if (extensionHeaderOptions != null) if (extensionHeaderOptions != null)
...@@ -451,5 +452,54 @@ namespace PcapDotNet.Packets.Test ...@@ -451,5 +452,54 @@ namespace PcapDotNet.Packets.Test
{ {
Assert.IsNull(new IpV6MobilityOptionBindingIdentifier(0, IpV6BindingAcknowledgementStatus.AcceptedBut, false, 0x80)); Assert.IsNull(new IpV6MobilityOptionBindingIdentifier(0, IpV6BindingAcknowledgementStatus.AcceptedBut, false, 0x80));
} }
[TestMethod]
public void IpV6ExtensionHeaderTooShort()
{
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderDestinationOptions(IpV4Protocol.Skip, IpV6Options.Empty))
});
Assert.IsTrue(packet.IsValid);
Packet invalidPacket = new Packet(packet.Buffer.Take(packet.Length - 1).ToArray(), DateTime.Now, DataLinkKind.Ethernet);
Assert.IsFalse(invalidPacket.IsValid);
}
[TestMethod]
public void IpV6ExtensionHeaderAuthenticationTooShort()
{
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderAuthentication(IpV4Protocol.Skip, 0, 0, DataSegment.Empty))
});
Assert.IsTrue(packet.IsValid);
Packet invalidPacket = new Packet(packet.Buffer.Take(packet.Length - 1).ToArray(), DateTime.Now, DataLinkKind.Ethernet);
Assert.IsFalse(invalidPacket.IsValid);
}
[TestMethod]
public void IpV6ExtensionHeaderAuthenticationBadPayloadLength()
{
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderAuthentication(IpV4Protocol.Skip, 0, 0, new DataSegment(new byte[12])))
});
Assert.IsTrue(packet.IsValid);
++packet.Buffer[14 + 40 + 1];
Packet invalidPacket = new Packet(packet.Buffer, DateTime.Now, DataLinkKind.Ethernet);
Assert.IsFalse(invalidPacket.IsValid);
}
} }
} }
...@@ -46,9 +46,6 @@ namespace PcapDotNet.Packets.IpV6 ...@@ -46,9 +46,6 @@ namespace PcapDotNet.Packets.IpV6
{ {
buffer.Write(offset + Offset.NextHeader, (byte)NextHeader); buffer.Write(offset + Offset.NextHeader, (byte)NextHeader);
int length = Length; int length = Length;
// TODO: Remove this check.
if (length % 8 != 0)
throw new InvalidOperationException("Must be divided by 8.");
buffer.Write(offset + Offset.HeaderExtensionLength, (byte)((length / 8) - 1)); buffer.Write(offset + Offset.HeaderExtensionLength, (byte)((length / 8) - 1));
WriteData(buffer, offset + Offset.Data); WriteData(buffer, offset + Offset.Data);
offset += length; offset += length;
......
...@@ -9,6 +9,8 @@ namespace PcapDotNet.Packets.IpV6 ...@@ -9,6 +9,8 @@ namespace PcapDotNet.Packets.IpV6
{ {
public sealed class IpV6Options : Options<IpV6Option> public sealed class IpV6Options : Options<IpV6Option>
{ {
public static IpV6Options Empty { get { return _empty; } }
public IpV6Options(IList<IpV6Option> options) public IpV6Options(IList<IpV6Option> options)
: base(options, true, null) : base(options, true, null)
{ {
...@@ -94,18 +96,16 @@ namespace PcapDotNet.Packets.IpV6 ...@@ -94,18 +96,16 @@ namespace PcapDotNet.Packets.IpV6
return factory.CreateInstance(data); return factory.CreateInstance(data);
} }
private static readonly Dictionary<IpV6OptionType, IIpV6OptionComplexFactory> _factories = InitializeFactories();
private static Dictionary<IpV6OptionType, IIpV6OptionComplexFactory> InitializeFactories() private static Dictionary<IpV6OptionType, IIpV6OptionComplexFactory> InitializeFactories()
{ {
var prototypes = var prototypes =
from type in Assembly.GetExecutingAssembly().GetTypes() from type in Assembly.GetExecutingAssembly().GetTypes()
where GetRegistrationAttribute(type) != null where GetRegistrationAttribute(type) != null
select new select new
{ {
GetRegistrationAttribute(type).OptionType, GetRegistrationAttribute(type).OptionType,
Option = (IIpV6OptionComplexFactory)Activator.CreateInstance(type, true) Option = (IIpV6OptionComplexFactory)Activator.CreateInstance(type, true)
}; };
return prototypes.ToDictionary(option => option.OptionType, option => option.Option); return prototypes.ToDictionary(option => option.OptionType, option => option.Option);
} }
...@@ -118,5 +118,8 @@ namespace PcapDotNet.Packets.IpV6 ...@@ -118,5 +118,8 @@ namespace PcapDotNet.Packets.IpV6
return registraionAttributes.First(); return registraionAttributes.First();
} }
private static readonly IpV6Options _empty = new IpV6Options();
private static readonly Dictionary<IpV6OptionType, IIpV6OptionComplexFactory> _factories = InitializeFactories();
} }
} }
\ No newline at end of file
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