Add NUnit test chassis and Powershelll test generator scripts

This commit is contained in:
Katy Coe
2019-10-21 00:28:52 +02:00
parent 982b4b168d
commit 5f26b2d6f0
11 changed files with 149268 additions and 4 deletions

View File

@@ -1,13 +1,15 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio Version 16
VisualStudioVersion = 15.0.26228.9 VisualStudioVersion = 16.0.29409.12
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bin2Object", "Bin2Object\Bin2Object\Bin2Object.csproj", "{55382D6C-01B6-4AFD-850C-7A79DAB6F270}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bin2Object", "Bin2Object\Bin2Object\Bin2Object.csproj", "{55382D6C-01B6-4AFD-850C-7A79DAB6F270}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Il2CppInspector", "Il2CppInspector\Il2CppInspector.csproj", "{E4721466-CC6F-47EB-AD48-F4DE70D77E5C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Il2CppInspector", "Il2CppInspector\Il2CppInspector.csproj", "{E4721466-CC6F-47EB-AD48-F4DE70D77E5C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Il2CppDumper", "Il2CppDumper\Il2CppDumper.csproj", "{EA4C27DF-4640-48DF-8CAF-5587884CAF30}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Il2CppDumper", "Il2CppDumper\Il2CppDumper.csproj", "{EA4C27DF-4640-48DF-8CAF-5587884CAF30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Il2CppTests", "Il2CppTests\Il2CppTests.csproj", "{389E4BDF-A749-4554-848B-32B3B6EE5684}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,8 +29,15 @@ Global
{EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Debug|Any CPU.Build.0 = Debug|Any CPU {EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Release|Any CPU.ActiveCfg = Release|Any CPU {EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Release|Any CPU.Build.0 = Release|Any CPU {EA4C27DF-4640-48DF-8CAF-5587884CAF30}.Release|Any CPU.Build.0 = Release|Any CPU
{389E4BDF-A749-4554-848B-32B3B6EE5684}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{389E4BDF-A749-4554-848B-32B3B6EE5684}.Debug|Any CPU.Build.0 = Debug|Any CPU
{389E4BDF-A749-4554-848B-32B3B6EE5684}.Release|Any CPU.ActiveCfg = Release|Any CPU
{389E4BDF-A749-4554-848B-32B3B6EE5684}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2F9DB35-5C65-4A94-8DD8-36FF9F3590C7}
EndGlobalSection
EndGlobal EndGlobal

View File

@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="TestAssemblies\**" />
<Compile Remove="TestBinaries\**" />
<Compile Remove="TestExpectedResults\**" />
<EmbeddedResource Remove="TestAssemblies\**" />
<EmbeddedResource Remove="TestBinaries\**" />
<EmbeddedResource Remove="TestExpectedResults\**" />
<None Remove="TestAssemblies\**" />
<None Remove="TestBinaries\**" />
<None Remove="TestExpectedResults\**" />
</ItemGroup>
<ItemGroup>
<Content Include="TestExpectedResults\GameAssembly-Generics.cs" />
<Content Include="TestExpectedResults\GameAssembly-Methods.cs" />
<Content Include="TestExpectedResults\Generics.cs" />
<Content Include="TestExpectedResults\Methods.cs" />
<Content Include="TestSources\Generics.cs" />
<Content Include="TestSources\Methods.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Il2CppDumper\Il2CppDumper.csproj" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

53
Il2CppTests/TestRunner.cs Normal file
View File

@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using NUnit.Framework;
namespace Il2CppInspector
{
[TestFixture]
public partial class TestRunner
{
private void runTest(string testPath) {
// Android
var testFile = testPath + @"\" + Path.GetFileName(testPath) + ".so";
// Windows
if (!File.Exists(testFile))
testFile = testPath + @"\" + Path.GetFileName(testPath) + ".dll";
// iOS
if (!File.Exists(testFile))
testFile = testPath + @"\" + Path.GetFileName(testPath);
// Android
if (!File.Exists(testFile))
testFile = testPath + @"\libil2cpp.so";
var inspectors = Il2CppInspector.LoadFromFile(testFile, testPath + @"\global-metadata.dat");
// If null here, there was a problem parsing the files
if (inspectors == null)
throw new Exception("Could not understand IL2CPP binary or metadata");
if (inspectors.Count == 0)
throw new Exception("Could not find any images in the IL2CPP binary");
// Dump each image in the binary separately
int i = 0;
foreach (var il2cpp in inspectors)
new Il2CppDumper(il2cpp).WriteFile(testPath + @"\test-result" + (i++ > 0 ? "-" + (i - 1) : "") + ".cs");
// Compare test result with expected result
for (i = 0; i < inspectors.Count; i++) {
var expected = File.ReadAllLines(testPath + @"\..\..\TestExpectedResults\" + Path.GetFileName(testPath) + (i > 0 ? "-" + (i - 1) : "") + ".cs");
var actual = File.ReadAllLines(testPath + @"\test-result" + (i > 0 ? "-" + (i - 1) : "") + ".cs");
// Get rid of blank lines and trim the remaining lines
expected = (from l in expected where !string.IsNullOrWhiteSpace(l) select l.Trim()).ToArray();
actual = (from l in actual where !string.IsNullOrWhiteSpace(l) select l.Trim()).ToArray();
Assert.IsTrue(expected.SequenceEqual(actual));
}
}
}
}

View File

@@ -0,0 +1,15 @@
/*
Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
All rights reserved.
*/
namespace Il2CppTests.TestSources
{
// Generic class
internal class GenericClass<T>
{
// Generic method using generic type parameter of class
public void GenericMethodWithClassGenericTypeParameter(T v) { }
}
}

View File

@@ -0,0 +1,22 @@
/*
Copyright 2017-2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
All rights reserved.
*/
using System.Text;
namespace Il2CppTests.TestSources
{
internal class Test
{
// A parameter-less method
public void ParameterlessMethod() { }
// Method with value type return type
public int ValueTypeReturnMethod() => 0;
// Method with reference type return type
public StringBuilder ReferenceTypeReturnMethod() => new StringBuilder();
}
}

View File

@@ -0,0 +1,100 @@
# Copyright 2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
# All rights reserved.
# Compile all of the test items in TestSources via IL2CPP to produce the binaries necessary to run the tests
# Requires Unity 2019.2.8f1 or later and Visual Studio 2017 (or MSBuild with C# 7+ support) or later to be installed
# Requires Android NDK r13b or newer for Android test builds (https://developer.android.com/ndk/downloads)
# Path to C¤ compiler (14.0 = Visual Studio 2017, 15.0 = Visual Studio 2019 etc.)
$CSC = (gci 'C:\Program Files (x86)\MSBuild\*\Bin\csc.exe' | sort name)[-1].FullName
# Path to latest installed version of Unity
# The introduction of Unity Hub changed the base path of the Unity editor
$UnityPath = (gci 'C:\Program Files\Unity\Hub\Editor\*\Editor\Data' | sort name)[-1].FullName
# Calculate Unity paths
$il2cpp = $UnityPath + '\il2cpp\build\il2cpp.exe'
$mscorlib = $UnityPath + '\Mono\lib\mono\unity\mscorlib.dll'
$AndroidPlayer = $UnityPath + '\PlaybackEngines\AndroidPlayer'
# Path to the Android NDK
# Different Unity versions require specific NDKs, see the section Change the NDK at:
# The NDK can also be installed standalone without AndroidPlayer
# https://docs.unity3d.com/2019.1/Documentation/Manual/android-sdksetup.html
$AndroidNDK = $AndroidPlayer + '\NDK'
# Check that everything is installed
if (!(Test-Path -Path $CSC -PathType leaf)) {
echo "Could not find C¤ compiler csc.exe at '$CSC' - aborting"
Exit
} else {
echo "Using C# compiler at '$CSC'"
}
if (!(Test-Path -Path $UnityPath -PathType container)) {
echo "Could not find Unity editor at '$UnityPath' - aborting"
Exit
} else {
echo "Using Unity installation at '$UnityPath'"
}
if (!(Test-Path -Path $AndroidNDK -PathType container)) {
echo "Could not find Android NDK at '$AndroidNDK' - aborting"
Exit
}
if (!(Test-Path -Path $il2cpp -PathType leaf)) {
echo "Could not find Unity IL2CPP build support - aborting"
Exit
}
if (!(Test-Path -Path $AndroidPlayer -PathType container)) {
echo "Could not find Unity Android build support - aborting"
Exit
}
# Workspace paths
$src = "$PSScriptRoot/TestSources"
$asm = "$PSScriptRoot/TestAssemblies"
$bin = "$PSScriptRoot/TestBinaries"
# We try to make the arguments as close as possible to a real Unity build
# "--lump-runtime-library" was added to reduce the number of C++ files generated by UnityEngine (Unity 2019)
$arg = '--convert-to-cpp', '--compile-cpp', '--libil2cpp-static', '--configuration=Release', `
'--emit-null-checks', '--enable-array-bounds-check', `
'--map-file-parser=$UnityPath\il2cpp\MapFileParser\MapFileParser.exe'
# Prepare output folders
md $asm, $bin 2>&1 >$null
# Compile all .cs files in TestSources
echo "Compiling source code..."
gci $src | % { & $csc "/t:library" "/nologo" "/out:$asm/$($_.BaseName).dll" "$src/$_" }
# Run IL2CPP on all generated assemblies for both x86 and ARM
# Earlier builds of Unity included mscorlib.dll automatically; in current versions we must specify its location
gci $asm | % {
# x86
$name = "GameAssembly-$($_.BaseName)"
echo "Running il2cpp for test assembly $name (Windows/x86)..."
md $bin/$name 2>&1 >$null
& $il2cpp $arg '--platform=WindowsDesktop', '--architecture=x86', `
"--assembly=$asm/$_,$mscorlib", `
"--outputpath=$bin/$name/$name.dll"
mv -Force $bin/$name/Data/metadata/global-metadata.dat $bin/$name
rm -Force -Recurse $bin/$name/Data
# ARM
$name = "$($_.BaseName)"
echo "Running il2cpp for test assembly $name (Android/ARMv7)..."
md $bin/$name 2>&1 >$null
& $il2cpp $arg '--platform=Android', '--architecture=ARMv7', `
"--assembly=$asm/$_,$mscorlib", `
"--outputpath=$bin/$name/$name.so", `
"--additional-include-directories=$AndroidPlayer/Tools/bdwgc/include" `
"--additional-include-directories=$AndroidPlayer/Tools/libil2cpp/include" `
"--tool-chain-path=$AndroidNDK"
mv -Force $bin/$name/Data/metadata/global-metadata.dat $bin/$name
rm -Force -Recurse $bin/$name/Data
}
# Generate test stubs
& "$PSScriptRoot/generate-tests.ps1"

View File

@@ -0,0 +1,33 @@
# Copyright 2019 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com
# All rights reserved.
# Generate Tests.cs
$testGen = "$PSScriptRoot/Tests.cs"
$bin = "$PSScriptRoot/TestBinaries"
echo @"
// THIS FILE IS AUTO-GENERATED BY GENERATE-TESTS.PS1
// DO NOT EDIT
using NUnit.Framework;
namespace Il2CppInspector
{
public partial class TestRunner
{
"@ > $testGen
gci $bin | % {
echo @"
[Test]
public void $($_.Name.Replace("-","_").Replace(" ","_").Replace(".","_"))() {
runTest(@"$($_.FullName)");
}
"@ >> $testGen
}
echo @"
}
}
"@ >> $testGen