AdGuardHome/internal/dhcpd/dhcpd_test.go
Eugene Burkov 6358240e9b Pull request: 2273 clean tests output
Merge in DNS/adguard-home from 2273-clean-tests-output to master

Closes #2273.

Squashed commit of the following:

commit 7571a33fc1f76300bd256578b3afa95338e399c4
Merge: f17df0f9c a19523b25
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:45:30 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:35:42 2020 +0300

    home: move build constraint on top line

commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:24:50 2020 +0300

    all: add improvements to changelog

commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56
Merge: 43d4f7acf 394fc5a9d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 15:04:38 2020 +0300

    Merge branch 'master' into 2273-clean-tests-output

commit 43d4f7acf188e810aa7277cb6479110c9842e8be
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 13:38:13 2020 +0300

    dnsfilter: remove redundant test logging

commit 7194c1413006b8f52fc454e89ab052bf52e4e517
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Nov 16 12:19:14 2020 +0300

    testutil: improve comments

commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:22:08 2020 +0300

    nclient4: fix wrong function name

commit f355749149b2a4485792ba2bdcbc0bb4b629d726
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Sat Nov 14 01:07:22 2020 +0300

    testutil: fix comments and naming

commit f8c50a260bfae08d594a7f37d603941d3680a45e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Nov 13 14:09:50 2020 +0300

    testutil: create a package and include it

commit f169cdc4084b719de65aa0cdc65200b48785322e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:15:58 2020 +0300

    agherr: discard loggers output

commit 360e736b5a2a30f2c5350448234f14b841e3ea27
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Nov 12 20:09:55 2020 +0300

    all: replace default log writer with ioutil.Discard

    Closes #2273.
2020-11-16 15:52:05 +03:00

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: "192.168.10.100",
RangeEnd: "192.168.10.200",
GatewayIP: "192.168.10.1",
SubnetMask: "255.255.255.0",
notify: testNotify,
}
s.srv4, err = v4Create(conf)
assert.True(t, err == nil)
s.srv6, err = v6Create(V6ServerConf{})
assert.True(t, err == nil)
l := Lease{}
l.IP = net.ParseIP("192.168.10.100").To4()
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.ParseIP("192.168.10.101").To4()
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.Equal(t, "192.168.10.101", ll[0].IP.String())
assert.Equal(t, int64(leaseExpireStatic), ll[0].Expiry.Unix())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String())
assert.Equal(t, "192.168.10.100", ll[1].IP.String())
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.True(t, !isValidSubnetMask([]byte{255, 255, 253, 0}))
assert.True(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.True(t, len(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.Equal(t, uint8(12), code)
assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val))
code, _ = parseOptionString(" 12 hex abcdef1 ")
assert.Equal(t, uint8(0), code)
code, val = parseOptionString("123 ip 1.2.3.4")
assert.Equal(t, uint8(123), code)
assert.Equal(t, "1.2.3.4", net.IP(string(val)).String())
code, _ = parseOptionString("256 ip 1.1.1.1")
assert.Equal(t, uint8(0), code)
code, _ = parseOptionString("-1 ip 1.1.1.1")
assert.Equal(t, uint8(0), code)
code, _ = parseOptionString("12 ip 1.1.1.1x")
assert.Equal(t, uint8(0), code)
code, _ = parseOptionString("12 x 1.1.1.1")
assert.Equal(t, uint8(0), code)
}