Move Run method from TestRunner to TestBase, remove TestRunner

This commit is contained in:
Ivan Kochurkin 2020-08-22 14:47:47 +03:00
Родитель 56e11d1d9b
Коммит 62f6bebf38
13 изменённых файлов: 97 добавлений и 103 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -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<Protection>("rename") {{"mode", renameMode}, {"flatten", flatten ? "True" : "False"}},

Просмотреть файл

@ -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",

Просмотреть файл

@ -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<Protection>("constants") {{"elements", "S"}}, outputHelper);
}

Просмотреть файл

@ -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",

Просмотреть файл

@ -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<Protection>("anti tamper") {{"mode", antiTamperMode}},
outputHelper);

Просмотреть файл

@ -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"

Просмотреть файл

@ -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> protection,
ITestOutputHelper outputHelper, string outputDirSuffix = "", Action<string> outputAction = null, SettingItem<Packer> packer = null,
Action<ProjectModule> projectModuleAction = null) =>
await Run(new[] { inputFileName }, expectedOutput, protection, outputHelper, outputDirSuffix, outputAction, packer,
projectModuleAction);
public async Task Run(string[] inputFileNames, string[] expectedOutput, SettingItem<Protection> protection, ITestOutputHelper outputHelper,
string outputDirSuffix = "", Action<string> outputAction = null, SettingItem<Packer> packer = null,
Action<ProjectModule> 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);
}
}
}
}
}

Просмотреть файл

@ -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> protection,
ITestOutputHelper outputHelper, string outputDirSuffix = "", Action<string> outputAction = null, SettingItem<Packer> packer = null,
Action<ProjectModule> projectModuleAction = null) =>
await Run(new[] {inputFileName}, expectedOutput, protection, outputHelper, outputDirSuffix, outputAction, packer,
projectModuleAction);
public static async Task Run(string[] inputFileNames, string[] expectedOutput, SettingItem<Protection> protection, ITestOutputHelper outputHelper,
string outputDirSuffix = "", Action<string> outputAction = null, SettingItem<Packer> packer = null,
Action<ProjectModule> 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);
}
}
}
}
}

Просмотреть файл

@ -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<Protection>("rename") { ["renPublic"] = "true" },

Просмотреть файл

@ -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<Protection>("rename"),
outputHelper);

Просмотреть файл

@ -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<Protection>("rename"),

Просмотреть файл

@ -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<Protection>("rename"),