mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 02:48:28 -07:00
7fab31beae
Merge in DNS/adguard-home from 2508-ip-conversion-vol2 to master Closes #2508. Squashed commit of the following: commit 5b9d33f9cd352756831f63e34c4aea48674628c1 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 20 17:15:17 2021 +0300 util: replace net.IPNet with pointer commit 680126de7d59464077f9edf1bbaa925dd3fcee19 Merge: d3ba6a6c5a50efad
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 20 17:02:41 2021 +0300 Merge branch 'master' into 2508-ip-conversion-vol2 commit d3ba6a6cdd01c0aa736418fdb86ed40120169fe9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 19 18:29:54 2021 +0300 all: remove last conversion commit 88b63f11a6c3f8705d7fa0c448c50dd646cc9214 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 19 14:12:45 2021 +0300 all: improve code quality commit 71af60c70a0dbaf55e2221023d6d2e4993c9e9a7 Merge: 98af37849f75725d
Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 17:13:27 2021 +0300 Merge branch 'master' into 2508-ip-conversion-vol2 commit 98af3784ce44d0993d171653c13d6e83bb8d1e6a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 16:32:53 2021 +0300 all: log changes commit e99595a172bae1e844019d344544be84ddd65e4e Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 18 16:06:49 2021 +0300 all: fix or remove remaining net.IP <-> string conversions commit 7fd0634ce945f7e4c9b856684c5199f8a84a543e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Jan 15 15:36:17 2021 +0300 all: remove redundant net.IP <-> string converions commit 5df8af030421237d41b67ed659f83526cc258199 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jan 14 16:35:25 2021 +0300 stats: remove redundant net.IP <-> string conversion commit fbe4e3fc015e6898063543a90c04401d76dbb18f Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jan 14 16:20:35 2021 +0300 querylog: remove redundant net.IP <-> string conversion
131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"bytes"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
func testNotify(flags uint32) {
|
|
}
|
|
|
|
// Leases database store/load
|
|
func TestDB(t *testing.T) {
|
|
var err error
|
|
s := Server{}
|
|
s.conf.DBFilePath = dbFilename
|
|
|
|
conf := V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
|
notify: testNotify,
|
|
}
|
|
s.srv4, err = v4Create(conf)
|
|
assert.Nil(t, err)
|
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
|
assert.Nil(t, err)
|
|
|
|
l := Lease{}
|
|
l.IP = net.IP{192, 168, 10, 100}
|
|
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
|
exp1 := time.Now().Add(time.Hour)
|
|
l.Expiry = exp1
|
|
s.srv4.(*v4Server).addLease(&l)
|
|
|
|
l2 := Lease{}
|
|
l2.IP = net.IP{192, 168, 10, 101}
|
|
l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb")
|
|
s.srv4.AddStaticLease(l2)
|
|
|
|
_ = os.Remove("leases.db")
|
|
s.dbStore()
|
|
s.srv4.ResetLeases(nil)
|
|
|
|
s.dbLoad()
|
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
|
|
|
assert.Equal(t, "aa:aa:aa:aa:aa:bb", ll[0].HWAddr.String())
|
|
assert.True(t, net.IP{192, 168, 10, 101}.Equal(ll[0].IP))
|
|
assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix())
|
|
|
|
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String())
|
|
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ll[1].IP))
|
|
assert.Equal(t, exp1.Unix(), ll[1].Expiry.Unix())
|
|
|
|
_ = os.Remove("leases.db")
|
|
}
|
|
|
|
func TestIsValidSubnetMask(t *testing.T) {
|
|
assert.True(t, isValidSubnetMask([]byte{255, 255, 255, 0}))
|
|
assert.True(t, isValidSubnetMask([]byte{255, 255, 254, 0}))
|
|
assert.True(t, isValidSubnetMask([]byte{255, 255, 252, 0}))
|
|
assert.False(t, isValidSubnetMask([]byte{255, 255, 253, 0}))
|
|
assert.False(t, isValidSubnetMask([]byte{255, 255, 255, 1}))
|
|
}
|
|
|
|
func TestNormalizeLeases(t *testing.T) {
|
|
dynLeases := []*Lease{}
|
|
staticLeases := []*Lease{}
|
|
|
|
lease := &Lease{}
|
|
lease.HWAddr = []byte{1, 2, 3, 4}
|
|
dynLeases = append(dynLeases, lease)
|
|
lease = new(Lease)
|
|
lease.HWAddr = []byte{1, 2, 3, 5}
|
|
dynLeases = append(dynLeases, lease)
|
|
|
|
lease = new(Lease)
|
|
lease.HWAddr = []byte{1, 2, 3, 4}
|
|
lease.IP = []byte{0, 2, 3, 4}
|
|
staticLeases = append(staticLeases, lease)
|
|
lease = new(Lease)
|
|
lease.HWAddr = []byte{2, 2, 3, 4}
|
|
staticLeases = append(staticLeases, lease)
|
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
|
|
|
assert.Len(t, leases, 3)
|
|
assert.True(t, bytes.Equal(leases[0].HWAddr, []byte{1, 2, 3, 4}))
|
|
assert.True(t, bytes.Equal(leases[0].IP, []byte{0, 2, 3, 4}))
|
|
assert.True(t, bytes.Equal(leases[1].HWAddr, []byte{2, 2, 3, 4}))
|
|
assert.True(t, bytes.Equal(leases[2].HWAddr, []byte{1, 2, 3, 5}))
|
|
}
|
|
|
|
func TestOptions(t *testing.T) {
|
|
code, val := parseOptionString(" 12 hex abcdef ")
|
|
assert.EqualValues(t, 12, code)
|
|
assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val))
|
|
|
|
code, _ = parseOptionString(" 12 hex abcdef1 ")
|
|
assert.EqualValues(t, 0, code)
|
|
|
|
code, val = parseOptionString("123 ip 1.2.3.4")
|
|
assert.EqualValues(t, 123, code)
|
|
assert.True(t, net.IP{1, 2, 3, 4}.Equal(net.IP(val)))
|
|
|
|
code, _ = parseOptionString("256 ip 1.1.1.1")
|
|
assert.EqualValues(t, 0, code)
|
|
code, _ = parseOptionString("-1 ip 1.1.1.1")
|
|
assert.EqualValues(t, 0, code)
|
|
code, _ = parseOptionString("12 ip 1.1.1.1x")
|
|
assert.EqualValues(t, 0, code)
|
|
code, _ = parseOptionString("12 x 1.1.1.1")
|
|
assert.EqualValues(t, 0, code)
|
|
}
|