2020-06-08 03:29:51 +03:00
|
|
|
package format_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-16 19:11:26 +03:00
|
|
|
"io/fs"
|
2020-06-08 03:29:51 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-11-16 19:11:26 +03:00
|
|
|
|
|
|
|
_ "github.com/wader/fq/format/all"
|
2022-02-12 02:16:47 +03:00
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-11-16 19:11:26 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-16 19:11:26 +03:00
|
|
|
type fuzzFS struct{}
|
|
|
|
|
|
|
|
func (fuzzFS) Open(name string) (fs.File, error) {
|
|
|
|
return nil, fmt.Errorf("%s: file not found", name)
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type fuzzTest struct {
|
|
|
|
b []byte
|
2022-02-12 02:16:47 +03:00
|
|
|
f decode.Format
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 19:11:26 +03:00
|
|
|
type fuzzTestInput struct {
|
|
|
|
interp.FileReader
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fuzzTestInput) IsTerminal() bool { return false }
|
|
|
|
func (fuzzTestInput) Size() (int, int) { return 120, 25 }
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type fuzzTestOutput struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o fuzzTestOutput) Size() (int, int) { return 120, 25 }
|
|
|
|
func (o fuzzTestOutput) IsTerminal() bool { return false }
|
|
|
|
|
2022-02-05 20:38:38 +03:00
|
|
|
func (ft *fuzzTest) Platform() interp.Platform { return interp.Platform{} }
|
2021-11-16 19:11:26 +03:00
|
|
|
func (ft *fuzzTest) Stdin() interp.Input {
|
|
|
|
return fuzzTestInput{FileReader: interp.FileReader{R: bytes.NewBuffer(ft.b)}}
|
|
|
|
}
|
2022-02-12 02:16:47 +03:00
|
|
|
func (ft *fuzzTest) Stdout() interp.Output { return fuzzTestOutput{ioutil.Discard} }
|
|
|
|
func (ft *fuzzTest) Stderr() interp.Output { return fuzzTestOutput{ioutil.Discard} }
|
2021-11-01 14:57:55 +03:00
|
|
|
func (ft *fuzzTest) InterruptChan() chan struct{} { return nil }
|
|
|
|
func (ft *fuzzTest) Environ() []string { return nil }
|
2020-06-08 03:29:51 +03:00
|
|
|
func (ft *fuzzTest) Args() []string {
|
2022-01-24 18:56:24 +03:00
|
|
|
return []string{
|
|
|
|
`fq`,
|
2022-02-12 02:16:47 +03:00
|
|
|
`-d`, ft.f.Name,
|
|
|
|
`.`,
|
2022-01-24 18:56:24 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
func (ft *fuzzTest) ConfigDir() (string, error) { return "/config", nil }
|
2021-11-16 19:11:26 +03:00
|
|
|
func (ft *fuzzTest) FS() fs.FS { return fuzzFS{} }
|
|
|
|
func (ft *fuzzTest) History() ([]string, error) { return nil, nil }
|
|
|
|
|
2022-02-12 02:16:47 +03:00
|
|
|
func (ft *fuzzTest) Readline(opts interp.ReadlineOpts) (string, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:51:36 +03:00
|
|
|
func FuzzFormats(f *testing.F) {
|
2022-07-20 19:13:31 +03:00
|
|
|
if os.Getenv("FUZZTEST") == "" {
|
|
|
|
f.Skip("run with FUZZTEST=1 do fuzz")
|
|
|
|
}
|
|
|
|
|
2022-02-05 20:38:38 +03:00
|
|
|
i := 0
|
2022-02-22 03:04:38 +03:00
|
|
|
|
2022-07-20 19:13:31 +03:00
|
|
|
if err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
2022-02-22 03:04:38 +03:00
|
|
|
if filepath.Base(path) != "testdata" {
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:13:31 +03:00
|
|
|
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
2022-02-22 03:04:38 +03:00
|
|
|
if filepath.Ext(path) == ".fqtest" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if st, err := os.Stat(path); err != nil || st.IsDir() {
|
2022-08-01 19:47:24 +03:00
|
|
|
return err
|
2022-02-22 03:04:38 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-07-20 19:13:31 +03:00
|
|
|
b, readErr := ioutil.ReadFile(path)
|
|
|
|
if readErr != nil {
|
2022-02-22 03:04:38 +03:00
|
|
|
f.Fatal(err)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-22 03:04:38 +03:00
|
|
|
f.Logf("seed#%d %s", i, path)
|
|
|
|
f.Add(b)
|
|
|
|
i++
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-22 03:04:38 +03:00
|
|
|
return nil
|
2022-07-20 19:13:31 +03:00
|
|
|
}); err != nil {
|
|
|
|
f.Fatal(f)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil
|
2022-07-20 19:13:31 +03:00
|
|
|
}); err != nil {
|
|
|
|
f.Fatal(f)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-12 02:16:47 +03:00
|
|
|
gi := 0
|
2022-07-20 19:13:31 +03:00
|
|
|
g := interp.DefaultRegistry.MustAll()
|
2022-02-12 02:16:47 +03:00
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
f.Fuzz(func(t *testing.T, b []byte) {
|
2022-02-12 02:16:47 +03:00
|
|
|
fz := &fuzzTest{b: b, f: g[gi]}
|
2022-07-20 19:13:31 +03:00
|
|
|
q, err := interp.New(fz, interp.DefaultRegistry)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:11:26 +03:00
|
|
|
_ = q.Main(context.Background(), fz.Stdout(), "dev")
|
|
|
|
// if err != nil {
|
|
|
|
// // TODO: expect error
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
2022-02-12 02:16:47 +03:00
|
|
|
|
|
|
|
gi = (gi + 1) % len(g)
|
2020-06-08 03:29:51 +03:00
|
|
|
})
|
|
|
|
}
|