transport_test.go 2.5 KB

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