ConfuserEx/Tests/AntiTamper.Test/AntiTamperTest.cs

70 строки
2.2 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Confuser.Core;
using Confuser.Core.Project;
using Confuser.UnitTest;
using Xunit;
using Xunit.Abstractions;
namespace AntiTamper.Test {
public sealed class AntiTamperTest {
private readonly ITestOutputHelper outputHelper;
public AntiTamperTest(ITestOutputHelper outputHelper) =>
this.outputHelper = outputHelper ?? throw new ArgumentNullException(nameof(outputHelper));
[Theory]
[InlineData("normal")]
[InlineData("anti")]
[InlineData("jit", Skip = "Runtime Component of the JIT AntiTamper protection is broken.")]
[Trait("Category", "Protection")]
[Trait("Protection", "anti tamper")]
public async Task ProtectAntiTamperAndExecute(string antiTamperMode) {
var baseDir = Environment.CurrentDirectory;
var outputDir = Path.Combine(baseDir, "testtmp");
var inputFile = Path.Combine(baseDir, "AntiTamper.exe");
var outputFile = Path.Combine(outputDir, "AntiTamper.exe");
FileUtilities.ClearOutput(outputFile);
var proj = new ConfuserProject {
BaseDirectory = baseDir,
OutputDirectory = outputDir
};
proj.Rules.Add(new Rule() {
new SettingItem<Protection>("anti tamper") {
{ "mode", antiTamperMode }
}
});
proj.Add(new ProjectModule() { Path = inputFile });
var parameters = new ConfuserParameters {
Project = proj,
Logger = new XunitLogger(outputHelper)
};
await ConfuserEngine.Run(parameters);
Assert.True(File.Exists(outputFile));
Assert.NotEqual(FileUtilities.ComputeFileChecksum(inputFile), FileUtilities.ComputeFileChecksum(outputFile));
var info = new ProcessStartInfo(outputFile) {
RedirectStandardOutput = true,
UseShellExecute = false
};
using (var process = Process.Start(info)) {
var stdout = process.StandardOutput;
Assert.Equal("START", await stdout.ReadLineAsync());
Assert.Equal("This is a test.", await stdout.ReadLineAsync());
Assert.Equal("END", await stdout.ReadLineAsync());
Assert.Empty(await stdout.ReadToEndAsync());
Assert.True(process.HasExited);
Assert.Equal(42, process.ExitCode);
}
FileUtilities.ClearOutput(outputFile);
}
}
}