github.com/expr-lang/expr@v1.16.9/test/fuzz/fuzz_test.go (about)

     1  package fuzz
     2  
     3  import (
     4  	_ "embed"
     5  	"regexp"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/expr-lang/expr"
    10  )
    11  
    12  //go:embed fuzz_corpus.txt
    13  var fuzzCorpus string
    14  
    15  func FuzzExpr(f *testing.F) {
    16  	corpus := strings.Split(strings.TrimSpace(fuzzCorpus), "\n")
    17  	for _, s := range corpus {
    18  		f.Add(s)
    19  	}
    20  
    21  	skip := []*regexp.Regexp{
    22  		regexp.MustCompile(`cannot fetch .* from .*`),
    23  		regexp.MustCompile(`cannot get .* from .*`),
    24  		regexp.MustCompile(`cannot slice`),
    25  		regexp.MustCompile(`slice index out of range`),
    26  		regexp.MustCompile(`error parsing regexp`),
    27  		regexp.MustCompile(`integer divide by zero`),
    28  		regexp.MustCompile(`interface conversion`),
    29  		regexp.MustCompile(`invalid argument for .*`),
    30  		regexp.MustCompile(`invalid character`),
    31  		regexp.MustCompile(`invalid operation`),
    32  		regexp.MustCompile(`invalid duration`),
    33  		regexp.MustCompile(`time: missing unit in duration`),
    34  		regexp.MustCompile(`time: unknown unit .* in duration`),
    35  		regexp.MustCompile(`unknown time zone`),
    36  		regexp.MustCompile(`json: unsupported value`),
    37  		regexp.MustCompile(`unexpected end of JSON input`),
    38  		regexp.MustCompile(`memory budget exceeded`),
    39  		regexp.MustCompile(`using interface \{} as type .*`),
    40  		regexp.MustCompile(`reflect.Value.MapIndex: value of type .* is not assignable to type .*`),
    41  		regexp.MustCompile(`reflect: Call using .* as type .*`),
    42  		regexp.MustCompile(`reflect: Call with too few input arguments`),
    43  		regexp.MustCompile(`reflect: call of reflect.Value.Call on .* Value`),
    44  		regexp.MustCompile(`reflect: call of reflect.Value.Index on map Value`),
    45  		regexp.MustCompile(`reflect: call of reflect.Value.Len on .* Value`),
    46  		regexp.MustCompile(`reflect: string index out of range`),
    47  		regexp.MustCompile(`strings: negative Repeat count`),
    48  		regexp.MustCompile(`strings: illegal bytes to escape`),
    49  		regexp.MustCompile(`operator "in" not defined on int`),
    50  		regexp.MustCompile(`invalid date .*`),
    51  		regexp.MustCompile(`cannot parse .* as .*`),
    52  		regexp.MustCompile(`operator "in" not defined on .*`),
    53  		regexp.MustCompile(`cannot sum .*`),
    54  	}
    55  
    56  	env := NewEnv()
    57  	fn := Func()
    58  
    59  	f.Fuzz(func(t *testing.T, code string) {
    60  		if len(code) > 1000 {
    61  			t.Skip("too long code")
    62  		}
    63  
    64  		program, err := expr.Compile(code, expr.Env(env), fn)
    65  		if err != nil {
    66  			t.Skipf("compile error: %s", err)
    67  		}
    68  
    69  		_, err = expr.Run(program, env)
    70  		if err != nil {
    71  			for _, r := range skip {
    72  				if r.MatchString(err.Error()) {
    73  					t.Skipf("skip error: %s", err)
    74  					return
    75  				}
    76  			}
    77  			t.Errorf("%s", err)
    78  		}
    79  	})
    80  }