-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathmatch_test.go
252 lines (217 loc) · 4.96 KB
/
match_test.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package filetype
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/h2non/filetype/matchers"
"github.com/h2non/filetype/types"
)
func TestMatch(t *testing.T) {
cases := []struct {
buf []byte
ext string
}{
{[]byte{0xFF, 0xD8, 0xFF}, "jpg"},
{[]byte{0xFF, 0xD8, 0x00}, "unknown"},
{[]byte{0x89, 0x50, 0x4E, 0x47}, "png"},
}
for _, test := range cases {
match, err := Match(test.buf)
if err != nil {
t.Fatalf("Error: %s", err)
}
if match.Extension != test.ext {
t.Fatalf("Invalid image type: %s != %s", match.Extension, test.ext)
}
}
}
func TestMatchFile(t *testing.T) {
cases := []struct {
ext string
}{
{"gif"},
{"jpg"},
{"png"},
{"zip"},
{"tar"},
{"tif"},
{"mp4"},
{"mkv"},
{"webm"},
{"docx"},
{"pptx"},
{"xlsx"},
{"mov"},
{"wasm"},
{"dwg"},
{"zst"},
{"exr"},
{"avif"},
}
for _, test := range cases {
kind, _ := MatchFile("./fixtures/sample." + test.ext)
if kind.Extension != test.ext {
t.Fatalf("Invalid type: %s != %s", kind.Extension, test.ext)
}
}
// test zstd with skippable frame
kind, _ := MatchFile("./fixtures/sample_skippable.zst")
if kind.Extension != "zst" {
t.Fatalf("Invalid type: %s != %s", kind.Extension, "zst")
}
}
func TestMatchReader(t *testing.T) {
cases := []struct {
buf io.Reader
ext string
}{
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0xFF}), "jpg"},
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0x00}), "unknown"},
{bytes.NewBuffer([]byte{0x89, 0x50, 0x4E, 0x47}), "png"},
}
for _, test := range cases {
match, err := MatchReader(test.buf)
if err != nil {
t.Fatalf("Error: %s", err)
}
if match.Extension != test.ext {
t.Fatalf("Invalid image type: %s", match.Extension)
}
}
}
func TestMatches(t *testing.T) {
cases := []struct {
buf []byte
match bool
}{
{[]byte{0xFF, 0xD8, 0xFF}, true},
{[]byte{0xFF, 0x0, 0x0}, false},
{[]byte{0x89, 0x50, 0x4E, 0x47}, true},
}
for _, test := range cases {
if Matches(test.buf) != test.match {
t.Fatalf("Do not matches: %#v", test.buf)
}
}
}
func TestAddMatcher(t *testing.T) {
fileType := AddType("foo", "foo/foo")
AddMatcher(fileType, func(buf []byte) bool {
return len(buf) == 2 && buf[0] == 0x00 && buf[1] == 0x00
})
if !Is([]byte{0x00, 0x00}, "foo") {
t.Fatalf("Type cannot match")
}
if !IsSupported("foo") {
t.Fatalf("Not supported extension")
}
if !IsMIMESupported("foo/foo") {
t.Fatalf("Not supported MIME type")
}
}
func TestMatchMap(t *testing.T) {
cases := []struct {
buf []byte
kind types.Type
}{
{[]byte{0xFF, 0xD8, 0xFF}, types.Get("jpg")},
{[]byte{0x89, 0x50, 0x4E, 0x47}, types.Get("png")},
{[]byte{0xFF, 0x0, 0x0}, Unknown},
}
for _, test := range cases {
if kind := MatchMap(test.buf, matchers.Image); kind != test.kind {
t.Fatalf("Do not matches: %#v", test.buf)
}
}
}
func TestMatchesMap(t *testing.T) {
cases := []struct {
buf []byte
match bool
}{
{[]byte{0xFF, 0xD8, 0xFF}, true},
{[]byte{0x89, 0x50, 0x4E, 0x47}, true},
{[]byte{0xFF, 0x0, 0x0}, false},
}
for _, test := range cases {
if match := MatchesMap(test.buf, matchers.Image); match != test.match {
t.Fatalf("Do not matches: %#v", test.buf)
}
}
}
//
// Benchmarks
//
var tarBuffer, _ = ioutil.ReadFile("./fixtures/sample.tar")
var zipBuffer, _ = ioutil.ReadFile("./fixtures/sample.zip")
var jpgBuffer, _ = ioutil.ReadFile("./fixtures/sample.jpg")
var gifBuffer, _ = ioutil.ReadFile("./fixtures/sample.gif")
var pngBuffer, _ = ioutil.ReadFile("./fixtures/sample.png")
var xlsxBuffer, _ = ioutil.ReadFile("./fixtures/sample.xlsx")
var pptxBuffer, _ = ioutil.ReadFile("./fixtures/sample.pptx")
var docxBuffer, _ = ioutil.ReadFile("./fixtures/sample.docx")
var dwgBuffer, _ = ioutil.ReadFile("./fixtures/sample.dwg")
var mkvBuffer, _ = ioutil.ReadFile("./fixtures/sample.mkv")
var webmBuffer, _ = ioutil.ReadFile("./fixtures/sample.webm")
var exrBuffer, _ = ioutil.ReadFile("./fixtures/sample.exr")
func BenchmarkMatchTar(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(tarBuffer)
}
}
func BenchmarkMatchZip(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(zipBuffer)
}
}
func BenchmarkMatchJpeg(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(jpgBuffer)
}
}
func BenchmarkMatchGif(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(gifBuffer)
}
}
func BenchmarkMatchPng(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(pngBuffer)
}
}
func BenchmarkMatchXlsx(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(xlsxBuffer)
}
}
func BenchmarkMatchPptx(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(pptxBuffer)
}
}
func BenchmarkMatchDocx(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(docxBuffer)
}
}
func BenchmarkMatchDwg(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(dwgBuffer)
}
}
func BenchmarkMatchMkv(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(mkvBuffer)
}
}
func BenchmarkMatchWebm(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(webmBuffer)
}
}
func BenchmarkMatchExr(b *testing.B) {
for n := 0; n < b.N; n++ {
Match(exrBuffer)
}
}