github.com/deis/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/Godeps/_workspace/src/golang.org/x/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 "bytes" 9 "math/big" 10 "math/rand" 11 "reflect" 12 "testing" 13 "testing/quick" 14 ) 15 16 var intLengthTests = []struct { 17 val, length int 18 }{ 19 {0, 4 + 0}, 20 {1, 4 + 1}, 21 {127, 4 + 1}, 22 {128, 4 + 2}, 23 {-1, 4 + 1}, 24 } 25 26 func TestIntLength(t *testing.T) { 27 for _, test := range intLengthTests { 28 v := new(big.Int).SetInt64(int64(test.val)) 29 length := intLength(v) 30 if length != test.length { 31 t.Errorf("For %d, got length %d but expected %d", test.val, length, test.length) 32 } 33 } 34 } 35 36 type msgAllTypes struct { 37 Bool bool `sshtype:"21"` 38 Array [16]byte 39 Uint64 uint64 40 Uint32 uint32 41 Uint8 uint8 42 String string 43 Strings []string 44 Bytes []byte 45 Int *big.Int 46 Rest []byte `ssh:"rest"` 47 } 48 49 func (t *msgAllTypes) Generate(rand *rand.Rand, size int) reflect.Value { 50 m := &msgAllTypes{} 51 m.Bool = rand.Intn(2) == 1 52 randomBytes(m.Array[:], rand) 53 m.Uint64 = uint64(rand.Int63n(1<<63 - 1)) 54 m.Uint32 = uint32(rand.Intn((1 << 31) - 1)) 55 m.Uint8 = uint8(rand.Intn(1 << 8)) 56 m.String = string(m.Array[:]) 57 m.Strings = randomNameList(rand) 58 m.Bytes = m.Array[:] 59 m.Int = randomInt(rand) 60 m.Rest = m.Array[:] 61 return reflect.ValueOf(m) 62 } 63 64 func TestMarshalUnmarshal(t *testing.T) { 65 rand := rand.New(rand.NewSource(0)) 66 iface := &msgAllTypes{} 67 ty := reflect.ValueOf(iface).Type() 68 69 n := 100 70 if testing.Short() { 71 n = 5 72 } 73 for j := 0; j < n; j++ { 74 v, ok := quick.Value(ty, rand) 75 if !ok { 76 t.Errorf("failed to create value") 77 break 78 } 79 80 m1 := v.Elem().Interface() 81 m2 := iface 82 83 marshaled := Marshal(m1) 84 if err := Unmarshal(marshaled, m2); err != nil { 85 t.Errorf("Unmarshal %#v: %s", m1, err) 86 break 87 } 88 89 if !reflect.DeepEqual(v.Interface(), m2) { 90 t.Errorf("got: %#v\nwant:%#v\n%x", m2, m1, marshaled) 91 break 92 } 93 } 94 } 95 96 func TestUnmarshalEmptyPacket(t *testing.T) { 97 var b []byte 98 var m channelRequestSuccessMsg 99 if err := Unmarshal(b, &m); err == nil { 100 t.Fatalf("unmarshal of empty slice succeeded") 101 } 102 } 103 104 func TestUnmarshalUnexpectedPacket(t *testing.T) { 105 type S struct { 106 I uint32 `sshtype:"43"` 107 S string 108 B bool 109 } 110 111 s := S{11, "hello", true} 112 packet := Marshal(s) 113 packet[0] = 42 114 roundtrip := S{} 115 err := Unmarshal(packet, &roundtrip) 116 if err == nil { 117 t.Fatal("expected error, not nil") 118 } 119 } 120 121 func TestMarshalPtr(t *testing.T) { 122 s := struct { 123 S string 124 }{"hello"} 125 126 m1 := Marshal(s) 127 m2 := Marshal(&s) 128 if !bytes.Equal(m1, m2) { 129 t.Errorf("got %q, want %q for marshaled pointer", m2, m1) 130 } 131 } 132 133 func TestBareMarshalUnmarshal(t *testing.T) { 134 type S struct { 135 I uint32 136 S string 137 B bool 138 } 139 140 s := S{42, "hello", true} 141 packet := Marshal(s) 142 roundtrip := S{} 143 Unmarshal(packet, &roundtrip) 144 145 if !reflect.DeepEqual(s, roundtrip) { 146 t.Errorf("got %#v, want %#v", roundtrip, s) 147 } 148 } 149 150 func TestBareMarshal(t *testing.T) { 151 type S2 struct { 152 I uint32 153 } 154 s := S2{42} 155 packet := Marshal(s) 156 i, rest, ok := parseUint32(packet) 157 if len(rest) > 0 || !ok { 158 t.Errorf("parseInt(%q): parse error", packet) 159 } 160 if i != s.I { 161 t.Errorf("got %d, want %d", i, s.I) 162 } 163 } 164 165 func randomBytes(out []byte, rand *rand.Rand) { 166 for i := 0; i < len(out); i++ { 167 out[i] = byte(rand.Int31()) 168 } 169 } 170 171 func randomNameList(rand *rand.Rand) []string { 172 ret := make([]string, rand.Int31()&15) 173 for i := range ret { 174 s := make([]byte, 1+(rand.Int31()&15)) 175 for j := range s { 176 s[j] = 'a' + uint8(rand.Int31()&15) 177 } 178 ret[i] = string(s) 179 } 180 return ret 181 } 182 183 func randomInt(rand *rand.Rand) *big.Int { 184 return new(big.Int).SetInt64(int64(int32(rand.Uint32()))) 185 } 186 187 func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 188 ki := &kexInitMsg{} 189 randomBytes(ki.Cookie[:], rand) 190 ki.KexAlgos = randomNameList(rand) 191 ki.ServerHostKeyAlgos = randomNameList(rand) 192 ki.CiphersClientServer = randomNameList(rand) 193 ki.CiphersServerClient = randomNameList(rand) 194 ki.MACsClientServer = randomNameList(rand) 195 ki.MACsServerClient = randomNameList(rand) 196 ki.CompressionClientServer = randomNameList(rand) 197 ki.CompressionServerClient = randomNameList(rand) 198 ki.LanguagesClientServer = randomNameList(rand) 199 ki.LanguagesServerClient = randomNameList(rand) 200 if rand.Int31()&1 == 1 { 201 ki.FirstKexFollows = true 202 } 203 return reflect.ValueOf(ki) 204 } 205 206 func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 207 dhi := &kexDHInitMsg{} 208 dhi.X = randomInt(rand) 209 return reflect.ValueOf(dhi) 210 } 211 212 var ( 213 _kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface() 214 _kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface() 215 216 _kexInit = Marshal(_kexInitMsg) 217 _kexDHInit = Marshal(_kexDHInitMsg) 218 ) 219 220 func BenchmarkMarshalKexInitMsg(b *testing.B) { 221 for i := 0; i < b.N; i++ { 222 Marshal(_kexInitMsg) 223 } 224 } 225 226 func BenchmarkUnmarshalKexInitMsg(b *testing.B) { 227 m := new(kexInitMsg) 228 for i := 0; i < b.N; i++ { 229 Unmarshal(_kexInit, m) 230 } 231 } 232 233 func BenchmarkMarshalKexDHInitMsg(b *testing.B) { 234 for i := 0; i < b.N; i++ { 235 Marshal(_kexDHInitMsg) 236 } 237 } 238 239 func BenchmarkUnmarshalKexDHInitMsg(b *testing.B) { 240 m := new(kexDHInitMsg) 241 for i := 0; i < b.N; i++ { 242 Unmarshal(_kexDHInit, m) 243 } 244 }