-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_test.go
More file actions
155 lines (141 loc) · 3.11 KB
/
Copy pathmagic_test.go
File metadata and controls
155 lines (141 loc) · 3.11 KB
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
package magic
import (
"sync"
"testing"
)
func TestDetectPrefix(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []byte
expect Result
}{
{
name: "empty",
input: nil,
expect: Result{Kind: KindUnknown, Reason: ReasonNeedMore},
},
{
name: "plain text",
input: []byte("package magic\n"),
expect: Result{
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Reason: ReasonNeedMore,
},
},
{
name: "truncated PNG",
input: []byte("\x89PNG\r\n"),
expect: Result{
Kind: KindUnknown,
Reason: ReasonNeedMore,
},
},
{
name: "complete PNG signature",
input: []byte("\x89PNG\r\n\x1a\n"),
expect: Result{
Kind: KindBinary,
MIME: mimePNG,
Format: FormatPNG,
},
},
{
name: "short binary heuristic",
input: []byte{'a', 0, 'b'},
expect: Result{
Kind: KindBinary,
Reason: ReasonNeedMore,
},
},
{
name: "long binary heuristic",
input: append([]byte{'a', 0, 'b'}, make([]byte, sniffLength)...),
expect: Result{
Kind: KindBinary,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assertResult(t, DetectPrefix(test.input), test.expect)
})
}
}
func TestDetectIsDeterministicUnderConcurrentUse(t *testing.T) {
t.Parallel()
inputs := [][]byte{
[]byte("package magic\n"),
[]byte("\xff\xfeh\x00i\x00"),
[]byte("\x89PNG\r\n\x1a\n"),
[]byte(" \n<?xml version=\"1.0\"?><svg>"),
[]byte{0xff},
}
expected := make([]Result, len(inputs))
for index, input := range inputs {
expected[index] = Detect(input)
}
const goroutines = 24
const iterations = 500
var wait sync.WaitGroup
errors := make(chan string, goroutines)
for worker := range goroutines {
wait.Add(1)
go func() {
defer wait.Done()
for iteration := range iterations {
index := (worker + iteration) % len(inputs)
if got := Detect(inputs[index]); got != expected[index] {
errors <- "Detect returned different results for the same bytes"
return
}
}
}()
}
wait.Wait()
close(errors)
for message := range errors {
t.Error(message)
}
}
func TestDetectDoesNotRetainInput(t *testing.T) {
t.Parallel()
input := []byte("hello")
got := Detect(input)
for index := range input {
input[index] = 0
}
assertResult(t, got, Result{
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
})
}
func TestDetectAllocations(t *testing.T) {
inputs := [][]byte{
make([]byte, 4096),
[]byte("package magic\n"),
[]byte(`{"schemaVersion":2}`),
[]byte("\xff\xfeh\x00i\x00"),
[]byte("\x89PNG\r\n\x1a\n"),
makeNativePHAR(pharTestStub, "", nil, pharTestEntry{name: "file", content: []byte("data")}),
}
for _, input := range inputs {
if allocations := testing.AllocsPerRun(1000, func() {
Detect(input)
}); allocations != 0 {
t.Fatalf("Detect allocated %.2f times for %x", allocations, input[:min(len(input), 16)])
}
}
}
func assertResult(t testing.TB, got, expect Result) {
t.Helper()
if got != expect {
t.Fatalf("Detect() = %#v, want %#v", got, expect)
}
}