From b945f8bae7d44ae98f660612f578bf5f7f725780 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Sat, 3 Nov 2018 12:12:05 -0300 Subject: [PATCH 1/6] basic unit testing for SimplePolicyPlugin --- .../SimplePolicy.UnitTests.csproj | 26 ++++++++++++++++ SimplePolicy.UnitTests/UT_SimplePolicy.cs | 30 +++++++++++++++++++ SimplePolicy.UnitTests/config.json | 10 +++++++ SimplePolicy/SimplePolicyPlugin.cs | 10 +++++++ 4 files changed, 76 insertions(+) create mode 100644 SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj create mode 100644 SimplePolicy.UnitTests/UT_SimplePolicy.cs create mode 100644 SimplePolicy.UnitTests/config.json diff --git a/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj b/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj new file mode 100644 index 000000000..80d047043 --- /dev/null +++ b/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj @@ -0,0 +1,26 @@ + + + + Exe + netcoreapp2.0 + SimplePolicy.UnitTests + SimplePolicy.UnitTests + + + + + + + + + + + + + + + diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs new file mode 100644 index 000000000..59f646614 --- /dev/null +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -0,0 +1,30 @@ +using Microsoft.Extensions.Configuration; +using System.IO; +using System.Reflection; +using System; +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Plugins; + + +namespace SimplePolicy.UnitTests +{ + [TestClass] + public class UT_SimplePolicy + { + SimplePolicyPlugin uut; + + [TestInitialize] + public void TestSetup() + { + uut = new SimplePolicyPlugin(); + } + + [TestMethod] + public void TestMaxTransactionsPerBlock() + { + SimplePolicyPlugin.GetMaxTransactionsPerBlock().Should().Be(10); + SimplePolicyPlugin.GetMaxFreeTransactionsPerBlock().Should().Be(3); + } + } +} diff --git a/SimplePolicy.UnitTests/config.json b/SimplePolicy.UnitTests/config.json new file mode 100644 index 000000000..ed20082d9 --- /dev/null +++ b/SimplePolicy.UnitTests/config.json @@ -0,0 +1,10 @@ +{ + "PluginConfiguration": { + "MaxTransactionsPerBlock": 500, + "MaxFreeTransactionsPerBlock": 20, + "BlockedAccounts": { + "Type": "AllowAll", + "List": [] + } + } +} diff --git a/SimplePolicy/SimplePolicyPlugin.cs b/SimplePolicy/SimplePolicyPlugin.cs index f292090aa..ae6ba1ee4 100644 --- a/SimplePolicy/SimplePolicyPlugin.cs +++ b/SimplePolicy/SimplePolicyPlugin.cs @@ -12,6 +12,16 @@ public class SimplePolicyPlugin : Plugin, ILogPlugin, IPolicyPlugin { private static string log_dictionary = Path.Combine(AppContext.BaseDirectory, "Logs"); + public static int GetMaxTransactionsPerBlock() + { + return Settings.Default.MaxTransactionsPerBlock; + } + + public static int GetMaxFreeTransactionsPerBlock() + { + return Settings.Default.MaxFreeTransactionsPerBlock; + } + public bool FilterForMemoryPool(Transaction tx) { switch (Settings.Default.BlockedAccounts.Type) From 4dbfa0f9fc6572fd98c8edc96bdfc9c247246863 Mon Sep 17 00:00:00 2001 From: Igor Machado Date: Sat, 3 Nov 2018 12:17:59 -0300 Subject: [PATCH 2/6] moved config.json for tests --- .../Debug/netcoreapp2.0/Plugins/SimplePolicy}/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename SimplePolicy.UnitTests/{ => bin/Debug/netcoreapp2.0/Plugins/SimplePolicy}/config.json (59%) diff --git a/SimplePolicy.UnitTests/config.json b/SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json similarity index 59% rename from SimplePolicy.UnitTests/config.json rename to SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json index ed20082d9..b0edc619b 100644 --- a/SimplePolicy.UnitTests/config.json +++ b/SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json @@ -1,7 +1,7 @@ { "PluginConfiguration": { - "MaxTransactionsPerBlock": 500, - "MaxFreeTransactionsPerBlock": 20, + "MaxTransactionsPerBlock": 10, + "MaxFreeTransactionsPerBlock": 3, "BlockedAccounts": { "Type": "AllowAll", "List": [] From 59d6f8f702ecb30de31fa2a58913ffa47da819bd Mon Sep 17 00:00:00 2001 From: erikzhang Date: Sat, 3 Nov 2018 16:37:30 +0100 Subject: [PATCH 3/6] update .sln file --- neo-plugins.sln | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/neo-plugins.sln b/neo-plugins.sln index bf0ece785..83027c37d 100644 --- a/neo-plugins.sln +++ b/neo-plugins.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "StatesDumpe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplePolicy.UnitTests", "SimplePolicy.UnitTests\SimplePolicy.UnitTests.csproj", "{0DA81327-DC81-4960-91C8-4C5F8B9B804C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.Build.0 = Debug|Any CPU {B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.ActiveCfg = Release|Any CPU {B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.Build.0 = Release|Any CPU + {0DA81327-DC81-4960-91C8-4C5F8B9B804C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0DA81327-DC81-4960-91C8-4C5F8B9B804C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0DA81327-DC81-4960-91C8-4C5F8B9B804C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0DA81327-DC81-4960-91C8-4C5F8B9B804C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From cbeb3fc54e162c76534f58f9f8a328d7d4e24e5f Mon Sep 17 00:00:00 2001 From: erikzhang Date: Sat, 3 Nov 2018 16:40:49 +0100 Subject: [PATCH 4/6] remove useless code --- SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj b/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj index 80d047043..d49e54aec 100644 --- a/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj +++ b/SimplePolicy.UnitTests/SimplePolicy.UnitTests.csproj @@ -1,4 +1,4 @@ - + Exe @@ -18,9 +18,4 @@ - From c3487d12aba3b3e59ddf39719cfe391237737181 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Sat, 3 Nov 2018 16:59:44 +0100 Subject: [PATCH 5/6] remove config.json --- SimplePolicy.UnitTests/UT_SimplePolicy.cs | 4 ++-- .../netcoreapp2.0/Plugins/SimplePolicy/config.json | 10 ---------- SimplePolicy/Helper.cs | 14 ++++++++++++++ SimplePolicy/Settings.cs | 12 +++--------- SimplePolicy/SimplePolicy.csproj | 4 ++-- 5 files changed, 21 insertions(+), 23 deletions(-) delete mode 100644 SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json create mode 100644 SimplePolicy/Helper.cs diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index 59f646614..f928d73dd 100644 --- a/SimplePolicy.UnitTests/UT_SimplePolicy.cs +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -23,8 +23,8 @@ public void TestSetup() [TestMethod] public void TestMaxTransactionsPerBlock() { - SimplePolicyPlugin.GetMaxTransactionsPerBlock().Should().Be(10); - SimplePolicyPlugin.GetMaxFreeTransactionsPerBlock().Should().Be(3); + SimplePolicyPlugin.GetMaxTransactionsPerBlock().Should().Be(500); + SimplePolicyPlugin.GetMaxFreeTransactionsPerBlock().Should().Be(20); } } } diff --git a/SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json b/SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json deleted file mode 100644 index b0edc619b..000000000 --- a/SimplePolicy.UnitTests/bin/Debug/netcoreapp2.0/Plugins/SimplePolicy/config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "PluginConfiguration": { - "MaxTransactionsPerBlock": 10, - "MaxFreeTransactionsPerBlock": 3, - "BlockedAccounts": { - "Type": "AllowAll", - "List": [] - } - } -} diff --git a/SimplePolicy/Helper.cs b/SimplePolicy/Helper.cs new file mode 100644 index 000000000..895731aaf --- /dev/null +++ b/SimplePolicy/Helper.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Configuration; +using System; + +namespace Neo.Plugins +{ + internal static class Helper + { + public static T GetValueOrDefault(this IConfigurationSection section, T defaultValue, Func selector) + { + if (section.Value == null) return defaultValue; + return selector(section.Value); + } + } +} diff --git a/SimplePolicy/Settings.cs b/SimplePolicy/Settings.cs index 3a88c6290..11a603eb1 100644 --- a/SimplePolicy/Settings.cs +++ b/SimplePolicy/Settings.cs @@ -22,16 +22,10 @@ static Settings() public Settings(IConfigurationSection section) { - this.MaxTransactionsPerBlock = GetValueOrDefault(section.GetSection("MaxTransactionsPerBlock"), 500, p => int.Parse(p)); - this.MaxFreeTransactionsPerBlock = GetValueOrDefault(section.GetSection("MaxFreeTransactionsPerBlock"), 20, p => int.Parse(p)); + this.MaxTransactionsPerBlock = section.GetSection("MaxTransactionsPerBlock").GetValueOrDefault(500, p => int.Parse(p)); + this.MaxFreeTransactionsPerBlock = section.GetSection("MaxFreeTransactionsPerBlock").GetValueOrDefault(20, p => int.Parse(p)); this.BlockedAccounts = new BlockedAccounts(section.GetSection("BlockedAccounts")); } - - public T GetValueOrDefault(IConfigurationSection section, T defaultValue, Func selector) - { - if (section.Value == null) return defaultValue; - return selector(section.Value); - } } internal enum PolicyType : byte @@ -49,7 +43,7 @@ internal class BlockedAccounts public BlockedAccounts(IConfigurationSection section) { - this.Type = (PolicyType)Enum.Parse(typeof(PolicyType), section.GetSection("Type").Value, true); + this.Type = section.GetSection("Type").GetValueOrDefault(PolicyType.AllowAll, p => (PolicyType)Enum.Parse(typeof(PolicyType), p, true)); this.List = new HashSet(section.GetSection("List").GetChildren().Select(p => p.Value.ToScriptHash())); } } diff --git a/SimplePolicy/SimplePolicy.csproj b/SimplePolicy/SimplePolicy.csproj index 7ef0c6446..929091fc8 100644 --- a/SimplePolicy/SimplePolicy.csproj +++ b/SimplePolicy/SimplePolicy.csproj @@ -1,7 +1,7 @@  - 2.9.0 + 2.9.2 netstandard2.0;net47 Neo.Plugins @@ -14,7 +14,7 @@ - + From 06b6fa4921b9374cfbf69e245ca72a2d1c9ae51a Mon Sep 17 00:00:00 2001 From: erikzhang Date: Tue, 20 Nov 2018 15:28:24 +0800 Subject: [PATCH 6/6] mark `SimplePolicy` internals visible to unit tests --- SimplePolicy.UnitTests/UT_SimplePolicy.cs | 8 ++------ SimplePolicy/SimplePolicyPlugin.cs | 13 +++---------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/SimplePolicy.UnitTests/UT_SimplePolicy.cs b/SimplePolicy.UnitTests/UT_SimplePolicy.cs index f928d73dd..3f7d7b9e1 100644 --- a/SimplePolicy.UnitTests/UT_SimplePolicy.cs +++ b/SimplePolicy.UnitTests/UT_SimplePolicy.cs @@ -1,7 +1,3 @@ -using Microsoft.Extensions.Configuration; -using System.IO; -using System.Reflection; -using System; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Plugins; @@ -23,8 +19,8 @@ public void TestSetup() [TestMethod] public void TestMaxTransactionsPerBlock() { - SimplePolicyPlugin.GetMaxTransactionsPerBlock().Should().Be(500); - SimplePolicyPlugin.GetMaxFreeTransactionsPerBlock().Should().Be(20); + Settings.Default.MaxTransactionsPerBlock.Should().Be(500); + Settings.Default.MaxFreeTransactionsPerBlock.Should().Be(20); } } } diff --git a/SimplePolicy/SimplePolicyPlugin.cs b/SimplePolicy/SimplePolicyPlugin.cs index 02a4b639b..933c5ae86 100644 --- a/SimplePolicy/SimplePolicyPlugin.cs +++ b/SimplePolicy/SimplePolicyPlugin.cs @@ -5,6 +5,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("SimplePolicy.UnitTests")] namespace Neo.Plugins { @@ -12,16 +15,6 @@ public class SimplePolicyPlugin : Plugin, ILogPlugin, IPolicyPlugin { private static readonly string log_dictionary = Path.Combine(AppContext.BaseDirectory, "Logs"); - public static int GetMaxTransactionsPerBlock() - { - return Settings.Default.MaxTransactionsPerBlock; - } - - public static int GetMaxFreeTransactionsPerBlock() - { - return Settings.Default.MaxFreeTransactionsPerBlock; - } - public bool FilterForMemoryPool(Transaction tx) { if (!VerifySizeLimits(tx)) return false;