Skip to content

Commit

Permalink
Unit Test for Smartcontract Module (#1090)
Browse files Browse the repository at this point in the history
* submit ut

* fix

* Enhance functions in TestDataCache

* git amend blockchain

* fix test related to TestDataCache

* dotnet format

* dotnet format

* add blank line

* fix test

* Optimize random

* Optimize Random

* fix test

* add decimal test

* fix

* 2019/9/25 16:54

change format

* Fixes events

* update assertion sentence

* update UT following code change

* format

* add type check

* recommit

* recommit
  • Loading branch information
eryeer authored and vncoelho committed Oct 23, 2019
1 parent e53f533 commit 0ed69cd
Show file tree
Hide file tree
Showing 32 changed files with 3,620 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Enumerators;
using Neo.SmartContract.Iterators;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Enumerators
{
[TestClass]
public class UT_ConcatenatedEnumerator
{
[TestMethod]
public void TestConcatenatedIteratorAndDispose()
{
List<StackItem> list1 = new List<StackItem>();
StackItem stackItem1 = new Integer(0);
list1.Add(stackItem1);
List<StackItem> list2 = new List<StackItem>();
StackItem stackItem2 = new Integer(0);
list2.Add(stackItem2);
ArrayWrapper arrayWrapper1 = new ArrayWrapper(list1);
ArrayWrapper arrayWrapper2 = new ArrayWrapper(list2);
IteratorKeysWrapper it1 = new IteratorKeysWrapper(arrayWrapper1);
IteratorKeysWrapper it2 = new IteratorKeysWrapper(arrayWrapper2);
ConcatenatedEnumerator uut = new ConcatenatedEnumerator(it1, it2);
Assert.IsNotNull(uut);
Action action = () => uut.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestNextAndValue()
{
List<StackItem> list1 = new List<StackItem>();
StackItem stackItem1 = new Integer(1);
list1.Add(stackItem1);
List<StackItem> list2 = new List<StackItem>();
StackItem stackItem2 = new Integer(0);
list2.Add(stackItem2);
ArrayWrapper arrayWrapper1 = new ArrayWrapper(list1);
ArrayWrapper arrayWrapper2 = new ArrayWrapper(list2);
IteratorKeysWrapper it1 = new IteratorKeysWrapper(arrayWrapper1);
IteratorKeysWrapper it2 = new IteratorKeysWrapper(arrayWrapper2);
ConcatenatedEnumerator uut = new ConcatenatedEnumerator(it1, it2);
Assert.AreEqual(true, uut.Next());
Assert.AreEqual(new Integer(0), uut.Value());
Assert.AreEqual(true, uut.Next());
Assert.AreEqual(new Integer(0), uut.Value());
Assert.AreEqual(false, uut.Next());
}
}
}
36 changes: 36 additions & 0 deletions neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Enumerators;
using Neo.SmartContract.Iterators;
using Neo.VM;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Enumerators
{
[TestClass]
public class UT_IteratorKeysWrapper
{
[TestMethod]
public void TestGeneratorAndDispose()
{
IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(new ArrayWrapper(new List<StackItem>()));
Assert.IsNotNull(iteratorKeysWrapper);
Action action = () => iteratorKeysWrapper.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestNextAndValue()
{
StackItem stackItem = new VM.Types.Boolean(true);
List<StackItem> list = new List<StackItem>();
list.Add(stackItem);
ArrayWrapper wrapper = new ArrayWrapper(list);
IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(wrapper);
Action action = () => iteratorKeysWrapper.Next();
action.Should().NotThrow<Exception>();
Assert.AreEqual(new VM.Types.Integer(0), iteratorKeysWrapper.Value());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Enumerators;
using Neo.SmartContract.Iterators;
using Neo.VM;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Enumerators
{

[TestClass]
public class UT_IteratorValuesWrapper
{
[TestMethod]
public void TestGeneratorAndDispose()
{
IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(new ArrayWrapper(new List<StackItem>()));
Assert.IsNotNull(iteratorValuesWrapper);
Action action = () => iteratorValuesWrapper.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestNextAndValue()
{
StackItem stackItem = new VM.Types.Boolean(true);
List<StackItem> list = new List<StackItem>();
list.Add(stackItem);
ArrayWrapper wrapper = new ArrayWrapper(list);
IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(wrapper);
Action action = () => iteratorValuesWrapper.Next();
action.Should().NotThrow<Exception>();
Assert.AreEqual(stackItem, iteratorValuesWrapper.Value());
}
}
}
50 changes: 50 additions & 0 deletions neo.UnitTests/SmartContract/Iterators/UT_ArrayWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Iterators;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Iterators
{
[TestClass]
public class UT_ArrayWrapper
{
[TestMethod]
public void TestGeneratorAndDispose()
{
ArrayWrapper arrayWrapper = new ArrayWrapper(new List<StackItem>());
Assert.IsNotNull(arrayWrapper);
Action action = () => arrayWrapper.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestKeyAndValue()
{
List<StackItem> list = new List<StackItem>();
StackItem stackItem = new Integer(0);
list.Add(stackItem);
ArrayWrapper arrayWrapper = new ArrayWrapper(list);
Action action1 = () => arrayWrapper.Key();
action1.Should().Throw<InvalidOperationException>();
Action action2 = () => arrayWrapper.Value();
action2.Should().Throw<InvalidOperationException>();
arrayWrapper.Next();
Assert.AreEqual(stackItem, arrayWrapper.Key());
Assert.AreEqual(stackItem, arrayWrapper.Value());
}

[TestMethod]
public void TestNext()
{
List<StackItem> list = new List<StackItem>();
ArrayWrapper arrayWrapper = new ArrayWrapper(list);
Assert.AreEqual(false, arrayWrapper.Next());
StackItem stackItem = new Integer(0);
list.Add(stackItem);
Assert.AreEqual(true, arrayWrapper.Next());
}
}
}
14 changes: 13 additions & 1 deletion neo.UnitTests/SmartContract/Iterators/UT_ConcatenatedIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Iterators;
using Neo.VM.Types;
using System;
using System.Numerics;

namespace Neo.UnitTests.SmartContract.Iterators
{

[TestClass]
public class UT_ConcatenatedIterator
{
Expand Down Expand Up @@ -65,5 +65,17 @@ private Integer MakeIntegerStackItem(int val)
{
return new Integer(new BigInteger(val));
}

[TestMethod]
public void TestDispose()
{
Integer[] array1 = { MakeIntegerStackItem(1), MakeIntegerStackItem(7), MakeIntegerStackItem(23) };
Integer[] array2 = { MakeIntegerStackItem(8), MakeIntegerStackItem(47) };
ArrayWrapper it1 = new ArrayWrapper(array1);
ArrayWrapper it2 = new ArrayWrapper(array2);
ConcatenatedIterator uut = new ConcatenatedIterator(it1, it2);
Action action = () => uut.Dispose();
action.Should().NotThrow<Exception>();
}
}
}
43 changes: 43 additions & 0 deletions neo.UnitTests/SmartContract/Iterators/UT_MapWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Iterators;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Iterators
{
[TestClass]
public class UT_MapWrapper
{
[TestMethod]
public void TestGeneratorAndDispose()
{
MapWrapper mapWrapper = new MapWrapper(new List<KeyValuePair<StackItem, StackItem>>());
Assert.IsNotNull(mapWrapper);
Action action = () => mapWrapper.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestKeyAndValue()
{
List<KeyValuePair<StackItem, StackItem>> list = new List<KeyValuePair<StackItem, StackItem>>();
StackItem stackItem1 = new Integer(0);
StackItem stackItem2 = new Integer(1);
list.Add(new KeyValuePair<StackItem, StackItem>(stackItem1, stackItem2));
MapWrapper mapWrapper = new MapWrapper(list);
mapWrapper.Next();
Assert.AreEqual(stackItem1, mapWrapper.Key());
Assert.AreEqual(stackItem2, mapWrapper.Value());
}

[TestMethod]
public void TestNext()
{
MapWrapper mapWrapper = new MapWrapper(new List<KeyValuePair<StackItem, StackItem>>());
Assert.AreEqual(false, mapWrapper.Next());
}
}
}
38 changes: 38 additions & 0 deletions neo.UnitTests/SmartContract/Iterators/UT_StorageIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Ledger;
using Neo.SmartContract.Iterators;
using Neo.VM.Types;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Iterators
{
[TestClass]
public class UT_StorageIterator
{
[TestMethod]
public void TestGeneratorAndDispose()
{
StorageIterator storageIterator = new StorageIterator(new List<KeyValuePair<StorageKey, StorageItem>>().GetEnumerator());
Assert.IsNotNull(storageIterator);
Action action = () => storageIterator.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestKeyAndValueAndNext()
{
List<KeyValuePair<StorageKey, StorageItem>> list = new List<KeyValuePair<StorageKey, StorageItem>>();
StorageKey storageKey = new StorageKey();
storageKey.Key = new byte[1];
StorageItem storageItem = new StorageItem();
storageItem.Value = new byte[1];
list.Add(new KeyValuePair<StorageKey, StorageItem>(storageKey, storageItem));
StorageIterator storageIterator = new StorageIterator(list.GetEnumerator());
storageIterator.Next();
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Key());
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Value());
}
}
}
22 changes: 22 additions & 0 deletions neo.UnitTests/SmartContract/Manifest/UT_ContractEventDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Manifest;

namespace Neo.UnitTests.SmartContract.Manifest
{
[TestClass]
public class UT_ContractEventDescriptor
{
[TestMethod]
public void TestFromJson()
{
ContractEventDescriptor expected = new ContractEventDescriptor
{
Name = "AAA",
Parameters = new ContractParameterDefinition[0]
};
ContractEventDescriptor actual = ContractEventDescriptor.FromJson(expected.ToJson());
Assert.AreEqual(expected.Name, actual.Name);
Assert.AreEqual(0, actual.Parameters.Length);
}
}
}
42 changes: 42 additions & 0 deletions neo.UnitTests/SmartContract/Manifest/UT_ContractGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.SmartContract.Manifest;
using Neo.Wallets;
using System;
using System.Linq;

namespace Neo.UnitTests.SmartContract.Manifest
{
[TestClass]
public class UT_ContractGroup
{
[TestMethod]
public void TestIsValid()
{
Random random = new Random();
byte[] privateKey = new byte[32];
random.NextBytes(privateKey);
KeyPair keyPair = new KeyPair(privateKey);
ContractGroup contractGroup = new ContractGroup
{
PubKey = keyPair.PublicKey,
Signature = new byte[20]
};
Assert.AreEqual(false, contractGroup.IsValid(UInt160.Zero));


byte[] message = new byte[] { 0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01 };
byte[] signature = Crypto.Default.Sign(message, keyPair.PrivateKey, keyPair.PublicKey.EncodePoint(false).Skip(1).ToArray());
contractGroup = new ContractGroup
{
PubKey = keyPair.PublicKey,
Signature = signature
};
Assert.AreEqual(true, contractGroup.IsValid(new UInt160(message)));
}
}
}
Loading

0 comments on commit 0ed69cd

Please sign in to comment.