mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 03:41:18 +03:00
move tests to csv
This commit is contained in:
parent
565158d9bd
commit
2cdd9f1e83
@ -1,7 +1,10 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeArray_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fa1_003Fbc9d4e81_003FAssemblyCodeArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssemblyCodeMemory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F6e_003F09b667c6_003FAssemblyCodeMemory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACsvReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Ff7b87edd534764eebf2388a77d49e5cd9c6d49eb6788dca9b1c07d4545412715_003FCsvReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADefaultTypeConverter_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F64b864a5d465bc24fc4b55e1026aba213beb1733ef631abeca5a9f25357eda_003FDefaultTypeConverter_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADisassembler_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003Fd4_003Fad0818f9_003FDisassembler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFailAsserts_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F807c15a7d8383b1548dff1ae33270e637836659d9caecd676ea6f2c59f1c71a_003FFailAsserts_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGL_002Egen_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F54e6df16dd99323ba9b0682ce5d5dac3648ccd10aafd29d5f3fad52b62bf3f75_003FGL_002Egen_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIAssemblyCode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa494e0aa381c41ff9484df33e5edb42535e00_003F8c_003F9fe9bac2_003FIAssemblyCode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMatrix4x4_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FAdmin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fed6aa59cd75423c5b655901d6ec4fb4be48ab669fa6fb01b3a7a7f31be95_003FMatrix4x4_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
@ -20,5 +23,7 @@
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=a2ceb6d1_002D1e39_002D4063_002Dbbb5_002D2f7a30b9386e/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="RunTestsOnJson" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>xUnit::D6A1F5A9-0C7A-4F8F-B8C5-83E9D3F3A1D5::net8.0::X86DisassemblerTests.GeneralDisassemblerInstructionTests.RunTestsOnJson</TestId>
|
||||
<TestId>xUnit::D6A1F5A9-0C7A-4F8F-B8C5-83E9D3F3A1D5::net8.0::X86DisassemblerTests.PushRegTests.PushRegTests</TestId>
|
||||
<TestId>xUnit::D6A1F5A9-0C7A-4F8F-B8C5-83E9D3F3A1D5::net8.0::X86DisassemblerTests.RawFromFileDisassemblyTests</TestId>
|
||||
</TestAncestor>
|
||||
</SessionState></s:String></wpf:ResourceDictionary>
|
25
X86DisassemblerTests/CsvJsonConverter.cs
Normal file
25
X86DisassemblerTests/CsvJsonConverter.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System.Text.Json;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using CsvHelper.TypeConversion;
|
||||
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public sealed class CsvJsonConverter<T> : DefaultTypeConverter
|
||||
{
|
||||
public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
|
||||
{
|
||||
if (text is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<T>(text);
|
||||
}
|
||||
|
||||
public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
|
||||
{
|
||||
return JsonSerializer.Serialize(value);
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Xunit;
|
||||
using X86Disassembler.X86;
|
||||
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
/// <summary>
|
||||
/// General tests for the X86 disassembler using a JSON test file
|
||||
/// </summary>
|
||||
public class GeneralDisassemblerInstructionTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs tests on all instructions defined in the JSON file
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void RunTestsOnJson()
|
||||
{
|
||||
// Load the JSON test file from embedded resources
|
||||
using var jsonStream = Assembly.GetExecutingAssembly()
|
||||
.GetManifestResourceStream("X86DisassemblerTests.instruction_test.json");
|
||||
|
||||
if (jsonStream == null)
|
||||
{
|
||||
throw new InvalidOperationException("Could not find instruction_test.json embedded resource");
|
||||
}
|
||||
|
||||
// Deserialize the JSON file
|
||||
var instructions = JsonSerializer.Deserialize<List<JsonInstructionEntry>>(jsonStream)!;
|
||||
|
||||
// Run tests for each instruction
|
||||
for (var index = 0; index < instructions.Count; index++)
|
||||
{
|
||||
var test = instructions[index];
|
||||
// Convert hex string to byte array
|
||||
byte[] code = HexStringToByteArray(test.RawBytes);
|
||||
|
||||
// Create a disassembler with the code
|
||||
Disassembler disassembler = new Disassembler(code, 0x1000);
|
||||
|
||||
// Disassemble the code
|
||||
var disassembledInstructions = disassembler.Disassemble();
|
||||
|
||||
// Verify the number of instructions
|
||||
if (test.Disassembled.Count != disassembledInstructions.Count)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Failed verifying test {index}: {test.RawBytes}. Instruction count mismatch.\n" +
|
||||
$"Expected \"{test.Disassembled.Count}\", but got \"{disassembledInstructions.Count}\".\n" +
|
||||
$"Disassembled instructions:\n" +
|
||||
$"{string.Join("\n", disassembledInstructions)}"
|
||||
);
|
||||
}
|
||||
|
||||
// Verify each instruction
|
||||
for (int i = 0; i < test.Disassembled.Count; i++)
|
||||
{
|
||||
var expected = test.Disassembled[i];
|
||||
var actual = disassembledInstructions[i];
|
||||
|
||||
if (expected.Mnemonic != actual.Mnemonic)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Failed verifying test {index}: {test.RawBytes}. Instruction {i}. Mnemonic mismatch. " +
|
||||
$"Expected \"{expected.Mnemonic}\", but got {actual.Mnemonic}\""
|
||||
);
|
||||
}
|
||||
if (expected.Operands != actual.Operands)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Failed verifying test {index}: {test.RawBytes}. Instruction {i}. Operands mismatch. " +
|
||||
$"Expected \"{expected.Operands}\", but got \"{actual.Operands}\""
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a hex string to a byte array
|
||||
/// </summary>
|
||||
/// <param name="hex">The hex string to convert</param>
|
||||
/// <returns>The byte array</returns>
|
||||
private static byte[] HexStringToByteArray(string hex)
|
||||
{
|
||||
// Remove any non-hex characters
|
||||
hex = hex.Replace(" ", "")
|
||||
.Replace("-", "");
|
||||
|
||||
// Create a byte array
|
||||
byte[] bytes = new byte[hex.Length / 2];
|
||||
|
||||
// Convert each pair of hex characters to a byte
|
||||
for (int i = 0; i < hex.Length; i += 2)
|
||||
{
|
||||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
@ -69,13 +69,13 @@ public class SubInstructionTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32SignExtendedHandler for decoding SUB r/m32, negative imm8 instruction (sign-extended)
|
||||
/// Tests the SubImmFromRm32SignExtendedHandler for decoding SUB r/m32, imm8 instruction with negative value
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32SignExtendedHandler_DecodesSubRm32NegativeImm8_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EAX, 0xF0 (83 E8 F0) - Subtract sign-extended 0xF0 from EAX
|
||||
// SUB EAX, -0x10 (83 E8 F0) - Subtract sign-extended -0x10 from EAX
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xE8, 0xF0 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
@ -111,23 +111,25 @@ public class SubInstructionTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubR32Rm32Handler for decoding SUB r32, r/m32 instruction
|
||||
/// Tests a sequence of SUB instructions in a common pattern
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubR32Rm32Handler_DecodesSubR32Rm32_Correctly()
|
||||
public void SubInstruction_DecodesSubSequence_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EBX, EAX (2B D8) - Subtract EAX from EBX
|
||||
byte[] codeBuffer = new byte[] { 0x2B, 0xD8 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
// SUB ESP, 0x10 (83 EC 10) - Create stack space
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xEC, 0x10 };
|
||||
var disassembler = new Disassembler(codeBuffer, 0);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
var instructions = disassembler.Disassemble();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("ebx, eax", instruction.Operands);
|
||||
Assert.Single(instructions);
|
||||
|
||||
// Instruction: SUB ESP, 0x10
|
||||
Assert.Equal("sub", instructions[0].Mnemonic);
|
||||
Assert.Equal("esp, 0x10", instructions[0].Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -170,6 +172,26 @@ public class SubInstructionTests
|
||||
Assert.Equal("dword ptr [ebx+0x10], ecx", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubR32Rm32Handler for decoding SUB r32, r/m32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubR32Rm32Handler_DecodesSubR32Rm32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EBX, EAX (2B D8) - Subtract EAX from EBX
|
||||
byte[] codeBuffer = new byte[] { 0x2B, 0xD8 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("ebx, eax", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubR32Rm32Handler for decoding SUB r32, memory instruction
|
||||
/// </summary>
|
||||
|
@ -1,13 +0,0 @@
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
public class JsonInstructionEntry
|
||||
{
|
||||
public string RawBytes { get; set; } = "";
|
||||
public List<JsonDisassembledInstruction> Disassembled { get; set; } = [];
|
||||
}
|
||||
|
||||
public class JsonDisassembledInstruction
|
||||
{
|
||||
public string Mnemonic { get; set; } = "";
|
||||
public string Operands { get; set; } = "";
|
||||
}
|
141
X86DisassemblerTests/RawFromFileDisassemblyTests.cs
Normal file
141
X86DisassemblerTests/RawFromFileDisassemblyTests.cs
Normal file
@ -0,0 +1,141 @@
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
using X86Disassembler.X86;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
public class RawFromFileDisassemblyTests(ITestOutputHelper output)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("pushreg_tests.csv")]
|
||||
[InlineData("popreg_tests.csv")]
|
||||
public void RunTests(string file)
|
||||
{
|
||||
// Load the CSV test file from embedded resources
|
||||
using var stream = Assembly.GetExecutingAssembly()
|
||||
.GetManifestResourceStream($"X86DisassemblerTests.TestData.{file}");
|
||||
|
||||
if (stream == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not find {file} embedded resource");
|
||||
}
|
||||
|
||||
// Configure CSV reader with semicolon delimiter
|
||||
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
|
||||
{
|
||||
HasHeaderRecord = true,
|
||||
Delimiter = ";",
|
||||
BadDataFound = null // Ignore bad data
|
||||
};
|
||||
|
||||
using var streamReader = new StreamReader(stream);
|
||||
using var csvReader = new CsvReader(streamReader, config);
|
||||
|
||||
// Register class map for TestFromFileEntry
|
||||
csvReader.Context.RegisterClassMap<TestFromFileEntryMap>();
|
||||
|
||||
// Read all records from CSV
|
||||
var tests = csvReader.GetRecords<TestFromFileEntry>()
|
||||
.ToList();
|
||||
|
||||
// Run tests for each instruction
|
||||
for (var index = 0; index < tests.Count; index++)
|
||||
{
|
||||
var test = tests[index];
|
||||
|
||||
// Convert hex string to byte array
|
||||
byte[] code = HexStringToByteArray(test.RawBytes);
|
||||
|
||||
// Create a disassembler with the code
|
||||
Disassembler disassembler = new Disassembler(code, 0x1000);
|
||||
|
||||
// Disassemble the code
|
||||
var disassembledInstructions = disassembler.Disassemble();
|
||||
|
||||
// Verify the number of instructions
|
||||
if (test.Instructions.Count != disassembledInstructions.Count)
|
||||
{
|
||||
AssertFailWithReason(
|
||||
index,
|
||||
file,
|
||||
test,
|
||||
disassembledInstructions,
|
||||
"Instruction count mismatch"
|
||||
);
|
||||
}
|
||||
|
||||
// Verify each instruction
|
||||
for (int i = 0; i < test.Instructions.Count; i++)
|
||||
{
|
||||
var expected = test.Instructions[i];
|
||||
var actual = disassembledInstructions[i];
|
||||
|
||||
if (expected.Mnemonic != actual.Mnemonic)
|
||||
{
|
||||
AssertFailWithReason(
|
||||
index,
|
||||
file,
|
||||
test,
|
||||
disassembledInstructions,
|
||||
"Mnemonic mismatch"
|
||||
);
|
||||
}
|
||||
|
||||
if (expected.Operands != actual.Operands)
|
||||
{
|
||||
AssertFailWithReason(
|
||||
index,
|
||||
file,
|
||||
test,
|
||||
disassembledInstructions,
|
||||
"Operands mismatch"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
output.WriteLine(
|
||||
$"Test succeeded {index} of file {file}: {test.RawBytes}.\n" +
|
||||
$"Instruction count \"{test.Instructions.Count}\".\n" +
|
||||
$"{string.Join("\n", test.Instructions.Select(x => $"{x.Mnemonic} {x.Operands}"))}\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertFailWithReason(int index, string file, TestFromFileEntry test, List<Instruction> disassembledInstructions, string reason)
|
||||
{
|
||||
Assert.Fail(
|
||||
$"Failed verifying test {index} of file {file}: {test.RawBytes}. {reason}.\n" +
|
||||
$"Expected \"{test.Instructions.Count}\", but got \"{disassembledInstructions.Count}\".\n" +
|
||||
$"\n" +
|
||||
$"Expected instructions:\n" +
|
||||
$"{string.Join("\n", test.Instructions.Select(x => $"{x.Mnemonic} {x.Operands}"))}\n" +
|
||||
$"\n" +
|
||||
$"Disassembled instructions:\n" +
|
||||
$"{string.Join("\n", disassembledInstructions)}"
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a hex string to a byte array
|
||||
/// </summary>
|
||||
/// <param name="hex">The hex string to convert</param>
|
||||
/// <returns>The byte array</returns>
|
||||
private static byte[] HexStringToByteArray(string hex)
|
||||
{
|
||||
// Remove any non-hex characters
|
||||
|
||||
// Create a byte array
|
||||
byte[] bytes = new byte[hex.Length / 2];
|
||||
|
||||
// Convert each pair of hex characters to a byte
|
||||
for (int i = 0; i < hex.Length; i += 2)
|
||||
{
|
||||
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
@ -1,249 +0,0 @@
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
using X86Disassembler.X86;
|
||||
using X86Disassembler.X86.Handlers.Sub;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for SUB instruction handlers
|
||||
/// </summary>
|
||||
public class SubInstructionTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32Handler for decoding SUB r/m32, imm32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32Handler_DecodesSubRm32Imm32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EAX, 0x12345678 (81 E8 78 56 34 12) - Subtract 0x12345678 from EAX
|
||||
byte[] codeBuffer = new byte[] { 0x81, 0xE8, 0x78, 0x56, 0x34, 0x12 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("eax, 0x12345678", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32Handler for decoding SUB memory, imm32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32Handler_DecodesSubMemImm32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB [EBX+0x10], 0x12345678 (81 6B 10 78 56 34 12) - Subtract 0x12345678 from memory at [EBX+0x10]
|
||||
byte[] codeBuffer = new byte[] { 0x81, 0x6B, 0x10, 0x78, 0x56, 0x34, 0x12 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
// The actual output from the disassembler for this instruction
|
||||
Assert.Equal("dword ptr [ebx+0x10], 0x12345678", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32SignExtendedHandler for decoding SUB r/m32, imm8 instruction (sign-extended)
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32SignExtendedHandler_DecodesSubRm32Imm8_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EAX, 0x42 (83 E8 42) - Subtract sign-extended 0x42 from EAX
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xE8, 0x42 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("eax, 0x42", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32SignExtendedHandler for decoding SUB r/m32, imm8 instruction with negative value
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32SignExtendedHandler_DecodesSubRm32NegativeImm8_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EAX, -0x10 (83 E8 F0) - Subtract sign-extended -0x10 from EAX
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xE8, 0xF0 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
// F0 is -16 in two's complement, should be sign-extended to 0xFFFFFFF0
|
||||
Assert.Equal("eax, 0xFFFFFFF0", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubImmFromRm32SignExtendedHandler for decoding SUB memory, imm8 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubImmFromRm32SignExtendedHandler_DecodesSubMemImm8_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB [EBX+0x10], 0x42 (83 6B 10 42) - Subtract sign-extended 0x42 from memory at [EBX+0x10]
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0x6B, 0x10, 0x42 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
// The actual output from the disassembler for this instruction
|
||||
Assert.Equal("dword ptr [ebx+0x10], 0x42", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests a sequence of SUB instructions in a common pattern
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubInstruction_DecodesSubSequence_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB ESP, 0x10 (83 EC 10) - Create stack space
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xEC, 0x10 };
|
||||
var disassembler = new Disassembler(codeBuffer, 0);
|
||||
|
||||
// Act
|
||||
var instructions = disassembler.Disassemble();
|
||||
|
||||
// Assert
|
||||
Assert.Single(instructions);
|
||||
|
||||
// Instruction: SUB ESP, 0x10
|
||||
Assert.Equal("sub", instructions[0].Mnemonic);
|
||||
Assert.Equal("esp, 0x10", instructions[0].Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubRm32R32Handler for decoding SUB r/m32, r32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubRm32R32Handler_DecodesSubRm32R32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EAX, EBX (29 D8) - Subtract EBX from EAX
|
||||
byte[] codeBuffer = new byte[] { 0x29, 0xD8 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("eax, ebx", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubRm32R32Handler for decoding SUB memory, r32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubRm32R32Handler_DecodesSubMemR32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB [EBX+0x10], ECX (29 4B 10) - Subtract ECX from memory at [EBX+0x10]
|
||||
byte[] codeBuffer = new byte[] { 0x29, 0x4B, 0x10 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("dword ptr [ebx+0x10], ecx", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubR32Rm32Handler for decoding SUB r32, r/m32 instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubR32Rm32Handler_DecodesSubR32Rm32_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB EBX, EAX (2B D8) - Subtract EAX from EBX
|
||||
byte[] codeBuffer = new byte[] { 0x2B, 0xD8 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("ebx, eax", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the SubR32Rm32Handler for decoding SUB r32, memory instruction
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubR32Rm32Handler_DecodesSubR32Mem_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB ECX, [EBX+0x10] (2B 4B 10) - Subtract memory at [EBX+0x10] from ECX
|
||||
byte[] codeBuffer = new byte[] { 0x2B, 0x4B, 0x10 };
|
||||
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||
|
||||
// Act
|
||||
var instruction = decoder.DecodeInstruction();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(instruction);
|
||||
Assert.Equal("sub", instruction.Mnemonic);
|
||||
Assert.Equal("ecx, dword ptr [ebx+0x10]", instruction.Operands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests a sequence of SUB instructions with different encoding
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void SubInstruction_DecodesComplexSubSequence_Correctly()
|
||||
{
|
||||
// Arrange
|
||||
// SUB ESP, 0x10 (83 EC 10) - Create stack space
|
||||
// SUB EAX, EBX (29 D8) - Subtract EBX from EAX
|
||||
// SUB ECX, [EBP-4] (2B 4D FC) - Subtract memory at [EBP-4] from ECX
|
||||
byte[] codeBuffer = new byte[] { 0x83, 0xEC, 0x10, 0x29, 0xD8, 0x2B, 0x4D, 0xFC };
|
||||
var disassembler = new Disassembler(codeBuffer, 0);
|
||||
|
||||
// Act
|
||||
var instructions = disassembler.Disassemble();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(3, instructions.Count);
|
||||
|
||||
// First instruction: SUB ESP, 0x10
|
||||
Assert.Equal("sub", instructions[0].Mnemonic);
|
||||
Assert.Equal("esp, 0x10", instructions[0].Operands);
|
||||
|
||||
// Second instruction: SUB EAX, EBX
|
||||
Assert.Equal("sub", instructions[1].Mnemonic);
|
||||
Assert.Equal("eax, ebx", instructions[1].Operands);
|
||||
|
||||
// Third instruction: SUB ECX, [EBP-4]
|
||||
Assert.Equal("sub", instructions[2].Mnemonic);
|
||||
Assert.Equal("ecx, dword ptr [ebp-0x04]", instructions[2].Operands);
|
||||
}
|
||||
}
|
9
X86DisassemblerTests/TestData/popreg_tests.csv
Normal file
9
X86DisassemblerTests/TestData/popreg_tests.csv
Normal file
@ -0,0 +1,9 @@
|
||||
RawBytes;Instructions
|
||||
58;[{ "Mnemonic": "pop", "Operands": "eax" }]
|
||||
59;[{ "Mnemonic": "pop", "Operands": "ecx" }]
|
||||
5A;[{ "Mnemonic": "pop", "Operands": "edx" }]
|
||||
5B;[{ "Mnemonic": "pop", "Operands": "ebx" }]
|
||||
5C;[{ "Mnemonic": "pop", "Operands": "esp" }]
|
||||
5D;[{ "Mnemonic": "pop", "Operands": "ebp" }]
|
||||
5E;[{ "Mnemonic": "pop", "Operands": "esi" }]
|
||||
5F;[{ "Mnemonic": "pop", "Operands": "edi" }]
|
Can't render this file because it contains an unexpected character in line 2 and column 7.
|
9
X86DisassemblerTests/TestData/pushreg_tests.csv
Normal file
9
X86DisassemblerTests/TestData/pushreg_tests.csv
Normal file
@ -0,0 +1,9 @@
|
||||
RawBytes;Instructions
|
||||
50;[{ "Mnemonic": "push", "Operands": "eax" }]
|
||||
51;[{ "Mnemonic": "push", "Operands": "ecx" }]
|
||||
52;[{ "Mnemonic": "push", "Operands": "edx" }]
|
||||
53;[{ "Mnemonic": "push", "Operands": "ebx" }]
|
||||
54;[{ "Mnemonic": "push", "Operands": "esp" }]
|
||||
55;[{ "Mnemonic": "push", "Operands": "ebp" }]
|
||||
56;[{ "Mnemonic": "push", "Operands": "esi" }]
|
||||
57;[{ "Mnemonic": "push", "Operands": "edi" }]
|
Can't render this file because it contains an unexpected character in line 2 and column 7.
|
48
X86DisassemblerTests/TestFromFileEntry.cs
Normal file
48
X86DisassemblerTests/TestFromFileEntry.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using CsvHelper.Configuration;
|
||||
|
||||
namespace X86DisassemblerTests;
|
||||
|
||||
public class TestFromFileEntry
|
||||
{
|
||||
public string RawBytes { get; set; } = string.Empty;
|
||||
public List<TestFromFileInstruction> Instructions { get; set; } = new();
|
||||
|
||||
public TestFromFileEntry()
|
||||
{
|
||||
}
|
||||
|
||||
public TestFromFileEntry(string rawBytes, List<TestFromFileInstruction> instructions)
|
||||
{
|
||||
RawBytes = rawBytes;
|
||||
Instructions = instructions;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestFromFileInstruction
|
||||
{
|
||||
public string Mnemonic { get; set; } = string.Empty;
|
||||
public string Operands { get; set; } = string.Empty;
|
||||
|
||||
// Parameterless constructor required by CsvHelper
|
||||
public TestFromFileInstruction()
|
||||
{
|
||||
}
|
||||
|
||||
public TestFromFileInstruction(string mnemonic, string operands)
|
||||
{
|
||||
Mnemonic = mnemonic;
|
||||
Operands = operands;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestFromFileEntryMap : ClassMap<TestFromFileEntry>
|
||||
{
|
||||
public TestFromFileEntryMap()
|
||||
{
|
||||
Map(m => m.RawBytes)
|
||||
.Name("RawBytes");
|
||||
Map(m => m.Instructions)
|
||||
.Name("Instructions")
|
||||
.TypeConverter<CsvJsonConverter<List<TestFromFileInstruction>>>();
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CsvHelper" Version="33.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
@ -27,8 +28,11 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="TestData\popreg_tests.csv" />
|
||||
<EmbeddedResource Include="TestData\pushreg_tests.csv" />
|
||||
<None Remove="instruction_test.json" />
|
||||
<EmbeddedResource Include="instruction_test.json" />
|
||||
<None Remove="PushTests\push_tests.csv" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,148 +1,4 @@
|
||||
[
|
||||
{
|
||||
"RawBytes": "50",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "eax"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "51",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "ecx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "52",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "edx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "53",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "ebx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "54",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "esp"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "55",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "ebp"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "56",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "esi"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "57",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "push",
|
||||
"Operands": "edi"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "58",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "eax"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "59",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "ecx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5A",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "edx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5B",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "ebx"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5C",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "esp"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5D",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "ebp"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5E",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "esi"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "5F",
|
||||
"Disassembled": [
|
||||
{
|
||||
"Mnemonic": "pop",
|
||||
"Operands": "edi"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"RawBytes": "6810000000",
|
||||
"Disassembled": [
|
||||
|
Loading…
x
Reference in New Issue
Block a user