-
Notifications
You must be signed in to change notification settings - Fork 10
/
treemagic_test.go
123 lines (118 loc) · 3.7 KB
/
treemagic_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
package mimemagic
import (
"os"
"path/filepath"
"testing"
)
var treeMagicTests = []struct {
path string
want string
wantErr bool
}{
{"image-dcf", "x-content/image-dcf", false},
{"image-picturecd", "x-content/image-picturecd", false},
{"software", "x-content/unix-software", false},
{"video-bluray", "x-content/video-bluray", false},
{"video-dvd", "x-content/video-dvd", false},
{"video-dvd-2", "x-content/video-dvd", false},
{"video-dvd-3", "x-content/video-dvd", false},
{"video-hddvd", "x-content/video-hddvd", false},
{"video-svcd", "x-content/video-svcd", false},
{"video-vcd", "x-content/video-vcd", false},
{"dir", "inode/directory", false},
{"test.random", "application/octet-stream", false},
{".", "inode/directory", false},
{"/root", "inode/directory", true},
{"/non/existent", "application/octet-stream", true},
}
func TestMatchTreeMagic(t *testing.T) {
path, err := unpackFixtures()
if err != nil {
t.Fatalf("couldn't unpack archive: %v", err)
}
defer func() {
err := os.RemoveAll(path)
if err != nil {
panic(err)
}
}()
for _, test := range treeMagicTests {
t.Run(test.path, func(t *testing.T) {
fpath := test.path
if fpath[0] != '/' {
fpath = filepath.Join(path, test.path)
}
got, err := MatchTreeMagic(fpath)
if (err != nil) != test.wantErr {
t.Errorf("MatchTreeMagic() error = %v, wantErr %v", err, test.wantErr)
return
}
if got.MediaType() != test.want {
t.Errorf("MatchTreeMagic() = %v, want %v", got.MediaType(), test.want)
}
})
}
preserve := treeMagicSignatures
t.Run("special cases", func(t *testing.T) {
treeMagicSignatures = []treeMagic{{0, []treeMatch{{
"mpegav", -1, fileType,
false, false, false, nil,
}}}}
testfunc := func(fpath, want string) (err error) {
fpath = filepath.Join(path, fpath)
got, err := MatchTreeMagic(fpath)
if err != nil {
t.Errorf("MatchTreeMagic() error = %v", err)
return
}
if got.MediaType() != want {
t.Errorf("MatchTreeMagic() = %v, want %v", got.MediaType(), want)
}
return nil
}
testfunc("video-vcd", "inode/directory")
treeMagicSignatures[0].matchers[0].path = "mpegav/AVSEQ01.DAT"
treeMagicSignatures[0].matchers[0].executable = true
testfunc("video-vcd", "inode/directory")
treeMagicSignatures[0].matchers[0].path = "mpegav"
treeMagicSignatures[0].matchers[0].mediaType = 0
treeMagicSignatures[0].matchers[0].objectType = directoryType
treeMagicSignatures[0].matchers[0].executable = false
testfunc("video-vcd", "inode/directory")
treeMagicSignatures[0].matchers[0].mediaType = -1
treeMagicSignatures[0].matchers[0].nonEmpty = true
treeMagicSignatures[0].matchers[0].path = "dir"
testfunc(".", "inode/directory")
treeMagicSignatures[0].matchers[0].path = "core"
treeMagicSignatures[0].matchers[0].objectType = fileType
testfunc(".", "inode/directory")
treeMagicSignatures[0].matchers[0].nonEmpty = false
treeMagicSignatures[0].matchers[0].next = make([]treeMatch, 1)
treeMagicSignatures[0].matchers[0].next[0] = treeMagicSignatures[0].matchers[0]
treeMagicSignatures[0].matchers[0].next[0].next = nil
testfunc(".", "all/all")
treeMagicSignatures[0].matchers[0].next[0].nonEmpty = true
testfunc(".", "inode/directory")
})
treeMagicSignatures = preserve
}
func benchmarkMatchTreeMagic(path string, b *testing.B) {
for n := 0; n < b.N; n++ {
MatchTreeMagic(path)
}
}
func BenchmarkMatchTreeMagic(b *testing.B) {
path, err := unpackFixtures()
if err != nil {
b.Fatalf("couldn't unpack archive: %v", err)
}
defer func() {
err := os.RemoveAll(path)
if err != nil {
panic(err)
}
}()
for _, dir := range treeMagicTests {
b.Run(dir.path, func(b *testing.B) { benchmarkMatchTreeMagic(filepath.Join(path, dir.path), b) })
}
}