AdGuardHome/internal/dhcpd/dhcpd_test.go
Ainar Garipov c6888326b0 Pull request: all: update go and backend tools
Closes #2576.
Updates #2275.
Updates #2419.
Updates #2443.

Squashed commit of the following:

commit b1a4809ada298d675de12740051ba26fb9945957
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri May 21 14:01:40 2021 +0300

    all: add --local-frontend, upd docker

commit 619ee7c82f27e3405753003dbec556ffb056d025
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 20 15:02:33 2021 +0300

    bamboo-specs: bump docker version

commit 5c2b2fbce80afdcc81fd0cb83674dc3d64facbf1
Merge: 6536b32d 9c60aef6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 20 15:01:47 2021 +0300

    Merge branch 'master' into 2275-upd-go

commit 6536b32dd4580425f7dedde6765463a79b9bd699
Merge: 9bb32bc4 6f7fd33a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 20:38:48 2021 +0300

    Merge branch 'master' into 2275-upd-go

commit 9bb32bc4c0ac0f3a97195adc75359e48c9c58897
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 18:48:50 2021 +0300

    all: fix build, imp err handling

commit 6868eac7f7d2980fb706881f53e72afe5f7c3447
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 18:09:32 2021 +0300

    all: fix github lint

commit ebbb9c55f32fbd57e34e8b161016aa6b291c097c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 17:36:56 2021 +0300

    all: update go and backend tools
2021-05-21 14:55:42 +03:00

134 lines
2.9 KiB
Go

// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
package dhcpd
import (
"net"
"os"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m)
}
func testNotify(flags uint32) {
}
// Leases database store/load.
func TestDB(t *testing.T) {
var err error
s := Server{
conf: ServerConfig{
DBFilePath: dbFilename,
},
}
s.srv4, err = v4Create(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,
})
require.NoError(t, err)
s.srv6, err = v6Create(V6ServerConf{})
require.NoError(t, err)
leases := []Lease{{
Expiry: time.Now().Add(time.Hour),
Hostname: "static-1.local",
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
IP: net.IP{192, 168, 10, 100},
}, {
Hostname: "static-2.local",
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
IP: net.IP{192, 168, 10, 101},
}}
srv4, ok := s.srv4.(*v4Server)
require.True(t, ok)
err = srv4.addLease(&leases[0])
require.NoError(t, err)
err = s.srv4.AddStaticLease(leases[1])
require.NoError(t, err)
s.dbStore()
t.Cleanup(func() {
assert.NoError(t, os.Remove(dbFilename))
})
s.srv4.ResetLeases(nil)
s.dbLoad()
ll := s.srv4.GetLeases(LeasesAll)
require.Len(t, ll, len(leases))
assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
assert.Equal(t, leases[1].IP, ll[0].IP)
assert.True(t, ll[0].IsStatic())
assert.Equal(t, leases[0].HWAddr, ll[1].HWAddr)
assert.Equal(t, leases[0].IP, ll[1].IP)
assert.Equal(t, leases[0].Expiry.Unix(), ll[1].Expiry.Unix())
}
func TestIsValidSubnetMask(t *testing.T) {
testCases := []struct {
mask net.IP
want bool
}{{
mask: net.IP{255, 255, 255, 0},
want: true,
}, {
mask: net.IP{255, 255, 254, 0},
want: true,
}, {
mask: net.IP{255, 255, 252, 0},
want: true,
}, {
mask: net.IP{255, 255, 253, 0},
}, {
mask: net.IP{255, 255, 255, 1},
}}
for _, tc := range testCases {
t.Run(tc.mask.String(), func(t *testing.T) {
assert.Equal(t, tc.want, isValidSubnetMask(tc.mask))
})
}
}
func TestNormalizeLeases(t *testing.T) {
dynLeases := []*Lease{{
HWAddr: net.HardwareAddr{1, 2, 3, 4},
}, {
HWAddr: net.HardwareAddr{1, 2, 3, 5},
}}
staticLeases := []*Lease{{
HWAddr: net.HardwareAddr{1, 2, 3, 4},
IP: net.IP{0, 2, 3, 4},
}, {
HWAddr: net.HardwareAddr{2, 2, 3, 4},
}}
leases := normalizeLeases(staticLeases, dynLeases)
require.Len(t, leases, 3)
assert.Equal(t, leases[0].HWAddr, dynLeases[0].HWAddr)
assert.Equal(t, leases[0].IP, staticLeases[0].IP)
assert.Equal(t, leases[1].HWAddr, staticLeases[1].HWAddr)
assert.Equal(t, leases[2].HWAddr, dynLeases[1].HWAddr)
}