Skip to content

Commit

Permalink
Fulls test coverage & Equals method for CryptoGroupAlgebra
Browse files Browse the repository at this point in the history
  • Loading branch information
lumip committed Jun 13, 2020
1 parent f49b866 commit f6db0e4
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<Compile Include="MultiplicativeGroupAlgebraTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="NISTP256Tests.cs" />
<Compile Include="MultiplicativeCryptoGroupTests.cs" />
<Compile Include="ECCryptoGroupTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
18 changes: 17 additions & 1 deletion CompactCryptoGroupAlgebra.Tests/CryptoGroupAlgebraTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests.CryptoAlgebra
namespace CompactCryptoGroupAlgebra.Tests
{
interface CryptoGroupAlgebraProtectedMembers
{
Expand Down Expand Up @@ -236,5 +236,21 @@ public void TestNegate()
);
}

[Test]
public void TestEqualsCallsSpecificEquals()
{
var algebraMock = new Mock<CryptoGroupAlgebra<int>>() { CallBase = true };
algebraMock.Setup(alg => alg.Equals(It.IsAny<CryptoGroupAlgebra<int>>())).Returns(true);

var otherAlgebraMock = new Mock<CryptoGroupAlgebra<int>>();

Assert.IsTrue(algebraMock.Object.Equals((object)(otherAlgebraMock.Object)));

algebraMock.Verify(
alg => alg.Equals(It.Is<CryptoGroupAlgebra<int>>(x => x == otherAlgebraMock.Object)),
Times.Once()
);
}

}
}
74 changes: 73 additions & 1 deletion CompactCryptoGroupAlgebra.Tests/CryptoGroupElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests.CryptoAlgebra
namespace CompactCryptoGroupAlgebra.Tests
{
[TestFixture]
public class CryptoGroupElementTests
Expand Down Expand Up @@ -120,6 +120,21 @@ public void TestAddRejectsNull()
);
}

[Test]
public void TestSpecificAddRejectsNull()
{
CryptoGroupElement<int> otherElement = null;

var algebraStub = new Mock<ICryptoGroupAlgebra<int>>();
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);

var element = new CryptoGroupElement<int>(0, algebraStub.Object);

Assert.Throws<ArgumentNullException>(
() => element.Add(otherElement)
);
}

[Test]
public void TestAddRejectsElementFromDifferentGroup()
{
Expand Down Expand Up @@ -245,6 +260,7 @@ public void TestEqualsFalseForDifferentAlgebras()

var algebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);
algebraStub.Setup(alg => alg.Equals(It.IsAny<ICryptoGroupAlgebra<int>>())).Returns(false);

var element = new CryptoGroupElement<int>(0, algebraStub.Object);

Expand All @@ -253,6 +269,24 @@ public void TestEqualsFalseForDifferentAlgebras()
Assert.IsFalse(result);
}

[Test]
public void TestEqualsTrueForEqualAlgebras()
{
var otherAlgebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
otherAlgebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);
var otherElement = new CryptoGroupElement<int>(0, otherAlgebraStub.Object);

var algebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);
algebraStub.Setup(alg => alg.Equals(It.IsAny<ICryptoGroupAlgebra<int>>())).Returns(true);

var element = new CryptoGroupElement<int>(0, algebraStub.Object);

bool result = element.Equals(otherElement);

Assert.IsTrue(result);
}

[Test]
public void TestEqualsTrue()
{
Expand All @@ -267,6 +301,20 @@ public void TestEqualsTrue()
Assert.IsTrue(result);
}

[Test]
public void TestEqualsUnrelatedObject()
{
var algebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);

var otherElement = new object { };
var element = new CryptoGroupElement<int>(5, algebraStub.Object);

bool result = element.Equals(otherElement);

Assert.IsFalse(result);
}

[Test]
public void TestOperatorPlus()
{
Expand Down Expand Up @@ -404,5 +452,29 @@ public void TestToBytesCallsAlgebra()

algebraMock.Verify(alg => alg.ToBytes(It.Is<int>(x => x == value)), Times.Once());
}

[Test]
public void TestGetHashCodeSameForEqual()
{
var algebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);

var otherElement = new CryptoGroupElement<int>(5, algebraStub.Object);
var element = new CryptoGroupElement<int>(5, algebraStub.Object);

Assert.AreEqual(element.GetHashCode(), otherElement.GetHashCode());
}

[Test]
public void TestToString()
{
var algebraStub = new Mock<ICryptoGroupAlgebra<int>>(MockBehavior.Strict);
algebraStub.Setup(alg => alg.IsValid(It.IsAny<int>())).Returns(true);

var element = new CryptoGroupElement<int>(5, algebraStub.Object);

var expected = "<CryptoGroupElement: 5>";
Assert.AreEqual(expected, element.ToString());
}
}
}
2 changes: 1 addition & 1 deletion CompactCryptoGroupAlgebra.Tests/CryptoGroupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests.CryptoAlgebra
namespace CompactCryptoGroupAlgebra.Tests
{
interface CryptoGroupProtectedMembers
{
Expand Down
2 changes: 0 additions & 2 deletions CompactCryptoGroupAlgebra.Tests/DefaultECParrameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using NUnit.Framework;

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests
{
[TestFixture]
Expand Down
83 changes: 83 additions & 0 deletions CompactCryptoGroupAlgebra.Tests/ECCryptoGroupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Numerics;

using NUnit.Framework;

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests
{
[TestFixture]
public class ECCryptoGroupTests
{
private readonly ECParameters ecParams;

public ECCryptoGroupTests()
{
ecParams = new ECParameters()
{
P = 23,
A = -2,
B = 2,
Generator = new ECPoint(1, 1),
Order = 16
};
}

/// <summary>
/// This indirectly tests <see cref="ECCryptoGroup.CreateGroupElement(byte[])"/>.
/// </summary>
[Test]
public void TestFromBytes()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var group = new ECCryptoGroup(groupAlgebra);

var expectedRaw = new ECPoint(5, 5);
var expected = new CryptoGroupElement<ECPoint>(expectedRaw, groupAlgebra);
var bytes = expected.ToBytes();
var result = group.FromBytes(bytes);
Assert.AreEqual(expected, result);
}

/// <summary>
/// Indirectly also tests <see cref="ECCryptoGroup.CreateGroupElement(ECPoint)"/>.
/// </summary>
[Test]
public void TestConstructorAlgebra()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var group = new ECCryptoGroup(groupAlgebra);

var expectedNeutralElement = new CryptoGroupElement<ECPoint>(ECPoint.PointAtInfinity, groupAlgebra);
var expectedGenerator = new CryptoGroupElement<ECPoint>(ecParams.Generator, groupAlgebra);

var resultNeutralElement = group.NeutralElement;
var resultGenerator = group.Generator;

Assert.AreEqual(expectedNeutralElement, resultNeutralElement, "verifying neutral element");
Assert.AreEqual(expectedGenerator, resultGenerator, "verifying generator");
Assert.AreEqual(ecParams.Order, group.Order, "verifying order");
}

/// <summary>
/// Indirectly also tests <see cref="ECCryptoGroup.CreateGroupElement(ECPoint)"/>.
/// </summary>
[Test]
public void TestConstructorParameters()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var group = new ECCryptoGroup(ecParams);

var expectedNeutralElement = new CryptoGroupElement<ECPoint>(ECPoint.PointAtInfinity, groupAlgebra);
var expectedGenerator = new CryptoGroupElement<ECPoint>(ecParams.Generator, groupAlgebra);

var resultNeutralElement = group.NeutralElement;
var resultGenerator = group.Generator;

Assert.AreEqual(expectedNeutralElement, resultNeutralElement, "verifying neutral element");
Assert.AreEqual(expectedGenerator, resultGenerator, "verifying generator");
Assert.AreEqual(ecParams.Order, group.Order, "verifying order");
}
}
}
66 changes: 66 additions & 0 deletions CompactCryptoGroupAlgebra.Tests/ECGroupAlgebraTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,71 @@ public void TestInvalidElementRejectedAsGenerator()
() => new ECGroupAlgebra(invalidParams)
);
}

[Test]
public void TestProperties()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);

Assert.AreEqual(ECPoint.PointAtInfinity, groupAlgebra.NeutralElement, "verifying neutral element");
Assert.AreEqual(ecParams.Generator, groupAlgebra.Generator, "verifying generator");
Assert.AreEqual(ecParams.Order, groupAlgebra.Order, "verifying order");
}


[Test]
public void TestEqualsTrue()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var otherAlgebra = new ECGroupAlgebra(ecParams);

bool result = groupAlgebra.Equals(otherAlgebra);
Assert.IsTrue(result);
}

[Test]
public void TestEqualsFalseForNull()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
MultiplicativeGroupAlgebra otherAlgebra = null;

bool result = groupAlgebra.Equals(otherAlgebra);
Assert.IsFalse(result);
}

[Test]
public void TestEqualsFalseForUnrelatedObject()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var otherAlgebra = new object { };

bool result = groupAlgebra.Equals(otherAlgebra);
Assert.IsFalse(result);
}

[Test]
public void TestEqualsFalseForOtherAlgebra()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
ECParameters otherParams = new ECParameters()
{
P = 134217728, // == 2 ^ 27
Generator = CompactCryptoGroupAlgebra.ECPoint.PointAtInfinity,
Order = 1
};
var otherAlgebra = new ECGroupAlgebra(otherParams);

bool result = groupAlgebra.Equals(otherAlgebra);
Assert.IsFalse(result);
}

[Test]
public void TestGetHashCodeSameForEqual()
{
var groupAlgebra = new ECGroupAlgebra(ecParams);
var otherAlgebra = new ECGroupAlgebra(ecParams);

Assert.AreEqual(groupAlgebra.GetHashCode(), otherAlgebra.GetHashCode());
}
}
}
16 changes: 16 additions & 0 deletions CompactCryptoGroupAlgebra.Tests/ECPointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,21 @@ public void TestPointEquality()
Assert.AreEqual(p, q);
Assert.AreEqual(q, p);
}

[Test]
public void TestToStringRegularPoint()
{
var p = new ECPoint(1, 2);
var expected = "(1, 2)";
Assert.AreEqual(expected, p.ToString());
}

[Test]
public void TestToStringPointAtInfinity()
{
var p = ECPoint.PointAtInfinity;
var expected = "(atInf)";
Assert.AreEqual(expected, p.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using CompactCryptoGroupAlgebra;

namespace CompactCryptoGroupAlgebra.Tests.CryptoAlgebra
namespace CompactCryptoGroupAlgebra.Tests
{

[TestFixture]
Expand Down

0 comments on commit f6db0e4

Please sign in to comment.