-
Notifications
You must be signed in to change notification settings - Fork 42
/
prefix_fuzz_test.go
64 lines (60 loc) · 1.32 KB
/
prefix_fuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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(ctx)
p, err := ipam.NewPrefix(ctx, prefixCIDR)
if err == nil {
prefix, err := netip.ParsePrefix(prefixCIDR)
if err != nil {
t.Error(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())
}
}
}
})
}