github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/go.crypto/ssh/messages_test.go (about) 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 5 package ssh 6 7 import ( 8 "math/big" 9 "math/rand" 10 "reflect" 11 "testing" 12 "testing/quick" 13 ) 14 15 var intLengthTests = []struct { 16 val, length int 17 }{ 18 {0, 4 + 0}, 19 {1, 4 + 1}, 20 {127, 4 + 1}, 21 {128, 4 + 2}, 22 {-1, 4 + 1}, 23 } 24 25 func TestIntLength(t *testing.T) { 26 for _, test := range intLengthTests { 27 v := new(big.Int).SetInt64(int64(test.val)) 28 length := intLength(v) 29 if length != test.length { 30 t.Errorf("For %d, got length %d but expected %d", test.val, length, test.length) 31 } 32 } 33 } 34 35 var messageTypes = []interface{}{ 36 &kexInitMsg{}, 37 &kexDHInitMsg{}, 38 &serviceRequestMsg{}, 39 &serviceAcceptMsg{}, 40 &userAuthRequestMsg{}, 41 &channelOpenMsg{}, 42 &channelOpenConfirmMsg{}, 43 &channelRequestMsg{}, 44 &channelRequestSuccessMsg{}, 45 } 46 47 func TestMarshalUnmarshal(t *testing.T) { 48 rand := rand.New(rand.NewSource(0)) 49 for i, iface := range messageTypes { 50 ty := reflect.ValueOf(iface).Type() 51 52 n := 100 53 if testing.Short() { 54 n = 5 55 } 56 for j := 0; j < n; j++ { 57 v, ok := quick.Value(ty, rand) 58 if !ok { 59 t.Errorf("#%d: failed to create value", i) 60 break 61 } 62 63 m1 := v.Elem().Interface() 64 m2 := iface 65 66 marshaled := marshal(msgIgnore, m1) 67 if err := unmarshal(m2, marshaled, msgIgnore); err != nil { 68 t.Errorf("#%d failed to unmarshal %#v: %s", i, m1, err) 69 break 70 } 71 72 if !reflect.DeepEqual(v.Interface(), m2) { 73 t.Errorf("#%d\ngot: %#v\nwant:%#v\n%x", i, m2, m1, marshaled) 74 break 75 } 76 } 77 } 78 } 79 80 func randomBytes(out []byte, rand *rand.Rand) { 81 for i := 0; i < len(out); i++ { 82 out[i] = byte(rand.Int31()) 83 } 84 } 85 86 func randomNameList(rand *rand.Rand) []string { 87 ret := make([]string, rand.Int31()&15) 88 for i := range ret { 89 s := make([]byte, 1+(rand.Int31()&15)) 90 for j := range s { 91 s[j] = 'a' + uint8(rand.Int31()&15) 92 } 93 ret[i] = string(s) 94 } 95 return ret 96 } 97 98 func randomInt(rand *rand.Rand) *big.Int { 99 return new(big.Int).SetInt64(int64(int32(rand.Uint32()))) 100 } 101 102 func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 103 ki := &kexInitMsg{} 104 randomBytes(ki.Cookie[:], rand) 105 ki.KexAlgos = randomNameList(rand) 106 ki.ServerHostKeyAlgos = randomNameList(rand) 107 ki.CiphersClientServer = randomNameList(rand) 108 ki.CiphersServerClient = randomNameList(rand) 109 ki.MACsClientServer = randomNameList(rand) 110 ki.MACsServerClient = randomNameList(rand) 111 ki.CompressionClientServer = randomNameList(rand) 112 ki.CompressionServerClient = randomNameList(rand) 113 ki.LanguagesClientServer = randomNameList(rand) 114 ki.LanguagesServerClient = randomNameList(rand) 115 if rand.Int31()&1 == 1 { 116 ki.FirstKexFollows = true 117 } 118 return reflect.ValueOf(ki) 119 } 120 121 func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 122 dhi := &kexDHInitMsg{} 123 dhi.X = randomInt(rand) 124 return reflect.ValueOf(dhi) 125 }