Skip to content

Commit

Permalink
Add first fuzzing test (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jan 9, 2023
1 parent d6f8f7a commit f55eb0c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ benchstat:
test:
CGO_ENABLED=1 $(GO) test ./... -coverprofile=coverage.out -covermode=atomic && go tool cover -func=coverage.out

.PHONY: fuzz
fuzz:
CGO_ENABLED=1 $(GO) test -fuzz=Fuzz -v -run ^Fuzz github.com/metal-stack/go-ipam

.PHONY: golangcicheck
golangcicheck:
@/bin/bash -c "type -P golangci-lint;" 2>/dev/null || (echo "golangci-lint is required but not available in current PATH. Install: https://github.com/golangci/golangci-lint#install"; exit 1)
Expand Down
64 changes: 64 additions & 0 deletions prefix_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ipam

import (
"context"
"net/netip"
"testing"
)

func FuzzIpamer_AcquireIP(f *testing.F) {
ctx := context.Background()
tests := []struct {
name string
prefixCIDR string
want string
}{
{
name: "Acquire next IP regularly",
prefixCIDR: "192.168.1.0/24",
want: "192.168.1.1",
},
{
name: "Acquire next IPv6 regularly",
prefixCIDR: "2001:db8:85a3::/124",
want: "2001:db8:85a3::1",
},
{
name: "Want next IP, but network is full",
prefixCIDR: "192.168.4.0/32",
want: "",
},
{
name: "Want next IPv6, but network is full",
prefixCIDR: "2001:db8:85a3::/128",
want: "",
},
}
for _, tc := range tests {
tc := tc
f.Add(tc.prefixCIDR, tc.want)
}

f.Fuzz(func(t *testing.T, prefixCIDR, want string) {
ipam := New()
p, err := ipam.NewPrefix(ctx, prefixCIDR)

if err == nil {
prefix, err := netip.ParsePrefix(prefixCIDR)
if err != nil {
t.Errorf(err.Error())
}
if prefix.Masked().String() != p.String() {
if err != nil {
t.Errorf("%q not equal %q", prefix.Masked().String(), p.String())
}
}
ip, err := ipam.AcquireIP(ctx, p.Cidr)
if err == nil && want != "" && want != ip.IP.String() {
if err != nil {
t.Errorf("%q not equal %q", want, ip.IP.String())
}
}
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
string("0")
string("0")

0 comments on commit f55eb0c

Please sign in to comment.