From 09cb4fad22a211d0ffddcfbda6947510411d6322 Mon Sep 17 00:00:00 2001 From: Charis Zhao Date: Wed, 6 Nov 2019 11:13:31 +0800 Subject: [PATCH] Unit tests for some auxiliary classes (#1192) --- neo.UnitTests/IO/UT_ByteArrayComparer.cs | 27 ++ neo.UnitTests/Plugins/TestLogPlugin.cs | 51 ++++ neo.UnitTests/Plugins/UT_Plugin.cs | 83 +++++ neo.UnitTests/UT_Helper.cs | 261 ++++++++++++++++ neo.UnitTests/UT_NefFile.cs | 75 ----- neo.UnitTests/UT_NeoSystem.cs | 32 ++ neo.UnitTests/UT_ProtocolSettings.cs | 18 ++ neo.UnitTests/UT_UInt160.cs | 94 ++++++ neo.UnitTests/UT_UInt256.cs | 94 ++++++ neo.UnitTests/UT_UIntBase.cs | 102 +++++++ neo.UnitTests/VM/UT_Helper.cs | 373 +++++++++++++++++++++++ 11 files changed, 1135 insertions(+), 75 deletions(-) create mode 100644 neo.UnitTests/IO/UT_ByteArrayComparer.cs create mode 100644 neo.UnitTests/Plugins/TestLogPlugin.cs create mode 100644 neo.UnitTests/Plugins/UT_Plugin.cs delete mode 100644 neo.UnitTests/UT_NefFile.cs create mode 100644 neo.UnitTests/UT_NeoSystem.cs create mode 100644 neo.UnitTests/UT_UInt160.cs create mode 100644 neo.UnitTests/UT_UInt256.cs create mode 100644 neo.UnitTests/UT_UIntBase.cs diff --git a/neo.UnitTests/IO/UT_ByteArrayComparer.cs b/neo.UnitTests/IO/UT_ByteArrayComparer.cs new file mode 100644 index 0000000000..dab1adcd8b --- /dev/null +++ b/neo.UnitTests/IO/UT_ByteArrayComparer.cs @@ -0,0 +1,27 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO; + +namespace Neo.UnitTests.IO +{ + [TestClass] + public class UT_ByteArrayComparer + { + [TestMethod] + public void TestCompare() + { + ByteArrayComparer comparer = new ByteArrayComparer(); + byte[] x = new byte[0], y = new byte[0]; + comparer.Compare(x, y).Should().Be(0); + + x = new byte[] { 1 }; + comparer.Compare(x, y).Should().Be(1); + y = x; + comparer.Compare(x, y).Should().Be(0); + + x = new byte[] { 1 }; + y = new byte[] { 2 }; + comparer.Compare(x, y).Should().Be(-1); + } + } +} diff --git a/neo.UnitTests/Plugins/TestLogPlugin.cs b/neo.UnitTests/Plugins/TestLogPlugin.cs new file mode 100644 index 0000000000..d9f0c824d2 --- /dev/null +++ b/neo.UnitTests/Plugins/TestLogPlugin.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Configuration; +using Neo.Plugins; + +namespace Neo.UnitTests.Plugins +{ + public class TestLogPlugin : Plugin, ILogPlugin + { + public TestLogPlugin() : base() { } + + public string Output { set; get; } + + public override void Configure() { } + + public new void Log(string source, LogLevel level, string message) + { + Output = source + "_" + level.ToString() + "_" + message; + } + + public void LogMessage(string message) + { + Log(message); + } + + public bool TestOnMessage(object message) + { + return OnMessage(message); + } + + public IConfigurationSection TestGetConfiguration() + { + return GetConfiguration(); + } + + protected override bool OnMessage(object message) => true; + + public static bool TestResumeNodeStartup() + { + return ResumeNodeStartup(); + } + + public static void TestSuspendNodeStartup() + { + SuspendNodeStartup(); + } + + public static void TestLoadPlugins(NeoSystem system) + { + LoadPlugins(system); + } + } +} diff --git a/neo.UnitTests/Plugins/UT_Plugin.cs b/neo.UnitTests/Plugins/UT_Plugin.cs new file mode 100644 index 0000000000..3c6098e47a --- /dev/null +++ b/neo.UnitTests/Plugins/UT_Plugin.cs @@ -0,0 +1,83 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Plugins; +using System; + +namespace Neo.UnitTests.Plugins +{ + [TestClass] + public class UT_Plugin + { + private static readonly object locker = new object(); + + [TestMethod] + public void TestGetConfigFile() + { + var pp = new TestLogPlugin(); + var file = pp.ConfigFile; + file.EndsWith("config.json").Should().BeTrue(); + } + + [TestMethod] + public void TestGetName() + { + var pp = new TestLogPlugin(); + pp.Name.Should().Be("TestLogPlugin"); + } + + [TestMethod] + public void TestGetVersion() + { + var pp = new TestLogPlugin(); + Action action = () => pp.Version.ToString(); + action.Should().NotThrow(); + } + + [TestMethod] + public void TestLog() + { + var lp = new TestLogPlugin(); + lp.LogMessage("Hello"); + lp.Output.Should().Be("Plugin:TestLogPlugin_Info_Hello"); + } + + [TestMethod] + public void TestSendMessage() + { + lock (locker) + { + Plugin.Plugins.Clear(); + Plugin.SendMessage("hey1").Should().BeFalse(); + + var lp = new TestLogPlugin(); + Plugin.SendMessage("hey2").Should().BeTrue(); + } + } + + [TestMethod] + public void TestNotifyPluginsLoadedAfterSystemConstructed() + { + var pp = new TestLogPlugin(); + Action action = () => Plugin.NotifyPluginsLoadedAfterSystemConstructed(); + action.Should().NotThrow(); + } + + [TestMethod] + public void TestResumeNodeStartupAndSuspendNodeStartup() + { + var system = TestBlockchain.InitializeMockNeoSystem(); + TestLogPlugin.TestLoadPlugins(system); + TestLogPlugin.TestSuspendNodeStartup(); + TestLogPlugin.TestSuspendNodeStartup(); + TestLogPlugin.TestResumeNodeStartup().Should().BeFalse(); + TestLogPlugin.TestResumeNodeStartup().Should().BeTrue(); + } + + [TestMethod] + public void TestGetConfiguration() + { + var pp = new TestLogPlugin(); + pp.TestGetConfiguration().Key.Should().Be("PluginConfiguration"); + } + } +} diff --git a/neo.UnitTests/UT_Helper.cs b/neo.UnitTests/UT_Helper.cs index aaad993bb1..45da27146c 100644 --- a/neo.UnitTests/UT_Helper.cs +++ b/neo.UnitTests/UT_Helper.cs @@ -3,6 +3,11 @@ using Neo.Network.P2P; using Neo.SmartContract; using Neo.Wallets; +using System; +using System.Collections.Generic; +using System.Net; +using System.Numerics; +using System.Security.Cryptography; namespace Neo.UnitTests { @@ -38,5 +43,261 @@ public void ToScriptHash() res.Should().Be(UInt160.Parse("2d3b96ae1bcc5a585e075e3b81920210dec16302")); } + [TestMethod] + public void TestGetLowestSetBit() + { + var big1 = new BigInteger(0); + big1.GetLowestSetBit().Should().Be(-1); + + var big2 = new BigInteger(512); + big2.GetLowestSetBit().Should().Be(9); + } + + [TestMethod] + public void TestGetBitLength() + { + var b1 = new BigInteger(100); + b1.GetBitLength().Should().Be(7); + + var b2 = new BigInteger(-100); + b2.GetBitLength().Should().Be(7); + } + + [TestMethod] + public void TestHexToBytes() + { + string nullStr = null; + nullStr.HexToBytes().ToHexString().Should().Be(new byte[0].ToHexString()); + string emptyStr = ""; + emptyStr.HexToBytes().ToHexString().Should().Be(new byte[0].ToHexString()); + string str1 = "hab"; + Action action = () => str1.HexToBytes(); + action.Should().Throw(); + string str2 = "0102"; + byte[] bytes = str2.HexToBytes(); + bytes.ToHexString().Should().Be(new byte[] { 0x01, 0x02 }.ToHexString()); + } + + [TestMethod] + public void TestNextBigIntegerForRandom() + { + Random ran = new Random(); + Action action1 = () => ran.NextBigInteger(-1); + action1.Should().Throw(); + + ran.NextBigInteger(0).Should().Be(0); + ran.NextBigInteger(8).Should().NotBeNull(); + ran.NextBigInteger(9).Should().NotBeNull(); + } + + [TestMethod] + public void TestNextBigIntegerForRandomNumberGenerator() + { + var ran = RandomNumberGenerator.Create(); + Action action1 = () => ran.NextBigInteger(-1); + action1.Should().Throw(); + + ran.NextBigInteger(0).Should().Be(0); + ran.NextBigInteger(8).Should().NotBeNull(); + ran.NextBigInteger(9).Should().NotBeNull(); + } + + [TestMethod] + public void TestToInt64() + { + byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; + var ret = bytes.ToInt64(0); + ret.GetType().Should().Be(typeof(long)); + ret.Should().Be(67305985); + } + + [TestMethod] + public void TestToUInt16() + { + byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; + var ret = bytes.ToUInt16(0); + ret.GetType().Should().Be(typeof(ushort)); + ret.Should().Be(513); + } + + [TestMethod] + public void TestToUInt64() + { + byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 }; + var ret = bytes.ToUInt64(0); + ret.GetType().Should().Be(typeof(ulong)); + ret.Should().Be(67305985); + } + + [TestMethod] + public void TestUnmapForIPAddress() + { + var addr = new IPAddress(new byte[] { 127, 0, 0, 1 }); + addr.Unmap().Should().Be(addr); + + var addr2 = addr.MapToIPv6(); + addr2.Unmap().Should().Be(addr); + } + + [TestMethod] + public void TestUnmapForIPEndPoin() + { + var addr = new IPAddress(new byte[] { 127, 0, 0, 1 }); + var endPoint = new IPEndPoint(addr, 8888); + endPoint.Unmap().Should().Be(endPoint); + + var addr2 = addr.MapToIPv6(); + var endPoint2 = new IPEndPoint(addr2, 8888); + endPoint2.Unmap().Should().Be(endPoint); + } + + [TestMethod] + public void TestWeightedAverage() + { + var foo1 = new Foo + { + Value = 1, + Weight = 2 + }; + var foo2 = new Foo + { + Value = 2, + Weight = 3 + }; + var list = new List + { + foo1,foo2 + }; + list.WeightedAverage(p => p.Value, p => p.Weight).Should().Be(new BigInteger(1)); + + var foo3 = new Foo + { + Value = 1, + Weight = 0 + }; + var foo4 = new Foo + { + Value = 2, + Weight = 0 + }; + var list2 = new List + { + foo3, foo4 + }; + list2.WeightedAverage(p => p.Value, p => p.Weight).Should().Be(BigInteger.Zero); + } + + [TestMethod] + public void WeightFilter() + { + var w1 = new Woo + { + Value = 1 + }; + var w2 = new Woo + { + Value = 2 + }; + var list = new List + { + w1, w2 + }; + var ret = list.WeightedFilter(0.3, 0.6, p => p.Value, (p, w) => new Result + { + Info = p, + Weight = w + }); + var sum = BigInteger.Zero; + foreach (Result res in ret) + { + sum = BigInteger.Add(res.Weight, sum); + } + sum.Should().Be(BigInteger.Zero); + + var w3 = new Woo + { + Value = 3 + }; + + var list2 = new List + { + w1, w2, w3 + }; + var ret2 = list2.WeightedFilter(0.3, 0.4, p => p.Value, (p, w) => new Result + { + Info = p, + Weight = w + }); + sum = BigInteger.Zero; + foreach (Result res in ret2) + { + sum = BigInteger.Add(res.Weight, sum); + } + sum.Should().Be(BigInteger.Zero); + + CheckArgumentOutOfRangeException(-1, 0.4, p => p.Value, list2); + + CheckArgumentOutOfRangeException(0.2, 1.4, p => p.Value, list2); + + CheckArgumentOutOfRangeException(0.8, 0.3, p => p.Value, list2); + + CheckArgumentOutOfRangeException(0.3, 0.8, p => p.Value, list2); + + CheckArgumentNullException(0.3, 0.6, null, list2); + + CheckArgumentNullException(0.3, 0.4, p => p.Value, null); + + list2.WeightedFilter(0.3, 0.3, p => p.Value, (p, w) => new Result + { + Info = p, + Weight = w + }).WeightedAverage(p => p.Weight, p => p.Weight).Should().Be(0); + + + var list3 = new List(); + list3.WeightedFilter(0.3, 0.6, p => p.Value, (p, w) => new Result + { + Info = p, + Weight = w + }).WeightedAverage(p => p.Weight, p => p.Weight).Should().Be(0); + + } + + private static void CheckArgumentOutOfRangeException(double start, double end, Func func, List list) + { + Action action = () => list.WeightedFilter(start, end, func, (p, w) => new Result + { + Info = p, + Weight = w + }).WeightedAverage(p => p.Weight, p => p.Weight); + action.Should().Throw(); + } + + private static void CheckArgumentNullException(double start, double end, Func func, List list) + { + Action action = () => list.WeightedFilter(start, end, func, (p, w) => new Result + { + Info = p, + Weight = w + }).WeightedAverage(p => p.Weight, p => p.Weight); + action.Should().Throw(); + } + } + + class Foo + { + public int Weight { set; get; } + public int Value { set; get; } + } + + class Woo + { + public int Value { set; get; } + } + + class Result + { + public Woo Info { set; get; } + public BigInteger Weight { set; get; } } } diff --git a/neo.UnitTests/UT_NefFile.cs b/neo.UnitTests/UT_NefFile.cs deleted file mode 100644 index 056333163d..0000000000 --- a/neo.UnitTests/UT_NefFile.cs +++ /dev/null @@ -1,75 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Neo.Cryptography; -using Neo.IO; -using Neo.SmartContract; -using System; - -namespace Neo.UnitTests -{ - [TestClass] - public class UT_NefFile - { - [TestMethod] - public void ParseTest() - { - var file = new NefFile() - { - Compiler = "".PadLeft(32, ' '), - Version = new Version(1, 2, 3, 4), - Script = new byte[] { 0x01, 0x02, 0x03 } - }; - - file.ScriptHash = file.Script.ToScriptHash(); - file.CheckSum = NefFile.ComputeChecksum(file); - - var data = file.ToArray(); - file = data.AsSerializable(); - - Assert.AreEqual("".PadLeft(32, ' '), file.Compiler); - Assert.AreEqual(new Version(1, 2, 3, 4), file.Version); - Assert.AreEqual(file.Script.ToScriptHash(), file.ScriptHash); - CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03 }, file.Script); - } - - [TestMethod] - public void LimitTest() - { - var file = new NefFile() - { - Compiler = "".PadLeft(byte.MaxValue, ' '), - Version = new Version(1, 2, 3, 4), - Script = new byte[1024 * 1024], - ScriptHash = new byte[1024 * 1024].ToScriptHash(), - CheckSum = 0 - }; - - // Wrong compiler - - Assert.ThrowsException(() => file.ToArray()); - - // Wrong script - - file.Compiler = ""; - file.Script = new byte[(1024 * 1024) + 1]; - file.ScriptHash = file.Script.ToScriptHash(); - var data = file.ToArray(); - - Assert.ThrowsException(() => data.AsSerializable()); - - // Wrong script hash - - file.Script = new byte[1024 * 1024]; - data = file.ToArray(); - - Assert.ThrowsException(() => data.AsSerializable()); - - // Wrong checksum - - file.Script = new byte[1024]; - data = file.ToArray(); - file.CheckSum = NefFile.ComputeChecksum(file) + 1; - - Assert.ThrowsException(() => data.AsSerializable()); - } - } -} diff --git a/neo.UnitTests/UT_NeoSystem.cs b/neo.UnitTests/UT_NeoSystem.cs new file mode 100644 index 0000000000..2d71db97df --- /dev/null +++ b/neo.UnitTests/UT_NeoSystem.cs @@ -0,0 +1,32 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Neo.UnitTests +{ + [TestClass] + public class UT_NeoSystem + { + private NeoSystem neoSystem; + + [TestInitialize] + public void Setup() + { + neoSystem = TestBlockchain.InitializeMockNeoSystem(); + } + + [TestMethod] + public void TestGetBlockchain() => neoSystem.Blockchain.Should().NotBeNull(); + + [TestMethod] + public void TestGetLocalNode() => neoSystem.LocalNode.Should().NotBeNull(); + + [TestMethod] + public void TestGetTaskManager() => neoSystem.TaskManager.Should().NotBeNull(); + + [TestMethod] + public void TestGetConsensus() => neoSystem.Consensus.Should().BeNull(); + + [TestMethod] + public void TestGetRpcServer() => neoSystem.RpcServer.Should().BeNull(); + } +} diff --git a/neo.UnitTests/UT_ProtocolSettings.cs b/neo.UnitTests/UT_ProtocolSettings.cs index 77c56df424..2e1b8c3143 100644 --- a/neo.UnitTests/UT_ProtocolSettings.cs +++ b/neo.UnitTests/UT_ProtocolSettings.cs @@ -90,5 +90,23 @@ public void Cant_initialize_ProtocolSettings_twice() ProtocolSettings.Initialize(config).Should().BeFalse(); ProtocolSettings.Default.Magic.Should().Be(expectedMagic); } + + [TestMethod] + public void TestGetMemoryPoolMaxTransactions() + { + ProtocolSettings.Default.MemoryPoolMaxTransactions.Should().Be(50000); + } + + [TestMethod] + public void TestGetMillisecondsPerBlock() + { + ProtocolSettings.Default.MillisecondsPerBlock.Should().Be(2000); + } + + [TestMethod] + public void TestGetSeedList() + { + ProtocolSettings.Default.SeedList.Should().BeEquivalentTo(new string[] { "seed1.neo.org:10333", "seed2.neo.org:10333", "seed3.neo.org:10333", "seed4.neo.org:10333", "seed5.neo.org:10333", }); + } } } diff --git a/neo.UnitTests/UT_UInt160.cs b/neo.UnitTests/UT_UInt160.cs new file mode 100644 index 0000000000..99cf91a5c3 --- /dev/null +++ b/neo.UnitTests/UT_UInt160.cs @@ -0,0 +1,94 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Neo.UnitTests.IO +{ + [TestClass] + public class UT_UInt160 + { + [TestMethod] + public void TestGernerator1() + { + UInt160 uInt160 = new UInt160(); + Assert.IsNotNull(uInt160); + } + + [TestMethod] + public void TestGernerator2() + { + UInt160 uInt160 = new UInt160(new byte[20]); + Assert.IsNotNull(uInt160); + } + + [TestMethod] + public void TestCompareTo() + { + byte[] temp = new byte[20]; + temp[19] = 0x01; + UInt160 result = new UInt160(temp); + Assert.AreEqual(0, UInt160.Zero.CompareTo(UInt160.Zero)); + Assert.AreEqual(-1, UInt160.Zero.CompareTo(result)); + Assert.AreEqual(1, result.CompareTo(UInt160.Zero)); + } + + [TestMethod] + public void TestEquals() + { + byte[] temp = new byte[20]; + temp[19] = 0x01; + UInt160 result = new UInt160(temp); + Assert.AreEqual(true, UInt160.Zero.Equals(UInt160.Zero)); + Assert.AreEqual(false, UInt160.Zero.Equals(result)); + Assert.AreEqual(false, result.Equals(null)); + } + + [TestMethod] + public void TestParse() + { + Action action = () => UInt160.Parse(null); + action.Should().Throw(); + UInt160 result = UInt160.Parse("0x0000000000000000000000000000000000000000"); + Assert.AreEqual(UInt160.Zero, result); + Action action1 = () => UInt160.Parse("000000000000000000000000000000000000000"); + action1.Should().Throw(); + UInt160 result1 = UInt160.Parse("0000000000000000000000000000000000000000"); + Assert.AreEqual(UInt160.Zero, result1); + } + + [TestMethod] + public void TestTryParse() + { + UInt160 temp = new UInt160(); + Assert.AreEqual(false, UInt160.TryParse(null, out temp)); + Assert.AreEqual(true, UInt160.TryParse("0x0000000000000000000000000000000000000000", out temp)); + Assert.AreEqual(UInt160.Zero, temp); + Assert.AreEqual(false, UInt160.TryParse("000000000000000000000000000000000000000", out temp)); + Assert.AreEqual(false, UInt160.TryParse("0xKK00000000000000000000000000000000000000", out temp)); + } + + [TestMethod] + public void TestOperatorLarger() + { + Assert.AreEqual(false, UInt160.Zero > UInt160.Zero); + } + + [TestMethod] + public void TestOperatorLargerAndEqual() + { + Assert.AreEqual(true, UInt160.Zero >= UInt160.Zero); + } + + [TestMethod] + public void TestOperatorSmaller() + { + Assert.AreEqual(false, UInt160.Zero < UInt160.Zero); + } + + [TestMethod] + public void TestOperatorSmallerAndEqual() + { + Assert.AreEqual(true, UInt160.Zero <= UInt160.Zero); + } + } +} diff --git a/neo.UnitTests/UT_UInt256.cs b/neo.UnitTests/UT_UInt256.cs new file mode 100644 index 0000000000..179e991110 --- /dev/null +++ b/neo.UnitTests/UT_UInt256.cs @@ -0,0 +1,94 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Neo.UnitTests.IO +{ + [TestClass] + public class UT_UInt256 + { + [TestMethod] + public void TestGernerator1() + { + UInt256 uInt256 = new UInt256(); + Assert.IsNotNull(uInt256); + } + + [TestMethod] + public void TestGernerator2() + { + UInt256 uInt256 = new UInt256(new byte[32]); + Assert.IsNotNull(uInt256); + } + + [TestMethod] + public void TestCompareTo() + { + byte[] temp = new byte[32]; + temp[31] = 0x01; + UInt256 result = new UInt256(temp); + Assert.AreEqual(0, UInt256.Zero.CompareTo(UInt256.Zero)); + Assert.AreEqual(-1, UInt256.Zero.CompareTo(result)); + Assert.AreEqual(1, result.CompareTo(UInt256.Zero)); + } + + [TestMethod] + public void TestEquals() + { + byte[] temp = new byte[32]; + temp[31] = 0x01; + UInt256 result = new UInt256(temp); + Assert.AreEqual(true, UInt256.Zero.Equals(UInt256.Zero)); + Assert.AreEqual(false, UInt256.Zero.Equals(result)); + Assert.AreEqual(false, result.Equals(null)); + } + + [TestMethod] + public void TestParse() + { + Action action = () => UInt256.Parse(null); + action.Should().Throw(); + UInt256 result = UInt256.Parse("0x0000000000000000000000000000000000000000000000000000000000000000"); + Assert.AreEqual(UInt256.Zero, result); + Action action1 = () => UInt256.Parse("000000000000000000000000000000000000000000000000000000000000000"); + action1.Should().Throw(); + UInt256 result1 = UInt256.Parse("0000000000000000000000000000000000000000000000000000000000000000"); + Assert.AreEqual(UInt256.Zero, result1); + } + + [TestMethod] + public void TestTryParse() + { + UInt256 temp = new UInt256(); + Assert.AreEqual(false, UInt256.TryParse(null, out temp)); + Assert.AreEqual(true, UInt256.TryParse("0x0000000000000000000000000000000000000000000000000000000000000000", out temp)); + Assert.AreEqual(UInt256.Zero, temp); + Assert.AreEqual(false, UInt256.TryParse("000000000000000000000000000000000000000000000000000000000000000", out temp)); + Assert.AreEqual(false, UInt256.TryParse("0xKK00000000000000000000000000000000000000000000000000000000000000", out temp)); + } + + [TestMethod] + public void TestOperatorLarger() + { + Assert.AreEqual(false, UInt256.Zero > UInt256.Zero); + } + + [TestMethod] + public void TestOperatorLargerAndEqual() + { + Assert.AreEqual(true, UInt256.Zero >= UInt256.Zero); + } + + [TestMethod] + public void TestOperatorSmaller() + { + Assert.AreEqual(false, UInt256.Zero < UInt256.Zero); + } + + [TestMethod] + public void TestOperatorSmallerAndEqual() + { + Assert.AreEqual(true, UInt256.Zero <= UInt256.Zero); + } + } +} diff --git a/neo.UnitTests/UT_UIntBase.cs b/neo.UnitTests/UT_UIntBase.cs new file mode 100644 index 0000000000..516b3d0504 --- /dev/null +++ b/neo.UnitTests/UT_UIntBase.cs @@ -0,0 +1,102 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO; +using System; +using System.IO; + +namespace Neo.UnitTests.IO +{ + [TestClass] + public class UT_UIntBase + { + [TestMethod] + public void TestDeserialize() + { + using (MemoryStream stream = new MemoryStream()) + using (BinaryWriter writer = new BinaryWriter(stream)) + using (BinaryReader reader = new BinaryReader(stream)) + { + writer.Write(new byte[20]); + stream.Seek(0, SeekOrigin.Begin); + MyUIntBase uIntBase = new MyUIntBase(); + Action action = () => ((ISerializable)uIntBase).Deserialize(reader); + action.Should().Throw(); + } + } + + [TestMethod] + public void TestEquals1() + { + MyUIntBase temp1 = new MyUIntBase(); + MyUIntBase temp2 = new MyUIntBase(); + UInt160 temp3 = new UInt160(); + Assert.AreEqual(false, temp1.Equals(null)); + Assert.AreEqual(true, temp1.Equals(temp1)); + Assert.AreEqual(true, temp1.Equals(temp2)); + Assert.AreEqual(false, temp1.Equals(temp3)); + } + + [TestMethod] + public void TestEquals2() + { + MyUIntBase temp1 = new MyUIntBase(); + object temp2 = null; + object temp3 = new object(); + Assert.AreEqual(false, temp1.Equals(temp2)); + Assert.AreEqual(false, temp1.Equals(temp3)); + } + + [TestMethod] + public void TestParse() + { + UInt160 uInt1601 = (UInt160)UIntBase.Parse("0x0000000000000000000000000000000000000000"); + UInt256 uInt2561 = (UInt256)UIntBase.Parse("0x0000000000000000000000000000000000000000000000000000000000000000"); + UInt160 uInt1602 = (UInt160)UIntBase.Parse("0000000000000000000000000000000000000000"); + UInt256 uInt2562 = (UInt256)UIntBase.Parse("0000000000000000000000000000000000000000000000000000000000000000"); + Assert.AreEqual(UInt160.Zero, uInt1601); + Assert.AreEqual(UInt256.Zero, uInt2561); + Assert.AreEqual(UInt160.Zero, uInt1602); + Assert.AreEqual(UInt256.Zero, uInt2562); + Action action = () => UIntBase.Parse("0000000"); + action.Should().Throw(); + } + + [TestMethod] + public void TestTryParse() + { + UInt160 uInt160 = new UInt160(); + Assert.AreEqual(true, UIntBase.TryParse("0x0000000000000000000000000000000000000000", out uInt160)); + Assert.AreEqual(UInt160.Zero, uInt160); + Assert.AreEqual(false, UIntBase.TryParse("0x00000000000000000000000000000000000000", out uInt160)); + UInt256 uInt256 = new UInt256(); + Assert.AreEqual(true, UIntBase.TryParse("0x0000000000000000000000000000000000000000000000000000000000000000", out uInt256)); + Assert.AreEqual(UInt256.Zero, uInt256); + Assert.AreEqual(false, UIntBase.TryParse("0x00000000000000000000000000000000000000000000000000000000000000", out uInt256)); + UIntBase uIntBase = new UInt160(); + Assert.AreEqual(true, UIntBase.TryParse("0x0000000000000000000000000000000000000000", out uIntBase)); + Assert.AreEqual(UInt160.Zero, uIntBase); + Assert.AreEqual(true, UIntBase.TryParse("0000000000000000000000000000000000000000", out uIntBase)); + Assert.AreEqual(UInt160.Zero, uIntBase); + uIntBase = new UInt256(); + Assert.AreEqual(true, UIntBase.TryParse("0x0000000000000000000000000000000000000000000000000000000000000000", out uIntBase)); + Assert.AreEqual(UInt256.Zero, uIntBase); + Assert.AreEqual(true, UIntBase.TryParse("0000000000000000000000000000000000000000000000000000000000000000", out uIntBase)); + Assert.AreEqual(UInt256.Zero, uIntBase); + Assert.AreEqual(false, UIntBase.TryParse("00000000000000000000000000000000000000000000000000000000000000", out uIntBase)); + } + + [TestMethod] + public void TestOperatorEqual() + { + Assert.AreEqual(false, new MyUIntBase() == null); + Assert.AreEqual(false, null == new MyUIntBase()); + } + } + + internal class MyUIntBase : UIntBase + { + public const int Length = 32; + public MyUIntBase() : this(null) { } + public MyUIntBase(byte[] value) : base(Length, value) { } + } +} diff --git a/neo.UnitTests/VM/UT_Helper.cs b/neo.UnitTests/VM/UT_Helper.cs index cf5ad0bff5..39f1ec104a 100644 --- a/neo.UnitTests/VM/UT_Helper.cs +++ b/neo.UnitTests/VM/UT_Helper.cs @@ -1,3 +1,4 @@ +using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Cryptography.ECC; using Neo.SmartContract; @@ -138,5 +139,377 @@ public void TestToStackItem() ContractParameter mapParameter = new ContractParameter { Type = ContractParameterType.Map, Value = new[] { new KeyValuePair(byteParameter, pkParameter) } }; Assert.AreEqual(30000000000000L, (long)((VM.Types.Map)mapParameter.ToStackItem()).Keys.First().GetBigInteger()); } + + [TestMethod] + public void TestEmitPush1() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(UInt160.Zero); + byte[] tempArray = new byte[21]; + tempArray[0] = 0x14; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + [TestMethod] + public void TestEmitPush2() + { + TestEmitPush2Signature(); + TestEmitPush2ByteArray(); + TestEmitPush2Boolean(); + TestEmitPush2Integer(); + TestEmitPush2BigInteger(); + TestEmitPush2Hash160(); + TestEmitPush2Hash256(); + TestEmitPush2PublicKey(); + TestEmitPush2String(); + TestEmitPush2Array(); + + ScriptBuilder sb = new ScriptBuilder(); + Action action = () => sb.EmitPush(new ContractParameter(ContractParameterType.Map)); + action.Should().Throw(); + } + + private void TestEmitPush2Array() + { + ScriptBuilder sb = new ScriptBuilder(); + ContractParameter parameter = new ContractParameter(ContractParameterType.Array); + IList values = new List(); + values.Add(new ContractParameter(ContractParameterType.Integer)); + values.Add(new ContractParameter(ContractParameterType.Integer)); + parameter.Value = values; + sb.EmitPush(parameter); + byte[] tempArray = new byte[4]; + tempArray[0] = 0x00; + tempArray[1] = 0x00; + tempArray[2] = 0x52; + tempArray[3] = 0xC1; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2String() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.String)); + byte[] tempArray = new byte[1]; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2PublicKey() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.PublicKey)); + byte[] tempArray = new byte[34]; + tempArray[0] = 0x21; + Array.Copy(ECCurve.Secp256r1.G.EncodePoint(true), 0, tempArray, 1, 33); + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2Hash256() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.Hash256)); + byte[] tempArray = new byte[33]; + tempArray[0] = 0x20; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2Hash160() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.Hash160)); + byte[] tempArray = new byte[21]; + tempArray[0] = 0x14; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2BigInteger() + { + ScriptBuilder sb = new ScriptBuilder(); + ContractParameter parameter = new ContractParameter(ContractParameterType.Integer) + { + Value = BigInteger.Zero + }; + sb.EmitPush(parameter); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2Integer() + { + ScriptBuilder sb = new ScriptBuilder(); + ContractParameter parameter = new ContractParameter(ContractParameterType.Integer); + sb.EmitPush(parameter); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2Boolean() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.Boolean)); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2ByteArray() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.ByteArray)); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush2Signature() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new ContractParameter(ContractParameterType.Signature)); + byte[] tempArray = new byte[65]; + tempArray[0] = 0x40; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + enum TestEnum : byte + { + case1 = 0 + } + + [TestMethod] + public void TestEmitPush3() + { + TestEmitPush3Bool(); + TestEmitPush3ByteArray(); + TestEmitPush3String(); + TestEmitPush3BigInteger(); + TestEmitPush3ISerializable(); + TestEmitPush3Sbyte(); + TestEmitPush3Byte(); + TestEmitPush3Short(); + TestEmitPush3Ushort(); + TestEmitPush3Int(); + TestEmitPush3Uint(); + TestEmitPush3Long(); + TestEmitPush3Ulong(); + TestEmitPush3Enum(); + + ScriptBuilder sb = new ScriptBuilder(); + Action action = () => sb.EmitPush(new object()); + action.Should().Throw(); + } + + + private void TestEmitPush3Enum() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(TestEnum.case1); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Ulong() + { + ScriptBuilder sb = new ScriptBuilder(); + ulong temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Long() + { + ScriptBuilder sb = new ScriptBuilder(); + long temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Uint() + { + ScriptBuilder sb = new ScriptBuilder(); + uint temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Int() + { + ScriptBuilder sb = new ScriptBuilder(); + int temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Ushort() + { + ScriptBuilder sb = new ScriptBuilder(); + ushort temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Short() + { + ScriptBuilder sb = new ScriptBuilder(); + short temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Byte() + { + ScriptBuilder sb = new ScriptBuilder(); + byte temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Sbyte() + { + ScriptBuilder sb = new ScriptBuilder(); + sbyte temp = 0; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3ISerializable() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(UInt160.Zero); + byte[] tempArray = new byte[21]; + tempArray[0] = 0x14; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3BigInteger() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(BigInteger.Zero); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3String() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(""); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3ByteArray() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(new byte[] { 0x01 }); + byte[] tempArray = new byte[2]; + tempArray[0] = 0x01; + tempArray[1] = 0x01; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + private void TestEmitPush3Bool() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitPush(true); + byte[] tempArray = new byte[1]; + tempArray[0] = 0x51; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + [TestMethod] + public void TestEmitSysCall() + { + ScriptBuilder sb = new ScriptBuilder(); + sb.EmitSysCall(0, true); + byte[] tempArray = new byte[6]; + tempArray[0] = 0x51; + tempArray[1] = 0x68; + tempArray[2] = 0x00; + tempArray[3] = 0x00; + tempArray[4] = 0x00; + tempArray[5] = 0x00; + Assert.AreEqual(Encoding.Default.GetString(tempArray), Encoding.Default.GetString(sb.ToArray())); + } + + [TestMethod] + public void TestToParameter2() + { + TestToParaMeter2VMArray(); + TestToParameter2Map(); + TestToParameter2VMBoolean(); + TestToParameter2ByteArray(); + TestToParameter2Integer(); + TestToParameter2InteropInterface(); + + Action action = () => VM.Helper.ToParameter(null); + action.Should().Throw(); + } + + private void TestToParameter2InteropInterface() + { + StackItem item = new VM.Types.InteropInterface(new VM.Types.Boolean(true)); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.InteropInterface, parameter.Type); + } + + private void TestToParameter2Integer() + { + StackItem item = new VM.Types.Integer(0); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.Integer, parameter.Type); + Assert.AreEqual(BigInteger.Zero, parameter.Value); + } + + private void TestToParameter2ByteArray() + { + StackItem item = new VM.Types.ByteArray(new byte[] { 0x00 }); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.ByteArray, parameter.Type); + Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0x00 }), Encoding.Default.GetString((byte[])parameter.Value)); + } + + private void TestToParameter2VMBoolean() + { + StackItem item = new VM.Types.Boolean(true); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.Boolean, parameter.Type); + Assert.AreEqual(true, parameter.Value); + } + + private void TestToParameter2Map() + { + StackItem item = new VM.Types.Map(); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.Map, parameter.Type); + Assert.AreEqual(0, ((List>)parameter.Value).Count); + } + + private void TestToParaMeter2VMArray() + { + VM.Types.Array item = new VM.Types.Array(); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.Array, parameter.Type); + Assert.AreEqual(0, ((List)parameter.Value).Count); + } } }