summaryrefslogtreecommitdiffstats
path: root/src/image/gif/fuzz_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
commit43a123c1ae6613b3efeed291fa552ecd909d3acf (patch)
treefd92518b7024bc74031f78a1cf9e454b65e73665 /src/image/gif/fuzz_test.go
parentInitial commit. (diff)
downloadgolang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.tar.xz
golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.zip
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/image/gif/fuzz_test.go')
-rw-r--r--src/image/gif/fuzz_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/image/gif/fuzz_test.go b/src/image/gif/fuzz_test.go
new file mode 100644
index 0000000..a4bc06e
--- /dev/null
+++ b/src/image/gif/fuzz_test.go
@@ -0,0 +1,65 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gif
+
+import (
+ "bytes"
+ "image"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func FuzzDecode(f *testing.F) {
+ if testing.Short() {
+ f.Skip("Skipping in short mode")
+ }
+
+ testdata, err := os.ReadDir("../testdata")
+ if err != nil {
+ f.Fatalf("failed to read testdata directory: %s", err)
+ }
+ for _, de := range testdata {
+ if de.IsDir() || !strings.HasSuffix(de.Name(), ".gif") {
+ continue
+ }
+ b, err := os.ReadFile(filepath.Join("../testdata", de.Name()))
+ if err != nil {
+ f.Fatalf("failed to read testdata: %s", err)
+ }
+ f.Add(b)
+ }
+
+ f.Fuzz(func(t *testing.T, b []byte) {
+ cfg, _, err := image.DecodeConfig(bytes.NewReader(b))
+ if err != nil {
+ return
+ }
+ if cfg.Width*cfg.Height > 1e6 {
+ return
+ }
+ img, typ, err := image.Decode(bytes.NewReader(b))
+ if err != nil || typ != "gif" {
+ return
+ }
+ for q := 1; q <= 256; q++ {
+ var w bytes.Buffer
+ err := Encode(&w, img, &Options{NumColors: q})
+ if err != nil {
+ t.Fatalf("failed to encode valid image: %s", err)
+ }
+ img1, err := Decode(&w)
+ if err != nil {
+ t.Fatalf("failed to decode roundtripped image: %s", err)
+ }
+ got := img1.Bounds()
+ want := img.Bounds()
+ if !got.Eq(want) {
+ t.Fatalf("roundtripped image bounds have changed, got: %v, want: %v", got, want)
+ }
+ }
+ })
+}