1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func FuzzDecode(f
*testing
.F
) {
17 testdata
, err
:= os
.ReadDir("../testdata")
19 f
.Fatalf("failed to read testdata directory: %s", err
)
21 for _
, de
:= range testdata
{
22 if de
.IsDir() ||
!strings
.HasSuffix(de
.Name(), ".jpeg") {
25 b
, err
:= os
.ReadFile(filepath
.Join("../testdata", de
.Name()))
27 f
.Fatalf("failed to read testdata: %s", err
)
32 f
.Fuzz(func(t
*testing
.T
, b
[]byte) {
33 cfg
, _
, err
:= image
.DecodeConfig(bytes
.NewReader(b
))
37 if cfg
.Width
*cfg
.Height
> 1e6
{
40 img
, typ
, err
:= image
.Decode(bytes
.NewReader(b
))
41 if err
!= nil || typ
!= "jpeg" {
44 for q
:= 1; q
<= 100; q
++ {
46 err
:= Encode(&w
, img
, &Options
{Quality
: q
})
48 t
.Fatalf("failed to encode valid image: %s", err
)
50 img1
, err
:= Decode(&w
)
52 t
.Fatalf("failed to decode roundtripped image: %s", err
)
57 t
.Fatalf("roundtripped image bounds have changed, got: %s, want: %s", got
, want
)