Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test for Smartcontract Module #1090

Merged
merged 34 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
602429b
submit ut
eryeer Aug 26, 2019
fc34a8d
fix
eryeer Aug 26, 2019
ed2c261
Merge remote-tracking branch 'upstream/master' into ut_smartcontract
eryeer Sep 9, 2019
094705d
Enhance functions in TestDataCache
eryeer Aug 30, 2019
c44ca6c
git amend blockchain
eryeer Sep 9, 2019
89de1d1
fix test related to TestDataCache
eryeer Sep 9, 2019
fa6e965
dotnet format
eryeer Sep 9, 2019
861a92d
dotnet format
eryeer Sep 9, 2019
5b5c261
add blank line
eryeer Sep 9, 2019
d4ef565
Merge branch 'master' into ut_smartcontract
eryeer Sep 12, 2019
ef58d48
fix test
eryeer Sep 16, 2019
b91f873
merge
eryeer Sep 16, 2019
d918c08
Merge branch 'master' into ut_smartcontract
eryeer Sep 17, 2019
33ae7dc
Optimize random
shargon Sep 17, 2019
d8774b9
Optimize Random
shargon Sep 17, 2019
abac84c
fix test
eryeer Sep 19, 2019
5db834e
add decimal test
eryeer Sep 20, 2019
9b4dc95
Merge branch 'master' into ut_smartcontract
eryeer Sep 23, 2019
e9630a5
Merge remote-tracking branch 'upstream/master' into ut_smartcontract
eryeer Sep 24, 2019
22325bc
fix
eryeer Sep 25, 2019
d5cb9d4
2019/9/25 16:54
doubiliu Sep 25, 2019
a6001f8
Merge branch 'master' into ut_smartcontract
vncoelho Sep 26, 2019
f6097e1
Fixes events
erikzhang Sep 26, 2019
ff83a9d
Merge branch 'master' into ut_smartcontract
eryeer Oct 8, 2019
8b2fea7
update assertion sentence
eryeer Oct 8, 2019
a88f04c
Merge branch 'master' into ut_smartcontract
eryeer Oct 16, 2019
ee49894
Merge branch 'master' into ut_smartcontract
eryeer Oct 17, 2019
71cbe33
Merge branch 'master' into ut_smartcontract
eryeer Oct 21, 2019
20ae481
update UT following code change
eryeer Oct 21, 2019
13da7ff
format
eryeer Oct 21, 2019
79f7f3a
add type check
eryeer Oct 22, 2019
f209dfe
recommit
eryeer Oct 22, 2019
4c765ac
recommit
eryeer Oct 22, 2019
3ba4229
Merge branch 'master' into ut_smartcontract
eryeer Oct 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.ShouldNotThrow<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());
eryeer marked this conversation as resolved.
Show resolved Hide resolved
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.ShouldNotThrow<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.ShouldNotThrow<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.ShouldNotThrow<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.ShouldNotThrow<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.ShouldNotThrow<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.ShouldThrow<InvalidOperationException>();
Action action2 = () => arrayWrapper.Value();
action2.ShouldThrow<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.ShouldNotThrow<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.ShouldNotThrow<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.ShouldNotThrow<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);
eryeer marked this conversation as resolved.
Show resolved Hide resolved
ContractGroup contractGroup = new ContractGroup
{
PubKey = keyPair.PublicKey,
Signature = new byte[20]
};
Assert.AreEqual(false, contractGroup.IsValid(UInt160.Zero));
eryeer marked this conversation as resolved.
Show resolved Hide resolved


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