Commit 3fcd9057 authored by Brickner_cp's avatar Brickner_cp

Code Coverage 90.24%

parent 98d30c1c
...@@ -655,13 +655,13 @@ namespace PcapDotNet.Core.Test ...@@ -655,13 +655,13 @@ namespace PcapDotNet.Core.Test
PacketHandler handler = new PacketHandler(packetToSend, communicator, numPacketsToBreakLoop); PacketHandler handler = new PacketHandler(packetToSend, communicator, numPacketsToBreakLoop);
DateTime startWaiting = DateTime.Now; DateTime startWaiting = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToGet, PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToGet,
handler.Handle); handler.Handle);
DateTime finishedWaiting = DateTime.Now; DateTime finishedWaiting = DateTime.Now;
Assert.AreEqual(expectedResult, result); Assert.AreEqual(expectedResult, result);
Assert.AreEqual(expectedNumPackets, numPacketsGot, "NumPacketsGot. Test: " + TestDescription); Assert.AreEqual(expectedNumPackets, numPacketsGot, "NumPacketsGot. Test: " + TestDescription);
Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled, "NumPacketsHandled. Test: " + TestDescription); Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled, "NumPacketsHandled. Test: " + TestDescription);
MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds); MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds, TestDescription);
} }
} }
......
...@@ -20,11 +20,16 @@ namespace PcapDotNet.Core.Test ...@@ -20,11 +20,16 @@ namespace PcapDotNet.Core.Test
"> Actual: <" + actual + ">."); "> Actual: <" + actual + ">.");
} }
public static void IsBiggerOrEqual<T>(T expectedMinimum, T actual) where T : IComparable<T> public static void IsBiggerOrEqual<T>(T expectedMinimum, T actual, string message) where T : IComparable<T>
{ {
if (expectedMinimum.CompareTo(actual) > 0) if (expectedMinimum.CompareTo(actual) > 0)
throw new AssertFailedException("Assert.IsBiggerOrEqual failed. Expected minimum: <" + expectedMinimum + throw new AssertFailedException("Assert.IsBiggerOrEqual failed. Expected minimum: <" + expectedMinimum +
"> Actual: <" + actual + ">."); "> Actual: <" + actual + ">. " + message);
}
public static void IsBiggerOrEqual<T>(T expectedMaximum, T actual) where T : IComparable<T>
{
IsBiggerOrEqual(expectedMaximum, actual, string.Empty);
} }
public static void IsSmallerOrEqual<T>(T expectedMaximum, T actual, string message) where T : IComparable<T> public static void IsSmallerOrEqual<T>(T expectedMaximum, T actual, string message) where T : IComparable<T>
...@@ -39,10 +44,15 @@ namespace PcapDotNet.Core.Test ...@@ -39,10 +44,15 @@ namespace PcapDotNet.Core.Test
IsSmallerOrEqual(expectedMaximum, actual, string.Empty); IsSmallerOrEqual(expectedMaximum, actual, string.Empty);
} }
public static void IsInRange<T>(T expectedMinimum, T expectedMaximum, T actual, string message) where T : IComparable<T>
{
IsBiggerOrEqual(expectedMinimum, actual, message);
IsSmallerOrEqual(expectedMaximum, actual, message);
}
public static void IsInRange<T>(T expectedMinimum, T expectedMaximum, T actual) where T : IComparable<T> public static void IsInRange<T>(T expectedMinimum, T expectedMaximum, T actual) where T : IComparable<T>
{ {
IsBiggerOrEqual(expectedMinimum, actual); IsInRange(expectedMinimum, expectedMaximum, actual, string.Empty);
IsSmallerOrEqual(expectedMaximum, actual);
} }
public static void IsMatch(string expectedPattern, string actualValue) public static void IsMatch(string expectedPattern, string actualValue)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
using namespace System; using namespace System;
using namespace System::Text; using namespace System::Text;
using namespace System::Net;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
using namespace PcapDotNet::Packets; using namespace PcapDotNet::Packets;
...@@ -26,5 +27,5 @@ IpV4SocketAddress::IpV4SocketAddress(sockaddr *address) ...@@ -26,5 +27,5 @@ IpV4SocketAddress::IpV4SocketAddress(sockaddr *address)
: SocketAddress(address->sa_family) : SocketAddress(address->sa_family)
{ {
sockaddr_in* ipV4Address = (struct sockaddr_in *)address; sockaddr_in* ipV4Address = (struct sockaddr_in *)address;
_address = IpV4Address::FromReversedEndianity(ipV4Address->sin_addr.S_un.S_addr); _address = IpV4Address((unsigned int)IPAddress::HostToNetworkOrder((int)(ipV4Address->sin_addr.S_un.S_addr)));
} }
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for DataLinkTests
/// </summary>
[TestClass]
public class DataLinkTests
{
public DataLinkTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void DataLinkTest()
{
Assert.AreEqual(DataLink.Ethernet, DataLink.Ethernet);
Assert.AreEqual(DataLinkKind.Ethernet.ToString(), DataLink.Ethernet.ToString());
Assert.AreEqual(DataLink.Ethernet.GetHashCode(), DataLink.Ethernet.GetHashCode());
Assert.IsTrue(DataLink.Ethernet == DataLink.Ethernet);
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for DatagramTests
/// </summary>
[TestClass]
public class DatagramTests
{
public DatagramTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void RandomDatagramTest()
{
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
Datagram datagram = random.NextDatagram(random.Next(1024));
Assert.AreEqual(datagram, new Datagram(new List<byte>(datagram).ToArray()));
Assert.AreEqual(datagram.GetHashCode(), new Datagram(new List<byte>(datagram).ToArray()).GetHashCode());
Assert.AreNotEqual(datagram, random.NextDatagram(random.Next(10 * 1024)));
Assert.AreNotEqual(datagram.GetHashCode(), random.NextDatagram(random.Next(10 * 1024)).GetHashCode());
if (datagram.Length != 0)
{
Assert.AreNotEqual(datagram, random.NextDatagram(datagram.Length));
Assert.AreNotEqual(datagram.GetHashCode(), random.NextDatagram(datagram.Length).GetHashCode());
}
}
}
}
}
\ No newline at end of file
...@@ -57,43 +57,43 @@ namespace PcapDotNet.Packets.Test ...@@ -57,43 +57,43 @@ namespace PcapDotNet.Packets.Test
#endregion #endregion
[TestMethod] [TestMethod]
public void UIntTest() public void UInt24Test()
{ {
const uint value = 0x01020304; UInt24 value = (UInt24)0x010203;
byte[] buffer = new byte[sizeof(uint)]; byte[] buffer = new byte[UInt24.SizeOf];
buffer.Write(0, value, Endianity.Big); buffer.Write(0, value, Endianity.Big);
Assert.AreEqual(value, buffer.ReadUInt(0, Endianity.Big)); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Big));
Assert.AreEqual(0x01, buffer[0]); Assert.AreEqual(0x01, buffer[0]);
Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x03, buffer[2]); Assert.AreEqual(0x03, buffer[2]);
Assert.AreEqual(0x04, buffer[3]);
buffer.Write(0, value, Endianity.Small); buffer.Write(0, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt(0, Endianity.Small)); Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small));
Assert.AreEqual(0x04, buffer[0]); Assert.AreEqual(0x03, buffer[0]);
Assert.AreEqual(0x03, buffer[1]); Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x02, buffer[2]); Assert.AreEqual(0x01, buffer[2]);
Assert.AreEqual(0x01, buffer[3]);
} }
[TestMethod] [TestMethod]
public void UInt24Test() public void UIntTest()
{ {
UInt24 value = (UInt24)0x010203; const uint value = 0x01020304;
byte[] buffer = new byte[UInt24.SizeOf]; byte[] buffer = new byte[sizeof(uint)];
buffer.Write(0, value, Endianity.Big); buffer.Write(0, value, Endianity.Big);
Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Big)); Assert.AreEqual(value, buffer.ReadUInt(0, Endianity.Big));
Assert.AreEqual(0x01, buffer[0]); Assert.AreEqual(0x01, buffer[0]);
Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x03, buffer[2]); Assert.AreEqual(0x03, buffer[2]);
Assert.AreEqual(0x04, buffer[3]);
buffer.Write(0, value, Endianity.Small); buffer.Write(0, value, Endianity.Small);
Assert.AreEqual(value, buffer.ReadUInt24(0, Endianity.Small)); Assert.AreEqual(value, buffer.ReadUInt(0, Endianity.Small));
Assert.AreEqual(0x03, buffer[0]); Assert.AreEqual(0x04, buffer[0]);
Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x03, buffer[1]);
Assert.AreEqual(0x01, buffer[2]); Assert.AreEqual(0x02, buffer[2]);
Assert.AreEqual(0x01, buffer[3]);
} }
[TestMethod] [TestMethod]
......
...@@ -76,8 +76,6 @@ namespace PcapDotNet.Packets.Test ...@@ -76,8 +76,6 @@ namespace PcapDotNet.Packets.Test
ethernetPayload); ethernetPayload);
Assert.IsTrue(packet.IsValid);
// Ethernet // Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength");
Assert.AreEqual(ethernetSource, packet.Ethernet.Source, "Ethernet Source"); Assert.AreEqual(ethernetSource, packet.Ethernet.Source, "Ethernet Source");
......
...@@ -3,6 +3,7 @@ using System.Text; ...@@ -3,6 +3,7 @@ using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test namespace PcapDotNet.Packets.Test
{ {
...@@ -60,9 +61,32 @@ namespace PcapDotNet.Packets.Test ...@@ -60,9 +61,32 @@ namespace PcapDotNet.Packets.Test
#endregion #endregion
[TestMethod] [TestMethod]
public void IpV4AddressTest() public void IpV4AddressRandomTest()
{
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
IpV4Address address = random.NextIpV4Address();
Assert.AreEqual(address, new IpV4Address(address.ToString()));
Assert.IsTrue(address == new IpV4Address(address.ToString()));
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());
}
}
[TestMethod]
public void IpV4AddressOrderTest()
{ {
Assert.AreEqual("0.0.0.0", new IpV4Address(0).ToString()); Assert.AreEqual("0.0.0.0", new IpV4Address(0).ToString());
Assert.AreEqual("0.0.0.0", IpV4Address.Zero.ToString());
Assert.AreEqual("0.0.0.1", new IpV4Address(1).ToString()); Assert.AreEqual("0.0.0.1", new IpV4Address(1).ToString());
Assert.AreEqual("0.0.0.255", new IpV4Address(255).ToString()); Assert.AreEqual("0.0.0.255", new IpV4Address(255).ToString());
Assert.AreEqual("0.0.1.0", new IpV4Address(256).ToString()); Assert.AreEqual("0.0.1.0", new IpV4Address(256).ToString());
...@@ -73,5 +97,26 @@ namespace PcapDotNet.Packets.Test ...@@ -73,5 +97,26 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual("255.0.0.0", new IpV4Address((uint)255 * 256 * 256 * 256).ToString()); Assert.AreEqual("255.0.0.0", new IpV4Address((uint)255 * 256 * 256 * 256).ToString());
Assert.AreEqual("255.254.253.252", new IpV4Address((uint)255 * 256 * 256 * 256 + 254 * 256 * 256 + 253 * 256 + 252).ToString()); Assert.AreEqual("255.254.253.252", new IpV4Address((uint)255 * 256 * 256 * 256 + 254 * 256 * 256 + 253 * 256 + 252).ToString());
} }
[TestMethod]
public void IpV4AddressWithBufferTest()
{
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
IpV4Address address = random.NextIpV4Address();
byte[] buffer = new byte[IpV4Address.SizeOf];
buffer.Write(0, address, Endianity.Big);
Assert.AreEqual(address, buffer.ReadIpV4Address(0, Endianity.Big));
Assert.AreNotEqual(address, buffer.ReadIpV4Address(0, Endianity.Small));
buffer.Write(0, address, Endianity.Small);
Assert.AreEqual(address, buffer.ReadIpV4Address(0, Endianity.Small));
Assert.AreNotEqual(address, buffer.ReadIpV4Address(0, Endianity.Big));
}
}
} }
} }
...@@ -140,23 +140,46 @@ namespace PcapDotNet.Packets.Test ...@@ -140,23 +140,46 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.IpV4.TotalLength, "IP TotalLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.IpV4.TotalLength, "IP TotalLength");
Assert.AreEqual(ipV4Identification, packet.Ethernet.IpV4.Identification, "IP Identification"); Assert.AreEqual(ipV4Identification, packet.Ethernet.IpV4.Identification, "IP Identification");
Assert.AreEqual(ipV4Fragmentation, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation"); Assert.AreEqual(ipV4Fragmentation, 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");
Assert.AreEqual(ipV4Fragmentation.Options, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation"); Assert.AreEqual(ipV4Fragmentation.Options, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation");
Assert.AreEqual(ipV4Fragmentation.Offset, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation"); Assert.AreEqual(ipV4Fragmentation.Offset, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation");
if (ipV4Fragmentation.Equals(IpV4Fragmentation.None))
{
Assert.AreEqual(IpV4FragmentationOptions.None, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation");
Assert.AreEqual(0, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation");
}
Assert.AreEqual(ipV4Ttl, packet.Ethernet.IpV4.Ttl, "IP Ttl"); Assert.AreEqual(ipV4Ttl, packet.Ethernet.IpV4.Ttl, "IP Ttl");
Assert.AreEqual(ipV4Protocol, packet.Ethernet.IpV4.Protocol, "IP Protocol"); Assert.AreEqual(ipV4Protocol, packet.Ethernet.IpV4.Protocol, "IP Protocol");
// Assert.AreEqual(0x9010, packet.Ethernet.IpV4.HeaderChecksum, "IP HeaderChecksum"); // Assert.AreEqual(0x9010, packet.Ethernet.IpV4.HeaderChecksum, "IP HeaderChecksum");
Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect"); Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect");
Assert.AreEqual(ipV4Source, packet.Ethernet.IpV4.Source, "IP Source"); Assert.AreEqual(ipV4Source, packet.Ethernet.IpV4.Source, "IP Source");
Assert.AreEqual(ipV4Destination, packet.Ethernet.IpV4.Destination, "IP Destination"); Assert.AreEqual(ipV4Destination, packet.Ethernet.IpV4.Destination, "IP Destination");
if (!ipV4Options.Equals(packet.Ethernet.IpV4.Options)) Assert.AreEqual(ipV4Options, packet.Ethernet.IpV4.Options, "IP Options");
{ Assert.AreEqual(ipV4Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode");
Assert.AreEqual(ipV4Options, packet.Ethernet.IpV4.Options, "IP Options"); 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.AreEqual(ipV4Payload, packet.Ethernet.IpV4.Payload, "IP Payload"); Assert.AreEqual(ipV4Payload, packet.Ethernet.IpV4.Payload, "IP Payload");
} }
} }
[TestMethod]
public void IpV4OptionsTest()
{
IpV4Options actual = new IpV4Options(new IpV4OptionSecurity(IpV4OptionSecurityLevel.Unclassified, 12345, 54321, (UInt24)123456));
IpV4Options expected = new IpV4Options(new IpV4OptionSecurity(IpV4OptionSecurityLevel.Unclassified, 12345, 54321, (UInt24)123456),
IpV4Option.End);
Assert.AreEqual(expected, actual);
Assert.IsTrue(expected.IsValid);
Assert.IsTrue(actual.IsValid);
}
private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind) private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind)
{ {
return HexToPacket(hexString, DateTime.MinValue, dataLinkKind); return HexToPacket(hexString, DateTime.MinValue, dataLinkKind);
......
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for MacAddressTests
/// </summary>
[TestClass]
public class MacAddressTests
{
public MacAddressTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void MacAddressTest()
{
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
MacAddress macAddress = random.NextMacAddress();
Assert.IsNotNull(macAddress.ToString());
Assert.AreEqual(macAddress, new MacAddress(macAddress.ToString()));
Assert.AreNotEqual(macAddress, random.NextMacAddress());
Assert.IsTrue(macAddress == new MacAddress(macAddress.ToString()));
Assert.AreEqual(macAddress.GetHashCode(), new MacAddress(macAddress.ToString()).GetHashCode());
Assert.IsTrue(macAddress != random.NextMacAddress());
Assert.AreNotEqual(macAddress.GetHashCode(), random.NextMacAddress().GetHashCode());
}
}
[TestMethod]
public void MacAddressWithBufferTest()
{
Random random = new Random();
MacAddress address = random.NextMacAddress();
byte[] buffer = new byte[MacAddress.SizeOf];
buffer.Write(0, address, Endianity.Big);
Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
int offset = 0;
Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Big));
Assert.AreEqual(MacAddress.SizeOf, offset);
buffer.Write(0, address, Endianity.Small);
Assert.AreEqual(address, buffer.ReadMacAddress(0, Endianity.Small));
Assert.AreNotEqual(address, buffer.ReadMacAddress(0, Endianity.Big));
offset = 0;
Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Small));
Assert.AreEqual(MacAddress.SizeOf, offset);
}
}
}
\ No newline at end of file
using System; using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test namespace PcapDotNet.Packets.Test
{ {
...@@ -60,6 +61,28 @@ namespace PcapDotNet.Packets.Test ...@@ -60,6 +61,28 @@ namespace PcapDotNet.Packets.Test
public void RandomPacketTest() public void RandomPacketTest()
{ {
Random random = new Random(); Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
Packet packet = random.NextPacket(random.Next(10 * 1024));
// Check Equals
Assert.AreEqual(packet, new Packet(packet.Buffer, packet.Timestamp.AddHours(1), packet.DataLink));
Assert.AreNotEqual(packet, random.NextPacket(random.Next(10 * 1024)));
if (packet.Length != 0)
Assert.AreNotEqual(packet, random.NextPacket(packet.Length));
// Check GetHashCode
Assert.AreEqual(packet.GetHashCode(), new Packet(packet.Buffer, packet.Timestamp.AddHours(1), packet.DataLink).GetHashCode());
Assert.AreNotEqual(packet.GetHashCode(), random.NextPacket(random.Next(10 * 1024)).GetHashCode());
if (packet.Length != 0)
Assert.AreNotEqual(packet.GetHashCode(), random.NextPacket(packet.Length).GetHashCode());
// Check ToString
Assert.IsNotNull(packet.ToString());
Assert.IsFalse(new Packet(packet.Buffer, DateTime.Now, (DataLinkKind)((int)DataLinkKind.Ethernet + 1)).IsValid);
}
} }
} }
} }
\ No newline at end of file
...@@ -42,9 +42,12 @@ ...@@ -42,9 +42,12 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DatagramTests.cs" />
<Compile Include="DataLinkTests.cs" />
<Compile Include="EndianitiyTests.cs" /> <Compile Include="EndianitiyTests.cs" />
<Compile Include="EthernetTests.cs" /> <Compile Include="EthernetTests.cs" />
<Compile Include="IpV4Tests.cs" /> <Compile Include="IpV4Tests.cs" />
<Compile Include="MacAddressTests.cs" />
<Compile Include="PacketTests.cs" /> <Compile Include="PacketTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" /> <Compile Include="IpV4AddressTests.cs" />
......
...@@ -16,6 +16,18 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -16,6 +16,18 @@ namespace PcapDotNet.Packets.TestUtils
return new Datagram(buffer); return new Datagram(buffer);
} }
public static DataLinkKind NextDataLinkKind(this Random random)
{
return random.NextEnum<DataLinkKind>();
}
public static Packet NextPacket(this Random random, int length)
{
byte[] buffer = new byte[length];
random.NextBytes(buffer);
return new Packet(buffer, DateTime.Now, random.NextDataLinkKind());
}
public static MacAddress NextMacAddress(this Random random) public static MacAddress NextMacAddress(this Random random)
{ {
return new MacAddress(random.NextUInt48()); return new MacAddress(random.NextUInt48());
......
...@@ -2,6 +2,11 @@ namespace PcapDotNet.Packets ...@@ -2,6 +2,11 @@ namespace PcapDotNet.Packets
{ {
public struct DataLink : IDataLink public struct DataLink : IDataLink
{ {
public static DataLink Ethernet
{
get { return _ethernet; }
}
public DataLink(DataLinkKind kind) public DataLink(DataLinkKind kind)
{ {
_kind = kind; _kind = kind;
...@@ -38,6 +43,12 @@ namespace PcapDotNet.Packets ...@@ -38,6 +43,12 @@ namespace PcapDotNet.Packets
return _kind.GetHashCode(); return _kind.GetHashCode();
} }
public override string ToString()
{
return Kind.ToString();
}
private static readonly DataLink _ethernet = new DataLink(DataLinkKind.Ethernet);
private readonly DataLinkKind _kind; private readonly DataLinkKind _kind;
} }
} }
\ No newline at end of file
...@@ -6,6 +6,8 @@ namespace PcapDotNet.Packets ...@@ -6,6 +6,8 @@ namespace PcapDotNet.Packets
{ {
public struct MacAddress public struct MacAddress
{ {
public const int SizeOf = UInt48.SizeOf;
public MacAddress(UInt48 value) public MacAddress(UInt48 value)
{ {
_value = value; _value = value;
...@@ -67,32 +69,6 @@ namespace PcapDotNet.Packets ...@@ -67,32 +69,6 @@ namespace PcapDotNet.Packets
(byte)(_value)); (byte)(_value));
} }
/* internal MacAddress(byte[] buffer, int offset)
{
_b1 = buffer[offset++];
_b2 = buffer[offset++];
_b3 = buffer[offset++];
_b4 = buffer[offset++];
_b5 = buffer[offset++];
_b6 = buffer[offset];
}
internal void Write(byte[] buffer, int offset)
{
buffer[offset++] = _b1;
buffer[offset++] = _b2;
buffer[offset++] = _b3;
buffer[offset++] = _b4;
buffer[offset++] = _b5;
buffer[offset] = _b6;
}
*/
private readonly UInt48 _value; private readonly UInt48 _value;
// private readonly byte _b1;
// private readonly byte _b2;
// private readonly byte _b3;
// private readonly byte _b4;
// private readonly byte _b5;
// private readonly byte _b6;
} }
} }
\ No newline at end of file
...@@ -5,6 +5,8 @@ namespace PcapDotNet.Packets ...@@ -5,6 +5,8 @@ namespace PcapDotNet.Packets
{ {
public struct IpV4Address public struct IpV4Address
{ {
public const int SizeOf = sizeof(uint);
public IpV4Address(uint value) public IpV4Address(uint value)
{ {
_value = value; _value = value;
...@@ -29,15 +31,6 @@ namespace PcapDotNet.Packets ...@@ -29,15 +31,6 @@ namespace PcapDotNet.Packets
return _value; return _value;
} }
public static IpV4Address FromReversedEndianity(uint value)
{
uint result = value >> 24;
result += (value >> 16) % 256 << 8;
result += (value >> 8) % 256 << 16;
result += value % 256 << 24;
return new IpV4Address(result);
}
public bool Equals(IpV4Address other) public bool Equals(IpV4Address other)
{ {
return ToValue() == other.ToValue(); return ToValue() == other.ToValue();
......
...@@ -113,6 +113,21 @@ namespace PcapDotNet.Packets ...@@ -113,6 +113,21 @@ namespace PcapDotNet.Packets
get { return Tcp; } get { return Tcp; }
} }
public override bool CalculateIsValid()
{
if (Length < HeaderMinimumLength || Length < HeaderLength)
return false;
switch (Protocol)
{
case IpV4Protocol.Tcp:
return Tcp.IsValid;
default:
// Todo check the protocl further
return true;
}
}
public Datagram Tcp public Datagram Tcp
{ {
get get
......
...@@ -75,6 +75,11 @@ namespace PcapDotNet.Packets ...@@ -75,6 +75,11 @@ namespace PcapDotNet.Packets
return hashCode; return hashCode;
} }
public override string ToString()
{
return typeof(Packet).Name + " <" + DataLink + ", " + Length + ">";
}
public bool IsValid public bool IsValid
{ {
get get
......
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