+ tests
This commit is contained in:
@@ -116,7 +116,9 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s6, err := v6Create(v6JSONToServerConf(newconfig.V6))
|
v6conf := v6JSONToServerConf(newconfig.V6)
|
||||||
|
v6conf.notify = s.conf.Conf6.notify
|
||||||
|
s6, err := v6Create(v6conf)
|
||||||
if s6 == nil {
|
if s6 == nil {
|
||||||
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err)
|
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ func (s *Server) CheckConfig(config ServerConfig) error {
|
|||||||
func Create(config ServerConfig) *Server {
|
func Create(config ServerConfig) *Server {
|
||||||
s := Server{}
|
s := Server{}
|
||||||
s.conf = config
|
s.conf = config
|
||||||
|
s.conf.Conf6.notify = s.notify6
|
||||||
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
|
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
|
||||||
if s.conf.Enabled {
|
if s.conf.Enabled {
|
||||||
err := s.setConfig(config)
|
err := s.setConfig(config)
|
||||||
@@ -146,6 +147,10 @@ func Create(config ServerConfig) *Server {
|
|||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) notify6(flags uint32) {
|
||||||
|
s.dbStore()
|
||||||
|
}
|
||||||
|
|
||||||
// Init checks the configuration and initializes the server
|
// Init checks the configuration and initializes the server
|
||||||
func (s *Server) Init(config ServerConfig) error {
|
func (s *Server) Init(config ServerConfig) error {
|
||||||
err := s.setConfig(config)
|
err := s.setConfig(config)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const valIAID = "ADGH"
|
|||||||
|
|
||||||
// V6Server - DHCPv6 server
|
// V6Server - DHCPv6 server
|
||||||
type V6Server struct {
|
type V6Server struct {
|
||||||
s4 *Server // for dbStore()
|
db *Server // for dbStore()
|
||||||
srv *server6.Server
|
srv *server6.Server
|
||||||
leases []*Lease
|
leases []*Lease
|
||||||
leasesLock sync.Mutex
|
leasesLock sync.Mutex
|
||||||
@@ -35,6 +35,8 @@ type V6ServerConf struct {
|
|||||||
leaseTime time.Duration
|
leaseTime time.Duration
|
||||||
dnsIPAddrs []net.IP // IPv6 addresses to return to DHCP clients as DNS server addresses
|
dnsIPAddrs []net.IP // IPv6 addresses to return to DHCP clients as DNS server addresses
|
||||||
sid dhcpv6.Duid
|
sid dhcpv6.Duid
|
||||||
|
|
||||||
|
notify func(uint32)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteDiskConfig - write configuration
|
// WriteDiskConfig - write configuration
|
||||||
@@ -72,7 +74,7 @@ func (s *V6Server) AddStaticLease(l Lease) error {
|
|||||||
s.leasesLock.Unlock()
|
s.leasesLock.Unlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.s4.dbStore()
|
s.conf.notify(LeaseChangedAddedStatic)
|
||||||
s.leasesLock.Unlock()
|
s.leasesLock.Unlock()
|
||||||
// s.notify(LeaseChangedAddedStatic)
|
// s.notify(LeaseChangedAddedStatic)
|
||||||
return nil
|
return nil
|
||||||
@@ -93,7 +95,7 @@ func (s *V6Server) RemoveStaticLease(l Lease) error {
|
|||||||
s.leasesLock.Unlock()
|
s.leasesLock.Unlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.s4.dbStore()
|
s.conf.notify(LeaseChangedRemovedStatic)
|
||||||
s.leasesLock.Unlock()
|
s.leasesLock.Unlock()
|
||||||
// s.notify(LeaseChangedRemovedStatic)
|
// s.notify(LeaseChangedRemovedStatic)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
53
dhcpd/v6_test.go
Normal file
53
dhcpd/v6_test.go
Normal file
@@ -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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user