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") } }