PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/libgo/go/crypto/tls/handshake_messages_test.go

https://github.com/thurday/gcc
Go | 206 lines | 171 code | 27 blank | 8 comment | 22 complexity | 139551b9b8794af6de3514d4b89f22bc MD5 | raw file
  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. package tls
  5. import (
  6. "rand"
  7. "reflect"
  8. "testing"
  9. "testing/quick"
  10. )
  11. var tests = []interface{}{
  12. &clientHelloMsg{},
  13. &serverHelloMsg{},
  14. &certificateMsg{},
  15. &certificateRequestMsg{},
  16. &certificateVerifyMsg{},
  17. &certificateStatusMsg{},
  18. &clientKeyExchangeMsg{},
  19. &finishedMsg{},
  20. &nextProtoMsg{},
  21. }
  22. type testMessage interface {
  23. marshal() []byte
  24. unmarshal([]byte) bool
  25. }
  26. func TestMarshalUnmarshal(t *testing.T) {
  27. rand := rand.New(rand.NewSource(0))
  28. for i, iface := range tests {
  29. ty := reflect.ValueOf(iface).Type()
  30. n := 100
  31. if testing.Short() {
  32. n = 5
  33. }
  34. for j := 0; j < n; j++ {
  35. v, ok := quick.Value(ty, rand)
  36. if !ok {
  37. t.Errorf("#%d: failed to create value", i)
  38. break
  39. }
  40. m1 := v.Interface().(testMessage)
  41. marshaled := m1.marshal()
  42. m2 := iface.(testMessage)
  43. if !m2.unmarshal(marshaled) {
  44. t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled)
  45. break
  46. }
  47. m2.marshal() // to fill any marshal cache in the message
  48. if !reflect.DeepEqual(m1, m2) {
  49. t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled)
  50. break
  51. }
  52. if i >= 2 {
  53. // The first two message types (ClientHello and
  54. // ServerHello) are allowed to have parsable
  55. // prefixes because the extension data is
  56. // optional.
  57. for j := 0; j < len(marshaled); j++ {
  58. if m2.unmarshal(marshaled[0:j]) {
  59. t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
  60. break
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. func TestFuzz(t *testing.T) {
  68. rand := rand.New(rand.NewSource(0))
  69. for _, iface := range tests {
  70. m := iface.(testMessage)
  71. for j := 0; j < 1000; j++ {
  72. len := rand.Intn(100)
  73. bytes := randomBytes(len, rand)
  74. // This just looks for crashes due to bounds errors etc.
  75. m.unmarshal(bytes)
  76. }
  77. }
  78. }
  79. func randomBytes(n int, rand *rand.Rand) []byte {
  80. r := make([]byte, n)
  81. for i := 0; i < n; i++ {
  82. r[i] = byte(rand.Int31())
  83. }
  84. return r
  85. }
  86. func randomString(n int, rand *rand.Rand) string {
  87. b := randomBytes(n, rand)
  88. return string(b)
  89. }
  90. func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  91. m := &clientHelloMsg{}
  92. m.vers = uint16(rand.Intn(65536))
  93. m.random = randomBytes(32, rand)
  94. m.sessionId = randomBytes(rand.Intn(32), rand)
  95. m.cipherSuites = make([]uint16, rand.Intn(63)+1)
  96. for i := 0; i < len(m.cipherSuites); i++ {
  97. m.cipherSuites[i] = uint16(rand.Int31())
  98. }
  99. m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
  100. if rand.Intn(10) > 5 {
  101. m.nextProtoNeg = true
  102. }
  103. if rand.Intn(10) > 5 {
  104. m.serverName = randomString(rand.Intn(255), rand)
  105. }
  106. m.ocspStapling = rand.Intn(10) > 5
  107. m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
  108. m.supportedCurves = make([]uint16, rand.Intn(5)+1)
  109. for i := range m.supportedCurves {
  110. m.supportedCurves[i] = uint16(rand.Intn(30000))
  111. }
  112. return reflect.ValueOf(m)
  113. }
  114. func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  115. m := &serverHelloMsg{}
  116. m.vers = uint16(rand.Intn(65536))
  117. m.random = randomBytes(32, rand)
  118. m.sessionId = randomBytes(rand.Intn(32), rand)
  119. m.cipherSuite = uint16(rand.Int31())
  120. m.compressionMethod = uint8(rand.Intn(256))
  121. if rand.Intn(10) > 5 {
  122. m.nextProtoNeg = true
  123. n := rand.Intn(10)
  124. m.nextProtos = make([]string, n)
  125. for i := 0; i < n; i++ {
  126. m.nextProtos[i] = randomString(20, rand)
  127. }
  128. }
  129. return reflect.ValueOf(m)
  130. }
  131. func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  132. m := &certificateMsg{}
  133. numCerts := rand.Intn(20)
  134. m.certificates = make([][]byte, numCerts)
  135. for i := 0; i < numCerts; i++ {
  136. m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
  137. }
  138. return reflect.ValueOf(m)
  139. }
  140. func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  141. m := &certificateRequestMsg{}
  142. m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
  143. numCAs := rand.Intn(100)
  144. m.certificateAuthorities = make([][]byte, numCAs)
  145. for i := 0; i < numCAs; i++ {
  146. m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
  147. }
  148. return reflect.ValueOf(m)
  149. }
  150. func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  151. m := &certificateVerifyMsg{}
  152. m.signature = randomBytes(rand.Intn(15)+1, rand)
  153. return reflect.ValueOf(m)
  154. }
  155. func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  156. m := &certificateStatusMsg{}
  157. if rand.Intn(10) > 5 {
  158. m.statusType = statusTypeOCSP
  159. m.response = randomBytes(rand.Intn(10)+1, rand)
  160. } else {
  161. m.statusType = 42
  162. }
  163. return reflect.ValueOf(m)
  164. }
  165. func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  166. m := &clientKeyExchangeMsg{}
  167. m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
  168. return reflect.ValueOf(m)
  169. }
  170. func (*finishedMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  171. m := &finishedMsg{}
  172. m.verifyData = randomBytes(12, rand)
  173. return reflect.ValueOf(m)
  174. }
  175. func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  176. m := &nextProtoMsg{}
  177. m.proto = randomString(rand.Intn(255), rand)
  178. return reflect.ValueOf(m)
  179. }