Skip to content

Remove macho fixtures #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.