From aa365e460b6b3ee27c9bbd1e1ee655a8be0b110c Mon Sep 17 00:00:00 2001 From: Charis Zhao Date: Thu, 24 Oct 2019 14:02:12 +0800 Subject: [PATCH] Unit Test For RPC Module (#1111) --- .../Network/RPC/Models/UT_RpcBlock.cs | 29 ++++++++ .../Network/RPC/Models/UT_RpcBlockHeader.cs | 29 ++++++++ .../Network/RPC/Models/UT_RpcNep5Balance.cs | 66 +++++++++++++++++++ .../Network/RPC/Models/UT_RpcNep5Balances.cs | 66 +++++++++++++++++++ .../Network/RPC/Models/UT_RpcPeer.cs | 23 +++++++ .../Network/RPC/Models/UT_RpcPeers.cs | 44 +++++++++++++ .../Network/RPC/Models/UT_RpcRawMemPool.cs | 29 ++++++++ .../Network/RPC/Models/UT_RpcRequest.cs | 31 +++++++++ .../Network/RPC/Models/UT_RpcResponse.cs | 34 ++++++++++ .../Network/RPC/Models/UT_RpcVersion.cs | 27 ++++++++ neo.UnitTests/Network/RPC/UT_RpcClient.cs | 36 +++++++++- neo.UnitTests/Network/RPC/UT_RpcServer.cs | 48 ++++++++++++++ 12 files changed, 459 insertions(+), 3 deletions(-) create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcBlock.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcPeer.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcPeers.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcRawMemPool.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcRequest.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcResponse.cs create mode 100644 neo.UnitTests/Network/RPC/Models/UT_RpcVersion.cs create mode 100644 neo.UnitTests/Network/RPC/UT_RpcServer.cs diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcBlock.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcBlock.cs new file mode 100644 index 0000000000..860eafa076 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcBlock.cs @@ -0,0 +1,29 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.P2P.Payloads; +using Neo.Network.RPC.Models; +using System; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcBlock + { + [TestMethod] + public void TestToJson() + { + var rpcBlock = new RpcBlock(); + var block = new Block(); + TestUtils.SetupBlockWithValues(block, UInt256.Zero, out UInt256 _, out UInt160 _, out ulong _, out uint _, out Witness _, out Transaction[] _, 1); + rpcBlock.Block = block; + var json = rpcBlock.ToJson(); + json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000"); + json.Should().NotBeNull(); + + rpcBlock.Confirmations = 1; + rpcBlock.NextBlockHash = UInt256.Zero; + json = rpcBlock.ToJson(); + json["confirmations"].AsNumber().Should().Be(1); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.cs new file mode 100644 index 0000000000..85a604d3e8 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.cs @@ -0,0 +1,29 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.P2P.Payloads; +using Neo.Network.RPC.Models; +using System; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcBlockHeader + { + [TestMethod] + public void TestToJson() + { + var rpcBlockHeader = new RpcBlockHeader(); + var header = new Header(); + TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 _, out UInt160 _, out ulong _, out uint _, out Witness _); + rpcBlockHeader.Header = header; + var json = rpcBlockHeader.ToJson(); + json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000"); + json.Should().NotBeNull(); + + rpcBlockHeader.Confirmations = 1; + rpcBlockHeader.NextBlockHash = UInt256.Zero; + json = rpcBlockHeader.ToJson(); + json["confirmations"].AsNumber().Should().Be(1); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs new file mode 100644 index 0000000000..ee32c1998e --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs @@ -0,0 +1,66 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO.Json; +using Neo.Network.RPC.Models; +using System.Numerics; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcNep5Balance + { + private RpcNep5Balance balance; + + [TestInitialize] + public void Setup() + { + balance = new RpcNep5Balance(); + } + + [TestMethod] + public void TestAssetHash() + { + balance.AssetHash = UInt160.Zero; + balance.AssetHash.Should().Be(UInt160.Zero); + } + + [TestMethod] + public void TestAmount() + { + balance.Amount = BigInteger.Zero; + balance.Amount.Should().Be(BigInteger.Zero); + } + + [TestMethod] + public void TestLastUpdatedBlock() + { + balance.LastUpdatedBlock = 0; + balance.LastUpdatedBlock.Should().Be(0); + } + + [TestMethod] + public void TestToJson() + { + balance.AssetHash = UInt160.Zero; + balance.Amount = BigInteger.Zero; + balance.LastUpdatedBlock = 0; + var json = balance.ToJson(); + json["asset_hash"].AsString().Should().Be("0000000000000000000000000000000000000000"); + json["amount"].AsNumber().Should().Be(0); + json["last_updated_block"].AsNumber().Should().Be(0); + } + + [TestMethod] + public void TestFromJson() + { + var json = new JObject(); + json["asset_hash"] = "0000000000000000000000000000000000000000"; + json["amount"] = "0"; + json["last_updated_block"] = "0"; + var rpcNep5Balance = RpcNep5Balance.FromJson(json); + rpcNep5Balance.AssetHash.Should().Be(UInt160.Zero); + rpcNep5Balance.Amount.Should().Be(BigInteger.Zero); + rpcNep5Balance.LastUpdatedBlock.Should().Be(0); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.cs new file mode 100644 index 0000000000..57601626ab --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.cs @@ -0,0 +1,66 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO.Json; +using Neo.Network.RPC.Models; +using System.Numerics; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcNep5Balances + { + private RpcNep5Balances balances; + + [TestInitialize] + public void Setup() + { + balances = new RpcNep5Balances() + { + Address = "abc", + Balances = new RpcNep5Balance[] { + new RpcNep5Balance() + { + AssetHash = UInt160.Zero, + Amount = BigInteger.Zero, + LastUpdatedBlock = 0 + }, + new RpcNep5Balance() + { + AssetHash = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"), + Amount = new BigInteger(1), + LastUpdatedBlock = 1 + } + } + }; + } + + [TestMethod] + public void TestAddress() + { + balances.Address.Should().Be("abc"); + } + + [TestMethod] + public void TestBalances() + { + balances.Balances.Length.Should().Be(2); + } + + [TestMethod] + public void TestToJson() + { + var json = balances.ToJson(); + json["address"].AsString().Should().Be("abc"); + ((JArray)json["balance"]).Count.Should().Be(2); + } + + [TestMethod] + public void TestFromJson() + { + var json = balances.ToJson(); + var rpcNep5Balances = RpcNep5Balances.FromJson(json); + rpcNep5Balances.Address.Should().Be("abc"); + rpcNep5Balances.Balances.Length.Should().Be(2); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcPeer.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcPeer.cs new file mode 100644 index 0000000000..b5c5044d13 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcPeer.cs @@ -0,0 +1,23 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcPeer + { + [TestMethod] + public void TestToJson() + { + var rpcPeer = new RpcPeer() + { + Address = "abc", + Port = 800 + }; + var json = rpcPeer.ToJson(); + json["address"].AsString().Should().Be("abc"); + json["port"].AsNumber().Should().Be(800); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcPeers.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcPeers.cs new file mode 100644 index 0000000000..cb6f6ff611 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcPeers.cs @@ -0,0 +1,44 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO.Json; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcPeers + { + [TestMethod] + public void TestToJson() + { + var rpcPeers = new RpcPeers() + { + Unconnected = new RpcPeer[] { + new RpcPeer() + { + Address = "Unconnected", + Port = 600 + } + }, + Bad = new RpcPeer[] { + new RpcPeer() + { + Address = "Bad", + Port = 700 + } + }, + Connected = new RpcPeer[] { + new RpcPeer() + { + Address = "Connected", + Port = 800 + } + } + }; + var json = rpcPeers.ToJson(); + ((JArray)json["unconnected"]).Count.Should().Be(1); + ((JArray)json["bad"]).Count.Should().Be(1); + ((JArray)json["connected"]).Count.Should().Be(1); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcRawMemPool.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcRawMemPool.cs new file mode 100644 index 0000000000..e8cb9bad7e --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcRawMemPool.cs @@ -0,0 +1,29 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcRawMemPool + { + [TestMethod] + public void TestToJson() + { + var pool = new RpcRawMemPool + { + Height = 1, + Verified = new string[] { + "a", "b" + }, + UnVerified = new string[] { + "c", "d" + } + }; + var json = pool.ToJson(); + json["height"].AsNumber().Should().Be(1); + json["verified"].AsString().Should().Be("a,b"); + json["unverified"].AsString().Should().Be("c,d"); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcRequest.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcRequest.cs new file mode 100644 index 0000000000..8f2a3b4f74 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcRequest.cs @@ -0,0 +1,31 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO.Json; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcRequest + { + [TestMethod] + public void TestFromJson() + { + var req = new RpcRequest() + { + Id = 1, + Jsonrpc = "myrpc", + Method = "get", + Params = new JObject[] { + new JBoolean(true) + } + }; + var json = req.ToJson(); + var rpcRequest = RpcRequest.FromJson(json); + rpcRequest.Jsonrpc.Should().Be("myrpc"); + rpcRequest.Method.Should().Be("get"); + rpcRequest.Id.Should().Be(1); + rpcRequest.Params.Length.Should().Be(1); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcResponse.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcResponse.cs new file mode 100644 index 0000000000..5c90eed92d --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcResponse.cs @@ -0,0 +1,34 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO.Json; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcResponse + { + [TestMethod] + public void TestToJson() + { + var error = new RpcResponseError() + { + Code = 0, + Message = "msg", + Data = new JBoolean(true) + }; + var rep = new RpcResponse() + { + Id = 1, + Jsonrpc = "rpc", + Error = error, + Result = new JBoolean(true) + }; + var json = rep.ToJson(); + json["id"].AsNumber().Should().Be(1); + json["jsonrpc"].AsString().Should().Be("rpc"); + json["error"].AsString().Should().Be(error.ToJson().AsString()); + json["result"].AsBoolean().Should().BeTrue(); + } + } +} diff --git a/neo.UnitTests/Network/RPC/Models/UT_RpcVersion.cs b/neo.UnitTests/Network/RPC/Models/UT_RpcVersion.cs new file mode 100644 index 0000000000..cbb9603907 --- /dev/null +++ b/neo.UnitTests/Network/RPC/Models/UT_RpcVersion.cs @@ -0,0 +1,27 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.RPC.Models; + +namespace Neo.UnitTests.Network.RPC.Models +{ + [TestClass] + public class UT_RpcVersion + { + [TestMethod] + public void TestToJson() + { + var version = new RpcVersion() + { + TcpPort = 800, + WsPort = 900, + Nonce = 1, + UserAgent = "agent" + }; + var json = version.ToJson(); + json["topPort"].AsNumber().Should().Be(800); + json["wsPort"].AsNumber().Should().Be(900); + json["nonce"].AsNumber().Should().Be(1); + json["useragent"].AsString().Should().Be("agent"); + } + } +} diff --git a/neo.UnitTests/Network/RPC/UT_RpcClient.cs b/neo.UnitTests/Network/RPC/UT_RpcClient.cs index 7294d3ccb3..eb256cc6d1 100644 --- a/neo.UnitTests/Network/RPC/UT_RpcClient.cs +++ b/neo.UnitTests/Network/RPC/UT_RpcClient.cs @@ -1,3 +1,4 @@ +using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Moq.Protected; @@ -108,10 +109,14 @@ public void TestGetBlockHex() { JObject response = CreateResponse(1); response["result"] = "000000002deadfa82cbc4682f5800"; - MockResponse(response.ToString()); + MockResponse(response.ToString()); var result = rpc.GetBlockHex("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"); Assert.AreEqual("000000002deadfa82cbc4682f5800", result); + + MockResponse(response.ToString()); + result = rpc.GetBlockHex("100"); + Assert.AreEqual("000000002deadfa82cbc4682f5800", result); } [TestMethod] @@ -131,14 +136,21 @@ public void TestGetBlock() JObject json = block.ToJson(); JObject response = CreateResponse(1); response["result"] = json; - MockResponse(response.ToString()); + MockResponse(response.ToString()); var result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"); Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString()); Assert.IsNull(result.Confirmations); Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length); Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString()); + MockResponse(response.ToString()); + result = rpc.GetBlock("100"); + Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString()); + Assert.IsNull(result.Confirmations); + Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length); + Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString()); + // verbose with confirmations json["confirmations"] = 20; json["nextblockhash"] = "773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"; @@ -177,10 +189,14 @@ public void TestGetBlockHeaderHex() { JObject response = CreateResponse(1); response["result"] = "0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2"; - MockResponse(response.ToString()); + MockResponse(response.ToString()); var result = rpc.GetBlockHeaderHex("100"); Assert.AreEqual("0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2", result); + + MockResponse(response.ToString()); + result = rpc.GetBlockHeaderHex("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"); + Assert.AreEqual("0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2", result); } [TestMethod] @@ -198,6 +214,11 @@ public void TestGetBlockHeader() Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString()); Assert.IsNull(result.Confirmations); + MockResponse(response.ToString()); + result = rpc.GetBlockHeader("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e"); + Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString()); + Assert.IsNull(result.Confirmations); + json["confirmations"] = 20; json["nextblockhash"] = "4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2"; MockResponse(response.ToString()); @@ -542,5 +563,14 @@ public void TestValidateAddress() var result = rpc.ValidateAddress("AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"); Assert.AreEqual(json.ToString(), result.ToJson().ToString()); } + + [TestMethod] + public void TestConstructorByUrlAndDispose() + { + //dummy url for test + var client = new RpcClient("http://www.xxx.yyy"); + Action action = () => client.Dispose(); + action.Should().NotThrow(); + } } } diff --git a/neo.UnitTests/Network/RPC/UT_RpcServer.cs b/neo.UnitTests/Network/RPC/UT_RpcServer.cs new file mode 100644 index 0000000000..fae945a21a --- /dev/null +++ b/neo.UnitTests/Network/RPC/UT_RpcServer.cs @@ -0,0 +1,48 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Network.RPC; +using System; +using System.Net; + +namespace Neo.UnitTests.Network.RPC +{ + [TestClass] + public class UT_RpcServer + { + private RpcServer server; + + [TestInitialize] + public void Setup() + { + var system = TestBlockchain.InitializeMockNeoSystem(); + server = new RpcServer(system); + } + + [TestCleanup] + public void TestDispose() + { + server.Dispose(); + } + + [TestMethod] + public void TestWallet() + { + var wallet = TestUtils.GenerateTestWallet(); + server.Wallet = wallet; + server.Wallet.Should().Be(wallet); + } + + [TestMethod] + public void TestMaxGasInvoke() + { + server.MaxGasInvoke.Should().Be(0); + } + + [TestMethod] + public void TestStart() + { + Action action = () => server.Start(IPAddress.Parse("127.0.0.1"), 8999); + action.Should().NotThrow(); + } + } +}