From 28ba47bfab46615c35a1036f8b84c918347e13f5 Mon Sep 17 00:00:00 2001 From: bird_egop Date: Sun, 13 Apr 2025 01:30:58 +0300 Subject: [PATCH] add factory tests --- .../X86/Handlers/Add/AddImmToRm32Handler.cs | 2 +- .../Add/AddImmToRm32SignExtendedHandler.cs | 2 +- .../X86/Handlers/Add/AddImmToRm8Handler.cs | 2 +- .../X86/Handlers/Cmp/CmpImmWithRm32Handler.cs | 2 +- .../Cmp/CmpImmWithRm32SignExtendedHandler.cs | 2 +- .../InstructionHandlerFactoryTests.cs | 42 ++++++++++++++++++- 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/X86Disassembler/X86/Handlers/Add/AddImmToRm32Handler.cs b/X86Disassembler/X86/Handlers/Add/AddImmToRm32Handler.cs index 8e97c18..c251e70 100644 --- a/X86Disassembler/X86/Handlers/Add/AddImmToRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Add/AddImmToRm32Handler.cs @@ -1,4 +1,4 @@ -namespace X86Disassembler.X86.Handlers.ArithmeticImmediate; +namespace X86Disassembler.X86.Handlers.Add; /// /// Handler for ADD r/m32, imm32 instruction (0x81 /0) diff --git a/X86Disassembler/X86/Handlers/Add/AddImmToRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Add/AddImmToRm32SignExtendedHandler.cs index abe907d..3513e10 100644 --- a/X86Disassembler/X86/Handlers/Add/AddImmToRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Add/AddImmToRm32SignExtendedHandler.cs @@ -1,4 +1,4 @@ -namespace X86Disassembler.X86.Handlers.ArithmeticImmediate; +namespace X86Disassembler.X86.Handlers.Add; /// /// Handler for ADD r/m32, imm8 (sign-extended) instruction (0x83 /0) diff --git a/X86Disassembler/X86/Handlers/Add/AddImmToRm8Handler.cs b/X86Disassembler/X86/Handlers/Add/AddImmToRm8Handler.cs index 10d9073..7870811 100644 --- a/X86Disassembler/X86/Handlers/Add/AddImmToRm8Handler.cs +++ b/X86Disassembler/X86/Handlers/Add/AddImmToRm8Handler.cs @@ -1,4 +1,4 @@ -namespace X86Disassembler.X86.Handlers.ArithmeticImmediate; +namespace X86Disassembler.X86.Handlers.Add; /// /// Handler for ADD r/m8, imm8 instruction (0x80 /0) diff --git a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32Handler.cs b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32Handler.cs index fcf98ce..713af27 100644 --- a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32Handler.cs +++ b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32Handler.cs @@ -1,4 +1,4 @@ -namespace X86Disassembler.X86.Handlers.ArithmeticImmediate; +namespace X86Disassembler.X86.Handlers.Cmp; /// /// Handler for CMP r/m32, imm32 instruction (0x81 /7) diff --git a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32SignExtendedHandler.cs b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32SignExtendedHandler.cs index 33d5ec8..14669b1 100644 --- a/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32SignExtendedHandler.cs +++ b/X86Disassembler/X86/Handlers/Cmp/CmpImmWithRm32SignExtendedHandler.cs @@ -1,4 +1,4 @@ -namespace X86Disassembler.X86.Handlers.ArithmeticImmediate; +namespace X86Disassembler.X86.Handlers.Cmp; /// /// Handler for CMP r/m32, imm8 (sign-extended) instruction (0x83 /7) diff --git a/X86DisassemblerTests/InstructionHandlerFactoryTests.cs b/X86DisassemblerTests/InstructionHandlerFactoryTests.cs index 2fdcd4d..8f00a8e 100644 --- a/X86DisassemblerTests/InstructionHandlerFactoryTests.cs +++ b/X86DisassemblerTests/InstructionHandlerFactoryTests.cs @@ -1,6 +1,44 @@ -namespace X86DisassemblerTests; +using System.Reflection; +using X86Disassembler.X86; +using X86Disassembler.X86.Handlers; + +namespace X86DisassemblerTests; public class InstructionHandlerFactoryTests { - + [Fact] + public void Factory_ShouldNotContainDuplicates() + { + byte[] code = new byte[] {0xCC, 0xCC, 0xCC}; + var sut = new InstructionHandlerFactory(code, new InstructionDecoder(code, code.Length), code.Length); + + var handlers = (List) sut.GetType() + .GetField("_handlers", BindingFlags.Instance | BindingFlags.NonPublic)! + .GetValue(sut)!; + + var distinctHandlersCount = handlers.Distinct() + .Count(); + + Assert.Equal(distinctHandlersCount, handlers.Count); + } + + [Fact] + public void Factory_ShouldContainAllKnownHandlers() + { + byte[] code = new byte[] {0xCC, 0xCC, 0xCC}; + var sut = new InstructionHandlerFactory(code, new InstructionDecoder(code, code.Length), code.Length); + + var handlers = (List) sut.GetType() + .GetField("_handlers", BindingFlags.Instance | BindingFlags.NonPublic)! + .GetValue(sut)!; + + var handlerTypes = typeof(InstructionHandler).Assembly.GetExportedTypes() + .Where(x => x.IsAssignableTo(typeof(InstructionHandler)) && x is {IsAbstract: false, IsInterface: false}) + .ToList(); + + foreach (var handlerType in handlerTypes) + { + Assert.Contains(handlers, x => x.GetType() == handlerType); + } + } } \ No newline at end of file