aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/pem
diff options
context:
space:
mode:
authorMatt T. Proud <matt.proud@gmail.com>2015-12-08 13:02:17 +0100
committerRuss Cox <rsc@golang.org>2015-12-08 19:27:20 +0000
commit616e45eaa13eee9411f0528e91f46212aa58dc97 (patch)
tree702c9ecb2b7f30b2ba2f0aa3a3a6b827ea9f9da6 /src/encoding/pem
parent1be2ddda9a372de052ba3428128085152b2e9044 (diff)
downloadgo-616e45eaa13eee9411f0528e91f46212aa58dc97.tar.gz
go-616e45eaa13eee9411f0528e91f46212aa58dc97.zip
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>
Diffstat (limited to 'src/encoding/pem')
-rw-r--r--src/encoding/pem/pem_test.go28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/encoding/pem/pem_test.go b/src/encoding/pem/pem_test.go
index ab656c6261..958dbc1a3a 100644
--- a/src/encoding/pem/pem_test.go
+++ b/src/encoding/pem/pem_test.go
@@ -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.