Commit 74bbb693 authored by Brickner_cp's avatar Brickner_cp

Code coverage 94.34%

parent 90666599
......@@ -67,7 +67,7 @@ namespace PcapDotNet.Core.Test
PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out actualPacket);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, actualPacket);
MoreAssert.IsInRange(expectedPacket.Timestamp.AddMicroseconds(-1), expectedPacket.Timestamp.AddMicroseconds(1), actualPacket.Timestamp);
MoreAssert.IsInRange(expectedPacket.Timestamp.AddMicroseconds(-2), expectedPacket.Timestamp.AddMicroseconds(1), actualPacket.Timestamp);
}
}
......
......@@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets.Arp;
using PcapDotNet.Packets.Dns;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
......@@ -91,6 +89,7 @@ namespace PcapDotNet.Packets.Test
Assert.IsTrue(record.Equals(record));
Assert.IsTrue(record.DomainName.Equals((object)record.DomainName));
Assert.IsTrue(record.DomainName.Equals((object)record.DomainName));
Assert.AreEqual(record.GetHashCode(), record.GetHashCode());
}
foreach (var record in packet.Ethernet.IpV4.Udp.Dns.DataResourceRecords)
......@@ -186,6 +185,15 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsDomainNameConstructorNullStringTest()
{
DnsDomainName domainName = new DnsDomainName(null);
Assert.IsNotNull(domainName);
Assert.Fail();
}
[TestMethod]
public void DnsOptResourceRecordTest()
{
......@@ -319,6 +327,26 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataTransactionKeyConstructorNullKeyTest()
{
var resourceData = new DnsResourceDataTransactionKey(DnsDomainName.Root, 0, 0, DnsTransactionKeyMode.KeyDeletion, DnsResponseCode.NoError,
null, DataSegment.Empty);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataTransactionKeyConstructorNullOtherTest()
{
var resourceData = new DnsResourceDataTransactionKey(DnsDomainName.Root, 0, 0, DnsTransactionKeyMode.KeyDeletion, DnsResponseCode.NoError,
DataSegment.Empty, null);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataTransactionKeyTooBigKeyTest()
......@@ -339,6 +367,35 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
public void DnsResourceDataTransactionKeyParseTooShortTest()
{
var resourceData = new DnsResourceDataTransactionKey(new DnsDomainName("pcapdot.net"), 0, 0, DnsTransactionKeyMode.KeyDeletion,
DnsResponseCode.NoError, new DataSegment(new byte[5]), new DataSegment(new byte[5]));
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TKey, resourceData, -1);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TKey, resourceData, -6);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TKey, resourceData, -11);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TKey, resourceData, -23);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataTransactionSignatureConstructorNullMessageAuthenticationCodeTest()
{
var resourceData = new DnsResourceDataTransactionSignature(DnsDomainName.Root, 0, 0, null, 0, DnsResponseCode.NoError, DataSegment.Empty);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataTransactionSignatureConstructorNullOtherTest()
{
var resourceData = new DnsResourceDataTransactionSignature(DnsDomainName.Root, 0, 0, DataSegment.Empty, 0, DnsResponseCode.NoError, null);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataTransactionSignatureTooBigMessageAuthenticationCodeTest()
......@@ -359,6 +416,17 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
public void DnsResourceDataTransactionSignatureParseWrongSizeTest()
{
var resourceData = new DnsResourceDataTransactionSignature(new DnsDomainName("pcapdot.net"), 0, 0, new DataSegment(new byte[5]), 0,
DnsResponseCode.NoError, new DataSegment(new byte[5]));
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TransactionSignature, resourceData, 1);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TransactionSignature, resourceData, -6);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TransactionSignature, resourceData, -11);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.TransactionSignature, resourceData, -23);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataHostIdentityProtocolTooBigHostIdentityTagTest()
......@@ -379,6 +447,38 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataHostIdentityProtocolConstructorNullHostIdentityTagTest()
{
var resourceData = new DnsResourceDataHostIdentityProtocol(null, DnsPublicKeyAlgorithm.None, DataSegment.Empty, new DnsDomainName[0]);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void DnsResourceDataHostIdentityProtocolConstructorNullPublicKeyTest()
{
var resourceData = new DnsResourceDataHostIdentityProtocol(DataSegment.Empty, DnsPublicKeyAlgorithm.None, null, new DnsDomainName[0]);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
public void DnsResourceDataHostIdentityProtocolParseWrongLengthTest()
{
var resourceData = new DnsResourceDataHostIdentityProtocol(new DataSegment(new byte[5]), DnsPublicKeyAlgorithm.None, new DataSegment(new byte[5]),
new[]
{
new DnsDomainName("pcapdot.net"),
new DnsDomainName("pcapdotnet.codeplex.com")
});
TestResourceRecordIsNotCreatedWithNewLength(DnsType.Hip, resourceData, -1);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.Hip, resourceData, -39);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.Hip, resourceData, -49);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataLocationInformationInvalidSizeTest()
......@@ -509,6 +609,34 @@ namespace PcapDotNet.Packets.Test
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataA6ConstructorAddressSuffixTooSmallTest()
{
var resourceData = new DnsResourceDataA6(127, IpV6Address.Zero, DnsDomainName.Root);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DnsResourceDataA6ConstructorAddressSuffixTooBigTest()
{
var resourceData = new DnsResourceDataA6(1, IpV6Address.MaxValue, DnsDomainName.Root);
Assert.IsNull(resourceData);
Assert.Fail();
}
[TestMethod]
public void DnsResourceDataA6ParseToShortTest()
{
var resourceData = new DnsResourceDataA6(100, new IpV6Address("::F12:3456"), new DnsDomainName("pcapdot.net"));
TestResourceRecordIsNotCreatedWithNewLength(DnsType.A6, resourceData, 1);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.A6, resourceData, -1);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.A6, resourceData, -14);
TestResourceRecordIsNotCreatedWithNewLength(DnsType.A6, resourceData, -17);
}
private static void TestDomainNameCompression(int expectedCompressionBenefit, DnsLayer dnsLayer)
{
dnsLayer.DomainNameCompressionMode = DnsDomainNameCompressionMode.Nothing;
......
......@@ -44,10 +44,10 @@ namespace PcapDotNet.Packets.Dns
/// <param name="prefixName">The name of the prefix, encoded as a domain name. This name must not be compressed. </param>
public DnsResourceDataA6(byte prefixLength, IpV6Address addressSuffix, DnsDomainName prefixName)
{
if (IsAddressSuffixTooBig(prefixLength, addressSuffix))
if (IsAddressSuffixTooSmall(prefixLength, addressSuffix))
throw new ArgumentOutOfRangeException("addressSuffix",
string.Format(CultureInfo.InvariantCulture, "Value is too small for prefix length {0}", prefixLength));
if (IsAddressSuffixTooSmall(prefixLength, addressSuffix))
if (IsAddressSuffixTooBig(prefixLength, addressSuffix))
throw new ArgumentOutOfRangeException("addressSuffix",
string.Format(CultureInfo.InvariantCulture, "Value is too big for prefix length {0}", prefixLength));
......@@ -144,7 +144,7 @@ namespace PcapDotNet.Packets.Dns
offsetInDns += addressSuffixLength;
length -= addressSuffixLength;
if (IsAddressSuffixTooBig(prefixLength, addressSuffix) || IsAddressSuffixTooSmall(prefixLength, addressSuffix))
if (IsAddressSuffixTooSmall(prefixLength, addressSuffix) || IsAddressSuffixTooBig(prefixLength, addressSuffix))
return null;
DnsDomainName prefixName;
......@@ -157,12 +157,12 @@ namespace PcapDotNet.Packets.Dns
return new DnsResourceDataA6(prefixLength, addressSuffix, prefixName);
}
private static bool IsAddressSuffixTooBig(byte prefixLength, IpV6Address addressSuffix)
private static bool IsAddressSuffixTooSmall(byte prefixLength, IpV6Address addressSuffix)
{
return (prefixLength < 128 && (addressSuffix.ToValue() < (UInt128.One << (127 - prefixLength))));
}
private static bool IsAddressSuffixTooSmall(byte prefixLength, IpV6Address addressSuffix)
private static bool IsAddressSuffixTooBig(byte prefixLength, IpV6Address addressSuffix)
{
return (prefixLength > 0 && (addressSuffix.ToValue() >= (UInt128.One << (128 - prefixLength))));
}
......
......@@ -184,7 +184,6 @@ namespace PcapDotNet.Packets.Dns
rendezvousServers.Add(rendezvousServer);
offsetInDns += rendezvousServerLength;
length -= rendezvousServerLength;
}
return new DnsResourceDataHostIdentityProtocol(hostIdentityTag, publicKeyAlgorithm, publicKey, rendezvousServers);
......
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