You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
723 B
30 lines
723 B
|
2 weeks ago
|
package codec
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// The Encoder wipes every buffer it outgrows: an encoding that holds a
|
||
|
|
// secret leaves no stale copy behind.
|
||
|
|
func TestEncoderWipesOutgrownBuffers(t *testing.T) {
|
||
|
|
secret := bytes.Repeat([]byte{0xab}, 32)
|
||
|
|
var e Encoder
|
||
|
|
e.Bstr(secret)
|
||
|
|
old := e.buf[:cap(e.buf)]
|
||
|
|
e.Bstr(make([]byte, 2*cap(e.buf)))
|
||
|
|
if bytes.Contains(old, secret[:8]) {
|
||
|
|
t.Fatalf("outgrown buffer not wiped: %x", old)
|
||
|
|
}
|
||
|
|
b, err := e.Out()
|
||
|
|
if err != nil || !bytes.Contains(b, secret) {
|
||
|
|
t.Fatalf("output lost the data: %v", err)
|
||
|
|
}
|
||
|
|
// A buffer with room is not replaced.
|
||
|
|
e = Encoder{buf: make([]byte, 0, 64)}
|
||
|
|
e.Text("fits")
|
||
|
|
if cap(e.buf) != 64 {
|
||
|
|
t.Fatal("buffer replaced although it had room")
|
||
|
|
}
|
||
|
|
}
|