From 62f6bebf38833d378c3f281be892f2f0cd2fcbc3 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Sat, 22 Aug 2020 14:47:47 +0300 Subject: [PATCH] Move Run method from TestRunner to TestBase, remove TestRunner --- .../EnhancedStrongNameTest.cs | 2 +- .../InheritCustomAttributeTest.cs | 2 +- .../RenameDynamicMethodTest.cs | 2 +- .../ConstantInliningTest.cs | 2 +- .../SignatureMismatchTest.cs | 2 +- Tests/AntiTamper.Test/AntiTamperTest.cs | 2 +- Tests/CompressorWithResx.Test/CompressTest.cs | 2 +- Tests/Confuser.UnitTest/TestBase.cs | 85 +++++++++++++++++ Tests/Confuser.UnitTest/TestRunner.cs | 91 ------------------- .../SignatureMismatch2Test.cs | 2 +- .../RenamingTest.cs | 2 +- .../RenameDataPropertyNameTest.cs | 2 +- Tests/WpfRenaming.Test/ProcessWpfTest.cs | 4 +- 13 files changed, 97 insertions(+), 103 deletions(-) delete mode 100644 Tests/Confuser.UnitTest/TestRunner.cs diff --git a/Tests/118_EnhancedStrongName.Test/EnhancedStrongNameTest.cs b/Tests/118_EnhancedStrongName.Test/EnhancedStrongNameTest.cs index 62ebf20..5207d2f 100644 --- a/Tests/118_EnhancedStrongName.Test/EnhancedStrongNameTest.cs +++ b/Tests/118_EnhancedStrongName.Test/EnhancedStrongNameTest.cs @@ -13,7 +13,7 @@ namespace EnhancedStrongName.Test { [Trait("Category", "core")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/118")] public async Task EnhancedStrongName() => - await TestRunner.Run("118_EnhancedStrongName.exe", + await Run("118_EnhancedStrongName.exe", new[] {"My strong key token: 79A18AF4CEA8A9BD", "My signature is valid!"}, null, outputHelper, diff --git a/Tests/123_InheritCustomAttr.Test/InheritCustomAttributeTest.cs b/Tests/123_InheritCustomAttr.Test/InheritCustomAttributeTest.cs index dc6ba65..edf46cb 100644 --- a/Tests/123_InheritCustomAttr.Test/InheritCustomAttributeTest.cs +++ b/Tests/123_InheritCustomAttr.Test/InheritCustomAttributeTest.cs @@ -19,7 +19,7 @@ namespace _123_InheritCustomAttr.Test { [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/123")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/161")] public async Task InheritCustomAttribute(string renameMode, bool flatten) => - await TestRunner.Run( + await Run( "123_InheritCustomAttr.exe", new[] {"Monday", "43", "1"}, new SettingItem("rename") {{"mode", renameMode}, {"flatten", flatten ? "True" : "False"}}, diff --git a/Tests/161_DynamicTypeRename.Test/RenameDynamicMethodTest.cs b/Tests/161_DynamicTypeRename.Test/RenameDynamicMethodTest.cs index b94a380..02549c7 100644 --- a/Tests/161_DynamicTypeRename.Test/RenameDynamicMethodTest.cs +++ b/Tests/161_DynamicTypeRename.Test/RenameDynamicMethodTest.cs @@ -18,7 +18,7 @@ namespace DynamicTypeRename.Test { [Trait("Protection", "rename")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/161")] public async Task RenameDynamicType(string renameMode, bool flatten) => - await TestRunner.Run( + await Run( "161_DynamicTypeRename.exe", new [] { "Type declaration done", diff --git a/Tests/193_ConstantsInlining.Test/ConstantInliningTest.cs b/Tests/193_ConstantsInlining.Test/ConstantInliningTest.cs index 7b96911..43ac5a5 100644 --- a/Tests/193_ConstantsInlining.Test/ConstantInliningTest.cs +++ b/Tests/193_ConstantsInlining.Test/ConstantInliningTest.cs @@ -14,7 +14,7 @@ namespace ConstantsInlining.Test { [Trait("Protection", "constants")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/193")] public async Task ConstantInlining() => - await TestRunner.Run(new[] {"193_ConstantsInlining.exe", "193_ConstantsInlining.Lib.dll"}, + await Run(new[] {"193_ConstantsInlining.exe", "193_ConstantsInlining.Lib.dll"}, new[] {"From External"}, new SettingItem("constants") {{"elements", "S"}}, outputHelper); } diff --git a/Tests/78_SignatureMismatch.Test/SignatureMismatchTest.cs b/Tests/78_SignatureMismatch.Test/SignatureMismatchTest.cs index 7d47b21..83bb965 100644 --- a/Tests/78_SignatureMismatch.Test/SignatureMismatchTest.cs +++ b/Tests/78_SignatureMismatch.Test/SignatureMismatchTest.cs @@ -14,7 +14,7 @@ namespace SignatureMismatch.Test { [Trait("Protection", "rename")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/78")] public async Task SignatureMismatch() => - await TestRunner.Run( + await Run( "78_SignatureMismatch.exe", new [] { "Dictionary created", diff --git a/Tests/AntiTamper.Test/AntiTamperTest.cs b/Tests/AntiTamper.Test/AntiTamperTest.cs index c85190f..6703484 100644 --- a/Tests/AntiTamper.Test/AntiTamperTest.cs +++ b/Tests/AntiTamper.Test/AntiTamperTest.cs @@ -16,7 +16,7 @@ namespace AntiTamper.Test { [Trait("Category", "Protection")] [Trait("Protection", "anti tamper")] public async Task ProtectAntiTamperAndExecute(string antiTamperMode) => - await TestRunner.Run("AntiTamper.exe", + await Run("AntiTamper.exe", new[] {"This is a test."}, new SettingItem("anti tamper") {{"mode", antiTamperMode}}, outputHelper); diff --git a/Tests/CompressorWithResx.Test/CompressTest.cs b/Tests/CompressorWithResx.Test/CompressTest.cs index 5248bb0..29eca78 100644 --- a/Tests/CompressorWithResx.Test/CompressTest.cs +++ b/Tests/CompressorWithResx.Test/CompressTest.cs @@ -16,7 +16,7 @@ namespace CompressorWithResx.Test { [Trait("Category", "Packer")] [Trait("Packer", "compressor")] public async Task CompressAndExecuteTest(string compatKey, string deriverKey, string resourceProtectionMode) => - await TestRunner.Run( + await Run( new[] {"CompressorWithResx.exe", Path.Combine("de", "CompressorWithResx.resources.dll")}, new[] {"Test (fallback)", "Test (deutsch)"}, resourceProtectionMode != "none" diff --git a/Tests/Confuser.UnitTest/TestBase.cs b/Tests/Confuser.UnitTest/TestBase.cs index 26caf3f..50ca485 100644 --- a/Tests/Confuser.UnitTest/TestBase.cs +++ b/Tests/Confuser.UnitTest/TestBase.cs @@ -1,4 +1,10 @@ using System; +using System.Diagnostics; +using System.IO; +using System.Threading.Tasks; +using Confuser.Core; +using Confuser.Core.Project; +using Xunit; using Xunit.Abstractions; namespace Confuser.UnitTest { @@ -7,5 +13,84 @@ namespace Confuser.UnitTest { public TestBase(ITestOutputHelper outputHelper) => this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper)); + + public async Task Run(string inputFileName, string[] expectedOutput, SettingItem protection, + ITestOutputHelper outputHelper, string outputDirSuffix = "", Action outputAction = null, SettingItem packer = null, + Action projectModuleAction = null) => + + await Run(new[] { inputFileName }, expectedOutput, protection, outputHelper, outputDirSuffix, outputAction, packer, + projectModuleAction); + + public async Task Run(string[] inputFileNames, string[] expectedOutput, SettingItem protection, ITestOutputHelper outputHelper, + string outputDirSuffix = "", Action outputAction = null, SettingItem packer = null, + Action projectModuleAction = null) { + + var baseDir = Environment.CurrentDirectory; + var outputDir = Path.Combine(baseDir, "obfuscated" + outputDirSuffix); + if (Directory.Exists(outputDir)) { + Directory.Delete(outputDir, true); + } + string entryInputFileName = Path.Combine(baseDir, inputFileNames[0]); + var entryOutputFileName = Path.Combine(outputDir, inputFileNames[0]); + var proj = new ConfuserProject { + BaseDirectory = baseDir, + OutputDirectory = outputDir, + Packer = packer + }; + + foreach (string name in inputFileNames) { + var projectModule = new ProjectModule { Path = Path.Combine(baseDir, name) }; + projectModuleAction?.Invoke(projectModule); + proj.Add(projectModule); + } + + if (protection != null) { + proj.Rules.Add(new Rule { protection }); + } + + var parameters = new ConfuserParameters { + Project = proj, + Logger = new XunitLogger(outputHelper, outputAction) + }; + + await ConfuserEngine.Run(parameters); + + for (var index = 0; index < inputFileNames.Length; index++) { + string name = inputFileNames[index]; + string outputName = Path.Combine(outputDir, name); + + bool exists; + if (index == 0) { + Assert.True(File.Exists(outputName)); + exists = true; + } + else { + exists = File.Exists(outputName); + } + + if (exists) { + // Check if output assemblies is obfuscated + Assert.NotEqual(FileUtilities.ComputeFileChecksum(Path.Combine(baseDir, name)), + FileUtilities.ComputeFileChecksum(outputName)); + } + } + + if (Path.GetExtension(entryInputFileName) == ".exe") { + var info = new ProcessStartInfo(entryOutputFileName) { RedirectStandardOutput = true, UseShellExecute = false }; + using (var process = Process.Start(info)) { + var stdout = process.StandardOutput; + Assert.Equal("START", await stdout.ReadLineAsync()); + + foreach (string line in expectedOutput) { + Assert.Equal(line, await stdout.ReadLineAsync()); + } + + Assert.Equal("END", await stdout.ReadLineAsync()); + Assert.Empty(await stdout.ReadToEndAsync()); + Assert.True(process.HasExited); + Assert.Equal(42, process.ExitCode); + } + } + } } } diff --git a/Tests/Confuser.UnitTest/TestRunner.cs b/Tests/Confuser.UnitTest/TestRunner.cs deleted file mode 100644 index 4f9629d..0000000 --- a/Tests/Confuser.UnitTest/TestRunner.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Threading.Tasks; -using Confuser.Core; -using Confuser.Core.Project; -using Xunit; -using Xunit.Abstractions; - -namespace Confuser.UnitTest { - public static class TestRunner { - public static async Task Run(string inputFileName, string[] expectedOutput, SettingItem protection, - ITestOutputHelper outputHelper, string outputDirSuffix = "", Action outputAction = null, SettingItem packer = null, - Action projectModuleAction = null) => - - await Run(new[] {inputFileName}, expectedOutput, protection, outputHelper, outputDirSuffix, outputAction, packer, - projectModuleAction); - - public static async Task Run(string[] inputFileNames, string[] expectedOutput, SettingItem protection, ITestOutputHelper outputHelper, - string outputDirSuffix = "", Action outputAction = null, SettingItem packer = null, - Action projectModuleAction = null) { - - var baseDir = Environment.CurrentDirectory; - var outputDir = Path.Combine(baseDir, "obfuscated" + outputDirSuffix); - if (Directory.Exists(outputDir)) { - Directory.Delete(outputDir, true); - } - string entryInputFileName = Path.Combine(baseDir, inputFileNames[0]); - var entryOutputFileName = Path.Combine(outputDir, inputFileNames[0]); - var proj = new ConfuserProject { - BaseDirectory = baseDir, - OutputDirectory = outputDir, - Packer = packer - }; - - foreach (string name in inputFileNames) { - var projectModule = new ProjectModule {Path = Path.Combine(baseDir, name)}; - projectModuleAction?.Invoke(projectModule); - proj.Add(projectModule); - } - - if (protection != null) { - proj.Rules.Add(new Rule {protection}); - } - - var parameters = new ConfuserParameters { - Project = proj, - Logger = new XunitLogger(outputHelper, outputAction) - }; - - await ConfuserEngine.Run(parameters); - - for (var index = 0; index < inputFileNames.Length; index++) { - string name = inputFileNames[index]; - string outputName = Path.Combine(outputDir, name); - - bool exists; - if (index == 0) { - Assert.True(File.Exists(outputName)); - exists = true; - } - else { - exists = File.Exists(outputName); - } - - if (exists) { - // Check if output assemblies is obfuscated - Assert.NotEqual(FileUtilities.ComputeFileChecksum(Path.Combine(baseDir, name)), - FileUtilities.ComputeFileChecksum(outputName)); - } - } - - if (Path.GetExtension(entryInputFileName) == ".exe") { - var info = new ProcessStartInfo(entryOutputFileName) {RedirectStandardOutput = true, UseShellExecute = false}; - using (var process = Process.Start(info)) { - var stdout = process.StandardOutput; - Assert.Equal("START", await stdout.ReadLineAsync()); - - foreach (string line in expectedOutput) { - Assert.Equal(line, await stdout.ReadLineAsync()); - } - - Assert.Equal("END", await stdout.ReadLineAsync()); - Assert.Empty(await stdout.ReadToEndAsync()); - Assert.True(process.HasExited); - Assert.Equal(42, process.ExitCode); - } - } - } - } -} diff --git a/Tests/SignatureMismatch2.Test/SignatureMismatch2Test.cs b/Tests/SignatureMismatch2.Test/SignatureMismatch2Test.cs index 81ea998..68cb707 100644 --- a/Tests/SignatureMismatch2.Test/SignatureMismatch2Test.cs +++ b/Tests/SignatureMismatch2.Test/SignatureMismatch2Test.cs @@ -14,7 +14,7 @@ namespace SignatureMismatch2.Test { [Trait("Protection", "rename")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/187")] public async Task SignatureMismatch2() => - await TestRunner.Run( + await Run( new [] { "SignatureMismatch2.exe", "SignatureMismatch2Helper.dll" }, new [] { "External" }, new SettingItem("rename") { ["renPublic"] = "true" }, diff --git a/Tests/VisualBasicRenamingResx.Test/RenamingTest.cs b/Tests/VisualBasicRenamingResx.Test/RenamingTest.cs index 6a26a90..ea5ba0b 100644 --- a/Tests/VisualBasicRenamingResx.Test/RenamingTest.cs +++ b/Tests/VisualBasicRenamingResx.Test/RenamingTest.cs @@ -14,7 +14,7 @@ namespace VisualBasicRenamingResx.Test{ [Trait("Protection", "rename")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/25")] public async Task ProtectAndExecuteTest() => - await TestRunner.Run("VisualBasicRenamingResx.exe", + await Run("VisualBasicRenamingResx.exe", new[] {"Test (neutral)"}, new SettingItem("rename"), outputHelper); diff --git a/Tests/WinFormsRenaming.Test/RenameDataPropertyNameTest.cs b/Tests/WinFormsRenaming.Test/RenameDataPropertyNameTest.cs index 1cf085d..f26f366 100644 --- a/Tests/WinFormsRenaming.Test/RenameDataPropertyNameTest.cs +++ b/Tests/WinFormsRenaming.Test/RenameDataPropertyNameTest.cs @@ -15,7 +15,7 @@ namespace WinFormsRenaming.Test { [Trait("Technology", "Windows Forms")] [Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/54")] public async Task RenameWindowsFormsTest() => - await TestRunner.Run( + await Run( "WinFormsRenaming.dll", null, new SettingItem("rename"), diff --git a/Tests/WpfRenaming.Test/ProcessWpfTest.cs b/Tests/WpfRenaming.Test/ProcessWpfTest.cs index 64b9cfc..03aae56 100644 --- a/Tests/WpfRenaming.Test/ProcessWpfTest.cs +++ b/Tests/WpfRenaming.Test/ProcessWpfTest.cs @@ -15,7 +15,7 @@ namespace WpfRenaming.Test { [Trait("Protection", "rename")] [Trait("Technology", "WPF")] public async Task ProcessWithoutObfuscationTest() => - await TestRunner.Run( + await Run( "WpfRenaming.dll", null, null, @@ -26,7 +26,7 @@ namespace WpfRenaming.Test { [Trait("Protection", "rename")] [Trait("Technology", "WPF")] public async Task ProcessWithObfuscationTest() => - await TestRunner.Run( + await Run( "WpfRenaming.dll", null, new SettingItem("rename"),