Commit 23b567f8 authored by Brickner_cp's avatar Brickner_cp

Code Coverage 90.84%

parent bc1fa27c
...@@ -62,6 +62,7 @@ namespace PcapDotNet.Packets.Test ...@@ -62,6 +62,7 @@ namespace PcapDotNet.Packets.Test
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);
Assert.IsFalse(DataLink.Ethernet != DataLink.Ethernet);
} }
} }
} }
\ No newline at end of file
...@@ -75,9 +75,12 @@ namespace PcapDotNet.Packets.Test ...@@ -75,9 +75,12 @@ namespace PcapDotNet.Packets.Test
if (datagram.Length != 0) if (datagram.Length != 0)
{ {
Assert.AreNotEqual(datagram, Datagram.Empty);
Assert.AreNotEqual(datagram, random.NextDatagram(datagram.Length)); Assert.AreNotEqual(datagram, random.NextDatagram(datagram.Length));
Assert.AreNotEqual(datagram.GetHashCode(), random.NextDatagram(datagram.Length).GetHashCode()); Assert.AreNotEqual(datagram.GetHashCode(), random.NextDatagram(datagram.Length).GetHashCode());
} }
else
Assert.AreEqual(datagram, Datagram.Empty);
} }
} }
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base; using PcapDotNet.Base;
using PcapDotNet.Packets.TestUtils; using PcapDotNet.Packets.TestUtils;
...@@ -176,8 +177,72 @@ namespace PcapDotNet.Packets.Test ...@@ -176,8 +177,72 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(expected, actual); Assert.AreEqual(expected, actual);
Assert.IsTrue(expected.IsValid); Assert.IsTrue(expected.IsValid);
Assert.IsTrue(actual.IsValid); Assert.IsTrue(actual.IsValid);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IpV4OptionTimestampOverflowErrorTest()
{
Random random = new Random();
IpV4Option option = new IpV4OptionTimestampOnly(random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1, byte.MaxValue + 1), 0);
Assert.IsNotNull(option);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IpV4OptionTimestampPointedIndexErrorTest()
{
Random random = new Random();
IpV4Option option = new IpV4OptionTimestampOnly(0, random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1, byte.MaxValue + 1));
Assert.IsNotNull(option);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IpV4OptionRoutePointedAddressIndexErrorTest()
{
Random random = new Random();
IpV4Option option = new IpV4OptionRecordRoute(random.NextByte(IpV4OptionRecordRoute.PointedAddressIndexMaxValue + 1, byte.MaxValue + 1));
Assert.IsNotNull(option);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4OptionsTooLongErrorTest()
{
IpV4Options options = new IpV4Options(new IpV4OptionTimestampOnly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20));
Assert.IsNotNull(options);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4OptionSimpleErrorTest()
{
IpV4Option option = new IpV4OptionSimple(IpV4OptionType.StrictSourceRouting);
Assert.IsNotNull(option);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4FragmentationOffsetErrorTest()
{
IpV4Fragmentation fragmentation = new IpV4Fragmentation(IpV4FragmentationOptions.None, 2);
Assert.IsNotNull(fragmentation);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void IpV4FragmentationOptionsErrorTest()
{
IpV4Fragmentation fragmentation = new IpV4Fragmentation((IpV4FragmentationOptions)12345, 8);
Assert.IsNotNull(fragmentation);
Assert.Fail();
} }
private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind) private static Packet HexToPacket(string hexString, DataLinkKind dataLinkKind)
......
...@@ -100,5 +100,14 @@ namespace PcapDotNet.Packets.Test ...@@ -100,5 +100,14 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Small)); Assert.AreEqual(address, buffer.ReadMacAddress(ref offset, Endianity.Small));
Assert.AreEqual(MacAddress.SizeOf, offset); Assert.AreEqual(MacAddress.SizeOf, offset);
} }
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MacAddressBadStringErrorTest()
{
MacAddress address = new MacAddress("12:34:56:78");
Assert.IsNotNull(address);
Assert.Fail();
}
} }
} }
\ No newline at end of file
...@@ -118,7 +118,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -118,7 +118,7 @@ namespace PcapDotNet.Packets.TestUtils
break; break;
case IpV4OptionType.RecordRoute: case IpV4OptionType.RecordRoute:
option = new IpV4OptionRecordRoute(addresses, pointedAddressIndex); option = new IpV4OptionRecordRoute(pointedAddressIndex, addresses);
break; break;
} }
break; break;
......
...@@ -5,18 +5,23 @@ namespace PcapDotNet.Packets ...@@ -5,18 +5,23 @@ namespace PcapDotNet.Packets
{ {
public class IpV4OptionRecordRoute : IpV4OptionRoute public class IpV4OptionRecordRoute : IpV4OptionRoute
{ {
public IpV4OptionRecordRoute(IList<IpV4Address> addresses, byte pointedAddressIndex) public IpV4OptionRecordRoute(byte pointedAddressIndex, IList<IpV4Address> addresses)
: base(IpV4OptionType.RecordRoute, addresses, pointedAddressIndex) : base(IpV4OptionType.RecordRoute, addresses, pointedAddressIndex)
{ {
} }
public IpV4OptionRecordRoute(byte pointedAddressIndex, params IpV4Address[] addresses)
: this(pointedAddressIndex, (IList<IpV4Address>)addresses)
{
}
internal static IpV4OptionRecordRoute ReadOptionRecordRoute(byte[] buffer, ref int offset, byte valueLength) internal static IpV4OptionRecordRoute ReadOptionRecordRoute(byte[] buffer, ref int offset, byte valueLength)
{ {
IpV4Address[] addresses; IpV4Address[] addresses;
byte pointedAddressIndex; byte pointedAddressIndex;
if (!TryRead(out addresses, out pointedAddressIndex, buffer, ref offset, valueLength)) if (!TryRead(out addresses, out pointedAddressIndex, buffer, ref offset, valueLength))
return null; return null;
return new IpV4OptionRecordRoute(addresses, pointedAddressIndex); return new IpV4OptionRecordRoute(pointedAddressIndex, addresses);
} }
} }
} }
\ No newline at end of file
...@@ -19,7 +19,7 @@ namespace PcapDotNet.Packets ...@@ -19,7 +19,7 @@ namespace PcapDotNet.Packets
public override int Length public override int Length
{ {
get { return OptionMinimumLength + 4 * _addresses.Count; } get { return OptionMinimumLength + IpV4Address.SizeOf * Route.Count; }
} }
public override bool IsAppearsAtMostOnce public override bool IsAppearsAtMostOnce
...@@ -34,7 +34,7 @@ namespace PcapDotNet.Packets ...@@ -34,7 +34,7 @@ namespace PcapDotNet.Packets
return Equivalent(other) && return Equivalent(other) &&
PointedAddressIndex == other.PointedAddressIndex && PointedAddressIndex == other.PointedAddressIndex &&
_addresses.SequenceEqual(other._addresses); Route.SequenceEqual(other.Route);
} }
public override bool Equals(IpV4Option other) public override bool Equals(IpV4Option other)
...@@ -46,7 +46,7 @@ namespace PcapDotNet.Packets ...@@ -46,7 +46,7 @@ namespace PcapDotNet.Packets
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^
PointedAddressIndex ^ PointedAddressIndex ^
_addresses.Aggregate(0, (value, address) => value ^ address.GetHashCode()); Route.Aggregate(0, (value, address) => value ^ address.GetHashCode());
} }
public ReadOnlyCollection<IpV4Address> Route public ReadOnlyCollection<IpV4Address> Route
...@@ -59,8 +59,8 @@ namespace PcapDotNet.Packets ...@@ -59,8 +59,8 @@ namespace PcapDotNet.Packets
base.Write(buffer, ref offset); base.Write(buffer, ref offset);
buffer[offset++] = (byte)Length; buffer[offset++] = (byte)Length;
buffer[offset++] = (byte)(OptionMinimumLength + 1 + PointedAddressIndex * 4); buffer[offset++] = (byte)(OptionMinimumLength + 1 + PointedAddressIndex * 4);
for (int i = 0; i != _addresses.Count; ++i) foreach (IpV4Address address in Route)
buffer.Write(ref offset, _addresses[i], Endianity.Big); buffer.Write(ref offset, address, Endianity.Big);
} }
protected static bool TryRead(out IpV4Address[] addresses, out byte pointedAddressIndex, protected static bool TryRead(out IpV4Address[] addresses, out byte pointedAddressIndex,
......
...@@ -28,9 +28,9 @@ namespace PcapDotNet.Packets ...@@ -28,9 +28,9 @@ namespace PcapDotNet.Packets
public override int GetHashCode() public override int GetHashCode()
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^
_addressesAndTimestamps.Aggregate(0, (value, pair) => value ^ TimedRoute.Aggregate(0, (value, pair) => value ^
pair.Key.GetHashCode() ^ pair.Key.GetHashCode() ^
(int)pair.Value); (int)pair.Value);
} }
internal static IpV4OptionTimestampAndAddress Read(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues) internal static IpV4OptionTimestampAndAddress Read(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues)
...@@ -50,17 +50,17 @@ namespace PcapDotNet.Packets ...@@ -50,17 +50,17 @@ namespace PcapDotNet.Packets
protected override int ValuesLength protected override int ValuesLength
{ {
get { return _addressesAndTimestamps.Count * 2 * 4; } get { return TimedRoute.Count * (IpV4Address.SizeOf + sizeof(uint)); }
} }
protected override bool EqualValues(IpV4OptionTimestamp other) protected override bool EqualValues(IpV4OptionTimestamp other)
{ {
return _addressesAndTimestamps.SequenceEqual(((IpV4OptionTimestampAndAddress)other)._addressesAndTimestamps); return TimedRoute.SequenceEqual(((IpV4OptionTimestampAndAddress)other).TimedRoute);
} }
protected override void WriteValues(byte[] buffer, ref int offset) protected override void WriteValues(byte[] buffer, ref int offset)
{ {
foreach (KeyValuePair<IpV4Address, uint> addressAndTimestamp in _addressesAndTimestamps) foreach (KeyValuePair<IpV4Address, uint> addressAndTimestamp in TimedRoute)
{ {
buffer.Write(ref offset, addressAndTimestamp.Key, Endianity.Big); buffer.Write(ref offset, addressAndTimestamp.Key, Endianity.Big);
buffer.Write(ref offset, addressAndTimestamp.Value, Endianity.Big); buffer.Write(ref offset, addressAndTimestamp.Value, Endianity.Big);
......
...@@ -13,6 +13,11 @@ namespace PcapDotNet.TestUtils ...@@ -13,6 +13,11 @@ namespace PcapDotNet.TestUtils
return random.Next() % 2 == 0; return random.Next() % 2 == 0;
} }
public static byte NextByte(this Random random, int minValue, int maxValue)
{
return (byte)random.Next(minValue, maxValue);
}
public static byte NextByte(this Random random, int maxValue) public static byte NextByte(this Random random, int maxValue)
{ {
return (byte)random.Next(maxValue); return (byte)random.Next(maxValue);
......
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