Skip to content

Commit

Permalink
test: Add fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 20, 2024
1 parent adf063a commit a543c06
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/0dc5ed1e283739f6
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("\xdfA7")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/10bb5b2960e772f0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("\xed")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/60c24a059993f7ae
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("010")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/64f100070ec426a6
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("0_")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/771e938e4458e983
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("0")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/906bad78e0106290
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("0._0")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/93076e9d6b6510b6
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string(".0")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/a941c4713b3a921d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("01000e80")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/db7ae63e0377da75
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("~")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/def578230616f8b9
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("\xff")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/e222fa9b3faf0bc5
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/ec1cb26f1a43547c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string(" ")
2 changes: 2 additions & 0 deletions cmd/testdata/fuzz/Fuzz_templateReader/fe0e339227bff527
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("\t\n")

0 comments on commit a543c06

Please sign in to comment.