From 738c7820fa279cdb8203b80acdf5f5e837ac66ae Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Thu, 30 Apr 2020 15:01:14 +0300 Subject: [PATCH] + tests --- dhcpd/dhcp_http.go | 4 +++- dhcpd/dhcpd.go | 5 +++++ dhcpd/v6.go | 8 ++++--- dhcpd/v6_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 dhcpd/v6_test.go diff --git a/dhcpd/dhcp_http.go b/dhcpd/dhcp_http.go index 26c23a45..8f77e82d 100644 --- a/dhcpd/dhcp_http.go +++ b/dhcpd/dhcp_http.go @@ -116,7 +116,9 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { return } - s6, err := v6Create(v6JSONToServerConf(newconfig.V6)) + v6conf := v6JSONToServerConf(newconfig.V6) + v6conf.notify = s.conf.Conf6.notify + s6, err := v6Create(v6conf) if s6 == nil { httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err) return diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 75c7246b..c8e12b9a 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -119,6 +119,7 @@ func (s *Server) CheckConfig(config ServerConfig) error { func Create(config ServerConfig) *Server { s := Server{} s.conf = config + s.conf.Conf6.notify = s.notify6 s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) if s.conf.Enabled { err := s.setConfig(config) @@ -146,6 +147,10 @@ func Create(config ServerConfig) *Server { return &s } +func (s *Server) notify6(flags uint32) { + s.dbStore() +} + // Init checks the configuration and initializes the server func (s *Server) Init(config ServerConfig) error { err := s.setConfig(config) diff --git a/dhcpd/v6.go b/dhcpd/v6.go index 1ec6b794..57bfbe56 100644 --- a/dhcpd/v6.go +++ b/dhcpd/v6.go @@ -17,7 +17,7 @@ const valIAID = "ADGH" // V6Server - DHCPv6 server type V6Server struct { - s4 *Server // for dbStore() + db *Server // for dbStore() srv *server6.Server leases []*Lease leasesLock sync.Mutex @@ -35,6 +35,8 @@ type V6ServerConf struct { leaseTime time.Duration dnsIPAddrs []net.IP // IPv6 addresses to return to DHCP clients as DNS server addresses sid dhcpv6.Duid + + notify func(uint32) } // WriteDiskConfig - write configuration @@ -72,7 +74,7 @@ func (s *V6Server) AddStaticLease(l Lease) error { s.leasesLock.Unlock() return err } - s.s4.dbStore() + s.conf.notify(LeaseChangedAddedStatic) s.leasesLock.Unlock() // s.notify(LeaseChangedAddedStatic) return nil @@ -93,7 +95,7 @@ func (s *V6Server) RemoveStaticLease(l Lease) error { s.leasesLock.Unlock() return err } - s.s4.dbStore() + s.conf.notify(LeaseChangedRemovedStatic) s.leasesLock.Unlock() // s.notify(LeaseChangedRemovedStatic) return nil diff --git a/dhcpd/v6_test.go b/dhcpd/v6_test.go new file mode 100644 index 00000000..83472cd3 --- /dev/null +++ b/dhcpd/v6_test.go @@ -0,0 +1,53 @@ +package dhcpd + +import ( + "net" + "testing" + + "github.com/stretchr/testify/assert" +) + +func notify(flags uint32) { +} + +func TestV6StaticLease(t *testing.T) { + conf := V6ServerConf{ + Enabled: true, + RangeStart: "2001::1", + notify: notify, + } + s, err := v6Create(conf) + assert.True(t, err == nil) + + ls := s.GetLeases(LeasesStatic) + assert.Equal(t, 0, len(ls)) + + // add static lease + l := Lease{} + l.IP = net.ParseIP("2001::1") + l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") + assert.True(t, s.AddStaticLease(l) == nil) + + // check + ls = s.GetLeases(LeasesStatic) + 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()) + assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic) + + // try to remove static lease - fail + l.IP = net.ParseIP("2001::2") + l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") + assert.True(t, s.RemoveStaticLease(l) != nil) + + // remove static lease + l.IP = net.ParseIP("2001::1") + l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") + assert.True(t, s.RemoveStaticLease(l) == nil) + + // check + ls = s.GetLeases(LeasesStatic) + assert.Equal(t, 0, len(ls)) + + s.Stop() +}