diff --git a/Makefile b/Makefile index 6404ab8..a449299 100644 --- a/Makefile +++ b/Makefile @@ -51,10 +51,13 @@ install: buildlocal install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/ clean: - rm -rf $(tool) coverage.out + rm -rf $(tool) coverage.out testdata -test: - go test -v ./... +test: clean + go test ./... $(ARGS) + +testfuzzy: clean + go test -fuzz ./... $(ARGS) singletest: @echo "Call like this: make singletest TEST=TestPrepareColumns ARGS=-v" diff --git a/calc_test.go b/calc_test.go index dd38780..87f0925 100644 --- a/calc_test.go +++ b/calc_test.go @@ -19,6 +19,8 @@ package main import ( "fmt" + "strconv" + "strings" "testing" lua "github.com/yuin/gopher-lua" @@ -75,7 +77,9 @@ func TestCommentsAndWhitespace(t *testing.T) { t.Run(testname, func(t *testing.T) { for _, line := range tt.cmd { - calc.Eval(line) + if err := calc.Eval(line); err != nil { + t.Errorf(err.Error()) + } } got := calc.stack.Last() @@ -288,7 +292,9 @@ func TestCalc(t *testing.T) { t.Run(testname, func(t *testing.T) { calc.batch = tt.batch - calc.Eval(tt.cmd) + if err := calc.Eval(tt.cmd); err != nil { + t.Errorf(err.Error()) + } got := calc.Result() calc.stack.Clear() if got != tt.exp { @@ -350,3 +356,60 @@ func TestCalcLua(t *testing.T) { }) } } + +func FuzzEval(f *testing.F) { + legal := []string{ + "dump", + "showstack", + "help", + "Pi 31 *", + "SqrtE Pi /", + "55.5 yards-to-meters", + "2 4 +", + "7 8 batch sum", + "7 8 %-", + "7 8 clear", + "7 8 /", + "b", + "#444", + "", line) + // not corpus and empty? + if !contains(legal, line) && len(line) > 0 { + item := strings.TrimSpace(calc.Comment.ReplaceAllString(line, "")) + _, hexerr := fmt.Sscanf(item, "0x%x", &i) + // no comment? + if len(item) > 0 { + // no known command or function? + if _, err := strconv.ParseFloat(item, 64); err != nil { + if !contains(calc.Constants, item) && + !exists(calc.Funcalls, item) && + !exists(calc.BatchFuncalls, item) && + !contains(calc.LuaFunctions, item) && + !exists(calc.Commands, item) && + !exists(calc.ShowCommands, item) && + !exists(calc.SettingsCommands, item) && + !exists(calc.StackCommands, item) && + !calc.Register.MatchString(item) && + item != "?" && item != "help" && + hexerr != nil { + t.Errorf("Fuzzy input accepted: <%s>", line) + } + } + } + } + } + }) +}