github.com/glycerine/xcryptossh@v7.0.4+incompatible/transport_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  	"context"
    10  	"crypto/rand"
    11  	"encoding/binary"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestReadVersion(t *testing.T) {
    17  	defer xtestend(xtestbegin(t))
    18  	longversion := strings.Repeat("SSH-2.0-bla", 50)[:253]
    19  	cases := map[string]string{
    20  		"SSH-2.0-bla\r\n":    "SSH-2.0-bla",
    21  		"SSH-2.0-bla\n":      "SSH-2.0-bla",
    22  		longversion + "\r\n": longversion,
    23  	}
    24  
    25  	for in, want := range cases {
    26  		result, err := readVersion(bytes.NewBufferString(in))
    27  		if err != nil {
    28  			t.Errorf("readVersion(%q): %s", in, err)
    29  		}
    30  		got := string(result)
    31  		if got != want {
    32  			t.Errorf("got %q, want %q", got, want)
    33  		}
    34  	}
    35  }
    36  
    37  func TestReadVersionError(t *testing.T) {
    38  	defer xtestend(xtestbegin(t))
    39  
    40  	longversion := strings.Repeat("SSH-2.0-bla", 50)[:253]
    41  	cases := []string{
    42  		longversion + "too-long\r\n",
    43  	}
    44  	for _, in := range cases {
    45  		if _, err := readVersion(bytes.NewBufferString(in)); err == nil {
    46  			t.Errorf("readVersion(%q) should have failed", in)
    47  		}
    48  	}
    49  }
    50  
    51  func TestExchangeVersionsBasic(t *testing.T) {
    52  	defer xtestend(xtestbegin(t))
    53  
    54  	v := "SSH-2.0-bla"
    55  	buf := bytes.NewBufferString(v + "\r\n")
    56  	them, err := exchangeVersions(buf, []byte("xyz"))
    57  	if err != nil {
    58  		t.Errorf("exchangeVersions: %v", err)
    59  	}
    60  
    61  	if want := "SSH-2.0-bla"; string(them) != want {
    62  		t.Errorf("got %q want %q for our version", them, want)
    63  	}
    64  }
    65  
    66  func TestExchangeVersions(t *testing.T) {
    67  	defer xtestend(xtestbegin(t))
    68  
    69  	cases := []string{
    70  		"not\x000allowed",
    71  		"not allowed\n",
    72  	}
    73  	for _, c := range cases {
    74  		buf := bytes.NewBufferString("SSH-2.0-bla\r\n")
    75  		if _, err := exchangeVersions(buf, []byte(c)); err == nil {
    76  			t.Errorf("exchangeVersions(%q): should have failed", c)
    77  		}
    78  	}
    79  }
    80  
    81  type closerBuffer struct {
    82  	bytes.Buffer
    83  }
    84  
    85  func (b *closerBuffer) Close() error {
    86  	return nil
    87  }
    88  
    89  func TestTransportMaxPacketWrite(t *testing.T) {
    90  	defer xtestend(xtestbegin(t))
    91  
    92  	buf := &closerBuffer{}
    93  	tr := newTransport(buf, rand.Reader, true, nil)
    94  	huge := make([]byte, maxPacket+1)
    95  	err := tr.writePacket(huge)
    96  	if err == nil {
    97  		t.Errorf("transport accepted write for a huge packet.")
    98  	}
    99  }
   100  
   101  func TestTransportMaxPacketReader(t *testing.T) {
   102  	defer xtestend(xtestbegin(t))
   103  
   104  	var header [5]byte
   105  	huge := make([]byte, maxPacket+128)
   106  	binary.BigEndian.PutUint32(header[0:], uint32(len(huge)))
   107  	// padding.
   108  	header[4] = 0
   109  
   110  	buf := &closerBuffer{}
   111  	buf.Write(header[:])
   112  	buf.Write(huge)
   113  
   114  	tr := newTransport(buf, rand.Reader, true, nil)
   115  	ctx := context.Background()
   116  	_, err := tr.readPacket(ctx)
   117  	if err == nil {
   118  		t.Errorf("transport succeeded reading huge packet.")
   119  	} else if !strings.Contains(err.Error(), "large") {
   120  		t.Errorf("got %q, should mention %q", err.Error(), "large")
   121  	}
   122  }