Tests: Update incorrect CppTypeDeclaration test and add alignment tests

This commit is contained in:
Katy Coe
2020-08-13 06:57:41 +02:00
parent 4dd3e7cb92
commit d1aafee184
2 changed files with 42 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2020 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com Copyright 2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved. All rights reserved.
*/ */
@@ -30,92 +30,102 @@ namespace Il2CppInspector
// Ensure we can interpret every header from every version of Unity without errors // Ensure we can interpret every header from every version of Unity without errors
// This will throw InvalidOperationException if there is a problem // This will throw InvalidOperationException if there is a problem
foreach (var unityTypeHeader in unityTypeHeaders) { foreach (var unityTypeHeader in unityTypeHeaders) {
var cppTypes = new CppTypeCollection(64); var cppUnityHeaderTypes = new CppTypeCollection(64);
cppTypes.AddFromDeclarationText(unityTypeHeader.GetText()); cppUnityHeaderTypes.AddFromDeclarationText(unityTypeHeader.GetText());
foreach (var cppType in cppTypes.Types) foreach (var cppType in cppUnityHeaderTypes.Types)
Debug.WriteLine("// " + cppType.Key + "\n" + cppType.Value.ToString("o")); Debug.WriteLine("// " + cppType.Key + "\n" + cppType.Value.ToString("o"));
} }
// Do a few sanity checks taken from real applications // Do a few sanity checks taken from real applications (64-bit)
// NOTE: Does not provide full code coverage! // NOTE: Does not provide full code coverage!
var cppTypes2 = CppTypeCollection.FromUnityVersion(new UnityVersion("2019.3.1f1")); var cppTypes = CppTypeCollection.FromUnityVersion(new UnityVersion("2019.3.1f1"));
CppComplexType ct; CppComplexType ct;
CppField field; CppField field;
// Field offset tests
// Un-nested class // Un-nested class
ct = (CppComplexType) cppTypes2["Il2CppClass"]; ct = (CppComplexType) cppTypes["Il2CppClass"];
field = ct[0xD8].First(); field = ct[0xE0].First();
Assert.AreEqual(field.Name, "cctor_finished"); Assert.AreEqual("cctor_finished", field.Name);
field = ct[0x128].First(); field = ct[0x130].First();
Assert.AreEqual(field.Name, "vtable"); Assert.AreEqual("vtable", field.Name);
field = ct["cctor_finished"]; field = ct["cctor_finished"];
Assert.AreEqual(field.OffsetBytes, 0xD8); Assert.AreEqual(0xE0, field.OffsetBytes);
field = ct["vtable"]; field = ct["vtable"];
Assert.AreEqual(field.OffsetBytes, 0x128); Assert.AreEqual(0x130, field.OffsetBytes);
// Nested class // Nested class
ct = (CppComplexType) cppTypes2["Il2CppClass_Merged"]; ct = (CppComplexType) cppTypes["Il2CppClass_Merged"];
var fields = ct.Flattened; var fields = ct.Flattened;
field = fields[0xD8].First(); field = fields[0xE0].First();
Assert.AreEqual(field.Name, "cctor_finished"); Assert.AreEqual("cctor_finished", field.Name);
field = fields[0x128].First(); field = fields[0x130].First();
Assert.AreEqual(field.Name, "vtable"); Assert.AreEqual("vtable", field.Name);
field = fields["cctor_finished"]; field = fields["cctor_finished"];
Assert.AreEqual(field.OffsetBytes, 0xD8); Assert.AreEqual(0xE0, field.OffsetBytes);
field = fields["vtable"]; field = fields["vtable"];
Assert.AreEqual(field.OffsetBytes, 0x128); Assert.AreEqual(0x130, field.OffsetBytes);
// Bitfield // Bitfield
ct = (CppComplexType) cppTypes2["Il2CppType"]; ct = (CppComplexType) cppTypes["Il2CppType"];
field = ct.Fields[0xB * 8 + 7].First(); field = ct.Fields[0xB * 8 + 7].First();
Assert.AreEqual(field.Name, "pinned"); Assert.AreEqual("pinned", field.Name);
// Nested fields // Nested fields
ct = (CppComplexType) cppTypes2["Il2CppWin32Decimal"]; ct = (CppComplexType) cppTypes["Il2CppWin32Decimal"];
fields = ct.Flattened; fields = ct.Flattened;
field = fields[0x08].First(); field = fields[0x08].First();
Assert.AreEqual(field.Name, "lo32"); Assert.AreEqual("lo32", field.Name);
field = fields[0x08].Last(); field = fields[0x08].Last();
Assert.AreEqual(field.Name, "lo64"); Assert.AreEqual("lo64", field.Name);
field = fields[0x0C].First(); field = fields[0x0C].First();
Assert.AreEqual(field.Name, "mid32"); Assert.AreEqual("mid32", field.Name);
// Pointer alias // Pointer alias
var alias = (CppAlias) cppTypes2.GetType("Il2CppHString"); var alias = (CppAlias) cppTypes.GetType("Il2CppHString");
Assert.AreEqual(alias.ElementType.GetType(), typeof(CppPointerType)); Assert.AreEqual(typeof(CppPointerType), alias.ElementType.GetType());
Assert.AreEqual(alias.ElementType.Name, "Il2CppHString__ *"); Assert.AreEqual("Il2CppHString__ *", alias.ElementType.Name);
// Typedef struct with no tag // Typedef struct with no tag
Assert.True(cppTypes2.Types.ContainsKey("Il2CppGenericMethodIndices")); Assert.True(cppTypes.Types.ContainsKey("Il2CppGenericMethodIndices"));
Assert.True(((CppComplexType)cppTypes2["Il2CppGenericMethodIndices"]).ComplexValueType == ComplexValueType.Struct); Assert.True(((CppComplexType)cppTypes["Il2CppGenericMethodIndices"]).ComplexValueType == ComplexValueType.Struct);
// Alignment tests
// Il2CppArrayType has an int * (sizes) after three uint8_t
Assert.AreEqual(0x10, cppTypes.GetComplexType("Il2CppArrayType")["sizes"].OffsetBytes);
// Il2CppSafeArray has a uint32_t (element_size) after two uint16_t
Assert.AreEqual(0x4, cppTypes.GetComplexType("Il2CppSafeArray")["element_size"].OffsetBytes);
} }
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2017-2020 Katy Coe - http://www.hearthcode.org - http://www.djkaty.com Copyright 2017-2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
Copyright 2020 Robert Xiao - https://robertxiao.ca Copyright 2020 Robert Xiao - https://robertxiao.ca
All rights reserved. All rights reserved.