github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/crypto/tls/handshake_messages_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tls 6 7 import ( 8 "math/rand" 9 "reflect" 10 "testing" 11 "testing/quick" 12 ) 13 14 var tests = []interface{}{ 15 &clientHelloMsg{}, 16 &serverHelloMsg{}, 17 &finishedMsg{}, 18 19 &certificateMsg{}, 20 &certificateRequestMsg{}, 21 &certificateVerifyMsg{}, 22 &certificateStatusMsg{}, 23 &clientKeyExchangeMsg{}, 24 &nextProtoMsg{}, 25 &newSessionTicketMsg{}, 26 &sessionState{}, 27 } 28 29 type testMessage interface { 30 marshal() []byte 31 unmarshal([]byte) bool 32 equal(interface{}) bool 33 } 34 35 func TestMarshalUnmarshal(t *testing.T) { 36 rand := rand.New(rand.NewSource(0)) 37 38 for i, iface := range tests { 39 ty := reflect.ValueOf(iface).Type() 40 41 n := 100 42 if testing.Short() { 43 n = 5 44 } 45 for j := 0; j < n; j++ { 46 v, ok := quick.Value(ty, rand) 47 if !ok { 48 t.Errorf("#%d: failed to create value", i) 49 break 50 } 51 52 m1 := v.Interface().(testMessage) 53 marshaled := m1.marshal() 54 m2 := iface.(testMessage) 55 if !m2.unmarshal(marshaled) { 56 t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled) 57 break 58 } 59 m2.marshal() // to fill any marshal cache in the message 60 61 if !m1.equal(m2) { 62 t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled) 63 break 64 } 65 66 if i >= 3 { 67 // The first three message types (ClientHello, 68 // ServerHello and Finished) are allowed to 69 // have parsable prefixes because the extension 70 // data is optional and the length of the 71 // Finished varies across versions. 72 for j := 0; j < len(marshaled); j++ { 73 if m2.unmarshal(marshaled[0:j]) { 74 t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1) 75 break 76 } 77 } 78 } 79 } 80 } 81 } 82 83 func TestFuzz(t *testing.T) { 84 rand := rand.New(rand.NewSource(0)) 85 for _, iface := range tests { 86 m := iface.(testMessage) 87 88 for j := 0; j < 1000; j++ { 89 len := rand.Intn(100) 90 bytes := randomBytes(len, rand) 91 // This just looks for crashes due to bounds errors etc. 92 m.unmarshal(bytes) 93 } 94 } 95 } 96 97 func randomBytes(n int, rand *rand.Rand) []byte { 98 r := make([]byte, n) 99 for i := 0; i < n; i++ { 100 r[i] = byte(rand.Int31()) 101 } 102 return r 103 } 104 105 func randomString(n int, rand *rand.Rand) string { 106 b := randomBytes(n, rand) 107 return string(b) 108 } 109 110 func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value { 111 m := &clientHelloMsg{} 112 m.vers = uint16(rand.Intn(65536)) 113 m.random = randomBytes(32, rand) 114 m.sessionId = randomBytes(rand.Intn(32), rand) 115 m.cipherSuites = make([]uint16, rand.Intn(63)+1) 116 for i := 0; i < len(m.cipherSuites); i++ { 117 m.cipherSuites[i] = uint16(rand.Int31()) 118 } 119 m.compressionMethods = randomBytes(rand.Intn(63)+1, rand) 120 if rand.Intn(10) > 5 { 121 m.nextProtoNeg = true 122 } 123 if rand.Intn(10) > 5 { 124 m.serverName = randomString(rand.Intn(255), rand) 125 } 126 m.ocspStapling = rand.Intn(10) > 5 127 m.supportedPoints = randomBytes(rand.Intn(5)+1, rand) 128 m.supportedCurves = make([]CurveID, rand.Intn(5)+1) 129 for i := range m.supportedCurves { 130 m.supportedCurves[i] = CurveID(rand.Intn(30000)) 131 } 132 if rand.Intn(10) > 5 { 133 m.ticketSupported = true 134 if rand.Intn(10) > 5 { 135 m.sessionTicket = randomBytes(rand.Intn(300), rand) 136 } 137 } 138 if rand.Intn(10) > 5 { 139 m.signatureAndHashes = supportedSKXSignatureAlgorithms 140 } 141 m.alpnProtocols = make([]string, rand.Intn(5)) 142 for i := range m.alpnProtocols { 143 m.alpnProtocols[i] = randomString(rand.Intn(20)+1, rand) 144 } 145 146 return reflect.ValueOf(m) 147 } 148 149 func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value { 150 m := &serverHelloMsg{} 151 m.vers = uint16(rand.Intn(65536)) 152 m.random = randomBytes(32, rand) 153 m.sessionId = randomBytes(rand.Intn(32), rand) 154 m.cipherSuite = uint16(rand.Int31()) 155 m.compressionMethod = uint8(rand.Intn(256)) 156 157 if rand.Intn(10) > 5 { 158 m.nextProtoNeg = true 159 160 n := rand.Intn(10) 161 m.nextProtos = make([]string, n) 162 for i := 0; i < n; i++ { 163 m.nextProtos[i] = randomString(20, rand) 164 } 165 } 166 167 if rand.Intn(10) > 5 { 168 m.ocspStapling = true 169 } 170 if rand.Intn(10) > 5 { 171 m.ticketSupported = true 172 } 173 m.alpnProtocol = randomString(rand.Intn(32)+1, rand) 174 175 return reflect.ValueOf(m) 176 } 177 178 func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value { 179 m := &certificateMsg{} 180 numCerts := rand.Intn(20) 181 m.certificates = make([][]byte, numCerts) 182 for i := 0; i < numCerts; i++ { 183 m.certificates[i] = randomBytes(rand.Intn(10)+1, rand) 184 } 185 return reflect.ValueOf(m) 186 } 187 188 func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value { 189 m := &certificateRequestMsg{} 190 m.certificateTypes = randomBytes(rand.Intn(5)+1, rand) 191 numCAs := rand.Intn(100) 192 m.certificateAuthorities = make([][]byte, numCAs) 193 for i := 0; i < numCAs; i++ { 194 m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand) 195 } 196 return reflect.ValueOf(m) 197 } 198 199 func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value { 200 m := &certificateVerifyMsg{} 201 m.signature = randomBytes(rand.Intn(15)+1, rand) 202 return reflect.ValueOf(m) 203 } 204 205 func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value { 206 m := &certificateStatusMsg{} 207 if rand.Intn(10) > 5 { 208 m.statusType = statusTypeOCSP 209 m.response = randomBytes(rand.Intn(10)+1, rand) 210 } else { 211 m.statusType = 42 212 } 213 return reflect.ValueOf(m) 214 } 215 216 func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value { 217 m := &clientKeyExchangeMsg{} 218 m.ciphertext = randomBytes(rand.Intn(1000)+1, rand) 219 return reflect.ValueOf(m) 220 } 221 222 func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value { 223 m := &finishedMsg{} 224 m.verifyData = randomBytes(12, rand) 225 return reflect.ValueOf(m) 226 } 227 228 func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value { 229 m := &nextProtoMsg{} 230 m.proto = randomString(rand.Intn(255), rand) 231 return reflect.ValueOf(m) 232 } 233 234 func (*newSessionTicketMsg) Generate(rand *rand.Rand, size int) reflect.Value { 235 m := &newSessionTicketMsg{} 236 m.ticket = randomBytes(rand.Intn(4), rand) 237 return reflect.ValueOf(m) 238 } 239 240 func (*sessionState) Generate(rand *rand.Rand, size int) reflect.Value { 241 s := &sessionState{} 242 s.vers = uint16(rand.Intn(10000)) 243 s.cipherSuite = uint16(rand.Intn(10000)) 244 s.masterSecret = randomBytes(rand.Intn(100), rand) 245 numCerts := rand.Intn(20) 246 s.certificates = make([][]byte, numCerts) 247 for i := 0; i < numCerts; i++ { 248 s.certificates[i] = randomBytes(rand.Intn(10)+1, rand) 249 } 250 return reflect.ValueOf(s) 251 }