Skip to content

Commit 70bc0a2

Browse files
authored
Increase coverage until 80% (neo-project#1823)
* 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
1 parent 5844fd4 commit 70bc0a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1603
-25
lines changed

src/neo/Network/P2P/TaskSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public TaskSession(VersionPayload version)
2020
{
2121
var fullNode = version.Capabilities.OfType<FullNodeCapability>().FirstOrDefault();
2222
this.IsFullNode = fullNode != null;
23-
this.LastBlockIndex = fullNode.StartHeight;
23+
this.LastBlockIndex = fullNode?.StartHeight ?? 0;
2424
}
2525
}
2626
}

src/neo/Network/UPnP.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static void ForwardPort(int port, ProtocolType protocol, string descripti
107107
{
108108
if (string.IsNullOrEmpty(_serviceUrl))
109109
throw new Exception("No UPnP service available or Discover() has not been called");
110-
XmlDocument xdoc = SOAPRequest(_serviceUrl, "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
110+
SOAPRequest(_serviceUrl, "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
111111
"<NewRemoteHost></NewRemoteHost><NewExternalPort>" + port.ToString() + "</NewExternalPort><NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
112112
"<NewInternalPort>" + port.ToString() + "</NewInternalPort><NewInternalClient>" + Dns.GetHostAddresses(Dns.GetHostName()).First(p => p.AddressFamily == AddressFamily.InterNetwork).ToString() +
113113
"</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>" + description +
@@ -118,7 +118,7 @@ public static void DeleteForwardingRule(int port, ProtocolType protocol)
118118
{
119119
if (string.IsNullOrEmpty(_serviceUrl))
120120
throw new Exception("No UPnP service available or Discover() has not been called");
121-
XmlDocument xdoc = SOAPRequest(_serviceUrl,
121+
SOAPRequest(_serviceUrl,
122122
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
123123
"<NewRemoteHost>" +
124124
"</NewRemoteHost>" +

src/neo/SmartContract/ApplicationEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ private class InvocationState
4949
public long GasConsumed { get; private set; } = 0;
5050
public long GasLeft => gas_amount - GasConsumed;
5151
public Exception FaultException { get; private set; }
52-
public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
52+
public UInt160 CurrentScriptHash => CurrentContext?.GetScriptHash();
5353
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
54-
public UInt160 EntryScriptHash => EntryContext?.GetState<ExecutionContextState>().ScriptHash;
54+
public UInt160 EntryScriptHash => EntryContext?.GetScriptHash();
5555
public IReadOnlyList<NotifyEventArgs> Notifications => notifications ?? (IReadOnlyList<NotifyEventArgs>)Array.Empty<NotifyEventArgs>();
5656

5757
protected ApplicationEngine(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Neo.Consensus;
4+
using Neo.IO;
5+
using Neo.Network.P2P.Payloads;
6+
7+
namespace Neo.UnitTests.Consensus
8+
{
9+
[TestClass]
10+
public class UT_ChangeViewPayloadCompact
11+
{
12+
[TestMethod]
13+
public void Size_Get()
14+
{
15+
var test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 1, InvocationScript = new byte[0], OriginalViewNumber = 1 };
16+
((ISerializable)test).Size.Should().Be(12);
17+
18+
test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 1, InvocationScript = new byte[1024], OriginalViewNumber = 1 };
19+
((ISerializable)test).Size.Should().Be(1038);
20+
}
21+
22+
[TestMethod]
23+
public void DeserializeAndSerialize()
24+
{
25+
var test = new RecoveryMessage.ChangeViewPayloadCompact() { Timestamp = 1, ValidatorIndex = 2, InvocationScript = new byte[] { 1, 2, 3 }, OriginalViewNumber = 3 };
26+
var clone = test.ToArray().AsSerializable<RecoveryMessage.ChangeViewPayloadCompact>();
27+
28+
Assert.AreEqual(test.Timestamp, clone.Timestamp);
29+
Assert.AreEqual(test.ValidatorIndex, clone.ValidatorIndex);
30+
Assert.AreEqual(test.OriginalViewNumber, clone.OriginalViewNumber);
31+
CollectionAssert.AreEqual(test.InvocationScript, clone.InvocationScript);
32+
33+
clone = RecoveryMessage.ChangeViewPayloadCompact.FromPayload(new ConsensusPayload()
34+
{
35+
Data = new ChangeView() { Timestamp = 1, ViewNumber = 3 }.ToArray(),
36+
ValidatorIndex = 2,
37+
Witness = new Witness() { InvocationScript = new byte[] { 1, 2, 3 } }
38+
});
39+
40+
Assert.AreEqual(test.Timestamp, clone.Timestamp);
41+
Assert.AreEqual(test.ValidatorIndex, clone.ValidatorIndex);
42+
Assert.AreEqual(test.OriginalViewNumber, clone.OriginalViewNumber);
43+
CollectionAssert.AreEqual(test.InvocationScript, clone.InvocationScript);
44+
}
45+
}
46+
}

tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Neo.Ledger;
77
using Neo.Network.P2P.Payloads;
88
using Neo.SmartContract.Native;
9-
using Neo.VM.Types;
109
using Neo.Wallets;
1110
using System;
1211
using System.Linq;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Neo.Consensus;
4+
using Neo.IO;
5+
6+
namespace Neo.UnitTests.Consensus
7+
{
8+
[TestClass]
9+
public class UT_RecoveryRequest
10+
{
11+
[TestMethod]
12+
public void Size_Get()
13+
{
14+
var test = new RecoveryRequest() { Timestamp = 1, ViewNumber = 1 };
15+
test.Size.Should().Be(10);
16+
}
17+
18+
[TestMethod]
19+
public void DeserializeAndSerialize()
20+
{
21+
var test = new RecoveryRequest() { ViewNumber = 1, Timestamp = 123 };
22+
var clone = test.ToArray().AsSerializable<RecoveryRequest>();
23+
24+
Assert.AreEqual(test.Timestamp, clone.Timestamp);
25+
Assert.AreEqual(test.Type, clone.Type);
26+
Assert.AreEqual(test.ViewNumber, clone.ViewNumber);
27+
}
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Neo.Cryptography.ECC;
3+
using System;
4+
using ECCurve = Neo.Cryptography.ECC.ECCurve;
5+
6+
namespace Neo.UnitTests.Cryptography.ECC
7+
{
8+
[TestClass]
9+
public class UT_ECDSA
10+
{
11+
[TestMethod]
12+
public void GenerateSignature()
13+
{
14+
var ecdsa = new ECDsa(ECCurve.Secp256k1.Infinity);
15+
Assert.ThrowsException<InvalidOperationException>(() => ecdsa.GenerateSignature(new byte[0]));
16+
17+
var pk = new byte[32];
18+
for (int x = 0; x < pk.Length; x++) pk[x] = (byte)x;
19+
20+
ecdsa = new ECDsa(pk, ECCurve.Secp256k1);
21+
var sig = ecdsa.GenerateSignature(new byte[] { 1 });
22+
23+
Assert.IsTrue(ecdsa.VerifySignature(new byte[] { 1 }, sig[0], sig[1]));
24+
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 2 }, sig[0], sig[1]));
25+
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0] + 1, sig[1]));
26+
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0], sig[1] + 1));
27+
}
28+
}
29+
}

tests/neo.UnitTests/Cryptography/ECC/UT_ECFieldElement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public void TestECFieldElementConstructor()
2121
input = ECCurve.Secp256k1.Q;
2222
action = () => new ECFieldElement(input, ECCurve.Secp256k1);
2323
action.Should().Throw<ArgumentException>();
24+
25+
action = () => new ECFieldElement(input, null);
26+
action.Should().Throw<ArgumentNullException>();
2427
}
2528

2629
[TestMethod]

tests/neo.UnitTests/Cryptography/ECC/UT_ECPoint.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Neo.UnitTests.Cryptography.ECC
1414
[TestClass]
1515
public class UT_ECPoint
1616
{
17-
public static byte[] generatePrivateKey(int privateKeyLength)
17+
public static byte[] GeneratePrivateKey(int privateKeyLength)
1818
{
1919
byte[] privateKey = new byte[privateKeyLength];
2020
for (int i = 0; i < privateKeyLength; i++)
@@ -184,17 +184,17 @@ public void TestFromBytes()
184184
ECPoint.FromBytes(input3, ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G);
185185
ECPoint.FromBytes(input2.Skip(1).ToArray(), ECCurve.Secp256k1).Should().Be(ECCurve.Secp256k1.G);
186186

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

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

197-
byte[] input6 = generatePrivateKey(104);
197+
byte[] input6 = GeneratePrivateKey(104);
198198
ECPoint.FromBytes(input6, ECCurve.Secp256k1).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("3634473727541135791764834762056624681715094789735830699031648" +
199199
"273128038409767"), ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("18165245710263168158644330920009617039772504630129940696140050972160274286151"),
200200
ECCurve.Secp256k1), ECCurve.Secp256k1));
@@ -231,17 +231,17 @@ public void TestMultiply()
231231
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("16721517996619732311261078486295444964227498319433363271180755596201863690708"), ECCurve.Secp256k1),
232232
ECCurve.Secp256k1));
233233

234-
k = new BigInteger(generatePrivateKey(100));
234+
k = new BigInteger(GeneratePrivateKey(100));
235235
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("19222995016448259376216431079553428738726180595337971417371897285865264889977"),
236236
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6637081904924493791520919212064582313497884724460823966446023080706723904419"), ECCurve.Secp256k1),
237237
ECCurve.Secp256k1));
238238

239-
k = new BigInteger(generatePrivateKey(120));
239+
k = new BigInteger(GeneratePrivateKey(120));
240240
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("79652345192111851576650978679091010173409410384772942769927955775006682639778"),
241241
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("6460429961979335115790346961011058418773289452368186110818621539624566803831"), ECCurve.Secp256k1),
242242
ECCurve.Secp256k1));
243243

244-
k = new BigInteger(generatePrivateKey(300));
244+
k = new BigInteger(GeneratePrivateKey(300));
245245
ECPoint.Multiply(p, k).Should().Be(new ECPoint(new ECFieldElement(BigInteger.Parse("105331914562708556186724786757483927866790351460145374033180496740107603569412"),
246246
ECCurve.Secp256k1), new ECFieldElement(BigInteger.Parse("60523670886755698512704385951571322569877668383890769288780681319304421873758"), ECCurve.Secp256k1),
247247
ECCurve.Secp256k1));

tests/neo.UnitTests/Cryptography/MPT/UT_MPTTrie.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ public void TestGetProof()
234234
Assert.IsTrue(proof.Contains(r.Encode()));
235235
Assert.IsTrue(proof.Contains(l1.Encode()));
236236
Assert.IsTrue(proof.Contains(v1.Encode()));
237+
238+
proof = mpt.GetProof(Array.Empty<byte>());
239+
Assert.IsNull(proof);
237240
}
238241

239242
[TestMethod]

tests/neo.UnitTests/IO/Caching/UT_HashSetCache.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public void TestHashSetCache()
1616
var bucket = new HashSetCache<int>(10);
1717
for (int i = 1; i <= 100; i++)
1818
{
19-
bucket.Add(i);
19+
Assert.IsTrue(bucket.Add(i));
20+
Assert.IsFalse(bucket.Add(i));
2021
}
2122
bucket.Count.Should().Be(100);
2223

@@ -131,6 +132,8 @@ public void TestExceptWith()
131132
};
132133
set.ExceptWith(new UInt256[] { b, c });
133134
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { a });
135+
set.ExceptWith(new UInt256[] { a });
136+
CollectionAssert.AreEqual(set.ToArray(), new UInt256[] { });
134137

135138
set = new HashSetCache<UInt256>(10)
136139
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Neo.IO;
3+
using System;
4+
using System.Linq;
5+
6+
namespace Neo.UnitTests
7+
{
8+
[TestClass]
9+
public class UT_ByteArrayEqualityComparer
10+
{
11+
[TestMethod]
12+
public void TestEqual()
13+
{
14+
var a = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
15+
var b = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
16+
var check = ByteArrayEqualityComparer.Default;
17+
18+
Assert.IsTrue(check.Equals(a, a));
19+
Assert.IsTrue(check.Equals(a, b));
20+
Assert.IsFalse(check.Equals(null, b));
21+
Assert.IsFalse(check.Equals(a, null));
22+
Assert.IsTrue(check.Equals(null, null));
23+
24+
Assert.IsFalse(check.Equals(a, new byte[] { 1, 2, 3 }));
25+
Assert.IsTrue(check.Equals(Array.Empty<byte>(), Array.Empty<byte>()));
26+
27+
b[8]++;
28+
Assert.IsFalse(check.Equals(a, b));
29+
b[8]--;
30+
b[0]--;
31+
Assert.IsFalse(check.Equals(a, b));
32+
}
33+
34+
[TestMethod]
35+
public void TestGetHashCode()
36+
{
37+
var a = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
38+
var b = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 5 };
39+
var check = ByteArrayEqualityComparer.Default;
40+
41+
Assert.AreEqual(check.GetHashCode(a), check.GetHashCode(b));
42+
Assert.AreNotEqual(check.GetHashCode(a), check.GetHashCode(b.Take(8).ToArray()));
43+
}
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Neo.IO;
3+
using System;
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
namespace Neo.UnitTests
7+
{
8+
[TestClass]
9+
public class UT_ReferenceEqualityComparer
10+
{
11+
private class FakeEquals : IEquatable<FakeEquals>
12+
{
13+
public bool Equals([AllowNull] FakeEquals other)
14+
{
15+
return true;
16+
}
17+
18+
public override int GetHashCode()
19+
{
20+
return 123;
21+
}
22+
}
23+
24+
[TestMethod]
25+
public void TestEqual()
26+
{
27+
var a = new FakeEquals();
28+
var b = new FakeEquals();
29+
var check = ReferenceEqualityComparer.Default;
30+
31+
Assert.IsFalse(check.Equals(a, b));
32+
Assert.AreNotEqual(check.GetHashCode(a), check.GetHashCode(b));
33+
Assert.AreNotEqual(123, check.GetHashCode(a));
34+
Assert.AreNotEqual(123, check.GetHashCode(b));
35+
}
36+
}
37+
}

tests/neo.UnitTests/IO/UT_SerializableWrapper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public void TestGetSize()
1414
Assert.AreEqual(4, temp.Size);
1515
}
1616

17+
[TestMethod]
18+
public void TestCast()
19+
{
20+
SerializableWrapper<uint> tempA = (SerializableWrapper<uint>)123;
21+
SerializableWrapper<uint> tempB = tempA.ToArray().AsSerializable<SerializableWrapper<uint>>();
22+
23+
Assert.IsTrue(tempA.Equals(tempB));
24+
Assert.AreEqual((uint)123, (uint)tempA);
25+
}
26+
1727
[TestMethod]
1828
public void TestEqualsOtherObject()
1929
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Neo.IO;
4+
using Neo.Ledger;
5+
using System;
6+
7+
namespace Neo.UnitTests.Ledger
8+
{
9+
[TestClass]
10+
public class UT_ContractIdState
11+
{
12+
[TestMethod]
13+
public void Size_Get()
14+
{
15+
var test = new ContractIdState() { NextId = 1 };
16+
((ISerializable)test).Size.Should().Be(4);
17+
18+
test = new ContractIdState() { NextId = int.MaxValue };
19+
((ISerializable)test).Size.Should().Be(4);
20+
}
21+
22+
[TestMethod]
23+
public void Clone()
24+
{
25+
var test = new ContractIdState() { NextId = 1 };
26+
var clone = ((ICloneable<ContractIdState>)test).Clone();
27+
28+
Assert.AreEqual(test.NextId, clone.NextId);
29+
30+
clone = new ContractIdState() { NextId = 2 };
31+
((ICloneable<ContractIdState>)clone).FromReplica(test);
32+
33+
Assert.AreEqual(test.NextId, clone.NextId);
34+
}
35+
36+
[TestMethod]
37+
public void DeserializeAndSerialize()
38+
{
39+
var test = new ContractIdState() { NextId = int.MaxValue };
40+
var clone = test.ToArray().AsSerializable<ContractIdState>();
41+
42+
Assert.AreEqual(test.NextId, clone.NextId);
43+
44+
test = new ContractIdState() { NextId = -1 };
45+
Assert.ThrowsException<FormatException>(() => test.ToArray().AsSerializable<ContractIdState>());
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)