Commit 638d4f30 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Expand out fuzz scenarios

parent 99db2615
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
# vim:ft=make
#
.PHONY: all build test clean install fmt fix vet lint sec security check \
	help dupl docs ci
	help dupl docs ci fuzz

BINARY_NAME=bulletin
GO=go
@@ -28,6 +28,7 @@ help:
	@echo "  install    - Install the binary to GOPATH/bin"
	@echo "  docs       - Generate docs"
	@echo "  dupl       - Find all duplicated code"
	@echo "  fuzz       - Find all duplicated code"
	@echo "  all        - Run check and build (default)"

build:
@@ -37,6 +38,9 @@ build:
	$(GO) build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BINARY_NAME) main.go
	@echo "Build complete: $(BINARY_NAME)"

fuzz:
	$(GO) test -fuzz=Fuzz -fuzztime=10s -v ./dclish

test:
	@echo "Running tests..."
	CGO_ENABLED=$(CGO_FLAG) $(GO) test -v $(RACE_FLAG) -coverprofile=coverage.out ./...
+66 −37
Original line number Diff line number Diff line
@@ -4,9 +4,7 @@ import (
	"testing"
)

// FuzzParseAndRun fuzzes the DCLish command parser.
func FuzzParseAndRun(f *testing.F) {
	commands := Commands{
var commands = Commands{
	"SEND": {
		Action:  func(c *Command) error { return nil },
		MinArgs: 1,
@@ -34,19 +32,50 @@ func FuzzParseAndRun(f *testing.F) {
		},
	},
}

var goodCmds = []string{
	"SEND message.txt /TO=USER1",
	`SEND "a message with spaces" /URGENT`,
	`S "a message with spaces" /U`,
	"READ NEW",
	"DIRECTORY",
	"REPLY /ALL",
	`SEND 'quoted string' /TO="another quoted"`,
	"SEND msg",
	"",
	" ",
}

var badCmds = []string{
	"R NEW", // Ambiguous command.
	"UNKNOWN COMMAND",
	"DIR /FULL",
	"DIR kitten",
	"DIRECTORY /FULL",
	"REPLY /ALL /NOALL",
	"SEND /",
	"SEND /TO=/URGENT",
}

// FuzzParseRun fuzzes the DCLish command parser.
func FuzzParseRun(f *testing.F) {
	commandTable := BuildCommandTable(commands)

	f.Add("SEND message.txt /TO=USER1")
	f.Add(`SEND "a message with spaces" /URGENT`)
	f.Add("READ NEW")
	f.Add("DIRECTORY /FULL")
	f.Add("REPLY /ALL /NOALL")
	f.Add("UNKNOWN COMMAND")
	f.Add(`SEND 'quoted string' /TO="another quoted"`)
	f.Add("SEND /")
	f.Add("SEND /TO=/URGENT")
	f.Add("")
	f.Add(" ")
	for _, cmd := range goodCmds {
		f.Add(cmd)
		err := commandTable.ParseAndRun(cmd)
		if err != nil {
			f.Errorf("Expected nil for '%s', got %v", cmd, err)
		}
	}

	for _, cmd := range badCmds {
		f.Add(cmd)
		err := commandTable.ParseAndRun(cmd)
		if err == nil {
			f.Errorf("Expected an error for '%s'", cmd)
		}
	}

	f.Fuzz(func(t *testing.T, input string) {
		_ = commandTable.ParseAndRun(input)