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.
16 var intLengthTests = []struct {
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)
36 type msgAllTypes struct {
37 Bool bool `sshtype:"21"`
46 Rest []byte `ssh:"rest"`
49 func (t *msgAllTypes) Generate(rand *rand.Rand, size int) reflect.Value {
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)
59 m.Int = randomInt(rand)
61 return reflect.ValueOf(m)
64 func TestMarshalUnmarshal(t *testing.T) {
65 rand := rand.New(rand.NewSource(0))
66 iface := &msgAllTypes{}
67 ty := reflect.ValueOf(iface).Type()
73 for j := 0; j < n; j++ {
74 v, ok := quick.Value(ty, rand)
76 t.Errorf("failed to create value")
80 m1 := v.Elem().Interface()
83 marshaled := Marshal(m1)
84 if err := Unmarshal(marshaled, m2); err != nil {
85 t.Errorf("Unmarshal %#v: %s", m1, err)
89 if !reflect.DeepEqual(v.Interface(), m2) {
90 t.Errorf("got: %#v\nwant:%#v\n%x", m2, m1, marshaled)
96 func TestUnmarshalEmptyPacket(t *testing.T) {
98 var m channelRequestSuccessMsg
99 if err := Unmarshal(b, &m); err == nil {
100 t.Fatalf("unmarshal of empty slice succeeded")
104 func TestUnmarshalUnexpectedPacket(t *testing.T) {
106 I uint32 `sshtype:"43"`
111 s := S{11, "hello", true}
115 err := Unmarshal(packet, &roundtrip)
117 t.Fatal("expected error, not nil")
121 func TestMarshalPtr(t *testing.T) {
128 if !bytes.Equal(m1, m2) {
129 t.Errorf("got %q, want %q for marshaled pointer", m2, m1)
133 func TestBareMarshalUnmarshal(t *testing.T) {
140 s := S{42, "hello", true}
143 Unmarshal(packet, &roundtrip)
145 if !reflect.DeepEqual(s, roundtrip) {
146 t.Errorf("got %#v, want %#v", roundtrip, s)
150 func TestBareMarshal(t *testing.T) {
156 i, rest, ok := parseUint32(packet)
157 if len(rest) > 0 || !ok {
158 t.Errorf("parseInt(%q): parse error", packet)
161 t.Errorf("got %d, want %d", i, s.I)
165 func TestUnmarshalShortKexInitPacket(t *testing.T) {
166 // This used to panic.
168 packet := []byte{0x14, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xff, 0xff, 0xff}
170 if err := Unmarshal(packet, kim); err == nil {
171 t.Error("truncated packet unmarshaled without error")
175 func TestMarshalMultiTag(t *testing.T) {
177 A uint32 `sshtype:"1|2"`
181 A uint32 `sshtype:"1"`
186 A uint32 `sshtype:"2"`
191 if e := Unmarshal(Marshal(good1), &res); e != nil {
192 t.Errorf("error unmarshaling multipart tag: %v", e)
195 if e := Unmarshal(Marshal(good2), &res); e != nil {
196 t.Errorf("error unmarshaling multipart tag: %v", e)
200 A uint32 `sshtype:"3"`
204 if e := Unmarshal(Marshal(bad1), &res); e == nil {
205 t.Errorf("bad struct unmarshaled without error")
209 func randomBytes(out []byte, rand *rand.Rand) {
210 for i := 0; i < len(out); i++ {
211 out[i] = byte(rand.Int31())
215 func randomNameList(rand *rand.Rand) []string {
216 ret := make([]string, rand.Int31()&15)
218 s := make([]byte, 1+(rand.Int31()&15))
220 s[j] = 'a' + uint8(rand.Int31()&15)
227 func randomInt(rand *rand.Rand) *big.Int {
228 return new(big.Int).SetInt64(int64(int32(rand.Uint32())))
231 func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
233 randomBytes(ki.Cookie[:], rand)
234 ki.KexAlgos = randomNameList(rand)
235 ki.ServerHostKeyAlgos = randomNameList(rand)
236 ki.CiphersClientServer = randomNameList(rand)
237 ki.CiphersServerClient = randomNameList(rand)
238 ki.MACsClientServer = randomNameList(rand)
239 ki.MACsServerClient = randomNameList(rand)
240 ki.CompressionClientServer = randomNameList(rand)
241 ki.CompressionServerClient = randomNameList(rand)
242 ki.LanguagesClientServer = randomNameList(rand)
243 ki.LanguagesServerClient = randomNameList(rand)
244 if rand.Int31()&1 == 1 {
245 ki.FirstKexFollows = true
247 return reflect.ValueOf(ki)
250 func (*kexDHInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
251 dhi := &kexDHInitMsg{}
252 dhi.X = randomInt(rand)
253 return reflect.ValueOf(dhi)
257 _kexInitMsg = new(kexInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
258 _kexDHInitMsg = new(kexDHInitMsg).Generate(rand.New(rand.NewSource(0)), 10).Elem().Interface()
260 _kexInit = Marshal(_kexInitMsg)
261 _kexDHInit = Marshal(_kexDHInitMsg)
264 func BenchmarkMarshalKexInitMsg(b *testing.B) {
265 for i := 0; i < b.N; i++ {
270 func BenchmarkUnmarshalKexInitMsg(b *testing.B) {
272 for i := 0; i < b.N; i++ {
273 Unmarshal(_kexInit, m)
277 func BenchmarkMarshalKexDHInitMsg(b *testing.B) {
278 for i := 0; i < b.N; i++ {
279 Marshal(_kexDHInitMsg)
283 func BenchmarkUnmarshalKexDHInitMsg(b *testing.B) {
284 m := new(kexDHInitMsg)
285 for i := 0; i < b.N; i++ {
286 Unmarshal(_kexDHInit, m)