Commit 1328e773 authored by Brickner_cp's avatar Brickner_cp

GRE

Overload UInt48.Parse()
Code Coverage 97.68%
parent 6f0f18cd
...@@ -56,12 +56,72 @@ namespace PcapDotNet.Base.Test ...@@ -56,12 +56,72 @@ namespace PcapDotNet.Base.Test
for (int i = 0; i != 100; ++i) for (int i = 0; i != 100; ++i)
{ {
UInt48 expected = (UInt48)random.NextLong(UInt48.MaxValue + 1); UInt48 expected = (UInt48)random.NextLong(UInt48.MaxValue + 1);
UInt48 actual = UInt48.Parse(expected.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture); UInt48 actual = UInt48.Parse(expected.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture);
Assert.AreEqual(expected, actual);
actual = UInt48.Parse(expected.ToString(), NumberStyles.Integer);
Assert.AreEqual(expected, actual);
actual = UInt48.Parse(expected.ToString(), CultureInfo.InvariantCulture);
Assert.AreEqual(expected, actual);
actual = UInt48.Parse(expected.ToString());
Assert.AreEqual(expected, actual); Assert.AreEqual(expected, actual);
} }
} }
[TestMethod]
[ExpectedException(typeof(OverflowException))]
public void ParseTooBigTest()
{
UInt48 value = UInt48.Parse(ulong.MaxValue.ToString());
Assert.IsNotNull(value);
}
[TestMethod]
[ExpectedException(typeof(OverflowException))]
public void ParseTooBigTestEvenForUInt64()
{
UInt48 value = UInt48.Parse(ulong.MaxValue + "0");
Assert.IsNotNull(value);
}
// [TestMethod]
// public void UInt48Test()
// {
// UInt48 value = (UInt48)0x010203040506;
// byte[] buffer = new byte[UInt48.SizeOf];
//
// buffer.Write(0, value, Endianity.Big);
// Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Big));
// Assert.AreEqual(0x01, buffer[0]);
// Assert.AreEqual(0x02, buffer[1]);
// Assert.AreEqual(0x03, buffer[2]);
// Assert.AreEqual(0x04, buffer[3]);
// 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]);
// Assert.AreEqual(0x05, buffer[1]);
// Assert.AreEqual(0x04, buffer[2]);
// 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);
// }
[TestMethod] [TestMethod]
public void UInt48Test() public void UInt48Test()
{ {
...@@ -71,6 +131,7 @@ namespace PcapDotNet.Base.Test ...@@ -71,6 +131,7 @@ namespace PcapDotNet.Base.Test
UInt48 value = random.NextUInt48(); UInt48 value = random.NextUInt48();
Assert.AreEqual(value, value); Assert.AreEqual(value, value);
Assert.AreNotEqual(value, "1");
Assert.IsTrue(value == value); Assert.IsTrue(value == value);
Assert.IsFalse(value != value); Assert.IsFalse(value != value);
Assert.IsNotNull(value.GetHashCode()); Assert.IsNotNull(value.GetHashCode());
......
This diff is collapsed.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
...@@ -73,7 +74,7 @@ namespace PcapDotNet.Core.Test ...@@ -73,7 +74,7 @@ namespace PcapDotNet.Core.Test
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result); Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.AreEqual<uint>(0, communicator.TotalStatistics.PacketsCaptured); Assert.AreEqual<uint>(0, communicator.TotalStatistics.PacketsCaptured);
MoreAssert.IsInRange(TimeSpan.FromSeconds(0.99), TimeSpan.FromSeconds(1.04), finishedWaiting - startWaiting); MoreAssert.IsInRange(TimeSpan.FromSeconds(0.99), TimeSpan.FromSeconds(1.075), finishedWaiting - startWaiting);
Packet sentPacket = _random.NextEthernetPacket(24, SourceMac, DestinationMac); Packet sentPacket = _random.NextEthernetPacket(24, SourceMac, DestinationMac);
...@@ -134,7 +135,7 @@ namespace PcapDotNet.Core.Test ...@@ -134,7 +135,7 @@ namespace PcapDotNet.Core.Test
TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.035); TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.035);
// Break loop // Break loop
TestReceivePackets(NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.02); TestReceivePackets(NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.027);
TestReceivePackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, NumPacketsToSend / 2, 0, 0.02); TestReceivePackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, NumPacketsToSend / 2, 0, 0.02);
} }
...@@ -148,11 +149,11 @@ namespace PcapDotNet.Core.Test ...@@ -148,11 +149,11 @@ namespace PcapDotNet.Core.Test
TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize, NumPacketsToSend, 0, 0.3); TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize, NumPacketsToSend, 0, 0.3);
// Wait for less packets // Wait for less packets
TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, NumPacketsToSend / 2, 0, 0.02); TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, NumPacketsToSend / 2, 0, 0.027);
// Wait for more packets // Wait for more packets
TestReceivePacketsEnumerable(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, NumPacketsToSend, 2, 2.02); TestReceivePacketsEnumerable(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, NumPacketsToSend, 2, 2.02);
TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, NumPacketsToSend, 2, 2.03); TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, NumPacketsToSend, 2, 2.13);
// Break loop // Break loop
TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize, 0, 0, 0.02); TestReceivePacketsEnumerable(NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize, 0, 0, 0.02);
...@@ -538,6 +539,9 @@ namespace PcapDotNet.Core.Test ...@@ -538,6 +539,9 @@ namespace PcapDotNet.Core.Test
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1))); communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
Packet expectedPacket = _random.NextEthernetPacket(60, sourceMac, destinationMac); Packet expectedPacket = _random.NextEthernetPacket(60, sourceMac, destinationMac);
List<Packet> packets = new List<Packet>(6);
Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6)));
thread.Start();
communicator.SendPacket(expectedPacket); communicator.SendPacket(expectedPacket);
Thread.Sleep(TimeSpan.FromSeconds(0.75)); Thread.Sleep(TimeSpan.FromSeconds(0.75));
for (int i = 0; i != 10; ++i) for (int i = 0; i != 10; ++i)
...@@ -547,15 +551,18 @@ namespace PcapDotNet.Core.Test ...@@ -547,15 +551,18 @@ namespace PcapDotNet.Core.Test
Thread.Sleep(TimeSpan.FromSeconds(0.5)); Thread.Sleep(TimeSpan.FromSeconds(0.5));
} }
if (!thread.Join(TimeSpan.FromSeconds(10)))
thread.Abort();
Assert.AreEqual(6, packets.Count);
Packet packet; Packet packet;
PacketCommunicatorReceiveResult result;
for (int i = 0; i != 6; ++i) for (int i = 0; i != 6; ++i)
{ {
result = communicator.ReceivePacket(out packet); // result = communicator.ReceivePacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); // Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(60 * (i * 2 + 1), packet.Length); // Assert.AreEqual(60 * (i * 2 + 1), packet.Length, i.ToString());
Assert.AreEqual(60 * (i * 2 + 1), packets[i].Length, i.ToString());
} }
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);
} }
...@@ -601,6 +608,26 @@ namespace PcapDotNet.Core.Test ...@@ -601,6 +608,26 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SetInvalidDataLink()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.DataLink = new PcapDataLink(0);
Assert.AreEqual(new PcapDataLink(0), communicator.DataLink);
}
}
[TestMethod]
public void SendZeroPacket()
{
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SendPacket(new Packet(new byte[0], DateTime.Now, DataLinkKind.Ethernet));
}
}
private static void TestGetStatistics(string sourceMac, string destinationMac, int numPacketsToSend, int numStatisticsToGather, int numStatisticsToBreakLoop, double secondsToWait, int packetSize, private static void TestGetStatistics(string sourceMac, string destinationMac, int numPacketsToSend, int numStatisticsToGather, int numStatisticsToBreakLoop, double secondsToWait, int packetSize,
PacketCommunicatorReceiveResult expectedResult, int expectedNumStatistics, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds) PacketCommunicatorReceiveResult expectedResult, int expectedNumStatistics, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{ {
...@@ -817,7 +844,9 @@ namespace PcapDotNet.Core.Test ...@@ -817,7 +844,9 @@ namespace PcapDotNet.Core.Test
Assert.AreNotEqual(null, totalStatistics); Assert.AreNotEqual(null, totalStatistics);
Assert.AreEqual(totalStatistics.GetHashCode(), totalStatistics.GetHashCode()); Assert.AreEqual(totalStatistics.GetHashCode(), totalStatistics.GetHashCode());
Assert.IsTrue(totalStatistics.Equals(totalStatistics)); Assert.IsTrue(totalStatistics.Equals(totalStatistics));
Assert.IsFalse(totalStatistics.Equals(null));
Assert.AreNotEqual(null, totalStatistics); Assert.AreNotEqual(null, totalStatistics);
Assert.AreNotEqual(totalStatistics, 2);
MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured"); MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver"); Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface"); Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface");
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets; using PcapDotNet.Packets;
......
...@@ -115,10 +115,14 @@ namespace PcapDotNet.Core.Test ...@@ -115,10 +115,14 @@ namespace PcapDotNet.Core.Test
dataLinkName = dataLink.Name; dataLinkName = dataLink.Name;
Assert.IsNotNull(dataLinkName); Assert.IsNotNull(dataLinkName);
} }
catch (ArgumentException) catch (InvalidOperationException)
{ {
return dataLink; return dataLink;
} }
catch (Exception)
{
Assert.Fail();
}
} }
Assert.Fail(); Assert.Fail();
return new PcapDataLink(); return new PcapDataLink();
......
...@@ -182,10 +182,20 @@ namespace PcapDotNet.Core.Test ...@@ -182,10 +182,20 @@ namespace PcapDotNet.Core.Test
private static void ComparePacketsToWireshark(IEnumerable<Packet> packets) private static void ComparePacketsToWireshark(IEnumerable<Packet> packets)
{ {
string pcapFilename = Path.GetTempPath() + "temp." + new Random().NextByte() + ".pcap"; string pcapFilename = Path.GetTempPath() + "temp." + new Random().NextByte() + ".pcap";
PacketDumpFile.Dump(pcapFilename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength, packets); // const bool isRetry = true;
// List<Packet> packetsList = new List<Packet>(); const bool isRetry = false;
// new OfflinePacketDevice(pcapFilename).Open().ReceivePackets(1000, packetsList.Add); if (!isRetry)
// packets = packetsList; {
PacketDumpFile.Dump(pcapFilename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength, packets);
}
else
{
const byte retryNumber = 24;
pcapFilename = Path.GetTempPath() + "temp." + retryNumber + ".pcap";
List<Packet> packetsList = new List<Packet>();
new OfflinePacketDevice(pcapFilename).Open().ReceivePackets(1000, packetsList.Add);
packets = packetsList;
}
// Create pdml file // Create pdml file
string documentFilename = pcapFilename + ".pdml"; string documentFilename = pcapFilename + ".pdml";
......
...@@ -101,40 +101,5 @@ namespace PcapDotNet.Packets.Test ...@@ -101,40 +101,5 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(0x02, buffer[2]); Assert.AreEqual(0x02, buffer[2]);
Assert.AreEqual(0x01, buffer[3]); Assert.AreEqual(0x01, buffer[3]);
} }
[TestMethod]
public void UInt48Test()
{
UInt48 value = (UInt48)0x010203040506;
byte[] buffer = new byte[UInt48.SizeOf];
buffer.Write(0, value, Endianity.Big);
Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Big));
Assert.AreEqual(0x01, buffer[0]);
Assert.AreEqual(0x02, buffer[1]);
Assert.AreEqual(0x03, buffer[2]);
Assert.AreEqual(0x04, buffer[3]);
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]);
Assert.AreEqual(0x05, buffer[1]);
Assert.AreEqual(0x04, buffer[2]);
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
...@@ -3,7 +3,7 @@ using System.Linq; ...@@ -3,7 +3,7 @@ using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.TestUtils; using PcapDotNet.Packets.TestUtils;
using PcapDotNet.TestUtils; using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.Test namespace PcapDotNet.Packets.Test
{ {
...@@ -77,5 +77,21 @@ namespace PcapDotNet.Packets.Test ...@@ -77,5 +77,21 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Payload); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Payload);
} }
} }
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void AutomaticEthernetTypeNoNextLayer()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer());
Assert.IsTrue(packet.IsValid);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void AutomaticEthernetTypeBadNextLayer()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new TcpLayer());
Assert.IsTrue(packet.IsValid);
}
} }
} }
\ No newline at end of file
...@@ -136,6 +136,7 @@ namespace PcapDotNet.Packets.Test ...@@ -136,6 +136,7 @@ namespace PcapDotNet.Packets.Test
foreach (GreSourceRouteEntry entry in actualGre.Routing) foreach (GreSourceRouteEntry entry in actualGre.Routing)
{ {
Assert.AreNotEqual(entry, 2); Assert.AreNotEqual(entry, 2);
Assert.AreEqual(entry.GetHashCode(), entry.GetHashCode());
switch (entry.AddressFamily) switch (entry.AddressFamily)
{ {
case GreSourceRouteEntryAddressFamily.AsSourceRoute: case GreSourceRouteEntryAddressFamily.AsSourceRoute:
...@@ -179,6 +180,29 @@ namespace PcapDotNet.Packets.Test ...@@ -179,6 +180,29 @@ namespace PcapDotNet.Packets.Test
} }
} }
[TestMethod]
public void GreAutomaticProtocolType()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new GreLayer(), new IpV4Layer(), new IcmpEchoLayer());
Assert.IsTrue(packet.IsValid);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void GreAutomaticProtocolTypeNoNextLayer()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new GreLayer());
Assert.IsTrue(packet.IsValid);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void GreAutomaticProtocolTypeBadNextLayer()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new GreLayer(), new PayloadLayer());
Assert.IsTrue(packet.IsValid);
}
[TestMethod] [TestMethod]
public void InvalidGreTest() public void InvalidGreTest()
{ {
...@@ -204,7 +228,7 @@ namespace PcapDotNet.Packets.Test ...@@ -204,7 +228,7 @@ namespace PcapDotNet.Packets.Test
PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer); PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer);
Packet packet = packetBuilder.Build(DateTime.Now); Packet packet = packetBuilder.Build(DateTime.Now);
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid, "IsValid");
GreDatagram gre = packet.Ethernet.IpV4.Gre; GreDatagram gre = packet.Ethernet.IpV4.Gre;
......
...@@ -138,7 +138,18 @@ namespace PcapDotNet.Packets.Gre ...@@ -138,7 +138,18 @@ namespace PcapDotNet.Packets.Gre
/// <param name="nextLayer">The layer that comes after this layer. null if this is the last layer.</param> /// <param name="nextLayer">The layer that comes after this layer. null if this is the last layer.</param>
public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer) public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer)
{ {
GreDatagram.WriteHeader(buffer, offset, RecursionControl, FutureUseBits, Version, ProtocolType, ChecksumPresent, Key, SequenceNumber, AcknowledgmentSequenceNumber, Routing, RoutingOffset, StrictSourceRoute); EthernetType protocolType = ProtocolType;
if (protocolType == EthernetType.None)
{
if (nextLayer == null)
throw new ArgumentException("Can't determine protocol type automatically from next layer because there is not next layer");
IEthernetNextLayer ethernetNextLayer = nextLayer as IEthernetNextLayer;
if (ethernetNextLayer == null)
throw new ArgumentException("Can't determine protocol type automatically from next layer (" + nextLayer.GetType() + ")");
protocolType = ethernetNextLayer.PreviousLayerEtherType;
}
GreDatagram.WriteHeader(buffer, offset, RecursionControl, FutureUseBits, Version, protocolType, ChecksumPresent, Key, SequenceNumber, AcknowledgmentSequenceNumber, Routing, RoutingOffset, StrictSourceRoute);
} }
/// <summary> /// <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