diff options
| -rw-r--r-- | elf/tests/test_elf.lua (renamed from elf/test.lua) | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/elf/test.lua b/elf/tests/test_elf.lua index e49c828..a8c5cf2 100644 --- a/elf/test.lua +++ b/elf/tests/test_elf.lua @@ -1,6 +1,7 @@ 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 @@ -452,6 +453,87 @@ test("segment.pack 64_be", function () 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 |
