Skip to content

Commit bdcb431

Browse files
authored
Add simple fuzz test (#34)
* Add simple fuzz test Signed-off-by: Jason Hall <[email protected]> * Fix fuzz finding, add fuzz tests to GitHub Actions CI --------- Signed-off-by: Jason Hall <[email protected]>
1 parent 805e3f9 commit bdcb431

7 files changed

+68
-0
lines changed

.github/workflows/test.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ jobs:
3131
run: |
3232
GO111MODULE=off go get github.com/mattn/goveralls
3333
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
34+
35+
- name: Fuzz tests
36+
run: make fuzz

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ test:
44
curl -Ls https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json
55
go test -v -cover ./...
66

7+
fuzz:
8+
go test -fuzztime=1m -fuzz .
9+
10+
clean:
11+
find . -name "test-suite-data.json" | xargs rm -f
12+
713
lint:
814
go get -u golang.org/x/lint/golint
915
golint -set_exit_status

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,19 @@ PASS
7272
github.com/package-url/packageurl-go coverage: 90.7% of statements
7373
ok github.com/package-url/packageurl-go 0.004s coverage: 90.7% of statements
7474
```
75+
76+
## Fuzzing
77+
78+
Fuzzing is done with standard [Go fuzzing](https://go.dev/doc/fuzz/), introduced in Go 1.18.
79+
80+
Fuzz tests check for inputs that cause `FromString` to panic.
81+
82+
Using `make fuzz` will run fuzz tests for one minute.
83+
84+
To run fuzz tests longer:
85+
86+
```
87+
go test -fuzztime=60m -fuzz .
88+
```
89+
90+
Or omit `-fuzztime` entirely to run indefinitely.

fuzz_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright (c) the purl authors
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package packageurl
24+
25+
import (
26+
"fmt"
27+
"testing"
28+
)
29+
30+
func FuzzFromString(f *testing.F) {
31+
f.Fuzz(func(t *testing.T, s string) {
32+
// Test that parsing doesn't panic.
33+
_, _ = FromString(s)
34+
fmt.Print(s)
35+
})
36+
}

packageurl.go

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ func FromString(purl string) (PackageURL, error) {
257257
qualifier := remainder[index+1:]
258258
for _, item := range strings.Split(qualifier, "&") {
259259
kv := strings.Split(item, "=")
260+
if len(kv) != 2 {
261+
return PackageURL{}, fmt.Errorf("wanted 2 kv segments, got %d", len(kv))
262+
}
260263
key := strings.ToLower(kv[0])
261264
key, err := url.PathUnescape(key)
262265
if err != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("?A")

0 commit comments

Comments
 (0)