github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/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 func TestBufferReadwrite(t *testing.T) { 15 b := newBuffer() 16 b.write(alphabet[:10]) 17 r, _ := b.Read(make([]byte, 10)) 18 if r != 10 { 19 t.Fatalf("Expected written == read == 10, written: 10, read %d", r) 20 } 21 22 b = newBuffer() 23 b.write(alphabet[:5]) 24 r, _ = b.Read(make([]byte, 10)) 25 if r != 5 { 26 t.Fatalf("Expected written == read == 5, written: 5, read %d", r) 27 } 28 29 b = newBuffer() 30 b.write(alphabet[:10]) 31 r, _ = b.Read(make([]byte, 5)) 32 if r != 5 { 33 t.Fatalf("Expected written == 10, read == 5, written: 10, read %d", r) 34 } 35 36 b = newBuffer() 37 b.write(alphabet[:5]) 38 b.write(alphabet[5:15]) 39 r, _ = b.Read(make([]byte, 10)) 40 r2, _ := b.Read(make([]byte, 10)) 41 if r != 10 || r2 != 5 || 15 != r+r2 { 42 t.Fatal("Expected written == read == 15") 43 } 44 } 45 46 func TestBufferClose(t *testing.T) { 47 b := newBuffer() 48 b.write(alphabet[:10]) 49 b.eof() 50 _, err := b.Read(make([]byte, 5)) 51 if err != nil { 52 t.Fatal("expected read of 5 to not return EOF") 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 if r != 5 || r2 != 5 || err != nil || err2 != nil { 60 t.Fatal("expected reads of 5 and 5") 61 } 62 63 b = newBuffer() 64 b.write(alphabet[:10]) 65 b.eof() 66 r, err = b.Read(make([]byte, 5)) 67 r2, err2 = b.Read(make([]byte, 10)) 68 r3, err3 := b.Read(make([]byte, 10)) 69 if r != 5 || r2 != 5 || r3 != 0 || err != nil || err2 != nil || err3 != io.EOF { 70 t.Fatal("expected reads of 5 and 5 and 0, with EOF") 71 } 72 73 b = newBuffer() 74 b.write(make([]byte, 5)) 75 b.write(make([]byte, 10)) 76 b.eof() 77 r, err = b.Read(make([]byte, 9)) 78 r2, err2 = b.Read(make([]byte, 3)) 79 r3, err3 = b.Read(make([]byte, 3)) 80 r4, err4 := b.Read(make([]byte, 10)) 81 if err != nil || err2 != nil || err3 != nil || err4 != io.EOF { 82 t.Fatalf("Expected EOF on forth read only, err=%v, err2=%v, err3=%v, err4=%v", err, err2, err3, err4) 83 } 84 if r != 9 || r2 != 3 || r3 != 3 || r4 != 0 { 85 t.Fatal("Expected written == read == 15", r, r2, r3, r4) 86 } 87 }