Skip to content

Commit f55eb0c

Browse files
authored
Add first fuzzing test (#94)
1 parent d6f8f7a commit f55eb0c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ benchstat:
3030
test:
3131
CGO_ENABLED=1 $(GO) test ./... -coverprofile=coverage.out -covermode=atomic && go tool cover -func=coverage.out
3232

33+
.PHONY: fuzz
34+
fuzz:
35+
CGO_ENABLED=1 $(GO) test -fuzz=Fuzz -v -run ^Fuzz github.com/metal-stack/go-ipam
36+
3337
.PHONY: golangcicheck
3438
golangcicheck:
3539
@/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)

prefix_fuzz_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ipam
2+
3+
import (
4+
"context"
5+
"net/netip"
6+
"testing"
7+
)
8+
9+
func FuzzIpamer_AcquireIP(f *testing.F) {
10+
ctx := context.Background()
11+
tests := []struct {
12+
name string
13+
prefixCIDR string
14+
want string
15+
}{
16+
{
17+
name: "Acquire next IP regularly",
18+
prefixCIDR: "192.168.1.0/24",
19+
want: "192.168.1.1",
20+
},
21+
{
22+
name: "Acquire next IPv6 regularly",
23+
prefixCIDR: "2001:db8:85a3::/124",
24+
want: "2001:db8:85a3::1",
25+
},
26+
{
27+
name: "Want next IP, but network is full",
28+
prefixCIDR: "192.168.4.0/32",
29+
want: "",
30+
},
31+
{
32+
name: "Want next IPv6, but network is full",
33+
prefixCIDR: "2001:db8:85a3::/128",
34+
want: "",
35+
},
36+
}
37+
for _, tc := range tests {
38+
tc := tc
39+
f.Add(tc.prefixCIDR, tc.want)
40+
}
41+
42+
f.Fuzz(func(t *testing.T, prefixCIDR, want string) {
43+
ipam := New()
44+
p, err := ipam.NewPrefix(ctx, prefixCIDR)
45+
46+
if err == nil {
47+
prefix, err := netip.ParsePrefix(prefixCIDR)
48+
if err != nil {
49+
t.Errorf(err.Error())
50+
}
51+
if prefix.Masked().String() != p.String() {
52+
if err != nil {
53+
t.Errorf("%q not equal %q", prefix.Masked().String(), p.String())
54+
}
55+
}
56+
ip, err := ipam.AcquireIP(ctx, p.Cidr)
57+
if err == nil && want != "" && want != ip.IP.String() {
58+
if err != nil {
59+
t.Errorf("%q not equal %q", want, ip.IP.String())
60+
}
61+
}
62+
}
63+
})
64+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go test fuzz v1
2+
string("0")
3+
string("0")

0 commit comments

Comments
 (0)