From 54c78686ac1bd8121ec87e74bb613cbab1186e0e Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Tue, 10 Nov 2026 17:47:44 +0530 Subject: section.normalize() tests. While here, move into tests/ sub-directory. --- elf/tests/test_elf.lua | 542 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 542 insertions(+) create mode 100644 elf/tests/test_elf.lua (limited to 'elf/tests/test_elf.lua') diff --git a/elf/tests/test_elf.lua b/elf/tests/test_elf.lua new file mode 100644 index 0000000..a8c5cf2 --- /dev/null +++ b/elf/tests/test_elf.lua @@ -0,0 +1,542 @@ +local fmt = require("elf.fmt") +local elf_header = require("elf.header") +local elf_segment = require("elf.segment") +local elf_section = require("elf.section") +local ec = require("elf.constants") + +local nfail = 0 +local function test(name, fn) + local ok, err = pcall(fn) + if not ok then + nfail = nfail + 1 + print("fail: " .. name .. ": " .. err) + end +end + +-- Test fmt.lua +test("fmt.endian big", function () + assert(fmt.endian(ec.Endian.big) == ">", "invalid format string") +end) + +test("fmt.endian little", function () + assert(fmt.endian(ec.Endian.little) == "<", "invalid format string") +end) + +test("fmt.endian", function () + ---@diagnostic disable-next-line: param-type-mismatch + local ok = pcall(function () fmt.endian(42) end) + assert(not ok, "invalid input accepted") +end) + +test("fmt.word 64", function () + assert(fmt.word(ec.Class.bit64) == "I8", "invalid format string") +end) + +test("fmt.word 32", function () + assert(fmt.word(ec.Class.bit32) == "I4", "invalid format string") +end) + +test("fmt.word", function () + ---@diagnostic disable-next-line: param-type-mismatch + local ok = pcall(function () fmt.word(42) end) + assert(not ok, "invalid input accepted") +end) + +-- Test header.lua +test("header.normalize ELF64 defaults", function () + local h = elf_header.normalize({ + class = ec.Class.bit64, + data = ec.Endian.little, + type = ec.Type.exec, + machine = ec.Machine.x86_64, + }) + + assert(h.magic[1] == 0x7f, "magic[1] mismatch") + assert(h.magic[2] == 0x45, "magic[2] mismatch") + assert(h.magic[3] == 0x4c, "magic[3] mismatch") + assert(h.magic[4] == 0x46, "magic[4] mismatch") + assert(h.class == ec.Class.bit64, "class mismatch") + assert(h.data == ec.Endian.little, "data mismatch") + assert(h.type == ec.Type.exec, "type mismatch") + assert(h.machine == ec.Machine.x86_64, "machine mismatch") + assert(h.ident_version == ec.VERSION, "ident_version mismatch") + assert(h.osabi == ec.Osabi.sysv, "osabi mismatch") + assert(h.abiversion == 0, "abiversion mismatch") + assert(h.version == ec.VERSION, "version mismatch") + assert(h.entry == 0, "entry mismatch") + assert(h.phoff == 0x40, "phoff mismatch") + assert(h.shoff == 0, "shoff mismatch") + assert(h.flags == 0, "flags mismatch") + assert(h.ehsize == 0x40, "ehsize mismatch") + assert(h.phentsize == 0x38, "phentsize mismatch") + assert(h.phnum == 0, "phnum mismatch") + assert(h.shentsize == 0x40, "shentsize mismatch") + assert(h.shnum == 0, "shnum mismatch") + assert(h.shstrndx == 0, "shstrndx mismatch") +end) + +test("header.normalize preserve specified values", function () + local h = elf_header.normalize({ + class = ec.Class.bit64, + data = ec.Endian.little, + type = ec.Type.exec, + machine = ec.Machine.x86_64, + entry = 0x401000, + flags = 3, + phnum = 2, + shnum = 4, + shstrndx = 3, + }) + assert(h.entry == 0x401000, "entry mismatch") + assert(h.flags == 3, "flags mismatch") + assert(h.phnum == 2, "phnum mismatch") + assert(h.shnum == 4, "shnum mismatch") + assert(h.shstrndx == 3, "shstrndx mismatch") + -- Layout defaults remain ELF64 values. + assert(h.phoff == 0x40, "phoff mismatch") + assert(h.ehsize == 0x40, "ehsize mismatch") + assert(h.phentsize == 0x38, "phentsize mismatch") + assert(h.shentsize == 0x40, "shentsize mismatch") +end) + +test("header.normalize ELF32 layout", function () + local h = elf_header.normalize({ + class = ec.Class.bit32, + data = ec.Endian.little, + type = ec.Type.exec, + machine = ec.Machine.i386, + }) + assert(h.phoff == 0x34, "phoff mismatch") + assert(h.ehsize == 0x34, "ehsize mismatch") + assert(h.phentsize == 0x20, "phentsize mismatch") + assert(h.shentsize == 0x28, "shentsize mismatch") +end) + +local function hex_to_bytes(hex) + return (hex:gsub("..", function (pair) + local byte = assert(tonumber(pair, 16), "Invalid hex") + return string.char(byte) + end)) +end + +test("header.pack 64_le_x86-64", function () + local h = { + class = ec.Class.bit64, + data = ec.Endian.little, + type = ec.Type.exec, + machine = ec.Machine.x86_64, + } + local expected = hex_to_bytes(table.concat({ + -- "BBBBBBBBBxxxxxxx" + "7f454c46020101000000000000000000", + -- "I2I2I4" + "02003e0001000000", + -- string.rep(fmt.word(hdr.class), 3), where I8 is selected + "0000000000000000", -- entry + "4000000000000000", -- phoff + "0000000000000000", -- shoff + -- "I4I2I2I2I2I2I2" + "00000000", -- flags + "4000", -- ehsize + "3800", -- phentsize + "0000", -- phnum + "4000", -- shentsize + "0000", -- shnum + "0000", -- shstrndx + })) + local actual = elf_header.pack(elf_header.normalize(h)) + assert(#actual == 64, "invalid ELF64 header size") + assert(actual == expected, "mismatch") +end) + +test("header.pack 32_le_i386", function () + local h = { + class = ec.Class.bit32, + data = ec.Endian.little, + type = ec.Type.exec, + machine = ec.Machine.i386, + entry = 0x01020304, + phoff = 0x11121314, + shoff = 0x21222324, + flags = 0x31323334, + ehsize = 0x3435, + phentsize = 0x3637, + phnum = 0x3839, + shentsize = 0x3a3b, + shnum = 0x3c3d, + shstrndx = 0x3e3f, + } + local expected = hex_to_bytes(table.concat({ + -- "BBBBBBBBBxxxxxxx" + "7f454c46010101000000000000000000", + -- "I2I2I4" + "0200030001000000", + -- string.rep(fmt.word(hdr.class), 3), where I4 is selected + "04030201", -- entry + "14131211", -- phoff + "24232221", -- shoff + -- "I4I2I2I2I2I2I2" + "34333231", -- flags + "3534", -- ehsize + "3736", -- phentsize + "3938", -- phnum + "3b3a", -- shentsize + "3d3c", -- shnum + "3f3e", -- shstrndx + })) + local actual = elf_header.pack(elf_header.normalize(h)) + assert(#actual == 52, "invalid ELF32 header size") + assert(actual == expected, "mismatch") +end) + +test("header.pack 32_be_i386", function () + local h = { + class = ec.Class.bit32, + data = ec.Endian.big, + type = ec.Type.exec, + machine = ec.Machine.i386, + entry = 0x01020304, + phoff = 0x11121314, + shoff = 0x21222324, + flags = 0x31323334, + ehsize = 0x3435, + phentsize = 0x3637, + phnum = 0x3839, + shentsize = 0x3a3b, + shnum = 0x3c3d, + shstrndx = 0x3e3f, + } + local expected = hex_to_bytes(table.concat({ + -- "BBBBBBBBBxxxxxxx" + "7f454c46010201000000000000000000", + -- "I2I2I4" + "0002000300000001", + -- string.rep(fmt.word(hdr.class), 3), where I4 is selected + "01020304", -- entry + "11121314", -- phoff + "21222324", -- shoff + -- "I4I2I2I2I2I2I2" + "31323334", -- flags + "3435", -- ehsize + "3637", -- phentsize + "3839", -- phnum + "3a3b", -- shentsize + "3c3d", -- shnum + "3e3f", -- shstrndx + })) + local actual = elf_header.pack(elf_header.normalize(h)) + assert(#actual == 52, "invalid ELF32 header size") + assert(actual == expected, "mismatch") +end) + +test("header.pack 64_be_x86-64", function () + local h = { + class = ec.Class.bit64, + data = ec.Endian.big, + type = ec.Type.exec, + machine = ec.Machine.x86_64, + entry = 0x0102030405060708, + phoff = 0x1112131415161718, + shoff = 0x2122232425262728, + flags = 0x31323334, + ehsize = 0x3435, + phentsize = 0x3637, + phnum = 0x3839, + shentsize = 0x3a3b, + shnum = 0x3c3d, + shstrndx = 0x3e3f, + } + local expected = hex_to_bytes(table.concat({ + -- "BBBBBBBBBxxxxxxx" + "7f454c46020201000000000000000000", + -- "I2I2I4" + "0002003e00000001", + -- string.rep(fmt.word(hdr.class), 3), where I8 is selected + "0102030405060708", -- entry + "1112131415161718", -- phoff + "2122232425262728", -- shoff + -- "I4I2I2I2I2I2I2" + "31323334", -- flags + "3435", -- ehsize + "3637", -- phentsize + "3839", -- phnum + "3a3b", -- shentsize + "3c3d", -- shnum + "3e3f", -- shstrndx + })) + local actual = elf_header.pack(elf_header.normalize(h)) + assert(#actual == 64, "invalid ELF64 header size") + assert(actual == expected, "mismatch") +end) + +-- Test segment.lua +test("segment.normalize defaults", function () + local s = elf_segment.normalize({ + type = ec.SegmentType.load, + }) + assert(s.type == ec.SegmentType.load, "type mismatch") + assert(s.flags == 0, "flags mismatch") + assert(s.offset == 0, "offset mismatch") + assert(s.vaddr == 0, "vaddr mismatch") + assert(s.paddr == 0, "paddr mismatch") + assert(s.filesz == 0, "filesz mismatch") + assert(s.memsz == 0, "memsz mismatch") + assert(s.align == 1, "align mismatch") + assert(s.first_section == "", "first_section mismatch") + assert(s.last_section == "", "last_section mismatch") +end) + +test("segment.normalize does not validate missing type", function () + local ok, segment = pcall(elf_segment.normalize, {}) + assert(ok, "missing type rejected") + assert(segment.type == nil, "type mismatch") +end) + +test("segment.normalize preserve specified values", function () + local s = elf_segment.normalize({ + type = ec.SegmentType.load, + flags = ec.SegmentFlags.r + ec.SegmentFlags.x, + offset = 0x1000, + vaddr = 0x401000, + paddr = 0x401000, + filesz = 0x200, + memsz = 0x300, + align = 0x1000, + first_section = ".text", + last_section = ".rodata", + }) + assert(s.type == ec.SegmentType.load, "type mismatch") + assert(s.flags == ec.SegmentFlags.r + ec.SegmentFlags.x, "flags mismatch") + assert(s.offset == 0x1000, "offset mismatch") + assert(s.vaddr == 0x401000, "vaddr mismatch") + assert(s.paddr == 0x401000, "paddr mismatch") + assert(s.filesz == 0x200, "filesz mismatch") + assert(s.memsz == 0x300, "memsz mismatch") + assert(s.align == 0x1000, "align mismatch") + assert(s.first_section == ".text", "first_section mismatch") + assert(s.last_section == ".rodata", "last_section mismatch") +end) + +test("segment.normalize preserve zero values", function () + local s = elf_segment.normalize({ + type = ec.SegmentType.null, + flags = 0, + offset = 0, + vaddr = 0, + paddr = 0, + filesz = 0, + memsz = 0, + align = 0, + }) + assert(s.type == ec.SegmentType.null, "type mismatch") + assert(s.flags == 0, "flags mismatch") + assert(s.offset == 0, "offset mismatch") + assert(s.vaddr == 0, "vaddr mismatch") + assert(s.paddr == 0, "paddr mismatch") + assert(s.filesz == 0, "filesz mismatch") + assert(s.memsz == 0, "memsz mismatch") + assert(s.align == 0, "align mismatch") +end) + +test("segment.pack 32_le", function () + local s = { + type = ec.SegmentType.dynamic, + flags = 0x11121314, + offset = 0x21222324, + vaddr = 0x31323334, + paddr = 0x41424344, + filesz = 0x51525354, + memsz = 0x61626364, + align = 0x71727374, + } + local expected = hex_to_bytes(table.concat({ + -- string.rep(fmt.word(ec.Class.bit32), 8) + "02000000", -- type + "24232221", -- offset + "34333231", -- vaddr + "44434241", -- paddr + "54535251", -- filesz + "64636261", -- memsz + "14131211", -- flags + "74737271", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.little, ec.Class.bit32) + assert(#actual == 32, "invalid ELF32 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 32_be", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x21222324, + vaddr = 0x31323334, + paddr = 0x41424344, + filesz = 0x51525354, + memsz = 0x61626364, + align = 0x71727374, + } + local expected = hex_to_bytes(table.concat({ + -- string.rep(fmt.word(ec.Class.bit32), 8) where I4 is selected + "01020304", -- type + "21222324", -- offset + "31323334", -- vaddr + "41424344", -- paddr + "51525354", -- filesz + "61626364", -- memsz + "11121314", -- flags + "71727374", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.big, ec.Class.bit32) + assert(#actual == 32, "invalid ELF32 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 64_le", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x2122232425262728, + vaddr = 0x3132333435363738, + paddr = 0x4142434445464748, + filesz = 0x5152535455565758, + memsz = 0x6162636465666768, + align = 0x7172737475767778, + } + local expected = hex_to_bytes(table.concat({ + -- "I4I4" + "04030201", -- type + "14131211", -- flags + -- string.rep(fmt.word(ec.Class.bit64), 6) where I8 is selected + "2827262524232221", -- offset + "3837363534333231", -- vaddr + "4847464544434241", -- paddr + "5857565554535251", -- filesz + "6867666564636261", -- memsz + "7877767574737271", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.little, ec.Class.bit64) + assert(#actual == 56, "invalid ELF64 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 64_be", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x2122232425262728, + vaddr = 0x3132333435363738, + paddr = 0x4142434445464748, + filesz = 0x5152535455565758, + memsz = 0x6162636465666768, + align = 0x7172737475767778, + } + local expected = hex_to_bytes(table.concat({ + -- "I4I4" + "01020304", -- type + "11121314", -- flags + -- string.rep(fmt.word(ec.Class.bit64), 6) where I8 is selected + "2122232425262728", -- offset + "3132333435363738", -- vaddr + "4142434445464748", -- paddr + "5152535455565758", -- filesz + "6162636465666768", -- memsz + "7172737475767778", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.big, ec.Class.bit64) + assert(#actual == 56, "invalid ELF64 segment header size") + assert(actual == expected, "mismatch") +end) + +-- Test section.lua +test("section.normalize defaults", function () + local s = elf_section.normalize({ + name = ".text", + type = ec.SectionType.progbits, + flags = ec.SectionFlags.alloc, + }) + assert(s.name == ".text", "name mismatch") + assert(s.type == ec.SectionType.progbits, "type mismatch") + assert(s.flags == ec.SectionFlags.alloc, "flags mismatch") + assert(s.addr == 0, "addr mismatch") + assert(s.offset == 0, "offset mismatch") + assert(s.size == 0, "size mismatch") + assert(s.link == 0, "link mismatch") + assert(s.info == 0, "info mismatch") + assert(s.addralign == 0, "addralign mismatch") + assert(s.entsize == 0, "entsize mismatch") + assert(s.content == "", "content mismatch") +end) + +test("section.normalize does not validate missing fields", function () + local ok, section = pcall(elf_section.normalize, {}) + assert(ok, "missing fields rejected") + assert(section.name == nil, "name mismatch") + assert(section.type == nil, "type mismatch") + assert(section.flags == nil, "flags mismatch") +end) + +test("section.normalize preserve specified values", function () + local s = elf_section.normalize({ + name = ".data", + type = ec.SectionType.progbits, + flags = ec.SectionFlags.alloc & ec.SectionFlags.write, + addr = 0x401000, + offset = 0x1000, + size = 0x200, + link = 1, + info = 2, + addralign = 0x1000, + entsize = 8, + content = "contents", + }) + assert(s.name == ".data", "name mismatch") + assert(s.type == ec.SectionType.progbits, "type mismatch") + assert(s.flags == 0x2 & 0x1,"flags mismatch") + assert(s.addr == 0x401000, "addr mismatch") + assert(s.offset == 0x1000, "offset mismatch") + assert(s.size == 0x200, "size mismatch") + assert(s.link == 1, "link mismatch") + assert(s.info == 2, "info mismatch") + assert(s.addralign == 0x1000, "addralign mismatch") + assert(s.entsize == 8, "entsize mismatch") + assert(s.content == "contents", "content mismatch") +end) + +test("section.normalize preserve zero values", function () + local s = elf_section.normalize({ + name = ".null", + type = ec.SectionType.null, + flags = 0, + addr = 0, + offset = 0, + size = 0, + link = 0, + info = 0, + addralign = 0, + entsize = 0, + content = "", + }) + assert(s.name == ".null", "name mismatch") + assert(s.type == ec.SectionType.null, "type mismatch") + assert(s.flags == 0, "flags mismatch") + assert(s.addr == 0, "addr mismatch") + assert(s.offset == 0, "offset mismatch") + assert(s.size == 0, "size mismatch") + assert(s.link == 0, "link mismatch") + assert(s.info == 0, "info mismatch") + assert(s.addralign == 0, "addralign mismatch") + assert(s.entsize == 0, "entsize mismatch") + assert(s.content == "", "content mismatch") +end) + +-- Test layout.lua + +if nfail > 0 then + print(("%d %s failed"):format(nfail, (nfail == 1) and "test" or "tests")) + os.exit(1) +end -- cgit v1.3