diff options
Diffstat (limited to 'netboot/netconf_test.go')
-rw-r--r-- | netboot/netconf_test.go | 137 |
1 files changed, 136 insertions, 1 deletions
diff --git a/netboot/netconf_test.go b/netboot/netconf_test.go index 066753b..ab40648 100644 --- a/netboot/netconf_test.go +++ b/netboot/netconf_test.go @@ -5,6 +5,7 @@ import ( "net" "testing" + "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" @@ -88,6 +89,140 @@ func TestGetNetConfFromPacketv6(t *testing.T) { dhcpv6.WithDNS(net.ParseIP("fe80::1")), dhcpv6.WithDomainSearchList("slackware.it"), ) - _, err := GetNetConfFromPacketv6(adv) + netconf, err := GetNetConfFromPacketv6(adv) + require.NoError(t, err) + // check addresses + require.Equal(t, 1, len(netconf.Addresses)) + require.Equal(t, net.ParseIP("::1"), netconf.Addresses[0].IPNet.IP) + require.Equal(t, 3600, netconf.Addresses[0].PreferredLifetime) + require.Equal(t, 5200, netconf.Addresses[0].ValidLifetime) + // check DNSes + require.Equal(t, 1, len(netconf.DNSServers)) + require.Equal(t, net.ParseIP("fe80::1"), netconf.DNSServers[0]) + // check DNS search list + require.Equal(t, 1, len(netconf.DNSSearchList)) + require.Equal(t, "slackware.it", netconf.DNSSearchList[0]) + // check routers + require.Equal(t, 0, len(netconf.Routers)) +} + +func TestGetNetConfFromPacketv4AddrZero(t *testing.T) { + d := dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.IPv4zero) + _, err := GetNetConfFromPacketv4(&d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NoMask(t *testing.T) { + d := dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + _, err := GetNetConfFromPacketv4(&d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NullMask(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(0, 0, 0, 0))(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NoLeaseTime(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NoDNS(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4EmptyDNSList(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + d = dhcpv4.WithDNS()(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NoSearchList(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + d = dhcpv4.WithDNS(net.ParseIP("10.10.0.1"), net.ParseIP("10.10.0.2"))(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4EmptySearchList(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + d = dhcpv4.WithDNS(net.ParseIP("10.10.0.1"), net.ParseIP("10.10.0.2"))(d) + d = dhcpv4.WithDomainSearchList()(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4NoRouter(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + d = dhcpv4.WithDNS(net.ParseIP("10.10.0.1"), net.ParseIP("10.10.0.2"))(d) + d = dhcpv4.WithDomainSearchList("slackware.it", "dhcp.slackware.it")(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4EmptyRouter(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(0))(d) + d = dhcpv4.WithDNS(net.ParseIP("10.10.0.1"), net.ParseIP("10.10.0.2"))(d) + d = dhcpv4.WithDomainSearchList("slackware.it", "dhcp.slackware.it")(d) + d = dhcpv4.WithRouter()(d) + _, err := GetNetConfFromPacketv4(d) + require.Error(t, err) +} + +func TestGetNetConfFromPacketv4(t *testing.T) { + d := &dhcpv4.DHCPv4{} + d.SetYourIPAddr(net.ParseIP("10.0.0.1")) + d = dhcpv4.WithNetmask(net.IPv4Mask(255, 255, 255, 0))(d) + d = dhcpv4.WithLeaseTime(uint32(5200))(d) + d = dhcpv4.WithDNS(net.ParseIP("10.10.0.1"), net.ParseIP("10.10.0.2"))(d) + d = dhcpv4.WithDomainSearchList("slackware.it", "dhcp.slackware.it")(d) + d = dhcpv4.WithRouter(net.ParseIP("10.0.0.254"))(d) + netconf, err := GetNetConfFromPacketv4(d) require.NoError(t, err) + // check addresses + require.Equal(t, 1, len(netconf.Addresses)) + require.Equal(t, net.ParseIP("10.0.0.1"), netconf.Addresses[0].IPNet.IP) + require.Equal(t, 0, netconf.Addresses[0].PreferredLifetime) + require.Equal(t, 5200, netconf.Addresses[0].ValidLifetime) + // check DNSes + require.Equal(t, 2, len(netconf.DNSServers)) + require.Equal(t, net.ParseIP("10.10.0.1"), netconf.DNSServers[0]) + require.Equal(t, net.ParseIP("10.10.0.2"), netconf.DNSServers[1]) + // check DNS search list + require.Equal(t, 2, len(netconf.DNSSearchList)) + require.Equal(t, "slackware.it", netconf.DNSSearchList[0]) + require.Equal(t, "dhcp.slackware.it", netconf.DNSSearchList[1]) + // check routers + require.Equal(t, 1, len(netconf.Routers)) + require.Equal(t, net.ParseIP("10.0.0.254"), netconf.Routers[0]) } |