aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2026-11-09 17:16:45 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2026-11-09 17:16:45 +0530
commit5b8b9f06d93e85c50e1a69e2542ee3de3792757f (patch)
treeeffded1c1679e6bfcb6a992fa9d17cb7d6f42ee9
parent1058681fde4d104cf01a804a488b0d94911c1240 (diff)
Some segment.normalize tests.
-rw-r--r--elf/test.lua69
1 files changed, 69 insertions, 0 deletions
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