From bbd0e2b4f15cbd74fa6a777503ae224f54d7fbbc Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 25 May 2020 14:43:33 +0300 Subject: [PATCH] tests --- dhcpd/dhcpd_test.go | 2 +- dhcpd/v4.go | 4 ++-- dhcpd/v4_test.go | 18 ++++++++++++------ dhcpd/v6.go | 20 +++++++++++++++++++- dhcpd/v6_test.go | 27 +++++++++++++++------------ 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/dhcpd/dhcpd_test.go b/dhcpd/dhcpd_test.go index 0060c1b1..95eb6461 100644 --- a/dhcpd/dhcpd_test.go +++ b/dhcpd/dhcpd_test.go @@ -44,7 +44,7 @@ func TestDB(t *testing.T) { l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") exp1 := time.Now().Add(time.Hour) l.Expiry = exp1 - s.srv4.addLease(&l) + s.srv4.(*v4Server).addLease(&l) l2 := Lease{} l2.IP = net.ParseIP("192.168.10.101").To4() diff --git a/dhcpd/v4.go b/dhcpd/v4.go index 0bfa6aa8..209f9c99 100644 --- a/dhcpd/v4.go +++ b/dhcpd/v4.go @@ -36,7 +36,7 @@ func (s *v4Server) WriteDiskConfig6(c *V6ServerConf) { } // Return TRUE if IP address is within range [start..stop] -func ipInRange(start net.IP, stop net.IP, ip net.IP) bool { +func ip4InRange(start net.IP, stop net.IP, ip net.IP) bool { if len(start) != 4 || len(stop) != 4 { return false } @@ -53,7 +53,7 @@ func (s *v4Server) ResetLeases(leases []*Lease) { for _, l := range leases { if l.Expiry.Unix() != leaseExpireStatic && - !ipInRange(s.conf.ipStart, s.conf.ipEnd, l.IP) { + !ip4InRange(s.conf.ipStart, s.conf.ipEnd, l.IP) { log.Debug("DHCPv4: skipping a lease with IP %v: not within current IP range", l.IP) continue diff --git a/dhcpd/v4_test.go b/dhcpd/v4_test.go index 68ffb88f..2aed1424 100644 --- a/dhcpd/v4_test.go +++ b/dhcpd/v4_test.go @@ -66,7 +66,8 @@ func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) { SubnetMask: "255.255.255.0", notify: notify4, } - s, err := v4Create(conf) + sIface, err := v4Create(conf) + s := sIface.(*v4Server) assert.True(t, err == nil) // add dynamic lease @@ -117,7 +118,8 @@ func TestV4StaticLeaseGet(t *testing.T) { SubnetMask: "255.255.255.0", notify: notify4, } - s, err := v4Create(conf) + sIface, err := v4Create(conf) + s := sIface.(*v4Server) assert.True(t, err == nil) s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()} @@ -164,8 +166,6 @@ func TestV4StaticLeaseGet(t *testing.T) { assert.Equal(t, 1, len(ls)) assert.Equal(t, "192.168.10.150", ls[0].IP.String()) assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) - - s.Stop() } func TestV4DynamicLeaseGet(t *testing.T) { @@ -177,7 +177,8 @@ func TestV4DynamicLeaseGet(t *testing.T) { SubnetMask: "255.255.255.0", notify: notify4, } - s, err := v4Create(conf) + sIface, err := v4Create(conf) + s := sIface.(*v4Server) assert.True(t, err == nil) s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()} @@ -220,5 +221,10 @@ func TestV4DynamicLeaseGet(t *testing.T) { assert.Equal(t, "192.168.10.100", ls[0].IP.String()) assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) - s.Stop() + start := net.ParseIP("192.168.10.100").To4() + stop := net.ParseIP("192.168.10.200").To4() + assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.10.99").To4())) + assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.100").To4())) + assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.201").To4())) + assert.True(t, ip4InRange(start, stop, net.ParseIP("192.168.10.100").To4())) } diff --git a/dhcpd/v6.go b/dhcpd/v6.go index d2a3eae6..6602d0c8 100644 --- a/dhcpd/v6.go +++ b/dhcpd/v6.go @@ -37,11 +37,29 @@ func (s *v6Server) WriteDiskConfig6(c *V6ServerConf) { *c = s.conf } +// Return TRUE if IP address is within range [start..0xff] +func ip6InRange(start net.IP, ip net.IP) bool { + if len(start) != 16 { + return false + } + if !bytes.Equal(start[:15], ip[:15]) { + return false + } + return start[15] <= ip[15] +} + // ResetLeases - reset leases func (s *v6Server) ResetLeases(ll []*Lease) { s.leases = nil for _, l := range ll { - // TODO + + if l.Expiry.Unix() != leaseExpireStatic && + !ip6InRange(s.conf.ipStart, l.IP) { + + log.Debug("DHCPv6: skipping a lease with IP %v: not within current IP range", l.IP) + continue + } + s.leases = append(s.leases, l) } } diff --git a/dhcpd/v6_test.go b/dhcpd/v6_test.go index 90a94ccc..0e13cac8 100644 --- a/dhcpd/v6_test.go +++ b/dhcpd/v6_test.go @@ -61,7 +61,8 @@ func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) { RangeStart: "2001::1", notify: notify6, } - s, err := v6Create(conf) + sIface, err := v6Create(conf) + s := sIface.(*v6Server) assert.True(t, err == nil) // add dynamic lease @@ -109,14 +110,15 @@ func TestV6GetLease(t *testing.T) { RangeStart: "2001::1", notify: notify6, } - s, err := v6Create(conf) + sIface, err := v6Create(conf) + s := sIface.(*v6Server) assert.True(t, err == nil) s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")} - s.conf.sid = dhcpv6.Duid{ + s.sid = dhcpv6.Duid{ Type: dhcpv6.DUID_LLT, HwType: iana.HWTypeEthernet, } - s.conf.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") + s.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") l := Lease{} l.IP = net.ParseIP("2001::1") @@ -129,7 +131,7 @@ func TestV6GetLease(t *testing.T) { msg, _ := req.GetInnerMessage() resp, _ := dhcpv6.NewAdvertiseFromSolicit(msg) assert.True(t, s.process(msg, req, resp)) - resp.AddOption(dhcpv6.OptServerID(s.conf.sid)) + resp.AddOption(dhcpv6.OptServerID(s.sid)) // check "Advertise" assert.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type()) @@ -160,8 +162,6 @@ func TestV6GetLease(t *testing.T) { assert.Equal(t, 1, len(ls)) assert.Equal(t, "2001::1", ls[0].IP.String()) assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) - - s.Stop() } func TestV6GetDynamicLease(t *testing.T) { @@ -170,14 +170,15 @@ func TestV6GetDynamicLease(t *testing.T) { RangeStart: "2001::2", notify: notify6, } - s, err := v6Create(conf) + sIface, err := v6Create(conf) + s := sIface.(*v6Server) assert.True(t, err == nil) s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")} - s.conf.sid = dhcpv6.Duid{ + s.sid = dhcpv6.Duid{ Type: dhcpv6.DUID_LLT, HwType: iana.HWTypeEthernet, } - s.conf.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") + s.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") // "Solicit" mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") @@ -185,7 +186,7 @@ func TestV6GetDynamicLease(t *testing.T) { msg, _ := req.GetInnerMessage() resp, _ := dhcpv6.NewAdvertiseFromSolicit(msg) assert.True(t, s.process(msg, req, resp)) - resp.AddOption(dhcpv6.OptServerID(s.conf.sid)) + resp.AddOption(dhcpv6.OptServerID(s.sid)) // check "Advertise" assert.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type()) @@ -215,5 +216,7 @@ func TestV6GetDynamicLease(t *testing.T) { assert.Equal(t, "2001::2", ls[0].IP.String()) assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) - s.Stop() + assert.True(t, !ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::1"))) + assert.True(t, !ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2002::2"))) + assert.True(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::2"))) }