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/17499
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Matt T. Proud 2015-12-08 13:02:17 +01:00 committed by Russ Cox
parent 1be2ddda9a
commit 616e45eaa1

View File

@ -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):
t.Errorf("Encode of %#v decoded as %#v", &block, decoded)
case len(rest) != 0:
t.Errorf("Encode of %#v decoded correctly, but with %x left over", block, rest)
default:
return true
return false
}
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)
return false
}
if len(rest) != 0 {
t.Errorf("Encode of %#v decoded correctly, but with %x left over", block, rest)
return false
}
return true
}
// Explicitly test the empty block.