-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
179 lines (166 loc) · 4.71 KB
/
Copy pathfuzz_test.go
File metadata and controls
179 lines (166 loc) · 4.71 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package magic
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"unicode/utf8"
)
func FuzzDetect(f *testing.F) {
seeds := [][]byte{
nil,
[]byte("hello\n"),
[]byte("\xef\xbb\xbfhello"),
[]byte("\xff\xfeh\x00i\x00"),
[]byte("\xfe\xff\x00h\x00i"),
[]byte("\xff\xfe\x01"),
[]byte("\xff\xfe\x01\x00"),
[]byte{'a', 0, 'b'},
[]byte("hello\x01"),
[]byte{0xff},
[]byte("PK\x03\x04"),
[]byte("\x1f\x8b\x08"),
[]byte("BZh9"),
[]byte("\xfd7zXZ\x00"),
[]byte("%PDF-"),
[]byte("\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1"),
[]byte("\x89PNG\r\n\x1a\n"),
[]byte("\xff\xd8\xff"),
[]byte("GIF89a"),
[]byte("<html>"),
[]byte("<?xml version=\"1.0\"?>"),
[]byte("<svg>"),
[]byte(`{"schemaVersion":2}`),
[]byte(`[1,"two",false,null]`),
[]byte(`"value"`),
[]byte(`1e+2`),
[]byte(`{"truncated"`),
[]byte(`1e+`),
}
for _, seed := range seeds {
f.Add(seed)
}
f.Add(makeTAR(f))
f.Add(makeNativePHAR(pharTestStub, "", nil, pharTestEntry{name: "file", content: []byte("data")}))
f.Add(makeJSONTARCollision(f))
f.Fuzz(func(t *testing.T, data []byte) {
first := Detect(data)
second := Detect(data)
if first != second {
t.Fatalf("Detect is not deterministic: %#v then %#v", first, second)
}
assertResultInvariants(t, first, false, len(data))
binary, _ := binaryFormat(data)
container := hasJSONContainerPrefix(data)
expectJSON := binary == "" && container && json.Valid(data) && utf8.Valid(data)
if got := first.Format == FormatJSON; got != expectJSON {
t.Fatalf("Detect JSON match = %v, want %v for %x", got, expectJSON, data)
}
prefix := DetectPrefix(data)
if len(data) > 0 {
expectedPrefix := first
if container && parseJSON(data) == jsonIncomplete {
expectedPrefix.Format = FormatJSON
expectedPrefix.MIME = mimeJSON
}
if prefix.Reason == ReasonNeedMore {
expectedPrefix.Reason = ReasonNeedMore
}
if prefix != expectedPrefix {
t.Fatalf("DetectPrefix = %#v, incompatible with Detect = %#v", prefix, first)
}
}
if binary != "" && prefix != first {
t.Fatalf("terminal binary signature changed for prefix: %#v, complete: %#v", prefix, first)
}
})
}
func makeJSONTARCollision(t testing.TB) []byte {
t.Helper()
data := bytes.Repeat([]byte{'a'}, sniffLength)
data[0] = '"'
data[len(data)-1] = '"'
copy(data[tarMagicOffset:tarMagicEnd], "ustar ")
checksum := 0
for index, value := range data {
if index >= tarChecksumFrom && index < tarChecksumTo {
checksum += ' '
} else {
checksum += int(value)
}
}
copy(data[tarChecksumFrom:tarChecksumTo], fmt.Sprintf("%06o ", checksum))
if !json.Valid(data) {
t.Fatal("JSON/TAR fixture is not valid JSON")
}
if format, _ := binaryFormat(data); format != FormatTAR {
t.Fatalf("JSON/TAR fixture format = %q, want %q", format, FormatTAR)
}
return data
}
func FuzzDetectPrefix(f *testing.F) {
seeds := [][]byte{
nil,
[]byte("hello"),
[]byte("\xef\xbb"),
[]byte("\xff\xfeh"),
[]byte("PK\x03"),
[]byte("\x89PNG\r\n"),
[]byte("\x89PNG\r\n\x1a\n"),
[]byte("<svg>"),
[]byte(`{"schemaVersion":2}`),
[]byte(`{"truncated"`),
[]byte(`1e+`),
}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, data []byte) {
first := DetectPrefix(data)
second := DetectPrefix(data)
if first != second {
t.Fatalf("DetectPrefix is not deterministic: %#v then %#v", first, second)
}
assertResultInvariants(t, first, true, len(data))
if len(data) == 0 {
assertResult(t, first, Result{Kind: KindUnknown, Reason: ReasonNeedMore})
}
})
}
func assertResultInvariants(t testing.TB, result Result, prefix bool, inputLength int) {
t.Helper()
if result.NeedBytes < 0 {
t.Fatalf("negative NeedBytes: %#v", result)
}
switch result.Kind {
case KindUnknown, KindText, KindBinary:
default:
t.Fatalf("invalid kind: %#v", result)
}
switch result.Reason {
case ReasonNone, ReasonNeedMore, ReasonInvalidText:
default:
t.Fatalf("invalid reason: %#v", result)
}
if !prefix && result.Reason == ReasonNeedMore {
t.Fatalf("complete input needs more bytes: %#v", result)
}
if prefix && inputLength > 0 && result.Reason == ReasonInvalidText {
t.Fatalf("prefix returned invalid-text: %#v", result)
}
if result.Reason == ReasonInvalidText && result.Kind != KindUnknown {
t.Fatalf("invalid-text with non-unknown kind: %#v", result)
}
if result.Reason == ReasonNeedMore && !prefix {
t.Fatalf("need-more for complete input: %#v", result)
}
if result.Encoding != "" && result.Kind != KindText {
t.Fatalf("encoding set for non-text result: %#v", result)
}
if result.Kind == KindText && (result.Format == "" || result.MIME == "") {
t.Fatalf("text result lacks metadata: %#v", result)
}
if (result.Format == "") != (result.MIME == "") {
t.Fatalf("format and MIME disagree: %#v", result)
}
}