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