Skip to content
Open
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
38 changes: 38 additions & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestMatch(t *testing.T) {
{[]byte{0xFF, 0xD8, 0xFF}, "jpg"},
{[]byte{0xFF, 0xD8, 0x00}, "unknown"},
{[]byte{0x89, 0x50, 0x4E, 0x47}, "png"},
{[]byte("!<arch>\ndebian-binary"), "deb"},
{[]byte("!<arch>\n"), "ar"},
}

for _, test := range cases {
Expand All @@ -32,6 +34,42 @@ func TestMatch(t *testing.T) {
}
}

func TestDebNotMatchedAsAr(t *testing.T) {
// Issue #126: deb files share the ar !<arch> magic; map iteration used to let ar win.
deb := []byte("!<arch>\ndebian-binary")
if !matchers.Deb(deb) {
t.Fatalf("Deb matcher rejected debian-binary header")
}
if matchers.Ar(deb) {
t.Fatalf("Ar matcher accepted a Debian package")
}

for i := 0; i < 50; i++ {
kind, err := Match(deb)
if err != nil {
t.Fatalf("Match: %v", err)
}
if kind.Extension != "deb" {
t.Fatalf("debian package matched as %q, want deb", kind.Extension)
}
}

ar := []byte("!<arch>\n")
if matchers.Deb(ar) {
t.Fatalf("Deb matcher accepted a generic ar header")
}
if !matchers.Ar(ar) {
t.Fatalf("Ar matcher rejected a generic ar header")
}
kind, err := Match(ar)
if err != nil {
t.Fatalf("Match ar: %v", err)
}
if kind.Extension != "ar" {
t.Fatalf("generic ar matched as %q, want ar", kind.Extension)
}
}

func TestMatchFile(t *testing.T) {
cases := []struct {
ext string
Expand Down
15 changes: 13 additions & 2 deletions matchers/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ var Archive = Map{
TypeNes: bytePrefixMatcher(nesMagic),
TypeCrx: bytePrefixMatcher(crxMagic),
TypeCab: Cab,
TypeDeb: bytePrefixMatcher(debMagic),
TypeAr: bytePrefixMatcher(arMagic),
TypeDeb: Deb,
TypeAr: Ar,
TypeZ: Z,
TypeLz: bytePrefixMatcher(lzMagic),
TypeRpm: Rpm,
Expand Down Expand Up @@ -106,6 +106,17 @@ func bytePrefixMatcher(magicPattern []byte) Matcher {
}
}

// Deb matches a Debian binary package (an ar archive whose first member is debian-binary).
func Deb(buf []byte) bool {
return compareBytes(buf, debMagic, 0)
}

// Ar matches a Unix ar archive that is not a Debian package.
// deb and ar share the !<arch> prefix; Match walks a map so ar can otherwise win first (issue #126).
func Ar(buf []byte) bool {
return compareBytes(buf, arMagic, 0) && !Deb(buf)
}

func Zip(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x50 && buf[1] == 0x4B &&
Expand Down
21 changes: 21 additions & 0 deletions matchers/archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package matchers

import "testing"

func TestArExcludesDebianPackages(t *testing.T) {
deb := []byte("!<arch>\ndebian-binary")
if !Deb(deb) {
t.Fatal("Deb should match debian-binary member")
}
if Ar(deb) {
t.Fatal("Ar should not match a Debian package")
}

plain := []byte("!<arch>\nfoo/")
if Deb(plain) {
t.Fatal("Deb should not match a generic ar archive")
}
if !Ar(plain) {
t.Fatal("Ar should match a generic ar archive")
}
}