-
Notifications
You must be signed in to change notification settings - Fork 36
/
fuzz_test.go
89 lines (79 loc) · 2.11 KB
/
fuzz_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
package mint
import (
"math/rand"
"testing"
)
var structs = []interface{}{
// Handshake messages
&ClientHelloBody{},
&ServerHelloBody{},
&FinishedBody{VerifyDataLen: 32},
&EncryptedExtensionsBody{},
&CertificateBody{},
&CertificateVerifyBody{},
// Extensions
&Extension{},
&ExtensionList{},
new(ServerNameExtension),
&ALPNExtension{},
&KeyShareExtension{HandshakeType: HandshakeTypeClientHello},
&KeyShareExtension{HandshakeType: HandshakeTypeHelloRetryRequest},
&KeyShareExtension{HandshakeType: HandshakeTypeServerHello},
&SupportedGroupsExtension{},
&SignatureAlgorithmsExtension{},
&PreSharedKeyExtension{HandshakeType: HandshakeTypeClientHello},
&PreSharedKeyExtension{HandshakeType: HandshakeTypeServerHello},
&SupportedVersionsExtension{},
}
var validHex = []string{
// Handshake messages
chValidHex,
shValidHex,
finValidHex,
encExtValidHex,
certValidHex,
certVerifyValidHex,
// Extensions
extValidHex,
extListValidHex,
validExtensionTestCases[ExtensionTypeServerName].marshaledHex,
validExtensionTestCases[ExtensionTypeALPN].marshaledHex,
keyShareClientHex,
keyShareHelloRetryHex,
keyShareServerHex,
validExtensionTestCases[ExtensionTypeSupportedGroups].marshaledHex,
validExtensionTestCases[ExtensionTypeSignatureAlgorithms].marshaledHex,
pskClientHex,
pskServerHex,
validExtensionTestCases[ExtensionTypeSupportedVersions].marshaledHex,
}
func randomBytes(n int, rand *rand.Rand) []byte {
r := make([]byte, n)
for i := 0; i < n; i++ {
r[i] = byte(rand.Int31())
}
return r
}
type unmarshaler interface {
Unmarshal([]byte) (int, error)
}
// This just looks for crashes due to bounds errors etc.
func TestFuzz(t *testing.T) {
rand := rand.New(rand.NewSource(0))
for i, iface := range structs {
m := iface.(unmarshaler)
// Provide random data
for j := 0; j < 100; j++ {
len := rand.Intn(1024)
bytes := randomBytes(len, rand)
m.Unmarshal(bytes)
}
// Provide partially valid data
valid := unhex(validHex[i])
random := randomBytes(10*len(valid), rand)
for cut := 0; cut < len(valid)-1; cut++ {
testCase := append(valid[:cut], random...)
m.Unmarshal(testCase)
}
}
}