summaryrefslogtreecommitdiff
path: root/tests/modules/binutils/test.lua
blob: bbb46614443ee7f3e648121f2db3b8e74507ab34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import("core.base.binutils")

function test_format(t)
    local tempdir = "temp/binutils_format"
    os.tryrm(tempdir)
    os.mkdir(tempdir)

    local unknown = path.join(tempdir, "unknown.bin")
    io.writefile(unknown, "12345678")
    local format = binutils.format(unknown)
    t:are_equal(format, "unknown")

    local scriptfile = path.join(tempdir, "a.sh")
    io.writefile(scriptfile, "#!/bin/sh\nexit 0\n")
    t:are_equal(binutils.format(scriptfile), "shebang")

    local apefile = path.join(tempdir, "a.ape")
    io.writefile(apefile, "MZqFpD00")
    t:are_equal(binutils.format(apefile), "ape")

    local apecom = path.join(tempdir, "a.com")
    io.writefile(apecom, "MZqFpD00")
    t:are_equal(binutils.format(apecom), "ape")

    local comfile = path.join(tempdir, "b.com")
    io.writefile(comfile, "12345678")
    t:are_equal(binutils.format(comfile), "unknown")

    local programfile = os.programfile()
    if programfile then
        local expected = "elf"
        if is_host("windows") then
            expected = "pe"
        elseif is_host("macosx", "iphoneos", "watchos", "appletvos") then
            expected = "macho"
        end
        local format = binutils.format(programfile)
        if format ~= "ape" then
            t:are_equal(format, expected)
        end
    end

    local ar = path.join(tempdir, "a.a")
    io.writefile(ar, "!<arch>\n")
    t:are_equal(binutils.format(ar), "ar")

    local elf = path.join(tempdir, "a.elf")
    io.writefile(elf, string.char(0x7f, string.byte("E"), string.byte("L"), string.byte("F"), 0, 0, 0, 0))
    t:are_equal(binutils.format(elf), "elf")

    local macho = path.join(tempdir, "a.macho")
    io.writefile(macho, string.char(0xfe, 0xed, 0xfa, 0xcf, 0, 0, 0, 0))
    t:are_equal(binutils.format(macho), "macho")

    local coff = path.join(tempdir, "a.obj")
    io.writefile(coff, string.char(0x4c, 0x01, 0, 0, 0, 0, 0, 0))
    t:are_equal(binutils.format(coff), "coff")

    local pefile = path.join(tempdir, "a.exe")
    local pe = {}
    for _ = 1, 0x44 do
        table.insert(pe, 0)
    end
    pe[1] = string.byte("M")
    pe[2] = string.byte("Z")
    pe[0x3c + 1] = 0x40
    pe[0x3c + 2] = 0
    pe[0x3c + 3] = 0
    pe[0x3c + 4] = 0
    pe[0x40 + 1] = string.byte("P")
    pe[0x40 + 2] = string.byte("E")
    pe[0x40 + 3] = 0
    pe[0x40 + 4] = 0
    io.writefile(pefile, string.char(table.unpack(pe)))
    t:are_equal(binutils.format(pefile), "pe")

    os.tryrm(tempdir)
end