| 1 |
package filetype |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"io/ioutil" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/h2non/filetype/matchers" |
| 10 |
"github.com/h2non/filetype/types" |
| 11 |
) |
| 12 |
|
| 13 |
func TestMatch(t *testing.T) { |
| 14 |
cases := []struct { |
| 15 |
buf []byte |
| 16 |
ext string |
| 17 |
}{ |
| 18 |
{[]byte{0xFF, 0xD8, 0xFF}, "jpg"}, |
| 19 |
{[]byte{0xFF, 0xD8, 0x00}, "unknown"}, |
| 20 |
{[]byte{0x89, 0x50, 0x4E, 0x47}, "png"}, |
| 21 |
} |
| 22 |
|
| 23 |
for _, test := range cases { |
| 24 |
match, err := Match(test.buf) |
| 25 |
if err != nil { |
| 26 |
t.Fatalf("Error: %s", err) |
| 27 |
} |
| 28 |
|
| 29 |
if match.Extension != test.ext { |
| 30 |
t.Fatalf("Invalid image type: %s != %s", match.Extension, test.ext) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestMatchFile(t *testing.T) { |
| 36 |
cases := []struct { |
| 37 |
ext string |
| 38 |
}{ |
| 39 |
{"gif"}, |
| 40 |
{"jpg"}, |
| 41 |
{"png"}, |
| 42 |
{"zip"}, |
| 43 |
{"tar"}, |
| 44 |
{"tif"}, |
| 45 |
{"mp4"}, |
| 46 |
{"mkv"}, |
| 47 |
{"webm"}, |
| 48 |
{"docx"}, |
| 49 |
{"pptx"}, |
| 50 |
{"xlsx"}, |
| 51 |
{"mov"}, |
| 52 |
{"wasm"}, |
| 53 |
{"dwg"}, |
| 54 |
{"zst"}, |
| 55 |
} |
| 56 |
|
| 57 |
for _, test := range cases { |
| 58 |
kind, _ := MatchFile("./fixtures/sample." + test.ext) |
| 59 |
if kind.Extension != test.ext { |
| 60 |
t.Fatalf("Invalid type: %s != %s", kind.Extension, test.ext) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
kind, _ := MatchFile("./fixtures/sample_skippable.zst") |
| 65 |
if kind.Extension != "zst" { |
| 66 |
t.Fatalf("Invalid type: %s != %s", kind.Extension, "zst") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestMatchReader(t *testing.T) { |
| 71 |
cases := []struct { |
| 72 |
buf io.Reader |
| 73 |
ext string |
| 74 |
}{ |
| 75 |
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0xFF}), "jpg"}, |
| 76 |
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0x00}), "unknown"}, |
| 77 |
{bytes.NewBuffer([]byte{0x89, 0x50, 0x4E, 0x47}), "png"}, |
| 78 |
} |
| 79 |
|
| 80 |
for _, test := range cases { |
| 81 |
match, err := MatchReader(test.buf) |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("Error: %s", err) |
| 84 |
} |
| 85 |
|
| 86 |
if match.Extension != test.ext { |
| 87 |
t.Fatalf("Invalid image type: %s", match.Extension) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestMatches(t *testing.T) { |
| 93 |
cases := []struct { |
| 94 |
buf []byte |
| 95 |
match bool |
| 96 |
}{ |
| 97 |
{[]byte{0xFF, 0xD8, 0xFF}, true}, |
| 98 |
{[]byte{0xFF, 0x0, 0x0}, false}, |
| 99 |
{[]byte{0x89, 0x50, 0x4E, 0x47}, true}, |
| 100 |
} |
| 101 |
|
| 102 |
for _, test := range cases { |
| 103 |
if Matches(test.buf) != test.match { |
| 104 |
t.Fatalf("Do not matches: %#v", test.buf) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestAddMatcher(t *testing.T) { |
| 110 |
fileType := AddType("foo", "foo/foo") |
| 111 |
|
| 112 |
AddMatcher(fileType, func(buf []byte) bool { |
| 113 |
return len(buf) == 2 && buf[0] == 0x00 && buf[1] == 0x00 |
| 114 |
}) |
| 115 |
|
| 116 |
if !Is([]byte{0x00, 0x00}, "foo") { |
| 117 |
t.Fatalf("Type cannot match") |
| 118 |
} |
| 119 |
|
| 120 |
if !IsSupported("foo") { |
| 121 |
t.Fatalf("Not supported extension") |
| 122 |
} |
| 123 |
|
| 124 |
if !IsMIMESupported("foo/foo") { |
| 125 |
t.Fatalf("Not supported MIME type") |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestMatchMap(t *testing.T) { |
| 130 |
cases := []struct { |
| 131 |
buf []byte |
| 132 |
kind types.Type |
| 133 |
}{ |
| 134 |
{[]byte{0xFF, 0xD8, 0xFF}, types.Get("jpg")}, |
| 135 |
{[]byte{0x89, 0x50, 0x4E, 0x47}, types.Get("png")}, |
| 136 |
{[]byte{0xFF, 0x0, 0x0}, Unknown}, |
| 137 |
} |
| 138 |
|
| 139 |
for _, test := range cases { |
| 140 |
if kind := MatchMap(test.buf, matchers.Image); kind != test.kind { |
| 141 |
t.Fatalf("Do not matches: %#v", test.buf) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestMatchesMap(t *testing.T) { |
| 147 |
cases := []struct { |
| 148 |
buf []byte |
| 149 |
match bool |
| 150 |
}{ |
| 151 |
{[]byte{0xFF, 0xD8, 0xFF}, true}, |
| 152 |
{[]byte{0x89, 0x50, 0x4E, 0x47}, true}, |
| 153 |
{[]byte{0xFF, 0x0, 0x0}, false}, |
| 154 |
} |
| 155 |
|
| 156 |
for _, test := range cases { |
| 157 |
if match := MatchesMap(test.buf, matchers.Image); match != test.match { |
| 158 |
t.Fatalf("Do not matches: %#v", test.buf) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
var tarBuffer, _ = ioutil.ReadFile("./fixtures/sample.tar") |
| 168 |
var zipBuffer, _ = ioutil.ReadFile("./fixtures/sample.zip") |
| 169 |
var jpgBuffer, _ = ioutil.ReadFile("./fixtures/sample.jpg") |
| 170 |
var gifBuffer, _ = ioutil.ReadFile("./fixtures/sample.gif") |
| 171 |
var pngBuffer, _ = ioutil.ReadFile("./fixtures/sample.png") |
| 172 |
var xlsxBuffer, _ = ioutil.ReadFile("./fixtures/sample.xlsx") |
| 173 |
var pptxBuffer, _ = ioutil.ReadFile("./fixtures/sample.pptx") |
| 174 |
var docxBuffer, _ = ioutil.ReadFile("./fixtures/sample.docx") |
| 175 |
var dwgBuffer, _ = ioutil.ReadFile("./fixtures/sample.dwg") |
| 176 |
var mkvBuffer, _ = ioutil.ReadFile("./fixtures/sample.mkv") |
| 177 |
var webmBuffer, _ = ioutil.ReadFile("./fixtures/sample.webm") |
| 178 |
|
| 179 |
func BenchmarkMatchTar(b *testing.B) { |
| 180 |
for n := 0; n < b.N; n++ { |
| 181 |
Match(tarBuffer) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func BenchmarkMatchZip(b *testing.B) { |
| 186 |
for n := 0; n < b.N; n++ { |
| 187 |
Match(zipBuffer) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func BenchmarkMatchJpeg(b *testing.B) { |
| 192 |
for n := 0; n < b.N; n++ { |
| 193 |
Match(jpgBuffer) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func BenchmarkMatchGif(b *testing.B) { |
| 198 |
for n := 0; n < b.N; n++ { |
| 199 |
Match(gifBuffer) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func BenchmarkMatchPng(b *testing.B) { |
| 204 |
for n := 0; n < b.N; n++ { |
| 205 |
Match(pngBuffer) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func BenchmarkMatchXlsx(b *testing.B) { |
| 210 |
for n := 0; n < b.N; n++ { |
| 211 |
Match(xlsxBuffer) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func BenchmarkMatchPptx(b *testing.B) { |
| 216 |
for n := 0; n < b.N; n++ { |
| 217 |
Match(pptxBuffer) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func BenchmarkMatchDocx(b *testing.B) { |
| 222 |
for n := 0; n < b.N; n++ { |
| 223 |
Match(docxBuffer) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func BenchmarkMatchDwg(b *testing.B) { |
| 228 |
for n := 0; n < b.N; n++ { |
| 229 |
Match(dwgBuffer) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func BenchmarkMatchMkv(b *testing.B) { |
| 234 |
for n := 0; n < b.N; n++ { |
| 235 |
Match(mkvBuffer) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func BenchmarkMatchWebm(b *testing.B) { |
| 240 |
for n := 0; n < b.N; n++ { |
| 241 |
Match(webmBuffer) |
| 242 |
} |
| 243 |
} |