forked from Code-Hex/gvisor-vmnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmnet_test.go
102 lines (98 loc) · 1.98 KB
/
vmnet_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package vmnet_test
import (
"net"
"net/netip"
"testing"
vmnet "github.com/Code-Hex/gvisor-vmnet"
"github.com/google/go-cmp/cmp"
)
func TestNetwork_LeaseIP(t *testing.T) {
gwHwAddr := net.HardwareAddr{
0x7a, 0x5b, 0x10, 0x21, 0x90, 0xe3,
}
tests := []struct {
cidr string
wantLen int
wantFromHead []vmnet.DHCPLease
wantErr bool
}{
{
cidr: "192.168.127.0/24",
wantLen: 254,
wantFromHead: []vmnet.DHCPLease{
{
HardwareAddr: gwHwAddr,
Addr: netip.MustParseAddr("192.168.127.1"),
},
{
Addr: netip.MustParseAddr("192.168.127.2"),
},
{
Addr: netip.MustParseAddr("192.168.127.3"),
},
},
},
{
cidr: "192.168.128.0/28",
wantLen: 14,
wantFromHead: []vmnet.DHCPLease{
{
HardwareAddr: gwHwAddr,
Addr: netip.MustParseAddr("192.168.128.1"),
},
{
Addr: netip.MustParseAddr("192.168.128.2"),
},
{
Addr: netip.MustParseAddr("192.168.128.3"),
},
},
},
{
cidr: "192.168.129.0/30",
wantLen: 2,
wantFromHead: []vmnet.DHCPLease{
{
HardwareAddr: gwHwAddr,
Addr: netip.MustParseAddr("192.168.129.1"),
},
{
Addr: netip.MustParseAddr("192.168.129.2"),
},
},
},
{
cidr: "192.168.130.0/31",
wantErr: true,
},
{
cidr: "192.168.130.0/32",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.cidr, func(t *testing.T) {
network, err := vmnet.New(tt.cidr, vmnet.WithGatewayMACAddress(gwHwAddr))
if err != nil {
if !tt.wantErr {
t.Fatalf("unexpected error: %v", err)
}
return
}
gw := network.Gateway()
got := gw.Leases()
if len(got) != tt.wantLen {
t.Fatalf("want len %d but got %d", tt.wantLen, len(got))
}
if diff := cmp.Diff(
tt.wantFromHead,
got[:len(tt.wantFromHead)],
cmp.Comparer(func(x, y netip.Addr) bool {
return x.Compare(y) == 0
}),
); diff != "" {
t.Errorf("(-want, +got)\n%s", diff)
}
})
}
}