messages_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. &channelRequestMsg{},
  39. &channelRequestSuccessMsg{},
  40. }
  41. func TestMarshalUnmarshal(t *testing.T) {
  42. rand := rand.New(rand.NewSource(0))
  43. for i, iface := range messageTypes {
  44. ty := reflect.ValueOf(iface).Type()
  45. n := 100
  46. if testing.Short() {
  47. n = 5
  48. }
  49. for j := 0; j < n; j++ {
  50. v, ok := quick.Value(ty, rand)
  51. if !ok {
  52. t.Errorf("#%d: failed to create value", i)
  53. break
  54. }
  55. m1 := v.Elem().Interface()
  56. m2 := iface
  57. marshaled := marshal(msgIgnore, m1)
  58. if err := unmarshal(m2, marshaled, msgIgnore); err != nil {
  59. t.Errorf("#%d failed to unmarshal %#v: %s", i, m1, err)
  60. break
  61. }
  62. if !reflect.DeepEqual(v.Interface(), m2) {
  63. t.Errorf("#%d\ngot: %#v\nwant:%#v\n%x", i, m2, m1, marshaled)
  64. break
  65. }
  66. }
  67. }
  68. }
  69. func randomBytes(out []byte, rand *rand.Rand) {
  70. for i := 0; i < len(out); i++ {
  71. out[i] = byte(rand.Int31())
  72. }
  73. }
  74. func randomNameList(rand *rand.Rand) []string {
  75. ret := make([]string, rand.Int31()&15)
  76. for i := range ret {
  77. s := make([]byte, 1+(rand.Int31()&15))
  78. for j := range s {
  79. s[j] = 'a' + uint8(rand.Int31()&15)
  80. }
  81. ret[i] = string(s)
  82. }
  83. return ret
  84. }
  85. func randomInt(rand *rand.Rand) *big.Int {
  86. return new(big.Int).SetInt64(int64(int32(rand.Uint32())))
  87. }
  88. func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  89. ki := &kexInitMsg{}
  90. randomBytes(ki.Cookie[:], rand)
  91. ki.KexAlgos = randomNameList(rand)
  92. ki.ServerHostKeyAlgos = randomNameList(rand)
  93. ki.CiphersClientServer = randomNameList(rand)
  94. ki.CiphersServerClient = randomNameList(rand)
  95. ki.MACsClientServer = randomNameList(rand)
  96. ki.MACsServerClient = randomNameList(rand)
  97. ki.CompressionClientServer = randomNameList(rand)
  98. ki.CompressionServerClient = randomNameList(rand)
  99. ki.LanguagesClientServer = randomNameList(rand)
  100. ki.LanguagesServerClient = randomNameList(rand)
  101. if rand.Int31()&1 == 1 {
  102. ki.FirstKexFollows = true
  103. }
  104. return reflect.ValueOf(ki)
  105. }
  106. func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
  107. dhi := &kexDHInitMsg{}
  108. dhi.X = randomInt(rand)
  109. return reflect.ValueOf(dhi)
  110. }