Commit 7a3ec961 authored by Brickner_cp's avatar Brickner_cp

Code coverage 95.83%

parent 4cf6f1a4
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Core.Test
{
/// <summary>
/// Summary description for PacketTimestampTests.
/// </summary>
[TestClass]
public class PacketTimestampTests
{
/// <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 MinMaxTests()
{
MoreAssert.IsBigger(PacketTimestamp.MinimumPacketTimestamp, PacketTimestamp.MaximumPacketTimestamp);
}
}
}
\ No newline at end of file
using System; using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets;
namespace PcapDotNet.Core.Test namespace PcapDotNet.Core.Test
{ {
...@@ -70,6 +71,15 @@ namespace PcapDotNet.Core.Test ...@@ -70,6 +71,15 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
public void ValidKindsTest()
{
foreach (DataLinkKind kind in typeof(DataLinkKind).GetEnumValues())
{
Assert.AreEqual(kind, new PcapDataLink(kind).Kind);
}
}
[TestMethod] [TestMethod]
[ExpectedException(typeof(NotSupportedException), AllowDerivedTypes = false)] [ExpectedException(typeof(NotSupportedException), AllowDerivedTypes = false)]
public void UnsupportedKindErrorTest() public void UnsupportedKindErrorTest()
...@@ -96,6 +106,16 @@ namespace PcapDotNet.Core.Test ...@@ -96,6 +106,16 @@ namespace PcapDotNet.Core.Test
Assert.Fail(); Assert.Fail();
} }
[TestMethod]
[ExpectedException(typeof(NotSupportedException), AllowDerivedTypes = false)]
public void InvalidKindTest()
{
const DataLinkKind InvalidKind = (DataLinkKind)100;
IDataLink dataLink = new PcapDataLink(InvalidKind);
Assert.IsNotNull(dataLink);
Assert.Fail();
}
private static PcapDataLink GetInvalidDataLink() private static PcapDataLink GetInvalidDataLink()
{ {
for (int i = 0; i != 1000; ++i) for (int i = 0; i != 1000; ++i)
......
...@@ -74,6 +74,7 @@ ...@@ -74,6 +74,7 @@
<Compile Include="LivePacketDeviceTests.cs" /> <Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MarshalingServicesTests.cs" /> <Compile Include="MarshalingServicesTests.cs" />
<Compile Include="IpV4OptionExtensions.cs" /> <Compile Include="IpV4OptionExtensions.cs" />
<Compile Include="PacketTimestampTests.cs" />
<Compile Include="TcpOptionExtensions.cs" /> <Compile Include="TcpOptionExtensions.cs" />
<Compile Include="WiresharkDatagramComparer.cs" /> <Compile Include="WiresharkDatagramComparer.cs" />
<Compile Include="WiresharkDatagramComparerArp.cs" /> <Compile Include="WiresharkDatagramComparerArp.cs" />
......
...@@ -4,7 +4,7 @@ using PcapDotNet.TestUtils; ...@@ -4,7 +4,7 @@ using PcapDotNet.TestUtils;
namespace PcapDotNet.Core.Test namespace PcapDotNet.Core.Test
{ {
/// <summary> /// <summary>
/// Summary description for PcapLibTests /// Summary description for PcapLibTests.
/// </summary> /// </summary>
[TestClass] [TestClass]
public class PcapLibTests public class PcapLibTests
......
...@@ -150,13 +150,39 @@ namespace PcapDotNet.Packets.Test ...@@ -150,13 +150,39 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
public void ByteArrayUnsignedBigIntegerTest() public void ByteArrayUnsignedBigIntegerTest()
{ {
byte[] buffer = new byte[100];
for (BigInteger expectedValue = 1; expectedValue <= ushort.MaxValue; expectedValue *= 10) for (BigInteger expectedValue = 1; expectedValue <= ushort.MaxValue; expectedValue *= 10)
{ {
byte[] buffer = new byte[100];
buffer.WriteUnsigned(5, expectedValue, 2, Endianity.Big); buffer.WriteUnsigned(5, expectedValue, 2, Endianity.Big);
BigInteger actualValue = buffer.ReadUnsignedBigInteger(5, 2, Endianity.Big); BigInteger actualValue = buffer.ReadUnsignedBigInteger(5, 2, Endianity.Big);
Assert.AreEqual(expectedValue, actualValue); Assert.AreEqual(expectedValue, actualValue);
buffer = new byte[100];
buffer.WriteUnsigned(5, expectedValue, 2, Endianity.Small);
actualValue = buffer.ReadUnsignedBigInteger(5, 2, Endianity.Small);
Assert.AreEqual(expectedValue, actualValue);
} }
for (BigInteger expectedValue = ushort.MaxValue; expectedValue > 0; expectedValue /= 10)
{
byte[] buffer = new byte[100];
buffer.WriteUnsigned(5, expectedValue, 2, Endianity.Big);
BigInteger actualValue = buffer.ReadUnsignedBigInteger(5, 2, Endianity.Big);
Assert.AreEqual(expectedValue, actualValue);
buffer = new byte[100];
buffer.WriteUnsigned(5, expectedValue, 2, Endianity.Small);
actualValue = buffer.ReadUnsignedBigInteger(5, 2, Endianity.Small);
Assert.AreEqual(expectedValue, actualValue);
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void ByteArrayUnsignedBigIntegerNullBufferTest()
{
byte[] buffer = null;
Assert.IsNotNull(buffer.ReadUnsignedBigInteger(0, 0, Endianity.Big));
Assert.Fail();
} }
[TestMethod] [TestMethod]
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for DataSegmentTests.
/// </summary>
[TestClass]
public class DataSegmentTests
{
/// <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 ToHexadecimalStringTest()
{
byte[] input = new byte[] {1, 2, 3, 4, 5, 6};
Assert.AreEqual(HexEncoding.Instance.GetString(input), new DataSegment(input).ToHexadecimalString());
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void DecodeNullEncodingTest()
{
Assert.IsNotNull(new DataSegment(new byte[1]).Decode(null));
Assert.Fail();
}
}
}
\ No newline at end of file
...@@ -11,7 +11,7 @@ using PcapDotNet.Packets.Transport; ...@@ -11,7 +11,7 @@ using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.Test namespace PcapDotNet.Packets.Test
{ {
/// <summary> /// <summary>
/// Summary description for DatagramTests /// Summary description for DatagramTests.
/// </summary> /// </summary>
[TestClass] [TestClass]
public class DatagramTests public class DatagramTests
......
...@@ -114,6 +114,7 @@ namespace PcapDotNet.Packets.Test ...@@ -114,6 +114,7 @@ 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.AreEqual(ipV4Layer.Destination, packet.Ethernet.IpV4.Destination, "Destination");
Assert.AreNotEqual(ipV4Layer, null); Assert.AreNotEqual(ipV4Layer, null);
Assert.AreNotEqual(ipV4Layer, new PayloadLayer()); Assert.AreNotEqual(ipV4Layer, new PayloadLayer());
Assert.IsNotNull(ipV4Layer.ToString()); Assert.IsNotNull(ipV4Layer.ToString());
......
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
<Compile Include="ByteArrayExtensionsTests.cs" /> <Compile Include="ByteArrayExtensionsTests.cs" />
<Compile Include="DatagramTests.cs" /> <Compile Include="DatagramTests.cs" />
<Compile Include="DataLinkTests.cs" /> <Compile Include="DataLinkTests.cs" />
<Compile Include="DataSegmentTests.cs" />
<Compile Include="DnsTests.cs" /> <Compile Include="DnsTests.cs" />
<Compile Include="EndianitiyTests.cs" /> <Compile Include="EndianitiyTests.cs" />
<Compile Include="EthernetTests.cs" /> <Compile Include="EthernetTests.cs" />
......
...@@ -175,9 +175,32 @@ namespace PcapDotNet.Packets.Test ...@@ -175,9 +175,32 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
[ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)]
public void TcpOptionMoodBadEmotionStringTest() public void TcpOptionMoodConstructorBadEmotionStringTest()
{ {
Assert.IsNotNull(new TcpOptionMood((TcpOptionMoodEmotion)202).EmotionString); Assert.IsNotNull(new TcpOptionMood((TcpOptionMoodEmotion)202).EmotionString);
Assert.Fail();
}
[TestMethod]
public void TcpOptionMoodReadFromBufferBadEmotionStringTest()
{
Packet packet = PacketBuilder.Build(DateTime.Now,
new IpV4Layer(),
new TcpLayer
{
Options = new TcpOptions(new TcpOptionMood(TcpOptionMoodEmotion.Happy))
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(1, packet.IpV4.Tcp.Options.Count);
byte[] newPacketBuffer = new byte[packet.Length];
packet.CopyTo(newPacketBuffer, 0);
newPacketBuffer[packet.Length - 1] = (byte)'a';
newPacketBuffer[packet.Length - 2] = (byte)'a';
Packet newPacket = new Packet(newPacketBuffer, DateTime.Now, DataLinkKind.IpV4);
Assert.IsFalse(newPacket.IsValid);
Assert.AreEqual(0, newPacket.IpV4.Tcp.Options.Count);
} }
[TestMethod] [TestMethod]
......
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