github.com/glycerine/xcryptossh@v7.0.4+incompatible/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 defer xtestend(xtestbegin(t)) 28 for _, test := range intLengthTests { 29 v := new(big.Int).SetInt64(int64(test.val)) 30 length := intLength(v) 31 if length != test.length { 32 t.Errorf("For %d, got length %d but expected %d", test.val, length, test.length) 33 } 34 } 35 } 36 37 type msgAllTypes struct { 38 Bool bool `sshtype:"21"` 39 Array [16]byte 40 Uint64 uint64 41 Uint32 uint32 42 Uint8 uint8 43 String string 44 Strings []string 45 Bytes []byte 46 Int *big.Int 47 Rest []byte `ssh:"rest"` 48 } 49 50 func (t *msgAllTypes) Generate(rand *rand.Rand, size int) reflect.Value { 51 m := &msgAllTypes{} 52 m.Bool = rand.Intn(2) == 1 53 randomBytes(m.Array[:], rand) 54 m.Uint64 = uint64(rand.Int63n(1<<63 - 1)) 55 m.Uint32 = uint32(rand.Intn((1 << 31) - 1)) 56 m.Uint8 = uint8(rand.Intn(1 << 8)) 57 m.String = string(m.Array[:]) 58 m.Strings = randomNameList(rand) 59 m.Bytes = m.Array[:] 60 m.Int = randomInt(rand) 61 m.Rest = m.Array[:] 62 return reflect.ValueOf(m) 63 } 64 65 func TestMarshalUnmarshal(t *testing.T) { 66 defer xtestend(xtestbegin(t)) 67 rand := rand.New(rand.NewSource(0)) 68 iface := &msgAllTypes{} 69 ty := reflect.ValueOf(iface).Type() 70 71 n := 100 72 if testing.Short() { 73 n = 5 74 } 75 for j := 0; j < n; j++ { 76 v, ok := quick.Value(ty, rand) 77 if !ok { 78 t.Errorf("failed to create value") 79 break 80 } 81 82 m1 := v.Elem().Interface() 83 m2 := iface 84 85 marshaled := Marshal(m1) 86 if err := Unmarshal(marshaled, m2); err != nil { 87 t.Errorf("Unmarshal %#v: %s", m1, err) 88 break 89 } 90 91 if !reflect.DeepEqual(v.Interface(), m2) { 92 t.Errorf("got: %#v\nwant:%#v\n%x", m2, m1, marshaled) 93 break 94 } 95 } 96 } 97 98 func TestUnmarshalEmptyPacket(t *testing.T) { 99 defer xtestend(xtestbegin(t)) 100 var b []byte 101 var m channelRequestSuccessMsg 102 if err := Unmarshal(b, &m); err == nil { 103 t.Fatalf("unmarshal of empty slice succeeded") 104 } 105 } 106 107 func TestUnmarshalUnexpectedPacket(t *testing.T) { 108 defer xtestend(xtestbegin(t)) 109 type S struct { 110 I uint32 `sshtype:"43"` 111 S string 112 B bool 113 } 114 115 s := S{11, "hello", true} 116 packet := Marshal(s) 117 packet[0] = 42 118 roundtrip := S{} 119 err := Unmarshal(packet, &roundtrip) 120 if err == nil { 121 t.Fatal("expected error, not nil") 122 } 123 } 124 125 func TestMarshalPtr(t *testing.T) { 126 defer xtestend(xtestbegin(t)) 127 s := struct { 128 S string 129 }{"hello"} 130 131 m1 := Marshal(s) 132 m2 := Marshal(&s) 133 if !bytes.Equal(m1, m2) { 134 t.Errorf("got %q, want %q for marshaled pointer", m2, m1) 135 } 136 } 137 138 func TestBareMarshalUnmarshal(t *testing.T) { 139 defer xtestend(xtestbegin(t)) 140 type S struct { 141 I uint32 142 S string 143 B bool 144 } 145 146 s := S{42, "hello", true} 147 packet := Marshal(s) 148 roundtrip := S{} 149 Unmarshal(packet, &roundtrip) 150 151 if !reflect.DeepEqual(s, roundtrip) { 152 t.Errorf("got %#v, want %#v", roundtrip, s) 153 } 154 } 155 156 func TestBareMarshal(t *testing.T) { 157 defer xtestend(xtestbegin(t)) 158 type S2 struct { 159 I uint32 160 } 161 s := S2{42} 162 packet := Marshal(s) 163 i, rest, ok := parseUint32(packet) 164 if len(rest) > 0 || !ok { 165 t.Errorf("parseInt(%q): parse error", packet) 166 } 167 if i != s.I { 168 t.Errorf("got %d, want %d", i, s.I) 169 } 170 } 171 172 func TestUnmarshalShortKexInitPacket(t *testing.T) { 173 defer xtestend(xtestbegin(t)) 174 // This used to panic. 175 // Issue 11348 176 packet := []byte{0x14, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xff, 0xff, 0xff} 177 kim := &kexInitMsg{} 178 if err := Unmarshal(packet, kim); err == nil { 179 t.Error("truncated packet unmarshaled without error") 180 } 181 } 182 183 func TestMarshalMultiTag(t *testing.T) { 184 defer xtestend(xtestbegin(t)) 185 var res struct { 186 A uint32 `sshtype:"1|2"` 187 } 188 189 good1 := struct { 190 A uint32 `sshtype:"1"` 191 }{ 192 1, 193 } 194 good2 := struct { 195 A uint32 `sshtype:"2"` 196 }{ 197 1, 198 } 199 200 if e := Unmarshal(Marshal(good1), &res); e != nil { 201 t.Errorf("error unmarshaling multipart tag: %v", e) 202 } 203 204 if e := Unmarshal(Marshal(good2), &res); e != nil { 205 t.Errorf("error unmarshaling multipart tag: %v", e) 206 } 207 208 bad1 := struct { 209 A uint32 `sshtype:"3"` 210 }{ 211 1, 212 } 213 if e := Unmarshal(Marshal(bad1), &res); e == nil { 214 t.Errorf("bad struct unmarshaled without error") 215 } 216 } 217 218 func randomBytes(out []byte, rand *rand.Rand) { 219 for i := 0; i < len(out); i++ { 220 out[i] = byte(rand.Int31()) 221 } 222 } 223 224 func randomNameList(rand *rand.Rand) []string { 225 ret := make([]string, rand.Int31()&15) 226 for i := range ret { 227 s := make([]byte, 1+(rand.Int31()&15)) 228 for j := range s { 229 s[j] = 'a' + uint8(rand.Int31()&15) 230 } 231 ret[i] = string(s) 232 } 233 return ret 234 } 235 236 func randomInt(rand *rand.Rand) *big.Int { 237 return new(big.Int).SetInt64(int64(int32(rand.Uint32()))) 238 } 239 240 func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 241 ki := &kexInitMsg{} 242 randomBytes(ki.Cookie[:], rand) 243 ki.KexAlgos = randomNameList(rand) 244 ki.ServerHostKeyAlgos = randomNameList(rand) 245 ki.CiphersClientServer = randomNameList(rand) 246 ki.CiphersServerClient = randomNameList(rand) 247 ki.MACsClientServer = randomNameList(rand) 248 ki.MACsServerClient = randomNameList(rand) 249 ki.CompressionClientServer = randomNameList(rand) 250 ki.CompressionServerClient = randomNameList(rand) 251 ki.LanguagesClientServer = randomNameList(rand) 252 ki.LanguagesServerClient = randomNameList(rand) 253 if rand.Int31()&1 == 1 { 254 ki.FirstKexFollows = true 255 } 256 return reflect.ValueOf(ki) 257 } 258 259 func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value { 260 dhi := &kexDHInitMsg{} 261 dhi.X = randomInt(rand) 262 return reflect.ValueOf(dhi) 263 } 264 265 var ( 266 _kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface() 267 _kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface() 268 269 _kexInit = Marshal(_kexInitMsg) 270 _kexDHInit = Marshal(_kexDHInitMsg) 271 ) 272 273 func BenchmarkMarshalKexInitMsg(b *testing.B) { 274 for i := 0; i < b.N; i++ { 275 Marshal(_kexInitMsg) 276 } 277 } 278 279 func BenchmarkUnmarshalKexInitMsg(b *testing.B) { 280 m := new(kexInitMsg) 281 for i := 0; i < b.N; i++ { 282 Unmarshal(_kexInit, m) 283 } 284 } 285 286 func BenchmarkMarshalKexDHInitMsg(b *testing.B) { 287 for i := 0; i < b.N; i++ { 288 Marshal(_kexDHInitMsg) 289 } 290 } 291 292 func BenchmarkUnmarshalKexDHInitMsg(b *testing.B) { 293 m := new(kexDHInitMsg) 294 for i := 0; i < b.N; i++ { 295 Unmarshal(_kexDHInit, m) 296 } 297 }