Commit a2b06200 authored by Brickner_cp's avatar Brickner_cp

Better comparison to Wireshark.

Improved Ethernet FCS.
parent cdf717f9
...@@ -2,6 +2,7 @@ using System; ...@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using PcapDotNet.Base;
using PcapDotNet.Core.Extensions; using PcapDotNet.Core.Extensions;
using PcapDotNet.Packets; using PcapDotNet.Packets;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
...@@ -160,7 +161,7 @@ namespace PcapDotNet.Core.Test ...@@ -160,7 +161,7 @@ namespace PcapDotNet.Core.Test
TestReceivePackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02); TestReceivePackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
// Wait for more packets // Wait for more packets
TestReceivePackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.071); TestReceivePackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.14);
TestReceivePackets(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.3); TestReceivePackets(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.3);
TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.051); TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.051);
...@@ -570,30 +571,30 @@ namespace PcapDotNet.Core.Test ...@@ -570,30 +571,30 @@ namespace PcapDotNet.Core.Test
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1))); communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
int numPacketsGot; int numPacketsGot;
communicator.ReceiveSomePackets(out numPacketsGot, 100, p => { }); communicator.ReceiveSomePackets(out numPacketsGot, 100, p => { });
Packet expectedPacket = _random.NextEthernetPacket(60, sourceMac, destinationMac);
Packet[] packetsToSend = new Packet[11];
packetsToSend[0] = _random.NextEthernetPacket(60, sourceMac, destinationMac);
for (int i = 0; i != 10; ++i)
packetsToSend[i + 1] = _random.NextEthernetPacket(60 * (i + 2), sourceMac, destinationMac);
List<Packet> packets = new List<Packet>(6); List<Packet> packets = new List<Packet>(6);
Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6))); Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6)));
thread.Start(); thread.Start();
communicator.SendPacket(expectedPacket);
Thread.Sleep(TimeSpan.FromSeconds(0.75)); communicator.SendPacket(packetsToSend[0]);
Thread.Sleep(TimeSpan.FromSeconds(0.7));
for (int i = 0; i != 10; ++i) for (int i = 0; i != 10; ++i)
{ {
expectedPacket = _random.NextEthernetPacket(60 * (i + 2), sourceMac, destinationMac); communicator.SendPacket(packetsToSend[i + 1]);
communicator.SendPacket(expectedPacket); Thread.Sleep(TimeSpan.FromSeconds(0.55));
Thread.Sleep(TimeSpan.FromSeconds(0.5));
} }
if (!thread.Join(TimeSpan.FromSeconds(10))) if (!thread.Join(TimeSpan.FromSeconds(10)))
thread.Abort(); thread.Abort();
Assert.AreEqual(6, packets.Count); Assert.AreEqual(6, packets.Count, packets.Select(p => (p.Timestamp-packets[0].Timestamp).TotalSeconds + "(" + p.Length + ")").SequenceToString(", "));
Packet packet; Packet packet;
for (int i = 0; i != 6; ++i) for (int i = 0; i != 6; ++i)
{
// result = communicator.ReceivePacket(out packet);
// Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
// Assert.AreEqual(60 * (i * 2 + 1), packet.Length, i.ToString());
Assert.AreEqual(60 * (i * 2 + 1), packets[i].Length, i.ToString()); Assert.AreEqual(60 * (i * 2 + 1), packets[i].Length, i.ToString());
}
PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet); PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result); Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.IsNull(packet); Assert.IsNull(packet);
......
...@@ -129,7 +129,7 @@ namespace PcapDotNet.Packets.Ethernet ...@@ -129,7 +129,7 @@ namespace PcapDotNet.Packets.Ethernet
if (payloadByEtherType == null) if (payloadByEtherType == null)
return null; return null;
if (Length - payloadByEtherType.Length >= 4 && Length >= 68) if (Length - HeaderLength - payloadByEtherType.Length >= 4 && Length >= 68)
return new Datagram(Buffer, Length - 4, 4); return new Datagram(Buffer, Length - 4, 4);
return null; return null;
} }
......
...@@ -11,21 +11,21 @@ namespace PcapDotNet.TestUtils ...@@ -11,21 +11,21 @@ namespace PcapDotNet.TestUtils
public static void IsBigger<T>(T expectedMinimum, T actual) where T : IComparable<T> public static void IsBigger<T>(T expectedMinimum, T actual) where T : IComparable<T>
{ {
if (expectedMinimum.CompareTo(actual) >= 0) if (expectedMinimum.CompareTo(actual) >= 0)
throw new AssertFailedException("Assert.IsBigger failed. Expected minimum: <" + expectedMinimum + throw new AssertFailedException("MoreAssert.IsBigger failed. Expected minimum: <" + expectedMinimum +
"> Actual: <" + actual + ">."); "> Actual: <" + actual + ">.");
} }
public static void IsSmaller<T>(T expectedMaximum, T actual) where T : IComparable<T> public static void IsSmaller<T>(T expectedMaximum, T actual) where T : IComparable<T>
{ {
if (expectedMaximum.CompareTo(actual) <= 0) if (expectedMaximum.CompareTo(actual) <= 0)
throw new AssertFailedException("Assert.IsSmaller failed. Expected maximum: <" + expectedMaximum + throw new AssertFailedException("MoreAssert.IsSmaller failed. Expected maximum: <" + expectedMaximum +
"> Actual: <" + actual + ">."); "> Actual: <" + actual + ">.");
} }
public static void IsBiggerOrEqual<T>(T expectedMinimum, T actual, string message) 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("MoreAssert.IsBiggerOrEqual failed. Expected minimum: <" + expectedMinimum +
"> Actual: <" + actual + ">. " + message); "> Actual: <" + actual + ">. " + message);
} }
...@@ -37,7 +37,7 @@ namespace PcapDotNet.TestUtils ...@@ -37,7 +37,7 @@ namespace PcapDotNet.TestUtils
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>
{ {
if (expectedMaximum.CompareTo(actual) < 0) if (expectedMaximum.CompareTo(actual) < 0)
throw new AssertFailedException("Assert.IsSmallerOrEqual failed. Expected maximum: <" + expectedMaximum + throw new AssertFailedException("MoreAssert.IsSmallerOrEqual failed. Expected maximum: <" + expectedMaximum +
"> Actual: <" + actual + ">. " + message); "> Actual: <" + actual + ">. " + message);
} }
...@@ -57,6 +57,13 @@ namespace PcapDotNet.TestUtils ...@@ -57,6 +57,13 @@ namespace PcapDotNet.TestUtils
IsInRange(expectedMinimum, expectedMaximum, actual, string.Empty); IsInRange(expectedMinimum, expectedMaximum, actual, string.Empty);
} }
public static void IsContains(string expectedContained, string actualValue, string message = "")
{
if (!actualValue.Contains(expectedContained))
throw new AssertFailedException(string.Format("MoreAssert.IsContains failed. Expected contained: <{0}> Actual: <{1}>. {2}",
expectedContained, actualValue, message));
}
public static void IsMatch(string expectedPattern, string actualValue) public static void IsMatch(string expectedPattern, string actualValue)
{ {
Assert.IsTrue(Regex.IsMatch(actualValue, expectedPattern), "Expected pattern: <" + expectedPattern + ">. Actual value: <" + actualValue + ">."); Assert.IsTrue(Regex.IsMatch(actualValue, expectedPattern), "Expected pattern: <" + expectedPattern + ">. Actual value: <" + actualValue + ">.");
......
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