2
0
mirror of https://github.com/soheilhy/cmux.git synced 2026-08-14 00:24:43 +08:00

buffer: do not retain passed-in memory

This is in violation of the io.Writer interface specification.
This commit is contained in:
Tamir Duberstein
2016-02-25 11:38:39 -05:00
parent 1a2fcbde3a
commit 48443df968
2 changed files with 40 additions and 10 deletions

View File

@@ -6,8 +6,31 @@ import (
"testing"
)
func TestWriteNoModify(t *testing.T) {
var b buffer
const origWriteByte = 0
const postWriteByte = 1
writeBytes := []byte{origWriteByte}
if _, err := b.Write(writeBytes); err != nil {
t.Fatal(err)
}
writeBytes[0] = postWriteByte
readBytes := make([]byte, 1)
if _, err := b.Read(readBytes); err != io.EOF {
t.Fatal(err)
}
if readBytes[0] != origWriteByte {
t.Fatalf("expected to read %x, but read %x; buffer retained passed-in slice", origWriteByte, postWriteByte)
}
}
const writeString = "deadbeef"
func TestBuffer(t *testing.T) {
writeBytes := []byte("deadbeef")
writeBytes := []byte(writeString)
const numWrites = 10
@@ -54,7 +77,7 @@ func TestBuffer(t *testing.T) {
}
func TestBufferOffset(t *testing.T) {
writeBytes := []byte("deadbeef")
writeBytes := []byte(writeString)
var b buffer
n, err := b.Write(writeBytes)