Commit 738f640a authored by Brickner_cp's avatar Brickner_cp

Code Coverage 93.60%

parent 9cd5f91f
......@@ -165,6 +165,9 @@ namespace PcapDotNet.Core.Test
{
string pcapFilename = Path.GetTempPath() + "temp.pcap";
PacketDumpFile.Dump(pcapFilename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength, packets);
// List<Packet> packetsList = new List<Packet>();
// new OfflinePacketDevice(pcapFilename).Open().ReceivePackets(1000, packetsList.Add);
// packets = packetsList;
// Create pdml file
string documentFilename = pcapFilename + ".pdml";
......@@ -429,6 +432,7 @@ namespace PcapDotNet.Core.Test
field.Show().StartsWith("Stream identifier (with option length = ") ||
field.Show().Contains("with too") ||
field.Show().Contains(" bytes says option goes past end of options") ||
field.Fields().First().Show().StartsWith("Pointer: ") && field.Fields().First().Show().EndsWith(" (points to middle of address)") ||
field.Fields().Where(value => value.Show() == "(suboption would go past end of option)").Count() != 0, field.Show());
break;
}
......
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.TestUtils;
......@@ -81,6 +82,12 @@ namespace PcapDotNet.Packets.Test
}
else
Assert.AreEqual(datagram, Datagram.Empty);
// Check Enumerable
IEnumerable enumerable = datagram;
int offset = 0;
foreach (byte b in enumerable)
Assert.AreEqual(datagram[offset++], b);
}
}
}
......
......@@ -68,11 +68,29 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x03, buffer[2]);
int offset = 0;
buffer.Write(ref offset, value, Endianity.Big);
Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Big));
Assert.AreEqual(3, offset);
offset = 0;
Assert.AreEqual(value, buffer.ReadUInt24(ref offset, Endianity.Big));
Assert.AreEqual(3, offset);
buffer.Write(0, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small));
Assert.AreEqual(0x03, buffer[0]);
Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x01, buffer[2]);
offset = 0;
buffer.Write(ref offset, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small));
Assert.AreEqual(3, offset);
offset = 0;
Assert.AreEqual(value, buffer.ReadUInt24(ref offset, Endianity.Small));
Assert.AreEqual(3, offset);
}
[TestMethod]
......@@ -111,6 +129,11 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(0x05, buffer[4]);
Assert.AreEqual(0x06, buffer[5]);
int offset = 0;
buffer.Write(ref offset, value, Endianity.Big);
Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Big));
Assert.AreEqual(6, offset);
buffer.Write(0, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Small));
Assert.AreEqual(0x06, buffer[0]);
......@@ -119,6 +142,11 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(0x03, buffer[3]);
Assert.AreEqual(0x02, buffer[4]);
Assert.AreEqual(0x01, buffer[5]);
offset = 0;
buffer.Write(ref offset, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Small));
Assert.AreEqual(6, offset);
}
}
}
\ No newline at end of file
......@@ -80,6 +80,7 @@ namespace PcapDotNet.Packets.Test
// Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength");
Assert.AreEqual(ethernetSource, packet.Ethernet.Source, "Ethernet Source");
Assert.AreNotEqual(2, packet.Ethernet.Source, "Ethernet Source");
Assert.AreEqual(ethernetDestination, packet.Ethernet.Destination, "Ethernet Destination");
Assert.AreEqual(ethernetType, packet.Ethernet.EtherType, "Ethernet Type");
......
......@@ -75,11 +75,13 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(address != new IpV4Address(address.ToString()));
Assert.AreEqual(address.GetHashCode(), new IpV4Address(address.ToString()).GetHashCode());
Assert.AreEqual(address, new IpV4Address(address.ToValue()));
Assert.AreNotEqual(address, random.NextIpV4Address());
Assert.IsFalse(address == random.NextIpV4Address());
Assert.IsTrue(address != random.NextIpV4Address());
Assert.AreNotEqual(address.GetHashCode(), random.NextIpV4Address().GetHashCode());
Assert.AreNotEqual(2, address);
}
}
......
......@@ -6,6 +6,7 @@ using PcapDotNet.Base;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.Packets.Transport;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Packets.Test
......@@ -143,6 +144,7 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.IpV4.TotalLength, "IP TotalLength");
Assert.AreEqual(ipV4Identification, packet.Ethernet.IpV4.Identification, "IP Identification");
Assert.AreEqual(ipV4Fragmentation, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.AreNotEqual(2, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.IsTrue(ipV4Fragmentation == packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.IsFalse(ipV4Fragmentation != packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.AreEqual(ipV4Fragmentation.GetHashCode(), packet.Ethernet.IpV4.Fragmentation.GetHashCode(), "IP Fragmentation");
......@@ -160,10 +162,22 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(ipV4Source, packet.Ethernet.IpV4.Source, "IP Source");
Assert.AreEqual(ipV4Destination, packet.Ethernet.IpV4.Destination, "IP Destination");
Assert.AreEqual(ipV4Options, packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreNotEqual(new IpV4Options(new IpV4OptionUnknown(0, new byte[35])), packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreEqual(ipV4Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode");
Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString());
for (int optionIndex = 0; optionIndex != ipV4Options.Count; ++optionIndex)
{
Assert.AreEqual(ipV4Options[optionIndex], packet.Ethernet.IpV4.Options[optionIndex]);
Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options[optionIndex]);
}
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(TcpDatagram));
else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(UdpDatagram));
else
Assert.IsNull(packet.Ethernet.IpV4.Transport);
Assert.AreEqual(ipV4Payload, packet.Ethernet.IpV4.Payload, "IP Payload");
}
......
......@@ -90,6 +90,11 @@ namespace PcapDotNet.Packets.Test
Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
int offset = 0;
buffer.Write(ref offset, address, Endianity.Big);
Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
Assert.AreEqual(6, offset);
offset = 0;
Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Big));
Assert.AreEqual(MacAddress.SizeOf, offset);
......@@ -97,6 +102,11 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
offset = 0;
buffer.Write(ref offset, address, Endianity.Small);
Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
Assert.AreEqual(6, offset);
offset = 0;
Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Small));
Assert.AreEqual(MacAddress.SizeOf, offset);
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
......@@ -85,6 +86,13 @@ namespace PcapDotNet.Packets.Test
Assert.IsNotNull(packet.ToString());
Assert.IsFalse(new Packet(packet.Buffer, DateTime.Now, (DataLinkKind)((int)DataLinkKind.Ethernet + 1)).IsValid);
// Check Enumerable
IEnumerable enumerable = packet;
int offset = 0;
foreach (byte b in enumerable)
Assert.AreEqual(packet[offset++], b);
}
}
......
......@@ -110,6 +110,13 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(tcpWindow, packet.Ethernet.IpV4.Tcp.Window, "Window");
Assert.AreEqual(tcpUrgentPointer, packet.Ethernet.IpV4.Tcp.UrgentPointer, "Urgent Pointer");
Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options");
foreach (TcpOption option in tcpOptions)
{
Assert.AreEqual(option, option);
Assert.AreEqual(option.GetHashCode(), option.GetHashCode());
Assert.IsFalse(string.IsNullOrEmpty(option.ToString()));
}
Assert.AreEqual(tcpOptions, packet.Ethernet.IpV4.Tcp.Options, "Options");
Assert.AreEqual((tcpFlags & TcpFlags.Ack) == TcpFlags.Ack, packet.Ethernet.IpV4.Tcp.IsAck, "IsAck");
Assert.AreEqual((tcpFlags & TcpFlags.Cwr) == TcpFlags.Cwr, packet.Ethernet.IpV4.Tcp.IsCwr, "IsCwr");
Assert.AreEqual((tcpFlags & TcpFlags.Ece) == TcpFlags.Ece, packet.Ethernet.IpV4.Tcp.IsEce, "IsEce");
......
......@@ -269,11 +269,6 @@ namespace PcapDotNet.Packets
offset += sizeof(byte);
}
public static void Write(this byte[] buffer, int offset, IEnumerable<byte> value)
{
buffer.Write(ref offset, value);
}
public static void Write(this byte[] buffer, ref int offset, IEnumerable<byte> value)
{
foreach (byte b in value)
......@@ -460,18 +455,6 @@ namespace PcapDotNet.Packets
buffer.Write(ref offset, value.ToValue(), endianity);
}
/// <summary>
/// Writes the given value to the buffer using the given endianity.
/// </summary>
/// <param name="buffer">The buffer to write the value to.</param>
/// <param name="offset">The offset in the buffer to start writing.</param>
/// <param name="value">The value to write.</param>
/// <param name="endianity">The endianity to use when converting the value to bytes.</param>
public static void Write(this byte[] buffer, int offset, IpV4OptionTimeOfDay value, Endianity endianity)
{
buffer.Write(offset, value.MillisecondsSinceMidnightUniversalTime, endianity);
}
/// <summary>
/// Writes the given value to the buffer using the given endianity and increments the offset by the number of bytes written.
/// </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