messages_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2011 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 ssh
  5. import (
  6. "math/big"
  7. "math/rand"
  8. "reflect"
  9. "testing"
  10. "testing/quick"
  11. )
  12. var intLengthTests = []struct {
  13. val, length int
  14. }{
  15. {0, 4 + 0},
  16. {1, 4 + 1},
  17. {127, 4 + 1},
  18. {128, 4 + 2},
  19. {-1, 4 + 1},
  20. }
  21. func TestIntLength(t *testing.T) {
  22. for _, test := range intLengthTests {
  23. v := new(big.Int).SetInt64(int64(test.val))
  24. length := intLength(v)
  25. if length != test.length {
  26. t.Errorf("For %d, got length %d but expected %d", test.val, length, test.length)
  27. }
  28. }
  29. }
  30. var messageTypes = []interface{}{
  31. &kexInitMsg{},
  32. &kexDHInitMsg{},
  33. &serviceRequestMsg{},
  34. &serviceAcceptMsg{},
  35. &userAuthRequestMsg{},
  36. &channelOpenMsg{},
  37. &channelOpenConfirmMsg{},
  38. &channelOpenFailureMsg{},
  39. &channelRequestMsg{},
  40. &channelRequestSuccessMsg{},
  41. }
  42. func TestMarshalUnmarshal(t *testing.T) {
  43. rand := rand.New(rand.NewSource(0))
  44. for i, iface := range messageTypes {
  45. ty := reflect.ValueOf(iface).Type()
  46. n := 100
  47. if testing.Short() {
  48. n = 5
  49. }
  50. for j := 0; j < n; j++ {
  51. v, ok := quick.Value(ty, rand)
  52. if !ok {
  53. t.Errorf("#%d: failed to create value", i)
  54. break
  55. }
  56. m1 := v.Elem().Interface()
  57. m2 := iface
  58. marshaled := marshal(msgIgnore, m1)
  59. if err := unmarshal(m2, marshaled, msgIgnore); err != nil {
  60. t.Errorf("#%d failed to unmarshal %#v: %s", i, m1, err)
  61. break
  62. }
  63. if !reflect.DeepEqual(v.Interface(), m2) {
  64. t.Errorf("#%d\ngot: %#v\nwant:%#v\n%x", i, m2, m1, marshaled)
  65. break
  66. }
  67. }
  68. }
  69. }
  70. func TestBareMarshalUnmarshal(t *testing.T) {
  71. type S struct {
  72. I uint32
  73. S string
  74. B bool
  75. }
  76. s := S{42, "hello", true}
  77. packet := marshal(0, s)
  78. roundtrip := S{}
  79. unmarshal(&roundtrip, packet, 0)
  80. if !reflect.DeepEqual(s, roundtrip) {
  81. t.Errorf("got %#v, want %#v", roundtrip, s)
  82. }
  83. }
  84. func TestBareMarshal(t *testing.T) {
  85. type S2 struct {
  86. I uint32
  87. }
  88. s := S2{42}
  89. packet := marshal(0, s)
  90. i, rest, ok := parseUint32(packet)
  91. if len(rest) > 0 || !ok {
  92. t.Errorf("parseInt(%q): parse error", packet)
  93. }
  94. if i != s.I {
  95. t.Errorf("got %d, want %d", i, s.I)
  96. }
  97. }
  98. func randomBytes(out []byte, rand *rand.Rand) {
  99. for i := 0; i < len(out); i++ {
  100. out[i] = byte(rand.Int31())
  101. }
  102. }
  103. func randomNameList(rand *rand.Rand) []string {
  104. ret := make([]string, rand.Int31()&15)
  105. for i := range ret {
  106. s := make([]byte, 1+(rand.Int31()&15))
  107. for j := range s {
  108. s[j] = 'a' + uint8(rand.Int31()&15)
  109. }
  110. ret[i] = string(s)
  111. }
  112. return ret
  113. }
  114. func randomInt(rand *rand.Rand) *big.Int {
  115. return new(big.Int).SetInt64(int64(int32(rand.Uint32())))
  116. }
  117. func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  118. ki := &kexInitMsg{}
  119. randomBytes(ki.Cookie[:], rand)
  120. ki.KexAlgos = randomNameList(rand)
  121. ki.ServerHostKeyAlgos = randomNameList(rand)
  122. ki.CiphersClientServer = randomNameList(rand)
  123. ki.CiphersServerClient = randomNameList(rand)
  124. ki.MACsClientServer = randomNameList(rand)
  125. ki.MACsServerClient = randomNameList(rand)
  126. ki.CompressionClientServer = randomNameList(rand)
  127. ki.CompressionServerClient = randomNameList(rand)
  128. ki.LanguagesClientServer = randomNameList(rand)
  129. ki.LanguagesServerClient = randomNameList(rand)
  130. if rand.Int31()&1 == 1 {
  131. ki.FirstKexFollows = true
  132. }
  133. return reflect.ValueOf(ki)
  134. }
  135. func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  136. dhi := &kexDHInitMsg{}
  137. dhi.X = randomInt(rand)
  138. return reflect.ValueOf(dhi)
  139. }
  140. // TODO(dfc) maybe this can be removed in the future if testing/quick can handle
  141. // derived basic types.
  142. func (RejectionReason) Generate(rand *rand.Rand, size int) reflect.Value {
  143. m := RejectionReason(Prohibited)
  144. return reflect.ValueOf(m)
  145. }
  146. var (
  147. _kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
  148. _kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
  149. _kexInit = marshal(msgKexInit, _kexInitMsg)
  150. _kexDHInit = marshal(msgKexDHInit, _kexDHInitMsg)
  151. )
  152. func BenchmarkMarshalKexInitMsg(b *testing.B) {
  153. for i := 0; i < b.N; i++ {
  154. marshal(msgKexInit, _kexInitMsg)
  155. }
  156. }
  157. func BenchmarkUnmarshalKexInitMsg(b *testing.B) {
  158. m := new(kexInitMsg)
  159. for i := 0; i < b.N; i++ {
  160. unmarshal(m, _kexInit, msgKexInit)
  161. }
  162. }
  163. func BenchmarkMarshalKexDHInitMsg(b *testing.B) {
  164. for i := 0; i < b.N; i++ {
  165. marshal(msgKexDHInit, _kexDHInitMsg)
  166. }
  167. }
  168. func BenchmarkUnmarshalKexDHInitMsg(b *testing.B) {
  169. m := new(kexDHInitMsg)
  170. for i := 0; i < b.N; i++ {
  171. unmarshal(m, _kexDHInit, msgKexDHInit)
  172. }
  173. }