Skip to content

Commit

Permalink
Remove macho fixtures (#602)
Browse files Browse the repository at this point in the history
* formatting: reduce ident size by 1

* testdata cleanup: remove macho fixtures

#575 (comment)

* macho: use signature from stdlib instead of magic signature

* macho: add unit tests

These tests are a replacement for the fixtures from the testdata
directory.
  • Loading branch information
gabriel-vasile authored Nov 4, 2024
1 parent f4b2ca5 commit 2f8f7d8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 70 deletions.
4 changes: 2 additions & 2 deletions internal/magic/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func classOrMachOFat(in []byte) bool {
return false
}

return bytes.HasPrefix(in, []byte{0xCA, 0xFE, 0xBA, 0xBE})
return binary.BigEndian.Uint32(in) == macho.MagicFat
}

// Class matches a java class file.
Expand All @@ -46,7 +46,7 @@ func Class(raw []byte, limit uint32) bool {

// MachO matches Mach-O binaries format.
func MachO(raw []byte, limit uint32) bool {
if classOrMachOFat(raw) && raw[7] < 20 {
if classOrMachOFat(raw) && raw[7] < 0x14 {
return true
}

Expand Down
154 changes: 89 additions & 65 deletions internal/magic/magic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,100 @@ func TestMagic(t *testing.T) {
raw string
limit uint32
res bool
}{
{
name: "incomplete JSON, limit 0",
detector: JSON,
raw: `["an incomplete JSON array`,
limit: 0,
res: false,
},
{
name: "incomplete JSON, limit 10",
detector: JSON,
raw: `["an incomplete JSON array`,
limit: 10,
res: true,
},
{
name: "basic JSON data type null",
detector: JSON,
raw: `null`,
limit: 10,
res: false,
},
{
name: "basic JSON data type string",
detector: JSON,
raw: `"abc"`,
limit: 10,
res: false,
},
{
name: "basic JSON data type integer",
detector: JSON,
raw: `120`,
limit: 10,
res: false,
},
{
name: "basic JSON data type float",
detector: JSON,
raw: `.120`,
limit: 10,
res: false,
},
{
name: "NdJSON with basic data types",
detector: NdJSON,
raw: "1\nnull\n\"foo\"\n0.1",
limit: 10,
res: false,
},
{
name: "NdJSON with basic data types and empty object",
detector: NdJSON,
raw: "1\n2\n3\n{}",
limit: 10,
res: true,
},
{
name: "NdJSON with empty objects types",
detector: NdJSON,
raw: "{}\n{}\n{}",
limit: 10,
res: true,
},
}
}{{
name: "incomplete JSON, limit 0",
detector: JSON,
raw: `["an incomplete JSON array`,
limit: 0,
res: false,
}, {
name: "incomplete JSON, limit 10",
detector: JSON,
raw: `["an incomplete JSON array`,
limit: 10,
res: true,
}, {
name: "basic JSON data type null",
detector: JSON,
raw: `null`,
limit: 10,
res: false,
}, {
name: "basic JSON data type string",
detector: JSON,
raw: `"abc"`,
limit: 10,
res: false,
}, {
name: "basic JSON data type integer",
detector: JSON,
raw: `120`,
limit: 10,
res: false,
}, {
name: "basic JSON data type float",
detector: JSON,
raw: `.120`,
limit: 10,
res: false,
}, {
name: "NdJSON with basic data types",
detector: NdJSON,
raw: "1\nnull\n\"foo\"\n0.1",
limit: 10,
res: false,
}, {
name: "NdJSON with basic data types and empty object",
detector: NdJSON,
raw: "1\n2\n3\n{}",
limit: 10,
res: true,
}, {
name: "NdJSON with empty objects types",
detector: NdJSON,
raw: "{}\n{}\n{}",
limit: 10,
res: true,
}, {
name: "MachO class or Fat but last byte > \\x14",
detector: MachO,
raw: "\xCA\xFE\xBA\xBE \x15",
res: false,
}, {
name: "MachO class or Fat and last byte < \\x14",
detector: MachO,
raw: "\xCA\xFE\xBA\xBE \x13",
res: true,
}, {
name: "MachO BE Magic32",
detector: MachO,
raw: "\xFE\xED\xFA\xCE",
res: true,
}, {
name: "MachO LE Magic32",
detector: MachO,
raw: "\xCE\xFA\xED\xFE",
res: true,
}, {
name: "MachO BE Magic64",
detector: MachO,
raw: "\xFE\xED\xFA\xCF",
res: true,
}, {
name: "MachO LE Magic64",
detector: MachO,
raw: "\xCF\xFA\xED\xFE",
res: true,
}}
for _, tt := range tCases {
t.Run(tt.name, func(t *testing.T) {
if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res {
t.Errorf("expected: %t; got: %t", tt.res, got)
}
// Empty inputs should not pass as anything.
if got := tt.detector(nil, 0); got != false {
t.Errorf("empty input: expected: %t; got: %t", false, got)
}
})
}
}
Expand Down
3 changes: 0 additions & 3 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ var testcases = []testcase{
{"m4a.m4a", m4a, "audio/x-m4a", true},
{"audio.mp4", aMp4, "audio/mp4", true},
{"lnk.lnk", lnk, "application/x-ms-shortcut", true},
{"macho.macho", macho, "application/x-mach-binary", true},
{"mdb.mdb", mdb, "application/x-msaccess", true},
{"midi.midi", midi, "audio/midi", true},
{"mkv.mkv", mkv, "video/x-matroska", true},
Expand Down Expand Up @@ -173,8 +172,6 @@ var testcases = []testcase{
{"rpm.rpm", rpm, "application/x-rpm", true},
{"rss.rss", rss, "application/rss+xml", true},
{"rtf.rtf", rtf, "text/rtf", true},
{"sample32.macho", macho, "application/x-mach-binary", false},
{"sample64.macho", macho, "application/x-mach-binary", false},
{"shp.shp", shp, "application/vnd.shp", true},
{"shx.shx", shx, "application/vnd.shx", true},
{"so.so", elfLib, "application/x-sharedlib", true},
Expand Down
Binary file removed testdata/macho.macho
Binary file not shown.
Binary file removed testdata/sample32.macho
Binary file not shown.
Binary file removed testdata/sample64.macho
Binary file not shown.

0 comments on commit 2f8f7d8

Please sign in to comment.