From ea9d844bc36e25b80a9953389ed6c7140a87aa8e Mon Sep 17 00:00:00 2001 From: "MATSUMOTO, Katsuyoshi" Date: Mon, 11 Mar 2024 09:14:12 +0900 Subject: [PATCH] add test for null section symtab --- .../compiler/assembler/elf/section/symtab.rb | 19 ++++++++++--------- .../assembler/elf/section/test_symtab.rb | 10 ++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 test/vaporware/compiler/assembler/elf/section/test_symtab.rb diff --git a/lib/vaporware/compiler/assembler/elf/section/symtab.rb b/lib/vaporware/compiler/assembler/elf/section/symtab.rb index d06f41f..b316e5d 100644 --- a/lib/vaporware/compiler/assembler/elf/section/symtab.rb +++ b/lib/vaporware/compiler/assembler/elf/section/symtab.rb @@ -1,16 +1,14 @@ class Vaporware::Compiler::Assembler::ELF::Section::Symtab include Vaporware::Compiler::Assembler::ELF::Utils - def initialize(name: 0, info: 0, other: 0, shndx: 0, value: 0, size: 0) - @name = num2bytes(name, 4) - @info = num2bytes(info, 1) - @other = num2bytes(other, 1) - @shndx = num2bytes(shndx, 4) - @value = num2bytes(value, 8) - @size = num2bytes(size, 8) + def initialize + @name = num2bytes(0, 4) + @info = num2bytes(0, 1) + @other = num2bytes(0, 1) + @shndx = num2bytes(0, 2) + @value = num2bytes(0, 8) + @size = num2bytes(0, 8) end - def build = bytes.flatten.pack("C*") - def set!(name: nil, info: nil, other: nil, shndx: nil, value: nil, size: nil) @name = num2bytes(name, 4) if check(name, 4) @info = num2bytes(info, 1) if check(info, 4) @@ -19,4 +17,7 @@ def set!(name: nil, info: nil, other: nil, shndx: nil, value: nil, size: nil) @value = num2bytes(value, 8) if check(value, 8) @size = num2bytes(size, 8) if check(size, 8) end + + private + def bytes = [@name, @info, @other, @shndx, @value, @size] end diff --git a/test/vaporware/compiler/assembler/elf/section/test_symtab.rb b/test/vaporware/compiler/assembler/elf/section/test_symtab.rb new file mode 100644 index 0000000..01bb13b --- /dev/null +++ b/test/vaporware/compiler/assembler/elf/section/test_symtab.rb @@ -0,0 +1,10 @@ +require "vaporware" +require "test/unit" + +class Vaporware::Compiler::Assembler::ELF::Section::TestSymtab < Test::Unit::TestCase + def setup = @symtab = Vaporware::Compiler::Assembler::ELF::Section::Symtab.new + def test_set_values + binary = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding("ASCII-8BIT") + assert_equal(@symtab.build.size, binary.size) + end +end