Skip to content

Commit

Permalink
Increase coverage until 80% (neo-project#1823)
Browse files Browse the repository at this point in the history
* Increase coverage

* Add more UT

* More UT

* Increase coverage

* More UT

* FixUT

* Add UT and fixed TaskSession when no FullNodeCapability

* dotnet format

* Add UT

* Add UT

* More UT

* More UT

* Comparer UT

* Use Helper in in ApplicationEngine

* Increase coverage

* dotnet format

* Add message UT

* Increase UT

* Rename test

* dotnet format

* Add UT

* Dotnet format

* Add UT

* Add coverage

* Add UT

* Add UT

* Remove internal and add UT
  • Loading branch information
shargon authored Aug 9, 2020
1 parent 5844fd4 commit 70bc0a2
Show file tree
Hide file tree
Showing 48 changed files with 1,603 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/TaskSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TaskSession(VersionPayload version)
{
var fullNode = version.Capabilities.OfType<FullNodeCapability>().FirstOrDefault();
this.IsFullNode = fullNode != null;
this.LastBlockIndex = fullNode.StartHeight;
this.LastBlockIndex = fullNode?.StartHeight ?? 0;
}
}
}
4 changes: 2 additions & 2 deletions src/neo/Network/UPnP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void ForwardPort(int port, ProtocolType protocol, string descripti
{
if (string.IsNullOrEmpty(_serviceUrl))
throw new Exception("No UPnP service available or Discover() has not been called");
XmlDocument xdoc = SOAPRequest(_serviceUrl, "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
SOAPRequest(_serviceUrl, "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
"<NewRemoteHost></NewRemoteHost><NewExternalPort>" + port.ToString() + "</NewExternalPort><NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
"<NewInternalPort>" + port.ToString() + "</NewInternalPort><NewInternalClient>" + Dns.GetHostAddresses(Dns.GetHostName()).First(p => p.AddressFamily == AddressFamily.InterNetwork).ToString() +
"</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>" + description +
Expand All @@ -118,7 +118,7 @@ public static void DeleteForwardingRule(int port, ProtocolType protocol)
{
if (string.IsNullOrEmpty(_serviceUrl))
throw new Exception("No UPnP service available or Discover() has not been called");
XmlDocument xdoc = SOAPRequest(_serviceUrl,
SOAPRequest(_serviceUrl,
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
"<NewRemoteHost>" +
"</NewRemoteHost>" +
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ private class InvocationState
public long GasConsumed { get; private set; } = 0;
public long GasLeft => gas_amount - GasConsumed;
public Exception FaultException { get; private set; }
public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 CurrentScriptHash => CurrentContext?.GetScriptHash();
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
public UInt160 EntryScriptHash => EntryContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 EntryScriptHash => EntryContext?.GetScriptHash();
public IReadOnlyList<NotifyEventArgs> Notifications => notifications ?? (IReadOnlyList<NotifyEventArgs>)Array.Empty<NotifyEventArgs>();

protected ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas)
Expand Down
46 changes: 46 additions & 0 deletions tests/neo.UnitTests/Consensus/UT_ChangeViewPayloadCompact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Consensus;
using Neo.IO;
using Neo.Network.P2P.Payloads;

namespace Neo.UnitTests.Consensus
{
[TestClass]
public class UT_ChangeViewPayloadCompact
{
[TestMethod]
public void Size_Get()
{
var test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 1, InvocationScript = new byte[0], OriginalViewNumber = 1 };
((ISerializable)test).Size.Should().Be(12);

test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 1, InvocationScript = new byte[1024], OriginalViewNumber = 1 };
((ISerializable)test).Size.Should().Be(1038);
}

[TestMethod]
public void DeserializeAndSerialize()
{
var test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 2, InvocationScript = new byte[] { 1, 2, 3 }, OriginalViewNumber = 3 };
var clone = test.ToArray().AsSerializable<RecoveryMessage.ChangeViewPayloadCompact>();

Assert.AreEqual(test.Timestamp, clone.Timestamp);
Assert.AreEqual(test.ValidatorIndex, clone.ValidatorIndex);
Assert.AreEqual(test.OriginalViewNumber, clone.OriginalViewNumber);
CollectionAssert.AreEqual(test.InvocationScript, clone.InvocationScript);

clone = RecoveryMessage.ChangeViewPayloadCompact.FromPayload(new ConsensusPayload()
{
Data = new ChangeView() { Timestamp = 1, ViewNumber = 3 }.ToArray(),
ValidatorIndex = 2,
Witness = new Witness() { InvocationScript = new byte[] { 1, 2, 3 } }
});

Assert.AreEqual(test.Timestamp, clone.Timestamp);
Assert.AreEqual(test.ValidatorIndex, clone.ValidatorIndex);
Assert.AreEqual(test.OriginalViewNumber, clone.OriginalViewNumber);
CollectionAssert.AreEqual(test.InvocationScript, clone.InvocationScript);
}
}
}
1 change: 0 additions & 1 deletion tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Native;
using Neo.VM.Types;
using Neo.Wallets;
using System;
using System.Linq;
Expand Down
29 changes: 29 additions & 0 deletions tests/neo.UnitTests/Consensus/UT_RecoveryRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Consensus;
using Neo.IO;

namespace Neo.UnitTests.Consensus
{
[TestClass]
public class UT_RecoveryRequest
{
[TestMethod]
public void Size_Get()
{
var test = new RecoveryRequest() { Timestamp = 1, ViewNumber = 1 };
test.Size.Should().Be(10);
}

[TestMethod]
public void DeserializeAndSerialize()
{
var test = new RecoveryRequest() { ViewNumber = 1, Timestamp = 123 };
var clone = test.ToArray().AsSerializable<RecoveryRequest>();

Assert.AreEqual(test.Timestamp, clone.Timestamp);
Assert.AreEqual(test.Type, clone.Type);
Assert.AreEqual(test.ViewNumber, clone.ViewNumber);
}
}
}
29 changes: 29 additions & 0 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECDSA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using System;
using ECCurve = Neo.Cryptography.ECC.ECCurve;

namespace Neo.UnitTests.Cryptography.ECC
{
[TestClass]
public class UT_ECDSA
{
[TestMethod]
public void GenerateSignature()
{
var ecdsa = new ECDsa(ECCurve.Secp256k1.Infinity);
Assert.ThrowsException<InvalidOperationException>(() => ecdsa.GenerateSignature(new byte[0]));

var pk = new byte[32];
for (int x = 0; x < pk.Length; x++) pk[x] = (byte)x;

ecdsa = new ECDsa(pk, ECCurve.Secp256k1);
var sig = ecdsa.GenerateSignature(new byte[] { 1 });

Assert.IsTrue(ecdsa.VerifySignature(new byte[] { 1 }, sig[0], sig[1]));
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 2 }, sig[0], sig[1]));
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0] + 1, sig[1]));
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0], sig[1] + 1));
}
}
}
3 changes: 3 additions & 0 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public void TestECFieldElementConstructor()
input = ECCurve.Secp256k1.Q;
action = () => new ECFieldElement(input, ECCurve.Secp256k1);
action.Should().Throw<ArgumentException>();

action = () => new ECFieldElement(input, null);
action.Should().Throw<ArgumentNullException>();
}

[TestMethod]
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Neo.UnitTests.Cryptography.ECC
[TestClass]
public class UT_ECPoint
{
public static byte[] generatePrivateKey(int privateKeyLength)
public static byte[] GeneratePrivateKey(int privateKeyLength)
{
byte[] privateKey = new byte[privateKeyLength];
for (int i = 0; i < privateKeyLength; i++)
Expand Down Expand Up @@ -184,17 +184,17 @@ public void TestFromBytes()
ECPoint.FromBytes(input3, ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G);
ECPoint.FromBytes(input2.Skip(1).ToArray(), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G);

byte[] input4 = generatePrivateKey(72);
byte[] input4 = GeneratePrivateKey(72);
ECPoint.FromBytes(input4, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("3634473727541135791764834762056624681715094789735830699031648" +
"273128038409767"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("18165245710263168158644330920009617039772504630129940696140050972160274286151"),
ECCurve.Secp256k1), ECCurve.Secp256k1));

byte[] input5 = generatePrivateKey(96);
byte[] input5 = GeneratePrivateKey(96);
ECPoint.FromBytes(input5, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("1780731860627700044960722568376592200742329637303199754547598" +
"369979440671"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("14532552714582660066924456880521368950258152170031413196862950297402215317055"),
ECCurve.Secp256k1), ECCurve.Secp256k1));

byte[] input6 = generatePrivateKey(104);
byte[] input6 = GeneratePrivateKey(104);
ECPoint.FromBytes(input6, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("3634473727541135791764834762056624681715094789735830699031648" +
"273128038409767"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("18165245710263168158644330920009617039772504630129940696140050972160274286151"),
ECCurve.Secp256k1), ECCurve.Secp256k1));
Expand Down Expand Up @@ -231,17 +231,17 @@ public void TestMultiply()
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("16721517996619732311261078486295444964227498319433363271180755596201863690708"), ECCurve.Secp256k1),
ECCurve.Secp256k1));

k = new BigInteger(generatePrivateKey(100));
k = new BigInteger(GeneratePrivateKey(100));
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("19222995016448259376216431079553428738726180595337971417371897285865264889977"),
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6637081904924493791520919212064582313497884724460823966446023080706723904419"), ECCurve.Secp256k1),
ECCurve.Secp256k1));

k = new BigInteger(generatePrivateKey(120));
k = new BigInteger(GeneratePrivateKey(120));
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("79652345192111851576650978679091010173409410384772942769927955775006682639778"),
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6460429961979335115790346961011058418773289452368186110818621539624566803831"), ECCurve.Secp256k1),
ECCurve.Secp256k1));

k = new BigInteger(generatePrivateKey(300));
k = new BigInteger(GeneratePrivateKey(300));
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("105331914562708556186724786757483927866790351460145374033180496740107603569412"),
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("60523670886755698512704385951571322569877668383890769288780681319304421873758"), ECCurve.Secp256k1),
ECCurve.Secp256k1));
Expand Down
3 changes: 3 additions & 0 deletions tests/neo.UnitTests/Cryptography/MPT/UT_MPTTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public void TestGetProof()
Assert.IsTrue(proof.Contains(r.Encode()));
Assert.IsTrue(proof.Contains(l1.Encode()));
Assert.IsTrue(proof.Contains(v1.Encode()));

proof = mpt.GetProof(Array.Empty<byte>());
Assert.IsNull(proof);
}

[TestMethod]
Expand Down
5 changes: 4 additions & 1 deletion tests/neo.UnitTests/IO/Caching/UT_HashSetCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public void TestHashSetCache()
var bucket = new HashSetCache<int>(10);
for (int i = 1; i <= 100; i++)
{
bucket.Add(i);
Assert.IsTrue(bucket.Add(i));
Assert.IsFalse(bucket.Add(i));
}
bucket.Count.Should().Be(100);

Expand Down Expand Up @@ -131,6 +132,8 @@ public void TestExceptWith()
};
set.ExceptWith(new UInt256[] { b, c });
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a });
set.ExceptWith(new UInt256[] { a });
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { });

set = new HashSetCache<UInt256>(10)
{
Expand Down
45 changes: 45 additions & 0 deletions tests/neo.UnitTests/IO/UT_ByteArrayEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using System;
using System.Linq;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ByteArrayEqualityComparer
{
[TestMethod]
public void TestEqual()
{
var a = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
var b = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
var check = ByteArrayEqualityComparer.Default;

Assert.IsTrue(check.Equals(a, a));
Assert.IsTrue(check.Equals(a, b));
Assert.IsFalse(check.Equals(null, b));
Assert.IsFalse(check.Equals(a, null));
Assert.IsTrue(check.Equals(null, null));

Assert.IsFalse(check.Equals(a, new byte[] { 1, 2, 3 }));
Assert.IsTrue(check.Equals(Array.Empty<byte>(), Array.Empty<byte>()));

b[8]++;
Assert.IsFalse(check.Equals(a, b));
b[8]--;
b[0]--;
Assert.IsFalse(check.Equals(a, b));
}

[TestMethod]
public void TestGetHashCode()
{
var a = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
var b = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
var check = ByteArrayEqualityComparer.Default;

Assert.AreEqual(check.GetHashCode(a), check.GetHashCode(b));
Assert.AreNotEqual(check.GetHashCode(a), check.GetHashCode(b.Take(8).ToArray()));
}
}
}
37 changes: 37 additions & 0 deletions tests/neo.UnitTests/IO/UT_ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ReferenceEqualityComparer
{
private class FakeEquals : IEquatable<FakeEquals>
{
public bool Equals([AllowNull] FakeEquals other)
{
return true;
}

public override int GetHashCode()
{
return 123;
}
}

[TestMethod]
public void TestEqual()
{
var a = new FakeEquals();
var b = new FakeEquals();
var check = ReferenceEqualityComparer.Default;

Assert.IsFalse(check.Equals(a, b));
Assert.AreNotEqual(check.GetHashCode(a), check.GetHashCode(b));
Assert.AreNotEqual(123, check.GetHashCode(a));
Assert.AreNotEqual(123, check.GetHashCode(b));
}
}
}
10 changes: 10 additions & 0 deletions tests/neo.UnitTests/IO/UT_SerializableWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public void TestGetSize()
Assert.AreEqual(4, temp.Size);
}

[TestMethod]
public void TestCast()
{
SerializableWrapper<uint> tempA = (SerializableWrapper<uint>)123;
SerializableWrapper<uint> tempB = tempA.ToArray().AsSerializable<SerializableWrapper<uint>>();

Assert.IsTrue(tempA.Equals(tempB));
Assert.AreEqual((uint)123, (uint)tempA);
}

[TestMethod]
public void TestEqualsOtherObject()
{
Expand Down
48 changes: 48 additions & 0 deletions tests/neo.UnitTests/Ledger/UT_ContractIdState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using System;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_ContractIdState
{
[TestMethod]
public void Size_Get()
{
var test = new ContractIdState() { NextId = 1 };
((ISerializable)test).Size.Should().Be(4);

test = new ContractIdState() { NextId = int.MaxValue };
((ISerializable)test).Size.Should().Be(4);
}

[TestMethod]
public void Clone()
{
var test = new ContractIdState() { NextId = 1 };
var clone = ((ICloneable<ContractIdState>)test).Clone();

Assert.AreEqual(test.NextId, clone.NextId);

clone = new ContractIdState() { NextId = 2 };
((ICloneable<ContractIdState>)clone).FromReplica(test);

Assert.AreEqual(test.NextId, clone.NextId);
}

[TestMethod]
public void DeserializeAndSerialize()
{
var test = new ContractIdState() { NextId = int.MaxValue };
var clone = test.ToArray().AsSerializable<ContractIdState>();

Assert.AreEqual(test.NextId, clone.NextId);

test = new ContractIdState() { NextId = -1 };
Assert.ThrowsException<FormatException>(() => test.ToArray().AsSerializable<ContractIdState>());
}
}
}
Loading

0 comments on commit 70bc0a2

Please sign in to comment.