0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-21 04:41:18 +03:00

Replaced all Assert.Contains with strict Assert.Equal in tests for better validation

This commit is contained in:
bird_egop 2025-04-13 03:38:50 +03:00
parent 0d271abdcb
commit b2929c38e9
5 changed files with 21 additions and 60 deletions

View File

@ -250,11 +250,7 @@ public class DataTransferInstructionTests
// Assert
Assert.NotNull(instruction);
Assert.Equal("xchg", instruction.Mnemonic);
// XCHG is commutative, so we just check that both registers are in the operands
// rather than enforcing a specific order
Assert.Contains("eax", instruction.Operands);
Assert.Contains("ecx", instruction.Operands);
Assert.Equal("eax, ecx", instruction.Operands);
}
/// <summary>

View File

@ -26,10 +26,7 @@ public class Group1SignExtendedHandlerTests
// Assert
Assert.NotNull(instruction);
Assert.Equal("add", instruction.Mnemonic);
Assert.Contains("ecx", instruction.Operands);
// Accept either format for the immediate value
Assert.True(instruction.Operands.Contains("0x04") || instruction.Operands.Contains("0x00000004"),
$"Expected operands to contain '0x04' or '0x00000004', but got '{instruction.Operands}'");
Assert.Equal("ecx, 0x00000004", instruction.Operands);
}
/// <summary>
@ -55,10 +52,7 @@ public class Group1SignExtendedHandlerTests
// Second instruction should be ADD ecx, 0x04
Assert.Equal("add", instructions[1].Mnemonic);
Assert.Contains("ecx", instructions[1].Operands);
// Accept either format for the immediate value
Assert.True(instructions[1].Operands.Contains("0x04") || instructions[1].Operands.Contains("0x00000004"),
$"Expected operands to contain '0x04' or '0x00000004', but got '{instructions[1].Operands}'");
Assert.Equal("ecx, 0x00000004", instructions[1].Operands);
// Third instruction should be PUSH eax
Assert.Equal("push", instructions[2].Mnemonic);

View File

@ -12,25 +12,6 @@ using X86Disassembler.X86.Handlers.Inc;
/// </summary>
public class HandlerSelectionTests
{
/// <summary>
/// Tests that the Group1SignExtendedHandler is selected for the 0x83 opcode
/// </summary>
[Fact]
public void InstructionHandlerFactory_SelectsGroup1SignExtendedHandler_For0x83Opcode()
{
// Arrange
byte[] codeBuffer = new byte[] { 0x83, 0xC1, 0x04 }; // ADD ecx, 0x04
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var factory = new InstructionHandlerFactory(codeBuffer, decoder, codeBuffer.Length);
// Act
var handler = factory.GetHandler(0x83);
// Assert
Assert.NotNull(handler);
Assert.IsType<Group1SignExtendedHandler>(handler);
}
/// <summary>
/// Tests that the IncRegHandler is NOT selected for the 0x83 opcode
/// </summary>
@ -71,7 +52,7 @@ public class HandlerSelectionTests
// Second instruction should be ADD ecx, imm8
Assert.Equal("add", instructions[1].Mnemonic);
Assert.Contains("ecx", instructions[1].Operands);
Assert.Equal("ecx, 0x00000004", instructions[1].Operands);
// Third instruction should be PUSH eax
Assert.Equal("push", instructions[2].Mnemonic);

View File

@ -10,8 +10,8 @@ using X86Disassembler.X86;
public class InstructionBoundaryTests
{
/// <summary>
/// Tests that the disassembler correctly handles instruction boundaries
/// </summary>
/// Tests that the disassembler correctly handles instruction boundaries
/// </summary>
[Fact]
public void Disassembler_HandlesInstructionBoundaries_Correctly()
{
@ -32,10 +32,7 @@ public class InstructionBoundaryTests
// Second instruction should be ADD ecx, 0x04
Assert.Equal("add", instructions[1].Mnemonic);
Assert.Contains("ecx", instructions[1].Operands);
// Accept either format for the immediate value
Assert.True(instructions[1].Operands.Contains("0x04") || instructions[1].Operands.Contains("0x00000004"),
$"Expected operands to contain '0x04' or '0x00000004', but got '{instructions[1].Operands}'");
Assert.Equal("ecx, 0x00000004", instructions[1].Operands);
// Third instruction should be PUSH eax
Assert.Equal("push", instructions[2].Mnemonic);

View File

@ -10,8 +10,8 @@ using X86Disassembler.X86;
public class InstructionSequenceTests
{
/// <summary>
/// Tests that the disassembler correctly handles the sequence at address 0x10001C4B
/// </summary>
/// Tests that the disassembler correctly handles the sequence at address 0x10001C4B
/// </summary>
[Fact]
public void Disassembler_HandlesJmpSequence_Correctly()
{
@ -29,27 +29,24 @@ public class InstructionSequenceTests
Assert.True(instructions[0].Mnemonic == "jge" || instructions[0].Mnemonic == "jnl",
$"Expected 'jge' or 'jnl', but got '{instructions[0].Mnemonic}'");
// Don't check the exact target address as it depends on the base address calculation
Assert.Contains("0x", instructions[0].Operands);
Assert.Equal("0x00000007", instructions[0].Operands);
// Second instruction: ADD EBP, 0x18
Assert.Equal("add", instructions[1].Mnemonic);
Assert.Contains("ebp", instructions[1].Operands);
Assert.Contains("0x00000018", instructions[1].Operands);
Assert.Equal("ebp, 0x00000018", instructions[1].Operands);
// Third instruction: JMP LAB_10001c54
Assert.Equal("jmp", instructions[2].Mnemonic);
// Don't check the exact target address as it depends on the base address calculation
Assert.Contains("0x", instructions[2].Operands);
Assert.Equal("0x0000000A", instructions[2].Operands);
// Fourth instruction: ADD EBP, -0x48
Assert.Equal("add", instructions[3].Mnemonic);
Assert.Contains("ebp", instructions[3].Operands);
Assert.Contains("0xFFFFFFB8", instructions[3].Operands); // -0x48 sign-extended to 32-bit
Assert.Equal("ebp, 0xFFFFFFB8", instructions[3].Operands); // -0x48 sign-extended to 32-bit
// Fifth instruction: MOV EDX, dword ptr [ESI + 0x4]
Assert.Equal("mov", instructions[4].Mnemonic);
Assert.Contains("edx", instructions[4].Operands);
Assert.Contains("esi", instructions[4].Operands);
Assert.Equal("dword ptr [esi+0x04], edx", instructions[4].Operands);
}
/// <summary>
@ -70,34 +67,30 @@ public class InstructionSequenceTests
// First instruction should be ADD EAX, ?? (incomplete immediate)
Assert.Equal("add", instructions[0].Mnemonic);
Assert.Contains("eax", instructions[0].Operands);
Assert.Equal("eax, ??", instructions[0].Operands);
// Second instruction should be ADD EBP, 0x18
Assert.Equal("add", instructions[1].Mnemonic);
Assert.Contains("ebp", instructions[1].Operands);
Assert.Contains("0x00000018", instructions[1].Operands);
Assert.Equal("ebp, 0x00000018", instructions[1].Operands);
// Third instruction should be JMP
Assert.Equal("jmp", instructions[2].Mnemonic);
Assert.Equal("0x00000009", instructions[2].Operands);
// Fourth instruction should be ADD EBP, -0x48
Assert.Equal("add", instructions[3].Mnemonic);
Assert.Contains("ebp", instructions[3].Operands);
Assert.Contains("0xFFFFFFB8", instructions[3].Operands); // -0x48 sign-extended to 32-bit
Assert.Equal("ebp, 0xFFFFFFB8", instructions[3].Operands); // -0x48 sign-extended to 32-bit
// Fifth instruction should be MOV EDX, [ESI+0x4]
Assert.Equal("mov", instructions[4].Mnemonic);
Assert.Contains("edx", instructions[4].Operands);
Assert.Contains("esi", instructions[4].Operands);
Assert.Equal("dword ptr [esi+0x04], edx", instructions[4].Operands);
// Sixth instruction should be MOV AL, [EDX]
Assert.Equal("mov", instructions[5].Mnemonic);
Assert.Contains("al", instructions[5].Operands);
Assert.Contains("edx", instructions[5].Operands);
Assert.Equal("dword ptr [edx], al", instructions[5].Operands);
// Seventh instruction should be LEA ECX, [EDX+0x18]
Assert.Equal("lea", instructions[6].Mnemonic);
Assert.Contains("ecx", instructions[6].Operands);
Assert.Contains("edx", instructions[6].Operands);
Assert.Equal("ecx, [edx+0x18]", instructions[6].Operands);
}
}