0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 00:18:02 +03:00

unbreak tests

This commit is contained in:
bird_egop
2025-04-14 23:08:52 +03:00
parent 685eeda03d
commit 9117830ff1
41 changed files with 3820 additions and 736 deletions

View File

@ -1,4 +1,5 @@
using X86Disassembler.X86;
using X86Disassembler.X86.Operands;
namespace X86DisassemblerTests.InstructionTests;
@ -16,15 +17,32 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("eax, 0x12345678", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (EAX)
var eaxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (EAX)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x12345678U, immediateOperand.Value);
}
/// <summary>
@ -36,16 +54,33 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
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);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (memory operand)
var memoryOperand = instruction.StructuredOperands[0];
Assert.IsType<DisplacementMemoryOperand>(memoryOperand);
var memory = (DisplacementMemoryOperand)memoryOperand;
Assert.Equal(RegisterIndex.B, memory.BaseRegister); // Base register is EBX
Assert.Equal(0x10, memory.Displacement); // Displacement is 0x10
Assert.Equal(32, memory.Size); // Memory size is 32 bits (DWORD)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x12345678U, immediateOperand.Value);
}
/// <summary>
@ -57,15 +92,32 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("eax, 0x42", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (EAX)
var eaxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (EAX)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x42, immediateOperand.Value);
}
/// <summary>
@ -77,16 +129,32 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
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);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (EAX)
var eaxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (EAX)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0xFFFFFFF0U, immediateOperand.Value);
}
/// <summary>
@ -98,16 +166,33 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
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);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (memory operand)
var memoryOperand = instruction.StructuredOperands[0];
Assert.IsType<DisplacementMemoryOperand>(memoryOperand);
var memory = (DisplacementMemoryOperand)memoryOperand;
Assert.Equal(RegisterIndex.B, memory.BaseRegister); // Base register is EBX
Assert.Equal(0x10, memory.Displacement); // Displacement is 0x10
Assert.Equal(32, memory.Size); // Memory size is 32 bits (DWORD)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x42, immediateOperand.Value);
}
/// <summary>
@ -128,8 +213,25 @@ public class SubInstructionTests
Assert.Single(instructions);
// Instruction: SUB ESP, 0x10
Assert.Equal("sub", instructions[0].Mnemonic);
Assert.Equal("esp, 0x10", instructions[0].Operands);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (ESP)
var espOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(espOperand);
var registerOperand = (RegisterOperand)espOperand;
Assert.Equal(RegisterIndex.Sp, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (ESP)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x10, immediateOperand.Value);
}
/// <summary>
@ -141,15 +243,33 @@ public class SubInstructionTests
// Arrange
// SUB EAX, EBX (29 D8) - Subtract EBX from EAX
byte[] codeBuffer = new byte[] { 0x29, 0xD8 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("eax, ebx", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (EAX)
var eaxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (EAX)
// Check the second operand (EBX)
var ebxOperand = instruction.StructuredOperands[1];
Assert.IsType<RegisterOperand>(ebxOperand);
var registerOperand2 = (RegisterOperand)ebxOperand;
Assert.Equal(RegisterIndex.B, registerOperand2.Register);
Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EBX)
}
/// <summary>
@ -161,15 +281,34 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("dword ptr [ebx+0x10], ecx", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (memory operand)
var memoryOperand = instruction.StructuredOperands[0];
Assert.IsType<DisplacementMemoryOperand>(memoryOperand);
var memory = (DisplacementMemoryOperand)memoryOperand;
Assert.Equal(RegisterIndex.B, memory.BaseRegister); // Base register is EBX
Assert.Equal(0x10, memory.Displacement); // Displacement is 0x10
Assert.Equal(32, memory.Size); // Memory size is 32 bits (DWORD)
// Check the second operand (ECX)
var ecxOperand = instruction.StructuredOperands[1];
Assert.IsType<RegisterOperand>(ecxOperand);
var registerOperand = (RegisterOperand)ecxOperand;
Assert.Equal(RegisterIndex.C, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (ECX)
}
/// <summary>
@ -181,15 +320,33 @@ public class SubInstructionTests
// Arrange
// SUB EBX, EAX (2B D8) - Subtract EAX from EBX
byte[] codeBuffer = new byte[] { 0x2B, 0xD8 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("ebx, eax", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (EBX)
var ebxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(ebxOperand);
var registerOperand = (RegisterOperand)ebxOperand;
Assert.Equal(RegisterIndex.B, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (EBX)
// Check the second operand (EAX)
var eaxOperand = instruction.StructuredOperands[1];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand2 = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand2.Register);
Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EAX)
}
/// <summary>
@ -201,15 +358,34 @@ public class SubInstructionTests
// 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);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("sub", instruction.Mnemonic);
Assert.Equal("ecx, dword ptr [ebx+0x10]", instruction.Operands);
Assert.Equal(InstructionType.Sub, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (ECX)
var ecxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(ecxOperand);
var registerOperand = (RegisterOperand)ecxOperand;
Assert.Equal(RegisterIndex.C, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (ECX)
// Check the second operand (memory operand)
var memoryOperand = instruction.StructuredOperands[1];
Assert.IsType<DisplacementMemoryOperand>(memoryOperand);
var memory = (DisplacementMemoryOperand)memoryOperand;
Assert.Equal(RegisterIndex.B, memory.BaseRegister); // Base register is EBX
Assert.Equal(0x10, memory.Displacement); // Displacement is 0x10
Assert.Equal(32, memory.Size); // Memory size is 32 bits (DWORD)
}
/// <summary>
@ -232,15 +408,69 @@ public class SubInstructionTests
Assert.Equal(3, instructions.Count);
// First instruction: SUB ESP, 0x10
Assert.Equal("sub", instructions[0].Mnemonic);
Assert.Equal("esp, 0x10", instructions[0].Operands);
var instruction1 = instructions[0];
Assert.NotNull(instruction1);
Assert.Equal(InstructionType.Sub, instruction1.Type);
// Check that we have two operands
Assert.Equal(2, instruction1.StructuredOperands.Count);
// Check the first operand (ESP)
var espOperand = instruction1.StructuredOperands[0];
Assert.IsType<RegisterOperand>(espOperand);
var registerOperand = (RegisterOperand)espOperand;
Assert.Equal(RegisterIndex.Sp, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (ESP)
// Check the second operand (immediate value)
var immOperand = instruction1.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x10, immediateOperand.Value);
// Second instruction: SUB EAX, EBX
Assert.Equal("sub", instructions[1].Mnemonic);
Assert.Equal("eax, ebx", instructions[1].Operands);
var instruction2 = instructions[1];
Assert.NotNull(instruction2);
Assert.Equal(InstructionType.Sub, instruction2.Type);
// Check that we have two operands
Assert.Equal(2, instruction2.StructuredOperands.Count);
// Check the first operand (EAX)
var eaxOperand = instruction2.StructuredOperands[0];
Assert.IsType<RegisterOperand>(eaxOperand);
var registerOperand2 = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, registerOperand2.Register);
Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EAX)
// Check the second operand (EBX)
var ebxOperand = instruction2.StructuredOperands[1];
Assert.IsType<RegisterOperand>(ebxOperand);
var registerOperand3 = (RegisterOperand)ebxOperand;
Assert.Equal(RegisterIndex.B, registerOperand3.Register);
Assert.Equal(32, registerOperand3.Size); // Validate that it's a 32-bit register (EBX)
// Third instruction: SUB ECX, [EBP-4]
Assert.Equal("sub", instructions[2].Mnemonic);
Assert.Equal("ecx, dword ptr [ebp-0x04]", instructions[2].Operands);
var instruction3 = instructions[2];
Assert.NotNull(instruction3);
Assert.Equal(InstructionType.Sub, instruction3.Type);
// Check that we have two operands
Assert.Equal(2, instruction3.StructuredOperands.Count);
// Check the first operand (ECX)
var ecxOperand = instruction3.StructuredOperands[0];
Assert.IsType<RegisterOperand>(ecxOperand);
var registerOperand4 = (RegisterOperand)ecxOperand;
Assert.Equal(RegisterIndex.C, registerOperand4.Register);
Assert.Equal(32, registerOperand4.Size); // Validate that it's a 32-bit register (ECX)
// Check the second operand (memory operand)
var memoryOperand = instruction3.StructuredOperands[1];
Assert.IsType<DisplacementMemoryOperand>(memoryOperand);
var memory = (DisplacementMemoryOperand)memoryOperand;
Assert.Equal(RegisterIndex.Bp, memory.BaseRegister); // Base register is EBP
Assert.Equal(-4, memory.Displacement); // Displacement is -4
Assert.Equal(32, memory.Size); // Memory size is 32 bits (DWORD)
}
}