]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/pem: make TestFuzz testing/quick safe
authorMatt T. Proud <matt.proud@gmail.com>
Tue, 8 Dec 2015 12:02:17 +0000 (13:02 +0100)
committerRuss Cox <rsc@golang.org>
Tue, 8 Dec 2015 19:27:20 +0000 (19:27 +0000)
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>
src/encoding/pem/pem_test.go

index ab656c6261c4012a112b7a9e426d5e6589c41ae4..958dbc1a3a6d1026e3bb0a6b976ee14b5b03d691 100644 (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):
+                       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.