mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 09:58:52 +03:00
tests
This commit is contained in:
parent
f99b4f07e9
commit
bbd0e2b4f1
5 changed files with 49 additions and 22 deletions
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()))
|
||||
}
|
||||
|
|
20
dhcpd/v6.go
20
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue