Commit ff603078 authored by Brickner_cp's avatar Brickner_cp

Packet validation for TCP and ARP.

Constructor check for IPv4 Basic Security Option.
Code Coverage 94.34%.
parent d9ff3069
...@@ -71,6 +71,8 @@ namespace PcapDotNet.Packets.Test ...@@ -71,6 +71,8 @@ namespace PcapDotNet.Packets.Test
arpSenderHardwareAddress, arpSenderProtocolAddress, arpSenderHardwareAddress, arpSenderProtocolAddress,
arpTargetHardwareAddress, arpTargetProtocolAddress); arpTargetHardwareAddress, arpTargetProtocolAddress);
Assert.IsTrue(packet.IsValid, "IsValid");
// Ethernet // Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength");
......
using System; using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
...@@ -115,7 +116,8 @@ namespace PcapDotNet.Packets.Test ...@@ -115,7 +116,8 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(option, option); Assert.AreEqual(option, option);
Assert.AreEqual(option.GetHashCode(), option.GetHashCode()); Assert.AreEqual(option.GetHashCode(), option.GetHashCode());
Assert.IsFalse(string.IsNullOrEmpty(option.ToString())); Assert.IsFalse(string.IsNullOrEmpty(option.ToString()));
Assert.IsFalse(option.Equals(null));
Assert.IsFalse(option.Equals(2));
} }
Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options"); Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options");
Assert.AreEqual((tcpControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.IpV4.Tcp.IsAcknowledgment, "IsAcknowledgment"); Assert.AreEqual((tcpControlBits & TcpControlBits.Acknowledgment) == TcpControlBits.Acknowledgment, packet.Ethernet.IpV4.Tcp.IsAcknowledgment, "IsAcknowledgment");
...@@ -132,5 +134,54 @@ namespace PcapDotNet.Packets.Test ...@@ -132,5 +134,54 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(tcpPayload, packet.Ethernet.IpV4.Tcp.Payload, "Payload"); Assert.AreEqual(tcpPayload, packet.Ethernet.IpV4.Tcp.Payload, "Payload");
} }
} }
[TestMethod]
public void TcpOptionSelectiveAcknowledgmentBlockTest()
{
TcpOptionSelectiveAcknowledgmentBlock block1 = new TcpOptionSelectiveAcknowledgmentBlock();
Assert.AreEqual<uint>(0, block1.LeftEdge);
Assert.AreEqual<uint>(0, block1.RightEdge);
block1 = new TcpOptionSelectiveAcknowledgmentBlock(1, 2);
Assert.AreEqual<uint>(1, block1.LeftEdge);
Assert.AreEqual<uint>(2, block1.RightEdge);
TcpOptionSelectiveAcknowledgmentBlock block2 = new TcpOptionSelectiveAcknowledgmentBlock();
Assert.AreNotEqual(block1, block2);
Assert.IsTrue(block1 != block2);
Assert.IsFalse(block1 == block2);
block2 = new TcpOptionSelectiveAcknowledgmentBlock(1, 2);
Assert.AreEqual(block1, block2);
Assert.IsFalse(block1 != block2);
Assert.IsTrue(block1 == block2);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TcpOptionMd5SignatureConstructorErrorDataLengthTest()
{
new TcpOptionMd5Signature(new byte[10]);
Assert.Fail();
}
[TestMethod]
public void TcpOptionMd5SignatureCreateInstanceErrorDataLengthTest()
{
Packet packet = PacketBuilder.EthernetIpV4Tcp(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, new IpV4Address(), new IpV4Address(), new IpV4Options(),
0, 0, 0, 0, new TcpControlBits(), 0, 0,
new TcpOptions(new TcpOptionMd5Signature(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16})), Datagram.Empty);
Assert.IsTrue(packet.IsValid);
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Options.IsValid);
byte[] buffer = packet.Buffer;
buffer[buffer.Length - packet.Ethernet.IpV4.Tcp.Length + TcpDatagram.HeaderMinimumLength + 1] = 2;
packet = new Packet(buffer, packet.Timestamp, packet.DataLink);
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Options.IsValid);
}
} }
} }
\ No newline at end of file
...@@ -44,6 +44,11 @@ namespace PcapDotNet.Packets.Arp ...@@ -44,6 +44,11 @@ namespace PcapDotNet.Packets.Arp
public const int HeaderBaseLength = 8; public const int HeaderBaseLength = 8;
public int HeaderLength
{
get { return GetHeaderLength(HardwareLength, ProtocolLength); }
}
/// <summary> /// <summary>
/// Each data link layer protocol is assigned a number used in this field. /// Each data link layer protocol is assigned a number used in this field.
/// </summary> /// </summary>
...@@ -117,6 +122,11 @@ namespace PcapDotNet.Packets.Arp ...@@ -117,6 +122,11 @@ namespace PcapDotNet.Packets.Arp
get { return ReadBytes(OffsetTargetProtocolAddress, ProtocolLength); } get { return ReadBytes(OffsetTargetProtocolAddress, ProtocolLength); }
} }
protected override bool CalculateIsValid()
{
return Length >= HeaderBaseLength && Length == HeaderLength;
}
internal ArpDatagram(byte[] buffer, int offset, int length) internal ArpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length) : base(buffer, offset, length)
{ {
......
...@@ -329,8 +329,6 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -329,8 +329,6 @@ namespace PcapDotNet.Packets.IpV4
switch (Protocol) switch (Protocol)
{ {
// case IpV4Protocol.Tcp:
// return Tcp.IsValid; //&& IsTransportChecksumCorrect;
case IpV4Protocol.Tcp: case IpV4Protocol.Tcp:
case IpV4Protocol.Udp: case IpV4Protocol.Udp:
return Transport.IsValid && (Transport.IsChecksumOptional && Transport.Checksum == 0 || return Transport.IsValid && (Transport.IsChecksumOptional && Transport.Checksum == 0 ||
......
...@@ -79,6 +79,14 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -79,6 +79,14 @@ namespace PcapDotNet.Packets.IpV4
"protectionAuthorities"); "protectionAuthorities");
} }
if (classificationLevel != IpV4OptionSecurityClassificationLevel.Confidential &&
classificationLevel != IpV4OptionSecurityClassificationLevel.Secret &&
classificationLevel != IpV4OptionSecurityClassificationLevel.TopSecret &&
classificationLevel != IpV4OptionSecurityClassificationLevel.Unclassified)
{
throw new ArgumentException("Invalid classification level " + classificationLevel);
}
_classificationLevel = classificationLevel; _classificationLevel = classificationLevel;
_protectionAuthorities = protectionAuthorities; _protectionAuthorities = protectionAuthorities;
_length = length; _length = length;
......
...@@ -230,6 +230,11 @@ namespace PcapDotNet.Packets.Transport ...@@ -230,6 +230,11 @@ namespace PcapDotNet.Packets.Transport
get { return (ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin; } get { return (ControlBits & TcpControlBits.Fin) == TcpControlBits.Fin; }
} }
protected override bool CalculateIsValid()
{
return Length >= HeaderMinimumLength && Length >= HeaderLength && Options.IsValid;
}
internal TcpDatagram(byte[] buffer, int offset, int length) internal TcpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length) : base(buffer, offset, length)
{ {
......
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