forked from h2non/filetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_test.go
167 lines (142 loc) · 3.21 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
package filetype
import (
"bytes"
"io"
"io/ioutil"
"testing"
)
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 := MatchBuffer(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"},
}
for _, test := range cases {
kind, _ := MatchFile("./test_data/sample." + test.ext)
if kind.Extension != test.ext {
t.Fatalf("Invalid image type: %s != %s", kind.Extension, test.ext)
}
}
}
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 := NewType("foo", "foo/foo")
NewMatcher(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 Typed
}{
{[]byte{0xFF, 0xD8, 0xFF}, GetByExtension("jpg")},
{[]byte{0x89, 0x50, 0x4E, 0x47}, GetByExtension("png")},
{[]byte{0xFF, 0x0, 0x0}, Unknown},
}
for _, test := range cases {
if kind := MatchMap(test.buf, Image); kind != test.kind {
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")
func BenchmarkMatchTar(b *testing.B) {
for n := 0; n < b.N; n++ {
MatchBuffer(tarBuffer)
}
}
func BenchmarkMatchZip(b *testing.B) {
for n := 0; n < b.N; n++ {
MatchBuffer(zipBuffer)
}
}
func BenchmarkMatchJpeg(b *testing.B) {
for n := 0; n < b.N; n++ {
MatchBuffer(jpgBuffer)
}
}
func BenchmarkMatchGif(b *testing.B) {
for n := 0; n < b.N; n++ {
MatchBuffer(gifBuffer)
}
}
func BenchmarkMatchPng(b *testing.B) {
for n := 0; n < b.N; n++ {
MatchBuffer(pngBuffer)
}
}