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 Legder module (#1038) #9

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
102 changes: 102 additions & 0 deletions neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;

namespace Neo.UnitTests.Ledger
{
internal class TestBlock : Block
{
public override bool Verify(Snapshot snapshot)
{
return true;
}

public static TestBlock Cast(Block input)
{
return input.ToArray().AsSerializable<TestBlock>();
}
}

internal class TestHeader : Header
{
public override bool Verify(Snapshot snapshot)
{
return true;
}

public static TestHeader Cast(Header input)
{
return input.ToArray().AsSerializable<TestHeader>();
}
}

[TestClass]
public class UT_Blockchain
{
private NeoSystem system;
private Store store;
Transaction txSample = Blockchain.GenesisBlock.Transactions[0];

[TestInitialize]
public void Initialize()
{
system = TestBlockchain.InitializeMockNeoSystem();
store = TestBlockchain.GetStore();
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
}

[TestMethod]
public void TestConstructor()
{
system.ActorSystem.ActorOf(Blockchain.Props(system, store)).Should().NotBeSameAs(system.Blockchain);
}

[TestMethod]
public void TestContainsBlock()
{
Blockchain.Singleton.ContainsBlock(UInt256.Zero).Should().BeFalse();
}

[TestMethod]
public void TestContainsTransaction()
{
Blockchain.Singleton.ContainsTransaction(UInt256.Zero).Should().BeFalse();
Blockchain.Singleton.ContainsTransaction(txSample.Hash).Should().BeTrue();
}

[TestMethod]
public void TestGetCurrentBlockHash()
{
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
}

[TestMethod]
public void TestGetCurrentHeaderHash()
{
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
}

[TestMethod]
public void TestGetBlock()
{
Blockchain.Singleton.GetBlock(UInt256.Zero).Should().BeNull();
}

[TestMethod]
public void TestGetBlockHash()
{
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
Blockchain.Singleton.GetBlockHash(10).Should().BeNull();
}

[TestMethod]
public void TestGetTransaction()
{
Blockchain.Singleton.GetTransaction(UInt256.Zero).Should().BeNull();
Blockchain.Singleton.GetTransaction(txSample.Hash).Should().NotBeNull();
}
}
}
99 changes: 99 additions & 0 deletions neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_ContractState
{
ContractState contract;
byte[] script = { 0x01 };
ContractManifest manifest;

[TestInitialize]
public void TestSetup()
{
manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"));
contract = new ContractState
{
Script = script,
Manifest = manifest
};
}

[TestMethod]
public void TestGetHasStorage()
{
contract.HasStorage.Should().BeFalse();
}

[TestMethod]
public void TestGetPayable()
{
contract.Payable.Should().BeFalse();
}

[TestMethod]
public void TestGetScriptHash()
{
// _scriptHash == null
contract.ScriptHash.Should().Be(script.ToScriptHash());
// _scriptHash != null
contract.ScriptHash.Should().Be(script.ToScriptHash());
}

[TestMethod]
public void TestClone()
{
ICloneable<ContractState> cloneable = contract;
ContractState clone = cloneable.Clone();
clone.ToJson().ToString().Should().Be(contract.ToJson().ToString());
}

[TestMethod]
public void TestFromReplica()
{
ICloneable<ContractState> cloneable = new ContractState();
cloneable.FromReplica(contract);
((ContractState)cloneable).ToJson().ToString().Should().Be(contract.ToJson().ToString());
}

[TestMethod]
public void TestDeserialize()
{
ISerializable newContract = new ContractState();
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)contract).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
newContract.Deserialize(reader);
}
((ContractState)newContract).Manifest.ToJson().ToString().Should().Be(contract.Manifest.ToJson().ToString());
((ContractState)newContract).Script.Should().BeEquivalentTo(contract.Script);
}

[TestMethod]
public void TestGetSize()
{
ISerializable newContract = contract;
newContract.Size.Should().Be(355);
}

[TestMethod]
public void TestToJson()
{
JObject json = contract.ToJson();
json["hash"].AsString().Should().Be("0x820944cfdc70976602d71b0091445eedbc661bc5");
json["script"].AsString().Should().Be("01");
json["manifest"].AsString().Should().Be(manifest.ToJson().AsString());
}
}
}
63 changes: 63 additions & 0 deletions neo.UnitTests/Ledger/UT_HashIndexState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_HashIndexState
{
HashIndexState origin;

[TestInitialize]
public void Initialize()
{
origin = new HashIndexState
{
Hash = UInt256.Zero,
Index = 10
};
}

[TestMethod]
public void TestClone()
{
HashIndexState dest = ((ICloneable<HashIndexState>)origin).Clone();
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}

[TestMethod]
public void TestFromReplica()
{
HashIndexState dest = new HashIndexState();
((ICloneable<HashIndexState>)dest).FromReplica(origin);
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}

[TestMethod]
public void TestGetSize()
{
((ISerializable)origin).Size.Should().Be(36);
}

[TestMethod]
public void TestDeserialize()
{
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)origin).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
HashIndexState dest = new HashIndexState();
((ISerializable)dest).Deserialize(reader);
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}
}
}
}
59 changes: 59 additions & 0 deletions neo.UnitTests/Ledger/UT_HeaderHashList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_HeaderHashList
{
HeaderHashList origin;

[TestInitialize]
public void Initialize()
{
origin = new HeaderHashList
{
Hashes = new UInt256[] { UInt256.Zero }
};
}

[TestMethod]
public void TestClone()
{
HeaderHashList dest = ((ICloneable<HeaderHashList>)origin).Clone();
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}

[TestMethod]
public void TestFromReplica()
{
HeaderHashList dest = new HeaderHashList();
((ICloneable<HeaderHashList>)dest).FromReplica(origin);
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}

[TestMethod]
public void TestDeserialize()
{
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)origin).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
HeaderHashList dest = new HeaderHashList();
((ISerializable)dest).Deserialize(reader);
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}
}

[TestMethod]
public void TestGetSize()
{
((ISerializable)origin).Size.Should().Be(33);
}
}
}
Loading