From 5b8b9f06d93e85c50e1a69e2542ee3de3792757f Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Mon, 9 Nov 2026 17:16:45 +0530 Subject: Some segment.normalize tests. --- elf/test.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/elf/test.lua b/elf/test.lua index 717e03e..2955d20 100644 --- a/elf/test.lua +++ b/elf/test.lua @@ -1,5 +1,6 @@ local fmt = require("elf.fmt") local elf_header = require("elf.header") +local elf_segment = require("elf.segment") local ec = require("elf.constants") local nfail = 0 @@ -268,6 +269,74 @@ test("header.pack 64_be_x86-64", function () 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 section.lua -- Test layout.lua -- cgit v1.3