2020-07-03 08:20:01 -07:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
2019-03-01 10:03:22 -07:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
2019-03-05 07:15:38 -07:00
|
|
|
"os"
|
2019-03-01 10:03:22 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-16 05:52:05 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
2019-12-03 09:24:30 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-03-01 10:03:22 -07:00
|
|
|
)
|
|
|
|
|
2020-11-16 05:52:05 -07:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testutil.DiscardLogOutput(m)
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
func testNotify(flags uint32) {
|
2019-03-01 10:03:22 -07:00
|
|
|
}
|
2019-03-05 07:15:38 -07:00
|
|
|
|
|
|
|
// Leases database store/load
|
|
|
|
func TestDB(t *testing.T) {
|
2020-07-03 08:20:01 -07:00
|
|
|
var err error
|
|
|
|
s := Server{}
|
2019-08-19 04:27:02 -07:00
|
|
|
s.conf.DBFilePath = dbFilename
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
conf := V4ServerConf{
|
|
|
|
Enabled: true,
|
2021-01-13 06:56:05 -07:00
|
|
|
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},
|
2020-07-03 08:20:01 -07:00
|
|
|
notify: testNotify,
|
2019-03-05 07:15:38 -07:00
|
|
|
}
|
2020-07-03 08:20:01 -07:00
|
|
|
s.srv4, err = v4Create(conf)
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.Nil(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.Nil(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
l := Lease{}
|
2021-01-13 06:56:05 -07:00
|
|
|
l.IP = net.IP{192, 168, 10, 100}
|
2020-07-03 08:20:01 -07:00
|
|
|
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
|
|
|
exp1 := time.Now().Add(time.Hour)
|
|
|
|
l.Expiry = exp1
|
2021-02-04 05:12:34 -07:00
|
|
|
|
|
|
|
srv4, ok := s.srv4.(*v4Server)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
srv4.addLease(&l)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
l2 := Lease{}
|
2021-01-13 06:56:05 -07:00
|
|
|
l2.IP = net.IP{192, 168, 10, 101}
|
2020-07-03 08:20:01 -07:00
|
|
|
l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb")
|
2021-02-04 05:12:34 -07:00
|
|
|
err = s.srv4.AddStaticLease(l2)
|
|
|
|
assert.Nil(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2019-12-23 06:59:02 -07:00
|
|
|
_ = os.Remove("leases.db")
|
2019-03-05 07:15:38 -07:00
|
|
|
s.dbStore()
|
2020-07-03 08:20:01 -07:00
|
|
|
s.srv4.ResetLeases(nil)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
|
|
|
s.dbLoad()
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
|
|
|
|
|
|
|
assert.Equal(t, "aa:aa:aa:aa:aa:bb", ll[0].HWAddr.String())
|
2021-01-20 07:27:53 -07:00
|
|
|
assert.True(t, net.IP{192, 168, 10, 101}.Equal(ll[0].IP))
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix())
|
2020-07-03 08:20:01 -07:00
|
|
|
|
|
|
|
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String())
|
2021-01-20 07:27:53 -07:00
|
|
|
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ll[1].IP))
|
2020-07-03 08:20:01 -07:00
|
|
|
assert.Equal(t, exp1.Unix(), ll[1].Expiry.Unix())
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2019-12-23 06:59:02 -07:00
|
|
|
_ = os.Remove("leases.db")
|
2019-03-05 07:15:38 -07:00
|
|
|
}
|
2019-07-17 01:55:21 -07:00
|
|
|
|
|
|
|
func TestIsValidSubnetMask(t *testing.T) {
|
2020-07-03 08:20:01 -07:00
|
|
|
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}))
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.False(t, isValidSubnetMask([]byte{255, 255, 253, 0}))
|
|
|
|
assert.False(t, isValidSubnetMask([]byte{255, 255, 255, 1}))
|
2019-07-17 01:55:21 -07:00
|
|
|
}
|
2019-12-03 09:24:30 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-10-30 09:18:51 -07:00
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
2019-12-03 09:24:30 -07:00
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.Len(t, leases, 3)
|
2019-12-03 09:24:30 -07:00
|
|
|
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}))
|
|
|
|
}
|
2020-08-25 04:07:11 -07:00
|
|
|
|
|
|
|
func TestOptions(t *testing.T) {
|
|
|
|
code, val := parseOptionString(" 12 hex abcdef ")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 12, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val))
|
|
|
|
|
|
|
|
code, _ = parseOptionString(" 12 hex abcdef1 ")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 0, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
|
|
|
|
code, val = parseOptionString("123 ip 1.2.3.4")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 123, code)
|
2021-01-20 07:27:53 -07:00
|
|
|
assert.True(t, net.IP{1, 2, 3, 4}.Equal(net.IP(val)))
|
2020-08-25 04:07:11 -07:00
|
|
|
|
|
|
|
code, _ = parseOptionString("256 ip 1.1.1.1")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 0, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
code, _ = parseOptionString("-1 ip 1.1.1.1")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 0, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
code, _ = parseOptionString("12 ip 1.1.1.1x")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 0, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
code, _ = parseOptionString("12 x 1.1.1.1")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.EqualValues(t, 0, code)
|
2020-08-25 04:07:11 -07:00
|
|
|
}
|