d7aafa7dc6
Merge in DNS/adguard-home from 3529-validate-dhcpv4 to master Squashed commit of the following: commit 2f2455aa13a41398cd2846f31be96da9d34ba95d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Tue Oct 19 19:18:12 2021 +0300 dhcpv4: better test && fix changelog commit ec4ff9180e8390fb739b3be0fc76fd2c715fe691 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 19:08:44 2021 +0300 dhcpv4: better tests commit e0e2f27b7a063ed84af170b16c3f87636cb738d2 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:55:47 2021 +0300 dhcpv4: better tests commit 73e1d08e1265e336ee6339d5021f90883fe3e395 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:21 2021 +0300 dhcpv4: better tests commit f636fc316123f26b6e2930afb4b22c18024ec93d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:07 2021 +0300 all: updated golibs commit 86dd107a1d483ac24bd8c26422324eb8b9c3d086 Merge: 51aaf6d9b296fa22
Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:17 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 51aaf6d9eb5fbe2b4304254dc6782305a19c53fa Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:02 2021 +0300 dhcpv4: better changelog commit 720b896bb595c57fab6d376f88c8a4b1d131db40 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:14:25 2021 +0300 dhcpv4: better tests commit 1098beffca8d5feb2ec104d26419210962c9a97d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:08:26 2021 +0300 dhcp: changelog commit d1f6c89d68657431fb261658133c67e9e3135c1c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:03:06 2021 +0300 dhcpv4: fixed tests commit 8b6713468fc04321c5238300df90bbb2d67ee679 Merge: 9991e9cb3fa38fb4
Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:57:57 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 9991e9cbee7dc87d8fa1d7e86e6cc7e09ab6938c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:55:40 2021 +0300 dhcpv4: added tests commit 5798a80de6c060365c1c647326d46cc13ccf28cb Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:46:03 2021 +0300 dhcpv4: validate subnet mask and ip range
193 lines
4.5 KiB
Go
193 lines
4.5 KiB
Go
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"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)
|
|
|
|
err = s.dbStore()
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
assert.NoError(t, os.Remove(dbFilename))
|
|
})
|
|
|
|
err = s.srv4.ResetLeases(nil)
|
|
require.NoError(t, err)
|
|
|
|
err = s.dbLoad()
|
|
require.NoError(t, err)
|
|
|
|
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)
|
|
}
|
|
|
|
func TestV4Server_badRange(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
gatewayIP net.IP
|
|
subnetMask net.IP
|
|
wantErrMsg string
|
|
}{{
|
|
name: "gateway_in_range",
|
|
gatewayIP: net.IP{192, 168, 10, 120},
|
|
subnetMask: net.IP{255, 255, 255, 0},
|
|
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
|
|
"192.168.10.20-192.168.10.200",
|
|
}, {
|
|
name: "outside_range_start",
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
|
subnetMask: net.IP{255, 255, 255, 240},
|
|
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
|
|
"192.168.10.1/28",
|
|
}, {
|
|
name: "outside_range_end",
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
|
subnetMask: net.IP{255, 255, 255, 224},
|
|
wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " +
|
|
"192.168.10.1/27",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
conf := V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 20},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: tc.gatewayIP,
|
|
SubnetMask: tc.subnetMask,
|
|
notify: testNotify,
|
|
}
|
|
|
|
_, err := v4Create(conf)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
// cloneUDPAddr returns a deep copy of a.
|
|
func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) {
|
|
return &net.UDPAddr{
|
|
IP: netutil.CloneIP(a.IP),
|
|
Port: a.Port,
|
|
Zone: a.Zone,
|
|
}
|
|
}
|