Commit 09526168 authored by Brickner_cp's avatar Brickner_cp

Code Coverage 94.48%

parent 7ea0becf
...@@ -18,23 +18,11 @@ namespace PcapDotNet.Base.Test ...@@ -18,23 +18,11 @@ namespace PcapDotNet.Base.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -17,23 +17,11 @@ namespace PcapDotNet.Base.Test ...@@ -17,23 +17,11 @@ namespace PcapDotNet.Base.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -94,23 +82,11 @@ namespace PcapDotNet.Base.Test ...@@ -94,23 +82,11 @@ namespace PcapDotNet.Base.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -18,23 +18,11 @@ namespace PcapDotNet.Base.Test ...@@ -18,23 +18,11 @@ namespace PcapDotNet.Base.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -21,23 +21,11 @@ namespace PcapDotNet.Base.Test ...@@ -21,23 +21,11 @@ namespace PcapDotNet.Base.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -18,23 +18,11 @@ namespace PcapDotNet.Core.Test ...@@ -18,23 +18,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext { get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -27,23 +27,11 @@ namespace PcapDotNet.Core.Test ...@@ -27,23 +27,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -85,7 +73,7 @@ namespace PcapDotNet.Core.Test ...@@ -85,7 +73,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(1), TimeSpan.FromSeconds(1.04), finishedWaiting - startWaiting); MoreAssert.IsInRange(TimeSpan.FromSeconds(0.99), TimeSpan.FromSeconds(1.04), finishedWaiting - startWaiting);
Packet sentPacket = _random.NextEthernetPacket(24, SourceMac, DestinationMac); Packet sentPacket = _random.NextEthernetPacket(24, SourceMac, DestinationMac);
...@@ -135,15 +123,15 @@ namespace PcapDotNet.Core.Test ...@@ -135,15 +123,15 @@ namespace PcapDotNet.Core.Test
const int PacketSize = 100; const int PacketSize = 100;
// Normal // Normal
TestReceivePackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02); TestReceivePackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.021);
// Wait for less packets // Wait for less packets
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.03); TestReceivePackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.03);
TestReceivePackets(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.1); 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.02); TestReceivePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.03);
// 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.02);
......
...@@ -22,23 +22,11 @@ namespace PcapDotNet.Core.Test ...@@ -22,23 +22,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -19,23 +19,11 @@ namespace PcapDotNet.Core.Test ...@@ -19,23 +19,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -20,23 +20,11 @@ namespace PcapDotNet.Core.Test ...@@ -20,23 +20,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -17,23 +17,11 @@ namespace PcapDotNet.Core.Test ...@@ -17,23 +17,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -20,23 +20,11 @@ namespace PcapDotNet.Core.Test ...@@ -20,23 +20,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -38,23 +38,11 @@ namespace PcapDotNet.Core.Test ...@@ -38,23 +38,11 @@ namespace PcapDotNet.Core.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -825,7 +813,8 @@ namespace PcapDotNet.Core.Test ...@@ -825,7 +813,8 @@ namespace PcapDotNet.Core.Test
break; break;
default: default:
if (!field.Name().StartsWith("lt2p.")) if (!field.Name().StartsWith("lt2p.") &&
!field.Name().StartsWith("pweth."))
throw new InvalidOperationException("Invalid icmp field " + field.Name()); throw new InvalidOperationException("Invalid icmp field " + field.Name());
break; break;
} }
...@@ -900,7 +889,7 @@ namespace PcapDotNet.Core.Test ...@@ -900,7 +889,7 @@ namespace PcapDotNet.Core.Test
break; break;
case "udp.checksum_coverage": case "udp.checksum_coverage":
field.AssertShowDecimal(udpDatagram.Length); field.AssertShowDecimal(udpDatagram.TotalLength);
break; break;
default: default:
......
...@@ -15,23 +15,11 @@ namespace PcapDotNet.Packets.Test ...@@ -15,23 +15,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -59,6 +47,7 @@ namespace PcapDotNet.Packets.Test ...@@ -59,6 +47,7 @@ namespace PcapDotNet.Packets.Test
public void DataLinkTest() public void DataLinkTest()
{ {
Assert.AreEqual(DataLink.Ethernet, DataLink.Ethernet); Assert.AreEqual(DataLink.Ethernet, DataLink.Ethernet);
Assert.AreNotEqual(DataLink.Ethernet, 2);
Assert.AreEqual(DataLinkKind.Ethernet.ToString(), DataLink.Ethernet.ToString()); Assert.AreEqual(DataLinkKind.Ethernet.ToString(), DataLink.Ethernet.ToString());
Assert.AreEqual(DataLink.Ethernet.GetHashCode(), DataLink.Ethernet.GetHashCode()); Assert.AreEqual(DataLink.Ethernet.GetHashCode(), DataLink.Ethernet.GetHashCode());
Assert.IsTrue(DataLink.Ethernet == DataLink.Ethernet); Assert.IsTrue(DataLink.Ethernet == DataLink.Ethernet);
......
...@@ -2,6 +2,7 @@ using System; ...@@ -2,6 +2,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.TestUtils; using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test namespace PcapDotNet.Packets.Test
...@@ -19,23 +20,11 @@ namespace PcapDotNet.Packets.Test ...@@ -19,23 +20,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -90,5 +79,27 @@ namespace PcapDotNet.Packets.Test ...@@ -90,5 +79,27 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(datagram[offset++], b); Assert.AreEqual(datagram[offset++], b);
} }
} }
[TestMethod]
public void DatagramExtractLayerTest()
{
PayloadLayer payloadLayer = new PayloadLayer
{
Data = new Datagram(new byte[] {100, 101, 102})
};
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer
{
EtherType = EthernetType.IpV4
}, payloadLayer);
Assert.AreEqual(payloadLayer, packet.Ethernet.Payload.ExtractLayer());
}
[TestMethod]
public void DatagramCalculateIsValidTest()
{
Datagram data = new Datagram(new byte[]{1,2,3});
Assert.IsTrue(data.IsValid);
}
} }
} }
\ No newline at end of file
...@@ -16,23 +16,11 @@ namespace PcapDotNet.Packets.Test ...@@ -16,23 +16,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -19,23 +19,11 @@ namespace PcapDotNet.Packets.Test ...@@ -19,23 +19,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -25,23 +25,11 @@ namespace PcapDotNet.Packets.Test ...@@ -25,23 +25,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
/// Gets or sets the test context which provides /// Gets or sets the test context which provides
/// information about and functionality for the current test run. /// information about and functionality for the current test run.
/// </summary> /// </summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -25,23 +25,11 @@ namespace PcapDotNet.Packets.Test ...@@ -25,23 +25,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -100,6 +88,14 @@ namespace PcapDotNet.Packets.Test ...@@ -100,6 +88,14 @@ namespace PcapDotNet.Packets.Test
// IGMP // IGMP
Assert.IsTrue(packet.Ethernet.IpV4.Igmp.IsChecksumCorrect); Assert.IsTrue(packet.Ethernet.IpV4.Igmp.IsChecksumCorrect);
Assert.AreEqual(igmpLayer, packet.Ethernet.IpV4.Igmp.ExtractLayer(), "IGMP Layer"); Assert.AreEqual(igmpLayer, packet.Ethernet.IpV4.Igmp.ExtractLayer(), "IGMP Layer");
Assert.AreNotEqual(igmpLayer, null);
Assert.AreNotEqual(igmpLayer, random.NextPayloadLayer(igmpLayer.Length));
if (packet.Ethernet.IpV4.Igmp.QueryVersion != IgmpQueryVersion.Version3)
MoreAssert.IsSmallerOrEqual(IgmpDatagram.MaxMaxResponseTime, packet.Ethernet.IpV4.Igmp.MaxResponseTime);
if (packet.Ethernet.IpV4.Igmp.MessageType != IgmpMessageType.MembershipQuery)
Assert.AreEqual(IgmpQueryVersion.None, packet.Ethernet.IpV4.Igmp.QueryVersion);
foreach (IgmpGroupRecordDatagram groupRecord in packet.Ethernet.IpV4.Igmp.GroupRecords)
Assert.IsNotNull(groupRecord.ToString());
} }
} }
...@@ -357,5 +353,29 @@ namespace PcapDotNet.Packets.Test ...@@ -357,5 +353,29 @@ namespace PcapDotNet.Packets.Test
Assert.AreNotEqual(record, new IgmpGroupRecord(record.RecordType, record.MulticastAddress, new List<IpV4Address>(new[] {new IpV4Address("2.3.4.5")}), record.AuxiliaryData)); Assert.AreNotEqual(record, new IgmpGroupRecord(record.RecordType, record.MulticastAddress, new List<IpV4Address>(new[] {new IpV4Address("2.3.4.5")}), record.AuxiliaryData));
Assert.AreNotEqual(record, new IgmpGroupRecord(record.RecordType, record.MulticastAddress, record.SourceAddresses, new Datagram(new byte[12]))); Assert.AreNotEqual(record, new IgmpGroupRecord(record.RecordType, record.MulticastAddress, record.SourceAddresses, new Datagram(new byte[12])));
} }
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void IgmpExtractLayerBadMessageTypeTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new IgmpReportVersion1Layer());
Assert.IsNotNull(packet.Ethernet.IpV4.Igmp.ExtractLayer());
byte[] buffer = (byte[])packet.Buffer.Clone();
buffer[packet.Length - packet.Ethernet.IpV4.Igmp.Length] = 0xFF;
packet = new Packet(buffer, DateTime.Now, packet.DataLink);
Assert.IsFalse(packet.IsValid);
Assert.IsNotNull(packet.Ethernet.IpV4.Igmp.ExtractLayer());
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpTooBigQueryRobustnessVariableTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new IgmpQueryVersion3Layer
{
QueryRobustnessVariable = 255
});
Assert.IsNull(packet);
}
} }
} }
\ No newline at end of file
...@@ -24,23 +24,11 @@ namespace PcapDotNet.Packets.Test ...@@ -24,23 +24,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -135,6 +123,9 @@ namespace PcapDotNet.Packets.Test ...@@ -135,6 +123,9 @@ namespace PcapDotNet.Packets.Test
// IpV4 // IpV4
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
Assert.AreNotEqual(ipV4Layer, null);
Assert.AreNotEqual(ipV4Layer, new PayloadLayer());
Assert.IsNotNull(ipV4Layer.ToString());
Assert.AreEqual(IpV4Datagram.HeaderMinimumLength + ipV4Layer.Options.BytesLength, packet.Ethernet.IpV4.HeaderLength, "IP HeaderLength"); Assert.AreEqual(IpV4Datagram.HeaderMinimumLength + ipV4Layer.Options.BytesLength, packet.Ethernet.IpV4.HeaderLength, "IP HeaderLength");
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.AreNotEqual(2, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation"); Assert.AreNotEqual(2, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
...@@ -150,6 +141,7 @@ namespace PcapDotNet.Packets.Test ...@@ -150,6 +141,7 @@ namespace PcapDotNet.Packets.Test
} }
Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect"); Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect");
Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options, "IP Options"); Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreNotEqual(packet.Ethernet.IpV4.Options, null, "IP Options");
Assert.AreNotEqual(new IpV4Options(new IpV4OptionUnknown(0, new byte[35])), packet.Ethernet.IpV4.Options, "IP Options"); Assert.AreNotEqual(new IpV4Options(new IpV4OptionUnknown(0, new byte[35])), packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreEqual(ipV4Layer.Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode"); Assert.AreEqual(ipV4Layer.Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode");
Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString()); Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString());
...@@ -566,6 +558,44 @@ namespace PcapDotNet.Packets.Test ...@@ -566,6 +558,44 @@ namespace PcapDotNet.Packets.Test
Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect); Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect);
} }
[TestMethod]
public void IpV4BrokenOptionTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(),
new IpV4Layer
{
Options = new IpV4Options(IpV4Option.Nop, IpV4Option.Nop,
IpV4Option.Nop, IpV4Option.Nop),
Protocol = IpV4Protocol.Tcp
});
byte[] buffer = (byte[])packet.Buffer.Clone();
buffer[buffer.Length - 1] = (byte)IpV4OptionType.BasicSecurity;
packet = new Packet(buffer, DateTime.Now, packet.DataLink);
Assert.IsFalse(packet.IsValid);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4NoNextLayerTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer());
Assert.IsNull(packet);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4BadNextLayerTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer(),
new PayloadLayer());
Assert.IsNull(packet);
}
private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind) private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind)
{ {
return Packet.FromHexadecimalString(hexString, DateTime.MinValue, dataLinkKind); return Packet.FromHexadecimalString(hexString, DateTime.MinValue, dataLinkKind);
......
...@@ -18,23 +18,11 @@ namespace PcapDotNet.Packets.Test ...@@ -18,23 +18,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for PacketBuilderTests
/// </summary>
[TestClass]
public class PacketBuilderTests
{
public PacketBuilderTests()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set; }
#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]
[ExpectedException(typeof(ArgumentException))]
public void NoLayersTest()
{
PacketBuilder packetBuilder = new PacketBuilder();
Assert.IsNull(packetBuilder);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void BadFirstLayerTest()
{
PacketBuilder packetBuilder = new PacketBuilder(new TcpLayer());
Assert.IsNull(packetBuilder);
}
}
}
\ No newline at end of file
...@@ -21,23 +21,11 @@ namespace PcapDotNet.Packets.Test ...@@ -21,23 +21,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets.TestUtils;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for PayloadLayerTests
/// </summary>
[TestClass]
public class PayloadLayerTests
{
public PayloadLayerTests()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext { get; set;}
#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 PayloadLayerEqualsTest()
{
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
PayloadLayer layer = random.NextPayloadLayer(random.Next(100));
Assert.AreNotEqual(layer, null);
Assert.AreEqual(layer, new PayloadLayer
{
Data = layer.Data
});
Assert.AreNotEqual(layer, new PayloadLayer
{
Data = new Datagram(layer.Data.Concat<byte>(1).ToArray())
});
if (layer.Length != 0)
{
Assert.AreNotEqual(layer, new PayloadLayer
{
Data = random.NextDatagram(layer.Length)
});
}
}
}
}
}
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion> <ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6C7326EB-F230-4934-B74B-F99F87204E44}</ProjectGuid> <ProjectGuid>{6C7326EB-F230-4934-B74B-F99F87204E44}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
...@@ -54,7 +54,9 @@ ...@@ -54,7 +54,9 @@
<Compile Include="IpV4Tests.cs" /> <Compile Include="IpV4Tests.cs" />
<Compile Include="IpV6AddressTests.cs" /> <Compile Include="IpV6AddressTests.cs" />
<Compile Include="MacAddressTests.cs" /> <Compile Include="MacAddressTests.cs" />
<Compile Include="PacketBuilderTests.cs" />
<Compile Include="PacketTests.cs" /> <Compile Include="PacketTests.cs" />
<Compile Include="PayloadLayerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" /> <Compile Include="IpV4AddressTests.cs" />
<Compile Include="TcpTests.cs" /> <Compile Include="TcpTests.cs" />
......
...@@ -22,23 +22,11 @@ namespace PcapDotNet.Packets.Test ...@@ -22,23 +22,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
......
...@@ -21,23 +21,11 @@ namespace PcapDotNet.Packets.Test ...@@ -21,23 +21,11 @@ namespace PcapDotNet.Packets.Test
// //
} }
private TestContext testContextInstance;
/// <summary> /// <summary>
///Gets or sets the test context which provides ///Gets or sets the test context which provides
///information about and functionality for the current test run. ///information about and functionality for the current test run.
///</summary> ///</summary>
public TestContext TestContext public TestContext TestContext{ get; set;}
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes #region Additional test attributes
// //
...@@ -116,5 +104,23 @@ namespace PcapDotNet.Packets.Test ...@@ -116,5 +104,23 @@ namespace PcapDotNet.Packets.Test
Assert.IsTrue(packet.Ethernet.IpV4.IsTransportChecksumCorrect); Assert.IsTrue(packet.Ethernet.IpV4.IsTransportChecksumCorrect);
} }
[TestMethod]
public void UdpZeroChecksumTest()
{
byte[] payload = new byte[2];
payload.Write(0, (ushort)65498, Endianity.Big);
Packet packet = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new UdpLayer
{
CalculateChecksum = true
},
new PayloadLayer
{
Data = new Datagram(payload)
});
Assert.IsTrue(packet.Ethernet.IpV4.IsTransportChecksumCorrect);
Assert.AreEqual(0xFFFF, packet.Ethernet.IpV4.Udp.Checksum);
}
} }
} }
\ No newline at end of file
...@@ -44,7 +44,12 @@ namespace PcapDotNet.Packets.Ethernet ...@@ -44,7 +44,12 @@ namespace PcapDotNet.Packets.Ethernet
/// </summary> /// </summary>
public Datagram Payload public Datagram Payload
{ {
get { return IpV4; } get
{
if (_payload == null && Length >= HeaderLength)
_payload = new Datagram(Buffer, StartOffset + HeaderLength, Length - HeaderLength);
return _payload;
}
} }
/// <summary> /// <summary>
...@@ -158,6 +163,7 @@ namespace PcapDotNet.Packets.Ethernet ...@@ -158,6 +163,7 @@ namespace PcapDotNet.Packets.Ethernet
} }
private static readonly MacAddress _broadcastAddress = new MacAddress("FF:FF:FF:FF:FF:FF"); private static readonly MacAddress _broadcastAddress = new MacAddress("FF:FF:FF:FF:FF:FF");
private Datagram _payload;
private IpV4Datagram _ipV4; private IpV4Datagram _ipV4;
private ArpDatagram _arp; private ArpDatagram _arp;
} }
......
...@@ -482,16 +482,6 @@ namespace PcapDotNet.Packets.Igmp ...@@ -482,16 +482,6 @@ namespace PcapDotNet.Packets.Igmp
public override ILayer ExtractLayer() public override ILayer ExtractLayer()
{ {
// IgmpMessageType igmpMessageType = random.NextEnum(IgmpMessageType.None);
// IgmpQueryVersion igmpQueryVersion = IgmpQueryVersion.None;
// TimeSpan igmpMaxResponseTime = random.NextTimeSpan(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(256 * 0.1) - TimeSpan.FromTicks(1));
// IpV4Address igmpGroupAddress = random.NextIpV4Address();
// bool? igmpIsSuppressRouterSideProcessing = null;
// byte? igmpQueryRobustnessVariable = null;
// TimeSpan? igmpQueryInterval = null;
// IpV4Address[] igmpSourceAddresses = null;
// IgmpGroupRecord[] igmpGroupRecords = null;
switch (MessageType) switch (MessageType)
{ {
case IgmpMessageType.MembershipQuery: case IgmpMessageType.MembershipQuery:
......
...@@ -199,7 +199,8 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -199,7 +199,8 @@ namespace PcapDotNet.Packets.IpV4
if (_isTransportChecksumCorrect == null) if (_isTransportChecksumCorrect == null)
{ {
ushort transportChecksum = Transport.Checksum; ushort transportChecksum = Transport.Checksum;
_isTransportChecksumCorrect = (Transport.IsChecksumOptional && transportChecksum == 0) || _isTransportChecksumCorrect = Length >= TotalLength &&
(Transport.IsChecksumOptional && transportChecksum == 0) ||
(CalculateTransportChecksum() == transportChecksum); (CalculateTransportChecksum() == transportChecksum);
} }
return _isTransportChecksumCorrect.Value; return _isTransportChecksumCorrect.Value;
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using PcapDotNet.Base; using PcapDotNet.Base;
namespace PcapDotNet.Packets namespace PcapDotNet.Packets
{ {
internal sealed class OptionComplexFactory<TOptionType> : OptionComplexFactoryBase internal static class OptionComplexFactory<TOptionType>// : OptionComplexFactoryBase
{ {
/// <summary>
/// The header length in bytes for the option (type and size).
/// </summary>
public const int OptionHeaderLength = 2;
internal static Option Read(TOptionType optionType, byte[] buffer, ref int offset, int length) internal static Option Read(TOptionType optionType, byte[] buffer, ref int offset, int length)
{ {
if (length < 1) if (length < 1)
...@@ -65,9 +71,11 @@ namespace PcapDotNet.Packets ...@@ -65,9 +71,11 @@ namespace PcapDotNet.Packets
return registraionAttributes.First(); return registraionAttributes.First();
} }
private OptionComplexFactory() // [DebuggerHidden]
{ // private OptionComplexFactory()
} // {
// throw new NotImplementedException();
// }
private static readonly Dictionary<TOptionType, IOptionComplexFactory> _complexOptions = InitializeComplexOptions(); private static readonly Dictionary<TOptionType, IOptionComplexFactory> _complexOptions = InitializeComplexOptions();
private static readonly IOptionUnknownFactory<TOptionType> UnknownFactoryOptionPrototype = InitializeUnknownOptionPrototype(); private static readonly IOptionUnknownFactory<TOptionType> UnknownFactoryOptionPrototype = InitializeUnknownOptionPrototype();
......
using System;
using System.Diagnostics;
namespace PcapDotNet.Packets namespace PcapDotNet.Packets
{ {
internal abstract class OptionComplexFactoryBase // internal static class OptionComplexFactoryBase
{ // {
/// <summary> // /// <summary>
/// The header length in bytes for the option (type and size). // /// The header length in bytes for the option (type and size).
/// </summary> // /// </summary>
public const int OptionHeaderLength = 2; // public const int OptionHeaderLength = 2;
//
protected OptionComplexFactoryBase() //// [DebuggerHidden]
{ //// protected OptionComplexFactoryBase()
} //// {
} //// throw new NotImplementedException();
//// }
// }
} }
\ No newline at end of file
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