From a543c064885a259acab51e48936f876756fb76ff Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 20 Sep 2024 15:07:31 -0500 Subject: [PATCH] test: Add fuzz test --- cmd/cmd_test.go | 36 +++++++++++++++++++ .../fuzz/Fuzz_templateReader/0dc5ed1e283739f6 | 2 ++ .../fuzz/Fuzz_templateReader/10bb5b2960e772f0 | 2 ++ .../fuzz/Fuzz_templateReader/60c24a059993f7ae | 2 ++ .../fuzz/Fuzz_templateReader/64f100070ec426a6 | 2 ++ .../fuzz/Fuzz_templateReader/771e938e4458e983 | 2 ++ .../fuzz/Fuzz_templateReader/906bad78e0106290 | 2 ++ .../fuzz/Fuzz_templateReader/93076e9d6b6510b6 | 2 ++ .../fuzz/Fuzz_templateReader/a941c4713b3a921d | 2 ++ .../fuzz/Fuzz_templateReader/db7ae63e0377da75 | 2 ++ .../fuzz/Fuzz_templateReader/def578230616f8b9 | 2 ++ .../fuzz/Fuzz_templateReader/e222fa9b3faf0bc5 | 2 ++ .../fuzz/Fuzz_templateReader/ec1cb26f1a43547c | 2 ++ .../fuzz/Fuzz_templateReader/fe0e339227bff527 | 2 ++ 14 files changed, 62 insertions(+) create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/0dc5ed1e283739f6 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/10bb5b2960e772f0 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/60c24a059993f7ae create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/64f100070ec426a6 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/771e938e4458e983 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/906bad78e0106290 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/93076e9d6b6510b6 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/a941c4713b3a921d create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/db7ae63e0377da75 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/def578230616f8b9 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/e222fa9b3faf0bc5 create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/ec1cb26f1a43547c create mode 100644 cmd/testdata/fuzz/Fuzz_templateReader/fe0e339227bff527 diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index b07a87c..bfc816b 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -1,15 +1,18 @@ package cmd import ( + "encoding/base64" "io" "os" "strings" "testing" + "unicode/utf8" "github.com/clevyr/yampl/internal/config" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func Test_run(t *testing.T) { @@ -183,3 +186,36 @@ func Test_openAndTemplateFile(t *testing.T) { }) } } + +func Fuzz_templateReader(f *testing.F) { + const template = `value: "" #yampl {{ .newVal }}` + f.Add("hello world") + f.Add("123") + f.Add("true") + + f.Fuzz(func(t *testing.T, content string) { + conf := config.New() + conf.Vars = config.Vars{"newVal": content} + + got, err := templateReader(conf, "", strings.NewReader(template), int64(len(content))) + require.NoError(t, err) + + var decoded map[string]any + require.NoError(t, yaml.Unmarshal([]byte(got), &decoded)) + + switch val := decoded["value"].(type) { + case string: + if utf8.ValidString(content) { + assert.Equal(t, content, val) + } else { + raw, err := base64.StdEncoding.DecodeString(val) + require.NoError(t, err) + assert.Equal(t, content, string(raw)) + } + default: + var want any + require.NoError(t, yaml.Unmarshal([]byte(content), &want)) + assert.Equal(t, want, val) + } + }) +} diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/0dc5ed1e283739f6 b/cmd/testdata/fuzz/Fuzz_templateReader/0dc5ed1e283739f6 new file mode 100644 index 0000000..3540611 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/0dc5ed1e283739f6 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("\xdfA7") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/10bb5b2960e772f0 b/cmd/testdata/fuzz/Fuzz_templateReader/10bb5b2960e772f0 new file mode 100644 index 0000000..262a93e --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/10bb5b2960e772f0 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("\xed") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/60c24a059993f7ae b/cmd/testdata/fuzz/Fuzz_templateReader/60c24a059993f7ae new file mode 100644 index 0000000..832f4c8 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/60c24a059993f7ae @@ -0,0 +1,2 @@ +go test fuzz v1 +string("010") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/64f100070ec426a6 b/cmd/testdata/fuzz/Fuzz_templateReader/64f100070ec426a6 new file mode 100644 index 0000000..491131f --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/64f100070ec426a6 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("0_") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/771e938e4458e983 b/cmd/testdata/fuzz/Fuzz_templateReader/771e938e4458e983 new file mode 100644 index 0000000..ee3f339 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/771e938e4458e983 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("0") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/906bad78e0106290 b/cmd/testdata/fuzz/Fuzz_templateReader/906bad78e0106290 new file mode 100644 index 0000000..31755fe --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/906bad78e0106290 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("0._0") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/93076e9d6b6510b6 b/cmd/testdata/fuzz/Fuzz_templateReader/93076e9d6b6510b6 new file mode 100644 index 0000000..d78afc8 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/93076e9d6b6510b6 @@ -0,0 +1,2 @@ +go test fuzz v1 +string(".0") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/a941c4713b3a921d b/cmd/testdata/fuzz/Fuzz_templateReader/a941c4713b3a921d new file mode 100644 index 0000000..05a9444 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/a941c4713b3a921d @@ -0,0 +1,2 @@ +go test fuzz v1 +string("01000e80") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/db7ae63e0377da75 b/cmd/testdata/fuzz/Fuzz_templateReader/db7ae63e0377da75 new file mode 100644 index 0000000..b426c1c --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/db7ae63e0377da75 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("~") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/def578230616f8b9 b/cmd/testdata/fuzz/Fuzz_templateReader/def578230616f8b9 new file mode 100644 index 0000000..4054d5f --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/def578230616f8b9 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("\xff") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/e222fa9b3faf0bc5 b/cmd/testdata/fuzz/Fuzz_templateReader/e222fa9b3faf0bc5 new file mode 100644 index 0000000..9da8da8 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/e222fa9b3faf0bc5 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("\"\"\"\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17\x17") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/ec1cb26f1a43547c b/cmd/testdata/fuzz/Fuzz_templateReader/ec1cb26f1a43547c new file mode 100644 index 0000000..73ea24d --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/ec1cb26f1a43547c @@ -0,0 +1,2 @@ +go test fuzz v1 +string(" ") diff --git a/cmd/testdata/fuzz/Fuzz_templateReader/fe0e339227bff527 b/cmd/testdata/fuzz/Fuzz_templateReader/fe0e339227bff527 new file mode 100644 index 0000000..5c6b832 --- /dev/null +++ b/cmd/testdata/fuzz/Fuzz_templateReader/fe0e339227bff527 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("\t\n")