Commit 616e45ea authored by Matt T. Proud's avatar Matt T. Proud Committed by Russ Cox

encoding/pem: make TestFuzz testing/quick safe

This adapts pem.TestFuzz to sanitize the generated Block fields,
because the encoder and wireformat do not differentiate between nil
and empty slices and maps, while reflect.DeepEqual rightfully does.
In the commit mentioned below, we adapt quick.Value in
testing/quick to generate these value states, which had heretofore
been impossible with the standard library fuzz test facility.

This commit is a piecemeal extraction from ...

  https://go-review.googlesource.com/#/c/16470

..., which rsc requested to be separated from the nil slice and map
generations.

Change-Id: Iec751a2b0082af6e672a09dc9b7f4b4fb309e8a8
Reviewed-on: https://go-review.googlesource.com/17499Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 1be2ddda
......@@ -155,20 +155,28 @@ func TestFuzz(t *testing.T) {
}
var buf bytes.Buffer
err := Encode(&buf, &block)
decoded, rest := Decode(buf.Bytes())
switch {
case err != nil:
if err := Encode(&buf, &block); err != nil {
t.Errorf("Encode of %#v resulted in error: %s", &block, err)
case !reflect.DeepEqual(&block, decoded):
return false
}
decoded, rest := Decode(buf.Bytes())
if block.Headers == nil {
// Encoder supports nil Headers but decoder returns initialized.
block.Headers = make(map[string]string)
}
if block.Bytes == nil {
// Encoder supports nil Bytes but decoder returns initialized.
block.Bytes = make([]byte, 0)
}
if !reflect.DeepEqual(decoded, &block) {
t.Errorf("Encode of %#v decoded as %#v", &block, decoded)
case len(rest) != 0:
return false
}
if len(rest) != 0 {
t.Errorf("Encode of %#v decoded correctly, but with %x left over", block, rest)
default:
return true
return false
}
return false
return true
}
// Explicitly test the empty block.
......
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