Skip to content

Commit 616e45e

Browse files
matttproudrsc
authored andcommitted
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 <[email protected]>
1 parent 1be2ddd commit 616e45e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/encoding/pem/pem_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,28 @@ func TestFuzz(t *testing.T) {
155155
}
156156

157157
var buf bytes.Buffer
158-
err := Encode(&buf, &block)
159-
decoded, rest := Decode(buf.Bytes())
160-
161-
switch {
162-
case err != nil:
158+
if err := Encode(&buf, &block); err != nil {
163159
t.Errorf("Encode of %#v resulted in error: %s", &block, err)
164-
case !reflect.DeepEqual(&block, decoded):
160+
return false
161+
}
162+
decoded, rest := Decode(buf.Bytes())
163+
if block.Headers == nil {
164+
// Encoder supports nil Headers but decoder returns initialized.
165+
block.Headers = make(map[string]string)
166+
}
167+
if block.Bytes == nil {
168+
// Encoder supports nil Bytes but decoder returns initialized.
169+
block.Bytes = make([]byte, 0)
170+
}
171+
if !reflect.DeepEqual(decoded, &block) {
165172
t.Errorf("Encode of %#v decoded as %#v", &block, decoded)
166-
case len(rest) != 0:
173+
return false
174+
}
175+
if len(rest) != 0 {
167176
t.Errorf("Encode of %#v decoded correctly, but with %x left over", block, rest)
168-
default:
169-
return true
177+
return false
170178
}
171-
return false
179+
return true
172180
}
173181

174182
// Explicitly test the empty block.

0 commit comments

Comments
 (0)