Skip to content

Commit e485a0d

Browse files
committed
Migrating Fuzz testing to Go built-in testing
This also sets up CI to regularly perform Fuzz testing. This first step with fuzzing migrated just the existing Fuzz tests to the new setup. In the future additional tests can be added. Signed-off-by: Matt Farina <[email protected]>
1 parent cc17577 commit e485a0d

File tree

5 files changed

+82
-34
lines changed

5 files changed

+82
-34
lines changed

Diff for: .github/workflows/fuzz.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Fuzz Testing
2+
on:
3+
# Perform Fuzz testing on PRs and on a daily basis. Daily will continue to
4+
# look for problems. Doing this on PRs will look for issues introduced in
5+
# a change.
6+
pull_request:
7+
schedule:
8+
- cron: '33 23 * * *' # Run at 11:33 every day
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Install Go
14+
uses: actions/setup-go@v3
15+
with:
16+
go-version: "1.20"
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
- name: Fuzz
20+
run: make fuzz

Diff for: Makefile

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
GOPATH=$(shell go env GOPATH)
22
GOLANGCI_LINT=$(GOPATH)/bin/golangci-lint
3-
GOFUZZBUILD = $(GOPATH)/bin/go-fuzz-build
4-
GOFUZZ = $(GOPATH)/bin/go-fuzz
53

64
.PHONY: lint
75
lint: $(GOLANGCI_LINT)
@@ -19,19 +17,14 @@ test-cover:
1917
GO111MODULE=on go test -cover .
2018

2119
.PHONY: fuzz
22-
fuzz: $(GOFUZZBUILD) $(GOFUZZ)
23-
@echo "==> Fuzz testing"
24-
$(GOFUZZBUILD)
25-
$(GOFUZZ) -workdir=_fuzz
20+
fuzz:
21+
@echo "==> Running Fuzz Tests"
22+
go test -fuzz=FuzzNewVersion -fuzztime=15s .
23+
go test -fuzz=FuzzStrictNewVersion -fuzztime=15s .
24+
go test -fuzz=FuzzNewConstraint -fuzztime=15s .
2625

2726
$(GOLANGCI_LINT):
2827
# Install golangci-lint. The configuration for it is in the .golangci.yml
2928
# file in the root of the repository
3029
echo ${GOPATH}
3130
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.17.1
32-
33-
$(GOFUZZBUILD):
34-
cd / && go get -u github.com/dvyukov/go-fuzz/go-fuzz-build
35-
36-
$(GOFUZZ):
37-
cd / && go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-dep

Diff for: constraints_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,36 @@ func TestTextUnmarshalConstraints(t *testing.T) {
776776
}
777777
}
778778
}
779+
780+
func FuzzNewConstraint(f *testing.F) {
781+
testcases := []string{
782+
"v1.2.3",
783+
" ",
784+
"......",
785+
"1",
786+
"1.2.3-beta.1",
787+
"1.2.3+foo",
788+
"2.3.4-alpha.1+bar",
789+
"lorem ipsum",
790+
"*",
791+
"!=1.2.3",
792+
"^4.5",
793+
"1.0.0 - 2",
794+
"1.2.3.4.5.6",
795+
">= 1",
796+
"~9.8.7",
797+
"<= 12.13.14",
798+
"987654321.123456789.654123789",
799+
"1.x",
800+
"2.3.x",
801+
"9.2-beta.0",
802+
}
803+
804+
for _, tc := range testcases {
805+
f.Add(tc)
806+
}
807+
808+
f.Fuzz(func(t *testing.T, a string) {
809+
_, _ = NewConstraint(a)
810+
})
811+
}

Diff for: fuzz.go

-22
This file was deleted.

Diff for: version_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,27 @@ func TestValidateMetadata(t *testing.T) {
681681
}
682682
}
683683
}
684+
685+
func FuzzNewVersion(f *testing.F) {
686+
testcases := []string{"v1.2.3", " ", "......", "1", "1.2.3-beta.1", "1.2.3+foo", "2.3.4-alpha.1+bar", "lorem ipsum"}
687+
688+
for _, tc := range testcases {
689+
f.Add(tc)
690+
}
691+
692+
f.Fuzz(func(t *testing.T, a string) {
693+
_, _ = NewVersion(a)
694+
})
695+
}
696+
697+
func FuzzStrictNewVersion(f *testing.F) {
698+
testcases := []string{"v1.2.3", " ", "......", "1", "1.2.3-beta.1", "1.2.3+foo", "2.3.4-alpha.1+bar", "lorem ipsum"}
699+
700+
for _, tc := range testcases {
701+
f.Add(tc)
702+
}
703+
704+
f.Fuzz(func(t *testing.T, a string) {
705+
_, _ = StrictNewVersion(a)
706+
})
707+
}

0 commit comments

Comments
 (0)