Skip to content

Commit

Permalink
Core unit tests 2 (neo-project#36)
Browse files Browse the repository at this point in the history
* Recreate unit tests on new branch, after source repo rebranding and renaming, faster than fixing a load of merge conflicts

* finish testing CalculateNetFee in block.cs. Add some dummy classes to help test. Normalise the namespaces and remove unnecessary usings.

* improve the culture testing of AssetState.cs GetName

* Add a general unit test that will run all other unit tests, in a variety of cultures

* Deserialize and Serialize unit tests for block.cs

* fix tests so that daylight savings dont affect expected result

* Block.cs tests for Equals() and Trim() and FromTrimmedData()

* Update README with NEO and correct folder

* test for RebuildMerkleRoot()

* tests for tojson() and verify() when completely is false

* test for block verify fail when consensus doesn't match

* fix failing test from merge conflicts
  • Loading branch information
AshRolls authored and Erik Zhang committed Jul 26, 2017
1 parent ce1c77a commit 12a6209
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 78 deletions.
6 changes: 3 additions & 3 deletions neo.UnitTests/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AntShares Unit Tests
NEO Unit Tests
====================

This project is a work in progress, aiming to provide unit test coverage for the core AntShares code.
This project is a work in progress, aiming to provide unit test coverage for the core NEO code.

Please note that we are aware that we are not using proper isolation / dependency injection / mocking techniques in these tests. To do that would require larger reworks of the base code which is a change for a later date in discussion with the core team, at the moment we are just aiming to get some basic coverage going.

Expand All @@ -12,7 +12,7 @@ We use built in Visual Studio functionality with MSTest and the Microsoft.Visual

To run the tests, build the solution to discover tests, then view and run the tests from the 'Test Explorer' window within Visual Studio.
OR
With .NET Core SDK installed, use the CLI to navigate to the src\AntShares.UnitTest and use the command 'dotnet restore' to get packages, followed by 'dotnet test' to run tests.
With .NET Core SDK installed, use the CLI to navigate to the neo.UnitTest folder and use the command 'dotnet restore' to get packages, followed by 'dotnet test' to run tests.

Coverage
====================
Expand Down
146 changes: 146 additions & 0 deletions neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using Neo.Core;
using System;
using System.Collections.Generic;
using Neo.Cryptography.ECC;

namespace Neo.UnitTests
{
public class TestBlockchain : Blockchain
{
private UInt256 _assetId;

public TestBlockchain(UInt256 assetId)
{
_assetId = assetId;
}

public override UInt256 CurrentBlockHash => throw new NotImplementedException();

public override UInt256 CurrentHeaderHash => throw new NotImplementedException();

public override uint HeaderHeight => throw new NotImplementedException();

public override uint Height => throw new NotImplementedException();

public override bool AddBlock(Block block)
{
throw new NotImplementedException();
}

public override bool ContainsBlock(UInt256 hash)
{
return true; // for verify in UT_Block
}

public override bool ContainsTransaction(UInt256 hash)
{
throw new NotImplementedException();
}

public override bool ContainsUnspent(UInt256 hash, ushort index)
{
throw new NotImplementedException();
}

public override void Dispose()
{
// do nothing
}

public override AccountState GetAccountState(UInt160 script_hash)
{
throw new NotImplementedException();
}

public override AssetState GetAssetState(UInt256 asset_id)
{
throw new NotImplementedException();
}

public override Block GetBlock(UInt256 hash)
{
throw new NotImplementedException();
}

public override UInt256 GetBlockHash(uint height)
{
throw new NotImplementedException();
}

public override ContractState GetContract(UInt160 hash)
{
throw new NotImplementedException();
}

public override IEnumerable<ValidatorState> GetEnrollments()
{
ECPoint ecp = TestUtils.StandbyValidators[0];
return new ValidatorState[] { new ValidatorState() { PublicKey = ecp } };
}

public override Header GetHeader(uint height)
{
throw new NotImplementedException();
}

public override Header GetHeader(UInt256 hash)
{
throw new NotImplementedException();
}

public override Block GetNextBlock(UInt256 hash)
{
throw new NotImplementedException();
}

public override UInt256 GetNextBlockHash(UInt256 hash)
{
throw new NotImplementedException();
}

public override StorageItem GetStorageItem(StorageKey key)
{
throw new NotImplementedException();
}

public override long GetSysFeeAmount(UInt256 hash)
{
throw new NotImplementedException();
}

public override Transaction GetTransaction(UInt256 hash, out int height)
{
height = 0;
return new TestTransaction(_assetId, TransactionType.ClaimTransaction);
}

public override Dictionary<ushort, SpentCoin> GetUnclaimed(UInt256 hash)
{
throw new NotImplementedException();
}

public override TransactionOutput GetUnspent(UInt256 hash, ushort index)
{
throw new NotImplementedException();
}

public override IEnumerable<VoteState> GetVotes(IEnumerable<Transaction> others)
{
VoteState vs = new VoteState() { Count = Fixed8.FromDecimal(1), PublicKeys = TestUtils.StandbyValidators};
return new VoteState[]
{
vs
};
}

public override bool IsDoubleSpend(Transaction tx)
{
throw new NotImplementedException();
}

protected override void AddHeaders(IEnumerable<Header> headers)
{
throw new NotImplementedException();
}
}
}
18 changes: 18 additions & 0 deletions neo.UnitTests/TestTransaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Neo.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace Neo.UnitTests
{
public class TestTransaction : Transaction
{
public TestTransaction(UInt256 assetId, TransactionType type) : base(type)
{
TransactionOutput transVal = new TransactionOutput();
transVal.Value = Fixed8.FromDecimal(50);
transVal.AssetId = assetId;
base.Outputs = new TransactionOutput[1] { transVal };
}
}
}
12 changes: 5 additions & 7 deletions neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Neo.Cryptography.ECC;

namespace Neo.UnitTest
namespace Neo.UnitTests
{
public static class TestUtils
{
Expand All @@ -18,5 +14,7 @@ public static byte[] GetByteArray(int length, byte firstByte)
}
return array;
}
}

public static readonly ECPoint[] StandbyValidators = new ECPoint[] { ECPoint.DecodePoint("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c".HexToBytes(), ECCurve.Secp256r1) };
}
}
1 change: 0 additions & 1 deletion neo.UnitTests/UT_AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Text;
using Neo.Core;
using Neo.UnitTest;

namespace Neo.UnitTests
{
Expand Down
9 changes: 1 addition & 8 deletions neo.UnitTests/UT_AssetState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
using Neo.Cryptography.ECC;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Neo.UnitTest
namespace Neo.UnitTests
{
[TestClass]
public class UT_AssetState
Expand Down Expand Up @@ -215,7 +211,6 @@ private void setupAssetStateWithValues(AssetState assetState, out UInt256 assetI
{
assetId = new UInt256(TestUtils.GetByteArray(32, 0x20));
assetState.AssetId = assetId;

assetType = AssetType.Token;
assetState.AssetType = assetType;

Expand Down Expand Up @@ -347,7 +342,6 @@ public void Deserialize()
bool isFrozen;
setupAssetStateWithValues(new AssetState(), out assetId, out assetType, out name, out amount, out available, out precision, out fee, out feeAddress, out owner, out admin, out issuer, out expiration, out isFrozen);


byte[] data = new byte[] { 0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 96, 3, 110, 101, 111, 42, 0, 0, 0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 66, 0, 44, 0, 0, 0, 0, 0, 0, 0, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 34, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 42, 0, 0, 0, 1 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
Expand Down Expand Up @@ -399,7 +393,6 @@ public void Serialize()
}

byte[] requiredData = new byte[] { 0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 96, 3, 110, 101, 111, 42, 0, 0, 0, 0, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 66, 0, 44, 0, 0, 0, 0, 0, 0, 0, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 34, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 42, 0, 0, 0, 1 };

data.Length.Should().Be(130);
for (int i = 0; i < 130; i++)
{
Expand Down
Loading

0 comments on commit 12a6209

Please sign in to comment.