Skip to content

Commit

Permalink
Unit Test for Wallets Module (neo-project#1018)
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

* update IO module test

* delete empty line

* changename

* delete empty line

* revert commit

* add wallet test

* update testUtil

* delete empty line

* Update UT_NEP6Wallet.cs

* update ut

* Optimize wallet test

* fix

* add change

* fix nep6wallet

* Fix

* Optimize

* fix optimize

* fix

* Optimize
  • Loading branch information
eryeer authored and Tommo-L committed Jun 22, 2020
1 parent aa15e37 commit 4b3c1f2
Show file tree
Hide file tree
Showing 17 changed files with 1,669 additions and 40 deletions.
32 changes: 10 additions & 22 deletions neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,12 @@ public void Size_Get()
uut.Size.Should().Be(83);
}

private NEP6Wallet GenerateTestWallet()
{
JObject wallet = new JObject();
wallet["name"] = "noname";
wallet["version"] = new System.Version().ToString();
wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson();
wallet["accounts"] = new JArray();
wallet["extra"] = null;
wallet.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"0.0\",\"scrypt\":{\"n\":0,\"r\":0,\"p\":0},\"accounts\":[],\"extra\":null}");
return new NEP6Wallet(wallet);
}

[TestMethod]
public void FeeIsMultiSigContract()
{
var store = TestBlockchain.GetStore();
var walletA = GenerateTestWallet();
var walletB = GenerateTestWallet();
var walletA = TestUtils.GenerateTestWallet();
var walletB = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

using (var unlockA = walletA.Unlock("123"))
Expand Down Expand Up @@ -188,7 +176,7 @@ public void FeeIsMultiSigContract()
[TestMethod]
public void FeeIsSignatureContractDetailed()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

using (var unlock = wallet.Unlock("123"))
Expand Down Expand Up @@ -303,7 +291,7 @@ public void FeeIsSignatureContractDetailed()
[TestMethod]
public void FeeIsSignatureContract_TestScope_Global()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -394,7 +382,7 @@ public void FeeIsSignatureContract_TestScope_Global()
[TestMethod]
public void FeeIsSignatureContract_TestScope_CurrentHash_GAS()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -486,7 +474,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS()
[TestMethod]
public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -581,7 +569,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS()
[TestMethod]
public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -639,7 +627,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT()
[TestMethod]
public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -736,7 +724,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS()
[TestMethod]
public void FeeIsSignatureContract_TestScope_NoScopeFAULT()
{
var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down Expand Up @@ -966,7 +954,7 @@ public void FeeIsSignatureContract_TestScope_Global_Default()
Cosigner cosigner = new Cosigner();
cosigner.Scopes.Should().Be(WitnessScope.Global);

var wallet = GenerateTestWallet();
var wallet = TestUtils.GenerateTestWallet();
var snapshot = store.GetSnapshot();

// no password on this wallet
Expand Down
38 changes: 36 additions & 2 deletions neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Neo.IO;
using FluentAssertions;
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Manifest;
using Neo.VM;
using Neo.Wallets.NEP6;
using System;
using System.IO;

Expand All @@ -21,6 +26,18 @@ public static byte[] GetByteArray(int length, byte firstByte)
return array;
}

public static NEP6Wallet GenerateTestWallet()
{
JObject wallet = new JObject();
wallet["name"] = "noname";
wallet["version"] = new System.Version().ToString();
wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson();
wallet["accounts"] = new JArray();
wallet["extra"] = null;
wallet.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"0.0\",\"scrypt\":{\"n\":0,\"r\":0,\"p\":0},\"accounts\":[],\"extra\":null}");
return new NEP6Wallet(wallet);
}

public static Transaction GetTransaction()
{
return new Transaction
Expand All @@ -37,6 +54,15 @@ public static Transaction GetTransaction()
};
}

internal static ContractState GetContract()
{
return new ContractState
{
Script = new byte[] { 0x01, 0x01, 0x01, 0x01 },
Manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"))
};
}

public static void SetupHeaderWithValues(Header header, UInt256 val256, out UInt256 merkRootVal, out UInt160 val160, out ulong timestampVal, out uint indexVal, out Witness scriptVal)
{
setupBlockBaseWithValues(header, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal);
Expand Down Expand Up @@ -109,5 +135,13 @@ public static T CopyMsgBySerialization<T>(T serializableObj, T newObj) where T :

return newObj;
}

public static void DeleteFile(string file)
{
if (File.Exists(file))
{
File.Delete(file);
}
}
}
}
}
139 changes: 139 additions & 0 deletions neo.UnitTests/Wallets/NEP6/UT_NEP6Account.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.Wallets;
using Neo.Wallets.NEP6;

namespace Neo.UnitTests.Wallets.NEP6
{
[TestClass]
public class UT_NEP6Account
{
NEP6Account account;
UInt160 hash;
NEP6Wallet wallet;
private static string nep2;
private static KeyPair keyPair;

[ClassInitialize]
public static void ClassSetup(TestContext context) {
byte[] privateKey = { 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
keyPair = new KeyPair(privateKey);
nep2 = keyPair.Export("Satoshi", 0, 0, 0);
}

[TestInitialize]
public void TestSetup()
{
wallet = TestUtils.GenerateTestWallet();
byte[] array1 = { 0x01 };
hash = new UInt160(Crypto.Default.Hash160(array1));
account = new NEP6Account(wallet, hash);
}

[TestMethod]
public void TestConstructorWithNep2Key()
{
account.ScriptHash.Should().Be(hash);
account.Decrypted.Should().BeTrue();
account.HasKey.Should().BeFalse();
}

[TestMethod]
public void TestConstructorWithKeyPair()
{
NEP6Wallet wallet = new NEP6Wallet("a");
byte[] array1 = { 0x01 };
var hash = new UInt160(Crypto.Default.Hash160(array1));
string password = "hello world";
NEP6Account account = new NEP6Account(wallet, hash, keyPair, password);
account.ScriptHash.Should().Be(hash);
account.Decrypted.Should().BeTrue();
account.HasKey.Should().BeTrue();
}

[TestMethod]
public void TestFromJson()
{
JObject json = new JObject();
json["address"] = "ARxgjcH2K1yeW5f5ryuRQNaBzSa9TZzmVS";
json["key"] = null;
json["label"] = null;
json["isDefault"] = true;
json["lock"] = false;
json["contract"] = null;
json["extra"] = null;
NEP6Account account = NEP6Account.FromJson(json, wallet);
account.ScriptHash.Should().Be("ARxgjcH2K1yeW5f5ryuRQNaBzSa9TZzmVS".ToScriptHash());
account.Label.Should().BeNull();
account.IsDefault.Should().BeTrue();
account.Lock.Should().BeFalse();
account.Contract.Should().BeNull();
account.Extra.Should().BeNull();
account.GetKey().Should().BeNull();

json["key"] = "6PYRjVE1gAbCRyv81FTiFz62cxuPGw91vMjN4yPa68bnoqJtioreTznezn";
json["label"] = "label";
account = NEP6Account.FromJson(json, wallet);
account.Label.Should().Be("label");
account.HasKey.Should().BeTrue();
}

[TestMethod]
public void TestGetKey()
{
account.GetKey().Should().BeNull();
wallet.Unlock("Satoshi");
account = new NEP6Account(wallet, hash, nep2);
account.GetKey().Should().Be(keyPair);
}

[TestMethod]
public void TestGetKeyWithString()
{
account.GetKey("Satoshi").Should().BeNull();
account = new NEP6Account(wallet, hash, nep2);
account.GetKey("Satoshi").Should().Be(keyPair);
}

[TestMethod]
public void TestToJson()
{
JObject nep6contract = new JObject();
nep6contract["script"] = "2103603f3880eb7aea0ad4500893925e4a42fea48a44ee6f898a10b3c7ce05d2a267ac";
JObject parameters = new JObject();
parameters["type"] = 0x00;
parameters["name"] = "Sig";
JArray array = new JArray
{
parameters
};
nep6contract["parameters"] = array;
nep6contract["deployed"] = false;
account.Contract = NEP6Contract.FromJson(nep6contract);
JObject json = account.ToJson();
json["address"].Should().Equals("AZk5bAanTtD6AvpeesmYgL8CLRYUt5JQsX");
json["label"].Should().BeNull();
json["isDefault"].ToString().Should().Be("false");
json["lock"].ToString().Should().Be("false");
json["key"].Should().BeNull();
json["contract"]["script"].ToString().Should().Be("\"2103603f3880eb7aea0ad4500893925e4a42fea48a44ee6f898a10b3c7ce05d2a267ac\"");
json["extra"].Should().BeNull();

account.Contract = null;
json = account.ToJson();
json["contract"].Should().BeNull();
}

[TestMethod]
public void TestVerifyPassword()
{
account = new NEP6Account(wallet, hash, nep2);
account.VerifyPassword("Satoshi").Should().BeTrue();
account.VerifyPassword("b").Should().BeFalse();
}
}
}
67 changes: 67 additions & 0 deletions neo.UnitTests/Wallets/NEP6/UT_NEP6Contract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.Wallets.NEP6;

namespace Neo.UnitTests.Wallets.NEP6
{
[TestClass]
public class UT_NEP6Contract
{
[TestMethod]
public void TestFromNullJson()
{
NEP6Contract nep6Contract = NEP6Contract.FromJson(null);
nep6Contract.Should().BeNull();
}

[TestMethod]
public void TestFromJson()
{
string json = "{\"script\":\"2103ef891df4c0b7eefb937d21ea0fb88cde8e0d82a7ff11872b5e7047969dafb4eb68747476aa\"," +
"\"parameters\":[{\"name\":\"signature\",\"type\":\"Signature\"}],\"deployed\":false}";
JObject @object = JObject.Parse(json);

NEP6Contract nep6Contract = NEP6Contract.FromJson(@object);
nep6Contract.Script.Should().BeEquivalentTo("2103ef891df4c0b7eefb937d21ea0fb88cde8e0d82a7ff11872b5e7047969dafb4eb68747476aa".HexToBytes());
nep6Contract.ParameterList.Length.Should().Be(1);
nep6Contract.ParameterList[0].Should().Be(ContractParameterType.Signature);
nep6Contract.ParameterNames.Length.Should().Be(1);
nep6Contract.ParameterNames[0].Should().Be("signature");
nep6Contract.Deployed.Should().BeFalse();
}

[TestMethod]
public void TestToJson()
{
NEP6Contract nep6Contract = new NEP6Contract()
{
Script = new byte[] { 0x00, 0x01 },
ParameterList = new ContractParameterType[] { ContractParameterType.Boolean, ContractParameterType.Integer },
ParameterNames = new string[] { "param1", "param2" },
Deployed = false
};

JObject @object = nep6Contract.ToJson();
JString jString = (JString)@object["script"];
jString.Value.Should().Be(nep6Contract.Script.ToHexString());

JBoolean jBoolean = (JBoolean)@object["deployed"];
jBoolean.Value.Should().BeFalse();

JArray parameters = (JArray)@object["parameters"];
parameters.Count.Should().Be(2);

jString = (JString)(parameters[0]["name"]);
jString.Value.Should().Be("param1");
jString = (JString)(parameters[0]["type"]);
jString.Value.Should().Be(ContractParameterType.Boolean.ToString());

jString = (JString)(parameters[1]["name"]);
jString.Value.Should().Be("param2");
jString = (JString)(parameters[1]["type"]);
jString.Value.Should().Be(ContractParameterType.Integer.ToString());
}
}
}
Loading

0 comments on commit 4b3c1f2

Please sign in to comment.