mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-02-26 06:49:02 +03:00
+ tests
This commit is contained in:
parent
61e071d275
commit
738c7820fa
4 changed files with 66 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
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()
|
||||
}
|
Loading…
Add table
Reference in a new issue