| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var structs = []interface{}{ |
| 9 |
|
| 10 |
&ClientHelloBody{}, |
| 11 |
&ServerHelloBody{}, |
| 12 |
&FinishedBody{VerifyDataLen: 32}, |
| 13 |
&EncryptedExtensionsBody{}, |
| 14 |
&CertificateBody{}, |
| 15 |
&CertificateVerifyBody{}, |
| 16 |
|
| 17 |
|
| 18 |
&Extension{}, |
| 19 |
&ExtensionList{}, |
| 20 |
new(ServerNameExtension), |
| 21 |
&ALPNExtension{}, |
| 22 |
&KeyShareExtension{HandshakeType: HandshakeTypeClientHello}, |
| 23 |
&KeyShareExtension{HandshakeType: HandshakeTypeHelloRetryRequest}, |
| 24 |
&KeyShareExtension{HandshakeType: HandshakeTypeServerHello}, |
| 25 |
&SupportedGroupsExtension{}, |
| 26 |
&SignatureAlgorithmsExtension{}, |
| 27 |
&PreSharedKeyExtension{HandshakeType: HandshakeTypeClientHello}, |
| 28 |
&PreSharedKeyExtension{HandshakeType: HandshakeTypeServerHello}, |
| 29 |
&SupportedVersionsExtension{}, |
| 30 |
} |
| 31 |
|
| 32 |
var validHex = []string{ |
| 33 |
|
| 34 |
chValidHex, |
| 35 |
shValidHex, |
| 36 |
finValidHex, |
| 37 |
encExtValidHex, |
| 38 |
certValidHex, |
| 39 |
certVerifyValidHex, |
| 40 |
|
| 41 |
|
| 42 |
extValidHex, |
| 43 |
extListValidHex, |
| 44 |
validExtensionTestCases[ExtensionTypeServerName].marshaledHex, |
| 45 |
validExtensionTestCases[ExtensionTypeALPN].marshaledHex, |
| 46 |
keyShareClientHex, |
| 47 |
keyShareHelloRetryHex, |
| 48 |
keyShareServerHex, |
| 49 |
validExtensionTestCases[ExtensionTypeSupportedGroups].marshaledHex, |
| 50 |
validExtensionTestCases[ExtensionTypeSignatureAlgorithms].marshaledHex, |
| 51 |
pskClientHex, |
| 52 |
pskServerHex, |
| 53 |
validExtensionTestCases[ExtensionTypeSupportedVersions].marshaledHex, |
| 54 |
} |
| 55 |
|
| 56 |
func randomBytes(n int, rand *rand.Rand) []byte { |
| 57 |
r := make([]byte, n) |
| 58 |
for i := 0; i < n; i++ { |
| 59 |
r[i] = byte(rand.Int31()) |
| 60 |
} |
| 61 |
return r |
| 62 |
} |
| 63 |
|
| 64 |
type unmarshaler interface { |
| 65 |
Unmarshal([]byte) (int, error) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func TestFuzz(t *testing.T) { |
| 70 |
rand := rand.New(rand.NewSource(0)) |
| 71 |
for i, iface := range structs { |
| 72 |
m := iface.(unmarshaler) |
| 73 |
|
| 74 |
|
| 75 |
for j := 0; j < 100; j++ { |
| 76 |
len := rand.Intn(1024) |
| 77 |
bytes := randomBytes(len, rand) |
| 78 |
m.Unmarshal(bytes) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
valid := unhex(validHex[i]) |
| 83 |
random := randomBytes(10*len(valid), rand) |
| 84 |
for cut := 0; cut < len(valid)-1; cut++ { |
| 85 |
testCase := append(valid[:cut], random...) |
| 86 |
m.Unmarshal(testCase) |
| 87 |
} |
| 88 |
} |
| 89 |
} |