Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

210 Zeilen
5.1 KiB

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