PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/MSOpenTech/azure-sdk-for-go/core/tls/handshake_messages_test.go

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