Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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