From 12b2bd19d0337337449218fa1002f51e2028fc90 Mon Sep 17 00:00:00 2001
From: Gabriel Vasile <gabriel.vasile@email.com>
Date: Tue, 14 Dec 2021 17:00:03 +0200
Subject: [PATCH] whole library fuzz

---
 mimetype_test.go | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/mimetype_test.go b/mimetype_test.go
index 4d30f49e..50f2d51d 100644
--- a/mimetype_test.go
+++ b/mimetype_test.go
@@ -602,3 +602,30 @@ func TestExtend(t *testing.T) {
 		})
 	}
 }
+
+// Because of the random nature of fuzzing I don't think there is a way to test
+// the correctness of the Detect results. Still there is value in fuzzing in
+// search for panics.
+func FuzzMimetype(f *testing.F) {
+	corpus := []string{
+		"testdata/mkv.mkv",
+		"testdata/webm.webm",
+		"testdata/docx.docx",
+		"testdata/pptx.pptx",
+		"testdata/xlsx.xlsx",
+		"testdata/3gp.3gp",
+		"testdata/class.class",
+	}
+	for _, c := range corpus {
+		data, err := ioutil.ReadFile(c)
+		if err != nil {
+			f.Fatal(err)
+		}
+		f.Add(data[:100])
+	}
+	f.Fuzz(func(t *testing.T, d []byte) {
+		if m := Detect(d); m.Extension() == "" {
+			t.Skip()
+		}
+	})
+}