Skip to content

Commit

Permalink
Unit Tests of Crypto Module (neo-project#962)
Browse files Browse the repository at this point in the history
* testDemo

* add dll lib

* add dbsnapshot dispose

* test get blocks in levelDBStore

* add levelDBStore test funcs

* fix levelDBStore funcs

* add DbCache addInternal

* differ db path

* space

* fix delete internal test

* add test getInternal tryGetInternal move libleveldb.dll

* add dbCache method test

* add store test

* add cache unit tests

* add cache unit tests

* up readonly max_capacity

* fix leveldbexception

* fix comment on UT_Cache

* format

* fix multithread test problem

* up cache

* update travis config

* update travis.yml

* test DbMetaDataCache

* fix db directory

* format and update travis for maxos

* fix mac env travis

* 2019/7/12 10:34

* 2019/7/12 11:01

* remove commented line

* test BigDecimal

* fix format and csproj

* rm  coverage.opencover.xml

* update method name

* add UT_P_Helper

* modify UT_P_Helper

* modify UT_P_helper

* Clean ut

* test Base58 & BloomFilter

* Update UT_Cache.cs

* Correct Typo

* test JsonArray

* update namespace

* update namespace

* update format

* update format

* organise folder structure

* add UT_JString

* test JBoolean JNumber & JObject

* 2019/7/16 10:30

add some test case for UInt32Wrapper and SerializableWrapper

* fix timestamp

* test ECDsa and Crypto

* test OrderedDictionary & complete IO.Json tests

* 2019/7/16 17:33

add some test case of SQLiteWallet

* test FIFOSet

* add CloneCache and DataCache unit tests

* fix namespace

* add UT_Cryptography_Helper

* format UT_CloneCache and UT_DataCache

* add UT_DataCache.GetAndChange unit test

* update namespace

* remove comment code

* delete Persistence part

* 2019/7/19 11:07

add some test case for Helper in VM

* Fix Base58 Test

* 2019/7/19 11:33

change some format

* update IOHelper exception assert

* 2019/7/19 14:22

change format

* format IOHelper

* review IO.Wrapper

* review Wallets.SQLite UT

* Test ECFieldElement ECPoint

* refactor package

* format ECDsa

* update namespace

* Code fix

* review cache

* modify UT_JString

* fomat

* using Actin replace with try-catch

* add UT_CloneMetaCache and UT_MetaDataCache

* update namespace

* format UT_DataCache.cs

* Code Fix

* format

* update csproj

* Code fix for UT_ECFieldElement and UT_ECPoint

* Code fix

* format

* update travis

* delete deleteFiles

* fix path and comment

* update travis

* delete test ToTimeStamp

* format UT_*Cache

* update format

* fomat

* use hex extensions in Cryptography_Helper

* remove reflection

* optimization of UT_DataCache

* update namespace

* modify TestSha256

* update UT in crypto module

* Rename UT_Scrypt.cs to UT_SCrypt.cs

* format

* update UT_Murmur3
  • Loading branch information
eryeer authored and Tommo-L committed Jun 22, 2020
1 parent c47f2e3 commit 1554050
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
42 changes: 42 additions & 0 deletions neo.UnitTests/Cryptography/ECC/UT_ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void TestECPointConstructor()
point.X.Should().Be(X);
point.Y.Should().Be(Y);
point.Curve.Should().Be(ECCurve.Secp256k1);
Action action = () => new ECPoint(X, null, ECCurve.Secp256k1);
action.ShouldThrow<ArgumentException>();
action = () => new ECPoint(null, Y, ECCurve.Secp256k1);
action.ShouldThrow<ArgumentException>();
}

[TestMethod]
Expand Down Expand Up @@ -145,6 +149,30 @@ public void TestEquals()
point1.Equals(point3).Should().BeFalse();
}

[TestMethod]
public void TestEqualsObject()
{
object point = ECCurve.Secp256k1.G;
point.Equals(point).Should().BeTrue();
point.Equals(null).Should().BeFalse();
point.Equals(1u).Should().BeFalse();

point = new ECPoint(null, null, ECCurve.Secp256k1);
point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeTrue();
point.Equals(ECCurve.Secp256r1.G).Should().BeFalse();
ECCurve.Secp256r1.G.Equals(point).Should().BeFalse();

ECFieldElement X1 = new ECFieldElement(new BigInteger(100), ECCurve.Secp256k1);
ECFieldElement Y1 = new ECFieldElement(new BigInteger(200), ECCurve.Secp256k1);
ECFieldElement X2 = new ECFieldElement(new BigInteger(300), ECCurve.Secp256k1);
ECFieldElement Y2 = new ECFieldElement(new BigInteger(400), ECCurve.Secp256k1);
object point1 = new ECPoint(X1, Y1, ECCurve.Secp256k1);
object point2 = new ECPoint(X2, Y1, ECCurve.Secp256k1);
object point3 = new ECPoint(X1, Y2, ECCurve.Secp256k1);
point1.Equals(point2).Should().BeFalse();
point1.Equals(point3).Should().BeFalse();
}

[TestMethod]
public void TestFromBytes()
{
Expand Down Expand Up @@ -176,6 +204,13 @@ public void TestFromBytes()
ECCurve.Secp256k1), ECCurve.Secp256k1));
}

[TestMethod]
public void TestGetSize()
{
ECCurve.Secp256k1.G.Size.Should().Be(33);
ECCurve.Secp256k1.Infinity.Size.Should().Be(1);
}

[TestMethod]
public void TestMultiply()
{
Expand Down Expand Up @@ -279,6 +314,13 @@ public void TestOpMultiply()
new ECFieldElement(BigInteger.Parse("29236048674093813394523910922582374630829081423043497254162533033164154049666"), ECCurve.Secp256k1), ECCurve.Secp256k1));
}

[TestMethod]
public void TestOpSubtraction()
{
(ECCurve.Secp256k1.G - ECCurve.Secp256k1.Infinity).Should().Be(ECCurve.Secp256k1.G);
(ECCurve.Secp256k1.G - ECCurve.Secp256k1.G).Should().Be(ECCurve.Secp256k1.Infinity);
}

[TestMethod]
public void TestOpUnaryNegation()
{
Expand Down
80 changes: 80 additions & 0 deletions neo.UnitTests/Cryptography/UT_MerkleTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Neo.UnitTests.Cryptography
{
[TestClass]
public class UT_MerkleTree
{
public UInt256 GetByteArrayHash(byte[] byteArray)
{
if(byteArray == null || byteArray.Length == 0) throw new ArgumentNullException();
var hash = new UInt256(Crypto.Default.Hash256(byteArray));
return hash;
}

[TestMethod]
public void TestBuildAndDepthFirstSearch()
{
IReadOnlyList<UInt256> hashNull = new UInt256[] { };
Action action = () => new MerkleTree(hashNull);
action.ShouldThrow<ArgumentException>();

byte[] array1 = { 0x01 };
var hash1 = GetByteArrayHash(array1);

byte[] array2 = { 0x02 };
var hash2 = GetByteArrayHash(array2);

byte[] array3 = { 0x03 };
var hash3 = GetByteArrayHash(array3);

IReadOnlyList<UInt256> hashes = new UInt256[] { hash1, hash2, hash3 };
MerkleTree tree = new MerkleTree(hashes);
var hashArray = tree.ToHashArray();
hashArray[0].Should().Be(hash1);
hashArray[1].Should().Be(hash2);
hashArray[2].Should().Be(hash3);
hashArray[3].Should().Be(hash3);

var rootHash = MerkleTree.ComputeRoot(hashes);
var hash4 = Crypto.Default.Hash256(hash1.ToArray().Concat(hash2.ToArray()).ToArray());
var hash5 = Crypto.Default.Hash256(hash3.ToArray().Concat(hash3.ToArray()).ToArray());
var result = new UInt256(Crypto.Default.Hash256(hash4.ToArray().Concat(hash5.ToArray()).ToArray()));
rootHash.Should().Be(result);
}

[TestMethod]
public void TestTrim()
{
byte[] array1 = { 0x01 };
var hash1 = GetByteArrayHash(array1);

byte[] array2 = { 0x02 };
var hash2 = GetByteArrayHash(array2);

byte[] array3 = { 0x03 };
var hash3 = GetByteArrayHash(array3);

IReadOnlyList<UInt256> hashes = new UInt256[] { hash1, hash2, hash3 };
MerkleTree tree = new MerkleTree(hashes);

bool[] boolArray = { false, false, false };
BitArray bitArray = new BitArray(boolArray);
tree.Trim(bitArray);
var hashArray = tree.ToHashArray();

hashArray.Length.Should().Be(1);
var rootHash = MerkleTree.ComputeRoot(hashes);
var hash4 = Crypto.Default.Hash256(hash1.ToArray().Concat(hash2.ToArray()).ToArray());
var hash5 = Crypto.Default.Hash256(hash3.ToArray().Concat(hash3.ToArray()).ToArray());
var result = new UInt256(Crypto.Default.Hash256(hash4.ToArray().Concat(hash5.ToArray()).ToArray()));
hashArray[0].Should().Be(result);
}
}
}
51 changes: 51 additions & 0 deletions neo.UnitTests/Cryptography/UT_MerkleTreeNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using System.Text;

namespace Neo.UnitTests.Cryptography
{
[TestClass]
public class UT_MerkleTreeNode
{
private MerkleTreeNode node = new MerkleTreeNode();

[TestInitialize]
public void TestSetup()
{
node.Hash = null;
node.Parent = null;
node.LeftChild = null;
node.RightChild = null;
}

[TestMethod]
public void TestConstructor()
{
byte[] byteArray = Encoding.ASCII.GetBytes("hello world");
var hash = new UInt256(Crypto.Default.Hash256(byteArray));
node.Hash = hash;

node.Hash.Should().Be(hash);
node.Parent.Should().BeNull();
node.LeftChild.Should().BeNull();
node.RightChild.Should().BeNull();
}

[TestMethod]
public void TestGetIsLeaf()
{
node.IsLeaf.Should().BeTrue();

MerkleTreeNode child = new MerkleTreeNode();
node.LeftChild = child;
node.IsLeaf.Should().BeFalse();
}

[TestMethod]
public void TestGetIsRoot()
{
node.IsRoot.Should().BeTrue();
}
}
}
24 changes: 24 additions & 0 deletions neo.UnitTests/Cryptography/UT_Murmur3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;

namespace Neo.UnitTests.Cryptography
{
[TestClass]
public class UT_Murmur3
{
[TestMethod]
public void TestGetHashSize()
{
Murmur3 murmur3 = new Murmur3(1);
murmur3.HashSize.Should().Be(32);
}

[TestMethod]
public void TestHashCore()
{
byte[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };
array.Murmur32(10u).Should().Be(378574820u);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using System;

namespace Neo.UnitTests.Cryptography
{
[TestClass]
public class UT_Scrypt
public class UT_SCrypt
{
[TestMethod]
public void DeriveKeyTest()
Expand Down

0 comments on commit 1554050

Please sign in to comment.