Commit e7b9860f authored by Aaron Jacobs's avatar Aaron Jacobs

buffer_test: expand the coverage of TestOutMessageGrow.

parent 02e3f789
...@@ -144,26 +144,44 @@ func TestOutMessageReset(t *testing.T) { ...@@ -144,26 +144,44 @@ func TestOutMessageReset(t *testing.T) {
func TestOutMessageGrow(t *testing.T) { func TestOutMessageGrow(t *testing.T) {
var om OutMessage var om OutMessage
om.Reset()
// Set up garbage where the payload will soon be.
const payloadSize = 1234
{
p := om.GrowNoZero(payloadSize)
if p == nil {
t.Fatal("GrowNoZero failed")
}
// Overwrite with garbage. err := fillWithGarbage(p, payloadSize)
err := fillWithGarbage(unsafe.Pointer(&om), int(unsafe.Sizeof(om))) if err != nil {
if err != nil { t.Fatalf("fillWithGarbage: %v", err)
t.Fatalf("fillWithGarbage: %v", err) }
om.ShrinkTo(OutMessageInitialSize)
} }
// Zero the header. // Call Grow.
om.Reset() if p := om.Grow(payloadSize); p == nil {
t.Fatal("Grow failed")
}
// Grow to the max size. This should zero the message. // Check the resulting length in two ways.
if p := om.Grow(MaxReadSize); p == nil { const wantLen = int(payloadSize + OutMessageInitialSize)
t.Fatal("Grow returned nil") if got, want := om.Len(), wantLen; got != want {
t.Errorf("om.Len() = %d, want %d", got)
} }
// Check that everything has been zeroed.
b := om.Bytes() b := om.Bytes()
for i, x := range b { if got, want := len(b), wantLen; got != want {
t.Fatalf("len(om.Len()) = %d, want %d", got)
}
// Check that the payload was zeroed.
for i, x := range b[OutMessageInitialSize:] {
if x != 0 { if x != 0 {
t.Fatalf("non-zero byte 0x%02x at offset %d", x, i) t.Fatalf("non-zero byte 0x%02x at payload offset %d", x, i)
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment