buffer_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "io"
  7. "testing"
  8. )
  9. var alphabet = []byte("abcdefghijklmnopqrstuvwxyz")
  10. func TestBufferReadwrite(t *testing.T) {
  11. b := newBuffer()
  12. b.write(alphabet[:10])
  13. r, _ := b.Read(make([]byte, 10))
  14. if r != 10 {
  15. t.Fatalf("Expected written == read == 10, written: 10, read %d", r)
  16. }
  17. b = newBuffer()
  18. b.write(alphabet[:5])
  19. r, _ = b.Read(make([]byte, 10))
  20. if r != 5 {
  21. t.Fatalf("Expected written == read == 5, written: 5, read %d", r)
  22. }
  23. b = newBuffer()
  24. b.write(alphabet[:10])
  25. r, _ = b.Read(make([]byte, 5))
  26. if r != 5 {
  27. t.Fatalf("Expected written == 10, read == 5, written: 10, read %d", r)
  28. }
  29. b = newBuffer()
  30. b.write(alphabet[:5])
  31. b.write(alphabet[5:15])
  32. r, _ = b.Read(make([]byte, 10))
  33. r2, _ := b.Read(make([]byte, 10))
  34. if r != 10 || r2 != 5 || 15 != r+r2 {
  35. t.Fatal("Expected written == read == 15")
  36. }
  37. }
  38. func TestBufferClose(t *testing.T) {
  39. b := newBuffer()
  40. b.write(alphabet[:10])
  41. b.eof()
  42. _, err := b.Read(make([]byte, 5))
  43. if err != nil {
  44. t.Fatal("expected read of 5 to not return EOF")
  45. }
  46. b = newBuffer()
  47. b.write(alphabet[:10])
  48. b.eof()
  49. r, err := b.Read(make([]byte, 5))
  50. r2, err2 := b.Read(make([]byte, 10))
  51. if r != 5 || r2 != 5 || err != nil || err2 != nil {
  52. t.Fatal("expected reads of 5 and 5")
  53. }
  54. b = newBuffer()
  55. b.write(alphabet[:10])
  56. b.eof()
  57. r, err = b.Read(make([]byte, 5))
  58. r2, err2 = b.Read(make([]byte, 10))
  59. r3, err3 := b.Read(make([]byte, 10))
  60. if r != 5 || r2 != 5 || r3 != 0 || err != nil || err2 != nil || err3 != io.EOF {
  61. t.Fatal("expected reads of 5 and 5 and 0, with EOF")
  62. }
  63. b = newBuffer()
  64. b.write(make([]byte, 5))
  65. b.write(make([]byte, 10))
  66. b.eof()
  67. r, err = b.Read(make([]byte, 9))
  68. r2, err2 = b.Read(make([]byte, 3))
  69. r3, err3 = b.Read(make([]byte, 3))
  70. r4, err4 := b.Read(make([]byte, 10))
  71. if err != nil || err2 != nil || err3 != nil || err4 != io.EOF {
  72. t.Fatalf("Expected EOF on forth read only, err=%v, err2=%v, err3=%v, err4=%v", err, err2, err3, err4)
  73. }
  74. if r != 9 || r2 != 3 || r3 != 3 || r4 != 0 {
  75. t.Fatal("Expected written == read == 15", r, r2, r3, r4)
  76. }
  77. }